12 static struct itimerval itimer;
13 static struct list_head disk_list = LIST_HEAD_INIT(disk_list);
14 static dev_t last_dev;
17 * Cheesy number->string conversion, complete with carry rounding error.
19 static char *num2str(unsigned long num, int maxlen, int base, int pow2)
21 char postfix[] = { ' ', 'K', 'M', 'G', 'P', 'E' };
22 unsigned int thousand;
33 for (i = 0; base > 1; i++)
39 len = sprintf(buf, "%'lu", num);
42 buf[len] = postfix[i];
48 if ((num % thousand) >= (thousand / 2))
59 static int get_io_ticks(struct disk_util *du, struct disk_util_stat *dus)
66 f = fopen(du->path, "r");
70 p = fgets(line, sizeof(line), f);
76 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) {
85 static void update_io_tick_disk(struct disk_util *du)
87 struct disk_util_stat __dus, *dus, *ldus;
90 if (get_io_ticks(du, &__dus))
96 dus->sectors[0] += (__dus.sectors[0] - ldus->sectors[0]);
97 dus->sectors[1] += (__dus.sectors[1] - ldus->sectors[1]);
98 dus->ios[0] += (__dus.ios[0] - ldus->ios[0]);
99 dus->ios[1] += (__dus.ios[1] - ldus->ios[1]);
100 dus->merges[0] += (__dus.merges[0] - ldus->merges[0]);
101 dus->merges[1] += (__dus.merges[1] - ldus->merges[1]);
102 dus->ticks[0] += (__dus.ticks[0] - ldus->ticks[0]);
103 dus->ticks[1] += (__dus.ticks[1] - ldus->ticks[1]);
104 dus->io_ticks += (__dus.io_ticks - ldus->io_ticks);
105 dus->time_in_queue += (__dus.time_in_queue - ldus->time_in_queue);
107 fio_gettime(&t, NULL);
108 du->msec += mtime_since(&du->time, &t);
109 memcpy(&du->time, &t, sizeof(t));
110 memcpy(ldus, &__dus, sizeof(__dus));
113 void update_io_ticks(void)
115 struct list_head *entry;
116 struct disk_util *du;
118 list_for_each(entry, &disk_list) {
119 du = list_entry(entry, struct disk_util, list);
120 update_io_tick_disk(du);
124 static int disk_util_exists(dev_t dev)
126 struct list_head *entry;
127 struct disk_util *du;
129 list_for_each(entry, &disk_list) {
130 du = list_entry(entry, struct disk_util, list);
139 static void disk_util_add(dev_t dev, char *path)
141 struct disk_util *du, *__du;
142 struct list_head *entry;
144 du = malloc(sizeof(*du));
145 memset(du, 0, sizeof(*du));
146 INIT_LIST_HEAD(&du->list);
147 sprintf(du->path, "%s/stat", path);
148 du->name = strdup(basename(path));
151 list_for_each(entry, &disk_list) {
152 __du = list_entry(entry, struct disk_util, list);
154 if (!strcmp(du->name, __du->name)) {
161 fio_gettime(&du->time, NULL);
162 get_io_ticks(du, &du->last_dus);
164 list_add_tail(&du->list, &disk_list);
167 static int check_dev_match(dev_t dev, char *path)
169 unsigned int major, minor;
173 f = fopen(path, "r");
179 p = fgets(line, sizeof(line), f);
185 if (sscanf(p, "%u:%u", &major, &minor) != 2) {
190 if (((major << 8) | minor) == dev) {
199 static int find_block_dir(dev_t dev, char *path)
210 while ((dir = readdir(D)) != NULL) {
213 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
216 sprintf(full_path, "%s/%s", path, dir->d_name);
218 if (!strcmp(dir->d_name, "dev")) {
219 if (!check_dev_match(dev, full_path)) {
225 if (lstat(full_path, &st) == -1) {
230 if (!S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode))
233 found = find_block_dir(dev, full_path);
235 strcpy(path, full_path);
244 void init_disk_util(struct thread_data *td)
248 char foo[PATH_MAX], tmp[PATH_MAX];
252 if (!td->do_disk_util || (td->io_ops->flags & FIO_DISKLESSIO))
256 * Just use the same file, they are on the same device.
259 if (!stat(f->file_name, &st)) {
260 if (S_ISBLK(st.st_mode))
266 * must be a file, open "." in that path
268 strncpy(foo, f->file_name, PATH_MAX - 1);
271 perror("disk util stat");
278 if (disk_util_exists(dev))
282 * for an fs without a device, we will repeatedly stat through
283 * sysfs which can take oodles of time for thousands of files. so
284 * cache the last lookup and compare with that before going through
292 sprintf(foo, "/sys/block");
293 if (!find_block_dir(dev, foo))
297 * If there's a ../queue/ directory there, we are inside a partition.
298 * Check if that is the case and jump back. For loop/md/dm etc we
299 * are already in the right spot.
301 sprintf(tmp, "%s/../queue", foo);
302 if (!stat(tmp, &st)) {
304 sprintf(tmp, "%s/queue", p);
305 if (stat(tmp, &st)) {
306 log_err("unknown sysfs layout\n");
309 strncpy(tmp, p, PATH_MAX - 1);
310 sprintf(foo, "%s", tmp);
314 td->sysfs_root = strdup(foo);
316 disk_util_add(dev, foo);
319 void disk_util_timer_arm(void)
321 itimer.it_value.tv_sec = 0;
322 itimer.it_value.tv_usec = DISK_UTIL_MSEC * 1000;
323 setitimer(ITIMER_REAL, &itimer, NULL);
326 void update_rusage_stat(struct thread_data *td)
328 struct thread_stat *ts = &td->ts;
330 getrusage(RUSAGE_SELF, &ts->ru_end);
332 ts->usr_time += mtime_since(&ts->ru_start.ru_utime, &ts->ru_end.ru_utime);
333 ts->sys_time += mtime_since(&ts->ru_start.ru_stime, &ts->ru_end.ru_stime);
334 ts->ctx += ts->ru_end.ru_nvcsw + ts->ru_end.ru_nivcsw - (ts->ru_start.ru_nvcsw + ts->ru_start.ru_nivcsw);
336 memcpy(&ts->ru_start, &ts->ru_end, sizeof(ts->ru_end));
339 static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max,
340 double *mean, double *dev)
342 double n = is->samples;
344 if (is->samples == 0)
350 n = (double) is->samples;
354 *dev = sqrt(is->S / (n - 1.0));
361 static void show_group_stats(struct group_run_stats *rs, int id)
363 char *p1, *p2, *p3, *p4;
364 const char *ddir_str[] = { " READ", " WRITE" };
367 fprintf(f_out, "\nRun status group %d (all jobs):\n", id);
369 for (i = 0; i <= DDIR_WRITE; i++) {
373 p1 = num2str(rs->io_kb[i], 6, 1000, 1);
374 p2 = num2str(rs->agg[i], 6, 1000, 1);
375 p3 = num2str(rs->min_bw[i], 6, 1000, 1);
376 p4 = num2str(rs->max_bw[i], 6, 1000, 1);
378 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[i], rs->max_run[i]);
387 static void show_disk_util(void)
389 struct disk_util_stat *dus;
390 struct list_head *entry, *next;
391 struct disk_util *du;
394 fprintf(f_out, "\nDisk stats (read/write):\n");
396 list_for_each(entry, &disk_list) {
397 du = list_entry(entry, struct disk_util, list);
400 util = (double) 100 * du->dus.io_ticks / (double) du->msec;
404 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);
410 list_for_each_safe(entry, next, &disk_list) {
412 du = list_entry(entry, struct disk_util, list);
418 #define ts_total_io_u(ts) \
419 ((ts)->total_io_u[0] + (ts)->total_io_u[1])
421 static void stat_calc_dist(struct thread_stat *ts, double *io_u_dist)
426 * Do depth distribution calculations
428 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
429 io_u_dist[i] = (double) ts->io_u_map[i] / (double) ts_total_io_u(ts);
430 io_u_dist[i] *= 100.0;
434 static void stat_calc_lat(struct thread_stat *ts, double *io_u_lat)
439 * Do latency distribution calculations
441 for (i = 0; i < FIO_IO_U_LAT_NR; i++) {
442 io_u_lat[i] = (double) ts->io_u_lat[i] / (double) ts_total_io_u(ts);
443 io_u_lat[i] *= 100.0;
447 static void show_ddir_status(struct group_run_stats *rs, struct thread_stat *ts,
450 const char *ddir_str[] = { "read ", "write" };
451 unsigned long min, max;
452 unsigned long long bw, iops;
454 char *io_p, *bw_p, *iops_p;
456 if (!ts->runtime[ddir])
459 bw = ts->io_bytes[ddir] / ts->runtime[ddir];
460 iops = (1000 * ts->total_io_u[ddir]) / ts->runtime[ddir];
461 io_p = num2str(ts->io_bytes[ddir] >> 10, 6, 1000, 1);
462 bw_p = num2str(bw, 6, 1000, 1);
463 iops_p = num2str(iops, 6, 1, 0);
465 fprintf(f_out, " %s: io=%siB, bw=%siB/s, iops=%s, runt=%6lumsec\n", ddir_str[ddir], io_p, bw_p, iops_p, ts->runtime[ddir]);
471 if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev))
472 fprintf(f_out, " slat (msec): min=%5lu, max=%5lu, avg=%5.02f, stdev=%5.02f\n", min, max, mean, dev);
474 if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev))
475 fprintf(f_out, " clat (msec): min=%5lu, max=%5lu, avg=%5.02f, stdev=%5.02f\n", min, max, mean, dev);
477 if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
480 p_of_agg = mean * 100 / (double) rs->agg[ddir];
481 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);
485 static void show_thread_status(struct thread_stat *ts,
486 struct group_run_stats *rs)
488 double usr_cpu, sys_cpu;
489 unsigned long runtime;
490 double io_u_dist[FIO_IO_U_MAP_NR];
491 double io_u_lat[FIO_IO_U_LAT_NR];
493 if (!(ts->io_bytes[0] + ts->io_bytes[1]))
497 fprintf(f_out, "%s: (groupid=%d, jobs=%d): err=%2d: pid=%d\n", ts->name, ts->groupid, ts->members, ts->error, ts->pid);
499 fprintf(f_out, "%s: (groupid=%d, jobs=%d): err=%2d (%s): pid=%d\n", ts->name, ts->groupid, ts->members, ts->error, ts->verror, ts->pid);
502 fprintf(f_out, " Description : [%s]\n", ts->description);
504 if (ts->io_bytes[DDIR_READ])
505 show_ddir_status(rs, ts, DDIR_READ);
506 if (ts->io_bytes[DDIR_WRITE])
507 show_ddir_status(rs, ts, DDIR_WRITE);
509 runtime = ts->total_run_time;
511 double runt = (double) runtime;
513 usr_cpu = (double) ts->usr_time * 100 / runt;
514 sys_cpu = (double) ts->sys_time * 100 / runt;
520 fprintf(f_out, " cpu : usr=%3.2f%%, sys=%3.2f%%, ctx=%lu\n", usr_cpu, sys_cpu, ts->ctx);
522 stat_calc_dist(ts, io_u_dist);
523 stat_calc_lat(ts, io_u_lat);
525 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]);
527 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]);
528 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]);
531 static void show_ddir_status_terse(struct thread_stat *ts,
532 struct group_run_stats *rs, int ddir)
534 unsigned long min, max;
535 unsigned long long bw;
539 if (ts->runtime[ddir])
540 bw = ts->io_bytes[ddir] / ts->runtime[ddir];
542 fprintf(f_out, ";%llu;%llu;%lu", ts->io_bytes[ddir] >> 10, bw, ts->runtime[ddir]);
544 if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev))
545 fprintf(f_out, ";%lu;%lu;%f;%f", min, max, mean, dev);
547 fprintf(f_out, ";%lu;%lu;%f;%f", 0UL, 0UL, 0.0, 0.0);
549 if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev))
550 fprintf(f_out, ";%lu;%lu;%f;%f", min, max, mean, dev);
552 fprintf(f_out, ";%lu;%lu;%f;%f", 0UL, 0UL, 0.0, 0.0);
554 if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
557 p_of_agg = mean * 100 / (double) rs->agg[ddir];
558 fprintf(f_out, ";%lu;%lu;%f%%;%f;%f", min, max, p_of_agg, mean, dev);
560 fprintf(f_out, ";%lu;%lu;%f%%;%f;%f", 0UL, 0UL, 0.0, 0.0, 0.0);
564 static void show_thread_status_terse(struct thread_stat *ts,
565 struct group_run_stats *rs)
567 double io_u_dist[FIO_IO_U_MAP_NR];
568 double io_u_lat[FIO_IO_U_LAT_NR];
569 double usr_cpu, sys_cpu;
571 fprintf(f_out, "%s;%d;%d", ts->name, ts->groupid, ts->error);
573 show_ddir_status_terse(ts, rs, 0);
574 show_ddir_status_terse(ts, rs, 1);
576 if (ts->total_run_time) {
577 double runt = (double) ts->total_run_time;
579 usr_cpu = (double) ts->usr_time * 100 / runt;
580 sys_cpu = (double) ts->sys_time * 100 / runt;
586 fprintf(f_out, ";%f%%;%f%%;%lu", usr_cpu, sys_cpu, ts->ctx);
588 stat_calc_dist(ts, io_u_dist);
589 stat_calc_lat(ts, io_u_lat);
591 fprintf(f_out, ";%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%", 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]);
593 fprintf(f_out, ";%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%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]);
594 fprintf(f_out, ";%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%", io_u_lat[6], io_u_lat[7], io_u_lat[8], io_u_lat[9], io_u_lat[10]);
597 fprintf(f_out, ";%s", ts->description);
599 fprintf(f_out, "\n");
602 static void sum_stat(struct io_stat *dst, struct io_stat *src, int nr)
606 dst->min_val = min(dst->min_val, src->min_val);
607 dst->max_val = max(dst->max_val, src->max_val);
608 dst->samples += src->samples;
611 * Needs a new method for calculating stddev, we cannot just
612 * average them we do below for nr > 1
618 mean = ((src->mean * (double) (nr - 1)) + dst->mean) / ((double) nr);
619 S = ((src->S * (double) (nr - 1)) + dst->S) / ((double) nr);
626 void show_run_stats(void)
628 struct group_run_stats *runstats, *rs;
629 struct thread_data *td;
630 struct thread_stat *threadstats, *ts;
631 int i, j, k, l, nr_ts, last_ts, idx;
633 runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1));
635 for (i = 0; i < groupid + 1; i++) {
638 memset(rs, 0, sizeof(*rs));
639 rs->min_bw[0] = rs->min_run[0] = ~0UL;
640 rs->min_bw[1] = rs->min_run[1] = ~0UL;
644 * find out how many threads stats we need. if group reporting isn't
645 * enabled, it's one-per-td.
650 if (!td->group_reporting) {
654 if (last_ts == td->groupid)
657 last_ts = td->groupid;
661 threadstats = malloc(nr_ts * sizeof(struct thread_stat));
663 for (i = 0; i < nr_ts; i++) {
664 ts = &threadstats[i];
666 memset(ts, 0, sizeof(*ts));
667 for (j = 0; j <= DDIR_WRITE; j++) {
668 ts->clat_stat[j].min_val = -1UL;
669 ts->slat_stat[j].min_val = -1UL;
670 ts->bw_stat[j].min_val = -1UL;
678 ts = &threadstats[j];
685 * These are per-group shared already
688 ts->description = td->description;
689 ts->groupid = td->groupid;
692 * first pid in group, not very useful...
697 if (td->error && !ts->error) {
698 ts->error = td->error;
699 ts->verror = td->verror;
702 for (l = 0; l <= DDIR_WRITE; l++) {
703 sum_stat(&ts->clat_stat[l], &td->ts.clat_stat[l], idx);
704 sum_stat(&ts->slat_stat[l], &td->ts.slat_stat[l], idx);
705 sum_stat(&ts->bw_stat[l], &td->ts.bw_stat[l], idx);
707 ts->stat_io_bytes[l] += td->ts.stat_io_bytes[l];
708 ts->io_bytes[l] += td->ts.io_bytes[l];
710 if (ts->runtime[l] < td->ts.runtime[l])
711 ts->runtime[l] = td->ts.runtime[l];
714 ts->usr_time += td->ts.usr_time;
715 ts->sys_time += td->ts.sys_time;
716 ts->ctx += td->ts.ctx;
718 for (k = 0; k < FIO_IO_U_MAP_NR; k++)
719 ts->io_u_map[k] += td->ts.io_u_map[k];
720 for (k = 0; k < FIO_IO_U_LAT_NR; k++)
721 ts->io_u_lat[k] += td->ts.io_u_lat[k];
723 for (k = 0; k <= DDIR_WRITE; k++)
724 ts->total_io_u[k] += td->ts.total_io_u[k];
726 ts->total_run_time += td->ts.total_run_time;
728 if (!td->group_reporting) {
733 if (last_ts == td->groupid)
741 last_ts = td->groupid;
744 for (i = 0; i < nr_ts; i++) {
745 unsigned long long bw;
747 ts = &threadstats[i];
748 rs = &runstats[ts->groupid];
750 for (j = 0; j <= DDIR_WRITE; j++) {
753 if (ts->runtime[j] < rs->min_run[j] || !rs->min_run[j])
754 rs->min_run[j] = ts->runtime[j];
755 if (ts->runtime[j] > rs->max_run[j])
756 rs->max_run[j] = ts->runtime[j];
760 bw = ts->io_bytes[j] / (unsigned long long) ts->runtime[j];
761 if (bw < rs->min_bw[j])
763 if (bw > rs->max_bw[j])
766 rs->io_kb[j] += ts->io_bytes[j] >> 10;
770 for (i = 0; i < groupid + 1; i++) {
774 rs->agg[0] = (rs->io_kb[0]*1024) / rs->max_run[0];
776 rs->agg[1] = (rs->io_kb[1]*1024) / rs->max_run[1];
780 * don't overwrite last signal output
785 for (i = 0; i < nr_ts; i++) {
786 ts = &threadstats[i];
787 rs = &runstats[ts->groupid];
790 show_thread_status_terse(ts, rs);
792 show_thread_status(ts, rs);
796 for (i = 0; i < groupid + 1; i++)
797 show_group_stats(&runstats[i], i);
806 static inline void add_stat_sample(struct io_stat *is, unsigned long data)
811 if (data > is->max_val)
813 if (data < is->min_val)
816 delta = val - is->mean;
817 is->mean += delta / (is->samples + 1.0);
818 is->S += delta * (val - is->mean);
823 static void __add_log_sample(struct io_log *iolog, unsigned long val,
824 enum fio_ddir ddir, unsigned long time)
826 if (iolog->nr_samples == iolog->max_samples) {
827 int new_size = sizeof(struct io_sample) * iolog->max_samples*2;
829 iolog->log = realloc(iolog->log, new_size);
830 iolog->max_samples <<= 1;
833 iolog->log[iolog->nr_samples].val = val;
834 iolog->log[iolog->nr_samples].time = time;
835 iolog->log[iolog->nr_samples].ddir = ddir;
839 static void add_log_sample(struct thread_data *td, struct io_log *iolog,
840 unsigned long val, enum fio_ddir ddir)
842 __add_log_sample(iolog, val, ddir, mtime_since_now(&td->epoch));
845 void add_agg_sample(unsigned long val, enum fio_ddir ddir)
847 struct io_log *iolog = agg_io_log[ddir];
849 __add_log_sample(iolog, val, ddir, mtime_since_genesis());
852 void add_clat_sample(struct thread_data *td, enum fio_ddir ddir,
855 struct thread_stat *ts = &td->ts;
857 add_stat_sample(&ts->clat_stat[ddir], msec);
860 add_log_sample(td, ts->clat_log, msec, ddir);
863 void add_slat_sample(struct thread_data *td, enum fio_ddir ddir,
866 struct thread_stat *ts = &td->ts;
868 add_stat_sample(&ts->slat_stat[ddir], msec);
871 add_log_sample(td, ts->slat_log, msec, ddir);
874 void add_bw_sample(struct thread_data *td, enum fio_ddir ddir,
877 struct thread_stat *ts = &td->ts;
878 unsigned long spent = mtime_since(&ts->stat_sample_time[ddir], t);
881 if (spent < td->bw_avg_time)
884 rate = (td->this_io_bytes[ddir] - ts->stat_io_bytes[ddir]) / spent;
885 add_stat_sample(&ts->bw_stat[ddir], rate);
888 add_log_sample(td, ts->bw_log, rate, ddir);
890 fio_gettime(&ts->stat_sample_time[ddir], NULL);
891 ts->stat_io_bytes[ddir] = td->this_io_bytes[ddir];