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