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