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