Add IOPS to terse output
[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"
802ad4a8 12#include "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
117/*
118 * Find and display the p-th percentile of clat
119 */
c59971e1 120static void show_clat_percentiles(unsigned int *io_u_plat, unsigned long nr,
802ad4a8 121 fio_fp64_t *plist)
83349190
YH
122{
123 unsigned long sum = 0;
4f6f8298
JA
124 unsigned int len, i, j = 0, minv = -1U, maxv = 0;
125 unsigned int *ovals = NULL, oval_len = 0;
07511a63 126 int is_last, scale_down;
83349190 127
802ad4a8
JA
128 len = 0;
129 while (len < FIO_IO_U_LIST_MAX_LEN && plist[len].u.f != 0.0)
130 len++;
716050f2 131
351de8de
JA
132 if (!len)
133 return;
134
716050f2 135 /*
802ad4a8
JA
136 * Sort the percentile list. Note that it may already be sorted if
137 * we are using the default values, but since it's a short list this
138 * isn't a worry. Also note that this does not work for NaN values.
716050f2 139 */
802ad4a8
JA
140 if (len > 1)
141 qsort((void*)plist, len, sizeof(plist[0]), double_cmp);
83349190 142
4f6f8298
JA
143 /*
144 * Calculate bucket values, note down max and min values
145 */
07511a63
JA
146 is_last = 0;
147 for (i = 0; i < FIO_IO_U_PLAT_NR && !is_last; i++) {
83349190 148 sum += io_u_plat[i];
802ad4a8
JA
149 while (sum >= (plist[j].u.f / 100.0 * nr)) {
150 assert(plist[j].u.f <= 100.0);
83349190 151
4f6f8298
JA
152 if (j == oval_len) {
153 oval_len += 100;
154 ovals = realloc(ovals, oval_len * sizeof(unsigned int));
155 }
156
157 ovals[j] = plat_idx_to_val(i);
158 if (ovals[j] < minv)
159 minv = ovals[j];
160 if (ovals[j] > maxv)
161 maxv = ovals[j];
07511a63
JA
162
163 is_last = (j == len - 1);
164 if (is_last)
165 break;
166
4f6f8298
JA
167 j++;
168 }
169 }
83349190 170
4f6f8298
JA
171 /*
172 * We default to usecs, but if the value range is such that we
173 * should scale down to msecs, do that.
174 */
175 if (minv > 2000 && maxv > 99999) {
176 scale_down = 1;
177 log_info(" clat percentiles (msec):\n |");
178 } else {
179 scale_down = 0;
180 log_info(" clat percentiles (usec):\n |");
181 }
83349190 182
4f6f8298
JA
183 for (j = 0; j < len; j++) {
184 char fbuf[8];
81ab0b3a 185
4f6f8298
JA
186 /* for formatting */
187 if (j != 0 && (j % 4) == 0)
188 log_info(" |");
83349190 189
4f6f8298
JA
190 /* end of the list */
191 is_last = (j == len - 1);
83349190 192
4f6f8298
JA
193 if (plist[j].u.f < 10.0)
194 sprintf(fbuf, " %2.2f", plist[j].u.f);
195 else
196 sprintf(fbuf, "%2.2f", plist[j].u.f);
197
198 if (scale_down)
199 ovals[j] = (ovals[j] + 999) / 1000;
200
201 log_info(" %sth=[%5u]%c", fbuf, ovals[j], is_last ? '\n' : ',');
202
203 if (is_last)
204 break;
205
206 if (j % 4 == 3) /* for formatting */
207 log_info("\n");
83349190 208 }
4f6f8298
JA
209
210 if (ovals)
211 free(ovals);
83349190
YH
212}
213
3c39a379
JA
214static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max,
215 double *mean, double *dev)
216{
68704084 217 double n = is->samples;
3c39a379
JA
218
219 if (is->samples == 0)
220 return 0;
221
222 *min = is->min_val;
223 *max = is->max_val;
224
225 n = (double) is->samples;
802ad4a8 226 *mean = is->mean.u.f;
e6d276f2 227
68704084 228 if (n > 1.0)
802ad4a8 229 *dev = sqrt(is->S.u.f / (n - 1.0));
ef9c5c40 230 else
4b43f54e 231 *dev = 0;
ef9c5c40 232
3c39a379
JA
233 return 1;
234}
235
a64e88da 236void show_group_stats(struct group_run_stats *rs)
3c39a379 237{
dbe1125e
JA
238 char *p1, *p2, *p3, *p4;
239 const char *ddir_str[] = { " READ", " WRITE" };
240 int i;
241
25050629 242 log_info("Run status group %d (all jobs):\n", rs->groupid);
3c39a379 243
dbe1125e 244 for (i = 0; i <= DDIR_WRITE; i++) {
90fef2d1
JA
245 const int i2p = is_power_of_2(rs->kb_base);
246
dbe1125e
JA
247 if (!rs->max_run[i])
248 continue;
249
90fef2d1
JA
250 p1 = num2str(rs->io_kb[i], 6, rs->kb_base, i2p);
251 p2 = num2str(rs->agg[i], 6, rs->kb_base, i2p);
252 p3 = num2str(rs->min_bw[i], 6, rs->kb_base, i2p);
253 p4 = num2str(rs->max_bw[i], 6, rs->kb_base, i2p);
dbe1125e 254
b22989b9 255 log_info("%s: io=%sB, aggrb=%sB/s, minb=%sB/s, maxb=%sB/s,"
5ec10eaa
JA
256 " mint=%llumsec, maxt=%llumsec\n", ddir_str[i], p1, p2,
257 p3, p4, rs->min_run[i],
258 rs->max_run[i]);
dbe1125e
JA
259
260 free(p1);
261 free(p2);
262 free(p3);
263 free(p4);
264 }
3c39a379
JA
265}
266
b3605062
JA
267#define ts_total_io_u(ts) \
268 ((ts)->total_io_u[0] + (ts)->total_io_u[1])
269
838bc709
JA
270static void stat_calc_dist(unsigned int *map, unsigned long total,
271 double *io_u_dist)
2270890c
JA
272{
273 int i;
274
275 /*
276 * Do depth distribution calculations
277 */
278 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
838bc709
JA
279 if (total) {
280 io_u_dist[i] = (double) map[i] / (double) total;
281 io_u_dist[i] *= 100.0;
282 if (io_u_dist[i] < 0.1 && map[i])
283 io_u_dist[i] = 0.1;
284 } else
285 io_u_dist[i] = 0.0;
2270890c
JA
286 }
287}
288
04a0feae
JA
289static void stat_calc_lat(struct thread_stat *ts, double *dst,
290 unsigned int *src, int nr)
2270890c 291{
838bc709 292 unsigned long total = ts_total_io_u(ts);
2270890c
JA
293 int i;
294
295 /*
296 * Do latency distribution calculations
297 */
04a0feae 298 for (i = 0; i < nr; i++) {
838bc709
JA
299 if (total) {
300 dst[i] = (double) src[i] / (double) total;
301 dst[i] *= 100.0;
302 if (dst[i] < 0.01 && src[i])
303 dst[i] = 0.01;
304 } else
305 dst[i] = 0.0;
2270890c
JA
306 }
307}
308
04a0feae
JA
309static void stat_calc_lat_u(struct thread_stat *ts, double *io_u_lat)
310{
311 stat_calc_lat(ts, io_u_lat, ts->io_u_lat_u, FIO_IO_U_LAT_U_NR);
312}
313
314static void stat_calc_lat_m(struct thread_stat *ts, double *io_u_lat)
315{
316 stat_calc_lat(ts, io_u_lat, ts->io_u_lat_m, FIO_IO_U_LAT_M_NR);
317}
318
ea2accc5
JA
319static int usec_to_msec(unsigned long *min, unsigned long *max, double *mean,
320 double *dev)
321{
322 if (*min > 1000 && *max > 1000 && *mean > 1000.0 && *dev > 1000.0) {
323 *min /= 1000;
324 *max /= 1000;
325 *mean /= 1000.0;
326 *dev /= 1000.0;
327 return 0;
328 }
329
330 return 1;
331}
332
756867bd 333static void show_ddir_status(struct group_run_stats *rs, struct thread_stat *ts,
3c39a379
JA
334 int ddir)
335{
3c9b60c1 336 const char *ddir_str[] = { "read ", "write" };
8879fd15 337 unsigned long min, max, runt;
b3605062 338 unsigned long long bw, iops;
3c39a379 339 double mean, dev;
b3605062 340 char *io_p, *bw_p, *iops_p;
90fef2d1 341 int i2p;
3c39a379 342
ff58fced
JA
343 assert(ddir_rw(ddir));
344
756867bd 345 if (!ts->runtime[ddir])
3c39a379
JA
346 return;
347
90fef2d1 348 i2p = is_power_of_2(rs->kb_base);
8879fd15
JA
349 runt = ts->runtime[ddir];
350
351 bw = (1000 * ts->io_bytes[ddir]) / runt;
90fef2d1
JA
352 io_p = num2str(ts->io_bytes[ddir], 6, 1, i2p);
353 bw_p = num2str(bw, 6, 1, i2p);
8879fd15 354
0aacc50c 355 iops = (1000 * (uint64_t)ts->total_io_u[ddir]) / runt;
b3605062 356 iops_p = num2str(iops, 6, 1, 0);
dbe1125e 357
cda99fa0 358 log_info(" %s: io=%sB, bw=%sB/s, iops=%s, runt=%6llumsec\n",
5ec10eaa
JA
359 ddir_str[ddir], io_p, bw_p, iops_p,
360 ts->runtime[ddir]);
dbe1125e
JA
361
362 free(io_p);
363 free(bw_p);
b3605062 364 free(iops_p);
3c39a379 365
d85f5118
JA
366 if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev)) {
367 const char *base = "(usec)";
d9309cb1 368 char *minp, *maxp;
d85f5118 369
ea2accc5 370 if (!usec_to_msec(&min, &max, &mean, &dev))
d85f5118 371 base = "(msec)";
ea2accc5 372
d9309cb1
JA
373 minp = num2str(min, 6, 1, 0);
374 maxp = num2str(max, 6, 1, 0);
375
5ec10eaa
JA
376 log_info(" slat %s: min=%s, max=%s, avg=%5.02f,"
377 " stdev=%5.02f\n", base, minp, maxp, mean, dev);
d9309cb1
JA
378
379 free(minp);
380 free(maxp);
d85f5118
JA
381 }
382 if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev)) {
383 const char *base = "(usec)";
d9309cb1 384 char *minp, *maxp;
d85f5118 385
ea2accc5
JA
386 if (!usec_to_msec(&min, &max, &mean, &dev))
387 base = "(msec)";
388
d9309cb1
JA
389 minp = num2str(min, 6, 1, 0);
390 maxp = num2str(max, 6, 1, 0);
5ec10eaa
JA
391
392 log_info(" clat %s: min=%s, max=%s, avg=%5.02f,"
393 " stdev=%5.02f\n", base, minp, maxp, mean, dev);
d9309cb1
JA
394
395 free(minp);
396 free(maxp);
d85f5118 397 }
02af0988
JA
398 if (calc_lat(&ts->lat_stat[ddir], &min, &max, &mean, &dev)) {
399 const char *base = "(usec)";
400 char *minp, *maxp;
401
402 if (!usec_to_msec(&min, &max, &mean, &dev))
403 base = "(msec)";
404
405 minp = num2str(min, 6, 1, 0);
406 maxp = num2str(max, 6, 1, 0);
407
408 log_info(" lat %s: min=%s, max=%s, avg=%5.02f,"
409 " stdev=%5.02f\n", base, minp, maxp, mean, dev);
410
411 free(minp);
412 free(maxp);
413 }
83349190
YH
414 if (ts->clat_percentiles) {
415 show_clat_percentiles(ts->io_u_plat[ddir],
416 ts->clat_stat[ddir].samples,
417 ts->percentile_list);
418 }
079ad09b 419 if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
3c39a379
JA
420 double p_of_agg;
421
422 p_of_agg = mean * 100 / (double) rs->agg[ddir];
c8eeb9df 423 log_info(" bw (KB/s) : min=%5lu, max=%5lu, per=%3.2f%%,"
5ec10eaa
JA
424 " avg=%5.02f, stdev=%5.02f\n", min, max, p_of_agg,
425 mean, dev);
3c39a379
JA
426 }
427}
428
04a0feae
JA
429static void show_lat(double *io_u_lat, int nr, const char **ranges,
430 const char *msg)
431{
432 int new_line = 1, i, line = 0;
433
434 for (i = 0; i < nr; i++) {
435 if (io_u_lat[i] <= 0.0)
436 continue;
437 if (new_line) {
4539ed73
JA
438 if (line)
439 log_info("\n");
04a0feae
JA
440 log_info(" lat (%s): ", msg);
441 new_line = 0;
442 line = 0;
443 }
444 if (line)
445 log_info(", ");
446 log_info("%s%3.2f%%", ranges[i], io_u_lat[i]);
447 line++;
448 if (line == 5)
449 new_line = 1;
450 }
04a0feae
JA
451}
452
453static void show_lat_u(double *io_u_lat_u)
454{
455 const char *ranges[] = { "2=", "4=", "10=", "20=", "50=", "100=",
456 "250=", "500=", "750=", "1000=", };
457
458 show_lat(io_u_lat_u, FIO_IO_U_LAT_U_NR, ranges, "usec");
459}
460
461static void show_lat_m(double *io_u_lat_m)
462{
463 const char *ranges[] = { "2=", "4=", "10=", "20=", "50=", "100=",
464 "250=", "500=", "750=", "1000=", "2000=",
465 ">=2000=", };
466
467 show_lat(io_u_lat_m, FIO_IO_U_LAT_M_NR, ranges, "msec");
468}
469
470static void show_latencies(double *io_u_lat_u, double *io_u_lat_m)
471{
472 show_lat_u(io_u_lat_u);
4539ed73 473 log_info("\n");
04a0feae
JA
474 show_lat_m(io_u_lat_m);
475 log_info("\n");
476}
477
a64e88da 478void show_thread_status(struct thread_stat *ts, struct group_run_stats *rs)
3c39a379
JA
479{
480 double usr_cpu, sys_cpu;
69008999 481 unsigned long runtime;
71619dc2 482 double io_u_dist[FIO_IO_U_MAP_NR];
04a0feae
JA
483 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
484 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
3c39a379 485
b4c5e1ac
JA
486 if (!(ts->io_bytes[0] + ts->io_bytes[1]) &&
487 !(ts->total_io_u[0] + ts->total_io_u[1]))
3c39a379
JA
488 return;
489
5ec10eaa
JA
490 if (!ts->error) {
491 log_info("%s: (groupid=%d, jobs=%d): err=%2d: pid=%d\n",
492 ts->name, ts->groupid, ts->members,
5921e80c 493 ts->error, (int) ts->pid);
5ec10eaa
JA
494 } else {
495 log_info("%s: (groupid=%d, jobs=%d): err=%2d (%s): pid=%d\n",
496 ts->name, ts->groupid, ts->members,
5921e80c 497 ts->error, ts->verror, (int) ts->pid);
5ec10eaa 498 }
3c39a379 499
7bdce1bd 500 if (ts->description)
6d86144d 501 log_info(" Description : [%s]\n", ts->description);
7bdce1bd 502
756867bd
JA
503 if (ts->io_bytes[DDIR_READ])
504 show_ddir_status(rs, ts, DDIR_READ);
505 if (ts->io_bytes[DDIR_WRITE])
506 show_ddir_status(rs, ts, DDIR_WRITE);
3c39a379 507
756867bd 508 runtime = ts->total_run_time;
69008999 509 if (runtime) {
1e97cce9 510 double runt = (double) runtime;
3c39a379 511
756867bd
JA
512 usr_cpu = (double) ts->usr_time * 100 / runt;
513 sys_cpu = (double) ts->sys_time * 100 / runt;
3c39a379
JA
514 } else {
515 usr_cpu = 0;
516 sys_cpu = 0;
517 }
518
5ec10eaa
JA
519 log_info(" cpu : usr=%3.2f%%, sys=%3.2f%%, ctx=%lu, majf=%lu,"
520 " minf=%lu\n", usr_cpu, sys_cpu, ts->ctx, ts->majf, ts->minf);
71619dc2 521
838bc709 522 stat_calc_dist(ts->io_u_map, ts_total_io_u(ts), io_u_dist);
5ec10eaa
JA
523 log_info(" IO depths : 1=%3.1f%%, 2=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%,"
524 " 16=%3.1f%%, 32=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
525 io_u_dist[1], io_u_dist[2],
526 io_u_dist[3], io_u_dist[4],
527 io_u_dist[5], io_u_dist[6]);
838bc709
JA
528
529 stat_calc_dist(ts->io_u_submit, ts->total_submit, io_u_dist);
530 log_info(" submit : 0=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%, 16=%3.1f%%,"
531 " 32=%3.1f%%, 64=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
532 io_u_dist[1], io_u_dist[2],
533 io_u_dist[3], io_u_dist[4],
534 io_u_dist[5], io_u_dist[6]);
535 stat_calc_dist(ts->io_u_complete, ts->total_complete, io_u_dist);
536 log_info(" complete : 0=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%, 16=%3.1f%%,"
537 " 32=%3.1f%%, 64=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
538 io_u_dist[1], io_u_dist[2],
539 io_u_dist[3], io_u_dist[4],
540 io_u_dist[5], io_u_dist[6]);
0d29de83 541 log_info(" issued r/w/d: total=%lu/%lu/%lu, short=%lu/%lu/%lu\n",
5ec10eaa 542 ts->total_io_u[0], ts->total_io_u[1],
0d29de83
JA
543 ts->total_io_u[2],
544 ts->short_io_u[0], ts->short_io_u[1],
545 ts->short_io_u[2]);
838bc709
JA
546 stat_calc_lat_u(ts, io_u_lat_u);
547 stat_calc_lat_m(ts, io_u_lat_m);
04a0feae 548 show_latencies(io_u_lat_u, io_u_lat_m);
f2bba182 549 if (ts->continue_on_error) {
1ec99eea
JA
550 log_info(" errors : total=%lu, first_error=%d/<%s>\n",
551 ts->total_err_count,
552 ts->first_error,
553 strerror(ts->first_error));
f2bba182 554 }
3c39a379
JA
555}
556
756867bd 557static void show_ddir_status_terse(struct thread_stat *ts,
c6ae0a5b
JA
558 struct group_run_stats *rs, int ddir)
559{
560 unsigned long min, max;
312b4af2 561 unsigned long long bw, iops;
c6ae0a5b
JA
562 double mean, dev;
563
ff58fced
JA
564 assert(ddir_rw(ddir));
565
312b4af2
JA
566 iops = bw = 0;
567 if (ts->runtime[ddir]) {
568 uint64_t runt = ts->runtime[ddir];
569
570 bw = ts->io_bytes[ddir] / runt;
571 iops = (1000 * (uint64_t) ts->total_io_u[ddir]) / runt;
572 }
c6ae0a5b 573
312b4af2 574 log_info(";%llu;%llu;%llu;%llu", ts->io_bytes[ddir] >> 10, bw, iops,
5ec10eaa 575 ts->runtime[ddir]);
c6ae0a5b 576
079ad09b 577 if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev))
6d86144d 578 log_info(";%lu;%lu;%f;%f", min, max, mean, dev);
c6ae0a5b 579 else
6d86144d 580 log_info(";%lu;%lu;%f;%f", 0UL, 0UL, 0.0, 0.0);
c6ae0a5b 581
079ad09b 582 if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev))
6d86144d 583 log_info(";%lu;%lu;%f;%f", min, max, mean, dev);
c6ae0a5b 584 else
6d86144d 585 log_info(";%lu;%lu;%f;%f", 0UL, 0UL, 0.0, 0.0);
c6ae0a5b 586
02af0988
JA
587 if (calc_lat(&ts->lat_stat[ddir], &min, &max, &mean, &dev))
588 log_info(";%lu;%lu;%f;%f", min, max, mean, dev);
589 else
590 log_info(";%lu;%lu;%f;%f", 0UL, 0UL, 0.0, 0.0);
591
079ad09b 592 if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
c6ae0a5b
JA
593 double p_of_agg;
594
595 p_of_agg = mean * 100 / (double) rs->agg[ddir];
6d86144d 596 log_info(";%lu;%lu;%f%%;%f;%f", min, max, p_of_agg, mean, dev);
c6ae0a5b 597 } else
6d86144d 598 log_info(";%lu;%lu;%f%%;%f;%f", 0UL, 0UL, 0.0, 0.0, 0.0);
c6ae0a5b
JA
599}
600
312b4af2 601#define FIO_TERSE_VERSION "3"
c6ae0a5b 602
756867bd 603static void show_thread_status_terse(struct thread_stat *ts,
c6ae0a5b
JA
604 struct group_run_stats *rs)
605{
2270890c 606 double io_u_dist[FIO_IO_U_MAP_NR];
04a0feae
JA
607 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
608 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
c6ae0a5b 609 double usr_cpu, sys_cpu;
04a0feae 610 int i;
c6ae0a5b 611
562c2d2f 612 /* General Info */
525c2bfa
JA
613 log_info("%s;%s;%d;%d", FIO_TERSE_VERSION, ts->name, ts->groupid,
614 ts->error);
562c2d2f 615 /* Log Read Status */
756867bd 616 show_ddir_status_terse(ts, rs, 0);
562c2d2f 617 /* Log Write Status */
756867bd 618 show_ddir_status_terse(ts, rs, 1);
c6ae0a5b 619
562c2d2f 620 /* CPU Usage */
756867bd
JA
621 if (ts->total_run_time) {
622 double runt = (double) ts->total_run_time;
c6ae0a5b 623
756867bd
JA
624 usr_cpu = (double) ts->usr_time * 100 / runt;
625 sys_cpu = (double) ts->sys_time * 100 / runt;
c6ae0a5b
JA
626 } else {
627 usr_cpu = 0;
628 sys_cpu = 0;
629 }
630
5ec10eaa
JA
631 log_info(";%f%%;%f%%;%lu;%lu;%lu", usr_cpu, sys_cpu, ts->ctx, ts->majf,
632 ts->minf);
2270890c 633
562c2d2f 634 /* Calc % distribution of IO depths, usecond, msecond latency */
838bc709 635 stat_calc_dist(ts->io_u_map, ts_total_io_u(ts), io_u_dist);
04a0feae
JA
636 stat_calc_lat_u(ts, io_u_lat_u);
637 stat_calc_lat_m(ts, io_u_lat_m);
2270890c 638
562c2d2f 639 /* Only show fixed 7 I/O depth levels*/
5ec10eaa
JA
640 log_info(";%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%",
641 io_u_dist[0], io_u_dist[1], io_u_dist[2], io_u_dist[3],
642 io_u_dist[4], io_u_dist[5], io_u_dist[6]);
2270890c 643
562c2d2f 644 /* Microsecond latency */
04a0feae
JA
645 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
646 log_info(";%3.2f%%", io_u_lat_u[i]);
562c2d2f 647 /* Millisecond latency */
04a0feae
JA
648 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
649 log_info(";%3.2f%%", io_u_lat_m[i]);
562c2d2f 650 /* Additional output if continue_on_error set - default off*/
f2bba182
RR
651 if (ts->continue_on_error)
652 log_info(";%lu;%d", ts->total_err_count, ts->first_error);
04a0feae 653 log_info("\n");
2270890c 654
562c2d2f 655 /* Additional output if description is set */
2270890c 656 if (ts->description)
6d86144d 657 log_info(";%s", ts->description);
2270890c 658
6d86144d 659 log_info("\n");
756867bd
JA
660}
661
197574e4 662static void sum_stat(struct io_stat *dst, struct io_stat *src, int nr)
756867bd
JA
663{
664 double mean, S;
665
e09231c2
ZL
666 if (src->samples == 0)
667 return;
668
756867bd
JA
669 dst->min_val = min(dst->min_val, src->min_val);
670 dst->max_val = max(dst->max_val, src->max_val);
756867bd
JA
671
672 /*
cdcac5cf
YH
673 * Compute new mean and S after the merge
674 * <http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
675 * #Parallel_algorithm>
756867bd
JA
676 */
677 if (nr == 1) {
802ad4a8
JA
678 mean = src->mean.u.f;
679 S = src->S.u.f;
756867bd 680 } else {
802ad4a8 681 double delta = src->mean.u.f - dst->mean.u.f;
cdcac5cf 682
802ad4a8
JA
683 mean = ((src->mean.u.f * src->samples) +
684 (dst->mean.u.f * dst->samples)) /
cdcac5cf
YH
685 (dst->samples + src->samples);
686
802ad4a8 687 S = src->S.u.f + dst->S.u.f + pow(delta, 2.0) *
cdcac5cf
YH
688 (dst->samples * src->samples) /
689 (dst->samples + src->samples);
756867bd
JA
690 }
691
cdcac5cf 692 dst->samples += src->samples;
802ad4a8
JA
693 dst->mean.u.f = mean;
694 dst->S.u.f = S;
756867bd
JA
695}
696
37f0c1ae
JA
697void sum_group_stats(struct group_run_stats *dst, struct group_run_stats *src)
698{
699 int i;
700
701 for (i = 0; i < 2; i++) {
702 if (dst->max_run[i] < src->max_run[i])
703 dst->max_run[i] = src->max_run[i];
704 if (dst->min_run[i] && dst->min_run[i] > src->min_run[i])
705 dst->min_run[i] = src->min_run[i];
706 if (dst->max_bw[i] < src->max_bw[i])
707 dst->max_bw[i] = src->max_bw[i];
708 if (dst->min_bw[i] && dst->min_bw[i] > src->min_bw[i])
709 dst->min_bw[i] = src->min_bw[i];
710
711 dst->io_kb[i] += src->io_kb[i];
712 dst->agg[i] += src->agg[i];
713 }
714
715}
716
5b9babb7
JA
717void sum_thread_stats(struct thread_stat *dst, struct thread_stat *src, int nr)
718{
719 int l, k;
720
721 for (l = 0; l <= DDIR_WRITE; l++) {
722 sum_stat(&dst->clat_stat[l], &src->clat_stat[l], nr);
723 sum_stat(&dst->slat_stat[l], &src->slat_stat[l], nr);
724 sum_stat(&dst->lat_stat[l], &src->lat_stat[l], nr);
725 sum_stat(&dst->bw_stat[l], &src->bw_stat[l], nr);
726
727 dst->io_bytes[l] += src->io_bytes[l];
728
729 if (dst->runtime[l] < src->runtime[l])
730 dst->runtime[l] = src->runtime[l];
731 }
732
733 dst->usr_time += src->usr_time;
734 dst->sys_time += src->sys_time;
735 dst->ctx += src->ctx;
736 dst->majf += src->majf;
737 dst->minf += src->minf;
738
739 for (k = 0; k < FIO_IO_U_MAP_NR; k++)
740 dst->io_u_map[k] += src->io_u_map[k];
741 for (k = 0; k < FIO_IO_U_MAP_NR; k++)
742 dst->io_u_submit[k] += src->io_u_submit[k];
743 for (k = 0; k < FIO_IO_U_MAP_NR; k++)
744 dst->io_u_complete[k] += src->io_u_complete[k];
745 for (k = 0; k < FIO_IO_U_LAT_U_NR; k++)
746 dst->io_u_lat_u[k] += src->io_u_lat_u[k];
747 for (k = 0; k < FIO_IO_U_LAT_M_NR; k++)
748 dst->io_u_lat_m[k] += src->io_u_lat_m[k];
749
750 for (k = 0; k <= 2; k++) {
751 dst->total_io_u[k] += src->total_io_u[k];
752 dst->short_io_u[k] += src->short_io_u[k];
753 }
754
755 for (k = 0; k <= DDIR_WRITE; k++) {
756 int m;
757 for (m = 0; m < FIO_IO_U_PLAT_NR; m++)
758 dst->io_u_plat[k][m] += src->io_u_plat[k][m];
759 }
760
761 dst->total_run_time += src->total_run_time;
762 dst->total_submit += src->total_submit;
763 dst->total_complete += src->total_complete;
764}
765
37f0c1ae
JA
766void init_group_run_stat(struct group_run_stats *gs)
767{
768 memset(gs, 0, sizeof(*gs));
769 gs->min_bw[0] = gs->min_run[0] = ~0UL;
770 gs->min_bw[1] = gs->min_run[1] = ~0UL;
771}
772
773void init_thread_stat(struct thread_stat *ts)
774{
775 int j;
776
777 memset(ts, 0, sizeof(*ts));
778
779 for (j = 0; j <= DDIR_WRITE; j++) {
780 ts->lat_stat[j].min_val = -1UL;
781 ts->clat_stat[j].min_val = -1UL;
782 ts->slat_stat[j].min_val = -1UL;
783 ts->bw_stat[j].min_val = -1UL;
784 }
785 ts->groupid = -1;
786}
787
3c39a379
JA
788void show_run_stats(void)
789{
790 struct group_run_stats *runstats, *rs;
791 struct thread_data *td;
756867bd 792 struct thread_stat *threadstats, *ts;
5b9babb7 793 int i, j, nr_ts, last_ts, idx;
90fef2d1 794 int kb_base_warned = 0;
3c39a379
JA
795
796 runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1));
797
37f0c1ae
JA
798 for (i = 0; i < groupid + 1; i++)
799 init_group_run_stat(&runstats[i]);
3c39a379 800
756867bd
JA
801 /*
802 * find out how many threads stats we need. if group reporting isn't
803 * enabled, it's one-per-td.
804 */
805 nr_ts = 0;
806 last_ts = -1;
807 for_each_td(td, i) {
2dc1bbeb 808 if (!td->o.group_reporting) {
756867bd
JA
809 nr_ts++;
810 continue;
811 }
812 if (last_ts == td->groupid)
813 continue;
814
815 last_ts = td->groupid;
816 nr_ts++;
817 }
818
819 threadstats = malloc(nr_ts * sizeof(struct thread_stat));
820
37f0c1ae
JA
821 for (i = 0; i < nr_ts; i++)
822 init_thread_stat(&threadstats[i]);
756867bd
JA
823
824 j = 0;
825 last_ts = -1;
197574e4 826 idx = 0;
34572e28 827 for_each_td(td, i) {
2dc1bbeb
JA
828 if (idx && (!td->o.group_reporting ||
829 (td->o.group_reporting && last_ts != td->groupid))) {
7abd0e3a
JA
830 idx = 0;
831 j++;
832 }
833
834 last_ts = td->groupid;
835
756867bd
JA
836 ts = &threadstats[j];
837
83349190
YH
838 ts->clat_percentiles = td->o.clat_percentiles;
839 if (td->o.overwrite_plist)
802ad4a8 840 memcpy(ts->percentile_list, td->o.percentile_list, sizeof(td->o.percentile_list));
83349190 841 else
802ad4a8 842 memcpy(ts->percentile_list, def_percentile_list, sizeof(def_percentile_list));
83349190 843
197574e4 844 idx++;
6586ee89 845 ts->members++;
756867bd 846
7abd0e3a 847 if (ts->groupid == -1) {
2dc84ba7
JA
848 /*
849 * These are per-group shared already
850 */
f6bb5b88 851 strncpy(ts->name, td->o.name, FIO_JOBNAME_SIZE);
a64e88da
JA
852 if (td->o.description)
853 strncpy(ts->description, td->o.description,
854 FIO_JOBNAME_SIZE);
855 else
856 memset(ts->description, 0, FIO_JOBNAME_SIZE);
857
756867bd 858 ts->groupid = td->groupid;
2dc84ba7
JA
859
860 /*
861 * first pid in group, not very useful...
862 */
756867bd 863 ts->pid = td->pid;
90fef2d1
JA
864
865 ts->kb_base = td->o.kb_base;
866 } else if (ts->kb_base != td->o.kb_base && !kb_base_warned) {
867 log_info("fio: kb_base differs for jobs in group, using"
868 " %u as the base\n", ts->kb_base);
869 kb_base_warned = 1;
2dc84ba7
JA
870 }
871
f2bba182
RR
872 ts->continue_on_error = td->o.continue_on_error;
873 ts->total_err_count += td->total_err_count;
874 ts->first_error = td->first_error;
875 if (!ts->error) {
876 if (!td->error && td->o.continue_on_error &&
877 td->first_error) {
878 ts->error = td->first_error;
f6bb5b88 879 strcpy(ts->verror, td->verror);
f2bba182
RR
880 } else if (td->error) {
881 ts->error = td->error;
f6bb5b88 882 strcpy(ts->verror, td->verror);
f2bba182 883 }
756867bd
JA
884 }
885
5b9babb7 886 sum_thread_stats(ts, &td->ts, idx);
756867bd
JA
887 }
888
889 for (i = 0; i < nr_ts; i++) {
94370ac4 890 unsigned long long bw;
3c39a379 891
756867bd
JA
892 ts = &threadstats[i];
893 rs = &runstats[ts->groupid];
90fef2d1 894 rs->kb_base = ts->kb_base;
3c39a379 895
de64df05 896 for (j = 0; j <= DDIR_WRITE; j++) {
94370ac4
JA
897 if (!ts->runtime[j])
898 continue;
899 if (ts->runtime[j] < rs->min_run[j] || !rs->min_run[j])
900 rs->min_run[j] = ts->runtime[j];
901 if (ts->runtime[j] > rs->max_run[j])
902 rs->max_run[j] = ts->runtime[j];
903
904 bw = 0;
8879fd15
JA
905 if (ts->runtime[j]) {
906 unsigned long runt;
907
90fef2d1 908 runt = ts->runtime[j];
8879fd15
JA
909 bw = ts->io_bytes[j] / runt;
910 }
94370ac4
JA
911 if (bw < rs->min_bw[j])
912 rs->min_bw[j] = bw;
913 if (bw > rs->max_bw[j])
914 rs->max_bw[j] = bw;
915
90fef2d1 916 rs->io_kb[j] += ts->io_bytes[j] / rs->kb_base;
94370ac4 917 }
3c39a379
JA
918 }
919
920 for (i = 0; i < groupid + 1; i++) {
8879fd15
JA
921 unsigned long max_run[2];
922
3c39a379 923 rs = &runstats[i];
90fef2d1
JA
924 max_run[0] = rs->max_run[0];
925 max_run[1] = rs->max_run[1];
3c39a379
JA
926
927 if (rs->max_run[0])
4cf1abc3 928 rs->agg[0] = (rs->io_kb[0] * 1000) / max_run[0];
3c39a379 929 if (rs->max_run[1])
4cf1abc3 930 rs->agg[1] = (rs->io_kb[1] * 1000) / max_run[1];
3c39a379
JA
931 }
932
933 /*
934 * don't overwrite last signal output
935 */
c6ae0a5b 936 if (!terse_output)
4ceb30d4 937 log_info("\n");
3c39a379 938
756867bd
JA
939 for (i = 0; i < nr_ts; i++) {
940 ts = &threadstats[i];
941 rs = &runstats[ts->groupid];
3c39a379 942
a64e88da
JA
943 if (is_backend)
944 fio_server_send_ts(ts, rs);
945 else if (terse_output)
756867bd 946 show_thread_status_terse(ts, rs);
c6ae0a5b 947 else
756867bd 948 show_thread_status(ts, rs);
3c39a379
JA
949 }
950
c6ae0a5b 951 if (!terse_output) {
a64e88da
JA
952 for (i = 0; i < groupid + 1; i++) {
953 rs = &runstats[i];
954
955 rs->groupid = i;
956 if (is_backend)
957 fio_server_send_gs(rs);
958 else
959 show_group_stats(rs);
960 }
3c39a379 961
d09a64a0
JA
962 if (is_backend)
963 fio_server_send_du();
964 else
965 show_disk_util();
966
967 free_disk_util();
c6ae0a5b 968 }
eecf272f
JA
969
970 free(runstats);
756867bd 971 free(threadstats);
3c39a379
JA
972}
973
68704084 974static inline void add_stat_sample(struct io_stat *is, unsigned long data)
3c39a379 975{
68704084 976 double val = data;
6660cc67 977 double delta;
68704084
JA
978
979 if (data > is->max_val)
980 is->max_val = data;
981 if (data < is->min_val)
982 is->min_val = data;
983
802ad4a8 984 delta = val - is->mean.u.f;
ef11d737 985 if (delta) {
802ad4a8
JA
986 is->mean.u.f += delta / (is->samples + 1.0);
987 is->S.u.f += delta * (val - is->mean.u.f);
ef11d737 988 }
3c39a379 989
3c39a379
JA
990 is->samples++;
991}
992
bb3884d8 993static void __add_log_sample(struct io_log *iolog, unsigned long val,
306ddc97 994 enum fio_ddir ddir, unsigned int bs,
2b13e716 995 unsigned long t)
3c39a379 996{
306ddc97
JA
997 const int nr_samples = iolog->nr_samples;
998
3c39a379
JA
999 if (iolog->nr_samples == iolog->max_samples) {
1000 int new_size = sizeof(struct io_sample) * iolog->max_samples*2;
1001
1002 iolog->log = realloc(iolog->log, new_size);
1003 iolog->max_samples <<= 1;
1004 }
1005
306ddc97 1006 iolog->log[nr_samples].val = val;
2b13e716 1007 iolog->log[nr_samples].time = t;
306ddc97
JA
1008 iolog->log[nr_samples].ddir = ddir;
1009 iolog->log[nr_samples].bs = bs;
3c39a379
JA
1010 iolog->nr_samples++;
1011}
1012
bb3884d8 1013static void add_log_sample(struct thread_data *td, struct io_log *iolog,
306ddc97
JA
1014 unsigned long val, enum fio_ddir ddir,
1015 unsigned int bs)
bb3884d8 1016{
ff58fced
JA
1017 if (!ddir_rw(ddir))
1018 return;
1019
306ddc97 1020 __add_log_sample(iolog, val, ddir, bs, mtime_since_now(&td->epoch));
bb3884d8
JA
1021}
1022
306ddc97 1023void add_agg_sample(unsigned long val, enum fio_ddir ddir, unsigned int bs)
bb3884d8 1024{
ff58fced 1025 struct io_log *iolog;
bb3884d8 1026
ff58fced
JA
1027 if (!ddir_rw(ddir))
1028 return;
1029
1030 iolog = agg_io_log[ddir];
306ddc97 1031 __add_log_sample(iolog, val, ddir, bs, mtime_since_genesis());
bb3884d8
JA
1032}
1033
83349190
YH
1034static void add_clat_percentile_sample(struct thread_stat *ts,
1035 unsigned long usec, enum fio_ddir ddir)
1036{
1037 unsigned int idx = plat_val_to_idx(usec);
1038 assert(idx < FIO_IO_U_PLAT_NR);
1039
1040 ts->io_u_plat[ddir][idx]++;
1041}
1042
1e97cce9 1043void add_clat_sample(struct thread_data *td, enum fio_ddir ddir,
306ddc97 1044 unsigned long usec, unsigned int bs)
3c39a379 1045{
756867bd 1046 struct thread_stat *ts = &td->ts;
079ad09b 1047
ff58fced
JA
1048 if (!ddir_rw(ddir))
1049 return;
1050
d85f5118 1051 add_stat_sample(&ts->clat_stat[ddir], usec);
3c39a379 1052
7b9f733a
JA
1053 if (td->clat_log)
1054 add_log_sample(td, td->clat_log, usec, ddir, bs);
83349190
YH
1055
1056 if (ts->clat_percentiles)
1057 add_clat_percentile_sample(ts, usec, ddir);
3c39a379
JA
1058}
1059
1e97cce9 1060void add_slat_sample(struct thread_data *td, enum fio_ddir ddir,
306ddc97 1061 unsigned long usec, unsigned int bs)
3c39a379 1062{
756867bd 1063 struct thread_stat *ts = &td->ts;
079ad09b 1064
ff58fced
JA
1065 if (!ddir_rw(ddir))
1066 return;
1067
d85f5118 1068 add_stat_sample(&ts->slat_stat[ddir], usec);
3c39a379 1069
7b9f733a
JA
1070 if (td->slat_log)
1071 add_log_sample(td, td->slat_log, usec, ddir, bs);
3c39a379
JA
1072}
1073
02af0988
JA
1074void add_lat_sample(struct thread_data *td, enum fio_ddir ddir,
1075 unsigned long usec, unsigned int bs)
1076{
1077 struct thread_stat *ts = &td->ts;
1078
ff58fced
JA
1079 if (!ddir_rw(ddir))
1080 return;
1081
02af0988
JA
1082 add_stat_sample(&ts->lat_stat[ddir], usec);
1083
7b9f733a
JA
1084 if (td->lat_log)
1085 add_log_sample(td, td->lat_log, usec, ddir, bs);
02af0988
JA
1086}
1087
306ddc97 1088void add_bw_sample(struct thread_data *td, enum fio_ddir ddir, unsigned int bs,
1e97cce9 1089 struct timeval *t)
3c39a379 1090{
756867bd 1091 struct thread_stat *ts = &td->ts;
ff58fced
JA
1092 unsigned long spent, rate;
1093
1094 if (!ddir_rw(ddir))
1095 return;
3c39a379 1096
c8eeb9df 1097 spent = mtime_since(&td->bw_sample_time, t);
2dc1bbeb 1098 if (spent < td->o.bw_avg_time)
3c39a379
JA
1099 return;
1100
f0505a14 1101 rate = (td->this_io_bytes[ddir] - td->stat_io_bytes[ddir]) *
0b9d69ec 1102 1000 / spent / 1024;
079ad09b 1103 add_stat_sample(&ts->bw_stat[ddir], rate);
3c39a379 1104
7b9f733a
JA
1105 if (td->bw_log)
1106 add_log_sample(td, td->bw_log, rate, ddir, bs);
3c39a379 1107
c8eeb9df 1108 fio_gettime(&td->bw_sample_time, NULL);
f0505a14 1109 td->stat_io_bytes[ddir] = td->this_io_bytes[ddir];
3c39a379 1110}
c8eeb9df
JA
1111
1112void add_iops_sample(struct thread_data *td, enum fio_ddir ddir,
1113 struct timeval *t)
1114{
1115 struct thread_stat *ts = &td->ts;
1116 unsigned long spent, iops;
1117
1118 if (!ddir_rw(ddir))
1119 return;
1120
1121 spent = mtime_since(&td->iops_sample_time, t);
1122 if (spent < td->o.iops_avg_time)
1123 return;
1124
1125 iops = ((td->this_io_blocks[ddir] - td->stat_io_blocks[ddir]) * 1000) / spent;
1126
1127 add_stat_sample(&ts->iops_stat[ddir], iops);
1128
1129 if (td->iops_log) {
1130 assert(iops);
1131 add_log_sample(td, td->iops_log, iops, ddir, 0);
1132 }
1133
1134 fio_gettime(&td->iops_sample_time, NULL);
1135 td->stat_io_blocks[ddir] = td->this_io_blocks[ddir];
1136}