Whamcloud - gitweb
LU-16555 obdclass: quote jobid with ':' char
[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.gnu.org/licenses/gpl-2.0.html
18  *
19  * GPL HEADER END
20  */
21 /*
22  * Copyright (c) 2012, 2016, 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 #include <obd_class.h>
34 #include <lprocfs_status.h>
35
36 #ifdef CONFIG_PROC_FS
37
38 /*
39  * JobID formats & JobID environment variable names for supported
40  * job schedulers:
41  *
42  * SLURM:
43  *   JobID format:  32 bit integer.
44  *   JobID env var: SLURM_JOB_ID.
45  * SGE:
46  *   JobID format:  Decimal integer range to 99999.
47  *   JobID env var: JOB_ID.
48  * LSF:
49  *   JobID format:  6 digit integer by default (up to 999999), can be
50  *                increased to 10 digit (up to 2147483646).
51  *   JobID env var: LSB_JOBID.
52  * Loadleveler:
53  *   JobID format:  String of machine_name.cluster_id.process_id, for
54  *                example: fr2n02.32.0
55  *   JobID env var: LOADL_STEP_ID.
56  * PBS:
57  *   JobID format:  String of sequence_number[.server_name][@server].
58  *   JobID env var: PBS_JOBID.
59  * Maui/MOAB:
60  *   JobID format:  Same as PBS.
61  *   JobID env var: Same as PBS.
62  */
63
64 struct job_stat {
65         struct hlist_node       js_hash;        /* hash struct for this jobid */
66         struct list_head        js_list;        /* on ojs_list, with ojs_lock */
67         atomic_t                js_refcount;    /* num users of this struct */
68         char                    js_jobid[LUSTRE_JOBID_SIZE]; /* job name + NUL*/
69         ktime_t                 js_time_init;   /* time of initial stat*/
70         ktime_t                 js_time_latest; /* time of most recent stat*/
71         struct lprocfs_stats    *js_stats;      /* per-job statistics */
72         struct obd_job_stats    *js_jobstats;   /* for accessing ojs_lock */
73 };
74
75 static unsigned
76 job_stat_hash(struct cfs_hash *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(struct cfs_hash *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 != NULL);
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_stats_free(&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(struct cfs_hash *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(struct cfs_hash *hs, struct hlist_node *hnode)
136 {
137         CERROR("should not have any items\n");
138 }
139
140 static struct cfs_hash_ops 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 /**
151  * Jobstats expiry iterator to clean up old jobids
152  *
153  * Called for each job_stat structure on this device, it should delete stats
154  * older than the specified \a oldest_time in seconds.  If \a oldest_time is
155  * in the future then this will delete all statistics (e.g. during shutdown).
156  *
157  * \param[in] hs        hash of all jobids on this device
158  * \param[in] bd        hash bucket containing this jobid
159  * \param[in] hnode     hash structure for this jobid
160  * \param[in] data      pointer to stats expiry time in seconds
161  */
162 static int job_cleanup_iter_callback(struct cfs_hash *hs,
163                                      struct cfs_hash_bd *bd,
164                                      struct hlist_node *hnode, void *data)
165 {
166         ktime_t oldest_time = *((ktime_t *)data);
167         struct job_stat *job;
168
169         job = hlist_entry(hnode, struct job_stat, js_hash);
170         if (ktime_before(job->js_time_latest, oldest_time))
171                 cfs_hash_bd_del_locked(hs, bd, hnode);
172
173         return 0;
174 }
175
176 /**
177  * Clean up jobstats that were updated more than \a before seconds ago.
178  *
179  * Since this function may be called frequently, do not scan all of the
180  * jobstats on each call, only twice per cleanup interval.  That means stats
181  * may be on average around cleanup_interval / 4 older than the cleanup
182  * interval, but that is not considered harmful.
183  *
184  * The value stored in ojs_cleanup_interval is how often to perform a cleanup
185  * scan, and 1/2 of the maximum age of the individual statistics.  This is
186  * done rather than dividing the interval by two each time, because it is
187  * much easier to do the division when the value is initially set (in seconds)
188  * rather than after it has been converted to ktime_t, and maybe a bit faster.
189  *
190  * If \a clear is true then this will force clean up all jobstats
191  * (e.g. at shutdown).
192  *
193  * If there is already another thread doing jobstats cleanup, don't try to
194  * do this again in the current thread unless this is a force cleanup.
195  *
196  * \param[in] stats     stucture tracking all job stats for this device
197  * \param[in] clear     clear all job stats if true
198  */
199 static void lprocfs_job_cleanup(struct obd_job_stats *stats, bool clear)
200 {
201         ktime_t cleanup_interval = stats->ojs_cleanup_interval;
202         ktime_t now = ktime_get_real();
203         ktime_t oldest;
204
205         if (likely(!clear)) {
206                 /* ojs_cleanup_interval of zero means never clean up stats */
207                 if (ktime_to_ns(cleanup_interval) == 0)
208                         return;
209
210                 if (ktime_before(now, ktime_add(stats->ojs_cleanup_last,
211                                                 cleanup_interval)))
212                         return;
213
214                 if (stats->ojs_cleaning)
215                         return;
216         }
217
218         write_lock(&stats->ojs_lock);
219         if (!clear && stats->ojs_cleaning) {
220                 write_unlock(&stats->ojs_lock);
221                 return;
222         }
223
224         stats->ojs_cleaning = true;
225         write_unlock(&stats->ojs_lock);
226
227         /* Can't hold ojs_lock over hash iteration, since it is grabbed by
228          * job_cleanup_iter_callback()
229          *   ->cfs_hash_bd_del_locked()
230          *     ->job_putref()
231          *       ->job_free()
232          *
233          * Holding ojs_lock isn't necessary for safety of the hash iteration,
234          * since locking of the hash is handled internally, but there isn't
235          * any benefit to having multiple threads doing cleanup at one time.
236          *
237          * Subtract twice the cleanup_interval, since it is 1/2 the maximum age.
238          */
239         oldest = ktime_sub(now, ktime_add(cleanup_interval, cleanup_interval));
240         cfs_hash_for_each_safe(stats->ojs_hash, job_cleanup_iter_callback,
241                                &oldest);
242
243         write_lock(&stats->ojs_lock);
244         stats->ojs_cleaning = false;
245         stats->ojs_cleanup_last = ktime_get_real();
246         write_unlock(&stats->ojs_lock);
247 }
248
249 static struct job_stat *job_alloc(char *jobid, struct obd_job_stats *jobs)
250 {
251         struct job_stat *job;
252
253         OBD_ALLOC_PTR(job);
254         if (job == NULL)
255                 return NULL;
256
257         job->js_stats = lprocfs_stats_alloc(jobs->ojs_cntr_num, 0);
258         if (job->js_stats == NULL) {
259                 OBD_FREE_PTR(job);
260                 return NULL;
261         }
262
263         jobs->ojs_cntr_init_fn(job->js_stats, 0, 0);
264
265         memcpy(job->js_jobid, jobid, sizeof(job->js_jobid));
266         job->js_time_latest = job->js_stats->ls_init;
267         job->js_jobstats = jobs;
268         INIT_HLIST_NODE(&job->js_hash);
269         INIT_LIST_HEAD(&job->js_list);
270         atomic_set(&job->js_refcount, 1);
271
272         return job;
273 }
274
275 int lprocfs_job_stats_log(struct obd_device *obd, char *jobid,
276                           int event, long amount)
277 {
278         struct obd_job_stats *stats = &obd2obt(obd)->obt_jobstats;
279         struct job_stat *job, *job2;
280         ENTRY;
281
282         LASSERT(stats != NULL);
283         LASSERT(stats->ojs_hash != NULL);
284
285         if (event >= stats->ojs_cntr_num)
286                 RETURN(-EINVAL);
287
288         if (jobid == NULL || strlen(jobid) == 0)
289                 RETURN(0);
290
291         /* unterminated jobid should be handled in lustre_msg_get_jobid() */
292         if (strlen(jobid) >= LUSTRE_JOBID_SIZE) {
293                 CERROR("%s: invalid jobid size %lu, expect %d\n", obd->obd_name,
294                        (unsigned long)strlen(jobid) + 1, LUSTRE_JOBID_SIZE);
295                 RETURN(-EINVAL);
296         }
297
298         job = cfs_hash_lookup(stats->ojs_hash, jobid);
299         if (job)
300                 goto found;
301
302         lprocfs_job_cleanup(stats, false);
303
304         job = job_alloc(jobid, stats);
305         if (job == NULL)
306                 RETURN(-ENOMEM);
307
308         job2 = cfs_hash_findadd_unique(stats->ojs_hash, job->js_jobid,
309                                        &job->js_hash);
310         if (job2 != job) {
311                 job_putref(job);
312                 job = job2;
313                 /* We cannot LASSERT(!list_empty(&job->js_list)) here,
314                  * since we just lost the race for inserting "job" into the
315                  * ojs_list, and some other thread is doing it _right_now_.
316                  * Instead, be content the other thread is doing this, since
317                  * "job2" was initialized in job_alloc() already. LU-2163 */
318         } else {
319                 LASSERT(list_empty(&job->js_list));
320                 write_lock(&stats->ojs_lock);
321                 list_add_tail(&job->js_list, &stats->ojs_list);
322                 write_unlock(&stats->ojs_lock);
323         }
324
325 found:
326         LASSERT(stats == job->js_jobstats);
327         job->js_time_latest = ktime_get_real();
328         lprocfs_counter_add(job->js_stats, event, amount);
329
330         job_putref(job);
331
332         RETURN(0);
333 }
334 EXPORT_SYMBOL(lprocfs_job_stats_log);
335
336 void lprocfs_job_stats_fini(struct obd_device *obd)
337 {
338         struct obd_job_stats *stats = &obd2obt(obd)->obt_jobstats;
339
340         if (stats->ojs_hash == NULL)
341                 return;
342
343         lprocfs_job_cleanup(stats, true);
344         cfs_hash_putref(stats->ojs_hash);
345         stats->ojs_hash = NULL;
346         LASSERT(list_empty(&stats->ojs_list));
347 }
348 EXPORT_SYMBOL(lprocfs_job_stats_fini);
349
350 static void *lprocfs_jobstats_seq_start(struct seq_file *p, loff_t *pos)
351 {
352         struct obd_job_stats *stats = p->private;
353         loff_t off = *pos;
354         struct job_stat *job;
355
356         read_lock(&stats->ojs_lock);
357         if (off == 0)
358                 return SEQ_START_TOKEN;
359         off--;
360         list_for_each_entry(job, &stats->ojs_list, js_list) {
361                 if (!off--)
362                         return job;
363         }
364         return NULL;
365 }
366
367 static void lprocfs_jobstats_seq_stop(struct seq_file *p, void *v)
368 {
369         struct obd_job_stats *stats = p->private;
370
371         read_unlock(&stats->ojs_lock);
372 }
373
374 static void *lprocfs_jobstats_seq_next(struct seq_file *p, void *v, loff_t *pos)
375 {
376         struct obd_job_stats *stats = p->private;
377         struct job_stat *job;
378         struct list_head *next;
379
380         ++*pos;
381         if (v == SEQ_START_TOKEN) {
382                 next = stats->ojs_list.next;
383         } else {
384                 job = (struct job_stat *)v;
385                 next = job->js_list.next;
386         }
387
388         return next == &stats->ojs_list ? NULL :
389                 list_entry(next, struct job_stat, js_list);
390 }
391
392 /*
393  * Example of output on MDT:
394  *
395  * job_stats:
396  * - job_id:        dd.4854
397  *   snapshot_time: 1322494486.123456789
398  *   start_time:    1322494476.012345678
399  *   elapsed_time:  10.111111111
400  *   open:          { samples:         1, unit: reqs }
401  *   close:         { samples:         1, unit: reqs }
402  *   mknod:         { samples:         0, unit: reqs }
403  *   link:          { samples:         0, unit: reqs }
404  *   unlink:        { samples:         0, unit: reqs }
405  *   mkdir:         { samples:         0, unit: reqs }
406  *   rmdir:         { samples:         0, unit: reqs }
407  *   rename:        { samples:         0, unit: reqs }
408  *   getattr:       { samples:         1, unit: reqs }
409  *   setattr:       { samples:         0, unit: reqs }
410  *   getxattr:      { samples:         0, unit: reqs }
411  *   setxattr:      { samples:         0, unit: reqs }
412  *   statfs:        { samples:         0, unit: reqs }
413  *   sync:          { samples:         0, unit: reqs }
414  *
415  * Example of output on OST:
416  *
417  * job_stats:
418  * - job_id         dd.4854
419  *   snapshot_time: 1322494602.123456789
420  *   start_time:    1322494592.987654321
421  *   elapsed_time:  9.135802468
422  *   read:          { samples: 0, unit: bytes, min:  0, max:  0, sum:  0 }
423  *   write:         { samples: 1, unit: bytes, min: 4096, max: 4096, sum: 4096 }
424  *   setattr:       { samples: 0, unit: reqs }
425  *   punch:         { samples: 0, unit: reqs }
426  *   sync:          { samples: 0, unit: reqs }
427  */
428
429 static const char spaces[] = "                    ";
430
431 static int inline width(const char *str, int len)
432 {
433         return len - min((int)strlen(str), 15);
434 }
435
436 static int lprocfs_jobstats_seq_show(struct seq_file *p, void *v)
437 {
438         struct job_stat *job = v;
439         struct lprocfs_stats *s;
440         struct lprocfs_counter ret;
441         struct lprocfs_counter_header *cntr_header;
442         char escaped[LUSTRE_JOBID_SIZE * 4] = "";
443         char *quote = "", *c, *end;
444         int i, joblen = 0;
445
446         if (v == SEQ_START_TOKEN) {
447                 seq_puts(p, "job_stats:\n");
448                 return 0;
449         }
450
451         /* Quote and escape jobid characters to escape hex codes "\xHH" if
452          * it contains any non-standard characters (space, newline, etc),
453          * so it will be confined to single line and not break parsing.
454          */
455         for (c = job->js_jobid, end = job->js_jobid + sizeof(job->js_jobid);
456              c < end && *c != '\0';
457              c++, joblen++) {
458                 if (!isalnum(*c) && strchr(".@-_:/", *c) == NULL) {
459                         quote = "\"";
460                         snprintf(escaped + joblen, sizeof(escaped), "\\x%02X",
461                                  (unsigned char)*c);
462                         joblen += 3;
463                 } else {
464                         escaped[joblen] = *c;
465                         /* if jobid has ':', it should be quoted too */
466                         if (*c == ':')
467                                 quote = "\"";
468                 }
469         }
470
471         seq_printf(p, "- %-16s %s%*s%s\n",
472                    "job_id:", quote, joblen, escaped, quote);
473         lprocfs_stats_header(p, job->js_time_latest, job->js_stats->ls_init,
474                              16, ":", true, "  ");
475
476         s = job->js_stats;
477         for (i = 0; i < s->ls_num; i++) {
478                 struct obd_histogram *hist;
479
480                 cntr_header = &s->ls_cnt_header[i];
481                 lprocfs_stats_collect(s, i, &ret);
482
483                 seq_printf(p, "  %s:%.*s { samples: %11llu",
484                            cntr_header->lc_name,
485                            width(cntr_header->lc_name, 15), spaces,
486                            ret.lc_count);
487                 if (cntr_header->lc_units[0] != '\0')
488                         seq_printf(p, ", unit: %5s", cntr_header->lc_units);
489
490                 if (cntr_header->lc_config & LPROCFS_CNTR_AVGMINMAX) {
491                         seq_printf(p, ", min: %8llu, max: %8llu, sum: %16llu",
492                                    ret.lc_count ? ret.lc_min : 0,
493                                    ret.lc_count ? ret.lc_max : 0,
494                                    ret.lc_count ? ret.lc_sum : 0);
495                 }
496                 if (cntr_header->lc_config & LPROCFS_CNTR_STDDEV) {
497                         seq_printf(p, ", sumsq: %18llu",
498                                    ret.lc_count ? ret.lc_sumsquare : 0);
499                 }
500
501                 /* show obd_histogram */
502                 hist = s->ls_cnt_header[i].lc_hist;
503                 if (hist != NULL) {
504                         bool first = true;
505                         int j;
506
507                         seq_puts(p, ", hist: { ");
508                         for (j = 0; j < ARRAY_SIZE(hist->oh_buckets); j++) {
509                                 unsigned long val = hist->oh_buckets[j];
510
511                                 if (val == 0)
512                                         continue;
513                                 if (first)
514                                         first = false;
515                                 else
516                                         seq_puts(p, ", ");
517
518                                 if (j < 10)
519                                         seq_printf(p, "%lu: %lu", BIT(j), val);
520                                 else if (j < 20)
521                                         seq_printf(p, "%luK: %lu", BIT(j - 10),
522                                                    val);
523                                 else if (j < 30)
524                                         seq_printf(p, "%luM: %lu", BIT(j - 20),
525                                                    val);
526                                 else
527                                         seq_printf(p, "%luG: %lu", BIT(j - 30),
528                                                    val);
529                         }
530                         seq_puts(p, " }");
531                 }
532                 seq_puts(p, " }\n");
533         }
534
535         return 0;
536 }
537
538 static const struct seq_operations lprocfs_jobstats_seq_sops = {
539         .start  = lprocfs_jobstats_seq_start,
540         .stop   = lprocfs_jobstats_seq_stop,
541         .next   = lprocfs_jobstats_seq_next,
542         .show   = lprocfs_jobstats_seq_show,
543 };
544
545 static int lprocfs_jobstats_seq_open(struct inode *inode, struct file *file)
546 {
547         struct seq_file *seq;
548         int rc;
549
550         rc = seq_open(file, &lprocfs_jobstats_seq_sops);
551         if (rc)
552                 return rc;
553         seq = file->private_data;
554         seq->private = pde_data(inode);
555         return 0;
556 }
557
558 static ssize_t lprocfs_jobstats_seq_write(struct file *file,
559                                           const char __user *buf,
560                                           size_t len, loff_t *off)
561 {
562         struct seq_file *seq = file->private_data;
563         struct obd_job_stats *stats = seq->private;
564         char jobid[LUSTRE_JOBID_SIZE];
565         struct job_stat *job;
566
567         if (len == 0 || len >= LUSTRE_JOBID_SIZE)
568                 return -EINVAL;
569
570         if (stats->ojs_hash == NULL)
571                 return -ENODEV;
572
573         if (copy_from_user(jobid, buf, len))
574                 return -EFAULT;
575         jobid[len] = 0;
576
577         /* Trim '\n' if any */
578         if (jobid[len - 1] == '\n')
579                 jobid[len - 1] = 0;
580
581         if (strcmp(jobid, "clear") == 0) {
582                 lprocfs_job_cleanup(stats, true);
583
584                 return len;
585         }
586
587         if (strlen(jobid) == 0)
588                 return -EINVAL;
589
590         job = cfs_hash_lookup(stats->ojs_hash, jobid);
591         if (!job)
592                 return -EINVAL;
593
594         cfs_hash_del_key(stats->ojs_hash, jobid);
595
596         job_putref(job);
597         return len;
598 }
599
600 /**
601  * Clean up the seq file state when the /proc file is closed.
602  *
603  * This also expires old job stats from the cache after they have been
604  * printed in case the system is idle and not generating new jobstats.
605  *
606  * \param[in] inode     struct inode for seq file being closed
607  * \param[in] file      struct file for seq file being closed
608  *
609  * \retval              0 on success
610  * \retval              negative errno on failure
611  */
612 static int lprocfs_jobstats_seq_release(struct inode *inode, struct file *file)
613 {
614         struct seq_file *seq = file->private_data;
615         struct obd_job_stats *stats = seq->private;
616
617         lprocfs_job_cleanup(stats, false);
618
619         return lprocfs_seq_release(inode, file);
620 }
621
622 static const struct proc_ops lprocfs_jobstats_seq_fops = {
623         PROC_OWNER(THIS_MODULE)
624         .proc_open      = lprocfs_jobstats_seq_open,
625         .proc_read      = seq_read,
626         .proc_write     = lprocfs_jobstats_seq_write,
627         .proc_lseek     = seq_lseek,
628         .proc_release   = lprocfs_jobstats_seq_release,
629 };
630
631 int lprocfs_job_stats_init(struct obd_device *obd, int cntr_num,
632                            cntr_init_callback init_fn)
633 {
634         struct proc_dir_entry *entry;
635         struct obd_job_stats *stats;
636         ENTRY;
637
638         LASSERT(obd->obd_proc_entry != NULL);
639         LASSERT(obd->obd_type->typ_name);
640
641         if (cntr_num <= 0)
642                 RETURN(-EINVAL);
643
644         if (init_fn == NULL)
645                 RETURN(-EINVAL);
646
647         /* Currently needs to be a target due to the use of obt_jobstats. */
648         if (strcmp(obd->obd_type->typ_name, LUSTRE_MDT_NAME) != 0 &&
649             strcmp(obd->obd_type->typ_name, LUSTRE_OST_NAME) != 0) {
650                 CERROR("%s: invalid device type %s for job stats: rc = %d\n",
651                        obd->obd_name, obd->obd_type->typ_name, -EINVAL);
652                 RETURN(-EINVAL);
653         }
654         stats = &obd2obt(obd)->obt_jobstats;
655
656         LASSERT(stats->ojs_hash == NULL);
657         stats->ojs_hash = cfs_hash_create("JOB_STATS",
658                                           HASH_JOB_STATS_CUR_BITS,
659                                           HASH_JOB_STATS_MAX_BITS,
660                                           HASH_JOB_STATS_BKT_BITS, 0,
661                                           CFS_HASH_MIN_THETA,
662                                           CFS_HASH_MAX_THETA,
663                                           &job_stats_hash_ops,
664                                           CFS_HASH_DEFAULT);
665         if (stats->ojs_hash == NULL)
666                 RETURN(-ENOMEM);
667
668         INIT_LIST_HEAD(&stats->ojs_list);
669         rwlock_init(&stats->ojs_lock);
670         stats->ojs_cntr_num = cntr_num;
671         stats->ojs_cntr_init_fn = init_fn;
672         /* Store 1/2 the actual interval, since we use that the most, and
673          * it is easier to work with.
674          */
675         stats->ojs_cleanup_interval = ktime_set(600 / 2, 0); /* default 10 min*/
676         stats->ojs_cleanup_last = ktime_get_real();
677
678         entry = lprocfs_add_simple(obd->obd_proc_entry, "job_stats", stats,
679                                    &lprocfs_jobstats_seq_fops);
680         if (IS_ERR(entry)) {
681                 lprocfs_job_stats_fini(obd);
682                 RETURN(-ENOMEM);
683         }
684         RETURN(0);
685 }
686 EXPORT_SYMBOL(lprocfs_job_stats_init);
687 #endif /* CONFIG_PROC_FS*/
688
689 ssize_t job_cleanup_interval_show(struct kobject *kobj, struct attribute *attr,
690                                   char *buf)
691 {
692         struct obd_device *obd = container_of(kobj, struct obd_device,
693                                               obd_kset.kobj);
694         struct obd_job_stats *stats;
695         struct timespec64 ts;
696
697         stats = &obd2obt(obd)->obt_jobstats;
698         ts = ktime_to_timespec64(stats->ojs_cleanup_interval);
699
700         return scnprintf(buf, PAGE_SIZE, "%lld\n", (long long)ts.tv_sec * 2);
701 }
702 EXPORT_SYMBOL(job_cleanup_interval_show);
703
704 ssize_t job_cleanup_interval_store(struct kobject *kobj,
705                                    struct attribute *attr,
706                                    const char *buffer, size_t count)
707 {
708         struct obd_device *obd = container_of(kobj, struct obd_device,
709                                               obd_kset.kobj);
710         struct obd_job_stats *stats;
711         unsigned int val;
712         int rc;
713
714         stats = &obd2obt(obd)->obt_jobstats;
715
716         rc = kstrtouint(buffer, 0, &val);
717         if (rc)
718                 return rc;
719
720         stats->ojs_cleanup_interval = ktime_set(val / 2, 0);
721         lprocfs_job_cleanup(stats, false);
722
723         return count;
724 }
725 EXPORT_SYMBOL(job_cleanup_interval_store);