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