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