Add support for trim as a workload type
[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 #define FIO_TERSE_VERSION       "3"
742
743 static void show_thread_status_terse_v3(struct thread_stat *ts,
744                                         struct group_run_stats *rs)
745 {
746         double io_u_dist[FIO_IO_U_MAP_NR];
747         double io_u_lat_u[FIO_IO_U_LAT_U_NR];
748         double io_u_lat_m[FIO_IO_U_LAT_M_NR];
749         double usr_cpu, sys_cpu;
750         int i;
751
752         /* General Info */
753         log_info("%s;%s;%s;%d;%d", FIO_TERSE_VERSION, fio_version_string,
754                                         ts->name, ts->groupid, ts->error);
755         /* Log Read Status */
756         show_ddir_status_terse(ts, rs, DDIR_READ);
757         /* Log Write Status */
758         show_ddir_status_terse(ts, rs, DDIR_WRITE);
759         /* Log Trim Status */
760         show_ddir_status_terse(ts, rs, DDIR_TRIM);
761
762         /* CPU Usage */
763         if (ts->total_run_time) {
764                 double runt = (double) ts->total_run_time;
765
766                 usr_cpu = (double) ts->usr_time * 100 / runt;
767                 sys_cpu = (double) ts->sys_time * 100 / runt;
768         } else {
769                 usr_cpu = 0;
770                 sys_cpu = 0;
771         }
772
773         log_info(";%f%%;%f%%;%lu;%lu;%lu", usr_cpu, sys_cpu, ts->ctx, ts->majf,
774                                                                 ts->minf);
775
776         /* Calc % distribution of IO depths, usecond, msecond latency */
777         stat_calc_dist(ts->io_u_map, ts_total_io_u(ts), io_u_dist);
778         stat_calc_lat_u(ts, io_u_lat_u);
779         stat_calc_lat_m(ts, io_u_lat_m);
780
781         /* Only show fixed 7 I/O depth levels*/
782         log_info(";%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%",
783                         io_u_dist[0], io_u_dist[1], io_u_dist[2], io_u_dist[3],
784                         io_u_dist[4], io_u_dist[5], io_u_dist[6]);
785
786         /* Microsecond latency */
787         for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
788                 log_info(";%3.2f%%", io_u_lat_u[i]);
789         /* Millisecond latency */
790         for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
791                 log_info(";%3.2f%%", io_u_lat_m[i]);
792
793         /* disk util stats, if any */
794         show_disk_util(1);
795
796         /* Additional output if continue_on_error set - default off*/
797         if (ts->continue_on_error)
798                 log_info(";%lu;%d", ts->total_err_count, ts->first_error);
799
800         /* Additional output if description is set */
801         if (strlen(ts->description))
802                 log_info(";%s", ts->description);
803
804         log_info("\n");
805 }
806
807 static void show_thread_status_terse(struct thread_stat *ts,
808                                      struct group_run_stats *rs)
809 {
810         if (terse_version == 2)
811                 show_thread_status_terse_v2(ts, rs);
812         else if (terse_version == 3)
813                 show_thread_status_terse_v3(ts, rs);
814         else
815                 log_err("fio: bad terse version!? %d\n", terse_version);
816 }
817
818 static void sum_stat(struct io_stat *dst, struct io_stat *src, int nr)
819 {
820         double mean, S;
821
822         if (src->samples == 0)
823                 return;
824
825         dst->min_val = min(dst->min_val, src->min_val);
826         dst->max_val = max(dst->max_val, src->max_val);
827
828         /*
829          * Compute new mean and S after the merge
830          * <http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
831          *  #Parallel_algorithm>
832          */
833         if (nr == 1) {
834                 mean = src->mean.u.f;
835                 S = src->S.u.f;
836         } else {
837                 double delta = src->mean.u.f - dst->mean.u.f;
838
839                 mean = ((src->mean.u.f * src->samples) +
840                         (dst->mean.u.f * dst->samples)) /
841                         (dst->samples + src->samples);
842
843                 S =  src->S.u.f + dst->S.u.f + pow(delta, 2.0) *
844                         (dst->samples * src->samples) /
845                         (dst->samples + src->samples);
846         }
847
848         dst->samples += src->samples;
849         dst->mean.u.f = mean;
850         dst->S.u.f = S;
851 }
852
853 void sum_group_stats(struct group_run_stats *dst, struct group_run_stats *src)
854 {
855         int i;
856
857         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
858                 if (dst->max_run[i] < src->max_run[i])
859                         dst->max_run[i] = src->max_run[i];
860                 if (dst->min_run[i] && dst->min_run[i] > src->min_run[i])
861                         dst->min_run[i] = src->min_run[i];
862                 if (dst->max_bw[i] < src->max_bw[i])
863                         dst->max_bw[i] = src->max_bw[i];
864                 if (dst->min_bw[i] && dst->min_bw[i] > src->min_bw[i])
865                         dst->min_bw[i] = src->min_bw[i];
866
867                 dst->io_kb[i] += src->io_kb[i];
868                 dst->agg[i] += src->agg[i];
869         }
870
871 }
872
873 void sum_thread_stats(struct thread_stat *dst, struct thread_stat *src, int nr)
874 {
875         int l, k;
876
877         for (l = 0; l < DDIR_RWDIR_CNT; l++) {
878                 sum_stat(&dst->clat_stat[l], &src->clat_stat[l], nr);
879                 sum_stat(&dst->slat_stat[l], &src->slat_stat[l], nr);
880                 sum_stat(&dst->lat_stat[l], &src->lat_stat[l], nr);
881                 sum_stat(&dst->bw_stat[l], &src->bw_stat[l], nr);
882
883                 dst->io_bytes[l] += src->io_bytes[l];
884
885                 if (dst->runtime[l] < src->runtime[l])
886                         dst->runtime[l] = src->runtime[l];
887         }
888
889         dst->usr_time += src->usr_time;
890         dst->sys_time += src->sys_time;
891         dst->ctx += src->ctx;
892         dst->majf += src->majf;
893         dst->minf += src->minf;
894
895         for (k = 0; k < FIO_IO_U_MAP_NR; k++)
896                 dst->io_u_map[k] += src->io_u_map[k];
897         for (k = 0; k < FIO_IO_U_MAP_NR; k++)
898                 dst->io_u_submit[k] += src->io_u_submit[k];
899         for (k = 0; k < FIO_IO_U_MAP_NR; k++)
900                 dst->io_u_complete[k] += src->io_u_complete[k];
901         for (k = 0; k < FIO_IO_U_LAT_U_NR; k++)
902                 dst->io_u_lat_u[k] += src->io_u_lat_u[k];
903         for (k = 0; k < FIO_IO_U_LAT_M_NR; k++)
904                 dst->io_u_lat_m[k] += src->io_u_lat_m[k];
905
906         for (k = 0; k < DDIR_RWDIR_CNT; k++) {
907                 dst->total_io_u[k] += src->total_io_u[k];
908                 dst->short_io_u[k] += src->short_io_u[k];
909         }
910
911         for (k = 0; k < DDIR_RWDIR_CNT; k++) {
912                 int m;
913                 for (m = 0; m < FIO_IO_U_PLAT_NR; m++)
914                         dst->io_u_plat[k][m] += src->io_u_plat[k][m];
915         }
916
917         dst->total_run_time += src->total_run_time;
918         dst->total_submit += src->total_submit;
919         dst->total_complete += src->total_complete;
920 }
921
922 void init_group_run_stat(struct group_run_stats *gs)
923 {
924         int i;
925         memset(gs, 0, sizeof(*gs));
926
927         for (i = 0; i < DDIR_RWDIR_CNT; i++)
928                 gs->min_bw[i] = gs->min_run[i] = ~0UL;
929 }
930
931 void init_thread_stat(struct thread_stat *ts)
932 {
933         int j;
934
935         memset(ts, 0, sizeof(*ts));
936
937         for (j = 0; j < DDIR_RWDIR_CNT; j++) {
938                 ts->lat_stat[j].min_val = -1UL;
939                 ts->clat_stat[j].min_val = -1UL;
940                 ts->slat_stat[j].min_val = -1UL;
941                 ts->bw_stat[j].min_val = -1UL;
942         }
943         ts->groupid = -1;
944 }
945
946 void show_run_stats(void)
947 {
948         struct group_run_stats *runstats, *rs;
949         struct thread_data *td;
950         struct thread_stat *threadstats, *ts;
951         int i, j, nr_ts, last_ts, idx;
952         int kb_base_warned = 0;
953
954         runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1));
955
956         for (i = 0; i < groupid + 1; i++)
957                 init_group_run_stat(&runstats[i]);
958
959         /*
960          * find out how many threads stats we need. if group reporting isn't
961          * enabled, it's one-per-td.
962          */
963         nr_ts = 0;
964         last_ts = -1;
965         for_each_td(td, i) {
966                 if (!td->o.group_reporting) {
967                         nr_ts++;
968                         continue;
969                 }
970                 if (last_ts == td->groupid)
971                         continue;
972
973                 last_ts = td->groupid;
974                 nr_ts++;
975         }
976
977         threadstats = malloc(nr_ts * sizeof(struct thread_stat));
978
979         for (i = 0; i < nr_ts; i++)
980                 init_thread_stat(&threadstats[i]);
981
982         j = 0;
983         last_ts = -1;
984         idx = 0;
985         for_each_td(td, i) {
986                 if (idx && (!td->o.group_reporting ||
987                     (td->o.group_reporting && last_ts != td->groupid))) {
988                         idx = 0;
989                         j++;
990                 }
991
992                 last_ts = td->groupid;
993
994                 ts = &threadstats[j];
995
996                 ts->clat_percentiles = td->o.clat_percentiles;
997                 if (td->o.overwrite_plist)
998                         memcpy(ts->percentile_list, td->o.percentile_list, sizeof(td->o.percentile_list));
999                 else
1000                         memcpy(ts->percentile_list, def_percentile_list, sizeof(def_percentile_list));
1001
1002                 idx++;
1003                 ts->members++;
1004
1005                 if (ts->groupid == -1) {
1006                         /*
1007                          * These are per-group shared already
1008                          */
1009                         strncpy(ts->name, td->o.name, FIO_JOBNAME_SIZE);
1010                         if (td->o.description)
1011                                 strncpy(ts->description, td->o.description,
1012                                                 FIO_JOBNAME_SIZE);
1013                         else
1014                                 memset(ts->description, 0, FIO_JOBNAME_SIZE);
1015
1016                         ts->groupid = td->groupid;
1017
1018                         /*
1019                          * first pid in group, not very useful...
1020                          */
1021                         ts->pid = td->pid;
1022
1023                         ts->kb_base = td->o.kb_base;
1024                 } else if (ts->kb_base != td->o.kb_base && !kb_base_warned) {
1025                         log_info("fio: kb_base differs for jobs in group, using"
1026                                  " %u as the base\n", ts->kb_base);
1027                         kb_base_warned = 1;
1028                 }
1029
1030                 ts->continue_on_error = td->o.continue_on_error;
1031                 ts->total_err_count += td->total_err_count;
1032                 ts->first_error = td->first_error;
1033                 if (!ts->error) {
1034                         if (!td->error && td->o.continue_on_error &&
1035                             td->first_error) {
1036                                 ts->error = td->first_error;
1037                                 strcpy(ts->verror, td->verror);
1038                         } else  if (td->error) {
1039                                 ts->error = td->error;
1040                                 strcpy(ts->verror, td->verror);
1041                         }
1042                 }
1043
1044                 sum_thread_stats(ts, &td->ts, idx);
1045         }
1046
1047         for (i = 0; i < nr_ts; i++) {
1048                 unsigned long long bw;
1049
1050                 ts = &threadstats[i];
1051                 rs = &runstats[ts->groupid];
1052                 rs->kb_base = ts->kb_base;
1053
1054                 for (j = 0; j < DDIR_RWDIR_CNT; j++) {
1055                         if (!ts->runtime[j])
1056                                 continue;
1057                         if (ts->runtime[j] < rs->min_run[j] || !rs->min_run[j])
1058                                 rs->min_run[j] = ts->runtime[j];
1059                         if (ts->runtime[j] > rs->max_run[j])
1060                                 rs->max_run[j] = ts->runtime[j];
1061
1062                         bw = 0;
1063                         if (ts->runtime[j]) {
1064                                 unsigned long runt = ts->runtime[j];
1065                                 unsigned long long kb;
1066
1067                                 kb = ts->io_bytes[j] / rs->kb_base;
1068                                 bw = kb * 1000 / runt;
1069                         }
1070                         if (bw < rs->min_bw[j])
1071                                 rs->min_bw[j] = bw;
1072                         if (bw > rs->max_bw[j])
1073                                 rs->max_bw[j] = bw;
1074
1075                         rs->io_kb[j] += ts->io_bytes[j] / rs->kb_base;
1076                 }
1077         }
1078
1079         for (i = 0; i < groupid + 1; i++) {
1080                 int ddir;
1081
1082                 rs = &runstats[i];
1083
1084                 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) {
1085                         if (rs->max_run[ddir])
1086                                 rs->agg[ddir] = (rs->io_kb[ddir] * 1000) /
1087                                                 rs->max_run[ddir];
1088                 }
1089         }
1090
1091         /*
1092          * don't overwrite last signal output
1093          */
1094         if (!terse_output)
1095                 log_info("\n");
1096
1097         for (i = 0; i < nr_ts; i++) {
1098                 ts = &threadstats[i];
1099                 rs = &runstats[ts->groupid];
1100
1101                 if (is_backend)
1102                         fio_server_send_ts(ts, rs);
1103                 else if (terse_output)
1104                         show_thread_status_terse(ts, rs);
1105                 else
1106                         show_thread_status(ts, rs);
1107         }
1108
1109         for (i = 0; i < groupid + 1; i++) {
1110                 rs = &runstats[i];
1111
1112                 rs->groupid = i;
1113                 if (is_backend)
1114                         fio_server_send_gs(rs);
1115                 else if (!terse_output)
1116                         show_group_stats(rs);
1117         }
1118
1119         if (is_backend)
1120                 fio_server_send_du();
1121         else if (!terse_output)
1122                 show_disk_util(0);
1123
1124         free(runstats);
1125         free(threadstats);
1126 }
1127
1128 static void *__show_running_run_stats(void *arg)
1129 {
1130         struct thread_data *td;
1131         unsigned long long *rt;
1132         struct timeval tv;
1133         int i;
1134
1135         rt = malloc(thread_number * sizeof(unsigned long long));
1136         fio_gettime(&tv, NULL);
1137
1138         for_each_td(td, i) {
1139                 rt[i] = mtime_since(&td->start, &tv);
1140                 if (td_read(td) && td->io_bytes[DDIR_READ])
1141                         td->ts.runtime[DDIR_READ] += rt[i];
1142                 if (td_write(td) && td->io_bytes[DDIR_WRITE])
1143                         td->ts.runtime[DDIR_WRITE] += rt[i];
1144                 if (td_trim(td) && td->io_bytes[DDIR_TRIM])
1145                         td->ts.runtime[DDIR_TRIM] += rt[i];
1146
1147                 update_rusage_stat(td);
1148                 td->ts.io_bytes[DDIR_READ] = td->io_bytes[DDIR_READ];
1149                 td->ts.io_bytes[DDIR_WRITE] = td->io_bytes[DDIR_WRITE];
1150                 td->ts.io_bytes[DDIR_TRIM] = td->io_bytes[DDIR_TRIM];
1151                 td->ts.total_run_time = mtime_since(&td->epoch, &tv);
1152         }
1153
1154         show_run_stats();
1155
1156         for_each_td(td, i) {
1157                 if (td_read(td) && td->io_bytes[DDIR_READ])
1158                         td->ts.runtime[DDIR_READ] -= rt[i];
1159                 if (td_write(td) && td->io_bytes[DDIR_WRITE])
1160                         td->ts.runtime[DDIR_WRITE] -= rt[i];
1161                 if (td_trim(td) && td->io_bytes[DDIR_TRIM])
1162                         td->ts.runtime[DDIR_TRIM] -= rt[i];
1163         }
1164
1165         free(rt);
1166         return NULL;
1167 }
1168
1169 /*
1170  * Called from signal handler. It _should_ be safe to just run this inline
1171  * in the sig handler, but we should be disturbing the system less by just
1172  * creating a thread to do it.
1173  */
1174 void show_running_run_stats(void)
1175 {
1176         pthread_t thread;
1177
1178         pthread_create(&thread, NULL, __show_running_run_stats, NULL);
1179         pthread_detach(thread);
1180 }
1181
1182 static inline void add_stat_sample(struct io_stat *is, unsigned long data)
1183 {
1184         double val = data;
1185         double delta;
1186
1187         if (data > is->max_val)
1188                 is->max_val = data;
1189         if (data < is->min_val)
1190                 is->min_val = data;
1191
1192         delta = val - is->mean.u.f;
1193         if (delta) {
1194                 is->mean.u.f += delta / (is->samples + 1.0);
1195                 is->S.u.f += delta * (val - is->mean.u.f);
1196         }
1197
1198         is->samples++;
1199 }
1200
1201 static void __add_log_sample(struct io_log *iolog, unsigned long val,
1202                              enum fio_ddir ddir, unsigned int bs,
1203                              unsigned long t)
1204 {
1205         const int nr_samples = iolog->nr_samples;
1206
1207         if (!iolog->nr_samples)
1208                 iolog->avg_last = t;
1209
1210         if (iolog->nr_samples == iolog->max_samples) {
1211                 int new_size = sizeof(struct io_sample) * iolog->max_samples*2;
1212
1213                 iolog->log = realloc(iolog->log, new_size);
1214                 iolog->max_samples <<= 1;
1215         }
1216
1217         iolog->log[nr_samples].val = val;
1218         iolog->log[nr_samples].time = t;
1219         iolog->log[nr_samples].ddir = ddir;
1220         iolog->log[nr_samples].bs = bs;
1221         iolog->nr_samples++;
1222 }
1223
1224 static inline void reset_io_stat(struct io_stat *ios)
1225 {
1226         ios->max_val = ios->min_val = ios->samples = 0;
1227         ios->mean.u.f = ios->S.u.f = 0;
1228 }
1229
1230 static void add_log_sample(struct thread_data *td, struct io_log *iolog,
1231                            unsigned long val, enum fio_ddir ddir,
1232                            unsigned int bs)
1233 {
1234         unsigned long elapsed, this_window;
1235
1236         if (!ddir_rw(ddir))
1237                 return;
1238
1239         elapsed = mtime_since_now(&td->epoch);
1240
1241         /*
1242          * If no time averaging, just add the log sample.
1243          */
1244         if (!iolog->avg_msec) {
1245                 __add_log_sample(iolog, val, ddir, bs, elapsed);
1246                 return;
1247         }
1248
1249         /*
1250          * Add the sample. If the time period has passed, then
1251          * add that entry to the log and clear.
1252          */
1253         add_stat_sample(&iolog->avg_window[ddir], val);
1254
1255         /*
1256          * If period hasn't passed, adding the above sample is all we
1257          * need to do.
1258          */
1259         this_window = elapsed - iolog->avg_last;
1260         if (this_window < iolog->avg_msec)
1261                 return;
1262
1263         /*
1264          * Note an entry in the log. Use the mean from the logged samples,
1265          * making sure to properly round up. Only write a log entry if we
1266          * had actual samples done.
1267          */
1268         if (iolog->avg_window[DDIR_READ].samples) {
1269                 unsigned long mr;
1270
1271                 mr = iolog->avg_window[DDIR_READ].mean.u.f + 0.50;
1272                 __add_log_sample(iolog, mr, DDIR_READ, 0, elapsed);
1273         }
1274         if (iolog->avg_window[DDIR_WRITE].samples) {
1275                 unsigned long mw;
1276
1277                 mw = iolog->avg_window[DDIR_WRITE].mean.u.f + 0.50;
1278                 __add_log_sample(iolog, mw, DDIR_WRITE, 0, elapsed);
1279         }
1280         if (iolog->avg_window[DDIR_TRIM].samples) {
1281                 unsigned long mw;
1282
1283                 mw = iolog->avg_window[DDIR_TRIM].mean.u.f + 0.50;
1284                 __add_log_sample(iolog, mw, DDIR_TRIM, 0, elapsed);
1285         }
1286
1287
1288         reset_io_stat(&iolog->avg_window[DDIR_READ]);
1289         reset_io_stat(&iolog->avg_window[DDIR_WRITE]);
1290         reset_io_stat(&iolog->avg_window[DDIR_TRIM]);
1291         iolog->avg_last = elapsed;
1292 }
1293
1294 void add_agg_sample(unsigned long val, enum fio_ddir ddir, unsigned int bs)
1295 {
1296         struct io_log *iolog;
1297
1298         if (!ddir_rw(ddir))
1299                 return;
1300
1301         iolog = agg_io_log[ddir];
1302         __add_log_sample(iolog, val, ddir, bs, mtime_since_genesis());
1303 }
1304
1305 static void add_clat_percentile_sample(struct thread_stat *ts,
1306                                 unsigned long usec, enum fio_ddir ddir)
1307 {
1308         unsigned int idx = plat_val_to_idx(usec);
1309         assert(idx < FIO_IO_U_PLAT_NR);
1310
1311         ts->io_u_plat[ddir][idx]++;
1312 }
1313
1314 void add_clat_sample(struct thread_data *td, enum fio_ddir ddir,
1315                      unsigned long usec, unsigned int bs)
1316 {
1317         struct thread_stat *ts = &td->ts;
1318
1319         if (!ddir_rw(ddir))
1320                 return;
1321
1322         add_stat_sample(&ts->clat_stat[ddir], usec);
1323
1324         if (td->clat_log)
1325                 add_log_sample(td, td->clat_log, usec, ddir, bs);
1326
1327         if (ts->clat_percentiles)
1328                 add_clat_percentile_sample(ts, usec, ddir);
1329 }
1330
1331 void add_slat_sample(struct thread_data *td, enum fio_ddir ddir,
1332                      unsigned long usec, unsigned int bs)
1333 {
1334         struct thread_stat *ts = &td->ts;
1335
1336         if (!ddir_rw(ddir))
1337                 return;
1338
1339         add_stat_sample(&ts->slat_stat[ddir], usec);
1340
1341         if (td->slat_log)
1342                 add_log_sample(td, td->slat_log, usec, ddir, bs);
1343 }
1344
1345 void add_lat_sample(struct thread_data *td, enum fio_ddir ddir,
1346                     unsigned long usec, unsigned int bs)
1347 {
1348         struct thread_stat *ts = &td->ts;
1349
1350         if (!ddir_rw(ddir))
1351                 return;
1352
1353         add_stat_sample(&ts->lat_stat[ddir], usec);
1354
1355         if (td->lat_log)
1356                 add_log_sample(td, td->lat_log, usec, ddir, bs);
1357 }
1358
1359 void add_bw_sample(struct thread_data *td, enum fio_ddir ddir, unsigned int bs,
1360                    struct timeval *t)
1361 {
1362         struct thread_stat *ts = &td->ts;
1363         unsigned long spent, rate;
1364
1365         if (!ddir_rw(ddir))
1366                 return;
1367
1368         spent = mtime_since(&td->bw_sample_time, t);
1369         if (spent < td->o.bw_avg_time)
1370                 return;
1371
1372         /*
1373          * Compute both read and write rates for the interval.
1374          */
1375         for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++) {
1376                 uint64_t delta;
1377
1378                 delta = td->this_io_bytes[ddir] - td->stat_io_bytes[ddir];
1379                 if (!delta)
1380                         continue; /* No entries for interval */
1381
1382                 rate = delta * 1000 / spent / 1024;
1383                 add_stat_sample(&ts->bw_stat[ddir], rate);
1384
1385                 if (td->bw_log)
1386                         add_log_sample(td, td->bw_log, rate, ddir, bs);
1387
1388                 td->stat_io_bytes[ddir] = td->this_io_bytes[ddir];
1389         }
1390
1391         fio_gettime(&td->bw_sample_time, NULL);
1392 }
1393
1394 void add_iops_sample(struct thread_data *td, enum fio_ddir ddir,
1395                      struct timeval *t)
1396 {
1397         struct thread_stat *ts = &td->ts;
1398         unsigned long spent, iops;
1399
1400         if (!ddir_rw(ddir))
1401                 return;
1402
1403         spent = mtime_since(&td->iops_sample_time, t);
1404         if (spent < td->o.iops_avg_time)
1405                 return;
1406
1407         /*
1408          * Compute both read and write rates for the interval.
1409          */
1410         for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++) {
1411                 uint64_t delta;
1412
1413                 delta = td->this_io_blocks[ddir] - td->stat_io_blocks[ddir];
1414                 if (!delta)
1415                         continue; /* No entries for interval */
1416
1417                 iops = (delta * 1000) / spent;
1418                 add_stat_sample(&ts->iops_stat[ddir], iops);
1419
1420                 if (td->iops_log)
1421                         add_log_sample(td, td->iops_log, iops, ddir, 0);
1422
1423                 td->stat_io_blocks[ddir] = td->this_io_blocks[ddir];
1424         }
1425
1426         fio_gettime(&td->iops_sample_time, NULL);
1427 }