Break double loop on end-of-clat percentiles
[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;
561 unsigned long long bw;
562 double mean, dev;
563
ff58fced
JA
564 assert(ddir_rw(ddir));
565
c6ae0a5b 566 bw = 0;
756867bd
JA
567 if (ts->runtime[ddir])
568 bw = ts->io_bytes[ddir] / ts->runtime[ddir];
c6ae0a5b 569
cda99fa0 570 log_info(";%llu;%llu;%llu", ts->io_bytes[ddir] >> 10, bw,
5ec10eaa 571 ts->runtime[ddir]);
c6ae0a5b 572
079ad09b 573 if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev))
6d86144d 574 log_info(";%lu;%lu;%f;%f", min, max, mean, dev);
c6ae0a5b 575 else
6d86144d 576 log_info(";%lu;%lu;%f;%f", 0UL, 0UL, 0.0, 0.0);
c6ae0a5b 577
079ad09b 578 if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev))
6d86144d 579 log_info(";%lu;%lu;%f;%f", min, max, mean, dev);
c6ae0a5b 580 else
6d86144d 581 log_info(";%lu;%lu;%f;%f", 0UL, 0UL, 0.0, 0.0);
c6ae0a5b 582
02af0988
JA
583 if (calc_lat(&ts->lat_stat[ddir], &min, &max, &mean, &dev))
584 log_info(";%lu;%lu;%f;%f", min, max, mean, dev);
585 else
586 log_info(";%lu;%lu;%f;%f", 0UL, 0UL, 0.0, 0.0);
587
079ad09b 588 if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
c6ae0a5b
JA
589 double p_of_agg;
590
591 p_of_agg = mean * 100 / (double) rs->agg[ddir];
6d86144d 592 log_info(";%lu;%lu;%f%%;%f;%f", min, max, p_of_agg, mean, dev);
c6ae0a5b 593 } else
6d86144d 594 log_info(";%lu;%lu;%f%%;%f;%f", 0UL, 0UL, 0.0, 0.0, 0.0);
c6ae0a5b
JA
595}
596
525c2bfa 597#define FIO_TERSE_VERSION "2"
c6ae0a5b 598
756867bd 599static void show_thread_status_terse(struct thread_stat *ts,
c6ae0a5b
JA
600 struct group_run_stats *rs)
601{
2270890c 602 double io_u_dist[FIO_IO_U_MAP_NR];
04a0feae
JA
603 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
604 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
c6ae0a5b 605 double usr_cpu, sys_cpu;
04a0feae 606 int i;
c6ae0a5b 607
562c2d2f 608 /* General Info */
525c2bfa
JA
609 log_info("%s;%s;%d;%d", FIO_TERSE_VERSION, ts->name, ts->groupid,
610 ts->error);
562c2d2f 611 /* Log Read Status */
756867bd 612 show_ddir_status_terse(ts, rs, 0);
562c2d2f 613 /* Log Write Status */
756867bd 614 show_ddir_status_terse(ts, rs, 1);
c6ae0a5b 615
562c2d2f 616 /* CPU Usage */
756867bd
JA
617 if (ts->total_run_time) {
618 double runt = (double) ts->total_run_time;
c6ae0a5b 619
756867bd
JA
620 usr_cpu = (double) ts->usr_time * 100 / runt;
621 sys_cpu = (double) ts->sys_time * 100 / runt;
c6ae0a5b
JA
622 } else {
623 usr_cpu = 0;
624 sys_cpu = 0;
625 }
626
5ec10eaa
JA
627 log_info(";%f%%;%f%%;%lu;%lu;%lu", usr_cpu, sys_cpu, ts->ctx, ts->majf,
628 ts->minf);
2270890c 629
562c2d2f 630 /* Calc % distribution of IO depths, usecond, msecond latency */
838bc709 631 stat_calc_dist(ts->io_u_map, ts_total_io_u(ts), io_u_dist);
04a0feae
JA
632 stat_calc_lat_u(ts, io_u_lat_u);
633 stat_calc_lat_m(ts, io_u_lat_m);
2270890c 634
562c2d2f 635 /* Only show fixed 7 I/O depth levels*/
5ec10eaa
JA
636 log_info(";%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%",
637 io_u_dist[0], io_u_dist[1], io_u_dist[2], io_u_dist[3],
638 io_u_dist[4], io_u_dist[5], io_u_dist[6]);
2270890c 639
562c2d2f 640 /* Microsecond latency */
04a0feae
JA
641 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
642 log_info(";%3.2f%%", io_u_lat_u[i]);
562c2d2f 643 /* Millisecond latency */
04a0feae
JA
644 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
645 log_info(";%3.2f%%", io_u_lat_m[i]);
562c2d2f 646 /* Additional output if continue_on_error set - default off*/
f2bba182
RR
647 if (ts->continue_on_error)
648 log_info(";%lu;%d", ts->total_err_count, ts->first_error);
04a0feae 649 log_info("\n");
2270890c 650
562c2d2f 651 /* Additional output if description is set */
2270890c 652 if (ts->description)
6d86144d 653 log_info(";%s", ts->description);
2270890c 654
6d86144d 655 log_info("\n");
756867bd
JA
656}
657
197574e4 658static void sum_stat(struct io_stat *dst, struct io_stat *src, int nr)
756867bd
JA
659{
660 double mean, S;
661
e09231c2
ZL
662 if (src->samples == 0)
663 return;
664
756867bd
JA
665 dst->min_val = min(dst->min_val, src->min_val);
666 dst->max_val = max(dst->max_val, src->max_val);
756867bd
JA
667
668 /*
cdcac5cf
YH
669 * Compute new mean and S after the merge
670 * <http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
671 * #Parallel_algorithm>
756867bd
JA
672 */
673 if (nr == 1) {
802ad4a8
JA
674 mean = src->mean.u.f;
675 S = src->S.u.f;
756867bd 676 } else {
802ad4a8 677 double delta = src->mean.u.f - dst->mean.u.f;
cdcac5cf 678
802ad4a8
JA
679 mean = ((src->mean.u.f * src->samples) +
680 (dst->mean.u.f * dst->samples)) /
cdcac5cf
YH
681 (dst->samples + src->samples);
682
802ad4a8 683 S = src->S.u.f + dst->S.u.f + pow(delta, 2.0) *
cdcac5cf
YH
684 (dst->samples * src->samples) /
685 (dst->samples + src->samples);
756867bd
JA
686 }
687
cdcac5cf 688 dst->samples += src->samples;
802ad4a8
JA
689 dst->mean.u.f = mean;
690 dst->S.u.f = S;
756867bd
JA
691}
692
37f0c1ae
JA
693void sum_group_stats(struct group_run_stats *dst, struct group_run_stats *src)
694{
695 int i;
696
697 for (i = 0; i < 2; i++) {
698 if (dst->max_run[i] < src->max_run[i])
699 dst->max_run[i] = src->max_run[i];
700 if (dst->min_run[i] && dst->min_run[i] > src->min_run[i])
701 dst->min_run[i] = src->min_run[i];
702 if (dst->max_bw[i] < src->max_bw[i])
703 dst->max_bw[i] = src->max_bw[i];
704 if (dst->min_bw[i] && dst->min_bw[i] > src->min_bw[i])
705 dst->min_bw[i] = src->min_bw[i];
706
707 dst->io_kb[i] += src->io_kb[i];
708 dst->agg[i] += src->agg[i];
709 }
710
711}
712
5b9babb7
JA
713void sum_thread_stats(struct thread_stat *dst, struct thread_stat *src, int nr)
714{
715 int l, k;
716
717 for (l = 0; l <= DDIR_WRITE; l++) {
718 sum_stat(&dst->clat_stat[l], &src->clat_stat[l], nr);
719 sum_stat(&dst->slat_stat[l], &src->slat_stat[l], nr);
720 sum_stat(&dst->lat_stat[l], &src->lat_stat[l], nr);
721 sum_stat(&dst->bw_stat[l], &src->bw_stat[l], nr);
722
723 dst->io_bytes[l] += src->io_bytes[l];
724
725 if (dst->runtime[l] < src->runtime[l])
726 dst->runtime[l] = src->runtime[l];
727 }
728
729 dst->usr_time += src->usr_time;
730 dst->sys_time += src->sys_time;
731 dst->ctx += src->ctx;
732 dst->majf += src->majf;
733 dst->minf += src->minf;
734
735 for (k = 0; k < FIO_IO_U_MAP_NR; k++)
736 dst->io_u_map[k] += src->io_u_map[k];
737 for (k = 0; k < FIO_IO_U_MAP_NR; k++)
738 dst->io_u_submit[k] += src->io_u_submit[k];
739 for (k = 0; k < FIO_IO_U_MAP_NR; k++)
740 dst->io_u_complete[k] += src->io_u_complete[k];
741 for (k = 0; k < FIO_IO_U_LAT_U_NR; k++)
742 dst->io_u_lat_u[k] += src->io_u_lat_u[k];
743 for (k = 0; k < FIO_IO_U_LAT_M_NR; k++)
744 dst->io_u_lat_m[k] += src->io_u_lat_m[k];
745
746 for (k = 0; k <= 2; k++) {
747 dst->total_io_u[k] += src->total_io_u[k];
748 dst->short_io_u[k] += src->short_io_u[k];
749 }
750
751 for (k = 0; k <= DDIR_WRITE; k++) {
752 int m;
753 for (m = 0; m < FIO_IO_U_PLAT_NR; m++)
754 dst->io_u_plat[k][m] += src->io_u_plat[k][m];
755 }
756
757 dst->total_run_time += src->total_run_time;
758 dst->total_submit += src->total_submit;
759 dst->total_complete += src->total_complete;
760}
761
37f0c1ae
JA
762void init_group_run_stat(struct group_run_stats *gs)
763{
764 memset(gs, 0, sizeof(*gs));
765 gs->min_bw[0] = gs->min_run[0] = ~0UL;
766 gs->min_bw[1] = gs->min_run[1] = ~0UL;
767}
768
769void init_thread_stat(struct thread_stat *ts)
770{
771 int j;
772
773 memset(ts, 0, sizeof(*ts));
774
775 for (j = 0; j <= DDIR_WRITE; j++) {
776 ts->lat_stat[j].min_val = -1UL;
777 ts->clat_stat[j].min_val = -1UL;
778 ts->slat_stat[j].min_val = -1UL;
779 ts->bw_stat[j].min_val = -1UL;
780 }
781 ts->groupid = -1;
782}
783
3c39a379
JA
784void show_run_stats(void)
785{
786 struct group_run_stats *runstats, *rs;
787 struct thread_data *td;
756867bd 788 struct thread_stat *threadstats, *ts;
5b9babb7 789 int i, j, nr_ts, last_ts, idx;
90fef2d1 790 int kb_base_warned = 0;
3c39a379
JA
791
792 runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1));
793
37f0c1ae
JA
794 for (i = 0; i < groupid + 1; i++)
795 init_group_run_stat(&runstats[i]);
3c39a379 796
756867bd
JA
797 /*
798 * find out how many threads stats we need. if group reporting isn't
799 * enabled, it's one-per-td.
800 */
801 nr_ts = 0;
802 last_ts = -1;
803 for_each_td(td, i) {
2dc1bbeb 804 if (!td->o.group_reporting) {
756867bd
JA
805 nr_ts++;
806 continue;
807 }
808 if (last_ts == td->groupid)
809 continue;
810
811 last_ts = td->groupid;
812 nr_ts++;
813 }
814
815 threadstats = malloc(nr_ts * sizeof(struct thread_stat));
816
37f0c1ae
JA
817 for (i = 0; i < nr_ts; i++)
818 init_thread_stat(&threadstats[i]);
756867bd
JA
819
820 j = 0;
821 last_ts = -1;
197574e4 822 idx = 0;
34572e28 823 for_each_td(td, i) {
2dc1bbeb
JA
824 if (idx && (!td->o.group_reporting ||
825 (td->o.group_reporting && last_ts != td->groupid))) {
7abd0e3a
JA
826 idx = 0;
827 j++;
828 }
829
830 last_ts = td->groupid;
831
756867bd
JA
832 ts = &threadstats[j];
833
83349190
YH
834 ts->clat_percentiles = td->o.clat_percentiles;
835 if (td->o.overwrite_plist)
802ad4a8 836 memcpy(ts->percentile_list, td->o.percentile_list, sizeof(td->o.percentile_list));
83349190 837 else
802ad4a8 838 memcpy(ts->percentile_list, def_percentile_list, sizeof(def_percentile_list));
83349190 839
197574e4 840 idx++;
6586ee89 841 ts->members++;
756867bd 842
7abd0e3a 843 if (ts->groupid == -1) {
2dc84ba7
JA
844 /*
845 * These are per-group shared already
846 */
f6bb5b88 847 strncpy(ts->name, td->o.name, FIO_JOBNAME_SIZE);
a64e88da
JA
848 if (td->o.description)
849 strncpy(ts->description, td->o.description,
850 FIO_JOBNAME_SIZE);
851 else
852 memset(ts->description, 0, FIO_JOBNAME_SIZE);
853
756867bd 854 ts->groupid = td->groupid;
2dc84ba7
JA
855
856 /*
857 * first pid in group, not very useful...
858 */
756867bd 859 ts->pid = td->pid;
90fef2d1
JA
860
861 ts->kb_base = td->o.kb_base;
862 } else if (ts->kb_base != td->o.kb_base && !kb_base_warned) {
863 log_info("fio: kb_base differs for jobs in group, using"
864 " %u as the base\n", ts->kb_base);
865 kb_base_warned = 1;
2dc84ba7
JA
866 }
867
f2bba182
RR
868 ts->continue_on_error = td->o.continue_on_error;
869 ts->total_err_count += td->total_err_count;
870 ts->first_error = td->first_error;
871 if (!ts->error) {
872 if (!td->error && td->o.continue_on_error &&
873 td->first_error) {
874 ts->error = td->first_error;
f6bb5b88 875 strcpy(ts->verror, td->verror);
f2bba182
RR
876 } else if (td->error) {
877 ts->error = td->error;
f6bb5b88 878 strcpy(ts->verror, td->verror);
f2bba182 879 }
756867bd
JA
880 }
881
5b9babb7 882 sum_thread_stats(ts, &td->ts, idx);
756867bd
JA
883 }
884
885 for (i = 0; i < nr_ts; i++) {
94370ac4 886 unsigned long long bw;
3c39a379 887
756867bd
JA
888 ts = &threadstats[i];
889 rs = &runstats[ts->groupid];
90fef2d1 890 rs->kb_base = ts->kb_base;
3c39a379 891
de64df05 892 for (j = 0; j <= DDIR_WRITE; j++) {
94370ac4
JA
893 if (!ts->runtime[j])
894 continue;
895 if (ts->runtime[j] < rs->min_run[j] || !rs->min_run[j])
896 rs->min_run[j] = ts->runtime[j];
897 if (ts->runtime[j] > rs->max_run[j])
898 rs->max_run[j] = ts->runtime[j];
899
900 bw = 0;
8879fd15
JA
901 if (ts->runtime[j]) {
902 unsigned long runt;
903
90fef2d1 904 runt = ts->runtime[j];
8879fd15
JA
905 bw = ts->io_bytes[j] / runt;
906 }
94370ac4
JA
907 if (bw < rs->min_bw[j])
908 rs->min_bw[j] = bw;
909 if (bw > rs->max_bw[j])
910 rs->max_bw[j] = bw;
911
90fef2d1 912 rs->io_kb[j] += ts->io_bytes[j] / rs->kb_base;
94370ac4 913 }
3c39a379
JA
914 }
915
916 for (i = 0; i < groupid + 1; i++) {
8879fd15
JA
917 unsigned long max_run[2];
918
3c39a379 919 rs = &runstats[i];
90fef2d1
JA
920 max_run[0] = rs->max_run[0];
921 max_run[1] = rs->max_run[1];
3c39a379
JA
922
923 if (rs->max_run[0])
4cf1abc3 924 rs->agg[0] = (rs->io_kb[0] * 1000) / max_run[0];
3c39a379 925 if (rs->max_run[1])
4cf1abc3 926 rs->agg[1] = (rs->io_kb[1] * 1000) / max_run[1];
3c39a379
JA
927 }
928
929 /*
930 * don't overwrite last signal output
931 */
c6ae0a5b 932 if (!terse_output)
4ceb30d4 933 log_info("\n");
3c39a379 934
756867bd
JA
935 for (i = 0; i < nr_ts; i++) {
936 ts = &threadstats[i];
937 rs = &runstats[ts->groupid];
3c39a379 938
a64e88da
JA
939 if (is_backend)
940 fio_server_send_ts(ts, rs);
941 else if (terse_output)
756867bd 942 show_thread_status_terse(ts, rs);
c6ae0a5b 943 else
756867bd 944 show_thread_status(ts, rs);
3c39a379
JA
945 }
946
c6ae0a5b 947 if (!terse_output) {
a64e88da
JA
948 for (i = 0; i < groupid + 1; i++) {
949 rs = &runstats[i];
950
951 rs->groupid = i;
952 if (is_backend)
953 fio_server_send_gs(rs);
954 else
955 show_group_stats(rs);
956 }
3c39a379 957
d09a64a0
JA
958 if (is_backend)
959 fio_server_send_du();
960 else
961 show_disk_util();
962
963 free_disk_util();
c6ae0a5b 964 }
eecf272f
JA
965
966 free(runstats);
756867bd 967 free(threadstats);
3c39a379
JA
968}
969
68704084 970static inline void add_stat_sample(struct io_stat *is, unsigned long data)
3c39a379 971{
68704084 972 double val = data;
6660cc67 973 double delta;
68704084
JA
974
975 if (data > is->max_val)
976 is->max_val = data;
977 if (data < is->min_val)
978 is->min_val = data;
979
802ad4a8 980 delta = val - is->mean.u.f;
ef11d737 981 if (delta) {
802ad4a8
JA
982 is->mean.u.f += delta / (is->samples + 1.0);
983 is->S.u.f += delta * (val - is->mean.u.f);
ef11d737 984 }
3c39a379 985
3c39a379
JA
986 is->samples++;
987}
988
bb3884d8 989static void __add_log_sample(struct io_log *iolog, unsigned long val,
306ddc97 990 enum fio_ddir ddir, unsigned int bs,
2b13e716 991 unsigned long t)
3c39a379 992{
306ddc97
JA
993 const int nr_samples = iolog->nr_samples;
994
3c39a379
JA
995 if (iolog->nr_samples == iolog->max_samples) {
996 int new_size = sizeof(struct io_sample) * iolog->max_samples*2;
997
998 iolog->log = realloc(iolog->log, new_size);
999 iolog->max_samples <<= 1;
1000 }
1001
306ddc97 1002 iolog->log[nr_samples].val = val;
2b13e716 1003 iolog->log[nr_samples].time = t;
306ddc97
JA
1004 iolog->log[nr_samples].ddir = ddir;
1005 iolog->log[nr_samples].bs = bs;
3c39a379
JA
1006 iolog->nr_samples++;
1007}
1008
bb3884d8 1009static void add_log_sample(struct thread_data *td, struct io_log *iolog,
306ddc97
JA
1010 unsigned long val, enum fio_ddir ddir,
1011 unsigned int bs)
bb3884d8 1012{
ff58fced
JA
1013 if (!ddir_rw(ddir))
1014 return;
1015
306ddc97 1016 __add_log_sample(iolog, val, ddir, bs, mtime_since_now(&td->epoch));
bb3884d8
JA
1017}
1018
306ddc97 1019void add_agg_sample(unsigned long val, enum fio_ddir ddir, unsigned int bs)
bb3884d8 1020{
ff58fced 1021 struct io_log *iolog;
bb3884d8 1022
ff58fced
JA
1023 if (!ddir_rw(ddir))
1024 return;
1025
1026 iolog = agg_io_log[ddir];
306ddc97 1027 __add_log_sample(iolog, val, ddir, bs, mtime_since_genesis());
bb3884d8
JA
1028}
1029
83349190
YH
1030static void add_clat_percentile_sample(struct thread_stat *ts,
1031 unsigned long usec, enum fio_ddir ddir)
1032{
1033 unsigned int idx = plat_val_to_idx(usec);
1034 assert(idx < FIO_IO_U_PLAT_NR);
1035
1036 ts->io_u_plat[ddir][idx]++;
1037}
1038
1e97cce9 1039void add_clat_sample(struct thread_data *td, enum fio_ddir ddir,
306ddc97 1040 unsigned long usec, unsigned int bs)
3c39a379 1041{
756867bd 1042 struct thread_stat *ts = &td->ts;
079ad09b 1043
ff58fced
JA
1044 if (!ddir_rw(ddir))
1045 return;
1046
d85f5118 1047 add_stat_sample(&ts->clat_stat[ddir], usec);
3c39a379 1048
7b9f733a
JA
1049 if (td->clat_log)
1050 add_log_sample(td, td->clat_log, usec, ddir, bs);
83349190
YH
1051
1052 if (ts->clat_percentiles)
1053 add_clat_percentile_sample(ts, usec, ddir);
3c39a379
JA
1054}
1055
1e97cce9 1056void add_slat_sample(struct thread_data *td, enum fio_ddir ddir,
306ddc97 1057 unsigned long usec, unsigned int bs)
3c39a379 1058{
756867bd 1059 struct thread_stat *ts = &td->ts;
079ad09b 1060
ff58fced
JA
1061 if (!ddir_rw(ddir))
1062 return;
1063
d85f5118 1064 add_stat_sample(&ts->slat_stat[ddir], usec);
3c39a379 1065
7b9f733a
JA
1066 if (td->slat_log)
1067 add_log_sample(td, td->slat_log, usec, ddir, bs);
3c39a379
JA
1068}
1069
02af0988
JA
1070void add_lat_sample(struct thread_data *td, enum fio_ddir ddir,
1071 unsigned long usec, unsigned int bs)
1072{
1073 struct thread_stat *ts = &td->ts;
1074
ff58fced
JA
1075 if (!ddir_rw(ddir))
1076 return;
1077
02af0988
JA
1078 add_stat_sample(&ts->lat_stat[ddir], usec);
1079
7b9f733a
JA
1080 if (td->lat_log)
1081 add_log_sample(td, td->lat_log, usec, ddir, bs);
02af0988
JA
1082}
1083
306ddc97 1084void add_bw_sample(struct thread_data *td, enum fio_ddir ddir, unsigned int bs,
1e97cce9 1085 struct timeval *t)
3c39a379 1086{
756867bd 1087 struct thread_stat *ts = &td->ts;
ff58fced
JA
1088 unsigned long spent, rate;
1089
1090 if (!ddir_rw(ddir))
1091 return;
3c39a379 1092
c8eeb9df 1093 spent = mtime_since(&td->bw_sample_time, t);
2dc1bbeb 1094 if (spent < td->o.bw_avg_time)
3c39a379
JA
1095 return;
1096
f0505a14 1097 rate = (td->this_io_bytes[ddir] - td->stat_io_bytes[ddir]) *
0b9d69ec 1098 1000 / spent / 1024;
079ad09b 1099 add_stat_sample(&ts->bw_stat[ddir], rate);
3c39a379 1100
7b9f733a
JA
1101 if (td->bw_log)
1102 add_log_sample(td, td->bw_log, rate, ddir, bs);
3c39a379 1103
c8eeb9df 1104 fio_gettime(&td->bw_sample_time, NULL);
f0505a14 1105 td->stat_io_bytes[ddir] = td->this_io_bytes[ddir];
3c39a379 1106}
c8eeb9df
JA
1107
1108void add_iops_sample(struct thread_data *td, enum fio_ddir ddir,
1109 struct timeval *t)
1110{
1111 struct thread_stat *ts = &td->ts;
1112 unsigned long spent, iops;
1113
1114 if (!ddir_rw(ddir))
1115 return;
1116
1117 spent = mtime_since(&td->iops_sample_time, t);
1118 if (spent < td->o.iops_avg_time)
1119 return;
1120
1121 iops = ((td->this_io_blocks[ddir] - td->stat_io_blocks[ddir]) * 1000) / spent;
1122
1123 add_stat_sample(&ts->iops_stat[ddir], iops);
1124
1125 if (td->iops_log) {
1126 assert(iops);
1127 add_log_sample(td, td->iops_log, iops, ddir, 0);
1128 }
1129
1130 fio_gettime(&td->iops_sample_time, NULL);
1131 td->stat_io_blocks[ddir] = td->this_io_blocks[ddir];
1132}