3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
22 * Copyright (c) 2011 Whamcloud, Inc.
23 * Use is subject to license terms.
25 * Author: Niu Yawei <niu@whamcloud.com>
28 * lustre/obdclass/lprocfs_jobstats.c
32 # define EXPORT_SYMTAB
34 #define DEBUG_SUBSYSTEM S_CLASS
37 # include <liblustre.h>
40 #include <obd_class.h>
41 #include <lprocfs_status.h>
42 #include <lustre/lustre_idl.h>
47 * JobID formats & JobID environment variable names for supported
51 * JobID format: 32 bit integer.
52 * JobID env var: SLURM_JOB_ID.
54 * JobID format: Decimal integer range to 99999.
55 * JobID env var: JOB_ID.
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.
61 * JobID format: String of machine_name.cluster_id.process_id, for
62 * example: fr2n02.32.0
63 * JobID env var: LOADL_STEP_ID.
65 * JobID format: String of sequence_number[.server_name][@server].
66 * JobID env var: PBS_JOBID.
68 * JobID format: Same as PBS.
69 * JobID env var: Same as PBS.
73 cfs_hlist_node_t js_hash;
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;
82 static unsigned job_stat_hash(cfs_hash_t *hs, const void *key, unsigned mask)
84 return cfs_hash_djb2_hash(key, strlen(key), mask);
87 static void *job_stat_key(cfs_hlist_node_t *hnode)
90 job = cfs_hlist_entry(hnode, struct job_stat, js_hash);
94 static int job_stat_keycmp(const void *key, cfs_hlist_node_t *hnode)
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));
102 static void *job_stat_object(cfs_hlist_node_t *hnode)
104 return cfs_hlist_entry(hnode, struct job_stat, js_hash);
107 static void job_stat_get(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
109 struct job_stat *job;
110 job = cfs_hlist_entry(hnode, struct job_stat, js_hash);
111 cfs_atomic_inc(&job->js_refcount);
114 static void job_free(struct job_stat *job)
116 LASSERT(atomic_read(&job->js_refcount) == 0);
117 LASSERT(job->js_jobstats);
119 cfs_write_lock(&job->js_jobstats->ojs_lock);
120 cfs_list_del_init(&job->js_list);
121 cfs_write_unlock(&job->js_jobstats->ojs_lock);
123 lprocfs_free_stats(&job->js_stats);
127 static void job_putref(struct job_stat *job)
129 LASSERT(atomic_read(&job->js_refcount) > 0);
130 if (atomic_dec_and_test(&job->js_refcount))
134 static void job_stat_put_locked(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
136 struct job_stat *job;
137 job = cfs_hlist_entry(hnode, struct job_stat, js_hash);
141 static void job_stat_exit(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
143 CERROR("Should not have any items!");
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,
156 static int job_iter_callback(cfs_hash_t *hs, cfs_hash_bd_t *bd,
157 cfs_hlist_node_t *hnode, void *data)
159 time_t oldest = *((time_t *)data);
160 struct job_stat *job;
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);
169 static void lprocfs_job_cleanup(struct obd_job_stats *stats, bool force)
173 if (stats->ojs_cleanup_interval == 0)
176 now = cfs_time_current_sec();
177 if (!force && now < stats->ojs_last_cleanup +
178 stats->ojs_cleanup_interval)
181 oldest = now - stats->ojs_cleanup_interval;
182 cfs_hash_for_each_safe(stats->ojs_hash, job_iter_callback,
184 stats->ojs_last_cleanup = cfs_time_current_sec();
187 static struct job_stat *job_alloc(char *jobid, struct obd_job_stats *jobs)
189 struct job_stat *job;
191 LASSERT(jobs->ojs_cntr_num && jobs->ojs_cntr_init_fn);
197 job->js_stats = lprocfs_alloc_stats(jobs->ojs_cntr_num, 0);
198 if (job->js_stats == NULL) {
203 jobs->ojs_cntr_init_fn(job->js_stats);
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);
215 int lprocfs_job_stats_log(struct obd_device *obd, char *jobid,
216 int event, long amount)
218 struct obd_job_stats *stats = &obd->u.obt.obt_jobstats;
219 struct job_stat *job, *job2;
222 LASSERT(stats && stats->ojs_hash);
224 lprocfs_job_cleanup(stats, false);
226 if (!jobid || !strlen(jobid))
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);
235 job = cfs_hash_lookup(stats->ojs_hash, jobid);
239 job = job_alloc(jobid, stats);
243 job2 = cfs_hash_findadd_unique(stats->ojs_hash, job->js_jobid,
248 LASSERT(!cfs_list_empty(&job->js_list));
250 LASSERT(cfs_list_empty(&job->js_list));
251 cfs_write_lock(&stats->ojs_lock);
252 cfs_list_add_tail(&job->js_list, &stats->ojs_list);
253 cfs_write_unlock(&stats->ojs_lock);
257 LASSERT(stats == job->js_jobstats);
258 LASSERT(stats->ojs_cntr_num > event);
259 job->js_timestamp = cfs_time_current_sec();
260 lprocfs_counter_add(job->js_stats, event, amount);
265 EXPORT_SYMBOL(lprocfs_job_stats_log);
267 void lprocfs_job_stats_fini(struct obd_device *obd)
269 struct obd_job_stats *stats = &obd->u.obt.obt_jobstats;
272 if (stats->ojs_hash == NULL)
274 cfs_hash_for_each_safe(stats->ojs_hash, job_iter_callback, &oldest);
275 cfs_hash_putref(stats->ojs_hash);
276 stats->ojs_hash = NULL;
277 LASSERT(cfs_list_empty(&stats->ojs_list));
279 EXPORT_SYMBOL(lprocfs_job_stats_fini);
281 static void *lprocfs_jobstats_seq_start(struct seq_file *p, loff_t *pos)
283 struct obd_job_stats *stats = p->private;
285 struct job_stat *job;
287 cfs_read_lock(&stats->ojs_lock);
289 return SEQ_START_TOKEN;
291 cfs_list_for_each_entry(job, &stats->ojs_list, js_list) {
298 static void lprocfs_jobstats_seq_stop(struct seq_file *p, void *v)
300 struct obd_job_stats *stats = p->private;
302 cfs_read_unlock(&stats->ojs_lock);
305 static void *lprocfs_jobstats_seq_next(struct seq_file *p, void *v, loff_t *pos)
307 struct obd_job_stats *stats = p->private;
308 struct job_stat *job;
312 if (v == SEQ_START_TOKEN) {
313 next = stats->ojs_list.next;
315 job = (struct job_stat *)v;
316 next = job->js_list.next;
319 return next == &stats->ojs_list ? NULL :
320 cfs_list_entry(next, struct job_stat, js_list);
324 * Example of output on MDT:
327 * - job_id: test_id.222.25844
328 * snapshot_time: 1322494486
329 * open: { samples: 3, unit: reqs }
330 * close: { samples: 3, unit: reqs }
331 * mknod: { samples: 0, unit: reqs }
332 * link: { samples: 0, unit: reqs }
333 * unlink: { samples: 0, unit: reqs }
334 * mkdir: { samples: 0, unit: reqs }
335 * rmdir: { samples: 0, unit: reqs }
336 * rename: { samples: 1, unit: reqs }
337 * getattr: { samples: 7, unit: reqs }
338 * setattr: { samples: 0, unit: reqs }
339 * getxattr: { samples: 0, unit: reqs }
340 * setxattr: { samples: 0, unit: reqs }
341 * statfs: { samples: 0, unit: reqs }
342 * sync: { samples: 0, unit: reqs }
344 * Example of output on OST:
348 * snapshot_time: 1322494602
349 * read: { samples: 0, unit: bytes, min: 0, max: 0, sum: 0 }
350 * write: { samples: 1, unit: bytes, min: 10, max: 10, sum: 10 }
351 * setattr: { samples: 0, unit: reqs }
352 * punch: { samples: 0, unit: reqs }
353 * sync: { samples: 0, unit: reqs }
356 static const char spaces[] = " ";
358 static int inline width(const char *str, int len)
360 return len - min((int)strlen(str), 15);
363 static int lprocfs_jobstats_seq_show(struct seq_file *p, void *v)
365 struct job_stat *job = v;
366 struct lprocfs_stats *s;
367 struct lprocfs_counter ret, *cntr;
370 if (v == SEQ_START_TOKEN) {
371 seq_printf(p, "job_stats:\n");
375 seq_printf(p, "- %-16s %s\n", "job_id:", job->js_jobid);
376 seq_printf(p, " %-16s %ld\n", "snapshot_time:", job->js_timestamp);
379 for (i = 0; i < s->ls_num; i++) {
380 cntr = &(s->ls_percpu[0]->lp_cntr[i]);
381 lprocfs_stats_collect(s, i, &ret);
383 seq_printf(p, " %s:%.*s { samples: %11"LPF64"u",
384 cntr->lc_name, width(cntr->lc_name, 15), spaces,
386 if (cntr->lc_units[0] != '\0')
387 seq_printf(p, ", unit: %5s", cntr->lc_units);
389 if (cntr->lc_config & LPROCFS_CNTR_AVGMINMAX) {
390 seq_printf(p, ", min:%8"LPF64"u, max:%8"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);
396 if (cntr->lc_config & LPROCFS_CNTR_STDDEV) {
397 seq_printf(p, ", sumsq: %18"LPF64"u",
398 ret.lc_count ? ret.lc_sumsquare : 0);
401 seq_printf(p, " }\n");
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,
414 static int lprocfs_jobstats_seq_open(struct inode *inode, struct file *file)
416 struct proc_dir_entry *dp = PDE(inode);
417 struct seq_file *seq;
420 if (LPROCFS_ENTRY_AND_CHECK(dp))
423 rc = seq_open(file, &lprocfs_jobstats_seq_sops);
428 seq = file->private_data;
429 seq->private = dp->data;
433 static ssize_t lprocfs_jobstats_seq_write(struct file *file, const char *buf,
434 size_t len, loff_t *off)
436 struct seq_file *seq = file->private_data;
437 struct obd_job_stats *stats = seq->private;
438 char jobid[JOBSTATS_JOBID_SIZE];
440 struct job_stat *job;
442 if (!memcmp(buf, "clear", strlen("clear"))) {
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);
450 memcpy(jobid, buf, len);
455 LASSERT(stats->ojs_hash);
458 cfs_hash_for_each_safe(stats->ojs_hash, job_iter_callback,
466 job = cfs_hash_lookup(stats->ojs_hash, jobid);
470 cfs_hash_del_key(stats->ojs_hash, jobid);
476 struct file_operations lprocfs_jobstats_seq_fops = {
477 .owner = THIS_MODULE,
478 .open = lprocfs_jobstats_seq_open,
480 .write = lprocfs_jobstats_seq_write,
482 .release = lprocfs_seq_release,
485 int lprocfs_job_stats_init(struct obd_device *obd, int cntr_num,
486 cntr_init_callback init_fn)
488 struct proc_dir_entry *entry;
489 struct obd_job_stats *stats;
492 LASSERT(obd->obd_proc_entry != NULL);
493 LASSERT(obd->obd_type->typ_name);
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");
500 stats = &obd->u.obt.obt_jobstats;
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,
511 if (stats->ojs_hash == NULL)
514 CFS_INIT_LIST_HEAD(&stats->ojs_list);
515 cfs_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();
521 LPROCFS_WRITE_ENTRY();
522 entry = create_proc_entry("job_stats", 0644, obd->obd_proc_entry);
523 LPROCFS_WRITE_EXIT();
525 entry->proc_fops = &lprocfs_jobstats_seq_fops;
529 lprocfs_job_stats_fini(obd);
533 EXPORT_SYMBOL(lprocfs_job_stats_init);
535 int lprocfs_rd_job_interval(char *page, char **start, off_t off,
536 int count, int *eof, void *data)
538 struct obd_device *obd = (struct obd_device *)data;
539 struct obd_job_stats *stats;
541 LASSERT(obd != NULL);
542 stats = &obd->u.obt.obt_jobstats;
544 return snprintf(page, count, "%d\n", stats->ojs_cleanup_interval);
546 EXPORT_SYMBOL(lprocfs_rd_job_interval);
548 int lprocfs_wr_job_interval(struct file *file, const char *buffer,
549 unsigned long count, void *data)
551 struct obd_device *obd = (struct obd_device *)data;
552 struct obd_job_stats *stats;
555 LASSERT(obd != NULL);
556 stats = &obd->u.obt.obt_jobstats;
558 rc = lprocfs_write_helper(buffer, count, &val);
562 stats->ojs_cleanup_interval = val;
563 lprocfs_job_cleanup(stats, true);
568 EXPORT_SYMBOL(lprocfs_wr_job_interval);