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