12 #include "lib/ieee754.h"
15 void update_rusage_stat(struct thread_data *td)
17 struct thread_stat *ts = &td->ts;
19 getrusage(RUSAGE_SELF, &td->ru_end);
21 ts->usr_time += mtime_since(&td->ru_start.ru_utime,
22 &td->ru_end.ru_utime);
23 ts->sys_time += mtime_since(&td->ru_start.ru_stime,
24 &td->ru_end.ru_stime);
25 ts->ctx += td->ru_end.ru_nvcsw + td->ru_end.ru_nivcsw
26 - (td->ru_start.ru_nvcsw + td->ru_start.ru_nivcsw);
27 ts->minf += td->ru_end.ru_minflt - td->ru_start.ru_minflt;
28 ts->majf += td->ru_end.ru_majflt - td->ru_start.ru_majflt;
30 memcpy(&td->ru_start, &td->ru_end, sizeof(td->ru_end));
34 * Given a latency, return the index of the corresponding bucket in
35 * the structure tracking percentiles.
37 * (1) find the group (and error bits) that the value (latency)
38 * belongs to by looking at its MSB. (2) find the bucket number in the
39 * group by looking at the index bits.
42 static unsigned int plat_val_to_idx(unsigned int val)
44 unsigned int msb, error_bits, base, offset, idx;
46 /* Find MSB starting from bit 0 */
50 msb = (sizeof(val)*8) - __builtin_clz(val) - 1;
53 * MSB <= (FIO_IO_U_PLAT_BITS-1), cannot be rounded off. Use
54 * all bits of the sample as index
56 if (msb <= FIO_IO_U_PLAT_BITS)
59 /* Compute the number of error bits to discard*/
60 error_bits = msb - FIO_IO_U_PLAT_BITS;
62 /* Compute the number of buckets before the group */
63 base = (error_bits + 1) << FIO_IO_U_PLAT_BITS;
66 * Discard the error bits and apply the mask to find the
67 * index for the buckets in the group
69 offset = (FIO_IO_U_PLAT_VAL - 1) & (val >> error_bits);
71 /* Make sure the index does not exceed (array size - 1) */
72 idx = (base + offset) < (FIO_IO_U_PLAT_NR - 1) ?
73 (base + offset) : (FIO_IO_U_PLAT_NR - 1);
79 * Convert the given index of the bucket array to the value
80 * represented by the bucket
82 static unsigned int plat_idx_to_val(unsigned int idx)
84 unsigned int error_bits, k, base;
86 assert(idx < FIO_IO_U_PLAT_NR);
88 /* MSB <= (FIO_IO_U_PLAT_BITS-1), cannot be rounded off. Use
89 * all bits of the sample as index */
90 if (idx < (FIO_IO_U_PLAT_VAL << 1))
93 /* Find the group and compute the minimum value of that group */
94 error_bits = (idx >> FIO_IO_U_PLAT_BITS) - 1;
95 base = 1 << (error_bits + FIO_IO_U_PLAT_BITS);
97 /* Find its bucket number of the group */
98 k = idx % FIO_IO_U_PLAT_VAL;
100 /* Return the mean of the range of the bucket */
101 return base + ((k + 0.5) * (1 << error_bits));
104 static int double_cmp(const void *a, const void *b)
106 const fio_fp64_t fa = *(const fio_fp64_t *) a;
107 const fio_fp64_t fb = *(const fio_fp64_t *) b;
112 else if (fa.u.f < fb.u.f)
118 unsigned int calc_clat_percentiles(unsigned int *io_u_plat, unsigned long nr,
119 fio_fp64_t *plist, unsigned int **output,
120 unsigned int *maxv, unsigned int *minv)
122 unsigned long sum = 0;
123 unsigned int len, i, j = 0;
124 unsigned int oval_len = 0;
125 unsigned int *ovals = NULL;
132 while (len < FIO_IO_U_LIST_MAX_LEN && plist[len].u.f != 0.0)
139 * Sort the percentile list. Note that it may already be sorted if
140 * we are using the default values, but since it's a short list this
141 * isn't a worry. Also note that this does not work for NaN values.
144 qsort((void *)plist, len, sizeof(plist[0]), double_cmp);
147 * Calculate bucket values, note down max and min values
150 for (i = 0; i < FIO_IO_U_PLAT_NR && !is_last; i++) {
152 while (sum >= (plist[j].u.f / 100.0 * nr)) {
153 assert(plist[j].u.f <= 100.0);
157 ovals = realloc(ovals, oval_len * sizeof(unsigned int));
160 ovals[j] = plat_idx_to_val(i);
161 if (ovals[j] < *minv)
163 if (ovals[j] > *maxv)
166 is_last = (j == len - 1);
179 * Find and display the p-th percentile of clat
181 static void show_clat_percentiles(unsigned int *io_u_plat, unsigned long nr,
184 unsigned int len, j = 0, minv, maxv;
186 int is_last, scale_down;
188 len = calc_clat_percentiles(io_u_plat, nr, plist, &ovals, &maxv, &minv);
193 * We default to usecs, but if the value range is such that we
194 * should scale down to msecs, do that.
196 if (minv > 2000 && maxv > 99999) {
198 log_info(" clat percentiles (msec):\n |");
201 log_info(" clat percentiles (usec):\n |");
204 for (j = 0; j < len; j++) {
208 if (j != 0 && (j % 4) == 0)
211 /* end of the list */
212 is_last = (j == len - 1);
214 if (plist[j].u.f < 10.0)
215 sprintf(fbuf, " %2.2f", plist[j].u.f);
217 sprintf(fbuf, "%2.2f", plist[j].u.f);
220 ovals[j] = (ovals[j] + 999) / 1000;
222 log_info(" %sth=[%5u]%c", fbuf, ovals[j], is_last ? '\n' : ',');
227 if (j % 4 == 3) /* for formatting */
236 int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max,
237 double *mean, double *dev)
239 double n = is->samples;
241 if (is->samples == 0)
247 n = (double) is->samples;
248 *mean = is->mean.u.f;
251 *dev = sqrt(is->S.u.f / (n - 1.0));
258 void show_group_stats(struct group_run_stats *rs)
260 char *p1, *p2, *p3, *p4;
261 const char *ddir_str[] = { " READ", " WRITE" , " TRIM"};
264 log_info("\nRun status group %d (all jobs):\n", rs->groupid);
266 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
267 const int i2p = is_power_of_2(rs->kb_base);
272 p1 = num2str(rs->io_kb[i], 6, rs->kb_base, i2p);
273 p2 = num2str(rs->agg[i], 6, rs->kb_base, i2p);
274 p3 = num2str(rs->min_bw[i], 6, rs->kb_base, i2p);
275 p4 = num2str(rs->max_bw[i], 6, rs->kb_base, i2p);
277 log_info("%s: io=%sB, aggrb=%sB/s, minb=%sB/s, maxb=%sB/s,"
278 " mint=%llumsec, maxt=%llumsec\n", ddir_str[i], p1, p2,
279 p3, p4, rs->min_run[i],
289 void stat_calc_dist(unsigned int *map, unsigned long total, double *io_u_dist)
294 * Do depth distribution calculations
296 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
298 io_u_dist[i] = (double) map[i] / (double) total;
299 io_u_dist[i] *= 100.0;
300 if (io_u_dist[i] < 0.1 && map[i])
307 static void stat_calc_lat(struct thread_stat *ts, double *dst,
308 unsigned int *src, int nr)
310 unsigned long total = ddir_rw_sum(ts->total_io_u);
314 * Do latency distribution calculations
316 for (i = 0; i < nr; i++) {
318 dst[i] = (double) src[i] / (double) total;
320 if (dst[i] < 0.01 && src[i])
327 void stat_calc_lat_u(struct thread_stat *ts, double *io_u_lat)
329 stat_calc_lat(ts, io_u_lat, ts->io_u_lat_u, FIO_IO_U_LAT_U_NR);
332 void stat_calc_lat_m(struct thread_stat *ts, double *io_u_lat)
334 stat_calc_lat(ts, io_u_lat, ts->io_u_lat_m, FIO_IO_U_LAT_M_NR);
337 static void display_lat(const char *name, unsigned long min, unsigned long max,
338 double mean, double dev)
340 const char *base = "(usec)";
343 if (!usec_to_msec(&min, &max, &mean, &dev))
346 minp = num2str(min, 6, 1, 0);
347 maxp = num2str(max, 6, 1, 0);
349 log_info(" %s %s: min=%s, max=%s, avg=%5.02f,"
350 " stdev=%5.02f\n", name, base, minp, maxp, mean, dev);
356 static void show_ddir_status(struct group_run_stats *rs, struct thread_stat *ts,
359 const char *ddir_str[] = { "read ", "write", "trim" };
360 unsigned long min, max, runt;
361 unsigned long long bw, iops;
363 char *io_p, *bw_p, *iops_p;
366 assert(ddir_rw(ddir));
368 if (!ts->runtime[ddir])
371 i2p = is_power_of_2(rs->kb_base);
372 runt = ts->runtime[ddir];
374 bw = (1000 * ts->io_bytes[ddir]) / runt;
375 io_p = num2str(ts->io_bytes[ddir], 6, 1, i2p);
376 bw_p = num2str(bw, 6, 1, i2p);
378 iops = (1000 * (uint64_t)ts->total_io_u[ddir]) / runt;
379 iops_p = num2str(iops, 6, 1, 0);
381 log_info(" %s: io=%sB, bw=%sB/s, iops=%s, runt=%6llumsec\n",
382 ddir_str[ddir], io_p, bw_p, iops_p,
389 if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev))
390 display_lat("slat", min, max, mean, dev);
391 if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev))
392 display_lat("clat", min, max, mean, dev);
393 if (calc_lat(&ts->lat_stat[ddir], &min, &max, &mean, &dev))
394 display_lat(" lat", min, max, mean, dev);
396 if (ts->clat_percentiles) {
397 show_clat_percentiles(ts->io_u_plat[ddir],
398 ts->clat_stat[ddir].samples,
399 ts->percentile_list);
401 if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
402 double p_of_agg = 100.0;
403 const char *bw_str = "KB";
406 p_of_agg = mean * 100 / (double) rs->agg[ddir];
407 if (p_of_agg > 100.0)
411 if (mean > 999999.9) {
419 log_info(" bw (%s/s) : min=%5lu, max=%5lu, per=%3.2f%%,"
420 " avg=%5.02f, stdev=%5.02f\n", bw_str, min, max,
421 p_of_agg, mean, dev);
425 static int show_lat(double *io_u_lat, int nr, const char **ranges,
428 int new_line = 1, i, line = 0, shown = 0;
430 for (i = 0; i < nr; i++) {
431 if (io_u_lat[i] <= 0.0)
437 log_info(" lat (%s) : ", msg);
443 log_info("%s%3.2f%%", ranges[i], io_u_lat[i]);
455 static void show_lat_u(double *io_u_lat_u)
457 const char *ranges[] = { "2=", "4=", "10=", "20=", "50=", "100=",
458 "250=", "500=", "750=", "1000=", };
460 show_lat(io_u_lat_u, FIO_IO_U_LAT_U_NR, ranges, "usec");
463 static void show_lat_m(double *io_u_lat_m)
465 const char *ranges[] = { "2=", "4=", "10=", "20=", "50=", "100=",
466 "250=", "500=", "750=", "1000=", "2000=",
469 show_lat(io_u_lat_m, FIO_IO_U_LAT_M_NR, ranges, "msec");
472 static void show_latencies(struct thread_stat *ts)
474 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
475 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
477 stat_calc_lat_u(ts, io_u_lat_u);
478 stat_calc_lat_m(ts, io_u_lat_m);
480 show_lat_u(io_u_lat_u);
481 show_lat_m(io_u_lat_m);
484 void show_thread_status(struct thread_stat *ts, struct group_run_stats *rs)
486 double usr_cpu, sys_cpu;
487 unsigned long runtime;
488 double io_u_dist[FIO_IO_U_MAP_NR];
492 if (!(ts->io_bytes[DDIR_READ] + ts->io_bytes[DDIR_WRITE] +
493 ts->io_bytes[DDIR_TRIM]) && !(ts->total_io_u[DDIR_READ] +
494 ts->total_io_u[DDIR_WRITE] + ts->total_io_u[DDIR_TRIM]))
498 ctime_r((const time_t *) &time_p, time_buf);
501 log_info("%s: (groupid=%d, jobs=%d): err=%2d: pid=%d: %s",
502 ts->name, ts->groupid, ts->members,
503 ts->error, (int) ts->pid, time_buf);
505 log_info("%s: (groupid=%d, jobs=%d): err=%2d (%s): pid=%d: %s",
506 ts->name, ts->groupid, ts->members,
507 ts->error, ts->verror, (int) ts->pid,
511 if (strlen(ts->description))
512 log_info(" Description : [%s]\n", ts->description);
514 if (ts->io_bytes[DDIR_READ])
515 show_ddir_status(rs, ts, DDIR_READ);
516 if (ts->io_bytes[DDIR_WRITE])
517 show_ddir_status(rs, ts, DDIR_WRITE);
518 if (ts->io_bytes[DDIR_TRIM])
519 show_ddir_status(rs, ts, DDIR_TRIM);
523 runtime = ts->total_run_time;
525 double runt = (double) runtime;
527 usr_cpu = (double) ts->usr_time * 100 / runt;
528 sys_cpu = (double) ts->sys_time * 100 / runt;
534 log_info(" cpu : usr=%3.2f%%, sys=%3.2f%%, ctx=%lu, majf=%lu,"
535 " minf=%lu\n", usr_cpu, sys_cpu, ts->ctx, ts->majf, ts->minf);
537 stat_calc_dist(ts->io_u_map, ddir_rw_sum(ts->total_io_u), io_u_dist);
538 log_info(" IO depths : 1=%3.1f%%, 2=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%,"
539 " 16=%3.1f%%, 32=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
540 io_u_dist[1], io_u_dist[2],
541 io_u_dist[3], io_u_dist[4],
542 io_u_dist[5], io_u_dist[6]);
544 stat_calc_dist(ts->io_u_submit, ts->total_submit, io_u_dist);
545 log_info(" submit : 0=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%, 16=%3.1f%%,"
546 " 32=%3.1f%%, 64=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
547 io_u_dist[1], io_u_dist[2],
548 io_u_dist[3], io_u_dist[4],
549 io_u_dist[5], io_u_dist[6]);
550 stat_calc_dist(ts->io_u_complete, ts->total_complete, io_u_dist);
551 log_info(" complete : 0=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%, 16=%3.1f%%,"
552 " 32=%3.1f%%, 64=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
553 io_u_dist[1], io_u_dist[2],
554 io_u_dist[3], io_u_dist[4],
555 io_u_dist[5], io_u_dist[6]);
556 log_info(" issued : total=r=%lu/w=%lu/d=%lu,"
557 " short=r=%lu/w=%lu/d=%lu\n",
558 ts->total_io_u[0], ts->total_io_u[1],
560 ts->short_io_u[0], ts->short_io_u[1],
562 if (ts->continue_on_error) {
563 log_info(" errors : total=%lu, first_error=%d/<%s>\n",
566 strerror(ts->first_error));
570 static void show_ddir_status_terse(struct thread_stat *ts,
571 struct group_run_stats *rs, int ddir)
573 unsigned long min, max;
574 unsigned long long bw, iops;
575 unsigned int *ovals = NULL;
577 unsigned int len, minv, maxv;
580 assert(ddir_rw(ddir));
583 if (ts->runtime[ddir]) {
584 uint64_t runt = ts->runtime[ddir];
586 bw = ((1000 * ts->io_bytes[ddir]) / runt) / 1024;
587 iops = (1000 * (uint64_t) ts->total_io_u[ddir]) / runt;
590 log_info(";%llu;%llu;%llu;%llu", ts->io_bytes[ddir] >> 10, bw, iops,
593 if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev))
594 log_info(";%lu;%lu;%f;%f", min, max, mean, dev);
596 log_info(";%lu;%lu;%f;%f", 0UL, 0UL, 0.0, 0.0);
598 if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev))
599 log_info(";%lu;%lu;%f;%f", min, max, mean, dev);
601 log_info(";%lu;%lu;%f;%f", 0UL, 0UL, 0.0, 0.0);
603 if (ts->clat_percentiles) {
604 len = calc_clat_percentiles(ts->io_u_plat[ddir],
605 ts->clat_stat[ddir].samples,
606 ts->percentile_list, &ovals, &maxv,
611 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
616 log_info(";%2.2f%%=%u", ts->percentile_list[i].u.f, ovals[i]);
619 if (calc_lat(&ts->lat_stat[ddir], &min, &max, &mean, &dev))
620 log_info(";%lu;%lu;%f;%f", min, max, mean, dev);
622 log_info(";%lu;%lu;%f;%f", 0UL, 0UL, 0.0, 0.0);
627 if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
628 double p_of_agg = 100.0;
631 p_of_agg = mean * 100 / (double) rs->agg[ddir];
632 if (p_of_agg > 100.0)
636 log_info(";%lu;%lu;%f%%;%f;%f", min, max, p_of_agg, mean, dev);
638 log_info(";%lu;%lu;%f%%;%f;%f", 0UL, 0UL, 0.0, 0.0, 0.0);
641 static void add_ddir_status_json(struct thread_stat *ts,
642 struct group_run_stats *rs, int ddir, struct json_object *parent)
644 unsigned long min, max;
645 unsigned long long bw, iops;
646 unsigned int *ovals = NULL;
648 unsigned int len, minv, maxv;
650 const char *ddirname[] = {"read", "write", "trim"};
651 struct json_object *dir_object, *tmp_object, *percentile_object;
653 double p_of_agg = 100.0;
655 assert(ddir_rw(ddir));
657 dir_object = json_create_object();
658 json_object_add_value_object(parent, ddirname[ddir], dir_object);
661 if (ts->runtime[ddir]) {
662 uint64_t runt = ts->runtime[ddir];
664 bw = ((1000 * ts->io_bytes[ddir]) / runt) / 1024;
665 iops = (1000 * (uint64_t) ts->total_io_u[ddir]) / runt;
668 json_object_add_value_int(dir_object, "io_bytes", ts->io_bytes[ddir] >> 10);
669 json_object_add_value_int(dir_object, "bw", bw);
670 json_object_add_value_int(dir_object, "iops", iops);
671 json_object_add_value_int(dir_object, "runtime", ts->runtime[ddir]);
673 if (!calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev)) {
677 tmp_object = json_create_object();
678 json_object_add_value_object(dir_object, "slat", tmp_object);
679 json_object_add_value_int(tmp_object, "min", min);
680 json_object_add_value_int(tmp_object, "max", max);
681 json_object_add_value_float(tmp_object, "mean", mean);
682 json_object_add_value_float(tmp_object, "stddev", dev);
684 if (!calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev)) {
688 tmp_object = json_create_object();
689 json_object_add_value_object(dir_object, "clat", tmp_object);
690 json_object_add_value_int(tmp_object, "min", min);
691 json_object_add_value_int(tmp_object, "max", max);
692 json_object_add_value_float(tmp_object, "mean", mean);
693 json_object_add_value_float(tmp_object, "stddev", dev);
695 if (ts->clat_percentiles) {
696 len = calc_clat_percentiles(ts->io_u_plat[ddir],
697 ts->clat_stat[ddir].samples,
698 ts->percentile_list, &ovals, &maxv,
703 percentile_object = json_create_object();
704 json_object_add_value_object(tmp_object, "percentile", percentile_object);
705 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
707 json_object_add_value_int(percentile_object, "0.00", 0);
710 snprintf(buf, sizeof(buf) - 1, "%2.2f", ts->percentile_list[i].u.f);
711 json_object_add_value_int(percentile_object, (const char *)buf, ovals[i]);
714 if (!calc_lat(&ts->lat_stat[ddir], &min, &max, &mean, &dev)) {
718 tmp_object = json_create_object();
719 json_object_add_value_object(dir_object, "lat", tmp_object);
720 json_object_add_value_int(tmp_object, "min", min);
721 json_object_add_value_int(tmp_object, "max", max);
722 json_object_add_value_float(tmp_object, "mean", mean);
723 json_object_add_value_float(tmp_object, "stddev", dev);
727 if (!calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
729 p_of_agg = mean * 100 / (double) rs->agg[ddir];
730 if (p_of_agg > 100.0)
735 p_of_agg = mean = dev = 0.0;
737 json_object_add_value_int(dir_object, "bw_min", min);
738 json_object_add_value_int(dir_object, "bw_max", max);
739 json_object_add_value_float(dir_object, "bw_agg", mean);
740 json_object_add_value_float(dir_object, "bw_mean", mean);
741 json_object_add_value_float(dir_object, "bw_dev", dev);
744 static void show_thread_status_terse_v2(struct thread_stat *ts,
745 struct group_run_stats *rs)
747 double io_u_dist[FIO_IO_U_MAP_NR];
748 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
749 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
750 double usr_cpu, sys_cpu;
754 log_info("2;%s;%d;%d", ts->name, ts->groupid, ts->error);
755 /* Log Read Status */
756 show_ddir_status_terse(ts, rs, DDIR_READ);
757 /* Log Write Status */
758 show_ddir_status_terse(ts, rs, DDIR_WRITE);
759 /* Log Trim Status */
760 show_ddir_status_terse(ts, rs, DDIR_TRIM);
763 if (ts->total_run_time) {
764 double runt = (double) ts->total_run_time;
766 usr_cpu = (double) ts->usr_time * 100 / runt;
767 sys_cpu = (double) ts->sys_time * 100 / runt;
773 log_info(";%f%%;%f%%;%lu;%lu;%lu", usr_cpu, sys_cpu, ts->ctx, ts->majf,
776 /* Calc % distribution of IO depths, usecond, msecond latency */
777 stat_calc_dist(ts->io_u_map, ddir_rw_sum(ts->total_io_u), io_u_dist);
778 stat_calc_lat_u(ts, io_u_lat_u);
779 stat_calc_lat_m(ts, io_u_lat_m);
781 /* Only show fixed 7 I/O depth levels*/
782 log_info(";%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%",
783 io_u_dist[0], io_u_dist[1], io_u_dist[2], io_u_dist[3],
784 io_u_dist[4], io_u_dist[5], io_u_dist[6]);
786 /* Microsecond latency */
787 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
788 log_info(";%3.2f%%", io_u_lat_u[i]);
789 /* Millisecond latency */
790 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
791 log_info(";%3.2f%%", io_u_lat_m[i]);
792 /* Additional output if continue_on_error set - default off*/
793 if (ts->continue_on_error)
794 log_info(";%lu;%d", ts->total_err_count, ts->first_error);
797 /* Additional output if description is set */
799 log_info(";%s", ts->description);
804 static void show_thread_status_terse_v3_v4(struct thread_stat *ts,
805 struct group_run_stats *rs, int ver)
807 double io_u_dist[FIO_IO_U_MAP_NR];
808 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
809 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
810 double usr_cpu, sys_cpu;
814 log_info("%d;%s;%s;%d;%d", ver, fio_version_string,
815 ts->name, ts->groupid, ts->error);
816 /* Log Read Status */
817 show_ddir_status_terse(ts, rs, DDIR_READ);
818 /* Log Write Status */
819 show_ddir_status_terse(ts, rs, DDIR_WRITE);
820 /* Log Trim Status */
822 show_ddir_status_terse(ts, rs, DDIR_TRIM);
825 if (ts->total_run_time) {
826 double runt = (double) ts->total_run_time;
828 usr_cpu = (double) ts->usr_time * 100 / runt;
829 sys_cpu = (double) ts->sys_time * 100 / runt;
835 log_info(";%f%%;%f%%;%lu;%lu;%lu", usr_cpu, sys_cpu, ts->ctx, ts->majf,
838 /* Calc % distribution of IO depths, usecond, msecond latency */
839 stat_calc_dist(ts->io_u_map, ddir_rw_sum(ts->total_io_u), io_u_dist);
840 stat_calc_lat_u(ts, io_u_lat_u);
841 stat_calc_lat_m(ts, io_u_lat_m);
843 /* Only show fixed 7 I/O depth levels*/
844 log_info(";%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%",
845 io_u_dist[0], io_u_dist[1], io_u_dist[2], io_u_dist[3],
846 io_u_dist[4], io_u_dist[5], io_u_dist[6]);
848 /* Microsecond latency */
849 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
850 log_info(";%3.2f%%", io_u_lat_u[i]);
851 /* Millisecond latency */
852 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
853 log_info(";%3.2f%%", io_u_lat_m[i]);
855 /* disk util stats, if any */
856 show_disk_util(1, NULL);
858 /* Additional output if continue_on_error set - default off*/
859 if (ts->continue_on_error)
860 log_info(";%lu;%d", ts->total_err_count, ts->first_error);
862 /* Additional output if description is set */
863 if (strlen(ts->description))
864 log_info(";%s", ts->description);
869 static struct json_object *show_thread_status_json(struct thread_stat *ts,
870 struct group_run_stats *rs)
872 struct json_object *root, *tmp;
873 double io_u_dist[FIO_IO_U_MAP_NR];
874 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
875 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
876 double usr_cpu, sys_cpu;
879 root = json_create_object();
880 json_object_add_value_string(root, "jobname", ts->name);
881 json_object_add_value_int(root, "groupid", ts->groupid);
882 json_object_add_value_int(root, "error", ts->error);
884 add_ddir_status_json(ts, rs, DDIR_READ, root);
885 add_ddir_status_json(ts, rs, DDIR_WRITE, root);
886 add_ddir_status_json(ts, rs, DDIR_TRIM, root);
889 if (ts->total_run_time) {
890 double runt = (double) ts->total_run_time;
892 usr_cpu = (double) ts->usr_time * 100 / runt;
893 sys_cpu = (double) ts->sys_time * 100 / runt;
898 json_object_add_value_float(root, "usr_cpu", usr_cpu);
899 json_object_add_value_float(root, "sys_cpu", sys_cpu);
900 json_object_add_value_int(root, "ctx", ts->ctx);
901 json_object_add_value_int(root, "majf", ts->majf);
902 json_object_add_value_int(root, "minf", ts->minf);
905 /* Calc % distribution of IO depths, usecond, msecond latency */
906 stat_calc_dist(ts->io_u_map, ddir_rw_sum(ts->total_io_u), io_u_dist);
907 stat_calc_lat_u(ts, io_u_lat_u);
908 stat_calc_lat_m(ts, io_u_lat_m);
910 tmp = json_create_object();
911 json_object_add_value_object(root, "iodepth_level", tmp);
912 /* Only show fixed 7 I/O depth levels*/
913 for (i = 0; i < 7; i++) {
916 snprintf(name, 19, "%d", 1 << i);
918 snprintf(name, 19, ">=%d", 1 << i);
919 json_object_add_value_float(tmp, (const char *)name, io_u_dist[i]);
922 tmp = json_create_object();
923 json_object_add_value_object(root, "latency_us", tmp);
924 /* Microsecond latency */
925 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
926 const char *ranges[] = { "2", "4", "10", "20", "50", "100",
927 "250", "500", "750", "1000", };
928 json_object_add_value_float(tmp, ranges[i], io_u_lat_u[i]);
930 /* Millisecond latency */
931 tmp = json_create_object();
932 json_object_add_value_object(root, "latency_ms", tmp);
933 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++) {
934 const char *ranges[] = { "2", "4", "10", "20", "50", "100",
935 "250", "500", "750", "1000", "2000",
937 json_object_add_value_float(tmp, ranges[i], io_u_lat_m[i]);
940 /* Additional output if continue_on_error set - default off*/
941 if (ts->continue_on_error) {
942 json_object_add_value_int(root, "total_err", ts->total_err_count);
943 json_object_add_value_int(root, "total_err", ts->first_error);
946 /* Additional output if description is set */
947 if (strlen(ts->description))
948 json_object_add_value_string(root, "desc", ts->description);
953 static void show_thread_status_terse(struct thread_stat *ts,
954 struct group_run_stats *rs)
956 if (terse_version == 2)
957 show_thread_status_terse_v2(ts, rs);
958 else if (terse_version == 3 || terse_version == 4)
959 show_thread_status_terse_v3_v4(ts, rs, terse_version);
961 log_err("fio: bad terse version!? %d\n", terse_version);
964 static void sum_stat(struct io_stat *dst, struct io_stat *src, int nr)
968 if (src->samples == 0)
971 dst->min_val = min(dst->min_val, src->min_val);
972 dst->max_val = max(dst->max_val, src->max_val);
975 * Compute new mean and S after the merge
976 * <http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
977 * #Parallel_algorithm>
980 mean = src->mean.u.f;
983 double delta = src->mean.u.f - dst->mean.u.f;
985 mean = ((src->mean.u.f * src->samples) +
986 (dst->mean.u.f * dst->samples)) /
987 (dst->samples + src->samples);
989 S = src->S.u.f + dst->S.u.f + pow(delta, 2.0) *
990 (dst->samples * src->samples) /
991 (dst->samples + src->samples);
994 dst->samples += src->samples;
995 dst->mean.u.f = mean;
999 void sum_group_stats(struct group_run_stats *dst, struct group_run_stats *src)
1003 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1004 if (dst->max_run[i] < src->max_run[i])
1005 dst->max_run[i] = src->max_run[i];
1006 if (dst->min_run[i] && dst->min_run[i] > src->min_run[i])
1007 dst->min_run[i] = src->min_run[i];
1008 if (dst->max_bw[i] < src->max_bw[i])
1009 dst->max_bw[i] = src->max_bw[i];
1010 if (dst->min_bw[i] && dst->min_bw[i] > src->min_bw[i])
1011 dst->min_bw[i] = src->min_bw[i];
1013 dst->io_kb[i] += src->io_kb[i];
1014 dst->agg[i] += src->agg[i];
1019 void sum_thread_stats(struct thread_stat *dst, struct thread_stat *src, int nr)
1023 for (l = 0; l < DDIR_RWDIR_CNT; l++) {
1024 sum_stat(&dst->clat_stat[l], &src->clat_stat[l], nr);
1025 sum_stat(&dst->slat_stat[l], &src->slat_stat[l], nr);
1026 sum_stat(&dst->lat_stat[l], &src->lat_stat[l], nr);
1027 sum_stat(&dst->bw_stat[l], &src->bw_stat[l], nr);
1029 dst->io_bytes[l] += src->io_bytes[l];
1031 if (dst->runtime[l] < src->runtime[l])
1032 dst->runtime[l] = src->runtime[l];
1035 dst->usr_time += src->usr_time;
1036 dst->sys_time += src->sys_time;
1037 dst->ctx += src->ctx;
1038 dst->majf += src->majf;
1039 dst->minf += src->minf;
1041 for (k = 0; k < FIO_IO_U_MAP_NR; k++)
1042 dst->io_u_map[k] += src->io_u_map[k];
1043 for (k = 0; k < FIO_IO_U_MAP_NR; k++)
1044 dst->io_u_submit[k] += src->io_u_submit[k];
1045 for (k = 0; k < FIO_IO_U_MAP_NR; k++)
1046 dst->io_u_complete[k] += src->io_u_complete[k];
1047 for (k = 0; k < FIO_IO_U_LAT_U_NR; k++)
1048 dst->io_u_lat_u[k] += src->io_u_lat_u[k];
1049 for (k = 0; k < FIO_IO_U_LAT_M_NR; k++)
1050 dst->io_u_lat_m[k] += src->io_u_lat_m[k];
1052 for (k = 0; k < DDIR_RWDIR_CNT; k++) {
1053 dst->total_io_u[k] += src->total_io_u[k];
1054 dst->short_io_u[k] += src->short_io_u[k];
1057 for (k = 0; k < DDIR_RWDIR_CNT; k++) {
1059 for (m = 0; m < FIO_IO_U_PLAT_NR; m++)
1060 dst->io_u_plat[k][m] += src->io_u_plat[k][m];
1063 dst->total_run_time += src->total_run_time;
1064 dst->total_submit += src->total_submit;
1065 dst->total_complete += src->total_complete;
1068 void init_group_run_stat(struct group_run_stats *gs)
1071 memset(gs, 0, sizeof(*gs));
1073 for (i = 0; i < DDIR_RWDIR_CNT; i++)
1074 gs->min_bw[i] = gs->min_run[i] = ~0UL;
1077 void init_thread_stat(struct thread_stat *ts)
1081 memset(ts, 0, sizeof(*ts));
1083 for (j = 0; j < DDIR_RWDIR_CNT; j++) {
1084 ts->lat_stat[j].min_val = -1UL;
1085 ts->clat_stat[j].min_val = -1UL;
1086 ts->slat_stat[j].min_val = -1UL;
1087 ts->bw_stat[j].min_val = -1UL;
1092 void show_run_stats(void)
1094 struct group_run_stats *runstats, *rs;
1095 struct thread_data *td;
1096 struct thread_stat *threadstats, *ts;
1097 int i, j, nr_ts, last_ts, idx;
1098 int kb_base_warned = 0;
1099 struct json_object *root = NULL;
1100 struct json_array *array = NULL;
1102 runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1));
1104 for (i = 0; i < groupid + 1; i++)
1105 init_group_run_stat(&runstats[i]);
1108 * find out how many threads stats we need. if group reporting isn't
1109 * enabled, it's one-per-td.
1113 for_each_td(td, i) {
1114 if (!td->o.group_reporting) {
1118 if (last_ts == td->groupid)
1121 last_ts = td->groupid;
1125 threadstats = malloc(nr_ts * sizeof(struct thread_stat));
1127 for (i = 0; i < nr_ts; i++)
1128 init_thread_stat(&threadstats[i]);
1133 for_each_td(td, i) {
1134 if (idx && (!td->o.group_reporting ||
1135 (td->o.group_reporting && last_ts != td->groupid))) {
1140 last_ts = td->groupid;
1142 ts = &threadstats[j];
1144 ts->clat_percentiles = td->o.clat_percentiles;
1145 if (td->o.overwrite_plist)
1146 memcpy(ts->percentile_list, td->o.percentile_list, sizeof(td->o.percentile_list));
1148 memcpy(ts->percentile_list, def_percentile_list, sizeof(def_percentile_list));
1153 if (ts->groupid == -1) {
1155 * These are per-group shared already
1157 strncpy(ts->name, td->o.name, FIO_JOBNAME_SIZE);
1158 if (td->o.description)
1159 strncpy(ts->description, td->o.description,
1162 memset(ts->description, 0, FIO_JOBNAME_SIZE);
1165 * If multiple entries in this group, this is
1168 ts->thread_number = td->thread_number;
1169 ts->groupid = td->groupid;
1172 * first pid in group, not very useful...
1176 ts->kb_base = td->o.kb_base;
1177 } else if (ts->kb_base != td->o.kb_base && !kb_base_warned) {
1178 log_info("fio: kb_base differs for jobs in group, using"
1179 " %u as the base\n", ts->kb_base);
1183 ts->continue_on_error = td->o.continue_on_error;
1184 ts->total_err_count += td->total_err_count;
1185 ts->first_error = td->first_error;
1187 if (!td->error && td->o.continue_on_error &&
1189 ts->error = td->first_error;
1190 strcpy(ts->verror, td->verror);
1191 } else if (td->error) {
1192 ts->error = td->error;
1193 strcpy(ts->verror, td->verror);
1197 sum_thread_stats(ts, &td->ts, idx);
1200 for (i = 0; i < nr_ts; i++) {
1201 unsigned long long bw;
1203 ts = &threadstats[i];
1204 rs = &runstats[ts->groupid];
1205 rs->kb_base = ts->kb_base;
1207 for (j = 0; j < DDIR_RWDIR_CNT; j++) {
1208 if (!ts->runtime[j])
1210 if (ts->runtime[j] < rs->min_run[j] || !rs->min_run[j])
1211 rs->min_run[j] = ts->runtime[j];
1212 if (ts->runtime[j] > rs->max_run[j])
1213 rs->max_run[j] = ts->runtime[j];
1216 if (ts->runtime[j]) {
1217 unsigned long runt = ts->runtime[j];
1218 unsigned long long kb;
1220 kb = ts->io_bytes[j] / rs->kb_base;
1221 bw = kb * 1000 / runt;
1223 if (bw < rs->min_bw[j])
1225 if (bw > rs->max_bw[j])
1228 rs->io_kb[j] += ts->io_bytes[j] / rs->kb_base;
1232 for (i = 0; i < groupid + 1; i++) {
1237 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) {
1238 if (rs->max_run[ddir])
1239 rs->agg[ddir] = (rs->io_kb[ddir] * 1000) /
1245 * don't overwrite last signal output
1247 if (output_format == FIO_OUTPUT_NORMAL)
1249 else if (output_format == FIO_OUTPUT_JSON) {
1250 root = json_create_object();
1251 json_object_add_value_string(root, "fio version", fio_version_string);
1252 array = json_create_array();
1253 json_object_add_value_array(root, "jobs", array);
1256 for (i = 0; i < nr_ts; i++) {
1257 ts = &threadstats[i];
1258 rs = &runstats[ts->groupid];
1261 fio_server_send_ts(ts, rs);
1262 else if (output_format == FIO_OUTPUT_TERSE)
1263 show_thread_status_terse(ts, rs);
1264 else if (output_format == FIO_OUTPUT_JSON) {
1265 struct json_object *tmp = show_thread_status_json(ts, rs);
1266 json_array_add_value_object(array, tmp);
1268 show_thread_status(ts, rs);
1270 if (output_format == FIO_OUTPUT_JSON) {
1271 /* disk util stats, if any */
1272 show_disk_util(1, root);
1274 json_print_object(root);
1276 json_free_object(root);
1279 for (i = 0; i < groupid + 1; i++) {
1284 fio_server_send_gs(rs);
1285 else if (output_format == FIO_OUTPUT_NORMAL)
1286 show_group_stats(rs);
1290 fio_server_send_du();
1291 else if (output_format == FIO_OUTPUT_NORMAL)
1292 show_disk_util(0, NULL);
1298 static void *__show_running_run_stats(void *arg)
1300 struct thread_data *td;
1301 unsigned long long *rt;
1305 rt = malloc(thread_number * sizeof(unsigned long long));
1306 fio_gettime(&tv, NULL);
1308 for_each_td(td, i) {
1309 rt[i] = mtime_since(&td->start, &tv);
1310 if (td_read(td) && td->io_bytes[DDIR_READ])
1311 td->ts.runtime[DDIR_READ] += rt[i];
1312 if (td_write(td) && td->io_bytes[DDIR_WRITE])
1313 td->ts.runtime[DDIR_WRITE] += rt[i];
1314 if (td_trim(td) && td->io_bytes[DDIR_TRIM])
1315 td->ts.runtime[DDIR_TRIM] += rt[i];
1317 update_rusage_stat(td);
1318 td->ts.io_bytes[DDIR_READ] = td->io_bytes[DDIR_READ];
1319 td->ts.io_bytes[DDIR_WRITE] = td->io_bytes[DDIR_WRITE];
1320 td->ts.io_bytes[DDIR_TRIM] = td->io_bytes[DDIR_TRIM];
1321 td->ts.total_run_time = mtime_since(&td->epoch, &tv);
1326 for_each_td(td, i) {
1327 if (td_read(td) && td->io_bytes[DDIR_READ])
1328 td->ts.runtime[DDIR_READ] -= rt[i];
1329 if (td_write(td) && td->io_bytes[DDIR_WRITE])
1330 td->ts.runtime[DDIR_WRITE] -= rt[i];
1331 if (td_trim(td) && td->io_bytes[DDIR_TRIM])
1332 td->ts.runtime[DDIR_TRIM] -= rt[i];
1340 * Called from signal handler. It _should_ be safe to just run this inline
1341 * in the sig handler, but we should be disturbing the system less by just
1342 * creating a thread to do it.
1344 void show_running_run_stats(void)
1348 pthread_create(&thread, NULL, __show_running_run_stats, NULL);
1349 pthread_detach(thread);
1352 static inline void add_stat_sample(struct io_stat *is, unsigned long data)
1357 if (data > is->max_val)
1359 if (data < is->min_val)
1362 delta = val - is->mean.u.f;
1364 is->mean.u.f += delta / (is->samples + 1.0);
1365 is->S.u.f += delta * (val - is->mean.u.f);
1371 static void __add_log_sample(struct io_log *iolog, unsigned long val,
1372 enum fio_ddir ddir, unsigned int bs,
1375 const int nr_samples = iolog->nr_samples;
1377 if (!iolog->nr_samples)
1378 iolog->avg_last = t;
1380 if (iolog->nr_samples == iolog->max_samples) {
1381 int new_size = sizeof(struct io_sample) * iolog->max_samples*2;
1383 iolog->log = realloc(iolog->log, new_size);
1384 iolog->max_samples <<= 1;
1387 iolog->log[nr_samples].val = val;
1388 iolog->log[nr_samples].time = t;
1389 iolog->log[nr_samples].ddir = ddir;
1390 iolog->log[nr_samples].bs = bs;
1391 iolog->nr_samples++;
1394 static inline void reset_io_stat(struct io_stat *ios)
1396 ios->max_val = ios->min_val = ios->samples = 0;
1397 ios->mean.u.f = ios->S.u.f = 0;
1400 static void add_log_sample(struct thread_data *td, struct io_log *iolog,
1401 unsigned long val, enum fio_ddir ddir,
1404 unsigned long elapsed, this_window;
1409 elapsed = mtime_since_now(&td->epoch);
1412 * If no time averaging, just add the log sample.
1414 if (!iolog->avg_msec) {
1415 __add_log_sample(iolog, val, ddir, bs, elapsed);
1420 * Add the sample. If the time period has passed, then
1421 * add that entry to the log and clear.
1423 add_stat_sample(&iolog->avg_window[ddir], val);
1426 * If period hasn't passed, adding the above sample is all we
1429 this_window = elapsed - iolog->avg_last;
1430 if (this_window < iolog->avg_msec)
1434 * Note an entry in the log. Use the mean from the logged samples,
1435 * making sure to properly round up. Only write a log entry if we
1436 * had actual samples done.
1438 if (iolog->avg_window[DDIR_READ].samples) {
1441 mr = iolog->avg_window[DDIR_READ].mean.u.f + 0.50;
1442 __add_log_sample(iolog, mr, DDIR_READ, 0, elapsed);
1444 if (iolog->avg_window[DDIR_WRITE].samples) {
1447 mw = iolog->avg_window[DDIR_WRITE].mean.u.f + 0.50;
1448 __add_log_sample(iolog, mw, DDIR_WRITE, 0, elapsed);
1450 if (iolog->avg_window[DDIR_TRIM].samples) {
1453 mw = iolog->avg_window[DDIR_TRIM].mean.u.f + 0.50;
1454 __add_log_sample(iolog, mw, DDIR_TRIM, 0, elapsed);
1458 reset_io_stat(&iolog->avg_window[DDIR_READ]);
1459 reset_io_stat(&iolog->avg_window[DDIR_WRITE]);
1460 reset_io_stat(&iolog->avg_window[DDIR_TRIM]);
1461 iolog->avg_last = elapsed;
1464 void add_agg_sample(unsigned long val, enum fio_ddir ddir, unsigned int bs)
1466 struct io_log *iolog;
1471 iolog = agg_io_log[ddir];
1472 __add_log_sample(iolog, val, ddir, bs, mtime_since_genesis());
1475 static void add_clat_percentile_sample(struct thread_stat *ts,
1476 unsigned long usec, enum fio_ddir ddir)
1478 unsigned int idx = plat_val_to_idx(usec);
1479 assert(idx < FIO_IO_U_PLAT_NR);
1481 ts->io_u_plat[ddir][idx]++;
1484 void add_clat_sample(struct thread_data *td, enum fio_ddir ddir,
1485 unsigned long usec, unsigned int bs)
1487 struct thread_stat *ts = &td->ts;
1492 add_stat_sample(&ts->clat_stat[ddir], usec);
1495 add_log_sample(td, td->clat_log, usec, ddir, bs);
1497 if (ts->clat_percentiles)
1498 add_clat_percentile_sample(ts, usec, ddir);
1501 void add_slat_sample(struct thread_data *td, enum fio_ddir ddir,
1502 unsigned long usec, unsigned int bs)
1504 struct thread_stat *ts = &td->ts;
1509 add_stat_sample(&ts->slat_stat[ddir], usec);
1512 add_log_sample(td, td->slat_log, usec, ddir, bs);
1515 void add_lat_sample(struct thread_data *td, enum fio_ddir ddir,
1516 unsigned long usec, unsigned int bs)
1518 struct thread_stat *ts = &td->ts;
1523 add_stat_sample(&ts->lat_stat[ddir], usec);
1526 add_log_sample(td, td->lat_log, usec, ddir, bs);
1529 void add_bw_sample(struct thread_data *td, enum fio_ddir ddir, unsigned int bs,
1532 struct thread_stat *ts = &td->ts;
1533 unsigned long spent, rate;
1538 spent = mtime_since(&td->bw_sample_time, t);
1539 if (spent < td->o.bw_avg_time)
1543 * Compute both read and write rates for the interval.
1545 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++) {
1548 delta = td->this_io_bytes[ddir] - td->stat_io_bytes[ddir];
1550 continue; /* No entries for interval */
1552 rate = delta * 1000 / spent / 1024;
1553 add_stat_sample(&ts->bw_stat[ddir], rate);
1556 add_log_sample(td, td->bw_log, rate, ddir, bs);
1558 td->stat_io_bytes[ddir] = td->this_io_bytes[ddir];
1561 fio_gettime(&td->bw_sample_time, NULL);
1564 void add_iops_sample(struct thread_data *td, enum fio_ddir ddir,
1567 struct thread_stat *ts = &td->ts;
1568 unsigned long spent, iops;
1573 spent = mtime_since(&td->iops_sample_time, t);
1574 if (spent < td->o.iops_avg_time)
1578 * Compute both read and write rates for the interval.
1580 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++) {
1583 delta = td->this_io_blocks[ddir] - td->stat_io_blocks[ddir];
1585 continue; /* No entries for interval */
1587 iops = (delta * 1000) / spent;
1588 add_stat_sample(&ts->iops_stat[ddir], iops);
1591 add_log_sample(td, td->iops_log, iops, ddir, 0);
1593 td->stat_io_blocks[ddir] = td->this_io_blocks[ddir];
1596 fio_gettime(&td->iops_sample_time, NULL);