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