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