IO engine callback need not dump possible values
[fio.git] / stat.c
CommitLineData
3c39a379
JA
1#include <stdio.h>
2#include <string.h>
3#include <sys/time.h>
4#include <sys/types.h>
5c4e1dbc 5#include <sys/stat.h>
3c39a379
JA
6#include <dirent.h>
7#include <libgen.h>
8#include <math.h>
9
10#include "fio.h"
3c39a379
JA
11
12static struct itimerval itimer;
5c4e1dbc 13static struct list_head disk_list = LIST_HEAD_INIT(disk_list);
3c39a379 14
dbe1125e
JA
15/*
16 * Cheasy number->string conversion, complete with carry rounding error.
17 */
18static char *num2str(unsigned long num, int maxlen, int base)
19{
20 /*
21 * could be passed in for 10^3 base, but every caller expects
22 * 2^10 base right now.
23 */
c8c51efd 24 const unsigned int thousand = 1024;
f3502ba2 25 char postfix[] = { 'K', 'M', 'G', 'P', 'E' };
dbe1125e
JA
26 char *buf;
27 int i;
28
29 buf = malloc(128);
30
31 for (i = 0; base > 1; i++)
32 base /= thousand;
33
34 do {
35 int len, carry = 0;
36
37 len = sprintf(buf, "%'lu", num);
38 if (len <= maxlen) {
39 buf[len] = postfix[i];
40 buf[len + 1] = '\0';
41 return buf;
42 }
43
44 if ((num % thousand) >= (thousand / 2))
45 carry = 1;
46
47 num /= thousand;
48 num += carry;
49 i++;
f3502ba2 50 } while (i <= 5);
dbe1125e
JA
51
52 return buf;
53}
54
3c39a379
JA
55static int get_io_ticks(struct disk_util *du, struct disk_util_stat *dus)
56{
57 unsigned in_flight;
58 char line[256];
59 FILE *f;
60 char *p;
61
62 f = fopen(du->path, "r");
63 if (!f)
64 return 1;
65
66 p = fgets(line, sizeof(line), f);
67 if (!p) {
68 fclose(f);
69 return 1;
70 }
71
72 if (sscanf(p, "%u %u %llu %u %u %u %llu %u %u %u %u\n", &dus->ios[0], &dus->merges[0], &dus->sectors[0], &dus->ticks[0], &dus->ios[1], &dus->merges[1], &dus->sectors[1], &dus->ticks[1], &in_flight, &dus->io_ticks, &dus->time_in_queue) != 11) {
73 fclose(f);
74 return 1;
75 }
76
77 fclose(f);
78 return 0;
79}
80
81static void update_io_tick_disk(struct disk_util *du)
82{
83 struct disk_util_stat __dus, *dus, *ldus;
84 struct timeval t;
85
86 if (get_io_ticks(du, &__dus))
87 return;
88
89 dus = &du->dus;
90 ldus = &du->last_dus;
91
92 dus->sectors[0] += (__dus.sectors[0] - ldus->sectors[0]);
93 dus->sectors[1] += (__dus.sectors[1] - ldus->sectors[1]);
94 dus->ios[0] += (__dus.ios[0] - ldus->ios[0]);
95 dus->ios[1] += (__dus.ios[1] - ldus->ios[1]);
96 dus->merges[0] += (__dus.merges[0] - ldus->merges[0]);
97 dus->merges[1] += (__dus.merges[1] - ldus->merges[1]);
98 dus->ticks[0] += (__dus.ticks[0] - ldus->ticks[0]);
99 dus->ticks[1] += (__dus.ticks[1] - ldus->ticks[1]);
100 dus->io_ticks += (__dus.io_ticks - ldus->io_ticks);
101 dus->time_in_queue += (__dus.time_in_queue - ldus->time_in_queue);
102
02bcaa8c 103 fio_gettime(&t, NULL);
3c39a379
JA
104 du->msec += mtime_since(&du->time, &t);
105 memcpy(&du->time, &t, sizeof(t));
106 memcpy(ldus, &__dus, sizeof(__dus));
107}
108
109void update_io_ticks(void)
110{
111 struct list_head *entry;
112 struct disk_util *du;
113
114 list_for_each(entry, &disk_list) {
115 du = list_entry(entry, struct disk_util, list);
116 update_io_tick_disk(du);
117 }
118}
119
120static int disk_util_exists(dev_t dev)
121{
122 struct list_head *entry;
123 struct disk_util *du;
124
125 list_for_each(entry, &disk_list) {
126 du = list_entry(entry, struct disk_util, list);
127
128 if (du->dev == dev)
129 return 1;
130 }
131
132 return 0;
133}
134
135static void disk_util_add(dev_t dev, char *path)
136{
1188caf8
JA
137 struct disk_util *du, *__du;
138 struct list_head *entry;
3c39a379 139
1188caf8 140 du = malloc(sizeof(*du));
3c39a379
JA
141 memset(du, 0, sizeof(*du));
142 INIT_LIST_HEAD(&du->list);
143 sprintf(du->path, "%s/stat", path);
144 du->name = strdup(basename(path));
145 du->dev = dev;
146
1188caf8
JA
147 list_for_each(entry, &disk_list) {
148 __du = list_entry(entry, struct disk_util, list);
149
150 if (!strcmp(du->name, __du->name)) {
151 free(du->name);
152 free(du);
153 return;
154 }
155 }
156
02bcaa8c 157 fio_gettime(&du->time, NULL);
3c39a379
JA
158 get_io_ticks(du, &du->last_dus);
159
160 list_add_tail(&du->list, &disk_list);
161}
162
163static int check_dev_match(dev_t dev, char *path)
164{
165 unsigned int major, minor;
166 char line[256], *p;
167 FILE *f;
168
169 f = fopen(path, "r");
170 if (!f) {
171 perror("open path");
172 return 1;
173 }
174
175 p = fgets(line, sizeof(line), f);
176 if (!p) {
177 fclose(f);
178 return 1;
179 }
180
181 if (sscanf(p, "%u:%u", &major, &minor) != 2) {
182 fclose(f);
183 return 1;
184 }
185
186 if (((major << 8) | minor) == dev) {
187 fclose(f);
188 return 0;
189 }
190
191 fclose(f);
192 return 1;
193}
194
195static int find_block_dir(dev_t dev, char *path)
196{
197 struct dirent *dir;
198 struct stat st;
199 int found = 0;
200 DIR *D;
201
202 D = opendir(path);
203 if (!D)
204 return 0;
205
206 while ((dir = readdir(D)) != NULL) {
207 char full_path[256];
208
209 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
210 continue;
3c39a379
JA
211
212 sprintf(full_path, "%s/%s", path, dir->d_name);
213
214 if (!strcmp(dir->d_name, "dev")) {
215 if (!check_dev_match(dev, full_path)) {
216 found = 1;
217 break;
218 }
219 }
220
3307d653 221 if (lstat(full_path, &st) == -1) {
3c39a379
JA
222 perror("stat");
223 break;
224 }
225
226 if (!S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode))
227 continue;
228
229 found = find_block_dir(dev, full_path);
230 if (found) {
231 strcpy(path, full_path);
232 break;
233 }
234 }
235
236 closedir(D);
237 return found;
238}
239
240void init_disk_util(struct thread_data *td)
241{
53cdc686 242 struct fio_file *f;
3c39a379 243 struct stat st;
0bbab0e7 244 char foo[PATH_MAX], tmp[PATH_MAX];
3c39a379
JA
245 dev_t dev;
246 char *p;
247
4d9345ae 248 if (!td->do_disk_util || (td->io_ops->flags & (FIO_NETIO | FIO_NULLIO)))
3c39a379
JA
249 return;
250
53cdc686
JA
251 /*
252 * Just use the same file, they are on the same device.
253 */
254 f = &td->files[0];
255 if (!stat(f->file_name, &st)) {
3c39a379
JA
256 if (S_ISBLK(st.st_mode))
257 dev = st.st_rdev;
258 else
259 dev = st.st_dev;
260 } else {
261 /*
262 * must be a file, open "." in that path
263 */
0bbab0e7 264 strncpy(foo, f->file_name, PATH_MAX - 1);
3c39a379
JA
265 p = dirname(foo);
266 if (stat(p, &st)) {
267 perror("disk util stat");
268 return;
269 }
270
271 dev = st.st_dev;
272 }
273
274 if (disk_util_exists(dev))
275 return;
276
277 sprintf(foo, "/sys/block");
278 if (!find_block_dir(dev, foo))
279 return;
280
281 /*
282 * If there's a ../queue/ directory there, we are inside a partition.
283 * Check if that is the case and jump back. For loop/md/dm etc we
284 * are already in the right spot.
285 */
286 sprintf(tmp, "%s/../queue", foo);
287 if (!stat(tmp, &st)) {
288 p = dirname(foo);
289 sprintf(tmp, "%s/queue", p);
290 if (stat(tmp, &st)) {
3b70d7e5 291 log_err("unknown sysfs layout\n");
3c39a379
JA
292 return;
293 }
0bbab0e7 294 strncpy(tmp, p, PATH_MAX - 1);
84585003 295 sprintf(foo, "%s", tmp);
3c39a379
JA
296 }
297
eecf272f
JA
298 if (td->ioscheduler)
299 td->sysfs_root = strdup(foo);
300
3c39a379
JA
301 disk_util_add(dev, foo);
302}
303
304void disk_util_timer_arm(void)
305{
306 itimer.it_value.tv_sec = 0;
307 itimer.it_value.tv_usec = DISK_UTIL_MSEC * 1000;
308 setitimer(ITIMER_REAL, &itimer, NULL);
309}
310
311void update_rusage_stat(struct thread_data *td)
312{
079ad09b 313 struct thread_stat *ts = &td->ts;
3c39a379 314
079ad09b
JA
315 getrusage(RUSAGE_SELF, &ts->ru_end);
316
317 ts->usr_time += mtime_since(&ts->ru_start.ru_utime, &ts->ru_end.ru_utime);
318 ts->sys_time += mtime_since(&ts->ru_start.ru_stime, &ts->ru_end.ru_stime);
319 ts->ctx += ts->ru_end.ru_nvcsw + ts->ru_end.ru_nivcsw - (ts->ru_start.ru_nvcsw + ts->ru_start.ru_nivcsw);
3c39a379 320
079ad09b 321 memcpy(&ts->ru_start, &ts->ru_end, sizeof(ts->ru_end));
3c39a379
JA
322}
323
324static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max,
325 double *mean, double *dev)
326{
68704084 327 double n = is->samples;
3c39a379
JA
328
329 if (is->samples == 0)
330 return 0;
331
332 *min = is->min_val;
333 *max = is->max_val;
334
335 n = (double) is->samples;
68704084 336 *mean = is->mean;
e6d276f2 337
68704084
JA
338 if (n > 1.0)
339 *dev = sqrt(is->S / (n - 1.0));
ef9c5c40 340 else
68704084 341 *dev = -1.0;
ef9c5c40 342
3c39a379
JA
343 return 1;
344}
345
346static void show_group_stats(struct group_run_stats *rs, int id)
347{
dbe1125e
JA
348 char *p1, *p2, *p3, *p4;
349 const char *ddir_str[] = { " READ", " WRITE" };
350 int i;
351
eb8bbf48 352 fprintf(f_out, "\nRun status group %d (all jobs):\n", id);
3c39a379 353
dbe1125e
JA
354 for (i = 0; i <= DDIR_WRITE; i++) {
355 if (!rs->max_run[i])
356 continue;
357
358 p1 = num2str(rs->io_kb[i], 6, 1);
359 p2 = num2str(rs->agg[i], 6, 1);
360 p3 = num2str(rs->min_bw[i], 6, 1);
361 p4 = num2str(rs->max_bw[i], 6, 1);
362
363 fprintf(f_out, "%s: io=%siB, aggrb=%siB/s, minb=%siB/s, maxb=%siB/s, mint=%llumsec, maxt=%llumsec\n", ddir_str[i], p1, p2, p3, p4, rs->min_run[0], rs->max_run[0]);
364
365 free(p1);
366 free(p2);
367 free(p3);
368 free(p4);
369 }
3c39a379
JA
370}
371
372static void show_disk_util(void)
373{
374 struct disk_util_stat *dus;
b4a6a59a 375 struct list_head *entry, *next;
3c39a379
JA
376 struct disk_util *du;
377 double util;
378
eb8bbf48 379 fprintf(f_out, "\nDisk stats (read/write):\n");
3c39a379
JA
380
381 list_for_each(entry, &disk_list) {
382 du = list_entry(entry, struct disk_util, list);
383 dus = &du->dus;
384
385 util = (double) 100 * du->dus.io_ticks / (double) du->msec;
386 if (util > 100.0)
387 util = 100.0;
388
eb8bbf48 389 fprintf(f_out, " %s: ios=%u/%u, merge=%u/%u, ticks=%u/%u, in_queue=%u, util=%3.2f%%\n", du->name, dus->ios[0], dus->ios[1], dus->merges[0], dus->merges[1], dus->ticks[0], dus->ticks[1], dus->time_in_queue, util);
3c39a379 390 }
b4a6a59a
JA
391
392 /*
393 * now free the list
394 */
395 list_for_each_safe(entry, next, &disk_list) {
396 list_del(entry);
397 du = list_entry(entry, struct disk_util, list);
398 free(du->name);
399 free(du);
400 }
3c39a379
JA
401}
402
403static void show_ddir_status(struct thread_data *td, struct group_run_stats *rs,
404 int ddir)
405{
3c9b60c1 406 const char *ddir_str[] = { "read ", "write" };
079ad09b 407 struct thread_stat *ts;
3c39a379
JA
408 unsigned long min, max;
409 unsigned long long bw;
410 double mean, dev;
dbe1125e 411 char *io_p, *bw_p;
3c39a379
JA
412
413 if (!td->runtime[ddir])
414 return;
415
416 bw = td->io_bytes[ddir] / td->runtime[ddir];
dbe1125e
JA
417 io_p = num2str(td->io_bytes[ddir] >> 10, 6, 1);
418 bw_p = num2str(bw, 6, 1);
419
420 fprintf(f_out, " %s: io=%siB, bw=%siB/s, runt=%6lumsec\n", ddir_str[ddir], io_p, bw_p, td->runtime[ddir]);
421
422 free(io_p);
423 free(bw_p);
3c39a379 424
079ad09b
JA
425 ts = &td->ts;
426 if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev))
6104ddb6 427 fprintf(f_out, " slat (msec): min=%5lu, max=%5lu, avg=%5.02f, stdev=%5.02f\n", min, max, mean, dev);
3c39a379 428
079ad09b 429 if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev))
6104ddb6 430 fprintf(f_out, " clat (msec): min=%5lu, max=%5lu, avg=%5.02f, stdev=%5.02f\n", min, max, mean, dev);
3c39a379 431
079ad09b 432 if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
3c39a379
JA
433 double p_of_agg;
434
435 p_of_agg = mean * 100 / (double) rs->agg[ddir];
6104ddb6 436 fprintf(f_out, " bw (KiB/s) : min=%5lu, max=%5lu, per=%3.2f%%, avg=%5.02f, stdev=%5.02f\n", min, max, p_of_agg, mean, dev);
3c39a379
JA
437 }
438}
439
440static void show_thread_status(struct thread_data *td,
441 struct group_run_stats *rs)
442{
443 double usr_cpu, sys_cpu;
69008999 444 unsigned long runtime;
71619dc2 445 double io_u_dist[FIO_IO_U_MAP_NR];
ec118304 446 double io_u_lat[FIO_IO_U_LAT_NR];
71619dc2 447 int i;
3c39a379 448
6d663077 449 if (!(td->io_bytes[0] + td->io_bytes[1]))
3c39a379
JA
450 return;
451
6d663077
JA
452 if (!td->error)
453 fprintf(f_out, "%s: (groupid=%d): err=%2d: pid=%d\n",td->name, td->groupid, td->error, td->pid);
454 else
455 fprintf(f_out, "%s: (groupid=%d): err=%2d (%s): pid=%d\n",td->name, td->groupid, td->error, td->verror, td->pid);
3c39a379 456
413dd459
JA
457 if (td_read(td))
458 show_ddir_status(td, rs, DDIR_READ);
459 if (td_write(td))
460 show_ddir_status(td, rs, DDIR_WRITE);
3c39a379 461
69008999
JA
462 runtime = mtime_since(&td->epoch, &td->end_time);
463 if (runtime) {
1e97cce9 464 double runt = (double) runtime;
3c39a379 465
079ad09b
JA
466 usr_cpu = (double) td->ts.usr_time * 100 / runt;
467 sys_cpu = (double) td->ts.sys_time * 100 / runt;
3c39a379
JA
468 } else {
469 usr_cpu = 0;
470 sys_cpu = 0;
471 }
472
079ad09b 473 fprintf(f_out, " cpu : usr=%3.2f%%, sys=%3.2f%%, ctx=%lu\n", usr_cpu, sys_cpu, td->ts.ctx);
71619dc2
JA
474
475 /*
476 * Do depth distribution calculations
477 */
478 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
479 io_u_dist[i] = (double) td->io_u_map[i] / (double) td->total_io_u;
480 io_u_dist[i] *= 100.0;
481 }
482
5d458077 483 fprintf(f_out, " IO depths : 1=%3.1f%%, 2=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%, 16=%3.1f%%, 32=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0], io_u_dist[1], io_u_dist[2], io_u_dist[3], io_u_dist[4], io_u_dist[5], io_u_dist[6]);
61697c37 484
ec118304
JA
485 /*
486 * Do latency distribution calculations
487 */
488 for (i = 0; i < FIO_IO_U_LAT_NR; i++) {
489 io_u_lat[i] = (double) td->io_u_lat[i] / (double) td->total_io_u;
490 io_u_lat[i] *= 100.0;
491 }
492
8abdce66
JA
493 fprintf(f_out, " lat (msec): 2=%3.1f%%, 4=%3.1f%%, 10=%3.1f%%, 20=%3.1f%%, 50=%3.1f%%, 100=%3.1f%%\n", io_u_lat[0], io_u_lat[1], io_u_lat[2], io_u_lat[3], io_u_lat[4], io_u_lat[5]);
494 fprintf(f_out, " lat (msec): 250=%3.1f%%, 500=%3.1f%%, 750=%3.1f%%, 1000=%3.1f%%, >=2000=%3.1f%%\n", io_u_lat[6], io_u_lat[7], io_u_lat[8], io_u_lat[9], io_u_lat[10]);
ec118304 495
61697c37
JA
496 if (td->description)
497 fprintf(f_out, "%s\n", td->description);
3c39a379
JA
498}
499
c6ae0a5b
JA
500static void show_ddir_status_terse(struct thread_data *td,
501 struct group_run_stats *rs, int ddir)
502{
079ad09b 503 struct thread_stat *ts = &td->ts;
c6ae0a5b
JA
504 unsigned long min, max;
505 unsigned long long bw;
506 double mean, dev;
507
508 bw = 0;
509 if (td->runtime[ddir])
510 bw = td->io_bytes[ddir] / td->runtime[ddir];
511
512 fprintf(f_out, ",%llu,%llu,%lu", td->io_bytes[ddir] >> 10, bw, td->runtime[ddir]);
513
079ad09b 514 if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev))
c6ae0a5b
JA
515 fprintf(f_out, ",%lu,%lu,%f,%f", min, max, mean, dev);
516 else
517 fprintf(f_out, ",%lu,%lu,%f,%f", 0UL, 0UL, 0.0, 0.0);
518
079ad09b 519 if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev))
c6ae0a5b
JA
520 fprintf(f_out, ",%lu,%lu,%f,%f", min, max, mean, dev);
521 else
522 fprintf(f_out, ",%lu,%lu,%f,%f", 0UL, 0UL, 0.0, 0.0);
523
079ad09b 524 if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
c6ae0a5b
JA
525 double p_of_agg;
526
527 p_of_agg = mean * 100 / (double) rs->agg[ddir];
528 fprintf(f_out, ",%lu,%lu,%f%%,%f,%f", min, max, p_of_agg, mean, dev);
529 } else
530 fprintf(f_out, ",%lu,%lu,%f%%,%f,%f", 0UL, 0UL, 0.0, 0.0, 0.0);
531
532}
533
534
535static void show_thread_status_terse(struct thread_data *td,
536 struct group_run_stats *rs)
537{
538 double usr_cpu, sys_cpu;
539
540 fprintf(f_out, "%s,%d,%d",td->name, td->groupid, td->error);
541
542 show_ddir_status_terse(td, rs, 0);
543 show_ddir_status_terse(td, rs, 1);
544
545 if (td->runtime[0] + td->runtime[1]) {
1e97cce9 546 double runt = (double) (td->runtime[0] + td->runtime[1]);
c6ae0a5b 547
079ad09b
JA
548 usr_cpu = (double) td->ts.usr_time * 100 / runt;
549 sys_cpu = (double) td->ts.sys_time * 100 / runt;
c6ae0a5b
JA
550 } else {
551 usr_cpu = 0;
552 sys_cpu = 0;
553 }
554
079ad09b 555 fprintf(f_out, ",%f%%,%f%%,%lu\n", usr_cpu, sys_cpu, td->ts.ctx);
c6ae0a5b
JA
556}
557
3c39a379
JA
558void show_run_stats(void)
559{
560 struct group_run_stats *runstats, *rs;
561 struct thread_data *td;
562 int i;
563
564 runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1));
565
566 for (i = 0; i < groupid + 1; i++) {
567 rs = &runstats[i];
568
569 memset(rs, 0, sizeof(*rs));
570 rs->min_bw[0] = rs->min_run[0] = ~0UL;
571 rs->min_bw[1] = rs->min_run[1] = ~0UL;
572 }
573
34572e28 574 for_each_td(td, i) {
3c39a379
JA
575 unsigned long long rbw, wbw;
576
3c39a379
JA
577 rs = &runstats[td->groupid];
578
579 if (td->runtime[0] < rs->min_run[0] || !rs->min_run[0])
580 rs->min_run[0] = td->runtime[0];
581 if (td->runtime[0] > rs->max_run[0])
582 rs->max_run[0] = td->runtime[0];
583 if (td->runtime[1] < rs->min_run[1] || !rs->min_run[1])
584 rs->min_run[1] = td->runtime[1];
585 if (td->runtime[1] > rs->max_run[1])
586 rs->max_run[1] = td->runtime[1];
587
588 rbw = wbw = 0;
589 if (td->runtime[0])
590 rbw = td->io_bytes[0] / (unsigned long long) td->runtime[0];
591 if (td->runtime[1])
592 wbw = td->io_bytes[1] / (unsigned long long) td->runtime[1];
593
594 if (rbw < rs->min_bw[0])
595 rs->min_bw[0] = rbw;
596 if (wbw < rs->min_bw[1])
597 rs->min_bw[1] = wbw;
598 if (rbw > rs->max_bw[0])
599 rs->max_bw[0] = rbw;
600 if (wbw > rs->max_bw[1])
601 rs->max_bw[1] = wbw;
602
603 rs->io_kb[0] += td->io_bytes[0] >> 10;
604 rs->io_kb[1] += td->io_bytes[1] >> 10;
605 }
606
607 for (i = 0; i < groupid + 1; i++) {
608 rs = &runstats[i];
609
610 if (rs->max_run[0])
611 rs->agg[0] = (rs->io_kb[0]*1024) / rs->max_run[0];
612 if (rs->max_run[1])
613 rs->agg[1] = (rs->io_kb[1]*1024) / rs->max_run[1];
614 }
615
616 /*
617 * don't overwrite last signal output
618 */
c6ae0a5b
JA
619 if (!terse_output)
620 printf("\n");
3c39a379 621
34572e28 622 for_each_td(td, i) {
3c39a379
JA
623 rs = &runstats[td->groupid];
624
c6ae0a5b
JA
625 if (terse_output)
626 show_thread_status_terse(td, rs);
627 else
628 show_thread_status(td, rs);
3c39a379
JA
629 }
630
c6ae0a5b
JA
631 if (!terse_output) {
632 for (i = 0; i < groupid + 1; i++)
633 show_group_stats(&runstats[i], i);
3c39a379 634
c6ae0a5b
JA
635 show_disk_util();
636 }
eecf272f
JA
637
638 free(runstats);
3c39a379
JA
639}
640
68704084 641static inline void add_stat_sample(struct io_stat *is, unsigned long data)
3c39a379 642{
68704084
JA
643 double val = data;
644 double delta, n;
645
646 if (data > is->max_val)
647 is->max_val = data;
648 if (data < is->min_val)
649 is->min_val = data;
650
651 delta = val - is->mean;
652 n = is->samples + 1.0;
653 is->mean += delta / n;
654 is->S += delta * (val - is->mean);
3c39a379 655
3c39a379
JA
656 is->samples++;
657}
658
bb3884d8
JA
659static void __add_log_sample(struct io_log *iolog, unsigned long val,
660 enum fio_ddir ddir, unsigned long time)
3c39a379
JA
661{
662 if (iolog->nr_samples == iolog->max_samples) {
663 int new_size = sizeof(struct io_sample) * iolog->max_samples*2;
664
665 iolog->log = realloc(iolog->log, new_size);
666 iolog->max_samples <<= 1;
667 }
668
669 iolog->log[iolog->nr_samples].val = val;
bb3884d8 670 iolog->log[iolog->nr_samples].time = time;
3c39a379
JA
671 iolog->log[iolog->nr_samples].ddir = ddir;
672 iolog->nr_samples++;
673}
674
bb3884d8
JA
675static void add_log_sample(struct thread_data *td, struct io_log *iolog,
676 unsigned long val, enum fio_ddir ddir)
677{
678 __add_log_sample(iolog, val, ddir, mtime_since_now(&td->epoch));
679}
680
681void add_agg_sample(unsigned long val, enum fio_ddir ddir)
682{
683 struct io_log *iolog = agg_io_log[ddir];
684
685 __add_log_sample(iolog, val, ddir, mtime_since_genesis());
686}
687
1e97cce9
JA
688void add_clat_sample(struct thread_data *td, enum fio_ddir ddir,
689 unsigned long msec)
3c39a379 690{
079ad09b
JA
691 struct thread_stat *ts = &td->ts;
692
693 add_stat_sample(&ts->clat_stat[ddir], msec);
3c39a379 694
079ad09b
JA
695 if (ts->clat_log)
696 add_log_sample(td, ts->clat_log, msec, ddir);
3c39a379
JA
697}
698
1e97cce9
JA
699void add_slat_sample(struct thread_data *td, enum fio_ddir ddir,
700 unsigned long msec)
3c39a379 701{
079ad09b
JA
702 struct thread_stat *ts = &td->ts;
703
704 add_stat_sample(&ts->slat_stat[ddir], msec);
3c39a379 705
079ad09b
JA
706 if (ts->slat_log)
707 add_log_sample(td, ts->slat_log, msec, ddir);
3c39a379
JA
708}
709
1e97cce9
JA
710void add_bw_sample(struct thread_data *td, enum fio_ddir ddir,
711 struct timeval *t)
3c39a379 712{
079ad09b
JA
713 struct thread_stat *ts = &td->ts;
714 unsigned long spent = mtime_since(&ts->stat_sample_time[ddir], t);
3c39a379
JA
715 unsigned long rate;
716
717 if (spent < td->bw_avg_time)
718 return;
719
079ad09b
JA
720 rate = (td->this_io_bytes[ddir] - ts->stat_io_bytes[ddir]) / spent;
721 add_stat_sample(&ts->bw_stat[ddir], rate);
3c39a379 722
079ad09b
JA
723 if (ts->bw_log)
724 add_log_sample(td, ts->bw_log, rate, ddir);
3c39a379 725
079ad09b
JA
726 fio_gettime(&ts->stat_sample_time[ddir], NULL);
727 ts->stat_io_bytes[ddir] = td->this_io_bytes[ddir];
3c39a379 728}