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