[PATCH] blktrace: need to free ts->buf for networked transfer
[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 <libgen.h>
33
34 #include "blktrace.h"
35 #include "rbtree.h"
36 #include "jhash.h"
37
38 static char blkparse_version[] = "0.99";
39
40 struct skip_info {
41         unsigned long start, end;
42         struct skip_info *prev, *next;
43 };
44
45 struct per_dev_info {
46         dev_t dev;
47         char *name;
48
49         int backwards;
50         unsigned long long events;
51         unsigned long long first_reported_time;
52         unsigned long long last_reported_time;
53         unsigned long long last_read_time;
54         struct io_stats io_stats;
55         unsigned long skips;
56         unsigned long long seq_skips;
57         unsigned int max_depth[2];
58         unsigned int cur_depth[2];
59
60         struct rb_root rb_track;
61
62         int nfiles;
63         int ncpus;
64
65         unsigned long *cpu_map;
66         unsigned int cpu_map_max;
67
68         struct per_cpu_info *cpus;
69 };
70
71 /*
72  * some duplicated effort here, we can unify this hash and the ppi hash later
73  */
74 struct process_pid_map {
75         pid_t pid;
76         char comm[16];
77         struct process_pid_map *hash_next, *list_next;
78 };
79
80 #define PPM_HASH_SHIFT  (8)
81 #define PPM_HASH_SIZE   (1 << PPM_HASH_SHIFT)
82 #define PPM_HASH_MASK   (PPM_HASH_SIZE - 1)
83 static struct process_pid_map *ppm_hash_table[PPM_HASH_SIZE];
84
85 struct per_process_info {
86         struct process_pid_map *ppm;
87         struct io_stats io_stats;
88         struct per_process_info *hash_next, *list_next;
89         int more_than_one;
90
91         /*
92          * individual io stats
93          */
94         unsigned long long longest_allocation_wait[2];
95         unsigned long long longest_dispatch_wait[2];
96         unsigned long long longest_completion_wait[2];
97 };
98
99 #define PPI_HASH_SHIFT  (8)
100 #define PPI_HASH_SIZE   (1 << PPI_HASH_SHIFT)
101 #define PPI_HASH_MASK   (PPI_HASH_SIZE - 1)
102 static struct per_process_info *ppi_hash_table[PPI_HASH_SIZE];
103 static struct per_process_info *ppi_list;
104 static int ppi_list_entries;
105
106 #define S_OPTS  "a:A:i:o:b:stqw:f:F:vVhD:"
107 static struct option l_opts[] = {
108         {
109                 .name = "act-mask",
110                 .has_arg = required_argument,
111                 .flag = NULL,
112                 .val = 'a'
113         },
114         {
115                 .name = "set-mask",
116                 .has_arg = required_argument,
117                 .flag = NULL,
118                 .val = 'A'
119         },
120         {
121                 .name = "input",
122                 .has_arg = required_argument,
123                 .flag = NULL,
124                 .val = 'i'
125         },
126         {
127                 .name = "output",
128                 .has_arg = required_argument,
129                 .flag = NULL,
130                 .val = 'o'
131         },
132         {
133                 .name = "batch",
134                 .has_arg = required_argument,
135                 .flag = NULL,
136                 .val = 'b'
137         },
138         {
139                 .name = "per-program-stats",
140                 .has_arg = no_argument,
141                 .flag = NULL,
142                 .val = 's'
143         },
144         {
145                 .name = "track-ios",
146                 .has_arg = no_argument,
147                 .flag = NULL,
148                 .val = 't'
149         },
150         {
151                 .name = "quiet",
152                 .has_arg = no_argument,
153                 .flag = NULL,
154                 .val = 'q'
155         },
156         {
157                 .name = "stopwatch",
158                 .has_arg = required_argument,
159                 .flag = NULL,
160                 .val = 'w'
161         },
162         {
163                 .name = "format",
164                 .has_arg = required_argument,
165                 .flag = NULL,
166                 .val = 'f'
167         },
168         {
169                 .name = "format-spec",
170                 .has_arg = required_argument,
171                 .flag = NULL,
172                 .val = 'F'
173         },
174         {
175                 .name = "hash-by-name",
176                 .has_arg = no_argument,
177                 .flag = NULL,
178                 .val = 'h'
179         },
180         {
181                 .name = "verbose",
182                 .has_arg = no_argument,
183                 .flag = NULL,
184                 .val = 'v'
185         },
186         {
187                 .name = "version",
188                 .has_arg = no_argument,
189                 .flag = NULL,
190                 .val = 'V'
191         },
192         {
193                 .name = "input-directory",
194                 .has_arg = required_argument,
195                 .flag = NULL,
196                 .val = 'D'
197         },
198         {
199                 .name = NULL,
200         }
201 };
202
203 /*
204  * for sorting the displayed output
205  */
206 struct trace {
207         struct blk_io_trace *bit;
208         struct rb_node rb_node;
209         struct trace *next;
210         unsigned long read_sequence;
211 };
212
213 static struct rb_root rb_sort_root;
214 static unsigned long rb_sort_entries;
215
216 static struct trace *trace_list;
217
218 /*
219  * allocation cache
220  */
221 static struct blk_io_trace *bit_alloc_list;
222 static struct trace *t_alloc_list;
223
224 /*
225  * for tracking individual ios
226  */
227 struct io_track {
228         struct rb_node rb_node;
229
230         struct process_pid_map *ppm;
231         __u64 sector;
232         unsigned long long allocation_time;
233         unsigned long long queue_time;
234         unsigned long long dispatch_time;
235         unsigned long long completion_time;
236 };
237
238 static int ndevices;
239 static struct per_dev_info *devices;
240 static char *get_dev_name(struct per_dev_info *, char *, int);
241 static int trace_rb_insert_last(struct per_dev_info *, struct trace *);
242
243 FILE *ofp = NULL;
244 static char *output_name;
245 static char *input_dir;
246
247 static unsigned long long genesis_time;
248 static unsigned long long last_allowed_time;
249 static unsigned long long stopwatch_start;      /* start from zero by default */
250 static unsigned long long stopwatch_end = -1ULL;        /* "infinity" */
251 static unsigned long read_sequence;
252
253 static int per_process_stats;
254 static int per_device_and_cpu_stats = 1;
255 static int track_ios;
256 static int ppi_hash_by_pid = 1;
257 static int verbose;
258 static unsigned int act_mask = -1U;
259 static int stats_printed;
260 int data_is_native = -1;
261
262 static unsigned int t_alloc_cache;
263 static unsigned int bit_alloc_cache;
264
265 #define RB_BATCH_DEFAULT        (512)
266 static unsigned int rb_batch = RB_BATCH_DEFAULT;
267
268 static int pipeline;
269
270 #define is_done()       (*(volatile int *)(&done))
271 static volatile int done;
272
273 #define JHASH_RANDOM    (0x3af5f2ee)
274
275 #define CPUS_PER_LONG   (8 * sizeof(unsigned long))
276 #define CPU_IDX(cpu)    ((cpu) / CPUS_PER_LONG)
277 #define CPU_BIT(cpu)    ((cpu) & (CPUS_PER_LONG - 1))
278
279 static void resize_cpu_info(struct per_dev_info *pdi, int cpu)
280 {
281         struct per_cpu_info *cpus = pdi->cpus;
282         int ncpus = pdi->ncpus;
283         int new_count = cpu + 1;
284         int new_space, size;
285         char *new_start;
286
287         size = new_count * sizeof(struct per_cpu_info);
288         cpus = realloc(cpus, size);
289         if (!cpus) {
290                 char name[20];
291                 fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n",
292                         get_dev_name(pdi, name, sizeof(name)), size);
293                 exit(1);
294         }
295
296         new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info));
297         new_space = (new_count - ncpus) * sizeof(struct per_cpu_info);
298         memset(new_start, 0, new_space);
299
300         pdi->ncpus = new_count;
301         pdi->cpus = cpus;
302
303         for (new_count = 0; new_count < pdi->ncpus; new_count++) {
304                 struct per_cpu_info *pci = &pdi->cpus[new_count];
305
306                 if (!pci->fd) {
307                         pci->fd = -1;
308                         memset(&pci->rb_last, 0, sizeof(pci->rb_last));
309                         pci->rb_last_entries = 0;
310                         pci->last_sequence = -1;
311                 }
312         }
313 }
314
315 static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu)
316 {
317         struct per_cpu_info *pci;
318
319         if (cpu >= pdi->ncpus)
320                 resize_cpu_info(pdi, cpu);
321
322         pci = &pdi->cpus[cpu];
323         pci->cpu = cpu;
324         return pci;
325 }
326
327
328 static int resize_devices(char *name)
329 {
330         int size = (ndevices + 1) * sizeof(struct per_dev_info);
331
332         devices = realloc(devices, size);
333         if (!devices) {
334                 fprintf(stderr, "Out of memory, device %s (%d)\n", name, size);
335                 return 1;
336         }
337         memset(&devices[ndevices], 0, sizeof(struct per_dev_info));
338         devices[ndevices].name = name;
339         ndevices++;
340         return 0;
341 }
342
343 static struct per_dev_info *get_dev_info(dev_t dev)
344 {
345         struct per_dev_info *pdi;
346         int i;
347
348         for (i = 0; i < ndevices; i++) {
349                 if (!devices[i].dev)
350                         devices[i].dev = dev;
351                 if (devices[i].dev == dev)
352                         return &devices[i];
353         }
354
355         if (resize_devices(NULL))
356                 return NULL;
357
358         pdi = &devices[ndevices - 1];
359         pdi->dev = dev;
360         pdi->first_reported_time = 0;
361         pdi->last_read_time = 0;
362
363         return pdi;
364 }
365
366 static void insert_skip(struct per_cpu_info *pci, unsigned long start,
367                         unsigned long end)
368 {
369         struct skip_info *sip;
370
371         for (sip = pci->skips_tail; sip != NULL; sip = sip->prev) {
372                 if (end == (sip->start - 1)) {
373                         sip->start = start;
374                         return;
375                 } else if (start == (sip->end + 1)) {
376                         sip->end = end;
377                         return;
378                 }
379         }
380
381         sip = malloc(sizeof(struct skip_info));
382         sip->start = start;
383         sip->end = end;
384         sip->prev = sip->next = NULL;
385         if (pci->skips_tail == NULL)
386                 pci->skips_head = pci->skips_tail = sip;
387         else {
388                 sip->prev = pci->skips_tail;
389                 pci->skips_tail->next = sip;
390                 pci->skips_tail = sip;
391         }
392 }
393
394 static void remove_sip(struct per_cpu_info *pci, struct skip_info *sip)
395 {
396         if (sip->prev == NULL) {
397                 if (sip->next == NULL)
398                         pci->skips_head = pci->skips_tail = NULL;
399                 else {
400                         pci->skips_head = sip->next;
401                         sip->next->prev = NULL;
402                 }
403         } else if (sip->next == NULL) {
404                 pci->skips_tail = sip->prev;
405                 sip->prev->next = NULL;
406         } else {
407                 sip->prev->next = sip->next;
408                 sip->next->prev = sip->prev;
409         }
410
411         sip->prev = sip->next = NULL;
412         free(sip);
413 }
414
415 #define IN_SKIP(sip,seq) (((sip)->start <= (seq)) && ((seq) <= sip->end))
416 static int check_current_skips(struct per_cpu_info *pci, unsigned long seq)
417 {
418         struct skip_info *sip;
419
420         for (sip = pci->skips_tail; sip != NULL; sip = sip->prev) {
421                 if (IN_SKIP(sip, seq)) {
422                         if (sip->start == seq) {
423                                 if (sip->end == seq)
424                                         remove_sip(pci, sip);
425                                 else
426                                         sip->start += 1;
427                         } else if (sip->end == seq)
428                                 sip->end -= 1;
429                         else {
430                                 sip->end = seq - 1;
431                                 insert_skip(pci, seq + 1, sip->end);
432                         }
433                         return 1;
434                 }
435         }
436
437         return 0;
438 }
439
440 static void collect_pdi_skips(struct per_dev_info *pdi)
441 {
442         struct skip_info *sip;
443         int cpu;
444
445         pdi->skips = 0;
446         pdi->seq_skips = 0;
447
448         for (cpu = 0; cpu < pdi->ncpus; cpu++) {
449                 struct per_cpu_info *pci = &pdi->cpus[cpu];
450
451                 for (sip = pci->skips_head; sip != NULL; sip = sip->next) {
452                         pdi->skips++;
453                         pdi->seq_skips += (sip->end - sip->start + 1);
454                         if (verbose)
455                                 fprintf(stderr,"(%d,%d): skipping %lu -> %lu\n",
456                                         MAJOR(pdi->dev), MINOR(pdi->dev),
457                                         sip->start, sip->end);
458                 }
459         }
460 }
461
462 static void cpu_mark_online(struct per_dev_info *pdi, unsigned int cpu)
463 {
464         if (cpu >= pdi->cpu_map_max || !pdi->cpu_map) {
465                 int new_max = (cpu + CPUS_PER_LONG) & ~(CPUS_PER_LONG - 1);
466                 unsigned long *map = malloc(new_max / sizeof(long));
467
468                 memset(map, 0, new_max / sizeof(long));
469
470                 if (pdi->cpu_map) {
471                         memcpy(map, pdi->cpu_map, pdi->cpu_map_max / sizeof(long));
472                         free(pdi->cpu_map);
473                 }
474
475                 pdi->cpu_map = map;
476                 pdi->cpu_map_max = new_max;
477         }
478
479         pdi->cpu_map[CPU_IDX(cpu)] |= (1UL << CPU_BIT(cpu));
480 }
481
482 static inline void cpu_mark_offline(struct per_dev_info *pdi, int cpu)
483 {
484         pdi->cpu_map[CPU_IDX(cpu)] &= ~(1UL << CPU_BIT(cpu));
485 }
486
487 static inline int cpu_is_online(struct per_dev_info *pdi, int cpu)
488 {
489         return (pdi->cpu_map[CPU_IDX(cpu)] & (1UL << CPU_BIT(cpu))) != 0;
490 }
491
492 static inline int ppm_hash_pid(pid_t pid)
493 {
494         return jhash_1word(pid, JHASH_RANDOM) & PPM_HASH_MASK;
495 }
496
497 static struct process_pid_map *find_ppm(pid_t pid)
498 {
499         const int hash_idx = ppm_hash_pid(pid);
500         struct process_pid_map *ppm;
501
502         ppm = ppm_hash_table[hash_idx];
503         while (ppm) {
504                 if (ppm->pid == pid)
505                         return ppm;
506
507                 ppm = ppm->hash_next;
508         }
509
510         return NULL;
511 }
512
513 static void add_ppm_hash(pid_t pid, const char *name)
514 {
515         const int hash_idx = ppm_hash_pid(pid);
516         struct process_pid_map *ppm;
517
518         ppm = find_ppm(pid);
519         if (!ppm) {
520                 ppm = malloc(sizeof(*ppm));
521                 memset(ppm, 0, sizeof(*ppm));
522                 ppm->pid = pid;
523                 strcpy(ppm->comm, name);
524                 ppm->hash_next = ppm_hash_table[hash_idx];
525                 ppm_hash_table[hash_idx] = ppm;
526         }
527 }
528
529 char *find_process_name(pid_t pid)
530 {
531         struct process_pid_map *ppm = find_ppm(pid);
532
533         if (ppm)
534                 return ppm->comm;
535
536         return NULL;
537 }
538
539 static inline int ppi_hash_pid(pid_t pid)
540 {
541         return jhash_1word(pid, JHASH_RANDOM) & PPI_HASH_MASK;
542 }
543
544 static inline int ppi_hash_name(const char *name)
545 {
546         return jhash(name, 16, JHASH_RANDOM) & PPI_HASH_MASK;
547 }
548
549 static inline int ppi_hash(struct per_process_info *ppi)
550 {
551         struct process_pid_map *ppm = ppi->ppm;
552
553         if (ppi_hash_by_pid)
554                 return ppi_hash_pid(ppm->pid);
555
556         return ppi_hash_name(ppm->comm);
557 }
558
559 static inline void add_ppi_to_hash(struct per_process_info *ppi)
560 {
561         const int hash_idx = ppi_hash(ppi);
562
563         ppi->hash_next = ppi_hash_table[hash_idx];
564         ppi_hash_table[hash_idx] = ppi;
565 }
566
567 static inline void add_ppi_to_list(struct per_process_info *ppi)
568 {
569         ppi->list_next = ppi_list;
570         ppi_list = ppi;
571         ppi_list_entries++;
572 }
573
574 static struct per_process_info *find_ppi_by_name(char *name)
575 {
576         const int hash_idx = ppi_hash_name(name);
577         struct per_process_info *ppi;
578
579         ppi = ppi_hash_table[hash_idx];
580         while (ppi) {
581                 struct process_pid_map *ppm = ppi->ppm;
582
583                 if (!strcmp(ppm->comm, name))
584                         return ppi;
585
586                 ppi = ppi->hash_next;
587         }
588
589         return NULL;
590 }
591
592 static struct per_process_info *find_ppi_by_pid(pid_t pid)
593 {
594         const int hash_idx = ppi_hash_pid(pid);
595         struct per_process_info *ppi;
596
597         ppi = ppi_hash_table[hash_idx];
598         while (ppi) {
599                 struct process_pid_map *ppm = ppi->ppm;
600
601                 if (ppm->pid == pid)
602                         return ppi;
603
604                 ppi = ppi->hash_next;
605         }
606
607         return NULL;
608 }
609
610 static struct per_process_info *find_ppi(pid_t pid)
611 {
612         struct per_process_info *ppi;
613         char *name;
614
615         if (ppi_hash_by_pid)
616                 return find_ppi_by_pid(pid);
617
618         name = find_process_name(pid);
619         if (!name)
620                 return NULL;
621
622         ppi = find_ppi_by_name(name);
623         if (ppi && ppi->ppm->pid != pid)
624                 ppi->more_than_one = 1;
625
626         return ppi;
627 }
628
629 /*
630  * struct trace and blktrace allocation cache, we do potentially
631  * millions of mallocs for these structures while only using at most
632  * a few thousand at the time
633  */
634 static inline void t_free(struct trace *t)
635 {
636         if (t_alloc_cache < 1024) {
637                 t->next = t_alloc_list;
638                 t_alloc_list = t;
639                 t_alloc_cache++;
640         } else
641                 free(t);
642 }
643
644 static inline struct trace *t_alloc(void)
645 {
646         struct trace *t = t_alloc_list;
647
648         if (t) {
649                 t_alloc_list = t->next;
650                 t_alloc_cache--;
651                 return t;
652         }
653
654         return malloc(sizeof(*t));
655 }
656
657 static inline void bit_free(struct blk_io_trace *bit)
658 {
659         if (bit_alloc_cache < 1024 && !bit->pdu_len) {
660                 /*
661                  * abuse a 64-bit field for a next pointer for the free item
662                  */
663                 bit->time = (__u64) (unsigned long) bit_alloc_list;
664                 bit_alloc_list = (struct blk_io_trace *) bit;
665                 bit_alloc_cache++;
666         } else
667                 free(bit);
668 }
669
670 static inline struct blk_io_trace *bit_alloc(void)
671 {
672         struct blk_io_trace *bit = bit_alloc_list;
673
674         if (bit) {
675                 bit_alloc_list = (struct blk_io_trace *) (unsigned long) \
676                                  bit->time;
677                 bit_alloc_cache--;
678                 return bit;
679         }
680
681         return malloc(sizeof(*bit));
682 }
683
684 static inline void __put_trace_last(struct per_dev_info *pdi, struct trace *t)
685 {
686         struct per_cpu_info *pci = get_cpu_info(pdi, t->bit->cpu);
687
688         rb_erase(&t->rb_node, &pci->rb_last);
689         pci->rb_last_entries--;
690
691         bit_free(t->bit);
692         t_free(t);
693 }
694
695 static void put_trace(struct per_dev_info *pdi, struct trace *t)
696 {
697         rb_erase(&t->rb_node, &rb_sort_root);
698         rb_sort_entries--;
699
700         trace_rb_insert_last(pdi, t);
701 }
702
703 static inline int trace_rb_insert(struct trace *t, struct rb_root *root)
704 {
705         struct rb_node **p = &root->rb_node;
706         struct rb_node *parent = NULL;
707         struct trace *__t;
708
709         while (*p) {
710                 parent = *p;
711
712                 __t = rb_entry(parent, struct trace, rb_node);
713
714                 if (t->bit->time < __t->bit->time)
715                         p = &(*p)->rb_left;
716                 else if (t->bit->time > __t->bit->time)
717                         p = &(*p)->rb_right;
718                 else if (t->bit->device < __t->bit->device)
719                         p = &(*p)->rb_left;
720                 else if (t->bit->device > __t->bit->device)
721                         p = &(*p)->rb_right;
722                 else if (t->bit->sequence < __t->bit->sequence)
723                         p = &(*p)->rb_left;
724                 else    /* >= sequence */
725                         p = &(*p)->rb_right;
726         }
727
728         rb_link_node(&t->rb_node, parent, p);
729         rb_insert_color(&t->rb_node, root);
730         return 0;
731 }
732
733 static inline int trace_rb_insert_sort(struct trace *t)
734 {
735         if (!trace_rb_insert(t, &rb_sort_root)) {
736                 rb_sort_entries++;
737                 return 0;
738         }
739
740         return 1;
741 }
742
743 static int trace_rb_insert_last(struct per_dev_info *pdi, struct trace *t)
744 {
745         struct per_cpu_info *pci = get_cpu_info(pdi, t->bit->cpu);
746
747         if (trace_rb_insert(t, &pci->rb_last))
748                 return 1;
749
750         pci->rb_last_entries++;
751
752         if (pci->rb_last_entries > rb_batch * pdi->nfiles) {
753                 struct rb_node *n = rb_first(&pci->rb_last);
754
755                 t = rb_entry(n, struct trace, rb_node);
756                 __put_trace_last(pdi, t);
757         }
758
759         return 0;
760 }
761
762 static struct trace *trace_rb_find(dev_t device, unsigned long sequence,
763                                    struct rb_root *root, int order)
764 {
765         struct rb_node *n = root->rb_node;
766         struct rb_node *prev = NULL;
767         struct trace *__t;
768
769         while (n) {
770                 __t = rb_entry(n, struct trace, rb_node);
771                 prev = n;
772
773                 if (device < __t->bit->device)
774                         n = n->rb_left;
775                 else if (device > __t->bit->device)
776                         n = n->rb_right;
777                 else if (sequence < __t->bit->sequence)
778                         n = n->rb_left;
779                 else if (sequence > __t->bit->sequence)
780                         n = n->rb_right;
781                 else
782                         return __t;
783         }
784
785         /*
786          * hack - the list may not be sequence ordered because some
787          * events don't have sequence and time matched. so we end up
788          * being a little off in the rb lookup here, because we don't
789          * know the time we are looking for. compensate by browsing
790          * a little ahead from the last entry to find the match
791          */
792         if (order && prev) {
793                 int max = 5;
794
795                 while (((n = rb_next(prev)) != NULL) && max--) {
796                         __t = rb_entry(n, struct trace, rb_node);
797
798                         if (__t->bit->device == device &&
799                             __t->bit->sequence == sequence)
800                                 return __t;
801
802                         prev = n;
803                 }
804         }
805
806         return NULL;
807 }
808
809 static inline struct trace *trace_rb_find_last(struct per_dev_info *pdi,
810                                                struct per_cpu_info *pci,
811                                                unsigned long seq)
812 {
813         return trace_rb_find(pdi->dev, seq, &pci->rb_last, 0);
814 }
815
816 static inline int track_rb_insert(struct per_dev_info *pdi,struct io_track *iot)
817 {
818         struct rb_node **p = &pdi->rb_track.rb_node;
819         struct rb_node *parent = NULL;
820         struct io_track *__iot;
821
822         while (*p) {
823                 parent = *p;
824                 __iot = rb_entry(parent, struct io_track, rb_node);
825
826                 if (iot->sector < __iot->sector)
827                         p = &(*p)->rb_left;
828                 else if (iot->sector > __iot->sector)
829                         p = &(*p)->rb_right;
830                 else {
831                         fprintf(stderr,
832                                 "sector alias (%Lu) on device %d,%d!\n",
833                                 (unsigned long long) iot->sector,
834                                 MAJOR(pdi->dev), MINOR(pdi->dev));
835                         return 1;
836                 }
837         }
838
839         rb_link_node(&iot->rb_node, parent, p);
840         rb_insert_color(&iot->rb_node, &pdi->rb_track);
841         return 0;
842 }
843
844 static struct io_track *__find_track(struct per_dev_info *pdi, __u64 sector)
845 {
846         struct rb_node *n = pdi->rb_track.rb_node;
847         struct io_track *__iot;
848
849         while (n) {
850                 __iot = rb_entry(n, struct io_track, rb_node);
851
852                 if (sector < __iot->sector)
853                         n = n->rb_left;
854                 else if (sector > __iot->sector)
855                         n = n->rb_right;
856                 else
857                         return __iot;
858         }
859
860         return NULL;
861 }
862
863 static struct io_track *find_track(struct per_dev_info *pdi, pid_t pid,
864                                    __u64 sector)
865 {
866         struct io_track *iot;
867
868         iot = __find_track(pdi, sector);
869         if (!iot) {
870                 iot = malloc(sizeof(*iot));
871                 iot->ppm = find_ppm(pid);
872                 iot->sector = sector;
873                 track_rb_insert(pdi, iot);
874         }
875
876         return iot;
877 }
878
879 static void log_track_frontmerge(struct per_dev_info *pdi,
880                                  struct blk_io_trace *t)
881 {
882         struct io_track *iot;
883
884         if (!track_ios)
885                 return;
886
887         iot = __find_track(pdi, t->sector + t_sec(t));
888         if (!iot) {
889                 if (verbose)
890                         fprintf(stderr, "merge not found for (%d,%d): %llu\n",
891                                 MAJOR(pdi->dev), MINOR(pdi->dev),
892                                 (unsigned long long) t->sector + t_sec(t));
893                 return;
894         }
895
896         rb_erase(&iot->rb_node, &pdi->rb_track);
897         iot->sector -= t_sec(t);
898         track_rb_insert(pdi, iot);
899 }
900
901 static void log_track_getrq(struct per_dev_info *pdi, struct blk_io_trace *t)
902 {
903         struct io_track *iot;
904
905         if (!track_ios)
906                 return;
907
908         iot = find_track(pdi, t->pid, t->sector);
909         iot->allocation_time = t->time;
910 }
911
912 static inline int is_remapper(struct per_dev_info *pdi)
913 {
914         int major = MAJOR(pdi->dev);
915
916         return (major == 253 || major == 9);
917 }
918
919 /*
920  * for md/dm setups, the interesting cycle is Q -> C. So track queueing
921  * time here, as dispatch time
922  */
923 static void log_track_queue(struct per_dev_info *pdi, struct blk_io_trace *t)
924 {
925         struct io_track *iot;
926
927         if (!track_ios)
928                 return;
929         if (!is_remapper(pdi))
930                 return;
931
932         iot = find_track(pdi, t->pid, t->sector);
933         iot->dispatch_time = t->time;
934 }
935
936 /*
937  * return time between rq allocation and insertion
938  */
939 static unsigned long long log_track_insert(struct per_dev_info *pdi,
940                                            struct blk_io_trace *t)
941 {
942         unsigned long long elapsed;
943         struct io_track *iot;
944
945         if (!track_ios)
946                 return -1;
947
948         iot = find_track(pdi, t->pid, t->sector);
949         iot->queue_time = t->time;
950
951         if (!iot->allocation_time)
952                 return -1;
953
954         elapsed = iot->queue_time - iot->allocation_time;
955
956         if (per_process_stats) {
957                 struct per_process_info *ppi = find_ppi(iot->ppm->pid);
958                 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
959
960                 if (ppi && elapsed > ppi->longest_allocation_wait[w])
961                         ppi->longest_allocation_wait[w] = elapsed;
962         }
963
964         return elapsed;
965 }
966
967 /*
968  * return time between queue and issue
969  */
970 static unsigned long long log_track_issue(struct per_dev_info *pdi,
971                                           struct blk_io_trace *t)
972 {
973         unsigned long long elapsed;
974         struct io_track *iot;
975
976         if (!track_ios)
977                 return -1;
978         if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
979                 return -1;
980
981         iot = __find_track(pdi, t->sector);
982         if (!iot) {
983                 if (verbose)
984                         fprintf(stderr, "issue not found for (%d,%d): %llu\n",
985                                 MAJOR(pdi->dev), MINOR(pdi->dev),
986                                 (unsigned long long) t->sector);
987                 return -1;
988         }
989
990         iot->dispatch_time = t->time;
991         elapsed = iot->dispatch_time - iot->queue_time;
992
993         if (per_process_stats) {
994                 struct per_process_info *ppi = find_ppi(iot->ppm->pid);
995                 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
996
997                 if (ppi && elapsed > ppi->longest_dispatch_wait[w])
998                         ppi->longest_dispatch_wait[w] = elapsed;
999         }
1000
1001         return elapsed;
1002 }
1003
1004 /*
1005  * return time between dispatch and complete
1006  */
1007 static unsigned long long log_track_complete(struct per_dev_info *pdi,
1008                                              struct blk_io_trace *t)
1009 {
1010         unsigned long long elapsed;
1011         struct io_track *iot;
1012
1013         if (!track_ios)
1014                 return -1;
1015
1016         iot = __find_track(pdi, t->sector);
1017         if (!iot) {
1018                 if (verbose)
1019                         fprintf(stderr,"complete not found for (%d,%d): %llu\n",
1020                                 MAJOR(pdi->dev), MINOR(pdi->dev),
1021                                 (unsigned long long) t->sector);
1022                 return -1;
1023         }
1024
1025         iot->completion_time = t->time;
1026         elapsed = iot->completion_time - iot->dispatch_time;
1027
1028         if (per_process_stats) {
1029                 struct per_process_info *ppi = find_ppi(iot->ppm->pid);
1030                 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
1031
1032                 if (ppi && elapsed > ppi->longest_completion_wait[w])
1033                         ppi->longest_completion_wait[w] = elapsed;
1034         }
1035
1036         /*
1037          * kill the trace, we don't need it after completion
1038          */
1039         rb_erase(&iot->rb_node, &pdi->rb_track);
1040         free(iot);
1041
1042         return elapsed;
1043 }
1044
1045
1046 static struct io_stats *find_process_io_stats(pid_t pid)
1047 {
1048         struct per_process_info *ppi = find_ppi(pid);
1049
1050         if (!ppi) {
1051                 ppi = malloc(sizeof(*ppi));
1052                 memset(ppi, 0, sizeof(*ppi));
1053                 ppi->ppm = find_ppm(pid);
1054                 add_ppi_to_hash(ppi);
1055                 add_ppi_to_list(ppi);
1056         }
1057
1058         return &ppi->io_stats;
1059 }
1060
1061 static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size)
1062 {
1063         if (pdi->name)
1064                 snprintf(buffer, size, "%s", pdi->name);
1065         else
1066                 snprintf(buffer, size, "%d,%d",MAJOR(pdi->dev),MINOR(pdi->dev));
1067         return buffer;
1068 }
1069
1070 static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit)
1071 {
1072         unsigned long long this = bit->time;
1073         unsigned long long last = pdi->last_reported_time;
1074
1075         pdi->backwards = (this < last) ? 'B' : ' ';
1076         pdi->last_reported_time = this;
1077 }
1078
1079 static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t,
1080                                int rw)
1081 {
1082         if (rw) {
1083                 ios->mwrites++;
1084                 ios->qwrite_kb += t_kb(t);
1085         } else {
1086                 ios->mreads++;
1087                 ios->qread_kb += t_kb(t);
1088         }
1089 }
1090
1091 static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci,
1092                              int rw)
1093 {
1094         __account_m(&pci->io_stats, t, rw);
1095
1096         if (per_process_stats) {
1097                 struct io_stats *ios = find_process_io_stats(t->pid);
1098
1099                 __account_m(ios, t, rw);
1100         }
1101 }
1102
1103 static inline void __account_queue(struct io_stats *ios, struct blk_io_trace *t,
1104                                    int rw)
1105 {
1106         if (rw) {
1107                 ios->qwrites++;
1108                 ios->qwrite_kb += t_kb(t);
1109         } else {
1110                 ios->qreads++;
1111                 ios->qread_kb += t_kb(t);
1112         }
1113 }
1114
1115 static inline void account_queue(struct blk_io_trace *t,
1116                                  struct per_cpu_info *pci, int rw)
1117 {
1118         __account_queue(&pci->io_stats, t, rw);
1119
1120         if (per_process_stats) {
1121                 struct io_stats *ios = find_process_io_stats(t->pid);
1122
1123                 __account_queue(ios, t, rw);
1124         }
1125 }
1126
1127 static inline void __account_c(struct io_stats *ios, int rw, int bytes)
1128 {
1129         if (rw) {
1130                 ios->cwrites++;
1131                 ios->cwrite_kb += bytes >> 10;
1132         } else {
1133                 ios->creads++;
1134                 ios->cread_kb += bytes >> 10;
1135         }
1136 }
1137
1138 static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci,
1139                              int rw, int bytes)
1140 {
1141         __account_c(&pci->io_stats, rw, bytes);
1142
1143         if (per_process_stats) {
1144                 struct io_stats *ios = find_process_io_stats(t->pid);
1145
1146                 __account_c(ios, rw, bytes);
1147         }
1148 }
1149
1150 static inline void __account_issue(struct io_stats *ios, int rw,
1151                                    unsigned int bytes)
1152 {
1153         if (rw) {
1154                 ios->iwrites++;
1155                 ios->iwrite_kb += bytes >> 10;
1156         } else {
1157                 ios->ireads++;
1158                 ios->iread_kb += bytes >> 10;
1159         }
1160 }
1161
1162 static inline void account_issue(struct blk_io_trace *t,
1163                                  struct per_cpu_info *pci, int rw)
1164 {
1165         __account_issue(&pci->io_stats, rw, t->bytes);
1166
1167         if (per_process_stats) {
1168                 struct io_stats *ios = find_process_io_stats(t->pid);
1169
1170                 __account_issue(ios, rw, t->bytes);
1171         }
1172 }
1173
1174 static inline void __account_unplug(struct io_stats *ios, int timer)
1175 {
1176         if (timer)
1177                 ios->timer_unplugs++;
1178         else
1179                 ios->io_unplugs++;
1180 }
1181
1182 static inline void account_unplug(struct blk_io_trace *t,
1183                                   struct per_cpu_info *pci, int timer)
1184 {
1185         __account_unplug(&pci->io_stats, timer);
1186
1187         if (per_process_stats) {
1188                 struct io_stats *ios = find_process_io_stats(t->pid);
1189
1190                 __account_unplug(ios, timer);
1191         }
1192 }
1193
1194 static inline void __account_requeue(struct io_stats *ios,
1195                                      struct blk_io_trace *t, int rw)
1196 {
1197         if (rw) {
1198                 ios->wrqueue++;
1199                 ios->iwrite_kb -= t_kb(t);
1200         } else {
1201                 ios->rrqueue++;
1202                 ios->iread_kb -= t_kb(t);
1203         }
1204 }
1205
1206 static inline void account_requeue(struct blk_io_trace *t,
1207                                    struct per_cpu_info *pci, int rw)
1208 {
1209         __account_requeue(&pci->io_stats, t, rw);
1210
1211         if (per_process_stats) {
1212                 struct io_stats *ios = find_process_io_stats(t->pid);
1213
1214                 __account_requeue(ios, t, rw);
1215         }
1216 }
1217
1218 static void log_complete(struct per_dev_info *pdi, struct per_cpu_info *pci,
1219                          struct blk_io_trace *t, char *act)
1220 {
1221         process_fmt(act, pci, t, log_track_complete(pdi, t), 0, NULL);
1222 }
1223
1224 static void log_insert(struct per_dev_info *pdi, struct per_cpu_info *pci,
1225                        struct blk_io_trace *t, char *act)
1226 {
1227         process_fmt(act, pci, t, log_track_insert(pdi, t), 0, NULL);
1228 }
1229
1230 static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t,
1231                       char *act)
1232 {
1233         process_fmt(act, pci, t, -1, 0, NULL);
1234 }
1235
1236 static void log_issue(struct per_dev_info *pdi, struct per_cpu_info *pci,
1237                       struct blk_io_trace *t, char *act)
1238 {
1239         process_fmt(act, pci, t, log_track_issue(pdi, t), 0, NULL);
1240 }
1241
1242 static void log_merge(struct per_dev_info *pdi, struct per_cpu_info *pci,
1243                       struct blk_io_trace *t, char *act)
1244 {
1245         if (act[0] == 'F')
1246                 log_track_frontmerge(pdi, t);
1247
1248         process_fmt(act, pci, t, -1ULL, 0, NULL);
1249 }
1250
1251 static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t,
1252                         char *act)
1253 {
1254         process_fmt(act, pci, t, -1ULL, 0, NULL);
1255 }
1256
1257 static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t,
1258                         char *act)
1259 {
1260         process_fmt(act, pci, t, -1ULL, 0, NULL);
1261 }
1262
1263 static void log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t,
1264                       char *act)
1265 {
1266         process_fmt(act, pci, t, -1ULL, 0, NULL);
1267 }
1268
1269 static void log_split(struct per_cpu_info *pci, struct blk_io_trace *t,
1270                       char *act)
1271 {
1272         process_fmt(act, pci, t, -1ULL, 0, NULL);
1273 }
1274
1275 static void log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act)
1276 {
1277         unsigned char *buf = (unsigned char *) t + sizeof(*t);
1278
1279         process_fmt(act, pci, t, -1ULL, t->pdu_len, buf);
1280 }
1281
1282 static void dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci)
1283 {
1284         int act = t->action & 0xffff;
1285
1286         switch (act) {
1287                 case __BLK_TA_QUEUE:
1288                         log_generic(pci, t, "Q");
1289                         break;
1290                 case __BLK_TA_GETRQ:
1291                         log_generic(pci, t, "G");
1292                         break;
1293                 case __BLK_TA_SLEEPRQ:
1294                         log_generic(pci, t, "S");
1295                         break;
1296                 case __BLK_TA_REQUEUE:
1297                         log_generic(pci, t, "R");
1298                         break;
1299                 case __BLK_TA_ISSUE:
1300                         log_pc(pci, t, "D");
1301                         break;
1302                 case __BLK_TA_COMPLETE:
1303                         log_pc(pci, t, "C");
1304                         break;
1305                 case __BLK_TA_INSERT:
1306                         log_pc(pci, t, "I");
1307                         break;
1308                 default:
1309                         fprintf(stderr, "Bad pc action %x\n", act);
1310                         break;
1311         }
1312 }
1313
1314 static void dump_trace_fs(struct blk_io_trace *t, struct per_dev_info *pdi,
1315                           struct per_cpu_info *pci)
1316 {
1317         int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
1318         int act = t->action & 0xffff;
1319
1320         switch (act) {
1321                 case __BLK_TA_QUEUE:
1322                         log_track_queue(pdi, t);
1323                         account_queue(t, pci, w);
1324                         log_queue(pci, t, "Q");
1325                         break;
1326                 case __BLK_TA_INSERT:
1327                         log_insert(pdi, pci, t, "I");
1328                         break;
1329                 case __BLK_TA_BACKMERGE:
1330                         account_m(t, pci, w);
1331                         log_merge(pdi, pci, t, "M");
1332                         break;
1333                 case __BLK_TA_FRONTMERGE:
1334                         account_m(t, pci, w);
1335                         log_merge(pdi, pci, t, "F");
1336                         break;
1337                 case __BLK_TA_GETRQ:
1338                         log_track_getrq(pdi, t);
1339                         log_generic(pci, t, "G");
1340                         break;
1341                 case __BLK_TA_SLEEPRQ:
1342                         log_generic(pci, t, "S");
1343                         break;
1344                 case __BLK_TA_REQUEUE:
1345                         /*
1346                          * can happen if we miss traces, don't let it go
1347                          * below zero
1348                          */
1349                         if (pdi->cur_depth[w])
1350                                 pdi->cur_depth[w]--;
1351                         account_requeue(t, pci, w);
1352                         log_queue(pci, t, "R");
1353                         break;
1354                 case __BLK_TA_ISSUE:
1355                         account_issue(t, pci, w);
1356                         pdi->cur_depth[w]++;
1357                         if (pdi->cur_depth[w] > pdi->max_depth[w])
1358                                 pdi->max_depth[w] = pdi->cur_depth[w];
1359                         log_issue(pdi, pci, t, "D");
1360                         break;
1361                 case __BLK_TA_COMPLETE:
1362                         if (pdi->cur_depth[w])
1363                                 pdi->cur_depth[w]--;
1364                         account_c(t, pci, w, t->bytes);
1365                         log_complete(pdi, pci, t, "C");
1366                         break;
1367                 case __BLK_TA_PLUG:
1368                         log_action(pci, t, "P");
1369                         break;
1370                 case __BLK_TA_UNPLUG_IO:
1371                         account_unplug(t, pci, 0);
1372                         log_unplug(pci, t, "U");
1373                         break;
1374                 case __BLK_TA_UNPLUG_TIMER:
1375                         account_unplug(t, pci, 1);
1376                         log_unplug(pci, t, "UT");
1377                         break;
1378                 case __BLK_TA_SPLIT:
1379                         log_split(pci, t, "X");
1380                         break;
1381                 case __BLK_TA_BOUNCE:
1382                         log_generic(pci, t, "B");
1383                         break;
1384                 case __BLK_TA_REMAP:
1385                         log_generic(pci, t, "A");
1386                         break;
1387                 default:
1388                         fprintf(stderr, "Bad fs action %x\n", t->action);
1389                         break;
1390         }
1391 }
1392
1393 static void dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
1394                        struct per_dev_info *pdi)
1395 {
1396         if (t->action & BLK_TC_ACT(BLK_TC_PC))
1397                 dump_trace_pc(t, pci);
1398         else
1399                 dump_trace_fs(t, pdi, pci);
1400
1401         if (!pdi->events)
1402                 pdi->first_reported_time = t->time;
1403
1404         pdi->events++;
1405 }
1406
1407 /*
1408  * print in a proper way, not too small and not too big. if more than
1409  * 1000,000K, turn into M and so on
1410  */
1411 static char *size_cnv(char *dst, unsigned long long num, int in_kb)
1412 {
1413         char suff[] = { '\0', 'K', 'M', 'G', 'P' };
1414         unsigned int i = 0;
1415
1416         if (in_kb)
1417                 i++;
1418
1419         while (num > 1000 * 1000ULL && (i < sizeof(suff) - 1)) {
1420                 i++;
1421                 num /= 1000;
1422         }
1423
1424         sprintf(dst, "%'8Lu%c", num, suff[i]);
1425         return dst;
1426 }
1427
1428 static void dump_io_stats(struct per_dev_info *pdi, struct io_stats *ios,
1429                           char *msg)
1430 {
1431         static char x[256], y[256];
1432
1433         fprintf(ofp, "%s\n", msg);
1434
1435         fprintf(ofp, " Reads Queued:    %s, %siB\t", size_cnv(x, ios->qreads, 0), size_cnv(y, ios->qread_kb, 1));
1436         fprintf(ofp, " Writes Queued:    %s, %siB\n", size_cnv(x, ios->qwrites, 0), size_cnv(y, ios->qwrite_kb, 1));
1437
1438         fprintf(ofp, " Read Dispatches: %s, %siB\t", size_cnv(x, ios->ireads, 0), size_cnv(y, ios->iread_kb, 1));
1439         fprintf(ofp, " Write Dispatches: %s, %siB\n", size_cnv(x, ios->iwrites, 0), size_cnv(y, ios->iwrite_kb, 1));
1440         fprintf(ofp, " Reads Requeued:  %s\t\t", size_cnv(x, ios->rrqueue, 0));
1441         fprintf(ofp, " Writes Requeued:  %s\n", size_cnv(x, ios->wrqueue, 0));
1442         fprintf(ofp, " Reads Completed: %s, %siB\t", size_cnv(x, ios->creads, 0), size_cnv(y, ios->cread_kb, 1));
1443         fprintf(ofp, " Writes Completed: %s, %siB\n", size_cnv(x, ios->cwrites, 0), size_cnv(y, ios->cwrite_kb, 1));
1444         fprintf(ofp, " Read Merges:     %'8lu%8c\t", ios->mreads, ' ');
1445         fprintf(ofp, " Write Merges:     %'8lu\n", ios->mwrites);
1446         if (pdi) {
1447                 fprintf(ofp, " Read depth:      %'8u%8c\t", pdi->max_depth[0], ' ');
1448                 fprintf(ofp, " Write depth:      %'8u\n", pdi->max_depth[1]);
1449         }
1450         fprintf(ofp, " IO unplugs:      %'8lu%8c\t", ios->io_unplugs, ' ');
1451         fprintf(ofp, " Timer unplugs:    %'8lu\n", ios->timer_unplugs);
1452 }
1453
1454 static void dump_wait_stats(struct per_process_info *ppi)
1455 {
1456         unsigned long rawait = ppi->longest_allocation_wait[0] / 1000;
1457         unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000;
1458         unsigned long rcwait = ppi->longest_completion_wait[0] / 1000;
1459         unsigned long wawait = ppi->longest_allocation_wait[1] / 1000;
1460         unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000;
1461         unsigned long wcwait = ppi->longest_completion_wait[1] / 1000;
1462
1463         fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' ');
1464         fprintf(ofp, " Allocation wait:  %'8lu\n", wawait);
1465         fprintf(ofp, " Dispatch wait:   %'8lu%8c\t", rdwait, ' ');
1466         fprintf(ofp, " Dispatch wait:    %'8lu\n", wdwait);
1467         fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' ');
1468         fprintf(ofp, " Completion wait:  %'8lu\n", wcwait);
1469 }
1470
1471 static int ppi_name_compare(const void *p1, const void *p2)
1472 {
1473         struct per_process_info *ppi1 = *((struct per_process_info **) p1);
1474         struct per_process_info *ppi2 = *((struct per_process_info **) p2);
1475         int res;
1476
1477         res = strverscmp(ppi1->ppm->comm, ppi2->ppm->comm);
1478         if (!res)
1479                 res = ppi1->ppm->pid > ppi2->ppm->pid;
1480
1481         return res;
1482 }
1483
1484 static void sort_process_list(void)
1485 {
1486         struct per_process_info **ppis;
1487         struct per_process_info *ppi;
1488         int i = 0;
1489
1490         ppis = malloc(ppi_list_entries * sizeof(struct per_process_info *));
1491
1492         ppi = ppi_list;
1493         while (ppi) {
1494                 ppis[i++] = ppi;
1495                 ppi = ppi->list_next;
1496         }
1497
1498         qsort(ppis, ppi_list_entries, sizeof(ppi), ppi_name_compare);
1499
1500         i = ppi_list_entries - 1;
1501         ppi_list = NULL;
1502         while (i >= 0) {
1503                 ppi = ppis[i];
1504
1505                 ppi->list_next = ppi_list;
1506                 ppi_list = ppi;
1507                 i--;
1508         }
1509
1510         free(ppis);
1511 }
1512
1513 static void show_process_stats(void)
1514 {
1515         struct per_process_info *ppi;
1516
1517         sort_process_list();
1518
1519         ppi = ppi_list;
1520         while (ppi) {
1521                 struct process_pid_map *ppm = ppi->ppm;
1522                 char name[64];
1523
1524                 if (ppi->more_than_one)
1525                         sprintf(name, "%s (%u, ...)", ppm->comm, ppm->pid);
1526                 else
1527                         sprintf(name, "%s (%u)", ppm->comm, ppm->pid);
1528
1529                 dump_io_stats(NULL, &ppi->io_stats, name);
1530                 dump_wait_stats(ppi);
1531                 ppi = ppi->list_next;
1532         }
1533
1534         fprintf(ofp, "\n");
1535 }
1536
1537 static void show_device_and_cpu_stats(void)
1538 {
1539         struct per_dev_info *pdi;
1540         struct per_cpu_info *pci;
1541         struct io_stats total, *ios;
1542         unsigned long long rrate, wrate, msec;
1543         int i, j, pci_events;
1544         char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3];
1545         char name[32];
1546
1547         for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
1548
1549                 memset(&total, 0, sizeof(total));
1550                 pci_events = 0;
1551
1552                 if (i > 0)
1553                         fprintf(ofp, "\n");
1554
1555                 for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) {
1556                         if (!pci->nelems)
1557                                 continue;
1558
1559                         ios = &pci->io_stats;
1560                         total.qreads += ios->qreads;
1561                         total.qwrites += ios->qwrites;
1562                         total.creads += ios->creads;
1563                         total.cwrites += ios->cwrites;
1564                         total.mreads += ios->mreads;
1565                         total.mwrites += ios->mwrites;
1566                         total.ireads += ios->ireads;
1567                         total.iwrites += ios->iwrites;
1568                         total.rrqueue += ios->rrqueue;
1569                         total.wrqueue += ios->wrqueue;
1570                         total.qread_kb += ios->qread_kb;
1571                         total.qwrite_kb += ios->qwrite_kb;
1572                         total.cread_kb += ios->cread_kb;
1573                         total.cwrite_kb += ios->cwrite_kb;
1574                         total.iread_kb += ios->iread_kb;
1575                         total.iwrite_kb += ios->iwrite_kb;
1576                         total.timer_unplugs += ios->timer_unplugs;
1577                         total.io_unplugs += ios->io_unplugs;
1578
1579                         snprintf(line, sizeof(line) - 1, "CPU%d (%s):",
1580                                  j, get_dev_name(pdi, name, sizeof(name)));
1581                         dump_io_stats(pdi, ios, line);
1582                         pci_events++;
1583                 }
1584
1585                 if (pci_events > 1) {
1586                         fprintf(ofp, "\n");
1587                         snprintf(line, sizeof(line) - 1, "Total (%s):",
1588                                  get_dev_name(pdi, name, sizeof(name)));
1589                         dump_io_stats(NULL, &total, line);
1590                 }
1591
1592                 wrate = rrate = 0;
1593                 msec = (pdi->last_reported_time - pdi->first_reported_time) / 1000000;
1594                 if (msec) {
1595                         rrate = 1000 * total.cread_kb / msec;
1596                         wrate = 1000 * total.cwrite_kb / msec;
1597                 }
1598
1599                 fprintf(ofp, "\nThroughput (R/W): %'LuKiB/s / %'LuKiB/s\n",
1600                         rrate, wrate);
1601                 fprintf(ofp, "Events (%s): %'Lu entries\n",
1602                         get_dev_name(pdi, line, sizeof(line)), pdi->events);
1603
1604                 collect_pdi_skips(pdi);
1605                 fprintf(ofp, "Skips: %'lu forward (%'llu - %5.1lf%%)\n",
1606                         pdi->skips,pdi->seq_skips,
1607                         100.0 * ((double)pdi->seq_skips /
1608                                 (double)(pdi->events + pdi->seq_skips)));
1609         }
1610 }
1611
1612 static void find_genesis(void)
1613 {
1614         struct trace *t = trace_list;
1615
1616         genesis_time = -1ULL;
1617         while (t != NULL) {
1618                 if (t->bit->time < genesis_time)
1619                         genesis_time = t->bit->time;
1620
1621                 t = t->next;
1622         }
1623 }
1624
1625 static inline int check_stopwatch(struct blk_io_trace *bit)
1626 {
1627         if (bit->time < stopwatch_end &&
1628             bit->time >= stopwatch_start)
1629                 return 0;
1630
1631         return 1;
1632 }
1633
1634 /*
1635  * return youngest entry read
1636  */
1637 static int sort_entries(unsigned long long *youngest)
1638 {
1639         struct per_dev_info *pdi = NULL;
1640         struct per_cpu_info *pci = NULL;
1641         struct trace *t;
1642
1643         if (!genesis_time)
1644                 find_genesis();
1645
1646         *youngest = 0;
1647         while ((t = trace_list) != NULL) {
1648                 struct blk_io_trace *bit = t->bit;
1649
1650                 trace_list = t->next;
1651
1652                 bit->time -= genesis_time;
1653
1654                 if (bit->time < *youngest || !*youngest)
1655                         *youngest = bit->time;
1656
1657                 if (!pdi || pdi->dev != bit->device) {
1658                         pdi = get_dev_info(bit->device);
1659                         pci = NULL;
1660                 }
1661
1662                 if (!pci || pci->cpu != bit->cpu)
1663                         pci = get_cpu_info(pdi, bit->cpu);
1664
1665                 if (bit->sequence < pci->smallest_seq_read)
1666                         pci->smallest_seq_read = bit->sequence;
1667
1668                 if (check_stopwatch(bit)) {
1669                         bit_free(bit);
1670                         t_free(t);
1671                         continue;
1672                 }
1673
1674                 if (trace_rb_insert_sort(t))
1675                         return -1;
1676         }
1677
1678         return 0;
1679 }
1680
1681 /*
1682  * to continue, we must have traces from all online cpus in the tree
1683  */
1684 static int check_cpu_map(struct per_dev_info *pdi)
1685 {
1686         unsigned long *cpu_map;
1687         struct rb_node *n;
1688         struct trace *__t;
1689         unsigned int i;
1690         int ret, cpu;
1691
1692         /*
1693          * create a map of the cpus we have traces for
1694          */
1695         cpu_map = malloc(pdi->cpu_map_max / sizeof(long));
1696         n = rb_first(&rb_sort_root);
1697         while (n) {
1698                 __t = rb_entry(n, struct trace, rb_node);
1699                 cpu = __t->bit->cpu;
1700
1701                 cpu_map[CPU_IDX(cpu)] |= (1UL << CPU_BIT(cpu));
1702                 n = rb_next(n);
1703         }
1704
1705         /*
1706          * we can't continue if pdi->cpu_map has entries set that we don't
1707          * have in the sort rbtree. the opposite is not a problem, though
1708          */
1709         ret = 0;
1710         for (i = 0; i < pdi->cpu_map_max / CPUS_PER_LONG; i++) {
1711                 if (pdi->cpu_map[i] & ~(cpu_map[i])) {
1712                         ret = 1;
1713                         break;
1714                 }
1715         }
1716
1717         free(cpu_map);
1718         return ret;
1719 }
1720
1721 static int check_sequence(struct per_dev_info *pdi, struct trace *t, int force)
1722 {
1723         struct blk_io_trace *bit = t->bit;
1724         unsigned long expected_sequence;
1725         struct per_cpu_info *pci;
1726         struct trace *__t;
1727
1728         pci = get_cpu_info(pdi, bit->cpu);
1729         expected_sequence = pci->last_sequence + 1;
1730
1731         if (!expected_sequence) {
1732                 /*
1733                  * 1 should be the first entry, just allow it
1734                  */
1735                 if (bit->sequence == 1)
1736                         return 0;
1737                 if (bit->sequence == pci->smallest_seq_read)
1738                         return 0;
1739
1740                 return check_cpu_map(pdi);
1741         }
1742
1743         if (bit->sequence == expected_sequence)
1744                 return 0;
1745
1746         /*
1747          * we may not have seen that sequence yet. if we are not doing
1748          * the final run, break and wait for more entries.
1749          */
1750         if (expected_sequence < pci->smallest_seq_read) {
1751                 __t = trace_rb_find_last(pdi, pci, expected_sequence);
1752                 if (!__t)
1753                         goto skip;
1754
1755                 __put_trace_last(pdi, __t);
1756                 return 0;
1757         } else if (!force) {
1758                 return 1;
1759         } else {
1760 skip:
1761                 if (check_current_skips(pci, bit->sequence))
1762                         return 0;
1763
1764                 if (expected_sequence < bit->sequence)
1765                         insert_skip(pci, expected_sequence, bit->sequence - 1);
1766                 return 0;
1767         }
1768 }
1769
1770 static void show_entries_rb(int force)
1771 {
1772         struct per_dev_info *pdi = NULL;
1773         struct per_cpu_info *pci = NULL;
1774         struct blk_io_trace *bit;
1775         struct rb_node *n;
1776         struct trace *t;
1777
1778         while ((n = rb_first(&rb_sort_root)) != NULL) {
1779                 if (is_done() && !force && !pipeline)
1780                         break;
1781
1782                 t = rb_entry(n, struct trace, rb_node);
1783                 bit = t->bit;
1784
1785                 if (read_sequence - t->read_sequence < 1 && !force)
1786                         break;
1787
1788                 if (!pdi || pdi->dev != bit->device) {
1789                         pdi = get_dev_info(bit->device);
1790                         pci = NULL;
1791                 }
1792
1793                 if (!pdi) {
1794                         fprintf(stderr, "Unknown device ID? (%d,%d)\n",
1795                                 MAJOR(bit->device), MINOR(bit->device));
1796                         break;
1797                 }
1798
1799                 if (check_sequence(pdi, t, force))
1800                         break;
1801
1802                 if (!force && bit->time > last_allowed_time)
1803                         break;
1804
1805                 check_time(pdi, bit);
1806
1807                 if (!pci || pci->cpu != bit->cpu)
1808                         pci = get_cpu_info(pdi, bit->cpu);
1809
1810                 pci->last_sequence = bit->sequence;
1811
1812                 pci->nelems++;
1813
1814                 if (bit->action & (act_mask << BLK_TC_SHIFT))
1815                         dump_trace(bit, pci, pdi);
1816
1817                 put_trace(pdi, t);
1818         }
1819 }
1820
1821 static int read_data(int fd, void *buffer, int bytes, int block, int *fdblock)
1822 {
1823         int ret, bytes_left, fl;
1824         void *p;
1825
1826         if (block != *fdblock) {
1827                 fl = fcntl(fd, F_GETFL);
1828
1829                 if (!block) {
1830                         *fdblock = 0;
1831                         fcntl(fd, F_SETFL, fl | O_NONBLOCK);
1832                 } else {
1833                         *fdblock = 1;
1834                         fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
1835                 }
1836         }
1837
1838         bytes_left = bytes;
1839         p = buffer;
1840         while (bytes_left > 0) {
1841                 ret = read(fd, p, bytes_left);
1842                 if (!ret)
1843                         return 1;
1844                 else if (ret < 0) {
1845                         if (errno != EAGAIN) {
1846                                 perror("read");
1847                                 return -1;
1848                         }
1849
1850                         /*
1851                          * never do partial reads. we can return if we
1852                          * didn't read anything and we should not block,
1853                          * otherwise wait for data
1854                          */
1855                         if ((bytes_left == bytes) && !block)
1856                                 return 1;
1857
1858                         usleep(10);
1859                         continue;
1860                 } else {
1861                         p += ret;
1862                         bytes_left -= ret;
1863                 }
1864         }
1865
1866         return 0;
1867 }
1868
1869 static inline __u16 get_pdulen(struct blk_io_trace *bit)
1870 {
1871         if (data_is_native)
1872                 return bit->pdu_len;
1873
1874         return __bswap_16(bit->pdu_len);
1875 }
1876
1877 static inline __u32 get_magic(struct blk_io_trace *bit)
1878 {
1879         if (data_is_native)
1880                 return bit->magic;
1881
1882         return __bswap_32(bit->magic);
1883 }
1884
1885 static int read_events(int fd, int always_block, int *fdblock)
1886 {
1887         struct per_dev_info *pdi = NULL;
1888         unsigned int events = 0;
1889
1890         while (!is_done() && events < rb_batch) {
1891                 struct blk_io_trace *bit;
1892                 struct trace *t;
1893                 int pdu_len, should_block, ret;
1894                 __u32 magic;
1895
1896                 bit = bit_alloc();
1897
1898                 should_block = !events || always_block;
1899
1900                 ret = read_data(fd, bit, sizeof(*bit), should_block, fdblock);
1901                 if (ret) {
1902                         bit_free(bit);
1903                         if (!events && ret < 0)
1904                                 events = ret;
1905                         break;
1906                 }
1907
1908                 /*
1909                  * look at first trace to check whether we need to convert
1910                  * data in the future
1911                  */
1912                 if (data_is_native == -1 && check_data_endianness(bit->magic))
1913                         break;
1914
1915                 magic = get_magic(bit);
1916                 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
1917                         fprintf(stderr, "Bad magic %x\n", magic);
1918                         break;
1919                 }
1920
1921                 pdu_len = get_pdulen(bit);
1922                 if (pdu_len) {
1923                         void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
1924
1925                         if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1, fdblock)) {
1926                                 bit_free(ptr);
1927                                 break;
1928                         }
1929
1930                         bit = ptr;
1931                 }
1932
1933                 trace_to_cpu(bit);
1934
1935                 if (verify_trace(bit)) {
1936                         bit_free(bit);
1937                         continue;
1938                 }
1939
1940                 /*
1941                  * not a real trace, so grab and handle it here
1942                  */
1943                 if (bit->action & BLK_TC_ACT(BLK_TC_NOTIFY)) {
1944                         add_ppm_hash(bit->pid, (char *) bit + sizeof(*bit));
1945                         continue;
1946                 }
1947
1948                 t = t_alloc();
1949                 memset(t, 0, sizeof(*t));
1950                 t->bit = bit;
1951                 t->read_sequence = read_sequence;
1952
1953                 t->next = trace_list;
1954                 trace_list = t;
1955
1956                 if (!pdi || pdi->dev != bit->device)
1957                         pdi = get_dev_info(bit->device);
1958
1959                 if (bit->time > pdi->last_read_time)
1960                         pdi->last_read_time = bit->time;
1961
1962                 events++;
1963         }
1964
1965         return events;
1966 }
1967
1968 static int do_file(void)
1969 {
1970         struct per_cpu_info *pci;
1971         struct per_dev_info *pdi;
1972         int i, j, events, events_added;
1973
1974         /*
1975          * first prepare all files for reading
1976          */
1977         for (i = 0; i < ndevices; i++) {
1978                 pdi = &devices[i];
1979                 pdi->nfiles = 0;
1980
1981                 for (j = 0;; j++) {
1982                         struct stat st;
1983                         int len = 0;
1984                         char *p, *dname;
1985
1986                         pci = get_cpu_info(pdi, j);
1987                         pci->cpu = j;
1988                         pci->fd = -1;
1989                         pci->fdblock = -1;
1990         
1991                         p = strdup(pdi->name);
1992                         dname = dirname(p);
1993                         if (strcmp(dname, ".")) {
1994                                 input_dir = dname;
1995                                 p = strdup(pdi->name);
1996                                 strcpy(pdi->name, basename(p));
1997                         }
1998                         free(p);
1999
2000                         if (input_dir)
2001                                 len = sprintf(pci->fname, "%s/", input_dir);
2002
2003                         snprintf(pci->fname + len, sizeof(pci->fname)-1-len,
2004                                  "%s.blktrace.%d", pdi->name, pci->cpu);
2005                         if (stat(pci->fname, &st) < 0)
2006                                 break;
2007                         if (st.st_size) {
2008                                 pci->fd = open(pci->fname, O_RDONLY);
2009                                 if (pci->fd < 0) {
2010                                         perror(pci->fname);
2011                                         continue;
2012                                 }
2013                         }
2014
2015                         printf("Input file %s added\n", pci->fname);
2016                         pdi->nfiles++;
2017                         cpu_mark_online(pdi, pci->cpu);
2018                 }
2019         }
2020
2021         /*
2022          * now loop over the files reading in the data
2023          */
2024         do {
2025                 unsigned long long youngest;
2026
2027                 events_added = 0;
2028                 last_allowed_time = -1ULL;
2029                 read_sequence++;
2030
2031                 for (i = 0; i < ndevices; i++) {
2032                         pdi = &devices[i];
2033                         pdi->last_read_time = -1ULL;
2034
2035                         for (j = 0; j < pdi->nfiles; j++) {
2036
2037                                 pci = get_cpu_info(pdi, j);
2038
2039                                 if (pci->fd == -1)
2040                                         continue;
2041
2042                                 pci->smallest_seq_read = -1;
2043
2044                                 events = read_events(pci->fd, 1, &pci->fdblock);
2045                                 if (events <= 0) {
2046                                         cpu_mark_offline(pdi, pci->cpu);
2047                                         close(pci->fd);
2048                                         pci->fd = -1;
2049                                         continue;
2050                                 }
2051
2052                                 if (pdi->last_read_time < last_allowed_time)
2053                                         last_allowed_time = pdi->last_read_time;
2054
2055                                 events_added += events;
2056                         }
2057                 }
2058
2059                 if (sort_entries(&youngest))
2060                         break;
2061
2062                 if (youngest > stopwatch_end)
2063                         break;
2064
2065                 show_entries_rb(0);
2066
2067         } while (events_added);
2068
2069         if (rb_sort_entries)
2070                 show_entries_rb(1);
2071
2072         return 0;
2073 }
2074
2075 static int do_stdin(void)
2076 {
2077         unsigned long long youngest;
2078         int fd, events, fdblock;
2079
2080         last_allowed_time = -1ULL;
2081         fd = dup(STDIN_FILENO);
2082         if (fd == -1) {
2083                 perror("dup stdin");
2084                 return -1;
2085         }
2086
2087         fdblock = -1;
2088         while ((events = read_events(fd, 0, &fdblock)) > 0) {
2089                 read_sequence++;
2090         
2091 #if 0
2092                 smallest_seq_read = -1U;
2093 #endif
2094
2095                 if (sort_entries(&youngest))
2096                         break;
2097
2098                 if (youngest > stopwatch_end)
2099                         break;
2100
2101                 show_entries_rb(0);
2102         }
2103
2104         if (rb_sort_entries)
2105                 show_entries_rb(1);
2106
2107         close(fd);
2108         return 0;
2109 }
2110
2111 static void show_stats(void)
2112 {
2113         if (!ofp)
2114                 return;
2115         if (stats_printed)
2116                 return;
2117
2118         stats_printed = 1;
2119
2120         if (per_process_stats)
2121                 show_process_stats();
2122
2123         if (per_device_and_cpu_stats)
2124                 show_device_and_cpu_stats();
2125
2126         fflush(ofp);
2127 }
2128
2129 static void handle_sigint(__attribute__((__unused__)) int sig)
2130 {
2131         done = 1;
2132 }
2133
2134 /*
2135  * Extract start and duration times from a string, allowing
2136  * us to specify a time interval of interest within a trace.
2137  * Format: "duration" (start is zero) or "start:duration".
2138  */
2139 static int find_stopwatch_interval(char *string)
2140 {
2141         double value;
2142         char *sp;
2143
2144         value = strtod(string, &sp);
2145         if (sp == string) {
2146                 fprintf(stderr,"Invalid stopwatch timer: %s\n", string);
2147                 return 1;
2148         }
2149         if (*sp == ':') {
2150                 stopwatch_start = DOUBLE_TO_NANO_ULL(value);
2151                 string = sp + 1;
2152                 value = strtod(string, &sp);
2153                 if (sp == string || *sp != '\0') {
2154                         fprintf(stderr,"Invalid stopwatch duration time: %s\n",
2155                                 string);
2156                         return 1;
2157                 }
2158         } else if (*sp != '\0') {
2159                 fprintf(stderr,"Invalid stopwatch start timer: %s\n", string);
2160                 return 1;
2161         }
2162         stopwatch_end = DOUBLE_TO_NANO_ULL(value);
2163         if (stopwatch_end <= stopwatch_start) {
2164                 fprintf(stderr, "Invalid stopwatch interval: %Lu -> %Lu\n",
2165                         stopwatch_start, stopwatch_end);
2166                 return 1;
2167         }
2168
2169         return 0;
2170 }
2171
2172 static char usage_str[] = \
2173         "[ -i <input name> ] [-o <output name> [ -s ] [ -t ] [ -q ]\n" \
2174         "[ -w start:stop ] [ -f output format ] [ -F format spec ] [ -v] \n\n" \
2175         "\t-i Input file containing trace data, or '-' for stdin\n" \
2176         "\t-D Directory to prepend to input file names\n" \
2177         "\t-o Output file. If not given, output is stdout\n" \
2178         "\t-b stdin read batching\n" \
2179         "\t-s Show per-program io statistics\n" \
2180         "\t-h Hash processes by name, not pid\n" \
2181         "\t-t Track individual ios. Will tell you the time a request took\n" \
2182         "\t   to get queued, to get dispatched, and to get completed\n" \
2183         "\t-q Quiet. Don't display any stats at the end of the trace\n" \
2184         "\t-w Only parse data between the given time interval in seconds.\n" \
2185         "\t   If 'start' isn't given, blkparse defaults the start time to 0\n" \
2186         "\t-f Output format. Customize the output format. The format field\n" \
2187         "\t   identifies can be found in the documentation\n" \
2188         "\t-F Format specification. Can be found in the documentation\n" \
2189         "\t-v More verbose for marginal errors\n" \
2190         "\t-V Print program version info\n\n";
2191
2192 static void usage(char *prog)
2193 {
2194         fprintf(stderr, "Usage: %s %s %s", prog, blkparse_version, usage_str);
2195 }
2196
2197 int main(int argc, char *argv[])
2198 {
2199         char *ofp_buffer;
2200         int i, c, ret, mode;
2201         int act_mask_tmp = 0;
2202
2203         while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
2204                 switch (c) {
2205                 case 'a':
2206                         i = find_mask_map(optarg);
2207                         if (i < 0) {
2208                                 fprintf(stderr,"Invalid action mask %s\n",
2209                                         optarg);
2210                                 return 1;
2211                         }
2212                         act_mask_tmp |= i;
2213                         break;
2214
2215                 case 'A':
2216                         if ((sscanf(optarg, "%x", &i) != 1) || 
2217                                                         !valid_act_opt(i)) {
2218                                 fprintf(stderr,
2219                                         "Invalid set action mask %s/0x%x\n",
2220                                         optarg, i);
2221                                 return 1;
2222                         }
2223                         act_mask_tmp = i;
2224                         break;
2225                 case 'i':
2226                         if (!strcmp(optarg, "-") && !pipeline)
2227                                 pipeline = 1;
2228                         else if (resize_devices(optarg) != 0)
2229                                 return 1;
2230                         break;
2231                 case 'D':
2232                         input_dir = optarg;
2233                         break;
2234                 case 'o':
2235                         output_name = optarg;
2236                         break;
2237                 case 'b':
2238                         rb_batch = atoi(optarg);
2239                         if (rb_batch <= 0)
2240                                 rb_batch = RB_BATCH_DEFAULT;
2241                         break;
2242                 case 's':
2243                         per_process_stats = 1;
2244                         break;
2245                 case 't':
2246                         track_ios = 1;
2247                         break;
2248                 case 'q':
2249                         per_device_and_cpu_stats = 0;
2250                         break;
2251                 case 'w':
2252                         if (find_stopwatch_interval(optarg) != 0)
2253                                 return 1;
2254                         break;
2255                 case 'f':
2256                         set_all_format_specs(optarg);
2257                         break;
2258                 case 'F':
2259                         if (add_format_spec(optarg) != 0)
2260                                 return 1;
2261                         break;
2262                 case 'h':
2263                         ppi_hash_by_pid = 0;
2264                         break;
2265                 case 'v':
2266                         verbose++;
2267                         break;
2268                 case 'V':
2269                         printf("%s version %s\n", argv[0], blkparse_version);
2270                         return 0;
2271                 default:
2272                         usage(argv[0]);
2273                         return 1;
2274                 }
2275         }
2276
2277         while (optind < argc) {
2278                 if (!strcmp(argv[optind], "-") && !pipeline)
2279                         pipeline = 1;
2280                 else if (resize_devices(argv[optind]) != 0)
2281                         return 1;
2282                 optind++;
2283         }
2284
2285         if (!pipeline && !ndevices) {
2286                 usage(argv[0]);
2287                 return 1;
2288         }
2289
2290         if (act_mask_tmp != 0)
2291                 act_mask = act_mask_tmp;
2292
2293         memset(&rb_sort_root, 0, sizeof(rb_sort_root));
2294
2295         signal(SIGINT, handle_sigint);
2296         signal(SIGHUP, handle_sigint);
2297         signal(SIGTERM, handle_sigint);
2298
2299         setlocale(LC_NUMERIC, "en_US");
2300
2301         if (!output_name) {
2302                 ofp = fdopen(STDOUT_FILENO, "w");
2303                 mode = _IOLBF;
2304         } else {
2305                 char ofname[128];
2306
2307                 snprintf(ofname, sizeof(ofname) - 1, "%s", output_name);
2308                 ofp = fopen(ofname, "w");
2309                 mode = _IOFBF;
2310         }
2311
2312         if (!ofp) {
2313                 perror("fopen");
2314                 return 1;
2315         }
2316
2317         ofp_buffer = malloc(4096);      
2318         if (setvbuf(ofp, ofp_buffer, mode, 4096)) {
2319                 perror("setvbuf");
2320                 return 1;
2321         }
2322
2323         if (pipeline)
2324                 ret = do_stdin();
2325         else
2326                 ret = do_file();
2327
2328         show_stats();
2329         free(ofp_buffer);
2330         return ret;
2331 }