Group reporting fixes
[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);
c0a6b0d8 14static dev_t last_dev;
3c39a379 15
dbe1125e 16/*
34403fb1 17 * Cheesy number->string conversion, complete with carry rounding error.
dbe1125e 18 */
b3605062 19static char *num2str(unsigned long num, int maxlen, int base, int pow2)
dbe1125e 20{
b3605062
JA
21 char postfix[] = { ' ', 'K', 'M', 'G', 'P', 'E' };
22 unsigned int thousand;
dbe1125e
JA
23 char *buf;
24 int i;
25
b3605062
JA
26 if (pow2)
27 thousand = 1024;
28 else
29 thousand = 1000;
30
dbe1125e
JA
31 buf = malloc(128);
32
33 for (i = 0; base > 1; i++)
34 base /= thousand;
35
36 do {
37 int len, carry = 0;
38
39 len = sprintf(buf, "%'lu", num);
40 if (len <= maxlen) {
b3605062
JA
41 if (i >= 1) {
42 buf[len] = postfix[i];
43 buf[len + 1] = '\0';
44 }
dbe1125e
JA
45 return buf;
46 }
47
48 if ((num % thousand) >= (thousand / 2))
49 carry = 1;
50
51 num /= thousand;
52 num += carry;
53 i++;
f3502ba2 54 } while (i <= 5);
dbe1125e
JA
55
56 return buf;
57}
58
3c39a379
JA
59static int get_io_ticks(struct disk_util *du, struct disk_util_stat *dus)
60{
61 unsigned in_flight;
62 char line[256];
63 FILE *f;
64 char *p;
65
66 f = fopen(du->path, "r");
67 if (!f)
68 return 1;
69
70 p = fgets(line, sizeof(line), f);
71 if (!p) {
72 fclose(f);
73 return 1;
74 }
75
76 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) {
77 fclose(f);
78 return 1;
79 }
80
81 fclose(f);
82 return 0;
83}
84
85static void update_io_tick_disk(struct disk_util *du)
86{
87 struct disk_util_stat __dus, *dus, *ldus;
88 struct timeval t;
89
90 if (get_io_ticks(du, &__dus))
91 return;
92
93 dus = &du->dus;
94 ldus = &du->last_dus;
95
96 dus->sectors[0] += (__dus.sectors[0] - ldus->sectors[0]);
97 dus->sectors[1] += (__dus.sectors[1] - ldus->sectors[1]);
98 dus->ios[0] += (__dus.ios[0] - ldus->ios[0]);
99 dus->ios[1] += (__dus.ios[1] - ldus->ios[1]);
100 dus->merges[0] += (__dus.merges[0] - ldus->merges[0]);
101 dus->merges[1] += (__dus.merges[1] - ldus->merges[1]);
102 dus->ticks[0] += (__dus.ticks[0] - ldus->ticks[0]);
103 dus->ticks[1] += (__dus.ticks[1] - ldus->ticks[1]);
104 dus->io_ticks += (__dus.io_ticks - ldus->io_ticks);
105 dus->time_in_queue += (__dus.time_in_queue - ldus->time_in_queue);
106
02bcaa8c 107 fio_gettime(&t, NULL);
3c39a379
JA
108 du->msec += mtime_since(&du->time, &t);
109 memcpy(&du->time, &t, sizeof(t));
110 memcpy(ldus, &__dus, sizeof(__dus));
111}
112
113void update_io_ticks(void)
114{
115 struct list_head *entry;
116 struct disk_util *du;
117
118 list_for_each(entry, &disk_list) {
119 du = list_entry(entry, struct disk_util, list);
120 update_io_tick_disk(du);
121 }
122}
123
124static int disk_util_exists(dev_t dev)
125{
126 struct list_head *entry;
127 struct disk_util *du;
128
129 list_for_each(entry, &disk_list) {
130 du = list_entry(entry, struct disk_util, list);
131
132 if (du->dev == dev)
133 return 1;
134 }
135
136 return 0;
137}
138
139static void disk_util_add(dev_t dev, char *path)
140{
1188caf8
JA
141 struct disk_util *du, *__du;
142 struct list_head *entry;
3c39a379 143
1188caf8 144 du = malloc(sizeof(*du));
3c39a379
JA
145 memset(du, 0, sizeof(*du));
146 INIT_LIST_HEAD(&du->list);
147 sprintf(du->path, "%s/stat", path);
148 du->name = strdup(basename(path));
149 du->dev = dev;
150
1188caf8
JA
151 list_for_each(entry, &disk_list) {
152 __du = list_entry(entry, struct disk_util, list);
153
154 if (!strcmp(du->name, __du->name)) {
155 free(du->name);
156 free(du);
157 return;
158 }
159 }
160
02bcaa8c 161 fio_gettime(&du->time, NULL);
3c39a379
JA
162 get_io_ticks(du, &du->last_dus);
163
164 list_add_tail(&du->list, &disk_list);
165}
166
167static int check_dev_match(dev_t dev, char *path)
168{
169 unsigned int major, minor;
170 char line[256], *p;
171 FILE *f;
172
173 f = fopen(path, "r");
174 if (!f) {
175 perror("open path");
176 return 1;
177 }
178
179 p = fgets(line, sizeof(line), f);
180 if (!p) {
181 fclose(f);
182 return 1;
183 }
184
185 if (sscanf(p, "%u:%u", &major, &minor) != 2) {
186 fclose(f);
187 return 1;
188 }
189
190 if (((major << 8) | minor) == dev) {
191 fclose(f);
192 return 0;
193 }
194
195 fclose(f);
196 return 1;
197}
198
199static int find_block_dir(dev_t dev, char *path)
200{
201 struct dirent *dir;
202 struct stat st;
203 int found = 0;
204 DIR *D;
205
206 D = opendir(path);
207 if (!D)
208 return 0;
209
210 while ((dir = readdir(D)) != NULL) {
211 char full_path[256];
212
213 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
214 continue;
3c39a379
JA
215
216 sprintf(full_path, "%s/%s", path, dir->d_name);
217
218 if (!strcmp(dir->d_name, "dev")) {
219 if (!check_dev_match(dev, full_path)) {
220 found = 1;
221 break;
222 }
223 }
224
3307d653 225 if (lstat(full_path, &st) == -1) {
3c39a379
JA
226 perror("stat");
227 break;
228 }
229
230 if (!S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode))
231 continue;
232
233 found = find_block_dir(dev, full_path);
234 if (found) {
235 strcpy(path, full_path);
236 break;
237 }
238 }
239
240 closedir(D);
241 return found;
242}
243
244void init_disk_util(struct thread_data *td)
245{
53cdc686 246 struct fio_file *f;
3c39a379 247 struct stat st;
0bbab0e7 248 char foo[PATH_MAX], tmp[PATH_MAX];
3c39a379
JA
249 dev_t dev;
250 char *p;
251
138502a9 252 if (!td->do_disk_util || (td->io_ops->flags & FIO_DISKLESSIO))
3c39a379
JA
253 return;
254
53cdc686
JA
255 /*
256 * Just use the same file, they are on the same device.
257 */
258 f = &td->files[0];
259 if (!stat(f->file_name, &st)) {
3c39a379
JA
260 if (S_ISBLK(st.st_mode))
261 dev = st.st_rdev;
262 else
263 dev = st.st_dev;
264 } else {
265 /*
266 * must be a file, open "." in that path
267 */
0bbab0e7 268 strncpy(foo, f->file_name, PATH_MAX - 1);
3c39a379
JA
269 p = dirname(foo);
270 if (stat(p, &st)) {
271 perror("disk util stat");
272 return;
273 }
274
275 dev = st.st_dev;
276 }
277
278 if (disk_util_exists(dev))
279 return;
c0a6b0d8
JA
280
281 /*
282 * for an fs without a device, we will repeatedly stat through
283 * sysfs which can take oodles of time for thousands of files. so
284 * cache the last lookup and compare with that before going through
285 * everything again.
286 */
287 if (dev == last_dev)
288 return;
289
290 last_dev = dev;
3c39a379
JA
291
292 sprintf(foo, "/sys/block");
293 if (!find_block_dir(dev, foo))
294 return;
295
296 /*
297 * If there's a ../queue/ directory there, we are inside a partition.
298 * Check if that is the case and jump back. For loop/md/dm etc we
299 * are already in the right spot.
300 */
301 sprintf(tmp, "%s/../queue", foo);
302 if (!stat(tmp, &st)) {
303 p = dirname(foo);
304 sprintf(tmp, "%s/queue", p);
305 if (stat(tmp, &st)) {
3b70d7e5 306 log_err("unknown sysfs layout\n");
3c39a379
JA
307 return;
308 }
0bbab0e7 309 strncpy(tmp, p, PATH_MAX - 1);
84585003 310 sprintf(foo, "%s", tmp);
3c39a379
JA
311 }
312
eecf272f
JA
313 if (td->ioscheduler)
314 td->sysfs_root = strdup(foo);
315
3c39a379
JA
316 disk_util_add(dev, foo);
317}
318
319void disk_util_timer_arm(void)
320{
321 itimer.it_value.tv_sec = 0;
322 itimer.it_value.tv_usec = DISK_UTIL_MSEC * 1000;
323 setitimer(ITIMER_REAL, &itimer, NULL);
324}
325
326void update_rusage_stat(struct thread_data *td)
327{
756867bd 328 struct thread_stat *ts = &td->ts;
3c39a379 329
079ad09b
JA
330 getrusage(RUSAGE_SELF, &ts->ru_end);
331
332 ts->usr_time += mtime_since(&ts->ru_start.ru_utime, &ts->ru_end.ru_utime);
333 ts->sys_time += mtime_since(&ts->ru_start.ru_stime, &ts->ru_end.ru_stime);
334 ts->ctx += ts->ru_end.ru_nvcsw + ts->ru_end.ru_nivcsw - (ts->ru_start.ru_nvcsw + ts->ru_start.ru_nivcsw);
3c39a379 335
079ad09b 336 memcpy(&ts->ru_start, &ts->ru_end, sizeof(ts->ru_end));
3c39a379
JA
337}
338
339static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max,
340 double *mean, double *dev)
341{
68704084 342 double n = is->samples;
3c39a379
JA
343
344 if (is->samples == 0)
345 return 0;
346
347 *min = is->min_val;
348 *max = is->max_val;
349
350 n = (double) is->samples;
68704084 351 *mean = is->mean;
e6d276f2 352
68704084
JA
353 if (n > 1.0)
354 *dev = sqrt(is->S / (n - 1.0));
ef9c5c40 355 else
68704084 356 *dev = -1.0;
ef9c5c40 357
3c39a379
JA
358 return 1;
359}
360
361static void show_group_stats(struct group_run_stats *rs, int id)
362{
dbe1125e
JA
363 char *p1, *p2, *p3, *p4;
364 const char *ddir_str[] = { " READ", " WRITE" };
365 int i;
366
eb8bbf48 367 fprintf(f_out, "\nRun status group %d (all jobs):\n", id);
3c39a379 368
dbe1125e
JA
369 for (i = 0; i <= DDIR_WRITE; i++) {
370 if (!rs->max_run[i])
371 continue;
372
b3605062
JA
373 p1 = num2str(rs->io_kb[i], 6, 1000, 1);
374 p2 = num2str(rs->agg[i], 6, 1000, 1);
375 p3 = num2str(rs->min_bw[i], 6, 1000, 1);
376 p4 = num2str(rs->max_bw[i], 6, 1000, 1);
dbe1125e 377
197574e4 378 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[i], rs->max_run[i]);
dbe1125e
JA
379
380 free(p1);
381 free(p2);
382 free(p3);
383 free(p4);
384 }
3c39a379
JA
385}
386
387static void show_disk_util(void)
388{
389 struct disk_util_stat *dus;
b4a6a59a 390 struct list_head *entry, *next;
3c39a379
JA
391 struct disk_util *du;
392 double util;
393
eb8bbf48 394 fprintf(f_out, "\nDisk stats (read/write):\n");
3c39a379
JA
395
396 list_for_each(entry, &disk_list) {
397 du = list_entry(entry, struct disk_util, list);
398 dus = &du->dus;
399
400 util = (double) 100 * du->dus.io_ticks / (double) du->msec;
401 if (util > 100.0)
402 util = 100.0;
403
eb8bbf48 404 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 405 }
b4a6a59a
JA
406
407 /*
408 * now free the list
409 */
410 list_for_each_safe(entry, next, &disk_list) {
411 list_del(entry);
412 du = list_entry(entry, struct disk_util, list);
413 free(du->name);
414 free(du);
415 }
3c39a379
JA
416}
417
b3605062
JA
418#define ts_total_io_u(ts) \
419 ((ts)->total_io_u[0] + (ts)->total_io_u[1])
420
2270890c
JA
421static void stat_calc_dist(struct thread_stat *ts, double *io_u_dist)
422{
423 int i;
424
425 /*
426 * Do depth distribution calculations
427 */
428 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
b3605062 429 io_u_dist[i] = (double) ts->io_u_map[i] / (double) ts_total_io_u(ts);
2270890c
JA
430 io_u_dist[i] *= 100.0;
431 }
432}
433
434static void stat_calc_lat(struct thread_stat *ts, double *io_u_lat)
435{
436 int i;
437
438 /*
439 * Do latency distribution calculations
440 */
441 for (i = 0; i < FIO_IO_U_LAT_NR; i++) {
b3605062 442 io_u_lat[i] = (double) ts->io_u_lat[i] / (double) ts_total_io_u(ts);
2270890c
JA
443 io_u_lat[i] *= 100.0;
444 }
445}
446
756867bd 447static void show_ddir_status(struct group_run_stats *rs, struct thread_stat *ts,
3c39a379
JA
448 int ddir)
449{
3c9b60c1 450 const char *ddir_str[] = { "read ", "write" };
3c39a379 451 unsigned long min, max;
b3605062 452 unsigned long long bw, iops;
3c39a379 453 double mean, dev;
b3605062 454 char *io_p, *bw_p, *iops_p;
3c39a379 455
756867bd 456 if (!ts->runtime[ddir])
3c39a379
JA
457 return;
458
756867bd 459 bw = ts->io_bytes[ddir] / ts->runtime[ddir];
b3605062
JA
460 iops = (1000 * ts->total_io_u[ddir]) / ts->runtime[ddir];
461 io_p = num2str(ts->io_bytes[ddir] >> 10, 6, 1000, 1);
462 bw_p = num2str(bw, 6, 1000, 1);
463 iops_p = num2str(iops, 6, 1, 0);
dbe1125e 464
b3605062 465 fprintf(f_out, " %s: io=%siB, bw=%siB/s, iops=%s, runt=%6lumsec\n", ddir_str[ddir], io_p, bw_p, iops_p, ts->runtime[ddir]);
dbe1125e
JA
466
467 free(io_p);
468 free(bw_p);
b3605062 469 free(iops_p);
3c39a379 470
079ad09b 471 if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev))
6104ddb6 472 fprintf(f_out, " slat (msec): min=%5lu, max=%5lu, avg=%5.02f, stdev=%5.02f\n", min, max, mean, dev);
3c39a379 473
079ad09b 474 if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev))
6104ddb6 475 fprintf(f_out, " clat (msec): min=%5lu, max=%5lu, avg=%5.02f, stdev=%5.02f\n", min, max, mean, dev);
3c39a379 476
079ad09b 477 if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
3c39a379
JA
478 double p_of_agg;
479
480 p_of_agg = mean * 100 / (double) rs->agg[ddir];
6104ddb6 481 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
482 }
483}
484
756867bd 485static void show_thread_status(struct thread_stat *ts,
3c39a379
JA
486 struct group_run_stats *rs)
487{
488 double usr_cpu, sys_cpu;
69008999 489 unsigned long runtime;
71619dc2 490 double io_u_dist[FIO_IO_U_MAP_NR];
ec118304 491 double io_u_lat[FIO_IO_U_LAT_NR];
3c39a379 492
756867bd 493 if (!(ts->io_bytes[0] + ts->io_bytes[1]))
3c39a379
JA
494 return;
495
756867bd 496 if (!ts->error)
6586ee89 497 fprintf(f_out, "%s: (groupid=%d, jobs=%d): err=%2d: pid=%d\n", ts->name, ts->groupid, ts->members, ts->error, ts->pid);
6d663077 498 else
6586ee89 499 fprintf(f_out, "%s: (groupid=%d, jobs=%d): err=%2d (%s): pid=%d\n", ts->name, ts->groupid, ts->members, ts->error, ts->verror, ts->pid);
3c39a379 500
7bdce1bd
JA
501 if (ts->description)
502 fprintf(f_out, " Description : [%s]\n", ts->description);
503
756867bd
JA
504 if (ts->io_bytes[DDIR_READ])
505 show_ddir_status(rs, ts, DDIR_READ);
506 if (ts->io_bytes[DDIR_WRITE])
507 show_ddir_status(rs, ts, DDIR_WRITE);
3c39a379 508
756867bd 509 runtime = ts->total_run_time;
69008999 510 if (runtime) {
1e97cce9 511 double runt = (double) runtime;
3c39a379 512
756867bd
JA
513 usr_cpu = (double) ts->usr_time * 100 / runt;
514 sys_cpu = (double) ts->sys_time * 100 / runt;
3c39a379
JA
515 } else {
516 usr_cpu = 0;
517 sys_cpu = 0;
518 }
519
756867bd 520 fprintf(f_out, " cpu : usr=%3.2f%%, sys=%3.2f%%, ctx=%lu\n", usr_cpu, sys_cpu, ts->ctx);
71619dc2 521
2270890c
JA
522 stat_calc_dist(ts, io_u_dist);
523 stat_calc_lat(ts, io_u_lat);
71619dc2 524
5d458077 525 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 526
8abdce66
JA
527 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]);
528 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]);
3c39a379
JA
529}
530
756867bd 531static void show_ddir_status_terse(struct thread_stat *ts,
c6ae0a5b
JA
532 struct group_run_stats *rs, int ddir)
533{
534 unsigned long min, max;
535 unsigned long long bw;
536 double mean, dev;
537
538 bw = 0;
756867bd
JA
539 if (ts->runtime[ddir])
540 bw = ts->io_bytes[ddir] / ts->runtime[ddir];
c6ae0a5b 541
6af019c9 542 fprintf(f_out, ";%llu;%llu;%lu", ts->io_bytes[ddir] >> 10, bw, ts->runtime[ddir]);
c6ae0a5b 543
079ad09b 544 if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev))
6af019c9 545 fprintf(f_out, ";%lu;%lu;%f;%f", min, max, mean, dev);
c6ae0a5b 546 else
6af019c9 547 fprintf(f_out, ";%lu;%lu;%f;%f", 0UL, 0UL, 0.0, 0.0);
c6ae0a5b 548
079ad09b 549 if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev))
6af019c9 550 fprintf(f_out, ";%lu;%lu;%f;%f", min, max, mean, dev);
c6ae0a5b 551 else
6af019c9 552 fprintf(f_out, ";%lu;%lu;%f;%f", 0UL, 0UL, 0.0, 0.0);
c6ae0a5b 553
079ad09b 554 if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
c6ae0a5b
JA
555 double p_of_agg;
556
557 p_of_agg = mean * 100 / (double) rs->agg[ddir];
6af019c9 558 fprintf(f_out, ";%lu;%lu;%f%%;%f;%f", min, max, p_of_agg, mean, dev);
c6ae0a5b 559 } else
6af019c9 560 fprintf(f_out, ";%lu;%lu;%f%%;%f;%f", 0UL, 0UL, 0.0, 0.0, 0.0);
c6ae0a5b
JA
561}
562
563
756867bd 564static void show_thread_status_terse(struct thread_stat *ts,
c6ae0a5b
JA
565 struct group_run_stats *rs)
566{
2270890c
JA
567 double io_u_dist[FIO_IO_U_MAP_NR];
568 double io_u_lat[FIO_IO_U_LAT_NR];
c6ae0a5b
JA
569 double usr_cpu, sys_cpu;
570
6af019c9 571 fprintf(f_out, "%s;%d;%d", ts->name, ts->groupid, ts->error);
c6ae0a5b 572
756867bd
JA
573 show_ddir_status_terse(ts, rs, 0);
574 show_ddir_status_terse(ts, rs, 1);
c6ae0a5b 575
756867bd
JA
576 if (ts->total_run_time) {
577 double runt = (double) ts->total_run_time;
c6ae0a5b 578
756867bd
JA
579 usr_cpu = (double) ts->usr_time * 100 / runt;
580 sys_cpu = (double) ts->sys_time * 100 / runt;
c6ae0a5b
JA
581 } else {
582 usr_cpu = 0;
583 sys_cpu = 0;
584 }
585
6af019c9 586 fprintf(f_out, ";%f%%;%f%%;%lu", usr_cpu, sys_cpu, ts->ctx);
2270890c
JA
587
588 stat_calc_dist(ts, io_u_dist);
589 stat_calc_lat(ts, io_u_lat);
590
6af019c9 591 fprintf(f_out, ";%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%", 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]);
2270890c 592
6af019c9
JA
593 fprintf(f_out, ";%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%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]);
594 fprintf(f_out, ";%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%;%3.1f%%", io_u_lat[6], io_u_lat[7], io_u_lat[8], io_u_lat[9], io_u_lat[10]);
2270890c
JA
595
596 if (ts->description)
6af019c9 597 fprintf(f_out, ";%s", ts->description);
2270890c
JA
598
599 fprintf(f_out, "\n");
756867bd
JA
600}
601
197574e4 602static void sum_stat(struct io_stat *dst, struct io_stat *src, int nr)
756867bd
JA
603{
604 double mean, S;
605
606 dst->min_val = min(dst->min_val, src->min_val);
607 dst->max_val = max(dst->max_val, src->max_val);
608 dst->samples += src->samples;
609
610 /*
611 * Needs a new method for calculating stddev, we cannot just
612 * average them we do below for nr > 1
613 */
614 if (nr == 1) {
615 mean = src->mean;
616 S = src->S;
617 } else {
c39cced6
JA
618 mean = ((src->mean * (double) (nr - 1)) + dst->mean) / ((double) nr);
619 S = ((src->S * (double) (nr - 1)) + dst->S) / ((double) nr);
756867bd
JA
620 }
621
622 dst->mean = mean;
623 dst->S = S;
624}
625
3c39a379
JA
626void show_run_stats(void)
627{
628 struct group_run_stats *runstats, *rs;
629 struct thread_data *td;
756867bd 630 struct thread_stat *threadstats, *ts;
197574e4 631 int i, j, k, l, nr_ts, last_ts, idx;
3c39a379
JA
632
633 runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1));
634
635 for (i = 0; i < groupid + 1; i++) {
636 rs = &runstats[i];
637
638 memset(rs, 0, sizeof(*rs));
639 rs->min_bw[0] = rs->min_run[0] = ~0UL;
640 rs->min_bw[1] = rs->min_run[1] = ~0UL;
641 }
642
756867bd
JA
643 /*
644 * find out how many threads stats we need. if group reporting isn't
645 * enabled, it's one-per-td.
646 */
647 nr_ts = 0;
648 last_ts = -1;
649 for_each_td(td, i) {
650 if (!td->group_reporting) {
651 nr_ts++;
652 continue;
653 }
654 if (last_ts == td->groupid)
655 continue;
656
657 last_ts = td->groupid;
658 nr_ts++;
659 }
660
661 threadstats = malloc(nr_ts * sizeof(struct thread_stat));
662
663 for (i = 0; i < nr_ts; i++) {
664 ts = &threadstats[i];
665
666 memset(ts, 0, sizeof(*ts));
de64df05 667 for (j = 0; j <= DDIR_WRITE; j++) {
197574e4
JA
668 ts->clat_stat[j].min_val = -1UL;
669 ts->slat_stat[j].min_val = -1UL;
670 ts->bw_stat[j].min_val = -1UL;
671 }
7abd0e3a 672 ts->groupid = -1;
756867bd
JA
673 }
674
675 j = 0;
676 last_ts = -1;
197574e4 677 idx = 0;
34572e28 678 for_each_td(td, i) {
7abd0e3a
JA
679 if (idx && (!td->group_reporting ||
680 (td->group_reporting && last_ts != td->groupid))) {
681 idx = 0;
682 j++;
683 }
684
685 last_ts = td->groupid;
686
756867bd
JA
687 ts = &threadstats[j];
688
197574e4 689 idx++;
6586ee89 690 ts->members++;
756867bd 691
7abd0e3a 692 if (ts->groupid == -1) {
2dc84ba7
JA
693 /*
694 * These are per-group shared already
695 */
756867bd
JA
696 ts->name = td->name;
697 ts->description = td->description;
756867bd 698 ts->groupid = td->groupid;
2dc84ba7
JA
699
700 /*
701 * first pid in group, not very useful...
702 */
756867bd 703 ts->pid = td->pid;
2dc84ba7
JA
704 }
705
706 if (td->error && !ts->error) {
707 ts->error = td->error;
756867bd
JA
708 ts->verror = td->verror;
709 }
710
de64df05 711 for (l = 0; l <= DDIR_WRITE; l++) {
197574e4
JA
712 sum_stat(&ts->clat_stat[l], &td->ts.clat_stat[l], idx);
713 sum_stat(&ts->slat_stat[l], &td->ts.slat_stat[l], idx);
714 sum_stat(&ts->bw_stat[l], &td->ts.bw_stat[l], idx);
756867bd 715
197574e4
JA
716 ts->stat_io_bytes[l] += td->ts.stat_io_bytes[l];
717 ts->io_bytes[l] += td->ts.io_bytes[l];
718
719 if (ts->runtime[l] < td->ts.runtime[l])
720 ts->runtime[l] = td->ts.runtime[l];
721 }
756867bd
JA
722
723 ts->usr_time += td->ts.usr_time;
724 ts->sys_time += td->ts.sys_time;
725 ts->ctx += td->ts.ctx;
726
727 for (k = 0; k < FIO_IO_U_MAP_NR; k++)
728 ts->io_u_map[k] += td->ts.io_u_map[k];
729 for (k = 0; k < FIO_IO_U_LAT_NR; k++)
730 ts->io_u_lat[k] += td->ts.io_u_lat[k];
731
b3605062
JA
732 for (k = 0; k <= DDIR_WRITE; k++)
733 ts->total_io_u[k] += td->ts.total_io_u[k];
756867bd
JA
734
735 ts->total_run_time += td->ts.total_run_time;
756867bd
JA
736 }
737
738 for (i = 0; i < nr_ts; i++) {
94370ac4 739 unsigned long long bw;
3c39a379 740
756867bd
JA
741 ts = &threadstats[i];
742 rs = &runstats[ts->groupid];
3c39a379 743
de64df05 744 for (j = 0; j <= DDIR_WRITE; j++) {
94370ac4
JA
745 if (!ts->runtime[j])
746 continue;
747 if (ts->runtime[j] < rs->min_run[j] || !rs->min_run[j])
748 rs->min_run[j] = ts->runtime[j];
749 if (ts->runtime[j] > rs->max_run[j])
750 rs->max_run[j] = ts->runtime[j];
751
752 bw = 0;
753 if (ts->runtime[j])
754 bw = ts->io_bytes[j] / (unsigned long long) ts->runtime[j];
755 if (bw < rs->min_bw[j])
756 rs->min_bw[j] = bw;
757 if (bw > rs->max_bw[j])
758 rs->max_bw[j] = bw;
759
760 rs->io_kb[j] += ts->io_bytes[j] >> 10;
761 }
3c39a379
JA
762 }
763
764 for (i = 0; i < groupid + 1; i++) {
765 rs = &runstats[i];
766
767 if (rs->max_run[0])
768 rs->agg[0] = (rs->io_kb[0]*1024) / rs->max_run[0];
769 if (rs->max_run[1])
770 rs->agg[1] = (rs->io_kb[1]*1024) / rs->max_run[1];
771 }
772
773 /*
774 * don't overwrite last signal output
775 */
c6ae0a5b
JA
776 if (!terse_output)
777 printf("\n");
3c39a379 778
756867bd
JA
779 for (i = 0; i < nr_ts; i++) {
780 ts = &threadstats[i];
781 rs = &runstats[ts->groupid];
3c39a379 782
c6ae0a5b 783 if (terse_output)
756867bd 784 show_thread_status_terse(ts, rs);
c6ae0a5b 785 else
756867bd 786 show_thread_status(ts, rs);
3c39a379
JA
787 }
788
c6ae0a5b
JA
789 if (!terse_output) {
790 for (i = 0; i < groupid + 1; i++)
791 show_group_stats(&runstats[i], i);
3c39a379 792
c6ae0a5b
JA
793 show_disk_util();
794 }
eecf272f
JA
795
796 free(runstats);
756867bd 797 free(threadstats);
3c39a379
JA
798}
799
68704084 800static inline void add_stat_sample(struct io_stat *is, unsigned long data)
3c39a379 801{
68704084 802 double val = data;
6660cc67 803 double delta;
68704084
JA
804
805 if (data > is->max_val)
806 is->max_val = data;
807 if (data < is->min_val)
808 is->min_val = data;
809
810 delta = val - is->mean;
6660cc67 811 is->mean += delta / (is->samples + 1.0);
68704084 812 is->S += delta * (val - is->mean);
3c39a379 813
3c39a379
JA
814 is->samples++;
815}
816
bb3884d8
JA
817static void __add_log_sample(struct io_log *iolog, unsigned long val,
818 enum fio_ddir ddir, unsigned long time)
3c39a379
JA
819{
820 if (iolog->nr_samples == iolog->max_samples) {
821 int new_size = sizeof(struct io_sample) * iolog->max_samples*2;
822
823 iolog->log = realloc(iolog->log, new_size);
824 iolog->max_samples <<= 1;
825 }
826
827 iolog->log[iolog->nr_samples].val = val;
bb3884d8 828 iolog->log[iolog->nr_samples].time = time;
3c39a379
JA
829 iolog->log[iolog->nr_samples].ddir = ddir;
830 iolog->nr_samples++;
831}
832
bb3884d8
JA
833static void add_log_sample(struct thread_data *td, struct io_log *iolog,
834 unsigned long val, enum fio_ddir ddir)
835{
836 __add_log_sample(iolog, val, ddir, mtime_since_now(&td->epoch));
837}
838
839void add_agg_sample(unsigned long val, enum fio_ddir ddir)
840{
841 struct io_log *iolog = agg_io_log[ddir];
842
843 __add_log_sample(iolog, val, ddir, mtime_since_genesis());
844}
845
1e97cce9
JA
846void add_clat_sample(struct thread_data *td, enum fio_ddir ddir,
847 unsigned long msec)
3c39a379 848{
756867bd 849 struct thread_stat *ts = &td->ts;
079ad09b
JA
850
851 add_stat_sample(&ts->clat_stat[ddir], msec);
3c39a379 852
079ad09b
JA
853 if (ts->clat_log)
854 add_log_sample(td, ts->clat_log, msec, ddir);
3c39a379
JA
855}
856
1e97cce9
JA
857void add_slat_sample(struct thread_data *td, enum fio_ddir ddir,
858 unsigned long msec)
3c39a379 859{
756867bd 860 struct thread_stat *ts = &td->ts;
079ad09b
JA
861
862 add_stat_sample(&ts->slat_stat[ddir], msec);
3c39a379 863
079ad09b
JA
864 if (ts->slat_log)
865 add_log_sample(td, ts->slat_log, msec, ddir);
3c39a379
JA
866}
867
1e97cce9
JA
868void add_bw_sample(struct thread_data *td, enum fio_ddir ddir,
869 struct timeval *t)
3c39a379 870{
756867bd 871 struct thread_stat *ts = &td->ts;
079ad09b 872 unsigned long spent = mtime_since(&ts->stat_sample_time[ddir], t);
3c39a379
JA
873 unsigned long rate;
874
875 if (spent < td->bw_avg_time)
876 return;
877
079ad09b
JA
878 rate = (td->this_io_bytes[ddir] - ts->stat_io_bytes[ddir]) / spent;
879 add_stat_sample(&ts->bw_stat[ddir], rate);
3c39a379 880
079ad09b
JA
881 if (ts->bw_log)
882 add_log_sample(td, ts->bw_log, rate, ddir);
3c39a379 883
079ad09b
JA
884 fio_gettime(&ts->stat_sample_time[ddir], NULL);
885 ts->stat_io_bytes[ddir] = td->this_io_bytes[ddir];
3c39a379 886}