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