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