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