13 static struct itimerval itimer;
14 static struct list_head disk_list = LIST_HEAD_INIT(disk_list);
15 static dev_t last_dev;
18 * Cheesy number->string conversion, complete with carry rounding error.
20 static char *num2str(unsigned long num, int maxlen, int base)
23 * could be passed in for 10^3 base, but every caller expects
24 * 2^10 base right now.
26 const unsigned int thousand = 1024;
27 char postfix[] = { 'K', 'M', 'G', 'P', 'E' };
33 for (i = 0; base > 1; i++)
39 len = sprintf(buf, "%'lu", num);
41 buf[len] = postfix[i];
46 if ((num % thousand) >= (thousand / 2))
57 static int get_io_ticks(struct disk_util *du, struct disk_util_stat *dus)
64 f = fopen(du->path, "r");
68 p = fgets(line, sizeof(line), f);
74 if (sscanf(p, "%u %u %llu %u %u %u %llu %u %u %u %u\n", &dus->ios[0], &dus->merges[0], &dus->sectors[0], &dus->ticks[0], &dus->ios[1], &dus->merges[1], &dus->sectors[1], &dus->ticks[1], &in_flight, &dus->io_ticks, &dus->time_in_queue) != 11) {
83 static void update_io_tick_disk(struct disk_util *du)
85 struct disk_util_stat __dus, *dus, *ldus;
88 if (get_io_ticks(du, &__dus))
94 dus->sectors[0] += (__dus.sectors[0] - ldus->sectors[0]);
95 dus->sectors[1] += (__dus.sectors[1] - ldus->sectors[1]);
96 dus->ios[0] += (__dus.ios[0] - ldus->ios[0]);
97 dus->ios[1] += (__dus.ios[1] - ldus->ios[1]);
98 dus->merges[0] += (__dus.merges[0] - ldus->merges[0]);
99 dus->merges[1] += (__dus.merges[1] - ldus->merges[1]);
100 dus->ticks[0] += (__dus.ticks[0] - ldus->ticks[0]);
101 dus->ticks[1] += (__dus.ticks[1] - ldus->ticks[1]);
102 dus->io_ticks += (__dus.io_ticks - ldus->io_ticks);
103 dus->time_in_queue += (__dus.time_in_queue - ldus->time_in_queue);
105 fio_gettime(&t, NULL);
106 du->msec += mtime_since(&du->time, &t);
107 memcpy(&du->time, &t, sizeof(t));
108 memcpy(ldus, &__dus, sizeof(__dus));
111 void update_io_ticks(void)
113 struct list_head *entry;
114 struct disk_util *du;
116 list_for_each(entry, &disk_list) {
117 du = list_entry(entry, struct disk_util, list);
118 update_io_tick_disk(du);
122 static int disk_util_exists(dev_t dev)
124 struct list_head *entry;
125 struct disk_util *du;
127 list_for_each(entry, &disk_list) {
128 du = list_entry(entry, struct disk_util, list);
137 static void disk_util_add(dev_t dev, char *path)
139 struct disk_util *du, *__du;
140 struct list_head *entry;
142 du = malloc(sizeof(*du));
143 memset(du, 0, sizeof(*du));
144 INIT_LIST_HEAD(&du->list);
145 sprintf(du->path, "%s/stat", path);
146 du->name = strdup(basename(path));
149 list_for_each(entry, &disk_list) {
150 __du = list_entry(entry, struct disk_util, list);
152 if (!strcmp(du->name, __du->name)) {
159 fio_gettime(&du->time, NULL);
160 get_io_ticks(du, &du->last_dus);
162 list_add_tail(&du->list, &disk_list);
165 static int check_dev_match(dev_t dev, char *path)
167 unsigned int major, minor;
171 f = fopen(path, "r");
177 p = fgets(line, sizeof(line), f);
183 if (sscanf(p, "%u:%u", &major, &minor) != 2) {
188 if (((major << 8) | minor) == dev) {
197 static int find_block_dir(dev_t dev, char *path)
208 while ((dir = readdir(D)) != NULL) {
211 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
214 sprintf(full_path, "%s/%s", path, dir->d_name);
216 if (!strcmp(dir->d_name, "dev")) {
217 if (!check_dev_match(dev, full_path)) {
223 if (lstat(full_path, &st) == -1) {
228 if (!S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode))
231 found = find_block_dir(dev, full_path);
233 strcpy(path, full_path);
242 void init_disk_util(struct thread_data *td)
246 char foo[PATH_MAX], tmp[PATH_MAX];
250 if (!td->do_disk_util || (td->io_ops->flags & FIO_DISKLESSIO))
254 * Just use the same file, they are on the same device.
257 if (!stat(f->file_name, &st)) {
258 if (S_ISBLK(st.st_mode))
264 * must be a file, open "." in that path
266 strncpy(foo, f->file_name, PATH_MAX - 1);
269 perror("disk util stat");
276 if (disk_util_exists(dev))
280 * for an fs without a device, we will repeatedly stat through
281 * sysfs which can take oodles of time for thousands of files. so
282 * cache the last lookup and compare with that before going through
290 sprintf(foo, "/sys/block");
291 if (!find_block_dir(dev, foo))
295 * If there's a ../queue/ directory there, we are inside a partition.
296 * Check if that is the case and jump back. For loop/md/dm etc we
297 * are already in the right spot.
299 sprintf(tmp, "%s/../queue", foo);
300 if (!stat(tmp, &st)) {
302 sprintf(tmp, "%s/queue", p);
303 if (stat(tmp, &st)) {
304 log_err("unknown sysfs layout\n");
307 strncpy(tmp, p, PATH_MAX - 1);
308 sprintf(foo, "%s", tmp);
312 td->sysfs_root = strdup(foo);
314 disk_util_add(dev, foo);
317 void disk_util_timer_arm(void)
319 itimer.it_value.tv_sec = 0;
320 itimer.it_value.tv_usec = DISK_UTIL_MSEC * 1000;
321 setitimer(ITIMER_REAL, &itimer, NULL);
324 void update_rusage_stat(struct thread_data *td)
326 struct thread_stat *ts = &td->ts;
328 getrusage(RUSAGE_SELF, &ts->ru_end);
330 ts->usr_time += mtime_since(&ts->ru_start.ru_utime, &ts->ru_end.ru_utime);
331 ts->sys_time += mtime_since(&ts->ru_start.ru_stime, &ts->ru_end.ru_stime);
332 ts->ctx += ts->ru_end.ru_nvcsw + ts->ru_end.ru_nivcsw - (ts->ru_start.ru_nvcsw + ts->ru_start.ru_nivcsw);
334 memcpy(&ts->ru_start, &ts->ru_end, sizeof(ts->ru_end));
337 static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max,
338 double *mean, double *dev)
340 double n = is->samples;
342 if (is->samples == 0)
348 n = (double) is->samples;
352 *dev = sqrt(is->S / (n - 1.0));
359 static void show_group_stats(struct group_run_stats *rs, int id)
361 char *p1, *p2, *p3, *p4;
362 const char *ddir_str[] = { " READ", " WRITE" };
365 fprintf(f_out, "\nRun status group %d (all jobs):\n", id);
367 for (i = 0; i <= DDIR_WRITE; i++) {
371 p1 = num2str(rs->io_kb[i], 6, 1);
372 p2 = num2str(rs->agg[i], 6, 1);
373 p3 = num2str(rs->min_bw[i], 6, 1);
374 p4 = num2str(rs->max_bw[i], 6, 1);
376 fprintf(f_out, "%s: io=%siB, aggrb=%siB/s, minb=%siB/s, maxb=%siB/s, mint=%llumsec, maxt=%llumsec\n", ddir_str[i], p1, p2, p3, p4, rs->min_run[0], rs->max_run[0]);
385 static void show_disk_util(void)
387 struct disk_util_stat *dus;
388 struct list_head *entry, *next;
389 struct disk_util *du;
392 fprintf(f_out, "\nDisk stats (read/write):\n");
394 list_for_each(entry, &disk_list) {
395 du = list_entry(entry, struct disk_util, list);
398 util = (double) 100 * du->dus.io_ticks / (double) du->msec;
402 fprintf(f_out, " %s: ios=%u/%u, merge=%u/%u, ticks=%u/%u, in_queue=%u, util=%3.2f%%\n", du->name, dus->ios[0], dus->ios[1], dus->merges[0], dus->merges[1], dus->ticks[0], dus->ticks[1], dus->time_in_queue, util);
408 list_for_each_safe(entry, next, &disk_list) {
410 du = list_entry(entry, struct disk_util, list);
416 static void show_ddir_status(struct group_run_stats *rs, struct thread_stat *ts,
419 const char *ddir_str[] = { "read ", "write" };
420 unsigned long min, max;
421 unsigned long long bw;
425 if (!ts->runtime[ddir])
428 bw = ts->io_bytes[ddir] / ts->runtime[ddir];
429 io_p = num2str(ts->io_bytes[ddir] >> 10, 6, 1);
430 bw_p = num2str(bw, 6, 1);
432 fprintf(f_out, " %s: io=%siB, bw=%siB/s, runt=%6lumsec\n", ddir_str[ddir], io_p, bw_p, ts->runtime[ddir]);
437 if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev))
438 fprintf(f_out, " slat (msec): min=%5lu, max=%5lu, avg=%5.02f, stdev=%5.02f\n", min, max, mean, dev);
440 if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev))
441 fprintf(f_out, " clat (msec): min=%5lu, max=%5lu, avg=%5.02f, stdev=%5.02f\n", min, max, mean, dev);
443 if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
446 p_of_agg = mean * 100 / (double) rs->agg[ddir];
447 fprintf(f_out, " bw (KiB/s) : min=%5lu, max=%5lu, per=%3.2f%%, avg=%5.02f, stdev=%5.02f\n", min, max, p_of_agg, mean, dev);
451 static void show_thread_status(struct thread_stat *ts,
452 struct group_run_stats *rs)
454 double usr_cpu, sys_cpu;
455 unsigned long runtime;
456 double io_u_dist[FIO_IO_U_MAP_NR];
457 double io_u_lat[FIO_IO_U_LAT_NR];
460 if (!(ts->io_bytes[0] + ts->io_bytes[1]))
464 fprintf(f_out, "%s: (groupid=%d): err=%2d: pid=%d\n", ts->name, ts->groupid, ts->error, ts->pid);
466 fprintf(f_out, "%s: (groupid=%d): err=%2d (%s): pid=%d\n", ts->name, ts->groupid, ts->error, ts->verror, ts->pid);
468 if (ts->io_bytes[DDIR_READ])
469 show_ddir_status(rs, ts, DDIR_READ);
470 if (ts->io_bytes[DDIR_WRITE])
471 show_ddir_status(rs, ts, DDIR_WRITE);
473 runtime = ts->total_run_time;
475 double runt = (double) runtime;
477 usr_cpu = (double) ts->usr_time * 100 / runt;
478 sys_cpu = (double) ts->sys_time * 100 / runt;
484 fprintf(f_out, " cpu : usr=%3.2f%%, sys=%3.2f%%, ctx=%lu\n", usr_cpu, sys_cpu, ts->ctx);
487 * Do depth distribution calculations
489 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
490 io_u_dist[i] = (double) ts->io_u_map[i] / (double) ts->total_io_u;
491 io_u_dist[i] *= 100.0;
494 fprintf(f_out, " IO depths : 1=%3.1f%%, 2=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%, 16=%3.1f%%, 32=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0], io_u_dist[1], io_u_dist[2], io_u_dist[3], io_u_dist[4], io_u_dist[5], io_u_dist[6]);
497 * Do latency distribution calculations
499 for (i = 0; i < FIO_IO_U_LAT_NR; i++) {
500 io_u_lat[i] = (double) ts->io_u_lat[i] / (double) ts->total_io_u;
501 io_u_lat[i] *= 100.0;
504 fprintf(f_out, " lat (msec): 2=%3.1f%%, 4=%3.1f%%, 10=%3.1f%%, 20=%3.1f%%, 50=%3.1f%%, 100=%3.1f%%\n", io_u_lat[0], io_u_lat[1], io_u_lat[2], io_u_lat[3], io_u_lat[4], io_u_lat[5]);
505 fprintf(f_out, " lat (msec): 250=%3.1f%%, 500=%3.1f%%, 750=%3.1f%%, 1000=%3.1f%%, >=2000=%3.1f%%\n", io_u_lat[6], io_u_lat[7], io_u_lat[8], io_u_lat[9], io_u_lat[10]);
508 fprintf(f_out, "%s\n", ts->description);
511 static void show_ddir_status_terse(struct thread_stat *ts,
512 struct group_run_stats *rs, int ddir)
514 unsigned long min, max;
515 unsigned long long bw;
519 if (ts->runtime[ddir])
520 bw = ts->io_bytes[ddir] / ts->runtime[ddir];
522 fprintf(f_out, ",%llu,%llu,%lu", ts->io_bytes[ddir] >> 10, bw, ts->runtime[ddir]);
524 if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev))
525 fprintf(f_out, ",%lu,%lu,%f,%f", min, max, mean, dev);
527 fprintf(f_out, ",%lu,%lu,%f,%f", 0UL, 0UL, 0.0, 0.0);
529 if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev))
530 fprintf(f_out, ",%lu,%lu,%f,%f", min, max, mean, dev);
532 fprintf(f_out, ",%lu,%lu,%f,%f", 0UL, 0UL, 0.0, 0.0);
534 if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
537 p_of_agg = mean * 100 / (double) rs->agg[ddir];
538 fprintf(f_out, ",%lu,%lu,%f%%,%f,%f", min, max, p_of_agg, mean, dev);
540 fprintf(f_out, ",%lu,%lu,%f%%,%f,%f", 0UL, 0UL, 0.0, 0.0, 0.0);
544 static void show_thread_status_terse(struct thread_stat *ts,
545 struct group_run_stats *rs)
547 double usr_cpu, sys_cpu;
549 fprintf(f_out, "%s,%d,%d", ts->name, ts->groupid, ts->error);
551 show_ddir_status_terse(ts, rs, 0);
552 show_ddir_status_terse(ts, rs, 1);
554 if (ts->total_run_time) {
555 double runt = (double) ts->total_run_time;
557 usr_cpu = (double) ts->usr_time * 100 / runt;
558 sys_cpu = (double) ts->sys_time * 100 / runt;
564 fprintf(f_out, ",%f%%,%f%%,%lu\n", usr_cpu, sys_cpu, ts->ctx);
567 static void __sum_stat(struct io_stat *dst, struct io_stat *src, int nr)
571 dst->min_val = min(dst->min_val, src->min_val);
572 dst->max_val = max(dst->max_val, src->max_val);
573 dst->samples += src->samples;
576 * Needs a new method for calculating stddev, we cannot just
577 * average them we do below for nr > 1
583 mean = ((src->mean * (double) nr) + dst->mean) / ((double) nr + 1.0);
584 S = ((src->S * (double) nr) + dst->S) / ((double) nr + 1.0);
591 static void sum_stat(struct io_stat *dst, struct io_stat *src, int nr)
593 __sum_stat(&dst[DDIR_READ], &src[DDIR_READ], nr);
594 __sum_stat(&dst[DDIR_WRITE], &src[DDIR_WRITE], nr);
597 void show_run_stats(void)
599 struct group_run_stats *runstats, *rs;
600 struct thread_data *td;
601 struct thread_stat *threadstats, *ts;
602 int i, j, k, nr_ts, last_ts, members;
604 runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1));
606 for (i = 0; i < groupid + 1; i++) {
609 memset(rs, 0, sizeof(*rs));
610 rs->min_bw[0] = rs->min_run[0] = ~0UL;
611 rs->min_bw[1] = rs->min_run[1] = ~0UL;
615 * find out how many threads stats we need. if group reporting isn't
616 * enabled, it's one-per-td.
621 if (!td->group_reporting) {
625 if (last_ts == td->groupid)
628 last_ts = td->groupid;
632 threadstats = malloc(nr_ts * sizeof(struct thread_stat));
634 for (i = 0; i < nr_ts; i++) {
635 ts = &threadstats[i];
637 memset(ts, 0, sizeof(*ts));
638 ts->clat_stat[0].min_val = -1UL;
639 ts->clat_stat[1].min_val = -1UL;
640 ts->slat_stat[0].min_val = -1UL;
641 ts->slat_stat[1].min_val = -1UL;
642 ts->bw_stat[0].min_val = -1UL;
643 ts->bw_stat[1].min_val = -1UL;
650 ts = &threadstats[j];
656 ts->description = td->description;
657 ts->error = td->error;
658 ts->groupid = td->groupid;
660 ts->verror = td->verror;
663 sum_stat(ts->clat_stat, td->ts.clat_stat, members);
664 sum_stat(ts->slat_stat, td->ts.slat_stat, members);
665 sum_stat(ts->bw_stat, td->ts.bw_stat, members);
667 ts->stat_io_bytes[0] += td->ts.stat_io_bytes[0];
668 ts->stat_io_bytes[1] += td->ts.stat_io_bytes[1];
670 ts->usr_time += td->ts.usr_time;
671 ts->sys_time += td->ts.sys_time;
672 ts->ctx += td->ts.ctx;
674 for (k = 0; k < FIO_IO_U_MAP_NR; k++)
675 ts->io_u_map[k] += td->ts.io_u_map[k];
676 for (k = 0; k < FIO_IO_U_LAT_NR; k++)
677 ts->io_u_lat[k] += td->ts.io_u_lat[k];
679 ts->total_io_u += td->ts.total_io_u;
680 ts->io_bytes[0] += td->ts.io_bytes[0];
681 ts->io_bytes[1] += td->ts.io_bytes[1];
683 if (ts->runtime[0] < td->ts.runtime[0])
684 ts->runtime[0] = td->ts.runtime[0];
685 if (ts->runtime[1] < td->ts.runtime[1])
686 ts->runtime[1] = td->ts.runtime[1];
688 ts->total_run_time += td->ts.total_run_time;
690 if (!td->group_reporting) {
695 if (last_ts == td->groupid)
703 last_ts = td->groupid;
706 for (i = 0; i < nr_ts; i++) {
707 unsigned long long rbw, wbw;
709 ts = &threadstats[i];
710 rs = &runstats[ts->groupid];
712 if (ts->runtime[0] < rs->min_run[0] || !rs->min_run[0])
713 rs->min_run[0] = ts->runtime[0];
714 if (ts->runtime[0] > rs->max_run[0])
715 rs->max_run[0] = ts->runtime[0];
716 if (ts->runtime[1] < rs->min_run[1] || !rs->min_run[1])
717 rs->min_run[1] = ts->runtime[1];
718 if (ts->runtime[1] > rs->max_run[1])
719 rs->max_run[1] = ts->runtime[1];
723 rbw = td->io_bytes[0] / (unsigned long long) ts->runtime[0];
725 wbw = td->io_bytes[1] / (unsigned long long) ts->runtime[1];
727 if (rbw < rs->min_bw[0])
729 if (wbw < rs->min_bw[1])
731 if (rbw > rs->max_bw[0])
733 if (wbw > rs->max_bw[1])
736 rs->io_kb[0] += ts->io_bytes[0] >> 10;
737 rs->io_kb[1] += ts->io_bytes[1] >> 10;
740 for (i = 0; i < groupid + 1; i++) {
744 rs->agg[0] = (rs->io_kb[0]*1024) / rs->max_run[0];
746 rs->agg[1] = (rs->io_kb[1]*1024) / rs->max_run[1];
750 * don't overwrite last signal output
755 for (i = 0; i < nr_ts; i++) {
756 ts = &threadstats[i];
757 rs = &runstats[ts->groupid];
760 show_thread_status_terse(ts, rs);
762 show_thread_status(ts, rs);
766 for (i = 0; i < groupid + 1; i++)
767 show_group_stats(&runstats[i], i);
776 static inline void add_stat_sample(struct io_stat *is, unsigned long data)
781 if (data > is->max_val)
783 if (data < is->min_val)
786 delta = val - is->mean;
787 n = is->samples + 1.0;
788 is->mean += delta / n;
789 is->S += delta * (val - is->mean);
794 static void __add_log_sample(struct io_log *iolog, unsigned long val,
795 enum fio_ddir ddir, unsigned long time)
797 if (iolog->nr_samples == iolog->max_samples) {
798 int new_size = sizeof(struct io_sample) * iolog->max_samples*2;
800 iolog->log = realloc(iolog->log, new_size);
801 iolog->max_samples <<= 1;
804 iolog->log[iolog->nr_samples].val = val;
805 iolog->log[iolog->nr_samples].time = time;
806 iolog->log[iolog->nr_samples].ddir = ddir;
810 static void add_log_sample(struct thread_data *td, struct io_log *iolog,
811 unsigned long val, enum fio_ddir ddir)
813 __add_log_sample(iolog, val, ddir, mtime_since_now(&td->epoch));
816 void add_agg_sample(unsigned long val, enum fio_ddir ddir)
818 struct io_log *iolog = agg_io_log[ddir];
820 __add_log_sample(iolog, val, ddir, mtime_since_genesis());
823 void add_clat_sample(struct thread_data *td, enum fio_ddir ddir,
826 struct thread_stat *ts = &td->ts;
828 add_stat_sample(&ts->clat_stat[ddir], msec);
831 add_log_sample(td, ts->clat_log, msec, ddir);
834 void add_slat_sample(struct thread_data *td, enum fio_ddir ddir,
837 struct thread_stat *ts = &td->ts;
839 add_stat_sample(&ts->slat_stat[ddir], msec);
842 add_log_sample(td, ts->slat_log, msec, ddir);
845 void add_bw_sample(struct thread_data *td, enum fio_ddir ddir,
848 struct thread_stat *ts = &td->ts;
849 unsigned long spent = mtime_since(&ts->stat_sample_time[ddir], t);
852 if (spent < td->bw_avg_time)
855 rate = (td->this_io_bytes[ddir] - ts->stat_io_bytes[ddir]) / spent;
856 add_stat_sample(&ts->bw_stat[ddir], rate);
859 add_log_sample(td, ts->bw_log, rate, ddir);
861 fio_gettime(&ts->stat_sample_time[ddir], NULL);
862 ts->stat_io_bytes[ddir] = td->this_io_bytes[ddir];