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