stat: add blocksize to averaged log, if it's consistent
[fio.git] / stat.c
CommitLineData
3c39a379
JA
1#include <stdio.h>
2#include <string.h>
3#include <sys/time.h>
4#include <sys/types.h>
5c4e1dbc 5#include <sys/stat.h>
3c39a379
JA
6#include <dirent.h>
7#include <libgen.h>
8#include <math.h>
9
10#include "fio.h"
7c9b1bce 11#include "diskutil.h"
c7c6cb4c 12#include "lib/ieee754.h"
cc372b17 13#include "json.h"
44404c5a 14#include "lib/getrusage.h"
f2a2ce0e 15#include "idletime.h"
0f38bbef 16#include "lib/pow2.h"
a666cab8 17#include "lib/output_buffer.h"
a39fb9ea 18#include "helper_thread.h"
3c39a379 19
e5437a07 20struct fio_mutex *stat_mutex;
cef9175e 21
210dd0fc
JA
22void clear_rusage_stat(struct thread_data *td)
23{
24 struct thread_stat *ts = &td->ts;
25
26 fio_getrusage(&td->ru_start);
27 ts->usr_time = ts->sys_time = 0;
28 ts->ctx = 0;
29 ts->minf = ts->majf = 0;
30}
31
3c39a379
JA
32void update_rusage_stat(struct thread_data *td)
33{
756867bd 34 struct thread_stat *ts = &td->ts;
3c39a379 35
44404c5a 36 fio_getrusage(&td->ru_end);
c8aaba19
JA
37 ts->usr_time += mtime_since(&td->ru_start.ru_utime,
38 &td->ru_end.ru_utime);
39 ts->sys_time += mtime_since(&td->ru_start.ru_stime,
40 &td->ru_end.ru_stime);
41 ts->ctx += td->ru_end.ru_nvcsw + td->ru_end.ru_nivcsw
42 - (td->ru_start.ru_nvcsw + td->ru_start.ru_nivcsw);
43 ts->minf += td->ru_end.ru_minflt - td->ru_start.ru_minflt;
44 ts->majf += td->ru_end.ru_majflt - td->ru_start.ru_majflt;
5ec10eaa 45
c8aaba19 46 memcpy(&td->ru_start, &td->ru_end, sizeof(td->ru_end));
3c39a379
JA
47}
48
83349190
YH
49/*
50 * Given a latency, return the index of the corresponding bucket in
51 * the structure tracking percentiles.
52 *
53 * (1) find the group (and error bits) that the value (latency)
54 * belongs to by looking at its MSB. (2) find the bucket number in the
55 * group by looking at the index bits.
56 *
57 */
58static unsigned int plat_val_to_idx(unsigned int val)
59{
60 unsigned int msb, error_bits, base, offset, idx;
61
62 /* Find MSB starting from bit 0 */
63 if (val == 0)
64 msb = 0;
65 else
66 msb = (sizeof(val)*8) - __builtin_clz(val) - 1;
67
716050f2
JA
68 /*
69 * MSB <= (FIO_IO_U_PLAT_BITS-1), cannot be rounded off. Use
70 * all bits of the sample as index
71 */
83349190
YH
72 if (msb <= FIO_IO_U_PLAT_BITS)
73 return val;
74
75 /* Compute the number of error bits to discard*/
76 error_bits = msb - FIO_IO_U_PLAT_BITS;
77
78 /* Compute the number of buckets before the group */
79 base = (error_bits + 1) << FIO_IO_U_PLAT_BITS;
80
716050f2
JA
81 /*
82 * Discard the error bits and apply the mask to find the
3c3ed070 83 * index for the buckets in the group
716050f2 84 */
83349190
YH
85 offset = (FIO_IO_U_PLAT_VAL - 1) & (val >> error_bits);
86
87 /* Make sure the index does not exceed (array size - 1) */
3c3ed070 88 idx = (base + offset) < (FIO_IO_U_PLAT_NR - 1) ?
83349190
YH
89 (base + offset) : (FIO_IO_U_PLAT_NR - 1);
90
91 return idx;
92}
93
94/*
95 * Convert the given index of the bucket array to the value
96 * represented by the bucket
97 */
98static unsigned int plat_idx_to_val(unsigned int idx)
99{
100 unsigned int error_bits, k, base;
101
102 assert(idx < FIO_IO_U_PLAT_NR);
103
104 /* MSB <= (FIO_IO_U_PLAT_BITS-1), cannot be rounded off. Use
105 * all bits of the sample as index */
3c3ed070 106 if (idx < (FIO_IO_U_PLAT_VAL << 1))
83349190
YH
107 return idx;
108
109 /* Find the group and compute the minimum value of that group */
3c3ed070 110 error_bits = (idx >> FIO_IO_U_PLAT_BITS) - 1;
83349190
YH
111 base = 1 << (error_bits + FIO_IO_U_PLAT_BITS);
112
113 /* Find its bucket number of the group */
114 k = idx % FIO_IO_U_PLAT_VAL;
115
116 /* Return the mean of the range of the bucket */
117 return base + ((k + 0.5) * (1 << error_bits));
118}
119
120static int double_cmp(const void *a, const void *b)
121{
802ad4a8
JA
122 const fio_fp64_t fa = *(const fio_fp64_t *) a;
123 const fio_fp64_t fb = *(const fio_fp64_t *) b;
83349190
YH
124 int cmp = 0;
125
802ad4a8 126 if (fa.u.f > fb.u.f)
83349190 127 cmp = 1;
802ad4a8 128 else if (fa.u.f < fb.u.f)
83349190
YH
129 cmp = -1;
130
131 return cmp;
132}
133
a269790c
JA
134unsigned int calc_clat_percentiles(unsigned int *io_u_plat, unsigned long nr,
135 fio_fp64_t *plist, unsigned int **output,
136 unsigned int *maxv, unsigned int *minv)
83349190
YH
137{
138 unsigned long sum = 0;
1db92cb6
JA
139 unsigned int len, i, j = 0;
140 unsigned int oval_len = 0;
141 unsigned int *ovals = NULL;
142 int is_last;
143
144 *minv = -1U;
145 *maxv = 0;
83349190 146
802ad4a8
JA
147 len = 0;
148 while (len < FIO_IO_U_LIST_MAX_LEN && plist[len].u.f != 0.0)
149 len++;
716050f2 150
351de8de 151 if (!len)
1db92cb6 152 return 0;
351de8de 153
716050f2 154 /*
802ad4a8
JA
155 * Sort the percentile list. Note that it may already be sorted if
156 * we are using the default values, but since it's a short list this
157 * isn't a worry. Also note that this does not work for NaN values.
716050f2 158 */
802ad4a8 159 if (len > 1)
3c3ed070 160 qsort((void *)plist, len, sizeof(plist[0]), double_cmp);
83349190 161
4f6f8298
JA
162 /*
163 * Calculate bucket values, note down max and min values
164 */
07511a63
JA
165 is_last = 0;
166 for (i = 0; i < FIO_IO_U_PLAT_NR && !is_last; i++) {
83349190 167 sum += io_u_plat[i];
802ad4a8
JA
168 while (sum >= (plist[j].u.f / 100.0 * nr)) {
169 assert(plist[j].u.f <= 100.0);
83349190 170
4f6f8298
JA
171 if (j == oval_len) {
172 oval_len += 100;
173 ovals = realloc(ovals, oval_len * sizeof(unsigned int));
174 }
175
176 ovals[j] = plat_idx_to_val(i);
1db92cb6
JA
177 if (ovals[j] < *minv)
178 *minv = ovals[j];
179 if (ovals[j] > *maxv)
180 *maxv = ovals[j];
07511a63
JA
181
182 is_last = (j == len - 1);
183 if (is_last)
184 break;
185
4f6f8298
JA
186 j++;
187 }
188 }
83349190 189
1db92cb6
JA
190 *output = ovals;
191 return len;
192}
193
194/*
195 * Find and display the p-th percentile of clat
196 */
197static void show_clat_percentiles(unsigned int *io_u_plat, unsigned long nr,
a666cab8
JA
198 fio_fp64_t *plist, unsigned int precision,
199 struct buf_output *out)
1db92cb6
JA
200{
201 unsigned int len, j = 0, minv, maxv;
202 unsigned int *ovals;
eef02441
JA
203 int is_last, per_line, scale_down;
204 char fmt[32];
1db92cb6
JA
205
206 len = calc_clat_percentiles(io_u_plat, nr, plist, &ovals, &maxv, &minv);
207 if (!len)
208 goto out;
209
4f6f8298
JA
210 /*
211 * We default to usecs, but if the value range is such that we
212 * should scale down to msecs, do that.
213 */
214 if (minv > 2000 && maxv > 99999) {
215 scale_down = 1;
a666cab8 216 log_buf(out, " clat percentiles (msec):\n |");
4f6f8298
JA
217 } else {
218 scale_down = 0;
a666cab8 219 log_buf(out, " clat percentiles (usec):\n |");
4f6f8298 220 }
83349190 221
3dc1e5b2 222 snprintf(fmt, sizeof(fmt), "%%1.%uf", precision);
eef02441 223 per_line = (80 - 7) / (precision + 14);
619adf9c 224
4f6f8298 225 for (j = 0; j < len; j++) {
619adf9c 226 char fbuf[16], *ptr = fbuf;
81ab0b3a 227
4f6f8298 228 /* for formatting */
eef02441 229 if (j != 0 && (j % per_line) == 0)
a666cab8 230 log_buf(out, " |");
83349190 231
4f6f8298
JA
232 /* end of the list */
233 is_last = (j == len - 1);
83349190 234
4f6f8298 235 if (plist[j].u.f < 10.0)
619adf9c
JA
236 ptr += sprintf(fbuf, " ");
237
eef02441 238 snprintf(ptr, sizeof(fbuf), fmt, plist[j].u.f);
4f6f8298
JA
239
240 if (scale_down)
241 ovals[j] = (ovals[j] + 999) / 1000;
242
a666cab8 243 log_buf(out, " %sth=[%5u]%c", fbuf, ovals[j], is_last ? '\n' : ',');
4f6f8298
JA
244
245 if (is_last)
246 break;
247
eef02441 248 if ((j % per_line) == per_line - 1) /* for formatting */
a666cab8 249 log_buf(out, "\n");
83349190 250 }
4f6f8298 251
1db92cb6 252out:
4f6f8298
JA
253 if (ovals)
254 free(ovals);
83349190
YH
255}
256
b29ad562
JA
257int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max,
258 double *mean, double *dev)
3c39a379 259{
ee0ccb79 260 double n = (double) is->samples;
3c39a379 261
ee0ccb79 262 if (n == 0)
3c39a379
JA
263 return 0;
264
265 *min = is->min_val;
266 *max = is->max_val;
802ad4a8 267 *mean = is->mean.u.f;
e6d276f2 268
68704084 269 if (n > 1.0)
802ad4a8 270 *dev = sqrt(is->S.u.f / (n - 1.0));
ef9c5c40 271 else
4b43f54e 272 *dev = 0;
ef9c5c40 273
3c39a379
JA
274 return 1;
275}
276
a666cab8 277void show_group_stats(struct group_run_stats *rs, struct buf_output *out)
3c39a379 278{
dbe1125e 279 char *p1, *p2, *p3, *p4;
42da5c8b 280 const char *str[] = { " READ", " WRITE" , " TRIM"};
dbe1125e
JA
281 int i;
282
a666cab8 283 log_buf(out, "\nRun status group %d (all jobs):\n", rs->groupid);
3c39a379 284
6eaf09d6 285 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
90fef2d1
JA
286 const int i2p = is_power_of_2(rs->kb_base);
287
dbe1125e
JA
288 if (!rs->max_run[i])
289 continue;
290
73798eb2 291 p1 = num2str(rs->io_kb[i], 6, rs->kb_base, i2p, 8);
ad705bcb
SN
292 p2 = num2str(rs->agg[i], 6, rs->kb_base, i2p, rs->unit_base);
293 p3 = num2str(rs->min_bw[i], 6, rs->kb_base, i2p, rs->unit_base);
294 p4 = num2str(rs->max_bw[i], 6, rs->kb_base, i2p, rs->unit_base);
dbe1125e 295
a666cab8 296 log_buf(out, "%s: io=%s, aggrb=%s/s, minb=%s/s, maxb=%s/s,"
771e58be 297 " mint=%llumsec, maxt=%llumsec\n",
42da5c8b 298 rs->unified_rw_rep ? " MIXED" : str[i],
4e0a8fa2
JA
299 p1, p2, p3, p4,
300 (unsigned long long) rs->min_run[i],
301 (unsigned long long) rs->max_run[i]);
dbe1125e
JA
302
303 free(p1);
304 free(p2);
305 free(p3);
306 free(p4);
307 }
3c39a379
JA
308}
309
2e33101f 310void stat_calc_dist(unsigned int *map, unsigned long total, double *io_u_dist)
2270890c
JA
311{
312 int i;
313
314 /*
315 * Do depth distribution calculations
316 */
317 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
838bc709
JA
318 if (total) {
319 io_u_dist[i] = (double) map[i] / (double) total;
320 io_u_dist[i] *= 100.0;
321 if (io_u_dist[i] < 0.1 && map[i])
322 io_u_dist[i] = 0.1;
323 } else
324 io_u_dist[i] = 0.0;
2270890c
JA
325 }
326}
327
04a0feae
JA
328static void stat_calc_lat(struct thread_stat *ts, double *dst,
329 unsigned int *src, int nr)
2270890c 330{
d79db122 331 unsigned long total = ddir_rw_sum(ts->total_io_u);
2270890c
JA
332 int i;
333
334 /*
335 * Do latency distribution calculations
336 */
04a0feae 337 for (i = 0; i < nr; i++) {
838bc709
JA
338 if (total) {
339 dst[i] = (double) src[i] / (double) total;
340 dst[i] *= 100.0;
341 if (dst[i] < 0.01 && src[i])
342 dst[i] = 0.01;
343 } else
344 dst[i] = 0.0;
2270890c
JA
345 }
346}
347
e5bd1347 348void stat_calc_lat_u(struct thread_stat *ts, double *io_u_lat)
04a0feae
JA
349{
350 stat_calc_lat(ts, io_u_lat, ts->io_u_lat_u, FIO_IO_U_LAT_U_NR);
351}
352
e5bd1347 353void stat_calc_lat_m(struct thread_stat *ts, double *io_u_lat)
04a0feae
JA
354{
355 stat_calc_lat(ts, io_u_lat, ts->io_u_lat_m, FIO_IO_U_LAT_M_NR);
356}
357
b29ad562 358static void display_lat(const char *name, unsigned long min, unsigned long max,
a666cab8 359 double mean, double dev, struct buf_output *out)
ea2accc5 360{
b29ad562
JA
361 const char *base = "(usec)";
362 char *minp, *maxp;
ea2accc5 363
b29ad562
JA
364 if (!usec_to_msec(&min, &max, &mean, &dev))
365 base = "(msec)";
366
22f80458
JA
367 minp = num2str(min, 6, 1, 0, 0);
368 maxp = num2str(max, 6, 1, 0, 0);
b29ad562 369
a666cab8 370 log_buf(out, " %s %s: min=%s, max=%s, avg=%5.02f,"
b29ad562
JA
371 " stdev=%5.02f\n", name, base, minp, maxp, mean, dev);
372
373 free(minp);
374 free(maxp);
ea2accc5
JA
375}
376
756867bd 377static void show_ddir_status(struct group_run_stats *rs, struct thread_stat *ts,
a666cab8 378 int ddir, struct buf_output *out)
3c39a379 379{
42da5c8b 380 const char *str[] = { "read ", "write", "trim" };
8879fd15 381 unsigned long min, max, runt;
b3605062 382 unsigned long long bw, iops;
3c39a379 383 double mean, dev;
b3605062 384 char *io_p, *bw_p, *iops_p;
90fef2d1 385 int i2p;
3c39a379 386
ff58fced
JA
387 assert(ddir_rw(ddir));
388
756867bd 389 if (!ts->runtime[ddir])
3c39a379
JA
390 return;
391
90fef2d1 392 i2p = is_power_of_2(rs->kb_base);
8879fd15
JA
393 runt = ts->runtime[ddir];
394
395 bw = (1000 * ts->io_bytes[ddir]) / runt;
73798eb2 396 io_p = num2str(ts->io_bytes[ddir], 6, 1, i2p, 8);
ad705bcb 397 bw_p = num2str(bw, 6, 1, i2p, ts->unit_base);
8879fd15 398
0aacc50c 399 iops = (1000 * (uint64_t)ts->total_io_u[ddir]) / runt;
73798eb2 400 iops_p = num2str(iops, 6, 1, 0, 0);
dbe1125e 401
a666cab8 402 log_buf(out, " %s: io=%s, bw=%s/s, iops=%s, runt=%6llumsec\n",
42da5c8b 403 rs->unified_rw_rep ? "mixed" : str[ddir],
4e0a8fa2
JA
404 io_p, bw_p, iops_p,
405 (unsigned long long) ts->runtime[ddir]);
dbe1125e
JA
406
407 free(io_p);
408 free(bw_p);
b3605062 409 free(iops_p);
3c39a379 410
b29ad562 411 if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev))
a666cab8 412 display_lat("slat", min, max, mean, dev, out);
b29ad562 413 if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev))
a666cab8 414 display_lat("clat", min, max, mean, dev, out);
b29ad562 415 if (calc_lat(&ts->lat_stat[ddir], &min, &max, &mean, &dev))
a666cab8 416 display_lat(" lat", min, max, mean, dev, out);
02af0988 417
83349190
YH
418 if (ts->clat_percentiles) {
419 show_clat_percentiles(ts->io_u_plat[ddir],
420 ts->clat_stat[ddir].samples,
435d195a 421 ts->percentile_list,
a666cab8 422 ts->percentile_precision, out);
83349190 423 }
079ad09b 424 if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
142c7f2d 425 double p_of_agg = 100.0, fkb_base = (double)rs->kb_base;
d686990a
SN
426 const char *bw_str = (rs->unit_base == 1 ? "Kbit" : "KB");
427
428 if (rs->unit_base == 1) {
429 min *= 8.0;
430 max *= 8.0;
431 mean *= 8.0;
432 dev *= 8.0;
433 }
3c39a379 434
2f2c6924 435 if (rs->agg[ddir]) {
19d3e967
JA
436 p_of_agg = mean * 100 / (double) rs->agg[ddir];
437 if (p_of_agg > 100.0)
438 p_of_agg = 100.0;
439 }
b7017e32 440
142c7f2d
SN
441 if (mean > fkb_base * fkb_base) {
442 min /= fkb_base;
443 max /= fkb_base;
444 mean /= fkb_base;
445 dev /= fkb_base;
d686990a 446 bw_str = (rs->unit_base == 1 ? "Mbit" : "MB");
b7017e32
JA
447 }
448
a666cab8 449 log_buf(out, " bw (%-4s/s): min=%5lu, max=%5lu, per=%3.2f%%,"
b7017e32
JA
450 " avg=%5.02f, stdev=%5.02f\n", bw_str, min, max,
451 p_of_agg, mean, dev);
3c39a379
JA
452 }
453}
454
7e1773ba 455static int show_lat(double *io_u_lat, int nr, const char **ranges,
a666cab8 456 const char *msg, struct buf_output *out)
04a0feae 457{
7e1773ba 458 int new_line = 1, i, line = 0, shown = 0;
04a0feae
JA
459
460 for (i = 0; i < nr; i++) {
461 if (io_u_lat[i] <= 0.0)
462 continue;
7e1773ba 463 shown = 1;
04a0feae 464 if (new_line) {
4539ed73 465 if (line)
a666cab8
JA
466 log_buf(out, "\n");
467 log_buf(out, " lat (%s) : ", msg);
04a0feae
JA
468 new_line = 0;
469 line = 0;
470 }
471 if (line)
a666cab8
JA
472 log_buf(out, ", ");
473 log_buf(out, "%s%3.2f%%", ranges[i], io_u_lat[i]);
04a0feae
JA
474 line++;
475 if (line == 5)
476 new_line = 1;
477 }
7e1773ba
JA
478
479 if (shown)
a666cab8 480 log_buf(out, "\n");
7e1773ba
JA
481
482 return shown;
04a0feae
JA
483}
484
a666cab8 485static void show_lat_u(double *io_u_lat_u, struct buf_output *out)
04a0feae
JA
486{
487 const char *ranges[] = { "2=", "4=", "10=", "20=", "50=", "100=",
488 "250=", "500=", "750=", "1000=", };
489
a666cab8 490 show_lat(io_u_lat_u, FIO_IO_U_LAT_U_NR, ranges, "usec", out);
04a0feae
JA
491}
492
a666cab8 493static void show_lat_m(double *io_u_lat_m, struct buf_output *out)
04a0feae
JA
494{
495 const char *ranges[] = { "2=", "4=", "10=", "20=", "50=", "100=",
496 "250=", "500=", "750=", "1000=", "2000=",
497 ">=2000=", };
498
a666cab8 499 show_lat(io_u_lat_m, FIO_IO_U_LAT_M_NR, ranges, "msec", out);
04a0feae
JA
500}
501
a666cab8 502static void show_latencies(struct thread_stat *ts, struct buf_output *out)
04a0feae 503{
c551f65a
JA
504 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
505 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
506
5a18988e
JA
507 stat_calc_lat_u(ts, io_u_lat_u);
508 stat_calc_lat_m(ts, io_u_lat_m);
509
a666cab8
JA
510 show_lat_u(io_u_lat_u, out);
511 show_lat_m(io_u_lat_m, out);
04a0feae
JA
512}
513
66347cfa
DE
514static int block_state_category(int block_state)
515{
516 switch (block_state) {
517 case BLOCK_STATE_UNINIT:
518 return 0;
519 case BLOCK_STATE_TRIMMED:
520 case BLOCK_STATE_WRITTEN:
521 return 1;
522 case BLOCK_STATE_WRITE_FAILURE:
523 case BLOCK_STATE_TRIM_FAILURE:
524 return 2;
525 default:
081c2dc3 526 /* Silence compile warning on some BSDs and have a return */
66347cfa 527 assert(0);
07ff5841 528 return -1;
66347cfa
DE
529 }
530}
531
532static int compare_block_infos(const void *bs1, const void *bs2)
533{
534 uint32_t block1 = *(uint32_t *)bs1;
535 uint32_t block2 = *(uint32_t *)bs2;
536 int state1 = BLOCK_INFO_STATE(block1);
537 int state2 = BLOCK_INFO_STATE(block2);
538 int bscat1 = block_state_category(state1);
539 int bscat2 = block_state_category(state2);
540 int cycles1 = BLOCK_INFO_TRIMS(block1);
541 int cycles2 = BLOCK_INFO_TRIMS(block2);
542
543 if (bscat1 < bscat2)
544 return -1;
545 if (bscat1 > bscat2)
546 return 1;
547
548 if (cycles1 < cycles2)
549 return -1;
550 if (cycles1 > cycles2)
551 return 1;
552
553 if (state1 < state2)
554 return -1;
555 if (state1 > state2)
556 return 1;
557
558 assert(block1 == block2);
559 return 0;
560}
561
562static int calc_block_percentiles(int nr_block_infos, uint32_t *block_infos,
563 fio_fp64_t *plist, unsigned int **percentiles,
564 unsigned int *types)
565{
566 int len = 0;
567 int i, nr_uninit;
568
569 qsort(block_infos, nr_block_infos, sizeof(uint32_t), compare_block_infos);
570
571 while (len < FIO_IO_U_LIST_MAX_LEN && plist[len].u.f != 0.0)
572 len++;
573
574 if (!len)
575 return 0;
576
577 /*
578 * Sort the percentile list. Note that it may already be sorted if
579 * we are using the default values, but since it's a short list this
580 * isn't a worry. Also note that this does not work for NaN values.
581 */
582 if (len > 1)
583 qsort((void *)plist, len, sizeof(plist[0]), double_cmp);
584
585 nr_uninit = 0;
586 /* Start only after the uninit entries end */
587 for (nr_uninit = 0;
588 nr_uninit < nr_block_infos
589 && BLOCK_INFO_STATE(block_infos[nr_uninit]) == BLOCK_STATE_UNINIT;
590 nr_uninit ++)
591 ;
592
593 if (nr_uninit == nr_block_infos)
594 return 0;
595
596 *percentiles = calloc(len, sizeof(**percentiles));
597
598 for (i = 0; i < len; i++) {
599 int idx = (plist[i].u.f * (nr_block_infos - nr_uninit) / 100)
600 + nr_uninit;
601 (*percentiles)[i] = BLOCK_INFO_TRIMS(block_infos[idx]);
602 }
603
604 memset(types, 0, sizeof(*types) * BLOCK_STATE_COUNT);
605 for (i = 0; i < nr_block_infos; i++)
606 types[BLOCK_INFO_STATE(block_infos[i])]++;
607
608 return len;
609}
610
611static const char *block_state_names[] = {
612 [BLOCK_STATE_UNINIT] = "unwritten",
613 [BLOCK_STATE_TRIMMED] = "trimmed",
614 [BLOCK_STATE_WRITTEN] = "written",
615 [BLOCK_STATE_TRIM_FAILURE] = "trim failure",
616 [BLOCK_STATE_WRITE_FAILURE] = "write failure",
617};
618
619static void show_block_infos(int nr_block_infos, uint32_t *block_infos,
a666cab8 620 fio_fp64_t *plist, struct buf_output *out)
66347cfa
DE
621{
622 int len, pos, i;
623 unsigned int *percentiles = NULL;
624 unsigned int block_state_counts[BLOCK_STATE_COUNT];
625
626 len = calc_block_percentiles(nr_block_infos, block_infos, plist,
627 &percentiles, block_state_counts);
628
a666cab8 629 log_buf(out, " block lifetime percentiles :\n |");
66347cfa
DE
630 pos = 0;
631 for (i = 0; i < len; i++) {
632 uint32_t block_info = percentiles[i];
633#define LINE_LENGTH 75
634 char str[LINE_LENGTH];
635 int strln = snprintf(str, LINE_LENGTH, " %3.2fth=%u%c",
636 plist[i].u.f, block_info,
637 i == len - 1 ? '\n' : ',');
638 assert(strln < LINE_LENGTH);
639 if (pos + strln > LINE_LENGTH) {
640 pos = 0;
a666cab8 641 log_buf(out, "\n |");
66347cfa 642 }
a666cab8 643 log_buf(out, "%s", str);
66347cfa
DE
644 pos += strln;
645#undef LINE_LENGTH
646 }
647 if (percentiles)
648 free(percentiles);
649
a666cab8 650 log_buf(out, " states :");
66347cfa 651 for (i = 0; i < BLOCK_STATE_COUNT; i++)
a666cab8 652 log_buf(out, " %s=%u%c",
66347cfa
DE
653 block_state_names[i], block_state_counts[i],
654 i == BLOCK_STATE_COUNT - 1 ? '\n' : ',');
655}
656
10aa136b 657static void show_thread_status_normal(struct thread_stat *ts,
a666cab8
JA
658 struct group_run_stats *rs,
659 struct buf_output *out)
3c39a379
JA
660{
661 double usr_cpu, sys_cpu;
69008999 662 unsigned long runtime;
71619dc2 663 double io_u_dist[FIO_IO_U_MAP_NR];
57a64324 664 time_t time_p;
35326842 665 char time_buf[32];
3c39a379 666
9966af7b 667 if (!ddir_rw_sum(ts->io_bytes) && !ddir_rw_sum(ts->total_io_u))
3c39a379
JA
668 return;
669
57a64324 670 time(&time_p);
45054cbe 671 os_ctime_r((const time_t *) &time_p, time_buf, sizeof(time_buf));
57a64324 672
5ec10eaa 673 if (!ts->error) {
a666cab8 674 log_buf(out, "%s: (groupid=%d, jobs=%d): err=%2d: pid=%d: %s",
5ec10eaa 675 ts->name, ts->groupid, ts->members,
57a64324 676 ts->error, (int) ts->pid, time_buf);
5ec10eaa 677 } else {
a666cab8 678 log_buf(out, "%s: (groupid=%d, jobs=%d): err=%2d (%s): pid=%d: %s",
5ec10eaa 679 ts->name, ts->groupid, ts->members,
57a64324
JA
680 ts->error, ts->verror, (int) ts->pid,
681 time_buf);
5ec10eaa 682 }
3c39a379 683
259e47de 684 if (strlen(ts->description))
a666cab8 685 log_buf(out, " Description : [%s]\n", ts->description);
7bdce1bd 686
756867bd 687 if (ts->io_bytes[DDIR_READ])
a666cab8 688 show_ddir_status(rs, ts, DDIR_READ, out);
756867bd 689 if (ts->io_bytes[DDIR_WRITE])
a666cab8 690 show_ddir_status(rs, ts, DDIR_WRITE, out);
6eaf09d6 691 if (ts->io_bytes[DDIR_TRIM])
a666cab8 692 show_ddir_status(rs, ts, DDIR_TRIM, out);
3c39a379 693
a666cab8 694 show_latencies(ts, out);
7e1773ba 695
756867bd 696 runtime = ts->total_run_time;
69008999 697 if (runtime) {
1e97cce9 698 double runt = (double) runtime;
3c39a379 699
756867bd
JA
700 usr_cpu = (double) ts->usr_time * 100 / runt;
701 sys_cpu = (double) ts->sys_time * 100 / runt;
3c39a379
JA
702 } else {
703 usr_cpu = 0;
704 sys_cpu = 0;
705 }
706
a666cab8 707 log_buf(out, " cpu : usr=%3.2f%%, sys=%3.2f%%, ctx=%llu,"
4e0a8fa2
JA
708 " majf=%llu, minf=%llu\n", usr_cpu, sys_cpu,
709 (unsigned long long) ts->ctx,
710 (unsigned long long) ts->majf,
711 (unsigned long long) ts->minf);
71619dc2 712
d79db122 713 stat_calc_dist(ts->io_u_map, ddir_rw_sum(ts->total_io_u), io_u_dist);
a666cab8 714 log_buf(out, " IO depths : 1=%3.1f%%, 2=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%,"
5ec10eaa
JA
715 " 16=%3.1f%%, 32=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
716 io_u_dist[1], io_u_dist[2],
717 io_u_dist[3], io_u_dist[4],
718 io_u_dist[5], io_u_dist[6]);
838bc709
JA
719
720 stat_calc_dist(ts->io_u_submit, ts->total_submit, io_u_dist);
a666cab8 721 log_buf(out, " submit : 0=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%, 16=%3.1f%%,"
838bc709
JA
722 " 32=%3.1f%%, 64=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
723 io_u_dist[1], io_u_dist[2],
724 io_u_dist[3], io_u_dist[4],
725 io_u_dist[5], io_u_dist[6]);
726 stat_calc_dist(ts->io_u_complete, ts->total_complete, io_u_dist);
a666cab8 727 log_buf(out, " complete : 0=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%, 16=%3.1f%%,"
838bc709
JA
728 " 32=%3.1f%%, 64=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
729 io_u_dist[1], io_u_dist[2],
730 io_u_dist[3], io_u_dist[4],
731 io_u_dist[5], io_u_dist[6]);
a666cab8 732 log_buf(out, " issued : total=r=%llu/w=%llu/d=%llu,"
3bcb9d94
JA
733 " short=r=%llu/w=%llu/d=%llu,"
734 " drop=r=%llu/w=%llu/d=%llu\n",
4e0a8fa2
JA
735 (unsigned long long) ts->total_io_u[0],
736 (unsigned long long) ts->total_io_u[1],
737 (unsigned long long) ts->total_io_u[2],
738 (unsigned long long) ts->short_io_u[0],
739 (unsigned long long) ts->short_io_u[1],
3bcb9d94
JA
740 (unsigned long long) ts->short_io_u[2],
741 (unsigned long long) ts->drop_io_u[0],
742 (unsigned long long) ts->drop_io_u[1],
743 (unsigned long long) ts->drop_io_u[2]);
f2bba182 744 if (ts->continue_on_error) {
a666cab8 745 log_buf(out, " errors : total=%llu, first_error=%d/<%s>\n",
4e0a8fa2 746 (unsigned long long)ts->total_err_count,
1ec99eea
JA
747 ts->first_error,
748 strerror(ts->first_error));
f2bba182 749 }
3e260a46 750 if (ts->latency_depth) {
a666cab8 751 log_buf(out, " latency : target=%llu, window=%llu, percentile=%.2f%%, depth=%u\n",
3e260a46
JA
752 (unsigned long long)ts->latency_target,
753 (unsigned long long)ts->latency_window,
754 ts->latency_percentile.u.f,
755 ts->latency_depth);
756 }
66347cfa
DE
757
758 if (ts->nr_block_infos)
759 show_block_infos(ts->nr_block_infos, ts->block_infos,
a666cab8 760 ts->percentile_list, out);
3c39a379
JA
761}
762
756867bd 763static void show_ddir_status_terse(struct thread_stat *ts,
a666cab8
JA
764 struct group_run_stats *rs, int ddir,
765 struct buf_output *out)
c6ae0a5b
JA
766{
767 unsigned long min, max;
312b4af2 768 unsigned long long bw, iops;
1db92cb6 769 unsigned int *ovals = NULL;
c6ae0a5b 770 double mean, dev;
1db92cb6
JA
771 unsigned int len, minv, maxv;
772 int i;
c6ae0a5b 773
ff58fced
JA
774 assert(ddir_rw(ddir));
775
312b4af2
JA
776 iops = bw = 0;
777 if (ts->runtime[ddir]) {
778 uint64_t runt = ts->runtime[ddir];
779
033bbb51 780 bw = ((1000 * ts->io_bytes[ddir]) / runt) / 1024;
312b4af2
JA
781 iops = (1000 * (uint64_t) ts->total_io_u[ddir]) / runt;
782 }
c6ae0a5b 783
a666cab8 784 log_buf(out, ";%llu;%llu;%llu;%llu",
4e0a8fa2
JA
785 (unsigned long long) ts->io_bytes[ddir] >> 10, bw, iops,
786 (unsigned long long) ts->runtime[ddir]);
c6ae0a5b 787
079ad09b 788 if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev))
a666cab8 789 log_buf(out, ";%lu;%lu;%f;%f", min, max, mean, dev);
c6ae0a5b 790 else
a666cab8 791 log_buf(out, ";%lu;%lu;%f;%f", 0UL, 0UL, 0.0, 0.0);
c6ae0a5b 792
079ad09b 793 if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev))
a666cab8 794 log_buf(out, ";%lu;%lu;%f;%f", min, max, mean, dev);
c6ae0a5b 795 else
a666cab8 796 log_buf(out, ";%lu;%lu;%f;%f", 0UL, 0UL, 0.0, 0.0);
c6ae0a5b 797
1db92cb6
JA
798 if (ts->clat_percentiles) {
799 len = calc_clat_percentiles(ts->io_u_plat[ddir],
800 ts->clat_stat[ddir].samples,
801 ts->percentile_list, &ovals, &maxv,
802 &minv);
803 } else
804 len = 0;
805
806 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
807 if (i >= len) {
a666cab8 808 log_buf(out, ";0%%=0");
1db92cb6
JA
809 continue;
810 }
a666cab8 811 log_buf(out, ";%f%%=%u", ts->percentile_list[i].u.f, ovals[i]);
1db92cb6 812 }
2341a37a
K
813
814 if (calc_lat(&ts->lat_stat[ddir], &min, &max, &mean, &dev))
a666cab8 815 log_buf(out, ";%lu;%lu;%f;%f", min, max, mean, dev);
2341a37a 816 else
a666cab8 817 log_buf(out, ";%lu;%lu;%f;%f", 0UL, 0UL, 0.0, 0.0);
2341a37a 818
1db92cb6
JA
819 if (ovals)
820 free(ovals);
821
079ad09b 822 if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
19d3e967
JA
823 double p_of_agg = 100.0;
824
825 if (rs->agg[ddir]) {
826 p_of_agg = mean * 100 / (double) rs->agg[ddir];
827 if (p_of_agg > 100.0)
828 p_of_agg = 100.0;
829 }
c6ae0a5b 830
a666cab8 831 log_buf(out, ";%lu;%lu;%f%%;%f;%f", min, max, p_of_agg, mean, dev);
c6ae0a5b 832 } else
a666cab8 833 log_buf(out, ";%lu;%lu;%f%%;%f;%f", 0UL, 0UL, 0.0, 0.0, 0.0);
c6ae0a5b
JA
834}
835
cc372b17
SL
836static void add_ddir_status_json(struct thread_stat *ts,
837 struct group_run_stats *rs, int ddir, struct json_object *parent)
838{
839 unsigned long min, max;
9f68fe3a 840 unsigned long long bw;
cc372b17 841 unsigned int *ovals = NULL;
9f68fe3a 842 double mean, dev, iops;
cc372b17
SL
843 unsigned int len, minv, maxv;
844 int i;
845 const char *ddirname[] = {"read", "write", "trim"};
513e37ee 846 struct json_object *dir_object, *tmp_object, *percentile_object, *clat_bins_object;
cc372b17
SL
847 char buf[120];
848 double p_of_agg = 100.0;
849
850 assert(ddir_rw(ddir));
851
771e58be
JA
852 if (ts->unified_rw_rep && ddir != DDIR_READ)
853 return;
854
cc372b17 855 dir_object = json_create_object();
771e58be
JA
856 json_object_add_value_object(parent,
857 ts->unified_rw_rep ? "mixed" : ddirname[ddir], dir_object);
cc372b17 858
9f68fe3a
BE
859 bw = 0;
860 iops = 0.0;
cc372b17
SL
861 if (ts->runtime[ddir]) {
862 uint64_t runt = ts->runtime[ddir];
863
864 bw = ((1000 * ts->io_bytes[ddir]) / runt) / 1024;
9f68fe3a 865 iops = (1000.0 * (uint64_t) ts->total_io_u[ddir]) / runt;
cc372b17
SL
866 }
867
868 json_object_add_value_int(dir_object, "io_bytes", ts->io_bytes[ddir] >> 10);
869 json_object_add_value_int(dir_object, "bw", bw);
9f68fe3a 870 json_object_add_value_float(dir_object, "iops", iops);
cc372b17 871 json_object_add_value_int(dir_object, "runtime", ts->runtime[ddir]);
9966af7b
JA
872 json_object_add_value_int(dir_object, "total_ios", ts->total_io_u[ddir]);
873 json_object_add_value_int(dir_object, "short_ios", ts->short_io_u[ddir]);
874 json_object_add_value_int(dir_object, "drop_ios", ts->drop_io_u[ddir]);
cc372b17
SL
875
876 if (!calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev)) {
877 min = max = 0;
878 mean = dev = 0.0;
879 }
880 tmp_object = json_create_object();
881 json_object_add_value_object(dir_object, "slat", tmp_object);
882 json_object_add_value_int(tmp_object, "min", min);
883 json_object_add_value_int(tmp_object, "max", max);
884 json_object_add_value_float(tmp_object, "mean", mean);
885 json_object_add_value_float(tmp_object, "stddev", dev);
886
887 if (!calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev)) {
888 min = max = 0;
889 mean = dev = 0.0;
890 }
891 tmp_object = json_create_object();
892 json_object_add_value_object(dir_object, "clat", tmp_object);
893 json_object_add_value_int(tmp_object, "min", min);
894 json_object_add_value_int(tmp_object, "max", max);
895 json_object_add_value_float(tmp_object, "mean", mean);
896 json_object_add_value_float(tmp_object, "stddev", dev);
897
898 if (ts->clat_percentiles) {
899 len = calc_clat_percentiles(ts->io_u_plat[ddir],
900 ts->clat_stat[ddir].samples,
901 ts->percentile_list, &ovals, &maxv,
902 &minv);
903 } else
904 len = 0;
905
906 percentile_object = json_create_object();
907 json_object_add_value_object(tmp_object, "percentile", percentile_object);
908 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
909 if (i >= len) {
910 json_object_add_value_int(percentile_object, "0.00", 0);
911 continue;
912 }
435d195a 913 snprintf(buf, sizeof(buf), "%f", ts->percentile_list[i].u.f);
cc372b17
SL
914 json_object_add_value_int(percentile_object, (const char *)buf, ovals[i]);
915 }
916
513e37ee
VF
917 if (output_format & FIO_OUTPUT_JSON_PLUS) {
918 clat_bins_object = json_create_object();
919 json_object_add_value_object(tmp_object, "bins", clat_bins_object);
920 for(i = 0; i < FIO_IO_U_PLAT_NR; i++) {
921 snprintf(buf, sizeof(buf), "%d", i);
922 json_object_add_value_int(clat_bins_object, (const char *)buf, ts->io_u_plat[ddir][i]);
923 }
924 json_object_add_value_int(clat_bins_object, "FIO_IO_U_PLAT_BITS", FIO_IO_U_PLAT_BITS);
925 json_object_add_value_int(clat_bins_object, "FIO_IO_U_PLAT_VAL", FIO_IO_U_PLAT_VAL);
926 json_object_add_value_int(clat_bins_object, "FIO_IO_U_PLAT_NR", FIO_IO_U_PLAT_NR);
927 }
928
cc372b17
SL
929 if (!calc_lat(&ts->lat_stat[ddir], &min, &max, &mean, &dev)) {
930 min = max = 0;
931 mean = dev = 0.0;
932 }
933 tmp_object = json_create_object();
934 json_object_add_value_object(dir_object, "lat", tmp_object);
935 json_object_add_value_int(tmp_object, "min", min);
936 json_object_add_value_int(tmp_object, "max", max);
937 json_object_add_value_float(tmp_object, "mean", mean);
938 json_object_add_value_float(tmp_object, "stddev", dev);
939 if (ovals)
940 free(ovals);
941
b9e1b491 942 if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
cc372b17
SL
943 if (rs->agg[ddir]) {
944 p_of_agg = mean * 100 / (double) rs->agg[ddir];
945 if (p_of_agg > 100.0)
946 p_of_agg = 100.0;
947 }
948 } else {
949 min = max = 0;
950 p_of_agg = mean = dev = 0.0;
951 }
952 json_object_add_value_int(dir_object, "bw_min", min);
953 json_object_add_value_int(dir_object, "bw_max", max);
a806bf2e 954 json_object_add_value_float(dir_object, "bw_agg", p_of_agg);
cc372b17
SL
955 json_object_add_value_float(dir_object, "bw_mean", mean);
956 json_object_add_value_float(dir_object, "bw_dev", dev);
957}
958
4d658652 959static void show_thread_status_terse_v2(struct thread_stat *ts,
a666cab8
JA
960 struct group_run_stats *rs,
961 struct buf_output *out)
4d658652
JA
962{
963 double io_u_dist[FIO_IO_U_MAP_NR];
964 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
965 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
966 double usr_cpu, sys_cpu;
967 int i;
968
969 /* General Info */
a666cab8 970 log_buf(out, "2;%s;%d;%d", ts->name, ts->groupid, ts->error);
4d658652 971 /* Log Read Status */
a666cab8 972 show_ddir_status_terse(ts, rs, DDIR_READ, out);
4d658652 973 /* Log Write Status */
a666cab8 974 show_ddir_status_terse(ts, rs, DDIR_WRITE, out);
6eaf09d6 975 /* Log Trim Status */
a666cab8 976 show_ddir_status_terse(ts, rs, DDIR_TRIM, out);
4d658652
JA
977
978 /* CPU Usage */
979 if (ts->total_run_time) {
980 double runt = (double) ts->total_run_time;
981
982 usr_cpu = (double) ts->usr_time * 100 / runt;
983 sys_cpu = (double) ts->sys_time * 100 / runt;
984 } else {
985 usr_cpu = 0;
986 sys_cpu = 0;
987 }
988
a666cab8 989 log_buf(out, ";%f%%;%f%%;%llu;%llu;%llu", usr_cpu, sys_cpu,
4e0a8fa2
JA
990 (unsigned long long) ts->ctx,
991 (unsigned long long) ts->majf,
992 (unsigned long long) ts->minf);
4d658652
JA
993
994 /* Calc % distribution of IO depths, usecond, msecond latency */
d79db122 995 stat_calc_dist(ts->io_u_map, ddir_rw_sum(ts->total_io_u), io_u_dist);
4d658652
JA
996 stat_calc_lat_u(ts, io_u_lat_u);
997 stat_calc_lat_m(ts, io_u_lat_m);
998
999 /* Only show fixed 7 I/O depth levels*/
a666cab8 1000 log_buf(out, ";%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%",
4d658652
JA
1001 io_u_dist[0], io_u_dist[1], io_u_dist[2], io_u_dist[3],
1002 io_u_dist[4], io_u_dist[5], io_u_dist[6]);
1003
1004 /* Microsecond latency */
1005 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
a666cab8 1006 log_buf(out, ";%3.2f%%", io_u_lat_u[i]);
4d658652
JA
1007 /* Millisecond latency */
1008 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
a666cab8 1009 log_buf(out, ";%3.2f%%", io_u_lat_m[i]);
4d658652
JA
1010 /* Additional output if continue_on_error set - default off*/
1011 if (ts->continue_on_error)
a666cab8
JA
1012 log_buf(out, ";%llu;%d", (unsigned long long) ts->total_err_count, ts->first_error);
1013 log_buf(out, "\n");
4d658652
JA
1014
1015 /* Additional output if description is set */
0a3c52f0 1016 if (strlen(ts->description))
a666cab8 1017 log_buf(out, ";%s", ts->description);
4d658652 1018
a666cab8 1019 log_buf(out, "\n");
4d658652
JA
1020}
1021
3449ab8c 1022static void show_thread_status_terse_v3_v4(struct thread_stat *ts,
a666cab8
JA
1023 struct group_run_stats *rs, int ver,
1024 struct buf_output *out)
c6ae0a5b 1025{
2270890c 1026 double io_u_dist[FIO_IO_U_MAP_NR];
04a0feae
JA
1027 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
1028 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
c6ae0a5b 1029 double usr_cpu, sys_cpu;
04a0feae 1030 int i;
c6ae0a5b 1031
562c2d2f 1032 /* General Info */
a666cab8 1033 log_buf(out, "%d;%s;%s;%d;%d", ver, fio_version_string,
5e726d0a 1034 ts->name, ts->groupid, ts->error);
562c2d2f 1035 /* Log Read Status */
a666cab8 1036 show_ddir_status_terse(ts, rs, DDIR_READ, out);
562c2d2f 1037 /* Log Write Status */
a666cab8 1038 show_ddir_status_terse(ts, rs, DDIR_WRITE, out);
6eaf09d6 1039 /* Log Trim Status */
3449ab8c 1040 if (ver == 4)
a666cab8 1041 show_ddir_status_terse(ts, rs, DDIR_TRIM, out);
c6ae0a5b 1042
562c2d2f 1043 /* CPU Usage */
756867bd
JA
1044 if (ts->total_run_time) {
1045 double runt = (double) ts->total_run_time;
c6ae0a5b 1046
756867bd
JA
1047 usr_cpu = (double) ts->usr_time * 100 / runt;
1048 sys_cpu = (double) ts->sys_time * 100 / runt;
c6ae0a5b
JA
1049 } else {
1050 usr_cpu = 0;
1051 sys_cpu = 0;
1052 }
1053
a666cab8 1054 log_buf(out, ";%f%%;%f%%;%llu;%llu;%llu", usr_cpu, sys_cpu,
4e0a8fa2
JA
1055 (unsigned long long) ts->ctx,
1056 (unsigned long long) ts->majf,
1057 (unsigned long long) ts->minf);
2270890c 1058
562c2d2f 1059 /* Calc % distribution of IO depths, usecond, msecond latency */
d79db122 1060 stat_calc_dist(ts->io_u_map, ddir_rw_sum(ts->total_io_u), io_u_dist);
04a0feae
JA
1061 stat_calc_lat_u(ts, io_u_lat_u);
1062 stat_calc_lat_m(ts, io_u_lat_m);
2270890c 1063
562c2d2f 1064 /* Only show fixed 7 I/O depth levels*/
a666cab8 1065 log_buf(out, ";%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%",
5ec10eaa
JA
1066 io_u_dist[0], io_u_dist[1], io_u_dist[2], io_u_dist[3],
1067 io_u_dist[4], io_u_dist[5], io_u_dist[6]);
2270890c 1068
562c2d2f 1069 /* Microsecond latency */
04a0feae 1070 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
a666cab8 1071 log_buf(out, ";%3.2f%%", io_u_lat_u[i]);
562c2d2f 1072 /* Millisecond latency */
04a0feae 1073 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
a666cab8 1074 log_buf(out, ";%3.2f%%", io_u_lat_m[i]);
f2f788dd
JA
1075
1076 /* disk util stats, if any */
a666cab8 1077 show_disk_util(1, NULL, out);
f2f788dd 1078
562c2d2f 1079 /* Additional output if continue_on_error set - default off*/
f2bba182 1080 if (ts->continue_on_error)
a666cab8 1081 log_buf(out, ";%llu;%d", (unsigned long long) ts->total_err_count, ts->first_error);
2270890c 1082
562c2d2f 1083 /* Additional output if description is set */
4b0f2258 1084 if (strlen(ts->description))
a666cab8 1085 log_buf(out, ";%s", ts->description);
946e4276 1086
a666cab8 1087 log_buf(out, "\n");
756867bd
JA
1088}
1089
0279b880
JA
1090void json_add_job_opts(struct json_object *root, const char *name,
1091 struct flist_head *opt_list, bool num_jobs)
66e19a38
JA
1092{
1093 struct json_object *dir_object;
1094 struct flist_head *entry;
1095 struct print_option *p;
1096
1097 if (flist_empty(opt_list))
1098 return;
1099
1100 dir_object = json_create_object();
1101 json_object_add_value_object(root, name, dir_object);
1102
1103 flist_for_each(entry, opt_list) {
1104 const char *pos = "";
1105
1106 p = flist_entry(entry, struct print_option, list);
876e1001
JA
1107 if (!num_jobs && !strcmp(p->name, "numjobs"))
1108 continue;
66e19a38
JA
1109 if (p->value)
1110 pos = p->value;
1111 json_object_add_value_string(dir_object, p->name, pos);
1112 }
1113}
1114
cc372b17 1115static struct json_object *show_thread_status_json(struct thread_stat *ts,
66e19a38
JA
1116 struct group_run_stats *rs,
1117 struct flist_head *opt_list)
cc372b17
SL
1118{
1119 struct json_object *root, *tmp;
b01af66b 1120 struct jobs_eta *je;
cc372b17
SL
1121 double io_u_dist[FIO_IO_U_MAP_NR];
1122 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
1123 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
1124 double usr_cpu, sys_cpu;
1125 int i;
b01af66b
CJ
1126 size_t size;
1127
cc372b17
SL
1128 root = json_create_object();
1129 json_object_add_value_string(root, "jobname", ts->name);
1130 json_object_add_value_int(root, "groupid", ts->groupid);
1131 json_object_add_value_int(root, "error", ts->error);
1132
b01af66b 1133 /* ETA Info */
c5103619 1134 je = get_jobs_eta(true, &size);
2de615ad
JA
1135 if (je) {
1136 json_object_add_value_int(root, "eta", je->eta_sec);
1137 json_object_add_value_int(root, "elapsed", je->elapsed_sec);
1138 }
b01af66b 1139
66e19a38 1140 if (opt_list)
876e1001 1141 json_add_job_opts(root, "job options", opt_list, true);
66e19a38 1142
cc372b17
SL
1143 add_ddir_status_json(ts, rs, DDIR_READ, root);
1144 add_ddir_status_json(ts, rs, DDIR_WRITE, root);
f3afa57e 1145 add_ddir_status_json(ts, rs, DDIR_TRIM, root);
cc372b17
SL
1146
1147 /* CPU Usage */
1148 if (ts->total_run_time) {
1149 double runt = (double) ts->total_run_time;
1150
1151 usr_cpu = (double) ts->usr_time * 100 / runt;
1152 sys_cpu = (double) ts->sys_time * 100 / runt;
1153 } else {
1154 usr_cpu = 0;
1155 sys_cpu = 0;
1156 }
1157 json_object_add_value_float(root, "usr_cpu", usr_cpu);
1158 json_object_add_value_float(root, "sys_cpu", sys_cpu);
1159 json_object_add_value_int(root, "ctx", ts->ctx);
1160 json_object_add_value_int(root, "majf", ts->majf);
1161 json_object_add_value_int(root, "minf", ts->minf);
1162
1163
1164 /* Calc % distribution of IO depths, usecond, msecond latency */
d79db122 1165 stat_calc_dist(ts->io_u_map, ddir_rw_sum(ts->total_io_u), io_u_dist);
cc372b17
SL
1166 stat_calc_lat_u(ts, io_u_lat_u);
1167 stat_calc_lat_m(ts, io_u_lat_m);
1168
1169 tmp = json_create_object();
1170 json_object_add_value_object(root, "iodepth_level", tmp);
1171 /* Only show fixed 7 I/O depth levels*/
1172 for (i = 0; i < 7; i++) {
1173 char name[20];
1174 if (i < 6)
98ffb8f3 1175 snprintf(name, 20, "%d", 1 << i);
cc372b17 1176 else
98ffb8f3 1177 snprintf(name, 20, ">=%d", 1 << i);
cc372b17
SL
1178 json_object_add_value_float(tmp, (const char *)name, io_u_dist[i]);
1179 }
1180
1181 tmp = json_create_object();
1182 json_object_add_value_object(root, "latency_us", tmp);
1183 /* Microsecond latency */
1184 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
1185 const char *ranges[] = { "2", "4", "10", "20", "50", "100",
1186 "250", "500", "750", "1000", };
1187 json_object_add_value_float(tmp, ranges[i], io_u_lat_u[i]);
1188 }
1189 /* Millisecond latency */
1190 tmp = json_create_object();
1191 json_object_add_value_object(root, "latency_ms", tmp);
1192 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++) {
1193 const char *ranges[] = { "2", "4", "10", "20", "50", "100",
1194 "250", "500", "750", "1000", "2000",
1195 ">=2000", };
1196 json_object_add_value_float(tmp, ranges[i], io_u_lat_m[i]);
1197 }
1198
1199 /* Additional output if continue_on_error set - default off*/
1200 if (ts->continue_on_error) {
1201 json_object_add_value_int(root, "total_err", ts->total_err_count);
952b05e0 1202 json_object_add_value_int(root, "first_error", ts->first_error);
cc372b17
SL
1203 }
1204
3e260a46
JA
1205 if (ts->latency_depth) {
1206 json_object_add_value_int(root, "latency_depth", ts->latency_depth);
1207 json_object_add_value_int(root, "latency_target", ts->latency_target);
1208 json_object_add_value_float(root, "latency_percentile", ts->latency_percentile.u.f);
1209 json_object_add_value_int(root, "latency_window", ts->latency_window);
1210 }
1211
cc372b17
SL
1212 /* Additional output if description is set */
1213 if (strlen(ts->description))
1214 json_object_add_value_string(root, "desc", ts->description);
1215
66347cfa
DE
1216 if (ts->nr_block_infos) {
1217 /* Block error histogram and types */
1218 int len;
1219 unsigned int *percentiles = NULL;
1220 unsigned int block_state_counts[BLOCK_STATE_COUNT];
1221
1222 len = calc_block_percentiles(ts->nr_block_infos, ts->block_infos,
1223 ts->percentile_list,
1224 &percentiles, block_state_counts);
1225
1226 if (len) {
1227 struct json_object *block, *percentile_object, *states;
8a68c41c 1228 int state;
66347cfa
DE
1229 block = json_create_object();
1230 json_object_add_value_object(root, "block", block);
1231
1232 percentile_object = json_create_object();
1233 json_object_add_value_object(block, "percentiles",
1234 percentile_object);
1235 for (i = 0; i < len; i++) {
1236 char buf[20];
1237 snprintf(buf, sizeof(buf), "%f",
1238 ts->percentile_list[i].u.f);
1239 json_object_add_value_int(percentile_object,
1240 (const char *)buf,
1241 percentiles[i]);
1242 }
1243
1244 states = json_create_object();
1245 json_object_add_value_object(block, "states", states);
1246 for (state = 0; state < BLOCK_STATE_COUNT; state++) {
1247 json_object_add_value_int(states,
1248 block_state_names[state],
1249 block_state_counts[state]);
1250 }
1251 free(percentiles);
1252 }
1253 }
1254
cc372b17
SL
1255 return root;
1256}
1257
4d658652 1258static void show_thread_status_terse(struct thread_stat *ts,
a666cab8
JA
1259 struct group_run_stats *rs,
1260 struct buf_output *out)
4d658652
JA
1261{
1262 if (terse_version == 2)
a666cab8 1263 show_thread_status_terse_v2(ts, rs, out);
3449ab8c 1264 else if (terse_version == 3 || terse_version == 4)
a666cab8 1265 show_thread_status_terse_v3_v4(ts, rs, terse_version, out);
4d658652
JA
1266 else
1267 log_err("fio: bad terse version!? %d\n", terse_version);
1268}
1269
952b05e0 1270struct json_object *show_thread_status(struct thread_stat *ts,
a666cab8 1271 struct group_run_stats *rs,
0279b880 1272 struct flist_head *opt_list,
a666cab8 1273 struct buf_output *out)
952b05e0 1274{
129fb2d4
JA
1275 struct json_object *ret = NULL;
1276
1277 if (output_format & FIO_OUTPUT_TERSE)
a666cab8 1278 show_thread_status_terse(ts, rs, out);
129fb2d4 1279 if (output_format & FIO_OUTPUT_JSON)
0279b880 1280 ret = show_thread_status_json(ts, rs, opt_list);
129fb2d4 1281 if (output_format & FIO_OUTPUT_NORMAL)
a666cab8 1282 show_thread_status_normal(ts, rs, out);
129fb2d4
JA
1283
1284 return ret;
952b05e0
CF
1285}
1286
fd595830 1287static void sum_stat(struct io_stat *dst, struct io_stat *src, bool first)
756867bd
JA
1288{
1289 double mean, S;
1290
e09231c2
ZL
1291 if (src->samples == 0)
1292 return;
1293
756867bd
JA
1294 dst->min_val = min(dst->min_val, src->min_val);
1295 dst->max_val = max(dst->max_val, src->max_val);
756867bd
JA
1296
1297 /*
cdcac5cf
YH
1298 * Compute new mean and S after the merge
1299 * <http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
1300 * #Parallel_algorithm>
756867bd 1301 */
fd595830 1302 if (first) {
802ad4a8
JA
1303 mean = src->mean.u.f;
1304 S = src->S.u.f;
756867bd 1305 } else {
802ad4a8 1306 double delta = src->mean.u.f - dst->mean.u.f;
cdcac5cf 1307
802ad4a8
JA
1308 mean = ((src->mean.u.f * src->samples) +
1309 (dst->mean.u.f * dst->samples)) /
cdcac5cf
YH
1310 (dst->samples + src->samples);
1311
802ad4a8 1312 S = src->S.u.f + dst->S.u.f + pow(delta, 2.0) *
cdcac5cf
YH
1313 (dst->samples * src->samples) /
1314 (dst->samples + src->samples);
756867bd
JA
1315 }
1316
cdcac5cf 1317 dst->samples += src->samples;
802ad4a8
JA
1318 dst->mean.u.f = mean;
1319 dst->S.u.f = S;
756867bd
JA
1320}
1321
37f0c1ae
JA
1322void sum_group_stats(struct group_run_stats *dst, struct group_run_stats *src)
1323{
1324 int i;
1325
6eaf09d6 1326 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
37f0c1ae
JA
1327 if (dst->max_run[i] < src->max_run[i])
1328 dst->max_run[i] = src->max_run[i];
1329 if (dst->min_run[i] && dst->min_run[i] > src->min_run[i])
1330 dst->min_run[i] = src->min_run[i];
1331 if (dst->max_bw[i] < src->max_bw[i])
1332 dst->max_bw[i] = src->max_bw[i];
1333 if (dst->min_bw[i] && dst->min_bw[i] > src->min_bw[i])
1334 dst->min_bw[i] = src->min_bw[i];
1335
1336 dst->io_kb[i] += src->io_kb[i];
1337 dst->agg[i] += src->agg[i];
1338 }
1339
dbae1bd6
JA
1340 if (!dst->kb_base)
1341 dst->kb_base = src->kb_base;
1342 if (!dst->unit_base)
1343 dst->unit_base = src->unit_base;
37f0c1ae
JA
1344}
1345
fd595830
JA
1346void sum_thread_stats(struct thread_stat *dst, struct thread_stat *src,
1347 bool first)
5b9babb7
JA
1348{
1349 int l, k;
1350
6eaf09d6 1351 for (l = 0; l < DDIR_RWDIR_CNT; l++) {
771e58be 1352 if (!dst->unified_rw_rep) {
fd595830
JA
1353 sum_stat(&dst->clat_stat[l], &src->clat_stat[l], first);
1354 sum_stat(&dst->slat_stat[l], &src->slat_stat[l], first);
1355 sum_stat(&dst->lat_stat[l], &src->lat_stat[l], first);
1356 sum_stat(&dst->bw_stat[l], &src->bw_stat[l], first);
771e58be
JA
1357
1358 dst->io_bytes[l] += src->io_bytes[l];
1359
1360 if (dst->runtime[l] < src->runtime[l])
1361 dst->runtime[l] = src->runtime[l];
1362 } else {
fd595830
JA
1363 sum_stat(&dst->clat_stat[0], &src->clat_stat[l], first);
1364 sum_stat(&dst->slat_stat[0], &src->slat_stat[l], first);
1365 sum_stat(&dst->lat_stat[0], &src->lat_stat[l], first);
1366 sum_stat(&dst->bw_stat[0], &src->bw_stat[l], first);
771e58be
JA
1367
1368 dst->io_bytes[0] += src->io_bytes[l];
1369
1370 if (dst->runtime[0] < src->runtime[l])
1371 dst->runtime[0] = src->runtime[l];
fd595830
JA
1372
1373 /*
1374 * We're summing to the same destination, so override
1375 * 'first' after the first iteration of the loop
1376 */
1377 first = false;
771e58be 1378 }
5b9babb7
JA
1379 }
1380
1381 dst->usr_time += src->usr_time;
1382 dst->sys_time += src->sys_time;
1383 dst->ctx += src->ctx;
1384 dst->majf += src->majf;
1385 dst->minf += src->minf;
1386
1387 for (k = 0; k < FIO_IO_U_MAP_NR; k++)
1388 dst->io_u_map[k] += src->io_u_map[k];
1389 for (k = 0; k < FIO_IO_U_MAP_NR; k++)
1390 dst->io_u_submit[k] += src->io_u_submit[k];
1391 for (k = 0; k < FIO_IO_U_MAP_NR; k++)
1392 dst->io_u_complete[k] += src->io_u_complete[k];
1393 for (k = 0; k < FIO_IO_U_LAT_U_NR; k++)
1394 dst->io_u_lat_u[k] += src->io_u_lat_u[k];
1395 for (k = 0; k < FIO_IO_U_LAT_M_NR; k++)
1396 dst->io_u_lat_m[k] += src->io_u_lat_m[k];
1397
6eaf09d6 1398 for (k = 0; k < DDIR_RWDIR_CNT; k++) {
771e58be
JA
1399 if (!dst->unified_rw_rep) {
1400 dst->total_io_u[k] += src->total_io_u[k];
1401 dst->short_io_u[k] += src->short_io_u[k];
3bcb9d94 1402 dst->drop_io_u[k] += src->drop_io_u[k];
771e58be
JA
1403 } else {
1404 dst->total_io_u[0] += src->total_io_u[k];
1405 dst->short_io_u[0] += src->short_io_u[k];
3bcb9d94 1406 dst->drop_io_u[0] += src->drop_io_u[k];
771e58be 1407 }
5b9babb7
JA
1408 }
1409
6eaf09d6 1410 for (k = 0; k < DDIR_RWDIR_CNT; k++) {
5b9babb7 1411 int m;
771e58be
JA
1412
1413 for (m = 0; m < FIO_IO_U_PLAT_NR; m++) {
1414 if (!dst->unified_rw_rep)
1415 dst->io_u_plat[k][m] += src->io_u_plat[k][m];
1416 else
1417 dst->io_u_plat[0][m] += src->io_u_plat[k][m];
1418 }
5b9babb7
JA
1419 }
1420
1421 dst->total_run_time += src->total_run_time;
1422 dst->total_submit += src->total_submit;
1423 dst->total_complete += src->total_complete;
1424}
1425
37f0c1ae
JA
1426void init_group_run_stat(struct group_run_stats *gs)
1427{
6eaf09d6 1428 int i;
37f0c1ae 1429 memset(gs, 0, sizeof(*gs));
6eaf09d6
SL
1430
1431 for (i = 0; i < DDIR_RWDIR_CNT; i++)
1432 gs->min_bw[i] = gs->min_run[i] = ~0UL;
37f0c1ae
JA
1433}
1434
1435void init_thread_stat(struct thread_stat *ts)
1436{
1437 int j;
1438
1439 memset(ts, 0, sizeof(*ts));
1440
6eaf09d6 1441 for (j = 0; j < DDIR_RWDIR_CNT; j++) {
37f0c1ae
JA
1442 ts->lat_stat[j].min_val = -1UL;
1443 ts->clat_stat[j].min_val = -1UL;
1444 ts->slat_stat[j].min_val = -1UL;
1445 ts->bw_stat[j].min_val = -1UL;
1446 }
1447 ts->groupid = -1;
1448}
1449
83f7b64e 1450void __show_run_stats(void)
3c39a379
JA
1451{
1452 struct group_run_stats *runstats, *rs;
1453 struct thread_data *td;
756867bd 1454 struct thread_stat *threadstats, *ts;
4da24b69 1455 int i, j, k, nr_ts, last_ts, idx;
90fef2d1 1456 int kb_base_warned = 0;
ad705bcb 1457 int unit_base_warned = 0;
cc372b17
SL
1458 struct json_object *root = NULL;
1459 struct json_array *array = NULL;
a666cab8 1460 struct buf_output output[FIO_OUTPUT_NR];
66e19a38 1461 struct flist_head **opt_lists;
a666cab8 1462
3c39a379
JA
1463 runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1));
1464
37f0c1ae
JA
1465 for (i = 0; i < groupid + 1; i++)
1466 init_group_run_stat(&runstats[i]);
3c39a379 1467
756867bd
JA
1468 /*
1469 * find out how many threads stats we need. if group reporting isn't
1470 * enabled, it's one-per-td.
1471 */
1472 nr_ts = 0;
1473 last_ts = -1;
1474 for_each_td(td, i) {
2dc1bbeb 1475 if (!td->o.group_reporting) {
756867bd
JA
1476 nr_ts++;
1477 continue;
1478 }
1479 if (last_ts == td->groupid)
1480 continue;
1481
1482 last_ts = td->groupid;
1483 nr_ts++;
1484 }
1485
1486 threadstats = malloc(nr_ts * sizeof(struct thread_stat));
66e19a38 1487 opt_lists = malloc(nr_ts * sizeof(struct flist_head *));
756867bd 1488
66e19a38 1489 for (i = 0; i < nr_ts; i++) {
37f0c1ae 1490 init_thread_stat(&threadstats[i]);
66e19a38
JA
1491 opt_lists[i] = NULL;
1492 }
756867bd
JA
1493
1494 j = 0;
1495 last_ts = -1;
197574e4 1496 idx = 0;
34572e28 1497 for_each_td(td, i) {
2dc1bbeb
JA
1498 if (idx && (!td->o.group_reporting ||
1499 (td->o.group_reporting && last_ts != td->groupid))) {
7abd0e3a
JA
1500 idx = 0;
1501 j++;
1502 }
1503
1504 last_ts = td->groupid;
1505
756867bd
JA
1506 ts = &threadstats[j];
1507
83349190 1508 ts->clat_percentiles = td->o.clat_percentiles;
435d195a 1509 ts->percentile_precision = td->o.percentile_precision;
fd112d34 1510 memcpy(ts->percentile_list, td->o.percentile_list, sizeof(td->o.percentile_list));
66e19a38 1511 opt_lists[j] = &td->opt_list;
83349190 1512
197574e4 1513 idx++;
6586ee89 1514 ts->members++;
756867bd 1515
7abd0e3a 1516 if (ts->groupid == -1) {
2dc84ba7
JA
1517 /*
1518 * These are per-group shared already
1519 */
4e59d0f3 1520 strncpy(ts->name, td->o.name, FIO_JOBNAME_SIZE - 1);
a64e88da
JA
1521 if (td->o.description)
1522 strncpy(ts->description, td->o.description,
4e59d0f3 1523 FIO_JOBDESC_SIZE - 1);
a64e88da 1524 else
4e59d0f3 1525 memset(ts->description, 0, FIO_JOBDESC_SIZE);
a64e88da 1526
2f122b13
JA
1527 /*
1528 * If multiple entries in this group, this is
1529 * the first member.
1530 */
1531 ts->thread_number = td->thread_number;
756867bd 1532 ts->groupid = td->groupid;
2dc84ba7
JA
1533
1534 /*
1535 * first pid in group, not very useful...
1536 */
756867bd 1537 ts->pid = td->pid;
90fef2d1
JA
1538
1539 ts->kb_base = td->o.kb_base;
ad705bcb 1540 ts->unit_base = td->o.unit_base;
771e58be 1541 ts->unified_rw_rep = td->o.unified_rw_rep;
90fef2d1
JA
1542 } else if (ts->kb_base != td->o.kb_base && !kb_base_warned) {
1543 log_info("fio: kb_base differs for jobs in group, using"
1544 " %u as the base\n", ts->kb_base);
1545 kb_base_warned = 1;
ad705bcb
SN
1546 } else if (ts->unit_base != td->o.unit_base && !unit_base_warned) {
1547 log_info("fio: unit_base differs for jobs in group, using"
1548 " %u as the base\n", ts->unit_base);
1549 unit_base_warned = 1;
2dc84ba7
JA
1550 }
1551
f2bba182
RR
1552 ts->continue_on_error = td->o.continue_on_error;
1553 ts->total_err_count += td->total_err_count;
1554 ts->first_error = td->first_error;
1555 if (!ts->error) {
1556 if (!td->error && td->o.continue_on_error &&
1557 td->first_error) {
1558 ts->error = td->first_error;
3660ceae
JA
1559 ts->verror[sizeof(ts->verror) - 1] = '\0';
1560 strncpy(ts->verror, td->verror, sizeof(ts->verror) - 1);
f2bba182
RR
1561 } else if (td->error) {
1562 ts->error = td->error;
3660ceae
JA
1563 ts->verror[sizeof(ts->verror) - 1] = '\0';
1564 strncpy(ts->verror, td->verror, sizeof(ts->verror) - 1);
f2bba182 1565 }
756867bd
JA
1566 }
1567
3e260a46
JA
1568 ts->latency_depth = td->latency_qd;
1569 ts->latency_target = td->o.latency_target;
1570 ts->latency_percentile = td->o.latency_percentile;
1571 ts->latency_window = td->o.latency_window;
1572
0e4dd95c 1573 ts->nr_block_infos = td->ts.nr_block_infos;
4da24b69
JA
1574 for (k = 0; k < ts->nr_block_infos; k++)
1575 ts->block_infos[k] = td->ts.block_infos[k];
0e4dd95c 1576
fd595830 1577 sum_thread_stats(ts, &td->ts, idx == 1);
756867bd
JA
1578 }
1579
1580 for (i = 0; i < nr_ts; i++) {
94370ac4 1581 unsigned long long bw;
3c39a379 1582
756867bd 1583 ts = &threadstats[i];
dedb88eb
JA
1584 if (ts->groupid == -1)
1585 continue;
756867bd 1586 rs = &runstats[ts->groupid];
90fef2d1 1587 rs->kb_base = ts->kb_base;
ad705bcb 1588 rs->unit_base = ts->unit_base;
771e58be 1589 rs->unified_rw_rep += ts->unified_rw_rep;
3c39a379 1590
6eaf09d6 1591 for (j = 0; j < DDIR_RWDIR_CNT; j++) {
94370ac4
JA
1592 if (!ts->runtime[j])
1593 continue;
1594 if (ts->runtime[j] < rs->min_run[j] || !rs->min_run[j])
1595 rs->min_run[j] = ts->runtime[j];
1596 if (ts->runtime[j] > rs->max_run[j])
1597 rs->max_run[j] = ts->runtime[j];
1598
1599 bw = 0;
8879fd15 1600 if (ts->runtime[j]) {
c857cfeb
JA
1601 unsigned long runt = ts->runtime[j];
1602 unsigned long long kb;
8879fd15 1603
c857cfeb
JA
1604 kb = ts->io_bytes[j] / rs->kb_base;
1605 bw = kb * 1000 / runt;
8879fd15 1606 }
94370ac4
JA
1607 if (bw < rs->min_bw[j])
1608 rs->min_bw[j] = bw;
1609 if (bw > rs->max_bw[j])
1610 rs->max_bw[j] = bw;
1611
90fef2d1 1612 rs->io_kb[j] += ts->io_bytes[j] / rs->kb_base;
94370ac4 1613 }
3c39a379
JA
1614 }
1615
1616 for (i = 0; i < groupid + 1; i++) {
6eaf09d6
SL
1617 int ddir;
1618
3c39a379
JA
1619 rs = &runstats[i];
1620
6eaf09d6
SL
1621 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) {
1622 if (rs->max_run[ddir])
1623 rs->agg[ddir] = (rs->io_kb[ddir] * 1000) /
1624 rs->max_run[ddir];
1625 }
3c39a379
JA
1626 }
1627
a666cab8 1628 for (i = 0; i < FIO_OUTPUT_NR; i++)
e250c0a9 1629 buf_output_init(&output[i]);
a666cab8 1630
3c39a379
JA
1631 /*
1632 * don't overwrite last signal output
1633 */
129fb2d4 1634 if (output_format & FIO_OUTPUT_NORMAL)
a666cab8 1635 log_buf(&output[__FIO_OUTPUT_NORMAL], "\n");
129fb2d4 1636 if (output_format & FIO_OUTPUT_JSON) {
66e19a38 1637 struct thread_data *global;
35326842 1638 char time_buf[32];
91e53870
SO
1639 time_t time_p;
1640
1641 time(&time_p);
1642 os_ctime_r((const time_t *) &time_p, time_buf,
1643 sizeof(time_buf));
1644 time_buf[strlen(time_buf) - 1] = '\0';
1645
cc372b17
SL
1646 root = json_create_object();
1647 json_object_add_value_string(root, "fio version", fio_version_string);
91e53870
SO
1648 json_object_add_value_int(root, "timestamp", time_p);
1649 json_object_add_value_string(root, "time", time_buf);
66e19a38 1650 global = get_global_options();
876e1001 1651 json_add_job_opts(root, "global options", &global->opt_list, false);
cc372b17
SL
1652 array = json_create_array();
1653 json_object_add_value_array(root, "jobs", array);
1654 }
3c39a379 1655
0279b880
JA
1656 if (is_backend)
1657 fio_server_send_job_options(&get_global_options()->opt_list, -1U);
1658
756867bd
JA
1659 for (i = 0; i < nr_ts; i++) {
1660 ts = &threadstats[i];
1661 rs = &runstats[ts->groupid];
3c39a379 1662
0279b880
JA
1663 if (is_backend) {
1664 fio_server_send_job_options(opt_lists[i], i);
a64e88da 1665 fio_server_send_ts(ts, rs);
0279b880 1666 } else {
129fb2d4 1667 if (output_format & FIO_OUTPUT_TERSE)
a666cab8 1668 show_thread_status_terse(ts, rs, &output[__FIO_OUTPUT_TERSE]);
129fb2d4 1669 if (output_format & FIO_OUTPUT_JSON) {
66e19a38 1670 struct json_object *tmp = show_thread_status_json(ts, rs, opt_lists[i]);
129fb2d4
JA
1671 json_array_add_value_object(array, tmp);
1672 }
1673 if (output_format & FIO_OUTPUT_NORMAL)
a666cab8 1674 show_thread_status_normal(ts, rs, &output[__FIO_OUTPUT_NORMAL]);
129fb2d4 1675 }
3c39a379 1676 }
ab34ddd1 1677 if (!is_backend && (output_format & FIO_OUTPUT_JSON)) {
cc372b17 1678 /* disk util stats, if any */
a666cab8 1679 show_disk_util(1, root, &output[__FIO_OUTPUT_JSON]);
cc372b17 1680
a666cab8 1681 show_idle_prof_stats(FIO_OUTPUT_JSON, root, &output[__FIO_OUTPUT_JSON]);
f2a2ce0e 1682
a666cab8
JA
1683 json_print_object(root, &output[__FIO_OUTPUT_JSON]);
1684 log_buf(&output[__FIO_OUTPUT_JSON], "\n");
cc372b17
SL
1685 json_free_object(root);
1686 }
3c39a379 1687
72c27ff8
JA
1688 for (i = 0; i < groupid + 1; i++) {
1689 rs = &runstats[i];
3c39a379 1690
72c27ff8 1691 rs->groupid = i;
d09a64a0 1692 if (is_backend)
72c27ff8 1693 fio_server_send_gs(rs);
129fb2d4 1694 else if (output_format & FIO_OUTPUT_NORMAL)
a666cab8 1695 show_group_stats(rs, &output[__FIO_OUTPUT_NORMAL]);
c6ae0a5b 1696 }
eecf272f 1697
72c27ff8
JA
1698 if (is_backend)
1699 fio_server_send_du();
129fb2d4 1700 else if (output_format & FIO_OUTPUT_NORMAL) {
a666cab8
JA
1701 show_disk_util(0, NULL, &output[__FIO_OUTPUT_NORMAL]);
1702 show_idle_prof_stats(FIO_OUTPUT_NORMAL, NULL, &output[__FIO_OUTPUT_NORMAL]);
60904003 1703 }
f2a2ce0e 1704
3d57c0e9 1705 for (i = 0; i < FIO_OUTPUT_NR; i++) {
a666cab8 1706 buf_output_flush(&output[i]);
3d57c0e9
JA
1707 buf_output_free(&output[i]);
1708 }
2b8c71b0 1709
fdd5f15f 1710 log_info_flush();
eecf272f 1711 free(runstats);
756867bd 1712 free(threadstats);
66e19a38 1713 free(opt_lists);
3c39a379
JA
1714}
1715
cef9175e
JA
1716void show_run_stats(void)
1717{
1718 fio_mutex_down(stat_mutex);
1719 __show_run_stats();
1720 fio_mutex_up(stat_mutex);
1721}
1722
5ddc6707 1723void __show_running_run_stats(void)
b852e7cf
JA
1724{
1725 struct thread_data *td;
1726 unsigned long long *rt;
1727 struct timeval tv;
1728 int i;
1729
90811930
JA
1730 fio_mutex_down(stat_mutex);
1731
b852e7cf
JA
1732 rt = malloc(thread_number * sizeof(unsigned long long));
1733 fio_gettime(&tv, NULL);
1734
1735 for_each_td(td, i) {
c97f1ad6 1736 td->update_rusage = 1;
6eaf09d6
SL
1737 td->ts.io_bytes[DDIR_READ] = td->io_bytes[DDIR_READ];
1738 td->ts.io_bytes[DDIR_WRITE] = td->io_bytes[DDIR_WRITE];
1739 td->ts.io_bytes[DDIR_TRIM] = td->io_bytes[DDIR_TRIM];
b852e7cf 1740 td->ts.total_run_time = mtime_since(&td->epoch, &tv);
6c041a88 1741
1742 rt[i] = mtime_since(&td->start, &tv);
1743 if (td_read(td) && td->ts.io_bytes[DDIR_READ])
1744 td->ts.runtime[DDIR_READ] += rt[i];
1745 if (td_write(td) && td->ts.io_bytes[DDIR_WRITE])
1746 td->ts.runtime[DDIR_WRITE] += rt[i];
1747 if (td_trim(td) && td->ts.io_bytes[DDIR_TRIM])
1748 td->ts.runtime[DDIR_TRIM] += rt[i];
b852e7cf
JA
1749 }
1750
c97f1ad6 1751 for_each_td(td, i) {
fda2cfac
JA
1752 if (td->runstate >= TD_EXITED)
1753 continue;
c97f1ad6
JA
1754 if (td->rusage_sem) {
1755 td->update_rusage = 1;
1756 fio_mutex_down(td->rusage_sem);
1757 }
1758 td->update_rusage = 0;
1759 }
1760
cef9175e 1761 __show_run_stats();
b852e7cf
JA
1762
1763 for_each_td(td, i) {
6c041a88 1764 if (td_read(td) && td->ts.io_bytes[DDIR_READ])
b852e7cf 1765 td->ts.runtime[DDIR_READ] -= rt[i];
6c041a88 1766 if (td_write(td) && td->ts.io_bytes[DDIR_WRITE])
b852e7cf 1767 td->ts.runtime[DDIR_WRITE] -= rt[i];
6c041a88 1768 if (td_trim(td) && td->ts.io_bytes[DDIR_TRIM])
6eaf09d6 1769 td->ts.runtime[DDIR_TRIM] -= rt[i];
b852e7cf
JA
1770 }
1771
1772 free(rt);
cef9175e 1773 fio_mutex_up(stat_mutex);
b852e7cf
JA
1774}
1775
06464907
JA
1776static int status_interval_init;
1777static struct timeval status_time;
77d99675 1778static int status_file_disabled;
06464907 1779
dac45f23 1780#define FIO_STATUS_FILE "fio-dump-status"
06464907
JA
1781
1782static int check_status_file(void)
1783{
1784 struct stat sb;
a0bafb7d
BC
1785 const char *temp_dir;
1786 char fio_status_file_path[PATH_MAX];
06464907 1787
77d99675
JA
1788 if (status_file_disabled)
1789 return 0;
1790
a0bafb7d 1791 temp_dir = getenv("TMPDIR");
48b16e27 1792 if (temp_dir == NULL) {
a0bafb7d 1793 temp_dir = getenv("TEMP");
48b16e27
JA
1794 if (temp_dir && strlen(temp_dir) >= PATH_MAX)
1795 temp_dir = NULL;
1796 }
a0bafb7d
BC
1797 if (temp_dir == NULL)
1798 temp_dir = "/tmp";
1799
1800 snprintf(fio_status_file_path, sizeof(fio_status_file_path), "%s/%s", temp_dir, FIO_STATUS_FILE);
1801
1802 if (stat(fio_status_file_path, &sb))
06464907
JA
1803 return 0;
1804
77d99675
JA
1805 if (unlink(fio_status_file_path) < 0) {
1806 log_err("fio: failed to unlink %s: %s\n", fio_status_file_path,
1807 strerror(errno));
1808 log_err("fio: disabling status file updates\n");
1809 status_file_disabled = 1;
1810 }
1811
06464907
JA
1812 return 1;
1813}
1814
1815void check_for_running_stats(void)
1816{
1817 if (status_interval) {
1818 if (!status_interval_init) {
1819 fio_gettime(&status_time, NULL);
1820 status_interval_init = 1;
1821 } else if (mtime_since_now(&status_time) >= status_interval) {
1822 show_running_run_stats();
1823 fio_gettime(&status_time, NULL);
1824 return;
1825 }
1826 }
1827 if (check_status_file()) {
1828 show_running_run_stats();
1829 return;
1830 }
1831}
1832
68704084 1833static inline void add_stat_sample(struct io_stat *is, unsigned long data)
3c39a379 1834{
68704084 1835 double val = data;
6660cc67 1836 double delta;
68704084
JA
1837
1838 if (data > is->max_val)
1839 is->max_val = data;
1840 if (data < is->min_val)
1841 is->min_val = data;
1842
802ad4a8 1843 delta = val - is->mean.u.f;
ef11d737 1844 if (delta) {
802ad4a8
JA
1845 is->mean.u.f += delta / (is->samples + 1.0);
1846 is->S.u.f += delta * (val - is->mean.u.f);
ef11d737 1847 }
3c39a379 1848
3c39a379
JA
1849 is->samples++;
1850}
1851
bb3884d8 1852static void __add_log_sample(struct io_log *iolog, unsigned long val,
306ddc97 1853 enum fio_ddir ddir, unsigned int bs,
ae588852 1854 unsigned long t, uint64_t offset)
3c39a379 1855{
aee2ab67 1856 uint64_t nr_samples = iolog->nr_samples;
ae588852 1857 struct io_sample *s;
306ddc97 1858
3c568239
JA
1859 if (iolog->disabled)
1860 return;
1861
b8bc8cba
JA
1862 if (!iolog->nr_samples)
1863 iolog->avg_last = t;
1864
3c39a379 1865 if (iolog->nr_samples == iolog->max_samples) {
a47591e4 1866 size_t new_size, new_samples;
3c568239 1867 void *new_log;
3c39a379 1868
a47591e4
JA
1869 if (!iolog->max_samples)
1870 new_samples = DEF_LOG_ENTRIES;
1871 else
1872 new_samples = iolog->max_samples * 2;
1873
1874 new_size = new_samples * log_entry_sz(iolog);
aee2ab67
JA
1875
1876 if (iolog->log_gz && (new_size > iolog->log_gz)) {
a47591e4
JA
1877 if (!iolog->log) {
1878 iolog->log = malloc(new_size);
1879 iolog->max_samples = new_samples;
1880 } else if (iolog_flush(iolog, 0)) {
aee2ab67
JA
1881 log_err("fio: failed flushing iolog! Will stop logging.\n");
1882 iolog->disabled = 1;
1883 return;
1884 }
1885 nr_samples = iolog->nr_samples;
1886 } else {
1887 new_log = realloc(iolog->log, new_size);
1888 if (!new_log) {
1889 log_err("fio: failed extending iolog! Will stop logging.\n");
1890 iolog->disabled = 1;
1891 return;
1892 }
1893 iolog->log = new_log;
a47591e4 1894 iolog->max_samples = new_samples;
3c568239 1895 }
3c39a379
JA
1896 }
1897
ae588852
JA
1898 s = get_sample(iolog, nr_samples);
1899
1900 s->val = val;
1901 s->time = t;
b26317c9 1902 io_sample_set_ddir(iolog, s, ddir);
ae588852
JA
1903 s->bs = bs;
1904
1905 if (iolog->log_offset) {
1906 struct io_sample_offset *so = (void *) s;
1907
1908 so->offset = offset;
1909 }
1910
3c39a379
JA
1911 iolog->nr_samples++;
1912}
1913
7fb28d36
JA
1914static inline void reset_io_stat(struct io_stat *ios)
1915{
1916 ios->max_val = ios->min_val = ios->samples = 0;
1917 ios->mean.u.f = ios->S.u.f = 0;
1918}
1919
6bb58215
JA
1920void reset_io_stats(struct thread_data *td)
1921{
1922 struct thread_stat *ts = &td->ts;
1923 int i, j;
1924
1925 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1926 reset_io_stat(&ts->clat_stat[i]);
1927 reset_io_stat(&ts->slat_stat[i]);
1928 reset_io_stat(&ts->lat_stat[i]);
1929 reset_io_stat(&ts->bw_stat[i]);
1930 reset_io_stat(&ts->iops_stat[i]);
1931
1932 ts->io_bytes[i] = 0;
1933 ts->runtime[i] = 0;
1934
1935 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
1936 ts->io_u_plat[i][j] = 0;
1937 }
1938
1939 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
1940 ts->io_u_map[i] = 0;
1941 ts->io_u_submit[i] = 0;
1942 ts->io_u_complete[i] = 0;
1943 ts->io_u_lat_u[i] = 0;
1944 ts->io_u_lat_m[i] = 0;
1945 ts->total_submit = 0;
1946 ts->total_complete = 0;
1947 }
1948
1949 for (i = 0; i < 3; i++) {
1950 ts->total_io_u[i] = 0;
1951 ts->short_io_u[i] = 0;
3bcb9d94 1952 ts->drop_io_u[i] = 0;
6bb58215
JA
1953 }
1954}
1955
d96d3bb3 1956static void __add_stat_to_log(struct io_log *iolog, enum fio_ddir ddir,
e6989e10 1957 unsigned long elapsed, bool log_max)
99007068
PO
1958{
1959 /*
1960 * Note an entry in the log. Use the mean from the logged samples,
1961 * making sure to properly round up. Only write a log entry if we
1962 * had actual samples done.
1963 */
d96d3bb3
JA
1964 if (iolog->avg_window[ddir].samples) {
1965 unsigned long val;
99007068 1966
e6989e10
JA
1967 if (log_max)
1968 val = iolog->avg_window[ddir].max_val;
1969 else
1970 val = iolog->avg_window[ddir].mean.u.f + 0.50;
1971
d96d3bb3 1972 __add_log_sample(iolog, val, ddir, 0, elapsed, 0);
99007068 1973 }
99007068 1974
d96d3bb3
JA
1975 reset_io_stat(&iolog->avg_window[ddir]);
1976}
99007068 1977
e6989e10
JA
1978static void _add_stat_to_log(struct io_log *iolog, unsigned long elapsed,
1979 bool log_max)
d96d3bb3
JA
1980{
1981 int ddir;
99007068 1982
d96d3bb3 1983 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++)
e6989e10 1984 __add_stat_to_log(iolog, ddir, elapsed, log_max);
99007068
PO
1985}
1986
bb3884d8 1987static void add_log_sample(struct thread_data *td, struct io_log *iolog,
306ddc97 1988 unsigned long val, enum fio_ddir ddir,
ae588852 1989 unsigned int bs, uint64_t offset)
bb3884d8 1990{
7fb28d36 1991 unsigned long elapsed, this_window;
b8bc8cba 1992
ff58fced
JA
1993 if (!ddir_rw(ddir))
1994 return;
1995
b8bc8cba
JA
1996 elapsed = mtime_since_now(&td->epoch);
1997
1998 /*
1999 * If no time averaging, just add the log sample.
2000 */
2001 if (!iolog->avg_msec) {
ae588852 2002 __add_log_sample(iolog, val, ddir, bs, elapsed, offset);
b8bc8cba
JA
2003 return;
2004 }
2005
2006 /*
2007 * Add the sample. If the time period has passed, then
2008 * add that entry to the log and clear.
2009 */
2010 add_stat_sample(&iolog->avg_window[ddir], val);
2011
7fb28d36
JA
2012 /*
2013 * If period hasn't passed, adding the above sample is all we
2014 * need to do.
2015 */
b8bc8cba
JA
2016 this_window = elapsed - iolog->avg_last;
2017 if (this_window < iolog->avg_msec)
2018 return;
2019
e6989e10 2020 _add_stat_to_log(iolog, elapsed, td->o.log_max != 0);
b8bc8cba 2021
99007068
PO
2022 iolog->avg_last = elapsed;
2023}
6eaf09d6 2024
a47591e4 2025void finalize_logs(struct thread_data *td, bool unit_logs)
99007068
PO
2026{
2027 unsigned long elapsed;
6eaf09d6 2028
99007068 2029 elapsed = mtime_since_now(&td->epoch);
b8bc8cba 2030
a47591e4 2031 if (td->clat_log && unit_logs)
e6989e10 2032 _add_stat_to_log(td->clat_log, elapsed, td->o.log_max != 0);
a47591e4 2033 if (td->slat_log && unit_logs)
e6989e10 2034 _add_stat_to_log(td->slat_log, elapsed, td->o.log_max != 0);
a47591e4 2035 if (td->lat_log && unit_logs)
e6989e10 2036 _add_stat_to_log(td->lat_log, elapsed, td->o.log_max != 0);
a47591e4 2037 if (td->bw_log && (unit_logs == per_unit_log(td->bw_log)))
e6989e10 2038 _add_stat_to_log(td->bw_log, elapsed, td->o.log_max != 0);
a47591e4 2039 if (td->iops_log && (unit_logs == per_unit_log(td->iops_log)))
e6989e10 2040 _add_stat_to_log(td->iops_log, elapsed, td->o.log_max != 0);
bb3884d8
JA
2041}
2042
306ddc97 2043void add_agg_sample(unsigned long val, enum fio_ddir ddir, unsigned int bs)
bb3884d8 2044{
ff58fced 2045 struct io_log *iolog;
bb3884d8 2046
ff58fced
JA
2047 if (!ddir_rw(ddir))
2048 return;
2049
2050 iolog = agg_io_log[ddir];
ae588852 2051 __add_log_sample(iolog, val, ddir, bs, mtime_since_genesis(), 0);
bb3884d8
JA
2052}
2053
83349190
YH
2054static void add_clat_percentile_sample(struct thread_stat *ts,
2055 unsigned long usec, enum fio_ddir ddir)
2056{
2057 unsigned int idx = plat_val_to_idx(usec);
2058 assert(idx < FIO_IO_U_PLAT_NR);
2059
2060 ts->io_u_plat[ddir][idx]++;
2061}
2062
1e97cce9 2063void add_clat_sample(struct thread_data *td, enum fio_ddir ddir,
ae588852 2064 unsigned long usec, unsigned int bs, uint64_t offset)
3c39a379 2065{
756867bd 2066 struct thread_stat *ts = &td->ts;
079ad09b 2067
75dc383e
JA
2068 td_io_u_lock(td);
2069
d85f5118 2070 add_stat_sample(&ts->clat_stat[ddir], usec);
3c39a379 2071
7b9f733a 2072 if (td->clat_log)
ae588852 2073 add_log_sample(td, td->clat_log, usec, ddir, bs, offset);
83349190
YH
2074
2075 if (ts->clat_percentiles)
2076 add_clat_percentile_sample(ts, usec, ddir);
75dc383e
JA
2077
2078 td_io_u_unlock(td);
3c39a379
JA
2079}
2080
1e97cce9 2081void add_slat_sample(struct thread_data *td, enum fio_ddir ddir,
ae588852 2082 unsigned long usec, unsigned int bs, uint64_t offset)
3c39a379 2083{
756867bd 2084 struct thread_stat *ts = &td->ts;
079ad09b 2085
ff58fced
JA
2086 if (!ddir_rw(ddir))
2087 return;
2088
75dc383e
JA
2089 td_io_u_lock(td);
2090
d85f5118 2091 add_stat_sample(&ts->slat_stat[ddir], usec);
3c39a379 2092
7b9f733a 2093 if (td->slat_log)
ae588852 2094 add_log_sample(td, td->slat_log, usec, ddir, bs, offset);
75dc383e
JA
2095
2096 td_io_u_unlock(td);
3c39a379
JA
2097}
2098
02af0988 2099void add_lat_sample(struct thread_data *td, enum fio_ddir ddir,
ae588852 2100 unsigned long usec, unsigned int bs, uint64_t offset)
02af0988
JA
2101{
2102 struct thread_stat *ts = &td->ts;
2103
ff58fced
JA
2104 if (!ddir_rw(ddir))
2105 return;
2106
75dc383e
JA
2107 td_io_u_lock(td);
2108
02af0988
JA
2109 add_stat_sample(&ts->lat_stat[ddir], usec);
2110
7b9f733a 2111 if (td->lat_log)
ae588852 2112 add_log_sample(td, td->lat_log, usec, ddir, bs, offset);
75dc383e
JA
2113
2114 td_io_u_unlock(td);
02af0988
JA
2115}
2116
a47591e4
JA
2117void add_bw_sample(struct thread_data *td, struct io_u *io_u,
2118 unsigned int bytes, unsigned long spent)
2119{
2120 struct thread_stat *ts = &td->ts;
2121 unsigned long rate;
2122
2123 if (spent)
2124 rate = bytes * 1000 / spent;
2125 else
2126 rate = 0;
2127
2128 td_io_u_lock(td);
2129
2130 add_stat_sample(&ts->bw_stat[io_u->ddir], rate);
2131
2132 if (td->bw_log)
2133 add_log_sample(td, td->bw_log, rate, io_u->ddir, bytes, io_u->offset);
2134
2135 td->stat_io_bytes[io_u->ddir] = td->this_io_bytes[io_u->ddir];
2136 td_io_u_unlock(td);
2137}
2138
2139static int add_bw_samples(struct thread_data *td, struct timeval *t)
3c39a379 2140{
756867bd 2141 struct thread_stat *ts = &td->ts;
ff58fced 2142 unsigned long spent, rate;
a47591e4 2143 enum fio_ddir ddir;
ff58fced 2144
a47591e4
JA
2145 if (per_unit_log(td->bw_log))
2146 return 0;
3c39a379 2147
c8eeb9df 2148 spent = mtime_since(&td->bw_sample_time, t);
a47591e4
JA
2149 if (spent < td->o.bw_avg_time &&
2150 td->o.bw_avg_time - spent >= 10)
2151 return td->o.bw_avg_time - spent;
9602d8df 2152
a9da8ab2
JA
2153 td_io_u_lock(td);
2154
9602d8df 2155 /*
5daa4ebe
JC
2156 * Compute both read and write rates for the interval.
2157 */
6eaf09d6 2158 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++) {
5daa4ebe
JC
2159 uint64_t delta;
2160
2161 delta = td->this_io_bytes[ddir] - td->stat_io_bytes[ddir];
2162 if (!delta)
2163 continue; /* No entries for interval */
3c39a379 2164
0956264f
JA
2165 if (spent)
2166 rate = delta * 1000 / spent / 1024;
2167 else
2168 rate = 0;
2169
5daa4ebe 2170 add_stat_sample(&ts->bw_stat[ddir], rate);
3c39a379 2171
66b98c9f
JA
2172 if (td->bw_log) {
2173 unsigned int bs = 0;
2174
2175 if (td->o.min_bs[ddir] == td->o.max_bs[ddir])
2176 bs = td->o.min_bs[ddir];
2177
2178 add_log_sample(td, td->bw_log, rate, ddir, bs, 0);
2179 }
5daa4ebe
JC
2180
2181 td->stat_io_bytes[ddir] = td->this_io_bytes[ddir];
2182 }
3c39a379 2183
a47591e4
JA
2184 timeval_add_msec(&td->bw_sample_time, td->o.bw_avg_time);
2185
2186 td_io_u_unlock(td);
2187
2188 if (spent <= td->o.bw_avg_time)
2189 return td->o.bw_avg_time;
2190
2191 return td->o.bw_avg_time - (1 + spent - td->o.bw_avg_time);
2192}
2193
2194void add_iops_sample(struct thread_data *td, struct io_u *io_u,
2195 unsigned int bytes)
2196{
2197 struct thread_stat *ts = &td->ts;
2198
2199 td_io_u_lock(td);
2200
2201 add_stat_sample(&ts->iops_stat[io_u->ddir], 1);
2202
2203 if (td->iops_log)
2204 add_log_sample(td, td->iops_log, 1, io_u->ddir, bytes, io_u->offset);
2205
2206 td->stat_io_blocks[io_u->ddir] = td->this_io_blocks[io_u->ddir];
a9da8ab2 2207 td_io_u_unlock(td);
3c39a379 2208}
c8eeb9df 2209
a47591e4 2210static int add_iops_samples(struct thread_data *td, struct timeval *t)
c8eeb9df
JA
2211{
2212 struct thread_stat *ts = &td->ts;
2213 unsigned long spent, iops;
a47591e4 2214 enum fio_ddir ddir;
c8eeb9df 2215
a47591e4
JA
2216 if (per_unit_log(td->iops_log))
2217 return 0;
c8eeb9df
JA
2218
2219 spent = mtime_since(&td->iops_sample_time, t);
a47591e4
JA
2220 if (spent < td->o.iops_avg_time &&
2221 td->o.iops_avg_time - spent >= 10)
2222 return td->o.iops_avg_time - spent;
c8eeb9df 2223
a9da8ab2
JA
2224 td_io_u_lock(td);
2225
9602d8df
JA
2226 /*
2227 * Compute both read and write rates for the interval.
2228 */
6eaf09d6 2229 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++) {
9602d8df 2230 uint64_t delta;
c8eeb9df 2231
9602d8df
JA
2232 delta = td->this_io_blocks[ddir] - td->stat_io_blocks[ddir];
2233 if (!delta)
2234 continue; /* No entries for interval */
2235
0956264f
JA
2236 if (spent)
2237 iops = (delta * 1000) / spent;
2238 else
2239 iops = 0;
2240
9602d8df 2241 add_stat_sample(&ts->iops_stat[ddir], iops);
c8eeb9df 2242
66b98c9f
JA
2243 if (td->iops_log) {
2244 unsigned int bs = 0;
2245
2246 if (td->o.min_bs[ddir] == td->o.max_bs[ddir])
2247 bs = td->o.min_bs[ddir];
2248
2249 add_log_sample(td, td->iops_log, iops, ddir, bs, 0);
2250 }
9602d8df 2251
421f4a82 2252 td->stat_io_blocks[ddir] = td->this_io_blocks[ddir];
9602d8df 2253 }
c8eeb9df 2254
a47591e4
JA
2255 timeval_add_msec(&td->iops_sample_time, td->o.iops_avg_time);
2256
a9da8ab2 2257 td_io_u_unlock(td);
a47591e4
JA
2258
2259 if (spent <= td->o.iops_avg_time)
2260 return td->o.iops_avg_time;
2261
a47591e4
JA
2262 return td->o.iops_avg_time - (1 + spent - td->o.iops_avg_time);
2263}
2264
2265/*
2266 * Returns msecs to next event
2267 */
2268int calc_log_samples(void)
2269{
2270 struct thread_data *td;
2271 unsigned int next = ~0U, tmp;
2272 struct timeval now;
2273 int i;
2274
2275 fio_gettime(&now, NULL);
2276
2277 for_each_td(td, i) {
2278 if (!ramp_time_over(td) ||
2279 !(td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING)) {
2280 next = min(td->o.iops_avg_time, td->o.bw_avg_time);
2281 continue;
2282 }
2283 if (!per_unit_log(td->bw_log)) {
2284 tmp = add_bw_samples(td, &now);
2285 if (tmp < next)
2286 next = tmp;
2287 }
2288 if (!per_unit_log(td->iops_log)) {
2289 tmp = add_iops_samples(td, &now);
2290 if (tmp < next)
2291 next = tmp;
2292 }
2293 }
2294
2295 return next == ~0U ? 0 : next;
c8eeb9df 2296}
cef9175e
JA
2297
2298void stat_init(void)
2299{
2300 stat_mutex = fio_mutex_init(FIO_MUTEX_UNLOCKED);
2301}
2302
2303void stat_exit(void)
2304{
2305 /*
2306 * When we have the mutex, we know out-of-band access to it
2307 * have ended.
2308 */
2309 fio_mutex_down(stat_mutex);
2310 fio_mutex_remove(stat_mutex);
2311}
b2ee7647 2312
b2ee7647
JA
2313/*
2314 * Called from signal handler. Wake up status thread.
2315 */
2316void show_running_run_stats(void)
2317{
a47591e4 2318 helper_do_stat();
b2ee7647 2319}
66347cfa
DE
2320
2321uint32_t *io_u_block_info(struct thread_data *td, struct io_u *io_u)
2322{
2323 /* Ignore io_u's which span multiple blocks--they will just get
2324 * inaccurate counts. */
2325 int idx = (io_u->offset - io_u->file->file_offset)
2326 / td->o.bs[DDIR_TRIM];
2327 uint32_t *info = &td->ts.block_infos[idx];
2328 assert(idx < td->ts.nr_block_infos);
2329 return info;
2330}