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