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