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
27 struct fio_sem *stat_sem;
29 void clear_rusage_stat(struct thread_data *td)
31 struct thread_stat *ts = &td->ts;
33 fio_getrusage(&td->ru_start);
34 ts->usr_time = ts->sys_time = 0;
36 ts->minf = ts->majf = 0;
39 void update_rusage_stat(struct thread_data *td)
41 struct thread_stat *ts = &td->ts;
43 fio_getrusage(&td->ru_end);
44 ts->usr_time += mtime_since_tv(&td->ru_start.ru_utime,
45 &td->ru_end.ru_utime);
46 ts->sys_time += mtime_since_tv(&td->ru_start.ru_stime,
47 &td->ru_end.ru_stime);
48 ts->ctx += td->ru_end.ru_nvcsw + td->ru_end.ru_nivcsw
49 - (td->ru_start.ru_nvcsw + td->ru_start.ru_nivcsw);
50 ts->minf += td->ru_end.ru_minflt - td->ru_start.ru_minflt;
51 ts->majf += td->ru_end.ru_majflt - td->ru_start.ru_majflt;
53 memcpy(&td->ru_start, &td->ru_end, sizeof(td->ru_end));
57 * Given a latency, return the index of the corresponding bucket in
58 * the structure tracking percentiles.
60 * (1) find the group (and error bits) that the value (latency)
61 * belongs to by looking at its MSB. (2) find the bucket number in the
62 * group by looking at the index bits.
65 static unsigned int plat_val_to_idx(unsigned long long val)
67 unsigned int msb, error_bits, base, offset, idx;
69 /* Find MSB starting from bit 0 */
73 msb = (sizeof(val)*8) - __builtin_clzll(val) - 1;
76 * MSB <= (FIO_IO_U_PLAT_BITS-1), cannot be rounded off. Use
77 * all bits of the sample as index
79 if (msb <= FIO_IO_U_PLAT_BITS)
82 /* Compute the number of error bits to discard*/
83 error_bits = msb - FIO_IO_U_PLAT_BITS;
85 /* Compute the number of buckets before the group */
86 base = (error_bits + 1) << FIO_IO_U_PLAT_BITS;
89 * Discard the error bits and apply the mask to find the
90 * index for the buckets in the group
92 offset = (FIO_IO_U_PLAT_VAL - 1) & (val >> error_bits);
94 /* Make sure the index does not exceed (array size - 1) */
95 idx = (base + offset) < (FIO_IO_U_PLAT_NR - 1) ?
96 (base + offset) : (FIO_IO_U_PLAT_NR - 1);
102 * Convert the given index of the bucket array to the value
103 * represented by the bucket
105 static unsigned long long plat_idx_to_val(unsigned int idx)
107 unsigned int error_bits;
108 unsigned long long k, base;
110 assert(idx < FIO_IO_U_PLAT_NR);
112 /* MSB <= (FIO_IO_U_PLAT_BITS-1), cannot be rounded off. Use
113 * all bits of the sample as index */
114 if (idx < (FIO_IO_U_PLAT_VAL << 1))
117 /* Find the group and compute the minimum value of that group */
118 error_bits = (idx >> FIO_IO_U_PLAT_BITS) - 1;
119 base = ((unsigned long long) 1) << (error_bits + FIO_IO_U_PLAT_BITS);
121 /* Find its bucket number of the group */
122 k = idx % FIO_IO_U_PLAT_VAL;
124 /* Return the mean of the range of the bucket */
125 return base + ((k + 0.5) * (1 << error_bits));
128 static int double_cmp(const void *a, const void *b)
130 const fio_fp64_t fa = *(const fio_fp64_t *) a;
131 const fio_fp64_t fb = *(const fio_fp64_t *) b;
136 else if (fa.u.f < fb.u.f)
142 unsigned int calc_clat_percentiles(uint64_t *io_u_plat, unsigned long long nr,
143 fio_fp64_t *plist, unsigned long long **output,
144 unsigned long long *maxv, unsigned long long *minv)
146 unsigned long long sum = 0;
147 unsigned int len, i, j = 0;
148 unsigned long long *ovals = NULL;
155 while (len < FIO_IO_U_LIST_MAX_LEN && plist[len].u.f != 0.0)
162 * Sort the percentile list. Note that it may already be sorted if
163 * we are using the default values, but since it's a short list this
164 * isn't a worry. Also note that this does not work for NaN values.
167 qsort(plist, len, sizeof(plist[0]), double_cmp);
169 ovals = malloc(len * sizeof(*ovals));
174 * Calculate bucket values, note down max and min values
177 for (i = 0; i < FIO_IO_U_PLAT_NR && !is_last; i++) {
179 while (sum >= ((long double) plist[j].u.f / 100.0 * nr)) {
180 assert(plist[j].u.f <= 100.0);
182 ovals[j] = plat_idx_to_val(i);
183 if (ovals[j] < *minv)
185 if (ovals[j] > *maxv)
188 is_last = (j == len - 1) != 0;
197 log_err("fio: error calculating latency percentiles\n");
204 * Find and display the p-th percentile of clat
206 static void show_clat_percentiles(uint64_t *io_u_plat, unsigned long long nr,
207 fio_fp64_t *plist, unsigned int precision,
208 const char *pre, struct buf_output *out)
210 unsigned int divisor, len, i, j = 0;
211 unsigned long long minv, maxv;
212 unsigned long long *ovals;
213 int per_line, scale_down, time_width;
217 len = calc_clat_percentiles(io_u_plat, nr, plist, &ovals, &maxv, &minv);
222 * We default to nsecs, but if the value range is such that we
223 * should scale down to usecs or msecs, do that.
225 if (minv > 2000000 && maxv > 99999999ULL) {
228 log_buf(out, " %s percentiles (msec):\n |", pre);
229 } else if (minv > 2000 && maxv > 99999) {
232 log_buf(out, " %s percentiles (usec):\n |", pre);
236 log_buf(out, " %s percentiles (nsec):\n |", pre);
240 time_width = max(5, (int) (log10(maxv / divisor) + 1));
241 snprintf(fmt, sizeof(fmt), " %%%u.%ufth=[%%%dllu]%%c", precision + 3,
242 precision, time_width);
243 /* fmt will be something like " %5.2fth=[%4llu]%c" */
244 per_line = (80 - 7) / (precision + 10 + time_width);
246 for (j = 0; j < len; j++) {
248 if (j != 0 && (j % per_line) == 0)
251 /* end of the list */
252 is_last = (j == len - 1) != 0;
254 for (i = 0; i < scale_down; i++)
255 ovals[j] = (ovals[j] + 999) / 1000;
257 log_buf(out, fmt, plist[j].u.f, ovals[j], is_last ? '\n' : ',');
262 if ((j % per_line) == per_line - 1) /* for formatting */
269 static int get_nr_prios_with_samples(struct thread_stat *ts, enum fio_ddir ddir)
271 int i, nr_prios_with_samples = 0;
273 for (i = 0; i < ts->nr_clat_prio[ddir]; i++) {
274 if (ts->clat_prio[ddir][i].clat_stat.samples)
275 nr_prios_with_samples++;
278 return nr_prios_with_samples;
281 bool calc_lat(struct io_stat *is, unsigned long long *min,
282 unsigned long long *max, double *mean, double *dev)
284 double n = (double) is->samples;
291 *mean = is->mean.u.f;
294 *dev = sqrt(is->S.u.f / (n - 1.0));
301 void show_mixed_group_stats(struct group_run_stats *rs, struct buf_output *out)
303 char *io, *agg, *min, *max;
304 char *ioalt, *aggalt, *minalt, *maxalt;
305 uint64_t io_mix = 0, agg_mix = 0, min_mix = -1, max_mix = 0;
306 uint64_t min_run = -1, max_run = 0;
307 const int i2p = is_power_of_2(rs->kb_base);
310 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
313 io_mix += rs->iobytes[i];
314 agg_mix += rs->agg[i];
315 min_mix = min_mix < rs->min_bw[i] ? min_mix : rs->min_bw[i];
316 max_mix = max_mix > rs->max_bw[i] ? max_mix : rs->max_bw[i];
317 min_run = min_run < rs->min_run[i] ? min_run : rs->min_run[i];
318 max_run = max_run > rs->max_run[i] ? max_run : rs->max_run[i];
320 io = num2str(io_mix, rs->sig_figs, 1, i2p, N2S_BYTE);
321 ioalt = num2str(io_mix, rs->sig_figs, 1, !i2p, N2S_BYTE);
322 agg = num2str(agg_mix, rs->sig_figs, 1, i2p, rs->unit_base);
323 aggalt = num2str(agg_mix, rs->sig_figs, 1, !i2p, rs->unit_base);
324 min = num2str(min_mix, rs->sig_figs, 1, i2p, rs->unit_base);
325 minalt = num2str(min_mix, rs->sig_figs, 1, !i2p, rs->unit_base);
326 max = num2str(max_mix, rs->sig_figs, 1, i2p, rs->unit_base);
327 maxalt = num2str(max_mix, rs->sig_figs, 1, !i2p, rs->unit_base);
328 log_buf(out, " MIXED: bw=%s (%s), %s-%s (%s-%s), io=%s (%s), run=%llu-%llumsec\n",
329 agg, aggalt, min, max, minalt, maxalt, io, ioalt,
330 (unsigned long long) min_run,
331 (unsigned long long) max_run);
342 void show_group_stats(struct group_run_stats *rs, struct buf_output *out)
344 char *io, *agg, *min, *max;
345 char *ioalt, *aggalt, *minalt, *maxalt;
346 const char *str[] = { " READ", " WRITE" , " TRIM"};
349 log_buf(out, "\nRun status group %d (all jobs):\n", rs->groupid);
351 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
352 const int i2p = is_power_of_2(rs->kb_base);
357 io = num2str(rs->iobytes[i], rs->sig_figs, 1, i2p, N2S_BYTE);
358 ioalt = num2str(rs->iobytes[i], rs->sig_figs, 1, !i2p, N2S_BYTE);
359 agg = num2str(rs->agg[i], rs->sig_figs, 1, i2p, rs->unit_base);
360 aggalt = num2str(rs->agg[i], rs->sig_figs, 1, !i2p, rs->unit_base);
361 min = num2str(rs->min_bw[i], rs->sig_figs, 1, i2p, rs->unit_base);
362 minalt = num2str(rs->min_bw[i], rs->sig_figs, 1, !i2p, rs->unit_base);
363 max = num2str(rs->max_bw[i], rs->sig_figs, 1, i2p, rs->unit_base);
364 maxalt = num2str(rs->max_bw[i], rs->sig_figs, 1, !i2p, rs->unit_base);
365 log_buf(out, "%s: bw=%s (%s), %s-%s (%s-%s), io=%s (%s), run=%llu-%llumsec\n",
366 (rs->unified_rw_rep == UNIFIED_MIXED) ? " MIXED" : str[i],
367 agg, aggalt, min, max, minalt, maxalt, io, ioalt,
368 (unsigned long long) rs->min_run[i],
369 (unsigned long long) rs->max_run[i]);
381 /* Need to aggregate statistics to show mixed values */
382 if (rs->unified_rw_rep == UNIFIED_BOTH)
383 show_mixed_group_stats(rs, out);
386 void stat_calc_dist(uint64_t *map, unsigned long total, double *io_u_dist)
391 * Do depth distribution calculations
393 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
395 io_u_dist[i] = (double) map[i] / (double) total;
396 io_u_dist[i] *= 100.0;
397 if (io_u_dist[i] < 0.1 && map[i])
404 static void stat_calc_lat(struct thread_stat *ts, double *dst,
405 uint64_t *src, int nr)
407 unsigned long total = ddir_rw_sum(ts->total_io_u);
411 * Do latency distribution calculations
413 for (i = 0; i < nr; i++) {
415 dst[i] = (double) src[i] / (double) total;
417 if (dst[i] < 0.01 && src[i])
425 * To keep the terse format unaltered, add all of the ns latency
426 * buckets to the first us latency bucket
428 static void stat_calc_lat_nu(struct thread_stat *ts, double *io_u_lat_u)
430 unsigned long ntotal = 0, total = ddir_rw_sum(ts->total_io_u);
433 stat_calc_lat(ts, io_u_lat_u, ts->io_u_lat_u, FIO_IO_U_LAT_U_NR);
435 for (i = 0; i < FIO_IO_U_LAT_N_NR; i++)
436 ntotal += ts->io_u_lat_n[i];
438 io_u_lat_u[0] += 100.0 * (double) ntotal / (double) total;
441 void stat_calc_lat_n(struct thread_stat *ts, double *io_u_lat)
443 stat_calc_lat(ts, io_u_lat, ts->io_u_lat_n, FIO_IO_U_LAT_N_NR);
446 void stat_calc_lat_u(struct thread_stat *ts, double *io_u_lat)
448 stat_calc_lat(ts, io_u_lat, ts->io_u_lat_u, FIO_IO_U_LAT_U_NR);
451 void stat_calc_lat_m(struct thread_stat *ts, double *io_u_lat)
453 stat_calc_lat(ts, io_u_lat, ts->io_u_lat_m, FIO_IO_U_LAT_M_NR);
456 static void display_lat(const char *name, unsigned long long min,
457 unsigned long long max, double mean, double dev,
458 struct buf_output *out)
460 const char *base = "(nsec)";
463 if (nsec_to_msec(&min, &max, &mean, &dev))
465 else if (nsec_to_usec(&min, &max, &mean, &dev))
468 minp = num2str(min, 6, 1, 0, N2S_NONE);
469 maxp = num2str(max, 6, 1, 0, N2S_NONE);
471 log_buf(out, " %s %s: min=%s, max=%s, avg=%5.02f,"
472 " stdev=%5.02f\n", name, base, minp, maxp, mean, dev);
478 static struct thread_stat *gen_mixed_ddir_stats_from_ts(struct thread_stat *ts)
480 struct thread_stat *ts_lcl;
483 * Handle aggregation of Reads (ddir = 0), Writes (ddir = 1), and
486 ts_lcl = malloc(sizeof(struct thread_stat));
488 log_err("fio: failed to allocate local thread stat\n");
492 init_thread_stat(ts_lcl);
494 /* calculate mixed stats */
495 ts_lcl->unified_rw_rep = UNIFIED_MIXED;
496 ts_lcl->lat_percentiles = ts->lat_percentiles;
497 ts_lcl->clat_percentiles = ts->clat_percentiles;
498 ts_lcl->slat_percentiles = ts->slat_percentiles;
499 ts_lcl->percentile_precision = ts->percentile_precision;
500 memcpy(ts_lcl->percentile_list, ts->percentile_list, sizeof(ts->percentile_list));
502 sum_thread_stats(ts_lcl, ts);
507 static double convert_agg_kbytes_percent(struct group_run_stats *rs,
508 enum fio_ddir ddir, int mean)
510 double p_of_agg = 100.0;
511 if (rs && rs->agg[ddir] > 1024) {
512 p_of_agg = mean * 100.0 / (double) (rs->agg[ddir] / 1024.0);
514 if (p_of_agg > 100.0)
520 static void show_ddir_status(struct group_run_stats *rs, struct thread_stat *ts,
521 enum fio_ddir ddir, struct buf_output *out)
524 unsigned long long min, max, bw, iops;
526 char *io_p, *bw_p, *bw_p_alt, *iops_p, *post_st = NULL;
528 const char *clat_type = ts->lat_percentiles ? "lat" : "clat";
530 if (ddir_sync(ddir)) {
531 if (calc_lat(&ts->sync_stat, &min, &max, &mean, &dev)) {
532 log_buf(out, " %s:\n", "fsync/fdatasync/sync_file_range");
533 display_lat(io_ddir_name(ddir), min, max, mean, dev, out);
534 show_clat_percentiles(ts->io_u_sync_plat,
535 ts->sync_stat.samples,
537 ts->percentile_precision,
538 io_ddir_name(ddir), out);
543 assert(ddir_rw(ddir));
545 if (!ts->runtime[ddir])
548 i2p = is_power_of_2(rs->kb_base);
549 runt = ts->runtime[ddir];
551 bw = (1000 * ts->io_bytes[ddir]) / runt;
552 io_p = num2str(ts->io_bytes[ddir], ts->sig_figs, 1, i2p, N2S_BYTE);
553 bw_p = num2str(bw, ts->sig_figs, 1, i2p, ts->unit_base);
554 bw_p_alt = num2str(bw, ts->sig_figs, 1, !i2p, ts->unit_base);
556 iops = (1000 * (uint64_t)ts->total_io_u[ddir]) / runt;
557 iops_p = num2str(iops, ts->sig_figs, 1, 0, N2S_NONE);
558 if (ddir == DDIR_WRITE || ddir == DDIR_TRIM)
559 post_st = zbd_write_status(ts);
560 else if (ddir == DDIR_READ && ts->cachehit && ts->cachemiss) {
564 total = ts->cachehit + ts->cachemiss;
565 hit = (double) ts->cachehit / (double) total;
567 if (asprintf(&post_st, "; Cachehit=%0.2f%%", hit) < 0)
571 log_buf(out, " %s: IOPS=%s, BW=%s (%s)(%s/%llumsec)%s\n",
572 (ts->unified_rw_rep == UNIFIED_MIXED) ? "mixed" : io_ddir_name(ddir),
573 iops_p, bw_p, bw_p_alt, io_p,
574 (unsigned long long) ts->runtime[ddir],
583 if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev))
584 display_lat("slat", min, max, mean, dev, out);
585 if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev))
586 display_lat("clat", min, max, mean, dev, out);
587 if (calc_lat(&ts->lat_stat[ddir], &min, &max, &mean, &dev))
588 display_lat(" lat", min, max, mean, dev, out);
590 /* Only print per prio stats if there are >= 2 prios with samples */
591 if (get_nr_prios_with_samples(ts, ddir) >= 2) {
592 for (i = 0; i < ts->nr_clat_prio[ddir]; i++) {
595 if (!calc_lat(&ts->clat_prio[ddir][i].clat_stat, &min,
599 snprintf(buf, sizeof(buf),
602 ioprio_class(ts->clat_prio[ddir][i].ioprio),
603 ioprio(ts->clat_prio[ddir][i].ioprio));
604 display_lat(buf, min, max, mean, dev, out);
608 if (ts->slat_percentiles && ts->slat_stat[ddir].samples > 0)
609 show_clat_percentiles(ts->io_u_plat[FIO_SLAT][ddir],
610 ts->slat_stat[ddir].samples,
612 ts->percentile_precision, "slat", out);
613 if (ts->clat_percentiles && ts->clat_stat[ddir].samples > 0)
614 show_clat_percentiles(ts->io_u_plat[FIO_CLAT][ddir],
615 ts->clat_stat[ddir].samples,
617 ts->percentile_precision, "clat", out);
618 if (ts->lat_percentiles && ts->lat_stat[ddir].samples > 0)
619 show_clat_percentiles(ts->io_u_plat[FIO_LAT][ddir],
620 ts->lat_stat[ddir].samples,
622 ts->percentile_precision, "lat", out);
624 if (ts->clat_percentiles || ts->lat_percentiles) {
628 if (ts->lat_percentiles)
629 samples = ts->lat_stat[ddir].samples;
631 samples = ts->clat_stat[ddir].samples;
633 /* Only print per prio stats if there are >= 2 prios with samples */
634 if (get_nr_prios_with_samples(ts, ddir) >= 2) {
635 for (i = 0; i < ts->nr_clat_prio[ddir]; i++) {
636 uint64_t prio_samples =
637 ts->clat_prio[ddir][i].clat_stat.samples;
642 snprintf(prio_name, sizeof(prio_name),
643 "%s prio %u/%u (%.2f%% of IOs)",
645 ioprio_class(ts->clat_prio[ddir][i].ioprio),
646 ioprio(ts->clat_prio[ddir][i].ioprio),
647 100. * (double) prio_samples / (double) samples);
648 show_clat_percentiles(ts->clat_prio[ddir][i].io_u_plat,
649 prio_samples, ts->percentile_list,
650 ts->percentile_precision,
656 if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
657 double p_of_agg = 100.0, fkb_base = (double)rs->kb_base;
660 if ((rs->unit_base == 1) && i2p)
662 else if (rs->unit_base == 1)
669 p_of_agg = convert_agg_kbytes_percent(rs, ddir, mean);
671 if (rs->unit_base == 1) {
678 if (mean > fkb_base * fkb_base) {
683 bw_str = (rs->unit_base == 1 ? "Mibit" : "MiB");
686 log_buf(out, " bw (%5s/s): min=%5llu, max=%5llu, per=%3.2f%%, "
687 "avg=%5.02f, stdev=%5.02f, samples=%" PRIu64 "\n",
688 bw_str, min, max, p_of_agg, mean, dev,
689 (&ts->bw_stat[ddir])->samples);
691 if (calc_lat(&ts->iops_stat[ddir], &min, &max, &mean, &dev)) {
692 log_buf(out, " iops : min=%5llu, max=%5llu, "
693 "avg=%5.02f, stdev=%5.02f, samples=%" PRIu64 "\n",
694 min, max, mean, dev, (&ts->iops_stat[ddir])->samples);
698 static void show_mixed_ddir_status(struct group_run_stats *rs,
699 struct thread_stat *ts,
700 struct buf_output *out)
702 struct thread_stat *ts_lcl = gen_mixed_ddir_stats_from_ts(ts);
705 show_ddir_status(rs, ts_lcl, DDIR_READ, out);
707 free_clat_prio_stats(ts_lcl);
711 static bool show_lat(double *io_u_lat, int nr, const char **ranges,
712 const char *msg, struct buf_output *out)
714 bool new_line = true, shown = false;
717 for (i = 0; i < nr; i++) {
718 if (io_u_lat[i] <= 0.0)
724 log_buf(out, " lat (%s) : ", msg);
730 log_buf(out, "%s%3.2f%%", ranges[i], io_u_lat[i]);
742 static void show_lat_n(double *io_u_lat_n, struct buf_output *out)
744 const char *ranges[] = { "2=", "4=", "10=", "20=", "50=", "100=",
745 "250=", "500=", "750=", "1000=", };
747 show_lat(io_u_lat_n, FIO_IO_U_LAT_N_NR, ranges, "nsec", out);
750 static void show_lat_u(double *io_u_lat_u, struct buf_output *out)
752 const char *ranges[] = { "2=", "4=", "10=", "20=", "50=", "100=",
753 "250=", "500=", "750=", "1000=", };
755 show_lat(io_u_lat_u, FIO_IO_U_LAT_U_NR, ranges, "usec", out);
758 static void show_lat_m(double *io_u_lat_m, struct buf_output *out)
760 const char *ranges[] = { "2=", "4=", "10=", "20=", "50=", "100=",
761 "250=", "500=", "750=", "1000=", "2000=",
764 show_lat(io_u_lat_m, FIO_IO_U_LAT_M_NR, ranges, "msec", out);
767 static void show_latencies(struct thread_stat *ts, struct buf_output *out)
769 double io_u_lat_n[FIO_IO_U_LAT_N_NR];
770 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
771 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
773 stat_calc_lat_n(ts, io_u_lat_n);
774 stat_calc_lat_u(ts, io_u_lat_u);
775 stat_calc_lat_m(ts, io_u_lat_m);
777 show_lat_n(io_u_lat_n, out);
778 show_lat_u(io_u_lat_u, out);
779 show_lat_m(io_u_lat_m, out);
782 static int block_state_category(int block_state)
784 switch (block_state) {
785 case BLOCK_STATE_UNINIT:
787 case BLOCK_STATE_TRIMMED:
788 case BLOCK_STATE_WRITTEN:
790 case BLOCK_STATE_WRITE_FAILURE:
791 case BLOCK_STATE_TRIM_FAILURE:
794 /* Silence compile warning on some BSDs and have a return */
800 static int compare_block_infos(const void *bs1, const void *bs2)
802 uint64_t block1 = *(uint64_t *)bs1;
803 uint64_t block2 = *(uint64_t *)bs2;
804 int state1 = BLOCK_INFO_STATE(block1);
805 int state2 = BLOCK_INFO_STATE(block2);
806 int bscat1 = block_state_category(state1);
807 int bscat2 = block_state_category(state2);
808 int cycles1 = BLOCK_INFO_TRIMS(block1);
809 int cycles2 = BLOCK_INFO_TRIMS(block2);
816 if (cycles1 < cycles2)
818 if (cycles1 > cycles2)
826 assert(block1 == block2);
830 static int calc_block_percentiles(int nr_block_infos, uint32_t *block_infos,
831 fio_fp64_t *plist, unsigned int **percentiles,
837 qsort(block_infos, nr_block_infos, sizeof(uint32_t), compare_block_infos);
839 while (len < FIO_IO_U_LIST_MAX_LEN && plist[len].u.f != 0.0)
846 * Sort the percentile list. Note that it may already be sorted if
847 * we are using the default values, but since it's a short list this
848 * isn't a worry. Also note that this does not work for NaN values.
851 qsort(plist, len, sizeof(plist[0]), double_cmp);
853 /* Start only after the uninit entries end */
855 nr_uninit < nr_block_infos
856 && BLOCK_INFO_STATE(block_infos[nr_uninit]) == BLOCK_STATE_UNINIT;
860 if (nr_uninit == nr_block_infos)
863 *percentiles = calloc(len, sizeof(**percentiles));
865 for (i = 0; i < len; i++) {
866 int idx = (plist[i].u.f * (nr_block_infos - nr_uninit) / 100)
868 (*percentiles)[i] = BLOCK_INFO_TRIMS(block_infos[idx]);
871 memset(types, 0, sizeof(*types) * BLOCK_STATE_COUNT);
872 for (i = 0; i < nr_block_infos; i++)
873 types[BLOCK_INFO_STATE(block_infos[i])]++;
878 static const char *block_state_names[] = {
879 [BLOCK_STATE_UNINIT] = "unwritten",
880 [BLOCK_STATE_TRIMMED] = "trimmed",
881 [BLOCK_STATE_WRITTEN] = "written",
882 [BLOCK_STATE_TRIM_FAILURE] = "trim failure",
883 [BLOCK_STATE_WRITE_FAILURE] = "write failure",
886 static void show_block_infos(int nr_block_infos, uint32_t *block_infos,
887 fio_fp64_t *plist, struct buf_output *out)
890 unsigned int *percentiles = NULL;
891 unsigned int block_state_counts[BLOCK_STATE_COUNT];
893 len = calc_block_percentiles(nr_block_infos, block_infos, plist,
894 &percentiles, block_state_counts);
896 log_buf(out, " block lifetime percentiles :\n |");
898 for (i = 0; i < len; i++) {
899 uint32_t block_info = percentiles[i];
900 #define LINE_LENGTH 75
901 char str[LINE_LENGTH];
902 int strln = snprintf(str, LINE_LENGTH, " %3.2fth=%u%c",
903 plist[i].u.f, block_info,
904 i == len - 1 ? '\n' : ',');
905 assert(strln < LINE_LENGTH);
906 if (pos + strln > LINE_LENGTH) {
908 log_buf(out, "\n |");
910 log_buf(out, "%s", str);
917 log_buf(out, " states :");
918 for (i = 0; i < BLOCK_STATE_COUNT; i++)
919 log_buf(out, " %s=%u%c",
920 block_state_names[i], block_state_counts[i],
921 i == BLOCK_STATE_COUNT - 1 ? '\n' : ',');
924 static void show_ss_normal(struct thread_stat *ts, struct buf_output *out)
926 char *p1, *p1alt, *p2;
927 unsigned long long bw_mean, iops_mean;
928 const int i2p = is_power_of_2(ts->kb_base);
933 bw_mean = steadystate_bw_mean(ts);
934 iops_mean = steadystate_iops_mean(ts);
936 p1 = num2str(bw_mean / ts->kb_base, ts->sig_figs, ts->kb_base, i2p, ts->unit_base);
937 p1alt = num2str(bw_mean / ts->kb_base, ts->sig_figs, ts->kb_base, !i2p, ts->unit_base);
938 p2 = num2str(iops_mean, ts->sig_figs, 1, 0, N2S_NONE);
940 log_buf(out, " steadystate : attained=%s, bw=%s (%s), iops=%s, %s%s=%.3f%s\n",
941 ts->ss_state & FIO_SS_ATTAINED ? "yes" : "no",
943 ts->ss_state & FIO_SS_IOPS ? "iops" : "bw",
944 ts->ss_state & FIO_SS_SLOPE ? " slope": " mean dev",
945 ts->ss_criterion.u.f,
946 ts->ss_state & FIO_SS_PCT ? "%" : "");
953 static void show_agg_stats(struct disk_util_agg *agg, int terse,
954 struct buf_output *out)
956 if (!agg->slavecount)
960 log_buf(out, ", aggrios=%llu/%llu, aggrmerge=%llu/%llu, "
961 "aggrticks=%llu/%llu, aggrin_queue=%llu, "
963 (unsigned long long) agg->ios[0] / agg->slavecount,
964 (unsigned long long) agg->ios[1] / agg->slavecount,
965 (unsigned long long) agg->merges[0] / agg->slavecount,
966 (unsigned long long) agg->merges[1] / agg->slavecount,
967 (unsigned long long) agg->ticks[0] / agg->slavecount,
968 (unsigned long long) agg->ticks[1] / agg->slavecount,
969 (unsigned long long) agg->time_in_queue / agg->slavecount,
972 log_buf(out, ";slaves;%llu;%llu;%llu;%llu;%llu;%llu;%llu;%3.2f%%",
973 (unsigned long long) agg->ios[0] / agg->slavecount,
974 (unsigned long long) agg->ios[1] / agg->slavecount,
975 (unsigned long long) agg->merges[0] / agg->slavecount,
976 (unsigned long long) agg->merges[1] / agg->slavecount,
977 (unsigned long long) agg->ticks[0] / agg->slavecount,
978 (unsigned long long) agg->ticks[1] / agg->slavecount,
979 (unsigned long long) agg->time_in_queue / agg->slavecount,
984 static void aggregate_slaves_stats(struct disk_util *masterdu)
986 struct disk_util_agg *agg = &masterdu->agg;
987 struct disk_util_stat *dus;
988 struct flist_head *entry;
989 struct disk_util *slavedu;
992 flist_for_each(entry, &masterdu->slaves) {
993 slavedu = flist_entry(entry, struct disk_util, slavelist);
995 agg->ios[0] += dus->s.ios[0];
996 agg->ios[1] += dus->s.ios[1];
997 agg->merges[0] += dus->s.merges[0];
998 agg->merges[1] += dus->s.merges[1];
999 agg->sectors[0] += dus->s.sectors[0];
1000 agg->sectors[1] += dus->s.sectors[1];
1001 agg->ticks[0] += dus->s.ticks[0];
1002 agg->ticks[1] += dus->s.ticks[1];
1003 agg->time_in_queue += dus->s.time_in_queue;
1006 util = (double) (100 * dus->s.io_ticks / (double) slavedu->dus.s.msec);
1007 /* System utilization is the utilization of the
1008 * component with the highest utilization.
1010 if (util > agg->max_util.u.f)
1011 agg->max_util.u.f = util;
1015 if (agg->max_util.u.f > 100.0)
1016 agg->max_util.u.f = 100.0;
1019 void print_disk_util(struct disk_util_stat *dus, struct disk_util_agg *agg,
1020 int terse, struct buf_output *out)
1025 util = (double) 100 * dus->s.io_ticks / (double) dus->s.msec;
1030 if (agg->slavecount)
1033 log_buf(out, " %s: ios=%llu/%llu, merge=%llu/%llu, "
1034 "ticks=%llu/%llu, in_queue=%llu, util=%3.2f%%",
1036 (unsigned long long) dus->s.ios[0],
1037 (unsigned long long) dus->s.ios[1],
1038 (unsigned long long) dus->s.merges[0],
1039 (unsigned long long) dus->s.merges[1],
1040 (unsigned long long) dus->s.ticks[0],
1041 (unsigned long long) dus->s.ticks[1],
1042 (unsigned long long) dus->s.time_in_queue,
1045 log_buf(out, ";%s;%llu;%llu;%llu;%llu;%llu;%llu;%llu;%3.2f%%",
1047 (unsigned long long) dus->s.ios[0],
1048 (unsigned long long) dus->s.ios[1],
1049 (unsigned long long) dus->s.merges[0],
1050 (unsigned long long) dus->s.merges[1],
1051 (unsigned long long) dus->s.ticks[0],
1052 (unsigned long long) dus->s.ticks[1],
1053 (unsigned long long) dus->s.time_in_queue,
1058 * If the device has slaves, aggregate the stats for
1059 * those slave devices also.
1061 show_agg_stats(agg, terse, out);
1067 void json_array_add_disk_util(struct disk_util_stat *dus,
1068 struct disk_util_agg *agg, struct json_array *array)
1070 struct json_object *obj;
1074 util = (double) 100 * dus->s.io_ticks / (double) dus->s.msec;
1078 obj = json_create_object();
1079 json_array_add_value_object(array, obj);
1081 json_object_add_value_string(obj, "name", (const char *)dus->name);
1082 json_object_add_value_int(obj, "read_ios", dus->s.ios[0]);
1083 json_object_add_value_int(obj, "write_ios", dus->s.ios[1]);
1084 json_object_add_value_int(obj, "read_merges", dus->s.merges[0]);
1085 json_object_add_value_int(obj, "write_merges", dus->s.merges[1]);
1086 json_object_add_value_int(obj, "read_ticks", dus->s.ticks[0]);
1087 json_object_add_value_int(obj, "write_ticks", dus->s.ticks[1]);
1088 json_object_add_value_int(obj, "in_queue", dus->s.time_in_queue);
1089 json_object_add_value_float(obj, "util", util);
1092 * If the device has slaves, aggregate the stats for
1093 * those slave devices also.
1095 if (!agg->slavecount)
1097 json_object_add_value_int(obj, "aggr_read_ios",
1098 agg->ios[0] / agg->slavecount);
1099 json_object_add_value_int(obj, "aggr_write_ios",
1100 agg->ios[1] / agg->slavecount);
1101 json_object_add_value_int(obj, "aggr_read_merges",
1102 agg->merges[0] / agg->slavecount);
1103 json_object_add_value_int(obj, "aggr_write_merge",
1104 agg->merges[1] / agg->slavecount);
1105 json_object_add_value_int(obj, "aggr_read_ticks",
1106 agg->ticks[0] / agg->slavecount);
1107 json_object_add_value_int(obj, "aggr_write_ticks",
1108 agg->ticks[1] / agg->slavecount);
1109 json_object_add_value_int(obj, "aggr_in_queue",
1110 agg->time_in_queue / agg->slavecount);
1111 json_object_add_value_float(obj, "aggr_util", agg->max_util.u.f);
1114 static void json_object_add_disk_utils(struct json_object *obj,
1115 struct flist_head *head)
1117 struct json_array *array = json_create_array();
1118 struct flist_head *entry;
1119 struct disk_util *du;
1121 json_object_add_value_array(obj, "disk_util", array);
1123 flist_for_each(entry, head) {
1124 du = flist_entry(entry, struct disk_util, list);
1126 aggregate_slaves_stats(du);
1127 json_array_add_disk_util(&du->dus, &du->agg, array);
1131 void show_disk_util(int terse, struct json_object *parent,
1132 struct buf_output *out)
1134 struct flist_head *entry;
1135 struct disk_util *du;
1138 if (!is_running_backend())
1141 if (flist_empty(&disk_list))
1144 if ((output_format & FIO_OUTPUT_JSON) && parent)
1149 if (!terse && !do_json)
1150 log_buf(out, "\nDisk stats (read/write):\n");
1153 json_object_add_disk_utils(parent, &disk_list);
1154 } else if (output_format & ~(FIO_OUTPUT_JSON | FIO_OUTPUT_JSON_PLUS)) {
1155 flist_for_each(entry, &disk_list) {
1156 du = flist_entry(entry, struct disk_util, list);
1158 aggregate_slaves_stats(du);
1159 print_disk_util(&du->dus, &du->agg, terse, out);
1164 static void show_thread_status_normal(struct thread_stat *ts,
1165 struct group_run_stats *rs,
1166 struct buf_output *out)
1168 double usr_cpu, sys_cpu;
1169 unsigned long runtime;
1170 double io_u_dist[FIO_IO_U_MAP_NR];
1174 if (!ddir_rw_sum(ts->io_bytes) && !ddir_rw_sum(ts->total_io_u))
1177 memset(time_buf, 0, sizeof(time_buf));
1180 os_ctime_r((const time_t *) &time_p, time_buf, sizeof(time_buf));
1183 log_buf(out, "%s: (groupid=%d, jobs=%d): err=%2d: pid=%d: %s",
1184 ts->name, ts->groupid, ts->members,
1185 ts->error, (int) ts->pid, time_buf);
1187 log_buf(out, "%s: (groupid=%d, jobs=%d): err=%2d (%s): pid=%d: %s",
1188 ts->name, ts->groupid, ts->members,
1189 ts->error, ts->verror, (int) ts->pid,
1193 if (strlen(ts->description))
1194 log_buf(out, " Description : [%s]\n", ts->description);
1196 for_each_rw_ddir(ddir) {
1197 if (ts->io_bytes[ddir])
1198 show_ddir_status(rs, ts, ddir, out);
1201 if (ts->unified_rw_rep == UNIFIED_BOTH)
1202 show_mixed_ddir_status(rs, ts, out);
1204 show_latencies(ts, out);
1206 if (ts->sync_stat.samples)
1207 show_ddir_status(rs, ts, DDIR_SYNC, out);
1209 runtime = ts->total_run_time;
1211 double runt = (double) runtime;
1213 usr_cpu = (double) ts->usr_time * 100 / runt;
1214 sys_cpu = (double) ts->sys_time * 100 / runt;
1220 log_buf(out, " cpu : usr=%3.2f%%, sys=%3.2f%%, ctx=%llu,"
1221 " majf=%llu, minf=%llu\n", usr_cpu, sys_cpu,
1222 (unsigned long long) ts->ctx,
1223 (unsigned long long) ts->majf,
1224 (unsigned long long) ts->minf);
1226 stat_calc_dist(ts->io_u_map, ddir_rw_sum(ts->total_io_u), io_u_dist);
1227 log_buf(out, " IO depths : 1=%3.1f%%, 2=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%,"
1228 " 16=%3.1f%%, 32=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
1229 io_u_dist[1], io_u_dist[2],
1230 io_u_dist[3], io_u_dist[4],
1231 io_u_dist[5], io_u_dist[6]);
1233 stat_calc_dist(ts->io_u_submit, ts->total_submit, io_u_dist);
1234 log_buf(out, " submit : 0=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%, 16=%3.1f%%,"
1235 " 32=%3.1f%%, 64=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
1236 io_u_dist[1], io_u_dist[2],
1237 io_u_dist[3], io_u_dist[4],
1238 io_u_dist[5], io_u_dist[6]);
1239 stat_calc_dist(ts->io_u_complete, ts->total_complete, io_u_dist);
1240 log_buf(out, " complete : 0=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%, 16=%3.1f%%,"
1241 " 32=%3.1f%%, 64=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
1242 io_u_dist[1], io_u_dist[2],
1243 io_u_dist[3], io_u_dist[4],
1244 io_u_dist[5], io_u_dist[6]);
1245 log_buf(out, " issued rwts: total=%llu,%llu,%llu,%llu"
1246 " short=%llu,%llu,%llu,0"
1247 " dropped=%llu,%llu,%llu,0\n",
1248 (unsigned long long) ts->total_io_u[0],
1249 (unsigned long long) ts->total_io_u[1],
1250 (unsigned long long) ts->total_io_u[2],
1251 (unsigned long long) ts->total_io_u[3],
1252 (unsigned long long) ts->short_io_u[0],
1253 (unsigned long long) ts->short_io_u[1],
1254 (unsigned long long) ts->short_io_u[2],
1255 (unsigned long long) ts->drop_io_u[0],
1256 (unsigned long long) ts->drop_io_u[1],
1257 (unsigned long long) ts->drop_io_u[2]);
1258 if (ts->continue_on_error) {
1259 log_buf(out, " errors : total=%llu, first_error=%d/<%s>\n",
1260 (unsigned long long)ts->total_err_count,
1262 strerror(ts->first_error));
1264 if (ts->latency_depth) {
1265 log_buf(out, " latency : target=%llu, window=%llu, percentile=%.2f%%, depth=%u\n",
1266 (unsigned long long)ts->latency_target,
1267 (unsigned long long)ts->latency_window,
1268 ts->latency_percentile.u.f,
1272 if (ts->nr_block_infos)
1273 show_block_infos(ts->nr_block_infos, ts->block_infos,
1274 ts->percentile_list, out);
1277 show_ss_normal(ts, out);
1280 static void show_ddir_status_terse(struct thread_stat *ts,
1281 struct group_run_stats *rs,
1282 enum fio_ddir ddir, int ver,
1283 struct buf_output *out)
1285 unsigned long long min, max, minv, maxv, bw, iops;
1286 unsigned long long *ovals = NULL;
1291 assert(ddir_rw(ddir));
1294 if (ts->runtime[ddir]) {
1295 uint64_t runt = ts->runtime[ddir];
1297 bw = ((1000 * ts->io_bytes[ddir]) / runt) / 1024; /* KiB/s */
1298 iops = (1000 * (uint64_t) ts->total_io_u[ddir]) / runt;
1301 log_buf(out, ";%llu;%llu;%llu;%llu",
1302 (unsigned long long) ts->io_bytes[ddir] >> 10, bw, iops,
1303 (unsigned long long) ts->runtime[ddir]);
1305 if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev))
1306 log_buf(out, ";%llu;%llu;%f;%f", min/1000, max/1000, mean/1000, dev/1000);
1308 log_buf(out, ";%llu;%llu;%f;%f", 0ULL, 0ULL, 0.0, 0.0);
1310 if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev))
1311 log_buf(out, ";%llu;%llu;%f;%f", min/1000, max/1000, mean/1000, dev/1000);
1313 log_buf(out, ";%llu;%llu;%f;%f", 0ULL, 0ULL, 0.0, 0.0);
1315 if (ts->lat_percentiles) {
1316 len = calc_clat_percentiles(ts->io_u_plat[FIO_LAT][ddir],
1317 ts->lat_stat[ddir].samples,
1318 ts->percentile_list, &ovals, &maxv,
1320 } else if (ts->clat_percentiles) {
1321 len = calc_clat_percentiles(ts->io_u_plat[FIO_CLAT][ddir],
1322 ts->clat_stat[ddir].samples,
1323 ts->percentile_list, &ovals, &maxv,
1329 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
1331 log_buf(out, ";0%%=0");
1334 log_buf(out, ";%f%%=%llu", ts->percentile_list[i].u.f, ovals[i]/1000);
1337 if (calc_lat(&ts->lat_stat[ddir], &min, &max, &mean, &dev))
1338 log_buf(out, ";%llu;%llu;%f;%f", min/1000, max/1000, mean/1000, dev/1000);
1340 log_buf(out, ";%llu;%llu;%f;%f", 0ULL, 0ULL, 0.0, 0.0);
1344 bw_stat = calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev);
1346 double p_of_agg = 100.0;
1348 if (rs->agg[ddir]) {
1349 p_of_agg = mean * 100 / (double) (rs->agg[ddir] / 1024);
1350 if (p_of_agg > 100.0)
1354 log_buf(out, ";%llu;%llu;%f%%;%f;%f", min, max, p_of_agg, mean, dev);
1356 log_buf(out, ";%llu;%llu;%f%%;%f;%f", 0ULL, 0ULL, 0.0, 0.0, 0.0);
1361 log_buf(out, ";%" PRIu64, (&ts->bw_stat[ddir])->samples);
1363 log_buf(out, ";%lu", 0UL);
1365 if (calc_lat(&ts->iops_stat[ddir], &min, &max, &mean, &dev))
1366 log_buf(out, ";%llu;%llu;%f;%f;%" PRIu64, min, max,
1367 mean, dev, (&ts->iops_stat[ddir])->samples);
1369 log_buf(out, ";%llu;%llu;%f;%f;%lu", 0ULL, 0ULL, 0.0, 0.0, 0UL);
1373 static void show_mixed_ddir_status_terse(struct thread_stat *ts,
1374 struct group_run_stats *rs,
1375 int ver, struct buf_output *out)
1377 struct thread_stat *ts_lcl = gen_mixed_ddir_stats_from_ts(ts);
1380 show_ddir_status_terse(ts_lcl, rs, DDIR_READ, ver, out);
1382 free_clat_prio_stats(ts_lcl);
1386 static struct json_object *add_ddir_lat_json(struct thread_stat *ts,
1387 uint32_t percentiles,
1388 struct io_stat *lat_stat,
1389 uint64_t *io_u_plat)
1393 unsigned int i, len;
1394 struct json_object *lat_object, *percentile_object, *clat_bins_object;
1395 unsigned long long min, max, maxv, minv, *ovals = NULL;
1397 if (!calc_lat(lat_stat, &min, &max, &mean, &dev)) {
1401 lat_object = json_create_object();
1402 json_object_add_value_int(lat_object, "min", min);
1403 json_object_add_value_int(lat_object, "max", max);
1404 json_object_add_value_float(lat_object, "mean", mean);
1405 json_object_add_value_float(lat_object, "stddev", dev);
1406 json_object_add_value_int(lat_object, "N", lat_stat->samples);
1408 if (percentiles && lat_stat->samples) {
1409 len = calc_clat_percentiles(io_u_plat, lat_stat->samples,
1410 ts->percentile_list, &ovals, &maxv, &minv);
1412 if (len > FIO_IO_U_LIST_MAX_LEN)
1413 len = FIO_IO_U_LIST_MAX_LEN;
1415 percentile_object = json_create_object();
1416 json_object_add_value_object(lat_object, "percentile", percentile_object);
1417 for (i = 0; i < len; i++) {
1418 snprintf(buf, sizeof(buf), "%f", ts->percentile_list[i].u.f);
1419 json_object_add_value_int(percentile_object, buf, ovals[i]);
1423 if (output_format & FIO_OUTPUT_JSON_PLUS) {
1424 clat_bins_object = json_create_object();
1425 json_object_add_value_object(lat_object, "bins", clat_bins_object);
1427 for(i = 0; i < FIO_IO_U_PLAT_NR; i++)
1429 snprintf(buf, sizeof(buf), "%llu", plat_idx_to_val(i));
1430 json_object_add_value_int(clat_bins_object, buf, io_u_plat[i]);
1438 static void add_ddir_status_json(struct thread_stat *ts,
1439 struct group_run_stats *rs, enum fio_ddir ddir,
1440 struct json_object *parent)
1442 unsigned long long min, max;
1443 unsigned long long bw_bytes, bw;
1444 double mean, dev, iops;
1445 struct json_object *dir_object, *tmp_object;
1446 double p_of_agg = 100.0;
1448 assert(ddir_rw(ddir) || ddir_sync(ddir));
1450 if ((ts->unified_rw_rep == UNIFIED_MIXED) && ddir != DDIR_READ)
1453 dir_object = json_create_object();
1454 json_object_add_value_object(parent,
1455 (ts->unified_rw_rep == UNIFIED_MIXED) ? "mixed" : io_ddir_name(ddir), dir_object);
1457 if (ddir_rw(ddir)) {
1461 if (ts->runtime[ddir]) {
1462 uint64_t runt = ts->runtime[ddir];
1464 bw_bytes = ((1000 * ts->io_bytes[ddir]) / runt); /* Bytes/s */
1465 bw = bw_bytes / 1024; /* KiB/s */
1466 iops = (1000.0 * (uint64_t) ts->total_io_u[ddir]) / runt;
1469 json_object_add_value_int(dir_object, "io_bytes", ts->io_bytes[ddir]);
1470 json_object_add_value_int(dir_object, "io_kbytes", ts->io_bytes[ddir] >> 10);
1471 json_object_add_value_int(dir_object, "bw_bytes", bw_bytes);
1472 json_object_add_value_int(dir_object, "bw", bw);
1473 json_object_add_value_float(dir_object, "iops", iops);
1474 json_object_add_value_int(dir_object, "runtime", ts->runtime[ddir]);
1475 json_object_add_value_int(dir_object, "total_ios", ts->total_io_u[ddir]);
1476 json_object_add_value_int(dir_object, "short_ios", ts->short_io_u[ddir]);
1477 json_object_add_value_int(dir_object, "drop_ios", ts->drop_io_u[ddir]);
1479 tmp_object = add_ddir_lat_json(ts, ts->slat_percentiles,
1480 &ts->slat_stat[ddir], ts->io_u_plat[FIO_SLAT][ddir]);
1481 json_object_add_value_object(dir_object, "slat_ns", tmp_object);
1483 tmp_object = add_ddir_lat_json(ts, ts->clat_percentiles,
1484 &ts->clat_stat[ddir], ts->io_u_plat[FIO_CLAT][ddir]);
1485 json_object_add_value_object(dir_object, "clat_ns", tmp_object);
1487 tmp_object = add_ddir_lat_json(ts, ts->lat_percentiles,
1488 &ts->lat_stat[ddir], ts->io_u_plat[FIO_LAT][ddir]);
1489 json_object_add_value_object(dir_object, "lat_ns", tmp_object);
1491 json_object_add_value_int(dir_object, "total_ios", ts->total_io_u[DDIR_SYNC]);
1492 tmp_object = add_ddir_lat_json(ts, ts->lat_percentiles | ts->clat_percentiles,
1493 &ts->sync_stat, ts->io_u_sync_plat);
1494 json_object_add_value_object(dir_object, "lat_ns", tmp_object);
1500 /* Only include per prio stats if there are >= 2 prios with samples */
1501 if (get_nr_prios_with_samples(ts, ddir) >= 2) {
1502 struct json_array *array = json_create_array();
1503 const char *obj_name;
1506 if (ts->lat_percentiles)
1507 obj_name = "lat_ns";
1509 obj_name = "clat_ns";
1511 json_object_add_value_array(dir_object, "prios", array);
1513 for (i = 0; i < ts->nr_clat_prio[ddir]; i++) {
1514 struct json_object *obj;
1516 if (!ts->clat_prio[ddir][i].clat_stat.samples)
1519 obj = json_create_object();
1521 json_object_add_value_int(obj, "prioclass",
1522 ioprio_class(ts->clat_prio[ddir][i].ioprio));
1523 json_object_add_value_int(obj, "prio",
1524 ioprio(ts->clat_prio[ddir][i].ioprio));
1526 tmp_object = add_ddir_lat_json(ts,
1527 ts->clat_percentiles | ts->lat_percentiles,
1528 &ts->clat_prio[ddir][i].clat_stat,
1529 ts->clat_prio[ddir][i].io_u_plat);
1530 json_object_add_value_object(obj, obj_name, tmp_object);
1531 json_array_add_value_object(array, obj);
1535 if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
1536 p_of_agg = convert_agg_kbytes_percent(rs, ddir, mean);
1539 p_of_agg = mean = dev = 0.0;
1542 json_object_add_value_int(dir_object, "bw_min", min);
1543 json_object_add_value_int(dir_object, "bw_max", max);
1544 json_object_add_value_float(dir_object, "bw_agg", p_of_agg);
1545 json_object_add_value_float(dir_object, "bw_mean", mean);
1546 json_object_add_value_float(dir_object, "bw_dev", dev);
1547 json_object_add_value_int(dir_object, "bw_samples",
1548 (&ts->bw_stat[ddir])->samples);
1550 if (!calc_lat(&ts->iops_stat[ddir], &min, &max, &mean, &dev)) {
1554 json_object_add_value_int(dir_object, "iops_min", min);
1555 json_object_add_value_int(dir_object, "iops_max", max);
1556 json_object_add_value_float(dir_object, "iops_mean", mean);
1557 json_object_add_value_float(dir_object, "iops_stddev", dev);
1558 json_object_add_value_int(dir_object, "iops_samples",
1559 (&ts->iops_stat[ddir])->samples);
1561 if (ts->cachehit + ts->cachemiss) {
1565 total = ts->cachehit + ts->cachemiss;
1566 hit = (double) ts->cachehit / (double) total;
1568 json_object_add_value_float(dir_object, "cachehit", hit);
1572 static void add_mixed_ddir_status_json(struct thread_stat *ts,
1573 struct group_run_stats *rs, struct json_object *parent)
1575 struct thread_stat *ts_lcl = gen_mixed_ddir_stats_from_ts(ts);
1577 /* add the aggregated stats to json parent */
1579 add_ddir_status_json(ts_lcl, rs, DDIR_READ, parent);
1581 free_clat_prio_stats(ts_lcl);
1585 static void show_thread_status_terse_all(struct thread_stat *ts,
1586 struct group_run_stats *rs, int ver,
1587 struct buf_output *out)
1589 double io_u_dist[FIO_IO_U_MAP_NR];
1590 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
1591 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
1592 double usr_cpu, sys_cpu;
1597 log_buf(out, "2;%s;%d;%d", ts->name, ts->groupid, ts->error);
1599 log_buf(out, "%d;%s;%s;%d;%d", ver, fio_version_string,
1600 ts->name, ts->groupid, ts->error);
1602 /* Log Read Status, or mixed if unified_rw_rep = 1 */
1603 show_ddir_status_terse(ts, rs, DDIR_READ, ver, out);
1604 if (ts->unified_rw_rep != UNIFIED_MIXED) {
1605 /* Log Write Status */
1606 show_ddir_status_terse(ts, rs, DDIR_WRITE, ver, out);
1607 /* Log Trim Status */
1608 if (ver == 2 || ver == 4 || ver == 5)
1609 show_ddir_status_terse(ts, rs, DDIR_TRIM, ver, out);
1611 if (ts->unified_rw_rep == UNIFIED_BOTH)
1612 show_mixed_ddir_status_terse(ts, rs, ver, out);
1614 if (ts->total_run_time) {
1615 double runt = (double) ts->total_run_time;
1617 usr_cpu = (double) ts->usr_time * 100 / runt;
1618 sys_cpu = (double) ts->sys_time * 100 / runt;
1624 log_buf(out, ";%f%%;%f%%;%llu;%llu;%llu", usr_cpu, sys_cpu,
1625 (unsigned long long) ts->ctx,
1626 (unsigned long long) ts->majf,
1627 (unsigned long long) ts->minf);
1629 /* Calc % distribution of IO depths, usecond, msecond latency */
1630 stat_calc_dist(ts->io_u_map, ddir_rw_sum(ts->total_io_u), io_u_dist);
1631 stat_calc_lat_nu(ts, io_u_lat_u);
1632 stat_calc_lat_m(ts, io_u_lat_m);
1634 /* Only show fixed 7 I/O depth levels*/
1635 log_buf(out, ";%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%",
1636 io_u_dist[0], io_u_dist[1], io_u_dist[2], io_u_dist[3],
1637 io_u_dist[4], io_u_dist[5], io_u_dist[6]);
1639 /* Microsecond latency */
1640 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
1641 log_buf(out, ";%3.2f%%", io_u_lat_u[i]);
1642 /* Millisecond latency */
1643 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
1644 log_buf(out, ";%3.2f%%", io_u_lat_m[i]);
1646 /* disk util stats, if any */
1647 if (ver >= 3 && is_running_backend())
1648 show_disk_util(1, NULL, out);
1650 /* Additional output if continue_on_error set - default off*/
1651 if (ts->continue_on_error)
1652 log_buf(out, ";%llu;%d", (unsigned long long) ts->total_err_count, ts->first_error);
1654 /* Additional output if description is set */
1655 if (strlen(ts->description)) {
1658 log_buf(out, ";%s", ts->description);
1664 static void json_add_job_opts(struct json_object *root, const char *name,
1665 struct flist_head *opt_list)
1667 struct json_object *dir_object;
1668 struct flist_head *entry;
1669 struct print_option *p;
1671 if (flist_empty(opt_list))
1674 dir_object = json_create_object();
1675 json_object_add_value_object(root, name, dir_object);
1677 flist_for_each(entry, opt_list) {
1678 p = flist_entry(entry, struct print_option, list);
1679 json_object_add_value_string(dir_object, p->name, p->value);
1683 static struct json_object *show_thread_status_json(struct thread_stat *ts,
1684 struct group_run_stats *rs,
1685 struct flist_head *opt_list)
1687 struct json_object *root, *tmp;
1688 struct jobs_eta *je;
1689 double io_u_dist[FIO_IO_U_MAP_NR];
1690 double io_u_lat_n[FIO_IO_U_LAT_N_NR];
1691 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
1692 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
1693 double usr_cpu, sys_cpu;
1697 root = json_create_object();
1698 json_object_add_value_string(root, "jobname", ts->name);
1699 json_object_add_value_int(root, "groupid", ts->groupid);
1700 json_object_add_value_int(root, "error", ts->error);
1703 je = get_jobs_eta(true, &size);
1705 json_object_add_value_int(root, "eta", je->eta_sec);
1706 json_object_add_value_int(root, "elapsed", je->elapsed_sec);
1711 json_add_job_opts(root, "job options", opt_list);
1713 add_ddir_status_json(ts, rs, DDIR_READ, root);
1714 add_ddir_status_json(ts, rs, DDIR_WRITE, root);
1715 add_ddir_status_json(ts, rs, DDIR_TRIM, root);
1716 add_ddir_status_json(ts, rs, DDIR_SYNC, root);
1718 if (ts->unified_rw_rep == UNIFIED_BOTH)
1719 add_mixed_ddir_status_json(ts, rs, root);
1722 if (ts->total_run_time) {
1723 double runt = (double) ts->total_run_time;
1725 usr_cpu = (double) ts->usr_time * 100 / runt;
1726 sys_cpu = (double) ts->sys_time * 100 / runt;
1731 json_object_add_value_int(root, "job_runtime", ts->total_run_time);
1732 json_object_add_value_float(root, "usr_cpu", usr_cpu);
1733 json_object_add_value_float(root, "sys_cpu", sys_cpu);
1734 json_object_add_value_int(root, "ctx", ts->ctx);
1735 json_object_add_value_int(root, "majf", ts->majf);
1736 json_object_add_value_int(root, "minf", ts->minf);
1738 /* Calc % distribution of IO depths */
1739 stat_calc_dist(ts->io_u_map, ddir_rw_sum(ts->total_io_u), io_u_dist);
1740 tmp = json_create_object();
1741 json_object_add_value_object(root, "iodepth_level", tmp);
1742 /* Only show fixed 7 I/O depth levels*/
1743 for (i = 0; i < 7; i++) {
1746 snprintf(name, 20, "%d", 1 << i);
1748 snprintf(name, 20, ">=%d", 1 << i);
1749 json_object_add_value_float(tmp, (const char *)name, io_u_dist[i]);
1752 /* Calc % distribution of submit IO depths */
1753 stat_calc_dist(ts->io_u_submit, ts->total_submit, io_u_dist);
1754 tmp = json_create_object();
1755 json_object_add_value_object(root, "iodepth_submit", tmp);
1756 /* Only show fixed 7 I/O depth levels*/
1757 for (i = 0; i < 7; i++) {
1760 snprintf(name, 20, "0");
1762 snprintf(name, 20, "%d", 1 << (i+1));
1764 snprintf(name, 20, ">=%d", 1 << i);
1765 json_object_add_value_float(tmp, (const char *)name, io_u_dist[i]);
1768 /* Calc % distribution of completion IO depths */
1769 stat_calc_dist(ts->io_u_complete, ts->total_complete, io_u_dist);
1770 tmp = json_create_object();
1771 json_object_add_value_object(root, "iodepth_complete", tmp);
1772 /* Only show fixed 7 I/O depth levels*/
1773 for (i = 0; i < 7; i++) {
1776 snprintf(name, 20, "0");
1778 snprintf(name, 20, "%d", 1 << (i+1));
1780 snprintf(name, 20, ">=%d", 1 << i);
1781 json_object_add_value_float(tmp, (const char *)name, io_u_dist[i]);
1784 /* Calc % distribution of nsecond, usecond, msecond latency */
1785 stat_calc_dist(ts->io_u_map, ddir_rw_sum(ts->total_io_u), io_u_dist);
1786 stat_calc_lat_n(ts, io_u_lat_n);
1787 stat_calc_lat_u(ts, io_u_lat_u);
1788 stat_calc_lat_m(ts, io_u_lat_m);
1790 /* Nanosecond latency */
1791 tmp = json_create_object();
1792 json_object_add_value_object(root, "latency_ns", tmp);
1793 for (i = 0; i < FIO_IO_U_LAT_N_NR; i++) {
1794 const char *ranges[] = { "2", "4", "10", "20", "50", "100",
1795 "250", "500", "750", "1000", };
1796 json_object_add_value_float(tmp, ranges[i], io_u_lat_n[i]);
1798 /* Microsecond latency */
1799 tmp = json_create_object();
1800 json_object_add_value_object(root, "latency_us", tmp);
1801 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
1802 const char *ranges[] = { "2", "4", "10", "20", "50", "100",
1803 "250", "500", "750", "1000", };
1804 json_object_add_value_float(tmp, ranges[i], io_u_lat_u[i]);
1806 /* Millisecond latency */
1807 tmp = json_create_object();
1808 json_object_add_value_object(root, "latency_ms", tmp);
1809 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++) {
1810 const char *ranges[] = { "2", "4", "10", "20", "50", "100",
1811 "250", "500", "750", "1000", "2000",
1813 json_object_add_value_float(tmp, ranges[i], io_u_lat_m[i]);
1816 /* Additional output if continue_on_error set - default off*/
1817 if (ts->continue_on_error) {
1818 json_object_add_value_int(root, "total_err", ts->total_err_count);
1819 json_object_add_value_int(root, "first_error", ts->first_error);
1822 if (ts->latency_depth) {
1823 json_object_add_value_int(root, "latency_depth", ts->latency_depth);
1824 json_object_add_value_int(root, "latency_target", ts->latency_target);
1825 json_object_add_value_float(root, "latency_percentile", ts->latency_percentile.u.f);
1826 json_object_add_value_int(root, "latency_window", ts->latency_window);
1829 /* Additional output if description is set */
1830 if (strlen(ts->description))
1831 json_object_add_value_string(root, "desc", ts->description);
1833 if (ts->nr_block_infos) {
1834 /* Block error histogram and types */
1836 unsigned int *percentiles = NULL;
1837 unsigned int block_state_counts[BLOCK_STATE_COUNT];
1839 len = calc_block_percentiles(ts->nr_block_infos, ts->block_infos,
1840 ts->percentile_list,
1841 &percentiles, block_state_counts);
1844 struct json_object *block, *percentile_object, *states;
1846 block = json_create_object();
1847 json_object_add_value_object(root, "block", block);
1849 percentile_object = json_create_object();
1850 json_object_add_value_object(block, "percentiles",
1852 for (i = 0; i < len; i++) {
1854 snprintf(buf, sizeof(buf), "%f",
1855 ts->percentile_list[i].u.f);
1856 json_object_add_value_int(percentile_object,
1861 states = json_create_object();
1862 json_object_add_value_object(block, "states", states);
1863 for (state = 0; state < BLOCK_STATE_COUNT; state++) {
1864 json_object_add_value_int(states,
1865 block_state_names[state],
1866 block_state_counts[state]);
1873 struct json_object *data;
1874 struct json_array *iops, *bw;
1877 int intervals = ts->ss_dur / (ss_check_interval / 1000L);
1879 snprintf(ss_buf, sizeof(ss_buf), "%s%s:%f%s",
1880 ts->ss_state & FIO_SS_IOPS ? "iops" : "bw",
1881 ts->ss_state & FIO_SS_SLOPE ? "_slope" : "",
1882 (float) ts->ss_limit.u.f,
1883 ts->ss_state & FIO_SS_PCT ? "%" : "");
1885 tmp = json_create_object();
1886 json_object_add_value_object(root, "steadystate", tmp);
1887 json_object_add_value_string(tmp, "ss", ss_buf);
1888 json_object_add_value_int(tmp, "duration", (int)ts->ss_dur);
1889 json_object_add_value_int(tmp, "attained", (ts->ss_state & FIO_SS_ATTAINED) > 0);
1891 snprintf(ss_buf, sizeof(ss_buf), "%f%s", (float) ts->ss_criterion.u.f,
1892 ts->ss_state & FIO_SS_PCT ? "%" : "");
1893 json_object_add_value_string(tmp, "criterion", ss_buf);
1894 json_object_add_value_float(tmp, "max_deviation", ts->ss_deviation.u.f);
1895 json_object_add_value_float(tmp, "slope", ts->ss_slope.u.f);
1897 data = json_create_object();
1898 json_object_add_value_object(tmp, "data", data);
1899 bw = json_create_array();
1900 iops = json_create_array();
1903 ** if ss was attained or the buffer is not full,
1904 ** ss->head points to the first element in the list.
1905 ** otherwise it actually points to the second element
1908 if ((ts->ss_state & FIO_SS_ATTAINED) || !(ts->ss_state & FIO_SS_BUFFER_FULL))
1911 j = ts->ss_head == 0 ? intervals - 1 : ts->ss_head - 1;
1912 for (l = 0; l < intervals; l++) {
1913 k = (j + l) % intervals;
1914 json_array_add_value_int(bw, ts->ss_bw_data[k]);
1915 json_array_add_value_int(iops, ts->ss_iops_data[k]);
1917 json_object_add_value_int(data, "bw_mean", steadystate_bw_mean(ts));
1918 json_object_add_value_int(data, "iops_mean", steadystate_iops_mean(ts));
1919 json_object_add_value_array(data, "iops", iops);
1920 json_object_add_value_array(data, "bw", bw);
1926 static void show_thread_status_terse(struct thread_stat *ts,
1927 struct group_run_stats *rs,
1928 struct buf_output *out)
1930 if (terse_version >= 2 && terse_version <= 5)
1931 show_thread_status_terse_all(ts, rs, terse_version, out);
1933 log_err("fio: bad terse version!? %d\n", terse_version);
1936 struct json_object *show_thread_status(struct thread_stat *ts,
1937 struct group_run_stats *rs,
1938 struct flist_head *opt_list,
1939 struct buf_output *out)
1941 struct json_object *ret = NULL;
1943 if (output_format & FIO_OUTPUT_TERSE)
1944 show_thread_status_terse(ts, rs, out);
1945 if (output_format & FIO_OUTPUT_JSON)
1946 ret = show_thread_status_json(ts, rs, opt_list);
1947 if (output_format & FIO_OUTPUT_NORMAL)
1948 show_thread_status_normal(ts, rs, out);
1953 static void __sum_stat(struct io_stat *dst, struct io_stat *src, bool first)
1957 dst->min_val = min(dst->min_val, src->min_val);
1958 dst->max_val = max(dst->max_val, src->max_val);
1961 * Compute new mean and S after the merge
1962 * <http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
1963 * #Parallel_algorithm>
1966 mean = src->mean.u.f;
1969 double delta = src->mean.u.f - dst->mean.u.f;
1971 mean = ((src->mean.u.f * src->samples) +
1972 (dst->mean.u.f * dst->samples)) /
1973 (dst->samples + src->samples);
1975 S = src->S.u.f + dst->S.u.f + pow(delta, 2.0) *
1976 (dst->samples * src->samples) /
1977 (dst->samples + src->samples);
1980 dst->samples += src->samples;
1981 dst->mean.u.f = mean;
1987 * We sum two kinds of stats - one that is time based, in which case we
1988 * apply the proper summing technique, and then one that is iops/bw
1989 * numbers. For group_reporting, we should just add those up, not make
1990 * them the mean of everything.
1992 static void sum_stat(struct io_stat *dst, struct io_stat *src, bool pure_sum)
1994 bool first = dst->samples == 0;
1996 if (src->samples == 0)
2000 __sum_stat(dst, src, first);
2005 dst->min_val = src->min_val;
2006 dst->max_val = src->max_val;
2007 dst->samples = src->samples;
2008 dst->mean.u.f = src->mean.u.f;
2009 dst->S.u.f = src->S.u.f;
2011 dst->min_val += src->min_val;
2012 dst->max_val += src->max_val;
2013 dst->samples += src->samples;
2014 dst->mean.u.f += src->mean.u.f;
2015 dst->S.u.f += src->S.u.f;
2019 void sum_group_stats(struct group_run_stats *dst, struct group_run_stats *src)
2023 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
2024 if (dst->max_run[i] < src->max_run[i])
2025 dst->max_run[i] = src->max_run[i];
2026 if (dst->min_run[i] && dst->min_run[i] > src->min_run[i])
2027 dst->min_run[i] = src->min_run[i];
2028 if (dst->max_bw[i] < src->max_bw[i])
2029 dst->max_bw[i] = src->max_bw[i];
2030 if (dst->min_bw[i] && dst->min_bw[i] > src->min_bw[i])
2031 dst->min_bw[i] = src->min_bw[i];
2033 dst->iobytes[i] += src->iobytes[i];
2034 dst->agg[i] += src->agg[i];
2038 dst->kb_base = src->kb_base;
2039 if (!dst->unit_base)
2040 dst->unit_base = src->unit_base;
2042 dst->sig_figs = src->sig_figs;
2046 * Free the clat_prio_stat arrays allocated by alloc_clat_prio_stat_ddir().
2048 void free_clat_prio_stats(struct thread_stat *ts)
2055 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) {
2056 sfree(ts->clat_prio[ddir]);
2057 ts->clat_prio[ddir] = NULL;
2058 ts->nr_clat_prio[ddir] = 0;
2063 * Allocate a clat_prio_stat array. The array has to be allocated/freed using
2064 * smalloc/sfree, so that it is accessible by the process/thread summing the
2067 int alloc_clat_prio_stat_ddir(struct thread_stat *ts, enum fio_ddir ddir,
2070 struct clat_prio_stat *clat_prio;
2073 clat_prio = scalloc(nr_prios, sizeof(*ts->clat_prio[ddir]));
2075 log_err("fio: failed to allocate ts clat data\n");
2079 for (i = 0; i < nr_prios; i++)
2080 clat_prio[i].clat_stat.min_val = ULONG_MAX;
2082 ts->clat_prio[ddir] = clat_prio;
2083 ts->nr_clat_prio[ddir] = nr_prios;
2088 static int grow_clat_prio_stat(struct thread_stat *dst, enum fio_ddir ddir)
2090 int curr_len = dst->nr_clat_prio[ddir];
2093 new_arr = scalloc(curr_len + 1, sizeof(*dst->clat_prio[ddir]));
2095 log_err("fio: failed to grow clat prio array\n");
2099 memcpy(new_arr, dst->clat_prio[ddir],
2100 curr_len * sizeof(*dst->clat_prio[ddir]));
2101 sfree(dst->clat_prio[ddir]);
2103 dst->clat_prio[ddir] = new_arr;
2104 dst->clat_prio[ddir][curr_len].clat_stat.min_val = ULONG_MAX;
2105 dst->nr_clat_prio[ddir]++;
2110 static int find_clat_prio_index(struct thread_stat *dst, enum fio_ddir ddir,
2113 int i, nr_prios = dst->nr_clat_prio[ddir];
2115 for (i = 0; i < nr_prios; i++) {
2116 if (dst->clat_prio[ddir][i].ioprio == ioprio)
2123 static int alloc_or_get_clat_prio_index(struct thread_stat *dst,
2124 enum fio_ddir ddir, uint32_t ioprio,
2127 int index = find_clat_prio_index(dst, ddir, ioprio);
2130 index = dst->nr_clat_prio[ddir];
2132 if (grow_clat_prio_stat(dst, ddir))
2135 dst->clat_prio[ddir][index].ioprio = ioprio;
2143 static int clat_prio_stats_copy(struct thread_stat *dst, struct thread_stat *src,
2144 enum fio_ddir dst_ddir, enum fio_ddir src_ddir)
2146 size_t sz = sizeof(*src->clat_prio[src_ddir]) *
2147 src->nr_clat_prio[src_ddir];
2149 dst->clat_prio[dst_ddir] = smalloc(sz);
2150 if (!dst->clat_prio[dst_ddir]) {
2151 log_err("fio: failed to alloc clat prio array\n");
2155 memcpy(dst->clat_prio[dst_ddir], src->clat_prio[src_ddir], sz);
2156 dst->nr_clat_prio[dst_ddir] = src->nr_clat_prio[src_ddir];
2161 static int clat_prio_stat_add_samples(struct thread_stat *dst,
2162 enum fio_ddir dst_ddir, uint32_t ioprio,
2163 struct io_stat *io_stat,
2164 uint64_t *io_u_plat)
2168 if (!io_stat->samples)
2171 if (alloc_or_get_clat_prio_index(dst, dst_ddir, ioprio, &dst_index))
2174 sum_stat(&dst->clat_prio[dst_ddir][dst_index].clat_stat, io_stat,
2177 for (i = 0; i < FIO_IO_U_PLAT_NR; i++)
2178 dst->clat_prio[dst_ddir][dst_index].io_u_plat[i] += io_u_plat[i];
2183 static int sum_clat_prio_stats_src_single_prio(struct thread_stat *dst,
2184 struct thread_stat *src,
2185 enum fio_ddir dst_ddir,
2186 enum fio_ddir src_ddir)
2188 struct io_stat *io_stat;
2189 uint64_t *io_u_plat;
2192 * If src ts has no clat_prio_stat array, then all I/Os were submitted
2193 * using src->ioprio. Thus, the global samples in src->clat_stat (or
2194 * src->lat_stat) can be used as the 'per prio' samples for src->ioprio.
2196 assert(!src->clat_prio[src_ddir]);
2197 assert(src->nr_clat_prio[src_ddir] == 0);
2199 if (src->lat_percentiles) {
2200 io_u_plat = src->io_u_plat[FIO_LAT][src_ddir];
2201 io_stat = &src->lat_stat[src_ddir];
2203 io_u_plat = src->io_u_plat[FIO_CLAT][src_ddir];
2204 io_stat = &src->clat_stat[src_ddir];
2207 return clat_prio_stat_add_samples(dst, dst_ddir, src->ioprio, io_stat,
2211 static int sum_clat_prio_stats_src_multi_prio(struct thread_stat *dst,
2212 struct thread_stat *src,
2213 enum fio_ddir dst_ddir,
2214 enum fio_ddir src_ddir)
2219 * If src ts has a clat_prio_stat array, then there are multiple prios
2220 * in use (i.e. src ts had cmdprio_percentage or cmdprio_bssplit set).
2221 * The samples for the default prio will exist in the src->clat_prio
2222 * array, just like the samples for any other prio.
2224 assert(src->clat_prio[src_ddir]);
2225 assert(src->nr_clat_prio[src_ddir]);
2227 /* If the dst ts doesn't yet have a clat_prio array, simply memcpy. */
2228 if (!dst->clat_prio[dst_ddir])
2229 return clat_prio_stats_copy(dst, src, dst_ddir, src_ddir);
2231 /* The dst ts already has a clat_prio_array, add src stats into it. */
2232 for (i = 0; i < src->nr_clat_prio[src_ddir]; i++) {
2233 struct io_stat *io_stat = &src->clat_prio[src_ddir][i].clat_stat;
2234 uint64_t *io_u_plat = src->clat_prio[src_ddir][i].io_u_plat;
2235 uint32_t ioprio = src->clat_prio[src_ddir][i].ioprio;
2237 if (clat_prio_stat_add_samples(dst, dst_ddir, ioprio, io_stat, io_u_plat))
2244 static int sum_clat_prio_stats(struct thread_stat *dst, struct thread_stat *src,
2245 enum fio_ddir dst_ddir, enum fio_ddir src_ddir)
2247 if (dst->disable_prio_stat)
2250 if (!src->clat_prio[src_ddir])
2251 return sum_clat_prio_stats_src_single_prio(dst, src, dst_ddir,
2254 return sum_clat_prio_stats_src_multi_prio(dst, src, dst_ddir, src_ddir);
2257 void sum_thread_stats(struct thread_stat *dst, struct thread_stat *src)
2261 for (l = 0; l < DDIR_RWDIR_CNT; l++) {
2262 if (dst->unified_rw_rep != UNIFIED_MIXED) {
2263 sum_stat(&dst->clat_stat[l], &src->clat_stat[l], false);
2264 sum_stat(&dst->slat_stat[l], &src->slat_stat[l], false);
2265 sum_stat(&dst->lat_stat[l], &src->lat_stat[l], false);
2266 sum_stat(&dst->bw_stat[l], &src->bw_stat[l], true);
2267 sum_stat(&dst->iops_stat[l], &src->iops_stat[l], true);
2268 sum_clat_prio_stats(dst, src, l, l);
2270 dst->io_bytes[l] += src->io_bytes[l];
2272 if (dst->runtime[l] < src->runtime[l])
2273 dst->runtime[l] = src->runtime[l];
2275 sum_stat(&dst->clat_stat[0], &src->clat_stat[l], false);
2276 sum_stat(&dst->slat_stat[0], &src->slat_stat[l], false);
2277 sum_stat(&dst->lat_stat[0], &src->lat_stat[l], false);
2278 sum_stat(&dst->bw_stat[0], &src->bw_stat[l], true);
2279 sum_stat(&dst->iops_stat[0], &src->iops_stat[l], true);
2280 sum_clat_prio_stats(dst, src, 0, l);
2282 dst->io_bytes[0] += src->io_bytes[l];
2284 if (dst->runtime[0] < src->runtime[l])
2285 dst->runtime[0] = src->runtime[l];
2289 sum_stat(&dst->sync_stat, &src->sync_stat, false);
2290 dst->usr_time += src->usr_time;
2291 dst->sys_time += src->sys_time;
2292 dst->ctx += src->ctx;
2293 dst->majf += src->majf;
2294 dst->minf += src->minf;
2296 for (k = 0; k < FIO_IO_U_MAP_NR; k++) {
2297 dst->io_u_map[k] += src->io_u_map[k];
2298 dst->io_u_submit[k] += src->io_u_submit[k];
2299 dst->io_u_complete[k] += src->io_u_complete[k];
2302 for (k = 0; k < FIO_IO_U_LAT_N_NR; k++)
2303 dst->io_u_lat_n[k] += src->io_u_lat_n[k];
2304 for (k = 0; k < FIO_IO_U_LAT_U_NR; k++)
2305 dst->io_u_lat_u[k] += src->io_u_lat_u[k];
2306 for (k = 0; k < FIO_IO_U_LAT_M_NR; k++)
2307 dst->io_u_lat_m[k] += src->io_u_lat_m[k];
2309 for (k = 0; k < DDIR_RWDIR_CNT; k++) {
2310 if (dst->unified_rw_rep != UNIFIED_MIXED) {
2311 dst->total_io_u[k] += src->total_io_u[k];
2312 dst->short_io_u[k] += src->short_io_u[k];
2313 dst->drop_io_u[k] += src->drop_io_u[k];
2315 dst->total_io_u[0] += src->total_io_u[k];
2316 dst->short_io_u[0] += src->short_io_u[k];
2317 dst->drop_io_u[0] += src->drop_io_u[k];
2321 dst->total_io_u[DDIR_SYNC] += src->total_io_u[DDIR_SYNC];
2323 for (k = 0; k < FIO_LAT_CNT; k++)
2324 for (l = 0; l < DDIR_RWDIR_CNT; l++)
2325 for (m = 0; m < FIO_IO_U_PLAT_NR; m++)
2326 if (dst->unified_rw_rep != UNIFIED_MIXED)
2327 dst->io_u_plat[k][l][m] += src->io_u_plat[k][l][m];
2329 dst->io_u_plat[k][0][m] += src->io_u_plat[k][l][m];
2331 for (k = 0; k < FIO_IO_U_PLAT_NR; k++)
2332 dst->io_u_sync_plat[k] += src->io_u_sync_plat[k];
2334 dst->total_run_time += src->total_run_time;
2335 dst->total_submit += src->total_submit;
2336 dst->total_complete += src->total_complete;
2337 dst->nr_zone_resets += src->nr_zone_resets;
2338 dst->cachehit += src->cachehit;
2339 dst->cachemiss += src->cachemiss;
2342 void init_group_run_stat(struct group_run_stats *gs)
2345 memset(gs, 0, sizeof(*gs));
2347 for (i = 0; i < DDIR_RWDIR_CNT; i++)
2348 gs->min_bw[i] = gs->min_run[i] = ~0UL;
2351 void init_thread_stat_min_vals(struct thread_stat *ts)
2355 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
2356 ts->clat_stat[i].min_val = ULONG_MAX;
2357 ts->slat_stat[i].min_val = ULONG_MAX;
2358 ts->lat_stat[i].min_val = ULONG_MAX;
2359 ts->bw_stat[i].min_val = ULONG_MAX;
2360 ts->iops_stat[i].min_val = ULONG_MAX;
2362 ts->sync_stat.min_val = ULONG_MAX;
2365 void init_thread_stat(struct thread_stat *ts)
2367 memset(ts, 0, sizeof(*ts));
2369 init_thread_stat_min_vals(ts);
2373 static void init_per_prio_stats(struct thread_stat *threadstats, int nr_ts)
2375 struct thread_stat *ts;
2376 int i, j, last_ts, idx;
2384 * Loop through all tds, if a td requires per prio stats, temporarily
2385 * store a 1 in ts->disable_prio_stat, and then do an additional
2386 * loop at the end where we invert the ts->disable_prio_stat values.
2392 (!td->o.group_reporting ||
2393 (td->o.group_reporting && last_ts != td->groupid))) {
2398 last_ts = td->groupid;
2399 ts = &threadstats[j];
2401 /* idx == 0 means first td in group, or td is not in a group. */
2403 ts->ioprio = td->ioprio;
2404 else if (td->ioprio != ts->ioprio)
2405 ts->disable_prio_stat = 1;
2407 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) {
2408 if (td->ts.clat_prio[ddir]) {
2409 ts->disable_prio_stat = 1;
2417 /* Loop through all dst threadstats and fixup the values. */
2418 for (i = 0; i < nr_ts; i++) {
2419 ts = &threadstats[i];
2420 ts->disable_prio_stat = !ts->disable_prio_stat;
2424 void __show_run_stats(void)
2426 struct group_run_stats *runstats, *rs;
2427 struct thread_stat *threadstats, *ts;
2428 int i, j, k, nr_ts, last_ts, idx;
2429 bool kb_base_warned = false;
2430 bool unit_base_warned = false;
2431 struct json_object *root = NULL;
2432 struct json_array *array = NULL;
2433 struct buf_output output[FIO_OUTPUT_NR];
2434 struct flist_head **opt_lists;
2436 runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1));
2438 for (i = 0; i < groupid + 1; i++)
2439 init_group_run_stat(&runstats[i]);
2442 * find out how many threads stats we need. if group reporting isn't
2443 * enabled, it's one-per-td.
2448 if (!td->o.group_reporting) {
2452 if (last_ts == td->groupid)
2457 last_ts = td->groupid;
2461 threadstats = malloc(nr_ts * sizeof(struct thread_stat));
2462 opt_lists = malloc(nr_ts * sizeof(struct flist_head *));
2464 for (i = 0; i < nr_ts; i++) {
2465 init_thread_stat(&threadstats[i]);
2466 opt_lists[i] = NULL;
2469 init_per_prio_stats(threadstats, nr_ts);
2477 if (idx && (!td->o.group_reporting ||
2478 (td->o.group_reporting && last_ts != td->groupid))) {
2483 last_ts = td->groupid;
2485 ts = &threadstats[j];
2487 ts->clat_percentiles = td->o.clat_percentiles;
2488 ts->lat_percentiles = td->o.lat_percentiles;
2489 ts->slat_percentiles = td->o.slat_percentiles;
2490 ts->percentile_precision = td->o.percentile_precision;
2491 memcpy(ts->percentile_list, td->o.percentile_list, sizeof(td->o.percentile_list));
2492 opt_lists[j] = &td->opt_list;
2496 if (ts->groupid == -1) {
2498 * These are per-group shared already
2500 snprintf(ts->name, sizeof(ts->name), "%s", td->o.name);
2501 if (td->o.description)
2502 snprintf(ts->description,
2503 sizeof(ts->description), "%s",
2506 memset(ts->description, 0, FIO_JOBDESC_SIZE);
2509 * If multiple entries in this group, this is
2512 ts->thread_number = td->thread_number;
2513 ts->groupid = td->groupid;
2516 * first pid in group, not very useful...
2520 ts->kb_base = td->o.kb_base;
2521 ts->unit_base = td->o.unit_base;
2522 ts->sig_figs = td->o.sig_figs;
2523 ts->unified_rw_rep = td->o.unified_rw_rep;
2524 } else if (ts->kb_base != td->o.kb_base && !kb_base_warned) {
2525 log_info("fio: kb_base differs for jobs in group, using"
2526 " %u as the base\n", ts->kb_base);
2527 kb_base_warned = true;
2528 } else if (ts->unit_base != td->o.unit_base && !unit_base_warned) {
2529 log_info("fio: unit_base differs for jobs in group, using"
2530 " %u as the base\n", ts->unit_base);
2531 unit_base_warned = true;
2534 ts->continue_on_error = td->o.continue_on_error;
2535 ts->total_err_count += td->total_err_count;
2536 ts->first_error = td->first_error;
2538 if (!td->error && td->o.continue_on_error &&
2540 ts->error = td->first_error;
2541 snprintf(ts->verror, sizeof(ts->verror), "%s",
2543 } else if (td->error) {
2544 ts->error = td->error;
2545 snprintf(ts->verror, sizeof(ts->verror), "%s",
2550 ts->latency_depth = td->latency_qd;
2551 ts->latency_target = td->o.latency_target;
2552 ts->latency_percentile = td->o.latency_percentile;
2553 ts->latency_window = td->o.latency_window;
2555 ts->nr_block_infos = td->ts.nr_block_infos;
2556 for (k = 0; k < ts->nr_block_infos; k++)
2557 ts->block_infos[k] = td->ts.block_infos[k];
2559 sum_thread_stats(ts, &td->ts);
2564 ts->ss_state = td->ss.state;
2565 ts->ss_dur = td->ss.dur;
2566 ts->ss_head = td->ss.head;
2567 ts->ss_bw_data = td->ss.bw_data;
2568 ts->ss_iops_data = td->ss.iops_data;
2569 ts->ss_limit.u.f = td->ss.limit;
2570 ts->ss_slope.u.f = td->ss.slope;
2571 ts->ss_deviation.u.f = td->ss.deviation;
2572 ts->ss_criterion.u.f = td->ss.criterion;
2575 ts->ss_dur = ts->ss_state = 0;
2578 for (i = 0; i < nr_ts; i++) {
2579 unsigned long long bw;
2581 ts = &threadstats[i];
2582 if (ts->groupid == -1)
2584 rs = &runstats[ts->groupid];
2585 rs->kb_base = ts->kb_base;
2586 rs->unit_base = ts->unit_base;
2587 rs->sig_figs = ts->sig_figs;
2588 rs->unified_rw_rep |= ts->unified_rw_rep;
2590 for (j = 0; j < DDIR_RWDIR_CNT; j++) {
2591 if (!ts->runtime[j])
2593 if (ts->runtime[j] < rs->min_run[j] || !rs->min_run[j])
2594 rs->min_run[j] = ts->runtime[j];
2595 if (ts->runtime[j] > rs->max_run[j])
2596 rs->max_run[j] = ts->runtime[j];
2600 bw = ts->io_bytes[j] * 1000 / ts->runtime[j];
2601 if (bw < rs->min_bw[j])
2603 if (bw > rs->max_bw[j])
2606 rs->iobytes[j] += ts->io_bytes[j];
2610 for (i = 0; i < groupid + 1; i++) {
2615 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) {
2616 if (rs->max_run[ddir])
2617 rs->agg[ddir] = (rs->iobytes[ddir] * 1000) /
2622 for (i = 0; i < FIO_OUTPUT_NR; i++)
2623 buf_output_init(&output[i]);
2626 * don't overwrite last signal output
2628 if (output_format & FIO_OUTPUT_NORMAL)
2629 log_buf(&output[__FIO_OUTPUT_NORMAL], "\n");
2630 if (output_format & FIO_OUTPUT_JSON) {
2631 struct thread_data *global;
2634 unsigned long long ms_since_epoch;
2637 gettimeofday(&now, NULL);
2638 ms_since_epoch = (unsigned long long)(now.tv_sec) * 1000 +
2639 (unsigned long long)(now.tv_usec) / 1000;
2641 tv_sec = now.tv_sec;
2642 os_ctime_r(&tv_sec, time_buf, sizeof(time_buf));
2643 if (time_buf[strlen(time_buf) - 1] == '\n')
2644 time_buf[strlen(time_buf) - 1] = '\0';
2646 root = json_create_object();
2647 json_object_add_value_string(root, "fio version", fio_version_string);
2648 json_object_add_value_int(root, "timestamp", now.tv_sec);
2649 json_object_add_value_int(root, "timestamp_ms", ms_since_epoch);
2650 json_object_add_value_string(root, "time", time_buf);
2651 global = get_global_options();
2652 json_add_job_opts(root, "global options", &global->opt_list);
2653 array = json_create_array();
2654 json_object_add_value_array(root, "jobs", array);
2658 fio_server_send_job_options(&get_global_options()->opt_list, -1U);
2660 for (i = 0; i < nr_ts; i++) {
2661 ts = &threadstats[i];
2662 rs = &runstats[ts->groupid];
2665 fio_server_send_job_options(opt_lists[i], i);
2666 fio_server_send_ts(ts, rs);
2668 if (output_format & FIO_OUTPUT_TERSE)
2669 show_thread_status_terse(ts, rs, &output[__FIO_OUTPUT_TERSE]);
2670 if (output_format & FIO_OUTPUT_JSON) {
2671 struct json_object *tmp = show_thread_status_json(ts, rs, opt_lists[i]);
2672 json_array_add_value_object(array, tmp);
2674 if (output_format & FIO_OUTPUT_NORMAL)
2675 show_thread_status_normal(ts, rs, &output[__FIO_OUTPUT_NORMAL]);
2678 if (!is_backend && (output_format & FIO_OUTPUT_JSON)) {
2679 /* disk util stats, if any */
2680 show_disk_util(1, root, &output[__FIO_OUTPUT_JSON]);
2682 show_idle_prof_stats(FIO_OUTPUT_JSON, root, &output[__FIO_OUTPUT_JSON]);
2684 json_print_object(root, &output[__FIO_OUTPUT_JSON]);
2685 log_buf(&output[__FIO_OUTPUT_JSON], "\n");
2686 json_free_object(root);
2689 for (i = 0; i < groupid + 1; i++) {
2694 fio_server_send_gs(rs);
2695 else if (output_format & FIO_OUTPUT_NORMAL)
2696 show_group_stats(rs, &output[__FIO_OUTPUT_NORMAL]);
2700 fio_server_send_du();
2701 else if (output_format & FIO_OUTPUT_NORMAL) {
2702 show_disk_util(0, NULL, &output[__FIO_OUTPUT_NORMAL]);
2703 show_idle_prof_stats(FIO_OUTPUT_NORMAL, NULL, &output[__FIO_OUTPUT_NORMAL]);
2706 for (i = 0; i < FIO_OUTPUT_NR; i++) {
2707 struct buf_output *out = &output[i];
2709 log_info_buf(out->buf, out->buflen);
2710 buf_output_free(out);
2713 fio_idle_prof_cleanup();
2718 /* free arrays allocated by sum_thread_stats(), if any */
2719 for (i = 0; i < nr_ts; i++) {
2720 ts = &threadstats[i];
2721 free_clat_prio_stats(ts);
2727 int __show_running_run_stats(void)
2729 unsigned long long *rt;
2732 fio_sem_down(stat_sem);
2734 rt = malloc(thread_number * sizeof(unsigned long long));
2735 fio_gettime(&ts, NULL);
2738 if (td->runstate >= TD_EXITED)
2741 td->update_rusage = 1;
2742 for_each_rw_ddir(ddir) {
2743 td->ts.io_bytes[ddir] = td->io_bytes[ddir];
2745 td->ts.total_run_time = mtime_since(&td->epoch, &ts);
2747 rt[__td_index] = mtime_since(&td->start, &ts);
2748 if (td_read(td) && td->ts.io_bytes[DDIR_READ])
2749 td->ts.runtime[DDIR_READ] += rt[__td_index];
2750 if (td_write(td) && td->ts.io_bytes[DDIR_WRITE])
2751 td->ts.runtime[DDIR_WRITE] += rt[__td_index];
2752 if (td_trim(td) && td->ts.io_bytes[DDIR_TRIM])
2753 td->ts.runtime[DDIR_TRIM] += rt[__td_index];
2757 if (td->runstate >= TD_EXITED)
2759 if (td->rusage_sem) {
2760 td->update_rusage = 1;
2761 fio_sem_down(td->rusage_sem);
2763 td->update_rusage = 0;
2769 if (td->runstate >= TD_EXITED)
2772 if (td_read(td) && td->ts.io_bytes[DDIR_READ])
2773 td->ts.runtime[DDIR_READ] -= rt[__td_index];
2774 if (td_write(td) && td->ts.io_bytes[DDIR_WRITE])
2775 td->ts.runtime[DDIR_WRITE] -= rt[__td_index];
2776 if (td_trim(td) && td->ts.io_bytes[DDIR_TRIM])
2777 td->ts.runtime[DDIR_TRIM] -= rt[__td_index];
2781 fio_sem_up(stat_sem);
2786 static bool status_file_disabled;
2788 #define FIO_STATUS_FILE "fio-dump-status"
2790 static int check_status_file(void)
2793 const char *temp_dir;
2794 char fio_status_file_path[PATH_MAX];
2796 if (status_file_disabled)
2799 temp_dir = getenv("TMPDIR");
2800 if (temp_dir == NULL) {
2801 temp_dir = getenv("TEMP");
2802 if (temp_dir && strlen(temp_dir) >= PATH_MAX)
2805 if (temp_dir == NULL)
2808 __coverity_tainted_data_sanitize__(temp_dir);
2811 snprintf(fio_status_file_path, sizeof(fio_status_file_path), "%s/%s", temp_dir, FIO_STATUS_FILE);
2813 if (stat(fio_status_file_path, &sb))
2816 if (unlink(fio_status_file_path) < 0) {
2817 log_err("fio: failed to unlink %s: %s\n", fio_status_file_path,
2819 log_err("fio: disabling status file updates\n");
2820 status_file_disabled = true;
2826 void check_for_running_stats(void)
2828 if (check_status_file()) {
2829 show_running_run_stats();
2834 static inline void add_stat_sample(struct io_stat *is, unsigned long long data)
2839 if (data > is->max_val)
2841 if (data < is->min_val)
2844 delta = val - is->mean.u.f;
2846 is->mean.u.f += delta / (is->samples + 1.0);
2847 is->S.u.f += delta * (val - is->mean.u.f);
2853 static inline void add_stat_prio_sample(struct clat_prio_stat *clat_prio,
2854 unsigned short clat_prio_index,
2855 unsigned long long nsec)
2858 add_stat_sample(&clat_prio[clat_prio_index].clat_stat, nsec);
2862 * Return a struct io_logs, which is added to the tail of the log
2865 static struct io_logs *get_new_log(struct io_log *iolog)
2868 struct io_logs *cur_log;
2871 * Cap the size at MAX_LOG_ENTRIES, so we don't keep doubling
2874 if (!iolog->cur_log_max) {
2876 new_samples = iolog->td->o.log_entries;
2878 new_samples = DEF_LOG_ENTRIES;
2880 new_samples = iolog->cur_log_max * 2;
2881 if (new_samples > MAX_LOG_ENTRIES)
2882 new_samples = MAX_LOG_ENTRIES;
2885 cur_log = smalloc(sizeof(*cur_log));
2887 INIT_FLIST_HEAD(&cur_log->list);
2888 cur_log->log = calloc(new_samples, log_entry_sz(iolog));
2890 cur_log->nr_samples = 0;
2891 cur_log->max_samples = new_samples;
2892 flist_add_tail(&cur_log->list, &iolog->io_logs);
2893 iolog->cur_log_max = new_samples;
2903 * Add and return a new log chunk, or return current log if big enough
2905 static struct io_logs *regrow_log(struct io_log *iolog)
2907 struct io_logs *cur_log;
2910 if (!iolog || iolog->disabled)
2913 cur_log = iolog_cur_log(iolog);
2915 cur_log = get_new_log(iolog);
2920 if (cur_log->nr_samples < cur_log->max_samples)
2924 * No room for a new sample. If we're compressing on the fly, flush
2925 * out the current chunk
2927 if (iolog->log_gz) {
2928 if (iolog_cur_flush(iolog, cur_log)) {
2929 log_err("fio: failed flushing iolog! Will stop logging.\n");
2935 * Get a new log array, and add to our list
2937 cur_log = get_new_log(iolog);
2939 log_err("fio: failed extending iolog! Will stop logging.\n");
2943 if (!iolog->pending || !iolog->pending->nr_samples)
2947 * Flush pending items to new log
2949 for (i = 0; i < iolog->pending->nr_samples; i++) {
2950 struct io_sample *src, *dst;
2952 src = get_sample(iolog, iolog->pending, i);
2953 dst = get_sample(iolog, cur_log, i);
2954 memcpy(dst, src, log_entry_sz(iolog));
2956 cur_log->nr_samples = iolog->pending->nr_samples;
2958 iolog->pending->nr_samples = 0;
2962 iolog->disabled = true;
2966 void regrow_logs(struct thread_data *td)
2968 regrow_log(td->slat_log);
2969 regrow_log(td->clat_log);
2970 regrow_log(td->clat_hist_log);
2971 regrow_log(td->lat_log);
2972 regrow_log(td->bw_log);
2973 regrow_log(td->iops_log);
2974 td->flags &= ~TD_F_REGROW_LOGS;
2977 void regrow_agg_logs(void)
2981 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++)
2982 regrow_log(agg_io_log[ddir]);
2985 static struct io_logs *get_cur_log(struct io_log *iolog)
2987 struct io_logs *cur_log;
2989 cur_log = iolog_cur_log(iolog);
2991 cur_log = get_new_log(iolog);
2996 if (cur_log->nr_samples < cur_log->max_samples)
3000 * Out of space. If we're in IO offload mode, or we're not doing
3001 * per unit logging (hence logging happens outside of the IO thread
3002 * as well), add a new log chunk inline. If we're doing inline
3003 * submissions, flag 'td' as needing a log regrow and we'll take
3004 * care of it on the submission side.
3006 if ((iolog->td && iolog->td->o.io_submit_mode == IO_MODE_OFFLOAD) ||
3007 !per_unit_log(iolog))
3008 return regrow_log(iolog);
3011 iolog->td->flags |= TD_F_REGROW_LOGS;
3013 assert(iolog->pending->nr_samples < iolog->pending->max_samples);
3014 return iolog->pending;
3017 static void __add_log_sample(struct io_log *iolog, union io_sample_data data,
3018 enum fio_ddir ddir, unsigned long long bs,
3019 unsigned long t, uint64_t offset,
3020 unsigned int priority)
3022 struct io_logs *cur_log;
3024 if (iolog->disabled)
3026 if (flist_empty(&iolog->io_logs))
3027 iolog->avg_last[ddir] = t;
3029 cur_log = get_cur_log(iolog);
3031 struct io_sample *s;
3033 s = get_sample(iolog, cur_log, cur_log->nr_samples);
3036 s->time = t + (iolog->td ? iolog->td->alternate_epoch : 0);
3037 io_sample_set_ddir(iolog, s, ddir);
3039 s->priority = priority;
3041 if (iolog->log_offset) {
3042 struct io_sample_offset *so = (void *) s;
3044 so->offset = offset;
3047 cur_log->nr_samples++;
3051 iolog->disabled = true;
3054 static inline void reset_io_stat(struct io_stat *ios)
3056 ios->min_val = -1ULL;
3057 ios->max_val = ios->samples = 0;
3058 ios->mean.u.f = ios->S.u.f = 0;
3061 static inline void reset_io_u_plat(uint64_t *io_u_plat)
3065 for (i = 0; i < FIO_IO_U_PLAT_NR; i++)
3069 static inline void reset_clat_prio_stats(struct thread_stat *ts)
3074 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) {
3075 if (!ts->clat_prio[ddir])
3078 for (i = 0; i < ts->nr_clat_prio[ddir]; i++) {
3079 reset_io_stat(&ts->clat_prio[ddir][i].clat_stat);
3080 reset_io_u_plat(ts->clat_prio[ddir][i].io_u_plat);
3085 void reset_io_stats(struct thread_data *td)
3087 struct thread_stat *ts = &td->ts;
3090 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
3091 reset_io_stat(&ts->clat_stat[i]);
3092 reset_io_stat(&ts->slat_stat[i]);
3093 reset_io_stat(&ts->lat_stat[i]);
3094 reset_io_stat(&ts->bw_stat[i]);
3095 reset_io_stat(&ts->iops_stat[i]);
3097 ts->io_bytes[i] = 0;
3099 ts->total_io_u[i] = 0;
3100 ts->short_io_u[i] = 0;
3101 ts->drop_io_u[i] = 0;
3104 for (i = 0; i < FIO_LAT_CNT; i++)
3105 for (j = 0; j < DDIR_RWDIR_CNT; j++)
3106 reset_io_u_plat(ts->io_u_plat[i][j]);
3108 reset_clat_prio_stats(ts);
3110 ts->total_io_u[DDIR_SYNC] = 0;
3111 reset_io_u_plat(ts->io_u_sync_plat);
3113 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
3114 ts->io_u_map[i] = 0;
3115 ts->io_u_submit[i] = 0;
3116 ts->io_u_complete[i] = 0;
3119 for (i = 0; i < FIO_IO_U_LAT_N_NR; i++)
3120 ts->io_u_lat_n[i] = 0;
3121 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
3122 ts->io_u_lat_u[i] = 0;
3123 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
3124 ts->io_u_lat_m[i] = 0;
3126 ts->total_submit = 0;
3127 ts->total_complete = 0;
3128 ts->nr_zone_resets = 0;
3129 ts->cachehit = ts->cachemiss = 0;
3132 static void __add_stat_to_log(struct io_log *iolog, enum fio_ddir ddir,
3133 unsigned long elapsed, bool log_max)
3136 * Note an entry in the log. Use the mean from the logged samples,
3137 * making sure to properly round up. Only write a log entry if we
3138 * had actual samples done.
3140 if (iolog->avg_window[ddir].samples) {
3141 union io_sample_data data;
3144 data.val = iolog->avg_window[ddir].max_val;
3146 data.val = iolog->avg_window[ddir].mean.u.f + 0.50;
3148 __add_log_sample(iolog, data, ddir, 0, elapsed, 0, 0);
3151 reset_io_stat(&iolog->avg_window[ddir]);
3154 static void _add_stat_to_log(struct io_log *iolog, unsigned long elapsed,
3159 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++)
3160 __add_stat_to_log(iolog, ddir, elapsed, log_max);
3163 static unsigned long add_log_sample(struct thread_data *td,
3164 struct io_log *iolog,
3165 union io_sample_data data,
3166 enum fio_ddir ddir, unsigned long long bs,
3167 uint64_t offset, unsigned int ioprio)
3169 unsigned long elapsed, this_window;
3174 elapsed = mtime_since_now(&td->epoch);
3177 * If no time averaging, just add the log sample.
3179 if (!iolog->avg_msec) {
3180 __add_log_sample(iolog, data, ddir, bs, elapsed, offset,
3186 * Add the sample. If the time period has passed, then
3187 * add that entry to the log and clear.
3189 add_stat_sample(&iolog->avg_window[ddir], data.val);
3192 * If period hasn't passed, adding the above sample is all we
3195 this_window = elapsed - iolog->avg_last[ddir];
3196 if (elapsed < iolog->avg_last[ddir])
3197 return iolog->avg_last[ddir] - elapsed;
3198 else if (this_window < iolog->avg_msec) {
3199 unsigned long diff = iolog->avg_msec - this_window;
3201 if (inline_log(iolog) || diff > LOG_MSEC_SLACK)
3205 __add_stat_to_log(iolog, ddir, elapsed, td->o.log_max != 0);
3207 iolog->avg_last[ddir] = elapsed - (elapsed % iolog->avg_msec);
3209 return iolog->avg_msec;
3212 void finalize_logs(struct thread_data *td, bool unit_logs)
3214 unsigned long elapsed;
3216 elapsed = mtime_since_now(&td->epoch);
3218 if (td->clat_log && unit_logs)
3219 _add_stat_to_log(td->clat_log, elapsed, td->o.log_max != 0);
3220 if (td->slat_log && unit_logs)
3221 _add_stat_to_log(td->slat_log, elapsed, td->o.log_max != 0);
3222 if (td->lat_log && unit_logs)
3223 _add_stat_to_log(td->lat_log, elapsed, td->o.log_max != 0);
3224 if (td->bw_log && (unit_logs == per_unit_log(td->bw_log)))
3225 _add_stat_to_log(td->bw_log, elapsed, td->o.log_max != 0);
3226 if (td->iops_log && (unit_logs == per_unit_log(td->iops_log)))
3227 _add_stat_to_log(td->iops_log, elapsed, td->o.log_max != 0);
3230 void add_agg_sample(union io_sample_data data, enum fio_ddir ddir,
3231 unsigned long long bs)
3233 struct io_log *iolog;
3238 iolog = agg_io_log[ddir];
3239 __add_log_sample(iolog, data, ddir, bs, mtime_since_genesis(), 0, 0);
3242 void add_sync_clat_sample(struct thread_stat *ts, unsigned long long nsec)
3244 unsigned int idx = plat_val_to_idx(nsec);
3245 assert(idx < FIO_IO_U_PLAT_NR);
3247 ts->io_u_sync_plat[idx]++;
3248 add_stat_sample(&ts->sync_stat, nsec);
3251 static inline void add_lat_percentile_sample(struct thread_stat *ts,
3252 unsigned long long nsec,
3256 unsigned int idx = plat_val_to_idx(nsec);
3257 assert(idx < FIO_IO_U_PLAT_NR);
3259 ts->io_u_plat[lat][ddir][idx]++;
3263 add_lat_percentile_prio_sample(struct thread_stat *ts, unsigned long long nsec,
3265 unsigned short clat_prio_index)
3267 unsigned int idx = plat_val_to_idx(nsec);
3269 if (ts->clat_prio[ddir])
3270 ts->clat_prio[ddir][clat_prio_index].io_u_plat[idx]++;
3273 void add_clat_sample(struct thread_data *td, enum fio_ddir ddir,
3274 unsigned long long nsec, unsigned long long bs,
3275 uint64_t offset, unsigned int ioprio,
3276 unsigned short clat_prio_index)
3278 const bool needs_lock = td_async_processing(td);
3279 unsigned long elapsed, this_window;
3280 struct thread_stat *ts = &td->ts;
3281 struct io_log *iolog = td->clat_hist_log;
3286 add_stat_sample(&ts->clat_stat[ddir], nsec);
3289 * When lat_percentiles=1 (default 0), the reported per priority
3290 * percentiles and stats are used for describing total latency values,
3291 * even though the variable names themselves start with clat_.
3293 * Because of the above definition, add a prio stat sample only when
3294 * lat_percentiles=0. add_lat_sample() will add the prio stat sample
3295 * when lat_percentiles=1.
3297 if (!ts->lat_percentiles)
3298 add_stat_prio_sample(ts->clat_prio[ddir], clat_prio_index,
3302 add_log_sample(td, td->clat_log, sample_val(nsec), ddir, bs,
3305 if (ts->clat_percentiles) {
3307 * Because of the above definition, add a prio lat percentile
3308 * sample only when lat_percentiles=0. add_lat_sample() will add
3309 * the prio lat percentile sample when lat_percentiles=1.
3311 add_lat_percentile_sample(ts, nsec, ddir, FIO_CLAT);
3312 if (!ts->lat_percentiles)
3313 add_lat_percentile_prio_sample(ts, nsec, ddir,
3317 if (iolog && iolog->hist_msec) {
3318 struct io_hist *hw = &iolog->hist_window[ddir];
3321 elapsed = mtime_since_now(&td->epoch);
3323 hw->hist_last = elapsed;
3324 this_window = elapsed - hw->hist_last;
3326 if (this_window >= iolog->hist_msec) {
3327 uint64_t *io_u_plat;
3328 struct io_u_plat_entry *dst;
3331 * Make a byte-for-byte copy of the latency histogram
3332 * stored in td->ts.io_u_plat[ddir], recording it in a
3333 * log sample. Note that the matching call to free() is
3334 * located in iolog.c after printing this sample to the
3337 io_u_plat = (uint64_t *) td->ts.io_u_plat[FIO_CLAT][ddir];
3338 dst = malloc(sizeof(struct io_u_plat_entry));
3339 memcpy(&(dst->io_u_plat), io_u_plat,
3340 FIO_IO_U_PLAT_NR * sizeof(uint64_t));
3341 flist_add(&dst->list, &hw->list);
3342 __add_log_sample(iolog, sample_plat(dst), ddir, bs,
3343 elapsed, offset, ioprio);
3346 * Update the last time we recorded as being now, minus
3347 * any drift in time we encountered before actually
3348 * making the record.
3350 hw->hist_last = elapsed - (this_window - iolog->hist_msec);
3356 __td_io_u_unlock(td);
3359 void add_slat_sample(struct thread_data *td, enum fio_ddir ddir,
3360 unsigned long long nsec, unsigned long long bs,
3361 uint64_t offset, unsigned int ioprio)
3363 const bool needs_lock = td_async_processing(td);
3364 struct thread_stat *ts = &td->ts;
3372 add_stat_sample(&ts->slat_stat[ddir], nsec);
3375 add_log_sample(td, td->slat_log, sample_val(nsec), ddir, bs,
3378 if (ts->slat_percentiles)
3379 add_lat_percentile_sample(ts, nsec, ddir, FIO_SLAT);
3382 __td_io_u_unlock(td);
3385 void add_lat_sample(struct thread_data *td, enum fio_ddir ddir,
3386 unsigned long long nsec, unsigned long long bs,
3387 uint64_t offset, unsigned int ioprio,
3388 unsigned short clat_prio_index)
3390 const bool needs_lock = td_async_processing(td);
3391 struct thread_stat *ts = &td->ts;
3399 add_stat_sample(&ts->lat_stat[ddir], nsec);
3402 add_log_sample(td, td->lat_log, sample_val(nsec), ddir, bs,
3406 * When lat_percentiles=1 (default 0), the reported per priority
3407 * percentiles and stats are used for describing total latency values,
3408 * even though the variable names themselves start with clat_.
3410 * Because of the above definition, add a prio stat and prio lat
3411 * percentile sample only when lat_percentiles=1. add_clat_sample() will
3412 * add the prio stat and prio lat percentile sample when
3413 * lat_percentiles=0.
3415 if (ts->lat_percentiles) {
3416 add_lat_percentile_sample(ts, nsec, ddir, FIO_LAT);
3417 add_lat_percentile_prio_sample(ts, nsec, ddir, clat_prio_index);
3418 add_stat_prio_sample(ts->clat_prio[ddir], clat_prio_index,
3422 __td_io_u_unlock(td);
3425 void add_bw_sample(struct thread_data *td, struct io_u *io_u,
3426 unsigned int bytes, unsigned long long spent)
3428 const bool needs_lock = td_async_processing(td);
3429 struct thread_stat *ts = &td->ts;
3433 rate = (unsigned long) (bytes * 1000000ULL / spent);
3440 add_stat_sample(&ts->bw_stat[io_u->ddir], rate);
3443 add_log_sample(td, td->bw_log, sample_val(rate), io_u->ddir,
3444 bytes, io_u->offset, io_u->ioprio);
3446 td->stat_io_bytes[io_u->ddir] = td->this_io_bytes[io_u->ddir];
3449 __td_io_u_unlock(td);
3452 static int __add_samples(struct thread_data *td, struct timespec *parent_tv,
3453 struct timespec *t, unsigned int avg_time,
3454 uint64_t *this_io_bytes, uint64_t *stat_io_bytes,
3455 struct io_stat *stat, struct io_log *log,
3458 const bool needs_lock = td_async_processing(td);
3459 unsigned long spent, rate;
3461 unsigned long next, next_log;
3463 next_log = avg_time;
3465 spent = mtime_since(parent_tv, t);
3466 if (spent < avg_time && avg_time - spent > LOG_MSEC_SLACK)
3467 return avg_time - spent;
3473 * Compute both read and write rates for the interval.
3475 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) {
3478 delta = this_io_bytes[ddir] - stat_io_bytes[ddir];
3480 continue; /* No entries for interval */
3484 rate = delta * 1000 / spent / 1024; /* KiB/s */
3486 rate = (delta * 1000) / spent;
3490 add_stat_sample(&stat[ddir], rate);
3493 unsigned long long bs = 0;
3495 if (td->o.min_bs[ddir] == td->o.max_bs[ddir])
3496 bs = td->o.min_bs[ddir];
3498 next = add_log_sample(td, log, sample_val(rate), ddir,
3500 next_log = min(next_log, next);
3503 stat_io_bytes[ddir] = this_io_bytes[ddir];
3509 __td_io_u_unlock(td);
3511 if (spent <= avg_time)
3514 next = avg_time - (1 + spent - avg_time);
3516 return min(next, next_log);
3519 static int add_bw_samples(struct thread_data *td, struct timespec *t)
3521 return __add_samples(td, &td->bw_sample_time, t, td->o.bw_avg_time,
3522 td->this_io_bytes, td->stat_io_bytes,
3523 td->ts.bw_stat, td->bw_log, true);
3526 void add_iops_sample(struct thread_data *td, struct io_u *io_u,
3529 const bool needs_lock = td_async_processing(td);
3530 struct thread_stat *ts = &td->ts;
3535 add_stat_sample(&ts->iops_stat[io_u->ddir], 1);
3538 add_log_sample(td, td->iops_log, sample_val(1), io_u->ddir,
3539 bytes, io_u->offset, io_u->ioprio);
3541 td->stat_io_blocks[io_u->ddir] = td->this_io_blocks[io_u->ddir];
3544 __td_io_u_unlock(td);
3547 static int add_iops_samples(struct thread_data *td, struct timespec *t)
3549 return __add_samples(td, &td->iops_sample_time, t, td->o.iops_avg_time,
3550 td->this_io_blocks, td->stat_io_blocks,
3551 td->ts.iops_stat, td->iops_log, false);
3555 * Returns msecs to next event
3557 int calc_log_samples(void)
3559 unsigned int next = ~0U, tmp = 0, next_mod = 0, log_avg_msec_min = -1U;
3560 struct timespec now;
3561 long elapsed_time = 0;
3563 fio_gettime(&now, NULL);
3566 elapsed_time = mtime_since_now(&td->epoch);
3570 if (in_ramp_time(td) ||
3571 !(td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING)) {
3572 next = min(td->o.iops_avg_time, td->o.bw_avg_time);
3576 (td->bw_log && !per_unit_log(td->bw_log))) {
3577 tmp = add_bw_samples(td, &now);
3580 log_avg_msec_min = min(log_avg_msec_min, (unsigned int)td->bw_log->avg_msec);
3582 if (!td->iops_log ||
3583 (td->iops_log && !per_unit_log(td->iops_log))) {
3584 tmp = add_iops_samples(td, &now);
3587 log_avg_msec_min = min(log_avg_msec_min, (unsigned int)td->iops_log->avg_msec);
3594 /* if log_avg_msec_min has not been changed, set it to 0 */
3595 if (log_avg_msec_min == -1U)
3596 log_avg_msec_min = 0;
3598 if (log_avg_msec_min == 0)
3599 next_mod = elapsed_time;
3601 next_mod = elapsed_time % log_avg_msec_min;
3603 /* correction to keep the time on the log avg msec boundary */
3604 next = min(next, (log_avg_msec_min - next_mod));
3606 return next == ~0U ? 0 : next;
3609 void stat_init(void)
3611 stat_sem = fio_sem_init(FIO_SEM_UNLOCKED);
3614 void stat_exit(void)
3617 * When we have the mutex, we know out-of-band access to it
3620 fio_sem_down(stat_sem);
3621 fio_sem_remove(stat_sem);
3625 * Called from signal handler. Wake up status thread.
3627 void show_running_run_stats(void)
3632 uint32_t *io_u_block_info(struct thread_data *td, struct io_u *io_u)
3634 /* Ignore io_u's which span multiple blocks--they will just get
3635 * inaccurate counts. */
3636 int idx = (io_u->offset - io_u->file->file_offset)
3637 / td->o.bs[DDIR_TRIM];
3638 uint32_t *info = &td->ts.block_infos[idx];
3639 assert(idx < td->ts.nr_block_infos);