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