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