Whamcloud - gitweb
LU-4343 tests: mkdir failing in sanity-hsm test 228
[fs/lustre-release.git] / lustre / obdclass / lprocfs_jobstats.c
1 /* GPL HEADER START
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 only,
7  * as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License version 2 for more details (a copy is included
13  * in the LICENSE file that accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License
16  * version 2 along with this program; If not, see
17  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
18  *
19  * GPL HEADER END
20  */
21 /*
22  * Copyright (c) 2012, 2013, Intel Corporation.
23  * Use is subject to license terms.
24  *
25  * Author: Niu Yawei <niu@whamcloud.com>
26  */
27 /*
28  * lustre/obdclass/lprocfs_jobstats.c
29  */
30
31 #define DEBUG_SUBSYSTEM S_CLASS
32
33 #ifndef __KERNEL__
34 # include <liblustre.h>
35 #endif
36
37 #include <obd_class.h>
38 #include <lprocfs_status.h>
39 #include <lustre/lustre_idl.h>
40
41 #if defined(LPROCFS)
42
43 /*
44  * JobID formats & JobID environment variable names for supported
45  * job schedulers:
46  *
47  * SLURM:
48  *   JobID format:  32 bit integer.
49  *   JobID env var: SLURM_JOB_ID.
50  * SGE:
51  *   JobID format:  Decimal integer range to 99999.
52  *   JobID env var: JOB_ID.
53  * LSF:
54  *   JobID format:  6 digit integer by default (up to 999999), can be
55  *                increased to 10 digit (up to 2147483646).
56  *   JobID env var: LSB_JOBID.
57  * Loadleveler:
58  *   JobID format:  String of machine_name.cluster_id.process_id, for
59  *                example: fr2n02.32.0
60  *   JobID env var: LOADL_STEP_ID.
61  * PBS:
62  *   JobID format:  String of sequence_number[.server_name][@server].
63  *   JobID env var: PBS_JOBID.
64  * Maui/MOAB:
65  *   JobID format:  Same as PBS.
66  *   JobID env var: Same as PBS.
67  */
68
69 struct job_stat {
70         cfs_hlist_node_t      js_hash;
71         cfs_list_t            js_list;
72         atomic_t          js_refcount;
73         char                  js_jobid[JOBSTATS_JOBID_SIZE];
74         time_t                js_timestamp; /* seconds */
75         struct lprocfs_stats *js_stats;
76         struct obd_job_stats *js_jobstats;
77 };
78
79 static unsigned job_stat_hash(cfs_hash_t *hs, const void *key, unsigned mask)
80 {
81         return cfs_hash_djb2_hash(key, strlen(key), mask);
82 }
83
84 static void *job_stat_key(cfs_hlist_node_t *hnode)
85 {
86         struct job_stat *job;
87         job = cfs_hlist_entry(hnode, struct job_stat, js_hash);
88         return job->js_jobid;
89 }
90
91 static int job_stat_keycmp(const void *key, cfs_hlist_node_t *hnode)
92 {
93         struct job_stat *job;
94         job = cfs_hlist_entry(hnode, struct job_stat, js_hash);
95         return (strlen(job->js_jobid) == strlen(key)) &&
96                !strncmp(job->js_jobid, key, strlen(key));
97 }
98
99 static void *job_stat_object(cfs_hlist_node_t *hnode)
100 {
101         return cfs_hlist_entry(hnode, struct job_stat, js_hash);
102 }
103
104 static void job_stat_get(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
105 {
106         struct job_stat *job;
107         job = cfs_hlist_entry(hnode, struct job_stat, js_hash);
108         atomic_inc(&job->js_refcount);
109 }
110
111 static void job_free(struct job_stat *job)
112 {
113         LASSERT(atomic_read(&job->js_refcount) == 0);
114         LASSERT(job->js_jobstats);
115
116         write_lock(&job->js_jobstats->ojs_lock);
117         cfs_list_del_init(&job->js_list);
118         write_unlock(&job->js_jobstats->ojs_lock);
119
120         lprocfs_free_stats(&job->js_stats);
121         OBD_FREE_PTR(job);
122 }
123
124 static void job_putref(struct job_stat *job)
125 {
126         LASSERT(atomic_read(&job->js_refcount) > 0);
127         if (atomic_dec_and_test(&job->js_refcount))
128                 job_free(job);
129 }
130
131 static void job_stat_put_locked(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
132 {
133         struct job_stat *job;
134         job = cfs_hlist_entry(hnode, struct job_stat, js_hash);
135         job_putref(job);
136 }
137
138 static void job_stat_exit(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
139 {
140         CERROR("Should not have any items!");
141 }
142
143 static cfs_hash_ops_t job_stats_hash_ops = {
144         .hs_hash       = job_stat_hash,
145         .hs_key        = job_stat_key,
146         .hs_keycmp     = job_stat_keycmp,
147         .hs_object     = job_stat_object,
148         .hs_get        = job_stat_get,
149         .hs_put_locked = job_stat_put_locked,
150         .hs_exit       = job_stat_exit,
151 };
152
153 static int job_iter_callback(cfs_hash_t *hs, cfs_hash_bd_t *bd,
154                              cfs_hlist_node_t *hnode, void *data)
155 {
156         time_t oldest = *((time_t *)data);
157         struct job_stat *job;
158
159         job = cfs_hlist_entry(hnode, struct job_stat, js_hash);
160         if (!oldest || job->js_timestamp < oldest)
161                 cfs_hash_bd_del_locked(hs, bd, hnode);
162
163         return 0;
164 }
165
166 static void lprocfs_job_cleanup(struct obd_job_stats *stats, bool force)
167 {
168         time_t oldest, now;
169
170         if (stats->ojs_cleanup_interval == 0)
171                 return;
172
173         now = cfs_time_current_sec();
174         if (!force && now < stats->ojs_last_cleanup +
175                             stats->ojs_cleanup_interval)
176                 return;
177
178         oldest = now - stats->ojs_cleanup_interval;
179         cfs_hash_for_each_safe(stats->ojs_hash, job_iter_callback,
180                                &oldest);
181         stats->ojs_last_cleanup = cfs_time_current_sec();
182 }
183
184 static struct job_stat *job_alloc(char *jobid, struct obd_job_stats *jobs)
185 {
186         struct job_stat *job;
187
188         LASSERT(jobs->ojs_cntr_num && jobs->ojs_cntr_init_fn);
189
190         OBD_ALLOC_PTR(job);
191         if (job == NULL)
192                 return NULL;
193
194         job->js_stats = lprocfs_alloc_stats(jobs->ojs_cntr_num, 0);
195         if (job->js_stats == NULL) {
196                 OBD_FREE_PTR(job);
197                 return NULL;
198         }
199
200         jobs->ojs_cntr_init_fn(job->js_stats);
201
202         memcpy(job->js_jobid, jobid, JOBSTATS_JOBID_SIZE);
203         job->js_timestamp = cfs_time_current_sec();
204         job->js_jobstats = jobs;
205         CFS_INIT_HLIST_NODE(&job->js_hash);
206         CFS_INIT_LIST_HEAD(&job->js_list);
207         atomic_set(&job->js_refcount, 1);
208
209         return job;
210 }
211
212 int lprocfs_job_stats_log(struct obd_device *obd, char *jobid,
213                           int event, long amount)
214 {
215         struct obd_job_stats *stats = &obd->u.obt.obt_jobstats;
216         struct job_stat *job, *job2;
217         ENTRY;
218
219         LASSERT(stats && stats->ojs_hash);
220
221         lprocfs_job_cleanup(stats, false);
222
223         if (!jobid || !strlen(jobid))
224                 RETURN(-EINVAL);
225
226         if (strlen(jobid) >= JOBSTATS_JOBID_SIZE) {
227                 CERROR("Invalid jobid size (%lu), expect(%d)\n",
228                        (unsigned long)strlen(jobid) + 1, JOBSTATS_JOBID_SIZE);
229                 RETURN(-EINVAL);
230         }
231
232         job = cfs_hash_lookup(stats->ojs_hash, jobid);
233         if (job)
234                 goto found;
235
236         job = job_alloc(jobid, stats);
237         if (job == NULL)
238                 RETURN(-ENOMEM);
239
240         job2 = cfs_hash_findadd_unique(stats->ojs_hash, job->js_jobid,
241                                        &job->js_hash);
242         if (job2 != job) {
243                 job_putref(job);
244                 job = job2;
245                 /* We cannot LASSERT(!cfs_list_empty(&job->js_list)) here,
246                  * since we just lost the race for inserting "job" into the
247                  * ojs_list, and some other thread is doing it _right_now_.
248                  * Instead, be content the other thread is doing this, since
249                  * "job2" was initialized in job_alloc() already. LU-2163 */
250         } else {
251                 LASSERT(cfs_list_empty(&job->js_list));
252                 write_lock(&stats->ojs_lock);
253                 cfs_list_add_tail(&job->js_list, &stats->ojs_list);
254                 write_unlock(&stats->ojs_lock);
255         }
256
257 found:
258         LASSERT(stats == job->js_jobstats);
259         LASSERT(stats->ojs_cntr_num > event);
260         job->js_timestamp = cfs_time_current_sec();
261         lprocfs_counter_add(job->js_stats, event, amount);
262
263         job_putref(job);
264         RETURN(0);
265 }
266 EXPORT_SYMBOL(lprocfs_job_stats_log);
267
268 void lprocfs_job_stats_fini(struct obd_device *obd)
269 {
270         struct obd_job_stats *stats = &obd->u.obt.obt_jobstats;
271         time_t oldest = 0;
272
273         if (stats->ojs_hash == NULL)
274                 return;
275         cfs_hash_for_each_safe(stats->ojs_hash, job_iter_callback, &oldest);
276         cfs_hash_putref(stats->ojs_hash);
277         stats->ojs_hash = NULL;
278         LASSERT(cfs_list_empty(&stats->ojs_list));
279 }
280 EXPORT_SYMBOL(lprocfs_job_stats_fini);
281
282 static void *lprocfs_jobstats_seq_start(struct seq_file *p, loff_t *pos)
283 {
284         struct obd_job_stats *stats = p->private;
285         loff_t off = *pos;
286         struct job_stat *job;
287
288         read_lock(&stats->ojs_lock);
289         if (off == 0)
290                 return SEQ_START_TOKEN;
291         off--;
292         cfs_list_for_each_entry(job, &stats->ojs_list, js_list) {
293                 if (!off--)
294                         return job;
295         }
296         return NULL;
297 }
298
299 static void lprocfs_jobstats_seq_stop(struct seq_file *p, void *v)
300 {
301         struct obd_job_stats *stats = p->private;
302
303         read_unlock(&stats->ojs_lock);
304 }
305
306 static void *lprocfs_jobstats_seq_next(struct seq_file *p, void *v, loff_t *pos)
307 {
308         struct obd_job_stats *stats = p->private;
309         struct job_stat *job;
310         cfs_list_t *next;
311
312         ++*pos;
313         if (v == SEQ_START_TOKEN) {
314                 next = stats->ojs_list.next;
315         } else {
316                 job = (struct job_stat *)v;
317                 next = job->js_list.next;
318         }
319
320         return next == &stats->ojs_list ? NULL :
321                 cfs_list_entry(next, struct job_stat, js_list);
322 }
323
324 /*
325  * Example of output on MDT:
326  *
327  * job_stats:
328  * - job_id:        test_id.222.25844
329  *   snapshot_time: 1322494486
330  *   open:          { samples:         3, unit: reqs }
331  *   close:         { samples:         3, unit: reqs }
332  *   mknod:         { samples:         0, unit: reqs }
333  *   link:          { samples:         0, unit: reqs }
334  *   unlink:        { samples:         0, unit: reqs }
335  *   mkdir:         { samples:         0, unit: reqs }
336  *   rmdir:         { samples:         0, unit: reqs }
337  *   rename:        { samples:         1, unit: reqs }
338  *   getattr:       { samples:         7, unit: reqs }
339  *   setattr:       { samples:         0, unit: reqs }
340  *   getxattr:      { samples:         0, unit: reqs }
341  *   setxattr:      { samples:         0, unit: reqs }
342  *   statfs:        { samples:         0, unit: reqs }
343  *   sync:          { samples:         0, unit: reqs }
344  *
345  * Example of output on OST:
346  *
347  * job_stats:
348  * - job_id         4854
349  *   snapshot_time: 1322494602
350  *   read:          { samples:  0, unit: bytes, min:  0, max:  0, sum:  0 }
351  *   write:         { samples:  1, unit: bytes, min: 10, max: 10, sum: 10 }
352  *   setattr:       { samples:  0, unit: reqs }
353  *   punch:         { samples:  0, unit: reqs }
354  *   sync:          { samples:  0, unit: reqs }
355  */
356
357 static const char spaces[] = "                    ";
358
359 static int inline width(const char *str, int len)
360 {
361         return len - min((int)strlen(str), 15);
362 }
363
364 static int lprocfs_jobstats_seq_show(struct seq_file *p, void *v)
365 {
366         struct job_stat                 *job = v;
367         struct lprocfs_stats            *s;
368         struct lprocfs_counter          ret;
369         struct lprocfs_counter_header   *cntr_header;
370         int                             i;
371
372         if (v == SEQ_START_TOKEN) {
373                 seq_printf(p, "job_stats:\n");
374                 return 0;
375         }
376
377         seq_printf(p, "- %-16s %s\n", "job_id:", job->js_jobid);
378         seq_printf(p, "  %-16s %ld\n", "snapshot_time:", job->js_timestamp);
379
380         s = job->js_stats;
381         for (i = 0; i < s->ls_num; i++) {
382                 cntr_header = &s->ls_cnt_header[i];
383                 lprocfs_stats_collect(s, i, &ret);
384
385                 seq_printf(p, "  %s:%.*s { samples: %11"LPF64"u",
386                            cntr_header->lc_name,
387                            width(cntr_header->lc_name, 15), spaces,
388                            ret.lc_count);
389                 if (cntr_header->lc_units[0] != '\0')
390                         seq_printf(p, ", unit: %5s", cntr_header->lc_units);
391
392                 if (cntr_header->lc_config & LPROCFS_CNTR_AVGMINMAX) {
393                         seq_printf(p, ", min:%8"LPF64"u, max:%8"LPF64"u,"
394                                    " sum:%16"LPF64"u",
395                                    ret.lc_count ? ret.lc_min : 0,
396                                    ret.lc_count ? ret.lc_max : 0,
397                                    ret.lc_count ? ret.lc_sum : 0);
398                 }
399                 if (cntr_header->lc_config & LPROCFS_CNTR_STDDEV) {
400                         seq_printf(p, ", sumsq: %18"LPF64"u",
401                                    ret.lc_count ? ret.lc_sumsquare : 0);
402                 }
403
404                 seq_printf(p, " }\n");
405
406         }
407         return 0;
408 }
409
410 struct seq_operations lprocfs_jobstats_seq_sops = {
411         start: lprocfs_jobstats_seq_start,
412         stop:  lprocfs_jobstats_seq_stop,
413         next:  lprocfs_jobstats_seq_next,
414         show:  lprocfs_jobstats_seq_show,
415 };
416
417 static int lprocfs_jobstats_seq_open(struct inode *inode, struct file *file)
418 {
419         struct proc_dir_entry *dp = PDE(inode);
420         struct seq_file *seq;
421         int rc;
422
423         if (LPROCFS_ENTRY_CHECK(dp))
424                 return -ENOENT;
425
426         rc = seq_open(file, &lprocfs_jobstats_seq_sops);
427         if (rc)
428                 return rc;
429         seq = file->private_data;
430         seq->private = dp->data;
431         return 0;
432 }
433
434 static ssize_t lprocfs_jobstats_seq_write(struct file *file, const char *buf,
435                                           size_t len, loff_t *off)
436 {
437         struct seq_file *seq = file->private_data;
438         struct obd_job_stats *stats = seq->private;
439         char jobid[JOBSTATS_JOBID_SIZE];
440         int all = 0;
441         struct job_stat *job;
442
443         if (!memcmp(buf, "clear", strlen("clear"))) {
444                 all = 1;
445         } else if (len < JOBSTATS_JOBID_SIZE) {
446                 memset(jobid, 0, JOBSTATS_JOBID_SIZE);
447                 /* Trim '\n' if any */
448                 if (buf[len - 1] == '\n')
449                         memcpy(jobid, buf, len - 1);
450                 else
451                         memcpy(jobid, buf, len);
452         } else {
453                 return -EINVAL;
454         }
455
456         LASSERT(stats->ojs_hash);
457         if (all) {
458                 time_t oldest = 0;
459                 cfs_hash_for_each_safe(stats->ojs_hash, job_iter_callback,
460                                        &oldest);
461                 return len;
462         }
463
464         if (!strlen(jobid))
465                 return -EINVAL;
466
467         job = cfs_hash_lookup(stats->ojs_hash, jobid);
468         if (!job)
469                 return -EINVAL;
470
471         cfs_hash_del_key(stats->ojs_hash, jobid);
472
473         job_putref(job);
474         return len;
475 }
476
477 struct file_operations lprocfs_jobstats_seq_fops = {
478         .owner   = THIS_MODULE,
479         .open    = lprocfs_jobstats_seq_open,
480         .read    = seq_read,
481         .write   = lprocfs_jobstats_seq_write,
482         .llseek  = seq_lseek,
483         .release = lprocfs_seq_release,
484 };
485
486 int lprocfs_job_stats_init(struct obd_device *obd, int cntr_num,
487                            cntr_init_callback init_fn)
488 {
489         struct proc_dir_entry *entry;
490         struct obd_job_stats *stats;
491         ENTRY;
492
493         LASSERT(obd->obd_proc_entry != NULL);
494         LASSERT(obd->obd_type->typ_name);
495
496         if (strcmp(obd->obd_type->typ_name, LUSTRE_MDT_NAME) &&
497             strcmp(obd->obd_type->typ_name, LUSTRE_OST_NAME)) {
498                 CERROR("Invalid obd device type.\n");
499                 RETURN(-EINVAL);
500         }
501         stats = &obd->u.obt.obt_jobstats;
502
503         LASSERT(stats->ojs_hash == NULL);
504         stats->ojs_hash = cfs_hash_create("JOB_STATS",
505                                           HASH_JOB_STATS_CUR_BITS,
506                                           HASH_JOB_STATS_MAX_BITS,
507                                           HASH_JOB_STATS_BKT_BITS, 0,
508                                           CFS_HASH_MIN_THETA,
509                                           CFS_HASH_MAX_THETA,
510                                           &job_stats_hash_ops,
511                                           CFS_HASH_DEFAULT);
512         if (stats->ojs_hash == NULL)
513                 RETURN(-ENOMEM);
514
515         CFS_INIT_LIST_HEAD(&stats->ojs_list);
516         rwlock_init(&stats->ojs_lock);
517         stats->ojs_cntr_num = cntr_num;
518         stats->ojs_cntr_init_fn = init_fn;
519         stats->ojs_cleanup_interval = 600; /* 10 mins by default */
520         stats->ojs_last_cleanup = cfs_time_current_sec();
521
522         LPROCFS_WRITE_ENTRY();
523         entry = create_proc_entry("job_stats", 0644, obd->obd_proc_entry);
524         LPROCFS_WRITE_EXIT();
525         if (entry) {
526                 entry->proc_fops = &lprocfs_jobstats_seq_fops;
527                 entry->data = stats;
528                 RETURN(0);
529         } else {
530                 lprocfs_job_stats_fini(obd);
531                 RETURN(-ENOMEM);
532         }
533 }
534 EXPORT_SYMBOL(lprocfs_job_stats_init);
535
536 int lprocfs_rd_job_interval(char *page, char **start, off_t off,
537                             int count, int *eof, void *data)
538 {
539         struct obd_device *obd = (struct obd_device *)data;
540         struct obd_job_stats *stats;
541
542         LASSERT(obd != NULL);
543         stats = &obd->u.obt.obt_jobstats;
544         *eof = 1;
545         return snprintf(page, count, "%d\n", stats->ojs_cleanup_interval);
546 }
547 EXPORT_SYMBOL(lprocfs_rd_job_interval);
548
549 int lprocfs_wr_job_interval(struct file *file, const char *buffer,
550                             unsigned long count, void *data)
551 {
552         struct obd_device *obd = (struct obd_device *)data;
553         struct obd_job_stats *stats;
554         int val, rc;
555
556         LASSERT(obd != NULL);
557         stats = &obd->u.obt.obt_jobstats;
558
559         rc = lprocfs_write_helper(buffer, count, &val);
560         if (rc)
561                 return rc;
562
563         stats->ojs_cleanup_interval = val;
564         lprocfs_job_cleanup(stats, true);
565
566         return count;
567
568 }
569 EXPORT_SYMBOL(lprocfs_wr_job_interval);
570
571 #endif /* LPROCFS*/