Whamcloud - gitweb
d4438418c787d55918b54faab5f96d1b44d5a2a8
[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 seq_file *seq;
420         int rc;
421
422         if (LPROCFS_ENTRY_CHECK(PDE(inode)))
423                 return -ENOENT;
424
425         rc = seq_open(file, &lprocfs_jobstats_seq_sops);
426         if (rc)
427                 return rc;
428         seq = file->private_data;
429         seq->private = PDE_DATA(inode);
430         return 0;
431 }
432
433 static ssize_t lprocfs_jobstats_seq_write(struct file *file, const char *buf,
434                                           size_t len, loff_t *off)
435 {
436         struct seq_file *seq = file->private_data;
437         struct obd_job_stats *stats = seq->private;
438         char jobid[JOBSTATS_JOBID_SIZE];
439         int all = 0;
440         struct job_stat *job;
441
442         if (!memcmp(buf, "clear", strlen("clear"))) {
443                 all = 1;
444         } else if (len < JOBSTATS_JOBID_SIZE) {
445                 memset(jobid, 0, JOBSTATS_JOBID_SIZE);
446                 /* Trim '\n' if any */
447                 if (buf[len - 1] == '\n')
448                         memcpy(jobid, buf, len - 1);
449                 else
450                         memcpy(jobid, buf, len);
451         } else {
452                 return -EINVAL;
453         }
454
455         LASSERT(stats->ojs_hash);
456         if (all) {
457                 time_t oldest = 0;
458                 cfs_hash_for_each_safe(stats->ojs_hash, job_iter_callback,
459                                        &oldest);
460                 return len;
461         }
462
463         if (!strlen(jobid))
464                 return -EINVAL;
465
466         job = cfs_hash_lookup(stats->ojs_hash, jobid);
467         if (!job)
468                 return -EINVAL;
469
470         cfs_hash_del_key(stats->ojs_hash, jobid);
471
472         job_putref(job);
473         return len;
474 }
475
476 struct file_operations lprocfs_jobstats_seq_fops = {
477         .owner   = THIS_MODULE,
478         .open    = lprocfs_jobstats_seq_open,
479         .read    = seq_read,
480         .write   = lprocfs_jobstats_seq_write,
481         .llseek  = seq_lseek,
482         .release = lprocfs_seq_release,
483 };
484
485 int lprocfs_job_stats_init(struct obd_device *obd, int cntr_num,
486                            cntr_init_callback init_fn)
487 {
488         struct proc_dir_entry *entry;
489         struct obd_job_stats *stats;
490         ENTRY;
491
492         LASSERT(obd->obd_proc_entry != NULL);
493         LASSERT(obd->obd_type->typ_name);
494
495         if (strcmp(obd->obd_type->typ_name, LUSTRE_MDT_NAME) &&
496             strcmp(obd->obd_type->typ_name, LUSTRE_OST_NAME)) {
497                 CERROR("Invalid obd device type.\n");
498                 RETURN(-EINVAL);
499         }
500         stats = &obd->u.obt.obt_jobstats;
501
502         LASSERT(stats->ojs_hash == NULL);
503         stats->ojs_hash = cfs_hash_create("JOB_STATS",
504                                           HASH_JOB_STATS_CUR_BITS,
505                                           HASH_JOB_STATS_MAX_BITS,
506                                           HASH_JOB_STATS_BKT_BITS, 0,
507                                           CFS_HASH_MIN_THETA,
508                                           CFS_HASH_MAX_THETA,
509                                           &job_stats_hash_ops,
510                                           CFS_HASH_DEFAULT);
511         if (stats->ojs_hash == NULL)
512                 RETURN(-ENOMEM);
513
514         CFS_INIT_LIST_HEAD(&stats->ojs_list);
515         rwlock_init(&stats->ojs_lock);
516         stats->ojs_cntr_num = cntr_num;
517         stats->ojs_cntr_init_fn = init_fn;
518         stats->ojs_cleanup_interval = 600; /* 10 mins by default */
519         stats->ojs_last_cleanup = cfs_time_current_sec();
520
521         LPROCFS_WRITE_ENTRY();
522         entry = proc_create_data("job_stats", 0644, obd->obd_proc_entry,
523                                 &lprocfs_jobstats_seq_fops, stats);
524         LPROCFS_WRITE_EXIT();
525         if (entry == NULL) {
526                 lprocfs_job_stats_fini(obd);
527                 RETURN(-ENOMEM);
528         }
529         RETURN(0);
530 }
531 EXPORT_SYMBOL(lprocfs_job_stats_init);
532
533 #ifndef HAVE_ONLY_PROCFS_SEQ
534 int lprocfs_rd_job_interval(char *page, char **start, off_t off,
535                             int count, int *eof, void *data)
536 {
537         struct obd_device *obd = (struct obd_device *)data;
538         struct obd_job_stats *stats;
539
540         LASSERT(obd != NULL);
541         stats = &obd->u.obt.obt_jobstats;
542         *eof = 1;
543         return snprintf(page, count, "%d\n", stats->ojs_cleanup_interval);
544 }
545 EXPORT_SYMBOL(lprocfs_rd_job_interval);
546
547 int lprocfs_wr_job_interval(struct file *file, const char *buffer,
548                             unsigned long count, void *data)
549 {
550         struct obd_device *obd = (struct obd_device *)data;
551         struct obd_job_stats *stats;
552         int val, rc;
553
554         LASSERT(obd != NULL);
555         stats = &obd->u.obt.obt_jobstats;
556
557         rc = lprocfs_write_helper(buffer, count, &val);
558         if (rc)
559                 return rc;
560
561         stats->ojs_cleanup_interval = val;
562         lprocfs_job_cleanup(stats, true);
563
564         return count;
565
566 }
567 EXPORT_SYMBOL(lprocfs_wr_job_interval);
568 #endif
569 int lprocfs_job_interval_seq_show(struct seq_file *m, void *data)
570 {
571         struct obd_device *obd = m->private;
572         struct obd_job_stats *stats;
573
574         LASSERT(obd != NULL);
575         stats = &obd->u.obt.obt_jobstats;
576         return seq_printf(m, "%d\n", stats->ojs_cleanup_interval);
577 }
578 EXPORT_SYMBOL(lprocfs_job_interval_seq_show);
579
580 ssize_t
581 lprocfs_job_interval_seq_write(struct file *file, const char *buffer,
582                                 size_t count, loff_t *off)
583 {
584         struct obd_device *obd = ((struct seq_file *)file->private_data)->private;
585         struct obd_job_stats *stats;
586         int val, rc;
587
588         LASSERT(obd != NULL);
589         stats = &obd->u.obt.obt_jobstats;
590
591         rc = lprocfs_write_helper(buffer, count, &val);
592         if (rc)
593                 return rc;
594
595         stats->ojs_cleanup_interval = val;
596         lprocfs_job_cleanup(stats, true);
597         return count;
598 }
599 EXPORT_SYMBOL(lprocfs_job_interval_seq_write);
600 #endif /* LPROCFS*/