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