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