Improve iodepth logging
[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
12 /*
13  * Cheesy number->string conversion, complete with carry rounding error.
14  */
15 static char *num2str(unsigned long num, int maxlen, int base, int pow2)
16 {
17         char postfix[] = { ' ', 'K', 'M', 'G', 'P', 'E' };
18         unsigned int thousand;
19         char *buf;
20         int i;
21
22         if (pow2)
23                 thousand = 1024;
24         else
25                 thousand = 1000;
26
27         buf = malloc(128);
28
29         for (i = 0; base > 1; i++)
30                 base /= thousand;
31
32         do {
33                 int len, carry = 0;
34
35                 len = sprintf(buf, "%'lu", num);
36                 if (len <= maxlen) {
37                         if (i >= 1) {
38                                 buf[len] = postfix[i];
39                                 buf[len + 1] = '\0';
40                         }
41                         return buf;
42                 }
43
44                 if ((num % thousand) >= (thousand / 2))
45                         carry = 1;
46
47                 num /= thousand;
48                 num += carry;
49                 i++;
50         } while (i <= 5);
51
52         return buf;
53 }
54
55 void update_rusage_stat(struct thread_data *td)
56 {
57         struct thread_stat *ts = &td->ts;
58
59         getrusage(RUSAGE_SELF, &ts->ru_end);
60
61         ts->usr_time += mtime_since(&ts->ru_start.ru_utime,
62                                         &ts->ru_end.ru_utime);
63         ts->sys_time += mtime_since(&ts->ru_start.ru_stime,
64                                         &ts->ru_end.ru_stime);
65         ts->ctx += ts->ru_end.ru_nvcsw + ts->ru_end.ru_nivcsw
66                         - (ts->ru_start.ru_nvcsw + ts->ru_start.ru_nivcsw);
67         ts->minf += ts->ru_end.ru_minflt - ts->ru_start.ru_minflt;
68         ts->majf += ts->ru_end.ru_majflt - ts->ru_start.ru_majflt;
69
70         memcpy(&ts->ru_start, &ts->ru_end, sizeof(ts->ru_end));
71 }
72
73 static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max,
74                     double *mean, double *dev)
75 {
76         double n = is->samples;
77
78         if (is->samples == 0)
79                 return 0;
80
81         *min = is->min_val;
82         *max = is->max_val;
83
84         n = (double) is->samples;
85         *mean = is->mean;
86
87         if (n > 1.0)
88                 *dev = sqrt(is->S / (n - 1.0));
89         else
90                 *dev = 0;
91
92         return 1;
93 }
94
95 static void show_group_stats(struct group_run_stats *rs, int id)
96 {
97         char *p1, *p2, *p3, *p4;
98         const char *ddir_str[] = { "   READ", "  WRITE" };
99         int i;
100
101         log_info("\nRun status group %d (all jobs):\n", id);
102
103         for (i = 0; i <= DDIR_WRITE; i++) {
104                 if (!rs->max_run[i])
105                         continue;
106
107                 p1 = num2str(rs->io_kb[i], 6, 1000, 1);
108                 p2 = num2str(rs->agg[i], 6, 1000, 1);
109                 p3 = num2str(rs->min_bw[i], 6, 1000, 1);
110                 p4 = num2str(rs->max_bw[i], 6, 1000, 1);
111
112                 log_info("%s: io=%siB, aggrb=%siB/s, minb=%siB/s, maxb=%siB/s,"
113                          " mint=%llumsec, maxt=%llumsec\n", ddir_str[i], p1, p2,
114                                                 p3, p4, rs->min_run[i],
115                                                 rs->max_run[i]);
116
117                 free(p1);
118                 free(p2);
119                 free(p3);
120                 free(p4);
121         }
122 }
123
124 #define ts_total_io_u(ts)       \
125         ((ts)->total_io_u[0] + (ts)->total_io_u[1])
126
127 static void stat_calc_dist(struct thread_stat *ts, double *io_u_dist)
128 {
129         int i;
130
131         /*
132          * Do depth distribution calculations
133          */
134         for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
135                 io_u_dist[i] = (double) ts->io_u_map[i]
136                                         / (double) ts_total_io_u(ts);
137                 io_u_dist[i] *= 100.0;
138                 if (io_u_dist[i] < 0.1 && ts->io_u_map[i])
139                         io_u_dist[i] = 0.1;
140         }
141 }
142
143 static void stat_calc_lat(struct thread_stat *ts, double *dst,
144                           unsigned int *src, int nr)
145 {
146         int i;
147
148         /*
149          * Do latency distribution calculations
150          */
151         for (i = 0; i < nr; i++) {
152                 dst[i] = (double) src[i] / (double) ts_total_io_u(ts);
153                 dst[i] *= 100.0;
154                 if (dst[i] < 0.01 && src[i])
155                         dst[i] = 0.01;
156         }
157 }
158
159 static void stat_calc_lat_u(struct thread_stat *ts, double *io_u_lat)
160 {
161         stat_calc_lat(ts, io_u_lat, ts->io_u_lat_u, FIO_IO_U_LAT_U_NR);
162 }
163
164 static void stat_calc_lat_m(struct thread_stat *ts, double *io_u_lat)
165 {
166         stat_calc_lat(ts, io_u_lat, ts->io_u_lat_m, FIO_IO_U_LAT_M_NR);
167 }
168
169 static int usec_to_msec(unsigned long *min, unsigned long *max, double *mean,
170                         double *dev)
171 {
172         if (*min > 1000 && *max > 1000 && *mean > 1000.0 && *dev > 1000.0) {
173                 *min /= 1000;
174                 *max /= 1000;
175                 *mean /= 1000.0;
176                 *dev /= 1000.0;
177                 return 0;
178         }
179
180         return 1;
181 }
182
183 static void show_ddir_status(struct group_run_stats *rs, struct thread_stat *ts,
184                              int ddir)
185 {
186         const char *ddir_str[] = { "read ", "write" };
187         unsigned long min, max;
188         unsigned long long bw, iops;
189         double mean, dev;
190         char *io_p, *bw_p, *iops_p;
191
192         if (!ts->runtime[ddir])
193                 return;
194
195         bw = ts->io_bytes[ddir] / ts->runtime[ddir];
196         iops = (1000 * ts->total_io_u[ddir]) / ts->runtime[ddir];
197         io_p = num2str(ts->io_bytes[ddir] >> 10, 6, 1000, 1);
198         bw_p = num2str(bw, 6, 1000, 1);
199         iops_p = num2str(iops, 6, 1, 0);
200
201         log_info("  %s: io=%siB, bw=%siB/s, iops=%s, runt=%6lumsec\n",
202                                         ddir_str[ddir], io_p, bw_p, iops_p,
203                                         ts->runtime[ddir]);
204
205         free(io_p);
206         free(bw_p);
207         free(iops_p);
208
209         if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev)) {
210                 const char *base = "(usec)";
211                 char *minp, *maxp;
212
213                 if (!usec_to_msec(&min, &max, &mean, &dev))
214                         base = "(msec)";
215
216                 minp = num2str(min, 6, 1, 0);
217                 maxp = num2str(max, 6, 1, 0);
218
219                 log_info("    slat %s: min=%s, max=%s, avg=%5.02f,"
220                          " stdev=%5.02f\n", base, minp, maxp, mean, dev);
221
222                 free(minp);
223                 free(maxp);
224         }
225         if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev)) {
226                 const char *base = "(usec)";
227                 char *minp, *maxp;
228
229                 if (!usec_to_msec(&min, &max, &mean, &dev))
230                         base = "(msec)";
231
232                 minp = num2str(min, 6, 1, 0);
233                 maxp = num2str(max, 6, 1, 0);
234
235                 log_info("    clat %s: min=%s, max=%s, avg=%5.02f,"
236                          " stdev=%5.02f\n", base, minp, maxp, mean, dev);
237
238                 free(minp);
239                 free(maxp);
240         }
241         if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
242                 double p_of_agg;
243
244                 p_of_agg = mean * 100 / (double) rs->agg[ddir];
245                 log_info("    bw (KiB/s) : min=%5lu, max=%5lu, per=%3.2f%%,"
246                          " avg=%5.02f, stdev=%5.02f\n", min, max, p_of_agg,
247                                                         mean, dev);
248         }
249 }
250
251 static void show_lat(double *io_u_lat, int nr, const char **ranges,
252                      const char *msg)
253 {
254         int new_line = 1, i, line = 0;
255
256         for (i = 0; i < nr; i++) {
257                 if (io_u_lat[i] <= 0.0)
258                         continue;
259                 if (new_line) {
260                         if (line)
261                                 log_info("\n");
262                         log_info("     lat (%s): ", msg);
263                         new_line = 0;
264                         line = 0;
265                 }
266                 if (line)
267                         log_info(", ");
268                 log_info("%s%3.2f%%", ranges[i], io_u_lat[i]);
269                 line++;
270                 if (line == 5)
271                         new_line = 1;
272         }
273 }
274
275 static void show_lat_u(double *io_u_lat_u)
276 {
277         const char *ranges[] = { "2=", "4=", "10=", "20=", "50=", "100=",
278                                  "250=", "500=", "750=", "1000=", };
279
280         show_lat(io_u_lat_u, FIO_IO_U_LAT_U_NR, ranges, "usec");
281 }
282
283 static void show_lat_m(double *io_u_lat_m)
284 {
285         const char *ranges[] = { "2=", "4=", "10=", "20=", "50=", "100=",
286                                  "250=", "500=", "750=", "1000=", "2000=",
287                                  ">=2000=", };
288
289         show_lat(io_u_lat_m, FIO_IO_U_LAT_M_NR, ranges, "msec");
290 }
291
292 static void show_latencies(double *io_u_lat_u, double *io_u_lat_m)
293 {
294         show_lat_u(io_u_lat_u);
295         log_info("\n");
296         show_lat_m(io_u_lat_m);
297         log_info("\n");
298 }
299
300 static void show_thread_status(struct thread_stat *ts,
301                                struct group_run_stats *rs)
302 {
303         double usr_cpu, sys_cpu;
304         unsigned long runtime;
305         double io_u_dist[FIO_IO_U_MAP_NR];
306         double io_u_lat_u[FIO_IO_U_LAT_U_NR];
307         double io_u_lat_m[FIO_IO_U_LAT_M_NR];
308
309         if (!(ts->io_bytes[0] + ts->io_bytes[1]) &&
310             !(ts->total_io_u[0] + ts->total_io_u[1]))
311                 return;
312
313         if (!ts->error) {
314                 log_info("%s: (groupid=%d, jobs=%d): err=%2d: pid=%d\n",
315                                         ts->name, ts->groupid, ts->members,
316                                         ts->error, ts->pid);
317         } else {
318                 log_info("%s: (groupid=%d, jobs=%d): err=%2d (%s): pid=%d\n",
319                                         ts->name, ts->groupid, ts->members,
320                                         ts->error, ts->verror, ts->pid);
321         }
322
323         if (ts->description)
324                 log_info("  Description  : [%s]\n", ts->description);
325
326         if (ts->io_bytes[DDIR_READ])
327                 show_ddir_status(rs, ts, DDIR_READ);
328         if (ts->io_bytes[DDIR_WRITE])
329                 show_ddir_status(rs, ts, DDIR_WRITE);
330
331         runtime = ts->total_run_time;
332         if (runtime) {
333                 double runt = (double) runtime;
334
335                 usr_cpu = (double) ts->usr_time * 100 / runt;
336                 sys_cpu = (double) ts->sys_time * 100 / runt;
337         } else {
338                 usr_cpu = 0;
339                 sys_cpu = 0;
340         }
341
342         log_info("  cpu          : usr=%3.2f%%, sys=%3.2f%%, ctx=%lu, majf=%lu,"
343                  " minf=%lu\n", usr_cpu, sys_cpu, ts->ctx, ts->majf, ts->minf);
344
345         stat_calc_dist(ts, io_u_dist);
346         stat_calc_lat_u(ts, io_u_lat_u);
347         stat_calc_lat_m(ts, io_u_lat_m);
348
349         log_info("  IO depths    : 1=%3.1f%%, 2=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%,"
350                  " 16=%3.1f%%, 32=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0],
351                                         io_u_dist[1], io_u_dist[2],
352                                         io_u_dist[3], io_u_dist[4],
353                                         io_u_dist[5], io_u_dist[6]);
354         log_info("     issued r/w: total=%lu/%lu, short=%lu/%lu\n",
355                                         ts->total_io_u[0], ts->total_io_u[1],
356                                         ts->short_io_u[0], ts->short_io_u[1]);
357
358         show_latencies(io_u_lat_u, io_u_lat_m);
359 }
360
361 static void show_ddir_status_terse(struct thread_stat *ts,
362                                    struct group_run_stats *rs, int ddir)
363 {
364         unsigned long min, max;
365         unsigned long long bw;
366         double mean, dev;
367
368         bw = 0;
369         if (ts->runtime[ddir])
370                 bw = ts->io_bytes[ddir] / ts->runtime[ddir];
371
372         log_info(";%llu;%llu;%lu", ts->io_bytes[ddir] >> 10, bw,
373                                                         ts->runtime[ddir]);
374
375         if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev))
376                 log_info(";%lu;%lu;%f;%f", min, max, mean, dev);
377         else
378                 log_info(";%lu;%lu;%f;%f", 0UL, 0UL, 0.0, 0.0);
379
380         if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev))
381                 log_info(";%lu;%lu;%f;%f", min, max, mean, dev);
382         else
383                 log_info(";%lu;%lu;%f;%f", 0UL, 0UL, 0.0, 0.0);
384
385         if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
386                 double p_of_agg;
387
388                 p_of_agg = mean * 100 / (double) rs->agg[ddir];
389                 log_info(";%lu;%lu;%f%%;%f;%f", min, max, p_of_agg, mean, dev);
390         } else
391                 log_info(";%lu;%lu;%f%%;%f;%f", 0UL, 0UL, 0.0, 0.0, 0.0);
392 }
393
394
395 static void show_thread_status_terse(struct thread_stat *ts,
396                                      struct group_run_stats *rs)
397 {
398         double io_u_dist[FIO_IO_U_MAP_NR];
399         double io_u_lat_u[FIO_IO_U_LAT_U_NR];
400         double io_u_lat_m[FIO_IO_U_LAT_M_NR];
401         double usr_cpu, sys_cpu;
402         int i;
403
404         log_info("%s;%d;%d", ts->name, ts->groupid, ts->error);
405
406         show_ddir_status_terse(ts, rs, 0);
407         show_ddir_status_terse(ts, rs, 1);
408
409         if (ts->total_run_time) {
410                 double runt = (double) ts->total_run_time;
411
412                 usr_cpu = (double) ts->usr_time * 100 / runt;
413                 sys_cpu = (double) ts->sys_time * 100 / runt;
414         } else {
415                 usr_cpu = 0;
416                 sys_cpu = 0;
417         }
418
419         log_info(";%f%%;%f%%;%lu;%lu;%lu", usr_cpu, sys_cpu, ts->ctx, ts->majf,
420                                                                 ts->minf);
421
422         stat_calc_dist(ts, io_u_dist);
423         stat_calc_lat_u(ts, io_u_lat_u);
424         stat_calc_lat_m(ts, io_u_lat_m);
425
426         log_info(";%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%",
427                         io_u_dist[0], io_u_dist[1], io_u_dist[2], io_u_dist[3],
428                         io_u_dist[4], io_u_dist[5], io_u_dist[6]);
429
430         for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
431                 log_info(";%3.2f%%", io_u_lat_u[i]);
432         for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
433                 log_info(";%3.2f%%", io_u_lat_m[i]);
434         log_info("\n");
435
436         if (ts->description)
437                 log_info(";%s", ts->description);
438
439         log_info("\n");
440 }
441
442 static void sum_stat(struct io_stat *dst, struct io_stat *src, int nr)
443 {
444         double mean, S;
445
446         dst->min_val = min(dst->min_val, src->min_val);
447         dst->max_val = max(dst->max_val, src->max_val);
448         dst->samples += src->samples;
449
450         /*
451          * Needs a new method for calculating stddev, we cannot just
452          * average them we do below for nr > 1
453          */
454         if (nr == 1) {
455                 mean = src->mean;
456                 S = src->S;
457         } else {
458                 mean = ((src->mean * (double) (nr - 1))
459                                 + dst->mean) / ((double) nr);
460                 S = ((src->S * (double) (nr - 1)) + dst->S) / ((double) nr);
461         }
462
463         dst->mean = mean;
464         dst->S = S;
465 }
466
467 void show_run_stats(void)
468 {
469         struct group_run_stats *runstats, *rs;
470         struct thread_data *td;
471         struct thread_stat *threadstats, *ts;
472         int i, j, k, l, nr_ts, last_ts, idx;
473
474         runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1));
475
476         for (i = 0; i < groupid + 1; i++) {
477                 rs = &runstats[i];
478
479                 memset(rs, 0, sizeof(*rs));
480                 rs->min_bw[0] = rs->min_run[0] = ~0UL;
481                 rs->min_bw[1] = rs->min_run[1] = ~0UL;
482         }
483
484         /*
485          * find out how many threads stats we need. if group reporting isn't
486          * enabled, it's one-per-td.
487          */
488         nr_ts = 0;
489         last_ts = -1;
490         for_each_td(td, i) {
491                 if (!td->o.group_reporting) {
492                         nr_ts++;
493                         continue;
494                 }
495                 if (last_ts == td->groupid)
496                         continue;
497
498                 last_ts = td->groupid;
499                 nr_ts++;
500         }
501
502         threadstats = malloc(nr_ts * sizeof(struct thread_stat));
503
504         for (i = 0; i < nr_ts; i++) {
505                 ts = &threadstats[i];
506
507                 memset(ts, 0, sizeof(*ts));
508                 for (j = 0; j <= DDIR_WRITE; j++) {
509                         ts->clat_stat[j].min_val = -1UL;
510                         ts->slat_stat[j].min_val = -1UL;
511                         ts->bw_stat[j].min_val = -1UL;
512                 }
513                 ts->groupid = -1;
514         }
515
516         j = 0;
517         last_ts = -1;
518         idx = 0;
519         for_each_td(td, i) {
520                 if (idx && (!td->o.group_reporting ||
521                     (td->o.group_reporting && last_ts != td->groupid))) {
522                         idx = 0;
523                         j++;
524                 }
525
526                 last_ts = td->groupid;
527
528                 ts = &threadstats[j];
529
530                 idx++;
531                 ts->members++;
532
533                 if (ts->groupid == -1) {
534                         /*
535                          * These are per-group shared already
536                          */
537                         ts->name = td->o.name;
538                         ts->description = td->o.description;
539                         ts->groupid = td->groupid;
540
541                         /*
542                          * first pid in group, not very useful...
543                          */
544                         ts->pid = td->pid;
545                 }
546
547                 if (td->error && !ts->error) {
548                         ts->error = td->error;
549                         ts->verror = td->verror;
550                 }
551
552                 for (l = 0; l <= DDIR_WRITE; l++) {
553                         sum_stat(&ts->clat_stat[l], &td->ts.clat_stat[l], idx);
554                         sum_stat(&ts->slat_stat[l], &td->ts.slat_stat[l], idx);
555                         sum_stat(&ts->bw_stat[l], &td->ts.bw_stat[l], idx);
556
557                         ts->stat_io_bytes[l] += td->ts.stat_io_bytes[l];
558                         ts->io_bytes[l] += td->ts.io_bytes[l];
559
560                         if (ts->runtime[l] < td->ts.runtime[l])
561                                 ts->runtime[l] = td->ts.runtime[l];
562                 }
563
564                 ts->usr_time += td->ts.usr_time;
565                 ts->sys_time += td->ts.sys_time;
566                 ts->ctx += td->ts.ctx;
567                 ts->majf += td->ts.majf;
568                 ts->minf += td->ts.minf;
569
570                 for (k = 0; k < FIO_IO_U_MAP_NR; k++)
571                         ts->io_u_map[k] += td->ts.io_u_map[k];
572                 for (k = 0; k < FIO_IO_U_LAT_U_NR; k++)
573                         ts->io_u_lat_u[k] += td->ts.io_u_lat_u[k];
574                 for (k = 0; k < FIO_IO_U_LAT_M_NR; k++)
575                         ts->io_u_lat_m[k] += td->ts.io_u_lat_m[k];
576
577
578                 for (k = 0; k <= DDIR_WRITE; k++) {
579                         ts->total_io_u[k] += td->ts.total_io_u[k];
580                         ts->short_io_u[k] += td->ts.short_io_u[k];
581                 }
582
583                 ts->total_run_time += td->ts.total_run_time;
584         }
585
586         for (i = 0; i < nr_ts; i++) {
587                 unsigned long long bw;
588
589                 ts = &threadstats[i];
590                 rs = &runstats[ts->groupid];
591
592                 for (j = 0; j <= DDIR_WRITE; j++) {
593                         if (!ts->runtime[j])
594                                 continue;
595                         if (ts->runtime[j] < rs->min_run[j] || !rs->min_run[j])
596                                 rs->min_run[j] = ts->runtime[j];
597                         if (ts->runtime[j] > rs->max_run[j])
598                                 rs->max_run[j] = ts->runtime[j];
599
600                         bw = 0;
601                         if (ts->runtime[j])
602                                 bw = ts->io_bytes[j]
603                                         / (unsigned long long) ts->runtime[j];
604                         if (bw < rs->min_bw[j])
605                                 rs->min_bw[j] = bw;
606                         if (bw > rs->max_bw[j])
607                                 rs->max_bw[j] = bw;
608
609                         rs->io_kb[j] += ts->io_bytes[j] >> 10;
610                 }
611         }
612
613         for (i = 0; i < groupid + 1; i++) {
614                 rs = &runstats[i];
615
616                 if (rs->max_run[0])
617                         rs->agg[0] = (rs->io_kb[0]*1024) / rs->max_run[0];
618                 if (rs->max_run[1])
619                         rs->agg[1] = (rs->io_kb[1]*1024) / rs->max_run[1];
620         }
621
622         /*
623          * don't overwrite last signal output
624          */
625         if (!terse_output)
626                 printf("\n");
627
628         for (i = 0; i < nr_ts; i++) {
629                 ts = &threadstats[i];
630                 rs = &runstats[ts->groupid];
631
632                 if (terse_output)
633                         show_thread_status_terse(ts, rs);
634                 else
635                         show_thread_status(ts, rs);
636         }
637
638         if (!terse_output) {
639                 for (i = 0; i < groupid + 1; i++)
640                         show_group_stats(&runstats[i], i);
641
642                 show_disk_util();
643         }
644
645         free(runstats);
646         free(threadstats);
647 }
648
649 static inline void add_stat_sample(struct io_stat *is, unsigned long data)
650 {
651         double val = data;
652         double delta;
653
654         if (data > is->max_val)
655                 is->max_val = data;
656         if (data < is->min_val)
657                 is->min_val = data;
658
659         delta = val - is->mean;
660         if (delta) {
661                 is->mean += delta / (is->samples + 1.0);
662                 is->S += delta * (val - is->mean);
663         }
664
665         is->samples++;
666 }
667
668 static void __add_log_sample(struct io_log *iolog, unsigned long val,
669                              enum fio_ddir ddir, unsigned long time)
670 {
671         if (iolog->nr_samples == iolog->max_samples) {
672                 int new_size = sizeof(struct io_sample) * iolog->max_samples*2;
673
674                 iolog->log = realloc(iolog->log, new_size);
675                 iolog->max_samples <<= 1;
676         }
677
678         iolog->log[iolog->nr_samples].val = val;
679         iolog->log[iolog->nr_samples].time = time;
680         iolog->log[iolog->nr_samples].ddir = ddir;
681         iolog->nr_samples++;
682 }
683
684 static void add_log_sample(struct thread_data *td, struct io_log *iolog,
685                            unsigned long val, enum fio_ddir ddir)
686 {
687         __add_log_sample(iolog, val, ddir, mtime_since_now(&td->epoch));
688 }
689
690 void add_agg_sample(unsigned long val, enum fio_ddir ddir)
691 {
692         struct io_log *iolog = agg_io_log[ddir];
693
694         __add_log_sample(iolog, val, ddir, mtime_since_genesis());
695 }
696
697 void add_clat_sample(struct thread_data *td, enum fio_ddir ddir,
698                      unsigned long usec)
699 {
700         struct thread_stat *ts = &td->ts;
701
702         add_stat_sample(&ts->clat_stat[ddir], usec);
703
704         if (ts->clat_log)
705                 add_log_sample(td, ts->clat_log, usec, ddir);
706 }
707
708 void add_slat_sample(struct thread_data *td, enum fio_ddir ddir,
709                      unsigned long usec)
710 {
711         struct thread_stat *ts = &td->ts;
712
713         add_stat_sample(&ts->slat_stat[ddir], usec);
714
715         if (ts->slat_log)
716                 add_log_sample(td, ts->slat_log, usec, ddir);
717 }
718
719 void add_bw_sample(struct thread_data *td, enum fio_ddir ddir,
720                    struct timeval *t)
721 {
722         struct thread_stat *ts = &td->ts;
723         unsigned long spent = mtime_since(&ts->stat_sample_time[ddir], t);
724         unsigned long rate;
725
726         if (spent < td->o.bw_avg_time)
727                 return;
728
729         rate = (td->this_io_bytes[ddir] - ts->stat_io_bytes[ddir]) / spent;
730         add_stat_sample(&ts->bw_stat[ddir], rate);
731
732         if (ts->bw_log)
733                 add_log_sample(td, ts->bw_log, rate, ddir);
734
735         fio_gettime(&ts->stat_sample_time[ddir], NULL);
736         ts->stat_io_bytes[ddir] = td->this_io_bytes[ddir];
737 }