sg engine: do type check only when sd has been setup
[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 static struct itimerval itimer;
13 static struct list_head disk_list = LIST_HEAD_INIT(disk_list);
14 static dev_t last_dev;
15
16 /*
17  * Cheesy number->string conversion, complete with carry rounding error.
18  */
19 static char *num2str(unsigned long num, int maxlen, int base, int pow2)
20 {
21         char postfix[] = { ' ', 'K', 'M', 'G', 'P', 'E' };
22         unsigned int thousand;
23         char *buf;
24         int i;
25
26         if (pow2)
27                 thousand = 1024;
28         else
29                 thousand = 1000;
30
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) {
41                         if (i >= 1) {
42                                 buf[len] = postfix[i];
43                                 buf[len + 1] = '\0';
44                         }
45                         return buf;
46                 }
47
48                 if ((num % thousand) >= (thousand / 2))
49                         carry = 1;
50
51                 num /= thousand;
52                 num += carry;
53                 i++;
54         } while (i <= 5);
55
56         return buf;
57 }
58
59 static 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
85 static 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
107         fio_gettime(&t, NULL);
108         du->msec += mtime_since(&du->time, &t);
109         memcpy(&du->time, &t, sizeof(t));
110         memcpy(ldus, &__dus, sizeof(__dus));
111 }
112
113 void 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
124 static 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
139 static void disk_util_add(dev_t dev, char *path)
140 {
141         struct disk_util *du, *__du;
142         struct list_head *entry;
143
144         du = malloc(sizeof(*du));
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
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
161         fio_gettime(&du->time, NULL);
162         get_io_ticks(du, &du->last_dus);
163
164         list_add_tail(&du->list, &disk_list);
165 }
166
167 static 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
199 static 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;
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
225                 if (lstat(full_path, &st) == -1) {
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
244 static void __init_disk_util(struct thread_data *td, struct fio_file *f)
245 {
246         struct stat st;
247         char foo[PATH_MAX], tmp[PATH_MAX];
248         dev_t dev;
249         char *p;
250
251         if (!stat(f->file_name, &st)) {
252                 if (S_ISBLK(st.st_mode))
253                         dev = st.st_rdev;
254                 else
255                         dev = st.st_dev;
256         } else {
257                 /*
258                  * must be a file, open "." in that path
259                  */
260                 strncpy(foo, f->file_name, PATH_MAX - 1);
261                 p = dirname(foo);
262                 if (stat(p, &st)) {
263                         perror("disk util stat");
264                         return;
265                 }
266
267                 dev = st.st_dev;
268         }
269
270         if (disk_util_exists(dev))
271                 return;
272
273         /*
274          * for an fs without a device, we will repeatedly stat through
275          * sysfs which can take oodles of time for thousands of files. so
276          * cache the last lookup and compare with that before going through
277          * everything again.
278          */
279         if (dev == last_dev)
280                 return;
281
282         last_dev = dev;
283                 
284         sprintf(foo, "/sys/block");
285         if (!find_block_dir(dev, foo))
286                 return;
287
288         /*
289          * If there's a ../queue/ directory there, we are inside a partition.
290          * Check if that is the case and jump back. For loop/md/dm etc we
291          * are already in the right spot.
292          */
293         sprintf(tmp, "%s/../queue", foo);
294         if (!stat(tmp, &st)) {
295                 p = dirname(foo);
296                 sprintf(tmp, "%s/queue", p);
297                 if (stat(tmp, &st)) {
298                         log_err("unknown sysfs layout\n");
299                         return;
300                 }
301                 strncpy(tmp, p, PATH_MAX - 1);
302                 sprintf(foo, "%s", tmp);
303         }
304
305         if (td->o.ioscheduler && !td->sysfs_root)
306                 td->sysfs_root = strdup(foo);
307
308         disk_util_add(dev, foo);
309 }
310
311 void init_disk_util(struct thread_data *td)
312 {
313         struct fio_file *f;
314         unsigned int i;
315
316         if (!td->o.do_disk_util ||
317             (td->io_ops->flags & (FIO_DISKLESSIO | FIO_NODISKUTIL)))
318                 return;
319
320         for_each_file(td, f, i)
321                 __init_disk_util(td, f);
322 }
323
324 void disk_util_timer_arm(void)
325 {
326         itimer.it_value.tv_sec = 0;
327         itimer.it_value.tv_usec = DISK_UTIL_MSEC * 1000;
328         setitimer(ITIMER_REAL, &itimer, NULL);
329 }
330
331 void update_rusage_stat(struct thread_data *td)
332 {
333         struct thread_stat *ts = &td->ts;
334
335         getrusage(RUSAGE_SELF, &ts->ru_end);
336
337         ts->usr_time += mtime_since(&ts->ru_start.ru_utime, &ts->ru_end.ru_utime);
338         ts->sys_time += mtime_since(&ts->ru_start.ru_stime, &ts->ru_end.ru_stime);
339         ts->ctx += ts->ru_end.ru_nvcsw + ts->ru_end.ru_nivcsw - (ts->ru_start.ru_nvcsw + ts->ru_start.ru_nivcsw);
340         
341         memcpy(&ts->ru_start, &ts->ru_end, sizeof(ts->ru_end));
342 }
343
344 static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max,
345                     double *mean, double *dev)
346 {
347         double n = is->samples;
348
349         if (is->samples == 0)
350                 return 0;
351
352         *min = is->min_val;
353         *max = is->max_val;
354
355         n = (double) is->samples;
356         *mean = is->mean;
357
358         if (n > 1.0)
359                 *dev = sqrt(is->S / (n - 1.0));
360         else
361                 *dev = -1.0;
362
363         return 1;
364 }
365
366 static void show_group_stats(struct group_run_stats *rs, int id)
367 {
368         char *p1, *p2, *p3, *p4;
369         const char *ddir_str[] = { "   READ", "  WRITE" };
370         int i;
371
372         log_info("\nRun status group %d (all jobs):\n", id);
373
374         for (i = 0; i <= DDIR_WRITE; i++) {
375                 if (!rs->max_run[i])
376                         continue;
377
378                 p1 = num2str(rs->io_kb[i], 6, 1000, 1);
379                 p2 = num2str(rs->agg[i], 6, 1000, 1);
380                 p3 = num2str(rs->min_bw[i], 6, 1000, 1);
381                 p4 = num2str(rs->max_bw[i], 6, 1000, 1);
382
383                 log_info("%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]);
384
385                 free(p1);
386                 free(p2);
387                 free(p3);
388                 free(p4);
389         }
390 }
391
392 static void show_disk_util(void)
393 {
394         struct disk_util_stat *dus;
395         struct list_head *entry, *next;
396         struct disk_util *du;
397         double util;
398
399         log_info("\nDisk stats (read/write):\n");
400
401         list_for_each(entry, &disk_list) {
402                 du = list_entry(entry, struct disk_util, list);
403                 dus = &du->dus;
404
405                 util = (double) 100 * du->dus.io_ticks / (double) du->msec;
406                 if (util > 100.0)
407                         util = 100.0;
408
409                 log_info("  %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);
410         }
411
412         /*
413          * now free the list
414          */
415         list_for_each_safe(entry, next, &disk_list) {
416                 list_del(entry);
417                 du = list_entry(entry, struct disk_util, list);
418                 free(du->name);
419                 free(du);
420         }
421 }
422
423 #define ts_total_io_u(ts)       \
424         ((ts)->total_io_u[0] + (ts)->total_io_u[1])
425
426 static void stat_calc_dist(struct thread_stat *ts, double *io_u_dist)
427 {
428         int i;
429
430         /*
431          * Do depth distribution calculations
432          */
433         for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
434                 io_u_dist[i] = (double) ts->io_u_map[i] / (double) ts_total_io_u(ts);
435                 io_u_dist[i] *= 100.0;
436                 if (io_u_dist[i] < 0.1 && ts->io_u_map[i])
437                         io_u_dist[i] = 0.1;
438         }
439 }
440
441 static void stat_calc_lat(struct thread_stat *ts, double *io_u_lat)
442 {
443         int i;
444
445         /*
446          * Do latency distribution calculations
447          */
448         for (i = 0; i < FIO_IO_U_LAT_NR; i++) {
449                 io_u_lat[i] = (double) ts->io_u_lat[i] / (double) ts_total_io_u(ts);
450                 io_u_lat[i] *= 100.0;
451                 if (io_u_lat[i] < 0.01 && ts->io_u_lat[i])
452                         io_u_lat[i] = 0.01;
453         }
454 }
455
456 static void show_ddir_status(struct group_run_stats *rs, struct thread_stat *ts,
457                              int ddir)
458 {
459         const char *ddir_str[] = { "read ", "write" };
460         unsigned long min, max;
461         unsigned long long bw, iops;
462         double mean, dev;
463         char *io_p, *bw_p, *iops_p;
464
465         if (!ts->runtime[ddir])
466                 return;
467
468         bw = ts->io_bytes[ddir] / ts->runtime[ddir];
469         iops = (1000 * ts->total_io_u[ddir]) / ts->runtime[ddir];
470         io_p = num2str(ts->io_bytes[ddir] >> 10, 6, 1000, 1);
471         bw_p = num2str(bw, 6, 1000, 1);
472         iops_p = num2str(iops, 6, 1, 0);
473
474         log_info("  %s: io=%siB, bw=%siB/s, iops=%s, runt=%6lumsec\n", ddir_str[ddir], io_p, bw_p, iops_p, ts->runtime[ddir]);
475
476         free(io_p);
477         free(bw_p);
478         free(iops_p);
479
480         if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev))
481                 log_info("    slat (msec): min=%5lu, max=%5lu, avg=%5.02f, stdev=%5.02f\n", min, max, mean, dev);
482
483         if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev))
484                 log_info("    clat (msec): min=%5lu, max=%5lu, avg=%5.02f, stdev=%5.02f\n", min, max, mean, dev);
485
486         if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
487                 double p_of_agg;
488
489                 p_of_agg = mean * 100 / (double) rs->agg[ddir];
490                 log_info("    bw (KiB/s) : min=%5lu, max=%5lu, per=%3.2f%%, avg=%5.02f, stdev=%5.02f\n", min, max, p_of_agg, mean, dev);
491         }
492 }
493
494 static void show_thread_status(struct thread_stat *ts,
495                                struct group_run_stats *rs)
496 {
497         double usr_cpu, sys_cpu;
498         unsigned long runtime;
499         double io_u_dist[FIO_IO_U_MAP_NR];
500         double io_u_lat[FIO_IO_U_LAT_NR];
501
502         if (!(ts->io_bytes[0] + ts->io_bytes[1]))
503                 return;
504
505         if (!ts->error)
506                 log_info("%s: (groupid=%d, jobs=%d): err=%2d: pid=%d\n", ts->name, ts->groupid, ts->members, ts->error, ts->pid);
507         else
508                 log_info("%s: (groupid=%d, jobs=%d): err=%2d (%s): pid=%d\n", ts->name, ts->groupid, ts->members, ts->error, ts->verror, ts->pid);
509
510         if (ts->description)
511                 log_info("  Description  : [%s]\n", ts->description);
512
513         if (ts->io_bytes[DDIR_READ])
514                 show_ddir_status(rs, ts, DDIR_READ);
515         if (ts->io_bytes[DDIR_WRITE])
516                 show_ddir_status(rs, ts, DDIR_WRITE);
517
518         runtime = ts->total_run_time;
519         if (runtime) {
520                 double runt = (double) runtime;
521
522                 usr_cpu = (double) ts->usr_time * 100 / runt;
523                 sys_cpu = (double) ts->sys_time * 100 / runt;
524         } else {
525                 usr_cpu = 0;
526                 sys_cpu = 0;
527         }
528
529         log_info("  cpu          : usr=%3.2f%%, sys=%3.2f%%, ctx=%lu\n", usr_cpu, sys_cpu, ts->ctx);
530
531         stat_calc_dist(ts, io_u_dist);
532         stat_calc_lat(ts, io_u_lat);
533
534         log_info("  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]);
535
536         log_info("     lat (msec): 2=%3.2f%%, 4=%3.2f%%, 10=%3.2f%%, 20=%3.2f%%, 50=%3.2f%%, 100=%3.2f%%\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]);
537         log_info("     lat (msec): 250=%3.2f%%, 500=%3.2f%%, 750=%3.2f%%, 1000=%3.2f%%, >=2000=%3.2f%%\n", io_u_lat[6], io_u_lat[7], io_u_lat[8], io_u_lat[9], io_u_lat[10]);
538 }
539
540 static void show_ddir_status_terse(struct thread_stat *ts,
541                                    struct group_run_stats *rs, int ddir)
542 {
543         unsigned long min, max;
544         unsigned long long bw;
545         double mean, dev;
546
547         bw = 0;
548         if (ts->runtime[ddir])
549                 bw = ts->io_bytes[ddir] / ts->runtime[ddir];
550
551         log_info(";%llu;%llu;%lu", ts->io_bytes[ddir] >> 10, bw, ts->runtime[ddir]);
552
553         if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev))
554                 log_info(";%lu;%lu;%f;%f", min, max, mean, dev);
555         else
556                 log_info(";%lu;%lu;%f;%f", 0UL, 0UL, 0.0, 0.0);
557
558         if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev))
559                 log_info(";%lu;%lu;%f;%f", min, max, mean, dev);
560         else
561                 log_info(";%lu;%lu;%f;%f", 0UL, 0UL, 0.0, 0.0);
562
563         if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) {
564                 double p_of_agg;
565
566                 p_of_agg = mean * 100 / (double) rs->agg[ddir];
567                 log_info(";%lu;%lu;%f%%;%f;%f", min, max, p_of_agg, mean, dev);
568         } else
569                 log_info(";%lu;%lu;%f%%;%f;%f", 0UL, 0UL, 0.0, 0.0, 0.0);
570 }
571
572
573 static void show_thread_status_terse(struct thread_stat *ts,
574                                      struct group_run_stats *rs)
575 {
576         double io_u_dist[FIO_IO_U_MAP_NR];
577         double io_u_lat[FIO_IO_U_LAT_NR];
578         double usr_cpu, sys_cpu;
579
580         log_info("%s;%d;%d", ts->name, ts->groupid, ts->error);
581
582         show_ddir_status_terse(ts, rs, 0);
583         show_ddir_status_terse(ts, rs, 1);
584
585         if (ts->total_run_time) {
586                 double runt = (double) ts->total_run_time;
587
588                 usr_cpu = (double) ts->usr_time * 100 / runt;
589                 sys_cpu = (double) ts->sys_time * 100 / runt;
590         } else {
591                 usr_cpu = 0;
592                 sys_cpu = 0;
593         }
594
595         log_info(";%f%%;%f%%;%lu", usr_cpu, sys_cpu, ts->ctx);
596
597         stat_calc_dist(ts, io_u_dist);
598         stat_calc_lat(ts, io_u_lat);
599
600         log_info(";%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]);
601
602         log_info(";%3.2f%%;%3.2f%%;%3.2f%%;%3.2f%%;%3.2f%%;%3.2f%%\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]);
603         log_info(";%3.2f%%;%3.2f%%;%3.2f%%;%3.2f%%;%3.2f%%", io_u_lat[6], io_u_lat[7], io_u_lat[8], io_u_lat[9], io_u_lat[10]);
604
605         if (ts->description)
606                 log_info(";%s", ts->description);
607
608         log_info("\n");
609 }
610
611 static void sum_stat(struct io_stat *dst, struct io_stat *src, int nr)
612 {
613         double mean, S;
614
615         dst->min_val = min(dst->min_val, src->min_val);
616         dst->max_val = max(dst->max_val, src->max_val);
617         dst->samples += src->samples;
618
619         /*
620          * Needs a new method for calculating stddev, we cannot just
621          * average them we do below for nr > 1
622          */
623         if (nr == 1) {
624                 mean = src->mean;
625                 S = src->S;
626         } else {
627                 mean = ((src->mean * (double) (nr - 1)) + dst->mean) / ((double) nr);
628                 S = ((src->S * (double) (nr - 1)) + dst->S) / ((double) nr);
629         }
630
631         dst->mean = mean;
632         dst->S = S;
633 }
634
635 void show_run_stats(void)
636 {
637         struct group_run_stats *runstats, *rs;
638         struct thread_data *td;
639         struct thread_stat *threadstats, *ts;
640         int i, j, k, l, nr_ts, last_ts, idx;
641
642         runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1));
643
644         for (i = 0; i < groupid + 1; i++) {
645                 rs = &runstats[i];
646
647                 memset(rs, 0, sizeof(*rs));
648                 rs->min_bw[0] = rs->min_run[0] = ~0UL;
649                 rs->min_bw[1] = rs->min_run[1] = ~0UL;
650         }
651
652         /*
653          * find out how many threads stats we need. if group reporting isn't
654          * enabled, it's one-per-td.
655          */
656         nr_ts = 0;
657         last_ts = -1;
658         for_each_td(td, i) {
659                 if (!td->o.group_reporting) {
660                         nr_ts++;
661                         continue;
662                 }
663                 if (last_ts == td->groupid)
664                         continue;
665
666                 last_ts = td->groupid;
667                 nr_ts++;
668         }
669
670         threadstats = malloc(nr_ts * sizeof(struct thread_stat));
671
672         for (i = 0; i < nr_ts; i++) {
673                 ts = &threadstats[i];
674
675                 memset(ts, 0, sizeof(*ts));
676                 for (j = 0; j <= DDIR_WRITE; j++) {
677                         ts->clat_stat[j].min_val = -1UL;
678                         ts->slat_stat[j].min_val = -1UL;
679                         ts->bw_stat[j].min_val = -1UL;
680                 }
681                 ts->groupid = -1;
682         }
683
684         j = 0;
685         last_ts = -1;
686         idx = 0;
687         for_each_td(td, i) {
688                 if (idx && (!td->o.group_reporting ||
689                     (td->o.group_reporting && last_ts != td->groupid))) {
690                         idx = 0;
691                         j++;
692                 }
693
694                 last_ts = td->groupid;
695
696                 ts = &threadstats[j];
697
698                 idx++;
699                 ts->members++;
700
701                 if (ts->groupid == -1) {
702                         /*
703                          * These are per-group shared already
704                          */
705                         ts->name = td->o.name;
706                         ts->description = td->o.description;
707                         ts->groupid = td->groupid;
708
709                         /*
710                          * first pid in group, not very useful...
711                          */
712                         ts->pid = td->pid;
713                 }
714
715                 if (td->error && !ts->error) {
716                         ts->error = td->error;
717                         ts->verror = td->verror;
718                 }
719
720                 for (l = 0; l <= DDIR_WRITE; l++) {
721                         sum_stat(&ts->clat_stat[l], &td->ts.clat_stat[l], idx);
722                         sum_stat(&ts->slat_stat[l], &td->ts.slat_stat[l], idx);
723                         sum_stat(&ts->bw_stat[l], &td->ts.bw_stat[l], idx);
724
725                         ts->stat_io_bytes[l] += td->ts.stat_io_bytes[l];
726                         ts->io_bytes[l] += td->ts.io_bytes[l];
727
728                         if (ts->runtime[l] < td->ts.runtime[l])
729                                 ts->runtime[l] = td->ts.runtime[l];
730                 }
731
732                 ts->usr_time += td->ts.usr_time;
733                 ts->sys_time += td->ts.sys_time;
734                 ts->ctx += td->ts.ctx;
735
736                 for (k = 0; k < FIO_IO_U_MAP_NR; k++)
737                         ts->io_u_map[k] += td->ts.io_u_map[k];
738                 for (k = 0; k < FIO_IO_U_LAT_NR; k++)
739                         ts->io_u_lat[k] += td->ts.io_u_lat[k];
740
741                 for (k = 0; k <= DDIR_WRITE; k++)
742                         ts->total_io_u[k] += td->ts.total_io_u[k];
743
744                 ts->total_run_time += td->ts.total_run_time;
745         }
746
747         for (i = 0; i < nr_ts; i++) {
748                 unsigned long long bw;
749
750                 ts = &threadstats[i];
751                 rs = &runstats[ts->groupid];
752
753                 for (j = 0; j <= DDIR_WRITE; j++) {
754                         if (!ts->runtime[j])
755                                 continue;
756                         if (ts->runtime[j] < rs->min_run[j] || !rs->min_run[j])
757                                 rs->min_run[j] = ts->runtime[j];
758                         if (ts->runtime[j] > rs->max_run[j])
759                                 rs->max_run[j] = ts->runtime[j];
760
761                         bw = 0;
762                         if (ts->runtime[j])
763                                 bw = ts->io_bytes[j] / (unsigned long long) ts->runtime[j];
764                         if (bw < rs->min_bw[j])
765                                 rs->min_bw[j] = bw;
766                         if (bw > rs->max_bw[j])
767                                 rs->max_bw[j] = bw;
768
769                         rs->io_kb[j] += ts->io_bytes[j] >> 10;
770                 }
771         }
772
773         for (i = 0; i < groupid + 1; i++) {
774                 rs = &runstats[i];
775
776                 if (rs->max_run[0])
777                         rs->agg[0] = (rs->io_kb[0]*1024) / rs->max_run[0];
778                 if (rs->max_run[1])
779                         rs->agg[1] = (rs->io_kb[1]*1024) / rs->max_run[1];
780         }
781
782         /*
783          * don't overwrite last signal output
784          */
785         if (!terse_output)
786                 printf("\n");
787
788         for (i = 0; i < nr_ts; i++) {
789                 ts = &threadstats[i];
790                 rs = &runstats[ts->groupid];
791
792                 if (terse_output)
793                         show_thread_status_terse(ts, rs);
794                 else
795                         show_thread_status(ts, rs);
796         }
797
798         if (!terse_output) {
799                 for (i = 0; i < groupid + 1; i++)
800                         show_group_stats(&runstats[i], i);
801
802                 show_disk_util();
803         }
804
805         free(runstats);
806         free(threadstats);
807 }
808
809 static inline void add_stat_sample(struct io_stat *is, unsigned long data)
810 {
811         double val = data;
812         double delta;
813
814         if (data > is->max_val)
815                 is->max_val = data;
816         if (data < is->min_val)
817                 is->min_val = data;
818
819         delta = val - is->mean;
820         if (delta) {
821                 is->mean += delta / (is->samples + 1.0);
822                 is->S += delta * (val - is->mean);
823         }
824
825         is->samples++;
826 }
827
828 static void __add_log_sample(struct io_log *iolog, unsigned long val,
829                              enum fio_ddir ddir, unsigned long time)
830 {
831         if (iolog->nr_samples == iolog->max_samples) {
832                 int new_size = sizeof(struct io_sample) * iolog->max_samples*2;
833
834                 iolog->log = realloc(iolog->log, new_size);
835                 iolog->max_samples <<= 1;
836         }
837
838         iolog->log[iolog->nr_samples].val = val;
839         iolog->log[iolog->nr_samples].time = time;
840         iolog->log[iolog->nr_samples].ddir = ddir;
841         iolog->nr_samples++;
842 }
843
844 static void add_log_sample(struct thread_data *td, struct io_log *iolog,
845                            unsigned long val, enum fio_ddir ddir)
846 {
847         __add_log_sample(iolog, val, ddir, mtime_since_now(&td->epoch));
848 }
849
850 void add_agg_sample(unsigned long val, enum fio_ddir ddir)
851 {
852         struct io_log *iolog = agg_io_log[ddir];
853
854         __add_log_sample(iolog, val, ddir, mtime_since_genesis());
855 }
856
857 void add_clat_sample(struct thread_data *td, enum fio_ddir ddir,
858                      unsigned long msec)
859 {
860         struct thread_stat *ts = &td->ts;
861
862         add_stat_sample(&ts->clat_stat[ddir], msec);
863
864         if (ts->clat_log)
865                 add_log_sample(td, ts->clat_log, msec, ddir);
866 }
867
868 void add_slat_sample(struct thread_data *td, enum fio_ddir ddir,
869                      unsigned long msec)
870 {
871         struct thread_stat *ts = &td->ts;
872
873         add_stat_sample(&ts->slat_stat[ddir], msec);
874
875         if (ts->slat_log)
876                 add_log_sample(td, ts->slat_log, msec, ddir);
877 }
878
879 void add_bw_sample(struct thread_data *td, enum fio_ddir ddir,
880                    struct timeval *t)
881 {
882         struct thread_stat *ts = &td->ts;
883         unsigned long spent = mtime_since(&ts->stat_sample_time[ddir], t);
884         unsigned long rate;
885
886         if (spent < td->o.bw_avg_time)
887                 return;
888
889         rate = (td->this_io_bytes[ddir] - ts->stat_io_bytes[ddir]) / spent;
890         add_stat_sample(&ts->bw_stat[ddir], rate);
891
892         if (ts->bw_log)
893                 add_log_sample(td, ts->bw_log, rate, ddir);
894
895         fio_gettime(&ts->stat_sample_time[ddir], NULL);
896         ts->stat_io_bytes[ddir] = td->this_io_bytes[ddir];
897 }