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