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