[PATCH] blkparse: Fixup breakage in file reads
[blktrace.git] / blkparse.c
1 /*
2  * block queue tracing parse application
3  *
4  * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
24 #include <stdio.h>
25 #include <fcntl.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <getopt.h>
29 #include <errno.h>
30 #include <signal.h>
31 #include <locale.h>
32 #include <limits.h>
33
34 #include "blktrace.h"
35 #include "rbtree.h"
36
37 #define SECONDS(x)              ((unsigned long long)(x) / 1000000000)
38 #define NANO_SECONDS(x)         ((unsigned long long)(x) % 1000000000)
39 #define DOUBLE_TO_NANO_ULL(d)   ((unsigned long long)((d) * 1000000000))
40
41 #define MINORBITS       20
42 #define MINORMASK       ((1U << MINORBITS) - 1)
43 #define MAJOR(dev)      ((unsigned int) ((dev) >> MINORBITS))
44 #define MINOR(dev)      ((unsigned int) ((dev) & MINORMASK))
45
46 #define min(a, b)       ((a) < (b) ? (a) : (b))
47
48 struct io_stats {
49         unsigned long qreads, qwrites, creads, cwrites, mreads, mwrites;
50         unsigned long ireads, iwrites;
51         unsigned long long qread_kb, qwrite_kb, cread_kb, cwrite_kb;
52         unsigned long long iread_kb, iwrite_kb;
53         unsigned long io_unplugs, timer_unplugs;
54 };
55
56 struct per_cpu_info {
57         int cpu;
58         int nelems;
59
60         int fd;
61         char fname[128];
62
63         struct io_stats io_stats;
64 };
65
66 struct per_dev_info {
67         dev_t id;
68         char *name;
69
70         int backwards;
71         unsigned long long events;
72         unsigned long long last_reported_time;
73         struct io_stats io_stats;
74         unsigned long last_sequence;
75
76         int ncpus;
77         struct per_cpu_info *cpus;
78 };
79
80 struct per_process_info {
81         char name[16];
82         __u32 pid;
83         struct io_stats io_stats;
84         struct per_process_info *hash_next, *list_next;
85
86         /*
87          * individual io stats
88          */
89         unsigned long long longest_allocation_wait[2];
90         unsigned long long longest_dispatch_wait[2];
91         unsigned long long longest_completion_wait[2];
92 };
93
94 #define PPI_HASH_SHIFT  (8)
95 static struct per_process_info *ppi_hash[1 << PPI_HASH_SHIFT];
96 static struct per_process_info *ppi_list;
97
98 #define S_OPTS  "i:o:b:stqw:"
99 static struct option l_opts[] = {
100         {
101                 .name = "input",
102                 .has_arg = 1,
103                 .flag = NULL,
104                 .val = 'i'
105         },
106         {
107                 .name = "output",
108                 .has_arg = 1,
109                 .flag = NULL,
110                 .val = 'o'
111         },
112         {
113                 .name = "batch",
114                 .has_arg = 1,
115                 .flag = NULL,
116                 .val = 'b'
117         },
118         {
119                 .name = "per program stats",
120                 .has_arg = 0,
121                 .flag = NULL,
122                 .val = 's'
123         },
124         {
125                 .name = "track ios",
126                 .has_arg = 0,
127                 .flag = NULL,
128                 .val = 't'
129         },
130         {
131                 .name = "quiet",
132                 .has_arg = 0,
133                 .flag = NULL,
134                 .val = 'q'
135         },
136         {
137                 .name = "stopwatch",
138                 .has_arg = 1,
139                 .flag = NULL,
140                 .val = 'w'
141         },
142         {
143                 .name = NULL,
144                 .has_arg = 0,
145                 .flag = NULL,
146                 .val = 0
147         }
148 };
149
150 /*
151  * for sorting the displayed output
152  */
153 struct trace {
154         struct blk_io_trace *bit;
155         struct rb_node rb_node;
156         struct trace *next;
157         int skipped;
158 };
159
160 static struct rb_root rb_sort_root;
161 static struct rb_root rb_track_root;
162
163 static struct trace *trace_list;
164
165 /*
166  * for tracking individual ios
167  */
168 struct io_track {
169         struct rb_node rb_node;
170
171         dev_t device;
172         __u64 sector;
173         __u32 pid;
174         unsigned long long allocation_time;
175         unsigned long long queue_time;
176         unsigned long long dispatch_time;
177         unsigned long long completion_time;
178 };
179
180 static int ndevices;
181 static struct per_dev_info *devices;
182 static char *get_dev_name(struct per_dev_info *, char *, int);
183
184 static FILE *ofp;
185 static char *output_name;
186
187 static unsigned long long genesis_time;
188 static unsigned long long stopwatch_start;      /* start from zero by default */
189 static unsigned long long stopwatch_end = ULONG_LONG_MAX;       /* "infinity" */
190
191 static int per_process_stats;
192 static int track_ios;
193
194 #define RB_BATCH_DEFAULT        (1024)
195 static int rb_batch = RB_BATCH_DEFAULT;
196
197 static int pipeline;
198
199 #define is_done()       (*(volatile int *)(&done))
200 static volatile int done;
201
202 static inline unsigned long hash_long(unsigned long val)
203 {
204 #if __WORDSIZE == 32
205         val *= 0x9e370001UL;
206 #elif __WORDSIZE == 64
207         val *= 0x9e37fffffffc0001UL;
208 #else
209 #error unknown word size
210 #endif
211
212         return val >> (__WORDSIZE - PPI_HASH_SHIFT);
213 }
214
215 static inline void add_process_to_hash(struct per_process_info *ppi)
216 {
217         const int hash_idx = hash_long(ppi->pid);
218
219         ppi->hash_next = ppi_hash[hash_idx];
220         ppi_hash[hash_idx] = ppi;
221 }
222
223 static inline void add_process_to_list(struct per_process_info *ppi)
224 {
225         ppi->list_next = ppi_list;
226         ppi_list = ppi;
227 }
228
229 static struct per_process_info *find_process_by_pid(__u32 pid)
230 {
231         const int hash_idx = hash_long(pid);
232         struct per_process_info *ppi;
233
234         ppi = ppi_hash[hash_idx];
235         while (ppi) {
236                 if (ppi->pid == pid)
237                         return ppi;
238
239                 ppi = ppi->hash_next;
240         }
241
242         return NULL;
243 }
244
245 static inline int trace_rb_insert(struct trace *t)
246 {
247         struct rb_node **p = &rb_sort_root.rb_node;
248         struct rb_node *parent = NULL;
249         struct trace *__t;
250
251         if (genesis_time == 0 || t->bit->time < genesis_time)
252                 genesis_time = t->bit->time;
253
254         while (*p) {
255                 parent = *p;
256                 __t = rb_entry(parent, struct trace, rb_node);
257
258                 if (t->bit->time < __t->bit->time)
259                         p = &(*p)->rb_left;
260                 else if (t->bit->time > __t->bit->time)
261                         p = &(*p)->rb_right;
262                 else if (t->bit->device < __t->bit->device)
263                         p = &(*p)->rb_left;
264                 else if (t->bit->device > __t->bit->device)
265                         p = &(*p)->rb_right;
266                 else if (t->bit->sequence < __t->bit->sequence)
267                         p = &(*p)->rb_left;
268                 else if (t->bit->sequence > __t->bit->sequence)
269                         p = &(*p)->rb_right;
270                 else if (t->bit->device == __t->bit->device) {
271                         fprintf(stderr,
272                                 "sequence alias (%d) on device %d,%d!\n",
273                                 t->bit->sequence,
274                                 MAJOR(t->bit->device), MINOR(t->bit->device));
275                         return 1;
276                 }
277         }
278
279         rb_link_node(&t->rb_node, parent, p);
280         rb_insert_color(&t->rb_node, &rb_sort_root);
281         return 0;
282 }
283
284 static inline int track_rb_insert(struct io_track *iot)
285 {
286         struct rb_node **p = &rb_track_root.rb_node;
287         struct rb_node *parent = NULL;
288         struct io_track *__iot;
289
290         while (*p) {
291                 parent = *p;
292
293                 __iot = rb_entry(parent, struct io_track, rb_node);
294
295                 if (iot->device < __iot->device)
296                         p = &(*p)->rb_left;
297                 else if (iot->device > __iot->device)
298                         p = &(*p)->rb_right;
299                 else if (iot->sector < __iot->sector)
300                         p = &(*p)->rb_left;
301                 else if (iot->sector > __iot->sector)
302                         p = &(*p)->rb_right;
303                 else {
304                         fprintf(stderr,
305                                 "sector alias (%llu) on device %d,%d!\n",
306                                 iot->sector,
307                                 MAJOR(iot->device), MINOR(iot->device));
308                         return 1;
309                 }
310         }
311
312         rb_link_node(&iot->rb_node, parent, p);
313         rb_insert_color(&iot->rb_node, &rb_track_root);
314         return 0;
315 }
316
317 static struct io_track *__find_track(dev_t device, __u64 sector)
318 {
319         struct rb_node **p = &rb_track_root.rb_node;
320         struct rb_node *parent = NULL;
321         struct io_track *__iot;
322
323         while (*p) {
324                 parent = *p;
325                 
326                 __iot = rb_entry(parent, struct io_track, rb_node);
327
328                 if (device < __iot->device)
329                         p = &(*p)->rb_left;
330                 else if (device > __iot->device)
331                         p = &(*p)->rb_right;
332                 else if (sector < __iot->sector)
333                         p = &(*p)->rb_left;
334                 else if (sector > __iot->sector)
335                         p = &(*p)->rb_right;
336                 else
337                         return __iot;
338         }
339
340         return NULL;
341 }
342
343 static struct io_track *find_track(__u32 pid, dev_t device, __u64 sector)
344 {
345         struct io_track *iot;
346
347         iot = __find_track(device, sector);
348         if (!iot) {
349                 iot = malloc(sizeof(*iot));
350                 iot->pid = pid;
351                 iot->device = device;
352                 iot->sector = sector;
353                 track_rb_insert(iot);
354         }
355
356         return iot;
357 }
358
359 static void log_track_frontmerge(struct blk_io_trace *t)
360 {
361         struct io_track *iot;
362
363         if (!track_ios)
364                 return;
365
366         iot = __find_track(t->device, t->sector + (t->bytes >> 9));
367         if (!iot) {
368                 fprintf(stderr, "failed to find mergeable event\n");
369                 return;
370         }
371
372         rb_erase(&iot->rb_node, &rb_track_root);
373         iot->sector -= t->bytes >> 9;
374         track_rb_insert(iot);
375 }
376
377 static void log_track_getrq(struct blk_io_trace *t)
378 {
379         struct io_track *iot;
380
381         if (!track_ios)
382                 return;
383
384         iot = find_track(t->pid, t->device, t->sector);
385         iot->allocation_time = t->time;
386 }
387
388
389 /*
390  * return time between rq allocation and queue
391  */
392 static unsigned long long log_track_queue(struct blk_io_trace *t)
393 {
394         unsigned long long elapsed;
395         struct io_track *iot;
396
397         if (!track_ios)
398                 return -1;
399
400         iot = find_track(t->pid, t->device, t->sector);
401         iot->queue_time = t->time;
402         elapsed = iot->queue_time - iot->allocation_time;
403
404         if (per_process_stats) {
405                 struct per_process_info *ppi = find_process_by_pid(iot->pid);
406                 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
407
408                 if (ppi && elapsed > ppi->longest_allocation_wait[w])
409                         ppi->longest_allocation_wait[w] = elapsed;
410         }
411
412         return elapsed;
413 }
414
415 /*
416  * return time between queue and issue
417  */
418 static unsigned long long log_track_issue(struct blk_io_trace *t)
419 {
420         unsigned long long elapsed;
421         struct io_track *iot;
422
423         if (!track_ios)
424                 return -1;
425         if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
426                 return -1;
427
428         iot = __find_track(t->device, t->sector);
429         if (!iot) {
430                 fprintf(stderr, "failed to find issue event\n");
431                 return -1;
432         }
433
434         iot->dispatch_time = t->time;
435         elapsed = iot->dispatch_time - iot->queue_time;
436
437         if (per_process_stats) {
438                 struct per_process_info *ppi = find_process_by_pid(iot->pid);
439                 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
440
441                 if (ppi && elapsed > ppi->longest_dispatch_wait[w])
442                         ppi->longest_dispatch_wait[w] = elapsed;
443         }
444
445         return elapsed;
446 }
447
448 /*
449  * return time between dispatch and complete
450  */
451 static unsigned long long log_track_complete(struct blk_io_trace *t)
452 {
453         unsigned long long elapsed;
454         struct io_track *iot;
455
456         if (!track_ios)
457                 return -1;
458         if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
459                 return -1;
460
461         iot = __find_track(t->device, t->sector);
462         if (!iot) {
463                 fprintf(stderr, "failed to find complete event\n");
464                 return -1;
465         }
466
467         iot->completion_time = t->time;
468         elapsed = iot->completion_time - iot->dispatch_time;
469
470         if (per_process_stats) {
471                 struct per_process_info *ppi = find_process_by_pid(iot->pid);
472                 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
473
474                 if (ppi && elapsed > ppi->longest_completion_wait[w])
475                         ppi->longest_completion_wait[w] = elapsed;
476         }
477
478         /*
479          * kill the trace, we don't need it after completion
480          */
481         rb_erase(&iot->rb_node, &rb_track_root);
482         free(iot);
483
484         return elapsed;
485 }
486
487
488 static struct io_stats *find_process_io_stats(__u32 pid, char *name)
489 {
490         struct per_process_info *ppi = find_process_by_pid(pid);
491
492         if (!ppi) {
493                 ppi = malloc(sizeof(*ppi));
494                 memset(ppi, 0, sizeof(*ppi));
495                 strncpy(ppi->name, name, sizeof(ppi->name));
496                 ppi->pid = pid;
497                 add_process_to_hash(ppi);
498                 add_process_to_list(ppi);
499         }
500
501         return &ppi->io_stats;
502 }
503
504
505 static void resize_cpu_info(struct per_dev_info *pdi, int cpu)
506 {
507         struct per_cpu_info *cpus = pdi->cpus;
508         int ncpus = pdi->ncpus;
509         int new_count = cpu + 1;
510         int new_space, size;
511         char *new_start;
512
513         size = new_count * sizeof(struct per_cpu_info);
514         cpus = realloc(cpus, size);
515         if (!cpus) {
516                 char name[20];
517                 fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n",
518                         get_dev_name(pdi, name, sizeof(name)), size);
519                 exit(1);
520         }
521
522         new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info));
523         new_space = (new_count - ncpus) * sizeof(struct per_cpu_info);
524         memset(new_start, 0, new_space);
525
526         pdi->ncpus = new_count;
527         pdi->cpus = cpus;
528 }
529
530 static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu)
531 {
532         struct per_cpu_info *pci;
533
534         if (cpu >= pdi->ncpus)
535                 resize_cpu_info(pdi, cpu);
536
537         pci = &pdi->cpus[cpu];
538         pci->cpu = cpu;
539         return pci;
540 }
541
542
543 static int resize_devices(char *name)
544 {
545         int size = (ndevices + 1) * sizeof(struct per_dev_info);
546
547         devices = realloc(devices, size);
548         if (!devices) {
549                 fprintf(stderr, "Out of memory, device %s (%d)\n", name, size);
550                 return 1;
551         }
552         memset(&devices[ndevices], 0, sizeof(struct per_dev_info));
553         devices[ndevices].name = name;
554         ndevices++;
555         return 0;
556 }
557
558 static struct per_dev_info *get_dev_info(dev_t id)
559 {
560         struct per_dev_info *pdi;
561         int i;
562
563         for (i = 0; i < ndevices; i++)
564                 if (devices[i].id == id)
565                         return &devices[i];
566
567         if (resize_devices(NULL) != 0)
568                 return NULL;
569
570         pdi = &devices[ndevices - 1];
571         pdi->id = id;
572         return pdi;
573 }
574
575 static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size)
576 {
577         if (pdi->name)
578                 snprintf(buffer, size, "%s", pdi->name);
579         else
580                 snprintf(buffer, size, "%d,%d", MAJOR(pdi->id), MINOR(pdi->id));
581         return buffer;
582 }
583
584 static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit)
585 {
586         unsigned long long this = bit->time;
587         unsigned long long last = pdi->last_reported_time;
588
589         pdi->backwards = (this < last) ? 'B' : ' ';
590         pdi->last_reported_time = this;
591 }
592
593 static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t,
594                                int rw)
595 {
596         if (rw) {
597                 ios->mwrites++;
598                 ios->qwrite_kb += t->bytes >> 10;
599         } else {
600                 ios->mreads++;
601                 ios->qread_kb += t->bytes >> 10;
602         }
603 }
604
605 static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci,
606                              int rw)
607 {
608         __account_m(&pci->io_stats, t, rw);
609
610         if (per_process_stats) {
611                 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
612
613                 __account_m(ios, t, rw);
614         }
615 }
616
617 static inline void __account_q(struct io_stats *ios, struct blk_io_trace *t,
618                                int rw)
619 {
620         if (rw) {
621                 ios->qwrites++;
622                 ios->qwrite_kb += t->bytes >> 10;
623         } else {
624                 ios->qreads++;
625                 ios->qread_kb += t->bytes >> 10;
626         }
627 }
628
629 static inline void account_q(struct blk_io_trace *t, struct per_cpu_info *pci,
630                              int rw)
631 {
632         __account_q(&pci->io_stats, t, rw);
633
634         if (per_process_stats) {
635                 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
636
637                 __account_q(ios, t, rw);
638         }
639 }
640
641 static inline void __account_c(struct io_stats *ios, int rw, unsigned int bytes)
642 {
643         if (rw) {
644                 ios->cwrites++;
645                 ios->cwrite_kb += bytes >> 10;
646         } else {
647                 ios->creads++;
648                 ios->cread_kb += bytes >> 10;
649         }
650 }
651
652 static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci,
653                              int rw, int bytes)
654 {
655         __account_c(&pci->io_stats, rw, bytes);
656
657         if (per_process_stats) {
658                 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
659
660                 __account_c(ios, rw, bytes);
661         }
662 }
663
664 static inline void __account_i(struct io_stats *ios, int rw, unsigned int bytes)
665 {
666         if (rw) {
667                 ios->iwrites++;
668                 ios->iwrite_kb += bytes >> 10;
669         } else {
670                 ios->ireads++;
671                 ios->iread_kb += bytes >> 10;
672         }
673 }
674
675 static inline void account_i(struct blk_io_trace *t, struct per_cpu_info *pci,
676                              int rw)
677 {
678         __account_i(&pci->io_stats, rw, t->bytes);
679
680         if (per_process_stats) {
681                 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
682
683                 __account_i(ios, rw, t->bytes);
684         }
685 }
686
687 static inline void __account_unplug(struct io_stats *ios, int timer)
688 {
689         if (timer)
690                 ios->timer_unplugs++;
691         else
692                 ios->io_unplugs++;
693 }
694
695 static inline void account_unplug(struct blk_io_trace *t,
696                                   struct per_cpu_info *pci, int timer)
697 {
698         __account_unplug(&pci->io_stats, timer);
699
700         if (per_process_stats) {
701                 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
702
703                 __account_unplug(ios, timer);
704         }
705 }
706
707 static void output(struct per_cpu_info *pci, char *s)
708 {
709         fprintf(ofp, "%s", s);
710 }
711
712 static char hstring[256];
713 static char tstring[256];
714
715 static inline char *setup_header(struct per_cpu_info *pci,
716                                  struct blk_io_trace *t, char *act)
717 {
718         int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
719         int b = t->action & BLK_TC_ACT(BLK_TC_BARRIER);
720         int s = t->action & BLK_TC_ACT(BLK_TC_SYNC);
721         char rwbs[4];
722         int i = 0;
723
724         if (w)
725                 rwbs[i++] = 'W';
726         else
727                 rwbs[i++] = 'R';
728         if (b)
729                 rwbs[i++] = 'B';
730         if (s)
731                 rwbs[i++] = 'S';
732
733         rwbs[i] = '\0';
734
735         sprintf(hstring, "%3d,%-3d %2d %8ld %5Lu.%09Lu %5u %2s %3s",
736                 MAJOR(t->device), MINOR(t->device), pci->cpu,
737                 (unsigned long)t->sequence, SECONDS(t->time),
738                 NANO_SECONDS(t->time), t->pid, act, rwbs);
739
740         return hstring;
741 }
742
743 static void log_complete(struct per_cpu_info *pci, struct blk_io_trace *t,
744                          char *act)
745 {
746         unsigned long long elapsed = log_track_complete(t);
747
748         if (elapsed != -1ULL) {
749                 unsigned long usec = elapsed / 1000;
750
751                 sprintf(tstring,"%s %Lu + %u (%8lu) [%d]\n",
752                         setup_header(pci, t, act),
753                         (unsigned long long)t->sector, t->bytes >> 9,
754                         usec, t->error);
755         } else {
756                 sprintf(tstring,"%s %Lu + %u [%d]\n", setup_header(pci, t, act),
757                         (unsigned long long)t->sector, t->bytes >> 9, t->error);
758         }
759         
760         output(pci, tstring);
761 }
762
763 static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t,
764                       char *act)
765 {
766         unsigned long long elapsed = log_track_queue(t);
767
768         if (elapsed != -1ULL) {
769                 unsigned long usec = elapsed / 1000;
770
771                 sprintf(tstring,"%s %Lu + %u (%8lu) [%s]\n",
772                         setup_header(pci, t, act),
773                         (unsigned long long)t->sector, t->bytes >> 9,
774                         usec, t->comm);
775         } else {
776                 sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act),
777                         (unsigned long long)t->sector, t->bytes >> 9, t->comm);
778         }
779         output(pci, tstring);
780 }
781
782 static void log_issue(struct per_cpu_info *pci, struct blk_io_trace *t,
783                       char *act)
784 {
785         unsigned long long elapsed = log_track_issue(t);
786
787         if (elapsed != -1ULL) {
788                 double usec = (double) elapsed / 1000;
789
790                 sprintf(tstring,"%s %Lu + %u (%8.2f) [%s]\n",
791                         setup_header(pci, t, act),
792                         (unsigned long long)t->sector, t->bytes >> 9,
793                         usec, t->comm);
794         } else {
795                 sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act),
796                         (unsigned long long)t->sector, t->bytes >> 9, t->comm);
797         }
798
799         output(pci, tstring);
800 }
801
802 static void log_merge(struct per_cpu_info *pci, struct blk_io_trace *t,
803                       char *act)
804 {
805         if (act[0] == 'F')
806                 log_track_frontmerge(t);
807
808         sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act),
809                 (unsigned long long)t->sector, t->bytes >> 9, t->comm);
810         output(pci, tstring);
811 }
812
813 static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t,
814                         char *act)
815 {
816         sprintf(tstring,"%s [%s]\n", setup_header(pci, t, act), t->comm);
817         output(pci, tstring);
818 }
819
820 static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t,
821                         char *act)
822 {
823         sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act),
824                 (unsigned long long)t->sector, t->bytes >> 9, t->comm);
825         output(pci, tstring);
826 }
827
828 static int log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t,
829                       char *act)
830 {
831         __u64 *depth;
832         int len;
833
834         len = sprintf(tstring,"%s [%s] ", setup_header(pci, t, act), t->comm);
835         depth = (__u64 *) ((char *) t + sizeof(*t));
836         sprintf(tstring + len, "%u\n", (unsigned int) be64_to_cpu(*depth));
837         output(pci, tstring);
838
839         return 0;
840 }
841
842 static int log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act)
843 {
844         unsigned char *buf;
845         int i;
846
847         sprintf(tstring,"%s ", setup_header(pci, t, act));
848         output(pci, tstring);
849
850         buf = (unsigned char *) t + sizeof(*t);
851         for (i = 0; i < t->pdu_len; i++) {
852                 sprintf(tstring,"%02x ", buf[i]);
853                 output(pci, tstring);
854         }
855
856         if (act[0] == 'C') {
857                 sprintf(tstring,"[%d]\n", t->error);
858                 output(pci, tstring);
859         } else {
860                 sprintf(tstring,"[%s]\n", t->comm);
861                 output(pci, tstring);
862         }
863         return 0;
864 }
865
866 static int dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci)
867 {
868         int ret = 0;
869
870         switch (t->action & 0xffff) {
871                 case __BLK_TA_QUEUE:
872                         log_generic(pci, t, "Q");
873                         break;
874                 case __BLK_TA_GETRQ:
875                         log_generic(pci, t, "G");
876                         break;
877                 case __BLK_TA_SLEEPRQ:
878                         log_generic(pci, t, "S");
879                         break;
880                 case __BLK_TA_REQUEUE:
881                         log_generic(pci, t, "R");
882                         break;
883                 case __BLK_TA_ISSUE:
884                         ret = log_pc(pci, t, "D");
885                         break;
886                 case __BLK_TA_COMPLETE:
887                         log_pc(pci, t, "C");
888                         break;
889                 default:
890                         fprintf(stderr, "Bad pc action %x\n", t->action);
891                         ret = 1;
892                         break;
893         }
894         
895         return ret;
896 }
897
898 static void dump_trace_fs(struct blk_io_trace *t, struct per_cpu_info *pci)
899 {
900         int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
901         int act = t->action & 0xffff;
902
903         switch (act) {
904                 case __BLK_TA_QUEUE:
905                         account_q(t, pci, w);
906                         log_queue(pci, t, "Q");
907                         break;
908                 case __BLK_TA_BACKMERGE:
909                         account_m(t, pci, w);
910                         log_merge(pci, t, "M");
911                         break;
912                 case __BLK_TA_FRONTMERGE:
913                         account_m(t, pci, w);
914                         log_merge(pci, t, "F");
915                         break;
916                 case __BLK_TA_GETRQ:
917                         log_track_getrq(t);
918                         log_generic(pci, t, "G");
919                         break;
920                 case __BLK_TA_SLEEPRQ:
921                         log_generic(pci, t, "S");
922                         break;
923                 case __BLK_TA_REQUEUE:
924                         account_c(t, pci, w, -t->bytes);
925                         log_queue(pci, t, "R");
926                         break;
927                 case __BLK_TA_ISSUE:
928                         account_i(t, pci, w);
929                         log_issue(pci, t, "D");
930                         break;
931                 case __BLK_TA_COMPLETE:
932                         account_c(t, pci, w, t->bytes);
933                         log_complete(pci, t, "C");
934                         break;
935                 case __BLK_TA_PLUG:
936                         log_action(pci, t, "P");
937                         break;
938                 case __BLK_TA_UNPLUG_IO:
939                         account_unplug(t, pci, 0);
940                         log_unplug(pci, t, "U");
941                         break;
942                 case __BLK_TA_UNPLUG_TIMER:
943                         account_unplug(t, pci, 1);
944                         log_unplug(pci, t, "UT");
945                         break;
946                 default:
947                         fprintf(stderr, "Bad fs action %x\n", t->action);
948                         break;
949         }
950 }
951
952 static int dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
953                         struct per_dev_info *pdi)
954 {
955         int ret = 0;
956
957         if (t->action & BLK_TC_ACT(BLK_TC_PC))
958                 ret = dump_trace_pc(t, pci);
959         else
960                 dump_trace_fs(t, pci);
961
962         pdi->events++;
963         return ret;
964 }
965
966 static void dump_io_stats(struct io_stats *ios, char *msg)
967 {
968         fprintf(ofp, "%s\n", msg);
969
970         fprintf(ofp, " Reads Queued:    %'8lu, %'8LuKiB\t", ios->qreads, ios->qread_kb);
971         fprintf(ofp, " Writes Queued:    %'8lu, %'8LuKiB\n", ios->qwrites,ios->qwrite_kb);
972
973         fprintf(ofp, " Read Dispatches: %'8lu, %'8LuKiB\t", ios->ireads, ios->iread_kb);
974         fprintf(ofp, " Write Dispatches: %'8lu, %'8LuKiB\n", ios->iwrites,ios->iwrite_kb);
975         fprintf(ofp, " Reads Completed: %'8lu, %'8LuKiB\t", ios->creads, ios->cread_kb);
976         fprintf(ofp, " Writes Completed: %'8lu, %'8LuKiB\n", ios->cwrites,ios->cwrite_kb);
977         fprintf(ofp, " Read Merges:     %'8lu%8c\t", ios->mreads, ' ');
978         fprintf(ofp, " Write Merges:     %'8lu\n", ios->mwrites);
979         fprintf(ofp, " IO unplugs:      %'8lu%8c\t", ios->io_unplugs, ' ');
980         fprintf(ofp, " Timer unplugs:    %'8lu\n", ios->timer_unplugs);
981 }
982
983 static void dump_wait_stats(struct per_process_info *ppi)
984 {
985         unsigned long rawait = ppi->longest_allocation_wait[0] / 1000;
986         unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000;
987         unsigned long rcwait = ppi->longest_completion_wait[0] / 1000;
988         unsigned long wawait = ppi->longest_allocation_wait[1] / 1000;
989         unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000;
990         unsigned long wcwait = ppi->longest_completion_wait[1] / 1000;
991
992         fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' ');
993         fprintf(ofp, " Allocation wait:  %'8lu\n", wawait);
994         fprintf(ofp, " Dispatch wait:   %'8lu%8c\t", rdwait, ' ');
995         fprintf(ofp, " Dispatch wait:    %'8lu\n", wdwait);
996         fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' ');
997         fprintf(ofp, " Completion wait:  %'8lu\n", wcwait);
998 }
999
1000 static void show_process_stats(void)
1001 {
1002         struct per_process_info *ppi;
1003
1004         ppi = ppi_list;
1005         while (ppi) {
1006                 dump_io_stats(&ppi->io_stats, ppi->name);
1007                 dump_wait_stats(ppi);
1008                 ppi = ppi->list_next;
1009         }
1010
1011         fprintf(ofp, "\n");
1012 }
1013
1014 static void show_device_and_cpu_stats(void)
1015 {
1016         struct per_dev_info *pdi;
1017         struct per_cpu_info *pci;
1018         struct io_stats total, *ios;
1019         int i, j, pci_events;
1020         char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3];
1021         char name[32];
1022
1023         for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
1024
1025                 memset(&total, 0, sizeof(total));
1026                 pci_events = 0;
1027
1028                 if (i > 0)
1029                         fprintf(ofp, "\n");
1030
1031                 for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) {
1032                         if (!pci->nelems)
1033                                 continue;
1034
1035                         ios = &pci->io_stats;
1036                         total.qreads += ios->qreads;
1037                         total.qwrites += ios->qwrites;
1038                         total.creads += ios->creads;
1039                         total.cwrites += ios->cwrites;
1040                         total.mreads += ios->mreads;
1041                         total.mwrites += ios->mwrites;
1042                         total.ireads += ios->ireads;
1043                         total.iwrites += ios->iwrites;
1044                         total.qread_kb += ios->qread_kb;
1045                         total.qwrite_kb += ios->qwrite_kb;
1046                         total.cread_kb += ios->cread_kb;
1047                         total.cwrite_kb += ios->cwrite_kb;
1048                         total.iread_kb += ios->iread_kb;
1049                         total.iwrite_kb += ios->iwrite_kb;
1050                         total.timer_unplugs += ios->timer_unplugs;
1051                         total.io_unplugs += ios->io_unplugs;
1052
1053                         snprintf(line, sizeof(line) - 1, "CPU%d (%s):",
1054                                  j, get_dev_name(pdi, name, sizeof(name)));
1055                         dump_io_stats(ios, line);
1056                         pci_events++;
1057                 }
1058
1059                 if (pci_events > 1) {
1060                         fprintf(ofp, "\n");
1061                         snprintf(line, sizeof(line) - 1, "Total (%s):",
1062                                  get_dev_name(pdi, name, sizeof(name)));
1063                         dump_io_stats(&total, line);
1064                 }
1065
1066                 fprintf(ofp, "Events (%s): %'Lu\n",
1067                         get_dev_name(pdi, line, sizeof(line)), pdi->events);
1068         }
1069 }
1070
1071 static struct blk_io_trace *find_trace(void *p, unsigned long offset)
1072 {
1073         unsigned long max_offset = offset;
1074         unsigned long off;
1075         struct blk_io_trace *bit;
1076         __u32 magic;
1077
1078         for (off = 0; off < max_offset; off++) {
1079                 bit = p + off;
1080
1081                 magic = be32_to_cpu(bit->magic);
1082                 if ((magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
1083                         return bit;
1084         }
1085
1086         return NULL;
1087 }
1088
1089 static int sort_entries(void)
1090 {
1091         struct per_dev_info *pdi;
1092         struct per_cpu_info *pci;
1093         struct blk_io_trace *bit;
1094         struct trace *t;
1095         int nr = 0;
1096
1097         while ((t = trace_list) != NULL) {
1098
1099                 trace_list = t->next;
1100                 bit = t->bit;
1101
1102                 memset(&t->rb_node, 0, sizeof(t->rb_node));
1103
1104                 trace_to_cpu(bit);
1105
1106                 if (verify_trace(bit))
1107                         break;
1108                 if (trace_rb_insert(t))
1109                         return -1;
1110
1111                 pdi = get_dev_info(bit->device);
1112                 pci = get_cpu_info(pdi, bit->cpu);
1113                 pci->nelems++;
1114
1115                 nr++;
1116         }
1117
1118         return nr;
1119 }
1120
1121 static void show_entries_rb(void)
1122 {
1123         struct per_dev_info *pdi;
1124         struct blk_io_trace *bit;
1125         struct rb_node *n;
1126         struct trace *t;
1127         int cpu;
1128
1129         while ((n = rb_first(&rb_sort_root)) != NULL) {
1130
1131                 t = rb_entry(n, struct trace, rb_node);
1132                 bit = t->bit;
1133
1134                 pdi = get_dev_info(bit->device);
1135                 if (!pdi) {
1136                         fprintf(stderr, "Unknown device ID? (%d,%d)\n",
1137                                 MAJOR(bit->device), MINOR(bit->device));
1138                         break;
1139                 }
1140                 cpu = bit->cpu;
1141                 if (cpu > pdi->ncpus) {
1142                         fprintf(stderr, "Unknown CPU ID? (%d, device %d,%d)\n",
1143                                 cpu, MAJOR(bit->device), MINOR(bit->device));
1144                         break;
1145                 }
1146
1147                 /*
1148                  * back off displaying more info if we are out of sync
1149                  * on SMP systems. to prevent stalling on lost events,
1150                  * only allow an event to skip us once
1151                  */
1152                 if (bit->sequence != (pdi->last_sequence + 1)) {
1153                         if (!t->skipped) {
1154                                 t->skipped = 1;
1155                                 break;
1156                         }
1157                 }
1158
1159                 pdi->last_sequence = bit->sequence;
1160
1161                 bit->time -= genesis_time;
1162                 if (bit->time < stopwatch_start)
1163                         continue;
1164                 if (bit->time >= stopwatch_end)
1165                         break;
1166
1167                 check_time(pdi, bit);
1168
1169                 if (dump_trace(bit, &pdi->cpus[cpu], pdi))
1170                         break;
1171
1172                 rb_erase(&t->rb_node, &rb_sort_root);
1173                 free(bit);
1174                 free(t);
1175         }
1176 }
1177
1178 static int read_data(int fd, void *buffer, int bytes, int block)
1179 {
1180         int ret, bytes_left, fl;
1181         void *p;
1182
1183         fl = fcntl(fd, F_GETFL);
1184
1185         if (!block)
1186                 fcntl(fd, F_SETFL, fl | O_NONBLOCK);
1187         else
1188                 fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
1189
1190         bytes_left = bytes;
1191         p = buffer;
1192         while (bytes_left > 0) {
1193                 ret = read(fd, p, bytes_left);
1194                 if (!ret)
1195                         return 1;
1196                 else if (ret < 0) {
1197                         if (errno != EAGAIN)
1198                                 perror("read");
1199                         return -1;
1200                 } else {
1201                         p += ret;
1202                         bytes_left -= ret;
1203                 }
1204         }
1205
1206         return 0;
1207 }
1208
1209 /*
1210  * Find the traces in 'tb' and add them to the list for sorting and
1211  * displaying
1212  */
1213 static int find_entries(void *tb, unsigned long size)
1214 {
1215         struct blk_io_trace *bit;
1216         struct trace *t;
1217         void *start = tb;
1218
1219         while (tb - start <= size - sizeof(*bit)) {
1220                 bit = find_trace(tb, size - (tb - start));
1221                 if (!bit)
1222                         break;
1223
1224                 t = malloc(sizeof(*t));
1225                 memset(t, 0, sizeof(*t));
1226                 t->bit = bit;
1227
1228                 t->next = trace_list;
1229                 trace_list = t;
1230
1231                 tb += sizeof(*bit) + bit->pdu_len;
1232         }
1233
1234         return 0;
1235 }
1236
1237 static int do_file(void)
1238 {
1239         struct per_dev_info *pdi;
1240         int i, j, nfiles = 0, nelems;
1241
1242         for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
1243                 for (j = 0;; j++, nfiles++) {
1244                         struct per_cpu_info *pci;
1245                         struct stat st;
1246                         void *tb;
1247
1248                         pci = get_cpu_info(pdi, j);
1249                         pci->cpu = j;
1250
1251                         snprintf(pci->fname, sizeof(pci->fname)-1,
1252                                  "%s_out.%d", pdi->name, j);
1253                         if (stat(pci->fname, &st) < 0)
1254                                 break;
1255                         if (!st.st_size)
1256                                 continue;
1257
1258                         printf("Processing %s\n", pci->fname);
1259
1260                         tb = malloc(st.st_size);
1261                         if (!tb) {
1262                                 fprintf(stderr, "Out of memory, skip file %s\n",
1263                                         pci->fname);
1264                                 continue;
1265                         }
1266
1267                         pci->fd = open(pci->fname, O_RDONLY);
1268                         if (pci->fd < 0) {
1269                                 perror(pci->fname);
1270                                 free(tb);
1271                                 continue;
1272                         }
1273
1274                         if (read_data(pci->fd, tb, st.st_size, 1)) {
1275                                 close(pci->fd);
1276                                 free(tb);
1277                                 continue;
1278                         }
1279
1280                         if (find_entries(tb, st.st_size)) {
1281                                 close(pci->fd);
1282                                 free(tb);
1283                         }
1284
1285                         nelems = sort_entries();
1286                         if (nelems == -1) {
1287                                 close(pci->fd);
1288                                 free(tb);
1289                                 continue;
1290                         }
1291
1292                         printf("Completed %s (CPU%d %d, entries)\n",
1293                                 pci->fname, j, nelems);
1294                         close(pci->fd);
1295                 }
1296         }
1297
1298         if (!nfiles) {
1299                 fprintf(stderr, "No files found\n");
1300                 return 1;
1301         }
1302
1303         show_entries_rb();
1304         return 0;
1305 }
1306
1307 static int read_sort_events(int fd)
1308 {
1309         int events = 0;
1310
1311         do {
1312                 struct blk_io_trace *bit;
1313                 struct trace *t;
1314                 int pdu_len;
1315                 __u32 magic;
1316
1317                 bit = malloc(sizeof(*bit));
1318
1319                 if (read_data(fd, bit, sizeof(*bit), !events))
1320                         break;
1321
1322                 magic = be32_to_cpu(bit->magic);
1323                 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
1324                         fprintf(stderr, "Bad magic %x\n", magic);
1325                         break;
1326                 }
1327
1328                 pdu_len = be16_to_cpu(bit->pdu_len);
1329                 if (pdu_len) {
1330                         void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
1331
1332                         if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1))
1333                                 break;
1334
1335                         bit = ptr;
1336                 }
1337
1338                 t = malloc(sizeof(*t));
1339                 memset(t, 0, sizeof(*t));
1340                 t->bit = bit;
1341                 t->next = trace_list;
1342                 trace_list = t;
1343
1344                 events++;
1345         } while (!is_done() && events < rb_batch);
1346
1347         return events;
1348 }
1349
1350 static int do_stdin(void)
1351 {
1352         int fd;
1353
1354         fd = dup(STDIN_FILENO);
1355         do {
1356                 int events;
1357
1358                 events = read_sort_events(fd);
1359                 if (!events)
1360                         break;
1361         
1362                 if (sort_entries() == -1)
1363                         break;
1364
1365                 show_entries_rb();
1366         } while (1);
1367
1368         close(fd);
1369         return 0;
1370 }
1371
1372 static void flush_output(void)
1373 {
1374         fflush(ofp);
1375 }
1376
1377 static void handle_sigint(int sig)
1378 {
1379         done = 1;
1380         flush_output();
1381 }
1382
1383 /*
1384  * Extract start and duration times from a string, allowing
1385  * us to specify a time interval of interest within a trace.
1386  * Format: "duration" (start is zero) or "start:duration".
1387  */
1388 static int find_stopwatch_interval(char *string)
1389 {
1390         double value;
1391         char *sp;
1392
1393         value = strtod(string, &sp);
1394         if (sp == string) {
1395                 fprintf(stderr,"Invalid stopwatch timer: %s\n", string);
1396                 return 1;
1397         }
1398         if (*sp == ':') {
1399                 stopwatch_start = DOUBLE_TO_NANO_ULL(value);
1400                 string = sp + 1;
1401                 value = strtod(string, &sp);
1402                 if (sp == string || *sp != '\0') {
1403                         fprintf(stderr,"Invalid stopwatch duration time: %s\n",
1404                                 string);
1405                         return 1;
1406                 }
1407         } else if (*sp != '\0') {
1408                 fprintf(stderr,"Invalid stopwatch start timer: %s\n", string);
1409                 return 1;
1410         }
1411         stopwatch_end = stopwatch_start + DOUBLE_TO_NANO_ULL(value);
1412         return 0;
1413 }
1414
1415 static void usage(char *prog)
1416 {
1417         fprintf(stderr, "Usage: %s "
1418                 "[-i <name>] [-o <output>] [-s] [-w N[:n]] <name>...\n",
1419                 prog);
1420 }
1421
1422 int main(int argc, char *argv[])
1423 {
1424         char *ofp_buffer;
1425         int c, ret, mode;
1426         int per_device_and_cpu_stats = 1;
1427
1428         while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
1429                 switch (c) {
1430                 case 'i':
1431                         if (!strcmp(optarg, "-") && !pipeline)
1432                                 pipeline = 1;
1433                         else if (resize_devices(optarg) != 0)
1434                                 return 1;
1435                         break;
1436                 case 'o':
1437                         output_name = optarg;
1438                         break;
1439                 case 'b':
1440                         rb_batch = atoi(optarg);
1441                         if (rb_batch <= 0)
1442                                 rb_batch = RB_BATCH_DEFAULT;
1443                         break;
1444                 case 's':
1445                         per_process_stats = 1;
1446                         break;
1447                 case 't':
1448                         track_ios = 1;
1449                         break;
1450                 case 'q':
1451                         per_device_and_cpu_stats = 0;
1452                         break;
1453                 case 'w':
1454                         if (find_stopwatch_interval(optarg) != 0)
1455                                 return 1;
1456                         break;
1457                 default:
1458                         usage(argv[0]);
1459                         return 1;
1460                 }
1461         }
1462
1463         while (optind < argc) {
1464                 if (!strcmp(argv[optind], "-") && !pipeline)
1465                         pipeline = 1;
1466                 else if (resize_devices(argv[optind]) != 0)
1467                         return 1;
1468                 optind++;
1469         }
1470
1471         if (!pipeline && !ndevices) {
1472                 usage(argv[0]);
1473                 return 1;
1474         }
1475
1476         memset(&rb_sort_root, 0, sizeof(rb_sort_root));
1477         memset(&rb_track_root, 0, sizeof(rb_track_root));
1478
1479         signal(SIGINT, handle_sigint);
1480         signal(SIGHUP, handle_sigint);
1481         signal(SIGTERM, handle_sigint);
1482
1483         setlocale(LC_NUMERIC, "en_US");
1484
1485         if (!output_name) {
1486                 ofp = fdopen(STDOUT_FILENO, "w");
1487                 mode = _IOLBF;
1488         } else {
1489                 char ofname[128];
1490
1491                 snprintf(ofname, sizeof(ofname) - 1, "%s.log", output_name);
1492                 ofp = fopen(ofname, "w");
1493                 mode = _IOFBF;
1494         }
1495
1496         if (!ofp) {
1497                 perror("fopen");
1498                 return 1;
1499         }
1500
1501         ofp_buffer = malloc(4096);      
1502         if (setvbuf(ofp, ofp_buffer, mode, 4096)) {
1503                 perror("setvbuf");
1504                 return 1;
1505         }
1506
1507         if (pipeline)
1508                 ret = do_stdin();
1509         else
1510                 ret = do_file();
1511
1512         if (per_process_stats)
1513                 show_process_stats();
1514
1515         if (per_device_and_cpu_stats)
1516                 show_device_and_cpu_stats();
1517
1518         flush_output();
1519         return ret;
1520 }