Don't output version for terse output
[fio.git] / stat.c
... / ...
CommitLineData
1#include <stdio.h>
2#include <string.h>
3#include <sys/time.h>
4#include <sys/types.h>
5#include <sys/stat.h>
6#include <dirent.h>
7#include <libgen.h>
8#include <math.h>
9
10#include "fio.h"
11#include "diskutil.h"
12#include "ieee754.h"
13
14void update_rusage_stat(struct thread_data *td)
15{
16 struct thread_stat *ts = &td->ts;
17
18 getrusage(RUSAGE_SELF, &td->ru_end);
19
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;
28
29 memcpy(&td->ru_start, &td->ru_end, sizeof(td->ru_end));
30}
31
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
51 /*
52 * MSB <= (FIO_IO_U_PLAT_BITS-1), cannot be rounded off. Use
53 * all bits of the sample as index
54 */
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
64 /*
65 * Discard the error bits and apply the mask to find the
66 * index for the buckets in the group
67 */
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{
105 const fio_fp64_t fa = *(const fio_fp64_t *) a;
106 const fio_fp64_t fb = *(const fio_fp64_t *) b;
107 int cmp = 0;
108
109 if (fa.u.f > fb.u.f)
110 cmp = 1;
111 else if (fa.u.f < fb.u.f)
112 cmp = -1;
113
114 return cmp;
115}
116
117/*
118 * Find and display the p-th percentile of clat
119 */
120static void show_clat_percentiles(unsigned int *io_u_plat, unsigned long nr,
121 fio_fp64_t *plist)
122{
123 unsigned long sum = 0;
124 unsigned int len, i, j = 0, minv = -1U, maxv = 0;
125 unsigned int *ovals = NULL, oval_len = 0;
126 int is_last, scale_down;
127
128 len = 0;
129 while (len < FIO_IO_U_LIST_MAX_LEN && plist[len].u.f != 0.0)
130 len++;
131
132 if (!len)
133 return;
134
135 /*
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.
139 */
140 if (len > 1)
141 qsort((void*)plist, len, sizeof(plist[0]), double_cmp);
142
143 /*
144 * Calculate bucket values, note down max and min values
145 */
146 is_last = 0;
147 for (i = 0; i < FIO_IO_U_PLAT_NR && !is_last; i++) {
148 sum += io_u_plat[i];
149 while (sum >= (plist[j].u.f / 100.0 * nr)) {
150 assert(plist[j].u.f <= 100.0);
151
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];
162
163 is_last = (j == len - 1);
164 if (is_last)
165 break;
166
167 j++;
168 }
169 }
170
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 }
182
183 for (j = 0; j < len; j++) {
184 char fbuf[8];
185
186 /* for formatting */
187 if (j != 0 && (j % 4) == 0)
188 log_info(" |");
189
190 /* end of the list */
191 is_last = (j == len - 1);
192
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");
208 }
209
210 if (ovals)
211 free(ovals);
212}
213
214static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max,
215 double *mean, double *dev)
216{
217 double n = is->samples;
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;
226 *mean = is->mean.u.f;
227
228 if (n > 1.0)
229 *dev = sqrt(is->S.u.f / (n - 1.0));
230 else
231 *dev = 0;
232
233 return 1;
234}
235
236void show_group_stats(struct group_run_stats *rs)
237{
238 char *p1, *p2, *p3, *p4;
239 const char *ddir_str[] = { " READ", " WRITE" };
240 int i;
241
242 log_info("Run status group %d (all jobs):\n", rs->groupid);
243
244 for (i = 0; i <= DDIR_WRITE; i++) {
245 const int i2p = is_power_of_2(rs->kb_base);
246
247 if (!rs->max_run[i])
248 continue;
249
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);
254
255 log_info("%s: io=%sB, aggrb=%sB/s, minb=%sB/s, maxb=%sB/s,"
256 " mint=%llumsec, maxt=%llumsec\n", ddir_str[i], p1, p2,
257 p3, p4, rs->min_run[i],
258 rs->max_run[i]);
259
260 free(p1);
261 free(p2);
262 free(p3);
263 free(p4);
264 }
265}
266
267#define ts_total_io_u(ts) \
268 ((ts)->total_io_u[0] + (ts)->total_io_u[1])
269
270static void stat_calc_dist(unsigned int *map, unsigned long total,
271 double *io_u_dist)
272{
273 int i;
274
275 /*
276 * Do depth distribution calculations
277 */
278 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
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;
286 }
287}
288
289static void stat_calc_lat(struct thread_stat *ts, double *dst,
290 unsigned int *src, int nr)
291{
292 unsigned long total = ts_total_io_u(ts);
293 int i;
294
295 /*
296 * Do latency distribution calculations
297 */
298 for (i = 0; i < nr; i++) {
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;
306 }
307}
308
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
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
333static void show_ddir_status(struct group_run_stats *rs, struct thread_stat *ts,
334 int ddir)
335{
336 const char *ddir_str[] = { "read ", "write" };
337 unsigned long min, max, runt;
338 unsigned long long bw, iops;
339 double mean, dev;
340 char *io_p, *bw_p, *iops_p;
341 int i2p;
342
343 assert(ddir_rw(ddir));
344
345 if (!ts->runtime[ddir])
346 return;
347
348 i2p = is_power_of_2(rs->kb_base);
349 runt = ts->runtime[ddir];
350
351 bw = (1000 * ts->io_bytes[ddir]) / runt;
352 io_p = num2str(ts->io_bytes[ddir], 6, 1, i2p);
353 bw_p = num2str(bw, 6, 1, i2p);
354
355 iops = (1000 * (uint64_t)ts->total_io_u[ddir]) / runt;
356 iops_p = num2str(iops, 6, 1, 0);
357
358 log_info(" %s: io=%sB, bw=%sB/s, iops=%s, runt=%6llumsec\n",
359 ddir_str[ddir], io_p, bw_p, iops_p,
360 ts->runtime[ddir]);
361
362 free(io_p);
363 free(bw_p);
364 free(iops_p);
365
366 if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev)) {
367 const char *base = "(usec)";
368 char *minp, *maxp;
369
370 if (!usec_to_msec(&min, &max, &mean, &dev))
371 base = "(msec)";
372
373 minp = num2str(min, 6, 1, 0);
374 maxp = num2str(max, 6, 1, 0);
375
376 log_info(" slat %s: min=%s, max=%s, avg=%5.02f,"
377 " stdev=%5.02f\n", base, minp, maxp, mean, dev);
378
379 free(minp);
380 free(maxp);
381 }
382 if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev)) {
383 const char *base = "(usec)";
384 char *minp, *maxp;
385
386 if (!usec_to_msec(&min, &max, &mean, &dev))
387 base = "(msec)";
388
389 minp = num2str(min, 6, 1, 0);
390 maxp = num2str(max, 6, 1, 0);
391
392 log_info(" clat %s: min=%s, max=%s, avg=%5.02f,"
393 " stdev=%5.02f\n", base, minp, maxp, mean, dev);
394
395 free(minp);
396 free(maxp);
397 }
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 }
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 }
419 if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
420 double p_of_agg;
421
422 p_of_agg = mean * 100 / (double) rs->agg[ddir];
423 log_info(" bw (KB/s) : min=%5lu, max=%5lu, per=%3.2f%%,"
424 " avg=%5.02f, stdev=%5.02f\n", min, max, p_of_agg,
425 mean, dev);
426 }
427}
428
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) {
438 if (line)
439 log_info("\n");
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 }
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);
473 log_info("\n");
474 show_lat_m(io_u_lat_m);
475 log_info("\n");
476}
477
478void show_thread_status(struct thread_stat *ts, struct group_run_stats *rs)
479{
480 double usr_cpu, sys_cpu;
481 unsigned long runtime;
482 double io_u_dist[FIO_IO_U_MAP_NR];
483 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
484 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
485
486 if (!(ts->io_bytes[0] + ts->io_bytes[1]) &&
487 !(ts->total_io_u[0] + ts->total_io_u[1]))
488 return;
489
490 if (!ts->error) {
491 log_info("%s: (groupid=%d, jobs=%d): err=%2d: pid=%d\n",
492 ts->name, ts->groupid, ts->members,
493 ts->error, (int) ts->pid);
494 } else {
495 log_info("%s: (groupid=%d, jobs=%d): err=%2d (%s): pid=%d\n",
496 ts->name, ts->groupid, ts->members,
497 ts->error, ts->verror, (int) ts->pid);
498 }
499
500 if (ts->description)
501 log_info(" Description : [%s]\n", ts->description);
502
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);
507
508 runtime = ts->total_run_time;
509 if (runtime) {
510 double runt = (double) runtime;
511
512 usr_cpu = (double) ts->usr_time * 100 / runt;
513 sys_cpu = (double) ts->sys_time * 100 / runt;
514 } else {
515 usr_cpu = 0;
516 sys_cpu = 0;
517 }
518
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);
521
522 stat_calc_dist(ts->io_u_map, ts_total_io_u(ts), io_u_dist);
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]);
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]);
541 log_info(" issued r/w/d: total=%lu/%lu/%lu, short=%lu/%lu/%lu\n",
542 ts->total_io_u[0], ts->total_io_u[1],
543 ts->total_io_u[2],
544 ts->short_io_u[0], ts->short_io_u[1],
545 ts->short_io_u[2]);
546 stat_calc_lat_u(ts, io_u_lat_u);
547 stat_calc_lat_m(ts, io_u_lat_m);
548 show_latencies(io_u_lat_u, io_u_lat_m);
549 if (ts->continue_on_error) {
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));
554 }
555}
556
557static void show_ddir_status_terse(struct thread_stat *ts,
558 struct group_run_stats *rs, int ddir)
559{
560 unsigned long min, max;
561 unsigned long long bw, iops;
562 double mean, dev;
563
564 assert(ddir_rw(ddir));
565
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 }
573
574 log_info(";%llu;%llu;%llu;%llu", ts->io_bytes[ddir] >> 10, bw, iops,
575 ts->runtime[ddir]);
576
577 if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev))
578 log_info(";%lu;%lu;%f;%f", min, max, mean, dev);
579 else
580 log_info(";%lu;%lu;%f;%f", 0UL, 0UL, 0.0, 0.0);
581
582 if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev))
583 log_info(";%lu;%lu;%f;%f", min, max, mean, dev);
584 else
585 log_info(";%lu;%lu;%f;%f", 0UL, 0UL, 0.0, 0.0);
586
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
592 if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
593 double p_of_agg;
594
595 p_of_agg = mean * 100 / (double) rs->agg[ddir];
596 log_info(";%lu;%lu;%f%%;%f;%f", min, max, p_of_agg, mean, dev);
597 } else
598 log_info(";%lu;%lu;%f%%;%f;%f", 0UL, 0UL, 0.0, 0.0, 0.0);
599}
600
601#define FIO_TERSE_VERSION "3"
602
603static void show_thread_status_terse(struct thread_stat *ts,
604 struct group_run_stats *rs)
605{
606 double io_u_dist[FIO_IO_U_MAP_NR];
607 double io_u_lat_u[FIO_IO_U_LAT_U_NR];
608 double io_u_lat_m[FIO_IO_U_LAT_M_NR];
609 double usr_cpu, sys_cpu;
610 int i;
611
612 /* General Info */
613 log_info("%s;%s;%d;%d", FIO_TERSE_VERSION, ts->name, ts->groupid,
614 ts->error);
615 /* Log Read Status */
616 show_ddir_status_terse(ts, rs, 0);
617 /* Log Write Status */
618 show_ddir_status_terse(ts, rs, 1);
619
620 /* CPU Usage */
621 if (ts->total_run_time) {
622 double runt = (double) ts->total_run_time;
623
624 usr_cpu = (double) ts->usr_time * 100 / runt;
625 sys_cpu = (double) ts->sys_time * 100 / runt;
626 } else {
627 usr_cpu = 0;
628 sys_cpu = 0;
629 }
630
631 log_info(";%f%%;%f%%;%lu;%lu;%lu", usr_cpu, sys_cpu, ts->ctx, ts->majf,
632 ts->minf);
633
634 /* Calc % distribution of IO depths, usecond, msecond latency */
635 stat_calc_dist(ts->io_u_map, ts_total_io_u(ts), io_u_dist);
636 stat_calc_lat_u(ts, io_u_lat_u);
637 stat_calc_lat_m(ts, io_u_lat_m);
638
639 /* Only show fixed 7 I/O depth levels*/
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]);
643
644 /* Microsecond latency */
645 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
646 log_info(";%3.2f%%", io_u_lat_u[i]);
647 /* Millisecond latency */
648 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
649 log_info(";%3.2f%%", io_u_lat_m[i]);
650 /* Additional output if continue_on_error set - default off*/
651 if (ts->continue_on_error)
652 log_info(";%lu;%d", ts->total_err_count, ts->first_error);
653 log_info("\n");
654
655 /* Additional output if description is set */
656 if (ts->description)
657 log_info(";%s", ts->description);
658
659 log_info("\n");
660}
661
662static void sum_stat(struct io_stat *dst, struct io_stat *src, int nr)
663{
664 double mean, S;
665
666 if (src->samples == 0)
667 return;
668
669 dst->min_val = min(dst->min_val, src->min_val);
670 dst->max_val = max(dst->max_val, src->max_val);
671
672 /*
673 * Compute new mean and S after the merge
674 * <http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
675 * #Parallel_algorithm>
676 */
677 if (nr == 1) {
678 mean = src->mean.u.f;
679 S = src->S.u.f;
680 } else {
681 double delta = src->mean.u.f - dst->mean.u.f;
682
683 mean = ((src->mean.u.f * src->samples) +
684 (dst->mean.u.f * dst->samples)) /
685 (dst->samples + src->samples);
686
687 S = src->S.u.f + dst->S.u.f + pow(delta, 2.0) *
688 (dst->samples * src->samples) /
689 (dst->samples + src->samples);
690 }
691
692 dst->samples += src->samples;
693 dst->mean.u.f = mean;
694 dst->S.u.f = S;
695}
696
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
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
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
788void show_run_stats(void)
789{
790 struct group_run_stats *runstats, *rs;
791 struct thread_data *td;
792 struct thread_stat *threadstats, *ts;
793 int i, j, nr_ts, last_ts, idx;
794 int kb_base_warned = 0;
795
796 runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1));
797
798 for (i = 0; i < groupid + 1; i++)
799 init_group_run_stat(&runstats[i]);
800
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) {
808 if (!td->o.group_reporting) {
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
821 for (i = 0; i < nr_ts; i++)
822 init_thread_stat(&threadstats[i]);
823
824 j = 0;
825 last_ts = -1;
826 idx = 0;
827 for_each_td(td, i) {
828 if (idx && (!td->o.group_reporting ||
829 (td->o.group_reporting && last_ts != td->groupid))) {
830 idx = 0;
831 j++;
832 }
833
834 last_ts = td->groupid;
835
836 ts = &threadstats[j];
837
838 ts->clat_percentiles = td->o.clat_percentiles;
839 if (td->o.overwrite_plist)
840 memcpy(ts->percentile_list, td->o.percentile_list, sizeof(td->o.percentile_list));
841 else
842 memcpy(ts->percentile_list, def_percentile_list, sizeof(def_percentile_list));
843
844 idx++;
845 ts->members++;
846
847 if (ts->groupid == -1) {
848 /*
849 * These are per-group shared already
850 */
851 strncpy(ts->name, td->o.name, FIO_JOBNAME_SIZE);
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
858 ts->groupid = td->groupid;
859
860 /*
861 * first pid in group, not very useful...
862 */
863 ts->pid = td->pid;
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;
870 }
871
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;
879 strcpy(ts->verror, td->verror);
880 } else if (td->error) {
881 ts->error = td->error;
882 strcpy(ts->verror, td->verror);
883 }
884 }
885
886 sum_thread_stats(ts, &td->ts, idx);
887 }
888
889 for (i = 0; i < nr_ts; i++) {
890 unsigned long long bw;
891
892 ts = &threadstats[i];
893 rs = &runstats[ts->groupid];
894 rs->kb_base = ts->kb_base;
895
896 for (j = 0; j <= DDIR_WRITE; j++) {
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;
905 if (ts->runtime[j]) {
906 unsigned long runt;
907
908 runt = ts->runtime[j];
909 bw = ts->io_bytes[j] / runt;
910 }
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
916 rs->io_kb[j] += ts->io_bytes[j] / rs->kb_base;
917 }
918 }
919
920 for (i = 0; i < groupid + 1; i++) {
921 unsigned long max_run[2];
922
923 rs = &runstats[i];
924 max_run[0] = rs->max_run[0];
925 max_run[1] = rs->max_run[1];
926
927 if (rs->max_run[0])
928 rs->agg[0] = (rs->io_kb[0] * 1000) / max_run[0];
929 if (rs->max_run[1])
930 rs->agg[1] = (rs->io_kb[1] * 1000) / max_run[1];
931 }
932
933 /*
934 * don't overwrite last signal output
935 */
936 if (!terse_output)
937 log_info("\n");
938
939 for (i = 0; i < nr_ts; i++) {
940 ts = &threadstats[i];
941 rs = &runstats[ts->groupid];
942
943 if (is_backend)
944 fio_server_send_ts(ts, rs);
945 else if (terse_output)
946 show_thread_status_terse(ts, rs);
947 else
948 show_thread_status(ts, rs);
949 }
950
951 if (!terse_output) {
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 }
961
962 if (is_backend)
963 fio_server_send_du();
964 else
965 show_disk_util();
966
967 free_disk_util();
968 }
969
970 free(runstats);
971 free(threadstats);
972}
973
974static inline void add_stat_sample(struct io_stat *is, unsigned long data)
975{
976 double val = data;
977 double delta;
978
979 if (data > is->max_val)
980 is->max_val = data;
981 if (data < is->min_val)
982 is->min_val = data;
983
984 delta = val - is->mean.u.f;
985 if (delta) {
986 is->mean.u.f += delta / (is->samples + 1.0);
987 is->S.u.f += delta * (val - is->mean.u.f);
988 }
989
990 is->samples++;
991}
992
993static void __add_log_sample(struct io_log *iolog, unsigned long val,
994 enum fio_ddir ddir, unsigned int bs,
995 unsigned long t)
996{
997 const int nr_samples = iolog->nr_samples;
998
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
1006 iolog->log[nr_samples].val = val;
1007 iolog->log[nr_samples].time = t;
1008 iolog->log[nr_samples].ddir = ddir;
1009 iolog->log[nr_samples].bs = bs;
1010 iolog->nr_samples++;
1011}
1012
1013static void add_log_sample(struct thread_data *td, struct io_log *iolog,
1014 unsigned long val, enum fio_ddir ddir,
1015 unsigned int bs)
1016{
1017 if (!ddir_rw(ddir))
1018 return;
1019
1020 __add_log_sample(iolog, val, ddir, bs, mtime_since_now(&td->epoch));
1021}
1022
1023void add_agg_sample(unsigned long val, enum fio_ddir ddir, unsigned int bs)
1024{
1025 struct io_log *iolog;
1026
1027 if (!ddir_rw(ddir))
1028 return;
1029
1030 iolog = agg_io_log[ddir];
1031 __add_log_sample(iolog, val, ddir, bs, mtime_since_genesis());
1032}
1033
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
1043void add_clat_sample(struct thread_data *td, enum fio_ddir ddir,
1044 unsigned long usec, unsigned int bs)
1045{
1046 struct thread_stat *ts = &td->ts;
1047
1048 if (!ddir_rw(ddir))
1049 return;
1050
1051 add_stat_sample(&ts->clat_stat[ddir], usec);
1052
1053 if (td->clat_log)
1054 add_log_sample(td, td->clat_log, usec, ddir, bs);
1055
1056 if (ts->clat_percentiles)
1057 add_clat_percentile_sample(ts, usec, ddir);
1058}
1059
1060void add_slat_sample(struct thread_data *td, enum fio_ddir ddir,
1061 unsigned long usec, unsigned int bs)
1062{
1063 struct thread_stat *ts = &td->ts;
1064
1065 if (!ddir_rw(ddir))
1066 return;
1067
1068 add_stat_sample(&ts->slat_stat[ddir], usec);
1069
1070 if (td->slat_log)
1071 add_log_sample(td, td->slat_log, usec, ddir, bs);
1072}
1073
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
1079 if (!ddir_rw(ddir))
1080 return;
1081
1082 add_stat_sample(&ts->lat_stat[ddir], usec);
1083
1084 if (td->lat_log)
1085 add_log_sample(td, td->lat_log, usec, ddir, bs);
1086}
1087
1088void add_bw_sample(struct thread_data *td, enum fio_ddir ddir, unsigned int bs,
1089 struct timeval *t)
1090{
1091 struct thread_stat *ts = &td->ts;
1092 unsigned long spent, rate;
1093
1094 if (!ddir_rw(ddir))
1095 return;
1096
1097 spent = mtime_since(&td->bw_sample_time, t);
1098 if (spent < td->o.bw_avg_time)
1099 return;
1100
1101 rate = (td->this_io_bytes[ddir] - td->stat_io_bytes[ddir]) *
1102 1000 / spent / 1024;
1103 add_stat_sample(&ts->bw_stat[ddir], rate);
1104
1105 if (td->bw_log)
1106 add_log_sample(td, td->bw_log, rate, ddir, bs);
1107
1108 fio_gettime(&td->bw_sample_time, NULL);
1109 td->stat_io_bytes[ddir] = td->this_io_bytes[ddir];
1110}
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}