Break double loop on end-of-clat percentiles
[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 "ieee754.h"
13
14 void update_rusage_stat(struct thread_data *td)
15 {
16         struct thread_stat *ts = &td->ts;
17
18         getrusage(RUSAGE_SELF, &td->ru_end);
19
20         ts->usr_time += mtime_since(&td->ru_start.ru_utime,
21                                         &td->ru_end.ru_utime);
22         ts->sys_time += mtime_since(&td->ru_start.ru_stime,
23                                         &td->ru_end.ru_stime);
24         ts->ctx += td->ru_end.ru_nvcsw + td->ru_end.ru_nivcsw
25                         - (td->ru_start.ru_nvcsw + td->ru_start.ru_nivcsw);
26         ts->minf += td->ru_end.ru_minflt - td->ru_start.ru_minflt;
27         ts->majf += td->ru_end.ru_majflt - td->ru_start.ru_majflt;
28
29         memcpy(&td->ru_start, &td->ru_end, sizeof(td->ru_end));
30 }
31
32 /*
33  * Given a latency, return the index of the corresponding bucket in
34  * the structure tracking percentiles.
35  *
36  * (1) find the group (and error bits) that the value (latency)
37  * belongs to by looking at its MSB. (2) find the bucket number in the
38  * group by looking at the index bits.
39  *
40  */
41 static unsigned int plat_val_to_idx(unsigned int val)
42 {
43         unsigned int msb, error_bits, base, offset, idx;
44
45         /* Find MSB starting from bit 0 */
46         if (val == 0)
47                 msb = 0;
48         else
49                 msb = (sizeof(val)*8) - __builtin_clz(val) - 1;
50
51         /*
52          * MSB <= (FIO_IO_U_PLAT_BITS-1), cannot be rounded off. Use
53          * all bits of the sample as index
54          */
55         if (msb <= FIO_IO_U_PLAT_BITS)
56                 return val;
57
58         /* Compute the number of error bits to discard*/
59         error_bits = msb - FIO_IO_U_PLAT_BITS;
60
61         /* Compute the number of buckets before the group */
62         base = (error_bits + 1) << FIO_IO_U_PLAT_BITS;
63
64         /*
65          * Discard the error bits and apply the mask to find the
66          * index for the buckets in the group
67          */
68         offset = (FIO_IO_U_PLAT_VAL - 1) & (val >> error_bits);
69
70         /* Make sure the index does not exceed (array size - 1) */
71         idx = (base + offset) < (FIO_IO_U_PLAT_NR - 1)?
72                 (base + offset) : (FIO_IO_U_PLAT_NR - 1);
73
74         return idx;
75 }
76
77 /*
78  * Convert the given index of the bucket array to the value
79  * represented by the bucket
80  */
81 static unsigned int plat_idx_to_val(unsigned int idx)
82 {
83         unsigned int error_bits, k, base;
84
85         assert(idx < FIO_IO_U_PLAT_NR);
86
87         /* MSB <= (FIO_IO_U_PLAT_BITS-1), cannot be rounded off. Use
88          * all bits of the sample as index */
89         if (idx < (FIO_IO_U_PLAT_VAL << 1) )
90                 return idx;
91
92         /* Find the group and compute the minimum value of that group */
93         error_bits = (idx >> FIO_IO_U_PLAT_BITS) -1;
94         base = 1 << (error_bits + FIO_IO_U_PLAT_BITS);
95
96         /* Find its bucket number of the group */
97         k = idx % FIO_IO_U_PLAT_VAL;
98
99         /* Return the mean of the range of the bucket */
100         return base + ((k + 0.5) * (1 << error_bits));
101 }
102
103 static int double_cmp(const void *a, const void *b)
104 {
105         const fio_fp64_t fa = *(const fio_fp64_t *) a;
106         const fio_fp64_t fb = *(const fio_fp64_t *) b;
107         int cmp = 0;
108
109         if (fa.u.f > fb.u.f)
110                 cmp = 1;
111         else if (fa.u.f < fb.u.f)
112                 cmp = -1;
113
114         return cmp;
115 }
116
117 /*
118  * Find and display the p-th percentile of clat
119  */
120 static void show_clat_percentiles(unsigned int *io_u_plat, unsigned long nr,
121                                   fio_fp64_t *plist)
122 {
123         unsigned long sum = 0;
124         unsigned int len, i, j = 0, minv = -1U, maxv = 0;
125         unsigned int *ovals = NULL, oval_len = 0;
126         int is_last, scale_down;
127
128         len = 0;
129         while (len < FIO_IO_U_LIST_MAX_LEN && plist[len].u.f != 0.0)
130                 len++;
131
132         if (!len)
133                 return;
134
135         /*
136          * Sort the percentile list. Note that it may already be sorted if
137          * we are using the default values, but since it's a short list this
138          * isn't a worry. Also note that this does not work for NaN values.
139          */
140         if (len > 1)
141                 qsort((void*)plist, len, sizeof(plist[0]), double_cmp);
142
143         /*
144          * Calculate bucket values, note down max and min values
145          */
146         is_last = 0;
147         for (i = 0; i < FIO_IO_U_PLAT_NR && !is_last; i++) {
148                 sum += io_u_plat[i];
149                 while (sum >= (plist[j].u.f / 100.0 * nr)) {
150                         assert(plist[j].u.f <= 100.0);
151
152                         if (j == oval_len) {
153                                 oval_len += 100;
154                                 ovals = realloc(ovals, oval_len * sizeof(unsigned int));
155                         }
156
157                         ovals[j] = plat_idx_to_val(i);
158                         if (ovals[j] < minv)
159                                 minv = ovals[j];
160                         if (ovals[j] > maxv)
161                                 maxv = ovals[j];
162
163                         is_last = (j == len - 1);
164                         if (is_last)
165                                 break;
166
167                         j++;
168                 }
169         }
170
171         /*
172          * We default to usecs, but if the value range is such that we
173          * should scale down to msecs, do that.
174          */
175         if (minv > 2000 && maxv > 99999) {
176                 scale_down = 1;
177                 log_info("    clat percentiles (msec):\n     |");
178         } else {
179                 scale_down = 0;
180                 log_info("    clat percentiles (usec):\n     |");
181         }
182
183         for (j = 0; j < len; j++) {
184                 char fbuf[8];
185
186                 /* for formatting */
187                 if (j != 0 && (j % 4) == 0)
188                         log_info("     |");
189
190                 /* end of the list */
191                 is_last = (j == len - 1);
192
193                 if (plist[j].u.f < 10.0)
194                         sprintf(fbuf, " %2.2f", plist[j].u.f);
195                 else
196                         sprintf(fbuf, "%2.2f", plist[j].u.f);
197
198                 if (scale_down)
199                         ovals[j] = (ovals[j] + 999) / 1000;
200
201                 log_info(" %sth=[%5u]%c", fbuf, ovals[j], is_last ? '\n' : ',');
202
203                 if (is_last)
204                         break;
205
206                 if (j % 4 == 3) /* for formatting */
207                         log_info("\n");
208         }
209
210         if (ovals)
211                 free(ovals);
212 }
213
214 static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max,
215                     double *mean, double *dev)
216 {
217         double n = is->samples;
218
219         if (is->samples == 0)
220                 return 0;
221
222         *min = is->min_val;
223         *max = is->max_val;
224
225         n = (double) is->samples;
226         *mean = is->mean.u.f;
227
228         if (n > 1.0)
229                 *dev = sqrt(is->S.u.f / (n - 1.0));
230         else
231                 *dev = 0;
232
233         return 1;
234 }
235
236 void show_group_stats(struct group_run_stats *rs)
237 {
238         char *p1, *p2, *p3, *p4;
239         const char *ddir_str[] = { "   READ", "  WRITE" };
240         int i;
241
242         log_info("Run status group %d (all jobs):\n", rs->groupid);
243
244         for (i = 0; i <= DDIR_WRITE; i++) {
245                 const int i2p = is_power_of_2(rs->kb_base);
246
247                 if (!rs->max_run[i])
248                         continue;
249
250                 p1 = num2str(rs->io_kb[i], 6, rs->kb_base, i2p);
251                 p2 = num2str(rs->agg[i], 6, rs->kb_base, i2p);
252                 p3 = num2str(rs->min_bw[i], 6, rs->kb_base, i2p);
253                 p4 = num2str(rs->max_bw[i], 6, rs->kb_base, i2p);
254
255                 log_info("%s: io=%sB, aggrb=%sB/s, minb=%sB/s, maxb=%sB/s,"
256                          " mint=%llumsec, maxt=%llumsec\n", ddir_str[i], p1, p2,
257                                                 p3, p4, rs->min_run[i],
258                                                 rs->max_run[i]);
259
260                 free(p1);
261                 free(p2);
262                 free(p3);
263                 free(p4);
264         }
265 }
266
267 #define ts_total_io_u(ts)       \
268         ((ts)->total_io_u[0] + (ts)->total_io_u[1])
269
270 static void stat_calc_dist(unsigned int *map, unsigned long total,
271                            double *io_u_dist)
272 {
273         int i;
274
275         /*
276          * Do depth distribution calculations
277          */
278         for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
279                 if (total) {
280                         io_u_dist[i] = (double) map[i] / (double) total;
281                         io_u_dist[i] *= 100.0;
282                         if (io_u_dist[i] < 0.1 && map[i])
283                                 io_u_dist[i] = 0.1;
284                 } else
285                         io_u_dist[i] = 0.0;
286         }
287 }
288
289 static void stat_calc_lat(struct thread_stat *ts, double *dst,
290                           unsigned int *src, int nr)
291 {
292         unsigned long total = ts_total_io_u(ts);
293         int i;
294
295         /*
296          * Do latency distribution calculations
297          */
298         for (i = 0; i < nr; i++) {
299                 if (total) {
300                         dst[i] = (double) src[i] / (double) total;
301                         dst[i] *= 100.0;
302                         if (dst[i] < 0.01 && src[i])
303                                 dst[i] = 0.01;
304                 } else
305                         dst[i] = 0.0;
306         }
307 }
308
309 static void stat_calc_lat_u(struct thread_stat *ts, double *io_u_lat)
310 {
311         stat_calc_lat(ts, io_u_lat, ts->io_u_lat_u, FIO_IO_U_LAT_U_NR);
312 }
313
314 static void stat_calc_lat_m(struct thread_stat *ts, double *io_u_lat)
315 {
316         stat_calc_lat(ts, io_u_lat, ts->io_u_lat_m, FIO_IO_U_LAT_M_NR);
317 }
318
319 static int usec_to_msec(unsigned long *min, unsigned long *max, double *mean,
320                         double *dev)
321 {
322         if (*min > 1000 && *max > 1000 && *mean > 1000.0 && *dev > 1000.0) {
323                 *min /= 1000;
324                 *max /= 1000;
325                 *mean /= 1000.0;
326                 *dev /= 1000.0;
327                 return 0;
328         }
329
330         return 1;
331 }
332
333 static void show_ddir_status(struct group_run_stats *rs, struct thread_stat *ts,
334                              int ddir)
335 {
336         const char *ddir_str[] = { "read ", "write" };
337         unsigned long min, max, runt;
338         unsigned long long bw, iops;
339         double mean, dev;
340         char *io_p, *bw_p, *iops_p;
341         int i2p;
342
343         assert(ddir_rw(ddir));
344
345         if (!ts->runtime[ddir])
346                 return;
347
348         i2p = is_power_of_2(rs->kb_base);
349         runt = ts->runtime[ddir];
350
351         bw = (1000 * ts->io_bytes[ddir]) / runt;
352         io_p = num2str(ts->io_bytes[ddir], 6, 1, i2p);
353         bw_p = num2str(bw, 6, 1, i2p);
354
355         iops = (1000 * (uint64_t)ts->total_io_u[ddir]) / runt;
356         iops_p = num2str(iops, 6, 1, 0);
357
358         log_info("  %s: io=%sB, bw=%sB/s, iops=%s, runt=%6llumsec\n",
359                                         ddir_str[ddir], io_p, bw_p, iops_p,
360                                         ts->runtime[ddir]);
361
362         free(io_p);
363         free(bw_p);
364         free(iops_p);
365
366         if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev)) {
367                 const char *base = "(usec)";
368                 char *minp, *maxp;
369
370                 if (!usec_to_msec(&min, &max, &mean, &dev))
371                         base = "(msec)";
372
373                 minp = num2str(min, 6, 1, 0);
374                 maxp = num2str(max, 6, 1, 0);
375
376                 log_info("    slat %s: min=%s, max=%s, avg=%5.02f,"
377                          " stdev=%5.02f\n", base, minp, maxp, mean, dev);
378
379                 free(minp);
380                 free(maxp);
381         }
382         if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev)) {
383                 const char *base = "(usec)";
384                 char *minp, *maxp;
385
386                 if (!usec_to_msec(&min, &max, &mean, &dev))
387                         base = "(msec)";
388
389                 minp = num2str(min, 6, 1, 0);
390                 maxp = num2str(max, 6, 1, 0);
391
392                 log_info("    clat %s: min=%s, max=%s, avg=%5.02f,"
393                          " stdev=%5.02f\n", base, minp, maxp, mean, dev);
394
395                 free(minp);
396                 free(maxp);
397         }
398         if (calc_lat(&ts->lat_stat[ddir], &min, &max, &mean, &dev)) {
399                 const char *base = "(usec)";
400                 char *minp, *maxp;
401
402                 if (!usec_to_msec(&min, &max, &mean, &dev))
403                         base = "(msec)";
404
405                 minp = num2str(min, 6, 1, 0);
406                 maxp = num2str(max, 6, 1, 0);
407
408                 log_info("     lat %s: min=%s, max=%s, avg=%5.02f,"
409                          " stdev=%5.02f\n", base, minp, maxp, mean, dev);
410
411                 free(minp);
412                 free(maxp);
413         }
414         if (ts->clat_percentiles) {
415                 show_clat_percentiles(ts->io_u_plat[ddir],
416                                         ts->clat_stat[ddir].samples,
417                                         ts->percentile_list);
418         }
419         if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
420                 double p_of_agg;
421
422                 p_of_agg = mean * 100 / (double) rs->agg[ddir];
423                 log_info("     bw (KB/s) : min=%5lu, max=%5lu, per=%3.2f%%,"
424                          " avg=%5.02f, stdev=%5.02f\n", min, max, p_of_agg,
425                                                         mean, dev);
426         }
427 }
428
429 static void show_lat(double *io_u_lat, int nr, const char **ranges,
430                      const char *msg)
431 {
432         int new_line = 1, i, line = 0;
433
434         for (i = 0; i < nr; i++) {
435                 if (io_u_lat[i] <= 0.0)
436                         continue;
437                 if (new_line) {
438                         if (line)
439                                 log_info("\n");
440                         log_info("     lat (%s): ", msg);
441                         new_line = 0;
442                         line = 0;
443                 }
444                 if (line)
445                         log_info(", ");
446                 log_info("%s%3.2f%%", ranges[i], io_u_lat[i]);
447                 line++;
448                 if (line == 5)
449                         new_line = 1;
450         }
451 }
452
453 static void show_lat_u(double *io_u_lat_u)
454 {
455         const char *ranges[] = { "2=", "4=", "10=", "20=", "50=", "100=",
456                                  "250=", "500=", "750=", "1000=", };
457
458         show_lat(io_u_lat_u, FIO_IO_U_LAT_U_NR, ranges, "usec");
459 }
460
461 static void show_lat_m(double *io_u_lat_m)
462 {
463         const char *ranges[] = { "2=", "4=", "10=", "20=", "50=", "100=",
464                                  "250=", "500=", "750=", "1000=", "2000=",
465                                  ">=2000=", };
466
467         show_lat(io_u_lat_m, FIO_IO_U_LAT_M_NR, ranges, "msec");
468 }
469
470 static void show_latencies(double *io_u_lat_u, double *io_u_lat_m)
471 {
472         show_lat_u(io_u_lat_u);
473         log_info("\n");
474         show_lat_m(io_u_lat_m);
475         log_info("\n");
476 }
477
478 void show_thread_status(struct thread_stat *ts, struct group_run_stats *rs)
479 {
480         double usr_cpu, sys_cpu;
481         unsigned long runtime;
482         double io_u_dist[FIO_IO_U_MAP_NR];
483         double io_u_lat_u[FIO_IO_U_LAT_U_NR];
484         double io_u_lat_m[FIO_IO_U_LAT_M_NR];
485
486         if (!(ts->io_bytes[0] + ts->io_bytes[1]) &&
487             !(ts->total_io_u[0] + ts->total_io_u[1]))
488                 return;
489
490         if (!ts->error) {
491                 log_info("%s: (groupid=%d, jobs=%d): err=%2d: pid=%d\n",
492                                         ts->name, ts->groupid, ts->members,
493                                         ts->error, (int) ts->pid);
494         } else {
495                 log_info("%s: (groupid=%d, jobs=%d): err=%2d (%s): pid=%d\n",
496                                         ts->name, ts->groupid, ts->members,
497                                         ts->error, ts->verror, (int) ts->pid);
498         }
499
500         if (ts->description)
501                 log_info("  Description  : [%s]\n", ts->description);
502
503         if (ts->io_bytes[DDIR_READ])
504                 show_ddir_status(rs, ts, DDIR_READ);
505         if (ts->io_bytes[DDIR_WRITE])
506                 show_ddir_status(rs, ts, DDIR_WRITE);
507
508         runtime = ts->total_run_time;
509         if (runtime) {
510                 double runt = (double) runtime;
511
512                 usr_cpu = (double) ts->usr_time * 100 / runt;
513                 sys_cpu = (double) ts->sys_time * 100 / runt;
514         } else {
515                 usr_cpu = 0;
516                 sys_cpu = 0;
517         }
518
519         log_info("  cpu          : usr=%3.2f%%, sys=%3.2f%%, ctx=%lu, majf=%lu,"
520                  " minf=%lu\n", usr_cpu, sys_cpu, ts->ctx, ts->majf, ts->minf);
521
522         stat_calc_dist(ts->io_u_map, ts_total_io_u(ts), io_u_dist);
523         log_info("  IO depths    : 1=%3.1f%%, 2=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%,"
524                  " 16=%3.1f%%, 32=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
525                                         io_u_dist[1], io_u_dist[2],
526                                         io_u_dist[3], io_u_dist[4],
527                                         io_u_dist[5], io_u_dist[6]);
528
529         stat_calc_dist(ts->io_u_submit, ts->total_submit, io_u_dist);
530         log_info("     submit    : 0=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%, 16=%3.1f%%,"
531                  " 32=%3.1f%%, 64=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
532                                         io_u_dist[1], io_u_dist[2],
533                                         io_u_dist[3], io_u_dist[4],
534                                         io_u_dist[5], io_u_dist[6]);
535         stat_calc_dist(ts->io_u_complete, ts->total_complete, io_u_dist);
536         log_info("     complete  : 0=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%, 16=%3.1f%%,"
537                  " 32=%3.1f%%, 64=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
538                                         io_u_dist[1], io_u_dist[2],
539                                         io_u_dist[3], io_u_dist[4],
540                                         io_u_dist[5], io_u_dist[6]);
541         log_info("     issued r/w/d: total=%lu/%lu/%lu, short=%lu/%lu/%lu\n",
542                                         ts->total_io_u[0], ts->total_io_u[1],
543                                         ts->total_io_u[2],
544                                         ts->short_io_u[0], ts->short_io_u[1],
545                                         ts->short_io_u[2]);
546         stat_calc_lat_u(ts, io_u_lat_u);
547         stat_calc_lat_m(ts, io_u_lat_m);
548         show_latencies(io_u_lat_u, io_u_lat_m);
549         if (ts->continue_on_error) {
550                 log_info("     errors    : total=%lu, first_error=%d/<%s>\n",
551                                         ts->total_err_count,
552                                         ts->first_error,
553                                         strerror(ts->first_error));
554         }
555 }
556
557 static void show_ddir_status_terse(struct thread_stat *ts,
558                                    struct group_run_stats *rs, int ddir)
559 {
560         unsigned long min, max;
561         unsigned long long bw;
562         double mean, dev;
563
564         assert(ddir_rw(ddir));
565
566         bw = 0;
567         if (ts->runtime[ddir])
568                 bw = ts->io_bytes[ddir] / ts->runtime[ddir];
569
570         log_info(";%llu;%llu;%llu", ts->io_bytes[ddir] >> 10, bw,
571                                                         ts->runtime[ddir]);
572
573         if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev))
574                 log_info(";%lu;%lu;%f;%f", min, max, mean, dev);
575         else
576                 log_info(";%lu;%lu;%f;%f", 0UL, 0UL, 0.0, 0.0);
577
578         if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev))
579                 log_info(";%lu;%lu;%f;%f", min, max, mean, dev);
580         else
581                 log_info(";%lu;%lu;%f;%f", 0UL, 0UL, 0.0, 0.0);
582
583         if (calc_lat(&ts->lat_stat[ddir], &min, &max, &mean, &dev))
584                 log_info(";%lu;%lu;%f;%f", min, max, mean, dev);
585         else
586                 log_info(";%lu;%lu;%f;%f", 0UL, 0UL, 0.0, 0.0);
587
588         if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
589                 double p_of_agg;
590
591                 p_of_agg = mean * 100 / (double) rs->agg[ddir];
592                 log_info(";%lu;%lu;%f%%;%f;%f", min, max, p_of_agg, mean, dev);
593         } else
594                 log_info(";%lu;%lu;%f%%;%f;%f", 0UL, 0UL, 0.0, 0.0, 0.0);
595 }
596
597 #define FIO_TERSE_VERSION       "2"
598
599 static void show_thread_status_terse(struct thread_stat *ts,
600                                      struct group_run_stats *rs)
601 {
602         double io_u_dist[FIO_IO_U_MAP_NR];
603         double io_u_lat_u[FIO_IO_U_LAT_U_NR];
604         double io_u_lat_m[FIO_IO_U_LAT_M_NR];
605         double usr_cpu, sys_cpu;
606         int i;
607
608         /* General Info */
609         log_info("%s;%s;%d;%d", FIO_TERSE_VERSION, ts->name, ts->groupid,
610                                 ts->error);
611         /* Log Read Status */
612         show_ddir_status_terse(ts, rs, 0);
613         /* Log Write Status */
614         show_ddir_status_terse(ts, rs, 1);
615
616         /* CPU Usage */
617         if (ts->total_run_time) {
618                 double runt = (double) ts->total_run_time;
619
620                 usr_cpu = (double) ts->usr_time * 100 / runt;
621                 sys_cpu = (double) ts->sys_time * 100 / runt;
622         } else {
623                 usr_cpu = 0;
624                 sys_cpu = 0;
625         }
626
627         log_info(";%f%%;%f%%;%lu;%lu;%lu", usr_cpu, sys_cpu, ts->ctx, ts->majf,
628                                                                 ts->minf);
629
630         /* Calc % distribution of IO depths, usecond, msecond latency */
631         stat_calc_dist(ts->io_u_map, ts_total_io_u(ts), io_u_dist);
632         stat_calc_lat_u(ts, io_u_lat_u);
633         stat_calc_lat_m(ts, io_u_lat_m);
634
635         /* Only show fixed 7 I/O depth levels*/
636         log_info(";%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%",
637                         io_u_dist[0], io_u_dist[1], io_u_dist[2], io_u_dist[3],
638                         io_u_dist[4], io_u_dist[5], io_u_dist[6]);
639
640         /* Microsecond latency */
641         for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
642                 log_info(";%3.2f%%", io_u_lat_u[i]);
643         /* Millisecond latency */
644         for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
645                 log_info(";%3.2f%%", io_u_lat_m[i]);
646         /* Additional output if continue_on_error set - default off*/
647         if (ts->continue_on_error)
648                 log_info(";%lu;%d", ts->total_err_count, ts->first_error);
649         log_info("\n");
650
651         /* Additional output if description is set */
652         if (ts->description)
653                 log_info(";%s", ts->description);
654
655         log_info("\n");
656 }
657
658 static void sum_stat(struct io_stat *dst, struct io_stat *src, int nr)
659 {
660         double mean, S;
661
662         if (src->samples == 0)
663                 return;
664
665         dst->min_val = min(dst->min_val, src->min_val);
666         dst->max_val = max(dst->max_val, src->max_val);
667
668         /*
669          * Compute new mean and S after the merge
670          * <http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
671          *  #Parallel_algorithm>
672          */
673         if (nr == 1) {
674                 mean = src->mean.u.f;
675                 S = src->S.u.f;
676         } else {
677                 double delta = src->mean.u.f - dst->mean.u.f;
678
679                 mean = ((src->mean.u.f * src->samples) +
680                         (dst->mean.u.f * dst->samples)) /
681                         (dst->samples + src->samples);
682
683                 S =  src->S.u.f + dst->S.u.f + pow(delta, 2.0) *
684                         (dst->samples * src->samples) /
685                         (dst->samples + src->samples);
686         }
687
688         dst->samples += src->samples;
689         dst->mean.u.f = mean;
690         dst->S.u.f = S;
691 }
692
693 void sum_group_stats(struct group_run_stats *dst, struct group_run_stats *src)
694 {
695         int i;
696
697         for (i = 0; i < 2; i++) {
698                 if (dst->max_run[i] < src->max_run[i])
699                         dst->max_run[i] = src->max_run[i];
700                 if (dst->min_run[i] && dst->min_run[i] > src->min_run[i])
701                         dst->min_run[i] = src->min_run[i];
702                 if (dst->max_bw[i] < src->max_bw[i])
703                         dst->max_bw[i] = src->max_bw[i];
704                 if (dst->min_bw[i] && dst->min_bw[i] > src->min_bw[i])
705                         dst->min_bw[i] = src->min_bw[i];
706
707                 dst->io_kb[i] += src->io_kb[i];
708                 dst->agg[i] += src->agg[i];
709         }
710
711 }
712
713 void sum_thread_stats(struct thread_stat *dst, struct thread_stat *src, int nr)
714 {
715         int l, k;
716
717         for (l = 0; l <= DDIR_WRITE; l++) {
718                 sum_stat(&dst->clat_stat[l], &src->clat_stat[l], nr);
719                 sum_stat(&dst->slat_stat[l], &src->slat_stat[l], nr);
720                 sum_stat(&dst->lat_stat[l], &src->lat_stat[l], nr);
721                 sum_stat(&dst->bw_stat[l], &src->bw_stat[l], nr);
722
723                 dst->io_bytes[l] += src->io_bytes[l];
724
725                 if (dst->runtime[l] < src->runtime[l])
726                         dst->runtime[l] = src->runtime[l];
727         }
728
729         dst->usr_time += src->usr_time;
730         dst->sys_time += src->sys_time;
731         dst->ctx += src->ctx;
732         dst->majf += src->majf;
733         dst->minf += src->minf;
734
735         for (k = 0; k < FIO_IO_U_MAP_NR; k++)
736                 dst->io_u_map[k] += src->io_u_map[k];
737         for (k = 0; k < FIO_IO_U_MAP_NR; k++)
738                 dst->io_u_submit[k] += src->io_u_submit[k];
739         for (k = 0; k < FIO_IO_U_MAP_NR; k++)
740                 dst->io_u_complete[k] += src->io_u_complete[k];
741         for (k = 0; k < FIO_IO_U_LAT_U_NR; k++)
742                 dst->io_u_lat_u[k] += src->io_u_lat_u[k];
743         for (k = 0; k < FIO_IO_U_LAT_M_NR; k++)
744                 dst->io_u_lat_m[k] += src->io_u_lat_m[k];
745
746         for (k = 0; k <= 2; k++) {
747                 dst->total_io_u[k] += src->total_io_u[k];
748                 dst->short_io_u[k] += src->short_io_u[k];
749         }
750
751         for (k = 0; k <= DDIR_WRITE; k++) {
752                 int m;
753                 for (m = 0; m < FIO_IO_U_PLAT_NR; m++)
754                         dst->io_u_plat[k][m] += src->io_u_plat[k][m];
755         }
756
757         dst->total_run_time += src->total_run_time;
758         dst->total_submit += src->total_submit;
759         dst->total_complete += src->total_complete;
760 }
761
762 void init_group_run_stat(struct group_run_stats *gs)
763 {
764         memset(gs, 0, sizeof(*gs));
765         gs->min_bw[0] = gs->min_run[0] = ~0UL;
766         gs->min_bw[1] = gs->min_run[1] = ~0UL;
767 }
768
769 void init_thread_stat(struct thread_stat *ts)
770 {
771         int j;
772
773         memset(ts, 0, sizeof(*ts));
774
775         for (j = 0; j <= DDIR_WRITE; j++) {
776                 ts->lat_stat[j].min_val = -1UL;
777                 ts->clat_stat[j].min_val = -1UL;
778                 ts->slat_stat[j].min_val = -1UL;
779                 ts->bw_stat[j].min_val = -1UL;
780         }
781         ts->groupid = -1;
782 }
783
784 void show_run_stats(void)
785 {
786         struct group_run_stats *runstats, *rs;
787         struct thread_data *td;
788         struct thread_stat *threadstats, *ts;
789         int i, j, nr_ts, last_ts, idx;
790         int kb_base_warned = 0;
791
792         runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1));
793
794         for (i = 0; i < groupid + 1; i++)
795                 init_group_run_stat(&runstats[i]);
796
797         /*
798          * find out how many threads stats we need. if group reporting isn't
799          * enabled, it's one-per-td.
800          */
801         nr_ts = 0;
802         last_ts = -1;
803         for_each_td(td, i) {
804                 if (!td->o.group_reporting) {
805                         nr_ts++;
806                         continue;
807                 }
808                 if (last_ts == td->groupid)
809                         continue;
810
811                 last_ts = td->groupid;
812                 nr_ts++;
813         }
814
815         threadstats = malloc(nr_ts * sizeof(struct thread_stat));
816
817         for (i = 0; i < nr_ts; i++)
818                 init_thread_stat(&threadstats[i]);
819
820         j = 0;
821         last_ts = -1;
822         idx = 0;
823         for_each_td(td, i) {
824                 if (idx && (!td->o.group_reporting ||
825                     (td->o.group_reporting && last_ts != td->groupid))) {
826                         idx = 0;
827                         j++;
828                 }
829
830                 last_ts = td->groupid;
831
832                 ts = &threadstats[j];
833
834                 ts->clat_percentiles = td->o.clat_percentiles;
835                 if (td->o.overwrite_plist)
836                         memcpy(ts->percentile_list, td->o.percentile_list, sizeof(td->o.percentile_list));
837                 else
838                         memcpy(ts->percentile_list, def_percentile_list, sizeof(def_percentile_list));
839
840                 idx++;
841                 ts->members++;
842
843                 if (ts->groupid == -1) {
844                         /*
845                          * These are per-group shared already
846                          */
847                         strncpy(ts->name, td->o.name, FIO_JOBNAME_SIZE);
848                         if (td->o.description)
849                                 strncpy(ts->description, td->o.description,
850                                                 FIO_JOBNAME_SIZE);
851                         else
852                                 memset(ts->description, 0, FIO_JOBNAME_SIZE);
853
854                         ts->groupid = td->groupid;
855
856                         /*
857                          * first pid in group, not very useful...
858                          */
859                         ts->pid = td->pid;
860
861                         ts->kb_base = td->o.kb_base;
862                 } else if (ts->kb_base != td->o.kb_base && !kb_base_warned) {
863                         log_info("fio: kb_base differs for jobs in group, using"
864                                  " %u as the base\n", ts->kb_base);
865                         kb_base_warned = 1;
866                 }
867
868                 ts->continue_on_error = td->o.continue_on_error;
869                 ts->total_err_count += td->total_err_count;
870                 ts->first_error = td->first_error;
871                 if (!ts->error) {
872                         if (!td->error && td->o.continue_on_error &&
873                             td->first_error) {
874                                 ts->error = td->first_error;
875                                 strcpy(ts->verror, td->verror);
876                         } else  if (td->error) {
877                                 ts->error = td->error;
878                                 strcpy(ts->verror, td->verror);
879                         }
880                 }
881
882                 sum_thread_stats(ts, &td->ts, idx);
883         }
884
885         for (i = 0; i < nr_ts; i++) {
886                 unsigned long long bw;
887
888                 ts = &threadstats[i];
889                 rs = &runstats[ts->groupid];
890                 rs->kb_base = ts->kb_base;
891
892                 for (j = 0; j <= DDIR_WRITE; j++) {
893                         if (!ts->runtime[j])
894                                 continue;
895                         if (ts->runtime[j] < rs->min_run[j] || !rs->min_run[j])
896                                 rs->min_run[j] = ts->runtime[j];
897                         if (ts->runtime[j] > rs->max_run[j])
898                                 rs->max_run[j] = ts->runtime[j];
899
900                         bw = 0;
901                         if (ts->runtime[j]) {
902                                 unsigned long runt;
903
904                                 runt = ts->runtime[j];
905                                 bw = ts->io_bytes[j] / runt;
906                         }
907                         if (bw < rs->min_bw[j])
908                                 rs->min_bw[j] = bw;
909                         if (bw > rs->max_bw[j])
910                                 rs->max_bw[j] = bw;
911
912                         rs->io_kb[j] += ts->io_bytes[j] / rs->kb_base;
913                 }
914         }
915
916         for (i = 0; i < groupid + 1; i++) {
917                 unsigned long max_run[2];
918
919                 rs = &runstats[i];
920                 max_run[0] = rs->max_run[0];
921                 max_run[1] = rs->max_run[1];
922
923                 if (rs->max_run[0])
924                         rs->agg[0] = (rs->io_kb[0] * 1000) / max_run[0];
925                 if (rs->max_run[1])
926                         rs->agg[1] = (rs->io_kb[1] * 1000) / max_run[1];
927         }
928
929         /*
930          * don't overwrite last signal output
931          */
932         if (!terse_output)
933                 log_info("\n");
934
935         for (i = 0; i < nr_ts; i++) {
936                 ts = &threadstats[i];
937                 rs = &runstats[ts->groupid];
938
939                 if (is_backend)
940                         fio_server_send_ts(ts, rs);
941                 else if (terse_output)
942                         show_thread_status_terse(ts, rs);
943                 else
944                         show_thread_status(ts, rs);
945         }
946
947         if (!terse_output) {
948                 for (i = 0; i < groupid + 1; i++) {
949                         rs = &runstats[i];
950
951                         rs->groupid = i;
952                         if (is_backend)
953                                 fio_server_send_gs(rs);
954                         else
955                                 show_group_stats(rs);
956                 }
957
958                 if (is_backend)
959                         fio_server_send_du();
960                 else
961                         show_disk_util();
962
963                 free_disk_util();
964         }
965
966         free(runstats);
967         free(threadstats);
968 }
969
970 static inline void add_stat_sample(struct io_stat *is, unsigned long data)
971 {
972         double val = data;
973         double delta;
974
975         if (data > is->max_val)
976                 is->max_val = data;
977         if (data < is->min_val)
978                 is->min_val = data;
979
980         delta = val - is->mean.u.f;
981         if (delta) {
982                 is->mean.u.f += delta / (is->samples + 1.0);
983                 is->S.u.f += delta * (val - is->mean.u.f);
984         }
985
986         is->samples++;
987 }
988
989 static void __add_log_sample(struct io_log *iolog, unsigned long val,
990                              enum fio_ddir ddir, unsigned int bs,
991                              unsigned long t)
992 {
993         const int nr_samples = iolog->nr_samples;
994
995         if (iolog->nr_samples == iolog->max_samples) {
996                 int new_size = sizeof(struct io_sample) * iolog->max_samples*2;
997
998                 iolog->log = realloc(iolog->log, new_size);
999                 iolog->max_samples <<= 1;
1000         }
1001
1002         iolog->log[nr_samples].val = val;
1003         iolog->log[nr_samples].time = t;
1004         iolog->log[nr_samples].ddir = ddir;
1005         iolog->log[nr_samples].bs = bs;
1006         iolog->nr_samples++;
1007 }
1008
1009 static void add_log_sample(struct thread_data *td, struct io_log *iolog,
1010                            unsigned long val, enum fio_ddir ddir,
1011                            unsigned int bs)
1012 {
1013         if (!ddir_rw(ddir))
1014                 return;
1015
1016         __add_log_sample(iolog, val, ddir, bs, mtime_since_now(&td->epoch));
1017 }
1018
1019 void add_agg_sample(unsigned long val, enum fio_ddir ddir, unsigned int bs)
1020 {
1021         struct io_log *iolog;
1022
1023         if (!ddir_rw(ddir))
1024                 return;
1025
1026         iolog = agg_io_log[ddir];
1027         __add_log_sample(iolog, val, ddir, bs, mtime_since_genesis());
1028 }
1029
1030 static void add_clat_percentile_sample(struct thread_stat *ts,
1031                                 unsigned long usec, enum fio_ddir ddir)
1032 {
1033         unsigned int idx = plat_val_to_idx(usec);
1034         assert(idx < FIO_IO_U_PLAT_NR);
1035
1036         ts->io_u_plat[ddir][idx]++;
1037 }
1038
1039 void add_clat_sample(struct thread_data *td, enum fio_ddir ddir,
1040                      unsigned long usec, unsigned int bs)
1041 {
1042         struct thread_stat *ts = &td->ts;
1043
1044         if (!ddir_rw(ddir))
1045                 return;
1046
1047         add_stat_sample(&ts->clat_stat[ddir], usec);
1048
1049         if (td->clat_log)
1050                 add_log_sample(td, td->clat_log, usec, ddir, bs);
1051
1052         if (ts->clat_percentiles)
1053                 add_clat_percentile_sample(ts, usec, ddir);
1054 }
1055
1056 void add_slat_sample(struct thread_data *td, enum fio_ddir ddir,
1057                      unsigned long usec, unsigned int bs)
1058 {
1059         struct thread_stat *ts = &td->ts;
1060
1061         if (!ddir_rw(ddir))
1062                 return;
1063
1064         add_stat_sample(&ts->slat_stat[ddir], usec);
1065
1066         if (td->slat_log)
1067                 add_log_sample(td, td->slat_log, usec, ddir, bs);
1068 }
1069
1070 void add_lat_sample(struct thread_data *td, enum fio_ddir ddir,
1071                     unsigned long usec, unsigned int bs)
1072 {
1073         struct thread_stat *ts = &td->ts;
1074
1075         if (!ddir_rw(ddir))
1076                 return;
1077
1078         add_stat_sample(&ts->lat_stat[ddir], usec);
1079
1080         if (td->lat_log)
1081                 add_log_sample(td, td->lat_log, usec, ddir, bs);
1082 }
1083
1084 void add_bw_sample(struct thread_data *td, enum fio_ddir ddir, unsigned int bs,
1085                    struct timeval *t)
1086 {
1087         struct thread_stat *ts = &td->ts;
1088         unsigned long spent, rate;
1089
1090         if (!ddir_rw(ddir))
1091                 return;
1092
1093         spent = mtime_since(&td->bw_sample_time, t);
1094         if (spent < td->o.bw_avg_time)
1095                 return;
1096
1097         rate = (td->this_io_bytes[ddir] - td->stat_io_bytes[ddir]) *
1098                         1000 / spent / 1024;
1099         add_stat_sample(&ts->bw_stat[ddir], rate);
1100
1101         if (td->bw_log)
1102                 add_log_sample(td, td->bw_log, rate, ddir, bs);
1103
1104         fio_gettime(&td->bw_sample_time, NULL);
1105         td->stat_io_bytes[ddir] = td->this_io_bytes[ddir];
1106 }
1107
1108 void add_iops_sample(struct thread_data *td, enum fio_ddir ddir,
1109                      struct timeval *t)
1110 {
1111         struct thread_stat *ts = &td->ts;
1112         unsigned long spent, iops;
1113
1114         if (!ddir_rw(ddir))
1115                 return;
1116
1117         spent = mtime_since(&td->iops_sample_time, t);
1118         if (spent < td->o.iops_avg_time)
1119                 return;
1120
1121         iops = ((td->this_io_blocks[ddir] - td->stat_io_blocks[ddir]) * 1000) / spent;
1122
1123         add_stat_sample(&ts->iops_stat[ddir], iops);
1124
1125         if (td->iops_log) {
1126                 assert(iops);
1127                 add_log_sample(td, td->iops_log, iops, ddir, 0);
1128         }
1129
1130         fio_gettime(&td->iops_sample_time, NULL);
1131         td->stat_io_blocks[ddir] = td->this_io_blocks[ddir];
1132 }