Add unified_rw_reporting option
[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"
3c39a379 15
3c39a379
JA
16void update_rusage_stat(struct thread_data *td)
17{
756867bd 18 struct thread_stat *ts = &td->ts;
3c39a379 19
44404c5a 20 fio_getrusage(&td->ru_end);
c8aaba19
JA
21 ts->usr_time += mtime_since(&td->ru_start.ru_utime,
22 &td->ru_end.ru_utime);
23 ts->sys_time += mtime_since(&td->ru_start.ru_stime,
24 &td->ru_end.ru_stime);
25 ts->ctx += td->ru_end.ru_nvcsw + td->ru_end.ru_nivcsw
26 - (td->ru_start.ru_nvcsw + td->ru_start.ru_nivcsw);
27 ts->minf += td->ru_end.ru_minflt - td->ru_start.ru_minflt;
28 ts->majf += td->ru_end.ru_majflt - td->ru_start.ru_majflt;
5ec10eaa 29
c8aaba19 30 memcpy(&td->ru_start, &td->ru_end, sizeof(td->ru_end));
3c39a379
JA
31}
32
83349190
YH
33/*
34 * Given a latency, return the index of the corresponding bucket in
35 * the structure tracking percentiles.
36 *
37 * (1) find the group (and error bits) that the value (latency)
38 * belongs to by looking at its MSB. (2) find the bucket number in the
39 * group by looking at the index bits.
40 *
41 */
42static unsigned int plat_val_to_idx(unsigned int val)
43{
44 unsigned int msb, error_bits, base, offset, idx;
45
46 /* Find MSB starting from bit 0 */
47 if (val == 0)
48 msb = 0;
49 else
50 msb = (sizeof(val)*8) - __builtin_clz(val) - 1;
51
716050f2
JA
52 /*
53 * MSB <= (FIO_IO_U_PLAT_BITS-1), cannot be rounded off. Use
54 * all bits of the sample as index
55 */
83349190
YH
56 if (msb <= FIO_IO_U_PLAT_BITS)
57 return val;
58
59 /* Compute the number of error bits to discard*/
60 error_bits = msb - FIO_IO_U_PLAT_BITS;
61
62 /* Compute the number of buckets before the group */
63 base = (error_bits + 1) << FIO_IO_U_PLAT_BITS;
64
716050f2
JA
65 /*
66 * Discard the error bits and apply the mask to find the
67 * index for the buckets in the group
68 */
83349190
YH
69 offset = (FIO_IO_U_PLAT_VAL - 1) & (val >> error_bits);
70
71 /* Make sure the index does not exceed (array size - 1) */
72 idx = (base + offset) < (FIO_IO_U_PLAT_NR - 1)?
73 (base + offset) : (FIO_IO_U_PLAT_NR - 1);
74
75 return idx;
76}
77
78/*
79 * Convert the given index of the bucket array to the value
80 * represented by the bucket
81 */
82static unsigned int plat_idx_to_val(unsigned int idx)
83{
84 unsigned int error_bits, k, base;
85
86 assert(idx < FIO_IO_U_PLAT_NR);
87
88 /* MSB <= (FIO_IO_U_PLAT_BITS-1), cannot be rounded off. Use
89 * all bits of the sample as index */
90 if (idx < (FIO_IO_U_PLAT_VAL << 1) )
91 return idx;
92
93 /* Find the group and compute the minimum value of that group */
94 error_bits = (idx >> FIO_IO_U_PLAT_BITS) -1;
95 base = 1 << (error_bits + FIO_IO_U_PLAT_BITS);
96
97 /* Find its bucket number of the group */
98 k = idx % FIO_IO_U_PLAT_VAL;
99
100 /* Return the mean of the range of the bucket */
101 return base + ((k + 0.5) * (1 << error_bits));
102}
103
104static int double_cmp(const void *a, const void *b)
105{
802ad4a8
JA
106 const fio_fp64_t fa = *(const fio_fp64_t *) a;
107 const fio_fp64_t fb = *(const fio_fp64_t *) b;
83349190
YH
108 int cmp = 0;
109
802ad4a8 110 if (fa.u.f > fb.u.f)
83349190 111 cmp = 1;
802ad4a8 112 else if (fa.u.f < fb.u.f)
83349190
YH
113 cmp = -1;
114
115 return cmp;
116}
117
1db92cb6
JA
118static unsigned int calc_clat_percentiles(unsigned int *io_u_plat,
119 unsigned long nr, fio_fp64_t *plist,
120 unsigned int **output,
121 unsigned int *maxv,
122 unsigned int *minv)
83349190
YH
123{
124 unsigned long sum = 0;
1db92cb6
JA
125 unsigned int len, i, j = 0;
126 unsigned int oval_len = 0;
127 unsigned int *ovals = NULL;
128 int is_last;
129
130 *minv = -1U;
131 *maxv = 0;
83349190 132
802ad4a8
JA
133 len = 0;
134 while (len < FIO_IO_U_LIST_MAX_LEN && plist[len].u.f != 0.0)
135 len++;
716050f2 136
351de8de 137 if (!len)
1db92cb6 138 return 0;
351de8de 139
716050f2 140 /*
802ad4a8
JA
141 * Sort the percentile list. Note that it may already be sorted if
142 * we are using the default values, but since it's a short list this
143 * isn't a worry. Also note that this does not work for NaN values.
716050f2 144 */
802ad4a8
JA
145 if (len > 1)
146 qsort((void*)plist, len, sizeof(plist[0]), double_cmp);
83349190 147
4f6f8298
JA
148 /*
149 * Calculate bucket values, note down max and min values
150 */
07511a63
JA
151 is_last = 0;
152 for (i = 0; i < FIO_IO_U_PLAT_NR && !is_last; i++) {
83349190 153 sum += io_u_plat[i];
802ad4a8
JA
154 while (sum >= (plist[j].u.f / 100.0 * nr)) {
155 assert(plist[j].u.f <= 100.0);
83349190 156
4f6f8298
JA
157 if (j == oval_len) {
158 oval_len += 100;
159 ovals = realloc(ovals, oval_len * sizeof(unsigned int));
160 }
161
162 ovals[j] = plat_idx_to_val(i);
1db92cb6
JA
163 if (ovals[j] < *minv)
164 *minv = ovals[j];
165 if (ovals[j] > *maxv)
166 *maxv = ovals[j];
07511a63
JA
167
168 is_last = (j == len - 1);
169 if (is_last)
170 break;
171
4f6f8298
JA
172 j++;
173 }
174 }
83349190 175
1db92cb6
JA
176 *output = ovals;
177 return len;
178}
179
180/*
181 * Find and display the p-th percentile of clat
182 */
183static void show_clat_percentiles(unsigned int *io_u_plat, unsigned long nr,
184 fio_fp64_t *plist)
185{
186 unsigned int len, j = 0, minv, maxv;
187 unsigned int *ovals;
188 int is_last, scale_down;
189
190 len = calc_clat_percentiles(io_u_plat, nr, plist, &ovals, &maxv, &minv);
191 if (!len)
192 goto out;
193
4f6f8298
JA
194 /*
195 * We default to usecs, but if the value range is such that we
196 * should scale down to msecs, do that.
197 */
198 if (minv > 2000 && maxv > 99999) {
199 scale_down = 1;
200 log_info(" clat percentiles (msec):\n |");
201 } else {
202 scale_down = 0;
203 log_info(" clat percentiles (usec):\n |");
204 }
83349190 205
4f6f8298
JA
206 for (j = 0; j < len; j++) {
207 char fbuf[8];
81ab0b3a 208
4f6f8298
JA
209 /* for formatting */
210 if (j != 0 && (j % 4) == 0)
211 log_info(" |");
83349190 212
4f6f8298
JA
213 /* end of the list */
214 is_last = (j == len - 1);
83349190 215
4f6f8298
JA
216 if (plist[j].u.f < 10.0)
217 sprintf(fbuf, " %2.2f", plist[j].u.f);
218 else
219 sprintf(fbuf, "%2.2f", plist[j].u.f);
220
221 if (scale_down)
222 ovals[j] = (ovals[j] + 999) / 1000;
223
224 log_info(" %sth=[%5u]%c", fbuf, ovals[j], is_last ? '\n' : ',');
225
226 if (is_last)
227 break;
228
229 if (j % 4 == 3) /* for formatting */
230 log_info("\n");
83349190 231 }
4f6f8298 232
1db92cb6 233out:
4f6f8298
JA
234 if (ovals)
235 free(ovals);
83349190
YH
236}
237
3c39a379
JA
238static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max,
239 double *mean, double *dev)
240{
68704084 241 double n = is->samples;
3c39a379
JA
242
243 if (is->samples == 0)
244 return 0;
245
246 *min = is->min_val;
247 *max = is->max_val;
248
249 n = (double) is->samples;
802ad4a8 250 *mean = is->mean.u.f;
e6d276f2 251
68704084 252 if (n > 1.0)
802ad4a8 253 *dev = sqrt(is->S.u.f / (n - 1.0));
ef9c5c40 254 else
4b43f54e 255 *dev = 0;
ef9c5c40 256
3c39a379
JA
257 return 1;
258}
259
a64e88da 260void show_group_stats(struct group_run_stats *rs)
3c39a379 261{
dbe1125e 262 char *p1, *p2, *p3, *p4;
6eaf09d6 263 const char *ddir_str[] = { " READ", " WRITE" , " TRIM"};
dbe1125e
JA
264 int i;
265
7e1773ba 266 log_info("\nRun status group %d (all jobs):\n", rs->groupid);
3c39a379 267
6eaf09d6 268 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
90fef2d1
JA
269 const int i2p = is_power_of_2(rs->kb_base);
270
dbe1125e
JA
271 if (!rs->max_run[i])
272 continue;
273
90fef2d1
JA
274 p1 = num2str(rs->io_kb[i], 6, rs->kb_base, i2p);
275 p2 = num2str(rs->agg[i], 6, rs->kb_base, i2p);
276 p3 = num2str(rs->min_bw[i], 6, rs->kb_base, i2p);
277 p4 = num2str(rs->max_bw[i], 6, rs->kb_base, i2p);
dbe1125e 278
b22989b9 279 log_info("%s: io=%sB, aggrb=%sB/s, minb=%sB/s, maxb=%sB/s,"
771e58be
JA
280 " mint=%llumsec, maxt=%llumsec\n",
281 rs->unified_rw_rep ? " MIXED" : ddir_str[i],
282 p1, p2, p3, p4, rs->min_run[i], rs->max_run[i]);
dbe1125e
JA
283
284 free(p1);
285 free(p2);
286 free(p3);
287 free(p4);
288 }
3c39a379
JA
289}
290
b3605062 291#define ts_total_io_u(ts) \
6eaf09d6
SL
292 ((ts)->total_io_u[DDIR_READ] + (ts)->total_io_u[DDIR_WRITE] +\
293 (ts)->total_io_u[DDIR_TRIM])
b3605062 294
838bc709
JA
295static void stat_calc_dist(unsigned int *map, unsigned long total,
296 double *io_u_dist)
2270890c
JA
297{
298 int i;
299
300 /*
301 * Do depth distribution calculations
302 */
303 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
838bc709
JA
304 if (total) {
305 io_u_dist[i] = (double) map[i] / (double) total;
306 io_u_dist[i] *= 100.0;
307 if (io_u_dist[i] < 0.1 && map[i])
308 io_u_dist[i] = 0.1;
309 } else
310 io_u_dist[i] = 0.0;
2270890c
JA
311 }
312}
313
04a0feae
JA
314static void stat_calc_lat(struct thread_stat *ts, double *dst,
315 unsigned int *src, int nr)
2270890c 316{
838bc709 317 unsigned long total = ts_total_io_u(ts);
2270890c
JA
318 int i;
319
320 /*
321 * Do latency distribution calculations
322 */
04a0feae 323 for (i = 0; i < nr; i++) {
838bc709
JA
324 if (total) {
325 dst[i] = (double) src[i] / (double) total;
326 dst[i] *= 100.0;
327 if (dst[i] < 0.01 && src[i])
328 dst[i] = 0.01;
329 } else
330 dst[i] = 0.0;
2270890c
JA
331 }
332}
333
04a0feae
JA
334static void stat_calc_lat_u(struct thread_stat *ts, double *io_u_lat)
335{
336 stat_calc_lat(ts, io_u_lat, ts->io_u_lat_u, FIO_IO_U_LAT_U_NR);
337}
338
339static void stat_calc_lat_m(struct thread_stat *ts, double *io_u_lat)
340{
341 stat_calc_lat(ts, io_u_lat, ts->io_u_lat_m, FIO_IO_U_LAT_M_NR);
342}
343
ea2accc5
JA
344static int usec_to_msec(unsigned long *min, unsigned long *max, double *mean,
345 double *dev)
346{
347 if (*min > 1000 && *max > 1000 && *mean > 1000.0 && *dev > 1000.0) {
348 *min /= 1000;
349 *max /= 1000;
350 *mean /= 1000.0;
351 *dev /= 1000.0;
352 return 0;
353 }
354
355 return 1;
356}
357
756867bd 358static void show_ddir_status(struct group_run_stats *rs, struct thread_stat *ts,
3c39a379
JA
359 int ddir)
360{
6eaf09d6 361 const char *ddir_str[] = { "read ", "write", "trim" };
8879fd15 362 unsigned long min, max, runt;
b3605062 363 unsigned long long bw, iops;
3c39a379 364 double mean, dev;
b3605062 365 char *io_p, *bw_p, *iops_p;
90fef2d1 366 int i2p;
3c39a379 367
ff58fced
JA
368 assert(ddir_rw(ddir));
369
756867bd 370 if (!ts->runtime[ddir])
3c39a379
JA
371 return;
372
90fef2d1 373 i2p = is_power_of_2(rs->kb_base);
8879fd15
JA
374 runt = ts->runtime[ddir];
375
376 bw = (1000 * ts->io_bytes[ddir]) / runt;
90fef2d1
JA
377 io_p = num2str(ts->io_bytes[ddir], 6, 1, i2p);
378 bw_p = num2str(bw, 6, 1, i2p);
8879fd15 379
0aacc50c 380 iops = (1000 * (uint64_t)ts->total_io_u[ddir]) / runt;
b3605062 381 iops_p = num2str(iops, 6, 1, 0);
dbe1125e 382
cda99fa0 383 log_info(" %s: io=%sB, bw=%sB/s, iops=%s, runt=%6llumsec\n",
771e58be
JA
384 rs->unified_rw_rep ? "mixed" : ddir_str[ddir],
385 io_p, bw_p, iops_p, ts->runtime[ddir]);
dbe1125e
JA
386
387 free(io_p);
388 free(bw_p);
b3605062 389 free(iops_p);
3c39a379 390
d85f5118
JA
391 if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev)) {
392 const char *base = "(usec)";
d9309cb1 393 char *minp, *maxp;
d85f5118 394
ea2accc5 395 if (!usec_to_msec(&min, &max, &mean, &dev))
d85f5118 396 base = "(msec)";
ea2accc5 397
d9309cb1
JA
398 minp = num2str(min, 6, 1, 0);
399 maxp = num2str(max, 6, 1, 0);
400
5ec10eaa
JA
401 log_info(" slat %s: min=%s, max=%s, avg=%5.02f,"
402 " stdev=%5.02f\n", base, minp, maxp, mean, dev);
d9309cb1
JA
403
404 free(minp);
405 free(maxp);
d85f5118
JA
406 }
407 if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev)) {
408 const char *base = "(usec)";
d9309cb1 409 char *minp, *maxp;
d85f5118 410
ea2accc5
JA
411 if (!usec_to_msec(&min, &max, &mean, &dev))
412 base = "(msec)";
413
d9309cb1
JA
414 minp = num2str(min, 6, 1, 0);
415 maxp = num2str(max, 6, 1, 0);
5ec10eaa
JA
416
417 log_info(" clat %s: min=%s, max=%s, avg=%5.02f,"
418 " stdev=%5.02f\n", base, minp, maxp, mean, dev);
d9309cb1
JA
419
420 free(minp);
421 free(maxp);
d85f5118 422 }
02af0988
JA
423 if (calc_lat(&ts->lat_stat[ddir], &min, &max, &mean, &dev)) {
424 const char *base = "(usec)";
425 char *minp, *maxp;
426
427 if (!usec_to_msec(&min, &max, &mean, &dev))
428 base = "(msec)";
429
430 minp = num2str(min, 6, 1, 0);
431 maxp = num2str(max, 6, 1, 0);
432
433 log_info(" lat %s: min=%s, max=%s, avg=%5.02f,"
434 " stdev=%5.02f\n", base, minp, maxp, mean, dev);
435
436 free(minp);
437 free(maxp);
438 }
83349190
YH
439 if (ts->clat_percentiles) {
440 show_clat_percentiles(ts->io_u_plat[ddir],
441 ts->clat_stat[ddir].samples,
442 ts->percentile_list);
443 }
079ad09b 444 if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
19d3e967 445 double p_of_agg = 100.0;
b7017e32 446 const char *bw_str = "KB";
3c39a379 447
2f2c6924 448 if (rs->agg[ddir]) {
19d3e967
JA
449 p_of_agg = mean * 100 / (double) rs->agg[ddir];
450 if (p_of_agg > 100.0)
451 p_of_agg = 100.0;
452 }
b7017e32
JA
453
454 if (mean > 999999.9) {
455 min /= 1000.0;
456 max /= 1000.0;
457 mean /= 1000.0;
458 dev /= 1000.0;
459 bw_str = "MB";
460 }
461
7e1773ba 462 log_info(" bw (%s/s) : min=%5lu, max=%5lu, per=%3.2f%%,"
b7017e32
JA
463 " avg=%5.02f, stdev=%5.02f\n", bw_str, min, max,
464 p_of_agg, mean, dev);
3c39a379
JA
465 }
466}
467
7e1773ba
JA
468static int show_lat(double *io_u_lat, int nr, const char **ranges,
469 const char *msg)
04a0feae 470{
7e1773ba 471 int new_line = 1, i, line = 0, shown = 0;
04a0feae
JA
472
473 for (i = 0; i < nr; i++) {
474 if (io_u_lat[i] <= 0.0)
475 continue;
7e1773ba 476 shown = 1;
04a0feae 477 if (new_line) {
4539ed73
JA
478 if (line)
479 log_info("\n");
7e1773ba 480 log_info(" lat (%s) : ", msg);
04a0feae
JA
481 new_line = 0;
482 line = 0;
483 }
484 if (line)
485 log_info(", ");
486 log_info("%s%3.2f%%", ranges[i], io_u_lat[i]);
487 line++;
488 if (line == 5)
489 new_line = 1;
490 }
7e1773ba
JA
491
492 if (shown)
493 log_info("\n");
494
495 return shown;
04a0feae
JA
496}
497
498static void show_lat_u(double *io_u_lat_u)
499{
500 const char *ranges[] = { "2=", "4=", "10=", "20=", "50=", "100=",
501 "250=", "500=", "750=", "1000=", };
502
503 show_lat(io_u_lat_u, FIO_IO_U_LAT_U_NR, ranges, "usec");
504}
505
506static void show_lat_m(double *io_u_lat_m)
507{
508 const char *ranges[] = { "2=", "4=", "10=", "20=", "50=", "100=",
509 "250=", "500=", "750=", "1000=", "2000=",
510 ">=2000=", };
511
512 show_lat(io_u_lat_m, FIO_IO_U_LAT_M_NR, ranges, "msec");
513}
514
515static void show_latencies(double *io_u_lat_u, double *io_u_lat_m)
516{
517 show_lat_u(io_u_lat_u);
518 show_lat_m(io_u_lat_m);
04a0feae
JA
519}
520
a64e88da 521void show_thread_status(struct thread_stat *ts, struct group_run_stats *rs)
3c39a379
JA
522{
523 double usr_cpu, sys_cpu;
69008999 524 unsigned long runtime;
71619dc2 525 double io_u_dist[FIO_IO_U_MAP_NR];
04a0feae
JA
526 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
527 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
57a64324
JA
528 time_t time_p;
529 char time_buf[64];
3c39a379 530
6eaf09d6
SL
531 if (!(ts->io_bytes[DDIR_READ] + ts->io_bytes[DDIR_WRITE] +
532 ts->io_bytes[DDIR_TRIM]) && !(ts->total_io_u[DDIR_READ] +
533 ts->total_io_u[DDIR_WRITE] + ts->total_io_u[DDIR_TRIM]))
3c39a379
JA
534 return;
535
57a64324 536 time(&time_p);
45054cbe 537 os_ctime_r((const time_t *) &time_p, time_buf, sizeof(time_buf));
57a64324 538
5ec10eaa 539 if (!ts->error) {
57a64324 540 log_info("%s: (groupid=%d, jobs=%d): err=%2d: pid=%d: %s",
5ec10eaa 541 ts->name, ts->groupid, ts->members,
57a64324 542 ts->error, (int) ts->pid, time_buf);
5ec10eaa 543 } else {
57a64324 544 log_info("%s: (groupid=%d, jobs=%d): err=%2d (%s): pid=%d: %s",
5ec10eaa 545 ts->name, ts->groupid, ts->members,
57a64324
JA
546 ts->error, ts->verror, (int) ts->pid,
547 time_buf);
5ec10eaa 548 }
3c39a379 549
259e47de 550 if (strlen(ts->description))
6d86144d 551 log_info(" Description : [%s]\n", ts->description);
7bdce1bd 552
756867bd
JA
553 if (ts->io_bytes[DDIR_READ])
554 show_ddir_status(rs, ts, DDIR_READ);
555 if (ts->io_bytes[DDIR_WRITE])
556 show_ddir_status(rs, ts, DDIR_WRITE);
6eaf09d6
SL
557 if (ts->io_bytes[DDIR_TRIM])
558 show_ddir_status(rs, ts, DDIR_TRIM);
3c39a379 559
7e1773ba
JA
560 stat_calc_lat_u(ts, io_u_lat_u);
561 stat_calc_lat_m(ts, io_u_lat_m);
562 show_latencies(io_u_lat_u, io_u_lat_m);
563
756867bd 564 runtime = ts->total_run_time;
69008999 565 if (runtime) {
1e97cce9 566 double runt = (double) runtime;
3c39a379 567
756867bd
JA
568 usr_cpu = (double) ts->usr_time * 100 / runt;
569 sys_cpu = (double) ts->sys_time * 100 / runt;
3c39a379
JA
570 } else {
571 usr_cpu = 0;
572 sys_cpu = 0;
573 }
574
5ec10eaa
JA
575 log_info(" cpu : usr=%3.2f%%, sys=%3.2f%%, ctx=%lu, majf=%lu,"
576 " minf=%lu\n", usr_cpu, sys_cpu, ts->ctx, ts->majf, ts->minf);
71619dc2 577
838bc709 578 stat_calc_dist(ts->io_u_map, ts_total_io_u(ts), io_u_dist);
5ec10eaa
JA
579 log_info(" IO depths : 1=%3.1f%%, 2=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%,"
580 " 16=%3.1f%%, 32=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
581 io_u_dist[1], io_u_dist[2],
582 io_u_dist[3], io_u_dist[4],
583 io_u_dist[5], io_u_dist[6]);
838bc709
JA
584
585 stat_calc_dist(ts->io_u_submit, ts->total_submit, io_u_dist);
586 log_info(" submit : 0=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%, 16=%3.1f%%,"
587 " 32=%3.1f%%, 64=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
588 io_u_dist[1], io_u_dist[2],
589 io_u_dist[3], io_u_dist[4],
590 io_u_dist[5], io_u_dist[6]);
591 stat_calc_dist(ts->io_u_complete, ts->total_complete, io_u_dist);
592 log_info(" complete : 0=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%, 16=%3.1f%%,"
593 " 32=%3.1f%%, 64=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
594 io_u_dist[1], io_u_dist[2],
595 io_u_dist[3], io_u_dist[4],
596 io_u_dist[5], io_u_dist[6]);
7e1773ba
JA
597 log_info(" issued : total=r=%lu/w=%lu/d=%lu,"
598 " short=r=%lu/w=%lu/d=%lu\n",
5ec10eaa 599 ts->total_io_u[0], ts->total_io_u[1],
0d29de83
JA
600 ts->total_io_u[2],
601 ts->short_io_u[0], ts->short_io_u[1],
602 ts->short_io_u[2]);
f2bba182 603 if (ts->continue_on_error) {
1ec99eea
JA
604 log_info(" errors : total=%lu, first_error=%d/<%s>\n",
605 ts->total_err_count,
606 ts->first_error,
607 strerror(ts->first_error));
f2bba182 608 }
3c39a379
JA
609}
610
756867bd 611static void show_ddir_status_terse(struct thread_stat *ts,
c6ae0a5b
JA
612 struct group_run_stats *rs, int ddir)
613{
614 unsigned long min, max;
312b4af2 615 unsigned long long bw, iops;
1db92cb6 616 unsigned int *ovals = NULL;
c6ae0a5b 617 double mean, dev;
1db92cb6
JA
618 unsigned int len, minv, maxv;
619 int i;
c6ae0a5b 620
ff58fced
JA
621 assert(ddir_rw(ddir));
622
312b4af2
JA
623 iops = bw = 0;
624 if (ts->runtime[ddir]) {
625 uint64_t runt = ts->runtime[ddir];
626
033bbb51 627 bw = ((1000 * ts->io_bytes[ddir]) / runt) / 1024;
312b4af2
JA
628 iops = (1000 * (uint64_t) ts->total_io_u[ddir]) / runt;
629 }
c6ae0a5b 630
312b4af2 631 log_info(";%llu;%llu;%llu;%llu", ts->io_bytes[ddir] >> 10, bw, iops,
5ec10eaa 632 ts->runtime[ddir]);
c6ae0a5b 633
079ad09b 634 if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev))
6d86144d 635 log_info(";%lu;%lu;%f;%f", min, max, mean, dev);
c6ae0a5b 636 else
6d86144d 637 log_info(";%lu;%lu;%f;%f", 0UL, 0UL, 0.0, 0.0);
c6ae0a5b 638
079ad09b 639 if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev))
6d86144d 640 log_info(";%lu;%lu;%f;%f", min, max, mean, dev);
c6ae0a5b 641 else
6d86144d 642 log_info(";%lu;%lu;%f;%f", 0UL, 0UL, 0.0, 0.0);
c6ae0a5b 643
1db92cb6
JA
644 if (ts->clat_percentiles) {
645 len = calc_clat_percentiles(ts->io_u_plat[ddir],
646 ts->clat_stat[ddir].samples,
647 ts->percentile_list, &ovals, &maxv,
648 &minv);
649 } else
650 len = 0;
651
652 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
653 if (i >= len) {
654 log_info(";0%%=0");
655 continue;
656 }
657 log_info(";%2.2f%%=%u", ts->percentile_list[i].u.f, ovals[i]);
658 }
2341a37a
K
659
660 if (calc_lat(&ts->lat_stat[ddir], &min, &max, &mean, &dev))
661 log_info(";%lu;%lu;%f;%f", min, max, mean, dev);
662 else
663 log_info(";%lu;%lu;%f;%f", 0UL, 0UL, 0.0, 0.0);
664
1db92cb6
JA
665 if (ovals)
666 free(ovals);
667
079ad09b 668 if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
19d3e967
JA
669 double p_of_agg = 100.0;
670
671 if (rs->agg[ddir]) {
672 p_of_agg = mean * 100 / (double) rs->agg[ddir];
673 if (p_of_agg > 100.0)
674 p_of_agg = 100.0;
675 }
c6ae0a5b 676
6d86144d 677 log_info(";%lu;%lu;%f%%;%f;%f", min, max, p_of_agg, mean, dev);
c6ae0a5b 678 } else
6d86144d 679 log_info(";%lu;%lu;%f%%;%f;%f", 0UL, 0UL, 0.0, 0.0, 0.0);
c6ae0a5b
JA
680}
681
cc372b17
SL
682static void add_ddir_status_json(struct thread_stat *ts,
683 struct group_run_stats *rs, int ddir, struct json_object *parent)
684{
685 unsigned long min, max;
686 unsigned long long bw, iops;
687 unsigned int *ovals = NULL;
688 double mean, dev;
689 unsigned int len, minv, maxv;
690 int i;
691 const char *ddirname[] = {"read", "write", "trim"};
692 struct json_object *dir_object, *tmp_object, *percentile_object;
693 char buf[120];
694 double p_of_agg = 100.0;
695
696 assert(ddir_rw(ddir));
697
771e58be
JA
698 if (ts->unified_rw_rep && ddir != DDIR_READ)
699 return;
700
cc372b17 701 dir_object = json_create_object();
771e58be
JA
702 json_object_add_value_object(parent,
703 ts->unified_rw_rep ? "mixed" : ddirname[ddir], dir_object);
cc372b17
SL
704
705 iops = bw = 0;
706 if (ts->runtime[ddir]) {
707 uint64_t runt = ts->runtime[ddir];
708
709 bw = ((1000 * ts->io_bytes[ddir]) / runt) / 1024;
710 iops = (1000 * (uint64_t) ts->total_io_u[ddir]) / runt;
711 }
712
713 json_object_add_value_int(dir_object, "io_bytes", ts->io_bytes[ddir] >> 10);
714 json_object_add_value_int(dir_object, "bw", bw);
715 json_object_add_value_int(dir_object, "iops", iops);
716 json_object_add_value_int(dir_object, "runtime", ts->runtime[ddir]);
717
718 if (!calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev)) {
719 min = max = 0;
720 mean = dev = 0.0;
721 }
722 tmp_object = json_create_object();
723 json_object_add_value_object(dir_object, "slat", tmp_object);
724 json_object_add_value_int(tmp_object, "min", min);
725 json_object_add_value_int(tmp_object, "max", max);
726 json_object_add_value_float(tmp_object, "mean", mean);
727 json_object_add_value_float(tmp_object, "stddev", dev);
728
729 if (!calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev)) {
730 min = max = 0;
731 mean = dev = 0.0;
732 }
733 tmp_object = json_create_object();
734 json_object_add_value_object(dir_object, "clat", tmp_object);
735 json_object_add_value_int(tmp_object, "min", min);
736 json_object_add_value_int(tmp_object, "max", max);
737 json_object_add_value_float(tmp_object, "mean", mean);
738 json_object_add_value_float(tmp_object, "stddev", dev);
739
740 if (ts->clat_percentiles) {
741 len = calc_clat_percentiles(ts->io_u_plat[ddir],
742 ts->clat_stat[ddir].samples,
743 ts->percentile_list, &ovals, &maxv,
744 &minv);
745 } else
746 len = 0;
747
748 percentile_object = json_create_object();
749 json_object_add_value_object(tmp_object, "percentile", percentile_object);
750 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
751 if (i >= len) {
752 json_object_add_value_int(percentile_object, "0.00", 0);
753 continue;
754 }
755 snprintf(buf, sizeof(buf) - 1, "%2.2f", ts->percentile_list[i].u.f);
756 json_object_add_value_int(percentile_object, (const char *)buf, ovals[i]);
757 }
758
759 if (!calc_lat(&ts->lat_stat[ddir], &min, &max, &mean, &dev)) {
760 min = max = 0;
761 mean = dev = 0.0;
762 }
763 tmp_object = json_create_object();
764 json_object_add_value_object(dir_object, "lat", tmp_object);
765 json_object_add_value_int(tmp_object, "min", min);
766 json_object_add_value_int(tmp_object, "max", max);
767 json_object_add_value_float(tmp_object, "mean", mean);
768 json_object_add_value_float(tmp_object, "stddev", dev);
769 if (ovals)
770 free(ovals);
771
772 if (!calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
773 if (rs->agg[ddir]) {
774 p_of_agg = mean * 100 / (double) rs->agg[ddir];
775 if (p_of_agg > 100.0)
776 p_of_agg = 100.0;
777 }
778 } else {
779 min = max = 0;
780 p_of_agg = mean = dev = 0.0;
781 }
782 json_object_add_value_int(dir_object, "bw_min", min);
783 json_object_add_value_int(dir_object, "bw_max", max);
784 json_object_add_value_float(dir_object, "bw_agg", mean);
785 json_object_add_value_float(dir_object, "bw_mean", mean);
786 json_object_add_value_float(dir_object, "bw_dev", dev);
787}
788
4d658652
JA
789static void show_thread_status_terse_v2(struct thread_stat *ts,
790 struct group_run_stats *rs)
791{
792 double io_u_dist[FIO_IO_U_MAP_NR];
793 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
794 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
795 double usr_cpu, sys_cpu;
796 int i;
797
798 /* General Info */
799 log_info("2;%s;%d;%d", ts->name, ts->groupid, ts->error);
800 /* Log Read Status */
6eaf09d6 801 show_ddir_status_terse(ts, rs, DDIR_READ);
4d658652 802 /* Log Write Status */
6eaf09d6
SL
803 show_ddir_status_terse(ts, rs, DDIR_WRITE);
804 /* Log Trim Status */
805 show_ddir_status_terse(ts, rs, DDIR_TRIM);
4d658652
JA
806
807 /* CPU Usage */
808 if (ts->total_run_time) {
809 double runt = (double) ts->total_run_time;
810
811 usr_cpu = (double) ts->usr_time * 100 / runt;
812 sys_cpu = (double) ts->sys_time * 100 / runt;
813 } else {
814 usr_cpu = 0;
815 sys_cpu = 0;
816 }
817
818 log_info(";%f%%;%f%%;%lu;%lu;%lu", usr_cpu, sys_cpu, ts->ctx, ts->majf,
819 ts->minf);
820
821 /* Calc % distribution of IO depths, usecond, msecond latency */
822 stat_calc_dist(ts->io_u_map, ts_total_io_u(ts), io_u_dist);
823 stat_calc_lat_u(ts, io_u_lat_u);
824 stat_calc_lat_m(ts, io_u_lat_m);
825
826 /* Only show fixed 7 I/O depth levels*/
827 log_info(";%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%",
828 io_u_dist[0], io_u_dist[1], io_u_dist[2], io_u_dist[3],
829 io_u_dist[4], io_u_dist[5], io_u_dist[6]);
830
831 /* Microsecond latency */
832 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
833 log_info(";%3.2f%%", io_u_lat_u[i]);
834 /* Millisecond latency */
835 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
836 log_info(";%3.2f%%", io_u_lat_m[i]);
837 /* Additional output if continue_on_error set - default off*/
838 if (ts->continue_on_error)
839 log_info(";%lu;%d", ts->total_err_count, ts->first_error);
840 log_info("\n");
841
842 /* Additional output if description is set */
843 if (ts->description)
844 log_info(";%s", ts->description);
845
846 log_info("\n");
847}
848
3449ab8c
JA
849static void show_thread_status_terse_v3_v4(struct thread_stat *ts,
850 struct group_run_stats *rs, int ver)
c6ae0a5b 851{
2270890c 852 double io_u_dist[FIO_IO_U_MAP_NR];
04a0feae
JA
853 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
854 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
c6ae0a5b 855 double usr_cpu, sys_cpu;
04a0feae 856 int i;
c6ae0a5b 857
562c2d2f 858 /* General Info */
f3afa57e 859 log_info("%d;%s;%s;%d;%d", ver, fio_version_string,
5e726d0a 860 ts->name, ts->groupid, ts->error);
562c2d2f 861 /* Log Read Status */
6eaf09d6 862 show_ddir_status_terse(ts, rs, DDIR_READ);
562c2d2f 863 /* Log Write Status */
6eaf09d6
SL
864 show_ddir_status_terse(ts, rs, DDIR_WRITE);
865 /* Log Trim Status */
3449ab8c
JA
866 if (ver == 4)
867 show_ddir_status_terse(ts, rs, DDIR_TRIM);
c6ae0a5b 868
562c2d2f 869 /* CPU Usage */
756867bd
JA
870 if (ts->total_run_time) {
871 double runt = (double) ts->total_run_time;
c6ae0a5b 872
756867bd
JA
873 usr_cpu = (double) ts->usr_time * 100 / runt;
874 sys_cpu = (double) ts->sys_time * 100 / runt;
c6ae0a5b
JA
875 } else {
876 usr_cpu = 0;
877 sys_cpu = 0;
878 }
879
5ec10eaa
JA
880 log_info(";%f%%;%f%%;%lu;%lu;%lu", usr_cpu, sys_cpu, ts->ctx, ts->majf,
881 ts->minf);
2270890c 882
562c2d2f 883 /* Calc % distribution of IO depths, usecond, msecond latency */
838bc709 884 stat_calc_dist(ts->io_u_map, ts_total_io_u(ts), io_u_dist);
04a0feae
JA
885 stat_calc_lat_u(ts, io_u_lat_u);
886 stat_calc_lat_m(ts, io_u_lat_m);
2270890c 887
562c2d2f 888 /* Only show fixed 7 I/O depth levels*/
5ec10eaa
JA
889 log_info(";%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%",
890 io_u_dist[0], io_u_dist[1], io_u_dist[2], io_u_dist[3],
891 io_u_dist[4], io_u_dist[5], io_u_dist[6]);
2270890c 892
562c2d2f 893 /* Microsecond latency */
04a0feae
JA
894 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
895 log_info(";%3.2f%%", io_u_lat_u[i]);
562c2d2f 896 /* Millisecond latency */
04a0feae
JA
897 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
898 log_info(";%3.2f%%", io_u_lat_m[i]);
f2f788dd
JA
899
900 /* disk util stats, if any */
cc372b17 901 show_disk_util(1, NULL);
f2f788dd 902
562c2d2f 903 /* Additional output if continue_on_error set - default off*/
f2bba182
RR
904 if (ts->continue_on_error)
905 log_info(";%lu;%d", ts->total_err_count, ts->first_error);
2270890c 906
562c2d2f 907 /* Additional output if description is set */
4b0f2258 908 if (strlen(ts->description))
946e4276
JA
909 log_info(";%s", ts->description);
910
911 log_info("\n");
756867bd
JA
912}
913
cc372b17
SL
914static struct json_object *show_thread_status_json(struct thread_stat *ts,
915 struct group_run_stats *rs)
916{
917 struct json_object *root, *tmp;
918 double io_u_dist[FIO_IO_U_MAP_NR];
919 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
920 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
921 double usr_cpu, sys_cpu;
922 int i;
923
924 root = json_create_object();
925 json_object_add_value_string(root, "jobname", ts->name);
926 json_object_add_value_int(root, "groupid", ts->groupid);
927 json_object_add_value_int(root, "error", ts->error);
928
929 add_ddir_status_json(ts, rs, DDIR_READ, root);
930 add_ddir_status_json(ts, rs, DDIR_WRITE, root);
f3afa57e 931 add_ddir_status_json(ts, rs, DDIR_TRIM, root);
cc372b17
SL
932
933 /* CPU Usage */
934 if (ts->total_run_time) {
935 double runt = (double) ts->total_run_time;
936
937 usr_cpu = (double) ts->usr_time * 100 / runt;
938 sys_cpu = (double) ts->sys_time * 100 / runt;
939 } else {
940 usr_cpu = 0;
941 sys_cpu = 0;
942 }
943 json_object_add_value_float(root, "usr_cpu", usr_cpu);
944 json_object_add_value_float(root, "sys_cpu", sys_cpu);
945 json_object_add_value_int(root, "ctx", ts->ctx);
946 json_object_add_value_int(root, "majf", ts->majf);
947 json_object_add_value_int(root, "minf", ts->minf);
948
949
950 /* Calc % distribution of IO depths, usecond, msecond latency */
951 stat_calc_dist(ts->io_u_map, ts_total_io_u(ts), io_u_dist);
952 stat_calc_lat_u(ts, io_u_lat_u);
953 stat_calc_lat_m(ts, io_u_lat_m);
954
955 tmp = json_create_object();
956 json_object_add_value_object(root, "iodepth_level", tmp);
957 /* Only show fixed 7 I/O depth levels*/
958 for (i = 0; i < 7; i++) {
959 char name[20];
960 if (i < 6)
961 snprintf(name, 19, "%d", 1 << i);
962 else
963 snprintf(name, 19, ">=%d", 1 << i);
964 json_object_add_value_float(tmp, (const char *)name, io_u_dist[i]);
965 }
966
967 tmp = json_create_object();
968 json_object_add_value_object(root, "latency_us", tmp);
969 /* Microsecond latency */
970 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
971 const char *ranges[] = { "2", "4", "10", "20", "50", "100",
972 "250", "500", "750", "1000", };
973 json_object_add_value_float(tmp, ranges[i], io_u_lat_u[i]);
974 }
975 /* Millisecond latency */
976 tmp = json_create_object();
977 json_object_add_value_object(root, "latency_ms", tmp);
978 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++) {
979 const char *ranges[] = { "2", "4", "10", "20", "50", "100",
980 "250", "500", "750", "1000", "2000",
981 ">=2000", };
982 json_object_add_value_float(tmp, ranges[i], io_u_lat_m[i]);
983 }
984
985 /* Additional output if continue_on_error set - default off*/
986 if (ts->continue_on_error) {
987 json_object_add_value_int(root, "total_err", ts->total_err_count);
988 json_object_add_value_int(root, "total_err", ts->first_error);
989 }
990
991 /* Additional output if description is set */
992 if (strlen(ts->description))
993 json_object_add_value_string(root, "desc", ts->description);
994
995 return root;
996}
997
4d658652
JA
998static void show_thread_status_terse(struct thread_stat *ts,
999 struct group_run_stats *rs)
1000{
1001 if (terse_version == 2)
1002 show_thread_status_terse_v2(ts, rs);
3449ab8c
JA
1003 else if (terse_version == 3 || terse_version == 4)
1004 show_thread_status_terse_v3_v4(ts, rs, terse_version);
4d658652
JA
1005 else
1006 log_err("fio: bad terse version!? %d\n", terse_version);
1007}
1008
197574e4 1009static void sum_stat(struct io_stat *dst, struct io_stat *src, int nr)
756867bd
JA
1010{
1011 double mean, S;
1012
e09231c2
ZL
1013 if (src->samples == 0)
1014 return;
1015
756867bd
JA
1016 dst->min_val = min(dst->min_val, src->min_val);
1017 dst->max_val = max(dst->max_val, src->max_val);
756867bd
JA
1018
1019 /*
cdcac5cf
YH
1020 * Compute new mean and S after the merge
1021 * <http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
1022 * #Parallel_algorithm>
756867bd
JA
1023 */
1024 if (nr == 1) {
802ad4a8
JA
1025 mean = src->mean.u.f;
1026 S = src->S.u.f;
756867bd 1027 } else {
802ad4a8 1028 double delta = src->mean.u.f - dst->mean.u.f;
cdcac5cf 1029
802ad4a8
JA
1030 mean = ((src->mean.u.f * src->samples) +
1031 (dst->mean.u.f * dst->samples)) /
cdcac5cf
YH
1032 (dst->samples + src->samples);
1033
802ad4a8 1034 S = src->S.u.f + dst->S.u.f + pow(delta, 2.0) *
cdcac5cf
YH
1035 (dst->samples * src->samples) /
1036 (dst->samples + src->samples);
756867bd
JA
1037 }
1038
cdcac5cf 1039 dst->samples += src->samples;
802ad4a8
JA
1040 dst->mean.u.f = mean;
1041 dst->S.u.f = S;
756867bd
JA
1042}
1043
37f0c1ae
JA
1044void sum_group_stats(struct group_run_stats *dst, struct group_run_stats *src)
1045{
1046 int i;
1047
6eaf09d6 1048 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
37f0c1ae
JA
1049 if (dst->max_run[i] < src->max_run[i])
1050 dst->max_run[i] = src->max_run[i];
1051 if (dst->min_run[i] && dst->min_run[i] > src->min_run[i])
1052 dst->min_run[i] = src->min_run[i];
1053 if (dst->max_bw[i] < src->max_bw[i])
1054 dst->max_bw[i] = src->max_bw[i];
1055 if (dst->min_bw[i] && dst->min_bw[i] > src->min_bw[i])
1056 dst->min_bw[i] = src->min_bw[i];
1057
1058 dst->io_kb[i] += src->io_kb[i];
1059 dst->agg[i] += src->agg[i];
1060 }
1061
1062}
1063
5b9babb7
JA
1064void sum_thread_stats(struct thread_stat *dst, struct thread_stat *src, int nr)
1065{
1066 int l, k;
1067
6eaf09d6 1068 for (l = 0; l < DDIR_RWDIR_CNT; l++) {
771e58be
JA
1069 if (!dst->unified_rw_rep) {
1070 sum_stat(&dst->clat_stat[l], &src->clat_stat[l], nr);
1071 sum_stat(&dst->slat_stat[l], &src->slat_stat[l], nr);
1072 sum_stat(&dst->lat_stat[l], &src->lat_stat[l], nr);
1073 sum_stat(&dst->bw_stat[l], &src->bw_stat[l], nr);
1074
1075 dst->io_bytes[l] += src->io_bytes[l];
1076
1077 if (dst->runtime[l] < src->runtime[l])
1078 dst->runtime[l] = src->runtime[l];
1079 } else {
1080 sum_stat(&dst->clat_stat[0], &src->clat_stat[l], nr);
1081 sum_stat(&dst->slat_stat[0], &src->slat_stat[l], nr);
1082 sum_stat(&dst->lat_stat[0], &src->lat_stat[l], nr);
1083 sum_stat(&dst->bw_stat[0], &src->bw_stat[l], nr);
1084
1085 dst->io_bytes[0] += src->io_bytes[l];
1086
1087 if (dst->runtime[0] < src->runtime[l])
1088 dst->runtime[0] = src->runtime[l];
1089 }
5b9babb7
JA
1090 }
1091
1092 dst->usr_time += src->usr_time;
1093 dst->sys_time += src->sys_time;
1094 dst->ctx += src->ctx;
1095 dst->majf += src->majf;
1096 dst->minf += src->minf;
1097
1098 for (k = 0; k < FIO_IO_U_MAP_NR; k++)
1099 dst->io_u_map[k] += src->io_u_map[k];
1100 for (k = 0; k < FIO_IO_U_MAP_NR; k++)
1101 dst->io_u_submit[k] += src->io_u_submit[k];
1102 for (k = 0; k < FIO_IO_U_MAP_NR; k++)
1103 dst->io_u_complete[k] += src->io_u_complete[k];
1104 for (k = 0; k < FIO_IO_U_LAT_U_NR; k++)
1105 dst->io_u_lat_u[k] += src->io_u_lat_u[k];
1106 for (k = 0; k < FIO_IO_U_LAT_M_NR; k++)
1107 dst->io_u_lat_m[k] += src->io_u_lat_m[k];
1108
6eaf09d6 1109 for (k = 0; k < DDIR_RWDIR_CNT; k++) {
771e58be
JA
1110 if (!dst->unified_rw_rep) {
1111 dst->total_io_u[k] += src->total_io_u[k];
1112 dst->short_io_u[k] += src->short_io_u[k];
1113 } else {
1114 dst->total_io_u[0] += src->total_io_u[k];
1115 dst->short_io_u[0] += src->short_io_u[k];
1116 }
5b9babb7
JA
1117 }
1118
6eaf09d6 1119 for (k = 0; k < DDIR_RWDIR_CNT; k++) {
5b9babb7 1120 int m;
771e58be
JA
1121
1122 for (m = 0; m < FIO_IO_U_PLAT_NR; m++) {
1123 if (!dst->unified_rw_rep)
1124 dst->io_u_plat[k][m] += src->io_u_plat[k][m];
1125 else
1126 dst->io_u_plat[0][m] += src->io_u_plat[k][m];
1127 }
5b9babb7
JA
1128 }
1129
1130 dst->total_run_time += src->total_run_time;
1131 dst->total_submit += src->total_submit;
1132 dst->total_complete += src->total_complete;
1133}
1134
37f0c1ae
JA
1135void init_group_run_stat(struct group_run_stats *gs)
1136{
6eaf09d6 1137 int i;
37f0c1ae 1138 memset(gs, 0, sizeof(*gs));
6eaf09d6
SL
1139
1140 for (i = 0; i < DDIR_RWDIR_CNT; i++)
1141 gs->min_bw[i] = gs->min_run[i] = ~0UL;
37f0c1ae
JA
1142}
1143
1144void init_thread_stat(struct thread_stat *ts)
1145{
1146 int j;
1147
1148 memset(ts, 0, sizeof(*ts));
1149
6eaf09d6 1150 for (j = 0; j < DDIR_RWDIR_CNT; j++) {
37f0c1ae
JA
1151 ts->lat_stat[j].min_val = -1UL;
1152 ts->clat_stat[j].min_val = -1UL;
1153 ts->slat_stat[j].min_val = -1UL;
1154 ts->bw_stat[j].min_val = -1UL;
1155 }
1156 ts->groupid = -1;
1157}
1158
3c39a379
JA
1159void show_run_stats(void)
1160{
1161 struct group_run_stats *runstats, *rs;
1162 struct thread_data *td;
756867bd 1163 struct thread_stat *threadstats, *ts;
5b9babb7 1164 int i, j, nr_ts, last_ts, idx;
90fef2d1 1165 int kb_base_warned = 0;
cc372b17
SL
1166 struct json_object *root = NULL;
1167 struct json_array *array = NULL;
3c39a379
JA
1168
1169 runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1));
1170
37f0c1ae
JA
1171 for (i = 0; i < groupid + 1; i++)
1172 init_group_run_stat(&runstats[i]);
3c39a379 1173
756867bd
JA
1174 /*
1175 * find out how many threads stats we need. if group reporting isn't
1176 * enabled, it's one-per-td.
1177 */
1178 nr_ts = 0;
1179 last_ts = -1;
1180 for_each_td(td, i) {
2dc1bbeb 1181 if (!td->o.group_reporting) {
756867bd
JA
1182 nr_ts++;
1183 continue;
1184 }
1185 if (last_ts == td->groupid)
1186 continue;
1187
1188 last_ts = td->groupid;
1189 nr_ts++;
1190 }
1191
1192 threadstats = malloc(nr_ts * sizeof(struct thread_stat));
1193
37f0c1ae
JA
1194 for (i = 0; i < nr_ts; i++)
1195 init_thread_stat(&threadstats[i]);
756867bd
JA
1196
1197 j = 0;
1198 last_ts = -1;
197574e4 1199 idx = 0;
34572e28 1200 for_each_td(td, i) {
2dc1bbeb
JA
1201 if (idx && (!td->o.group_reporting ||
1202 (td->o.group_reporting && last_ts != td->groupid))) {
7abd0e3a
JA
1203 idx = 0;
1204 j++;
1205 }
1206
1207 last_ts = td->groupid;
1208
756867bd
JA
1209 ts = &threadstats[j];
1210
83349190
YH
1211 ts->clat_percentiles = td->o.clat_percentiles;
1212 if (td->o.overwrite_plist)
802ad4a8 1213 memcpy(ts->percentile_list, td->o.percentile_list, sizeof(td->o.percentile_list));
83349190 1214 else
802ad4a8 1215 memcpy(ts->percentile_list, def_percentile_list, sizeof(def_percentile_list));
83349190 1216
197574e4 1217 idx++;
6586ee89 1218 ts->members++;
756867bd 1219
7abd0e3a 1220 if (ts->groupid == -1) {
2dc84ba7
JA
1221 /*
1222 * These are per-group shared already
1223 */
f6bb5b88 1224 strncpy(ts->name, td->o.name, FIO_JOBNAME_SIZE);
a64e88da
JA
1225 if (td->o.description)
1226 strncpy(ts->description, td->o.description,
1227 FIO_JOBNAME_SIZE);
1228 else
1229 memset(ts->description, 0, FIO_JOBNAME_SIZE);
1230
756867bd 1231 ts->groupid = td->groupid;
2dc84ba7
JA
1232
1233 /*
1234 * first pid in group, not very useful...
1235 */
756867bd 1236 ts->pid = td->pid;
90fef2d1
JA
1237
1238 ts->kb_base = td->o.kb_base;
771e58be 1239 ts->unified_rw_rep = td->o.unified_rw_rep;
90fef2d1
JA
1240 } else if (ts->kb_base != td->o.kb_base && !kb_base_warned) {
1241 log_info("fio: kb_base differs for jobs in group, using"
1242 " %u as the base\n", ts->kb_base);
1243 kb_base_warned = 1;
2dc84ba7
JA
1244 }
1245
f2bba182
RR
1246 ts->continue_on_error = td->o.continue_on_error;
1247 ts->total_err_count += td->total_err_count;
1248 ts->first_error = td->first_error;
1249 if (!ts->error) {
1250 if (!td->error && td->o.continue_on_error &&
1251 td->first_error) {
1252 ts->error = td->first_error;
f6bb5b88 1253 strcpy(ts->verror, td->verror);
f2bba182
RR
1254 } else if (td->error) {
1255 ts->error = td->error;
f6bb5b88 1256 strcpy(ts->verror, td->verror);
f2bba182 1257 }
756867bd
JA
1258 }
1259
5b9babb7 1260 sum_thread_stats(ts, &td->ts, idx);
756867bd
JA
1261 }
1262
1263 for (i = 0; i < nr_ts; i++) {
94370ac4 1264 unsigned long long bw;
3c39a379 1265
756867bd
JA
1266 ts = &threadstats[i];
1267 rs = &runstats[ts->groupid];
90fef2d1 1268 rs->kb_base = ts->kb_base;
771e58be 1269 rs->unified_rw_rep += ts->unified_rw_rep;
3c39a379 1270
6eaf09d6 1271 for (j = 0; j < DDIR_RWDIR_CNT; j++) {
94370ac4
JA
1272 if (!ts->runtime[j])
1273 continue;
1274 if (ts->runtime[j] < rs->min_run[j] || !rs->min_run[j])
1275 rs->min_run[j] = ts->runtime[j];
1276 if (ts->runtime[j] > rs->max_run[j])
1277 rs->max_run[j] = ts->runtime[j];
1278
1279 bw = 0;
8879fd15 1280 if (ts->runtime[j]) {
c857cfeb
JA
1281 unsigned long runt = ts->runtime[j];
1282 unsigned long long kb;
8879fd15 1283
c857cfeb
JA
1284 kb = ts->io_bytes[j] / rs->kb_base;
1285 bw = kb * 1000 / runt;
8879fd15 1286 }
94370ac4
JA
1287 if (bw < rs->min_bw[j])
1288 rs->min_bw[j] = bw;
1289 if (bw > rs->max_bw[j])
1290 rs->max_bw[j] = bw;
1291
90fef2d1 1292 rs->io_kb[j] += ts->io_bytes[j] / rs->kb_base;
94370ac4 1293 }
3c39a379
JA
1294 }
1295
1296 for (i = 0; i < groupid + 1; i++) {
6eaf09d6
SL
1297 int ddir;
1298
3c39a379
JA
1299 rs = &runstats[i];
1300
6eaf09d6
SL
1301 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) {
1302 if (rs->max_run[ddir])
1303 rs->agg[ddir] = (rs->io_kb[ddir] * 1000) /
1304 rs->max_run[ddir];
1305 }
3c39a379
JA
1306 }
1307
1308 /*
1309 * don't overwrite last signal output
1310 */
f3afa57e 1311 if (output_format == FIO_OUTPUT_NORMAL)
4ceb30d4 1312 log_info("\n");
f3afa57e 1313 else if (output_format == FIO_OUTPUT_JSON) {
cc372b17
SL
1314 root = json_create_object();
1315 json_object_add_value_string(root, "fio version", fio_version_string);
1316 array = json_create_array();
1317 json_object_add_value_array(root, "jobs", array);
1318 }
1319
756867bd
JA
1320 for (i = 0; i < nr_ts; i++) {
1321 ts = &threadstats[i];
1322 rs = &runstats[ts->groupid];
3c39a379 1323
a64e88da
JA
1324 if (is_backend)
1325 fio_server_send_ts(ts, rs);
f3afa57e
JA
1326 else if (output_format == FIO_OUTPUT_TERSE)
1327 show_thread_status_terse(ts, rs);
1328 else if (output_format == FIO_OUTPUT_JSON) {
1329 struct json_object *tmp = show_thread_status_json(ts, rs);
1330 json_array_add_value_object(array, tmp);
cc372b17 1331 } else
756867bd 1332 show_thread_status(ts, rs);
3c39a379 1333 }
f3afa57e 1334 if (output_format == FIO_OUTPUT_JSON) {
cc372b17
SL
1335 /* disk util stats, if any */
1336 show_disk_util(1, root);
1337
1338 json_print_object(root);
1339 log_info("\n");
1340 json_free_object(root);
1341 }
3c39a379 1342
72c27ff8
JA
1343 for (i = 0; i < groupid + 1; i++) {
1344 rs = &runstats[i];
3c39a379 1345
72c27ff8 1346 rs->groupid = i;
d09a64a0 1347 if (is_backend)
72c27ff8 1348 fio_server_send_gs(rs);
f3afa57e 1349 else if (output_format == FIO_OUTPUT_NORMAL)
72c27ff8 1350 show_group_stats(rs);
c6ae0a5b 1351 }
eecf272f 1352
72c27ff8
JA
1353 if (is_backend)
1354 fio_server_send_du();
f3afa57e 1355 else if (output_format == FIO_OUTPUT_NORMAL)
cc372b17 1356 show_disk_util(0, NULL);
72c27ff8 1357
eecf272f 1358 free(runstats);
756867bd 1359 free(threadstats);
3c39a379
JA
1360}
1361
4c6d91e8
JA
1362static void *__show_running_run_stats(void *arg)
1363{
1364 struct thread_data *td;
1365 unsigned long long *rt;
1366 struct timeval tv;
1367 int i;
1368
1369 rt = malloc(thread_number * sizeof(unsigned long long));
1370 fio_gettime(&tv, NULL);
1371
1372 for_each_td(td, i) {
1373 rt[i] = mtime_since(&td->start, &tv);
1374 if (td_read(td) && td->io_bytes[DDIR_READ])
1375 td->ts.runtime[DDIR_READ] += rt[i];
1376 if (td_write(td) && td->io_bytes[DDIR_WRITE])
1377 td->ts.runtime[DDIR_WRITE] += rt[i];
6eaf09d6
SL
1378 if (td_trim(td) && td->io_bytes[DDIR_TRIM])
1379 td->ts.runtime[DDIR_TRIM] += rt[i];
4c6d91e8
JA
1380
1381 update_rusage_stat(td);
6eaf09d6
SL
1382 td->ts.io_bytes[DDIR_READ] = td->io_bytes[DDIR_READ];
1383 td->ts.io_bytes[DDIR_WRITE] = td->io_bytes[DDIR_WRITE];
1384 td->ts.io_bytes[DDIR_TRIM] = td->io_bytes[DDIR_TRIM];
4c6d91e8
JA
1385 td->ts.total_run_time = mtime_since(&td->epoch, &tv);
1386 }
1387
1388 show_run_stats();
1389
1390 for_each_td(td, i) {
1391 if (td_read(td) && td->io_bytes[DDIR_READ])
1392 td->ts.runtime[DDIR_READ] -= rt[i];
1393 if (td_write(td) && td->io_bytes[DDIR_WRITE])
1394 td->ts.runtime[DDIR_WRITE] -= rt[i];
6eaf09d6
SL
1395 if (td_trim(td) && td->io_bytes[DDIR_TRIM])
1396 td->ts.runtime[DDIR_TRIM] -= rt[i];
4c6d91e8
JA
1397 }
1398
1399 free(rt);
1400 return NULL;
1401}
1402
1403/*
1404 * Called from signal handler. It _should_ be safe to just run this inline
1405 * in the sig handler, but we should be disturbing the system less by just
1406 * creating a thread to do it.
1407 */
1408void show_running_run_stats(void)
1409{
1410 pthread_t thread;
1411
1412 pthread_create(&thread, NULL, __show_running_run_stats, NULL);
1413 pthread_detach(thread);
1414}
1415
68704084 1416static inline void add_stat_sample(struct io_stat *is, unsigned long data)
3c39a379 1417{
68704084 1418 double val = data;
6660cc67 1419 double delta;
68704084
JA
1420
1421 if (data > is->max_val)
1422 is->max_val = data;
1423 if (data < is->min_val)
1424 is->min_val = data;
1425
802ad4a8 1426 delta = val - is->mean.u.f;
ef11d737 1427 if (delta) {
802ad4a8
JA
1428 is->mean.u.f += delta / (is->samples + 1.0);
1429 is->S.u.f += delta * (val - is->mean.u.f);
ef11d737 1430 }
3c39a379 1431
3c39a379
JA
1432 is->samples++;
1433}
1434
bb3884d8 1435static void __add_log_sample(struct io_log *iolog, unsigned long val,
306ddc97 1436 enum fio_ddir ddir, unsigned int bs,
2b13e716 1437 unsigned long t)
3c39a379 1438{
306ddc97
JA
1439 const int nr_samples = iolog->nr_samples;
1440
b8bc8cba
JA
1441 if (!iolog->nr_samples)
1442 iolog->avg_last = t;
1443
3c39a379
JA
1444 if (iolog->nr_samples == iolog->max_samples) {
1445 int new_size = sizeof(struct io_sample) * iolog->max_samples*2;
1446
1447 iolog->log = realloc(iolog->log, new_size);
1448 iolog->max_samples <<= 1;
1449 }
1450
306ddc97 1451 iolog->log[nr_samples].val = val;
2b13e716 1452 iolog->log[nr_samples].time = t;
306ddc97
JA
1453 iolog->log[nr_samples].ddir = ddir;
1454 iolog->log[nr_samples].bs = bs;
3c39a379
JA
1455 iolog->nr_samples++;
1456}
1457
7fb28d36
JA
1458static inline void reset_io_stat(struct io_stat *ios)
1459{
1460 ios->max_val = ios->min_val = ios->samples = 0;
1461 ios->mean.u.f = ios->S.u.f = 0;
1462}
1463
bb3884d8 1464static void add_log_sample(struct thread_data *td, struct io_log *iolog,
306ddc97
JA
1465 unsigned long val, enum fio_ddir ddir,
1466 unsigned int bs)
bb3884d8 1467{
7fb28d36 1468 unsigned long elapsed, this_window;
b8bc8cba 1469
ff58fced
JA
1470 if (!ddir_rw(ddir))
1471 return;
1472
b8bc8cba
JA
1473 elapsed = mtime_since_now(&td->epoch);
1474
1475 /*
1476 * If no time averaging, just add the log sample.
1477 */
1478 if (!iolog->avg_msec) {
1479 __add_log_sample(iolog, val, ddir, bs, elapsed);
1480 return;
1481 }
1482
1483 /*
1484 * Add the sample. If the time period has passed, then
1485 * add that entry to the log and clear.
1486 */
1487 add_stat_sample(&iolog->avg_window[ddir], val);
1488
7fb28d36
JA
1489 /*
1490 * If period hasn't passed, adding the above sample is all we
1491 * need to do.
1492 */
b8bc8cba
JA
1493 this_window = elapsed - iolog->avg_last;
1494 if (this_window < iolog->avg_msec)
1495 return;
1496
7fb28d36
JA
1497 /*
1498 * Note an entry in the log. Use the mean from the logged samples,
1499 * making sure to properly round up. Only write a log entry if we
1500 * had actual samples done.
1501 */
1502 if (iolog->avg_window[DDIR_READ].samples) {
1503 unsigned long mr;
b8bc8cba 1504
7fb28d36 1505 mr = iolog->avg_window[DDIR_READ].mean.u.f + 0.50;
b8bc8cba 1506 __add_log_sample(iolog, mr, DDIR_READ, 0, elapsed);
7fb28d36
JA
1507 }
1508 if (iolog->avg_window[DDIR_WRITE].samples) {
1509 unsigned long mw;
1510
1511 mw = iolog->avg_window[DDIR_WRITE].mean.u.f + 0.50;
b8bc8cba 1512 __add_log_sample(iolog, mw, DDIR_WRITE, 0, elapsed);
7fb28d36 1513 }
6eaf09d6
SL
1514 if (iolog->avg_window[DDIR_TRIM].samples) {
1515 unsigned long mw;
1516
1517 mw = iolog->avg_window[DDIR_TRIM].mean.u.f + 0.50;
1518 __add_log_sample(iolog, mw, DDIR_TRIM, 0, elapsed);
1519 }
1520
b8bc8cba 1521
7fb28d36
JA
1522 reset_io_stat(&iolog->avg_window[DDIR_READ]);
1523 reset_io_stat(&iolog->avg_window[DDIR_WRITE]);
6eaf09d6 1524 reset_io_stat(&iolog->avg_window[DDIR_TRIM]);
b8bc8cba 1525 iolog->avg_last = elapsed;
bb3884d8
JA
1526}
1527
306ddc97 1528void add_agg_sample(unsigned long val, enum fio_ddir ddir, unsigned int bs)
bb3884d8 1529{
ff58fced 1530 struct io_log *iolog;
bb3884d8 1531
ff58fced
JA
1532 if (!ddir_rw(ddir))
1533 return;
1534
1535 iolog = agg_io_log[ddir];
306ddc97 1536 __add_log_sample(iolog, val, ddir, bs, mtime_since_genesis());
bb3884d8
JA
1537}
1538
83349190
YH
1539static void add_clat_percentile_sample(struct thread_stat *ts,
1540 unsigned long usec, enum fio_ddir ddir)
1541{
1542 unsigned int idx = plat_val_to_idx(usec);
1543 assert(idx < FIO_IO_U_PLAT_NR);
1544
1545 ts->io_u_plat[ddir][idx]++;
1546}
1547
1e97cce9 1548void add_clat_sample(struct thread_data *td, enum fio_ddir ddir,
306ddc97 1549 unsigned long usec, unsigned int bs)
3c39a379 1550{
756867bd 1551 struct thread_stat *ts = &td->ts;
079ad09b 1552
ff58fced
JA
1553 if (!ddir_rw(ddir))
1554 return;
1555
d85f5118 1556 add_stat_sample(&ts->clat_stat[ddir], usec);
3c39a379 1557
7b9f733a
JA
1558 if (td->clat_log)
1559 add_log_sample(td, td->clat_log, usec, ddir, bs);
83349190
YH
1560
1561 if (ts->clat_percentiles)
1562 add_clat_percentile_sample(ts, usec, ddir);
3c39a379
JA
1563}
1564
1e97cce9 1565void add_slat_sample(struct thread_data *td, enum fio_ddir ddir,
306ddc97 1566 unsigned long usec, unsigned int bs)
3c39a379 1567{
756867bd 1568 struct thread_stat *ts = &td->ts;
079ad09b 1569
ff58fced
JA
1570 if (!ddir_rw(ddir))
1571 return;
1572
d85f5118 1573 add_stat_sample(&ts->slat_stat[ddir], usec);
3c39a379 1574
7b9f733a
JA
1575 if (td->slat_log)
1576 add_log_sample(td, td->slat_log, usec, ddir, bs);
3c39a379
JA
1577}
1578
02af0988
JA
1579void add_lat_sample(struct thread_data *td, enum fio_ddir ddir,
1580 unsigned long usec, unsigned int bs)
1581{
1582 struct thread_stat *ts = &td->ts;
1583
ff58fced
JA
1584 if (!ddir_rw(ddir))
1585 return;
1586
02af0988
JA
1587 add_stat_sample(&ts->lat_stat[ddir], usec);
1588
7b9f733a
JA
1589 if (td->lat_log)
1590 add_log_sample(td, td->lat_log, usec, ddir, bs);
02af0988
JA
1591}
1592
306ddc97 1593void add_bw_sample(struct thread_data *td, enum fio_ddir ddir, unsigned int bs,
1e97cce9 1594 struct timeval *t)
3c39a379 1595{
756867bd 1596 struct thread_stat *ts = &td->ts;
ff58fced
JA
1597 unsigned long spent, rate;
1598
1599 if (!ddir_rw(ddir))
1600 return;
3c39a379 1601
c8eeb9df 1602 spent = mtime_since(&td->bw_sample_time, t);
2dc1bbeb 1603 if (spent < td->o.bw_avg_time)
3c39a379 1604 return;
9602d8df
JA
1605
1606 /*
5daa4ebe
JC
1607 * Compute both read and write rates for the interval.
1608 */
6eaf09d6 1609 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++) {
5daa4ebe
JC
1610 uint64_t delta;
1611
1612 delta = td->this_io_bytes[ddir] - td->stat_io_bytes[ddir];
1613 if (!delta)
1614 continue; /* No entries for interval */
3c39a379 1615
5daa4ebe
JC
1616 rate = delta * 1000 / spent / 1024;
1617 add_stat_sample(&ts->bw_stat[ddir], rate);
3c39a379 1618
5daa4ebe
JC
1619 if (td->bw_log)
1620 add_log_sample(td, td->bw_log, rate, ddir, bs);
1621
1622 td->stat_io_bytes[ddir] = td->this_io_bytes[ddir];
1623 }
3c39a379 1624
c8eeb9df 1625 fio_gettime(&td->bw_sample_time, NULL);
3c39a379 1626}
c8eeb9df
JA
1627
1628void add_iops_sample(struct thread_data *td, enum fio_ddir ddir,
1629 struct timeval *t)
1630{
1631 struct thread_stat *ts = &td->ts;
1632 unsigned long spent, iops;
1633
1634 if (!ddir_rw(ddir))
1635 return;
1636
1637 spent = mtime_since(&td->iops_sample_time, t);
1638 if (spent < td->o.iops_avg_time)
1639 return;
1640
9602d8df
JA
1641 /*
1642 * Compute both read and write rates for the interval.
1643 */
6eaf09d6 1644 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++) {
9602d8df 1645 uint64_t delta;
c8eeb9df 1646
9602d8df
JA
1647 delta = td->this_io_blocks[ddir] - td->stat_io_blocks[ddir];
1648 if (!delta)
1649 continue; /* No entries for interval */
1650
1651 iops = (delta * 1000) / spent;
1652 add_stat_sample(&ts->iops_stat[ddir], iops);
c8eeb9df 1653
9602d8df
JA
1654 if (td->iops_log)
1655 add_log_sample(td, td->iops_log, iops, ddir, 0);
1656
421f4a82 1657 td->stat_io_blocks[ddir] = td->this_io_blocks[ddir];
9602d8df 1658 }
c8eeb9df
JA
1659
1660 fio_gettime(&td->iops_sample_time, NULL);
c8eeb9df 1661}