12 #include "lib/ieee754.h"
15 void update_rusage_stat(struct thread_data *td)
17 struct thread_stat *ts = &td->ts;
20 getrusage(RUSAGE_THREAD, &td->ru_end);
22 getrusage(RUSAGE_SELF, &td->ru_end);
25 ts->usr_time += mtime_since(&td->ru_start.ru_utime,
26 &td->ru_end.ru_utime);
27 ts->sys_time += mtime_since(&td->ru_start.ru_stime,
28 &td->ru_end.ru_stime);
29 ts->ctx += td->ru_end.ru_nvcsw + td->ru_end.ru_nivcsw
30 - (td->ru_start.ru_nvcsw + td->ru_start.ru_nivcsw);
31 ts->minf += td->ru_end.ru_minflt - td->ru_start.ru_minflt;
32 ts->majf += td->ru_end.ru_majflt - td->ru_start.ru_majflt;
34 memcpy(&td->ru_start, &td->ru_end, sizeof(td->ru_end));
38 * Given a latency, return the index of the corresponding bucket in
39 * the structure tracking percentiles.
41 * (1) find the group (and error bits) that the value (latency)
42 * belongs to by looking at its MSB. (2) find the bucket number in the
43 * group by looking at the index bits.
46 static unsigned int plat_val_to_idx(unsigned int val)
48 unsigned int msb, error_bits, base, offset, idx;
50 /* Find MSB starting from bit 0 */
54 msb = (sizeof(val)*8) - __builtin_clz(val) - 1;
57 * MSB <= (FIO_IO_U_PLAT_BITS-1), cannot be rounded off. Use
58 * all bits of the sample as index
60 if (msb <= FIO_IO_U_PLAT_BITS)
63 /* Compute the number of error bits to discard*/
64 error_bits = msb - FIO_IO_U_PLAT_BITS;
66 /* Compute the number of buckets before the group */
67 base = (error_bits + 1) << FIO_IO_U_PLAT_BITS;
70 * Discard the error bits and apply the mask to find the
71 * index for the buckets in the group
73 offset = (FIO_IO_U_PLAT_VAL - 1) & (val >> error_bits);
75 /* Make sure the index does not exceed (array size - 1) */
76 idx = (base + offset) < (FIO_IO_U_PLAT_NR - 1) ?
77 (base + offset) : (FIO_IO_U_PLAT_NR - 1);
83 * Convert the given index of the bucket array to the value
84 * represented by the bucket
86 static unsigned int plat_idx_to_val(unsigned int idx)
88 unsigned int error_bits, k, base;
90 assert(idx < FIO_IO_U_PLAT_NR);
92 /* MSB <= (FIO_IO_U_PLAT_BITS-1), cannot be rounded off. Use
93 * all bits of the sample as index */
94 if (idx < (FIO_IO_U_PLAT_VAL << 1))
97 /* Find the group and compute the minimum value of that group */
98 error_bits = (idx >> FIO_IO_U_PLAT_BITS) - 1;
99 base = 1 << (error_bits + FIO_IO_U_PLAT_BITS);
101 /* Find its bucket number of the group */
102 k = idx % FIO_IO_U_PLAT_VAL;
104 /* Return the mean of the range of the bucket */
105 return base + ((k + 0.5) * (1 << error_bits));
108 static int double_cmp(const void *a, const void *b)
110 const fio_fp64_t fa = *(const fio_fp64_t *) a;
111 const fio_fp64_t fb = *(const fio_fp64_t *) b;
116 else if (fa.u.f < fb.u.f)
122 unsigned int calc_clat_percentiles(unsigned int *io_u_plat, unsigned long nr,
123 fio_fp64_t *plist, unsigned int **output,
124 unsigned int *maxv, unsigned int *minv)
126 unsigned long sum = 0;
127 unsigned int len, i, j = 0;
128 unsigned int oval_len = 0;
129 unsigned int *ovals = NULL;
136 while (len < FIO_IO_U_LIST_MAX_LEN && plist[len].u.f != 0.0)
143 * Sort the percentile list. Note that it may already be sorted if
144 * we are using the default values, but since it's a short list this
145 * isn't a worry. Also note that this does not work for NaN values.
148 qsort((void *)plist, len, sizeof(plist[0]), double_cmp);
151 * Calculate bucket values, note down max and min values
154 for (i = 0; i < FIO_IO_U_PLAT_NR && !is_last; i++) {
156 while (sum >= (plist[j].u.f / 100.0 * nr)) {
157 assert(plist[j].u.f <= 100.0);
161 ovals = realloc(ovals, oval_len * sizeof(unsigned int));
164 ovals[j] = plat_idx_to_val(i);
165 if (ovals[j] < *minv)
167 if (ovals[j] > *maxv)
170 is_last = (j == len - 1);
183 * Find and display the p-th percentile of clat
185 static void show_clat_percentiles(unsigned int *io_u_plat, unsigned long nr,
188 unsigned int len, j = 0, minv, maxv;
190 int is_last, scale_down;
192 len = calc_clat_percentiles(io_u_plat, nr, plist, &ovals, &maxv, &minv);
197 * We default to usecs, but if the value range is such that we
198 * should scale down to msecs, do that.
200 if (minv > 2000 && maxv > 99999) {
202 log_info(" clat percentiles (msec):\n |");
205 log_info(" clat percentiles (usec):\n |");
208 for (j = 0; j < len; j++) {
212 if (j != 0 && (j % 4) == 0)
215 /* end of the list */
216 is_last = (j == len - 1);
218 if (plist[j].u.f < 10.0)
219 sprintf(fbuf, " %2.2f", plist[j].u.f);
221 sprintf(fbuf, "%2.2f", plist[j].u.f);
224 ovals[j] = (ovals[j] + 999) / 1000;
226 log_info(" %sth=[%5u]%c", fbuf, ovals[j], is_last ? '\n' : ',');
231 if (j % 4 == 3) /* for formatting */
240 int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max,
241 double *mean, double *dev)
243 double n = is->samples;
245 if (is->samples == 0)
251 n = (double) is->samples;
252 *mean = is->mean.u.f;
255 *dev = sqrt(is->S.u.f / (n - 1.0));
262 void show_group_stats(struct group_run_stats *rs)
264 char *p1, *p2, *p3, *p4;
265 const char *ddir_str[] = { " READ", " WRITE" , " TRIM"};
268 log_info("\nRun status group %d (all jobs):\n", rs->groupid);
270 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
271 const int i2p = is_power_of_2(rs->kb_base);
276 p1 = num2str(rs->io_kb[i], 6, rs->kb_base, i2p);
277 p2 = num2str(rs->agg[i], 6, rs->kb_base, i2p);
278 p3 = num2str(rs->min_bw[i], 6, rs->kb_base, i2p);
279 p4 = num2str(rs->max_bw[i], 6, rs->kb_base, i2p);
281 log_info("%s: io=%sB, aggrb=%sB/s, minb=%sB/s, maxb=%sB/s,"
282 " mint=%llumsec, maxt=%llumsec\n", ddir_str[i], p1, p2,
283 p3, p4, rs->min_run[i],
293 void stat_calc_dist(unsigned int *map, unsigned long total, double *io_u_dist)
298 * Do depth distribution calculations
300 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
302 io_u_dist[i] = (double) map[i] / (double) total;
303 io_u_dist[i] *= 100.0;
304 if (io_u_dist[i] < 0.1 && map[i])
311 static void stat_calc_lat(struct thread_stat *ts, double *dst,
312 unsigned int *src, int nr)
314 unsigned long total = ddir_rw_sum(ts->total_io_u);
318 * Do latency distribution calculations
320 for (i = 0; i < nr; i++) {
322 dst[i] = (double) src[i] / (double) total;
324 if (dst[i] < 0.01 && src[i])
331 void stat_calc_lat_u(struct thread_stat *ts, double *io_u_lat)
333 stat_calc_lat(ts, io_u_lat, ts->io_u_lat_u, FIO_IO_U_LAT_U_NR);
336 void stat_calc_lat_m(struct thread_stat *ts, double *io_u_lat)
338 stat_calc_lat(ts, io_u_lat, ts->io_u_lat_m, FIO_IO_U_LAT_M_NR);
341 static void display_lat(const char *name, unsigned long min, unsigned long max,
342 double mean, double dev)
344 const char *base = "(usec)";
347 if (!usec_to_msec(&min, &max, &mean, &dev))
350 minp = num2str(min, 6, 1, 0);
351 maxp = num2str(max, 6, 1, 0);
353 log_info(" %s %s: min=%s, max=%s, avg=%5.02f,"
354 " stdev=%5.02f\n", name, base, minp, maxp, mean, dev);
360 static void show_ddir_status(struct group_run_stats *rs, struct thread_stat *ts,
363 const char *ddir_str[] = { "read ", "write", "trim" };
364 unsigned long min, max, runt;
365 unsigned long long bw, iops;
367 char *io_p, *bw_p, *iops_p;
370 assert(ddir_rw(ddir));
372 if (!ts->runtime[ddir])
375 i2p = is_power_of_2(rs->kb_base);
376 runt = ts->runtime[ddir];
378 bw = (1000 * ts->io_bytes[ddir]) / runt;
379 io_p = num2str(ts->io_bytes[ddir], 6, 1, i2p);
380 bw_p = num2str(bw, 6, 1, i2p);
382 iops = (1000 * (uint64_t)ts->total_io_u[ddir]) / runt;
383 iops_p = num2str(iops, 6, 1, 0);
385 log_info(" %s: io=%sB, bw=%sB/s, iops=%s, runt=%6llumsec\n",
386 ddir_str[ddir], io_p, bw_p, iops_p,
393 if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev))
394 display_lat("slat", min, max, mean, dev);
395 if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev))
396 display_lat("clat", min, max, mean, dev);
397 if (calc_lat(&ts->lat_stat[ddir], &min, &max, &mean, &dev))
398 display_lat(" lat", min, max, mean, dev);
400 if (ts->clat_percentiles) {
401 show_clat_percentiles(ts->io_u_plat[ddir],
402 ts->clat_stat[ddir].samples,
403 ts->percentile_list);
405 if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
406 double p_of_agg = 100.0;
407 const char *bw_str = "KB";
410 p_of_agg = mean * 100 / (double) rs->agg[ddir];
411 if (p_of_agg > 100.0)
415 if (mean > 999999.9) {
423 log_info(" bw (%s/s) : min=%5lu, max=%5lu, per=%3.2f%%,"
424 " avg=%5.02f, stdev=%5.02f\n", bw_str, min, max,
425 p_of_agg, mean, dev);
429 static int show_lat(double *io_u_lat, int nr, const char **ranges,
432 int new_line = 1, i, line = 0, shown = 0;
434 for (i = 0; i < nr; i++) {
435 if (io_u_lat[i] <= 0.0)
441 log_info(" lat (%s) : ", msg);
447 log_info("%s%3.2f%%", ranges[i], io_u_lat[i]);
459 static void show_lat_u(double *io_u_lat_u)
461 const char *ranges[] = { "2=", "4=", "10=", "20=", "50=", "100=",
462 "250=", "500=", "750=", "1000=", };
464 show_lat(io_u_lat_u, FIO_IO_U_LAT_U_NR, ranges, "usec");
467 static void show_lat_m(double *io_u_lat_m)
469 const char *ranges[] = { "2=", "4=", "10=", "20=", "50=", "100=",
470 "250=", "500=", "750=", "1000=", "2000=",
473 show_lat(io_u_lat_m, FIO_IO_U_LAT_M_NR, ranges, "msec");
476 static void show_latencies(struct thread_stat *ts)
478 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
479 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
481 stat_calc_lat_u(ts, io_u_lat_u);
482 stat_calc_lat_m(ts, io_u_lat_m);
484 show_lat_u(io_u_lat_u);
485 show_lat_m(io_u_lat_m);
488 void show_thread_status(struct thread_stat *ts, struct group_run_stats *rs)
490 double usr_cpu, sys_cpu;
491 unsigned long runtime;
492 double io_u_dist[FIO_IO_U_MAP_NR];
496 if (!(ts->io_bytes[DDIR_READ] + ts->io_bytes[DDIR_WRITE] +
497 ts->io_bytes[DDIR_TRIM]) && !(ts->total_io_u[DDIR_READ] +
498 ts->total_io_u[DDIR_WRITE] + ts->total_io_u[DDIR_TRIM]))
502 os_ctime_r((const time_t *) &time_p, time_buf, sizeof(time_buf));
505 log_info("%s: (groupid=%d, jobs=%d): err=%2d: pid=%d: %s",
506 ts->name, ts->groupid, ts->members,
507 ts->error, (int) ts->pid, time_buf);
509 log_info("%s: (groupid=%d, jobs=%d): err=%2d (%s): pid=%d: %s",
510 ts->name, ts->groupid, ts->members,
511 ts->error, ts->verror, (int) ts->pid,
515 if (strlen(ts->description))
516 log_info(" Description : [%s]\n", ts->description);
518 if (ts->io_bytes[DDIR_READ])
519 show_ddir_status(rs, ts, DDIR_READ);
520 if (ts->io_bytes[DDIR_WRITE])
521 show_ddir_status(rs, ts, DDIR_WRITE);
522 if (ts->io_bytes[DDIR_TRIM])
523 show_ddir_status(rs, ts, DDIR_TRIM);
527 runtime = ts->total_run_time;
529 double runt = (double) runtime;
531 usr_cpu = (double) ts->usr_time * 100 / runt;
532 sys_cpu = (double) ts->sys_time * 100 / runt;
538 log_info(" cpu : usr=%3.2f%%, sys=%3.2f%%, ctx=%lu, majf=%lu,"
539 " minf=%lu\n", usr_cpu, sys_cpu, ts->ctx, ts->majf, ts->minf);
541 stat_calc_dist(ts->io_u_map, ddir_rw_sum(ts->total_io_u), io_u_dist);
542 log_info(" IO depths : 1=%3.1f%%, 2=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%,"
543 " 16=%3.1f%%, 32=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
544 io_u_dist[1], io_u_dist[2],
545 io_u_dist[3], io_u_dist[4],
546 io_u_dist[5], io_u_dist[6]);
548 stat_calc_dist(ts->io_u_submit, ts->total_submit, io_u_dist);
549 log_info(" submit : 0=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%, 16=%3.1f%%,"
550 " 32=%3.1f%%, 64=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
551 io_u_dist[1], io_u_dist[2],
552 io_u_dist[3], io_u_dist[4],
553 io_u_dist[5], io_u_dist[6]);
554 stat_calc_dist(ts->io_u_complete, ts->total_complete, io_u_dist);
555 log_info(" complete : 0=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%, 16=%3.1f%%,"
556 " 32=%3.1f%%, 64=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
557 io_u_dist[1], io_u_dist[2],
558 io_u_dist[3], io_u_dist[4],
559 io_u_dist[5], io_u_dist[6]);
560 log_info(" issued : total=r=%lu/w=%lu/d=%lu,"
561 " short=r=%lu/w=%lu/d=%lu\n",
562 ts->total_io_u[0], ts->total_io_u[1],
564 ts->short_io_u[0], ts->short_io_u[1],
566 if (ts->continue_on_error) {
567 log_info(" errors : total=%lu, first_error=%d/<%s>\n",
570 strerror(ts->first_error));
574 static void show_ddir_status_terse(struct thread_stat *ts,
575 struct group_run_stats *rs, int ddir)
577 unsigned long min, max;
578 unsigned long long bw, iops;
579 unsigned int *ovals = NULL;
581 unsigned int len, minv, maxv;
584 assert(ddir_rw(ddir));
587 if (ts->runtime[ddir]) {
588 uint64_t runt = ts->runtime[ddir];
590 bw = ((1000 * ts->io_bytes[ddir]) / runt) / 1024;
591 iops = (1000 * (uint64_t) ts->total_io_u[ddir]) / runt;
594 log_info(";%llu;%llu;%llu;%llu", ts->io_bytes[ddir] >> 10, bw, iops,
597 if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev))
598 log_info(";%lu;%lu;%f;%f", min, max, mean, dev);
600 log_info(";%lu;%lu;%f;%f", 0UL, 0UL, 0.0, 0.0);
602 if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev))
603 log_info(";%lu;%lu;%f;%f", min, max, mean, dev);
605 log_info(";%lu;%lu;%f;%f", 0UL, 0UL, 0.0, 0.0);
607 if (ts->clat_percentiles) {
608 len = calc_clat_percentiles(ts->io_u_plat[ddir],
609 ts->clat_stat[ddir].samples,
610 ts->percentile_list, &ovals, &maxv,
615 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
620 log_info(";%2.2f%%=%u", ts->percentile_list[i].u.f, ovals[i]);
623 if (calc_lat(&ts->lat_stat[ddir], &min, &max, &mean, &dev))
624 log_info(";%lu;%lu;%f;%f", min, max, mean, dev);
626 log_info(";%lu;%lu;%f;%f", 0UL, 0UL, 0.0, 0.0);
631 if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
632 double p_of_agg = 100.0;
635 p_of_agg = mean * 100 / (double) rs->agg[ddir];
636 if (p_of_agg > 100.0)
640 log_info(";%lu;%lu;%f%%;%f;%f", min, max, p_of_agg, mean, dev);
642 log_info(";%lu;%lu;%f%%;%f;%f", 0UL, 0UL, 0.0, 0.0, 0.0);
645 static void add_ddir_status_json(struct thread_stat *ts,
646 struct group_run_stats *rs, int ddir, struct json_object *parent)
648 unsigned long min, max;
649 unsigned long long bw, iops;
650 unsigned int *ovals = NULL;
652 unsigned int len, minv, maxv;
654 const char *ddirname[] = {"read", "write", "trim"};
655 struct json_object *dir_object, *tmp_object, *percentile_object;
657 double p_of_agg = 100.0;
659 assert(ddir_rw(ddir));
661 dir_object = json_create_object();
662 json_object_add_value_object(parent, ddirname[ddir], dir_object);
665 if (ts->runtime[ddir]) {
666 uint64_t runt = ts->runtime[ddir];
668 bw = ((1000 * ts->io_bytes[ddir]) / runt) / 1024;
669 iops = (1000 * (uint64_t) ts->total_io_u[ddir]) / runt;
672 json_object_add_value_int(dir_object, "io_bytes", ts->io_bytes[ddir] >> 10);
673 json_object_add_value_int(dir_object, "bw", bw);
674 json_object_add_value_int(dir_object, "iops", iops);
675 json_object_add_value_int(dir_object, "runtime", ts->runtime[ddir]);
677 if (!calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev)) {
681 tmp_object = json_create_object();
682 json_object_add_value_object(dir_object, "slat", tmp_object);
683 json_object_add_value_int(tmp_object, "min", min);
684 json_object_add_value_int(tmp_object, "max", max);
685 json_object_add_value_float(tmp_object, "mean", mean);
686 json_object_add_value_float(tmp_object, "stddev", dev);
688 if (!calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev)) {
692 tmp_object = json_create_object();
693 json_object_add_value_object(dir_object, "clat", tmp_object);
694 json_object_add_value_int(tmp_object, "min", min);
695 json_object_add_value_int(tmp_object, "max", max);
696 json_object_add_value_float(tmp_object, "mean", mean);
697 json_object_add_value_float(tmp_object, "stddev", dev);
699 if (ts->clat_percentiles) {
700 len = calc_clat_percentiles(ts->io_u_plat[ddir],
701 ts->clat_stat[ddir].samples,
702 ts->percentile_list, &ovals, &maxv,
707 percentile_object = json_create_object();
708 json_object_add_value_object(tmp_object, "percentile", percentile_object);
709 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
711 json_object_add_value_int(percentile_object, "0.00", 0);
714 snprintf(buf, sizeof(buf) - 1, "%2.2f", ts->percentile_list[i].u.f);
715 json_object_add_value_int(percentile_object, (const char *)buf, ovals[i]);
718 if (!calc_lat(&ts->lat_stat[ddir], &min, &max, &mean, &dev)) {
722 tmp_object = json_create_object();
723 json_object_add_value_object(dir_object, "lat", tmp_object);
724 json_object_add_value_int(tmp_object, "min", min);
725 json_object_add_value_int(tmp_object, "max", max);
726 json_object_add_value_float(tmp_object, "mean", mean);
727 json_object_add_value_float(tmp_object, "stddev", dev);
731 if (!calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
733 p_of_agg = mean * 100 / (double) rs->agg[ddir];
734 if (p_of_agg > 100.0)
739 p_of_agg = mean = dev = 0.0;
741 json_object_add_value_int(dir_object, "bw_min", min);
742 json_object_add_value_int(dir_object, "bw_max", max);
743 json_object_add_value_float(dir_object, "bw_agg", mean);
744 json_object_add_value_float(dir_object, "bw_mean", mean);
745 json_object_add_value_float(dir_object, "bw_dev", dev);
748 static void show_thread_status_terse_v2(struct thread_stat *ts,
749 struct group_run_stats *rs)
751 double io_u_dist[FIO_IO_U_MAP_NR];
752 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
753 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
754 double usr_cpu, sys_cpu;
758 log_info("2;%s;%d;%d", ts->name, ts->groupid, ts->error);
759 /* Log Read Status */
760 show_ddir_status_terse(ts, rs, DDIR_READ);
761 /* Log Write Status */
762 show_ddir_status_terse(ts, rs, DDIR_WRITE);
763 /* Log Trim Status */
764 show_ddir_status_terse(ts, rs, DDIR_TRIM);
767 if (ts->total_run_time) {
768 double runt = (double) ts->total_run_time;
770 usr_cpu = (double) ts->usr_time * 100 / runt;
771 sys_cpu = (double) ts->sys_time * 100 / runt;
777 log_info(";%f%%;%f%%;%lu;%lu;%lu", usr_cpu, sys_cpu, ts->ctx, ts->majf,
780 /* Calc % distribution of IO depths, usecond, msecond latency */
781 stat_calc_dist(ts->io_u_map, ddir_rw_sum(ts->total_io_u), io_u_dist);
782 stat_calc_lat_u(ts, io_u_lat_u);
783 stat_calc_lat_m(ts, io_u_lat_m);
785 /* Only show fixed 7 I/O depth levels*/
786 log_info(";%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%",
787 io_u_dist[0], io_u_dist[1], io_u_dist[2], io_u_dist[3],
788 io_u_dist[4], io_u_dist[5], io_u_dist[6]);
790 /* Microsecond latency */
791 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
792 log_info(";%3.2f%%", io_u_lat_u[i]);
793 /* Millisecond latency */
794 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
795 log_info(";%3.2f%%", io_u_lat_m[i]);
796 /* Additional output if continue_on_error set - default off*/
797 if (ts->continue_on_error)
798 log_info(";%lu;%d", ts->total_err_count, ts->first_error);
801 /* Additional output if description is set */
803 log_info(";%s", ts->description);
808 static void show_thread_status_terse_v3_v4(struct thread_stat *ts,
809 struct group_run_stats *rs, int ver)
811 double io_u_dist[FIO_IO_U_MAP_NR];
812 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
813 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
814 double usr_cpu, sys_cpu;
818 log_info("%d;%s;%s;%d;%d", ver, fio_version_string,
819 ts->name, ts->groupid, ts->error);
820 /* Log Read Status */
821 show_ddir_status_terse(ts, rs, DDIR_READ);
822 /* Log Write Status */
823 show_ddir_status_terse(ts, rs, DDIR_WRITE);
824 /* Log Trim Status */
826 show_ddir_status_terse(ts, rs, DDIR_TRIM);
829 if (ts->total_run_time) {
830 double runt = (double) ts->total_run_time;
832 usr_cpu = (double) ts->usr_time * 100 / runt;
833 sys_cpu = (double) ts->sys_time * 100 / runt;
839 log_info(";%f%%;%f%%;%lu;%lu;%lu", usr_cpu, sys_cpu, ts->ctx, ts->majf,
842 /* Calc % distribution of IO depths, usecond, msecond latency */
843 stat_calc_dist(ts->io_u_map, ddir_rw_sum(ts->total_io_u), io_u_dist);
844 stat_calc_lat_u(ts, io_u_lat_u);
845 stat_calc_lat_m(ts, io_u_lat_m);
847 /* Only show fixed 7 I/O depth levels*/
848 log_info(";%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%",
849 io_u_dist[0], io_u_dist[1], io_u_dist[2], io_u_dist[3],
850 io_u_dist[4], io_u_dist[5], io_u_dist[6]);
852 /* Microsecond latency */
853 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
854 log_info(";%3.2f%%", io_u_lat_u[i]);
855 /* Millisecond latency */
856 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
857 log_info(";%3.2f%%", io_u_lat_m[i]);
859 /* disk util stats, if any */
860 show_disk_util(1, NULL);
862 /* Additional output if continue_on_error set - default off*/
863 if (ts->continue_on_error)
864 log_info(";%lu;%d", ts->total_err_count, ts->first_error);
866 /* Additional output if description is set */
867 if (strlen(ts->description))
868 log_info(";%s", ts->description);
873 static struct json_object *show_thread_status_json(struct thread_stat *ts,
874 struct group_run_stats *rs)
876 struct json_object *root, *tmp;
877 double io_u_dist[FIO_IO_U_MAP_NR];
878 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
879 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
880 double usr_cpu, sys_cpu;
883 root = json_create_object();
884 json_object_add_value_string(root, "jobname", ts->name);
885 json_object_add_value_int(root, "groupid", ts->groupid);
886 json_object_add_value_int(root, "error", ts->error);
888 add_ddir_status_json(ts, rs, DDIR_READ, root);
889 add_ddir_status_json(ts, rs, DDIR_WRITE, root);
890 add_ddir_status_json(ts, rs, DDIR_TRIM, root);
893 if (ts->total_run_time) {
894 double runt = (double) ts->total_run_time;
896 usr_cpu = (double) ts->usr_time * 100 / runt;
897 sys_cpu = (double) ts->sys_time * 100 / runt;
902 json_object_add_value_float(root, "usr_cpu", usr_cpu);
903 json_object_add_value_float(root, "sys_cpu", sys_cpu);
904 json_object_add_value_int(root, "ctx", ts->ctx);
905 json_object_add_value_int(root, "majf", ts->majf);
906 json_object_add_value_int(root, "minf", ts->minf);
909 /* Calc % distribution of IO depths, usecond, msecond latency */
910 stat_calc_dist(ts->io_u_map, ddir_rw_sum(ts->total_io_u), io_u_dist);
911 stat_calc_lat_u(ts, io_u_lat_u);
912 stat_calc_lat_m(ts, io_u_lat_m);
914 tmp = json_create_object();
915 json_object_add_value_object(root, "iodepth_level", tmp);
916 /* Only show fixed 7 I/O depth levels*/
917 for (i = 0; i < 7; i++) {
920 snprintf(name, 19, "%d", 1 << i);
922 snprintf(name, 19, ">=%d", 1 << i);
923 json_object_add_value_float(tmp, (const char *)name, io_u_dist[i]);
926 tmp = json_create_object();
927 json_object_add_value_object(root, "latency_us", tmp);
928 /* Microsecond latency */
929 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
930 const char *ranges[] = { "2", "4", "10", "20", "50", "100",
931 "250", "500", "750", "1000", };
932 json_object_add_value_float(tmp, ranges[i], io_u_lat_u[i]);
934 /* Millisecond latency */
935 tmp = json_create_object();
936 json_object_add_value_object(root, "latency_ms", tmp);
937 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++) {
938 const char *ranges[] = { "2", "4", "10", "20", "50", "100",
939 "250", "500", "750", "1000", "2000",
941 json_object_add_value_float(tmp, ranges[i], io_u_lat_m[i]);
944 /* Additional output if continue_on_error set - default off*/
945 if (ts->continue_on_error) {
946 json_object_add_value_int(root, "total_err", ts->total_err_count);
947 json_object_add_value_int(root, "total_err", ts->first_error);
950 /* Additional output if description is set */
951 if (strlen(ts->description))
952 json_object_add_value_string(root, "desc", ts->description);
957 static void show_thread_status_terse(struct thread_stat *ts,
958 struct group_run_stats *rs)
960 if (terse_version == 2)
961 show_thread_status_terse_v2(ts, rs);
962 else if (terse_version == 3 || terse_version == 4)
963 show_thread_status_terse_v3_v4(ts, rs, terse_version);
965 log_err("fio: bad terse version!? %d\n", terse_version);
968 static void sum_stat(struct io_stat *dst, struct io_stat *src, int nr)
972 if (src->samples == 0)
975 dst->min_val = min(dst->min_val, src->min_val);
976 dst->max_val = max(dst->max_val, src->max_val);
979 * Compute new mean and S after the merge
980 * <http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
981 * #Parallel_algorithm>
984 mean = src->mean.u.f;
987 double delta = src->mean.u.f - dst->mean.u.f;
989 mean = ((src->mean.u.f * src->samples) +
990 (dst->mean.u.f * dst->samples)) /
991 (dst->samples + src->samples);
993 S = src->S.u.f + dst->S.u.f + pow(delta, 2.0) *
994 (dst->samples * src->samples) /
995 (dst->samples + src->samples);
998 dst->samples += src->samples;
999 dst->mean.u.f = mean;
1003 void sum_group_stats(struct group_run_stats *dst, struct group_run_stats *src)
1007 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1008 if (dst->max_run[i] < src->max_run[i])
1009 dst->max_run[i] = src->max_run[i];
1010 if (dst->min_run[i] && dst->min_run[i] > src->min_run[i])
1011 dst->min_run[i] = src->min_run[i];
1012 if (dst->max_bw[i] < src->max_bw[i])
1013 dst->max_bw[i] = src->max_bw[i];
1014 if (dst->min_bw[i] && dst->min_bw[i] > src->min_bw[i])
1015 dst->min_bw[i] = src->min_bw[i];
1017 dst->io_kb[i] += src->io_kb[i];
1018 dst->agg[i] += src->agg[i];
1023 void sum_thread_stats(struct thread_stat *dst, struct thread_stat *src, int nr)
1027 for (l = 0; l < DDIR_RWDIR_CNT; l++) {
1028 sum_stat(&dst->clat_stat[l], &src->clat_stat[l], nr);
1029 sum_stat(&dst->slat_stat[l], &src->slat_stat[l], nr);
1030 sum_stat(&dst->lat_stat[l], &src->lat_stat[l], nr);
1031 sum_stat(&dst->bw_stat[l], &src->bw_stat[l], nr);
1033 dst->io_bytes[l] += src->io_bytes[l];
1035 if (dst->runtime[l] < src->runtime[l])
1036 dst->runtime[l] = src->runtime[l];
1039 dst->usr_time += src->usr_time;
1040 dst->sys_time += src->sys_time;
1041 dst->ctx += src->ctx;
1042 dst->majf += src->majf;
1043 dst->minf += src->minf;
1045 for (k = 0; k < FIO_IO_U_MAP_NR; k++)
1046 dst->io_u_map[k] += src->io_u_map[k];
1047 for (k = 0; k < FIO_IO_U_MAP_NR; k++)
1048 dst->io_u_submit[k] += src->io_u_submit[k];
1049 for (k = 0; k < FIO_IO_U_MAP_NR; k++)
1050 dst->io_u_complete[k] += src->io_u_complete[k];
1051 for (k = 0; k < FIO_IO_U_LAT_U_NR; k++)
1052 dst->io_u_lat_u[k] += src->io_u_lat_u[k];
1053 for (k = 0; k < FIO_IO_U_LAT_M_NR; k++)
1054 dst->io_u_lat_m[k] += src->io_u_lat_m[k];
1056 for (k = 0; k < DDIR_RWDIR_CNT; k++) {
1057 dst->total_io_u[k] += src->total_io_u[k];
1058 dst->short_io_u[k] += src->short_io_u[k];
1061 for (k = 0; k < DDIR_RWDIR_CNT; k++) {
1063 for (m = 0; m < FIO_IO_U_PLAT_NR; m++)
1064 dst->io_u_plat[k][m] += src->io_u_plat[k][m];
1067 dst->total_run_time += src->total_run_time;
1068 dst->total_submit += src->total_submit;
1069 dst->total_complete += src->total_complete;
1072 void init_group_run_stat(struct group_run_stats *gs)
1075 memset(gs, 0, sizeof(*gs));
1077 for (i = 0; i < DDIR_RWDIR_CNT; i++)
1078 gs->min_bw[i] = gs->min_run[i] = ~0UL;
1081 void init_thread_stat(struct thread_stat *ts)
1085 memset(ts, 0, sizeof(*ts));
1087 for (j = 0; j < DDIR_RWDIR_CNT; j++) {
1088 ts->lat_stat[j].min_val = -1UL;
1089 ts->clat_stat[j].min_val = -1UL;
1090 ts->slat_stat[j].min_val = -1UL;
1091 ts->bw_stat[j].min_val = -1UL;
1096 void show_run_stats(void)
1098 struct group_run_stats *runstats, *rs;
1099 struct thread_data *td;
1100 struct thread_stat *threadstats, *ts;
1101 int i, j, nr_ts, last_ts, idx;
1102 int kb_base_warned = 0;
1103 struct json_object *root = NULL;
1104 struct json_array *array = NULL;
1106 runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1));
1108 for (i = 0; i < groupid + 1; i++)
1109 init_group_run_stat(&runstats[i]);
1112 * find out how many threads stats we need. if group reporting isn't
1113 * enabled, it's one-per-td.
1117 for_each_td(td, i) {
1118 if (!td->o.group_reporting) {
1122 if (last_ts == td->groupid)
1125 last_ts = td->groupid;
1129 threadstats = malloc(nr_ts * sizeof(struct thread_stat));
1131 for (i = 0; i < nr_ts; i++)
1132 init_thread_stat(&threadstats[i]);
1137 for_each_td(td, i) {
1138 if (idx && (!td->o.group_reporting ||
1139 (td->o.group_reporting && last_ts != td->groupid))) {
1144 last_ts = td->groupid;
1146 ts = &threadstats[j];
1148 ts->clat_percentiles = td->o.clat_percentiles;
1149 if (td->o.overwrite_plist)
1150 memcpy(ts->percentile_list, td->o.percentile_list, sizeof(td->o.percentile_list));
1152 memcpy(ts->percentile_list, def_percentile_list, sizeof(def_percentile_list));
1157 if (ts->groupid == -1) {
1159 * These are per-group shared already
1161 strncpy(ts->name, td->o.name, FIO_JOBNAME_SIZE);
1162 if (td->o.description)
1163 strncpy(ts->description, td->o.description,
1166 memset(ts->description, 0, FIO_JOBNAME_SIZE);
1169 * If multiple entries in this group, this is
1172 ts->thread_number = td->thread_number;
1173 ts->groupid = td->groupid;
1176 * first pid in group, not very useful...
1180 ts->kb_base = td->o.kb_base;
1181 } else if (ts->kb_base != td->o.kb_base && !kb_base_warned) {
1182 log_info("fio: kb_base differs for jobs in group, using"
1183 " %u as the base\n", ts->kb_base);
1187 ts->continue_on_error = td->o.continue_on_error;
1188 ts->total_err_count += td->total_err_count;
1189 ts->first_error = td->first_error;
1191 if (!td->error && td->o.continue_on_error &&
1193 ts->error = td->first_error;
1194 strcpy(ts->verror, td->verror);
1195 } else if (td->error) {
1196 ts->error = td->error;
1197 strcpy(ts->verror, td->verror);
1201 sum_thread_stats(ts, &td->ts, idx);
1204 for (i = 0; i < nr_ts; i++) {
1205 unsigned long long bw;
1207 ts = &threadstats[i];
1208 rs = &runstats[ts->groupid];
1209 rs->kb_base = ts->kb_base;
1211 for (j = 0; j < DDIR_RWDIR_CNT; j++) {
1212 if (!ts->runtime[j])
1214 if (ts->runtime[j] < rs->min_run[j] || !rs->min_run[j])
1215 rs->min_run[j] = ts->runtime[j];
1216 if (ts->runtime[j] > rs->max_run[j])
1217 rs->max_run[j] = ts->runtime[j];
1220 if (ts->runtime[j]) {
1221 unsigned long runt = ts->runtime[j];
1222 unsigned long long kb;
1224 kb = ts->io_bytes[j] / rs->kb_base;
1225 bw = kb * 1000 / runt;
1227 if (bw < rs->min_bw[j])
1229 if (bw > rs->max_bw[j])
1232 rs->io_kb[j] += ts->io_bytes[j] / rs->kb_base;
1236 for (i = 0; i < groupid + 1; i++) {
1241 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) {
1242 if (rs->max_run[ddir])
1243 rs->agg[ddir] = (rs->io_kb[ddir] * 1000) /
1249 * don't overwrite last signal output
1251 if (output_format == FIO_OUTPUT_NORMAL)
1253 else if (output_format == FIO_OUTPUT_JSON) {
1254 root = json_create_object();
1255 json_object_add_value_string(root, "fio version", fio_version_string);
1256 array = json_create_array();
1257 json_object_add_value_array(root, "jobs", array);
1260 for (i = 0; i < nr_ts; i++) {
1261 ts = &threadstats[i];
1262 rs = &runstats[ts->groupid];
1265 fio_server_send_ts(ts, rs);
1266 else if (output_format == FIO_OUTPUT_TERSE)
1267 show_thread_status_terse(ts, rs);
1268 else if (output_format == FIO_OUTPUT_JSON) {
1269 struct json_object *tmp = show_thread_status_json(ts, rs);
1270 json_array_add_value_object(array, tmp);
1272 show_thread_status(ts, rs);
1274 if (output_format == FIO_OUTPUT_JSON) {
1275 /* disk util stats, if any */
1276 show_disk_util(1, root);
1278 json_print_object(root);
1280 json_free_object(root);
1283 for (i = 0; i < groupid + 1; i++) {
1288 fio_server_send_gs(rs);
1289 else if (output_format == FIO_OUTPUT_NORMAL)
1290 show_group_stats(rs);
1294 fio_server_send_du();
1295 else if (output_format == FIO_OUTPUT_NORMAL)
1296 show_disk_util(0, NULL);
1302 static void *__show_running_run_stats(void *arg)
1304 struct thread_data *td;
1305 unsigned long long *rt;
1309 rt = malloc(thread_number * sizeof(unsigned long long));
1310 fio_gettime(&tv, NULL);
1312 for_each_td(td, i) {
1313 rt[i] = mtime_since(&td->start, &tv);
1314 if (td_read(td) && td->io_bytes[DDIR_READ])
1315 td->ts.runtime[DDIR_READ] += rt[i];
1316 if (td_write(td) && td->io_bytes[DDIR_WRITE])
1317 td->ts.runtime[DDIR_WRITE] += rt[i];
1318 if (td_trim(td) && td->io_bytes[DDIR_TRIM])
1319 td->ts.runtime[DDIR_TRIM] += rt[i];
1321 update_rusage_stat(td);
1322 td->ts.io_bytes[DDIR_READ] = td->io_bytes[DDIR_READ];
1323 td->ts.io_bytes[DDIR_WRITE] = td->io_bytes[DDIR_WRITE];
1324 td->ts.io_bytes[DDIR_TRIM] = td->io_bytes[DDIR_TRIM];
1325 td->ts.total_run_time = mtime_since(&td->epoch, &tv);
1330 for_each_td(td, i) {
1331 if (td_read(td) && td->io_bytes[DDIR_READ])
1332 td->ts.runtime[DDIR_READ] -= rt[i];
1333 if (td_write(td) && td->io_bytes[DDIR_WRITE])
1334 td->ts.runtime[DDIR_WRITE] -= rt[i];
1335 if (td_trim(td) && td->io_bytes[DDIR_TRIM])
1336 td->ts.runtime[DDIR_TRIM] -= rt[i];
1344 * Called from signal handler. It _should_ be safe to just run this inline
1345 * in the sig handler, but we should be disturbing the system less by just
1346 * creating a thread to do it.
1348 void show_running_run_stats(void)
1352 pthread_create(&thread, NULL, __show_running_run_stats, NULL);
1353 pthread_detach(thread);
1356 static inline void add_stat_sample(struct io_stat *is, unsigned long data)
1361 if (data > is->max_val)
1363 if (data < is->min_val)
1366 delta = val - is->mean.u.f;
1368 is->mean.u.f += delta / (is->samples + 1.0);
1369 is->S.u.f += delta * (val - is->mean.u.f);
1375 static void __add_log_sample(struct io_log *iolog, unsigned long val,
1376 enum fio_ddir ddir, unsigned int bs,
1379 const int nr_samples = iolog->nr_samples;
1381 if (!iolog->nr_samples)
1382 iolog->avg_last = t;
1384 if (iolog->nr_samples == iolog->max_samples) {
1385 int new_size = sizeof(struct io_sample) * iolog->max_samples*2;
1387 iolog->log = realloc(iolog->log, new_size);
1388 iolog->max_samples <<= 1;
1391 iolog->log[nr_samples].val = val;
1392 iolog->log[nr_samples].time = t;
1393 iolog->log[nr_samples].ddir = ddir;
1394 iolog->log[nr_samples].bs = bs;
1395 iolog->nr_samples++;
1398 static inline void reset_io_stat(struct io_stat *ios)
1400 ios->max_val = ios->min_val = ios->samples = 0;
1401 ios->mean.u.f = ios->S.u.f = 0;
1404 static void add_log_sample(struct thread_data *td, struct io_log *iolog,
1405 unsigned long val, enum fio_ddir ddir,
1408 unsigned long elapsed, this_window;
1413 elapsed = mtime_since_now(&td->epoch);
1416 * If no time averaging, just add the log sample.
1418 if (!iolog->avg_msec) {
1419 __add_log_sample(iolog, val, ddir, bs, elapsed);
1424 * Add the sample. If the time period has passed, then
1425 * add that entry to the log and clear.
1427 add_stat_sample(&iolog->avg_window[ddir], val);
1430 * If period hasn't passed, adding the above sample is all we
1433 this_window = elapsed - iolog->avg_last;
1434 if (this_window < iolog->avg_msec)
1438 * Note an entry in the log. Use the mean from the logged samples,
1439 * making sure to properly round up. Only write a log entry if we
1440 * had actual samples done.
1442 if (iolog->avg_window[DDIR_READ].samples) {
1445 mr = iolog->avg_window[DDIR_READ].mean.u.f + 0.50;
1446 __add_log_sample(iolog, mr, DDIR_READ, 0, elapsed);
1448 if (iolog->avg_window[DDIR_WRITE].samples) {
1451 mw = iolog->avg_window[DDIR_WRITE].mean.u.f + 0.50;
1452 __add_log_sample(iolog, mw, DDIR_WRITE, 0, elapsed);
1454 if (iolog->avg_window[DDIR_TRIM].samples) {
1457 mw = iolog->avg_window[DDIR_TRIM].mean.u.f + 0.50;
1458 __add_log_sample(iolog, mw, DDIR_TRIM, 0, elapsed);
1462 reset_io_stat(&iolog->avg_window[DDIR_READ]);
1463 reset_io_stat(&iolog->avg_window[DDIR_WRITE]);
1464 reset_io_stat(&iolog->avg_window[DDIR_TRIM]);
1465 iolog->avg_last = elapsed;
1468 void add_agg_sample(unsigned long val, enum fio_ddir ddir, unsigned int bs)
1470 struct io_log *iolog;
1475 iolog = agg_io_log[ddir];
1476 __add_log_sample(iolog, val, ddir, bs, mtime_since_genesis());
1479 static void add_clat_percentile_sample(struct thread_stat *ts,
1480 unsigned long usec, enum fio_ddir ddir)
1482 unsigned int idx = plat_val_to_idx(usec);
1483 assert(idx < FIO_IO_U_PLAT_NR);
1485 ts->io_u_plat[ddir][idx]++;
1488 void add_clat_sample(struct thread_data *td, enum fio_ddir ddir,
1489 unsigned long usec, unsigned int bs)
1491 struct thread_stat *ts = &td->ts;
1496 add_stat_sample(&ts->clat_stat[ddir], usec);
1499 add_log_sample(td, td->clat_log, usec, ddir, bs);
1501 if (ts->clat_percentiles)
1502 add_clat_percentile_sample(ts, usec, ddir);
1505 void add_slat_sample(struct thread_data *td, enum fio_ddir ddir,
1506 unsigned long usec, unsigned int bs)
1508 struct thread_stat *ts = &td->ts;
1513 add_stat_sample(&ts->slat_stat[ddir], usec);
1516 add_log_sample(td, td->slat_log, usec, ddir, bs);
1519 void add_lat_sample(struct thread_data *td, enum fio_ddir ddir,
1520 unsigned long usec, unsigned int bs)
1522 struct thread_stat *ts = &td->ts;
1527 add_stat_sample(&ts->lat_stat[ddir], usec);
1530 add_log_sample(td, td->lat_log, usec, ddir, bs);
1533 void add_bw_sample(struct thread_data *td, enum fio_ddir ddir, unsigned int bs,
1536 struct thread_stat *ts = &td->ts;
1537 unsigned long spent, rate;
1542 spent = mtime_since(&td->bw_sample_time, t);
1543 if (spent < td->o.bw_avg_time)
1547 * Compute both read and write rates for the interval.
1549 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++) {
1552 delta = td->this_io_bytes[ddir] - td->stat_io_bytes[ddir];
1554 continue; /* No entries for interval */
1556 rate = delta * 1000 / spent / 1024;
1557 add_stat_sample(&ts->bw_stat[ddir], rate);
1560 add_log_sample(td, td->bw_log, rate, ddir, bs);
1562 td->stat_io_bytes[ddir] = td->this_io_bytes[ddir];
1565 fio_gettime(&td->bw_sample_time, NULL);
1568 void add_iops_sample(struct thread_data *td, enum fio_ddir ddir,
1571 struct thread_stat *ts = &td->ts;
1572 unsigned long spent, iops;
1577 spent = mtime_since(&td->iops_sample_time, t);
1578 if (spent < td->o.iops_avg_time)
1582 * Compute both read and write rates for the interval.
1584 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++) {
1587 delta = td->this_io_blocks[ddir] - td->stat_io_blocks[ddir];
1589 continue; /* No entries for interval */
1591 iops = (delta * 1000) / spent;
1592 add_stat_sample(&ts->iops_stat[ddir], iops);
1595 add_log_sample(td, td->iops_log, iops, ddir, 0);
1597 td->stat_io_blocks[ddir] = td->this_io_blocks[ddir];
1600 fio_gettime(&td->iops_sample_time, NULL);