12 #include "lib/ieee754.h"
14 #include "lib/getrusage.h"
17 #include "lib/output_buffer.h"
19 struct fio_mutex *stat_mutex;
21 void update_rusage_stat(struct thread_data *td)
23 struct thread_stat *ts = &td->ts;
25 fio_getrusage(&td->ru_end);
26 ts->usr_time += mtime_since(&td->ru_start.ru_utime,
27 &td->ru_end.ru_utime);
28 ts->sys_time += mtime_since(&td->ru_start.ru_stime,
29 &td->ru_end.ru_stime);
30 ts->ctx += td->ru_end.ru_nvcsw + td->ru_end.ru_nivcsw
31 - (td->ru_start.ru_nvcsw + td->ru_start.ru_nivcsw);
32 ts->minf += td->ru_end.ru_minflt - td->ru_start.ru_minflt;
33 ts->majf += td->ru_end.ru_majflt - td->ru_start.ru_majflt;
35 memcpy(&td->ru_start, &td->ru_end, sizeof(td->ru_end));
39 * Given a latency, return the index of the corresponding bucket in
40 * the structure tracking percentiles.
42 * (1) find the group (and error bits) that the value (latency)
43 * belongs to by looking at its MSB. (2) find the bucket number in the
44 * group by looking at the index bits.
47 static unsigned int plat_val_to_idx(unsigned int val)
49 unsigned int msb, error_bits, base, offset, idx;
51 /* Find MSB starting from bit 0 */
55 msb = (sizeof(val)*8) - __builtin_clz(val) - 1;
58 * MSB <= (FIO_IO_U_PLAT_BITS-1), cannot be rounded off. Use
59 * all bits of the sample as index
61 if (msb <= FIO_IO_U_PLAT_BITS)
64 /* Compute the number of error bits to discard*/
65 error_bits = msb - FIO_IO_U_PLAT_BITS;
67 /* Compute the number of buckets before the group */
68 base = (error_bits + 1) << FIO_IO_U_PLAT_BITS;
71 * Discard the error bits and apply the mask to find the
72 * index for the buckets in the group
74 offset = (FIO_IO_U_PLAT_VAL - 1) & (val >> error_bits);
76 /* Make sure the index does not exceed (array size - 1) */
77 idx = (base + offset) < (FIO_IO_U_PLAT_NR - 1) ?
78 (base + offset) : (FIO_IO_U_PLAT_NR - 1);
84 * Convert the given index of the bucket array to the value
85 * represented by the bucket
87 static unsigned int plat_idx_to_val(unsigned int idx)
89 unsigned int error_bits, k, base;
91 assert(idx < FIO_IO_U_PLAT_NR);
93 /* MSB <= (FIO_IO_U_PLAT_BITS-1), cannot be rounded off. Use
94 * all bits of the sample as index */
95 if (idx < (FIO_IO_U_PLAT_VAL << 1))
98 /* Find the group and compute the minimum value of that group */
99 error_bits = (idx >> FIO_IO_U_PLAT_BITS) - 1;
100 base = 1 << (error_bits + FIO_IO_U_PLAT_BITS);
102 /* Find its bucket number of the group */
103 k = idx % FIO_IO_U_PLAT_VAL;
105 /* Return the mean of the range of the bucket */
106 return base + ((k + 0.5) * (1 << error_bits));
109 static int double_cmp(const void *a, const void *b)
111 const fio_fp64_t fa = *(const fio_fp64_t *) a;
112 const fio_fp64_t fb = *(const fio_fp64_t *) b;
117 else if (fa.u.f < fb.u.f)
123 unsigned int calc_clat_percentiles(unsigned int *io_u_plat, unsigned long nr,
124 fio_fp64_t *plist, unsigned int **output,
125 unsigned int *maxv, unsigned int *minv)
127 unsigned long sum = 0;
128 unsigned int len, i, j = 0;
129 unsigned int oval_len = 0;
130 unsigned int *ovals = NULL;
137 while (len < FIO_IO_U_LIST_MAX_LEN && plist[len].u.f != 0.0)
144 * Sort the percentile list. Note that it may already be sorted if
145 * we are using the default values, but since it's a short list this
146 * isn't a worry. Also note that this does not work for NaN values.
149 qsort((void *)plist, len, sizeof(plist[0]), double_cmp);
152 * Calculate bucket values, note down max and min values
155 for (i = 0; i < FIO_IO_U_PLAT_NR && !is_last; i++) {
157 while (sum >= (plist[j].u.f / 100.0 * nr)) {
158 assert(plist[j].u.f <= 100.0);
162 ovals = realloc(ovals, oval_len * sizeof(unsigned int));
165 ovals[j] = plat_idx_to_val(i);
166 if (ovals[j] < *minv)
168 if (ovals[j] > *maxv)
171 is_last = (j == len - 1);
184 * Find and display the p-th percentile of clat
186 static void show_clat_percentiles(unsigned int *io_u_plat, unsigned long nr,
187 fio_fp64_t *plist, unsigned int precision,
188 struct buf_output *out)
190 unsigned int len, j = 0, minv, maxv;
192 int is_last, per_line, scale_down;
195 len = calc_clat_percentiles(io_u_plat, nr, plist, &ovals, &maxv, &minv);
200 * We default to usecs, but if the value range is such that we
201 * should scale down to msecs, do that.
203 if (minv > 2000 && maxv > 99999) {
205 log_buf(out, " clat percentiles (msec):\n |");
208 log_buf(out, " clat percentiles (usec):\n |");
211 snprintf(fmt, sizeof(fmt), "%%1.%uf", precision);
212 per_line = (80 - 7) / (precision + 14);
214 for (j = 0; j < len; j++) {
215 char fbuf[16], *ptr = fbuf;
218 if (j != 0 && (j % per_line) == 0)
221 /* end of the list */
222 is_last = (j == len - 1);
224 if (plist[j].u.f < 10.0)
225 ptr += sprintf(fbuf, " ");
227 snprintf(ptr, sizeof(fbuf), fmt, plist[j].u.f);
230 ovals[j] = (ovals[j] + 999) / 1000;
232 log_buf(out, " %sth=[%5u]%c", fbuf, ovals[j], is_last ? '\n' : ',');
237 if ((j % per_line) == per_line - 1) /* for formatting */
246 int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max,
247 double *mean, double *dev)
249 double n = (double) is->samples;
256 *mean = is->mean.u.f;
259 *dev = sqrt(is->S.u.f / (n - 1.0));
266 void show_group_stats(struct group_run_stats *rs, struct buf_output *out)
268 char *p1, *p2, *p3, *p4;
269 const char *str[] = { " READ", " WRITE" , " TRIM"};
272 log_buf(out, "\nRun status group %d (all jobs):\n", rs->groupid);
274 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
275 const int i2p = is_power_of_2(rs->kb_base);
280 p1 = num2str(rs->io_kb[i], 6, rs->kb_base, i2p, 8);
281 p2 = num2str(rs->agg[i], 6, rs->kb_base, i2p, rs->unit_base);
282 p3 = num2str(rs->min_bw[i], 6, rs->kb_base, i2p, rs->unit_base);
283 p4 = num2str(rs->max_bw[i], 6, rs->kb_base, i2p, rs->unit_base);
285 log_buf(out, "%s: io=%s, aggrb=%s/s, minb=%s/s, maxb=%s/s,"
286 " mint=%llumsec, maxt=%llumsec\n",
287 rs->unified_rw_rep ? " MIXED" : str[i],
289 (unsigned long long) rs->min_run[i],
290 (unsigned long long) rs->max_run[i]);
299 void stat_calc_dist(unsigned int *map, unsigned long total, double *io_u_dist)
304 * Do depth distribution calculations
306 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
308 io_u_dist[i] = (double) map[i] / (double) total;
309 io_u_dist[i] *= 100.0;
310 if (io_u_dist[i] < 0.1 && map[i])
317 static void stat_calc_lat(struct thread_stat *ts, double *dst,
318 unsigned int *src, int nr)
320 unsigned long total = ddir_rw_sum(ts->total_io_u);
324 * Do latency distribution calculations
326 for (i = 0; i < nr; i++) {
328 dst[i] = (double) src[i] / (double) total;
330 if (dst[i] < 0.01 && src[i])
337 void stat_calc_lat_u(struct thread_stat *ts, double *io_u_lat)
339 stat_calc_lat(ts, io_u_lat, ts->io_u_lat_u, FIO_IO_U_LAT_U_NR);
342 void stat_calc_lat_m(struct thread_stat *ts, double *io_u_lat)
344 stat_calc_lat(ts, io_u_lat, ts->io_u_lat_m, FIO_IO_U_LAT_M_NR);
347 static void display_lat(const char *name, unsigned long min, unsigned long max,
348 double mean, double dev, struct buf_output *out)
350 const char *base = "(usec)";
353 if (!usec_to_msec(&min, &max, &mean, &dev))
356 minp = num2str(min, 6, 1, 0, 0);
357 maxp = num2str(max, 6, 1, 0, 0);
359 log_buf(out, " %s %s: min=%s, max=%s, avg=%5.02f,"
360 " stdev=%5.02f\n", name, base, minp, maxp, mean, dev);
366 static void show_ddir_status(struct group_run_stats *rs, struct thread_stat *ts,
367 int ddir, struct buf_output *out)
369 const char *str[] = { "read ", "write", "trim" };
370 unsigned long min, max, runt;
371 unsigned long long bw, iops;
373 char *io_p, *bw_p, *iops_p;
376 assert(ddir_rw(ddir));
378 if (!ts->runtime[ddir])
381 i2p = is_power_of_2(rs->kb_base);
382 runt = ts->runtime[ddir];
384 bw = (1000 * ts->io_bytes[ddir]) / runt;
385 io_p = num2str(ts->io_bytes[ddir], 6, 1, i2p, 8);
386 bw_p = num2str(bw, 6, 1, i2p, ts->unit_base);
388 iops = (1000 * (uint64_t)ts->total_io_u[ddir]) / runt;
389 iops_p = num2str(iops, 6, 1, 0, 0);
391 log_buf(out, " %s: io=%s, bw=%s/s, iops=%s, runt=%6llumsec\n",
392 rs->unified_rw_rep ? "mixed" : str[ddir],
394 (unsigned long long) ts->runtime[ddir]);
400 if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev))
401 display_lat("slat", min, max, mean, dev, out);
402 if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev))
403 display_lat("clat", min, max, mean, dev, out);
404 if (calc_lat(&ts->lat_stat[ddir], &min, &max, &mean, &dev))
405 display_lat(" lat", min, max, mean, dev, out);
407 if (ts->clat_percentiles) {
408 show_clat_percentiles(ts->io_u_plat[ddir],
409 ts->clat_stat[ddir].samples,
411 ts->percentile_precision, out);
413 if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
414 double p_of_agg = 100.0, fkb_base = (double)rs->kb_base;
415 const char *bw_str = (rs->unit_base == 1 ? "Kbit" : "KB");
417 if (rs->unit_base == 1) {
425 p_of_agg = mean * 100 / (double) rs->agg[ddir];
426 if (p_of_agg > 100.0)
430 if (mean > fkb_base * fkb_base) {
435 bw_str = (rs->unit_base == 1 ? "Mbit" : "MB");
438 log_buf(out, " bw (%-4s/s): min=%5lu, max=%5lu, per=%3.2f%%,"
439 " avg=%5.02f, stdev=%5.02f\n", bw_str, min, max,
440 p_of_agg, mean, dev);
444 static int show_lat(double *io_u_lat, int nr, const char **ranges,
445 const char *msg, struct buf_output *out)
447 int new_line = 1, i, line = 0, shown = 0;
449 for (i = 0; i < nr; i++) {
450 if (io_u_lat[i] <= 0.0)
456 log_buf(out, " lat (%s) : ", msg);
462 log_buf(out, "%s%3.2f%%", ranges[i], io_u_lat[i]);
474 static void show_lat_u(double *io_u_lat_u, struct buf_output *out)
476 const char *ranges[] = { "2=", "4=", "10=", "20=", "50=", "100=",
477 "250=", "500=", "750=", "1000=", };
479 show_lat(io_u_lat_u, FIO_IO_U_LAT_U_NR, ranges, "usec", out);
482 static void show_lat_m(double *io_u_lat_m, struct buf_output *out)
484 const char *ranges[] = { "2=", "4=", "10=", "20=", "50=", "100=",
485 "250=", "500=", "750=", "1000=", "2000=",
488 show_lat(io_u_lat_m, FIO_IO_U_LAT_M_NR, ranges, "msec", out);
491 static void show_latencies(struct thread_stat *ts, struct buf_output *out)
493 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
494 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
496 stat_calc_lat_u(ts, io_u_lat_u);
497 stat_calc_lat_m(ts, io_u_lat_m);
499 show_lat_u(io_u_lat_u, out);
500 show_lat_m(io_u_lat_m, out);
503 static int block_state_category(int block_state)
505 switch (block_state) {
506 case BLOCK_STATE_UNINIT:
508 case BLOCK_STATE_TRIMMED:
509 case BLOCK_STATE_WRITTEN:
511 case BLOCK_STATE_WRITE_FAILURE:
512 case BLOCK_STATE_TRIM_FAILURE:
515 /* Silence compile warning on some BSDs and have a return */
521 static int compare_block_infos(const void *bs1, const void *bs2)
523 uint32_t block1 = *(uint32_t *)bs1;
524 uint32_t block2 = *(uint32_t *)bs2;
525 int state1 = BLOCK_INFO_STATE(block1);
526 int state2 = BLOCK_INFO_STATE(block2);
527 int bscat1 = block_state_category(state1);
528 int bscat2 = block_state_category(state2);
529 int cycles1 = BLOCK_INFO_TRIMS(block1);
530 int cycles2 = BLOCK_INFO_TRIMS(block2);
537 if (cycles1 < cycles2)
539 if (cycles1 > cycles2)
547 assert(block1 == block2);
551 static int calc_block_percentiles(int nr_block_infos, uint32_t *block_infos,
552 fio_fp64_t *plist, unsigned int **percentiles,
558 qsort(block_infos, nr_block_infos, sizeof(uint32_t), compare_block_infos);
560 while (len < FIO_IO_U_LIST_MAX_LEN && plist[len].u.f != 0.0)
567 * Sort the percentile list. Note that it may already be sorted if
568 * we are using the default values, but since it's a short list this
569 * isn't a worry. Also note that this does not work for NaN values.
572 qsort((void *)plist, len, sizeof(plist[0]), double_cmp);
575 /* Start only after the uninit entries end */
577 nr_uninit < nr_block_infos
578 && BLOCK_INFO_STATE(block_infos[nr_uninit]) == BLOCK_STATE_UNINIT;
582 if (nr_uninit == nr_block_infos)
585 *percentiles = calloc(len, sizeof(**percentiles));
587 for (i = 0; i < len; i++) {
588 int idx = (plist[i].u.f * (nr_block_infos - nr_uninit) / 100)
590 (*percentiles)[i] = BLOCK_INFO_TRIMS(block_infos[idx]);
593 memset(types, 0, sizeof(*types) * BLOCK_STATE_COUNT);
594 for (i = 0; i < nr_block_infos; i++)
595 types[BLOCK_INFO_STATE(block_infos[i])]++;
600 static const char *block_state_names[] = {
601 [BLOCK_STATE_UNINIT] = "unwritten",
602 [BLOCK_STATE_TRIMMED] = "trimmed",
603 [BLOCK_STATE_WRITTEN] = "written",
604 [BLOCK_STATE_TRIM_FAILURE] = "trim failure",
605 [BLOCK_STATE_WRITE_FAILURE] = "write failure",
608 static void show_block_infos(int nr_block_infos, uint32_t *block_infos,
609 fio_fp64_t *plist, struct buf_output *out)
612 unsigned int *percentiles = NULL;
613 unsigned int block_state_counts[BLOCK_STATE_COUNT];
615 len = calc_block_percentiles(nr_block_infos, block_infos, plist,
616 &percentiles, block_state_counts);
618 log_buf(out, " block lifetime percentiles :\n |");
620 for (i = 0; i < len; i++) {
621 uint32_t block_info = percentiles[i];
622 #define LINE_LENGTH 75
623 char str[LINE_LENGTH];
624 int strln = snprintf(str, LINE_LENGTH, " %3.2fth=%u%c",
625 plist[i].u.f, block_info,
626 i == len - 1 ? '\n' : ',');
627 assert(strln < LINE_LENGTH);
628 if (pos + strln > LINE_LENGTH) {
630 log_buf(out, "\n |");
632 log_buf(out, "%s", str);
639 log_buf(out, " states :");
640 for (i = 0; i < BLOCK_STATE_COUNT; i++)
641 log_buf(out, " %s=%u%c",
642 block_state_names[i], block_state_counts[i],
643 i == BLOCK_STATE_COUNT - 1 ? '\n' : ',');
646 static void show_thread_status_normal(struct thread_stat *ts,
647 struct group_run_stats *rs,
648 struct buf_output *out)
650 double usr_cpu, sys_cpu;
651 unsigned long runtime;
652 double io_u_dist[FIO_IO_U_MAP_NR];
656 if (!ddir_rw_sum(ts->io_bytes) && !ddir_rw_sum(ts->total_io_u))
660 os_ctime_r((const time_t *) &time_p, time_buf, sizeof(time_buf));
663 log_buf(out, "%s: (groupid=%d, jobs=%d): err=%2d: pid=%d: %s",
664 ts->name, ts->groupid, ts->members,
665 ts->error, (int) ts->pid, time_buf);
667 log_buf(out, "%s: (groupid=%d, jobs=%d): err=%2d (%s): pid=%d: %s",
668 ts->name, ts->groupid, ts->members,
669 ts->error, ts->verror, (int) ts->pid,
673 if (strlen(ts->description))
674 log_buf(out, " Description : [%s]\n", ts->description);
676 if (ts->io_bytes[DDIR_READ])
677 show_ddir_status(rs, ts, DDIR_READ, out);
678 if (ts->io_bytes[DDIR_WRITE])
679 show_ddir_status(rs, ts, DDIR_WRITE, out);
680 if (ts->io_bytes[DDIR_TRIM])
681 show_ddir_status(rs, ts, DDIR_TRIM, out);
683 show_latencies(ts, out);
685 runtime = ts->total_run_time;
687 double runt = (double) runtime;
689 usr_cpu = (double) ts->usr_time * 100 / runt;
690 sys_cpu = (double) ts->sys_time * 100 / runt;
696 log_buf(out, " cpu : usr=%3.2f%%, sys=%3.2f%%, ctx=%llu,"
697 " majf=%llu, minf=%llu\n", usr_cpu, sys_cpu,
698 (unsigned long long) ts->ctx,
699 (unsigned long long) ts->majf,
700 (unsigned long long) ts->minf);
702 stat_calc_dist(ts->io_u_map, ddir_rw_sum(ts->total_io_u), io_u_dist);
703 log_buf(out, " IO depths : 1=%3.1f%%, 2=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%,"
704 " 16=%3.1f%%, 32=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
705 io_u_dist[1], io_u_dist[2],
706 io_u_dist[3], io_u_dist[4],
707 io_u_dist[5], io_u_dist[6]);
709 stat_calc_dist(ts->io_u_submit, ts->total_submit, io_u_dist);
710 log_buf(out, " submit : 0=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%, 16=%3.1f%%,"
711 " 32=%3.1f%%, 64=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
712 io_u_dist[1], io_u_dist[2],
713 io_u_dist[3], io_u_dist[4],
714 io_u_dist[5], io_u_dist[6]);
715 stat_calc_dist(ts->io_u_complete, ts->total_complete, io_u_dist);
716 log_buf(out, " complete : 0=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%, 16=%3.1f%%,"
717 " 32=%3.1f%%, 64=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
718 io_u_dist[1], io_u_dist[2],
719 io_u_dist[3], io_u_dist[4],
720 io_u_dist[5], io_u_dist[6]);
721 log_buf(out, " issued : total=r=%llu/w=%llu/d=%llu,"
722 " short=r=%llu/w=%llu/d=%llu,"
723 " drop=r=%llu/w=%llu/d=%llu\n",
724 (unsigned long long) ts->total_io_u[0],
725 (unsigned long long) ts->total_io_u[1],
726 (unsigned long long) ts->total_io_u[2],
727 (unsigned long long) ts->short_io_u[0],
728 (unsigned long long) ts->short_io_u[1],
729 (unsigned long long) ts->short_io_u[2],
730 (unsigned long long) ts->drop_io_u[0],
731 (unsigned long long) ts->drop_io_u[1],
732 (unsigned long long) ts->drop_io_u[2]);
733 if (ts->continue_on_error) {
734 log_buf(out, " errors : total=%llu, first_error=%d/<%s>\n",
735 (unsigned long long)ts->total_err_count,
737 strerror(ts->first_error));
739 if (ts->latency_depth) {
740 log_buf(out, " latency : target=%llu, window=%llu, percentile=%.2f%%, depth=%u\n",
741 (unsigned long long)ts->latency_target,
742 (unsigned long long)ts->latency_window,
743 ts->latency_percentile.u.f,
747 if (ts->nr_block_infos)
748 show_block_infos(ts->nr_block_infos, ts->block_infos,
749 ts->percentile_list, out);
752 static void show_ddir_status_terse(struct thread_stat *ts,
753 struct group_run_stats *rs, int ddir,
754 struct buf_output *out)
756 unsigned long min, max;
757 unsigned long long bw, iops;
758 unsigned int *ovals = NULL;
760 unsigned int len, minv, maxv;
763 assert(ddir_rw(ddir));
766 if (ts->runtime[ddir]) {
767 uint64_t runt = ts->runtime[ddir];
769 bw = ((1000 * ts->io_bytes[ddir]) / runt) / 1024;
770 iops = (1000 * (uint64_t) ts->total_io_u[ddir]) / runt;
773 log_buf(out, ";%llu;%llu;%llu;%llu",
774 (unsigned long long) ts->io_bytes[ddir] >> 10, bw, iops,
775 (unsigned long long) ts->runtime[ddir]);
777 if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev))
778 log_buf(out, ";%lu;%lu;%f;%f", min, max, mean, dev);
780 log_buf(out, ";%lu;%lu;%f;%f", 0UL, 0UL, 0.0, 0.0);
782 if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev))
783 log_buf(out, ";%lu;%lu;%f;%f", min, max, mean, dev);
785 log_buf(out, ";%lu;%lu;%f;%f", 0UL, 0UL, 0.0, 0.0);
787 if (ts->clat_percentiles) {
788 len = calc_clat_percentiles(ts->io_u_plat[ddir],
789 ts->clat_stat[ddir].samples,
790 ts->percentile_list, &ovals, &maxv,
795 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
797 log_buf(out, ";0%%=0");
800 log_buf(out, ";%f%%=%u", ts->percentile_list[i].u.f, ovals[i]);
803 if (calc_lat(&ts->lat_stat[ddir], &min, &max, &mean, &dev))
804 log_buf(out, ";%lu;%lu;%f;%f", min, max, mean, dev);
806 log_buf(out, ";%lu;%lu;%f;%f", 0UL, 0UL, 0.0, 0.0);
811 if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
812 double p_of_agg = 100.0;
815 p_of_agg = mean * 100 / (double) rs->agg[ddir];
816 if (p_of_agg > 100.0)
820 log_buf(out, ";%lu;%lu;%f%%;%f;%f", min, max, p_of_agg, mean, dev);
822 log_buf(out, ";%lu;%lu;%f%%;%f;%f", 0UL, 0UL, 0.0, 0.0, 0.0);
825 static void add_ddir_status_json(struct thread_stat *ts,
826 struct group_run_stats *rs, int ddir, struct json_object *parent)
828 unsigned long min, max;
829 unsigned long long bw;
830 unsigned int *ovals = NULL;
831 double mean, dev, iops;
832 unsigned int len, minv, maxv;
834 const char *ddirname[] = {"read", "write", "trim"};
835 struct json_object *dir_object, *tmp_object, *percentile_object;
837 double p_of_agg = 100.0;
839 assert(ddir_rw(ddir));
841 if (ts->unified_rw_rep && ddir != DDIR_READ)
844 dir_object = json_create_object();
845 json_object_add_value_object(parent,
846 ts->unified_rw_rep ? "mixed" : ddirname[ddir], dir_object);
850 if (ts->runtime[ddir]) {
851 uint64_t runt = ts->runtime[ddir];
853 bw = ((1000 * ts->io_bytes[ddir]) / runt) / 1024;
854 iops = (1000.0 * (uint64_t) ts->total_io_u[ddir]) / runt;
857 json_object_add_value_int(dir_object, "io_bytes", ts->io_bytes[ddir] >> 10);
858 json_object_add_value_int(dir_object, "bw", bw);
859 json_object_add_value_float(dir_object, "iops", iops);
860 json_object_add_value_int(dir_object, "runtime", ts->runtime[ddir]);
861 json_object_add_value_int(dir_object, "total_ios", ts->total_io_u[ddir]);
862 json_object_add_value_int(dir_object, "short_ios", ts->short_io_u[ddir]);
863 json_object_add_value_int(dir_object, "drop_ios", ts->drop_io_u[ddir]);
865 if (!calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev)) {
869 tmp_object = json_create_object();
870 json_object_add_value_object(dir_object, "slat", tmp_object);
871 json_object_add_value_int(tmp_object, "min", min);
872 json_object_add_value_int(tmp_object, "max", max);
873 json_object_add_value_float(tmp_object, "mean", mean);
874 json_object_add_value_float(tmp_object, "stddev", dev);
876 if (!calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev)) {
880 tmp_object = json_create_object();
881 json_object_add_value_object(dir_object, "clat", tmp_object);
882 json_object_add_value_int(tmp_object, "min", min);
883 json_object_add_value_int(tmp_object, "max", max);
884 json_object_add_value_float(tmp_object, "mean", mean);
885 json_object_add_value_float(tmp_object, "stddev", dev);
887 if (ts->clat_percentiles) {
888 len = calc_clat_percentiles(ts->io_u_plat[ddir],
889 ts->clat_stat[ddir].samples,
890 ts->percentile_list, &ovals, &maxv,
895 percentile_object = json_create_object();
896 json_object_add_value_object(tmp_object, "percentile", percentile_object);
897 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
899 json_object_add_value_int(percentile_object, "0.00", 0);
902 snprintf(buf, sizeof(buf), "%f", ts->percentile_list[i].u.f);
903 json_object_add_value_int(percentile_object, (const char *)buf, ovals[i]);
906 if (!calc_lat(&ts->lat_stat[ddir], &min, &max, &mean, &dev)) {
910 tmp_object = json_create_object();
911 json_object_add_value_object(dir_object, "lat", tmp_object);
912 json_object_add_value_int(tmp_object, "min", min);
913 json_object_add_value_int(tmp_object, "max", max);
914 json_object_add_value_float(tmp_object, "mean", mean);
915 json_object_add_value_float(tmp_object, "stddev", dev);
919 if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
921 p_of_agg = mean * 100 / (double) rs->agg[ddir];
922 if (p_of_agg > 100.0)
927 p_of_agg = mean = dev = 0.0;
929 json_object_add_value_int(dir_object, "bw_min", min);
930 json_object_add_value_int(dir_object, "bw_max", max);
931 json_object_add_value_float(dir_object, "bw_agg", p_of_agg);
932 json_object_add_value_float(dir_object, "bw_mean", mean);
933 json_object_add_value_float(dir_object, "bw_dev", dev);
936 static void show_thread_status_terse_v2(struct thread_stat *ts,
937 struct group_run_stats *rs,
938 struct buf_output *out)
940 double io_u_dist[FIO_IO_U_MAP_NR];
941 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
942 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
943 double usr_cpu, sys_cpu;
947 log_buf(out, "2;%s;%d;%d", ts->name, ts->groupid, ts->error);
948 /* Log Read Status */
949 show_ddir_status_terse(ts, rs, DDIR_READ, out);
950 /* Log Write Status */
951 show_ddir_status_terse(ts, rs, DDIR_WRITE, out);
952 /* Log Trim Status */
953 show_ddir_status_terse(ts, rs, DDIR_TRIM, out);
956 if (ts->total_run_time) {
957 double runt = (double) ts->total_run_time;
959 usr_cpu = (double) ts->usr_time * 100 / runt;
960 sys_cpu = (double) ts->sys_time * 100 / runt;
966 log_buf(out, ";%f%%;%f%%;%llu;%llu;%llu", usr_cpu, sys_cpu,
967 (unsigned long long) ts->ctx,
968 (unsigned long long) ts->majf,
969 (unsigned long long) ts->minf);
971 /* Calc % distribution of IO depths, usecond, msecond latency */
972 stat_calc_dist(ts->io_u_map, ddir_rw_sum(ts->total_io_u), io_u_dist);
973 stat_calc_lat_u(ts, io_u_lat_u);
974 stat_calc_lat_m(ts, io_u_lat_m);
976 /* Only show fixed 7 I/O depth levels*/
977 log_buf(out, ";%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%",
978 io_u_dist[0], io_u_dist[1], io_u_dist[2], io_u_dist[3],
979 io_u_dist[4], io_u_dist[5], io_u_dist[6]);
981 /* Microsecond latency */
982 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
983 log_buf(out, ";%3.2f%%", io_u_lat_u[i]);
984 /* Millisecond latency */
985 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
986 log_buf(out, ";%3.2f%%", io_u_lat_m[i]);
987 /* Additional output if continue_on_error set - default off*/
988 if (ts->continue_on_error)
989 log_buf(out, ";%llu;%d", (unsigned long long) ts->total_err_count, ts->first_error);
992 /* Additional output if description is set */
993 if (strlen(ts->description))
994 log_buf(out, ";%s", ts->description);
999 static void show_thread_status_terse_v3_v4(struct thread_stat *ts,
1000 struct group_run_stats *rs, int ver,
1001 struct buf_output *out)
1003 double io_u_dist[FIO_IO_U_MAP_NR];
1004 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
1005 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
1006 double usr_cpu, sys_cpu;
1010 log_buf(out, "%d;%s;%s;%d;%d", ver, fio_version_string,
1011 ts->name, ts->groupid, ts->error);
1012 /* Log Read Status */
1013 show_ddir_status_terse(ts, rs, DDIR_READ, out);
1014 /* Log Write Status */
1015 show_ddir_status_terse(ts, rs, DDIR_WRITE, out);
1016 /* Log Trim Status */
1018 show_ddir_status_terse(ts, rs, DDIR_TRIM, out);
1021 if (ts->total_run_time) {
1022 double runt = (double) ts->total_run_time;
1024 usr_cpu = (double) ts->usr_time * 100 / runt;
1025 sys_cpu = (double) ts->sys_time * 100 / runt;
1031 log_buf(out, ";%f%%;%f%%;%llu;%llu;%llu", usr_cpu, sys_cpu,
1032 (unsigned long long) ts->ctx,
1033 (unsigned long long) ts->majf,
1034 (unsigned long long) ts->minf);
1036 /* Calc % distribution of IO depths, usecond, msecond latency */
1037 stat_calc_dist(ts->io_u_map, ddir_rw_sum(ts->total_io_u), io_u_dist);
1038 stat_calc_lat_u(ts, io_u_lat_u);
1039 stat_calc_lat_m(ts, io_u_lat_m);
1041 /* Only show fixed 7 I/O depth levels*/
1042 log_buf(out, ";%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%",
1043 io_u_dist[0], io_u_dist[1], io_u_dist[2], io_u_dist[3],
1044 io_u_dist[4], io_u_dist[5], io_u_dist[6]);
1046 /* Microsecond latency */
1047 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
1048 log_buf(out, ";%3.2f%%", io_u_lat_u[i]);
1049 /* Millisecond latency */
1050 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
1051 log_buf(out, ";%3.2f%%", io_u_lat_m[i]);
1053 /* disk util stats, if any */
1054 show_disk_util(1, NULL, out);
1056 /* Additional output if continue_on_error set - default off*/
1057 if (ts->continue_on_error)
1058 log_buf(out, ";%llu;%d", (unsigned long long) ts->total_err_count, ts->first_error);
1060 /* Additional output if description is set */
1061 if (strlen(ts->description))
1062 log_buf(out, ";%s", ts->description);
1067 static struct json_object *show_thread_status_json(struct thread_stat *ts,
1068 struct group_run_stats *rs)
1070 struct json_object *root, *tmp;
1071 struct jobs_eta *je;
1072 double io_u_dist[FIO_IO_U_MAP_NR];
1073 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
1074 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
1075 double usr_cpu, sys_cpu;
1079 root = json_create_object();
1080 json_object_add_value_string(root, "jobname", ts->name);
1081 json_object_add_value_int(root, "groupid", ts->groupid);
1082 json_object_add_value_int(root, "error", ts->error);
1085 je = get_jobs_eta(1, &size);
1087 json_object_add_value_int(root, "eta", je->eta_sec);
1088 json_object_add_value_int(root, "elapsed", je->elapsed_sec);
1091 add_ddir_status_json(ts, rs, DDIR_READ, root);
1092 add_ddir_status_json(ts, rs, DDIR_WRITE, root);
1093 add_ddir_status_json(ts, rs, DDIR_TRIM, root);
1096 if (ts->total_run_time) {
1097 double runt = (double) ts->total_run_time;
1099 usr_cpu = (double) ts->usr_time * 100 / runt;
1100 sys_cpu = (double) ts->sys_time * 100 / runt;
1105 json_object_add_value_float(root, "usr_cpu", usr_cpu);
1106 json_object_add_value_float(root, "sys_cpu", sys_cpu);
1107 json_object_add_value_int(root, "ctx", ts->ctx);
1108 json_object_add_value_int(root, "majf", ts->majf);
1109 json_object_add_value_int(root, "minf", ts->minf);
1112 /* Calc % distribution of IO depths, usecond, msecond latency */
1113 stat_calc_dist(ts->io_u_map, ddir_rw_sum(ts->total_io_u), io_u_dist);
1114 stat_calc_lat_u(ts, io_u_lat_u);
1115 stat_calc_lat_m(ts, io_u_lat_m);
1117 tmp = json_create_object();
1118 json_object_add_value_object(root, "iodepth_level", tmp);
1119 /* Only show fixed 7 I/O depth levels*/
1120 for (i = 0; i < 7; i++) {
1123 snprintf(name, 20, "%d", 1 << i);
1125 snprintf(name, 20, ">=%d", 1 << i);
1126 json_object_add_value_float(tmp, (const char *)name, io_u_dist[i]);
1129 tmp = json_create_object();
1130 json_object_add_value_object(root, "latency_us", tmp);
1131 /* Microsecond latency */
1132 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
1133 const char *ranges[] = { "2", "4", "10", "20", "50", "100",
1134 "250", "500", "750", "1000", };
1135 json_object_add_value_float(tmp, ranges[i], io_u_lat_u[i]);
1137 /* Millisecond latency */
1138 tmp = json_create_object();
1139 json_object_add_value_object(root, "latency_ms", tmp);
1140 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++) {
1141 const char *ranges[] = { "2", "4", "10", "20", "50", "100",
1142 "250", "500", "750", "1000", "2000",
1144 json_object_add_value_float(tmp, ranges[i], io_u_lat_m[i]);
1147 /* Additional output if continue_on_error set - default off*/
1148 if (ts->continue_on_error) {
1149 json_object_add_value_int(root, "total_err", ts->total_err_count);
1150 json_object_add_value_int(root, "first_error", ts->first_error);
1153 if (ts->latency_depth) {
1154 json_object_add_value_int(root, "latency_depth", ts->latency_depth);
1155 json_object_add_value_int(root, "latency_target", ts->latency_target);
1156 json_object_add_value_float(root, "latency_percentile", ts->latency_percentile.u.f);
1157 json_object_add_value_int(root, "latency_window", ts->latency_window);
1160 /* Additional output if description is set */
1161 if (strlen(ts->description))
1162 json_object_add_value_string(root, "desc", ts->description);
1164 if (ts->nr_block_infos) {
1165 /* Block error histogram and types */
1167 unsigned int *percentiles = NULL;
1168 unsigned int block_state_counts[BLOCK_STATE_COUNT];
1170 len = calc_block_percentiles(ts->nr_block_infos, ts->block_infos,
1171 ts->percentile_list,
1172 &percentiles, block_state_counts);
1175 struct json_object *block, *percentile_object, *states;
1177 block = json_create_object();
1178 json_object_add_value_object(root, "block", block);
1180 percentile_object = json_create_object();
1181 json_object_add_value_object(block, "percentiles",
1183 for (i = 0; i < len; i++) {
1185 snprintf(buf, sizeof(buf), "%f",
1186 ts->percentile_list[i].u.f);
1187 json_object_add_value_int(percentile_object,
1192 states = json_create_object();
1193 json_object_add_value_object(block, "states", states);
1194 for (state = 0; state < BLOCK_STATE_COUNT; state++) {
1195 json_object_add_value_int(states,
1196 block_state_names[state],
1197 block_state_counts[state]);
1206 static void show_thread_status_terse(struct thread_stat *ts,
1207 struct group_run_stats *rs,
1208 struct buf_output *out)
1210 if (terse_version == 2)
1211 show_thread_status_terse_v2(ts, rs, out);
1212 else if (terse_version == 3 || terse_version == 4)
1213 show_thread_status_terse_v3_v4(ts, rs, terse_version, out);
1215 log_err("fio: bad terse version!? %d\n", terse_version);
1218 struct json_object *show_thread_status(struct thread_stat *ts,
1219 struct group_run_stats *rs,
1220 struct buf_output *out)
1222 struct json_object *ret = NULL;
1224 if (output_format & FIO_OUTPUT_TERSE)
1225 show_thread_status_terse(ts, rs, out);
1226 if (output_format & FIO_OUTPUT_JSON)
1227 ret = show_thread_status_json(ts, rs);
1228 if (output_format & FIO_OUTPUT_NORMAL)
1229 show_thread_status_normal(ts, rs, out);
1234 static void sum_stat(struct io_stat *dst, struct io_stat *src, int nr)
1238 if (src->samples == 0)
1241 dst->min_val = min(dst->min_val, src->min_val);
1242 dst->max_val = max(dst->max_val, src->max_val);
1245 * Compute new mean and S after the merge
1246 * <http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
1247 * #Parallel_algorithm>
1250 mean = src->mean.u.f;
1253 double delta = src->mean.u.f - dst->mean.u.f;
1255 mean = ((src->mean.u.f * src->samples) +
1256 (dst->mean.u.f * dst->samples)) /
1257 (dst->samples + src->samples);
1259 S = src->S.u.f + dst->S.u.f + pow(delta, 2.0) *
1260 (dst->samples * src->samples) /
1261 (dst->samples + src->samples);
1264 dst->samples += src->samples;
1265 dst->mean.u.f = mean;
1269 void sum_group_stats(struct group_run_stats *dst, struct group_run_stats *src)
1273 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1274 if (dst->max_run[i] < src->max_run[i])
1275 dst->max_run[i] = src->max_run[i];
1276 if (dst->min_run[i] && dst->min_run[i] > src->min_run[i])
1277 dst->min_run[i] = src->min_run[i];
1278 if (dst->max_bw[i] < src->max_bw[i])
1279 dst->max_bw[i] = src->max_bw[i];
1280 if (dst->min_bw[i] && dst->min_bw[i] > src->min_bw[i])
1281 dst->min_bw[i] = src->min_bw[i];
1283 dst->io_kb[i] += src->io_kb[i];
1284 dst->agg[i] += src->agg[i];
1288 dst->kb_base = src->kb_base;
1289 if (!dst->unit_base)
1290 dst->unit_base = src->unit_base;
1293 void sum_thread_stats(struct thread_stat *dst, struct thread_stat *src, int nr)
1297 for (l = 0; l < DDIR_RWDIR_CNT; l++) {
1298 if (!dst->unified_rw_rep) {
1299 sum_stat(&dst->clat_stat[l], &src->clat_stat[l], nr);
1300 sum_stat(&dst->slat_stat[l], &src->slat_stat[l], nr);
1301 sum_stat(&dst->lat_stat[l], &src->lat_stat[l], nr);
1302 sum_stat(&dst->bw_stat[l], &src->bw_stat[l], nr);
1304 dst->io_bytes[l] += src->io_bytes[l];
1306 if (dst->runtime[l] < src->runtime[l])
1307 dst->runtime[l] = src->runtime[l];
1309 sum_stat(&dst->clat_stat[0], &src->clat_stat[l], nr);
1310 sum_stat(&dst->slat_stat[0], &src->slat_stat[l], nr);
1311 sum_stat(&dst->lat_stat[0], &src->lat_stat[l], nr);
1312 sum_stat(&dst->bw_stat[0], &src->bw_stat[l], nr);
1314 dst->io_bytes[0] += src->io_bytes[l];
1316 if (dst->runtime[0] < src->runtime[l])
1317 dst->runtime[0] = src->runtime[l];
1321 dst->usr_time += src->usr_time;
1322 dst->sys_time += src->sys_time;
1323 dst->ctx += src->ctx;
1324 dst->majf += src->majf;
1325 dst->minf += src->minf;
1327 for (k = 0; k < FIO_IO_U_MAP_NR; k++)
1328 dst->io_u_map[k] += src->io_u_map[k];
1329 for (k = 0; k < FIO_IO_U_MAP_NR; k++)
1330 dst->io_u_submit[k] += src->io_u_submit[k];
1331 for (k = 0; k < FIO_IO_U_MAP_NR; k++)
1332 dst->io_u_complete[k] += src->io_u_complete[k];
1333 for (k = 0; k < FIO_IO_U_LAT_U_NR; k++)
1334 dst->io_u_lat_u[k] += src->io_u_lat_u[k];
1335 for (k = 0; k < FIO_IO_U_LAT_M_NR; k++)
1336 dst->io_u_lat_m[k] += src->io_u_lat_m[k];
1338 for (k = 0; k < DDIR_RWDIR_CNT; k++) {
1339 if (!dst->unified_rw_rep) {
1340 dst->total_io_u[k] += src->total_io_u[k];
1341 dst->short_io_u[k] += src->short_io_u[k];
1342 dst->drop_io_u[k] += src->drop_io_u[k];
1344 dst->total_io_u[0] += src->total_io_u[k];
1345 dst->short_io_u[0] += src->short_io_u[k];
1346 dst->drop_io_u[0] += src->drop_io_u[k];
1350 for (k = 0; k < DDIR_RWDIR_CNT; k++) {
1353 for (m = 0; m < FIO_IO_U_PLAT_NR; m++) {
1354 if (!dst->unified_rw_rep)
1355 dst->io_u_plat[k][m] += src->io_u_plat[k][m];
1357 dst->io_u_plat[0][m] += src->io_u_plat[k][m];
1361 dst->total_run_time += src->total_run_time;
1362 dst->total_submit += src->total_submit;
1363 dst->total_complete += src->total_complete;
1366 void init_group_run_stat(struct group_run_stats *gs)
1369 memset(gs, 0, sizeof(*gs));
1371 for (i = 0; i < DDIR_RWDIR_CNT; i++)
1372 gs->min_bw[i] = gs->min_run[i] = ~0UL;
1375 void init_thread_stat(struct thread_stat *ts)
1379 memset(ts, 0, sizeof(*ts));
1381 for (j = 0; j < DDIR_RWDIR_CNT; j++) {
1382 ts->lat_stat[j].min_val = -1UL;
1383 ts->clat_stat[j].min_val = -1UL;
1384 ts->slat_stat[j].min_val = -1UL;
1385 ts->bw_stat[j].min_val = -1UL;
1390 void __show_run_stats(void)
1392 struct group_run_stats *runstats, *rs;
1393 struct thread_data *td;
1394 struct thread_stat *threadstats, *ts;
1395 int i, j, k, nr_ts, last_ts, idx;
1396 int kb_base_warned = 0;
1397 int unit_base_warned = 0;
1398 struct json_object *root = NULL;
1399 struct json_array *array = NULL;
1400 struct buf_output output[FIO_OUTPUT_NR];
1402 runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1));
1404 for (i = 0; i < groupid + 1; i++)
1405 init_group_run_stat(&runstats[i]);
1408 * find out how many threads stats we need. if group reporting isn't
1409 * enabled, it's one-per-td.
1413 for_each_td(td, i) {
1414 if (!td->o.group_reporting) {
1418 if (last_ts == td->groupid)
1421 last_ts = td->groupid;
1425 threadstats = malloc(nr_ts * sizeof(struct thread_stat));
1427 for (i = 0; i < nr_ts; i++)
1428 init_thread_stat(&threadstats[i]);
1433 for_each_td(td, i) {
1434 if (idx && (!td->o.group_reporting ||
1435 (td->o.group_reporting && last_ts != td->groupid))) {
1440 last_ts = td->groupid;
1442 ts = &threadstats[j];
1444 ts->clat_percentiles = td->o.clat_percentiles;
1445 ts->percentile_precision = td->o.percentile_precision;
1446 memcpy(ts->percentile_list, td->o.percentile_list, sizeof(td->o.percentile_list));
1451 if (ts->groupid == -1) {
1453 * These are per-group shared already
1455 strncpy(ts->name, td->o.name, FIO_JOBNAME_SIZE - 1);
1456 if (td->o.description)
1457 strncpy(ts->description, td->o.description,
1458 FIO_JOBDESC_SIZE - 1);
1460 memset(ts->description, 0, FIO_JOBDESC_SIZE);
1463 * If multiple entries in this group, this is
1466 ts->thread_number = td->thread_number;
1467 ts->groupid = td->groupid;
1470 * first pid in group, not very useful...
1474 ts->kb_base = td->o.kb_base;
1475 ts->unit_base = td->o.unit_base;
1476 ts->unified_rw_rep = td->o.unified_rw_rep;
1477 } else if (ts->kb_base != td->o.kb_base && !kb_base_warned) {
1478 log_info("fio: kb_base differs for jobs in group, using"
1479 " %u as the base\n", ts->kb_base);
1481 } else if (ts->unit_base != td->o.unit_base && !unit_base_warned) {
1482 log_info("fio: unit_base differs for jobs in group, using"
1483 " %u as the base\n", ts->unit_base);
1484 unit_base_warned = 1;
1487 ts->continue_on_error = td->o.continue_on_error;
1488 ts->total_err_count += td->total_err_count;
1489 ts->first_error = td->first_error;
1491 if (!td->error && td->o.continue_on_error &&
1493 ts->error = td->first_error;
1494 ts->verror[sizeof(ts->verror) - 1] = '\0';
1495 strncpy(ts->verror, td->verror, sizeof(ts->verror) - 1);
1496 } else if (td->error) {
1497 ts->error = td->error;
1498 ts->verror[sizeof(ts->verror) - 1] = '\0';
1499 strncpy(ts->verror, td->verror, sizeof(ts->verror) - 1);
1503 ts->latency_depth = td->latency_qd;
1504 ts->latency_target = td->o.latency_target;
1505 ts->latency_percentile = td->o.latency_percentile;
1506 ts->latency_window = td->o.latency_window;
1508 ts->nr_block_infos = td->ts.nr_block_infos;
1509 for (k = 0; k < ts->nr_block_infos; k++)
1510 ts->block_infos[k] = td->ts.block_infos[k];
1512 sum_thread_stats(ts, &td->ts, idx);
1515 for (i = 0; i < nr_ts; i++) {
1516 unsigned long long bw;
1518 ts = &threadstats[i];
1519 rs = &runstats[ts->groupid];
1520 rs->kb_base = ts->kb_base;
1521 rs->unit_base = ts->unit_base;
1522 rs->unified_rw_rep += ts->unified_rw_rep;
1524 for (j = 0; j < DDIR_RWDIR_CNT; j++) {
1525 if (!ts->runtime[j])
1527 if (ts->runtime[j] < rs->min_run[j] || !rs->min_run[j])
1528 rs->min_run[j] = ts->runtime[j];
1529 if (ts->runtime[j] > rs->max_run[j])
1530 rs->max_run[j] = ts->runtime[j];
1533 if (ts->runtime[j]) {
1534 unsigned long runt = ts->runtime[j];
1535 unsigned long long kb;
1537 kb = ts->io_bytes[j] / rs->kb_base;
1538 bw = kb * 1000 / runt;
1540 if (bw < rs->min_bw[j])
1542 if (bw > rs->max_bw[j])
1545 rs->io_kb[j] += ts->io_bytes[j] / rs->kb_base;
1549 for (i = 0; i < groupid + 1; i++) {
1554 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) {
1555 if (rs->max_run[ddir])
1556 rs->agg[ddir] = (rs->io_kb[ddir] * 1000) /
1561 for (i = 0; i < FIO_OUTPUT_NR; i++)
1562 buf_output_init(&output[i]);
1565 * don't overwrite last signal output
1567 if (output_format & FIO_OUTPUT_NORMAL)
1568 log_buf(&output[__FIO_OUTPUT_NORMAL], "\n");
1569 if (output_format & FIO_OUTPUT_JSON) {
1574 os_ctime_r((const time_t *) &time_p, time_buf,
1576 time_buf[strlen(time_buf) - 1] = '\0';
1578 root = json_create_object();
1579 json_object_add_value_string(root, "fio version", fio_version_string);
1580 json_object_add_value_int(root, "timestamp", time_p);
1581 json_object_add_value_string(root, "time", time_buf);
1582 array = json_create_array();
1583 json_object_add_value_array(root, "jobs", array);
1586 for (i = 0; i < nr_ts; i++) {
1587 ts = &threadstats[i];
1588 rs = &runstats[ts->groupid];
1591 fio_server_send_ts(ts, rs);
1593 if (output_format & FIO_OUTPUT_TERSE)
1594 show_thread_status_terse(ts, rs, &output[__FIO_OUTPUT_TERSE]);
1595 if (output_format & FIO_OUTPUT_JSON) {
1596 struct json_object *tmp = show_thread_status_json(ts, rs);
1597 json_array_add_value_object(array, tmp);
1599 if (output_format & FIO_OUTPUT_NORMAL)
1600 show_thread_status_normal(ts, rs, &output[__FIO_OUTPUT_NORMAL]);
1603 if (output_format & FIO_OUTPUT_JSON) {
1604 /* disk util stats, if any */
1605 show_disk_util(1, root, &output[__FIO_OUTPUT_JSON]);
1607 show_idle_prof_stats(FIO_OUTPUT_JSON, root, &output[__FIO_OUTPUT_JSON]);
1609 json_print_object(root, &output[__FIO_OUTPUT_JSON]);
1610 log_buf(&output[__FIO_OUTPUT_JSON], "\n");
1611 json_free_object(root);
1614 for (i = 0; i < groupid + 1; i++) {
1619 fio_server_send_gs(rs);
1620 else if (output_format & FIO_OUTPUT_NORMAL)
1621 show_group_stats(rs, &output[__FIO_OUTPUT_NORMAL]);
1625 fio_server_send_du();
1626 else if (output_format & FIO_OUTPUT_NORMAL) {
1627 show_disk_util(0, NULL, &output[__FIO_OUTPUT_NORMAL]);
1628 show_idle_prof_stats(FIO_OUTPUT_NORMAL, NULL, &output[__FIO_OUTPUT_NORMAL]);
1631 for (i = 0; i < FIO_OUTPUT_NR; i++) {
1632 buf_output_flush(&output[i]);
1633 buf_output_free(&output[i]);
1641 void show_run_stats(void)
1643 fio_mutex_down(stat_mutex);
1645 fio_mutex_up(stat_mutex);
1648 void __show_running_run_stats(void)
1650 struct thread_data *td;
1651 unsigned long long *rt;
1655 fio_mutex_down(stat_mutex);
1657 rt = malloc(thread_number * sizeof(unsigned long long));
1658 fio_gettime(&tv, NULL);
1660 for_each_td(td, i) {
1661 rt[i] = mtime_since(&td->start, &tv);
1662 if (td_read(td) && td->io_bytes[DDIR_READ])
1663 td->ts.runtime[DDIR_READ] += rt[i];
1664 if (td_write(td) && td->io_bytes[DDIR_WRITE])
1665 td->ts.runtime[DDIR_WRITE] += rt[i];
1666 if (td_trim(td) && td->io_bytes[DDIR_TRIM])
1667 td->ts.runtime[DDIR_TRIM] += rt[i];
1669 td->update_rusage = 1;
1670 td->ts.io_bytes[DDIR_READ] = td->io_bytes[DDIR_READ];
1671 td->ts.io_bytes[DDIR_WRITE] = td->io_bytes[DDIR_WRITE];
1672 td->ts.io_bytes[DDIR_TRIM] = td->io_bytes[DDIR_TRIM];
1673 td->ts.total_run_time = mtime_since(&td->epoch, &tv);
1676 for_each_td(td, i) {
1677 if (td->runstate >= TD_EXITED)
1679 if (td->rusage_sem) {
1680 td->update_rusage = 1;
1681 fio_mutex_down(td->rusage_sem);
1683 td->update_rusage = 0;
1688 for_each_td(td, i) {
1689 if (td_read(td) && td->io_bytes[DDIR_READ])
1690 td->ts.runtime[DDIR_READ] -= rt[i];
1691 if (td_write(td) && td->io_bytes[DDIR_WRITE])
1692 td->ts.runtime[DDIR_WRITE] -= rt[i];
1693 if (td_trim(td) && td->io_bytes[DDIR_TRIM])
1694 td->ts.runtime[DDIR_TRIM] -= rt[i];
1698 fio_mutex_up(stat_mutex);
1701 static int status_interval_init;
1702 static struct timeval status_time;
1703 static int status_file_disabled;
1705 #define FIO_STATUS_FILE "fio-dump-status"
1707 static int check_status_file(void)
1710 const char *temp_dir;
1711 char fio_status_file_path[PATH_MAX];
1713 if (status_file_disabled)
1716 temp_dir = getenv("TMPDIR");
1717 if (temp_dir == NULL) {
1718 temp_dir = getenv("TEMP");
1719 if (temp_dir && strlen(temp_dir) >= PATH_MAX)
1722 if (temp_dir == NULL)
1725 snprintf(fio_status_file_path, sizeof(fio_status_file_path), "%s/%s", temp_dir, FIO_STATUS_FILE);
1727 if (stat(fio_status_file_path, &sb))
1730 if (unlink(fio_status_file_path) < 0) {
1731 log_err("fio: failed to unlink %s: %s\n", fio_status_file_path,
1733 log_err("fio: disabling status file updates\n");
1734 status_file_disabled = 1;
1740 void check_for_running_stats(void)
1742 if (status_interval) {
1743 if (!status_interval_init) {
1744 fio_gettime(&status_time, NULL);
1745 status_interval_init = 1;
1746 } else if (mtime_since_now(&status_time) >= status_interval) {
1747 show_running_run_stats();
1748 fio_gettime(&status_time, NULL);
1752 if (check_status_file()) {
1753 show_running_run_stats();
1758 static inline void add_stat_sample(struct io_stat *is, unsigned long data)
1763 if (data > is->max_val)
1765 if (data < is->min_val)
1768 delta = val - is->mean.u.f;
1770 is->mean.u.f += delta / (is->samples + 1.0);
1771 is->S.u.f += delta * (val - is->mean.u.f);
1777 static void __add_log_sample(struct io_log *iolog, unsigned long val,
1778 enum fio_ddir ddir, unsigned int bs,
1779 unsigned long t, uint64_t offset)
1781 uint64_t nr_samples = iolog->nr_samples;
1782 struct io_sample *s;
1784 if (iolog->disabled)
1787 if (!iolog->nr_samples)
1788 iolog->avg_last = t;
1790 if (iolog->nr_samples == iolog->max_samples) {
1794 new_size = 2 * iolog->max_samples * log_entry_sz(iolog);
1796 if (iolog->log_gz && (new_size > iolog->log_gz)) {
1797 if (iolog_flush(iolog, 0)) {
1798 log_err("fio: failed flushing iolog! Will stop logging.\n");
1799 iolog->disabled = 1;
1802 nr_samples = iolog->nr_samples;
1804 new_log = realloc(iolog->log, new_size);
1806 log_err("fio: failed extending iolog! Will stop logging.\n");
1807 iolog->disabled = 1;
1810 iolog->log = new_log;
1811 iolog->max_samples <<= 1;
1815 s = get_sample(iolog, nr_samples);
1819 io_sample_set_ddir(iolog, s, ddir);
1822 if (iolog->log_offset) {
1823 struct io_sample_offset *so = (void *) s;
1825 so->offset = offset;
1828 iolog->nr_samples++;
1831 static inline void reset_io_stat(struct io_stat *ios)
1833 ios->max_val = ios->min_val = ios->samples = 0;
1834 ios->mean.u.f = ios->S.u.f = 0;
1837 void reset_io_stats(struct thread_data *td)
1839 struct thread_stat *ts = &td->ts;
1842 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1843 reset_io_stat(&ts->clat_stat[i]);
1844 reset_io_stat(&ts->slat_stat[i]);
1845 reset_io_stat(&ts->lat_stat[i]);
1846 reset_io_stat(&ts->bw_stat[i]);
1847 reset_io_stat(&ts->iops_stat[i]);
1849 ts->io_bytes[i] = 0;
1852 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
1853 ts->io_u_plat[i][j] = 0;
1856 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
1857 ts->io_u_map[i] = 0;
1858 ts->io_u_submit[i] = 0;
1859 ts->io_u_complete[i] = 0;
1860 ts->io_u_lat_u[i] = 0;
1861 ts->io_u_lat_m[i] = 0;
1862 ts->total_submit = 0;
1863 ts->total_complete = 0;
1866 for (i = 0; i < 3; i++) {
1867 ts->total_io_u[i] = 0;
1868 ts->short_io_u[i] = 0;
1869 ts->drop_io_u[i] = 0;
1873 static void _add_stat_to_log(struct io_log *iolog, unsigned long elapsed)
1876 * Note an entry in the log. Use the mean from the logged samples,
1877 * making sure to properly round up. Only write a log entry if we
1878 * had actual samples done.
1880 if (iolog->avg_window[DDIR_READ].samples) {
1883 mr = iolog->avg_window[DDIR_READ].mean.u.f + 0.50;
1884 __add_log_sample(iolog, mr, DDIR_READ, 0, elapsed, 0);
1886 if (iolog->avg_window[DDIR_WRITE].samples) {
1889 mw = iolog->avg_window[DDIR_WRITE].mean.u.f + 0.50;
1890 __add_log_sample(iolog, mw, DDIR_WRITE, 0, elapsed, 0);
1892 if (iolog->avg_window[DDIR_TRIM].samples) {
1895 mw = iolog->avg_window[DDIR_TRIM].mean.u.f + 0.50;
1896 __add_log_sample(iolog, mw, DDIR_TRIM, 0, elapsed, 0);
1899 reset_io_stat(&iolog->avg_window[DDIR_READ]);
1900 reset_io_stat(&iolog->avg_window[DDIR_WRITE]);
1901 reset_io_stat(&iolog->avg_window[DDIR_TRIM]);
1904 static void add_log_sample(struct thread_data *td, struct io_log *iolog,
1905 unsigned long val, enum fio_ddir ddir,
1906 unsigned int bs, uint64_t offset)
1908 unsigned long elapsed, this_window;
1913 elapsed = mtime_since_now(&td->epoch);
1916 * If no time averaging, just add the log sample.
1918 if (!iolog->avg_msec) {
1919 __add_log_sample(iolog, val, ddir, bs, elapsed, offset);
1924 * Add the sample. If the time period has passed, then
1925 * add that entry to the log and clear.
1927 add_stat_sample(&iolog->avg_window[ddir], val);
1930 * If period hasn't passed, adding the above sample is all we
1933 this_window = elapsed - iolog->avg_last;
1934 if (this_window < iolog->avg_msec)
1937 _add_stat_to_log(iolog, elapsed);
1939 iolog->avg_last = elapsed;
1942 void finalize_logs(struct thread_data *td)
1944 unsigned long elapsed;
1946 elapsed = mtime_since_now(&td->epoch);
1949 _add_stat_to_log(td->clat_log, elapsed);
1951 _add_stat_to_log(td->slat_log, elapsed);
1953 _add_stat_to_log(td->lat_log, elapsed);
1955 _add_stat_to_log(td->bw_log, elapsed);
1957 _add_stat_to_log(td->iops_log, elapsed);
1960 void add_agg_sample(unsigned long val, enum fio_ddir ddir, unsigned int bs)
1962 struct io_log *iolog;
1967 iolog = agg_io_log[ddir];
1968 __add_log_sample(iolog, val, ddir, bs, mtime_since_genesis(), 0);
1971 static void add_clat_percentile_sample(struct thread_stat *ts,
1972 unsigned long usec, enum fio_ddir ddir)
1974 unsigned int idx = plat_val_to_idx(usec);
1975 assert(idx < FIO_IO_U_PLAT_NR);
1977 ts->io_u_plat[ddir][idx]++;
1980 void add_clat_sample(struct thread_data *td, enum fio_ddir ddir,
1981 unsigned long usec, unsigned int bs, uint64_t offset)
1983 struct thread_stat *ts = &td->ts;
1990 add_stat_sample(&ts->clat_stat[ddir], usec);
1993 add_log_sample(td, td->clat_log, usec, ddir, bs, offset);
1995 if (ts->clat_percentiles)
1996 add_clat_percentile_sample(ts, usec, ddir);
2001 void add_slat_sample(struct thread_data *td, enum fio_ddir ddir,
2002 unsigned long usec, unsigned int bs, uint64_t offset)
2004 struct thread_stat *ts = &td->ts;
2011 add_stat_sample(&ts->slat_stat[ddir], usec);
2014 add_log_sample(td, td->slat_log, usec, ddir, bs, offset);
2019 void add_lat_sample(struct thread_data *td, enum fio_ddir ddir,
2020 unsigned long usec, unsigned int bs, uint64_t offset)
2022 struct thread_stat *ts = &td->ts;
2029 add_stat_sample(&ts->lat_stat[ddir], usec);
2032 add_log_sample(td, td->lat_log, usec, ddir, bs, offset);
2037 void add_bw_sample(struct thread_data *td, enum fio_ddir ddir, unsigned int bs,
2040 struct thread_stat *ts = &td->ts;
2041 unsigned long spent, rate;
2046 spent = mtime_since(&td->bw_sample_time, t);
2047 if (spent < td->o.bw_avg_time)
2053 * Compute both read and write rates for the interval.
2055 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++) {
2058 delta = td->this_io_bytes[ddir] - td->stat_io_bytes[ddir];
2060 continue; /* No entries for interval */
2063 rate = delta * 1000 / spent / 1024;
2067 add_stat_sample(&ts->bw_stat[ddir], rate);
2070 add_log_sample(td, td->bw_log, rate, ddir, bs, 0);
2072 td->stat_io_bytes[ddir] = td->this_io_bytes[ddir];
2075 fio_gettime(&td->bw_sample_time, NULL);
2079 void add_iops_sample(struct thread_data *td, enum fio_ddir ddir, unsigned int bs,
2082 struct thread_stat *ts = &td->ts;
2083 unsigned long spent, iops;
2088 spent = mtime_since(&td->iops_sample_time, t);
2089 if (spent < td->o.iops_avg_time)
2095 * Compute both read and write rates for the interval.
2097 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++) {
2100 delta = td->this_io_blocks[ddir] - td->stat_io_blocks[ddir];
2102 continue; /* No entries for interval */
2105 iops = (delta * 1000) / spent;
2109 add_stat_sample(&ts->iops_stat[ddir], iops);
2112 add_log_sample(td, td->iops_log, iops, ddir, bs, 0);
2114 td->stat_io_blocks[ddir] = td->this_io_blocks[ddir];
2117 fio_gettime(&td->iops_sample_time, NULL);
2121 void stat_init(void)
2123 stat_mutex = fio_mutex_init(FIO_MUTEX_UNLOCKED);
2126 void stat_exit(void)
2129 * When we have the mutex, we know out-of-band access to it
2132 fio_mutex_down(stat_mutex);
2133 fio_mutex_remove(stat_mutex);
2137 * Called from signal handler. Wake up status thread.
2139 void show_running_run_stats(void)
2142 pthread_cond_signal(&helper_cond);
2145 uint32_t *io_u_block_info(struct thread_data *td, struct io_u *io_u)
2147 /* Ignore io_u's which span multiple blocks--they will just get
2148 * inaccurate counts. */
2149 int idx = (io_u->offset - io_u->file->file_offset)
2150 / td->o.bs[DDIR_TRIM];
2151 uint32_t *info = &td->ts.block_infos[idx];
2152 assert(idx < td->ts.nr_block_infos);