configure: fix check_min_lib_version() eval
[fio.git] / stat.c
CommitLineData
3c39a379
JA
1#include <stdio.h>
2#include <string.h>
3#include <sys/time.h>
5c4e1dbc 4#include <sys/stat.h>
3c39a379
JA
5#include <math.h>
6
7#include "fio.h"
7c9b1bce 8#include "diskutil.h"
c7c6cb4c 9#include "lib/ieee754.h"
cc372b17 10#include "json.h"
44404c5a 11#include "lib/getrusage.h"
f2a2ce0e 12#include "idletime.h"
0f38bbef 13#include "lib/pow2.h"
a666cab8 14#include "lib/output_buffer.h"
a39fb9ea 15#include "helper_thread.h"
90d2d53f 16#include "smalloc.h"
fd5d733f 17#include "zbd.h"
f5bff36e 18#include "oslib/asprintf.h"
3c39a379 19
674456bf 20#define LOG_MSEC_SLACK 1
d454a205 21
971caeb1 22struct fio_sem *stat_sem;
cef9175e 23
210dd0fc
JA
24void clear_rusage_stat(struct thread_data *td)
25{
26 struct thread_stat *ts = &td->ts;
27
28 fio_getrusage(&td->ru_start);
29 ts->usr_time = ts->sys_time = 0;
30 ts->ctx = 0;
31 ts->minf = ts->majf = 0;
32}
33
3c39a379
JA
34void update_rusage_stat(struct thread_data *td)
35{
756867bd 36 struct thread_stat *ts = &td->ts;
3c39a379 37
44404c5a 38 fio_getrusage(&td->ru_end);
8b6a404c 39 ts->usr_time += mtime_since_tv(&td->ru_start.ru_utime,
c8aaba19 40 &td->ru_end.ru_utime);
8b6a404c 41 ts->sys_time += mtime_since_tv(&td->ru_start.ru_stime,
c8aaba19
JA
42 &td->ru_end.ru_stime);
43 ts->ctx += td->ru_end.ru_nvcsw + td->ru_end.ru_nivcsw
44 - (td->ru_start.ru_nvcsw + td->ru_start.ru_nivcsw);
45 ts->minf += td->ru_end.ru_minflt - td->ru_start.ru_minflt;
46 ts->majf += td->ru_end.ru_majflt - td->ru_start.ru_majflt;
5ec10eaa 47
c8aaba19 48 memcpy(&td->ru_start, &td->ru_end, sizeof(td->ru_end));
3c39a379
JA
49}
50
83349190
YH
51/*
52 * Given a latency, return the index of the corresponding bucket in
53 * the structure tracking percentiles.
54 *
55 * (1) find the group (and error bits) that the value (latency)
56 * belongs to by looking at its MSB. (2) find the bucket number in the
57 * group by looking at the index bits.
58 *
59 */
d6bb626e 60static unsigned int plat_val_to_idx(unsigned long long val)
83349190
YH
61{
62 unsigned int msb, error_bits, base, offset, idx;
63
64 /* Find MSB starting from bit 0 */
65 if (val == 0)
66 msb = 0;
67 else
d6bb626e 68 msb = (sizeof(val)*8) - __builtin_clzll(val) - 1;
83349190 69
716050f2
JA
70 /*
71 * MSB <= (FIO_IO_U_PLAT_BITS-1), cannot be rounded off. Use
72 * all bits of the sample as index
73 */
83349190
YH
74 if (msb <= FIO_IO_U_PLAT_BITS)
75 return val;
76
77 /* Compute the number of error bits to discard*/
78 error_bits = msb - FIO_IO_U_PLAT_BITS;
79
80 /* Compute the number of buckets before the group */
81 base = (error_bits + 1) << FIO_IO_U_PLAT_BITS;
82
716050f2
JA
83 /*
84 * Discard the error bits and apply the mask to find the
3c3ed070 85 * index for the buckets in the group
716050f2 86 */
83349190
YH
87 offset = (FIO_IO_U_PLAT_VAL - 1) & (val >> error_bits);
88
89 /* Make sure the index does not exceed (array size - 1) */
3c3ed070 90 idx = (base + offset) < (FIO_IO_U_PLAT_NR - 1) ?
83349190
YH
91 (base + offset) : (FIO_IO_U_PLAT_NR - 1);
92
93 return idx;
94}
95
96/*
97 * Convert the given index of the bucket array to the value
98 * represented by the bucket
99 */
129e4193 100static unsigned long long plat_idx_to_val(unsigned int idx)
83349190 101{
c8b44cfa
RL
102 unsigned int error_bits;
103 unsigned long long k, base;
83349190
YH
104
105 assert(idx < FIO_IO_U_PLAT_NR);
106
107 /* MSB <= (FIO_IO_U_PLAT_BITS-1), cannot be rounded off. Use
108 * all bits of the sample as index */
3c3ed070 109 if (idx < (FIO_IO_U_PLAT_VAL << 1))
83349190
YH
110 return idx;
111
112 /* Find the group and compute the minimum value of that group */
3c3ed070 113 error_bits = (idx >> FIO_IO_U_PLAT_BITS) - 1;
c8b44cfa 114 base = ((unsigned long long) 1) << (error_bits + FIO_IO_U_PLAT_BITS);
83349190
YH
115
116 /* Find its bucket number of the group */
117 k = idx % FIO_IO_U_PLAT_VAL;
118
119 /* Return the mean of the range of the bucket */
120 return base + ((k + 0.5) * (1 << error_bits));
121}
122
123static int double_cmp(const void *a, const void *b)
124{
802ad4a8
JA
125 const fio_fp64_t fa = *(const fio_fp64_t *) a;
126 const fio_fp64_t fb = *(const fio_fp64_t *) b;
83349190
YH
127 int cmp = 0;
128
802ad4a8 129 if (fa.u.f > fb.u.f)
83349190 130 cmp = 1;
802ad4a8 131 else if (fa.u.f < fb.u.f)
83349190
YH
132 cmp = -1;
133
134 return cmp;
135}
136
6cc0e5aa 137unsigned int calc_clat_percentiles(uint64_t *io_u_plat, unsigned long long nr,
d6bb626e
VF
138 fio_fp64_t *plist, unsigned long long **output,
139 unsigned long long *maxv, unsigned long long *minv)
83349190 140{
447b94e1 141 unsigned long long sum = 0;
1db92cb6 142 unsigned int len, i, j = 0;
d6bb626e 143 unsigned long long *ovals = NULL;
8985b491 144 bool is_last;
1db92cb6 145
d6bb626e 146 *minv = -1ULL;
1db92cb6 147 *maxv = 0;
83349190 148
802ad4a8
JA
149 len = 0;
150 while (len < FIO_IO_U_LIST_MAX_LEN && plist[len].u.f != 0.0)
151 len++;
716050f2 152
351de8de 153 if (!len)
1db92cb6 154 return 0;
351de8de 155
716050f2 156 /*
802ad4a8
JA
157 * Sort the percentile list. Note that it may already be sorted if
158 * we are using the default values, but since it's a short list this
159 * isn't a worry. Also note that this does not work for NaN values.
716050f2 160 */
802ad4a8 161 if (len > 1)
68e0dc1a 162 qsort(plist, len, sizeof(plist[0]), double_cmp);
83349190 163
5d89c625
VF
164 ovals = malloc(len * sizeof(*ovals));
165 if (!ovals)
166 return 0;
167
4f6f8298
JA
168 /*
169 * Calculate bucket values, note down max and min values
170 */
8985b491 171 is_last = false;
07511a63 172 for (i = 0; i < FIO_IO_U_PLAT_NR && !is_last; i++) {
83349190 173 sum += io_u_plat[i];
298d751e 174 while (sum >= ((long double) plist[j].u.f / 100.0 * nr)) {
802ad4a8 175 assert(plist[j].u.f <= 100.0);
83349190 176
4f6f8298 177 ovals[j] = plat_idx_to_val(i);
1db92cb6
JA
178 if (ovals[j] < *minv)
179 *minv = ovals[j];
180 if (ovals[j] > *maxv)
181 *maxv = ovals[j];
07511a63 182
8985b491 183 is_last = (j == len - 1) != 0;
07511a63
JA
184 if (is_last)
185 break;
186
4f6f8298
JA
187 j++;
188 }
189 }
83349190 190
298d751e
VF
191 if (!is_last)
192 log_err("fio: error calculating latency percentiles\n");
193
1db92cb6
JA
194 *output = ovals;
195 return len;
196}
197
198/*
199 * Find and display the p-th percentile of clat
200 */
6cc0e5aa 201static void show_clat_percentiles(uint64_t *io_u_plat, unsigned long long nr,
a666cab8 202 fio_fp64_t *plist, unsigned int precision,
b2b3eefe 203 const char *pre, struct buf_output *out)
1db92cb6 204{
d6bb626e
VF
205 unsigned int divisor, len, i, j = 0;
206 unsigned long long minv, maxv;
207 unsigned long long *ovals;
8985b491 208 int per_line, scale_down, time_width;
8985b491 209 bool is_last;
eef02441 210 char fmt[32];
1db92cb6
JA
211
212 len = calc_clat_percentiles(io_u_plat, nr, plist, &ovals, &maxv, &minv);
b2b3eefe 213 if (!len || !ovals)
1db92cb6
JA
214 goto out;
215
4f6f8298 216 /*
d6bb626e
VF
217 * We default to nsecs, but if the value range is such that we
218 * should scale down to usecs or msecs, do that.
4f6f8298 219 */
d6bb626e
VF
220 if (minv > 2000000 && maxv > 99999999ULL) {
221 scale_down = 2;
222 divisor = 1000000;
b599759b 223 log_buf(out, " %s percentiles (msec):\n |", pre);
d6bb626e
VF
224 } else if (minv > 2000 && maxv > 99999) {
225 scale_down = 1;
226 divisor = 1000;
b599759b 227 log_buf(out, " %s percentiles (usec):\n |", pre);
4f6f8298
JA
228 } else {
229 scale_down = 0;
d6bb626e 230 divisor = 1;
b599759b 231 log_buf(out, " %s percentiles (nsec):\n |", pre);
4f6f8298 232 }
83349190 233
619adf9c 234
d6bb626e 235 time_width = max(5, (int) (log10(maxv / divisor) + 1));
74558486
JA
236 snprintf(fmt, sizeof(fmt), " %%%u.%ufth=[%%%dllu]%%c", precision + 3,
237 precision, time_width);
238 /* fmt will be something like " %5.2fth=[%4llu]%c" */
d6bb626e 239 per_line = (80 - 7) / (precision + 10 + time_width);
81ab0b3a 240
d6bb626e 241 for (j = 0; j < len; j++) {
4f6f8298 242 /* for formatting */
eef02441 243 if (j != 0 && (j % per_line) == 0)
a666cab8 244 log_buf(out, " |");
83349190 245
4f6f8298 246 /* end of the list */
8985b491 247 is_last = (j == len - 1) != 0;
83349190 248
d6bb626e 249 for (i = 0; i < scale_down; i++)
4f6f8298
JA
250 ovals[j] = (ovals[j] + 999) / 1000;
251
d6bb626e 252 log_buf(out, fmt, plist[j].u.f, ovals[j], is_last ? '\n' : ',');
4f6f8298
JA
253
254 if (is_last)
255 break;
256
eef02441 257 if ((j % per_line) == per_line - 1) /* for formatting */
a666cab8 258 log_buf(out, "\n");
83349190 259 }
4f6f8298 260
1db92cb6 261out:
283feb79 262 free(ovals);
83349190
YH
263}
264
74558486
JA
265bool calc_lat(struct io_stat *is, unsigned long long *min,
266 unsigned long long *max, double *mean, double *dev)
3c39a379 267{
ee0ccb79 268 double n = (double) is->samples;
3c39a379 269
ee0ccb79 270 if (n == 0)
8aa89d70 271 return false;
3c39a379
JA
272
273 *min = is->min_val;
274 *max = is->max_val;
802ad4a8 275 *mean = is->mean.u.f;
e6d276f2 276
68704084 277 if (n > 1.0)
802ad4a8 278 *dev = sqrt(is->S.u.f / (n - 1.0));
ef9c5c40 279 else
4b43f54e 280 *dev = 0;
ef9c5c40 281
8aa89d70 282 return true;
3c39a379
JA
283}
284
5cb8a8cd
BP
285void show_mixed_group_stats(struct group_run_stats *rs, struct buf_output *out)
286{
287 char *io, *agg, *min, *max;
288 char *ioalt, *aggalt, *minalt, *maxalt;
289 uint64_t io_mix = 0, agg_mix = 0, min_mix = -1, max_mix = 0, min_run = -1, max_run = 0;
290 int i;
291 const int i2p = is_power_of_2(rs->kb_base);
292
293 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
294 if (!rs->max_run[i])
295 continue;
296 io_mix += rs->iobytes[i];
297 agg_mix += rs->agg[i];
298 min_mix = min_mix < rs->min_bw[i] ? min_mix : rs->min_bw[i];
299 max_mix = max_mix > rs->max_bw[i] ? max_mix : rs->max_bw[i];
300 min_run = min_run < rs->min_run[i] ? min_run : rs->min_run[i];
301 max_run = max_run > rs->max_run[i] ? max_run : rs->max_run[i];
302 }
303 io = num2str(io_mix, rs->sig_figs, 1, i2p, N2S_BYTE);
304 ioalt = num2str(io_mix, rs->sig_figs, 1, !i2p, N2S_BYTE);
305 agg = num2str(agg_mix, rs->sig_figs, 1, i2p, rs->unit_base);
306 aggalt = num2str(agg_mix, rs->sig_figs, 1, !i2p, rs->unit_base);
307 min = num2str(min_mix, rs->sig_figs, 1, i2p, rs->unit_base);
308 minalt = num2str(min_mix, rs->sig_figs, 1, !i2p, rs->unit_base);
309 max = num2str(max_mix, rs->sig_figs, 1, i2p, rs->unit_base);
310 maxalt = num2str(max_mix, rs->sig_figs, 1, !i2p, rs->unit_base);
311 log_buf(out, " MIXED: bw=%s (%s), %s-%s (%s-%s), io=%s (%s), run=%llu-%llumsec\n",
312 agg, aggalt, min, max, minalt, maxalt, io, ioalt,
313 (unsigned long long) min_run,
314 (unsigned long long) max_run);
315 free(io);
316 free(agg);
317 free(min);
318 free(max);
319 free(ioalt);
320 free(aggalt);
321 free(minalt);
322 free(maxalt);
323}
324
a666cab8 325void show_group_stats(struct group_run_stats *rs, struct buf_output *out)
3c39a379 326{
d694a6a7
RE
327 char *io, *agg, *min, *max;
328 char *ioalt, *aggalt, *minalt, *maxalt;
42da5c8b 329 const char *str[] = { " READ", " WRITE" , " TRIM"};
dbe1125e
JA
330 int i;
331
a666cab8 332 log_buf(out, "\nRun status group %d (all jobs):\n", rs->groupid);
3c39a379 333
6eaf09d6 334 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
90fef2d1
JA
335 const int i2p = is_power_of_2(rs->kb_base);
336
dbe1125e
JA
337 if (!rs->max_run[i])
338 continue;
339
e883cb35
JF
340 io = num2str(rs->iobytes[i], rs->sig_figs, 1, i2p, N2S_BYTE);
341 ioalt = num2str(rs->iobytes[i], rs->sig_figs, 1, !i2p, N2S_BYTE);
342 agg = num2str(rs->agg[i], rs->sig_figs, 1, i2p, rs->unit_base);
343 aggalt = num2str(rs->agg[i], rs->sig_figs, 1, !i2p, rs->unit_base);
344 min = num2str(rs->min_bw[i], rs->sig_figs, 1, i2p, rs->unit_base);
345 minalt = num2str(rs->min_bw[i], rs->sig_figs, 1, !i2p, rs->unit_base);
346 max = num2str(rs->max_bw[i], rs->sig_figs, 1, i2p, rs->unit_base);
347 maxalt = num2str(rs->max_bw[i], rs->sig_figs, 1, !i2p, rs->unit_base);
d694a6a7 348 log_buf(out, "%s: bw=%s (%s), %s-%s (%s-%s), io=%s (%s), run=%llu-%llumsec\n",
5cb8a8cd 349 (rs->unified_rw_rep == UNIFIED_MIXED) ? " MIXED" : str[i],
d694a6a7 350 agg, aggalt, min, max, minalt, maxalt, io, ioalt,
4e0a8fa2
JA
351 (unsigned long long) rs->min_run[i],
352 (unsigned long long) rs->max_run[i]);
dbe1125e 353
d694a6a7
RE
354 free(io);
355 free(agg);
356 free(min);
357 free(max);
358 free(ioalt);
359 free(aggalt);
360 free(minalt);
361 free(maxalt);
dbe1125e 362 }
5cb8a8cd
BP
363
364 /* Need to aggregate statisitics to show mixed values */
365 if (rs->unified_rw_rep == UNIFIED_BOTH)
366 show_mixed_group_stats(rs, out);
3c39a379
JA
367}
368
6cc0e5aa 369void stat_calc_dist(uint64_t *map, unsigned long total, double *io_u_dist)
2270890c
JA
370{
371 int i;
372
373 /*
374 * Do depth distribution calculations
375 */
376 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
838bc709
JA
377 if (total) {
378 io_u_dist[i] = (double) map[i] / (double) total;
379 io_u_dist[i] *= 100.0;
380 if (io_u_dist[i] < 0.1 && map[i])
381 io_u_dist[i] = 0.1;
382 } else
383 io_u_dist[i] = 0.0;
2270890c
JA
384 }
385}
386
04a0feae 387static void stat_calc_lat(struct thread_stat *ts, double *dst,
6cc0e5aa 388 uint64_t *src, int nr)
2270890c 389{
d79db122 390 unsigned long total = ddir_rw_sum(ts->total_io_u);
2270890c
JA
391 int i;
392
393 /*
394 * Do latency distribution calculations
395 */
04a0feae 396 for (i = 0; i < nr; i++) {
838bc709
JA
397 if (total) {
398 dst[i] = (double) src[i] / (double) total;
399 dst[i] *= 100.0;
400 if (dst[i] < 0.01 && src[i])
401 dst[i] = 0.01;
402 } else
403 dst[i] = 0.0;
2270890c
JA
404 }
405}
406
247823cc
VF
407/*
408 * To keep the terse format unaltered, add all of the ns latency
409 * buckets to the first us latency bucket
410 */
52e4c651 411static void stat_calc_lat_nu(struct thread_stat *ts, double *io_u_lat_u)
247823cc
VF
412{
413 unsigned long ntotal = 0, total = ddir_rw_sum(ts->total_io_u);
414 int i;
415
416 stat_calc_lat(ts, io_u_lat_u, ts->io_u_lat_u, FIO_IO_U_LAT_U_NR);
417
418 for (i = 0; i < FIO_IO_U_LAT_N_NR; i++)
419 ntotal += ts->io_u_lat_n[i];
420
421 io_u_lat_u[0] += 100.0 * (double) ntotal / (double) total;
422}
423
d6bb626e
VF
424void stat_calc_lat_n(struct thread_stat *ts, double *io_u_lat)
425{
426 stat_calc_lat(ts, io_u_lat, ts->io_u_lat_n, FIO_IO_U_LAT_N_NR);
427}
428
e5bd1347 429void stat_calc_lat_u(struct thread_stat *ts, double *io_u_lat)
04a0feae
JA
430{
431 stat_calc_lat(ts, io_u_lat, ts->io_u_lat_u, FIO_IO_U_LAT_U_NR);
432}
433
e5bd1347 434void stat_calc_lat_m(struct thread_stat *ts, double *io_u_lat)
04a0feae
JA
435{
436 stat_calc_lat(ts, io_u_lat, ts->io_u_lat_m, FIO_IO_U_LAT_M_NR);
437}
438
74558486
JA
439static void display_lat(const char *name, unsigned long long min,
440 unsigned long long max, double mean, double dev,
441 struct buf_output *out)
ea2accc5 442{
d6bb626e 443 const char *base = "(nsec)";
b29ad562 444 char *minp, *maxp;
ea2accc5 445
d6bb626e 446 if (nsec_to_msec(&min, &max, &mean, &dev))
b29ad562 447 base = "(msec)";
d6bb626e
VF
448 else if (nsec_to_usec(&min, &max, &mean, &dev))
449 base = "(usec)";
b29ad562 450
d694a6a7
RE
451 minp = num2str(min, 6, 1, 0, N2S_NONE);
452 maxp = num2str(max, 6, 1, 0, N2S_NONE);
b29ad562 453
a666cab8 454 log_buf(out, " %s %s: min=%s, max=%s, avg=%5.02f,"
b29ad562
JA
455 " stdev=%5.02f\n", name, base, minp, maxp, mean, dev);
456
457 free(minp);
458 free(maxp);
ea2accc5
JA
459}
460
5c0abd5e 461static double convert_agg_kbytes_percent(struct group_run_stats *rs, int ddir, int mean)
462{
463 double p_of_agg = 100.0;
464 if (rs && rs->agg[ddir] > 1024) {
465 p_of_agg = mean * 100 / (double) (rs->agg[ddir] / 1024.0);
466
467 if (p_of_agg > 100.0)
468 p_of_agg = 100.0;
469 }
470 return p_of_agg;
471}
472
5cb8a8cd
BP
473static void show_mixed_ddir_status(struct group_run_stats *rs, struct thread_stat *ts,
474 struct buf_output *out)
475{
476 unsigned long runt;
477 unsigned long long min, max, bw, iops;
478 double mean, dev;
479 char *io_p, *bw_p, *bw_p_alt, *iops_p, *post_st = NULL;
480 struct thread_stat *ts_lcl;
481
482 int i2p;
483 int ddir = 0, i;
484
485 /* Handle aggregation of Reads (ddir = 0), Writes (ddir = 1), and Trims (ddir = 2) */
486 ts_lcl = malloc(sizeof(struct thread_stat));
487 memset((void *)ts_lcl, 0, sizeof(struct thread_stat));
488 ts_lcl->unified_rw_rep = UNIFIED_MIXED; /* calculate mixed stats */
489 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
490 ts_lcl->clat_stat[i].min_val = ULONG_MAX;
491 ts_lcl->slat_stat[i].min_val = ULONG_MAX;
492 ts_lcl->lat_stat[i].min_val = ULONG_MAX;
493 ts_lcl->bw_stat[i].min_val = ULONG_MAX;
494 ts_lcl->iops_stat[i].min_val = ULONG_MAX;
495 ts_lcl->clat_high_prio_stat[i].min_val = ULONG_MAX;
496 ts_lcl->clat_low_prio_stat[i].min_val = ULONG_MAX;
497 }
498 ts_lcl->sync_stat.min_val = ULONG_MAX;
499
500 sum_thread_stats(ts_lcl, ts, 1);
501
502 assert(ddir_rw(ddir));
503
504 if (!ts_lcl->runtime[ddir])
505 return;
506
507 i2p = is_power_of_2(rs->kb_base);
508 runt = ts_lcl->runtime[ddir];
509
510 bw = (1000 * ts_lcl->io_bytes[ddir]) / runt;
511 io_p = num2str(ts_lcl->io_bytes[ddir], ts->sig_figs, 1, i2p, N2S_BYTE);
512 bw_p = num2str(bw, ts->sig_figs, 1, i2p, ts->unit_base);
513 bw_p_alt = num2str(bw, ts->sig_figs, 1, !i2p, ts->unit_base);
514
515 iops = (1000 * ts_lcl->total_io_u[ddir]) / runt;
516 iops_p = num2str(iops, ts->sig_figs, 1, 0, N2S_NONE);
517
518 log_buf(out, " mixed: IOPS=%s, BW=%s (%s)(%s/%llumsec)%s\n",
519 iops_p, bw_p, bw_p_alt, io_p,
520 (unsigned long long) ts_lcl->runtime[ddir],
521 post_st ? : "");
522
523 free(post_st);
524 free(io_p);
525 free(bw_p);
526 free(bw_p_alt);
527 free(iops_p);
528
529 if (calc_lat(&ts_lcl->slat_stat[ddir], &min, &max, &mean, &dev))
530 display_lat("slat", min, max, mean, dev, out);
531 if (calc_lat(&ts_lcl->clat_stat[ddir], &min, &max, &mean, &dev))
532 display_lat("clat", min, max, mean, dev, out);
533 if (calc_lat(&ts_lcl->lat_stat[ddir], &min, &max, &mean, &dev))
534 display_lat(" lat", min, max, mean, dev, out);
535 if (calc_lat(&ts_lcl->clat_high_prio_stat[ddir], &min, &max, &mean, &dev)) {
536 display_lat(ts_lcl->lat_percentiles ? "high prio_lat" : "high prio_clat",
537 min, max, mean, dev, out);
538 if (calc_lat(&ts_lcl->clat_low_prio_stat[ddir], &min, &max, &mean, &dev))
539 display_lat(ts_lcl->lat_percentiles ? "low prio_lat" : "low prio_clat",
540 min, max, mean, dev, out);
541 }
542
543 if (ts->slat_percentiles && ts_lcl->slat_stat[ddir].samples > 0)
544 show_clat_percentiles(ts_lcl->io_u_plat[FIO_SLAT][ddir],
545 ts_lcl->slat_stat[ddir].samples,
546 ts->percentile_list,
547 ts->percentile_precision, "slat", out);
548 if (ts->clat_percentiles && ts_lcl->clat_stat[ddir].samples > 0)
549 show_clat_percentiles(ts_lcl->io_u_plat[FIO_CLAT][ddir],
550 ts_lcl->clat_stat[ddir].samples,
551 ts->percentile_list,
552 ts->percentile_precision, "clat", out);
553 if (ts->lat_percentiles && ts_lcl->lat_stat[ddir].samples > 0)
554 show_clat_percentiles(ts_lcl->io_u_plat[FIO_LAT][ddir],
555 ts_lcl->lat_stat[ddir].samples,
556 ts->percentile_list,
557 ts->percentile_precision, "lat", out);
558
559 if (ts->clat_percentiles || ts->lat_percentiles) {
560 const char *name = ts->lat_percentiles ? "lat" : "clat";
561 char prio_name[32];
562 uint64_t samples;
563
564 if (ts->lat_percentiles)
565 samples = ts_lcl->lat_stat[ddir].samples;
566 else
567 samples = ts_lcl->clat_stat[ddir].samples;
568
569 /* Only print this if some high and low priority stats were collected */
570 if (ts_lcl->clat_high_prio_stat[ddir].samples > 0 &&
571 ts_lcl->clat_low_prio_stat[ddir].samples > 0)
572 {
573 sprintf(prio_name, "high prio (%.2f%%) %s",
574 100. * (double) ts_lcl->clat_high_prio_stat[ddir].samples / (double) samples,
575 name);
576 show_clat_percentiles(ts_lcl->io_u_plat_high_prio[ddir],
577 ts_lcl->clat_high_prio_stat[ddir].samples,
578 ts->percentile_list,
579 ts->percentile_precision, prio_name, out);
580
581 sprintf(prio_name, "low prio (%.2f%%) %s",
582 100. * (double) ts_lcl->clat_low_prio_stat[ddir].samples / (double) samples,
583 name);
584 show_clat_percentiles(ts_lcl->io_u_plat_low_prio[ddir],
585 ts_lcl->clat_low_prio_stat[ddir].samples,
586 ts->percentile_list,
587 ts->percentile_precision, prio_name, out);
588 }
589 }
590
591 if (calc_lat(&ts_lcl->bw_stat[ddir], &min, &max, &mean, &dev)) {
592 double p_of_agg = 100.0, fkb_base = (double)rs->kb_base;
593 const char *bw_str;
594
595 if ((rs->unit_base == 1) && i2p)
596 bw_str = "Kibit";
597 else if (rs->unit_base == 1)
598 bw_str = "kbit";
599 else if (i2p)
600 bw_str = "KiB";
601 else
602 bw_str = "kB";
603
604 p_of_agg = convert_agg_kbytes_percent(rs, ddir, mean);
605
606 if (rs->unit_base == 1) {
607 min *= 8.0;
608 max *= 8.0;
609 mean *= 8.0;
610 dev *= 8.0;
611 }
612
613 if (mean > fkb_base * fkb_base) {
614 min /= fkb_base;
615 max /= fkb_base;
616 mean /= fkb_base;
617 dev /= fkb_base;
618 bw_str = (rs->unit_base == 1 ? "Mibit" : "MiB");
619 }
620
621 log_buf(out, " bw (%5s/s): min=%5llu, max=%5llu, per=%3.2f%%, "
622 "avg=%5.02f, stdev=%5.02f, samples=%" PRIu64 "\n",
623 bw_str, min, max, p_of_agg, mean, dev,
624 (&ts_lcl->bw_stat[ddir])->samples);
625 }
626 if (calc_lat(&ts_lcl->iops_stat[ddir], &min, &max, &mean, &dev)) {
627 log_buf(out, " iops : min=%5llu, max=%5llu, "
628 "avg=%5.02f, stdev=%5.02f, samples=%" PRIu64 "\n",
629 min, max, mean, dev, (&ts_lcl->iops_stat[ddir])->samples);
630 }
631
632 free(ts_lcl);
633}
634
756867bd 635static void show_ddir_status(struct group_run_stats *rs, struct thread_stat *ts,
a666cab8 636 int ddir, struct buf_output *out)
3c39a379 637{
d6bb626e
VF
638 unsigned long runt;
639 unsigned long long min, max, bw, iops;
3c39a379 640 double mean, dev;
96563db9 641 char *io_p, *bw_p, *bw_p_alt, *iops_p, *post_st = NULL;
90fef2d1 642 int i2p;
3c39a379 643
b2b3eefe
JA
644 if (ddir_sync(ddir)) {
645 if (calc_lat(&ts->sync_stat, &min, &max, &mean, &dev)) {
646 log_buf(out, " %s:\n", "fsync/fdatasync/sync_file_range");
425d3e0e 647 display_lat(io_ddir_name(ddir), min, max, mean, dev, out);
b2b3eefe
JA
648 show_clat_percentiles(ts->io_u_sync_plat,
649 ts->sync_stat.samples,
650 ts->percentile_list,
651 ts->percentile_precision,
425d3e0e 652 io_ddir_name(ddir), out);
b2b3eefe
JA
653 }
654 return;
655 }
656
ff58fced
JA
657 assert(ddir_rw(ddir));
658
756867bd 659 if (!ts->runtime[ddir])
3c39a379
JA
660 return;
661
90fef2d1 662 i2p = is_power_of_2(rs->kb_base);
8879fd15
JA
663 runt = ts->runtime[ddir];
664
665 bw = (1000 * ts->io_bytes[ddir]) / runt;
e883cb35
JF
666 io_p = num2str(ts->io_bytes[ddir], ts->sig_figs, 1, i2p, N2S_BYTE);
667 bw_p = num2str(bw, ts->sig_figs, 1, i2p, ts->unit_base);
668 bw_p_alt = num2str(bw, ts->sig_figs, 1, !i2p, ts->unit_base);
8879fd15 669
0aacc50c 670 iops = (1000 * (uint64_t)ts->total_io_u[ddir]) / runt;
e883cb35 671 iops_p = num2str(iops, ts->sig_figs, 1, 0, N2S_NONE);
fd5d733f 672 if (ddir == DDIR_WRITE)
96563db9
JA
673 post_st = zbd_write_status(ts);
674 else if (ddir == DDIR_READ && ts->cachehit && ts->cachemiss) {
675 uint64_t total;
676 double hit;
677
678 total = ts->cachehit + ts->cachemiss;
679 hit = (double) ts->cachehit / (double) total;
680 hit *= 100.0;
681 if (asprintf(&post_st, "; Cachehit=%0.2f%%", hit) < 0)
682 post_st = NULL;
683 }
dbe1125e 684
fd5d733f 685 log_buf(out, " %s: IOPS=%s, BW=%s (%s)(%s/%llumsec)%s\n",
5cb8a8cd 686 (ts->unified_rw_rep == UNIFIED_MIXED) ? "mixed" : io_ddir_name(ddir),
d694a6a7 687 iops_p, bw_p, bw_p_alt, io_p,
fd5d733f 688 (unsigned long long) ts->runtime[ddir],
96563db9 689 post_st ? : "");
dbe1125e 690
96563db9 691 free(post_st);
dbe1125e
JA
692 free(io_p);
693 free(bw_p);
d694a6a7 694 free(bw_p_alt);
b3605062 695 free(iops_p);
3c39a379 696
b29ad562 697 if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev))
a666cab8 698 display_lat("slat", min, max, mean, dev, out);
b29ad562 699 if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev))
a666cab8 700 display_lat("clat", min, max, mean, dev, out);
b29ad562 701 if (calc_lat(&ts->lat_stat[ddir], &min, &max, &mean, &dev))
a666cab8 702 display_lat(" lat", min, max, mean, dev, out);
2e86a90c
VF
703 if (calc_lat(&ts->clat_high_prio_stat[ddir], &min, &max, &mean, &dev)) {
704 display_lat(ts->lat_percentiles ? "high prio_lat" : "high prio_clat",
38ec5c51 705 min, max, mean, dev, out);
2e86a90c
VF
706 if (calc_lat(&ts->clat_low_prio_stat[ddir], &min, &max, &mean, &dev))
707 display_lat(ts->lat_percentiles ? "low prio_lat" : "low prio_clat",
708 min, max, mean, dev, out);
709 }
02af0988 710
56440e63
VF
711 if (ts->slat_percentiles && ts->slat_stat[ddir].samples > 0)
712 show_clat_percentiles(ts->io_u_plat[FIO_SLAT][ddir],
713 ts->slat_stat[ddir].samples,
714 ts->percentile_list,
715 ts->percentile_precision, "slat", out);
716 if (ts->clat_percentiles && ts->clat_stat[ddir].samples > 0)
717 show_clat_percentiles(ts->io_u_plat[FIO_CLAT][ddir],
718 ts->clat_stat[ddir].samples,
719 ts->percentile_list,
720 ts->percentile_precision, "clat", out);
721 if (ts->lat_percentiles && ts->lat_stat[ddir].samples > 0)
722 show_clat_percentiles(ts->io_u_plat[FIO_LAT][ddir],
723 ts->lat_stat[ddir].samples,
724 ts->percentile_list,
725 ts->percentile_precision, "lat", out);
726
b599759b 727 if (ts->clat_percentiles || ts->lat_percentiles) {
38ec5c51 728 const char *name = ts->lat_percentiles ? "lat" : "clat";
b2a432bf 729 char prio_name[32];
af1600c1
SW
730 uint64_t samples;
731
56440e63 732 if (ts->lat_percentiles)
af1600c1 733 samples = ts->lat_stat[ddir].samples;
56440e63
VF
734 else
735 samples = ts->clat_stat[ddir].samples;
b2a432bf
PC
736
737 /* Only print this if some high and low priority stats were collected */
738 if (ts->clat_high_prio_stat[ddir].samples > 0 &&
efd78265 739 ts->clat_low_prio_stat[ddir].samples > 0)
b2a432bf
PC
740 {
741 sprintf(prio_name, "high prio (%.2f%%) %s",
742 100. * (double) ts->clat_high_prio_stat[ddir].samples / (double) samples,
743 name);
744 show_clat_percentiles(ts->io_u_plat_high_prio[ddir],
745 ts->clat_high_prio_stat[ddir].samples,
746 ts->percentile_list,
747 ts->percentile_precision, prio_name, out);
748
749 sprintf(prio_name, "low prio (%.2f%%) %s",
efd78265 750 100. * (double) ts->clat_low_prio_stat[ddir].samples / (double) samples,
b2a432bf 751 name);
efd78265
VF
752 show_clat_percentiles(ts->io_u_plat_low_prio[ddir],
753 ts->clat_low_prio_stat[ddir].samples,
b2a432bf
PC
754 ts->percentile_list,
755 ts->percentile_precision, prio_name, out);
756 }
83349190 757 }
56440e63 758
079ad09b 759 if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
142c7f2d 760 double p_of_agg = 100.0, fkb_base = (double)rs->kb_base;
d694a6a7
RE
761 const char *bw_str;
762
763 if ((rs->unit_base == 1) && i2p)
764 bw_str = "Kibit";
765 else if (rs->unit_base == 1)
766 bw_str = "kbit";
767 else if (i2p)
768 bw_str = "KiB";
769 else
770 bw_str = "kB";
d686990a 771
5c0abd5e 772 p_of_agg = convert_agg_kbytes_percent(rs, ddir, mean);
d1707474 773
d686990a
SN
774 if (rs->unit_base == 1) {
775 min *= 8.0;
776 max *= 8.0;
777 mean *= 8.0;
778 dev *= 8.0;
779 }
3c39a379 780
142c7f2d
SN
781 if (mean > fkb_base * fkb_base) {
782 min /= fkb_base;
783 max /= fkb_base;
784 mean /= fkb_base;
785 dev /= fkb_base;
d694a6a7 786 bw_str = (rs->unit_base == 1 ? "Mibit" : "MiB");
b7017e32
JA
787 }
788
54c05828 789 log_buf(out, " bw (%5s/s): min=%5llu, max=%5llu, per=%3.2f%%, "
29eb371b 790 "avg=%5.02f, stdev=%5.02f, samples=%" PRIu64 "\n",
54c05828
AH
791 bw_str, min, max, p_of_agg, mean, dev,
792 (&ts->bw_stat[ddir])->samples);
3c39a379 793 }
188b6016 794 if (calc_lat(&ts->iops_stat[ddir], &min, &max, &mean, &dev)) {
21ba6606 795 log_buf(out, " iops : min=%5llu, max=%5llu, "
29eb371b 796 "avg=%5.02f, stdev=%5.02f, samples=%" PRIu64 "\n",
54c05828 797 min, max, mean, dev, (&ts->iops_stat[ddir])->samples);
188b6016 798 }
3c39a379
JA
799}
800
8985b491
JA
801static bool show_lat(double *io_u_lat, int nr, const char **ranges,
802 const char *msg, struct buf_output *out)
04a0feae 803{
8985b491
JA
804 bool new_line = true, shown = false;
805 int i, line = 0;
04a0feae
JA
806
807 for (i = 0; i < nr; i++) {
808 if (io_u_lat[i] <= 0.0)
809 continue;
8985b491 810 shown = true;
04a0feae 811 if (new_line) {
4539ed73 812 if (line)
a666cab8 813 log_buf(out, "\n");
4a19fcaa 814 log_buf(out, " lat (%s) : ", msg);
8985b491 815 new_line = false;
04a0feae
JA
816 line = 0;
817 }
818 if (line)
a666cab8
JA
819 log_buf(out, ", ");
820 log_buf(out, "%s%3.2f%%", ranges[i], io_u_lat[i]);
04a0feae
JA
821 line++;
822 if (line == 5)
8985b491 823 new_line = true;
04a0feae 824 }
7e1773ba
JA
825
826 if (shown)
a666cab8 827 log_buf(out, "\n");
7e1773ba 828
8985b491 829 return true;
04a0feae
JA
830}
831
d6bb626e
VF
832static void show_lat_n(double *io_u_lat_n, struct buf_output *out)
833{
834 const char *ranges[] = { "2=", "4=", "10=", "20=", "50=", "100=",
835 "250=", "500=", "750=", "1000=", };
836
837 show_lat(io_u_lat_n, FIO_IO_U_LAT_N_NR, ranges, "nsec", out);
838}
839
a666cab8 840static void show_lat_u(double *io_u_lat_u, struct buf_output *out)
04a0feae
JA
841{
842 const char *ranges[] = { "2=", "4=", "10=", "20=", "50=", "100=",
843 "250=", "500=", "750=", "1000=", };
844
a666cab8 845 show_lat(io_u_lat_u, FIO_IO_U_LAT_U_NR, ranges, "usec", out);
04a0feae
JA
846}
847
a666cab8 848static void show_lat_m(double *io_u_lat_m, struct buf_output *out)
04a0feae
JA
849{
850 const char *ranges[] = { "2=", "4=", "10=", "20=", "50=", "100=",
851 "250=", "500=", "750=", "1000=", "2000=",
852 ">=2000=", };
853
a666cab8 854 show_lat(io_u_lat_m, FIO_IO_U_LAT_M_NR, ranges, "msec", out);
04a0feae
JA
855}
856
a666cab8 857static void show_latencies(struct thread_stat *ts, struct buf_output *out)
04a0feae 858{
d6bb626e 859 double io_u_lat_n[FIO_IO_U_LAT_N_NR];
c551f65a
JA
860 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
861 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
862
d6bb626e 863 stat_calc_lat_n(ts, io_u_lat_n);
5a18988e
JA
864 stat_calc_lat_u(ts, io_u_lat_u);
865 stat_calc_lat_m(ts, io_u_lat_m);
866
d6bb626e 867 show_lat_n(io_u_lat_n, out);
a666cab8
JA
868 show_lat_u(io_u_lat_u, out);
869 show_lat_m(io_u_lat_m, out);
04a0feae
JA
870}
871
66347cfa
DE
872static int block_state_category(int block_state)
873{
874 switch (block_state) {
875 case BLOCK_STATE_UNINIT:
876 return 0;
877 case BLOCK_STATE_TRIMMED:
878 case BLOCK_STATE_WRITTEN:
879 return 1;
880 case BLOCK_STATE_WRITE_FAILURE:
881 case BLOCK_STATE_TRIM_FAILURE:
882 return 2;
883 default:
081c2dc3 884 /* Silence compile warning on some BSDs and have a return */
66347cfa 885 assert(0);
07ff5841 886 return -1;
66347cfa
DE
887 }
888}
889
890static int compare_block_infos(const void *bs1, const void *bs2)
891{
5fff9543
JF
892 uint64_t block1 = *(uint64_t *)bs1;
893 uint64_t block2 = *(uint64_t *)bs2;
66347cfa
DE
894 int state1 = BLOCK_INFO_STATE(block1);
895 int state2 = BLOCK_INFO_STATE(block2);
896 int bscat1 = block_state_category(state1);
897 int bscat2 = block_state_category(state2);
898 int cycles1 = BLOCK_INFO_TRIMS(block1);
899 int cycles2 = BLOCK_INFO_TRIMS(block2);
900
901 if (bscat1 < bscat2)
902 return -1;
903 if (bscat1 > bscat2)
904 return 1;
905
906 if (cycles1 < cycles2)
907 return -1;
908 if (cycles1 > cycles2)
909 return 1;
910
911 if (state1 < state2)
912 return -1;
913 if (state1 > state2)
914 return 1;
915
916 assert(block1 == block2);
917 return 0;
918}
919
920static int calc_block_percentiles(int nr_block_infos, uint32_t *block_infos,
921 fio_fp64_t *plist, unsigned int **percentiles,
922 unsigned int *types)
923{
924 int len = 0;
925 int i, nr_uninit;
926
927 qsort(block_infos, nr_block_infos, sizeof(uint32_t), compare_block_infos);
928
929 while (len < FIO_IO_U_LIST_MAX_LEN && plist[len].u.f != 0.0)
930 len++;
931
932 if (!len)
933 return 0;
934
935 /*
936 * Sort the percentile list. Note that it may already be sorted if
937 * we are using the default values, but since it's a short list this
938 * isn't a worry. Also note that this does not work for NaN values.
939 */
940 if (len > 1)
68e0dc1a 941 qsort(plist, len, sizeof(plist[0]), double_cmp);
66347cfa 942
66347cfa
DE
943 /* Start only after the uninit entries end */
944 for (nr_uninit = 0;
945 nr_uninit < nr_block_infos
946 && BLOCK_INFO_STATE(block_infos[nr_uninit]) == BLOCK_STATE_UNINIT;
947 nr_uninit ++)
948 ;
949
950 if (nr_uninit == nr_block_infos)
951 return 0;
952
953 *percentiles = calloc(len, sizeof(**percentiles));
954
955 for (i = 0; i < len; i++) {
956 int idx = (plist[i].u.f * (nr_block_infos - nr_uninit) / 100)
957 + nr_uninit;
958 (*percentiles)[i] = BLOCK_INFO_TRIMS(block_infos[idx]);
959 }
960
961 memset(types, 0, sizeof(*types) * BLOCK_STATE_COUNT);
962 for (i = 0; i < nr_block_infos; i++)
963 types[BLOCK_INFO_STATE(block_infos[i])]++;
964
965 return len;
966}
967
968static const char *block_state_names[] = {
969 [BLOCK_STATE_UNINIT] = "unwritten",
970 [BLOCK_STATE_TRIMMED] = "trimmed",
971 [BLOCK_STATE_WRITTEN] = "written",
972 [BLOCK_STATE_TRIM_FAILURE] = "trim failure",
973 [BLOCK_STATE_WRITE_FAILURE] = "write failure",
974};
975
976static void show_block_infos(int nr_block_infos, uint32_t *block_infos,
a666cab8 977 fio_fp64_t *plist, struct buf_output *out)
66347cfa
DE
978{
979 int len, pos, i;
980 unsigned int *percentiles = NULL;
981 unsigned int block_state_counts[BLOCK_STATE_COUNT];
982
983 len = calc_block_percentiles(nr_block_infos, block_infos, plist,
984 &percentiles, block_state_counts);
985
a666cab8 986 log_buf(out, " block lifetime percentiles :\n |");
66347cfa
DE
987 pos = 0;
988 for (i = 0; i < len; i++) {
989 uint32_t block_info = percentiles[i];
990#define LINE_LENGTH 75
991 char str[LINE_LENGTH];
992 int strln = snprintf(str, LINE_LENGTH, " %3.2fth=%u%c",
993 plist[i].u.f, block_info,
994 i == len - 1 ? '\n' : ',');
995 assert(strln < LINE_LENGTH);
996 if (pos + strln > LINE_LENGTH) {
997 pos = 0;
a666cab8 998 log_buf(out, "\n |");
66347cfa 999 }
a666cab8 1000 log_buf(out, "%s", str);
66347cfa
DE
1001 pos += strln;
1002#undef LINE_LENGTH
1003 }
1004 if (percentiles)
1005 free(percentiles);
1006
a666cab8 1007 log_buf(out, " states :");
66347cfa 1008 for (i = 0; i < BLOCK_STATE_COUNT; i++)
a666cab8 1009 log_buf(out, " %s=%u%c",
66347cfa
DE
1010 block_state_names[i], block_state_counts[i],
1011 i == BLOCK_STATE_COUNT - 1 ? '\n' : ',');
1012}
1013
d685adfb
VF
1014static void show_ss_normal(struct thread_stat *ts, struct buf_output *out)
1015{
d694a6a7 1016 char *p1, *p1alt, *p2;
d685adfb
VF
1017 unsigned long long bw_mean, iops_mean;
1018 const int i2p = is_power_of_2(ts->kb_base);
1019
0c13c969 1020 if (!ts->ss_dur)
d685adfb
VF
1021 return;
1022
bb49c8bd
VF
1023 bw_mean = steadystate_bw_mean(ts);
1024 iops_mean = steadystate_iops_mean(ts);
d685adfb 1025
e883cb35
JF
1026 p1 = num2str(bw_mean / ts->kb_base, ts->sig_figs, ts->kb_base, i2p, ts->unit_base);
1027 p1alt = num2str(bw_mean / ts->kb_base, ts->sig_figs, ts->kb_base, !i2p, ts->unit_base);
1028 p2 = num2str(iops_mean, ts->sig_figs, 1, 0, N2S_NONE);
d685adfb 1029
d694a6a7 1030 log_buf(out, " steadystate : attained=%s, bw=%s (%s), iops=%s, %s%s=%.3f%s\n",
c8caba48 1031 ts->ss_state & FIO_SS_ATTAINED ? "yes" : "no",
d694a6a7 1032 p1, p1alt, p2,
c8caba48
JA
1033 ts->ss_state & FIO_SS_IOPS ? "iops" : "bw",
1034 ts->ss_state & FIO_SS_SLOPE ? " slope": " mean dev",
bb49c8bd 1035 ts->ss_criterion.u.f,
c8caba48 1036 ts->ss_state & FIO_SS_PCT ? "%" : "");
d685adfb
VF
1037
1038 free(p1);
d694a6a7 1039 free(p1alt);
d685adfb
VF
1040 free(p2);
1041}
1042
2a2fdab1
L
1043static void show_agg_stats(struct disk_util_agg *agg, int terse,
1044 struct buf_output *out)
1045{
1046 if (!agg->slavecount)
1047 return;
1048
1049 if (!terse) {
1050 log_buf(out, ", aggrios=%llu/%llu, aggrmerge=%llu/%llu, "
1051 "aggrticks=%llu/%llu, aggrin_queue=%llu, "
1052 "aggrutil=%3.2f%%",
1053 (unsigned long long) agg->ios[0] / agg->slavecount,
1054 (unsigned long long) agg->ios[1] / agg->slavecount,
1055 (unsigned long long) agg->merges[0] / agg->slavecount,
1056 (unsigned long long) agg->merges[1] / agg->slavecount,
1057 (unsigned long long) agg->ticks[0] / agg->slavecount,
1058 (unsigned long long) agg->ticks[1] / agg->slavecount,
1059 (unsigned long long) agg->time_in_queue / agg->slavecount,
1060 agg->max_util.u.f);
1061 } else {
1062 log_buf(out, ";slaves;%llu;%llu;%llu;%llu;%llu;%llu;%llu;%3.2f%%",
1063 (unsigned long long) agg->ios[0] / agg->slavecount,
1064 (unsigned long long) agg->ios[1] / agg->slavecount,
1065 (unsigned long long) agg->merges[0] / agg->slavecount,
1066 (unsigned long long) agg->merges[1] / agg->slavecount,
1067 (unsigned long long) agg->ticks[0] / agg->slavecount,
1068 (unsigned long long) agg->ticks[1] / agg->slavecount,
1069 (unsigned long long) agg->time_in_queue / agg->slavecount,
1070 agg->max_util.u.f);
1071 }
1072}
1073
1074static void aggregate_slaves_stats(struct disk_util *masterdu)
1075{
1076 struct disk_util_agg *agg = &masterdu->agg;
1077 struct disk_util_stat *dus;
1078 struct flist_head *entry;
1079 struct disk_util *slavedu;
1080 double util;
1081
1082 flist_for_each(entry, &masterdu->slaves) {
1083 slavedu = flist_entry(entry, struct disk_util, slavelist);
1084 dus = &slavedu->dus;
1085 agg->ios[0] += dus->s.ios[0];
1086 agg->ios[1] += dus->s.ios[1];
1087 agg->merges[0] += dus->s.merges[0];
1088 agg->merges[1] += dus->s.merges[1];
1089 agg->sectors[0] += dus->s.sectors[0];
1090 agg->sectors[1] += dus->s.sectors[1];
1091 agg->ticks[0] += dus->s.ticks[0];
1092 agg->ticks[1] += dus->s.ticks[1];
1093 agg->time_in_queue += dus->s.time_in_queue;
1094 agg->slavecount++;
1095
1096 util = (double) (100 * dus->s.io_ticks / (double) slavedu->dus.s.msec);
1097 /* System utilization is the utilization of the
1098 * component with the highest utilization.
1099 */
1100 if (util > agg->max_util.u.f)
1101 agg->max_util.u.f = util;
1102
1103 }
1104
1105 if (agg->max_util.u.f > 100.0)
1106 agg->max_util.u.f = 100.0;
1107}
1108
1109void print_disk_util(struct disk_util_stat *dus, struct disk_util_agg *agg,
1110 int terse, struct buf_output *out)
1111{
1112 double util = 0;
1113
1114 if (dus->s.msec)
1115 util = (double) 100 * dus->s.io_ticks / (double) dus->s.msec;
1116 if (util > 100.0)
1117 util = 100.0;
1118
1119 if (!terse) {
1120 if (agg->slavecount)
1121 log_buf(out, " ");
1122
1123 log_buf(out, " %s: ios=%llu/%llu, merge=%llu/%llu, "
1124 "ticks=%llu/%llu, in_queue=%llu, util=%3.2f%%",
1125 dus->name,
1126 (unsigned long long) dus->s.ios[0],
1127 (unsigned long long) dus->s.ios[1],
1128 (unsigned long long) dus->s.merges[0],
1129 (unsigned long long) dus->s.merges[1],
1130 (unsigned long long) dus->s.ticks[0],
1131 (unsigned long long) dus->s.ticks[1],
1132 (unsigned long long) dus->s.time_in_queue,
1133 util);
1134 } else {
1135 log_buf(out, ";%s;%llu;%llu;%llu;%llu;%llu;%llu;%llu;%3.2f%%",
1136 dus->name,
1137 (unsigned long long) dus->s.ios[0],
1138 (unsigned long long) dus->s.ios[1],
1139 (unsigned long long) dus->s.merges[0],
1140 (unsigned long long) dus->s.merges[1],
1141 (unsigned long long) dus->s.ticks[0],
1142 (unsigned long long) dus->s.ticks[1],
1143 (unsigned long long) dus->s.time_in_queue,
1144 util);
1145 }
1146
1147 /*
1148 * If the device has slaves, aggregate the stats for
1149 * those slave devices also.
1150 */
1151 show_agg_stats(agg, terse, out);
1152
1153 if (!terse)
1154 log_buf(out, "\n");
1155}
1156
1157void json_array_add_disk_util(struct disk_util_stat *dus,
1158 struct disk_util_agg *agg, struct json_array *array)
1159{
1160 struct json_object *obj;
1161 double util = 0;
1162
1163 if (dus->s.msec)
1164 util = (double) 100 * dus->s.io_ticks / (double) dus->s.msec;
1165 if (util > 100.0)
1166 util = 100.0;
1167
1168 obj = json_create_object();
1169 json_array_add_value_object(array, obj);
1170
eb2f29b7 1171 json_object_add_value_string(obj, "name", (const char *)dus->name);
2a2fdab1
L
1172 json_object_add_value_int(obj, "read_ios", dus->s.ios[0]);
1173 json_object_add_value_int(obj, "write_ios", dus->s.ios[1]);
1174 json_object_add_value_int(obj, "read_merges", dus->s.merges[0]);
1175 json_object_add_value_int(obj, "write_merges", dus->s.merges[1]);
1176 json_object_add_value_int(obj, "read_ticks", dus->s.ticks[0]);
1177 json_object_add_value_int(obj, "write_ticks", dus->s.ticks[1]);
1178 json_object_add_value_int(obj, "in_queue", dus->s.time_in_queue);
1179 json_object_add_value_float(obj, "util", util);
1180
1181 /*
1182 * If the device has slaves, aggregate the stats for
1183 * those slave devices also.
1184 */
1185 if (!agg->slavecount)
1186 return;
1187 json_object_add_value_int(obj, "aggr_read_ios",
1188 agg->ios[0] / agg->slavecount);
1189 json_object_add_value_int(obj, "aggr_write_ios",
1190 agg->ios[1] / agg->slavecount);
1191 json_object_add_value_int(obj, "aggr_read_merges",
1192 agg->merges[0] / agg->slavecount);
1193 json_object_add_value_int(obj, "aggr_write_merge",
1194 agg->merges[1] / agg->slavecount);
1195 json_object_add_value_int(obj, "aggr_read_ticks",
1196 agg->ticks[0] / agg->slavecount);
1197 json_object_add_value_int(obj, "aggr_write_ticks",
1198 agg->ticks[1] / agg->slavecount);
1199 json_object_add_value_int(obj, "aggr_in_queue",
1200 agg->time_in_queue / agg->slavecount);
1201 json_object_add_value_float(obj, "aggr_util", agg->max_util.u.f);
1202}
1203
1204static void json_object_add_disk_utils(struct json_object *obj,
1205 struct flist_head *head)
1206{
1207 struct json_array *array = json_create_array();
1208 struct flist_head *entry;
1209 struct disk_util *du;
1210
1211 json_object_add_value_array(obj, "disk_util", array);
1212
1213 flist_for_each(entry, head) {
1214 du = flist_entry(entry, struct disk_util, list);
1215
1216 aggregate_slaves_stats(du);
1217 json_array_add_disk_util(&du->dus, &du->agg, array);
1218 }
1219}
1220
1221void show_disk_util(int terse, struct json_object *parent,
1222 struct buf_output *out)
1223{
1224 struct flist_head *entry;
1225 struct disk_util *du;
1226 bool do_json;
1227
1228 if (!is_running_backend())
1229 return;
1230
1231 if (flist_empty(&disk_list)) {
1232 return;
1233 }
1234
1235 if ((output_format & FIO_OUTPUT_JSON) && parent)
1236 do_json = true;
1237 else
1238 do_json = false;
1239
1240 if (!terse && !do_json)
1241 log_buf(out, "\nDisk stats (read/write):\n");
1242
1243 if (do_json)
1244 json_object_add_disk_utils(parent, &disk_list);
1245 else if (output_format & ~(FIO_OUTPUT_JSON | FIO_OUTPUT_JSON_PLUS)) {
1246 flist_for_each(entry, &disk_list) {
1247 du = flist_entry(entry, struct disk_util, list);
1248
1249 aggregate_slaves_stats(du);
1250 print_disk_util(&du->dus, &du->agg, terse, out);
1251 }
1252 }
1253}
1254
10aa136b 1255static void show_thread_status_normal(struct thread_stat *ts,
a666cab8
JA
1256 struct group_run_stats *rs,
1257 struct buf_output *out)
3c39a379
JA
1258{
1259 double usr_cpu, sys_cpu;
69008999 1260 unsigned long runtime;
71619dc2 1261 double io_u_dist[FIO_IO_U_MAP_NR];
57a64324 1262 time_t time_p;
35326842 1263 char time_buf[32];
3c39a379 1264
9966af7b 1265 if (!ddir_rw_sum(ts->io_bytes) && !ddir_rw_sum(ts->total_io_u))
3c39a379 1266 return;
7a4e480d 1267
33477b4b 1268 memset(time_buf, 0, sizeof(time_buf));
3c39a379 1269
57a64324 1270 time(&time_p);
45054cbe 1271 os_ctime_r((const time_t *) &time_p, time_buf, sizeof(time_buf));
57a64324 1272
5ec10eaa 1273 if (!ts->error) {
a666cab8 1274 log_buf(out, "%s: (groupid=%d, jobs=%d): err=%2d: pid=%d: %s",
5ec10eaa 1275 ts->name, ts->groupid, ts->members,
57a64324 1276 ts->error, (int) ts->pid, time_buf);
5ec10eaa 1277 } else {
a666cab8 1278 log_buf(out, "%s: (groupid=%d, jobs=%d): err=%2d (%s): pid=%d: %s",
5ec10eaa 1279 ts->name, ts->groupid, ts->members,
57a64324
JA
1280 ts->error, ts->verror, (int) ts->pid,
1281 time_buf);
5ec10eaa 1282 }
3c39a379 1283
259e47de 1284 if (strlen(ts->description))
a666cab8 1285 log_buf(out, " Description : [%s]\n", ts->description);
7bdce1bd 1286
dde39f8c
AD
1287 for_each_rw_ddir(ddir) {
1288 if (ts->io_bytes[ddir])
1289 show_ddir_status(rs, ts, ddir, out);
1290 }
3c39a379 1291
5cb8a8cd
BP
1292 if (ts->unified_rw_rep == UNIFIED_BOTH)
1293 show_mixed_ddir_status(rs, ts, out);
1294
a666cab8 1295 show_latencies(ts, out);
7e1773ba 1296
b2b3eefe
JA
1297 if (ts->sync_stat.samples)
1298 show_ddir_status(rs, ts, DDIR_SYNC, out);
1299
756867bd 1300 runtime = ts->total_run_time;
69008999 1301 if (runtime) {
1e97cce9 1302 double runt = (double) runtime;
3c39a379 1303
756867bd
JA
1304 usr_cpu = (double) ts->usr_time * 100 / runt;
1305 sys_cpu = (double) ts->sys_time * 100 / runt;
3c39a379
JA
1306 } else {
1307 usr_cpu = 0;
1308 sys_cpu = 0;
1309 }
1310
a666cab8 1311 log_buf(out, " cpu : usr=%3.2f%%, sys=%3.2f%%, ctx=%llu,"
4e0a8fa2
JA
1312 " majf=%llu, minf=%llu\n", usr_cpu, sys_cpu,
1313 (unsigned long long) ts->ctx,
1314 (unsigned long long) ts->majf,
1315 (unsigned long long) ts->minf);
71619dc2 1316
d79db122 1317 stat_calc_dist(ts->io_u_map, ddir_rw_sum(ts->total_io_u), io_u_dist);
a666cab8 1318 log_buf(out, " IO depths : 1=%3.1f%%, 2=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%,"
5ec10eaa
JA
1319 " 16=%3.1f%%, 32=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
1320 io_u_dist[1], io_u_dist[2],
1321 io_u_dist[3], io_u_dist[4],
1322 io_u_dist[5], io_u_dist[6]);
838bc709
JA
1323
1324 stat_calc_dist(ts->io_u_submit, ts->total_submit, io_u_dist);
a666cab8 1325 log_buf(out, " submit : 0=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%, 16=%3.1f%%,"
838bc709
JA
1326 " 32=%3.1f%%, 64=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
1327 io_u_dist[1], io_u_dist[2],
1328 io_u_dist[3], io_u_dist[4],
1329 io_u_dist[5], io_u_dist[6]);
1330 stat_calc_dist(ts->io_u_complete, ts->total_complete, io_u_dist);
a666cab8 1331 log_buf(out, " complete : 0=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%, 16=%3.1f%%,"
838bc709
JA
1332 " 32=%3.1f%%, 64=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
1333 io_u_dist[1], io_u_dist[2],
1334 io_u_dist[3], io_u_dist[4],
1335 io_u_dist[5], io_u_dist[6]);
7f3ecee2
JA
1336 log_buf(out, " issued rwts: total=%llu,%llu,%llu,%llu"
1337 " short=%llu,%llu,%llu,0"
1338 " dropped=%llu,%llu,%llu,0\n",
4e0a8fa2
JA
1339 (unsigned long long) ts->total_io_u[0],
1340 (unsigned long long) ts->total_io_u[1],
1341 (unsigned long long) ts->total_io_u[2],
7f3ecee2 1342 (unsigned long long) ts->total_io_u[3],
4e0a8fa2
JA
1343 (unsigned long long) ts->short_io_u[0],
1344 (unsigned long long) ts->short_io_u[1],
3bcb9d94
JA
1345 (unsigned long long) ts->short_io_u[2],
1346 (unsigned long long) ts->drop_io_u[0],
1347 (unsigned long long) ts->drop_io_u[1],
1348 (unsigned long long) ts->drop_io_u[2]);
f2bba182 1349 if (ts->continue_on_error) {
a666cab8 1350 log_buf(out, " errors : total=%llu, first_error=%d/<%s>\n",
4e0a8fa2 1351 (unsigned long long)ts->total_err_count,
1ec99eea
JA
1352 ts->first_error,
1353 strerror(ts->first_error));
f2bba182 1354 }
3e260a46 1355 if (ts->latency_depth) {
a666cab8 1356 log_buf(out, " latency : target=%llu, window=%llu, percentile=%.2f%%, depth=%u\n",
3e260a46
JA
1357 (unsigned long long)ts->latency_target,
1358 (unsigned long long)ts->latency_window,
1359 ts->latency_percentile.u.f,
1360 ts->latency_depth);
1361 }
66347cfa
DE
1362
1363 if (ts->nr_block_infos)
1364 show_block_infos(ts->nr_block_infos, ts->block_infos,
a666cab8 1365 ts->percentile_list, out);
d685adfb 1366
bb49c8bd 1367 if (ts->ss_dur)
d685adfb 1368 show_ss_normal(ts, out);
3c39a379
JA
1369}
1370
756867bd 1371static void show_ddir_status_terse(struct thread_stat *ts,
a666cab8 1372 struct group_run_stats *rs, int ddir,
a2c95580 1373 int ver, struct buf_output *out)
c6ae0a5b 1374{
d6bb626e
VF
1375 unsigned long long min, max, minv, maxv, bw, iops;
1376 unsigned long long *ovals = NULL;
c6ae0a5b 1377 double mean, dev;
d6bb626e 1378 unsigned int len;
a2c95580 1379 int i, bw_stat;
c6ae0a5b 1380
ff58fced
JA
1381 assert(ddir_rw(ddir));
1382
312b4af2
JA
1383 iops = bw = 0;
1384 if (ts->runtime[ddir]) {
1385 uint64_t runt = ts->runtime[ddir];
1386
420b104a 1387 bw = ((1000 * ts->io_bytes[ddir]) / runt) / 1024; /* KiB/s */
312b4af2
JA
1388 iops = (1000 * (uint64_t) ts->total_io_u[ddir]) / runt;
1389 }
c6ae0a5b 1390
a666cab8 1391 log_buf(out, ";%llu;%llu;%llu;%llu",
4e0a8fa2
JA
1392 (unsigned long long) ts->io_bytes[ddir] >> 10, bw, iops,
1393 (unsigned long long) ts->runtime[ddir]);
c6ae0a5b 1394
079ad09b 1395 if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev))
247823cc 1396 log_buf(out, ";%llu;%llu;%f;%f", min/1000, max/1000, mean/1000, dev/1000);
c6ae0a5b 1397 else
d6bb626e 1398 log_buf(out, ";%llu;%llu;%f;%f", 0ULL, 0ULL, 0.0, 0.0);
c6ae0a5b 1399
079ad09b 1400 if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev))
247823cc 1401 log_buf(out, ";%llu;%llu;%f;%f", min/1000, max/1000, mean/1000, dev/1000);
c6ae0a5b 1402 else
d6bb626e 1403 log_buf(out, ";%llu;%llu;%f;%f", 0ULL, 0ULL, 0.0, 0.0);
c6ae0a5b 1404
56440e63
VF
1405 if (ts->lat_percentiles)
1406 len = calc_clat_percentiles(ts->io_u_plat[FIO_LAT][ddir],
1407 ts->lat_stat[ddir].samples,
1408 ts->percentile_list, &ovals, &maxv,
1409 &minv);
1410 else if (ts->clat_percentiles)
df8781b6 1411 len = calc_clat_percentiles(ts->io_u_plat[FIO_CLAT][ddir],
1db92cb6
JA
1412 ts->clat_stat[ddir].samples,
1413 ts->percentile_list, &ovals, &maxv,
1414 &minv);
56440e63 1415 else
1db92cb6 1416 len = 0;
5cb8a8cd 1417
1db92cb6
JA
1418 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
1419 if (i >= len) {
a666cab8 1420 log_buf(out, ";0%%=0");
1db92cb6
JA
1421 continue;
1422 }
247823cc 1423 log_buf(out, ";%f%%=%llu", ts->percentile_list[i].u.f, ovals[i]/1000);
1db92cb6 1424 }
2341a37a
K
1425
1426 if (calc_lat(&ts->lat_stat[ddir], &min, &max, &mean, &dev))
247823cc 1427 log_buf(out, ";%llu;%llu;%f;%f", min/1000, max/1000, mean/1000, dev/1000);
2341a37a 1428 else
d6bb626e 1429 log_buf(out, ";%llu;%llu;%f;%f", 0ULL, 0ULL, 0.0, 0.0);
2341a37a 1430
283feb79 1431 free(ovals);
1db92cb6 1432
a2c95580
AH
1433 bw_stat = calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev);
1434 if (bw_stat) {
19d3e967
JA
1435 double p_of_agg = 100.0;
1436
1437 if (rs->agg[ddir]) {
d1707474 1438 p_of_agg = mean * 100 / (double) (rs->agg[ddir] / 1024);
19d3e967
JA
1439 if (p_of_agg > 100.0)
1440 p_of_agg = 100.0;
1441 }
c6ae0a5b 1442
d6bb626e 1443 log_buf(out, ";%llu;%llu;%f%%;%f;%f", min, max, p_of_agg, mean, dev);
c6ae0a5b 1444 } else
d6bb626e 1445 log_buf(out, ";%llu;%llu;%f%%;%f;%f", 0ULL, 0ULL, 0.0, 0.0, 0.0);
a2c95580
AH
1446
1447 if (ver == 5) {
1448 if (bw_stat)
29eb371b 1449 log_buf(out, ";%" PRIu64, (&ts->bw_stat[ddir])->samples);
a2c95580
AH
1450 else
1451 log_buf(out, ";%lu", 0UL);
1452
1453 if (calc_lat(&ts->iops_stat[ddir], &min, &max, &mean, &dev))
29eb371b 1454 log_buf(out, ";%llu;%llu;%f;%f;%" PRIu64, min, max,
a2c95580
AH
1455 mean, dev, (&ts->iops_stat[ddir])->samples);
1456 else
1457 log_buf(out, ";%llu;%llu;%f;%f;%lu", 0ULL, 0ULL, 0.0, 0.0, 0UL);
1458 }
c6ae0a5b
JA
1459}
1460
5cb8a8cd
BP
1461static void show_mixed_ddir_status_terse(struct thread_stat *ts,
1462 struct group_run_stats *rs,
1463 int ver, struct buf_output *out)
1464{
1465 struct thread_stat *ts_lcl;
1466 int i;
1467
1468 /* Handle aggregation of Reads (ddir = 0), Writes (ddir = 1), and Trims (ddir = 2) */
1469 ts_lcl = malloc(sizeof(struct thread_stat));
1470 memset((void *)ts_lcl, 0, sizeof(struct thread_stat));
1471 ts_lcl->unified_rw_rep = UNIFIED_MIXED; /* calculate mixed stats */
1472 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1473 ts_lcl->clat_stat[i].min_val = ULONG_MAX;
1474 ts_lcl->slat_stat[i].min_val = ULONG_MAX;
1475 ts_lcl->lat_stat[i].min_val = ULONG_MAX;
1476 ts_lcl->bw_stat[i].min_val = ULONG_MAX;
1477 ts_lcl->iops_stat[i].min_val = ULONG_MAX;
1478 ts_lcl->clat_high_prio_stat[i].min_val = ULONG_MAX;
1479 ts_lcl->clat_low_prio_stat[i].min_val = ULONG_MAX;
1480 }
1481 ts_lcl->sync_stat.min_val = ULONG_MAX;
1482 ts_lcl->lat_percentiles = ts->lat_percentiles;
1483 ts_lcl->clat_percentiles = ts->clat_percentiles;
1484 ts_lcl->slat_percentiles = ts->slat_percentiles;
1485 ts_lcl->percentile_precision = ts->percentile_precision;
1486 memcpy(ts_lcl->percentile_list, ts->percentile_list, sizeof(ts->percentile_list));
1487
1488 sum_thread_stats(ts_lcl, ts, 1);
1489
1490 /* add the aggregated stats to json parent */
1491 show_ddir_status_terse(ts_lcl, rs, DDIR_READ, ver, out);
1492 free(ts_lcl);
1493}
1494
56440e63
VF
1495static struct json_object *add_ddir_lat_json(struct thread_stat *ts, uint32_t percentiles,
1496 struct io_stat *lat_stat, uint64_t *io_u_plat)
1497{
1498 char buf[120];
1499 double mean, dev;
1500 unsigned int i, len;
1501 struct json_object *lat_object, *percentile_object, *clat_bins_object;
1502 unsigned long long min, max, maxv, minv, *ovals = NULL;
1503
1504 if (!calc_lat(lat_stat, &min, &max, &mean, &dev)) {
1505 min = max = 0;
1506 mean = dev = 0.0;
1507 }
1508 lat_object = json_create_object();
1509 json_object_add_value_int(lat_object, "min", min);
1510 json_object_add_value_int(lat_object, "max", max);
1511 json_object_add_value_float(lat_object, "mean", mean);
1512 json_object_add_value_float(lat_object, "stddev", dev);
1513 json_object_add_value_int(lat_object, "N", lat_stat->samples);
1514
1515 if (percentiles && lat_stat->samples) {
1516 len = calc_clat_percentiles(io_u_plat, lat_stat->samples,
1517 ts->percentile_list, &ovals, &maxv, &minv);
1518
1519 if (len > FIO_IO_U_LIST_MAX_LEN)
1520 len = FIO_IO_U_LIST_MAX_LEN;
1521
1522 percentile_object = json_create_object();
1523 json_object_add_value_object(lat_object, "percentile", percentile_object);
1524 for (i = 0; i < len; i++) {
1525 snprintf(buf, sizeof(buf), "%f", ts->percentile_list[i].u.f);
1526 json_object_add_value_int(percentile_object, buf, ovals[i]);
1527 }
1528 free(ovals);
1529
1530 if (output_format & FIO_OUTPUT_JSON_PLUS) {
1531 clat_bins_object = json_create_object();
1532 json_object_add_value_object(lat_object, "bins", clat_bins_object);
1533
1534 for(i = 0; i < FIO_IO_U_PLAT_NR; i++)
1535 if (io_u_plat[i]) {
1536 snprintf(buf, sizeof(buf), "%llu", plat_idx_to_val(i));
1537 json_object_add_value_int(clat_bins_object, buf, io_u_plat[i]);
1538 }
1539 }
1540 }
1541
1542 return lat_object;
1543}
1544
cc372b17
SL
1545static void add_ddir_status_json(struct thread_stat *ts,
1546 struct group_run_stats *rs, int ddir, struct json_object *parent)
1547{
56440e63 1548 unsigned long long min, max;
aedd021d 1549 unsigned long long bw_bytes, bw;
9f68fe3a 1550 double mean, dev, iops;
56440e63 1551 struct json_object *dir_object, *tmp_object;
cc372b17
SL
1552 double p_of_agg = 100.0;
1553
b2b3eefe 1554 assert(ddir_rw(ddir) || ddir_sync(ddir));
cc372b17 1555
5cb8a8cd 1556 if ((ts->unified_rw_rep == UNIFIED_MIXED) && ddir != DDIR_READ)
771e58be
JA
1557 return;
1558
cc372b17 1559 dir_object = json_create_object();
771e58be 1560 json_object_add_value_object(parent,
5cb8a8cd 1561 (ts->unified_rw_rep == UNIFIED_MIXED) ? "mixed" : io_ddir_name(ddir), dir_object);
cc372b17 1562
b2b3eefe
JA
1563 if (ddir_rw(ddir)) {
1564 bw_bytes = 0;
1565 bw = 0;
1566 iops = 0.0;
1567 if (ts->runtime[ddir]) {
1568 uint64_t runt = ts->runtime[ddir];
cc372b17 1569
b2b3eefe
JA
1570 bw_bytes = ((1000 * ts->io_bytes[ddir]) / runt); /* Bytes/s */
1571 bw = bw_bytes / 1024; /* KiB/s */
1572 iops = (1000.0 * (uint64_t) ts->total_io_u[ddir]) / runt;
1573 }
cc372b17 1574
b2b3eefe
JA
1575 json_object_add_value_int(dir_object, "io_bytes", ts->io_bytes[ddir]);
1576 json_object_add_value_int(dir_object, "io_kbytes", ts->io_bytes[ddir] >> 10);
1577 json_object_add_value_int(dir_object, "bw_bytes", bw_bytes);
1578 json_object_add_value_int(dir_object, "bw", bw);
1579 json_object_add_value_float(dir_object, "iops", iops);
1580 json_object_add_value_int(dir_object, "runtime", ts->runtime[ddir]);
1581 json_object_add_value_int(dir_object, "total_ios", ts->total_io_u[ddir]);
1582 json_object_add_value_int(dir_object, "short_ios", ts->short_io_u[ddir]);
1583 json_object_add_value_int(dir_object, "drop_ios", ts->drop_io_u[ddir]);
1584
56440e63
VF
1585 tmp_object = add_ddir_lat_json(ts, ts->slat_percentiles,
1586 &ts->slat_stat[ddir], ts->io_u_plat[FIO_SLAT][ddir]);
b2b3eefe 1587 json_object_add_value_object(dir_object, "slat_ns", tmp_object);
56440e63
VF
1588
1589 tmp_object = add_ddir_lat_json(ts, ts->clat_percentiles,
1590 &ts->clat_stat[ddir], ts->io_u_plat[FIO_CLAT][ddir]);
b2b3eefe 1591 json_object_add_value_object(dir_object, "clat_ns", tmp_object);
cc372b17 1592
56440e63
VF
1593 tmp_object = add_ddir_lat_json(ts, ts->lat_percentiles,
1594 &ts->lat_stat[ddir], ts->io_u_plat[FIO_LAT][ddir]);
b2b3eefe 1595 json_object_add_value_object(dir_object, "lat_ns", tmp_object);
56440e63 1596 } else {
f5ec8123 1597 json_object_add_value_int(dir_object, "total_ios", ts->total_io_u[DDIR_SYNC]);
56440e63
VF
1598 tmp_object = add_ddir_lat_json(ts, ts->lat_percentiles | ts->clat_percentiles,
1599 &ts->sync_stat, ts->io_u_sync_plat);
1600 json_object_add_value_object(dir_object, "lat_ns", tmp_object);
513e37ee
VF
1601 }
1602
56440e63
VF
1603 if (!ddir_rw(ddir))
1604 return;
b2a432bf
PC
1605
1606 /* Only print PRIO latencies if some high priority samples were gathered */
1607 if (ts->clat_high_prio_stat[ddir].samples > 0) {
56440e63 1608 const char *high, *low;
b2a432bf 1609
56440e63 1610 if (ts->lat_percentiles) {
efd78265 1611 high = "lat_high_prio";
56440e63
VF
1612 low = "lat_low_prio";
1613 } else {
efd78265 1614 high = "clat_high_prio";
56440e63 1615 low = "clat_low_prio";
b2a432bf 1616 }
b2a432bf 1617
56440e63
VF
1618 tmp_object = add_ddir_lat_json(ts, ts->clat_percentiles | ts->lat_percentiles,
1619 &ts->clat_high_prio_stat[ddir], ts->io_u_plat_high_prio[ddir]);
1620 json_object_add_value_object(dir_object, high, tmp_object);
b2b3eefe 1621
56440e63 1622 tmp_object = add_ddir_lat_json(ts, ts->clat_percentiles | ts->lat_percentiles,
efd78265 1623 &ts->clat_low_prio_stat[ddir], ts->io_u_plat_low_prio[ddir]);
56440e63 1624 json_object_add_value_object(dir_object, low, tmp_object);
cc372b17 1625 }
24cab44e 1626
b9e1b491 1627 if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
5c0abd5e 1628 p_of_agg = convert_agg_kbytes_percent(rs, ddir, mean);
cc372b17
SL
1629 } else {
1630 min = max = 0;
1631 p_of_agg = mean = dev = 0.0;
1632 }
56440e63 1633
cc372b17
SL
1634 json_object_add_value_int(dir_object, "bw_min", min);
1635 json_object_add_value_int(dir_object, "bw_max", max);
a806bf2e 1636 json_object_add_value_float(dir_object, "bw_agg", p_of_agg);
cc372b17
SL
1637 json_object_add_value_float(dir_object, "bw_mean", mean);
1638 json_object_add_value_float(dir_object, "bw_dev", dev);
54c05828
AH
1639 json_object_add_value_int(dir_object, "bw_samples",
1640 (&ts->bw_stat[ddir])->samples);
188b6016
AH
1641
1642 if (!calc_lat(&ts->iops_stat[ddir], &min, &max, &mean, &dev)) {
1643 min = max = 0;
1644 mean = dev = 0.0;
1645 }
1646 json_object_add_value_int(dir_object, "iops_min", min);
1647 json_object_add_value_int(dir_object, "iops_max", max);
1648 json_object_add_value_float(dir_object, "iops_mean", mean);
1649 json_object_add_value_float(dir_object, "iops_stddev", dev);
54c05828
AH
1650 json_object_add_value_int(dir_object, "iops_samples",
1651 (&ts->iops_stat[ddir])->samples);
96563db9
JA
1652
1653 if (ts->cachehit + ts->cachemiss) {
1654 uint64_t total;
1655 double hit;
1656
1657 total = ts->cachehit + ts->cachemiss;
1658 hit = (double) ts->cachehit / (double) total;
1659 hit *= 100.0;
1660 json_object_add_value_float(dir_object, "cachehit", hit);
1661 }
cc372b17
SL
1662}
1663
5cb8a8cd
BP
1664static void add_mixed_ddir_status_json(struct thread_stat *ts,
1665 struct group_run_stats *rs, struct json_object *parent)
1666{
1667 struct thread_stat *ts_lcl;
1668 int i;
1669
1670 /* Handle aggregation of Reads (ddir = 0), Writes (ddir = 1), and Trims (ddir = 2) */
1671 ts_lcl = malloc(sizeof(struct thread_stat));
1672 memset((void *)ts_lcl, 0, sizeof(struct thread_stat));
1673 ts_lcl->unified_rw_rep = UNIFIED_MIXED; /* calculate mixed stats */
1674 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1675 ts_lcl->clat_stat[i].min_val = ULONG_MAX;
1676 ts_lcl->slat_stat[i].min_val = ULONG_MAX;
1677 ts_lcl->lat_stat[i].min_val = ULONG_MAX;
1678 ts_lcl->bw_stat[i].min_val = ULONG_MAX;
1679 ts_lcl->iops_stat[i].min_val = ULONG_MAX;
1680 ts_lcl->clat_high_prio_stat[i].min_val = ULONG_MAX;
1681 ts_lcl->clat_low_prio_stat[i].min_val = ULONG_MAX;
1682 }
1683 ts_lcl->sync_stat.min_val = ULONG_MAX;
1684 ts_lcl->lat_percentiles = ts->lat_percentiles;
1685 ts_lcl->clat_percentiles = ts->clat_percentiles;
1686 ts_lcl->slat_percentiles = ts->slat_percentiles;
1687 ts_lcl->percentile_precision = ts->percentile_precision;
1688 memcpy(ts_lcl->percentile_list, ts->percentile_list, sizeof(ts->percentile_list));
1689
1690 sum_thread_stats(ts_lcl, ts, 1);
1691
1692 /* add the aggregated stats to json parent */
1693 add_ddir_status_json(ts_lcl, rs, DDIR_READ, parent);
1694 free(ts_lcl);
1695}
1696
bef2112b
AH
1697static void show_thread_status_terse_all(struct thread_stat *ts,
1698 struct group_run_stats *rs, int ver,
1699 struct buf_output *out)
4d658652
JA
1700{
1701 double io_u_dist[FIO_IO_U_MAP_NR];
1702 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
1703 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
1704 double usr_cpu, sys_cpu;
1705 int i;
1706
1707 /* General Info */
bef2112b
AH
1708 if (ver == 2)
1709 log_buf(out, "2;%s;%d;%d", ts->name, ts->groupid, ts->error);
1710 else
1711 log_buf(out, "%d;%s;%s;%d;%d", ver, fio_version_string,
1712 ts->name, ts->groupid, ts->error);
c6ae0a5b 1713
5cb8a8cd 1714 /* Log Read Status, or mixed if unified_rw_rep = 1 */
a2c95580 1715 show_ddir_status_terse(ts, rs, DDIR_READ, ver, out);
5cb8a8cd
BP
1716 if (ts->unified_rw_rep != UNIFIED_MIXED) {
1717 /* Log Write Status */
1718 show_ddir_status_terse(ts, rs, DDIR_WRITE, ver, out);
1719 /* Log Trim Status */
1720 if (ver == 2 || ver == 4 || ver == 5)
1721 show_ddir_status_terse(ts, rs, DDIR_TRIM, ver, out);
1722 }
1723 if (ts->unified_rw_rep == UNIFIED_BOTH)
1724 show_mixed_ddir_status_terse(ts, rs, ver, out);
562c2d2f 1725 /* CPU Usage */
756867bd
JA
1726 if (ts->total_run_time) {
1727 double runt = (double) ts->total_run_time;
c6ae0a5b 1728
756867bd
JA
1729 usr_cpu = (double) ts->usr_time * 100 / runt;
1730 sys_cpu = (double) ts->sys_time * 100 / runt;
c6ae0a5b
JA
1731 } else {
1732 usr_cpu = 0;
1733 sys_cpu = 0;
1734 }
1735
a666cab8 1736 log_buf(out, ";%f%%;%f%%;%llu;%llu;%llu", usr_cpu, sys_cpu,
4e0a8fa2
JA
1737 (unsigned long long) ts->ctx,
1738 (unsigned long long) ts->majf,
1739 (unsigned long long) ts->minf);
2270890c 1740
562c2d2f 1741 /* Calc % distribution of IO depths, usecond, msecond latency */
d79db122 1742 stat_calc_dist(ts->io_u_map, ddir_rw_sum(ts->total_io_u), io_u_dist);
247823cc 1743 stat_calc_lat_nu(ts, io_u_lat_u);
04a0feae 1744 stat_calc_lat_m(ts, io_u_lat_m);
2270890c 1745
562c2d2f 1746 /* Only show fixed 7 I/O depth levels*/
a666cab8 1747 log_buf(out, ";%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%",
5ec10eaa
JA
1748 io_u_dist[0], io_u_dist[1], io_u_dist[2], io_u_dist[3],
1749 io_u_dist[4], io_u_dist[5], io_u_dist[6]);
2270890c 1750
562c2d2f 1751 /* Microsecond latency */
04a0feae 1752 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
a666cab8 1753 log_buf(out, ";%3.2f%%", io_u_lat_u[i]);
562c2d2f 1754 /* Millisecond latency */
04a0feae 1755 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
a666cab8 1756 log_buf(out, ";%3.2f%%", io_u_lat_m[i]);
f2f788dd
JA
1757
1758 /* disk util stats, if any */
24a97130 1759 if (ver >= 3 && is_running_backend())
bef2112b 1760 show_disk_util(1, NULL, out);
f2f788dd 1761
562c2d2f 1762 /* Additional output if continue_on_error set - default off*/
f2bba182 1763 if (ts->continue_on_error)
a666cab8 1764 log_buf(out, ";%llu;%d", (unsigned long long) ts->total_err_count, ts->first_error);
2270890c 1765
562c2d2f 1766 /* Additional output if description is set */
d5495f0b
VF
1767 if (strlen(ts->description)) {
1768 if (ver == 2)
1769 log_buf(out, "\n");
a666cab8 1770 log_buf(out, ";%s", ts->description);
d5495f0b 1771 }
946e4276 1772
a666cab8 1773 log_buf(out, "\n");
756867bd
JA
1774}
1775
a89ba4b1 1776static void json_add_job_opts(struct json_object *root, const char *name,
0cf542af 1777 struct flist_head *opt_list)
66e19a38
JA
1778{
1779 struct json_object *dir_object;
1780 struct flist_head *entry;
1781 struct print_option *p;
1782
1783 if (flist_empty(opt_list))
1784 return;
1785
1786 dir_object = json_create_object();
1787 json_object_add_value_object(root, name, dir_object);
1788
1789 flist_for_each(entry, opt_list) {
66e19a38 1790 p = flist_entry(entry, struct print_option, list);
c42b48fe 1791 json_object_add_value_string(dir_object, p->name, p->value);
66e19a38
JA
1792 }
1793}
1794
cc372b17 1795static struct json_object *show_thread_status_json(struct thread_stat *ts,
66e19a38
JA
1796 struct group_run_stats *rs,
1797 struct flist_head *opt_list)
cc372b17
SL
1798{
1799 struct json_object *root, *tmp;
b01af66b 1800 struct jobs_eta *je;
cc372b17 1801 double io_u_dist[FIO_IO_U_MAP_NR];
d6bb626e 1802 double io_u_lat_n[FIO_IO_U_LAT_N_NR];
cc372b17
SL
1803 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
1804 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
1805 double usr_cpu, sys_cpu;
1806 int i;
b01af66b
CJ
1807 size_t size;
1808
cc372b17
SL
1809 root = json_create_object();
1810 json_object_add_value_string(root, "jobname", ts->name);
1811 json_object_add_value_int(root, "groupid", ts->groupid);
1812 json_object_add_value_int(root, "error", ts->error);
1813
b01af66b 1814 /* ETA Info */
c5103619 1815 je = get_jobs_eta(true, &size);
2de615ad
JA
1816 if (je) {
1817 json_object_add_value_int(root, "eta", je->eta_sec);
1818 json_object_add_value_int(root, "elapsed", je->elapsed_sec);
1819 }
b01af66b 1820
66e19a38 1821 if (opt_list)
0cf542af 1822 json_add_job_opts(root, "job options", opt_list);
66e19a38 1823
cc372b17
SL
1824 add_ddir_status_json(ts, rs, DDIR_READ, root);
1825 add_ddir_status_json(ts, rs, DDIR_WRITE, root);
f3afa57e 1826 add_ddir_status_json(ts, rs, DDIR_TRIM, root);
b2b3eefe 1827 add_ddir_status_json(ts, rs, DDIR_SYNC, root);
cc372b17 1828
5cb8a8cd
BP
1829 if (ts->unified_rw_rep == UNIFIED_BOTH)
1830 add_mixed_ddir_status_json(ts, rs, root);
1831
cc372b17
SL
1832 /* CPU Usage */
1833 if (ts->total_run_time) {
1834 double runt = (double) ts->total_run_time;
1835
1836 usr_cpu = (double) ts->usr_time * 100 / runt;
1837 sys_cpu = (double) ts->sys_time * 100 / runt;
1838 } else {
1839 usr_cpu = 0;
1840 sys_cpu = 0;
1841 }
bcbb4c6c 1842 json_object_add_value_int(root, "job_runtime", ts->total_run_time);
cc372b17
SL
1843 json_object_add_value_float(root, "usr_cpu", usr_cpu);
1844 json_object_add_value_float(root, "sys_cpu", sys_cpu);
1845 json_object_add_value_int(root, "ctx", ts->ctx);
1846 json_object_add_value_int(root, "majf", ts->majf);
1847 json_object_add_value_int(root, "minf", ts->minf);
1848
ec3e3648 1849 /* Calc % distribution of IO depths */
d79db122 1850 stat_calc_dist(ts->io_u_map, ddir_rw_sum(ts->total_io_u), io_u_dist);
cc372b17
SL
1851 tmp = json_create_object();
1852 json_object_add_value_object(root, "iodepth_level", tmp);
1853 /* Only show fixed 7 I/O depth levels*/
1854 for (i = 0; i < 7; i++) {
1855 char name[20];
1856 if (i < 6)
98ffb8f3 1857 snprintf(name, 20, "%d", 1 << i);
cc372b17 1858 else
98ffb8f3 1859 snprintf(name, 20, ">=%d", 1 << i);
cc372b17
SL
1860 json_object_add_value_float(tmp, (const char *)name, io_u_dist[i]);
1861 }
1862
ec3e3648
VF
1863 /* Calc % distribution of submit IO depths */
1864 stat_calc_dist(ts->io_u_submit, ts->total_submit, io_u_dist);
1865 tmp = json_create_object();
1866 json_object_add_value_object(root, "iodepth_submit", tmp);
1867 /* Only show fixed 7 I/O depth levels*/
1868 for (i = 0; i < 7; i++) {
1869 char name[20];
1870 if (i == 0)
1871 snprintf(name, 20, "0");
1872 else if (i < 6)
1873 snprintf(name, 20, "%d", 1 << (i+1));
1874 else
1875 snprintf(name, 20, ">=%d", 1 << i);
1876 json_object_add_value_float(tmp, (const char *)name, io_u_dist[i]);
1877 }
1878
1879 /* Calc % distribution of completion IO depths */
1880 stat_calc_dist(ts->io_u_complete, ts->total_complete, io_u_dist);
1881 tmp = json_create_object();
1882 json_object_add_value_object(root, "iodepth_complete", tmp);
1883 /* Only show fixed 7 I/O depth levels*/
1884 for (i = 0; i < 7; i++) {
1885 char name[20];
1886 if (i == 0)
1887 snprintf(name, 20, "0");
1888 else if (i < 6)
1889 snprintf(name, 20, "%d", 1 << (i+1));
1890 else
1891 snprintf(name, 20, ">=%d", 1 << i);
1892 json_object_add_value_float(tmp, (const char *)name, io_u_dist[i]);
1893 }
1894
1895 /* Calc % distribution of nsecond, usecond, msecond latency */
1896 stat_calc_dist(ts->io_u_map, ddir_rw_sum(ts->total_io_u), io_u_dist);
1897 stat_calc_lat_n(ts, io_u_lat_n);
1898 stat_calc_lat_u(ts, io_u_lat_u);
1899 stat_calc_lat_m(ts, io_u_lat_m);
1900
d6bb626e 1901 /* Nanosecond latency */
cc372b17 1902 tmp = json_create_object();
d6bb626e
VF
1903 json_object_add_value_object(root, "latency_ns", tmp);
1904 for (i = 0; i < FIO_IO_U_LAT_N_NR; i++) {
1905 const char *ranges[] = { "2", "4", "10", "20", "50", "100",
1906 "250", "500", "750", "1000", };
1907 json_object_add_value_float(tmp, ranges[i], io_u_lat_n[i]);
1908 }
cc372b17 1909 /* Microsecond latency */
d6bb626e
VF
1910 tmp = json_create_object();
1911 json_object_add_value_object(root, "latency_us", tmp);
cc372b17
SL
1912 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
1913 const char *ranges[] = { "2", "4", "10", "20", "50", "100",
1914 "250", "500", "750", "1000", };
1915 json_object_add_value_float(tmp, ranges[i], io_u_lat_u[i]);
1916 }
1917 /* Millisecond latency */
1918 tmp = json_create_object();
1919 json_object_add_value_object(root, "latency_ms", tmp);
1920 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++) {
1921 const char *ranges[] = { "2", "4", "10", "20", "50", "100",
1922 "250", "500", "750", "1000", "2000",
1923 ">=2000", };
1924 json_object_add_value_float(tmp, ranges[i], io_u_lat_m[i]);
1925 }
1926
1927 /* Additional output if continue_on_error set - default off*/
1928 if (ts->continue_on_error) {
1929 json_object_add_value_int(root, "total_err", ts->total_err_count);
952b05e0 1930 json_object_add_value_int(root, "first_error", ts->first_error);
cc372b17
SL
1931 }
1932
3e260a46
JA
1933 if (ts->latency_depth) {
1934 json_object_add_value_int(root, "latency_depth", ts->latency_depth);
1935 json_object_add_value_int(root, "latency_target", ts->latency_target);
1936 json_object_add_value_float(root, "latency_percentile", ts->latency_percentile.u.f);
1937 json_object_add_value_int(root, "latency_window", ts->latency_window);
1938 }
1939
cc372b17
SL
1940 /* Additional output if description is set */
1941 if (strlen(ts->description))
1942 json_object_add_value_string(root, "desc", ts->description);
1943
66347cfa
DE
1944 if (ts->nr_block_infos) {
1945 /* Block error histogram and types */
1946 int len;
1947 unsigned int *percentiles = NULL;
1948 unsigned int block_state_counts[BLOCK_STATE_COUNT];
1949
1950 len = calc_block_percentiles(ts->nr_block_infos, ts->block_infos,
1951 ts->percentile_list,
1952 &percentiles, block_state_counts);
1953
1954 if (len) {
1955 struct json_object *block, *percentile_object, *states;
8a68c41c 1956 int state;
66347cfa
DE
1957 block = json_create_object();
1958 json_object_add_value_object(root, "block", block);
1959
1960 percentile_object = json_create_object();
1961 json_object_add_value_object(block, "percentiles",
1962 percentile_object);
1963 for (i = 0; i < len; i++) {
1964 char buf[20];
1965 snprintf(buf, sizeof(buf), "%f",
1966 ts->percentile_list[i].u.f);
1967 json_object_add_value_int(percentile_object,
c0a6be6c 1968 buf,
66347cfa
DE
1969 percentiles[i]);
1970 }
1971
1972 states = json_create_object();
1973 json_object_add_value_object(block, "states", states);
1974 for (state = 0; state < BLOCK_STATE_COUNT; state++) {
1975 json_object_add_value_int(states,
1976 block_state_names[state],
1977 block_state_counts[state]);
1978 }
1979 free(percentiles);
1980 }
1981 }
1982
bb49c8bd 1983 if (ts->ss_dur) {
ba8fb6f6
VF
1984 struct json_object *data;
1985 struct json_array *iops, *bw;
a43f4461 1986 int j, k, l;
6da94b07 1987 char ss_buf[64];
16e56d25 1988
6da94b07 1989 snprintf(ss_buf, sizeof(ss_buf), "%s%s:%f%s",
c8caba48
JA
1990 ts->ss_state & FIO_SS_IOPS ? "iops" : "bw",
1991 ts->ss_state & FIO_SS_SLOPE ? "_slope" : "",
bb49c8bd 1992 (float) ts->ss_limit.u.f,
c8caba48 1993 ts->ss_state & FIO_SS_PCT ? "%" : "");
16e56d25
VF
1994
1995 tmp = json_create_object();
1996 json_object_add_value_object(root, "steadystate", tmp);
6da94b07 1997 json_object_add_value_string(tmp, "ss", ss_buf);
bb49c8bd 1998 json_object_add_value_int(tmp, "duration", (int)ts->ss_dur);
c8caba48 1999 json_object_add_value_int(tmp, "attained", (ts->ss_state & FIO_SS_ATTAINED) > 0);
6da94b07 2000
bb49c8bd 2001 snprintf(ss_buf, sizeof(ss_buf), "%f%s", (float) ts->ss_criterion.u.f,
c8caba48 2002 ts->ss_state & FIO_SS_PCT ? "%" : "");
6da94b07 2003 json_object_add_value_string(tmp, "criterion", ss_buf);
bb49c8bd
VF
2004 json_object_add_value_float(tmp, "max_deviation", ts->ss_deviation.u.f);
2005 json_object_add_value_float(tmp, "slope", ts->ss_slope.u.f);
ba8fb6f6
VF
2006
2007 data = json_create_object();
2008 json_object_add_value_object(tmp, "data", data);
2009 bw = json_create_array();
2010 iops = json_create_array();
412c7d91
VF
2011
2012 /*
2013 ** if ss was attained or the buffer is not full,
2014 ** ss->head points to the first element in the list.
2015 ** otherwise it actually points to the second element
2016 ** in the list
2017 */
c8caba48 2018 if ((ts->ss_state & FIO_SS_ATTAINED) || !(ts->ss_state & FIO_SS_BUFFER_FULL))
bb49c8bd 2019 j = ts->ss_head;
412c7d91 2020 else
bb49c8bd 2021 j = ts->ss_head == 0 ? ts->ss_dur - 1 : ts->ss_head - 1;
a43f4461
JA
2022 for (l = 0; l < ts->ss_dur; l++) {
2023 k = (j + l) % ts->ss_dur;
bb49c8bd
VF
2024 json_array_add_value_int(bw, ts->ss_bw_data[k]);
2025 json_array_add_value_int(iops, ts->ss_iops_data[k]);
16e56d25 2026 }
bb49c8bd
VF
2027 json_object_add_value_int(data, "bw_mean", steadystate_bw_mean(ts));
2028 json_object_add_value_int(data, "iops_mean", steadystate_iops_mean(ts));
6da94b07
VF
2029 json_object_add_value_array(data, "iops", iops);
2030 json_object_add_value_array(data, "bw", bw);
16e56d25
VF
2031 }
2032
cc372b17
SL
2033 return root;
2034}
2035
4d658652 2036static void show_thread_status_terse(struct thread_stat *ts,
a666cab8
JA
2037 struct group_run_stats *rs,
2038 struct buf_output *out)
4d658652 2039{
a2c95580 2040 if (terse_version >= 2 && terse_version <= 5)
bef2112b 2041 show_thread_status_terse_all(ts, rs, terse_version, out);
4d658652
JA
2042 else
2043 log_err("fio: bad terse version!? %d\n", terse_version);
2044}
2045
952b05e0 2046struct json_object *show_thread_status(struct thread_stat *ts,
a666cab8 2047 struct group_run_stats *rs,
0279b880 2048 struct flist_head *opt_list,
a666cab8 2049 struct buf_output *out)
952b05e0 2050{
129fb2d4
JA
2051 struct json_object *ret = NULL;
2052
2053 if (output_format & FIO_OUTPUT_TERSE)
a666cab8 2054 show_thread_status_terse(ts, rs, out);
129fb2d4 2055 if (output_format & FIO_OUTPUT_JSON)
0279b880 2056 ret = show_thread_status_json(ts, rs, opt_list);
129fb2d4 2057 if (output_format & FIO_OUTPUT_NORMAL)
a666cab8 2058 show_thread_status_normal(ts, rs, out);
129fb2d4
JA
2059
2060 return ret;
952b05e0
CF
2061}
2062
70750d6a 2063static void __sum_stat(struct io_stat *dst, struct io_stat *src, bool first)
756867bd
JA
2064{
2065 double mean, S;
2066
2067 dst->min_val = min(dst->min_val, src->min_val);
2068 dst->max_val = max(dst->max_val, src->max_val);
756867bd
JA
2069
2070 /*
cdcac5cf
YH
2071 * Compute new mean and S after the merge
2072 * <http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
2073 * #Parallel_algorithm>
756867bd 2074 */
fd595830 2075 if (first) {
802ad4a8
JA
2076 mean = src->mean.u.f;
2077 S = src->S.u.f;
756867bd 2078 } else {
802ad4a8 2079 double delta = src->mean.u.f - dst->mean.u.f;
cdcac5cf 2080
802ad4a8
JA
2081 mean = ((src->mean.u.f * src->samples) +
2082 (dst->mean.u.f * dst->samples)) /
cdcac5cf
YH
2083 (dst->samples + src->samples);
2084
802ad4a8 2085 S = src->S.u.f + dst->S.u.f + pow(delta, 2.0) *
cdcac5cf
YH
2086 (dst->samples * src->samples) /
2087 (dst->samples + src->samples);
756867bd
JA
2088 }
2089
cdcac5cf 2090 dst->samples += src->samples;
802ad4a8
JA
2091 dst->mean.u.f = mean;
2092 dst->S.u.f = S;
70750d6a
JA
2093
2094}
2095
2096/*
2097 * We sum two kinds of stats - one that is time based, in which case we
2098 * apply the proper summing technique, and then one that is iops/bw
2099 * numbers. For group_reporting, we should just add those up, not make
2100 * them the mean of everything.
2101 */
2102static void sum_stat(struct io_stat *dst, struct io_stat *src, bool first,
2103 bool pure_sum)
2104{
2105 if (src->samples == 0)
2106 return;
2107
2108 if (!pure_sum) {
2109 __sum_stat(dst, src, first);
2110 return;
2111 }
2112
68afa5b5
JA
2113 if (first) {
2114 dst->min_val = src->min_val;
2115 dst->max_val = src->max_val;
2116 dst->samples = src->samples;
2117 dst->mean.u.f = src->mean.u.f;
2118 dst->S.u.f = src->S.u.f;
2119 } else {
2120 dst->min_val += src->min_val;
2121 dst->max_val += src->max_val;
2122 dst->samples += src->samples;
2123 dst->mean.u.f += src->mean.u.f;
2124 dst->S.u.f += src->S.u.f;
2125 }
756867bd
JA
2126}
2127
37f0c1ae
JA
2128void sum_group_stats(struct group_run_stats *dst, struct group_run_stats *src)
2129{
2130 int i;
2131
6eaf09d6 2132 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
37f0c1ae
JA
2133 if (dst->max_run[i] < src->max_run[i])
2134 dst->max_run[i] = src->max_run[i];
2135 if (dst->min_run[i] && dst->min_run[i] > src->min_run[i])
2136 dst->min_run[i] = src->min_run[i];
2137 if (dst->max_bw[i] < src->max_bw[i])
2138 dst->max_bw[i] = src->max_bw[i];
2139 if (dst->min_bw[i] && dst->min_bw[i] > src->min_bw[i])
2140 dst->min_bw[i] = src->min_bw[i];
2141
af7f87cb 2142 dst->iobytes[i] += src->iobytes[i];
37f0c1ae
JA
2143 dst->agg[i] += src->agg[i];
2144 }
2145
dbae1bd6
JA
2146 if (!dst->kb_base)
2147 dst->kb_base = src->kb_base;
2148 if (!dst->unit_base)
2149 dst->unit_base = src->unit_base;
18aa1998
JF
2150 if (!dst->sig_figs)
2151 dst->sig_figs = src->sig_figs;
37f0c1ae
JA
2152}
2153
fd595830
JA
2154void sum_thread_stats(struct thread_stat *dst, struct thread_stat *src,
2155 bool first)
5b9babb7 2156{
df8781b6 2157 int k, l, m;
5b9babb7 2158
6eaf09d6 2159 for (l = 0; l < DDIR_RWDIR_CNT; l++) {
5cb8a8cd 2160 if (!(dst->unified_rw_rep == UNIFIED_MIXED)) {
70750d6a 2161 sum_stat(&dst->clat_stat[l], &src->clat_stat[l], first, false);
b2a432bf 2162 sum_stat(&dst->clat_high_prio_stat[l], &src->clat_high_prio_stat[l], first, false);
efd78265 2163 sum_stat(&dst->clat_low_prio_stat[l], &src->clat_low_prio_stat[l], first, false);
70750d6a
JA
2164 sum_stat(&dst->slat_stat[l], &src->slat_stat[l], first, false);
2165 sum_stat(&dst->lat_stat[l], &src->lat_stat[l], first, false);
2166 sum_stat(&dst->bw_stat[l], &src->bw_stat[l], first, true);
2167 sum_stat(&dst->iops_stat[l], &src->iops_stat[l], first, true);
771e58be
JA
2168
2169 dst->io_bytes[l] += src->io_bytes[l];
2170
2171 if (dst->runtime[l] < src->runtime[l])
2172 dst->runtime[l] = src->runtime[l];
2173 } else {
70750d6a 2174 sum_stat(&dst->clat_stat[0], &src->clat_stat[l], first, false);
77e352c9
VF
2175 sum_stat(&dst->clat_high_prio_stat[0], &src->clat_high_prio_stat[l], first, false);
2176 sum_stat(&dst->clat_low_prio_stat[0], &src->clat_low_prio_stat[l], first, false);
70750d6a
JA
2177 sum_stat(&dst->slat_stat[0], &src->slat_stat[l], first, false);
2178 sum_stat(&dst->lat_stat[0], &src->lat_stat[l], first, false);
2179 sum_stat(&dst->bw_stat[0], &src->bw_stat[l], first, true);
2180 sum_stat(&dst->iops_stat[0], &src->iops_stat[l], first, true);
771e58be
JA
2181
2182 dst->io_bytes[0] += src->io_bytes[l];
2183
2184 if (dst->runtime[0] < src->runtime[l])
2185 dst->runtime[0] = src->runtime[l];
fd595830
JA
2186
2187 /*
2188 * We're summing to the same destination, so override
2189 * 'first' after the first iteration of the loop
2190 */
2191 first = false;
771e58be 2192 }
5b9babb7
JA
2193 }
2194
70750d6a 2195 sum_stat(&dst->sync_stat, &src->sync_stat, first, false);
5b9babb7
JA
2196 dst->usr_time += src->usr_time;
2197 dst->sys_time += src->sys_time;
2198 dst->ctx += src->ctx;
2199 dst->majf += src->majf;
2200 dst->minf += src->minf;
2201
b2b3eefe 2202 for (k = 0; k < FIO_IO_U_MAP_NR; k++) {
5b9babb7 2203 dst->io_u_map[k] += src->io_u_map[k];
5b9babb7 2204 dst->io_u_submit[k] += src->io_u_submit[k];
5b9babb7 2205 dst->io_u_complete[k] += src->io_u_complete[k];
b2b3eefe 2206 }
bf14b39e
VF
2207
2208 for (k = 0; k < FIO_IO_U_LAT_N_NR; k++)
d6bb626e 2209 dst->io_u_lat_n[k] += src->io_u_lat_n[k];
bf14b39e 2210 for (k = 0; k < FIO_IO_U_LAT_U_NR; k++)
5b9babb7 2211 dst->io_u_lat_u[k] += src->io_u_lat_u[k];
bf14b39e 2212 for (k = 0; k < FIO_IO_U_LAT_M_NR; k++)
5b9babb7 2213 dst->io_u_lat_m[k] += src->io_u_lat_m[k];
bf14b39e 2214
6eaf09d6 2215 for (k = 0; k < DDIR_RWDIR_CNT; k++) {
5cb8a8cd 2216 if (!(dst->unified_rw_rep == UNIFIED_MIXED)) {
771e58be
JA
2217 dst->total_io_u[k] += src->total_io_u[k];
2218 dst->short_io_u[k] += src->short_io_u[k];
3bcb9d94 2219 dst->drop_io_u[k] += src->drop_io_u[k];
771e58be
JA
2220 } else {
2221 dst->total_io_u[0] += src->total_io_u[k];
2222 dst->short_io_u[0] += src->short_io_u[k];
3bcb9d94 2223 dst->drop_io_u[0] += src->drop_io_u[k];
771e58be 2224 }
5b9babb7
JA
2225 }
2226
7f3ecee2
JA
2227 dst->total_io_u[DDIR_SYNC] += src->total_io_u[DDIR_SYNC];
2228
df8781b6
VF
2229 for (k = 0; k < FIO_LAT_CNT; k++)
2230 for (l = 0; l < DDIR_RWDIR_CNT; l++)
2231 for (m = 0; m < FIO_IO_U_PLAT_NR; m++)
5cb8a8cd 2232 if (!(dst->unified_rw_rep == UNIFIED_MIXED))
df8781b6
VF
2233 dst->io_u_plat[k][l][m] += src->io_u_plat[k][l][m];
2234 else
2235 dst->io_u_plat[k][0][m] += src->io_u_plat[k][l][m];
771e58be 2236
df8781b6
VF
2237 for (k = 0; k < FIO_IO_U_PLAT_NR; k++)
2238 dst->io_u_sync_plat[k] += src->io_u_sync_plat[k];
2239
2240 for (k = 0; k < DDIR_RWDIR_CNT; k++) {
771e58be 2241 for (m = 0; m < FIO_IO_U_PLAT_NR; m++) {
5cb8a8cd 2242 if (!(dst->unified_rw_rep == UNIFIED_MIXED)) {
b2a432bf 2243 dst->io_u_plat_high_prio[k][m] += src->io_u_plat_high_prio[k][m];
efd78265 2244 dst->io_u_plat_low_prio[k][m] += src->io_u_plat_low_prio[k][m];
b2a432bf 2245 } else {
b2a432bf 2246 dst->io_u_plat_high_prio[0][m] += src->io_u_plat_high_prio[k][m];
efd78265 2247 dst->io_u_plat_low_prio[0][m] += src->io_u_plat_low_prio[k][m];
b2a432bf
PC
2248 }
2249
771e58be 2250 }
5b9babb7
JA
2251 }
2252
2253 dst->total_run_time += src->total_run_time;
2254 dst->total_submit += src->total_submit;
2255 dst->total_complete += src->total_complete;
fd5d733f 2256 dst->nr_zone_resets += src->nr_zone_resets;
96563db9
JA
2257 dst->cachehit += src->cachehit;
2258 dst->cachemiss += src->cachemiss;
5b9babb7
JA
2259}
2260
37f0c1ae
JA
2261void init_group_run_stat(struct group_run_stats *gs)
2262{
6eaf09d6 2263 int i;
37f0c1ae 2264 memset(gs, 0, sizeof(*gs));
6eaf09d6
SL
2265
2266 for (i = 0; i < DDIR_RWDIR_CNT; i++)
2267 gs->min_bw[i] = gs->min_run[i] = ~0UL;
37f0c1ae
JA
2268}
2269
2270void init_thread_stat(struct thread_stat *ts)
2271{
2272 int j;
2273
2274 memset(ts, 0, sizeof(*ts));
2275
6eaf09d6 2276 for (j = 0; j < DDIR_RWDIR_CNT; j++) {
37f0c1ae
JA
2277 ts->lat_stat[j].min_val = -1UL;
2278 ts->clat_stat[j].min_val = -1UL;
2279 ts->slat_stat[j].min_val = -1UL;
2280 ts->bw_stat[j].min_val = -1UL;
188b6016 2281 ts->iops_stat[j].min_val = -1UL;
b2a432bf 2282 ts->clat_high_prio_stat[j].min_val = -1UL;
efd78265 2283 ts->clat_low_prio_stat[j].min_val = -1UL;
37f0c1ae 2284 }
7f3ecee2 2285 ts->sync_stat.min_val = -1UL;
37f0c1ae
JA
2286 ts->groupid = -1;
2287}
2288
83f7b64e 2289void __show_run_stats(void)
3c39a379
JA
2290{
2291 struct group_run_stats *runstats, *rs;
2292 struct thread_data *td;
756867bd 2293 struct thread_stat *threadstats, *ts;
4da24b69 2294 int i, j, k, nr_ts, last_ts, idx;
8985b491
JA
2295 bool kb_base_warned = false;
2296 bool unit_base_warned = false;
cc372b17
SL
2297 struct json_object *root = NULL;
2298 struct json_array *array = NULL;
a666cab8 2299 struct buf_output output[FIO_OUTPUT_NR];
66e19a38 2300 struct flist_head **opt_lists;
a666cab8 2301
3c39a379
JA
2302 runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1));
2303
37f0c1ae
JA
2304 for (i = 0; i < groupid + 1; i++)
2305 init_group_run_stat(&runstats[i]);
3c39a379 2306
756867bd
JA
2307 /*
2308 * find out how many threads stats we need. if group reporting isn't
2309 * enabled, it's one-per-td.
2310 */
2311 nr_ts = 0;
2312 last_ts = -1;
2313 for_each_td(td, i) {
2dc1bbeb 2314 if (!td->o.group_reporting) {
756867bd
JA
2315 nr_ts++;
2316 continue;
2317 }
2318 if (last_ts == td->groupid)
2319 continue;
8243be59
JA
2320 if (!td->o.stats)
2321 continue;
756867bd
JA
2322
2323 last_ts = td->groupid;
2324 nr_ts++;
2325 }
2326
2327 threadstats = malloc(nr_ts * sizeof(struct thread_stat));
66e19a38 2328 opt_lists = malloc(nr_ts * sizeof(struct flist_head *));
756867bd 2329
66e19a38 2330 for (i = 0; i < nr_ts; i++) {
37f0c1ae 2331 init_thread_stat(&threadstats[i]);
66e19a38
JA
2332 opt_lists[i] = NULL;
2333 }
756867bd
JA
2334
2335 j = 0;
2336 last_ts = -1;
197574e4 2337 idx = 0;
34572e28 2338 for_each_td(td, i) {
8243be59
JA
2339 if (!td->o.stats)
2340 continue;
2dc1bbeb
JA
2341 if (idx && (!td->o.group_reporting ||
2342 (td->o.group_reporting && last_ts != td->groupid))) {
7abd0e3a
JA
2343 idx = 0;
2344 j++;
2345 }
2346
2347 last_ts = td->groupid;
2348
756867bd
JA
2349 ts = &threadstats[j];
2350
83349190 2351 ts->clat_percentiles = td->o.clat_percentiles;
b599759b 2352 ts->lat_percentiles = td->o.lat_percentiles;
56440e63 2353 ts->slat_percentiles = td->o.slat_percentiles;
435d195a 2354 ts->percentile_precision = td->o.percentile_precision;
fd112d34 2355 memcpy(ts->percentile_list, td->o.percentile_list, sizeof(td->o.percentile_list));
66e19a38 2356 opt_lists[j] = &td->opt_list;
83349190 2357
197574e4 2358 idx++;
6586ee89 2359 ts->members++;
756867bd 2360
7abd0e3a 2361 if (ts->groupid == -1) {
2dc84ba7
JA
2362 /*
2363 * These are per-group shared already
2364 */
36833fb0 2365 snprintf(ts->name, sizeof(ts->name), "%s", td->o.name);
a64e88da 2366 if (td->o.description)
36833fb0
BVA
2367 snprintf(ts->description,
2368 sizeof(ts->description), "%s",
2369 td->o.description);
a64e88da 2370 else
4e59d0f3 2371 memset(ts->description, 0, FIO_JOBDESC_SIZE);
a64e88da 2372
2f122b13
JA
2373 /*
2374 * If multiple entries in this group, this is
2375 * the first member.
2376 */
2377 ts->thread_number = td->thread_number;
756867bd 2378 ts->groupid = td->groupid;
2dc84ba7
JA
2379
2380 /*
2381 * first pid in group, not very useful...
2382 */
756867bd 2383 ts->pid = td->pid;
90fef2d1
JA
2384
2385 ts->kb_base = td->o.kb_base;
ad705bcb 2386 ts->unit_base = td->o.unit_base;
e883cb35 2387 ts->sig_figs = td->o.sig_figs;
771e58be 2388 ts->unified_rw_rep = td->o.unified_rw_rep;
90fef2d1
JA
2389 } else if (ts->kb_base != td->o.kb_base && !kb_base_warned) {
2390 log_info("fio: kb_base differs for jobs in group, using"
2391 " %u as the base\n", ts->kb_base);
8985b491 2392 kb_base_warned = true;
ad705bcb
SN
2393 } else if (ts->unit_base != td->o.unit_base && !unit_base_warned) {
2394 log_info("fio: unit_base differs for jobs in group, using"
2395 " %u as the base\n", ts->unit_base);
8985b491 2396 unit_base_warned = true;
2dc84ba7
JA
2397 }
2398
f2bba182
RR
2399 ts->continue_on_error = td->o.continue_on_error;
2400 ts->total_err_count += td->total_err_count;
2401 ts->first_error = td->first_error;
2402 if (!ts->error) {
2403 if (!td->error && td->o.continue_on_error &&
2404 td->first_error) {
2405 ts->error = td->first_error;
36833fb0
BVA
2406 snprintf(ts->verror, sizeof(ts->verror), "%s",
2407 td->verror);
f2bba182
RR
2408 } else if (td->error) {
2409 ts->error = td->error;
36833fb0
BVA
2410 snprintf(ts->verror, sizeof(ts->verror), "%s",
2411 td->verror);
f2bba182 2412 }
756867bd
JA
2413 }
2414
3e260a46
JA
2415 ts->latency_depth = td->latency_qd;
2416 ts->latency_target = td->o.latency_target;
2417 ts->latency_percentile = td->o.latency_percentile;
2418 ts->latency_window = td->o.latency_window;
2419
0e4dd95c 2420 ts->nr_block_infos = td->ts.nr_block_infos;
4da24b69
JA
2421 for (k = 0; k < ts->nr_block_infos; k++)
2422 ts->block_infos[k] = td->ts.block_infos[k];
0e4dd95c 2423
fd595830 2424 sum_thread_stats(ts, &td->ts, idx == 1);
16e56d25 2425
bb49c8bd
VF
2426 if (td->o.ss_dur) {
2427 ts->ss_state = td->ss.state;
2428 ts->ss_dur = td->ss.dur;
2429 ts->ss_head = td->ss.head;
bb49c8bd
VF
2430 ts->ss_bw_data = td->ss.bw_data;
2431 ts->ss_iops_data = td->ss.iops_data;
2432 ts->ss_limit.u.f = td->ss.limit;
2433 ts->ss_slope.u.f = td->ss.slope;
2434 ts->ss_deviation.u.f = td->ss.deviation;
2435 ts->ss_criterion.u.f = td->ss.criterion;
2436 }
16e56d25 2437 else
bb49c8bd 2438 ts->ss_dur = ts->ss_state = 0;
756867bd
JA
2439 }
2440
2441 for (i = 0; i < nr_ts; i++) {
94370ac4 2442 unsigned long long bw;
3c39a379 2443
756867bd 2444 ts = &threadstats[i];
dedb88eb
JA
2445 if (ts->groupid == -1)
2446 continue;
756867bd 2447 rs = &runstats[ts->groupid];
90fef2d1 2448 rs->kb_base = ts->kb_base;
ad705bcb 2449 rs->unit_base = ts->unit_base;
e883cb35 2450 rs->sig_figs = ts->sig_figs;
5cb8a8cd 2451 rs->unified_rw_rep |= ts->unified_rw_rep;
3c39a379 2452
6eaf09d6 2453 for (j = 0; j < DDIR_RWDIR_CNT; j++) {
94370ac4
JA
2454 if (!ts->runtime[j])
2455 continue;
2456 if (ts->runtime[j] < rs->min_run[j] || !rs->min_run[j])
2457 rs->min_run[j] = ts->runtime[j];
2458 if (ts->runtime[j] > rs->max_run[j])
2459 rs->max_run[j] = ts->runtime[j];
2460
2461 bw = 0;
af7f87cb
RE
2462 if (ts->runtime[j])
2463 bw = ts->io_bytes[j] * 1000 / ts->runtime[j];
94370ac4
JA
2464 if (bw < rs->min_bw[j])
2465 rs->min_bw[j] = bw;
2466 if (bw > rs->max_bw[j])
2467 rs->max_bw[j] = bw;
2468
af7f87cb 2469 rs->iobytes[j] += ts->io_bytes[j];
94370ac4 2470 }
3c39a379
JA
2471 }
2472
2473 for (i = 0; i < groupid + 1; i++) {
6eaf09d6
SL
2474 int ddir;
2475
3c39a379
JA
2476 rs = &runstats[i];
2477
6eaf09d6
SL
2478 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) {
2479 if (rs->max_run[ddir])
af7f87cb 2480 rs->agg[ddir] = (rs->iobytes[ddir] * 1000) /
6eaf09d6
SL
2481 rs->max_run[ddir];
2482 }
3c39a379
JA
2483 }
2484
a666cab8 2485 for (i = 0; i < FIO_OUTPUT_NR; i++)
e250c0a9 2486 buf_output_init(&output[i]);
a666cab8 2487
3c39a379
JA
2488 /*
2489 * don't overwrite last signal output
2490 */
129fb2d4 2491 if (output_format & FIO_OUTPUT_NORMAL)
a666cab8 2492 log_buf(&output[__FIO_OUTPUT_NORMAL], "\n");
129fb2d4 2493 if (output_format & FIO_OUTPUT_JSON) {
66e19a38 2494 struct thread_data *global;
35326842 2495 char time_buf[32];
aa7d2ef0
RH
2496 struct timeval now;
2497 unsigned long long ms_since_epoch;
afd74e2a 2498 time_t tv_sec;
91e53870 2499
aa7d2ef0
RH
2500 gettimeofday(&now, NULL);
2501 ms_since_epoch = (unsigned long long)(now.tv_sec) * 1000 +
2502 (unsigned long long)(now.tv_usec) / 1000;
2503
afd74e2a
BVA
2504 tv_sec = now.tv_sec;
2505 os_ctime_r(&tv_sec, time_buf, sizeof(time_buf));
1d272416
JA
2506 if (time_buf[strlen(time_buf) - 1] == '\n')
2507 time_buf[strlen(time_buf) - 1] = '\0';
91e53870 2508
cc372b17
SL
2509 root = json_create_object();
2510 json_object_add_value_string(root, "fio version", fio_version_string);
aa7d2ef0
RH
2511 json_object_add_value_int(root, "timestamp", now.tv_sec);
2512 json_object_add_value_int(root, "timestamp_ms", ms_since_epoch);
91e53870 2513 json_object_add_value_string(root, "time", time_buf);
66e19a38 2514 global = get_global_options();
0cf542af 2515 json_add_job_opts(root, "global options", &global->opt_list);
cc372b17
SL
2516 array = json_create_array();
2517 json_object_add_value_array(root, "jobs", array);
2518 }
3c39a379 2519
0279b880
JA
2520 if (is_backend)
2521 fio_server_send_job_options(&get_global_options()->opt_list, -1U);
2522
756867bd
JA
2523 for (i = 0; i < nr_ts; i++) {
2524 ts = &threadstats[i];
2525 rs = &runstats[ts->groupid];
3c39a379 2526
0279b880
JA
2527 if (is_backend) {
2528 fio_server_send_job_options(opt_lists[i], i);
a64e88da 2529 fio_server_send_ts(ts, rs);
0279b880 2530 } else {
129fb2d4 2531 if (output_format & FIO_OUTPUT_TERSE)
a666cab8 2532 show_thread_status_terse(ts, rs, &output[__FIO_OUTPUT_TERSE]);
129fb2d4 2533 if (output_format & FIO_OUTPUT_JSON) {
66e19a38 2534 struct json_object *tmp = show_thread_status_json(ts, rs, opt_lists[i]);
129fb2d4
JA
2535 json_array_add_value_object(array, tmp);
2536 }
2537 if (output_format & FIO_OUTPUT_NORMAL)
a666cab8 2538 show_thread_status_normal(ts, rs, &output[__FIO_OUTPUT_NORMAL]);
129fb2d4 2539 }
3c39a379 2540 }
ab34ddd1 2541 if (!is_backend && (output_format & FIO_OUTPUT_JSON)) {
cc372b17 2542 /* disk util stats, if any */
a666cab8 2543 show_disk_util(1, root, &output[__FIO_OUTPUT_JSON]);
cc372b17 2544
a666cab8 2545 show_idle_prof_stats(FIO_OUTPUT_JSON, root, &output[__FIO_OUTPUT_JSON]);
f2a2ce0e 2546
a666cab8
JA
2547 json_print_object(root, &output[__FIO_OUTPUT_JSON]);
2548 log_buf(&output[__FIO_OUTPUT_JSON], "\n");
cc372b17
SL
2549 json_free_object(root);
2550 }
3c39a379 2551
72c27ff8
JA
2552 for (i = 0; i < groupid + 1; i++) {
2553 rs = &runstats[i];
3c39a379 2554
72c27ff8 2555 rs->groupid = i;
d09a64a0 2556 if (is_backend)
72c27ff8 2557 fio_server_send_gs(rs);
129fb2d4 2558 else if (output_format & FIO_OUTPUT_NORMAL)
a666cab8 2559 show_group_stats(rs, &output[__FIO_OUTPUT_NORMAL]);
c6ae0a5b 2560 }
eecf272f 2561
72c27ff8
JA
2562 if (is_backend)
2563 fio_server_send_du();
129fb2d4 2564 else if (output_format & FIO_OUTPUT_NORMAL) {
a666cab8
JA
2565 show_disk_util(0, NULL, &output[__FIO_OUTPUT_NORMAL]);
2566 show_idle_prof_stats(FIO_OUTPUT_NORMAL, NULL, &output[__FIO_OUTPUT_NORMAL]);
60904003 2567 }
f2a2ce0e 2568
3d57c0e9 2569 for (i = 0; i < FIO_OUTPUT_NR; i++) {
3dc3aa41 2570 struct buf_output *out = &output[i];
8dd0eca3 2571
3dc3aa41 2572 log_info_buf(out->buf, out->buflen);
3dc3aa41 2573 buf_output_free(out);
3d57c0e9 2574 }
2b8c71b0 2575
7ceefc09
FS
2576 fio_idle_prof_cleanup();
2577
fdd5f15f 2578 log_info_flush();
eecf272f 2579 free(runstats);
756867bd 2580 free(threadstats);
66e19a38 2581 free(opt_lists);
3c39a379
JA
2582}
2583
966f8ef9 2584int __show_running_run_stats(void)
b852e7cf
JA
2585{
2586 struct thread_data *td;
2587 unsigned long long *rt;
8b6a404c 2588 struct timespec ts;
b852e7cf
JA
2589 int i;
2590
971caeb1 2591 fio_sem_down(stat_sem);
90811930 2592
b852e7cf 2593 rt = malloc(thread_number * sizeof(unsigned long long));
8b6a404c 2594 fio_gettime(&ts, NULL);
b852e7cf
JA
2595
2596 for_each_td(td, i) {
c97f1ad6 2597 td->update_rusage = 1;
dde39f8c
AD
2598 for_each_rw_ddir(ddir) {
2599 td->ts.io_bytes[ddir] = td->io_bytes[ddir];
2600 }
8b6a404c 2601 td->ts.total_run_time = mtime_since(&td->epoch, &ts);
6c041a88 2602
8b6a404c 2603 rt[i] = mtime_since(&td->start, &ts);
6c041a88 2604 if (td_read(td) && td->ts.io_bytes[DDIR_READ])
2605 td->ts.runtime[DDIR_READ] += rt[i];
2606 if (td_write(td) && td->ts.io_bytes[DDIR_WRITE])
2607 td->ts.runtime[DDIR_WRITE] += rt[i];
2608 if (td_trim(td) && td->ts.io_bytes[DDIR_TRIM])
2609 td->ts.runtime[DDIR_TRIM] += rt[i];
b852e7cf
JA
2610 }
2611
c97f1ad6 2612 for_each_td(td, i) {
fda2cfac
JA
2613 if (td->runstate >= TD_EXITED)
2614 continue;
c97f1ad6
JA
2615 if (td->rusage_sem) {
2616 td->update_rusage = 1;
971caeb1 2617 fio_sem_down(td->rusage_sem);
c97f1ad6
JA
2618 }
2619 td->update_rusage = 0;
2620 }
2621
cef9175e 2622 __show_run_stats();
b852e7cf
JA
2623
2624 for_each_td(td, i) {
6c041a88 2625 if (td_read(td) && td->ts.io_bytes[DDIR_READ])
b852e7cf 2626 td->ts.runtime[DDIR_READ] -= rt[i];
6c041a88 2627 if (td_write(td) && td->ts.io_bytes[DDIR_WRITE])
b852e7cf 2628 td->ts.runtime[DDIR_WRITE] -= rt[i];
6c041a88 2629 if (td_trim(td) && td->ts.io_bytes[DDIR_TRIM])
6eaf09d6 2630 td->ts.runtime[DDIR_TRIM] -= rt[i];
b852e7cf
JA
2631 }
2632
2633 free(rt);
971caeb1 2634 fio_sem_up(stat_sem);
966f8ef9
BVA
2635
2636 return 0;
b852e7cf
JA
2637}
2638
8985b491 2639static bool status_file_disabled;
06464907 2640
dac45f23 2641#define FIO_STATUS_FILE "fio-dump-status"
06464907
JA
2642
2643static int check_status_file(void)
2644{
2645 struct stat sb;
a0bafb7d
BC
2646 const char *temp_dir;
2647 char fio_status_file_path[PATH_MAX];
06464907 2648
77d99675
JA
2649 if (status_file_disabled)
2650 return 0;
2651
a0bafb7d 2652 temp_dir = getenv("TMPDIR");
48b16e27 2653 if (temp_dir == NULL) {
a0bafb7d 2654 temp_dir = getenv("TEMP");
48b16e27
JA
2655 if (temp_dir && strlen(temp_dir) >= PATH_MAX)
2656 temp_dir = NULL;
2657 }
a0bafb7d
BC
2658 if (temp_dir == NULL)
2659 temp_dir = "/tmp";
3544731c
BVA
2660#ifdef __COVERITY__
2661 __coverity_tainted_data_sanitize__(temp_dir);
2662#endif
a0bafb7d
BC
2663
2664 snprintf(fio_status_file_path, sizeof(fio_status_file_path), "%s/%s", temp_dir, FIO_STATUS_FILE);
2665
2666 if (stat(fio_status_file_path, &sb))
06464907
JA
2667 return 0;
2668
77d99675
JA
2669 if (unlink(fio_status_file_path) < 0) {
2670 log_err("fio: failed to unlink %s: %s\n", fio_status_file_path,
2671 strerror(errno));
2672 log_err("fio: disabling status file updates\n");
8985b491 2673 status_file_disabled = true;
77d99675
JA
2674 }
2675
06464907
JA
2676 return 1;
2677}
2678
2679void check_for_running_stats(void)
2680{
06464907
JA
2681 if (check_status_file()) {
2682 show_running_run_stats();
2683 return;
2684 }
2685}
2686
d6bb626e 2687static inline void add_stat_sample(struct io_stat *is, unsigned long long data)
3c39a379 2688{
68704084 2689 double val = data;
6660cc67 2690 double delta;
68704084
JA
2691
2692 if (data > is->max_val)
2693 is->max_val = data;
2694 if (data < is->min_val)
2695 is->min_val = data;
2696
802ad4a8 2697 delta = val - is->mean.u.f;
ef11d737 2698 if (delta) {
802ad4a8
JA
2699 is->mean.u.f += delta / (is->samples + 1.0);
2700 is->S.u.f += delta * (val - is->mean.u.f);
ef11d737 2701 }
3c39a379 2702
3c39a379
JA
2703 is->samples++;
2704}
2705
7e419452
JA
2706/*
2707 * Return a struct io_logs, which is added to the tail of the log
2708 * list for 'iolog'.
2709 */
2710static struct io_logs *get_new_log(struct io_log *iolog)
2711{
2712 size_t new_size, new_samples;
2713 struct io_logs *cur_log;
2714
2715 /*
2716 * Cap the size at MAX_LOG_ENTRIES, so we don't keep doubling
2717 * forever
2718 */
2719 if (!iolog->cur_log_max)
2720 new_samples = DEF_LOG_ENTRIES;
2721 else {
2722 new_samples = iolog->cur_log_max * 2;
2723 if (new_samples > MAX_LOG_ENTRIES)
2724 new_samples = MAX_LOG_ENTRIES;
2725 }
2726
7e419452 2727 new_size = new_samples * log_entry_sz(iolog);
7e419452 2728
90d2d53f 2729 cur_log = smalloc(sizeof(*cur_log));
7e419452
JA
2730 if (cur_log) {
2731 INIT_FLIST_HEAD(&cur_log->list);
2732 cur_log->log = malloc(new_size);
2733 if (cur_log->log) {
2734 cur_log->nr_samples = 0;
2735 cur_log->max_samples = new_samples;
2736 flist_add_tail(&cur_log->list, &iolog->io_logs);
2737 iolog->cur_log_max = new_samples;
2738 return cur_log;
2739 }
90d2d53f 2740 sfree(cur_log);
7e419452
JA
2741 }
2742
2743 return NULL;
2744}
2745
1fed2080
JA
2746/*
2747 * Add and return a new log chunk, or return current log if big enough
2748 */
2749static struct io_logs *regrow_log(struct io_log *iolog)
7e419452
JA
2750{
2751 struct io_logs *cur_log;
1fed2080 2752 int i;
7e419452 2753
1fed2080 2754 if (!iolog || iolog->disabled)
2ab71dc4 2755 goto disable;
7e419452 2756
1fed2080 2757 cur_log = iolog_cur_log(iolog);
a9afa45a
JA
2758 if (!cur_log) {
2759 cur_log = get_new_log(iolog);
2760 if (!cur_log)
2761 return NULL;
2762 }
2763
7e419452
JA
2764 if (cur_log->nr_samples < cur_log->max_samples)
2765 return cur_log;
2766
2767 /*
2768 * No room for a new sample. If we're compressing on the fly, flush
2769 * out the current chunk
2770 */
2771 if (iolog->log_gz) {
2772 if (iolog_cur_flush(iolog, cur_log)) {
2773 log_err("fio: failed flushing iolog! Will stop logging.\n");
2774 return NULL;
2775 }
2776 }
2777
2778 /*
2779 * Get a new log array, and add to our list
2780 */
2781 cur_log = get_new_log(iolog);
1fed2080
JA
2782 if (!cur_log) {
2783 log_err("fio: failed extending iolog! Will stop logging.\n");
2784 return NULL;
2785 }
2786
2787 if (!iolog->pending || !iolog->pending->nr_samples)
80a24ba9 2788 return cur_log;
7e419452 2789
1fed2080
JA
2790 /*
2791 * Flush pending items to new log
2792 */
2793 for (i = 0; i < iolog->pending->nr_samples; i++) {
2794 struct io_sample *src, *dst;
2795
2796 src = get_sample(iolog, iolog->pending, i);
2797 dst = get_sample(iolog, cur_log, i);
2798 memcpy(dst, src, log_entry_sz(iolog));
2799 }
0b2eef49 2800 cur_log->nr_samples = iolog->pending->nr_samples;
1fed2080
JA
2801
2802 iolog->pending->nr_samples = 0;
2803 return cur_log;
2ab71dc4
JA
2804disable:
2805 if (iolog)
2806 iolog->disabled = true;
2807 return NULL;
1fed2080
JA
2808}
2809
2810void regrow_logs(struct thread_data *td)
2811{
2ab71dc4
JA
2812 regrow_log(td->slat_log);
2813 regrow_log(td->clat_log);
1e613c9c 2814 regrow_log(td->clat_hist_log);
2ab71dc4
JA
2815 regrow_log(td->lat_log);
2816 regrow_log(td->bw_log);
2817 regrow_log(td->iops_log);
1fed2080
JA
2818 td->flags &= ~TD_F_REGROW_LOGS;
2819}
2820
76204de3
PM
2821void regrow_agg_logs(void)
2822{
2823 enum fio_ddir ddir;
2824
2825 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++)
2826 regrow_log(agg_io_log[ddir]);
2827}
2828
1fed2080
JA
2829static struct io_logs *get_cur_log(struct io_log *iolog)
2830{
2831 struct io_logs *cur_log;
2832
2833 cur_log = iolog_cur_log(iolog);
2834 if (!cur_log) {
2835 cur_log = get_new_log(iolog);
2836 if (!cur_log)
2837 return NULL;
2838 }
2839
2840 if (cur_log->nr_samples < cur_log->max_samples)
2841 return cur_log;
2842
2843 /*
1eb467fb
JA
2844 * Out of space. If we're in IO offload mode, or we're not doing
2845 * per unit logging (hence logging happens outside of the IO thread
2846 * as well), add a new log chunk inline. If we're doing inline
2847 * submissions, flag 'td' as needing a log regrow and we'll take
2848 * care of it on the submission side.
1fed2080 2849 */
ab5643cb 2850 if ((iolog->td && iolog->td->o.io_submit_mode == IO_MODE_OFFLOAD) ||
1eb467fb 2851 !per_unit_log(iolog))
1fed2080
JA
2852 return regrow_log(iolog);
2853
ab5643cb
IK
2854 if (iolog->td)
2855 iolog->td->flags |= TD_F_REGROW_LOGS;
2856 if (iolog->pending)
2857 assert(iolog->pending->nr_samples < iolog->pending->max_samples);
1fed2080 2858 return iolog->pending;
7e419452
JA
2859}
2860
af2fde19 2861static void __add_log_sample(struct io_log *iolog, union io_sample_data data,
5fff9543 2862 enum fio_ddir ddir, unsigned long long bs,
b2a432bf 2863 unsigned long t, uint64_t offset, uint8_t priority_bit)
3c39a379 2864{
7e419452 2865 struct io_logs *cur_log;
306ddc97 2866
3c568239
JA
2867 if (iolog->disabled)
2868 return;
7e419452 2869 if (flist_empty(&iolog->io_logs))
8355119b 2870 iolog->avg_last[ddir] = t;
b8bc8cba 2871
7e419452
JA
2872 cur_log = get_cur_log(iolog);
2873 if (cur_log) {
2874 struct io_sample *s;
3c39a379 2875
7e419452 2876 s = get_sample(iolog, cur_log, cur_log->nr_samples);
3c39a379 2877
af2fde19 2878 s->data = data;
a9179eeb 2879 s->time = t + (iolog->td ? iolog->td->unix_epoch : 0);
7e419452
JA
2880 io_sample_set_ddir(iolog, s, ddir);
2881 s->bs = bs;
b2a432bf 2882 s->priority_bit = priority_bit;
ae588852 2883
7e419452
JA
2884 if (iolog->log_offset) {
2885 struct io_sample_offset *so = (void *) s;
ae588852 2886
7e419452
JA
2887 so->offset = offset;
2888 }
ae588852 2889
7e419452
JA
2890 cur_log->nr_samples++;
2891 return;
ae588852
JA
2892 }
2893
7e419452 2894 iolog->disabled = true;
3c39a379
JA
2895}
2896
7fb28d36
JA
2897static inline void reset_io_stat(struct io_stat *ios)
2898{
5b8f19b7
JA
2899 ios->min_val = -1ULL;
2900 ios->max_val = ios->samples = 0;
7fb28d36
JA
2901 ios->mean.u.f = ios->S.u.f = 0;
2902}
2903
6bb58215
JA
2904void reset_io_stats(struct thread_data *td)
2905{
2906 struct thread_stat *ts = &td->ts;
df8781b6 2907 int i, j, k;
6bb58215
JA
2908
2909 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
b2a432bf 2910 reset_io_stat(&ts->clat_high_prio_stat[i]);
efd78265 2911 reset_io_stat(&ts->clat_low_prio_stat[i]);
6bb58215
JA
2912 reset_io_stat(&ts->clat_stat[i]);
2913 reset_io_stat(&ts->slat_stat[i]);
2914 reset_io_stat(&ts->lat_stat[i]);
2915 reset_io_stat(&ts->bw_stat[i]);
2916 reset_io_stat(&ts->iops_stat[i]);
2917
2918 ts->io_bytes[i] = 0;
2919 ts->runtime[i] = 0;
71cb78c1
VF
2920 ts->total_io_u[i] = 0;
2921 ts->short_io_u[i] = 0;
2922 ts->drop_io_u[i] = 0;
6bb58215 2923
b2b3eefe 2924 for (j = 0; j < FIO_IO_U_PLAT_NR; j++) {
b2a432bf 2925 ts->io_u_plat_high_prio[i][j] = 0;
efd78265 2926 ts->io_u_plat_low_prio[i][j] = 0;
b2b3eefe
JA
2927 if (!i)
2928 ts->io_u_sync_plat[j] = 0;
2929 }
6bb58215
JA
2930 }
2931
df8781b6
VF
2932 for (i = 0; i < FIO_LAT_CNT; i++)
2933 for (j = 0; j < DDIR_RWDIR_CNT; j++)
2934 for (k = 0; k < FIO_IO_U_PLAT_NR; k++)
2935 ts->io_u_plat[i][j][k] = 0;
2936
7f3ecee2
JA
2937 ts->total_io_u[DDIR_SYNC] = 0;
2938
6bb58215
JA
2939 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
2940 ts->io_u_map[i] = 0;
2941 ts->io_u_submit[i] = 0;
2942 ts->io_u_complete[i] = 0;
71cb78c1
VF
2943 }
2944
d6bb626e
VF
2945 for (i = 0; i < FIO_IO_U_LAT_N_NR; i++)
2946 ts->io_u_lat_n[i] = 0;
71cb78c1 2947 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
6bb58215 2948 ts->io_u_lat_u[i] = 0;
71cb78c1 2949 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
6bb58215 2950 ts->io_u_lat_m[i] = 0;
6bb58215 2951
71cb78c1
VF
2952 ts->total_submit = 0;
2953 ts->total_complete = 0;
fd5d733f 2954 ts->nr_zone_resets = 0;
96563db9 2955 ts->cachehit = ts->cachemiss = 0;
6bb58215
JA
2956}
2957
d96d3bb3 2958static void __add_stat_to_log(struct io_log *iolog, enum fio_ddir ddir,
b2a432bf 2959 unsigned long elapsed, bool log_max, uint8_t priority_bit)
99007068
PO
2960{
2961 /*
2962 * Note an entry in the log. Use the mean from the logged samples,
2963 * making sure to properly round up. Only write a log entry if we
2964 * had actual samples done.
2965 */
d96d3bb3 2966 if (iolog->avg_window[ddir].samples) {
af2fde19 2967 union io_sample_data data;
99007068 2968
e6989e10 2969 if (log_max)
af2fde19 2970 data.val = iolog->avg_window[ddir].max_val;
e6989e10 2971 else
af2fde19 2972 data.val = iolog->avg_window[ddir].mean.u.f + 0.50;
e6989e10 2973
b2a432bf 2974 __add_log_sample(iolog, data, ddir, 0, elapsed, 0, priority_bit);
99007068 2975 }
99007068 2976
d96d3bb3
JA
2977 reset_io_stat(&iolog->avg_window[ddir]);
2978}
99007068 2979
e6989e10 2980static void _add_stat_to_log(struct io_log *iolog, unsigned long elapsed,
b2a432bf 2981 bool log_max, uint8_t priority_bit)
d96d3bb3
JA
2982{
2983 int ddir;
99007068 2984
d96d3bb3 2985 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++)
b2a432bf 2986 __add_stat_to_log(iolog, ddir, elapsed, log_max, priority_bit);
99007068
PO
2987}
2988
674456bf
JF
2989static unsigned long add_log_sample(struct thread_data *td,
2990 struct io_log *iolog,
2991 union io_sample_data data,
5fff9543 2992 enum fio_ddir ddir, unsigned long long bs,
b2a432bf 2993 uint64_t offset, uint8_t priority_bit)
bb3884d8 2994{
7fb28d36 2995 unsigned long elapsed, this_window;
b8bc8cba 2996
ff58fced 2997 if (!ddir_rw(ddir))
d454a205 2998 return 0;
ff58fced 2999
b8bc8cba
JA
3000 elapsed = mtime_since_now(&td->epoch);
3001
3002 /*
3003 * If no time averaging, just add the log sample.
3004 */
3005 if (!iolog->avg_msec) {
b2a432bf 3006 __add_log_sample(iolog, data, ddir, bs, elapsed, offset, priority_bit);
d454a205 3007 return 0;
b8bc8cba
JA
3008 }
3009
3010 /*
3011 * Add the sample. If the time period has passed, then
3012 * add that entry to the log and clear.
3013 */
af2fde19 3014 add_stat_sample(&iolog->avg_window[ddir], data.val);
b8bc8cba 3015
7fb28d36
JA
3016 /*
3017 * If period hasn't passed, adding the above sample is all we
3018 * need to do.
3019 */
8355119b
SW
3020 this_window = elapsed - iolog->avg_last[ddir];
3021 if (elapsed < iolog->avg_last[ddir])
3022 return iolog->avg_last[ddir] - elapsed;
f5a568cf 3023 else if (this_window < iolog->avg_msec) {
674456bf 3024 unsigned long diff = iolog->avg_msec - this_window;
d454a205 3025
b392f36d 3026 if (inline_log(iolog) || diff > LOG_MSEC_SLACK)
d454a205
JA
3027 return diff;
3028 }
b8bc8cba 3029
640150c1 3030 __add_stat_to_log(iolog, ddir, elapsed, td->o.log_max != 0, priority_bit);
b8bc8cba 3031
58a2d29d
JL
3032 iolog->avg_last[ddir] = elapsed - (elapsed % iolog->avg_msec);
3033
d454a205 3034 return iolog->avg_msec;
99007068 3035}
6eaf09d6 3036
a47591e4 3037void finalize_logs(struct thread_data *td, bool unit_logs)
99007068
PO
3038{
3039 unsigned long elapsed;
6eaf09d6 3040
99007068 3041 elapsed = mtime_since_now(&td->epoch);
b8bc8cba 3042
a47591e4 3043 if (td->clat_log && unit_logs)
b2a432bf 3044 _add_stat_to_log(td->clat_log, elapsed, td->o.log_max != 0, 0);
a47591e4 3045 if (td->slat_log && unit_logs)
b2a432bf 3046 _add_stat_to_log(td->slat_log, elapsed, td->o.log_max != 0, 0);
a47591e4 3047 if (td->lat_log && unit_logs)
b2a432bf 3048 _add_stat_to_log(td->lat_log, elapsed, td->o.log_max != 0, 0);
a47591e4 3049 if (td->bw_log && (unit_logs == per_unit_log(td->bw_log)))
b2a432bf 3050 _add_stat_to_log(td->bw_log, elapsed, td->o.log_max != 0, 0);
a47591e4 3051 if (td->iops_log && (unit_logs == per_unit_log(td->iops_log)))
b2a432bf 3052 _add_stat_to_log(td->iops_log, elapsed, td->o.log_max != 0, 0);
bb3884d8
JA
3053}
3054
b2a432bf
PC
3055void add_agg_sample(union io_sample_data data, enum fio_ddir ddir, unsigned long long bs,
3056 uint8_t priority_bit)
bb3884d8 3057{
ff58fced 3058 struct io_log *iolog;
bb3884d8 3059
ff58fced
JA
3060 if (!ddir_rw(ddir))
3061 return;
3062
3063 iolog = agg_io_log[ddir];
b2a432bf 3064 __add_log_sample(iolog, data, ddir, bs, mtime_since_genesis(), 0, priority_bit);
bb3884d8
JA
3065}
3066
b2b3eefe
JA
3067void add_sync_clat_sample(struct thread_stat *ts, unsigned long long nsec)
3068{
3069 unsigned int idx = plat_val_to_idx(nsec);
3070 assert(idx < FIO_IO_U_PLAT_NR);
3071
3072 ts->io_u_sync_plat[idx]++;
3073 add_stat_sample(&ts->sync_stat, nsec);
3074}
3075
56440e63
VF
3076static void add_lat_percentile_sample_noprio(struct thread_stat *ts,
3077 unsigned long long nsec, enum fio_ddir ddir, enum fio_lat lat)
83349190 3078{
d6bb626e 3079 unsigned int idx = plat_val_to_idx(nsec);
83349190
YH
3080 assert(idx < FIO_IO_U_PLAT_NR);
3081
56440e63
VF
3082 ts->io_u_plat[lat][ddir][idx]++;
3083}
b2a432bf 3084
56440e63
VF
3085static void add_lat_percentile_sample(struct thread_stat *ts,
3086 unsigned long long nsec, enum fio_ddir ddir, uint8_t priority_bit,
3087 enum fio_lat lat)
3088{
3089 unsigned int idx = plat_val_to_idx(nsec);
3090
3091 add_lat_percentile_sample_noprio(ts, nsec, ddir, lat);
3092
3093 if (!priority_bit)
efd78265 3094 ts->io_u_plat_low_prio[ddir][idx]++;
56440e63 3095 else
b2a432bf 3096 ts->io_u_plat_high_prio[ddir][idx]++;
83349190
YH
3097}
3098
1e97cce9 3099void add_clat_sample(struct thread_data *td, enum fio_ddir ddir,
5fff9543 3100 unsigned long long nsec, unsigned long long bs,
b2a432bf 3101 uint64_t offset, uint8_t priority_bit)
3c39a379 3102{
26b3a188 3103 const bool needs_lock = td_async_processing(td);
1e613c9c 3104 unsigned long elapsed, this_window;
756867bd 3105 struct thread_stat *ts = &td->ts;
1e613c9c 3106 struct io_log *iolog = td->clat_hist_log;
079ad09b 3107
26b3a188
JA
3108 if (needs_lock)
3109 __td_io_u_lock(td);
75dc383e 3110
d6bb626e 3111 add_stat_sample(&ts->clat_stat[ddir], nsec);
3c39a379 3112
38ec5c51
VF
3113 if (!ts->lat_percentiles) {
3114 if (priority_bit)
3115 add_stat_sample(&ts->clat_high_prio_stat[ddir], nsec);
3116 else
efd78265 3117 add_stat_sample(&ts->clat_low_prio_stat[ddir], nsec);
b2a432bf
PC
3118 }
3119
7b9f733a 3120 if (td->clat_log)
d6bb626e 3121 add_log_sample(td, td->clat_log, sample_val(nsec), ddir, bs,
b2a432bf 3122 offset, priority_bit);
83349190 3123
b2a432bf 3124 if (ts->clat_percentiles) {
56440e63
VF
3125 if (ts->lat_percentiles)
3126 add_lat_percentile_sample_noprio(ts, nsec, ddir, FIO_CLAT);
3127 else
3128 add_lat_percentile_sample(ts, nsec, ddir, priority_bit, FIO_CLAT);
b2a432bf 3129 }
75dc383e 3130
1e613c9c 3131 if (iolog && iolog->hist_msec) {
93168285
JA
3132 struct io_hist *hw = &iolog->hist_window[ddir];
3133
3134 hw->samples++;
1e613c9c 3135 elapsed = mtime_since_now(&td->epoch);
93168285 3136 if (!hw->hist_last)
1e613c9c
KC
3137 hw->hist_last = elapsed;
3138 this_window = elapsed - hw->hist_last;
7a4e480d 3139
1e613c9c 3140 if (this_window >= iolog->hist_msec) {
6cc0e5aa 3141 uint64_t *io_u_plat;
65a4d15c 3142 struct io_u_plat_entry *dst;
93168285 3143
1e613c9c 3144 /*
93168285
JA
3145 * Make a byte-for-byte copy of the latency histogram
3146 * stored in td->ts.io_u_plat[ddir], recording it in a
3147 * log sample. Note that the matching call to free() is
3148 * located in iolog.c after printing this sample to the
3149 * log file.
1e613c9c 3150 */
df8781b6 3151 io_u_plat = (uint64_t *) td->ts.io_u_plat[FIO_CLAT][ddir];
65a4d15c
KC
3152 dst = malloc(sizeof(struct io_u_plat_entry));
3153 memcpy(&(dst->io_u_plat), io_u_plat,
1fb9250b 3154 FIO_IO_U_PLAT_NR * sizeof(uint64_t));
d730bc58 3155 flist_add(&dst->list, &hw->list);
af2fde19 3156 __add_log_sample(iolog, sample_plat(dst), ddir, bs,
b2a432bf 3157 elapsed, offset, priority_bit);
1e613c9c
KC
3158
3159 /*
93168285
JA
3160 * Update the last time we recorded as being now, minus
3161 * any drift in time we encountered before actually
3162 * making the record.
1e613c9c
KC
3163 */
3164 hw->hist_last = elapsed - (this_window - iolog->hist_msec);
3165 hw->samples = 0;
3166 }
3167 }
3168
26b3a188
JA
3169 if (needs_lock)
3170 __td_io_u_unlock(td);
3c39a379
JA
3171}
3172
1e97cce9 3173void add_slat_sample(struct thread_data *td, enum fio_ddir ddir,
56440e63 3174 unsigned long long nsec, unsigned long long bs, uint64_t offset,
b2a432bf 3175 uint8_t priority_bit)
3c39a379 3176{
26b3a188 3177 const bool needs_lock = td_async_processing(td);
756867bd 3178 struct thread_stat *ts = &td->ts;
079ad09b 3179
ff58fced
JA
3180 if (!ddir_rw(ddir))
3181 return;
3182
26b3a188
JA
3183 if (needs_lock)
3184 __td_io_u_lock(td);
75dc383e 3185
56440e63 3186 add_stat_sample(&ts->slat_stat[ddir], nsec);
3c39a379 3187
7b9f733a 3188 if (td->slat_log)
56440e63 3189 add_log_sample(td, td->slat_log, sample_val(nsec), ddir, bs, offset,
b2a432bf 3190 priority_bit);
75dc383e 3191
56440e63
VF
3192 if (ts->slat_percentiles)
3193 add_lat_percentile_sample_noprio(ts, nsec, ddir, FIO_SLAT);
3194
26b3a188
JA
3195 if (needs_lock)
3196 __td_io_u_unlock(td);
3c39a379
JA
3197}
3198
02af0988 3199void add_lat_sample(struct thread_data *td, enum fio_ddir ddir,
5fff9543 3200 unsigned long long nsec, unsigned long long bs,
b2a432bf 3201 uint64_t offset, uint8_t priority_bit)
02af0988 3202{
26b3a188 3203 const bool needs_lock = td_async_processing(td);
02af0988
JA
3204 struct thread_stat *ts = &td->ts;
3205
ff58fced
JA
3206 if (!ddir_rw(ddir))
3207 return;
3208
26b3a188
JA
3209 if (needs_lock)
3210 __td_io_u_lock(td);
75dc383e 3211
d6bb626e 3212 add_stat_sample(&ts->lat_stat[ddir], nsec);
02af0988 3213
7b9f733a 3214 if (td->lat_log)
d6bb626e 3215 add_log_sample(td, td->lat_log, sample_val(nsec), ddir, bs,
b2a432bf 3216 offset, priority_bit);
75dc383e 3217
38ec5c51 3218 if (ts->lat_percentiles) {
56440e63 3219 add_lat_percentile_sample(ts, nsec, ddir, priority_bit, FIO_LAT);
38ec5c51
VF
3220 if (priority_bit)
3221 add_stat_sample(&ts->clat_high_prio_stat[ddir], nsec);
3222 else
efd78265 3223 add_stat_sample(&ts->clat_low_prio_stat[ddir], nsec);
b599759b 3224
38ec5c51 3225 }
26b3a188
JA
3226 if (needs_lock)
3227 __td_io_u_unlock(td);
02af0988
JA
3228}
3229
a47591e4 3230void add_bw_sample(struct thread_data *td, struct io_u *io_u,
d6bb626e 3231 unsigned int bytes, unsigned long long spent)
a47591e4 3232{
26b3a188 3233 const bool needs_lock = td_async_processing(td);
a47591e4
JA
3234 struct thread_stat *ts = &td->ts;
3235 unsigned long rate;
3236
3237 if (spent)
d6bb626e 3238 rate = (unsigned long) (bytes * 1000000ULL / spent);
a47591e4
JA
3239 else
3240 rate = 0;
3241
26b3a188
JA
3242 if (needs_lock)
3243 __td_io_u_lock(td);
a47591e4
JA
3244
3245 add_stat_sample(&ts->bw_stat[io_u->ddir], rate);
3246
3247 if (td->bw_log)
af2fde19 3248 add_log_sample(td, td->bw_log, sample_val(rate), io_u->ddir,
b2a432bf 3249 bytes, io_u->offset, io_u_is_prio(io_u));
a47591e4
JA
3250
3251 td->stat_io_bytes[io_u->ddir] = td->this_io_bytes[io_u->ddir];
26b3a188
JA
3252
3253 if (needs_lock)
3254 __td_io_u_unlock(td);
a47591e4
JA
3255}
3256
8b6a404c
VF
3257static int __add_samples(struct thread_data *td, struct timespec *parent_tv,
3258 struct timespec *t, unsigned int avg_time,
4843277a
JA
3259 uint64_t *this_io_bytes, uint64_t *stat_io_bytes,
3260 struct io_stat *stat, struct io_log *log,
3261 bool is_kb)
3c39a379 3262{
26b3a188 3263 const bool needs_lock = td_async_processing(td);
ff58fced 3264 unsigned long spent, rate;
a47591e4 3265 enum fio_ddir ddir;
674456bf 3266 unsigned long next, next_log;
d454a205 3267
4843277a 3268 next_log = avg_time;
ff58fced 3269
4843277a 3270 spent = mtime_since(parent_tv, t);
58a2d29d 3271 if (spent < avg_time && avg_time - spent > LOG_MSEC_SLACK)
4843277a 3272 return avg_time - spent;
9602d8df 3273
26b3a188
JA
3274 if (needs_lock)
3275 __td_io_u_lock(td);
a9da8ab2 3276
9602d8df 3277 /*
5daa4ebe
JC
3278 * Compute both read and write rates for the interval.
3279 */
c1f50f76 3280 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) {
5daa4ebe
JC
3281 uint64_t delta;
3282
4843277a 3283 delta = this_io_bytes[ddir] - stat_io_bytes[ddir];
5daa4ebe
JC
3284 if (!delta)
3285 continue; /* No entries for interval */
3c39a379 3286
4843277a
JA
3287 if (spent) {
3288 if (is_kb)
3289 rate = delta * 1000 / spent / 1024; /* KiB/s */
3290 else
3291 rate = (delta * 1000) / spent;
3292 } else
0956264f
JA
3293 rate = 0;
3294
4843277a 3295 add_stat_sample(&stat[ddir], rate);
3c39a379 3296
37181709 3297 if (log) {
5fff9543 3298 unsigned long long bs = 0;
66b98c9f
JA
3299
3300 if (td->o.min_bs[ddir] == td->o.max_bs[ddir])
3301 bs = td->o.min_bs[ddir];
3302
b2a432bf 3303 next = add_log_sample(td, log, sample_val(rate), ddir, bs, 0, 0);
d454a205 3304 next_log = min(next_log, next);
66b98c9f 3305 }
5daa4ebe 3306
4843277a 3307 stat_io_bytes[ddir] = this_io_bytes[ddir];
5daa4ebe 3308 }
3c39a379 3309
cdb8308d 3310 *parent_tv = *t;
a47591e4 3311
26b3a188
JA
3312 if (needs_lock)
3313 __td_io_u_unlock(td);
a47591e4 3314
4843277a
JA
3315 if (spent <= avg_time)
3316 next = avg_time;
306fea38 3317 else
4843277a 3318 next = avg_time - (1 + spent - avg_time);
a47591e4 3319
d454a205 3320 return min(next, next_log);
a47591e4
JA
3321}
3322
8b6a404c 3323static int add_bw_samples(struct thread_data *td, struct timespec *t)
4843277a
JA
3324{
3325 return __add_samples(td, &td->bw_sample_time, t, td->o.bw_avg_time,
3326 td->this_io_bytes, td->stat_io_bytes,
3327 td->ts.bw_stat, td->bw_log, true);
3328}
3329
a47591e4
JA
3330void add_iops_sample(struct thread_data *td, struct io_u *io_u,
3331 unsigned int bytes)
3332{
26b3a188 3333 const bool needs_lock = td_async_processing(td);
a47591e4
JA
3334 struct thread_stat *ts = &td->ts;
3335
26b3a188
JA
3336 if (needs_lock)
3337 __td_io_u_lock(td);
a47591e4
JA
3338
3339 add_stat_sample(&ts->iops_stat[io_u->ddir], 1);
3340
3341 if (td->iops_log)
af2fde19 3342 add_log_sample(td, td->iops_log, sample_val(1), io_u->ddir,
b2a432bf 3343 bytes, io_u->offset, io_u_is_prio(io_u));
a47591e4
JA
3344
3345 td->stat_io_blocks[io_u->ddir] = td->this_io_blocks[io_u->ddir];
26b3a188
JA
3346
3347 if (needs_lock)
3348 __td_io_u_unlock(td);
3c39a379 3349}
c8eeb9df 3350
8b6a404c 3351static int add_iops_samples(struct thread_data *td, struct timespec *t)
c8eeb9df 3352{
4843277a
JA
3353 return __add_samples(td, &td->iops_sample_time, t, td->o.iops_avg_time,
3354 td->this_io_blocks, td->stat_io_blocks,
3355 td->ts.iops_stat, td->iops_log, false);
a47591e4
JA
3356}
3357
3358/*
3359 * Returns msecs to next event
3360 */
3361int calc_log_samples(void)
3362{
3363 struct thread_data *td;
58a2d29d 3364 unsigned int next = ~0U, tmp = 0, next_mod = 0, log_avg_msec_min = -1U;
8b6a404c 3365 struct timespec now;
a47591e4 3366 int i;
58a2d29d 3367 long elapsed_time = 0;
a47591e4
JA
3368
3369 fio_gettime(&now, NULL);
3370
3371 for_each_td(td, i) {
58a2d29d
JL
3372 elapsed_time = mtime_since_now(&td->epoch);
3373
8243be59
JA
3374 if (!td->o.stats)
3375 continue;
6a89b401 3376 if (in_ramp_time(td) ||
a47591e4
JA
3377 !(td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING)) {
3378 next = min(td->o.iops_avg_time, td->o.bw_avg_time);
3379 continue;
3380 }
37181709
AH
3381 if (!td->bw_log ||
3382 (td->bw_log && !per_unit_log(td->bw_log))) {
a47591e4 3383 tmp = add_bw_samples(td, &now);
58a2d29d
JL
3384
3385 if (td->bw_log)
3386 log_avg_msec_min = min(log_avg_msec_min, (unsigned int)td->bw_log->avg_msec);
a47591e4 3387 }
37181709
AH
3388 if (!td->iops_log ||
3389 (td->iops_log && !per_unit_log(td->iops_log))) {
a47591e4 3390 tmp = add_iops_samples(td, &now);
58a2d29d
JL
3391
3392 if (td->iops_log)
3393 log_avg_msec_min = min(log_avg_msec_min, (unsigned int)td->iops_log->avg_msec);
a47591e4 3394 }
58a2d29d
JL
3395
3396 if (tmp < next)
3397 next = tmp;
a47591e4
JA
3398 }
3399
58a2d29d
JL
3400 /* if log_avg_msec_min has not been changed, set it to 0 */
3401 if (log_avg_msec_min == -1U)
3402 log_avg_msec_min = 0;
3403
3404 if (log_avg_msec_min == 0)
3405 next_mod = elapsed_time;
3406 else
3407 next_mod = elapsed_time % log_avg_msec_min;
3408
3409 /* correction to keep the time on the log avg msec boundary */
3410 next = min(next, (log_avg_msec_min - next_mod));
3411
a47591e4 3412 return next == ~0U ? 0 : next;
c8eeb9df 3413}
cef9175e
JA
3414
3415void stat_init(void)
3416{
971caeb1 3417 stat_sem = fio_sem_init(FIO_SEM_UNLOCKED);
cef9175e
JA
3418}
3419
3420void stat_exit(void)
3421{
3422 /*
3423 * When we have the mutex, we know out-of-band access to it
3424 * have ended.
3425 */
971caeb1
BVA
3426 fio_sem_down(stat_sem);
3427 fio_sem_remove(stat_sem);
cef9175e 3428}
b2ee7647 3429
b2ee7647
JA
3430/*
3431 * Called from signal handler. Wake up status thread.
3432 */
3433void show_running_run_stats(void)
3434{
a47591e4 3435 helper_do_stat();
b2ee7647 3436}
66347cfa
DE
3437
3438uint32_t *io_u_block_info(struct thread_data *td, struct io_u *io_u)
3439{
3440 /* Ignore io_u's which span multiple blocks--they will just get
3441 * inaccurate counts. */
3442 int idx = (io_u->offset - io_u->file->file_offset)
3443 / td->o.bs[DDIR_TRIM];
3444 uint32_t *info = &td->ts.block_infos[idx];
3445 assert(idx < td->ts.nr_block_infos);
3446 return info;
3447}
5c0abd5e 3448