diskutil: Report how many sectors have been read and written
[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);
16be6037 558 if (ddir == DDIR_WRITE || ddir == DDIR_TRIM)
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
75cbc26d
BVA
1033 log_buf(out, " %s: ios=%llu/%llu, sectors=%llu/%llu, "
1034 "merge=%llu/%llu, ticks=%llu/%llu, in_queue=%llu, "
1035 "util=%3.2f%%",
2a2fdab1
L
1036 dus->name,
1037 (unsigned long long) dus->s.ios[0],
1038 (unsigned long long) dus->s.ios[1],
75cbc26d
BVA
1039 (unsigned long long) dus->s.sectors[0],
1040 (unsigned long long) dus->s.sectors[1],
2a2fdab1
L
1041 (unsigned long long) dus->s.merges[0],
1042 (unsigned long long) dus->s.merges[1],
1043 (unsigned long long) dus->s.ticks[0],
1044 (unsigned long long) dus->s.ticks[1],
1045 (unsigned long long) dus->s.time_in_queue,
1046 util);
1047 } else {
1048 log_buf(out, ";%s;%llu;%llu;%llu;%llu;%llu;%llu;%llu;%3.2f%%",
1049 dus->name,
1050 (unsigned long long) dus->s.ios[0],
1051 (unsigned long long) dus->s.ios[1],
1052 (unsigned long long) dus->s.merges[0],
1053 (unsigned long long) dus->s.merges[1],
1054 (unsigned long long) dus->s.ticks[0],
1055 (unsigned long long) dus->s.ticks[1],
1056 (unsigned long long) dus->s.time_in_queue,
1057 util);
1058 }
1059
1060 /*
1061 * If the device has slaves, aggregate the stats for
1062 * those slave devices also.
1063 */
1064 show_agg_stats(agg, terse, out);
1065
1066 if (!terse)
1067 log_buf(out, "\n");
1068}
1069
1070void json_array_add_disk_util(struct disk_util_stat *dus,
1071 struct disk_util_agg *agg, struct json_array *array)
1072{
1073 struct json_object *obj;
1074 double util = 0;
1075
1076 if (dus->s.msec)
1077 util = (double) 100 * dus->s.io_ticks / (double) dus->s.msec;
1078 if (util > 100.0)
1079 util = 100.0;
1080
1081 obj = json_create_object();
1082 json_array_add_value_object(array, obj);
1083
eb2f29b7 1084 json_object_add_value_string(obj, "name", (const char *)dus->name);
2a2fdab1
L
1085 json_object_add_value_int(obj, "read_ios", dus->s.ios[0]);
1086 json_object_add_value_int(obj, "write_ios", dus->s.ios[1]);
1087 json_object_add_value_int(obj, "read_merges", dus->s.merges[0]);
1088 json_object_add_value_int(obj, "write_merges", dus->s.merges[1]);
1089 json_object_add_value_int(obj, "read_ticks", dus->s.ticks[0]);
1090 json_object_add_value_int(obj, "write_ticks", dus->s.ticks[1]);
1091 json_object_add_value_int(obj, "in_queue", dus->s.time_in_queue);
1092 json_object_add_value_float(obj, "util", util);
1093
1094 /*
1095 * If the device has slaves, aggregate the stats for
1096 * those slave devices also.
1097 */
1098 if (!agg->slavecount)
1099 return;
1100 json_object_add_value_int(obj, "aggr_read_ios",
1101 agg->ios[0] / agg->slavecount);
1102 json_object_add_value_int(obj, "aggr_write_ios",
1103 agg->ios[1] / agg->slavecount);
1104 json_object_add_value_int(obj, "aggr_read_merges",
1105 agg->merges[0] / agg->slavecount);
1106 json_object_add_value_int(obj, "aggr_write_merge",
1107 agg->merges[1] / agg->slavecount);
1108 json_object_add_value_int(obj, "aggr_read_ticks",
1109 agg->ticks[0] / agg->slavecount);
1110 json_object_add_value_int(obj, "aggr_write_ticks",
1111 agg->ticks[1] / agg->slavecount);
1112 json_object_add_value_int(obj, "aggr_in_queue",
1113 agg->time_in_queue / agg->slavecount);
1114 json_object_add_value_float(obj, "aggr_util", agg->max_util.u.f);
1115}
1116
1117static void json_object_add_disk_utils(struct json_object *obj,
1118 struct flist_head *head)
1119{
1120 struct json_array *array = json_create_array();
1121 struct flist_head *entry;
1122 struct disk_util *du;
1123
1124 json_object_add_value_array(obj, "disk_util", array);
1125
1126 flist_for_each(entry, head) {
1127 du = flist_entry(entry, struct disk_util, list);
1128
1129 aggregate_slaves_stats(du);
1130 json_array_add_disk_util(&du->dus, &du->agg, array);
1131 }
1132}
1133
1134void show_disk_util(int terse, struct json_object *parent,
1135 struct buf_output *out)
1136{
1137 struct flist_head *entry;
1138 struct disk_util *du;
1139 bool do_json;
1140
1141 if (!is_running_backend())
1142 return;
1143
6dc75062 1144 if (flist_empty(&disk_list))
2a2fdab1 1145 return;
2a2fdab1
L
1146
1147 if ((output_format & FIO_OUTPUT_JSON) && parent)
1148 do_json = true;
1149 else
1150 do_json = false;
1151
1152 if (!terse && !do_json)
1153 log_buf(out, "\nDisk stats (read/write):\n");
1154
6dc75062 1155 if (do_json) {
2a2fdab1 1156 json_object_add_disk_utils(parent, &disk_list);
6dc75062 1157 } else if (output_format & ~(FIO_OUTPUT_JSON | FIO_OUTPUT_JSON_PLUS)) {
2a2fdab1
L
1158 flist_for_each(entry, &disk_list) {
1159 du = flist_entry(entry, struct disk_util, list);
1160
1161 aggregate_slaves_stats(du);
1162 print_disk_util(&du->dus, &du->agg, terse, out);
1163 }
1164 }
1165}
1166
10aa136b 1167static void show_thread_status_normal(struct thread_stat *ts,
a666cab8
JA
1168 struct group_run_stats *rs,
1169 struct buf_output *out)
3c39a379
JA
1170{
1171 double usr_cpu, sys_cpu;
69008999 1172 unsigned long runtime;
71619dc2 1173 double io_u_dist[FIO_IO_U_MAP_NR];
57a64324 1174 time_t time_p;
35326842 1175 char time_buf[32];
3c39a379 1176
9966af7b 1177 if (!ddir_rw_sum(ts->io_bytes) && !ddir_rw_sum(ts->total_io_u))
3c39a379 1178 return;
7a4e480d 1179
33477b4b 1180 memset(time_buf, 0, sizeof(time_buf));
3c39a379 1181
57a64324 1182 time(&time_p);
45054cbe 1183 os_ctime_r((const time_t *) &time_p, time_buf, sizeof(time_buf));
57a64324 1184
5ec10eaa 1185 if (!ts->error) {
a666cab8 1186 log_buf(out, "%s: (groupid=%d, jobs=%d): err=%2d: pid=%d: %s",
5ec10eaa 1187 ts->name, ts->groupid, ts->members,
57a64324 1188 ts->error, (int) ts->pid, time_buf);
5ec10eaa 1189 } else {
a666cab8 1190 log_buf(out, "%s: (groupid=%d, jobs=%d): err=%2d (%s): pid=%d: %s",
5ec10eaa 1191 ts->name, ts->groupid, ts->members,
57a64324
JA
1192 ts->error, ts->verror, (int) ts->pid,
1193 time_buf);
5ec10eaa 1194 }
3c39a379 1195
259e47de 1196 if (strlen(ts->description))
a666cab8 1197 log_buf(out, " Description : [%s]\n", ts->description);
7bdce1bd 1198
dde39f8c
AD
1199 for_each_rw_ddir(ddir) {
1200 if (ts->io_bytes[ddir])
1201 show_ddir_status(rs, ts, ddir, out);
1202 }
3c39a379 1203
5cb8a8cd
BP
1204 if (ts->unified_rw_rep == UNIFIED_BOTH)
1205 show_mixed_ddir_status(rs, ts, out);
1206
a666cab8 1207 show_latencies(ts, out);
7e1773ba 1208
b2b3eefe
JA
1209 if (ts->sync_stat.samples)
1210 show_ddir_status(rs, ts, DDIR_SYNC, out);
1211
756867bd 1212 runtime = ts->total_run_time;
69008999 1213 if (runtime) {
1e97cce9 1214 double runt = (double) runtime;
3c39a379 1215
756867bd
JA
1216 usr_cpu = (double) ts->usr_time * 100 / runt;
1217 sys_cpu = (double) ts->sys_time * 100 / runt;
3c39a379
JA
1218 } else {
1219 usr_cpu = 0;
1220 sys_cpu = 0;
1221 }
1222
a666cab8 1223 log_buf(out, " cpu : usr=%3.2f%%, sys=%3.2f%%, ctx=%llu,"
4e0a8fa2
JA
1224 " majf=%llu, minf=%llu\n", usr_cpu, sys_cpu,
1225 (unsigned long long) ts->ctx,
1226 (unsigned long long) ts->majf,
1227 (unsigned long long) ts->minf);
71619dc2 1228
d79db122 1229 stat_calc_dist(ts->io_u_map, ddir_rw_sum(ts->total_io_u), io_u_dist);
a666cab8 1230 log_buf(out, " IO depths : 1=%3.1f%%, 2=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%,"
5ec10eaa
JA
1231 " 16=%3.1f%%, 32=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
1232 io_u_dist[1], io_u_dist[2],
1233 io_u_dist[3], io_u_dist[4],
1234 io_u_dist[5], io_u_dist[6]);
838bc709
JA
1235
1236 stat_calc_dist(ts->io_u_submit, ts->total_submit, io_u_dist);
a666cab8 1237 log_buf(out, " submit : 0=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%, 16=%3.1f%%,"
838bc709
JA
1238 " 32=%3.1f%%, 64=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
1239 io_u_dist[1], io_u_dist[2],
1240 io_u_dist[3], io_u_dist[4],
1241 io_u_dist[5], io_u_dist[6]);
1242 stat_calc_dist(ts->io_u_complete, ts->total_complete, io_u_dist);
a666cab8 1243 log_buf(out, " complete : 0=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%, 16=%3.1f%%,"
838bc709
JA
1244 " 32=%3.1f%%, 64=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
1245 io_u_dist[1], io_u_dist[2],
1246 io_u_dist[3], io_u_dist[4],
1247 io_u_dist[5], io_u_dist[6]);
7f3ecee2
JA
1248 log_buf(out, " issued rwts: total=%llu,%llu,%llu,%llu"
1249 " short=%llu,%llu,%llu,0"
1250 " dropped=%llu,%llu,%llu,0\n",
4e0a8fa2
JA
1251 (unsigned long long) ts->total_io_u[0],
1252 (unsigned long long) ts->total_io_u[1],
1253 (unsigned long long) ts->total_io_u[2],
7f3ecee2 1254 (unsigned long long) ts->total_io_u[3],
4e0a8fa2
JA
1255 (unsigned long long) ts->short_io_u[0],
1256 (unsigned long long) ts->short_io_u[1],
3bcb9d94
JA
1257 (unsigned long long) ts->short_io_u[2],
1258 (unsigned long long) ts->drop_io_u[0],
1259 (unsigned long long) ts->drop_io_u[1],
1260 (unsigned long long) ts->drop_io_u[2]);
f2bba182 1261 if (ts->continue_on_error) {
a666cab8 1262 log_buf(out, " errors : total=%llu, first_error=%d/<%s>\n",
4e0a8fa2 1263 (unsigned long long)ts->total_err_count,
1ec99eea
JA
1264 ts->first_error,
1265 strerror(ts->first_error));
f2bba182 1266 }
3e260a46 1267 if (ts->latency_depth) {
a666cab8 1268 log_buf(out, " latency : target=%llu, window=%llu, percentile=%.2f%%, depth=%u\n",
3e260a46
JA
1269 (unsigned long long)ts->latency_target,
1270 (unsigned long long)ts->latency_window,
1271 ts->latency_percentile.u.f,
1272 ts->latency_depth);
1273 }
66347cfa
DE
1274
1275 if (ts->nr_block_infos)
1276 show_block_infos(ts->nr_block_infos, ts->block_infos,
a666cab8 1277 ts->percentile_list, out);
d685adfb 1278
bb49c8bd 1279 if (ts->ss_dur)
d685adfb 1280 show_ss_normal(ts, out);
3c39a379
JA
1281}
1282
756867bd 1283static void show_ddir_status_terse(struct thread_stat *ts,
2e545522
NC
1284 struct group_run_stats *rs,
1285 enum fio_ddir ddir, int ver,
1286 struct buf_output *out)
c6ae0a5b 1287{
d6bb626e
VF
1288 unsigned long long min, max, minv, maxv, bw, iops;
1289 unsigned long long *ovals = NULL;
c6ae0a5b 1290 double mean, dev;
d6bb626e 1291 unsigned int len;
a2c95580 1292 int i, bw_stat;
c6ae0a5b 1293
ff58fced
JA
1294 assert(ddir_rw(ddir));
1295
312b4af2
JA
1296 iops = bw = 0;
1297 if (ts->runtime[ddir]) {
1298 uint64_t runt = ts->runtime[ddir];
1299
420b104a 1300 bw = ((1000 * ts->io_bytes[ddir]) / runt) / 1024; /* KiB/s */
312b4af2
JA
1301 iops = (1000 * (uint64_t) ts->total_io_u[ddir]) / runt;
1302 }
c6ae0a5b 1303
a666cab8 1304 log_buf(out, ";%llu;%llu;%llu;%llu",
4e0a8fa2
JA
1305 (unsigned long long) ts->io_bytes[ddir] >> 10, bw, iops,
1306 (unsigned long long) ts->runtime[ddir]);
c6ae0a5b 1307
079ad09b 1308 if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev))
247823cc 1309 log_buf(out, ";%llu;%llu;%f;%f", min/1000, max/1000, mean/1000, dev/1000);
c6ae0a5b 1310 else
d6bb626e 1311 log_buf(out, ";%llu;%llu;%f;%f", 0ULL, 0ULL, 0.0, 0.0);
c6ae0a5b 1312
079ad09b 1313 if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev))
247823cc 1314 log_buf(out, ";%llu;%llu;%f;%f", min/1000, max/1000, mean/1000, dev/1000);
c6ae0a5b 1315 else
d6bb626e 1316 log_buf(out, ";%llu;%llu;%f;%f", 0ULL, 0ULL, 0.0, 0.0);
c6ae0a5b 1317
6dc75062 1318 if (ts->lat_percentiles) {
56440e63
VF
1319 len = calc_clat_percentiles(ts->io_u_plat[FIO_LAT][ddir],
1320 ts->lat_stat[ddir].samples,
1321 ts->percentile_list, &ovals, &maxv,
1322 &minv);
6dc75062 1323 } else if (ts->clat_percentiles) {
df8781b6 1324 len = calc_clat_percentiles(ts->io_u_plat[FIO_CLAT][ddir],
1db92cb6
JA
1325 ts->clat_stat[ddir].samples,
1326 ts->percentile_list, &ovals, &maxv,
1327 &minv);
6dc75062 1328 } else {
1db92cb6 1329 len = 0;
6dc75062
JA
1330 }
1331
1db92cb6
JA
1332 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
1333 if (i >= len) {
a666cab8 1334 log_buf(out, ";0%%=0");
1db92cb6
JA
1335 continue;
1336 }
247823cc 1337 log_buf(out, ";%f%%=%llu", ts->percentile_list[i].u.f, ovals[i]/1000);
1db92cb6 1338 }
2341a37a
K
1339
1340 if (calc_lat(&ts->lat_stat[ddir], &min, &max, &mean, &dev))
247823cc 1341 log_buf(out, ";%llu;%llu;%f;%f", min/1000, max/1000, mean/1000, dev/1000);
2341a37a 1342 else
d6bb626e 1343 log_buf(out, ";%llu;%llu;%f;%f", 0ULL, 0ULL, 0.0, 0.0);
2341a37a 1344
283feb79 1345 free(ovals);
1db92cb6 1346
a2c95580
AH
1347 bw_stat = calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev);
1348 if (bw_stat) {
19d3e967
JA
1349 double p_of_agg = 100.0;
1350
1351 if (rs->agg[ddir]) {
d1707474 1352 p_of_agg = mean * 100 / (double) (rs->agg[ddir] / 1024);
19d3e967
JA
1353 if (p_of_agg > 100.0)
1354 p_of_agg = 100.0;
1355 }
c6ae0a5b 1356
d6bb626e 1357 log_buf(out, ";%llu;%llu;%f%%;%f;%f", min, max, p_of_agg, mean, dev);
6dc75062 1358 } else {
d6bb626e 1359 log_buf(out, ";%llu;%llu;%f%%;%f;%f", 0ULL, 0ULL, 0.0, 0.0, 0.0);
6dc75062 1360 }
a2c95580
AH
1361
1362 if (ver == 5) {
1363 if (bw_stat)
29eb371b 1364 log_buf(out, ";%" PRIu64, (&ts->bw_stat[ddir])->samples);
a2c95580
AH
1365 else
1366 log_buf(out, ";%lu", 0UL);
1367
1368 if (calc_lat(&ts->iops_stat[ddir], &min, &max, &mean, &dev))
29eb371b 1369 log_buf(out, ";%llu;%llu;%f;%f;%" PRIu64, min, max,
a2c95580
AH
1370 mean, dev, (&ts->iops_stat[ddir])->samples);
1371 else
1372 log_buf(out, ";%llu;%llu;%f;%f;%lu", 0ULL, 0ULL, 0.0, 0.0, 0UL);
1373 }
c6ae0a5b
JA
1374}
1375
5cb8a8cd
BP
1376static void show_mixed_ddir_status_terse(struct thread_stat *ts,
1377 struct group_run_stats *rs,
1378 int ver, struct buf_output *out)
1379{
b182f077 1380 struct thread_stat *ts_lcl = gen_mixed_ddir_stats_from_ts(ts);
5cb8a8cd 1381
b182f077
NC
1382 if (ts_lcl)
1383 show_ddir_status_terse(ts_lcl, rs, DDIR_READ, ver, out);
5cb8a8cd 1384
692dec0c 1385 free_clat_prio_stats(ts_lcl);
5cb8a8cd
BP
1386 free(ts_lcl);
1387}
1388
6dc75062
JA
1389static struct json_object *add_ddir_lat_json(struct thread_stat *ts,
1390 uint32_t percentiles,
1391 struct io_stat *lat_stat,
1392 uint64_t *io_u_plat)
56440e63
VF
1393{
1394 char buf[120];
1395 double mean, dev;
1396 unsigned int i, len;
1397 struct json_object *lat_object, *percentile_object, *clat_bins_object;
1398 unsigned long long min, max, maxv, minv, *ovals = NULL;
1399
1400 if (!calc_lat(lat_stat, &min, &max, &mean, &dev)) {
1401 min = max = 0;
1402 mean = dev = 0.0;
1403 }
1404 lat_object = json_create_object();
1405 json_object_add_value_int(lat_object, "min", min);
1406 json_object_add_value_int(lat_object, "max", max);
1407 json_object_add_value_float(lat_object, "mean", mean);
1408 json_object_add_value_float(lat_object, "stddev", dev);
1409 json_object_add_value_int(lat_object, "N", lat_stat->samples);
1410
1411 if (percentiles && lat_stat->samples) {
1412 len = calc_clat_percentiles(io_u_plat, lat_stat->samples,
1413 ts->percentile_list, &ovals, &maxv, &minv);
1414
1415 if (len > FIO_IO_U_LIST_MAX_LEN)
1416 len = FIO_IO_U_LIST_MAX_LEN;
1417
1418 percentile_object = json_create_object();
1419 json_object_add_value_object(lat_object, "percentile", percentile_object);
1420 for (i = 0; i < len; i++) {
1421 snprintf(buf, sizeof(buf), "%f", ts->percentile_list[i].u.f);
1422 json_object_add_value_int(percentile_object, buf, ovals[i]);
1423 }
1424 free(ovals);
1425
1426 if (output_format & FIO_OUTPUT_JSON_PLUS) {
1427 clat_bins_object = json_create_object();
1428 json_object_add_value_object(lat_object, "bins", clat_bins_object);
1429
1430 for(i = 0; i < FIO_IO_U_PLAT_NR; i++)
1431 if (io_u_plat[i]) {
1432 snprintf(buf, sizeof(buf), "%llu", plat_idx_to_val(i));
1433 json_object_add_value_int(clat_bins_object, buf, io_u_plat[i]);
1434 }
1435 }
1436 }
1437
1438 return lat_object;
1439}
1440
cc372b17 1441static void add_ddir_status_json(struct thread_stat *ts,
2e545522
NC
1442 struct group_run_stats *rs, enum fio_ddir ddir,
1443 struct json_object *parent)
cc372b17 1444{
56440e63 1445 unsigned long long min, max;
aedd021d 1446 unsigned long long bw_bytes, bw;
9f68fe3a 1447 double mean, dev, iops;
56440e63 1448 struct json_object *dir_object, *tmp_object;
cc372b17
SL
1449 double p_of_agg = 100.0;
1450
b2b3eefe 1451 assert(ddir_rw(ddir) || ddir_sync(ddir));
cc372b17 1452
5cb8a8cd 1453 if ((ts->unified_rw_rep == UNIFIED_MIXED) && ddir != DDIR_READ)
771e58be
JA
1454 return;
1455
cc372b17 1456 dir_object = json_create_object();
771e58be 1457 json_object_add_value_object(parent,
5cb8a8cd 1458 (ts->unified_rw_rep == UNIFIED_MIXED) ? "mixed" : io_ddir_name(ddir), dir_object);
cc372b17 1459
b2b3eefe
JA
1460 if (ddir_rw(ddir)) {
1461 bw_bytes = 0;
1462 bw = 0;
1463 iops = 0.0;
1464 if (ts->runtime[ddir]) {
1465 uint64_t runt = ts->runtime[ddir];
cc372b17 1466
b2b3eefe
JA
1467 bw_bytes = ((1000 * ts->io_bytes[ddir]) / runt); /* Bytes/s */
1468 bw = bw_bytes / 1024; /* KiB/s */
1469 iops = (1000.0 * (uint64_t) ts->total_io_u[ddir]) / runt;
1470 }
cc372b17 1471
b2b3eefe
JA
1472 json_object_add_value_int(dir_object, "io_bytes", ts->io_bytes[ddir]);
1473 json_object_add_value_int(dir_object, "io_kbytes", ts->io_bytes[ddir] >> 10);
1474 json_object_add_value_int(dir_object, "bw_bytes", bw_bytes);
1475 json_object_add_value_int(dir_object, "bw", bw);
1476 json_object_add_value_float(dir_object, "iops", iops);
1477 json_object_add_value_int(dir_object, "runtime", ts->runtime[ddir]);
1478 json_object_add_value_int(dir_object, "total_ios", ts->total_io_u[ddir]);
1479 json_object_add_value_int(dir_object, "short_ios", ts->short_io_u[ddir]);
1480 json_object_add_value_int(dir_object, "drop_ios", ts->drop_io_u[ddir]);
1481
56440e63
VF
1482 tmp_object = add_ddir_lat_json(ts, ts->slat_percentiles,
1483 &ts->slat_stat[ddir], ts->io_u_plat[FIO_SLAT][ddir]);
b2b3eefe 1484 json_object_add_value_object(dir_object, "slat_ns", tmp_object);
56440e63
VF
1485
1486 tmp_object = add_ddir_lat_json(ts, ts->clat_percentiles,
1487 &ts->clat_stat[ddir], ts->io_u_plat[FIO_CLAT][ddir]);
b2b3eefe 1488 json_object_add_value_object(dir_object, "clat_ns", tmp_object);
cc372b17 1489
56440e63
VF
1490 tmp_object = add_ddir_lat_json(ts, ts->lat_percentiles,
1491 &ts->lat_stat[ddir], ts->io_u_plat[FIO_LAT][ddir]);
b2b3eefe 1492 json_object_add_value_object(dir_object, "lat_ns", tmp_object);
56440e63 1493 } else {
f5ec8123 1494 json_object_add_value_int(dir_object, "total_ios", ts->total_io_u[DDIR_SYNC]);
56440e63
VF
1495 tmp_object = add_ddir_lat_json(ts, ts->lat_percentiles | ts->clat_percentiles,
1496 &ts->sync_stat, ts->io_u_sync_plat);
1497 json_object_add_value_object(dir_object, "lat_ns", tmp_object);
513e37ee
VF
1498 }
1499
56440e63
VF
1500 if (!ddir_rw(ddir))
1501 return;
b2a432bf 1502
1cbbba65
NC
1503 /* Only include per prio stats if there are >= 2 prios with samples */
1504 if (get_nr_prios_with_samples(ts, ddir) >= 2) {
1505 struct json_array *array = json_create_array();
1506 const char *obj_name;
1507 int i;
1508
1509 if (ts->lat_percentiles)
1510 obj_name = "lat_ns";
1511 else
1512 obj_name = "clat_ns";
b2a432bf 1513
1cbbba65 1514 json_object_add_value_array(dir_object, "prios", array);
b2b3eefe 1515
1cbbba65 1516 for (i = 0; i < ts->nr_clat_prio[ddir]; i++) {
a967e54d
DLM
1517 struct json_object *obj;
1518
1519 if (!ts->clat_prio[ddir][i].clat_stat.samples)
1520 continue;
1521
1522 obj = json_create_object();
1523
1524 json_object_add_value_int(obj, "prioclass",
1525 ioprio_class(ts->clat_prio[ddir][i].ioprio));
1526 json_object_add_value_int(obj, "prio",
1527 ioprio(ts->clat_prio[ddir][i].ioprio));
1528
1529 tmp_object = add_ddir_lat_json(ts,
1530 ts->clat_percentiles | ts->lat_percentiles,
1531 &ts->clat_prio[ddir][i].clat_stat,
1532 ts->clat_prio[ddir][i].io_u_plat);
1533 json_object_add_value_object(obj, obj_name, tmp_object);
1534 json_array_add_value_object(array, obj);
1cbbba65 1535 }
cc372b17 1536 }
24cab44e 1537
b9e1b491 1538 if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
5c0abd5e 1539 p_of_agg = convert_agg_kbytes_percent(rs, ddir, mean);
cc372b17
SL
1540 } else {
1541 min = max = 0;
1542 p_of_agg = mean = dev = 0.0;
1543 }
56440e63 1544
cc372b17
SL
1545 json_object_add_value_int(dir_object, "bw_min", min);
1546 json_object_add_value_int(dir_object, "bw_max", max);
a806bf2e 1547 json_object_add_value_float(dir_object, "bw_agg", p_of_agg);
cc372b17
SL
1548 json_object_add_value_float(dir_object, "bw_mean", mean);
1549 json_object_add_value_float(dir_object, "bw_dev", dev);
54c05828
AH
1550 json_object_add_value_int(dir_object, "bw_samples",
1551 (&ts->bw_stat[ddir])->samples);
188b6016
AH
1552
1553 if (!calc_lat(&ts->iops_stat[ddir], &min, &max, &mean, &dev)) {
1554 min = max = 0;
1555 mean = dev = 0.0;
1556 }
1557 json_object_add_value_int(dir_object, "iops_min", min);
1558 json_object_add_value_int(dir_object, "iops_max", max);
1559 json_object_add_value_float(dir_object, "iops_mean", mean);
1560 json_object_add_value_float(dir_object, "iops_stddev", dev);
54c05828
AH
1561 json_object_add_value_int(dir_object, "iops_samples",
1562 (&ts->iops_stat[ddir])->samples);
96563db9
JA
1563
1564 if (ts->cachehit + ts->cachemiss) {
1565 uint64_t total;
1566 double hit;
1567
1568 total = ts->cachehit + ts->cachemiss;
1569 hit = (double) ts->cachehit / (double) total;
1570 hit *= 100.0;
1571 json_object_add_value_float(dir_object, "cachehit", hit);
1572 }
cc372b17
SL
1573}
1574
5cb8a8cd
BP
1575static void add_mixed_ddir_status_json(struct thread_stat *ts,
1576 struct group_run_stats *rs, struct json_object *parent)
1577{
b182f077 1578 struct thread_stat *ts_lcl = gen_mixed_ddir_stats_from_ts(ts);
5cb8a8cd
BP
1579
1580 /* add the aggregated stats to json parent */
b182f077
NC
1581 if (ts_lcl)
1582 add_ddir_status_json(ts_lcl, rs, DDIR_READ, parent);
1583
692dec0c 1584 free_clat_prio_stats(ts_lcl);
5cb8a8cd
BP
1585 free(ts_lcl);
1586}
1587
bef2112b
AH
1588static void show_thread_status_terse_all(struct thread_stat *ts,
1589 struct group_run_stats *rs, int ver,
1590 struct buf_output *out)
4d658652
JA
1591{
1592 double io_u_dist[FIO_IO_U_MAP_NR];
1593 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
1594 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
1595 double usr_cpu, sys_cpu;
1596 int i;
1597
1598 /* General Info */
bef2112b
AH
1599 if (ver == 2)
1600 log_buf(out, "2;%s;%d;%d", ts->name, ts->groupid, ts->error);
1601 else
1602 log_buf(out, "%d;%s;%s;%d;%d", ver, fio_version_string,
1603 ts->name, ts->groupid, ts->error);
c6ae0a5b 1604
5cb8a8cd 1605 /* Log Read Status, or mixed if unified_rw_rep = 1 */
a2c95580 1606 show_ddir_status_terse(ts, rs, DDIR_READ, ver, out);
5cb8a8cd
BP
1607 if (ts->unified_rw_rep != UNIFIED_MIXED) {
1608 /* Log Write Status */
1609 show_ddir_status_terse(ts, rs, DDIR_WRITE, ver, out);
1610 /* Log Trim Status */
1611 if (ver == 2 || ver == 4 || ver == 5)
1612 show_ddir_status_terse(ts, rs, DDIR_TRIM, ver, out);
1613 }
1614 if (ts->unified_rw_rep == UNIFIED_BOTH)
1615 show_mixed_ddir_status_terse(ts, rs, ver, out);
562c2d2f 1616 /* CPU Usage */
756867bd
JA
1617 if (ts->total_run_time) {
1618 double runt = (double) ts->total_run_time;
c6ae0a5b 1619
756867bd
JA
1620 usr_cpu = (double) ts->usr_time * 100 / runt;
1621 sys_cpu = (double) ts->sys_time * 100 / runt;
c6ae0a5b
JA
1622 } else {
1623 usr_cpu = 0;
1624 sys_cpu = 0;
1625 }
1626
a666cab8 1627 log_buf(out, ";%f%%;%f%%;%llu;%llu;%llu", usr_cpu, sys_cpu,
4e0a8fa2
JA
1628 (unsigned long long) ts->ctx,
1629 (unsigned long long) ts->majf,
1630 (unsigned long long) ts->minf);
2270890c 1631
562c2d2f 1632 /* Calc % distribution of IO depths, usecond, msecond latency */
d79db122 1633 stat_calc_dist(ts->io_u_map, ddir_rw_sum(ts->total_io_u), io_u_dist);
247823cc 1634 stat_calc_lat_nu(ts, io_u_lat_u);
04a0feae 1635 stat_calc_lat_m(ts, io_u_lat_m);
2270890c 1636
562c2d2f 1637 /* Only show fixed 7 I/O depth levels*/
a666cab8 1638 log_buf(out, ";%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%",
5ec10eaa
JA
1639 io_u_dist[0], io_u_dist[1], io_u_dist[2], io_u_dist[3],
1640 io_u_dist[4], io_u_dist[5], io_u_dist[6]);
2270890c 1641
562c2d2f 1642 /* Microsecond latency */
04a0feae 1643 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
a666cab8 1644 log_buf(out, ";%3.2f%%", io_u_lat_u[i]);
562c2d2f 1645 /* Millisecond latency */
04a0feae 1646 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
a666cab8 1647 log_buf(out, ";%3.2f%%", io_u_lat_m[i]);
f2f788dd
JA
1648
1649 /* disk util stats, if any */
24a97130 1650 if (ver >= 3 && is_running_backend())
bef2112b 1651 show_disk_util(1, NULL, out);
f2f788dd 1652
562c2d2f 1653 /* Additional output if continue_on_error set - default off*/
f2bba182 1654 if (ts->continue_on_error)
a666cab8 1655 log_buf(out, ";%llu;%d", (unsigned long long) ts->total_err_count, ts->first_error);
2270890c 1656
562c2d2f 1657 /* Additional output if description is set */
d5495f0b
VF
1658 if (strlen(ts->description)) {
1659 if (ver == 2)
1660 log_buf(out, "\n");
a666cab8 1661 log_buf(out, ";%s", ts->description);
d5495f0b 1662 }
946e4276 1663
a666cab8 1664 log_buf(out, "\n");
756867bd
JA
1665}
1666
a89ba4b1 1667static void json_add_job_opts(struct json_object *root, const char *name,
0cf542af 1668 struct flist_head *opt_list)
66e19a38
JA
1669{
1670 struct json_object *dir_object;
1671 struct flist_head *entry;
1672 struct print_option *p;
1673
1674 if (flist_empty(opt_list))
1675 return;
1676
1677 dir_object = json_create_object();
1678 json_object_add_value_object(root, name, dir_object);
1679
1680 flist_for_each(entry, opt_list) {
66e19a38 1681 p = flist_entry(entry, struct print_option, list);
c42b48fe 1682 json_object_add_value_string(dir_object, p->name, p->value);
66e19a38
JA
1683 }
1684}
1685
cc372b17 1686static struct json_object *show_thread_status_json(struct thread_stat *ts,
66e19a38
JA
1687 struct group_run_stats *rs,
1688 struct flist_head *opt_list)
cc372b17
SL
1689{
1690 struct json_object *root, *tmp;
b01af66b 1691 struct jobs_eta *je;
cc372b17 1692 double io_u_dist[FIO_IO_U_MAP_NR];
d6bb626e 1693 double io_u_lat_n[FIO_IO_U_LAT_N_NR];
cc372b17
SL
1694 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
1695 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
1696 double usr_cpu, sys_cpu;
1697 int i;
b01af66b
CJ
1698 size_t size;
1699
cc372b17
SL
1700 root = json_create_object();
1701 json_object_add_value_string(root, "jobname", ts->name);
1702 json_object_add_value_int(root, "groupid", ts->groupid);
1703 json_object_add_value_int(root, "error", ts->error);
1704
b01af66b 1705 /* ETA Info */
c5103619 1706 je = get_jobs_eta(true, &size);
2de615ad
JA
1707 if (je) {
1708 json_object_add_value_int(root, "eta", je->eta_sec);
1709 json_object_add_value_int(root, "elapsed", je->elapsed_sec);
0fc3cb4c 1710 free(je);
2de615ad 1711 }
b01af66b 1712
66e19a38 1713 if (opt_list)
0cf542af 1714 json_add_job_opts(root, "job options", opt_list);
66e19a38 1715
cc372b17
SL
1716 add_ddir_status_json(ts, rs, DDIR_READ, root);
1717 add_ddir_status_json(ts, rs, DDIR_WRITE, root);
f3afa57e 1718 add_ddir_status_json(ts, rs, DDIR_TRIM, root);
b2b3eefe 1719 add_ddir_status_json(ts, rs, DDIR_SYNC, root);
cc372b17 1720
5cb8a8cd
BP
1721 if (ts->unified_rw_rep == UNIFIED_BOTH)
1722 add_mixed_ddir_status_json(ts, rs, root);
1723
cc372b17
SL
1724 /* CPU Usage */
1725 if (ts->total_run_time) {
1726 double runt = (double) ts->total_run_time;
1727
1728 usr_cpu = (double) ts->usr_time * 100 / runt;
1729 sys_cpu = (double) ts->sys_time * 100 / runt;
1730 } else {
1731 usr_cpu = 0;
1732 sys_cpu = 0;
1733 }
bcbb4c6c 1734 json_object_add_value_int(root, "job_runtime", ts->total_run_time);
cc372b17
SL
1735 json_object_add_value_float(root, "usr_cpu", usr_cpu);
1736 json_object_add_value_float(root, "sys_cpu", sys_cpu);
1737 json_object_add_value_int(root, "ctx", ts->ctx);
1738 json_object_add_value_int(root, "majf", ts->majf);
1739 json_object_add_value_int(root, "minf", ts->minf);
1740
ec3e3648 1741 /* Calc % distribution of IO depths */
d79db122 1742 stat_calc_dist(ts->io_u_map, ddir_rw_sum(ts->total_io_u), io_u_dist);
cc372b17
SL
1743 tmp = json_create_object();
1744 json_object_add_value_object(root, "iodepth_level", tmp);
1745 /* Only show fixed 7 I/O depth levels*/
1746 for (i = 0; i < 7; i++) {
1747 char name[20];
1748 if (i < 6)
98ffb8f3 1749 snprintf(name, 20, "%d", 1 << i);
cc372b17 1750 else
98ffb8f3 1751 snprintf(name, 20, ">=%d", 1 << i);
cc372b17
SL
1752 json_object_add_value_float(tmp, (const char *)name, io_u_dist[i]);
1753 }
1754
ec3e3648
VF
1755 /* Calc % distribution of submit IO depths */
1756 stat_calc_dist(ts->io_u_submit, ts->total_submit, io_u_dist);
1757 tmp = json_create_object();
1758 json_object_add_value_object(root, "iodepth_submit", tmp);
1759 /* Only show fixed 7 I/O depth levels*/
1760 for (i = 0; i < 7; i++) {
1761 char name[20];
1762 if (i == 0)
1763 snprintf(name, 20, "0");
1764 else if (i < 6)
1765 snprintf(name, 20, "%d", 1 << (i+1));
1766 else
1767 snprintf(name, 20, ">=%d", 1 << i);
1768 json_object_add_value_float(tmp, (const char *)name, io_u_dist[i]);
1769 }
1770
1771 /* Calc % distribution of completion IO depths */
1772 stat_calc_dist(ts->io_u_complete, ts->total_complete, io_u_dist);
1773 tmp = json_create_object();
1774 json_object_add_value_object(root, "iodepth_complete", tmp);
1775 /* Only show fixed 7 I/O depth levels*/
1776 for (i = 0; i < 7; i++) {
1777 char name[20];
1778 if (i == 0)
1779 snprintf(name, 20, "0");
1780 else if (i < 6)
1781 snprintf(name, 20, "%d", 1 << (i+1));
1782 else
1783 snprintf(name, 20, ">=%d", 1 << i);
1784 json_object_add_value_float(tmp, (const char *)name, io_u_dist[i]);
1785 }
1786
1787 /* Calc % distribution of nsecond, usecond, msecond latency */
1788 stat_calc_dist(ts->io_u_map, ddir_rw_sum(ts->total_io_u), io_u_dist);
1789 stat_calc_lat_n(ts, io_u_lat_n);
1790 stat_calc_lat_u(ts, io_u_lat_u);
1791 stat_calc_lat_m(ts, io_u_lat_m);
1792
d6bb626e 1793 /* Nanosecond latency */
cc372b17 1794 tmp = json_create_object();
d6bb626e
VF
1795 json_object_add_value_object(root, "latency_ns", tmp);
1796 for (i = 0; i < FIO_IO_U_LAT_N_NR; i++) {
1797 const char *ranges[] = { "2", "4", "10", "20", "50", "100",
1798 "250", "500", "750", "1000", };
1799 json_object_add_value_float(tmp, ranges[i], io_u_lat_n[i]);
1800 }
cc372b17 1801 /* Microsecond latency */
d6bb626e
VF
1802 tmp = json_create_object();
1803 json_object_add_value_object(root, "latency_us", tmp);
cc372b17
SL
1804 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
1805 const char *ranges[] = { "2", "4", "10", "20", "50", "100",
1806 "250", "500", "750", "1000", };
1807 json_object_add_value_float(tmp, ranges[i], io_u_lat_u[i]);
1808 }
1809 /* Millisecond latency */
1810 tmp = json_create_object();
1811 json_object_add_value_object(root, "latency_ms", tmp);
1812 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++) {
1813 const char *ranges[] = { "2", "4", "10", "20", "50", "100",
1814 "250", "500", "750", "1000", "2000",
1815 ">=2000", };
1816 json_object_add_value_float(tmp, ranges[i], io_u_lat_m[i]);
1817 }
1818
1819 /* Additional output if continue_on_error set - default off*/
1820 if (ts->continue_on_error) {
1821 json_object_add_value_int(root, "total_err", ts->total_err_count);
952b05e0 1822 json_object_add_value_int(root, "first_error", ts->first_error);
cc372b17
SL
1823 }
1824
3e260a46
JA
1825 if (ts->latency_depth) {
1826 json_object_add_value_int(root, "latency_depth", ts->latency_depth);
1827 json_object_add_value_int(root, "latency_target", ts->latency_target);
1828 json_object_add_value_float(root, "latency_percentile", ts->latency_percentile.u.f);
1829 json_object_add_value_int(root, "latency_window", ts->latency_window);
1830 }
1831
cc372b17
SL
1832 /* Additional output if description is set */
1833 if (strlen(ts->description))
1834 json_object_add_value_string(root, "desc", ts->description);
1835
66347cfa
DE
1836 if (ts->nr_block_infos) {
1837 /* Block error histogram and types */
1838 int len;
1839 unsigned int *percentiles = NULL;
1840 unsigned int block_state_counts[BLOCK_STATE_COUNT];
1841
1842 len = calc_block_percentiles(ts->nr_block_infos, ts->block_infos,
1843 ts->percentile_list,
1844 &percentiles, block_state_counts);
1845
1846 if (len) {
1847 struct json_object *block, *percentile_object, *states;
8a68c41c 1848 int state;
66347cfa
DE
1849 block = json_create_object();
1850 json_object_add_value_object(root, "block", block);
1851
1852 percentile_object = json_create_object();
1853 json_object_add_value_object(block, "percentiles",
1854 percentile_object);
1855 for (i = 0; i < len; i++) {
1856 char buf[20];
1857 snprintf(buf, sizeof(buf), "%f",
1858 ts->percentile_list[i].u.f);
1859 json_object_add_value_int(percentile_object,
c0a6be6c 1860 buf,
66347cfa
DE
1861 percentiles[i]);
1862 }
1863
1864 states = json_create_object();
1865 json_object_add_value_object(block, "states", states);
1866 for (state = 0; state < BLOCK_STATE_COUNT; state++) {
1867 json_object_add_value_int(states,
1868 block_state_names[state],
1869 block_state_counts[state]);
1870 }
1871 free(percentiles);
1872 }
1873 }
1874
bb49c8bd 1875 if (ts->ss_dur) {
ba8fb6f6
VF
1876 struct json_object *data;
1877 struct json_array *iops, *bw;
a43f4461 1878 int j, k, l;
6da94b07 1879 char ss_buf[64];
90e678ba 1880 int intervals = ts->ss_dur / (ss_check_interval / 1000L);
16e56d25 1881
6da94b07 1882 snprintf(ss_buf, sizeof(ss_buf), "%s%s:%f%s",
c8caba48
JA
1883 ts->ss_state & FIO_SS_IOPS ? "iops" : "bw",
1884 ts->ss_state & FIO_SS_SLOPE ? "_slope" : "",
bb49c8bd 1885 (float) ts->ss_limit.u.f,
c8caba48 1886 ts->ss_state & FIO_SS_PCT ? "%" : "");
16e56d25
VF
1887
1888 tmp = json_create_object();
1889 json_object_add_value_object(root, "steadystate", tmp);
6da94b07 1890 json_object_add_value_string(tmp, "ss", ss_buf);
bb49c8bd 1891 json_object_add_value_int(tmp, "duration", (int)ts->ss_dur);
c8caba48 1892 json_object_add_value_int(tmp, "attained", (ts->ss_state & FIO_SS_ATTAINED) > 0);
6da94b07 1893
bb49c8bd 1894 snprintf(ss_buf, sizeof(ss_buf), "%f%s", (float) ts->ss_criterion.u.f,
c8caba48 1895 ts->ss_state & FIO_SS_PCT ? "%" : "");
6da94b07 1896 json_object_add_value_string(tmp, "criterion", ss_buf);
bb49c8bd
VF
1897 json_object_add_value_float(tmp, "max_deviation", ts->ss_deviation.u.f);
1898 json_object_add_value_float(tmp, "slope", ts->ss_slope.u.f);
ba8fb6f6
VF
1899
1900 data = json_create_object();
1901 json_object_add_value_object(tmp, "data", data);
1902 bw = json_create_array();
1903 iops = json_create_array();
412c7d91
VF
1904
1905 /*
1906 ** if ss was attained or the buffer is not full,
1907 ** ss->head points to the first element in the list.
1908 ** otherwise it actually points to the second element
1909 ** in the list
1910 */
c8caba48 1911 if ((ts->ss_state & FIO_SS_ATTAINED) || !(ts->ss_state & FIO_SS_BUFFER_FULL))
bb49c8bd 1912 j = ts->ss_head;
412c7d91 1913 else
90e678ba
CL
1914 j = ts->ss_head == 0 ? intervals - 1 : ts->ss_head - 1;
1915 for (l = 0; l < intervals; l++) {
1916 k = (j + l) % intervals;
bb49c8bd
VF
1917 json_array_add_value_int(bw, ts->ss_bw_data[k]);
1918 json_array_add_value_int(iops, ts->ss_iops_data[k]);
16e56d25 1919 }
bb49c8bd
VF
1920 json_object_add_value_int(data, "bw_mean", steadystate_bw_mean(ts));
1921 json_object_add_value_int(data, "iops_mean", steadystate_iops_mean(ts));
6da94b07
VF
1922 json_object_add_value_array(data, "iops", iops);
1923 json_object_add_value_array(data, "bw", bw);
16e56d25
VF
1924 }
1925
cc372b17
SL
1926 return root;
1927}
1928
4d658652 1929static void show_thread_status_terse(struct thread_stat *ts,
a666cab8
JA
1930 struct group_run_stats *rs,
1931 struct buf_output *out)
4d658652 1932{
a2c95580 1933 if (terse_version >= 2 && terse_version <= 5)
bef2112b 1934 show_thread_status_terse_all(ts, rs, terse_version, out);
4d658652
JA
1935 else
1936 log_err("fio: bad terse version!? %d\n", terse_version);
1937}
1938
952b05e0 1939struct json_object *show_thread_status(struct thread_stat *ts,
a666cab8 1940 struct group_run_stats *rs,
0279b880 1941 struct flist_head *opt_list,
a666cab8 1942 struct buf_output *out)
952b05e0 1943{
129fb2d4
JA
1944 struct json_object *ret = NULL;
1945
1946 if (output_format & FIO_OUTPUT_TERSE)
a666cab8 1947 show_thread_status_terse(ts, rs, out);
129fb2d4 1948 if (output_format & FIO_OUTPUT_JSON)
0279b880 1949 ret = show_thread_status_json(ts, rs, opt_list);
129fb2d4 1950 if (output_format & FIO_OUTPUT_NORMAL)
a666cab8 1951 show_thread_status_normal(ts, rs, out);
129fb2d4
JA
1952
1953 return ret;
952b05e0
CF
1954}
1955
70750d6a 1956static void __sum_stat(struct io_stat *dst, struct io_stat *src, bool first)
756867bd
JA
1957{
1958 double mean, S;
1959
1960 dst->min_val = min(dst->min_val, src->min_val);
1961 dst->max_val = max(dst->max_val, src->max_val);
756867bd
JA
1962
1963 /*
cdcac5cf
YH
1964 * Compute new mean and S after the merge
1965 * <http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
1966 * #Parallel_algorithm>
756867bd 1967 */
fd595830 1968 if (first) {
802ad4a8
JA
1969 mean = src->mean.u.f;
1970 S = src->S.u.f;
756867bd 1971 } else {
802ad4a8 1972 double delta = src->mean.u.f - dst->mean.u.f;
cdcac5cf 1973
802ad4a8
JA
1974 mean = ((src->mean.u.f * src->samples) +
1975 (dst->mean.u.f * dst->samples)) /
cdcac5cf
YH
1976 (dst->samples + src->samples);
1977
802ad4a8 1978 S = src->S.u.f + dst->S.u.f + pow(delta, 2.0) *
cdcac5cf
YH
1979 (dst->samples * src->samples) /
1980 (dst->samples + src->samples);
756867bd
JA
1981 }
1982
cdcac5cf 1983 dst->samples += src->samples;
802ad4a8
JA
1984 dst->mean.u.f = mean;
1985 dst->S.u.f = S;
70750d6a
JA
1986
1987}
1988
1989/*
1990 * We sum two kinds of stats - one that is time based, in which case we
1991 * apply the proper summing technique, and then one that is iops/bw
1992 * numbers. For group_reporting, we should just add those up, not make
1993 * them the mean of everything.
1994 */
016869be 1995static void sum_stat(struct io_stat *dst, struct io_stat *src, bool pure_sum)
70750d6a 1996{
016869be
NC
1997 bool first = dst->samples == 0;
1998
70750d6a
JA
1999 if (src->samples == 0)
2000 return;
2001
2002 if (!pure_sum) {
2003 __sum_stat(dst, src, first);
2004 return;
2005 }
2006
68afa5b5
JA
2007 if (first) {
2008 dst->min_val = src->min_val;
2009 dst->max_val = src->max_val;
2010 dst->samples = src->samples;
2011 dst->mean.u.f = src->mean.u.f;
2012 dst->S.u.f = src->S.u.f;
2013 } else {
2014 dst->min_val += src->min_val;
2015 dst->max_val += src->max_val;
2016 dst->samples += src->samples;
2017 dst->mean.u.f += src->mean.u.f;
2018 dst->S.u.f += src->S.u.f;
2019 }
756867bd
JA
2020}
2021
37f0c1ae
JA
2022void sum_group_stats(struct group_run_stats *dst, struct group_run_stats *src)
2023{
2024 int i;
2025
6eaf09d6 2026 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
37f0c1ae
JA
2027 if (dst->max_run[i] < src->max_run[i])
2028 dst->max_run[i] = src->max_run[i];
2029 if (dst->min_run[i] && dst->min_run[i] > src->min_run[i])
2030 dst->min_run[i] = src->min_run[i];
2031 if (dst->max_bw[i] < src->max_bw[i])
2032 dst->max_bw[i] = src->max_bw[i];
2033 if (dst->min_bw[i] && dst->min_bw[i] > src->min_bw[i])
2034 dst->min_bw[i] = src->min_bw[i];
2035
af7f87cb 2036 dst->iobytes[i] += src->iobytes[i];
37f0c1ae
JA
2037 dst->agg[i] += src->agg[i];
2038 }
2039
dbae1bd6
JA
2040 if (!dst->kb_base)
2041 dst->kb_base = src->kb_base;
2042 if (!dst->unit_base)
2043 dst->unit_base = src->unit_base;
18aa1998
JF
2044 if (!dst->sig_figs)
2045 dst->sig_figs = src->sig_figs;
37f0c1ae
JA
2046}
2047
4ad85649
NC
2048/*
2049 * Free the clat_prio_stat arrays allocated by alloc_clat_prio_stat_ddir().
2050 */
2051void free_clat_prio_stats(struct thread_stat *ts)
2052{
2053 enum fio_ddir ddir;
2054
e23aa817
NC
2055 if (!ts)
2056 return;
2057
4ad85649
NC
2058 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) {
2059 sfree(ts->clat_prio[ddir]);
2060 ts->clat_prio[ddir] = NULL;
2061 ts->nr_clat_prio[ddir] = 0;
2062 }
2063}
2064
2065/*
2066 * Allocate a clat_prio_stat array. The array has to be allocated/freed using
2067 * smalloc/sfree, so that it is accessible by the process/thread summing the
2068 * thread_stats.
2069 */
2070int alloc_clat_prio_stat_ddir(struct thread_stat *ts, enum fio_ddir ddir,
2071 int nr_prios)
2072{
2073 struct clat_prio_stat *clat_prio;
2074 int i;
2075
2076 clat_prio = scalloc(nr_prios, sizeof(*ts->clat_prio[ddir]));
2077 if (!clat_prio) {
2078 log_err("fio: failed to allocate ts clat data\n");
2079 return 1;
2080 }
2081
2082 for (i = 0; i < nr_prios; i++)
2083 clat_prio[i].clat_stat.min_val = ULONG_MAX;
2084
2085 ts->clat_prio[ddir] = clat_prio;
2086 ts->nr_clat_prio[ddir] = nr_prios;
2087
2088 return 0;
2089}
2090
692dec0c
NC
2091static int grow_clat_prio_stat(struct thread_stat *dst, enum fio_ddir ddir)
2092{
2093 int curr_len = dst->nr_clat_prio[ddir];
2094 void *new_arr;
2095
2096 new_arr = scalloc(curr_len + 1, sizeof(*dst->clat_prio[ddir]));
2097 if (!new_arr) {
2098 log_err("fio: failed to grow clat prio array\n");
2099 return 1;
2100 }
2101
2102 memcpy(new_arr, dst->clat_prio[ddir],
2103 curr_len * sizeof(*dst->clat_prio[ddir]));
2104 sfree(dst->clat_prio[ddir]);
2105
2106 dst->clat_prio[ddir] = new_arr;
2107 dst->clat_prio[ddir][curr_len].clat_stat.min_val = ULONG_MAX;
2108 dst->nr_clat_prio[ddir]++;
2109
2110 return 0;
2111}
2112
2113static int find_clat_prio_index(struct thread_stat *dst, enum fio_ddir ddir,
2114 uint32_t ioprio)
2115{
2116 int i, nr_prios = dst->nr_clat_prio[ddir];
2117
2118 for (i = 0; i < nr_prios; i++) {
2119 if (dst->clat_prio[ddir][i].ioprio == ioprio)
2120 return i;
2121 }
2122
2123 return -1;
2124}
2125
2126static int alloc_or_get_clat_prio_index(struct thread_stat *dst,
2127 enum fio_ddir ddir, uint32_t ioprio,
2128 int *idx)
2129{
2130 int index = find_clat_prio_index(dst, ddir, ioprio);
2131
2132 if (index == -1) {
2133 index = dst->nr_clat_prio[ddir];
2134
2135 if (grow_clat_prio_stat(dst, ddir))
2136 return 1;
2137
2138 dst->clat_prio[ddir][index].ioprio = ioprio;
2139 }
2140
2141 *idx = index;
2142
2143 return 0;
2144}
2145
2146static int clat_prio_stats_copy(struct thread_stat *dst, struct thread_stat *src,
2147 enum fio_ddir dst_ddir, enum fio_ddir src_ddir)
2148{
2149 size_t sz = sizeof(*src->clat_prio[src_ddir]) *
2150 src->nr_clat_prio[src_ddir];
2151
2152 dst->clat_prio[dst_ddir] = smalloc(sz);
2153 if (!dst->clat_prio[dst_ddir]) {
2154 log_err("fio: failed to alloc clat prio array\n");
2155 return 1;
2156 }
2157
2158 memcpy(dst->clat_prio[dst_ddir], src->clat_prio[src_ddir], sz);
2159 dst->nr_clat_prio[dst_ddir] = src->nr_clat_prio[src_ddir];
2160
2161 return 0;
2162}
2163
2164static int clat_prio_stat_add_samples(struct thread_stat *dst,
2165 enum fio_ddir dst_ddir, uint32_t ioprio,
2166 struct io_stat *io_stat,
2167 uint64_t *io_u_plat)
2168{
2169 int i, dst_index;
2170
2171 if (!io_stat->samples)
2172 return 0;
2173
2174 if (alloc_or_get_clat_prio_index(dst, dst_ddir, ioprio, &dst_index))
2175 return 1;
2176
2177 sum_stat(&dst->clat_prio[dst_ddir][dst_index].clat_stat, io_stat,
2178 false);
2179
2180 for (i = 0; i < FIO_IO_U_PLAT_NR; i++)
2181 dst->clat_prio[dst_ddir][dst_index].io_u_plat[i] += io_u_plat[i];
2182
2183 return 0;
2184}
2185
2186static int sum_clat_prio_stats_src_single_prio(struct thread_stat *dst,
2187 struct thread_stat *src,
2188 enum fio_ddir dst_ddir,
2189 enum fio_ddir src_ddir)
2190{
2191 struct io_stat *io_stat;
2192 uint64_t *io_u_plat;
2193
2194 /*
2195 * If src ts has no clat_prio_stat array, then all I/Os were submitted
2196 * using src->ioprio. Thus, the global samples in src->clat_stat (or
2197 * src->lat_stat) can be used as the 'per prio' samples for src->ioprio.
2198 */
2199 assert(!src->clat_prio[src_ddir]);
2200 assert(src->nr_clat_prio[src_ddir] == 0);
2201
2202 if (src->lat_percentiles) {
2203 io_u_plat = src->io_u_plat[FIO_LAT][src_ddir];
2204 io_stat = &src->lat_stat[src_ddir];
2205 } else {
2206 io_u_plat = src->io_u_plat[FIO_CLAT][src_ddir];
2207 io_stat = &src->clat_stat[src_ddir];
2208 }
2209
2210 return clat_prio_stat_add_samples(dst, dst_ddir, src->ioprio, io_stat,
2211 io_u_plat);
2212}
2213
2214static int sum_clat_prio_stats_src_multi_prio(struct thread_stat *dst,
2215 struct thread_stat *src,
2216 enum fio_ddir dst_ddir,
2217 enum fio_ddir src_ddir)
2218{
2219 int i;
2220
2221 /*
2222 * If src ts has a clat_prio_stat array, then there are multiple prios
2223 * in use (i.e. src ts had cmdprio_percentage or cmdprio_bssplit set).
2224 * The samples for the default prio will exist in the src->clat_prio
2225 * array, just like the samples for any other prio.
2226 */
2227 assert(src->clat_prio[src_ddir]);
2228 assert(src->nr_clat_prio[src_ddir]);
2229
2230 /* If the dst ts doesn't yet have a clat_prio array, simply memcpy. */
2231 if (!dst->clat_prio[dst_ddir])
2232 return clat_prio_stats_copy(dst, src, dst_ddir, src_ddir);
2233
2234 /* The dst ts already has a clat_prio_array, add src stats into it. */
2235 for (i = 0; i < src->nr_clat_prio[src_ddir]; i++) {
2236 struct io_stat *io_stat = &src->clat_prio[src_ddir][i].clat_stat;
2237 uint64_t *io_u_plat = src->clat_prio[src_ddir][i].io_u_plat;
2238 uint32_t ioprio = src->clat_prio[src_ddir][i].ioprio;
2239
2240 if (clat_prio_stat_add_samples(dst, dst_ddir, ioprio, io_stat, io_u_plat))
2241 return 1;
2242 }
2243
2244 return 0;
2245}
2246
2247static int sum_clat_prio_stats(struct thread_stat *dst, struct thread_stat *src,
2248 enum fio_ddir dst_ddir, enum fio_ddir src_ddir)
2249{
2250 if (dst->disable_prio_stat)
2251 return 0;
2252
2253 if (!src->clat_prio[src_ddir])
2254 return sum_clat_prio_stats_src_single_prio(dst, src, dst_ddir,
2255 src_ddir);
2256
2257 return sum_clat_prio_stats_src_multi_prio(dst, src, dst_ddir, src_ddir);
2258}
2259
016869be 2260void sum_thread_stats(struct thread_stat *dst, struct thread_stat *src)
5b9babb7 2261{
df8781b6 2262 int k, l, m;
5b9babb7 2263
6eaf09d6 2264 for (l = 0; l < DDIR_RWDIR_CNT; l++) {
6dc75062 2265 if (dst->unified_rw_rep != UNIFIED_MIXED) {
016869be 2266 sum_stat(&dst->clat_stat[l], &src->clat_stat[l], false);
016869be
NC
2267 sum_stat(&dst->slat_stat[l], &src->slat_stat[l], false);
2268 sum_stat(&dst->lat_stat[l], &src->lat_stat[l], false);
2269 sum_stat(&dst->bw_stat[l], &src->bw_stat[l], true);
2270 sum_stat(&dst->iops_stat[l], &src->iops_stat[l], true);
692dec0c 2271 sum_clat_prio_stats(dst, src, l, l);
771e58be
JA
2272
2273 dst->io_bytes[l] += src->io_bytes[l];
2274
2275 if (dst->runtime[l] < src->runtime[l])
2276 dst->runtime[l] = src->runtime[l];
2277 } else {
016869be 2278 sum_stat(&dst->clat_stat[0], &src->clat_stat[l], false);
016869be
NC
2279 sum_stat(&dst->slat_stat[0], &src->slat_stat[l], false);
2280 sum_stat(&dst->lat_stat[0], &src->lat_stat[l], false);
2281 sum_stat(&dst->bw_stat[0], &src->bw_stat[l], true);
2282 sum_stat(&dst->iops_stat[0], &src->iops_stat[l], true);
692dec0c 2283 sum_clat_prio_stats(dst, src, 0, l);
771e58be
JA
2284
2285 dst->io_bytes[0] += src->io_bytes[l];
2286
2287 if (dst->runtime[0] < src->runtime[l])
2288 dst->runtime[0] = src->runtime[l];
2289 }
5b9babb7
JA
2290 }
2291
016869be 2292 sum_stat(&dst->sync_stat, &src->sync_stat, false);
5b9babb7
JA
2293 dst->usr_time += src->usr_time;
2294 dst->sys_time += src->sys_time;
2295 dst->ctx += src->ctx;
2296 dst->majf += src->majf;
2297 dst->minf += src->minf;
2298
b2b3eefe 2299 for (k = 0; k < FIO_IO_U_MAP_NR; k++) {
5b9babb7 2300 dst->io_u_map[k] += src->io_u_map[k];
5b9babb7 2301 dst->io_u_submit[k] += src->io_u_submit[k];
5b9babb7 2302 dst->io_u_complete[k] += src->io_u_complete[k];
b2b3eefe 2303 }
bf14b39e
VF
2304
2305 for (k = 0; k < FIO_IO_U_LAT_N_NR; k++)
d6bb626e 2306 dst->io_u_lat_n[k] += src->io_u_lat_n[k];
bf14b39e 2307 for (k = 0; k < FIO_IO_U_LAT_U_NR; k++)
5b9babb7 2308 dst->io_u_lat_u[k] += src->io_u_lat_u[k];
bf14b39e 2309 for (k = 0; k < FIO_IO_U_LAT_M_NR; k++)
5b9babb7 2310 dst->io_u_lat_m[k] += src->io_u_lat_m[k];
bf14b39e 2311
6eaf09d6 2312 for (k = 0; k < DDIR_RWDIR_CNT; k++) {
6dc75062 2313 if (dst->unified_rw_rep != UNIFIED_MIXED) {
771e58be
JA
2314 dst->total_io_u[k] += src->total_io_u[k];
2315 dst->short_io_u[k] += src->short_io_u[k];
3bcb9d94 2316 dst->drop_io_u[k] += src->drop_io_u[k];
771e58be
JA
2317 } else {
2318 dst->total_io_u[0] += src->total_io_u[k];
2319 dst->short_io_u[0] += src->short_io_u[k];
3bcb9d94 2320 dst->drop_io_u[0] += src->drop_io_u[k];
771e58be 2321 }
5b9babb7
JA
2322 }
2323
7f3ecee2
JA
2324 dst->total_io_u[DDIR_SYNC] += src->total_io_u[DDIR_SYNC];
2325
df8781b6
VF
2326 for (k = 0; k < FIO_LAT_CNT; k++)
2327 for (l = 0; l < DDIR_RWDIR_CNT; l++)
2328 for (m = 0; m < FIO_IO_U_PLAT_NR; m++)
6dc75062 2329 if (dst->unified_rw_rep != UNIFIED_MIXED)
df8781b6
VF
2330 dst->io_u_plat[k][l][m] += src->io_u_plat[k][l][m];
2331 else
2332 dst->io_u_plat[k][0][m] += src->io_u_plat[k][l][m];
771e58be 2333
df8781b6
VF
2334 for (k = 0; k < FIO_IO_U_PLAT_NR; k++)
2335 dst->io_u_sync_plat[k] += src->io_u_sync_plat[k];
2336
5b9babb7
JA
2337 dst->total_run_time += src->total_run_time;
2338 dst->total_submit += src->total_submit;
2339 dst->total_complete += src->total_complete;
fd5d733f 2340 dst->nr_zone_resets += src->nr_zone_resets;
96563db9
JA
2341 dst->cachehit += src->cachehit;
2342 dst->cachemiss += src->cachemiss;
5b9babb7
JA
2343}
2344
37f0c1ae
JA
2345void init_group_run_stat(struct group_run_stats *gs)
2346{
6eaf09d6 2347 int i;
37f0c1ae 2348 memset(gs, 0, sizeof(*gs));
6eaf09d6
SL
2349
2350 for (i = 0; i < DDIR_RWDIR_CNT; i++)
2351 gs->min_bw[i] = gs->min_run[i] = ~0UL;
37f0c1ae
JA
2352}
2353
6619fc32 2354void init_thread_stat_min_vals(struct thread_stat *ts)
37f0c1ae 2355{
6619fc32
NC
2356 int i;
2357
2358 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
2359 ts->clat_stat[i].min_val = ULONG_MAX;
2360 ts->slat_stat[i].min_val = ULONG_MAX;
2361 ts->lat_stat[i].min_val = ULONG_MAX;
2362 ts->bw_stat[i].min_val = ULONG_MAX;
2363 ts->iops_stat[i].min_val = ULONG_MAX;
6619fc32
NC
2364 }
2365 ts->sync_stat.min_val = ULONG_MAX;
2366}
37f0c1ae 2367
6619fc32
NC
2368void init_thread_stat(struct thread_stat *ts)
2369{
37f0c1ae
JA
2370 memset(ts, 0, sizeof(*ts));
2371
6619fc32 2372 init_thread_stat_min_vals(ts);
37f0c1ae
JA
2373 ts->groupid = -1;
2374}
2375
691310e2
NC
2376static void init_per_prio_stats(struct thread_stat *threadstats, int nr_ts)
2377{
691310e2
NC
2378 struct thread_stat *ts;
2379 int i, j, last_ts, idx;
2380 enum fio_ddir ddir;
2381
2382 j = 0;
2383 last_ts = -1;
2384 idx = 0;
2385
2386 /*
2387 * Loop through all tds, if a td requires per prio stats, temporarily
2388 * store a 1 in ts->disable_prio_stat, and then do an additional
2389 * loop at the end where we invert the ts->disable_prio_stat values.
2390 */
da8f124f 2391 for_each_td(td) {
691310e2
NC
2392 if (!td->o.stats)
2393 continue;
2394 if (idx &&
2395 (!td->o.group_reporting ||
2396 (td->o.group_reporting && last_ts != td->groupid))) {
2397 idx = 0;
2398 j++;
2399 }
2400
2401 last_ts = td->groupid;
2402 ts = &threadstats[j];
2403
2404 /* idx == 0 means first td in group, or td is not in a group. */
2405 if (idx == 0)
2406 ts->ioprio = td->ioprio;
2407 else if (td->ioprio != ts->ioprio)
2408 ts->disable_prio_stat = 1;
2409
2410 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) {
2411 if (td->ts.clat_prio[ddir]) {
2412 ts->disable_prio_stat = 1;
2413 break;
2414 }
2415 }
2416
2417 idx++;
da8f124f 2418 } end_for_each();
691310e2
NC
2419
2420 /* Loop through all dst threadstats and fixup the values. */
2421 for (i = 0; i < nr_ts; i++) {
2422 ts = &threadstats[i];
2423 ts->disable_prio_stat = !ts->disable_prio_stat;
2424 }
2425}
2426
83f7b64e 2427void __show_run_stats(void)
3c39a379
JA
2428{
2429 struct group_run_stats *runstats, *rs;
756867bd 2430 struct thread_stat *threadstats, *ts;
4da24b69 2431 int i, j, k, nr_ts, last_ts, idx;
8985b491
JA
2432 bool kb_base_warned = false;
2433 bool unit_base_warned = false;
cc372b17
SL
2434 struct json_object *root = NULL;
2435 struct json_array *array = NULL;
a666cab8 2436 struct buf_output output[FIO_OUTPUT_NR];
66e19a38 2437 struct flist_head **opt_lists;
a666cab8 2438
3c39a379
JA
2439 runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1));
2440
37f0c1ae
JA
2441 for (i = 0; i < groupid + 1; i++)
2442 init_group_run_stat(&runstats[i]);
3c39a379 2443
756867bd
JA
2444 /*
2445 * find out how many threads stats we need. if group reporting isn't
2446 * enabled, it's one-per-td.
2447 */
2448 nr_ts = 0;
2449 last_ts = -1;
da8f124f 2450 for_each_td(td) {
2dc1bbeb 2451 if (!td->o.group_reporting) {
756867bd
JA
2452 nr_ts++;
2453 continue;
2454 }
2455 if (last_ts == td->groupid)
2456 continue;
8243be59
JA
2457 if (!td->o.stats)
2458 continue;
756867bd
JA
2459
2460 last_ts = td->groupid;
2461 nr_ts++;
da8f124f 2462 } end_for_each();
756867bd
JA
2463
2464 threadstats = malloc(nr_ts * sizeof(struct thread_stat));
66e19a38 2465 opt_lists = malloc(nr_ts * sizeof(struct flist_head *));
756867bd 2466
66e19a38 2467 for (i = 0; i < nr_ts; i++) {
37f0c1ae 2468 init_thread_stat(&threadstats[i]);
66e19a38
JA
2469 opt_lists[i] = NULL;
2470 }
756867bd 2471
691310e2
NC
2472 init_per_prio_stats(threadstats, nr_ts);
2473
756867bd
JA
2474 j = 0;
2475 last_ts = -1;
197574e4 2476 idx = 0;
da8f124f 2477 for_each_td(td) {
8243be59
JA
2478 if (!td->o.stats)
2479 continue;
2dc1bbeb
JA
2480 if (idx && (!td->o.group_reporting ||
2481 (td->o.group_reporting && last_ts != td->groupid))) {
7abd0e3a
JA
2482 idx = 0;
2483 j++;
2484 }
2485
2486 last_ts = td->groupid;
2487
756867bd
JA
2488 ts = &threadstats[j];
2489
83349190 2490 ts->clat_percentiles = td->o.clat_percentiles;
b599759b 2491 ts->lat_percentiles = td->o.lat_percentiles;
56440e63 2492 ts->slat_percentiles = td->o.slat_percentiles;
435d195a 2493 ts->percentile_precision = td->o.percentile_precision;
fd112d34 2494 memcpy(ts->percentile_list, td->o.percentile_list, sizeof(td->o.percentile_list));
66e19a38 2495 opt_lists[j] = &td->opt_list;
83349190 2496
197574e4 2497 idx++;
756867bd 2498
7abd0e3a 2499 if (ts->groupid == -1) {
2dc84ba7
JA
2500 /*
2501 * These are per-group shared already
2502 */
36833fb0 2503 snprintf(ts->name, sizeof(ts->name), "%s", td->o.name);
a64e88da 2504 if (td->o.description)
36833fb0
BVA
2505 snprintf(ts->description,
2506 sizeof(ts->description), "%s",
2507 td->o.description);
a64e88da 2508 else
4e59d0f3 2509 memset(ts->description, 0, FIO_JOBDESC_SIZE);
a64e88da 2510
2f122b13
JA
2511 /*
2512 * If multiple entries in this group, this is
2513 * the first member.
2514 */
2515 ts->thread_number = td->thread_number;
756867bd 2516 ts->groupid = td->groupid;
2dc84ba7
JA
2517
2518 /*
2519 * first pid in group, not very useful...
2520 */
756867bd 2521 ts->pid = td->pid;
90fef2d1
JA
2522
2523 ts->kb_base = td->o.kb_base;
ad705bcb 2524 ts->unit_base = td->o.unit_base;
e883cb35 2525 ts->sig_figs = td->o.sig_figs;
771e58be 2526 ts->unified_rw_rep = td->o.unified_rw_rep;
90fef2d1
JA
2527 } else if (ts->kb_base != td->o.kb_base && !kb_base_warned) {
2528 log_info("fio: kb_base differs for jobs in group, using"
2529 " %u as the base\n", ts->kb_base);
8985b491 2530 kb_base_warned = true;
ad705bcb
SN
2531 } else if (ts->unit_base != td->o.unit_base && !unit_base_warned) {
2532 log_info("fio: unit_base differs for jobs in group, using"
2533 " %u as the base\n", ts->unit_base);
8985b491 2534 unit_base_warned = true;
2dc84ba7
JA
2535 }
2536
f2bba182
RR
2537 ts->continue_on_error = td->o.continue_on_error;
2538 ts->total_err_count += td->total_err_count;
2539 ts->first_error = td->first_error;
2540 if (!ts->error) {
2541 if (!td->error && td->o.continue_on_error &&
2542 td->first_error) {
2543 ts->error = td->first_error;
36833fb0
BVA
2544 snprintf(ts->verror, sizeof(ts->verror), "%s",
2545 td->verror);
f2bba182
RR
2546 } else if (td->error) {
2547 ts->error = td->error;
36833fb0
BVA
2548 snprintf(ts->verror, sizeof(ts->verror), "%s",
2549 td->verror);
f2bba182 2550 }
756867bd
JA
2551 }
2552
3e260a46
JA
2553 ts->latency_depth = td->latency_qd;
2554 ts->latency_target = td->o.latency_target;
2555 ts->latency_percentile = td->o.latency_percentile;
2556 ts->latency_window = td->o.latency_window;
2557
0e4dd95c 2558 ts->nr_block_infos = td->ts.nr_block_infos;
4da24b69
JA
2559 for (k = 0; k < ts->nr_block_infos; k++)
2560 ts->block_infos[k] = td->ts.block_infos[k];
0e4dd95c 2561
016869be 2562 sum_thread_stats(ts, &td->ts);
16e56d25 2563
a381c018
NC
2564 ts->members++;
2565
bb49c8bd
VF
2566 if (td->o.ss_dur) {
2567 ts->ss_state = td->ss.state;
2568 ts->ss_dur = td->ss.dur;
2569 ts->ss_head = td->ss.head;
bb49c8bd
VF
2570 ts->ss_bw_data = td->ss.bw_data;
2571 ts->ss_iops_data = td->ss.iops_data;
2572 ts->ss_limit.u.f = td->ss.limit;
2573 ts->ss_slope.u.f = td->ss.slope;
2574 ts->ss_deviation.u.f = td->ss.deviation;
2575 ts->ss_criterion.u.f = td->ss.criterion;
2576 }
16e56d25 2577 else
bb49c8bd 2578 ts->ss_dur = ts->ss_state = 0;
da8f124f 2579 } end_for_each();
756867bd
JA
2580
2581 for (i = 0; i < nr_ts; i++) {
94370ac4 2582 unsigned long long bw;
3c39a379 2583
756867bd 2584 ts = &threadstats[i];
dedb88eb
JA
2585 if (ts->groupid == -1)
2586 continue;
756867bd 2587 rs = &runstats[ts->groupid];
90fef2d1 2588 rs->kb_base = ts->kb_base;
ad705bcb 2589 rs->unit_base = ts->unit_base;
e883cb35 2590 rs->sig_figs = ts->sig_figs;
5cb8a8cd 2591 rs->unified_rw_rep |= ts->unified_rw_rep;
3c39a379 2592
6eaf09d6 2593 for (j = 0; j < DDIR_RWDIR_CNT; j++) {
94370ac4
JA
2594 if (!ts->runtime[j])
2595 continue;
2596 if (ts->runtime[j] < rs->min_run[j] || !rs->min_run[j])
2597 rs->min_run[j] = ts->runtime[j];
2598 if (ts->runtime[j] > rs->max_run[j])
2599 rs->max_run[j] = ts->runtime[j];
2600
2601 bw = 0;
af7f87cb
RE
2602 if (ts->runtime[j])
2603 bw = ts->io_bytes[j] * 1000 / ts->runtime[j];
94370ac4
JA
2604 if (bw < rs->min_bw[j])
2605 rs->min_bw[j] = bw;
2606 if (bw > rs->max_bw[j])
2607 rs->max_bw[j] = bw;
2608
af7f87cb 2609 rs->iobytes[j] += ts->io_bytes[j];
94370ac4 2610 }
3c39a379
JA
2611 }
2612
2613 for (i = 0; i < groupid + 1; i++) {
2e545522 2614 enum fio_ddir ddir;
6eaf09d6 2615
3c39a379
JA
2616 rs = &runstats[i];
2617
6eaf09d6
SL
2618 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) {
2619 if (rs->max_run[ddir])
af7f87cb 2620 rs->agg[ddir] = (rs->iobytes[ddir] * 1000) /
6eaf09d6
SL
2621 rs->max_run[ddir];
2622 }
3c39a379
JA
2623 }
2624
a666cab8 2625 for (i = 0; i < FIO_OUTPUT_NR; i++)
e250c0a9 2626 buf_output_init(&output[i]);
a666cab8 2627
3c39a379
JA
2628 /*
2629 * don't overwrite last signal output
2630 */
129fb2d4 2631 if (output_format & FIO_OUTPUT_NORMAL)
a666cab8 2632 log_buf(&output[__FIO_OUTPUT_NORMAL], "\n");
129fb2d4 2633 if (output_format & FIO_OUTPUT_JSON) {
66e19a38 2634 struct thread_data *global;
35326842 2635 char time_buf[32];
aa7d2ef0
RH
2636 struct timeval now;
2637 unsigned long long ms_since_epoch;
afd74e2a 2638 time_t tv_sec;
91e53870 2639
aa7d2ef0
RH
2640 gettimeofday(&now, NULL);
2641 ms_since_epoch = (unsigned long long)(now.tv_sec) * 1000 +
2642 (unsigned long long)(now.tv_usec) / 1000;
2643
afd74e2a
BVA
2644 tv_sec = now.tv_sec;
2645 os_ctime_r(&tv_sec, time_buf, sizeof(time_buf));
1d272416
JA
2646 if (time_buf[strlen(time_buf) - 1] == '\n')
2647 time_buf[strlen(time_buf) - 1] = '\0';
91e53870 2648
cc372b17
SL
2649 root = json_create_object();
2650 json_object_add_value_string(root, "fio version", fio_version_string);
aa7d2ef0
RH
2651 json_object_add_value_int(root, "timestamp", now.tv_sec);
2652 json_object_add_value_int(root, "timestamp_ms", ms_since_epoch);
91e53870 2653 json_object_add_value_string(root, "time", time_buf);
66e19a38 2654 global = get_global_options();
0cf542af 2655 json_add_job_opts(root, "global options", &global->opt_list);
cc372b17
SL
2656 array = json_create_array();
2657 json_object_add_value_array(root, "jobs", array);
2658 }
3c39a379 2659
0279b880
JA
2660 if (is_backend)
2661 fio_server_send_job_options(&get_global_options()->opt_list, -1U);
2662
756867bd
JA
2663 for (i = 0; i < nr_ts; i++) {
2664 ts = &threadstats[i];
2665 rs = &runstats[ts->groupid];
3c39a379 2666
0279b880
JA
2667 if (is_backend) {
2668 fio_server_send_job_options(opt_lists[i], i);
a64e88da 2669 fio_server_send_ts(ts, rs);
0279b880 2670 } else {
129fb2d4 2671 if (output_format & FIO_OUTPUT_TERSE)
a666cab8 2672 show_thread_status_terse(ts, rs, &output[__FIO_OUTPUT_TERSE]);
129fb2d4 2673 if (output_format & FIO_OUTPUT_JSON) {
66e19a38 2674 struct json_object *tmp = show_thread_status_json(ts, rs, opt_lists[i]);
129fb2d4
JA
2675 json_array_add_value_object(array, tmp);
2676 }
2677 if (output_format & FIO_OUTPUT_NORMAL)
a666cab8 2678 show_thread_status_normal(ts, rs, &output[__FIO_OUTPUT_NORMAL]);
129fb2d4 2679 }
3c39a379 2680 }
ab34ddd1 2681 if (!is_backend && (output_format & FIO_OUTPUT_JSON)) {
cc372b17 2682 /* disk util stats, if any */
a666cab8 2683 show_disk_util(1, root, &output[__FIO_OUTPUT_JSON]);
cc372b17 2684
a666cab8 2685 show_idle_prof_stats(FIO_OUTPUT_JSON, root, &output[__FIO_OUTPUT_JSON]);
f2a2ce0e 2686
a666cab8
JA
2687 json_print_object(root, &output[__FIO_OUTPUT_JSON]);
2688 log_buf(&output[__FIO_OUTPUT_JSON], "\n");
cc372b17
SL
2689 json_free_object(root);
2690 }
3c39a379 2691
72c27ff8
JA
2692 for (i = 0; i < groupid + 1; i++) {
2693 rs = &runstats[i];
3c39a379 2694
72c27ff8 2695 rs->groupid = i;
d09a64a0 2696 if (is_backend)
72c27ff8 2697 fio_server_send_gs(rs);
129fb2d4 2698 else if (output_format & FIO_OUTPUT_NORMAL)
a666cab8 2699 show_group_stats(rs, &output[__FIO_OUTPUT_NORMAL]);
c6ae0a5b 2700 }
eecf272f 2701
72c27ff8
JA
2702 if (is_backend)
2703 fio_server_send_du();
129fb2d4 2704 else if (output_format & FIO_OUTPUT_NORMAL) {
a666cab8
JA
2705 show_disk_util(0, NULL, &output[__FIO_OUTPUT_NORMAL]);
2706 show_idle_prof_stats(FIO_OUTPUT_NORMAL, NULL, &output[__FIO_OUTPUT_NORMAL]);
60904003 2707 }
f2a2ce0e 2708
3d57c0e9 2709 for (i = 0; i < FIO_OUTPUT_NR; i++) {
3dc3aa41 2710 struct buf_output *out = &output[i];
8dd0eca3 2711
3dc3aa41 2712 log_info_buf(out->buf, out->buflen);
3dc3aa41 2713 buf_output_free(out);
3d57c0e9 2714 }
2b8c71b0 2715
7ceefc09
FS
2716 fio_idle_prof_cleanup();
2717
fdd5f15f 2718 log_info_flush();
eecf272f 2719 free(runstats);
692dec0c
NC
2720
2721 /* free arrays allocated by sum_thread_stats(), if any */
2722 for (i = 0; i < nr_ts; i++) {
2723 ts = &threadstats[i];
2724 free_clat_prio_stats(ts);
2725 }
756867bd 2726 free(threadstats);
66e19a38 2727 free(opt_lists);
3c39a379
JA
2728}
2729
966f8ef9 2730int __show_running_run_stats(void)
b852e7cf 2731{
b852e7cf 2732 unsigned long long *rt;
8b6a404c 2733 struct timespec ts;
b852e7cf 2734
971caeb1 2735 fio_sem_down(stat_sem);
90811930 2736
b852e7cf 2737 rt = malloc(thread_number * sizeof(unsigned long long));
8b6a404c 2738 fio_gettime(&ts, NULL);
b852e7cf 2739
da8f124f 2740 for_each_td(td) {
61850e56
KM
2741 if (td->runstate >= TD_EXITED)
2742 continue;
2743
c97f1ad6 2744 td->update_rusage = 1;
dde39f8c
AD
2745 for_each_rw_ddir(ddir) {
2746 td->ts.io_bytes[ddir] = td->io_bytes[ddir];
2747 }
8b6a404c 2748 td->ts.total_run_time = mtime_since(&td->epoch, &ts);
6c041a88 2749
da8f124f 2750 rt[__td_index] = mtime_since(&td->start, &ts);
6c041a88 2751 if (td_read(td) && td->ts.io_bytes[DDIR_READ])
da8f124f 2752 td->ts.runtime[DDIR_READ] += rt[__td_index];
6c041a88 2753 if (td_write(td) && td->ts.io_bytes[DDIR_WRITE])
da8f124f 2754 td->ts.runtime[DDIR_WRITE] += rt[__td_index];
6c041a88 2755 if (td_trim(td) && td->ts.io_bytes[DDIR_TRIM])
da8f124f
H
2756 td->ts.runtime[DDIR_TRIM] += rt[__td_index];
2757 } end_for_each();
b852e7cf 2758
da8f124f 2759 for_each_td(td) {
fda2cfac
JA
2760 if (td->runstate >= TD_EXITED)
2761 continue;
c97f1ad6
JA
2762 if (td->rusage_sem) {
2763 td->update_rusage = 1;
971caeb1 2764 fio_sem_down(td->rusage_sem);
c97f1ad6
JA
2765 }
2766 td->update_rusage = 0;
da8f124f 2767 } end_for_each();
c97f1ad6 2768
cef9175e 2769 __show_run_stats();
b852e7cf 2770
da8f124f 2771 for_each_td(td) {
61850e56
KM
2772 if (td->runstate >= TD_EXITED)
2773 continue;
2774
6c041a88 2775 if (td_read(td) && td->ts.io_bytes[DDIR_READ])
da8f124f 2776 td->ts.runtime[DDIR_READ] -= rt[__td_index];
6c041a88 2777 if (td_write(td) && td->ts.io_bytes[DDIR_WRITE])
da8f124f 2778 td->ts.runtime[DDIR_WRITE] -= rt[__td_index];
6c041a88 2779 if (td_trim(td) && td->ts.io_bytes[DDIR_TRIM])
da8f124f
H
2780 td->ts.runtime[DDIR_TRIM] -= rt[__td_index];
2781 } end_for_each();
b852e7cf
JA
2782
2783 free(rt);
971caeb1 2784 fio_sem_up(stat_sem);
966f8ef9
BVA
2785
2786 return 0;
b852e7cf
JA
2787}
2788
8985b491 2789static bool status_file_disabled;
06464907 2790
dac45f23 2791#define FIO_STATUS_FILE "fio-dump-status"
06464907
JA
2792
2793static int check_status_file(void)
2794{
2795 struct stat sb;
a0bafb7d
BC
2796 const char *temp_dir;
2797 char fio_status_file_path[PATH_MAX];
06464907 2798
77d99675
JA
2799 if (status_file_disabled)
2800 return 0;
2801
a0bafb7d 2802 temp_dir = getenv("TMPDIR");
48b16e27 2803 if (temp_dir == NULL) {
a0bafb7d 2804 temp_dir = getenv("TEMP");
48b16e27
JA
2805 if (temp_dir && strlen(temp_dir) >= PATH_MAX)
2806 temp_dir = NULL;
2807 }
a0bafb7d
BC
2808 if (temp_dir == NULL)
2809 temp_dir = "/tmp";
3544731c
BVA
2810#ifdef __COVERITY__
2811 __coverity_tainted_data_sanitize__(temp_dir);
2812#endif
a0bafb7d
BC
2813
2814 snprintf(fio_status_file_path, sizeof(fio_status_file_path), "%s/%s", temp_dir, FIO_STATUS_FILE);
2815
2816 if (stat(fio_status_file_path, &sb))
06464907
JA
2817 return 0;
2818
77d99675
JA
2819 if (unlink(fio_status_file_path) < 0) {
2820 log_err("fio: failed to unlink %s: %s\n", fio_status_file_path,
2821 strerror(errno));
2822 log_err("fio: disabling status file updates\n");
8985b491 2823 status_file_disabled = true;
77d99675
JA
2824 }
2825
06464907
JA
2826 return 1;
2827}
2828
2829void check_for_running_stats(void)
2830{
06464907
JA
2831 if (check_status_file()) {
2832 show_running_run_stats();
2833 return;
2834 }
2835}
2836
d6bb626e 2837static inline void add_stat_sample(struct io_stat *is, unsigned long long data)
3c39a379 2838{
68704084 2839 double val = data;
6660cc67 2840 double delta;
68704084
JA
2841
2842 if (data > is->max_val)
2843 is->max_val = data;
2844 if (data < is->min_val)
2845 is->min_val = data;
2846
802ad4a8 2847 delta = val - is->mean.u.f;
ef11d737 2848 if (delta) {
802ad4a8
JA
2849 is->mean.u.f += delta / (is->samples + 1.0);
2850 is->S.u.f += delta * (val - is->mean.u.f);
ef11d737 2851 }
3c39a379 2852
3c39a379
JA
2853 is->samples++;
2854}
2855
692dec0c
NC
2856static inline void add_stat_prio_sample(struct clat_prio_stat *clat_prio,
2857 unsigned short clat_prio_index,
2858 unsigned long long nsec)
2859{
2860 if (clat_prio)
2861 add_stat_sample(&clat_prio[clat_prio_index].clat_stat, nsec);
2862}
2863
7e419452
JA
2864/*
2865 * Return a struct io_logs, which is added to the tail of the log
2866 * list for 'iolog'.
2867 */
2868static struct io_logs *get_new_log(struct io_log *iolog)
2869{
0a852a50 2870 size_t new_samples;
7e419452
JA
2871 struct io_logs *cur_log;
2872
2873 /*
2874 * Cap the size at MAX_LOG_ENTRIES, so we don't keep doubling
2875 * forever
2876 */
0a852a50 2877 if (!iolog->cur_log_max) {
d7224476
AK
2878 if (iolog->td)
2879 new_samples = iolog->td->o.log_entries;
2880 else
2881 new_samples = DEF_LOG_ENTRIES;
0a852a50 2882 } else {
7e419452
JA
2883 new_samples = iolog->cur_log_max * 2;
2884 if (new_samples > MAX_LOG_ENTRIES)
2885 new_samples = MAX_LOG_ENTRIES;
2886 }
2887
90d2d53f 2888 cur_log = smalloc(sizeof(*cur_log));
7e419452
JA
2889 if (cur_log) {
2890 INIT_FLIST_HEAD(&cur_log->list);
0a852a50 2891 cur_log->log = calloc(new_samples, log_entry_sz(iolog));
7e419452
JA
2892 if (cur_log->log) {
2893 cur_log->nr_samples = 0;
2894 cur_log->max_samples = new_samples;
2895 flist_add_tail(&cur_log->list, &iolog->io_logs);
2896 iolog->cur_log_max = new_samples;
2897 return cur_log;
2898 }
90d2d53f 2899 sfree(cur_log);
7e419452
JA
2900 }
2901
2902 return NULL;
2903}
2904
1fed2080
JA
2905/*
2906 * Add and return a new log chunk, or return current log if big enough
2907 */
2908static struct io_logs *regrow_log(struct io_log *iolog)
7e419452
JA
2909{
2910 struct io_logs *cur_log;
1fed2080 2911 int i;
7e419452 2912
1fed2080 2913 if (!iolog || iolog->disabled)
2ab71dc4 2914 goto disable;
7e419452 2915
1fed2080 2916 cur_log = iolog_cur_log(iolog);
a9afa45a
JA
2917 if (!cur_log) {
2918 cur_log = get_new_log(iolog);
2919 if (!cur_log)
2920 return NULL;
2921 }
2922
7e419452
JA
2923 if (cur_log->nr_samples < cur_log->max_samples)
2924 return cur_log;
2925
2926 /*
2927 * No room for a new sample. If we're compressing on the fly, flush
2928 * out the current chunk
2929 */
2930 if (iolog->log_gz) {
2931 if (iolog_cur_flush(iolog, cur_log)) {
2932 log_err("fio: failed flushing iolog! Will stop logging.\n");
2933 return NULL;
2934 }
2935 }
2936
2937 /*
2938 * Get a new log array, and add to our list
2939 */
2940 cur_log = get_new_log(iolog);
1fed2080
JA
2941 if (!cur_log) {
2942 log_err("fio: failed extending iolog! Will stop logging.\n");
2943 return NULL;
2944 }
2945
2946 if (!iolog->pending || !iolog->pending->nr_samples)
80a24ba9 2947 return cur_log;
7e419452 2948
1fed2080
JA
2949 /*
2950 * Flush pending items to new log
2951 */
2952 for (i = 0; i < iolog->pending->nr_samples; i++) {
2953 struct io_sample *src, *dst;
2954
2955 src = get_sample(iolog, iolog->pending, i);
2956 dst = get_sample(iolog, cur_log, i);
2957 memcpy(dst, src, log_entry_sz(iolog));
2958 }
0b2eef49 2959 cur_log->nr_samples = iolog->pending->nr_samples;
1fed2080
JA
2960
2961 iolog->pending->nr_samples = 0;
2962 return cur_log;
2ab71dc4
JA
2963disable:
2964 if (iolog)
2965 iolog->disabled = true;
2966 return NULL;
1fed2080
JA
2967}
2968
2969void regrow_logs(struct thread_data *td)
2970{
2ab71dc4
JA
2971 regrow_log(td->slat_log);
2972 regrow_log(td->clat_log);
1e613c9c 2973 regrow_log(td->clat_hist_log);
2ab71dc4
JA
2974 regrow_log(td->lat_log);
2975 regrow_log(td->bw_log);
2976 regrow_log(td->iops_log);
1fed2080
JA
2977 td->flags &= ~TD_F_REGROW_LOGS;
2978}
2979
76204de3
PM
2980void regrow_agg_logs(void)
2981{
2982 enum fio_ddir ddir;
2983
2984 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++)
2985 regrow_log(agg_io_log[ddir]);
2986}
2987
1fed2080
JA
2988static struct io_logs *get_cur_log(struct io_log *iolog)
2989{
2990 struct io_logs *cur_log;
2991
2992 cur_log = iolog_cur_log(iolog);
2993 if (!cur_log) {
2994 cur_log = get_new_log(iolog);
2995 if (!cur_log)
2996 return NULL;
2997 }
2998
2999 if (cur_log->nr_samples < cur_log->max_samples)
3000 return cur_log;
3001
3002 /*
1eb467fb
JA
3003 * Out of space. If we're in IO offload mode, or we're not doing
3004 * per unit logging (hence logging happens outside of the IO thread
3005 * as well), add a new log chunk inline. If we're doing inline
3006 * submissions, flag 'td' as needing a log regrow and we'll take
3007 * care of it on the submission side.
1fed2080 3008 */
ab5643cb 3009 if ((iolog->td && iolog->td->o.io_submit_mode == IO_MODE_OFFLOAD) ||
1eb467fb 3010 !per_unit_log(iolog))
1fed2080
JA
3011 return regrow_log(iolog);
3012
ab5643cb
IK
3013 if (iolog->td)
3014 iolog->td->flags |= TD_F_REGROW_LOGS;
3015 if (iolog->pending)
3016 assert(iolog->pending->nr_samples < iolog->pending->max_samples);
1fed2080 3017 return iolog->pending;
7e419452
JA
3018}
3019
af2fde19 3020static void __add_log_sample(struct io_log *iolog, union io_sample_data data,
5fff9543 3021 enum fio_ddir ddir, unsigned long long bs,
03ec570f
DLM
3022 unsigned long t, uint64_t offset,
3023 unsigned int priority)
3c39a379 3024{
7e419452 3025 struct io_logs *cur_log;
306ddc97 3026
3c568239
JA
3027 if (iolog->disabled)
3028 return;
7e419452 3029 if (flist_empty(&iolog->io_logs))
8355119b 3030 iolog->avg_last[ddir] = t;
b8bc8cba 3031
7e419452
JA
3032 cur_log = get_cur_log(iolog);
3033 if (cur_log) {
3034 struct io_sample *s;
3c39a379 3035
7e419452 3036 s = get_sample(iolog, cur_log, cur_log->nr_samples);
3c39a379 3037
af2fde19 3038 s->data = data;
d5b3cfd4 3039 s->time = t + (iolog->td ? iolog->td->alternate_epoch : 0);
7e419452
JA
3040 io_sample_set_ddir(iolog, s, ddir);
3041 s->bs = bs;
03ec570f 3042 s->priority = priority;
ae588852 3043
7e419452
JA
3044 if (iolog->log_offset) {
3045 struct io_sample_offset *so = (void *) s;
ae588852 3046
7e419452
JA
3047 so->offset = offset;
3048 }
ae588852 3049
7e419452
JA
3050 cur_log->nr_samples++;
3051 return;
ae588852
JA
3052 }
3053
7e419452 3054 iolog->disabled = true;
3c39a379
JA
3055}
3056
7fb28d36
JA
3057static inline void reset_io_stat(struct io_stat *ios)
3058{
5b8f19b7
JA
3059 ios->min_val = -1ULL;
3060 ios->max_val = ios->samples = 0;
7fb28d36
JA
3061 ios->mean.u.f = ios->S.u.f = 0;
3062}
3063
2f045d2e
NC
3064static inline void reset_io_u_plat(uint64_t *io_u_plat)
3065{
3066 int i;
3067
3068 for (i = 0; i < FIO_IO_U_PLAT_NR; i++)
3069 io_u_plat[i] = 0;
3070}
3071
692dec0c
NC
3072static inline void reset_clat_prio_stats(struct thread_stat *ts)
3073{
3074 enum fio_ddir ddir;
3075 int i;
3076
3077 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) {
3078 if (!ts->clat_prio[ddir])
3079 continue;
3080
3081 for (i = 0; i < ts->nr_clat_prio[ddir]; i++) {
3082 reset_io_stat(&ts->clat_prio[ddir][i].clat_stat);
3083 reset_io_u_plat(ts->clat_prio[ddir][i].io_u_plat);
3084 }
3085 }
3086}
3087
6bb58215
JA
3088void reset_io_stats(struct thread_data *td)
3089{
3090 struct thread_stat *ts = &td->ts;
2f045d2e 3091 int i, j;
6bb58215
JA
3092
3093 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
3094 reset_io_stat(&ts->clat_stat[i]);
3095 reset_io_stat(&ts->slat_stat[i]);
3096 reset_io_stat(&ts->lat_stat[i]);
3097 reset_io_stat(&ts->bw_stat[i]);
3098 reset_io_stat(&ts->iops_stat[i]);
3099
3100 ts->io_bytes[i] = 0;
3101 ts->runtime[i] = 0;
71cb78c1
VF
3102 ts->total_io_u[i] = 0;
3103 ts->short_io_u[i] = 0;
3104 ts->drop_io_u[i] = 0;
6bb58215
JA
3105 }
3106
df8781b6
VF
3107 for (i = 0; i < FIO_LAT_CNT; i++)
3108 for (j = 0; j < DDIR_RWDIR_CNT; j++)
2f045d2e 3109 reset_io_u_plat(ts->io_u_plat[i][j]);
df8781b6 3110
692dec0c
NC
3111 reset_clat_prio_stats(ts);
3112
7f3ecee2 3113 ts->total_io_u[DDIR_SYNC] = 0;
2f045d2e 3114 reset_io_u_plat(ts->io_u_sync_plat);
7f3ecee2 3115
6bb58215
JA
3116 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
3117 ts->io_u_map[i] = 0;
3118 ts->io_u_submit[i] = 0;
3119 ts->io_u_complete[i] = 0;
71cb78c1
VF
3120 }
3121
d6bb626e
VF
3122 for (i = 0; i < FIO_IO_U_LAT_N_NR; i++)
3123 ts->io_u_lat_n[i] = 0;
71cb78c1 3124 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
6bb58215 3125 ts->io_u_lat_u[i] = 0;
71cb78c1 3126 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
6bb58215 3127 ts->io_u_lat_m[i] = 0;
6bb58215 3128
71cb78c1
VF
3129 ts->total_submit = 0;
3130 ts->total_complete = 0;
fd5d733f 3131 ts->nr_zone_resets = 0;
96563db9 3132 ts->cachehit = ts->cachemiss = 0;
6bb58215
JA
3133}
3134
d96d3bb3 3135static void __add_stat_to_log(struct io_log *iolog, enum fio_ddir ddir,
03ec570f 3136 unsigned long elapsed, bool log_max)
99007068
PO
3137{
3138 /*
3139 * Note an entry in the log. Use the mean from the logged samples,
3140 * making sure to properly round up. Only write a log entry if we
3141 * had actual samples done.
3142 */
d96d3bb3 3143 if (iolog->avg_window[ddir].samples) {
af2fde19 3144 union io_sample_data data;
99007068 3145
e6989e10 3146 if (log_max)
af2fde19 3147 data.val = iolog->avg_window[ddir].max_val;
e6989e10 3148 else
af2fde19 3149 data.val = iolog->avg_window[ddir].mean.u.f + 0.50;
e6989e10 3150
03ec570f 3151 __add_log_sample(iolog, data, ddir, 0, elapsed, 0, 0);
99007068 3152 }
99007068 3153
d96d3bb3
JA
3154 reset_io_stat(&iolog->avg_window[ddir]);
3155}
99007068 3156
e6989e10 3157static void _add_stat_to_log(struct io_log *iolog, unsigned long elapsed,
03ec570f 3158 bool log_max)
d96d3bb3 3159{
2e545522 3160 enum fio_ddir ddir;
99007068 3161
d96d3bb3 3162 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++)
03ec570f 3163 __add_stat_to_log(iolog, ddir, elapsed, log_max);
99007068
PO
3164}
3165
674456bf
JF
3166static unsigned long add_log_sample(struct thread_data *td,
3167 struct io_log *iolog,
3168 union io_sample_data data,
5fff9543 3169 enum fio_ddir ddir, unsigned long long bs,
03ec570f 3170 uint64_t offset, unsigned int ioprio)
bb3884d8 3171{
7fb28d36 3172 unsigned long elapsed, this_window;
b8bc8cba 3173
ff58fced 3174 if (!ddir_rw(ddir))
d454a205 3175 return 0;
ff58fced 3176
b8bc8cba
JA
3177 elapsed = mtime_since_now(&td->epoch);
3178
3179 /*
3180 * If no time averaging, just add the log sample.
3181 */
3182 if (!iolog->avg_msec) {
03ec570f
DLM
3183 __add_log_sample(iolog, data, ddir, bs, elapsed, offset,
3184 ioprio);
d454a205 3185 return 0;
b8bc8cba
JA
3186 }
3187
3188 /*
3189 * Add the sample. If the time period has passed, then
3190 * add that entry to the log and clear.
3191 */
af2fde19 3192 add_stat_sample(&iolog->avg_window[ddir], data.val);
b8bc8cba 3193
7fb28d36
JA
3194 /*
3195 * If period hasn't passed, adding the above sample is all we
3196 * need to do.
3197 */
8355119b
SW
3198 this_window = elapsed - iolog->avg_last[ddir];
3199 if (elapsed < iolog->avg_last[ddir])
3200 return iolog->avg_last[ddir] - elapsed;
f5a568cf 3201 else if (this_window < iolog->avg_msec) {
674456bf 3202 unsigned long diff = iolog->avg_msec - this_window;
d454a205 3203
b392f36d 3204 if (inline_log(iolog) || diff > LOG_MSEC_SLACK)
d454a205
JA
3205 return diff;
3206 }
b8bc8cba 3207
03ec570f 3208 __add_stat_to_log(iolog, ddir, elapsed, td->o.log_max != 0);
b8bc8cba 3209
58a2d29d
JL
3210 iolog->avg_last[ddir] = elapsed - (elapsed % iolog->avg_msec);
3211
d454a205 3212 return iolog->avg_msec;
99007068 3213}
6eaf09d6 3214
a47591e4 3215void finalize_logs(struct thread_data *td, bool unit_logs)
99007068
PO
3216{
3217 unsigned long elapsed;
6eaf09d6 3218
99007068 3219 elapsed = mtime_since_now(&td->epoch);
b8bc8cba 3220
a47591e4 3221 if (td->clat_log && unit_logs)
03ec570f 3222 _add_stat_to_log(td->clat_log, elapsed, td->o.log_max != 0);
a47591e4 3223 if (td->slat_log && unit_logs)
03ec570f 3224 _add_stat_to_log(td->slat_log, elapsed, td->o.log_max != 0);
a47591e4 3225 if (td->lat_log && unit_logs)
03ec570f 3226 _add_stat_to_log(td->lat_log, elapsed, td->o.log_max != 0);
a47591e4 3227 if (td->bw_log && (unit_logs == per_unit_log(td->bw_log)))
03ec570f 3228 _add_stat_to_log(td->bw_log, elapsed, td->o.log_max != 0);
a47591e4 3229 if (td->iops_log && (unit_logs == per_unit_log(td->iops_log)))
03ec570f 3230 _add_stat_to_log(td->iops_log, elapsed, td->o.log_max != 0);
bb3884d8
JA
3231}
3232
03ec570f
DLM
3233void add_agg_sample(union io_sample_data data, enum fio_ddir ddir,
3234 unsigned long long bs)
bb3884d8 3235{
ff58fced 3236 struct io_log *iolog;
bb3884d8 3237
ff58fced
JA
3238 if (!ddir_rw(ddir))
3239 return;
3240
3241 iolog = agg_io_log[ddir];
03ec570f 3242 __add_log_sample(iolog, data, ddir, bs, mtime_since_genesis(), 0, 0);
bb3884d8
JA
3243}
3244
b2b3eefe
JA
3245void add_sync_clat_sample(struct thread_stat *ts, unsigned long long nsec)
3246{
3247 unsigned int idx = plat_val_to_idx(nsec);
3248 assert(idx < FIO_IO_U_PLAT_NR);
3249
3250 ts->io_u_sync_plat[idx]++;
3251 add_stat_sample(&ts->sync_stat, nsec);
3252}
3253
ed7f3a07
NC
3254static inline void add_lat_percentile_sample(struct thread_stat *ts,
3255 unsigned long long nsec,
3256 enum fio_ddir ddir,
3257 enum fio_lat lat)
83349190 3258{
d6bb626e 3259 unsigned int idx = plat_val_to_idx(nsec);
83349190
YH
3260 assert(idx < FIO_IO_U_PLAT_NR);
3261
56440e63
VF
3262 ts->io_u_plat[lat][ddir][idx]++;
3263}
b2a432bf 3264
692dec0c
NC
3265static inline void
3266add_lat_percentile_prio_sample(struct thread_stat *ts, unsigned long long nsec,
3267 enum fio_ddir ddir,
3268 unsigned short clat_prio_index)
56440e63
VF
3269{
3270 unsigned int idx = plat_val_to_idx(nsec);
3271
692dec0c
NC
3272 if (ts->clat_prio[ddir])
3273 ts->clat_prio[ddir][clat_prio_index].io_u_plat[idx]++;
83349190
YH
3274}
3275
1e97cce9 3276void add_clat_sample(struct thread_data *td, enum fio_ddir ddir,
5fff9543 3277 unsigned long long nsec, unsigned long long bs,
692dec0c
NC
3278 uint64_t offset, unsigned int ioprio,
3279 unsigned short clat_prio_index)
3c39a379 3280{
26b3a188 3281 const bool needs_lock = td_async_processing(td);
1e613c9c 3282 unsigned long elapsed, this_window;
756867bd 3283 struct thread_stat *ts = &td->ts;
1e613c9c 3284 struct io_log *iolog = td->clat_hist_log;
079ad09b 3285
26b3a188
JA
3286 if (needs_lock)
3287 __td_io_u_lock(td);
75dc383e 3288
d6bb626e 3289 add_stat_sample(&ts->clat_stat[ddir], nsec);
3c39a379 3290
717569b1 3291 /*
692dec0c 3292 * When lat_percentiles=1 (default 0), the reported per priority
717569b1
NC
3293 * percentiles and stats are used for describing total latency values,
3294 * even though the variable names themselves start with clat_.
3295 *
3296 * Because of the above definition, add a prio stat sample only when
3297 * lat_percentiles=0. add_lat_sample() will add the prio stat sample
3298 * when lat_percentiles=1.
3299 */
692dec0c
NC
3300 if (!ts->lat_percentiles)
3301 add_stat_prio_sample(ts->clat_prio[ddir], clat_prio_index,
3302 nsec);
b2a432bf 3303
7b9f733a 3304 if (td->clat_log)
d6bb626e 3305 add_log_sample(td, td->clat_log, sample_val(nsec), ddir, bs,
03ec570f 3306 offset, ioprio);
83349190 3307
b2a432bf 3308 if (ts->clat_percentiles) {
717569b1
NC
3309 /*
3310 * Because of the above definition, add a prio lat percentile
3311 * sample only when lat_percentiles=0. add_lat_sample() will add
3312 * the prio lat percentile sample when lat_percentiles=1.
3313 */
bf65f7c8
NC
3314 add_lat_percentile_sample(ts, nsec, ddir, FIO_CLAT);
3315 if (!ts->lat_percentiles)
3316 add_lat_percentile_prio_sample(ts, nsec, ddir,
692dec0c 3317 clat_prio_index);
b2a432bf 3318 }
75dc383e 3319
1e613c9c 3320 if (iolog && iolog->hist_msec) {
93168285
JA
3321 struct io_hist *hw = &iolog->hist_window[ddir];
3322
3323 hw->samples++;
1e613c9c 3324 elapsed = mtime_since_now(&td->epoch);
93168285 3325 if (!hw->hist_last)
1e613c9c
KC
3326 hw->hist_last = elapsed;
3327 this_window = elapsed - hw->hist_last;
7a4e480d 3328
1e613c9c 3329 if (this_window >= iolog->hist_msec) {
6cc0e5aa 3330 uint64_t *io_u_plat;
65a4d15c 3331 struct io_u_plat_entry *dst;
93168285 3332
1e613c9c 3333 /*
93168285
JA
3334 * Make a byte-for-byte copy of the latency histogram
3335 * stored in td->ts.io_u_plat[ddir], recording it in a
3336 * log sample. Note that the matching call to free() is
3337 * located in iolog.c after printing this sample to the
3338 * log file.
1e613c9c 3339 */
df8781b6 3340 io_u_plat = (uint64_t *) td->ts.io_u_plat[FIO_CLAT][ddir];
65a4d15c
KC
3341 dst = malloc(sizeof(struct io_u_plat_entry));
3342 memcpy(&(dst->io_u_plat), io_u_plat,
1fb9250b 3343 FIO_IO_U_PLAT_NR * sizeof(uint64_t));
d730bc58 3344 flist_add(&dst->list, &hw->list);
af2fde19 3345 __add_log_sample(iolog, sample_plat(dst), ddir, bs,
03ec570f 3346 elapsed, offset, ioprio);
1e613c9c
KC
3347
3348 /*
93168285
JA
3349 * Update the last time we recorded as being now, minus
3350 * any drift in time we encountered before actually
3351 * making the record.
1e613c9c
KC
3352 */
3353 hw->hist_last = elapsed - (this_window - iolog->hist_msec);
3354 hw->samples = 0;
3355 }
3356 }
3357
26b3a188
JA
3358 if (needs_lock)
3359 __td_io_u_unlock(td);
3c39a379
JA
3360}
3361
1e97cce9 3362void add_slat_sample(struct thread_data *td, enum fio_ddir ddir,
03ec570f
DLM
3363 unsigned long long nsec, unsigned long long bs,
3364 uint64_t offset, unsigned int ioprio)
3c39a379 3365{
26b3a188 3366 const bool needs_lock = td_async_processing(td);
756867bd 3367 struct thread_stat *ts = &td->ts;
079ad09b 3368
ff58fced
JA
3369 if (!ddir_rw(ddir))
3370 return;
3371
26b3a188
JA
3372 if (needs_lock)
3373 __td_io_u_lock(td);
75dc383e 3374
56440e63 3375 add_stat_sample(&ts->slat_stat[ddir], nsec);
3c39a379 3376
7b9f733a 3377 if (td->slat_log)
03ec570f
DLM
3378 add_log_sample(td, td->slat_log, sample_val(nsec), ddir, bs,
3379 offset, ioprio);
75dc383e 3380
56440e63 3381 if (ts->slat_percentiles)
3dd92a75 3382 add_lat_percentile_sample(ts, nsec, ddir, FIO_SLAT);
56440e63 3383
26b3a188
JA
3384 if (needs_lock)
3385 __td_io_u_unlock(td);
3c39a379
JA
3386}
3387
02af0988 3388void add_lat_sample(struct thread_data *td, enum fio_ddir ddir,
5fff9543 3389 unsigned long long nsec, unsigned long long bs,
692dec0c
NC
3390 uint64_t offset, unsigned int ioprio,
3391 unsigned short clat_prio_index)
02af0988 3392{
26b3a188 3393 const bool needs_lock = td_async_processing(td);
02af0988
JA
3394 struct thread_stat *ts = &td->ts;
3395
ff58fced
JA
3396 if (!ddir_rw(ddir))
3397 return;
3398
26b3a188
JA
3399 if (needs_lock)
3400 __td_io_u_lock(td);
75dc383e 3401
d6bb626e 3402 add_stat_sample(&ts->lat_stat[ddir], nsec);
02af0988 3403
7b9f733a 3404 if (td->lat_log)
d6bb626e 3405 add_log_sample(td, td->lat_log, sample_val(nsec), ddir, bs,
03ec570f 3406 offset, ioprio);
75dc383e 3407
717569b1 3408 /*
692dec0c 3409 * When lat_percentiles=1 (default 0), the reported per priority
717569b1
NC
3410 * percentiles and stats are used for describing total latency values,
3411 * even though the variable names themselves start with clat_.
3412 *
3413 * Because of the above definition, add a prio stat and prio lat
3414 * percentile sample only when lat_percentiles=1. add_clat_sample() will
3415 * add the prio stat and prio lat percentile sample when
3416 * lat_percentiles=0.
3417 */
38ec5c51 3418 if (ts->lat_percentiles) {
bf65f7c8 3419 add_lat_percentile_sample(ts, nsec, ddir, FIO_LAT);
692dec0c
NC
3420 add_lat_percentile_prio_sample(ts, nsec, ddir, clat_prio_index);
3421 add_stat_prio_sample(ts->clat_prio[ddir], clat_prio_index,
3422 nsec);
38ec5c51 3423 }
26b3a188
JA
3424 if (needs_lock)
3425 __td_io_u_unlock(td);
02af0988
JA
3426}
3427
a47591e4 3428void add_bw_sample(struct thread_data *td, struct io_u *io_u,
d6bb626e 3429 unsigned int bytes, unsigned long long spent)
a47591e4 3430{
26b3a188 3431 const bool needs_lock = td_async_processing(td);
a47591e4
JA
3432 struct thread_stat *ts = &td->ts;
3433 unsigned long rate;
3434
3435 if (spent)
d6bb626e 3436 rate = (unsigned long) (bytes * 1000000ULL / spent);
a47591e4
JA
3437 else
3438 rate = 0;
3439
26b3a188
JA
3440 if (needs_lock)
3441 __td_io_u_lock(td);
a47591e4
JA
3442
3443 add_stat_sample(&ts->bw_stat[io_u->ddir], rate);
3444
3445 if (td->bw_log)
af2fde19 3446 add_log_sample(td, td->bw_log, sample_val(rate), io_u->ddir,
03ec570f 3447 bytes, io_u->offset, io_u->ioprio);
a47591e4
JA
3448
3449 td->stat_io_bytes[io_u->ddir] = td->this_io_bytes[io_u->ddir];
26b3a188
JA
3450
3451 if (needs_lock)
3452 __td_io_u_unlock(td);
a47591e4
JA
3453}
3454
8b6a404c
VF
3455static int __add_samples(struct thread_data *td, struct timespec *parent_tv,
3456 struct timespec *t, unsigned int avg_time,
4843277a
JA
3457 uint64_t *this_io_bytes, uint64_t *stat_io_bytes,
3458 struct io_stat *stat, struct io_log *log,
3459 bool is_kb)
3c39a379 3460{
26b3a188 3461 const bool needs_lock = td_async_processing(td);
ff58fced 3462 unsigned long spent, rate;
a47591e4 3463 enum fio_ddir ddir;
674456bf 3464 unsigned long next, next_log;
d454a205 3465
4843277a 3466 next_log = avg_time;
ff58fced 3467
4843277a 3468 spent = mtime_since(parent_tv, t);
58a2d29d 3469 if (spent < avg_time && avg_time - spent > LOG_MSEC_SLACK)
4843277a 3470 return avg_time - spent;
9602d8df 3471
26b3a188
JA
3472 if (needs_lock)
3473 __td_io_u_lock(td);
a9da8ab2 3474
9602d8df 3475 /*
5daa4ebe
JC
3476 * Compute both read and write rates for the interval.
3477 */
c1f50f76 3478 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) {
5daa4ebe
JC
3479 uint64_t delta;
3480
4843277a 3481 delta = this_io_bytes[ddir] - stat_io_bytes[ddir];
5daa4ebe
JC
3482 if (!delta)
3483 continue; /* No entries for interval */
3c39a379 3484
4843277a
JA
3485 if (spent) {
3486 if (is_kb)
3487 rate = delta * 1000 / spent / 1024; /* KiB/s */
3488 else
3489 rate = (delta * 1000) / spent;
3490 } else
0956264f
JA
3491 rate = 0;
3492
4843277a 3493 add_stat_sample(&stat[ddir], rate);
3c39a379 3494
37181709 3495 if (log) {
5fff9543 3496 unsigned long long bs = 0;
66b98c9f
JA
3497
3498 if (td->o.min_bs[ddir] == td->o.max_bs[ddir])
3499 bs = td->o.min_bs[ddir];
3500
03ec570f
DLM
3501 next = add_log_sample(td, log, sample_val(rate), ddir,
3502 bs, 0, 0);
d454a205 3503 next_log = min(next_log, next);
66b98c9f 3504 }
5daa4ebe 3505
4843277a 3506 stat_io_bytes[ddir] = this_io_bytes[ddir];
5daa4ebe 3507 }
3c39a379 3508
cdb8308d 3509 *parent_tv = *t;
a47591e4 3510
26b3a188
JA
3511 if (needs_lock)
3512 __td_io_u_unlock(td);
a47591e4 3513
4843277a
JA
3514 if (spent <= avg_time)
3515 next = avg_time;
306fea38 3516 else
4843277a 3517 next = avg_time - (1 + spent - avg_time);
a47591e4 3518
d454a205 3519 return min(next, next_log);
a47591e4
JA
3520}
3521
8b6a404c 3522static int add_bw_samples(struct thread_data *td, struct timespec *t)
4843277a
JA
3523{
3524 return __add_samples(td, &td->bw_sample_time, t, td->o.bw_avg_time,
3525 td->this_io_bytes, td->stat_io_bytes,
3526 td->ts.bw_stat, td->bw_log, true);
3527}
3528
a47591e4
JA
3529void add_iops_sample(struct thread_data *td, struct io_u *io_u,
3530 unsigned int bytes)
3531{
26b3a188 3532 const bool needs_lock = td_async_processing(td);
a47591e4
JA
3533 struct thread_stat *ts = &td->ts;
3534
26b3a188
JA
3535 if (needs_lock)
3536 __td_io_u_lock(td);
a47591e4
JA
3537
3538 add_stat_sample(&ts->iops_stat[io_u->ddir], 1);
3539
3540 if (td->iops_log)
af2fde19 3541 add_log_sample(td, td->iops_log, sample_val(1), io_u->ddir,
03ec570f 3542 bytes, io_u->offset, io_u->ioprio);
a47591e4
JA
3543
3544 td->stat_io_blocks[io_u->ddir] = td->this_io_blocks[io_u->ddir];
26b3a188
JA
3545
3546 if (needs_lock)
3547 __td_io_u_unlock(td);
3c39a379 3548}
c8eeb9df 3549
8b6a404c 3550static int add_iops_samples(struct thread_data *td, struct timespec *t)
c8eeb9df 3551{
4843277a
JA
3552 return __add_samples(td, &td->iops_sample_time, t, td->o.iops_avg_time,
3553 td->this_io_blocks, td->stat_io_blocks,
3554 td->ts.iops_stat, td->iops_log, false);
a47591e4
JA
3555}
3556
3557/*
3558 * Returns msecs to next event
3559 */
3560int calc_log_samples(void)
3561{
58a2d29d 3562 unsigned int next = ~0U, tmp = 0, next_mod = 0, log_avg_msec_min = -1U;
8b6a404c 3563 struct timespec now;
58a2d29d 3564 long elapsed_time = 0;
a47591e4
JA
3565
3566 fio_gettime(&now, NULL);
3567
da8f124f 3568 for_each_td(td) {
58a2d29d
JL
3569 elapsed_time = mtime_since_now(&td->epoch);
3570
8243be59
JA
3571 if (!td->o.stats)
3572 continue;
6a89b401 3573 if (in_ramp_time(td) ||
a47591e4
JA
3574 !(td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING)) {
3575 next = min(td->o.iops_avg_time, td->o.bw_avg_time);
3576 continue;
3577 }
37181709
AH
3578 if (!td->bw_log ||
3579 (td->bw_log && !per_unit_log(td->bw_log))) {
a47591e4 3580 tmp = add_bw_samples(td, &now);
58a2d29d
JL
3581
3582 if (td->bw_log)
3583 log_avg_msec_min = min(log_avg_msec_min, (unsigned int)td->bw_log->avg_msec);
a47591e4 3584 }
37181709
AH
3585 if (!td->iops_log ||
3586 (td->iops_log && !per_unit_log(td->iops_log))) {
a47591e4 3587 tmp = add_iops_samples(td, &now);
58a2d29d
JL
3588
3589 if (td->iops_log)
3590 log_avg_msec_min = min(log_avg_msec_min, (unsigned int)td->iops_log->avg_msec);
a47591e4 3591 }
58a2d29d
JL
3592
3593 if (tmp < next)
3594 next = tmp;
da8f124f 3595 } end_for_each();
a47591e4 3596
58a2d29d
JL
3597 /* if log_avg_msec_min has not been changed, set it to 0 */
3598 if (log_avg_msec_min == -1U)
3599 log_avg_msec_min = 0;
3600
3601 if (log_avg_msec_min == 0)
3602 next_mod = elapsed_time;
3603 else
3604 next_mod = elapsed_time % log_avg_msec_min;
3605
3606 /* correction to keep the time on the log avg msec boundary */
3607 next = min(next, (log_avg_msec_min - next_mod));
3608
a47591e4 3609 return next == ~0U ? 0 : next;
c8eeb9df 3610}
cef9175e
JA
3611
3612void stat_init(void)
3613{
971caeb1 3614 stat_sem = fio_sem_init(FIO_SEM_UNLOCKED);
cef9175e
JA
3615}
3616
3617void stat_exit(void)
3618{
3619 /*
3620 * When we have the mutex, we know out-of-band access to it
3621 * have ended.
3622 */
971caeb1
BVA
3623 fio_sem_down(stat_sem);
3624 fio_sem_remove(stat_sem);
cef9175e 3625}
b2ee7647 3626
b2ee7647
JA
3627/*
3628 * Called from signal handler. Wake up status thread.
3629 */
3630void show_running_run_stats(void)
3631{
a47591e4 3632 helper_do_stat();
b2ee7647 3633}
66347cfa
DE
3634
3635uint32_t *io_u_block_info(struct thread_data *td, struct io_u *io_u)
3636{
3637 /* Ignore io_u's which span multiple blocks--they will just get
3638 * inaccurate counts. */
3639 int idx = (io_u->offset - io_u->file->file_offset)
3640 / td->o.bs[DDIR_TRIM];
3641 uint32_t *info = &td->ts.block_infos[idx];
3642 assert(idx < td->ts.nr_block_infos);
3643 return info;
3644}
5c0abd5e 3645