10 #include "lib/ieee754.h"
12 #include "lib/getrusage.h"
15 #include "lib/output_buffer.h"
16 #include "helper_thread.h"
19 #include "oslib/asprintf.h"
22 #define LOG_MSEC_SLACK 2
24 #define LOG_MSEC_SLACK 1
28 union io_sample_data data;
36 struct fio_sem *stat_sem;
38 void clear_rusage_stat(struct thread_data *td)
40 struct thread_stat *ts = &td->ts;
42 fio_getrusage(&td->ru_start);
43 ts->usr_time = ts->sys_time = 0;
45 ts->minf = ts->majf = 0;
48 void update_rusage_stat(struct thread_data *td)
50 struct thread_stat *ts = &td->ts;
52 fio_getrusage(&td->ru_end);
53 ts->usr_time += mtime_since_tv(&td->ru_start.ru_utime,
54 &td->ru_end.ru_utime);
55 ts->sys_time += mtime_since_tv(&td->ru_start.ru_stime,
56 &td->ru_end.ru_stime);
57 ts->ctx += td->ru_end.ru_nvcsw + td->ru_end.ru_nivcsw
58 - (td->ru_start.ru_nvcsw + td->ru_start.ru_nivcsw);
59 ts->minf += td->ru_end.ru_minflt - td->ru_start.ru_minflt;
60 ts->majf += td->ru_end.ru_majflt - td->ru_start.ru_majflt;
62 memcpy(&td->ru_start, &td->ru_end, sizeof(td->ru_end));
66 * Given a latency, return the index of the corresponding bucket in
67 * the structure tracking percentiles.
69 * (1) find the group (and error bits) that the value (latency)
70 * belongs to by looking at its MSB. (2) find the bucket number in the
71 * group by looking at the index bits.
74 static unsigned int plat_val_to_idx(unsigned long long val)
76 unsigned int msb, error_bits, base, offset, idx;
78 /* Find MSB starting from bit 0 */
82 msb = (sizeof(val)*8) - __builtin_clzll(val) - 1;
85 * MSB <= (FIO_IO_U_PLAT_BITS-1), cannot be rounded off. Use
86 * all bits of the sample as index
88 if (msb <= FIO_IO_U_PLAT_BITS)
91 /* Compute the number of error bits to discard*/
92 error_bits = msb - FIO_IO_U_PLAT_BITS;
94 /* Compute the number of buckets before the group */
95 base = (error_bits + 1) << FIO_IO_U_PLAT_BITS;
98 * Discard the error bits and apply the mask to find the
99 * index for the buckets in the group
101 offset = (FIO_IO_U_PLAT_VAL - 1) & (val >> error_bits);
103 /* Make sure the index does not exceed (array size - 1) */
104 idx = (base + offset) < (FIO_IO_U_PLAT_NR - 1) ?
105 (base + offset) : (FIO_IO_U_PLAT_NR - 1);
111 * Convert the given index of the bucket array to the value
112 * represented by the bucket
114 static unsigned long long plat_idx_to_val(unsigned int idx)
116 unsigned int error_bits;
117 unsigned long long k, base;
119 assert(idx < FIO_IO_U_PLAT_NR);
121 /* MSB <= (FIO_IO_U_PLAT_BITS-1), cannot be rounded off. Use
122 * all bits of the sample as index */
123 if (idx < (FIO_IO_U_PLAT_VAL << 1))
126 /* Find the group and compute the minimum value of that group */
127 error_bits = (idx >> FIO_IO_U_PLAT_BITS) - 1;
128 base = ((unsigned long long) 1) << (error_bits + FIO_IO_U_PLAT_BITS);
130 /* Find its bucket number of the group */
131 k = idx % FIO_IO_U_PLAT_VAL;
133 /* Return the mean of the range of the bucket */
134 return base + ((k + 0.5) * (1 << error_bits));
137 static int double_cmp(const void *a, const void *b)
139 const fio_fp64_t fa = *(const fio_fp64_t *) a;
140 const fio_fp64_t fb = *(const fio_fp64_t *) b;
145 else if (fa.u.f < fb.u.f)
151 unsigned int calc_clat_percentiles(uint64_t *io_u_plat, unsigned long long nr,
152 fio_fp64_t *plist, unsigned long long **output,
153 unsigned long long *maxv, unsigned long long *minv)
155 unsigned long long sum = 0;
156 unsigned int len, i, j = 0;
157 unsigned long long *ovals = NULL;
164 while (len < FIO_IO_U_LIST_MAX_LEN && plist[len].u.f != 0.0)
171 * Sort the percentile list. Note that it may already be sorted if
172 * we are using the default values, but since it's a short list this
173 * isn't a worry. Also note that this does not work for NaN values.
176 qsort(plist, len, sizeof(plist[0]), double_cmp);
178 ovals = malloc(len * sizeof(*ovals));
183 * Calculate bucket values, note down max and min values
186 for (i = 0; i < FIO_IO_U_PLAT_NR && !is_last; i++) {
188 while (sum >= ((long double) plist[j].u.f / 100.0 * nr)) {
189 assert(plist[j].u.f <= 100.0);
191 ovals[j] = plat_idx_to_val(i);
192 if (ovals[j] < *minv)
194 if (ovals[j] > *maxv)
197 is_last = (j == len - 1) != 0;
206 log_err("fio: error calculating latency percentiles\n");
213 * Find and display the p-th percentile of clat
215 static void show_clat_percentiles(uint64_t *io_u_plat, unsigned long long nr,
216 fio_fp64_t *plist, unsigned int precision,
217 const char *pre, struct buf_output *out)
219 unsigned int divisor, len, i, j = 0;
220 unsigned long long minv, maxv;
221 unsigned long long *ovals;
222 int per_line, scale_down, time_width;
226 len = calc_clat_percentiles(io_u_plat, nr, plist, &ovals, &maxv, &minv);
231 * We default to nsecs, but if the value range is such that we
232 * should scale down to usecs or msecs, do that.
234 if (minv > 2000000 && maxv > 99999999ULL) {
237 log_buf(out, " %s percentiles (msec):\n |", pre);
238 } else if (minv > 2000 && maxv > 99999) {
241 log_buf(out, " %s percentiles (usec):\n |", pre);
245 log_buf(out, " %s percentiles (nsec):\n |", pre);
249 time_width = max(5, (int) (log10(maxv / divisor) + 1));
250 snprintf(fmt, sizeof(fmt), " %%%u.%ufth=[%%%dllu]%%c", precision + 3,
251 precision, time_width);
252 /* fmt will be something like " %5.2fth=[%4llu]%c" */
253 per_line = (80 - 7) / (precision + 10 + time_width);
255 for (j = 0; j < len; j++) {
257 if (j != 0 && (j % per_line) == 0)
260 /* end of the list */
261 is_last = (j == len - 1) != 0;
263 for (i = 0; i < scale_down; i++)
264 ovals[j] = (ovals[j] + 999) / 1000;
266 log_buf(out, fmt, plist[j].u.f, ovals[j], is_last ? '\n' : ',');
271 if ((j % per_line) == per_line - 1) /* for formatting */
278 static int get_nr_prios_with_samples(struct thread_stat *ts, enum fio_ddir ddir)
280 int i, nr_prios_with_samples = 0;
282 for (i = 0; i < ts->nr_clat_prio[ddir]; i++) {
283 if (ts->clat_prio[ddir][i].clat_stat.samples)
284 nr_prios_with_samples++;
287 return nr_prios_with_samples;
290 bool calc_lat(struct io_stat *is, unsigned long long *min,
291 unsigned long long *max, double *mean, double *dev)
293 double n = (double) is->samples;
300 *mean = is->mean.u.f;
303 *dev = sqrt(is->S.u.f / (n - 1.0));
310 void show_mixed_group_stats(struct group_run_stats *rs, struct buf_output *out)
312 char *io, *agg, *min, *max;
313 char *ioalt, *aggalt, *minalt, *maxalt;
314 uint64_t io_mix = 0, agg_mix = 0, min_mix = -1, max_mix = 0;
315 uint64_t min_run = -1, max_run = 0;
316 const int i2p = is_power_of_2(rs->kb_base);
319 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
322 io_mix += rs->iobytes[i];
323 agg_mix += rs->agg[i];
324 min_mix = min_mix < rs->min_bw[i] ? min_mix : rs->min_bw[i];
325 max_mix = max_mix > rs->max_bw[i] ? max_mix : rs->max_bw[i];
326 min_run = min_run < rs->min_run[i] ? min_run : rs->min_run[i];
327 max_run = max_run > rs->max_run[i] ? max_run : rs->max_run[i];
329 io = num2str(io_mix, rs->sig_figs, 1, i2p, N2S_BYTE);
330 ioalt = num2str(io_mix, rs->sig_figs, 1, !i2p, N2S_BYTE);
331 agg = num2str(agg_mix, rs->sig_figs, 1, i2p, rs->unit_base);
332 aggalt = num2str(agg_mix, rs->sig_figs, 1, !i2p, rs->unit_base);
333 min = num2str(min_mix, rs->sig_figs, 1, i2p, rs->unit_base);
334 minalt = num2str(min_mix, rs->sig_figs, 1, !i2p, rs->unit_base);
335 max = num2str(max_mix, rs->sig_figs, 1, i2p, rs->unit_base);
336 maxalt = num2str(max_mix, rs->sig_figs, 1, !i2p, rs->unit_base);
337 log_buf(out, " MIXED: bw=%s (%s), %s-%s (%s-%s), io=%s (%s), run=%llu-%llumsec\n",
338 agg, aggalt, min, max, minalt, maxalt, io, ioalt,
339 (unsigned long long) min_run,
340 (unsigned long long) max_run);
351 void show_group_stats(struct group_run_stats *rs, struct buf_output *out)
353 char *io, *agg, *min, *max;
354 char *ioalt, *aggalt, *minalt, *maxalt;
355 const char *str[] = { " READ", " WRITE" , " TRIM"};
358 log_buf(out, "\nRun status group %d (all jobs):\n", rs->groupid);
360 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
361 const int i2p = is_power_of_2(rs->kb_base);
366 io = num2str(rs->iobytes[i], rs->sig_figs, 1, i2p, N2S_BYTE);
367 ioalt = num2str(rs->iobytes[i], rs->sig_figs, 1, !i2p, N2S_BYTE);
368 agg = num2str(rs->agg[i], rs->sig_figs, 1, i2p, rs->unit_base);
369 aggalt = num2str(rs->agg[i], rs->sig_figs, 1, !i2p, rs->unit_base);
370 min = num2str(rs->min_bw[i], rs->sig_figs, 1, i2p, rs->unit_base);
371 minalt = num2str(rs->min_bw[i], rs->sig_figs, 1, !i2p, rs->unit_base);
372 max = num2str(rs->max_bw[i], rs->sig_figs, 1, i2p, rs->unit_base);
373 maxalt = num2str(rs->max_bw[i], rs->sig_figs, 1, !i2p, rs->unit_base);
374 log_buf(out, "%s: bw=%s (%s), %s-%s (%s-%s), io=%s (%s), run=%llu-%llumsec\n",
375 (rs->unified_rw_rep == UNIFIED_MIXED) ? " MIXED" : str[i],
376 agg, aggalt, min, max, minalt, maxalt, io, ioalt,
377 (unsigned long long) rs->min_run[i],
378 (unsigned long long) rs->max_run[i]);
390 /* Need to aggregate statistics to show mixed values */
391 if (rs->unified_rw_rep == UNIFIED_BOTH)
392 show_mixed_group_stats(rs, out);
395 void stat_calc_dist(uint64_t *map, unsigned long total, double *io_u_dist)
400 * Do depth distribution calculations
402 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
404 io_u_dist[i] = (double) map[i] / (double) total;
405 io_u_dist[i] *= 100.0;
406 if (io_u_dist[i] < 0.1 && map[i])
413 static void stat_calc_lat(struct thread_stat *ts, double *dst,
414 uint64_t *src, int nr)
416 unsigned long total = ddir_rw_sum(ts->total_io_u);
420 * Do latency distribution calculations
422 for (i = 0; i < nr; i++) {
424 dst[i] = (double) src[i] / (double) total;
426 if (dst[i] < 0.01 && src[i])
434 * To keep the terse format unaltered, add all of the ns latency
435 * buckets to the first us latency bucket
437 static void stat_calc_lat_nu(struct thread_stat *ts, double *io_u_lat_u)
439 unsigned long ntotal = 0, total = ddir_rw_sum(ts->total_io_u);
442 stat_calc_lat(ts, io_u_lat_u, ts->io_u_lat_u, FIO_IO_U_LAT_U_NR);
444 for (i = 0; i < FIO_IO_U_LAT_N_NR; i++)
445 ntotal += ts->io_u_lat_n[i];
447 io_u_lat_u[0] += 100.0 * (double) ntotal / (double) total;
450 void stat_calc_lat_n(struct thread_stat *ts, double *io_u_lat)
452 stat_calc_lat(ts, io_u_lat, ts->io_u_lat_n, FIO_IO_U_LAT_N_NR);
455 void stat_calc_lat_u(struct thread_stat *ts, double *io_u_lat)
457 stat_calc_lat(ts, io_u_lat, ts->io_u_lat_u, FIO_IO_U_LAT_U_NR);
460 void stat_calc_lat_m(struct thread_stat *ts, double *io_u_lat)
462 stat_calc_lat(ts, io_u_lat, ts->io_u_lat_m, FIO_IO_U_LAT_M_NR);
465 static void display_lat(const char *name, unsigned long long min,
466 unsigned long long max, double mean, double dev,
467 struct buf_output *out)
469 const char *base = "(nsec)";
472 if (nsec_to_msec(&min, &max, &mean, &dev))
474 else if (nsec_to_usec(&min, &max, &mean, &dev))
477 minp = num2str(min, 6, 1, 0, N2S_NONE);
478 maxp = num2str(max, 6, 1, 0, N2S_NONE);
480 log_buf(out, " %s %s: min=%s, max=%s, avg=%5.02f,"
481 " stdev=%5.02f\n", name, base, minp, maxp, mean, dev);
487 static struct thread_stat *gen_mixed_ddir_stats_from_ts(struct thread_stat *ts)
489 struct thread_stat *ts_lcl;
492 * Handle aggregation of Reads (ddir = 0), Writes (ddir = 1), and
495 ts_lcl = malloc(sizeof(struct thread_stat));
497 log_err("fio: failed to allocate local thread stat\n");
501 init_thread_stat(ts_lcl);
503 /* calculate mixed stats */
504 ts_lcl->unified_rw_rep = UNIFIED_MIXED;
505 ts_lcl->lat_percentiles = ts->lat_percentiles;
506 ts_lcl->clat_percentiles = ts->clat_percentiles;
507 ts_lcl->slat_percentiles = ts->slat_percentiles;
508 ts_lcl->percentile_precision = ts->percentile_precision;
509 memcpy(ts_lcl->percentile_list, ts->percentile_list, sizeof(ts->percentile_list));
510 ts_lcl->sig_figs = ts->sig_figs;
512 sum_thread_stats(ts_lcl, ts);
517 static double convert_agg_kbytes_percent(struct group_run_stats *rs,
518 enum fio_ddir ddir, int mean)
520 double p_of_agg = 100.0;
521 if (rs && rs->agg[ddir] > 1024) {
522 p_of_agg = mean * 100.0 / (double) (rs->agg[ddir] / 1024.0);
524 if (p_of_agg > 100.0)
530 static void show_ddir_status(struct group_run_stats *rs, struct thread_stat *ts,
531 enum fio_ddir ddir, struct buf_output *out)
534 unsigned long long min, max, bw, iops;
536 char *io_p, *bw_p, *bw_p_alt, *iops_p, *post_st = NULL;
538 const char *clat_type = ts->lat_percentiles ? "lat" : "clat";
540 if (ddir_sync(ddir)) {
541 if (calc_lat(&ts->sync_stat, &min, &max, &mean, &dev)) {
542 log_buf(out, " %s:\n", "fsync/fdatasync/sync_file_range");
543 display_lat(io_ddir_name(ddir), min, max, mean, dev, out);
544 show_clat_percentiles(ts->io_u_sync_plat,
545 ts->sync_stat.samples,
547 ts->percentile_precision,
548 io_ddir_name(ddir), out);
553 assert(ddir_rw(ddir));
555 if (!ts->runtime[ddir])
558 i2p = is_power_of_2(rs->kb_base);
559 runt = ts->runtime[ddir];
561 bw = (1000 * ts->io_bytes[ddir]) / runt;
562 io_p = num2str(ts->io_bytes[ddir], ts->sig_figs, 1, i2p, N2S_BYTE);
563 bw_p = num2str(bw, ts->sig_figs, 1, i2p, ts->unit_base);
564 bw_p_alt = num2str(bw, ts->sig_figs, 1, !i2p, ts->unit_base);
566 iops = (1000 * (uint64_t)ts->total_io_u[ddir]) / runt;
567 iops_p = num2str(iops, ts->sig_figs, 1, 0, N2S_NONE);
568 if (ddir == DDIR_WRITE || ddir == DDIR_TRIM)
569 post_st = zbd_write_status(ts);
570 else if (ddir == DDIR_READ && ts->cachehit && ts->cachemiss) {
574 total = ts->cachehit + ts->cachemiss;
575 hit = (double) ts->cachehit / (double) total;
577 if (asprintf(&post_st, "; Cachehit=%0.2f%%", hit) < 0)
581 log_buf(out, " %s: IOPS=%s, BW=%s (%s)(%s/%llumsec)%s\n",
582 (ts->unified_rw_rep == UNIFIED_MIXED) ? "mixed" : io_ddir_name(ddir),
583 iops_p, bw_p, bw_p_alt, io_p,
584 (unsigned long long) ts->runtime[ddir],
593 if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev))
594 display_lat("slat", min, max, mean, dev, out);
595 if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev))
596 display_lat("clat", min, max, mean, dev, out);
597 if (calc_lat(&ts->lat_stat[ddir], &min, &max, &mean, &dev))
598 display_lat(" lat", min, max, mean, dev, out);
600 /* Only print per prio stats if there are >= 2 prios with samples */
601 if (get_nr_prios_with_samples(ts, ddir) >= 2) {
602 for (i = 0; i < ts->nr_clat_prio[ddir]; i++) {
605 if (!calc_lat(&ts->clat_prio[ddir][i].clat_stat, &min,
609 snprintf(buf, sizeof(buf),
612 ioprio_class(ts->clat_prio[ddir][i].ioprio),
613 ioprio(ts->clat_prio[ddir][i].ioprio),
614 ioprio_hint(ts->clat_prio[ddir][i].ioprio));
615 display_lat(buf, min, max, mean, dev, out);
619 if (ts->slat_percentiles && ts->slat_stat[ddir].samples > 0)
620 show_clat_percentiles(ts->io_u_plat[FIO_SLAT][ddir],
621 ts->slat_stat[ddir].samples,
623 ts->percentile_precision, "slat", out);
624 if (ts->clat_percentiles && ts->clat_stat[ddir].samples > 0)
625 show_clat_percentiles(ts->io_u_plat[FIO_CLAT][ddir],
626 ts->clat_stat[ddir].samples,
628 ts->percentile_precision, "clat", out);
629 if (ts->lat_percentiles && ts->lat_stat[ddir].samples > 0)
630 show_clat_percentiles(ts->io_u_plat[FIO_LAT][ddir],
631 ts->lat_stat[ddir].samples,
633 ts->percentile_precision, "lat", out);
635 if (ts->clat_percentiles || ts->lat_percentiles) {
639 if (ts->lat_percentiles)
640 samples = ts->lat_stat[ddir].samples;
642 samples = ts->clat_stat[ddir].samples;
644 /* Only print per prio stats if there are >= 2 prios with samples */
645 if (get_nr_prios_with_samples(ts, ddir) >= 2) {
646 for (i = 0; i < ts->nr_clat_prio[ddir]; i++) {
647 uint64_t prio_samples =
648 ts->clat_prio[ddir][i].clat_stat.samples;
653 snprintf(prio_name, sizeof(prio_name),
654 "%s prio %u/%u/%u (%.2f%% of IOs)",
656 ioprio_class(ts->clat_prio[ddir][i].ioprio),
657 ioprio(ts->clat_prio[ddir][i].ioprio),
658 ioprio_hint(ts->clat_prio[ddir][i].ioprio),
659 100. * (double) prio_samples / (double) samples);
660 show_clat_percentiles(ts->clat_prio[ddir][i].io_u_plat,
661 prio_samples, ts->percentile_list,
662 ts->percentile_precision,
668 if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
669 double p_of_agg = 100.0, fkb_base = (double)rs->kb_base;
672 if ((rs->unit_base == 1) && i2p)
674 else if (rs->unit_base == 1)
681 p_of_agg = convert_agg_kbytes_percent(rs, ddir, mean);
683 if (rs->unit_base == 1) {
690 if (mean > fkb_base * fkb_base) {
695 bw_str = (rs->unit_base == 1 ? "Mibit" : "MiB");
698 log_buf(out, " bw (%5s/s): min=%5llu, max=%5llu, per=%3.2f%%, "
699 "avg=%5.02f, stdev=%5.02f, samples=%" PRIu64 "\n",
700 bw_str, min, max, p_of_agg, mean, dev,
701 (&ts->bw_stat[ddir])->samples);
703 if (calc_lat(&ts->iops_stat[ddir], &min, &max, &mean, &dev)) {
704 log_buf(out, " iops : min=%5llu, max=%5llu, "
705 "avg=%5.02f, stdev=%5.02f, samples=%" PRIu64 "\n",
706 min, max, mean, dev, (&ts->iops_stat[ddir])->samples);
710 static void show_mixed_ddir_status(struct group_run_stats *rs,
711 struct thread_stat *ts,
712 struct buf_output *out)
714 struct thread_stat *ts_lcl = gen_mixed_ddir_stats_from_ts(ts);
717 show_ddir_status(rs, ts_lcl, DDIR_READ, out);
719 free_clat_prio_stats(ts_lcl);
723 static bool show_lat(double *io_u_lat, int nr, const char **ranges,
724 const char *msg, struct buf_output *out)
726 bool new_line = true, shown = false;
729 for (i = 0; i < nr; i++) {
730 if (io_u_lat[i] <= 0.0)
736 log_buf(out, " lat (%s) : ", msg);
742 log_buf(out, "%s%3.2f%%", ranges[i], io_u_lat[i]);
754 static void show_lat_n(double *io_u_lat_n, struct buf_output *out)
756 const char *ranges[] = { "2=", "4=", "10=", "20=", "50=", "100=",
757 "250=", "500=", "750=", "1000=", };
759 show_lat(io_u_lat_n, FIO_IO_U_LAT_N_NR, ranges, "nsec", out);
762 static void show_lat_u(double *io_u_lat_u, struct buf_output *out)
764 const char *ranges[] = { "2=", "4=", "10=", "20=", "50=", "100=",
765 "250=", "500=", "750=", "1000=", };
767 show_lat(io_u_lat_u, FIO_IO_U_LAT_U_NR, ranges, "usec", out);
770 static void show_lat_m(double *io_u_lat_m, struct buf_output *out)
772 const char *ranges[] = { "2=", "4=", "10=", "20=", "50=", "100=",
773 "250=", "500=", "750=", "1000=", "2000=",
776 show_lat(io_u_lat_m, FIO_IO_U_LAT_M_NR, ranges, "msec", out);
779 static void show_latencies(struct thread_stat *ts, struct buf_output *out)
781 double io_u_lat_n[FIO_IO_U_LAT_N_NR];
782 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
783 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
785 stat_calc_lat_n(ts, io_u_lat_n);
786 stat_calc_lat_u(ts, io_u_lat_u);
787 stat_calc_lat_m(ts, io_u_lat_m);
789 show_lat_n(io_u_lat_n, out);
790 show_lat_u(io_u_lat_u, out);
791 show_lat_m(io_u_lat_m, out);
794 static int block_state_category(int block_state)
796 switch (block_state) {
797 case BLOCK_STATE_UNINIT:
799 case BLOCK_STATE_TRIMMED:
800 case BLOCK_STATE_WRITTEN:
802 case BLOCK_STATE_WRITE_FAILURE:
803 case BLOCK_STATE_TRIM_FAILURE:
806 /* Silence compile warning on some BSDs and have a return */
812 static int compare_block_infos(const void *bs1, const void *bs2)
814 uint64_t block1 = *(uint64_t *)bs1;
815 uint64_t block2 = *(uint64_t *)bs2;
816 int state1 = BLOCK_INFO_STATE(block1);
817 int state2 = BLOCK_INFO_STATE(block2);
818 int bscat1 = block_state_category(state1);
819 int bscat2 = block_state_category(state2);
820 int cycles1 = BLOCK_INFO_TRIMS(block1);
821 int cycles2 = BLOCK_INFO_TRIMS(block2);
828 if (cycles1 < cycles2)
830 if (cycles1 > cycles2)
838 assert(block1 == block2);
842 static int calc_block_percentiles(int nr_block_infos, uint32_t *block_infos,
843 fio_fp64_t *plist, unsigned int **percentiles,
849 qsort(block_infos, nr_block_infos, sizeof(uint32_t), compare_block_infos);
851 while (len < FIO_IO_U_LIST_MAX_LEN && plist[len].u.f != 0.0)
858 * Sort the percentile list. Note that it may already be sorted if
859 * we are using the default values, but since it's a short list this
860 * isn't a worry. Also note that this does not work for NaN values.
863 qsort(plist, len, sizeof(plist[0]), double_cmp);
865 /* Start only after the uninit entries end */
867 nr_uninit < nr_block_infos
868 && BLOCK_INFO_STATE(block_infos[nr_uninit]) == BLOCK_STATE_UNINIT;
872 if (nr_uninit == nr_block_infos)
875 *percentiles = calloc(len, sizeof(**percentiles));
877 for (i = 0; i < len; i++) {
878 int idx = (plist[i].u.f * (nr_block_infos - nr_uninit) / 100)
880 (*percentiles)[i] = BLOCK_INFO_TRIMS(block_infos[idx]);
883 memset(types, 0, sizeof(*types) * BLOCK_STATE_COUNT);
884 for (i = 0; i < nr_block_infos; i++)
885 types[BLOCK_INFO_STATE(block_infos[i])]++;
890 static const char *block_state_names[] = {
891 [BLOCK_STATE_UNINIT] = "unwritten",
892 [BLOCK_STATE_TRIMMED] = "trimmed",
893 [BLOCK_STATE_WRITTEN] = "written",
894 [BLOCK_STATE_TRIM_FAILURE] = "trim failure",
895 [BLOCK_STATE_WRITE_FAILURE] = "write failure",
898 static void show_block_infos(int nr_block_infos, uint32_t *block_infos,
899 fio_fp64_t *plist, struct buf_output *out)
902 unsigned int *percentiles = NULL;
903 unsigned int block_state_counts[BLOCK_STATE_COUNT];
905 len = calc_block_percentiles(nr_block_infos, block_infos, plist,
906 &percentiles, block_state_counts);
908 log_buf(out, " block lifetime percentiles :\n |");
910 for (i = 0; i < len; i++) {
911 uint32_t block_info = percentiles[i];
912 #define LINE_LENGTH 75
913 char str[LINE_LENGTH];
914 int strln = snprintf(str, LINE_LENGTH, " %3.2fth=%u%c",
915 plist[i].u.f, block_info,
916 i == len - 1 ? '\n' : ',');
917 assert(strln < LINE_LENGTH);
918 if (pos + strln > LINE_LENGTH) {
920 log_buf(out, "\n |");
922 log_buf(out, "%s", str);
929 log_buf(out, " states :");
930 for (i = 0; i < BLOCK_STATE_COUNT; i++)
931 log_buf(out, " %s=%u%c",
932 block_state_names[i], block_state_counts[i],
933 i == BLOCK_STATE_COUNT - 1 ? '\n' : ',');
936 static void show_ss_normal(struct thread_stat *ts, struct buf_output *out)
938 char *p1, *p1alt, *p2;
939 unsigned long long bw_mean, iops_mean;
940 const int i2p = is_power_of_2(ts->kb_base);
945 bw_mean = steadystate_bw_mean(ts);
946 iops_mean = steadystate_iops_mean(ts);
948 p1 = num2str(bw_mean / ts->kb_base, ts->sig_figs, ts->kb_base, i2p, ts->unit_base);
949 p1alt = num2str(bw_mean / ts->kb_base, ts->sig_figs, ts->kb_base, !i2p, ts->unit_base);
950 p2 = num2str(iops_mean, ts->sig_figs, 1, 0, N2S_NONE);
952 log_buf(out, " steadystate : attained=%s, bw=%s (%s), iops=%s, %s%s=%.3f%s\n",
953 ts->ss_state & FIO_SS_ATTAINED ? "yes" : "no",
955 ts->ss_state & FIO_SS_IOPS ? "iops" : "bw",
956 ts->ss_state & FIO_SS_SLOPE ? " slope": " mean dev",
957 ts->ss_criterion.u.f,
958 ts->ss_state & FIO_SS_PCT ? "%" : "");
965 static void show_agg_stats(struct disk_util_agg *agg, int terse,
966 struct buf_output *out)
968 if (!agg->slavecount)
972 log_buf(out, ", aggrios=%llu/%llu, aggsectors=%llu/%llu, "
973 "aggrmerge=%llu/%llu, aggrticks=%llu/%llu, "
974 "aggrin_queue=%llu, aggrutil=%3.2f%%",
975 (unsigned long long) agg->ios[0] / agg->slavecount,
976 (unsigned long long) agg->ios[1] / agg->slavecount,
977 (unsigned long long) agg->sectors[0] / agg->slavecount,
978 (unsigned long long) agg->sectors[1] / agg->slavecount,
979 (unsigned long long) agg->merges[0] / agg->slavecount,
980 (unsigned long long) agg->merges[1] / agg->slavecount,
981 (unsigned long long) agg->ticks[0] / agg->slavecount,
982 (unsigned long long) agg->ticks[1] / agg->slavecount,
983 (unsigned long long) agg->time_in_queue / agg->slavecount,
986 log_buf(out, ";slaves;%llu;%llu;%llu;%llu;%llu;%llu;%llu;%3.2f%%",
987 (unsigned long long) agg->ios[0] / agg->slavecount,
988 (unsigned long long) agg->ios[1] / agg->slavecount,
989 (unsigned long long) agg->merges[0] / agg->slavecount,
990 (unsigned long long) agg->merges[1] / agg->slavecount,
991 (unsigned long long) agg->ticks[0] / agg->slavecount,
992 (unsigned long long) agg->ticks[1] / agg->slavecount,
993 (unsigned long long) agg->time_in_queue / agg->slavecount,
998 static void aggregate_slaves_stats(struct disk_util *masterdu)
1000 struct disk_util_agg *agg = &masterdu->agg;
1001 struct disk_util_stat *dus;
1002 struct flist_head *entry;
1003 struct disk_util *slavedu;
1006 flist_for_each(entry, &masterdu->slaves) {
1007 slavedu = flist_entry(entry, struct disk_util, slavelist);
1008 dus = &slavedu->dus;
1009 agg->ios[0] += dus->s.ios[0];
1010 agg->ios[1] += dus->s.ios[1];
1011 agg->merges[0] += dus->s.merges[0];
1012 agg->merges[1] += dus->s.merges[1];
1013 agg->sectors[0] += dus->s.sectors[0];
1014 agg->sectors[1] += dus->s.sectors[1];
1015 agg->ticks[0] += dus->s.ticks[0];
1016 agg->ticks[1] += dus->s.ticks[1];
1017 agg->time_in_queue += dus->s.time_in_queue;
1020 util = (double) (100 * dus->s.io_ticks / (double) slavedu->dus.s.msec);
1021 /* System utilization is the utilization of the
1022 * component with the highest utilization.
1024 if (util > agg->max_util.u.f)
1025 agg->max_util.u.f = util;
1029 if (agg->max_util.u.f > 100.0)
1030 agg->max_util.u.f = 100.0;
1033 void print_disk_util(struct disk_util_stat *dus, struct disk_util_agg *agg,
1034 int terse, struct buf_output *out)
1039 util = (double) 100 * dus->s.io_ticks / (double) dus->s.msec;
1044 if (agg->slavecount)
1047 log_buf(out, " %s: ios=%llu/%llu, sectors=%llu/%llu, "
1048 "merge=%llu/%llu, ticks=%llu/%llu, in_queue=%llu, "
1051 (unsigned long long) dus->s.ios[0],
1052 (unsigned long long) dus->s.ios[1],
1053 (unsigned long long) dus->s.sectors[0],
1054 (unsigned long long) dus->s.sectors[1],
1055 (unsigned long long) dus->s.merges[0],
1056 (unsigned long long) dus->s.merges[1],
1057 (unsigned long long) dus->s.ticks[0],
1058 (unsigned long long) dus->s.ticks[1],
1059 (unsigned long long) dus->s.time_in_queue,
1062 log_buf(out, ";%s;%llu;%llu;%llu;%llu;%llu;%llu;%llu;%3.2f%%",
1064 (unsigned long long) dus->s.ios[0],
1065 (unsigned long long) dus->s.ios[1],
1066 (unsigned long long) dus->s.merges[0],
1067 (unsigned long long) dus->s.merges[1],
1068 (unsigned long long) dus->s.ticks[0],
1069 (unsigned long long) dus->s.ticks[1],
1070 (unsigned long long) dus->s.time_in_queue,
1075 * If the device has slaves, aggregate the stats for
1076 * those slave devices also.
1078 show_agg_stats(agg, terse, out);
1084 void json_array_add_disk_util(struct disk_util_stat *dus,
1085 struct disk_util_agg *agg, struct json_array *array)
1087 struct json_object *obj;
1091 util = (double) 100 * dus->s.io_ticks / (double) dus->s.msec;
1095 obj = json_create_object();
1096 json_array_add_value_object(array, obj);
1098 json_object_add_value_string(obj, "name", (const char *)dus->name);
1099 json_object_add_value_int(obj, "read_ios", dus->s.ios[0]);
1100 json_object_add_value_int(obj, "write_ios", dus->s.ios[1]);
1101 json_object_add_value_int(obj, "read_sectors", dus->s.sectors[0]);
1102 json_object_add_value_int(obj, "write_sectors", dus->s.sectors[1]);
1103 json_object_add_value_int(obj, "read_merges", dus->s.merges[0]);
1104 json_object_add_value_int(obj, "write_merges", dus->s.merges[1]);
1105 json_object_add_value_int(obj, "read_ticks", dus->s.ticks[0]);
1106 json_object_add_value_int(obj, "write_ticks", dus->s.ticks[1]);
1107 json_object_add_value_int(obj, "in_queue", dus->s.time_in_queue);
1108 json_object_add_value_float(obj, "util", util);
1111 * If the device has slaves, aggregate the stats for
1112 * those slave devices also.
1114 if (!agg->slavecount)
1116 json_object_add_value_int(obj, "aggr_read_ios",
1117 agg->ios[0] / agg->slavecount);
1118 json_object_add_value_int(obj, "aggr_write_ios",
1119 agg->ios[1] / agg->slavecount);
1120 json_object_add_value_int(obj, "aggr_read_sectors",
1121 agg->sectors[0] / agg->slavecount);
1122 json_object_add_value_int(obj, "aggr_write_sectors",
1123 agg->sectors[1] / agg->slavecount);
1124 json_object_add_value_int(obj, "aggr_read_merges",
1125 agg->merges[0] / agg->slavecount);
1126 json_object_add_value_int(obj, "aggr_write_merge",
1127 agg->merges[1] / agg->slavecount);
1128 json_object_add_value_int(obj, "aggr_read_ticks",
1129 agg->ticks[0] / agg->slavecount);
1130 json_object_add_value_int(obj, "aggr_write_ticks",
1131 agg->ticks[1] / agg->slavecount);
1132 json_object_add_value_int(obj, "aggr_in_queue",
1133 agg->time_in_queue / agg->slavecount);
1134 json_object_add_value_float(obj, "aggr_util", agg->max_util.u.f);
1137 static void json_object_add_disk_utils(struct json_object *obj,
1138 struct flist_head *head)
1140 struct json_array *array = json_create_array();
1141 struct flist_head *entry;
1142 struct disk_util *du;
1144 json_object_add_value_array(obj, "disk_util", array);
1146 flist_for_each(entry, head) {
1147 du = flist_entry(entry, struct disk_util, list);
1149 aggregate_slaves_stats(du);
1150 json_array_add_disk_util(&du->dus, &du->agg, array);
1154 void show_disk_util(int terse, struct json_object *parent,
1155 struct buf_output *out)
1157 struct flist_head *entry;
1158 struct disk_util *du;
1161 if (!is_running_backend())
1164 if (flist_empty(&disk_list))
1167 if ((output_format & FIO_OUTPUT_JSON) && parent)
1172 if (!terse && !do_json)
1173 log_buf(out, "\nDisk stats (read/write):\n");
1176 json_object_add_disk_utils(parent, &disk_list);
1177 } else if (output_format & ~(FIO_OUTPUT_JSON | FIO_OUTPUT_JSON_PLUS)) {
1178 flist_for_each(entry, &disk_list) {
1179 du = flist_entry(entry, struct disk_util, list);
1181 aggregate_slaves_stats(du);
1182 print_disk_util(&du->dus, &du->agg, terse, out);
1187 static void show_thread_status_normal(struct thread_stat *ts,
1188 struct group_run_stats *rs,
1189 struct buf_output *out)
1191 double usr_cpu, sys_cpu;
1192 unsigned long runtime;
1193 double io_u_dist[FIO_IO_U_MAP_NR];
1197 if (!ddir_rw_sum(ts->io_bytes) && !ddir_rw_sum(ts->total_io_u))
1200 memset(time_buf, 0, sizeof(time_buf));
1203 os_ctime_r((const time_t *) &time_p, time_buf, sizeof(time_buf));
1206 log_buf(out, "%s: (groupid=%d, jobs=%d): err=%2d: pid=%d: %s",
1207 ts->name, ts->groupid, ts->members,
1208 ts->error, (int) ts->pid, time_buf);
1210 log_buf(out, "%s: (groupid=%d, jobs=%d): err=%2d (%s): pid=%d: %s",
1211 ts->name, ts->groupid, ts->members,
1212 ts->error, ts->verror, (int) ts->pid,
1216 if (strlen(ts->description))
1217 log_buf(out, " Description : [%s]\n", ts->description);
1219 for_each_rw_ddir(ddir) {
1220 if (ts->io_bytes[ddir])
1221 show_ddir_status(rs, ts, ddir, out);
1224 if (ts->unified_rw_rep == UNIFIED_BOTH)
1225 show_mixed_ddir_status(rs, ts, out);
1227 show_latencies(ts, out);
1229 if (ts->sync_stat.samples)
1230 show_ddir_status(rs, ts, DDIR_SYNC, out);
1232 runtime = ts->total_run_time;
1234 double runt = (double) runtime;
1236 usr_cpu = (double) ts->usr_time * 100 / runt;
1237 sys_cpu = (double) ts->sys_time * 100 / runt;
1243 log_buf(out, " cpu : usr=%3.2f%%, sys=%3.2f%%, ctx=%llu,"
1244 " majf=%llu, minf=%llu\n", usr_cpu, sys_cpu,
1245 (unsigned long long) ts->ctx,
1246 (unsigned long long) ts->majf,
1247 (unsigned long long) ts->minf);
1249 stat_calc_dist(ts->io_u_map, ddir_rw_sum(ts->total_io_u), io_u_dist);
1250 log_buf(out, " IO depths : 1=%3.1f%%, 2=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%,"
1251 " 16=%3.1f%%, 32=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
1252 io_u_dist[1], io_u_dist[2],
1253 io_u_dist[3], io_u_dist[4],
1254 io_u_dist[5], io_u_dist[6]);
1256 stat_calc_dist(ts->io_u_submit, ts->total_submit, io_u_dist);
1257 log_buf(out, " submit : 0=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%, 16=%3.1f%%,"
1258 " 32=%3.1f%%, 64=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
1259 io_u_dist[1], io_u_dist[2],
1260 io_u_dist[3], io_u_dist[4],
1261 io_u_dist[5], io_u_dist[6]);
1262 stat_calc_dist(ts->io_u_complete, ts->total_complete, io_u_dist);
1263 log_buf(out, " complete : 0=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%, 16=%3.1f%%,"
1264 " 32=%3.1f%%, 64=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
1265 io_u_dist[1], io_u_dist[2],
1266 io_u_dist[3], io_u_dist[4],
1267 io_u_dist[5], io_u_dist[6]);
1268 log_buf(out, " issued rwts: total=%llu,%llu,%llu,%llu"
1269 " short=%llu,%llu,%llu,0"
1270 " dropped=%llu,%llu,%llu,0\n",
1271 (unsigned long long) ts->total_io_u[0],
1272 (unsigned long long) ts->total_io_u[1],
1273 (unsigned long long) ts->total_io_u[2],
1274 (unsigned long long) ts->total_io_u[3],
1275 (unsigned long long) ts->short_io_u[0],
1276 (unsigned long long) ts->short_io_u[1],
1277 (unsigned long long) ts->short_io_u[2],
1278 (unsigned long long) ts->drop_io_u[0],
1279 (unsigned long long) ts->drop_io_u[1],
1280 (unsigned long long) ts->drop_io_u[2]);
1281 if (ts->continue_on_error) {
1282 log_buf(out, " errors : total=%llu, first_error=%d/<%s>\n",
1283 (unsigned long long)ts->total_err_count,
1285 strerror(ts->first_error));
1287 if (ts->latency_depth) {
1288 log_buf(out, " latency : target=%llu, window=%llu, percentile=%.2f%%, depth=%u\n",
1289 (unsigned long long)ts->latency_target,
1290 (unsigned long long)ts->latency_window,
1291 ts->latency_percentile.u.f,
1295 if (ts->nr_block_infos)
1296 show_block_infos(ts->nr_block_infos, ts->block_infos,
1297 ts->percentile_list, out);
1300 show_ss_normal(ts, out);
1303 static void show_ddir_status_terse(struct thread_stat *ts,
1304 struct group_run_stats *rs,
1305 enum fio_ddir ddir, int ver,
1306 struct buf_output *out)
1308 unsigned long long min, max, minv, maxv, bw, iops;
1309 unsigned long long *ovals = NULL;
1314 assert(ddir_rw(ddir));
1317 if (ts->runtime[ddir]) {
1318 uint64_t runt = ts->runtime[ddir];
1320 bw = ((1000 * ts->io_bytes[ddir]) / runt) / 1024; /* KiB/s */
1321 iops = (1000 * (uint64_t) ts->total_io_u[ddir]) / runt;
1324 log_buf(out, ";%llu;%llu;%llu;%llu",
1325 (unsigned long long) ts->io_bytes[ddir] >> 10, bw, iops,
1326 (unsigned long long) ts->runtime[ddir]);
1328 if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev))
1329 log_buf(out, ";%llu;%llu;%f;%f", min/1000, max/1000, mean/1000, dev/1000);
1331 log_buf(out, ";%llu;%llu;%f;%f", 0ULL, 0ULL, 0.0, 0.0);
1333 if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev))
1334 log_buf(out, ";%llu;%llu;%f;%f", min/1000, max/1000, mean/1000, dev/1000);
1336 log_buf(out, ";%llu;%llu;%f;%f", 0ULL, 0ULL, 0.0, 0.0);
1338 if (ts->lat_percentiles) {
1339 len = calc_clat_percentiles(ts->io_u_plat[FIO_LAT][ddir],
1340 ts->lat_stat[ddir].samples,
1341 ts->percentile_list, &ovals, &maxv,
1343 } else if (ts->clat_percentiles) {
1344 len = calc_clat_percentiles(ts->io_u_plat[FIO_CLAT][ddir],
1345 ts->clat_stat[ddir].samples,
1346 ts->percentile_list, &ovals, &maxv,
1352 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
1354 log_buf(out, ";0%%=0");
1357 log_buf(out, ";%f%%=%llu", ts->percentile_list[i].u.f, ovals[i]/1000);
1360 if (calc_lat(&ts->lat_stat[ddir], &min, &max, &mean, &dev))
1361 log_buf(out, ";%llu;%llu;%f;%f", min/1000, max/1000, mean/1000, dev/1000);
1363 log_buf(out, ";%llu;%llu;%f;%f", 0ULL, 0ULL, 0.0, 0.0);
1367 bw_stat = calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev);
1369 double p_of_agg = 100.0;
1371 if (rs->agg[ddir]) {
1372 p_of_agg = mean * 100 / (double) (rs->agg[ddir] / 1024);
1373 if (p_of_agg > 100.0)
1377 log_buf(out, ";%llu;%llu;%f%%;%f;%f", min, max, p_of_agg, mean, dev);
1379 log_buf(out, ";%llu;%llu;%f%%;%f;%f", 0ULL, 0ULL, 0.0, 0.0, 0.0);
1384 log_buf(out, ";%" PRIu64, (&ts->bw_stat[ddir])->samples);
1386 log_buf(out, ";%lu", 0UL);
1388 if (calc_lat(&ts->iops_stat[ddir], &min, &max, &mean, &dev))
1389 log_buf(out, ";%llu;%llu;%f;%f;%" PRIu64, min, max,
1390 mean, dev, (&ts->iops_stat[ddir])->samples);
1392 log_buf(out, ";%llu;%llu;%f;%f;%lu", 0ULL, 0ULL, 0.0, 0.0, 0UL);
1396 static void show_mixed_ddir_status_terse(struct thread_stat *ts,
1397 struct group_run_stats *rs,
1398 int ver, struct buf_output *out)
1400 struct thread_stat *ts_lcl = gen_mixed_ddir_stats_from_ts(ts);
1403 show_ddir_status_terse(ts_lcl, rs, DDIR_READ, ver, out);
1405 free_clat_prio_stats(ts_lcl);
1409 static struct json_object *add_ddir_lat_json(struct thread_stat *ts,
1410 uint32_t percentiles,
1411 struct io_stat *lat_stat,
1412 uint64_t *io_u_plat)
1416 unsigned int i, len;
1417 struct json_object *lat_object, *percentile_object, *clat_bins_object;
1418 unsigned long long min, max, maxv, minv, *ovals = NULL;
1420 if (!calc_lat(lat_stat, &min, &max, &mean, &dev)) {
1424 lat_object = json_create_object();
1425 json_object_add_value_int(lat_object, "min", min);
1426 json_object_add_value_int(lat_object, "max", max);
1427 json_object_add_value_float(lat_object, "mean", mean);
1428 json_object_add_value_float(lat_object, "stddev", dev);
1429 json_object_add_value_int(lat_object, "N", lat_stat->samples);
1431 if (percentiles && lat_stat->samples) {
1432 len = calc_clat_percentiles(io_u_plat, lat_stat->samples,
1433 ts->percentile_list, &ovals, &maxv, &minv);
1435 if (len > FIO_IO_U_LIST_MAX_LEN)
1436 len = FIO_IO_U_LIST_MAX_LEN;
1438 percentile_object = json_create_object();
1439 json_object_add_value_object(lat_object, "percentile", percentile_object);
1440 for (i = 0; i < len; i++) {
1441 snprintf(buf, sizeof(buf), "%f", ts->percentile_list[i].u.f);
1442 json_object_add_value_int(percentile_object, buf, ovals[i]);
1446 if (output_format & FIO_OUTPUT_JSON_PLUS) {
1447 clat_bins_object = json_create_object();
1448 json_object_add_value_object(lat_object, "bins", clat_bins_object);
1450 for(i = 0; i < FIO_IO_U_PLAT_NR; i++)
1452 snprintf(buf, sizeof(buf), "%llu", plat_idx_to_val(i));
1453 json_object_add_value_int(clat_bins_object, buf, io_u_plat[i]);
1461 static void add_ddir_status_json(struct thread_stat *ts,
1462 struct group_run_stats *rs, enum fio_ddir ddir,
1463 struct json_object *parent)
1465 unsigned long long min, max;
1466 unsigned long long bw_bytes, bw;
1467 double mean, dev, iops;
1468 struct json_object *dir_object, *tmp_object;
1469 double p_of_agg = 100.0;
1471 assert(ddir_rw(ddir) || ddir_sync(ddir));
1473 if ((ts->unified_rw_rep == UNIFIED_MIXED) && ddir != DDIR_READ)
1476 dir_object = json_create_object();
1477 json_object_add_value_object(parent,
1478 (ts->unified_rw_rep == UNIFIED_MIXED) ? "mixed" : io_ddir_name(ddir), dir_object);
1480 if (ddir_rw(ddir)) {
1484 if (ts->runtime[ddir]) {
1485 uint64_t runt = ts->runtime[ddir];
1487 bw_bytes = ((1000 * ts->io_bytes[ddir]) / runt); /* Bytes/s */
1488 bw = bw_bytes / 1024; /* KiB/s */
1489 iops = (1000.0 * (uint64_t) ts->total_io_u[ddir]) / runt;
1492 json_object_add_value_int(dir_object, "io_bytes", ts->io_bytes[ddir]);
1493 json_object_add_value_int(dir_object, "io_kbytes", ts->io_bytes[ddir] >> 10);
1494 json_object_add_value_int(dir_object, "bw_bytes", bw_bytes);
1495 json_object_add_value_int(dir_object, "bw", bw);
1496 json_object_add_value_float(dir_object, "iops", iops);
1497 json_object_add_value_int(dir_object, "runtime", ts->runtime[ddir]);
1498 json_object_add_value_int(dir_object, "total_ios", ts->total_io_u[ddir]);
1499 json_object_add_value_int(dir_object, "short_ios", ts->short_io_u[ddir]);
1500 json_object_add_value_int(dir_object, "drop_ios", ts->drop_io_u[ddir]);
1502 tmp_object = add_ddir_lat_json(ts, ts->slat_percentiles,
1503 &ts->slat_stat[ddir], ts->io_u_plat[FIO_SLAT][ddir]);
1504 json_object_add_value_object(dir_object, "slat_ns", tmp_object);
1506 tmp_object = add_ddir_lat_json(ts, ts->clat_percentiles,
1507 &ts->clat_stat[ddir], ts->io_u_plat[FIO_CLAT][ddir]);
1508 json_object_add_value_object(dir_object, "clat_ns", tmp_object);
1510 tmp_object = add_ddir_lat_json(ts, ts->lat_percentiles,
1511 &ts->lat_stat[ddir], ts->io_u_plat[FIO_LAT][ddir]);
1512 json_object_add_value_object(dir_object, "lat_ns", tmp_object);
1514 json_object_add_value_int(dir_object, "total_ios", ts->total_io_u[DDIR_SYNC]);
1515 tmp_object = add_ddir_lat_json(ts, ts->lat_percentiles | ts->clat_percentiles,
1516 &ts->sync_stat, ts->io_u_sync_plat);
1517 json_object_add_value_object(dir_object, "lat_ns", tmp_object);
1523 /* Only include per prio stats if there are >= 2 prios with samples */
1524 if (get_nr_prios_with_samples(ts, ddir) >= 2) {
1525 struct json_array *array = json_create_array();
1526 const char *obj_name;
1529 if (ts->lat_percentiles)
1530 obj_name = "lat_ns";
1532 obj_name = "clat_ns";
1534 json_object_add_value_array(dir_object, "prios", array);
1536 for (i = 0; i < ts->nr_clat_prio[ddir]; i++) {
1537 struct json_object *obj;
1539 if (!ts->clat_prio[ddir][i].clat_stat.samples)
1542 obj = json_create_object();
1544 json_object_add_value_int(obj, "prioclass",
1545 ioprio_class(ts->clat_prio[ddir][i].ioprio));
1546 json_object_add_value_int(obj, "prio",
1547 ioprio(ts->clat_prio[ddir][i].ioprio));
1548 json_object_add_value_int(obj, "priohint",
1549 ioprio_hint(ts->clat_prio[ddir][i].ioprio));
1551 tmp_object = add_ddir_lat_json(ts,
1552 ts->clat_percentiles | ts->lat_percentiles,
1553 &ts->clat_prio[ddir][i].clat_stat,
1554 ts->clat_prio[ddir][i].io_u_plat);
1555 json_object_add_value_object(obj, obj_name, tmp_object);
1556 json_array_add_value_object(array, obj);
1560 if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
1561 p_of_agg = convert_agg_kbytes_percent(rs, ddir, mean);
1564 p_of_agg = mean = dev = 0.0;
1567 json_object_add_value_int(dir_object, "bw_min", min);
1568 json_object_add_value_int(dir_object, "bw_max", max);
1569 json_object_add_value_float(dir_object, "bw_agg", p_of_agg);
1570 json_object_add_value_float(dir_object, "bw_mean", mean);
1571 json_object_add_value_float(dir_object, "bw_dev", dev);
1572 json_object_add_value_int(dir_object, "bw_samples",
1573 (&ts->bw_stat[ddir])->samples);
1575 if (!calc_lat(&ts->iops_stat[ddir], &min, &max, &mean, &dev)) {
1579 json_object_add_value_int(dir_object, "iops_min", min);
1580 json_object_add_value_int(dir_object, "iops_max", max);
1581 json_object_add_value_float(dir_object, "iops_mean", mean);
1582 json_object_add_value_float(dir_object, "iops_stddev", dev);
1583 json_object_add_value_int(dir_object, "iops_samples",
1584 (&ts->iops_stat[ddir])->samples);
1586 if (ts->cachehit + ts->cachemiss) {
1590 total = ts->cachehit + ts->cachemiss;
1591 hit = (double) ts->cachehit / (double) total;
1593 json_object_add_value_float(dir_object, "cachehit", hit);
1597 static void add_mixed_ddir_status_json(struct thread_stat *ts,
1598 struct group_run_stats *rs, struct json_object *parent)
1600 struct thread_stat *ts_lcl = gen_mixed_ddir_stats_from_ts(ts);
1602 /* add the aggregated stats to json parent */
1604 add_ddir_status_json(ts_lcl, rs, DDIR_READ, parent);
1606 free_clat_prio_stats(ts_lcl);
1610 static void show_thread_status_terse_all(struct thread_stat *ts,
1611 struct group_run_stats *rs, int ver,
1612 struct buf_output *out)
1614 double io_u_dist[FIO_IO_U_MAP_NR];
1615 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
1616 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
1617 double usr_cpu, sys_cpu;
1622 log_buf(out, "2;%s;%d;%d", ts->name, ts->groupid, ts->error);
1624 log_buf(out, "%d;%s;%s;%d;%d", ver, fio_version_string,
1625 ts->name, ts->groupid, ts->error);
1627 /* Log Read Status, or mixed if unified_rw_rep = 1 */
1628 show_ddir_status_terse(ts, rs, DDIR_READ, ver, out);
1629 if (ts->unified_rw_rep != UNIFIED_MIXED) {
1630 /* Log Write Status */
1631 show_ddir_status_terse(ts, rs, DDIR_WRITE, ver, out);
1632 /* Log Trim Status */
1633 if (ver == 2 || ver == 4 || ver == 5)
1634 show_ddir_status_terse(ts, rs, DDIR_TRIM, ver, out);
1636 if (ts->unified_rw_rep == UNIFIED_BOTH)
1637 show_mixed_ddir_status_terse(ts, rs, ver, out);
1639 if (ts->total_run_time) {
1640 double runt = (double) ts->total_run_time;
1642 usr_cpu = (double) ts->usr_time * 100 / runt;
1643 sys_cpu = (double) ts->sys_time * 100 / runt;
1649 log_buf(out, ";%f%%;%f%%;%llu;%llu;%llu", usr_cpu, sys_cpu,
1650 (unsigned long long) ts->ctx,
1651 (unsigned long long) ts->majf,
1652 (unsigned long long) ts->minf);
1654 /* Calc % distribution of IO depths, usecond, msecond latency */
1655 stat_calc_dist(ts->io_u_map, ddir_rw_sum(ts->total_io_u), io_u_dist);
1656 stat_calc_lat_nu(ts, io_u_lat_u);
1657 stat_calc_lat_m(ts, io_u_lat_m);
1659 /* Only show fixed 7 I/O depth levels*/
1660 log_buf(out, ";%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%",
1661 io_u_dist[0], io_u_dist[1], io_u_dist[2], io_u_dist[3],
1662 io_u_dist[4], io_u_dist[5], io_u_dist[6]);
1664 /* Microsecond latency */
1665 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
1666 log_buf(out, ";%3.2f%%", io_u_lat_u[i]);
1667 /* Millisecond latency */
1668 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
1669 log_buf(out, ";%3.2f%%", io_u_lat_m[i]);
1671 /* disk util stats, if any */
1672 if (ver >= 3 && is_running_backend())
1673 show_disk_util(1, NULL, out);
1675 /* Additional output if continue_on_error set - default off*/
1676 if (ts->continue_on_error)
1677 log_buf(out, ";%llu;%d", (unsigned long long) ts->total_err_count, ts->first_error);
1679 /* Additional output if description is set */
1680 if (strlen(ts->description)) {
1683 log_buf(out, ";%s", ts->description);
1689 static void json_add_job_opts(struct json_object *root, const char *name,
1690 struct flist_head *opt_list)
1692 struct json_object *dir_object;
1693 struct flist_head *entry;
1694 struct print_option *p;
1696 if (flist_empty(opt_list))
1699 dir_object = json_create_object();
1700 json_object_add_value_object(root, name, dir_object);
1702 flist_for_each(entry, opt_list) {
1703 p = flist_entry(entry, struct print_option, list);
1704 json_object_add_value_string(dir_object, p->name, p->value);
1708 static struct json_object *show_thread_status_json(struct thread_stat *ts,
1709 struct group_run_stats *rs,
1710 struct flist_head *opt_list)
1712 struct json_object *root, *tmp;
1713 struct jobs_eta *je;
1714 double io_u_dist[FIO_IO_U_MAP_NR];
1715 double io_u_lat_n[FIO_IO_U_LAT_N_NR];
1716 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
1717 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
1718 double usr_cpu, sys_cpu;
1722 root = json_create_object();
1723 json_object_add_value_string(root, "jobname", ts->name);
1724 json_object_add_value_int(root, "groupid", ts->groupid);
1725 json_object_add_value_int(root, "job_start", ts->job_start);
1726 json_object_add_value_int(root, "error", ts->error);
1729 je = get_jobs_eta(true, &size);
1731 json_object_add_value_int(root, "eta", je->eta_sec);
1732 json_object_add_value_int(root, "elapsed", je->elapsed_sec);
1737 json_add_job_opts(root, "job options", opt_list);
1739 add_ddir_status_json(ts, rs, DDIR_READ, root);
1740 add_ddir_status_json(ts, rs, DDIR_WRITE, root);
1741 add_ddir_status_json(ts, rs, DDIR_TRIM, root);
1742 add_ddir_status_json(ts, rs, DDIR_SYNC, root);
1744 if (ts->unified_rw_rep == UNIFIED_BOTH)
1745 add_mixed_ddir_status_json(ts, rs, root);
1748 if (ts->total_run_time) {
1749 double runt = (double) ts->total_run_time;
1751 usr_cpu = (double) ts->usr_time * 100 / runt;
1752 sys_cpu = (double) ts->sys_time * 100 / runt;
1757 json_object_add_value_int(root, "job_runtime", ts->total_run_time);
1758 json_object_add_value_float(root, "usr_cpu", usr_cpu);
1759 json_object_add_value_float(root, "sys_cpu", sys_cpu);
1760 json_object_add_value_int(root, "ctx", ts->ctx);
1761 json_object_add_value_int(root, "majf", ts->majf);
1762 json_object_add_value_int(root, "minf", ts->minf);
1764 /* Calc % distribution of IO depths */
1765 stat_calc_dist(ts->io_u_map, ddir_rw_sum(ts->total_io_u), io_u_dist);
1766 tmp = json_create_object();
1767 json_object_add_value_object(root, "iodepth_level", tmp);
1768 /* Only show fixed 7 I/O depth levels*/
1769 for (i = 0; i < 7; i++) {
1772 snprintf(name, 20, "%d", 1 << i);
1774 snprintf(name, 20, ">=%d", 1 << i);
1775 json_object_add_value_float(tmp, (const char *)name, io_u_dist[i]);
1778 /* Calc % distribution of submit IO depths */
1779 stat_calc_dist(ts->io_u_submit, ts->total_submit, io_u_dist);
1780 tmp = json_create_object();
1781 json_object_add_value_object(root, "iodepth_submit", tmp);
1782 /* Only show fixed 7 I/O depth levels*/
1783 for (i = 0; i < 7; i++) {
1786 snprintf(name, 20, "0");
1788 snprintf(name, 20, "%d", 1 << (i+1));
1790 snprintf(name, 20, ">=%d", 1 << i);
1791 json_object_add_value_float(tmp, (const char *)name, io_u_dist[i]);
1794 /* Calc % distribution of completion IO depths */
1795 stat_calc_dist(ts->io_u_complete, ts->total_complete, io_u_dist);
1796 tmp = json_create_object();
1797 json_object_add_value_object(root, "iodepth_complete", tmp);
1798 /* Only show fixed 7 I/O depth levels*/
1799 for (i = 0; i < 7; i++) {
1802 snprintf(name, 20, "0");
1804 snprintf(name, 20, "%d", 1 << (i+1));
1806 snprintf(name, 20, ">=%d", 1 << i);
1807 json_object_add_value_float(tmp, (const char *)name, io_u_dist[i]);
1810 /* Calc % distribution of nsecond, usecond, msecond latency */
1811 stat_calc_dist(ts->io_u_map, ddir_rw_sum(ts->total_io_u), io_u_dist);
1812 stat_calc_lat_n(ts, io_u_lat_n);
1813 stat_calc_lat_u(ts, io_u_lat_u);
1814 stat_calc_lat_m(ts, io_u_lat_m);
1816 /* Nanosecond latency */
1817 tmp = json_create_object();
1818 json_object_add_value_object(root, "latency_ns", tmp);
1819 for (i = 0; i < FIO_IO_U_LAT_N_NR; i++) {
1820 const char *ranges[] = { "2", "4", "10", "20", "50", "100",
1821 "250", "500", "750", "1000", };
1822 json_object_add_value_float(tmp, ranges[i], io_u_lat_n[i]);
1824 /* Microsecond latency */
1825 tmp = json_create_object();
1826 json_object_add_value_object(root, "latency_us", tmp);
1827 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
1828 const char *ranges[] = { "2", "4", "10", "20", "50", "100",
1829 "250", "500", "750", "1000", };
1830 json_object_add_value_float(tmp, ranges[i], io_u_lat_u[i]);
1832 /* Millisecond latency */
1833 tmp = json_create_object();
1834 json_object_add_value_object(root, "latency_ms", tmp);
1835 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++) {
1836 const char *ranges[] = { "2", "4", "10", "20", "50", "100",
1837 "250", "500", "750", "1000", "2000",
1839 json_object_add_value_float(tmp, ranges[i], io_u_lat_m[i]);
1842 /* Additional output if continue_on_error set - default off*/
1843 if (ts->continue_on_error) {
1844 json_object_add_value_int(root, "total_err", ts->total_err_count);
1845 json_object_add_value_int(root, "first_error", ts->first_error);
1848 if (ts->latency_depth) {
1849 json_object_add_value_int(root, "latency_depth", ts->latency_depth);
1850 json_object_add_value_int(root, "latency_target", ts->latency_target);
1851 json_object_add_value_float(root, "latency_percentile", ts->latency_percentile.u.f);
1852 json_object_add_value_int(root, "latency_window", ts->latency_window);
1855 /* Additional output if description is set */
1856 if (strlen(ts->description))
1857 json_object_add_value_string(root, "desc", ts->description);
1859 if (ts->nr_block_infos) {
1860 /* Block error histogram and types */
1862 unsigned int *percentiles = NULL;
1863 unsigned int block_state_counts[BLOCK_STATE_COUNT];
1865 len = calc_block_percentiles(ts->nr_block_infos, ts->block_infos,
1866 ts->percentile_list,
1867 &percentiles, block_state_counts);
1870 struct json_object *block, *percentile_object, *states;
1872 block = json_create_object();
1873 json_object_add_value_object(root, "block", block);
1875 percentile_object = json_create_object();
1876 json_object_add_value_object(block, "percentiles",
1878 for (i = 0; i < len; i++) {
1880 snprintf(buf, sizeof(buf), "%f",
1881 ts->percentile_list[i].u.f);
1882 json_object_add_value_int(percentile_object,
1887 states = json_create_object();
1888 json_object_add_value_object(block, "states", states);
1889 for (state = 0; state < BLOCK_STATE_COUNT; state++) {
1890 json_object_add_value_int(states,
1891 block_state_names[state],
1892 block_state_counts[state]);
1899 struct json_object *data;
1900 struct json_array *iops, *bw;
1903 int intervals = ts->ss_dur / (ss_check_interval / 1000L);
1905 snprintf(ss_buf, sizeof(ss_buf), "%s%s:%f%s",
1906 ts->ss_state & FIO_SS_IOPS ? "iops" : "bw",
1907 ts->ss_state & FIO_SS_SLOPE ? "_slope" : "",
1908 (float) ts->ss_limit.u.f,
1909 ts->ss_state & FIO_SS_PCT ? "%" : "");
1911 tmp = json_create_object();
1912 json_object_add_value_object(root, "steadystate", tmp);
1913 json_object_add_value_string(tmp, "ss", ss_buf);
1914 json_object_add_value_int(tmp, "duration", (int)ts->ss_dur);
1915 json_object_add_value_int(tmp, "attained", (ts->ss_state & FIO_SS_ATTAINED) > 0);
1917 snprintf(ss_buf, sizeof(ss_buf), "%f%s", (float) ts->ss_criterion.u.f,
1918 ts->ss_state & FIO_SS_PCT ? "%" : "");
1919 json_object_add_value_string(tmp, "criterion", ss_buf);
1920 json_object_add_value_float(tmp, "max_deviation", ts->ss_deviation.u.f);
1921 json_object_add_value_float(tmp, "slope", ts->ss_slope.u.f);
1923 data = json_create_object();
1924 json_object_add_value_object(tmp, "data", data);
1925 bw = json_create_array();
1926 iops = json_create_array();
1929 ** if ss was attained or the buffer is not full,
1930 ** ss->head points to the first element in the list.
1931 ** otherwise it actually points to the second element
1934 if ((ts->ss_state & FIO_SS_ATTAINED) || !(ts->ss_state & FIO_SS_BUFFER_FULL))
1937 j = ts->ss_head == 0 ? intervals - 1 : ts->ss_head - 1;
1938 for (l = 0; l < intervals; l++) {
1939 k = (j + l) % intervals;
1940 json_array_add_value_int(bw, ts->ss_bw_data[k]);
1941 json_array_add_value_int(iops, ts->ss_iops_data[k]);
1943 json_object_add_value_int(data, "bw_mean", steadystate_bw_mean(ts));
1944 json_object_add_value_int(data, "iops_mean", steadystate_iops_mean(ts));
1945 json_object_add_value_array(data, "iops", iops);
1946 json_object_add_value_array(data, "bw", bw);
1952 static void show_thread_status_terse(struct thread_stat *ts,
1953 struct group_run_stats *rs,
1954 struct buf_output *out)
1956 if (terse_version >= 2 && terse_version <= 5)
1957 show_thread_status_terse_all(ts, rs, terse_version, out);
1959 log_err("fio: bad terse version!? %d\n", terse_version);
1962 struct json_object *show_thread_status(struct thread_stat *ts,
1963 struct group_run_stats *rs,
1964 struct flist_head *opt_list,
1965 struct buf_output *out)
1967 struct json_object *ret = NULL;
1969 if (output_format & FIO_OUTPUT_TERSE)
1970 show_thread_status_terse(ts, rs, out);
1971 if (output_format & FIO_OUTPUT_JSON)
1972 ret = show_thread_status_json(ts, rs, opt_list);
1973 if (output_format & FIO_OUTPUT_NORMAL)
1974 show_thread_status_normal(ts, rs, out);
1979 static void __sum_stat(struct io_stat *dst, struct io_stat *src, bool first)
1983 dst->min_val = min(dst->min_val, src->min_val);
1984 dst->max_val = max(dst->max_val, src->max_val);
1987 * Compute new mean and S after the merge
1988 * <http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
1989 * #Parallel_algorithm>
1992 mean = src->mean.u.f;
1995 double delta = src->mean.u.f - dst->mean.u.f;
1997 mean = ((src->mean.u.f * src->samples) +
1998 (dst->mean.u.f * dst->samples)) /
1999 (dst->samples + src->samples);
2001 S = src->S.u.f + dst->S.u.f + pow(delta, 2.0) *
2002 (dst->samples * src->samples) /
2003 (dst->samples + src->samples);
2006 dst->samples += src->samples;
2007 dst->mean.u.f = mean;
2013 * We sum two kinds of stats - one that is time based, in which case we
2014 * apply the proper summing technique, and then one that is iops/bw
2015 * numbers. For group_reporting, we should just add those up, not make
2016 * them the mean of everything.
2018 static void sum_stat(struct io_stat *dst, struct io_stat *src, bool pure_sum)
2020 bool first = dst->samples == 0;
2022 if (src->samples == 0)
2026 __sum_stat(dst, src, first);
2031 dst->min_val = src->min_val;
2032 dst->max_val = src->max_val;
2033 dst->samples = src->samples;
2034 dst->mean.u.f = src->mean.u.f;
2035 dst->S.u.f = src->S.u.f;
2037 dst->min_val += src->min_val;
2038 dst->max_val += src->max_val;
2039 dst->samples += src->samples;
2040 dst->mean.u.f += src->mean.u.f;
2041 dst->S.u.f += src->S.u.f;
2045 void sum_group_stats(struct group_run_stats *dst, struct group_run_stats *src)
2049 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
2050 if (dst->max_run[i] < src->max_run[i])
2051 dst->max_run[i] = src->max_run[i];
2052 if (dst->min_run[i] && dst->min_run[i] > src->min_run[i])
2053 dst->min_run[i] = src->min_run[i];
2054 if (dst->max_bw[i] < src->max_bw[i])
2055 dst->max_bw[i] = src->max_bw[i];
2056 if (dst->min_bw[i] && dst->min_bw[i] > src->min_bw[i])
2057 dst->min_bw[i] = src->min_bw[i];
2059 dst->iobytes[i] += src->iobytes[i];
2060 dst->agg[i] += src->agg[i];
2064 dst->kb_base = src->kb_base;
2065 if (!dst->unit_base)
2066 dst->unit_base = src->unit_base;
2068 dst->sig_figs = src->sig_figs;
2072 * Free the clat_prio_stat arrays allocated by alloc_clat_prio_stat_ddir().
2074 void free_clat_prio_stats(struct thread_stat *ts)
2081 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) {
2082 sfree(ts->clat_prio[ddir]);
2083 ts->clat_prio[ddir] = NULL;
2084 ts->nr_clat_prio[ddir] = 0;
2089 * Allocate a clat_prio_stat array. The array has to be allocated/freed using
2090 * smalloc/sfree, so that it is accessible by the process/thread summing the
2093 int alloc_clat_prio_stat_ddir(struct thread_stat *ts, enum fio_ddir ddir,
2096 struct clat_prio_stat *clat_prio;
2099 clat_prio = scalloc(nr_prios, sizeof(*ts->clat_prio[ddir]));
2101 log_err("fio: failed to allocate ts clat data\n");
2105 for (i = 0; i < nr_prios; i++)
2106 clat_prio[i].clat_stat.min_val = ULONG_MAX;
2108 ts->clat_prio[ddir] = clat_prio;
2109 ts->nr_clat_prio[ddir] = nr_prios;
2114 static int grow_clat_prio_stat(struct thread_stat *dst, enum fio_ddir ddir)
2116 int curr_len = dst->nr_clat_prio[ddir];
2119 new_arr = scalloc(curr_len + 1, sizeof(*dst->clat_prio[ddir]));
2121 log_err("fio: failed to grow clat prio array\n");
2125 memcpy(new_arr, dst->clat_prio[ddir],
2126 curr_len * sizeof(*dst->clat_prio[ddir]));
2127 sfree(dst->clat_prio[ddir]);
2129 dst->clat_prio[ddir] = new_arr;
2130 dst->clat_prio[ddir][curr_len].clat_stat.min_val = ULONG_MAX;
2131 dst->nr_clat_prio[ddir]++;
2136 static int find_clat_prio_index(struct thread_stat *dst, enum fio_ddir ddir,
2139 int i, nr_prios = dst->nr_clat_prio[ddir];
2141 for (i = 0; i < nr_prios; i++) {
2142 if (dst->clat_prio[ddir][i].ioprio == ioprio)
2149 static int alloc_or_get_clat_prio_index(struct thread_stat *dst,
2150 enum fio_ddir ddir, uint32_t ioprio,
2153 int index = find_clat_prio_index(dst, ddir, ioprio);
2156 index = dst->nr_clat_prio[ddir];
2158 if (grow_clat_prio_stat(dst, ddir))
2161 dst->clat_prio[ddir][index].ioprio = ioprio;
2169 static int clat_prio_stats_copy(struct thread_stat *dst, struct thread_stat *src,
2170 enum fio_ddir dst_ddir, enum fio_ddir src_ddir)
2172 size_t sz = sizeof(*src->clat_prio[src_ddir]) *
2173 src->nr_clat_prio[src_ddir];
2175 dst->clat_prio[dst_ddir] = smalloc(sz);
2176 if (!dst->clat_prio[dst_ddir]) {
2177 log_err("fio: failed to alloc clat prio array\n");
2181 memcpy(dst->clat_prio[dst_ddir], src->clat_prio[src_ddir], sz);
2182 dst->nr_clat_prio[dst_ddir] = src->nr_clat_prio[src_ddir];
2187 static int clat_prio_stat_add_samples(struct thread_stat *dst,
2188 enum fio_ddir dst_ddir, uint32_t ioprio,
2189 struct io_stat *io_stat,
2190 uint64_t *io_u_plat)
2194 if (!io_stat->samples)
2197 if (alloc_or_get_clat_prio_index(dst, dst_ddir, ioprio, &dst_index))
2200 sum_stat(&dst->clat_prio[dst_ddir][dst_index].clat_stat, io_stat,
2203 for (i = 0; i < FIO_IO_U_PLAT_NR; i++)
2204 dst->clat_prio[dst_ddir][dst_index].io_u_plat[i] += io_u_plat[i];
2209 static int sum_clat_prio_stats_src_single_prio(struct thread_stat *dst,
2210 struct thread_stat *src,
2211 enum fio_ddir dst_ddir,
2212 enum fio_ddir src_ddir)
2214 struct io_stat *io_stat;
2215 uint64_t *io_u_plat;
2218 * If src ts has no clat_prio_stat array, then all I/Os were submitted
2219 * using src->ioprio. Thus, the global samples in src->clat_stat (or
2220 * src->lat_stat) can be used as the 'per prio' samples for src->ioprio.
2222 assert(!src->clat_prio[src_ddir]);
2223 assert(src->nr_clat_prio[src_ddir] == 0);
2225 if (src->lat_percentiles) {
2226 io_u_plat = src->io_u_plat[FIO_LAT][src_ddir];
2227 io_stat = &src->lat_stat[src_ddir];
2229 io_u_plat = src->io_u_plat[FIO_CLAT][src_ddir];
2230 io_stat = &src->clat_stat[src_ddir];
2233 return clat_prio_stat_add_samples(dst, dst_ddir, src->ioprio, io_stat,
2237 static int sum_clat_prio_stats_src_multi_prio(struct thread_stat *dst,
2238 struct thread_stat *src,
2239 enum fio_ddir dst_ddir,
2240 enum fio_ddir src_ddir)
2245 * If src ts has a clat_prio_stat array, then there are multiple prios
2246 * in use (i.e. src ts had cmdprio_percentage or cmdprio_bssplit set).
2247 * The samples for the default prio will exist in the src->clat_prio
2248 * array, just like the samples for any other prio.
2250 assert(src->clat_prio[src_ddir]);
2251 assert(src->nr_clat_prio[src_ddir]);
2253 /* If the dst ts doesn't yet have a clat_prio array, simply memcpy. */
2254 if (!dst->clat_prio[dst_ddir])
2255 return clat_prio_stats_copy(dst, src, dst_ddir, src_ddir);
2257 /* The dst ts already has a clat_prio_array, add src stats into it. */
2258 for (i = 0; i < src->nr_clat_prio[src_ddir]; i++) {
2259 struct io_stat *io_stat = &src->clat_prio[src_ddir][i].clat_stat;
2260 uint64_t *io_u_plat = src->clat_prio[src_ddir][i].io_u_plat;
2261 uint32_t ioprio = src->clat_prio[src_ddir][i].ioprio;
2263 if (clat_prio_stat_add_samples(dst, dst_ddir, ioprio, io_stat, io_u_plat))
2270 static int sum_clat_prio_stats(struct thread_stat *dst, struct thread_stat *src,
2271 enum fio_ddir dst_ddir, enum fio_ddir src_ddir)
2273 if (dst->disable_prio_stat)
2276 if (!src->clat_prio[src_ddir])
2277 return sum_clat_prio_stats_src_single_prio(dst, src, dst_ddir,
2280 return sum_clat_prio_stats_src_multi_prio(dst, src, dst_ddir, src_ddir);
2283 void sum_thread_stats(struct thread_stat *dst, struct thread_stat *src)
2287 for (l = 0; l < DDIR_RWDIR_CNT; l++) {
2288 if (dst->unified_rw_rep != UNIFIED_MIXED) {
2289 sum_stat(&dst->clat_stat[l], &src->clat_stat[l], false);
2290 sum_stat(&dst->slat_stat[l], &src->slat_stat[l], false);
2291 sum_stat(&dst->lat_stat[l], &src->lat_stat[l], false);
2292 sum_stat(&dst->bw_stat[l], &src->bw_stat[l], true);
2293 sum_stat(&dst->iops_stat[l], &src->iops_stat[l], true);
2294 sum_clat_prio_stats(dst, src, l, l);
2296 dst->io_bytes[l] += src->io_bytes[l];
2298 if (dst->runtime[l] < src->runtime[l])
2299 dst->runtime[l] = src->runtime[l];
2301 sum_stat(&dst->clat_stat[0], &src->clat_stat[l], false);
2302 sum_stat(&dst->slat_stat[0], &src->slat_stat[l], false);
2303 sum_stat(&dst->lat_stat[0], &src->lat_stat[l], false);
2304 sum_stat(&dst->bw_stat[0], &src->bw_stat[l], true);
2305 sum_stat(&dst->iops_stat[0], &src->iops_stat[l], true);
2306 sum_clat_prio_stats(dst, src, 0, l);
2308 dst->io_bytes[0] += src->io_bytes[l];
2310 if (dst->runtime[0] < src->runtime[l])
2311 dst->runtime[0] = src->runtime[l];
2315 sum_stat(&dst->sync_stat, &src->sync_stat, false);
2316 dst->usr_time += src->usr_time;
2317 dst->sys_time += src->sys_time;
2318 dst->ctx += src->ctx;
2319 dst->majf += src->majf;
2320 dst->minf += src->minf;
2322 for (k = 0; k < FIO_IO_U_MAP_NR; k++) {
2323 dst->io_u_map[k] += src->io_u_map[k];
2324 dst->io_u_submit[k] += src->io_u_submit[k];
2325 dst->io_u_complete[k] += src->io_u_complete[k];
2328 for (k = 0; k < FIO_IO_U_LAT_N_NR; k++)
2329 dst->io_u_lat_n[k] += src->io_u_lat_n[k];
2330 for (k = 0; k < FIO_IO_U_LAT_U_NR; k++)
2331 dst->io_u_lat_u[k] += src->io_u_lat_u[k];
2332 for (k = 0; k < FIO_IO_U_LAT_M_NR; k++)
2333 dst->io_u_lat_m[k] += src->io_u_lat_m[k];
2335 for (k = 0; k < DDIR_RWDIR_CNT; k++) {
2336 if (dst->unified_rw_rep != UNIFIED_MIXED) {
2337 dst->total_io_u[k] += src->total_io_u[k];
2338 dst->short_io_u[k] += src->short_io_u[k];
2339 dst->drop_io_u[k] += src->drop_io_u[k];
2341 dst->total_io_u[0] += src->total_io_u[k];
2342 dst->short_io_u[0] += src->short_io_u[k];
2343 dst->drop_io_u[0] += src->drop_io_u[k];
2347 dst->total_io_u[DDIR_SYNC] += src->total_io_u[DDIR_SYNC];
2349 for (k = 0; k < FIO_LAT_CNT; k++)
2350 for (l = 0; l < DDIR_RWDIR_CNT; l++)
2351 for (m = 0; m < FIO_IO_U_PLAT_NR; m++)
2352 if (dst->unified_rw_rep != UNIFIED_MIXED)
2353 dst->io_u_plat[k][l][m] += src->io_u_plat[k][l][m];
2355 dst->io_u_plat[k][0][m] += src->io_u_plat[k][l][m];
2357 for (k = 0; k < FIO_IO_U_PLAT_NR; k++)
2358 dst->io_u_sync_plat[k] += src->io_u_sync_plat[k];
2360 dst->total_run_time += src->total_run_time;
2361 dst->total_submit += src->total_submit;
2362 dst->total_complete += src->total_complete;
2363 dst->nr_zone_resets += src->nr_zone_resets;
2364 dst->cachehit += src->cachehit;
2365 dst->cachemiss += src->cachemiss;
2368 void init_group_run_stat(struct group_run_stats *gs)
2371 memset(gs, 0, sizeof(*gs));
2373 for (i = 0; i < DDIR_RWDIR_CNT; i++)
2374 gs->min_bw[i] = gs->min_run[i] = ~0UL;
2377 void init_thread_stat_min_vals(struct thread_stat *ts)
2381 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
2382 ts->clat_stat[i].min_val = ULONG_MAX;
2383 ts->slat_stat[i].min_val = ULONG_MAX;
2384 ts->lat_stat[i].min_val = ULONG_MAX;
2385 ts->bw_stat[i].min_val = ULONG_MAX;
2386 ts->iops_stat[i].min_val = ULONG_MAX;
2388 ts->sync_stat.min_val = ULONG_MAX;
2391 void init_thread_stat(struct thread_stat *ts)
2393 memset(ts, 0, sizeof(*ts));
2395 init_thread_stat_min_vals(ts);
2399 static void init_per_prio_stats(struct thread_stat *threadstats, int nr_ts)
2401 struct thread_stat *ts;
2402 int i, j, last_ts, idx;
2410 * Loop through all tds, if a td requires per prio stats, temporarily
2411 * store a 1 in ts->disable_prio_stat, and then do an additional
2412 * loop at the end where we invert the ts->disable_prio_stat values.
2418 (!td->o.group_reporting ||
2419 (td->o.group_reporting && last_ts != td->groupid))) {
2424 last_ts = td->groupid;
2425 ts = &threadstats[j];
2427 /* idx == 0 means first td in group, or td is not in a group. */
2429 ts->ioprio = td->ioprio;
2430 else if (td->ioprio != ts->ioprio)
2431 ts->disable_prio_stat = 1;
2433 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) {
2434 if (td->ts.clat_prio[ddir]) {
2435 ts->disable_prio_stat = 1;
2443 /* Loop through all dst threadstats and fixup the values. */
2444 for (i = 0; i < nr_ts; i++) {
2445 ts = &threadstats[i];
2446 ts->disable_prio_stat = !ts->disable_prio_stat;
2450 void __show_run_stats(void)
2452 struct group_run_stats *runstats, *rs;
2453 struct thread_stat *threadstats, *ts;
2454 int i, j, k, nr_ts, last_ts, idx;
2455 bool kb_base_warned = false;
2456 bool unit_base_warned = false;
2457 struct json_object *root = NULL;
2458 struct json_array *array = NULL;
2459 struct buf_output output[FIO_OUTPUT_NR];
2460 struct flist_head **opt_lists;
2462 runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1));
2464 for (i = 0; i < groupid + 1; i++)
2465 init_group_run_stat(&runstats[i]);
2468 * find out how many threads stats we need. if group reporting isn't
2469 * enabled, it's one-per-td.
2474 if (!td->o.group_reporting) {
2478 if (last_ts == td->groupid)
2483 last_ts = td->groupid;
2487 threadstats = malloc(nr_ts * sizeof(struct thread_stat));
2488 opt_lists = malloc(nr_ts * sizeof(struct flist_head *));
2490 for (i = 0; i < nr_ts; i++) {
2491 init_thread_stat(&threadstats[i]);
2492 opt_lists[i] = NULL;
2495 init_per_prio_stats(threadstats, nr_ts);
2503 if (idx && (!td->o.group_reporting ||
2504 (td->o.group_reporting && last_ts != td->groupid))) {
2509 last_ts = td->groupid;
2511 ts = &threadstats[j];
2513 ts->clat_percentiles = td->o.clat_percentiles;
2514 ts->lat_percentiles = td->o.lat_percentiles;
2515 ts->slat_percentiles = td->o.slat_percentiles;
2516 ts->percentile_precision = td->o.percentile_precision;
2517 memcpy(ts->percentile_list, td->o.percentile_list, sizeof(td->o.percentile_list));
2518 opt_lists[j] = &td->opt_list;
2522 if (ts->groupid == -1) {
2524 * These are per-group shared already
2526 snprintf(ts->name, sizeof(ts->name), "%s", td->o.name);
2527 if (td->o.description)
2528 snprintf(ts->description,
2529 sizeof(ts->description), "%s",
2532 memset(ts->description, 0, FIO_JOBDESC_SIZE);
2535 * If multiple entries in this group, this is
2538 ts->thread_number = td->thread_number;
2539 ts->groupid = td->groupid;
2540 ts->job_start = td->job_start;
2543 * first pid in group, not very useful...
2547 ts->kb_base = td->o.kb_base;
2548 ts->unit_base = td->o.unit_base;
2549 ts->sig_figs = td->o.sig_figs;
2550 ts->unified_rw_rep = td->o.unified_rw_rep;
2551 } else if (ts->kb_base != td->o.kb_base && !kb_base_warned) {
2552 log_info("fio: kb_base differs for jobs in group, using"
2553 " %u as the base\n", ts->kb_base);
2554 kb_base_warned = true;
2555 } else if (ts->unit_base != td->o.unit_base && !unit_base_warned) {
2556 log_info("fio: unit_base differs for jobs in group, using"
2557 " %u as the base\n", ts->unit_base);
2558 unit_base_warned = true;
2561 ts->continue_on_error = td->o.continue_on_error;
2562 ts->total_err_count += td->total_err_count;
2563 ts->first_error = td->first_error;
2565 if (!td->error && td->o.continue_on_error &&
2567 ts->error = td->first_error;
2568 snprintf(ts->verror, sizeof(ts->verror), "%s",
2570 } else if (td->error) {
2571 ts->error = td->error;
2572 snprintf(ts->verror, sizeof(ts->verror), "%s",
2577 ts->latency_depth = td->latency_qd;
2578 ts->latency_target = td->o.latency_target;
2579 ts->latency_percentile = td->o.latency_percentile;
2580 ts->latency_window = td->o.latency_window;
2582 ts->nr_block_infos = td->ts.nr_block_infos;
2583 for (k = 0; k < ts->nr_block_infos; k++)
2584 ts->block_infos[k] = td->ts.block_infos[k];
2586 sum_thread_stats(ts, &td->ts);
2591 ts->ss_state = td->ss.state;
2592 ts->ss_dur = td->ss.dur;
2593 ts->ss_head = td->ss.head;
2594 ts->ss_bw_data = td->ss.bw_data;
2595 ts->ss_iops_data = td->ss.iops_data;
2596 ts->ss_limit.u.f = td->ss.limit;
2597 ts->ss_slope.u.f = td->ss.slope;
2598 ts->ss_deviation.u.f = td->ss.deviation;
2599 ts->ss_criterion.u.f = td->ss.criterion;
2602 ts->ss_dur = ts->ss_state = 0;
2605 for (i = 0; i < nr_ts; i++) {
2606 unsigned long long bw;
2608 ts = &threadstats[i];
2609 if (ts->groupid == -1)
2611 rs = &runstats[ts->groupid];
2612 rs->kb_base = ts->kb_base;
2613 rs->unit_base = ts->unit_base;
2614 rs->sig_figs = ts->sig_figs;
2615 rs->unified_rw_rep |= ts->unified_rw_rep;
2617 for (j = 0; j < DDIR_RWDIR_CNT; j++) {
2618 if (!ts->runtime[j])
2620 if (ts->runtime[j] < rs->min_run[j] || !rs->min_run[j])
2621 rs->min_run[j] = ts->runtime[j];
2622 if (ts->runtime[j] > rs->max_run[j])
2623 rs->max_run[j] = ts->runtime[j];
2627 bw = ts->io_bytes[j] * 1000 / ts->runtime[j];
2628 if (bw < rs->min_bw[j])
2630 if (bw > rs->max_bw[j])
2633 rs->iobytes[j] += ts->io_bytes[j];
2637 for (i = 0; i < groupid + 1; i++) {
2642 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) {
2643 if (rs->max_run[ddir])
2644 rs->agg[ddir] = (rs->iobytes[ddir] * 1000) /
2649 for (i = 0; i < FIO_OUTPUT_NR; i++)
2650 buf_output_init(&output[i]);
2653 * don't overwrite last signal output
2655 if (output_format & FIO_OUTPUT_NORMAL)
2656 log_buf(&output[__FIO_OUTPUT_NORMAL], "\n");
2657 if (output_format & FIO_OUTPUT_JSON) {
2658 struct thread_data *global;
2661 unsigned long long ms_since_epoch;
2664 gettimeofday(&now, NULL);
2665 ms_since_epoch = (unsigned long long)(now.tv_sec) * 1000 +
2666 (unsigned long long)(now.tv_usec) / 1000;
2668 tv_sec = now.tv_sec;
2669 os_ctime_r(&tv_sec, time_buf, sizeof(time_buf));
2670 if (time_buf[strlen(time_buf) - 1] == '\n')
2671 time_buf[strlen(time_buf) - 1] = '\0';
2673 root = json_create_object();
2674 json_object_add_value_string(root, "fio version", fio_version_string);
2675 json_object_add_value_int(root, "timestamp", now.tv_sec);
2676 json_object_add_value_int(root, "timestamp_ms", ms_since_epoch);
2677 json_object_add_value_string(root, "time", time_buf);
2678 global = get_global_options();
2679 json_add_job_opts(root, "global options", &global->opt_list);
2680 array = json_create_array();
2681 json_object_add_value_array(root, "jobs", array);
2685 fio_server_send_job_options(&get_global_options()->opt_list, -1U);
2687 for (i = 0; i < nr_ts; i++) {
2688 ts = &threadstats[i];
2689 rs = &runstats[ts->groupid];
2692 fio_server_send_job_options(opt_lists[i], i);
2693 fio_server_send_ts(ts, rs);
2695 if (output_format & FIO_OUTPUT_TERSE)
2696 show_thread_status_terse(ts, rs, &output[__FIO_OUTPUT_TERSE]);
2697 if (output_format & FIO_OUTPUT_JSON) {
2698 struct json_object *tmp = show_thread_status_json(ts, rs, opt_lists[i]);
2699 json_array_add_value_object(array, tmp);
2701 if (output_format & FIO_OUTPUT_NORMAL)
2702 show_thread_status_normal(ts, rs, &output[__FIO_OUTPUT_NORMAL]);
2705 if (!is_backend && (output_format & FIO_OUTPUT_JSON)) {
2706 /* disk util stats, if any */
2707 show_disk_util(1, root, &output[__FIO_OUTPUT_JSON]);
2709 show_idle_prof_stats(FIO_OUTPUT_JSON, root, &output[__FIO_OUTPUT_JSON]);
2711 json_print_object(root, &output[__FIO_OUTPUT_JSON]);
2712 log_buf(&output[__FIO_OUTPUT_JSON], "\n");
2713 json_free_object(root);
2716 for (i = 0; i < groupid + 1; i++) {
2721 fio_server_send_gs(rs);
2722 else if (output_format & FIO_OUTPUT_NORMAL)
2723 show_group_stats(rs, &output[__FIO_OUTPUT_NORMAL]);
2727 fio_server_send_du();
2728 else if (output_format & FIO_OUTPUT_NORMAL) {
2729 show_disk_util(0, NULL, &output[__FIO_OUTPUT_NORMAL]);
2730 show_idle_prof_stats(FIO_OUTPUT_NORMAL, NULL, &output[__FIO_OUTPUT_NORMAL]);
2733 for (i = 0; i < FIO_OUTPUT_NR; i++) {
2734 struct buf_output *out = &output[i];
2736 log_info_buf(out->buf, out->buflen);
2737 buf_output_free(out);
2740 fio_idle_prof_cleanup();
2745 /* free arrays allocated by sum_thread_stats(), if any */
2746 for (i = 0; i < nr_ts; i++) {
2747 ts = &threadstats[i];
2748 free_clat_prio_stats(ts);
2754 int __show_running_run_stats(void)
2756 unsigned long long *rt;
2759 fio_sem_down(stat_sem);
2761 rt = malloc(thread_number * sizeof(unsigned long long));
2762 fio_gettime(&ts, NULL);
2765 if (td->runstate >= TD_EXITED)
2768 td->update_rusage = 1;
2769 for_each_rw_ddir(ddir) {
2770 td->ts.io_bytes[ddir] = td->io_bytes[ddir];
2772 td->ts.total_run_time = mtime_since(&td->epoch, &ts);
2774 rt[__td_index] = mtime_since(&td->start, &ts);
2775 if (td_read(td) && td->ts.io_bytes[DDIR_READ])
2776 td->ts.runtime[DDIR_READ] += rt[__td_index];
2777 if (td_write(td) && td->ts.io_bytes[DDIR_WRITE])
2778 td->ts.runtime[DDIR_WRITE] += rt[__td_index];
2779 if (td_trim(td) && td->ts.io_bytes[DDIR_TRIM])
2780 td->ts.runtime[DDIR_TRIM] += rt[__td_index];
2784 if (td->runstate >= TD_EXITED)
2786 if (td->rusage_sem) {
2787 td->update_rusage = 1;
2788 fio_sem_down(td->rusage_sem);
2790 td->update_rusage = 0;
2796 if (td->runstate >= TD_EXITED)
2799 if (td_read(td) && td->ts.io_bytes[DDIR_READ])
2800 td->ts.runtime[DDIR_READ] -= rt[__td_index];
2801 if (td_write(td) && td->ts.io_bytes[DDIR_WRITE])
2802 td->ts.runtime[DDIR_WRITE] -= rt[__td_index];
2803 if (td_trim(td) && td->ts.io_bytes[DDIR_TRIM])
2804 td->ts.runtime[DDIR_TRIM] -= rt[__td_index];
2808 fio_sem_up(stat_sem);
2813 static bool status_file_disabled;
2815 #define FIO_STATUS_FILE "fio-dump-status"
2817 static int check_status_file(void)
2820 const char *temp_dir;
2821 char fio_status_file_path[PATH_MAX];
2823 if (status_file_disabled)
2826 temp_dir = getenv("TMPDIR");
2827 if (temp_dir == NULL) {
2828 temp_dir = getenv("TEMP");
2829 if (temp_dir && strlen(temp_dir) >= PATH_MAX)
2832 if (temp_dir == NULL)
2835 __coverity_tainted_data_sanitize__(temp_dir);
2838 snprintf(fio_status_file_path, sizeof(fio_status_file_path), "%s/%s", temp_dir, FIO_STATUS_FILE);
2840 if (stat(fio_status_file_path, &sb))
2843 if (unlink(fio_status_file_path) < 0) {
2844 log_err("fio: failed to unlink %s: %s\n", fio_status_file_path,
2846 log_err("fio: disabling status file updates\n");
2847 status_file_disabled = true;
2853 void check_for_running_stats(void)
2855 if (check_status_file()) {
2856 show_running_run_stats();
2861 static inline void add_stat_sample(struct io_stat *is, unsigned long long data)
2866 if (data > is->max_val)
2868 if (data < is->min_val)
2871 delta = val - is->mean.u.f;
2873 is->mean.u.f += delta / (is->samples + 1.0);
2874 is->S.u.f += delta * (val - is->mean.u.f);
2880 static inline void add_stat_prio_sample(struct clat_prio_stat *clat_prio,
2881 unsigned short clat_prio_index,
2882 unsigned long long nsec)
2885 add_stat_sample(&clat_prio[clat_prio_index].clat_stat, nsec);
2889 * Return a struct io_logs, which is added to the tail of the log
2892 static struct io_logs *get_new_log(struct io_log *iolog)
2895 struct io_logs *cur_log;
2898 * Cap the size at MAX_LOG_ENTRIES, so we don't keep doubling
2901 if (!iolog->cur_log_max) {
2903 new_samples = iolog->td->o.log_entries;
2905 new_samples = DEF_LOG_ENTRIES;
2907 new_samples = iolog->cur_log_max * 2;
2908 if (new_samples > MAX_LOG_ENTRIES)
2909 new_samples = MAX_LOG_ENTRIES;
2912 cur_log = smalloc(sizeof(*cur_log));
2914 INIT_FLIST_HEAD(&cur_log->list);
2915 cur_log->log = calloc(new_samples, log_entry_sz(iolog));
2917 cur_log->nr_samples = 0;
2918 cur_log->max_samples = new_samples;
2919 flist_add_tail(&cur_log->list, &iolog->io_logs);
2920 iolog->cur_log_max = new_samples;
2930 * Add and return a new log chunk, or return current log if big enough
2932 static struct io_logs *regrow_log(struct io_log *iolog)
2934 struct io_logs *cur_log;
2937 if (!iolog || iolog->disabled)
2940 cur_log = iolog_cur_log(iolog);
2942 cur_log = get_new_log(iolog);
2947 if (cur_log->nr_samples < cur_log->max_samples)
2951 * No room for a new sample. If we're compressing on the fly, flush
2952 * out the current chunk
2954 if (iolog->log_gz) {
2955 if (iolog_cur_flush(iolog, cur_log)) {
2956 log_err("fio: failed flushing iolog! Will stop logging.\n");
2962 * Get a new log array, and add to our list
2964 cur_log = get_new_log(iolog);
2966 log_err("fio: failed extending iolog! Will stop logging.\n");
2970 if (!iolog->pending || !iolog->pending->nr_samples)
2974 * Flush pending items to new log
2976 for (i = 0; i < iolog->pending->nr_samples; i++) {
2977 struct io_sample *src, *dst;
2979 src = get_sample(iolog, iolog->pending, i);
2980 dst = get_sample(iolog, cur_log, i);
2981 memcpy(dst, src, log_entry_sz(iolog));
2983 cur_log->nr_samples = iolog->pending->nr_samples;
2985 iolog->pending->nr_samples = 0;
2989 iolog->disabled = true;
2993 void regrow_logs(struct thread_data *td)
2995 regrow_log(td->slat_log);
2996 regrow_log(td->clat_log);
2997 regrow_log(td->clat_hist_log);
2998 regrow_log(td->lat_log);
2999 regrow_log(td->bw_log);
3000 regrow_log(td->iops_log);
3001 td->flags &= ~TD_F_REGROW_LOGS;
3004 void regrow_agg_logs(void)
3008 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++)
3009 regrow_log(agg_io_log[ddir]);
3012 static struct io_logs *get_cur_log(struct io_log *iolog)
3014 struct io_logs *cur_log;
3016 cur_log = iolog_cur_log(iolog);
3018 cur_log = get_new_log(iolog);
3023 if (cur_log->nr_samples < cur_log->max_samples)
3027 * Out of space. If we're in IO offload mode, or we're not doing
3028 * per unit logging (hence logging happens outside of the IO thread
3029 * as well), add a new log chunk inline. If we're doing inline
3030 * submissions, flag 'td' as needing a log regrow and we'll take
3031 * care of it on the submission side.
3033 if ((iolog->td && iolog->td->o.io_submit_mode == IO_MODE_OFFLOAD) ||
3034 !per_unit_log(iolog))
3035 return regrow_log(iolog);
3038 iolog->td->flags |= TD_F_REGROW_LOGS;
3040 assert(iolog->pending->nr_samples < iolog->pending->max_samples);
3041 return iolog->pending;
3044 static void __add_log_sample(struct io_log *iolog, unsigned long t,
3045 struct log_sample *sample)
3047 struct io_logs *cur_log;
3049 if (iolog->disabled)
3051 if (flist_empty(&iolog->io_logs))
3052 iolog->avg_last[sample->ddir] = t;
3054 cur_log = get_cur_log(iolog);
3056 struct io_sample *s;
3058 s = get_sample(iolog, cur_log, cur_log->nr_samples);
3060 s->data = sample->data;
3062 if (iolog->td && iolog->td->o.log_alternate_epoch)
3063 s->time += iolog->td->alternate_epoch;
3064 io_sample_set_ddir(iolog, s, sample->ddir);
3066 s->priority = sample->priority;
3068 if (iolog->log_offset)
3069 s->aux[IOS_AUX_OFFSET_INDEX] = sample->offset;
3071 if (iolog->log_issue_time)
3072 s->aux[IOS_AUX_ISSUE_TIME_INDEX] = sample->issue_time;
3074 cur_log->nr_samples++;
3078 iolog->disabled = true;
3081 static inline void reset_io_stat(struct io_stat *ios)
3083 ios->min_val = -1ULL;
3084 ios->max_val = ios->samples = 0;
3085 ios->mean.u.f = ios->S.u.f = 0;
3088 static inline void reset_io_u_plat(uint64_t *io_u_plat)
3092 for (i = 0; i < FIO_IO_U_PLAT_NR; i++)
3096 static inline void reset_clat_prio_stats(struct thread_stat *ts)
3101 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) {
3102 if (!ts->clat_prio[ddir])
3105 for (i = 0; i < ts->nr_clat_prio[ddir]; i++) {
3106 reset_io_stat(&ts->clat_prio[ddir][i].clat_stat);
3107 reset_io_u_plat(ts->clat_prio[ddir][i].io_u_plat);
3112 void reset_io_stats(struct thread_data *td)
3114 struct thread_stat *ts = &td->ts;
3117 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
3118 reset_io_stat(&ts->clat_stat[i]);
3119 reset_io_stat(&ts->slat_stat[i]);
3120 reset_io_stat(&ts->lat_stat[i]);
3121 reset_io_stat(&ts->bw_stat[i]);
3122 reset_io_stat(&ts->iops_stat[i]);
3124 ts->io_bytes[i] = 0;
3126 ts->total_io_u[i] = 0;
3127 ts->short_io_u[i] = 0;
3128 ts->drop_io_u[i] = 0;
3131 for (i = 0; i < FIO_LAT_CNT; i++)
3132 for (j = 0; j < DDIR_RWDIR_CNT; j++)
3133 reset_io_u_plat(ts->io_u_plat[i][j]);
3135 reset_clat_prio_stats(ts);
3137 ts->total_io_u[DDIR_SYNC] = 0;
3138 reset_io_u_plat(ts->io_u_sync_plat);
3140 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
3141 ts->io_u_map[i] = 0;
3142 ts->io_u_submit[i] = 0;
3143 ts->io_u_complete[i] = 0;
3146 for (i = 0; i < FIO_IO_U_LAT_N_NR; i++)
3147 ts->io_u_lat_n[i] = 0;
3148 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
3149 ts->io_u_lat_u[i] = 0;
3150 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
3151 ts->io_u_lat_m[i] = 0;
3153 ts->total_submit = 0;
3154 ts->total_complete = 0;
3155 ts->nr_zone_resets = 0;
3156 ts->cachehit = ts->cachemiss = 0;
3159 static void __add_stat_to_log(struct io_log *iolog, enum fio_ddir ddir,
3160 unsigned long elapsed, int log_max)
3163 * Note an entry in the log. Use the mean from the logged samples,
3164 * making sure to properly round up. Only write a log entry if we
3165 * had actual samples done.
3167 if (iolog->avg_window[ddir].samples) {
3168 struct log_sample sample = { {{ 0, 0 }}, ddir, 0, 0, 0, 0 };
3169 union io_sample_data *d = &sample.data;
3171 if (log_max == IO_LOG_SAMPLE_AVG) {
3172 d->val.val0 = iolog->avg_window[ddir].mean.u.f + 0.50;
3174 } else if (log_max == IO_LOG_SAMPLE_MAX) {
3175 d->val.val0 = iolog->avg_window[ddir].max_val;
3178 d->val.val0 = iolog->avg_window[ddir].mean.u.f + 0.50;
3179 d->val.val1 = iolog->avg_window[ddir].max_val;
3182 __add_log_sample(iolog, elapsed, &sample);
3185 reset_io_stat(&iolog->avg_window[ddir]);
3188 static void _add_stat_to_log(struct io_log *iolog, unsigned long elapsed,
3193 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++)
3194 __add_stat_to_log(iolog, ddir, elapsed, log_max);
3197 static unsigned long add_log_sample(struct thread_data *td,
3198 struct io_log *iolog,
3199 struct log_sample *sample)
3201 unsigned long elapsed, this_window;
3202 enum fio_ddir ddir = sample->ddir;
3207 elapsed = mtime_since_now(&td->epoch);
3210 * If no time averaging, just add the log sample.
3212 if (!iolog->avg_msec) {
3213 __add_log_sample(iolog, elapsed, sample);
3218 * Add the sample. If the time period has passed, then
3219 * add that entry to the log and clear.
3221 add_stat_sample(&iolog->avg_window[ddir], sample->data.val.val0);
3224 * If period hasn't passed, adding the above sample is all we
3227 this_window = elapsed - iolog->avg_last[ddir];
3228 if (elapsed < iolog->avg_last[ddir])
3229 return iolog->avg_last[ddir] - elapsed;
3230 else if (this_window < iolog->avg_msec) {
3231 unsigned long diff = iolog->avg_msec - this_window;
3233 if (inline_log(iolog) || diff > LOG_MSEC_SLACK)
3237 __add_stat_to_log(iolog, ddir, elapsed, td->o.log_max);
3239 iolog->avg_last[ddir] = elapsed - (elapsed % iolog->avg_msec);
3241 return iolog->avg_msec;
3244 void finalize_logs(struct thread_data *td, bool unit_logs)
3246 unsigned long elapsed;
3248 elapsed = mtime_since_now(&td->epoch);
3250 if (td->clat_log && unit_logs)
3251 _add_stat_to_log(td->clat_log, elapsed, td->o.log_max);
3252 if (td->slat_log && unit_logs)
3253 _add_stat_to_log(td->slat_log, elapsed, td->o.log_max);
3254 if (td->lat_log && unit_logs)
3255 _add_stat_to_log(td->lat_log, elapsed, td->o.log_max);
3256 if (td->bw_log && (unit_logs == per_unit_log(td->bw_log)))
3257 _add_stat_to_log(td->bw_log, elapsed, td->o.log_max);
3258 if (td->iops_log && (unit_logs == per_unit_log(td->iops_log)))
3259 _add_stat_to_log(td->iops_log, elapsed, td->o.log_max);
3262 void add_agg_sample(union io_sample_data data, enum fio_ddir ddir,
3263 unsigned long long bs)
3265 struct io_log *iolog;
3266 struct log_sample sample = { data, ddir, bs, 0, 0, 0 };
3271 iolog = agg_io_log[ddir];
3272 __add_log_sample(iolog, mtime_since_genesis(), &sample);
3275 void add_sync_clat_sample(struct thread_stat *ts, unsigned long long nsec)
3277 unsigned int idx = plat_val_to_idx(nsec);
3278 assert(idx < FIO_IO_U_PLAT_NR);
3280 ts->io_u_sync_plat[idx]++;
3281 add_stat_sample(&ts->sync_stat, nsec);
3284 static inline void add_lat_percentile_sample(struct thread_stat *ts,
3285 unsigned long long nsec,
3289 unsigned int idx = plat_val_to_idx(nsec);
3290 assert(idx < FIO_IO_U_PLAT_NR);
3292 ts->io_u_plat[lat][ddir][idx]++;
3296 add_lat_percentile_prio_sample(struct thread_stat *ts, unsigned long long nsec,
3298 unsigned short clat_prio_index)
3300 unsigned int idx = plat_val_to_idx(nsec);
3302 if (ts->clat_prio[ddir])
3303 ts->clat_prio[ddir][clat_prio_index].io_u_plat[idx]++;
3306 void add_clat_sample(struct thread_data *td, enum fio_ddir ddir,
3307 unsigned long long nsec, unsigned long long bs,
3310 const bool needs_lock = td_async_processing(td);
3311 unsigned long elapsed, this_window;
3312 struct thread_stat *ts = &td->ts;
3313 struct io_log *iolog = td->clat_hist_log;
3314 uint64_t offset = 0;
3315 unsigned int ioprio = 0;
3316 unsigned short clat_prio_index = 0;
3322 offset = io_u->offset;
3323 ioprio = io_u->ioprio;
3324 clat_prio_index = io_u->clat_prio_index;
3327 add_stat_sample(&ts->clat_stat[ddir], nsec);
3330 * When lat_percentiles=1 (default 0), the reported per priority
3331 * percentiles and stats are used for describing total latency values,
3332 * even though the variable names themselves start with clat_.
3334 * Because of the above definition, add a prio stat sample only when
3335 * lat_percentiles=0. add_lat_sample() will add the prio stat sample
3336 * when lat_percentiles=1.
3338 if (!ts->lat_percentiles)
3339 add_stat_prio_sample(ts->clat_prio[ddir], clat_prio_index,
3343 struct log_sample sample = { sample_val(nsec), ddir, bs,
3344 offset, ioprio, 0 };
3348 ntime_since(&td->epoch, &io_u->issue_time);
3350 add_log_sample(td, td->clat_log, &sample);
3353 if (ts->clat_percentiles) {
3355 * Because of the above definition, add a prio lat percentile
3356 * sample only when lat_percentiles=0. add_lat_sample() will add
3357 * the prio lat percentile sample when lat_percentiles=1.
3359 add_lat_percentile_sample(ts, nsec, ddir, FIO_CLAT);
3360 if (!ts->lat_percentiles)
3361 add_lat_percentile_prio_sample(ts, nsec, ddir,
3365 if (iolog && iolog->hist_msec) {
3366 struct io_hist *hw = &iolog->hist_window[ddir];
3369 elapsed = mtime_since_now(&td->epoch);
3371 hw->hist_last = elapsed;
3372 this_window = elapsed - hw->hist_last;
3374 if (this_window >= iolog->hist_msec) {
3375 uint64_t *io_u_plat;
3376 struct io_u_plat_entry *dst;
3377 struct log_sample sample = { {{ 0, 0 }}, ddir, bs,
3378 offset, ioprio, 0 };
3381 * Make a byte-for-byte copy of the latency histogram
3382 * stored in td->ts.io_u_plat[ddir], recording it in a
3383 * log sample. Note that the matching call to free() is
3384 * located in iolog.c after printing this sample to the
3387 io_u_plat = (uint64_t *) td->ts.io_u_plat[FIO_CLAT][ddir];
3388 dst = malloc(sizeof(struct io_u_plat_entry));
3389 memcpy(&(dst->io_u_plat), io_u_plat,
3390 FIO_IO_U_PLAT_NR * sizeof(uint64_t));
3391 flist_add(&dst->list, &hw->list);
3393 sample.data = sample_plat(dst);
3394 __add_log_sample(iolog, elapsed, &sample);
3397 * Update the last time we recorded as being now, minus
3398 * any drift in time we encountered before actually
3399 * making the record.
3401 hw->hist_last = elapsed - (this_window - iolog->hist_msec);
3407 __td_io_u_unlock(td);
3410 void add_slat_sample(struct thread_data *td, struct io_u *io_u)
3412 const bool needs_lock = td_async_processing(td);
3413 struct thread_stat *ts = &td->ts;
3415 unsigned long long nsec;
3424 nsec = ntime_since(&io_u->start_time, &io_u->issue_time);
3426 add_stat_sample(&ts->slat_stat[ddir], nsec);
3429 struct log_sample sample = { sample_val(nsec), ddir,
3430 io_u->xfer_buflen, io_u->offset, io_u->ioprio,
3431 ntime_since(&td->epoch, &io_u->issue_time) };
3433 add_log_sample(td, td->slat_log, &sample);
3436 if (ts->slat_percentiles)
3437 add_lat_percentile_sample(ts, nsec, ddir, FIO_SLAT);
3440 __td_io_u_unlock(td);
3443 void add_lat_sample(struct thread_data *td, enum fio_ddir ddir,
3444 unsigned long long nsec, unsigned long long bs,
3447 const bool needs_lock = td_async_processing(td);
3448 struct thread_stat *ts = &td->ts;
3456 add_stat_sample(&ts->lat_stat[ddir], nsec);
3459 struct log_sample sample = { sample_val(nsec), ddir, bs,
3460 io_u->offset, io_u->ioprio, 0 };
3462 add_log_sample(td, td->lat_log, &sample);
3466 * When lat_percentiles=1 (default 0), the reported per priority
3467 * percentiles and stats are used for describing total latency values,
3468 * even though the variable names themselves start with clat_.
3470 * Because of the above definition, add a prio stat and prio lat
3471 * percentile sample only when lat_percentiles=1. add_clat_sample() will
3472 * add the prio stat and prio lat percentile sample when
3473 * lat_percentiles=0.
3475 if (ts->lat_percentiles) {
3476 add_lat_percentile_sample(ts, nsec, ddir, FIO_LAT);
3477 add_lat_percentile_prio_sample(ts, nsec, ddir,
3478 io_u->clat_prio_index);
3479 add_stat_prio_sample(ts->clat_prio[ddir], io_u->clat_prio_index,
3483 __td_io_u_unlock(td);
3486 void add_bw_sample(struct thread_data *td, struct io_u *io_u,
3487 unsigned int bytes, unsigned long long spent)
3489 const bool needs_lock = td_async_processing(td);
3490 struct thread_stat *ts = &td->ts;
3494 rate = (unsigned long) (bytes * 1000000ULL / spent);
3501 add_stat_sample(&ts->bw_stat[io_u->ddir], rate);
3504 struct log_sample sample = { sample_val(rate), io_u->ddir,
3505 bytes, io_u->offset, io_u->ioprio, 0 };
3507 add_log_sample(td, td->bw_log, &sample);
3510 td->stat_io_bytes[io_u->ddir] = td->this_io_bytes[io_u->ddir];
3513 __td_io_u_unlock(td);
3516 static int __add_samples(struct thread_data *td, struct timespec *parent_tv,
3517 struct timespec *t, unsigned int avg_time,
3518 uint64_t *this_io_bytes, uint64_t *stat_io_bytes,
3519 struct io_stat *stat, struct io_log *log,
3522 const bool needs_lock = td_async_processing(td);
3523 unsigned long spent, rate;
3525 unsigned long next, next_log;
3527 next_log = avg_time;
3529 spent = mtime_since(parent_tv, t);
3530 if (spent < avg_time && avg_time - spent > LOG_MSEC_SLACK)
3531 return avg_time - spent;
3537 * Compute both read and write rates for the interval.
3539 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) {
3542 delta = this_io_bytes[ddir] - stat_io_bytes[ddir];
3544 continue; /* No entries for interval */
3548 rate = delta * 1000 / spent / 1024; /* KiB/s */
3550 rate = (delta * 1000) / spent;
3554 add_stat_sample(&stat[ddir], rate);
3557 struct log_sample sample = {
3558 sample_val(rate), ddir, 0, 0, 0, 0 };
3560 if (td->o.min_bs[ddir] == td->o.max_bs[ddir])
3561 sample.bs = td->o.min_bs[ddir];
3562 next = add_log_sample(td, log, &sample);
3563 next_log = min(next_log, next);
3566 stat_io_bytes[ddir] = this_io_bytes[ddir];
3572 __td_io_u_unlock(td);
3574 if (spent <= avg_time)
3577 next = avg_time - (1 + spent - avg_time);
3579 return min(next, next_log);
3582 static int add_bw_samples(struct thread_data *td, struct timespec *t)
3584 return __add_samples(td, &td->bw_sample_time, t, td->o.bw_avg_time,
3585 td->this_io_bytes, td->stat_io_bytes,
3586 td->ts.bw_stat, td->bw_log, true);
3589 void add_iops_sample(struct thread_data *td, struct io_u *io_u,
3592 const bool needs_lock = td_async_processing(td);
3593 struct thread_stat *ts = &td->ts;
3598 add_stat_sample(&ts->iops_stat[io_u->ddir], 1);
3601 struct log_sample sample = { sample_val(1), io_u->ddir, bytes,
3602 io_u->offset, io_u->ioprio, 0 };
3604 add_log_sample(td, td->iops_log, &sample);
3607 td->stat_io_blocks[io_u->ddir] = td->this_io_blocks[io_u->ddir];
3610 __td_io_u_unlock(td);
3613 static int add_iops_samples(struct thread_data *td, struct timespec *t)
3615 return __add_samples(td, &td->iops_sample_time, t, td->o.iops_avg_time,
3616 td->this_io_blocks, td->stat_io_blocks,
3617 td->ts.iops_stat, td->iops_log, false);
3620 static bool td_in_logging_state(struct thread_data *td)
3622 if (in_ramp_time(td))
3625 switch(td->runstate) {
3637 * Returns msecs to next event
3639 int calc_log_samples(void)
3641 unsigned int next = ~0U, tmp = 0, next_mod = 0, log_avg_msec_min = -1U;
3642 struct timespec now;
3643 long elapsed_time = 0;
3646 fio_gettime(&now, NULL);
3647 elapsed_time = mtime_since(&td->epoch, &now);
3651 if (!td_in_logging_state(td)) {
3652 next = min(td->o.iops_avg_time, td->o.bw_avg_time);
3656 (td->bw_log && !per_unit_log(td->bw_log))) {
3657 tmp = add_bw_samples(td, &now);
3660 log_avg_msec_min = min(log_avg_msec_min, (unsigned int)td->bw_log->avg_msec);
3662 if (!td->iops_log ||
3663 (td->iops_log && !per_unit_log(td->iops_log))) {
3664 tmp = add_iops_samples(td, &now);
3667 log_avg_msec_min = min(log_avg_msec_min, (unsigned int)td->iops_log->avg_msec);
3674 /* if log_avg_msec_min has not been changed, set it to 0 */
3675 if (log_avg_msec_min == -1U)
3676 log_avg_msec_min = 0;
3678 if (log_avg_msec_min == 0)
3679 next_mod = elapsed_time;
3681 next_mod = elapsed_time % log_avg_msec_min;
3683 /* correction to keep the time on the log avg msec boundary */
3684 next = min(next, (log_avg_msec_min - next_mod));
3686 return next == ~0U ? 0 : next;
3689 void stat_init(void)
3691 stat_sem = fio_sem_init(FIO_SEM_UNLOCKED);
3694 void stat_exit(void)
3697 * When we have the mutex, we know out-of-band access to it
3700 fio_sem_down(stat_sem);
3701 fio_sem_remove(stat_sem);
3705 * Called from signal handler. Wake up status thread.
3707 void show_running_run_stats(void)
3712 uint32_t *io_u_block_info(struct thread_data *td, struct io_u *io_u)
3714 /* Ignore io_u's which span multiple blocks--they will just get
3715 * inaccurate counts. */
3716 int idx = (io_u->offset - io_u->file->file_offset)
3717 / td->o.bs[DDIR_TRIM];
3718 uint32_t *info = &td->ts.block_infos[idx];
3719 assert(idx < td->ts.nr_block_infos);