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