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