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