2 * block queue tracing parse application
4 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
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.
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.
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
21 #include <sys/types.h>
38 static char blkparse_version[] = "0.99";
41 unsigned long start, end;
42 struct skip_info *prev, *next;
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;
56 unsigned long long seq_skips;
57 unsigned int max_depth[2];
58 unsigned int cur_depth[2];
60 struct rb_root rb_track;
65 unsigned long *cpu_map;
66 unsigned int cpu_map_max;
68 struct per_cpu_info *cpus;
72 * some duplicated effort here, we can unify this hash and the ppi hash later
74 struct process_pid_map {
77 struct process_pid_map *hash_next, *list_next;
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];
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;
94 unsigned long long longest_allocation_wait[2];
95 unsigned long long longest_dispatch_wait[2];
96 unsigned long long longest_completion_wait[2];
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;
106 #define S_OPTS "a:A:i:o:b:stqw:f:F:vVhD:"
107 static struct option l_opts[] = {
110 .has_arg = required_argument,
116 .has_arg = required_argument,
122 .has_arg = required_argument,
128 .has_arg = required_argument,
134 .has_arg = required_argument,
139 .name = "per-program-stats",
140 .has_arg = no_argument,
146 .has_arg = no_argument,
152 .has_arg = no_argument,
158 .has_arg = required_argument,
164 .has_arg = required_argument,
169 .name = "format-spec",
170 .has_arg = required_argument,
175 .name = "hash-by-name",
176 .has_arg = no_argument,
182 .has_arg = no_argument,
188 .has_arg = no_argument,
193 .name = "input-directory",
194 .has_arg = required_argument,
204 * for sorting the displayed output
207 struct blk_io_trace *bit;
208 struct rb_node rb_node;
210 unsigned long read_sequence;
213 static struct rb_root rb_sort_root;
214 static unsigned long rb_sort_entries;
216 static struct trace *trace_list;
221 static struct blk_io_trace *bit_alloc_list;
222 static struct trace *t_alloc_list;
225 * for tracking individual ios
228 struct rb_node rb_node;
230 struct process_pid_map *ppm;
232 unsigned long long allocation_time;
233 unsigned long long queue_time;
234 unsigned long long dispatch_time;
235 unsigned long long completion_time;
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 *);
244 static char *output_name;
245 static char *input_dir;
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;
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;
258 static unsigned int act_mask = -1U;
259 static int stats_printed;
260 int data_is_native = -1;
262 static unsigned int t_alloc_cache;
263 static unsigned int bit_alloc_cache;
265 #define RB_BATCH_DEFAULT (512)
266 static unsigned int rb_batch = RB_BATCH_DEFAULT;
270 #define is_done() (*(volatile int *)(&done))
271 static volatile int done;
273 #define JHASH_RANDOM (0x3af5f2ee)
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))
279 static void resize_cpu_info(struct per_dev_info *pdi, int cpu)
281 struct per_cpu_info *cpus = pdi->cpus;
282 int ncpus = pdi->ncpus;
283 int new_count = cpu + 1;
287 size = new_count * sizeof(struct per_cpu_info);
288 cpus = realloc(cpus, size);
291 fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n",
292 get_dev_name(pdi, name, sizeof(name)), size);
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);
300 pdi->ncpus = new_count;
303 for (new_count = 0; new_count < pdi->ncpus; new_count++) {
304 struct per_cpu_info *pci = &pdi->cpus[new_count];
308 memset(&pci->rb_last, 0, sizeof(pci->rb_last));
309 pci->rb_last_entries = 0;
310 pci->last_sequence = -1;
315 static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu)
317 struct per_cpu_info *pci;
319 if (cpu >= pdi->ncpus)
320 resize_cpu_info(pdi, cpu);
322 pci = &pdi->cpus[cpu];
328 static int resize_devices(char *name)
330 int size = (ndevices + 1) * sizeof(struct per_dev_info);
332 devices = realloc(devices, size);
334 fprintf(stderr, "Out of memory, device %s (%d)\n", name, size);
337 memset(&devices[ndevices], 0, sizeof(struct per_dev_info));
338 devices[ndevices].name = name;
343 static struct per_dev_info *get_dev_info(dev_t dev)
345 struct per_dev_info *pdi;
348 for (i = 0; i < ndevices; i++) {
350 devices[i].dev = dev;
351 if (devices[i].dev == dev)
355 if (resize_devices(NULL))
358 pdi = &devices[ndevices - 1];
360 pdi->first_reported_time = 0;
361 pdi->last_read_time = 0;
366 static void insert_skip(struct per_cpu_info *pci, unsigned long start,
369 struct skip_info *sip;
371 for (sip = pci->skips_tail; sip != NULL; sip = sip->prev) {
372 if (end == (sip->start - 1)) {
375 } else if (start == (sip->end + 1)) {
381 sip = malloc(sizeof(struct skip_info));
384 sip->prev = sip->next = NULL;
385 if (pci->skips_tail == NULL)
386 pci->skips_head = pci->skips_tail = sip;
388 sip->prev = pci->skips_tail;
389 pci->skips_tail->next = sip;
390 pci->skips_tail = sip;
394 static void remove_sip(struct per_cpu_info *pci, struct skip_info *sip)
396 if (sip->prev == NULL) {
397 if (sip->next == NULL)
398 pci->skips_head = pci->skips_tail = NULL;
400 pci->skips_head = sip->next;
401 sip->next->prev = NULL;
403 } else if (sip->next == NULL) {
404 pci->skips_tail = sip->prev;
405 sip->prev->next = NULL;
407 sip->prev->next = sip->next;
408 sip->next->prev = sip->prev;
411 sip->prev = sip->next = NULL;
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)
418 struct skip_info *sip;
420 for (sip = pci->skips_tail; sip != NULL; sip = sip->prev) {
421 if (IN_SKIP(sip, seq)) {
422 if (sip->start == seq) {
424 remove_sip(pci, sip);
427 } else if (sip->end == seq)
431 insert_skip(pci, seq + 1, sip->end);
440 static void collect_pdi_skips(struct per_dev_info *pdi)
442 struct skip_info *sip;
448 for (cpu = 0; cpu < pdi->ncpus; cpu++) {
449 struct per_cpu_info *pci = &pdi->cpus[cpu];
451 for (sip = pci->skips_head; sip != NULL; sip = sip->next) {
453 pdi->seq_skips += (sip->end - sip->start + 1);
455 fprintf(stderr,"(%d,%d): skipping %lu -> %lu\n",
456 MAJOR(pdi->dev), MINOR(pdi->dev),
457 sip->start, sip->end);
462 static void cpu_mark_online(struct per_dev_info *pdi, unsigned int cpu)
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));
468 memset(map, 0, new_max / sizeof(long));
471 memcpy(map, pdi->cpu_map, pdi->cpu_map_max / sizeof(long));
476 pdi->cpu_map_max = new_max;
479 pdi->cpu_map[CPU_IDX(cpu)] |= (1UL << CPU_BIT(cpu));
482 static inline void cpu_mark_offline(struct per_dev_info *pdi, int cpu)
484 pdi->cpu_map[CPU_IDX(cpu)] &= ~(1UL << CPU_BIT(cpu));
487 static inline int cpu_is_online(struct per_dev_info *pdi, int cpu)
489 return (pdi->cpu_map[CPU_IDX(cpu)] & (1UL << CPU_BIT(cpu))) != 0;
492 static inline int ppm_hash_pid(pid_t pid)
494 return jhash_1word(pid, JHASH_RANDOM) & PPM_HASH_MASK;
497 static struct process_pid_map *find_ppm(pid_t pid)
499 const int hash_idx = ppm_hash_pid(pid);
500 struct process_pid_map *ppm;
502 ppm = ppm_hash_table[hash_idx];
507 ppm = ppm->hash_next;
513 static void add_ppm_hash(pid_t pid, const char *name)
515 const int hash_idx = ppm_hash_pid(pid);
516 struct process_pid_map *ppm;
520 ppm = malloc(sizeof(*ppm));
521 memset(ppm, 0, sizeof(*ppm));
523 strcpy(ppm->comm, name);
524 ppm->hash_next = ppm_hash_table[hash_idx];
525 ppm_hash_table[hash_idx] = ppm;
529 char *find_process_name(pid_t pid)
531 struct process_pid_map *ppm = find_ppm(pid);
539 static inline int ppi_hash_pid(pid_t pid)
541 return jhash_1word(pid, JHASH_RANDOM) & PPI_HASH_MASK;
544 static inline int ppi_hash_name(const char *name)
546 return jhash(name, 16, JHASH_RANDOM) & PPI_HASH_MASK;
549 static inline int ppi_hash(struct per_process_info *ppi)
551 struct process_pid_map *ppm = ppi->ppm;
554 return ppi_hash_pid(ppm->pid);
556 return ppi_hash_name(ppm->comm);
559 static inline void add_ppi_to_hash(struct per_process_info *ppi)
561 const int hash_idx = ppi_hash(ppi);
563 ppi->hash_next = ppi_hash_table[hash_idx];
564 ppi_hash_table[hash_idx] = ppi;
567 static inline void add_ppi_to_list(struct per_process_info *ppi)
569 ppi->list_next = ppi_list;
574 static struct per_process_info *find_ppi_by_name(char *name)
576 const int hash_idx = ppi_hash_name(name);
577 struct per_process_info *ppi;
579 ppi = ppi_hash_table[hash_idx];
581 struct process_pid_map *ppm = ppi->ppm;
583 if (!strcmp(ppm->comm, name))
586 ppi = ppi->hash_next;
592 static struct per_process_info *find_ppi_by_pid(pid_t pid)
594 const int hash_idx = ppi_hash_pid(pid);
595 struct per_process_info *ppi;
597 ppi = ppi_hash_table[hash_idx];
599 struct process_pid_map *ppm = ppi->ppm;
604 ppi = ppi->hash_next;
610 static struct per_process_info *find_ppi(pid_t pid)
612 struct per_process_info *ppi;
616 return find_ppi_by_pid(pid);
618 name = find_process_name(pid);
622 ppi = find_ppi_by_name(name);
623 if (ppi && ppi->ppm->pid != pid)
624 ppi->more_than_one = 1;
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
634 static inline void t_free(struct trace *t)
636 if (t_alloc_cache < 1024) {
637 t->next = t_alloc_list;
644 static inline struct trace *t_alloc(void)
646 struct trace *t = t_alloc_list;
649 t_alloc_list = t->next;
654 return malloc(sizeof(*t));
657 static inline void bit_free(struct blk_io_trace *bit)
659 if (bit_alloc_cache < 1024 && !bit->pdu_len) {
661 * abuse a 64-bit field for a next pointer for the free item
663 bit->time = (__u64) (unsigned long) bit_alloc_list;
664 bit_alloc_list = (struct blk_io_trace *) bit;
670 static inline struct blk_io_trace *bit_alloc(void)
672 struct blk_io_trace *bit = bit_alloc_list;
675 bit_alloc_list = (struct blk_io_trace *) (unsigned long) \
681 return malloc(sizeof(*bit));
684 static inline void __put_trace_last(struct per_dev_info *pdi, struct trace *t)
686 struct per_cpu_info *pci = get_cpu_info(pdi, t->bit->cpu);
688 rb_erase(&t->rb_node, &pci->rb_last);
689 pci->rb_last_entries--;
695 static void put_trace(struct per_dev_info *pdi, struct trace *t)
697 rb_erase(&t->rb_node, &rb_sort_root);
700 trace_rb_insert_last(pdi, t);
703 static inline int trace_rb_insert(struct trace *t, struct rb_root *root)
705 struct rb_node **p = &root->rb_node;
706 struct rb_node *parent = NULL;
712 __t = rb_entry(parent, struct trace, rb_node);
714 if (t->bit->time < __t->bit->time)
716 else if (t->bit->time > __t->bit->time)
718 else if (t->bit->device < __t->bit->device)
720 else if (t->bit->device > __t->bit->device)
722 else if (t->bit->sequence < __t->bit->sequence)
724 else /* >= sequence */
728 rb_link_node(&t->rb_node, parent, p);
729 rb_insert_color(&t->rb_node, root);
733 static inline int trace_rb_insert_sort(struct trace *t)
735 if (!trace_rb_insert(t, &rb_sort_root)) {
743 static int trace_rb_insert_last(struct per_dev_info *pdi, struct trace *t)
745 struct per_cpu_info *pci = get_cpu_info(pdi, t->bit->cpu);
747 if (trace_rb_insert(t, &pci->rb_last))
750 pci->rb_last_entries++;
752 if (pci->rb_last_entries > rb_batch * pdi->nfiles) {
753 struct rb_node *n = rb_first(&pci->rb_last);
755 t = rb_entry(n, struct trace, rb_node);
756 __put_trace_last(pdi, t);
762 static struct trace *trace_rb_find(dev_t device, unsigned long sequence,
763 struct rb_root *root, int order)
765 struct rb_node *n = root->rb_node;
766 struct rb_node *prev = NULL;
770 __t = rb_entry(n, struct trace, rb_node);
773 if (device < __t->bit->device)
775 else if (device > __t->bit->device)
777 else if (sequence < __t->bit->sequence)
779 else if (sequence > __t->bit->sequence)
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
795 while (((n = rb_next(prev)) != NULL) && max--) {
796 __t = rb_entry(n, struct trace, rb_node);
798 if (__t->bit->device == device &&
799 __t->bit->sequence == sequence)
809 static inline struct trace *trace_rb_find_last(struct per_dev_info *pdi,
810 struct per_cpu_info *pci,
813 return trace_rb_find(pdi->dev, seq, &pci->rb_last, 0);
816 static inline int track_rb_insert(struct per_dev_info *pdi,struct io_track *iot)
818 struct rb_node **p = &pdi->rb_track.rb_node;
819 struct rb_node *parent = NULL;
820 struct io_track *__iot;
824 __iot = rb_entry(parent, struct io_track, rb_node);
826 if (iot->sector < __iot->sector)
828 else if (iot->sector > __iot->sector)
832 "sector alias (%Lu) on device %d,%d!\n",
833 (unsigned long long) iot->sector,
834 MAJOR(pdi->dev), MINOR(pdi->dev));
839 rb_link_node(&iot->rb_node, parent, p);
840 rb_insert_color(&iot->rb_node, &pdi->rb_track);
844 static struct io_track *__find_track(struct per_dev_info *pdi, __u64 sector)
846 struct rb_node *n = pdi->rb_track.rb_node;
847 struct io_track *__iot;
850 __iot = rb_entry(n, struct io_track, rb_node);
852 if (sector < __iot->sector)
854 else if (sector > __iot->sector)
863 static struct io_track *find_track(struct per_dev_info *pdi, pid_t pid,
866 struct io_track *iot;
868 iot = __find_track(pdi, sector);
870 iot = malloc(sizeof(*iot));
871 iot->ppm = find_ppm(pid);
872 iot->sector = sector;
873 track_rb_insert(pdi, iot);
879 static void log_track_frontmerge(struct per_dev_info *pdi,
880 struct blk_io_trace *t)
882 struct io_track *iot;
887 iot = __find_track(pdi, t->sector + t_sec(t));
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));
896 rb_erase(&iot->rb_node, &pdi->rb_track);
897 iot->sector -= t_sec(t);
898 track_rb_insert(pdi, iot);
901 static void log_track_getrq(struct per_dev_info *pdi, struct blk_io_trace *t)
903 struct io_track *iot;
908 iot = find_track(pdi, t->pid, t->sector);
909 iot->allocation_time = t->time;
912 static inline int is_remapper(struct per_dev_info *pdi)
914 int major = MAJOR(pdi->dev);
916 return (major == 253 || major == 9);
920 * for md/dm setups, the interesting cycle is Q -> C. So track queueing
921 * time here, as dispatch time
923 static void log_track_queue(struct per_dev_info *pdi, struct blk_io_trace *t)
925 struct io_track *iot;
929 if (!is_remapper(pdi))
932 iot = find_track(pdi, t->pid, t->sector);
933 iot->dispatch_time = t->time;
937 * return time between rq allocation and insertion
939 static unsigned long long log_track_insert(struct per_dev_info *pdi,
940 struct blk_io_trace *t)
942 unsigned long long elapsed;
943 struct io_track *iot;
948 iot = find_track(pdi, t->pid, t->sector);
949 iot->queue_time = t->time;
951 if (!iot->allocation_time)
954 elapsed = iot->queue_time - iot->allocation_time;
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;
960 if (ppi && elapsed > ppi->longest_allocation_wait[w])
961 ppi->longest_allocation_wait[w] = elapsed;
968 * return time between queue and issue
970 static unsigned long long log_track_issue(struct per_dev_info *pdi,
971 struct blk_io_trace *t)
973 unsigned long long elapsed;
974 struct io_track *iot;
978 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
981 iot = __find_track(pdi, t->sector);
984 fprintf(stderr, "issue not found for (%d,%d): %llu\n",
985 MAJOR(pdi->dev), MINOR(pdi->dev),
986 (unsigned long long) t->sector);
990 iot->dispatch_time = t->time;
991 elapsed = iot->dispatch_time - iot->queue_time;
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;
997 if (ppi && elapsed > ppi->longest_dispatch_wait[w])
998 ppi->longest_dispatch_wait[w] = elapsed;
1005 * return time between dispatch and complete
1007 static unsigned long long log_track_complete(struct per_dev_info *pdi,
1008 struct blk_io_trace *t)
1010 unsigned long long elapsed;
1011 struct io_track *iot;
1016 iot = __find_track(pdi, t->sector);
1019 fprintf(stderr,"complete not found for (%d,%d): %llu\n",
1020 MAJOR(pdi->dev), MINOR(pdi->dev),
1021 (unsigned long long) t->sector);
1025 iot->completion_time = t->time;
1026 elapsed = iot->completion_time - iot->dispatch_time;
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;
1032 if (ppi && elapsed > ppi->longest_completion_wait[w])
1033 ppi->longest_completion_wait[w] = elapsed;
1037 * kill the trace, we don't need it after completion
1039 rb_erase(&iot->rb_node, &pdi->rb_track);
1046 static struct io_stats *find_process_io_stats(pid_t pid)
1048 struct per_process_info *ppi = find_ppi(pid);
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);
1058 return &ppi->io_stats;
1061 static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size)
1064 snprintf(buffer, size, "%s", pdi->name);
1066 snprintf(buffer, size, "%d,%d",MAJOR(pdi->dev),MINOR(pdi->dev));
1070 static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit)
1072 unsigned long long this = bit->time;
1073 unsigned long long last = pdi->last_reported_time;
1075 pdi->backwards = (this < last) ? 'B' : ' ';
1076 pdi->last_reported_time = this;
1079 static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t,
1084 ios->qwrite_kb += t_kb(t);
1087 ios->qread_kb += t_kb(t);
1091 static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci,
1094 __account_m(&pci->io_stats, t, rw);
1096 if (per_process_stats) {
1097 struct io_stats *ios = find_process_io_stats(t->pid);
1099 __account_m(ios, t, rw);
1103 static inline void __account_queue(struct io_stats *ios, struct blk_io_trace *t,
1108 ios->qwrite_kb += t_kb(t);
1111 ios->qread_kb += t_kb(t);
1115 static inline void account_queue(struct blk_io_trace *t,
1116 struct per_cpu_info *pci, int rw)
1118 __account_queue(&pci->io_stats, t, rw);
1120 if (per_process_stats) {
1121 struct io_stats *ios = find_process_io_stats(t->pid);
1123 __account_queue(ios, t, rw);
1127 static inline void __account_c(struct io_stats *ios, int rw, int bytes)
1131 ios->cwrite_kb += bytes >> 10;
1134 ios->cread_kb += bytes >> 10;
1138 static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci,
1141 __account_c(&pci->io_stats, rw, bytes);
1143 if (per_process_stats) {
1144 struct io_stats *ios = find_process_io_stats(t->pid);
1146 __account_c(ios, rw, bytes);
1150 static inline void __account_issue(struct io_stats *ios, int rw,
1155 ios->iwrite_kb += bytes >> 10;
1158 ios->iread_kb += bytes >> 10;
1162 static inline void account_issue(struct blk_io_trace *t,
1163 struct per_cpu_info *pci, int rw)
1165 __account_issue(&pci->io_stats, rw, t->bytes);
1167 if (per_process_stats) {
1168 struct io_stats *ios = find_process_io_stats(t->pid);
1170 __account_issue(ios, rw, t->bytes);
1174 static inline void __account_unplug(struct io_stats *ios, int timer)
1177 ios->timer_unplugs++;
1182 static inline void account_unplug(struct blk_io_trace *t,
1183 struct per_cpu_info *pci, int timer)
1185 __account_unplug(&pci->io_stats, timer);
1187 if (per_process_stats) {
1188 struct io_stats *ios = find_process_io_stats(t->pid);
1190 __account_unplug(ios, timer);
1194 static inline void __account_requeue(struct io_stats *ios,
1195 struct blk_io_trace *t, int rw)
1199 ios->iwrite_kb -= t_kb(t);
1202 ios->iread_kb -= t_kb(t);
1206 static inline void account_requeue(struct blk_io_trace *t,
1207 struct per_cpu_info *pci, int rw)
1209 __account_requeue(&pci->io_stats, t, rw);
1211 if (per_process_stats) {
1212 struct io_stats *ios = find_process_io_stats(t->pid);
1214 __account_requeue(ios, t, rw);
1218 static void log_complete(struct per_dev_info *pdi, struct per_cpu_info *pci,
1219 struct blk_io_trace *t, char *act)
1221 process_fmt(act, pci, t, log_track_complete(pdi, t), 0, NULL);
1224 static void log_insert(struct per_dev_info *pdi, struct per_cpu_info *pci,
1225 struct blk_io_trace *t, char *act)
1227 process_fmt(act, pci, t, log_track_insert(pdi, t), 0, NULL);
1230 static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t,
1233 process_fmt(act, pci, t, -1, 0, NULL);
1236 static void log_issue(struct per_dev_info *pdi, struct per_cpu_info *pci,
1237 struct blk_io_trace *t, char *act)
1239 process_fmt(act, pci, t, log_track_issue(pdi, t), 0, NULL);
1242 static void log_merge(struct per_dev_info *pdi, struct per_cpu_info *pci,
1243 struct blk_io_trace *t, char *act)
1246 log_track_frontmerge(pdi, t);
1248 process_fmt(act, pci, t, -1ULL, 0, NULL);
1251 static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t,
1254 process_fmt(act, pci, t, -1ULL, 0, NULL);
1257 static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t,
1260 process_fmt(act, pci, t, -1ULL, 0, NULL);
1263 static void log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t,
1266 process_fmt(act, pci, t, -1ULL, 0, NULL);
1269 static void log_split(struct per_cpu_info *pci, struct blk_io_trace *t,
1272 process_fmt(act, pci, t, -1ULL, 0, NULL);
1275 static void log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act)
1277 unsigned char *buf = (unsigned char *) t + sizeof(*t);
1279 process_fmt(act, pci, t, -1ULL, t->pdu_len, buf);
1282 static void dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci)
1284 int act = t->action & 0xffff;
1287 case __BLK_TA_QUEUE:
1288 log_generic(pci, t, "Q");
1290 case __BLK_TA_GETRQ:
1291 log_generic(pci, t, "G");
1293 case __BLK_TA_SLEEPRQ:
1294 log_generic(pci, t, "S");
1296 case __BLK_TA_REQUEUE:
1297 log_generic(pci, t, "R");
1299 case __BLK_TA_ISSUE:
1300 log_pc(pci, t, "D");
1302 case __BLK_TA_COMPLETE:
1303 log_pc(pci, t, "C");
1305 case __BLK_TA_INSERT:
1306 log_pc(pci, t, "I");
1309 fprintf(stderr, "Bad pc action %x\n", act);
1314 static void dump_trace_fs(struct blk_io_trace *t, struct per_dev_info *pdi,
1315 struct per_cpu_info *pci)
1317 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
1318 int act = t->action & 0xffff;
1321 case __BLK_TA_QUEUE:
1322 log_track_queue(pdi, t);
1323 account_queue(t, pci, w);
1324 log_queue(pci, t, "Q");
1326 case __BLK_TA_INSERT:
1327 log_insert(pdi, pci, t, "I");
1329 case __BLK_TA_BACKMERGE:
1330 account_m(t, pci, w);
1331 log_merge(pdi, pci, t, "M");
1333 case __BLK_TA_FRONTMERGE:
1334 account_m(t, pci, w);
1335 log_merge(pdi, pci, t, "F");
1337 case __BLK_TA_GETRQ:
1338 log_track_getrq(pdi, t);
1339 log_generic(pci, t, "G");
1341 case __BLK_TA_SLEEPRQ:
1342 log_generic(pci, t, "S");
1344 case __BLK_TA_REQUEUE:
1346 * can happen if we miss traces, don't let it go
1349 if (pdi->cur_depth[w])
1350 pdi->cur_depth[w]--;
1351 account_requeue(t, pci, w);
1352 log_queue(pci, t, "R");
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");
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");
1368 log_action(pci, t, "P");
1370 case __BLK_TA_UNPLUG_IO:
1371 account_unplug(t, pci, 0);
1372 log_unplug(pci, t, "U");
1374 case __BLK_TA_UNPLUG_TIMER:
1375 account_unplug(t, pci, 1);
1376 log_unplug(pci, t, "UT");
1378 case __BLK_TA_SPLIT:
1379 log_split(pci, t, "X");
1381 case __BLK_TA_BOUNCE:
1382 log_generic(pci, t, "B");
1384 case __BLK_TA_REMAP:
1385 log_generic(pci, t, "A");
1388 fprintf(stderr, "Bad fs action %x\n", t->action);
1393 static void dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
1394 struct per_dev_info *pdi)
1396 if (t->action & BLK_TC_ACT(BLK_TC_PC))
1397 dump_trace_pc(t, pci);
1399 dump_trace_fs(t, pdi, pci);
1402 pdi->first_reported_time = t->time;
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
1411 static char *size_cnv(char *dst, unsigned long long num, int in_kb)
1413 char suff[] = { '\0', 'K', 'M', 'G', 'P' };
1419 while (num > 1000 * 1000ULL && (i < sizeof(suff) - 1)) {
1424 sprintf(dst, "%'8Lu%c", num, suff[i]);
1428 static void dump_io_stats(struct per_dev_info *pdi, struct io_stats *ios,
1431 static char x[256], y[256];
1433 fprintf(ofp, "%s\n", msg);
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));
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);
1447 fprintf(ofp, " Read depth: %'8u%8c\t", pdi->max_depth[0], ' ');
1448 fprintf(ofp, " Write depth: %'8u\n", pdi->max_depth[1]);
1450 fprintf(ofp, " IO unplugs: %'8lu%8c\t", ios->io_unplugs, ' ');
1451 fprintf(ofp, " Timer unplugs: %'8lu\n", ios->timer_unplugs);
1454 static void dump_wait_stats(struct per_process_info *ppi)
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;
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);
1471 static int ppi_name_compare(const void *p1, const void *p2)
1473 struct per_process_info *ppi1 = *((struct per_process_info **) p1);
1474 struct per_process_info *ppi2 = *((struct per_process_info **) p2);
1477 res = strverscmp(ppi1->ppm->comm, ppi2->ppm->comm);
1479 res = ppi1->ppm->pid > ppi2->ppm->pid;
1484 static void sort_process_list(void)
1486 struct per_process_info **ppis;
1487 struct per_process_info *ppi;
1490 ppis = malloc(ppi_list_entries * sizeof(struct per_process_info *));
1495 ppi = ppi->list_next;
1498 qsort(ppis, ppi_list_entries, sizeof(ppi), ppi_name_compare);
1500 i = ppi_list_entries - 1;
1505 ppi->list_next = ppi_list;
1513 static void show_process_stats(void)
1515 struct per_process_info *ppi;
1517 sort_process_list();
1521 struct process_pid_map *ppm = ppi->ppm;
1524 if (ppi->more_than_one)
1525 sprintf(name, "%s (%u, ...)", ppm->comm, ppm->pid);
1527 sprintf(name, "%s (%u)", ppm->comm, ppm->pid);
1529 dump_io_stats(NULL, &ppi->io_stats, name);
1530 dump_wait_stats(ppi);
1531 ppi = ppi->list_next;
1537 static void show_device_and_cpu_stats(void)
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];
1547 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
1549 memset(&total, 0, sizeof(total));
1555 for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) {
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;
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);
1585 if (pci_events > 1) {
1587 snprintf(line, sizeof(line) - 1, "Total (%s):",
1588 get_dev_name(pdi, name, sizeof(name)));
1589 dump_io_stats(NULL, &total, line);
1593 msec = (pdi->last_reported_time - pdi->first_reported_time) / 1000000;
1595 rrate = 1000 * total.cread_kb / msec;
1596 wrate = 1000 * total.cwrite_kb / msec;
1599 fprintf(ofp, "\nThroughput (R/W): %'LuKiB/s / %'LuKiB/s\n",
1601 fprintf(ofp, "Events (%s): %'Lu entries\n",
1602 get_dev_name(pdi, line, sizeof(line)), pdi->events);
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)));
1612 static void find_genesis(void)
1614 struct trace *t = trace_list;
1616 genesis_time = -1ULL;
1618 if (t->bit->time < genesis_time)
1619 genesis_time = t->bit->time;
1625 static inline int check_stopwatch(struct blk_io_trace *bit)
1627 if (bit->time < stopwatch_end &&
1628 bit->time >= stopwatch_start)
1635 * return youngest entry read
1637 static int sort_entries(unsigned long long *youngest)
1639 struct per_dev_info *pdi = NULL;
1640 struct per_cpu_info *pci = NULL;
1647 while ((t = trace_list) != NULL) {
1648 struct blk_io_trace *bit = t->bit;
1650 trace_list = t->next;
1652 bit->time -= genesis_time;
1654 if (bit->time < *youngest || !*youngest)
1655 *youngest = bit->time;
1657 if (!pdi || pdi->dev != bit->device) {
1658 pdi = get_dev_info(bit->device);
1662 if (!pci || pci->cpu != bit->cpu)
1663 pci = get_cpu_info(pdi, bit->cpu);
1665 if (bit->sequence < pci->smallest_seq_read)
1666 pci->smallest_seq_read = bit->sequence;
1668 if (check_stopwatch(bit)) {
1674 if (trace_rb_insert_sort(t))
1682 * to continue, we must have traces from all online cpus in the tree
1684 static int check_cpu_map(struct per_dev_info *pdi)
1686 unsigned long *cpu_map;
1693 * create a map of the cpus we have traces for
1695 cpu_map = malloc(pdi->cpu_map_max / sizeof(long));
1696 n = rb_first(&rb_sort_root);
1698 __t = rb_entry(n, struct trace, rb_node);
1699 cpu = __t->bit->cpu;
1701 cpu_map[CPU_IDX(cpu)] |= (1UL << CPU_BIT(cpu));
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
1710 for (i = 0; i < pdi->cpu_map_max / CPUS_PER_LONG; i++) {
1711 if (pdi->cpu_map[i] & ~(cpu_map[i])) {
1721 static int check_sequence(struct per_dev_info *pdi, struct trace *t, int force)
1723 struct blk_io_trace *bit = t->bit;
1724 unsigned long expected_sequence;
1725 struct per_cpu_info *pci;
1728 pci = get_cpu_info(pdi, bit->cpu);
1729 expected_sequence = pci->last_sequence + 1;
1731 if (!expected_sequence) {
1733 * 1 should be the first entry, just allow it
1735 if (bit->sequence == 1)
1737 if (bit->sequence == pci->smallest_seq_read)
1740 return check_cpu_map(pdi);
1743 if (bit->sequence == expected_sequence)
1747 * we may not have seen that sequence yet. if we are not doing
1748 * the final run, break and wait for more entries.
1750 if (expected_sequence < pci->smallest_seq_read) {
1751 __t = trace_rb_find_last(pdi, pci, expected_sequence);
1755 __put_trace_last(pdi, __t);
1757 } else if (!force) {
1761 if (check_current_skips(pci, bit->sequence))
1764 if (expected_sequence < bit->sequence)
1765 insert_skip(pci, expected_sequence, bit->sequence - 1);
1770 static void show_entries_rb(int force)
1772 struct per_dev_info *pdi = NULL;
1773 struct per_cpu_info *pci = NULL;
1774 struct blk_io_trace *bit;
1778 while ((n = rb_first(&rb_sort_root)) != NULL) {
1779 if (is_done() && !force && !pipeline)
1782 t = rb_entry(n, struct trace, rb_node);
1785 if (read_sequence - t->read_sequence < 1 && !force)
1788 if (!pdi || pdi->dev != bit->device) {
1789 pdi = get_dev_info(bit->device);
1794 fprintf(stderr, "Unknown device ID? (%d,%d)\n",
1795 MAJOR(bit->device), MINOR(bit->device));
1799 if (check_sequence(pdi, t, force))
1802 if (!force && bit->time > last_allowed_time)
1805 check_time(pdi, bit);
1807 if (!pci || pci->cpu != bit->cpu)
1808 pci = get_cpu_info(pdi, bit->cpu);
1810 pci->last_sequence = bit->sequence;
1814 if (bit->action & (act_mask << BLK_TC_SHIFT))
1815 dump_trace(bit, pci, pdi);
1821 static int read_data(int fd, void *buffer, int bytes, int block, int *fdblock)
1823 int ret, bytes_left, fl;
1826 if (block != *fdblock) {
1827 fl = fcntl(fd, F_GETFL);
1831 fcntl(fd, F_SETFL, fl | O_NONBLOCK);
1834 fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
1840 while (bytes_left > 0) {
1841 ret = read(fd, p, bytes_left);
1845 if (errno != EAGAIN) {
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
1855 if ((bytes_left == bytes) && !block)
1869 static inline __u16 get_pdulen(struct blk_io_trace *bit)
1872 return bit->pdu_len;
1874 return __bswap_16(bit->pdu_len);
1877 static inline __u32 get_magic(struct blk_io_trace *bit)
1882 return __bswap_32(bit->magic);
1885 static int read_events(int fd, int always_block, int *fdblock)
1887 struct per_dev_info *pdi = NULL;
1888 unsigned int events = 0;
1890 while (!is_done() && events < rb_batch) {
1891 struct blk_io_trace *bit;
1893 int pdu_len, should_block, ret;
1898 should_block = !events || always_block;
1900 ret = read_data(fd, bit, sizeof(*bit), should_block, fdblock);
1903 if (!events && ret < 0)
1909 * look at first trace to check whether we need to convert
1910 * data in the future
1912 if (data_is_native == -1 && check_data_endianness(bit->magic))
1915 magic = get_magic(bit);
1916 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
1917 fprintf(stderr, "Bad magic %x\n", magic);
1921 pdu_len = get_pdulen(bit);
1923 void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
1925 if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1, fdblock)) {
1935 if (verify_trace(bit)) {
1941 * not a real trace, so grab and handle it here
1943 if (bit->action & BLK_TC_ACT(BLK_TC_NOTIFY)) {
1944 add_ppm_hash(bit->pid, (char *) bit + sizeof(*bit));
1949 memset(t, 0, sizeof(*t));
1951 t->read_sequence = read_sequence;
1953 t->next = trace_list;
1956 if (!pdi || pdi->dev != bit->device)
1957 pdi = get_dev_info(bit->device);
1959 if (bit->time > pdi->last_read_time)
1960 pdi->last_read_time = bit->time;
1968 static int do_file(void)
1970 struct per_cpu_info *pci;
1971 struct per_dev_info *pdi;
1972 int i, j, events, events_added;
1975 * first prepare all files for reading
1977 for (i = 0; i < ndevices; i++) {
1986 pci = get_cpu_info(pdi, j);
1991 p = strdup(pdi->name);
1993 if (strcmp(dname, ".")) {
1995 p = strdup(pdi->name);
1996 strcpy(pdi->name, basename(p));
2001 len = sprintf(pci->fname, "%s/", input_dir);
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)
2008 pci->fd = open(pci->fname, O_RDONLY);
2015 printf("Input file %s added\n", pci->fname);
2017 cpu_mark_online(pdi, pci->cpu);
2022 * now loop over the files reading in the data
2025 unsigned long long youngest;
2028 last_allowed_time = -1ULL;
2031 for (i = 0; i < ndevices; i++) {
2033 pdi->last_read_time = -1ULL;
2035 for (j = 0; j < pdi->nfiles; j++) {
2037 pci = get_cpu_info(pdi, j);
2042 pci->smallest_seq_read = -1;
2044 events = read_events(pci->fd, 1, &pci->fdblock);
2046 cpu_mark_offline(pdi, pci->cpu);
2052 if (pdi->last_read_time < last_allowed_time)
2053 last_allowed_time = pdi->last_read_time;
2055 events_added += events;
2059 if (sort_entries(&youngest))
2062 if (youngest > stopwatch_end)
2067 } while (events_added);
2069 if (rb_sort_entries)
2075 static int do_stdin(void)
2077 unsigned long long youngest;
2078 int fd, events, fdblock;
2080 last_allowed_time = -1ULL;
2081 fd = dup(STDIN_FILENO);
2083 perror("dup stdin");
2088 while ((events = read_events(fd, 0, &fdblock)) > 0) {
2092 smallest_seq_read = -1U;
2095 if (sort_entries(&youngest))
2098 if (youngest > stopwatch_end)
2104 if (rb_sort_entries)
2111 static void show_stats(void)
2120 if (per_process_stats)
2121 show_process_stats();
2123 if (per_device_and_cpu_stats)
2124 show_device_and_cpu_stats();
2129 static void handle_sigint(__attribute__((__unused__)) int sig)
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".
2139 static int find_stopwatch_interval(char *string)
2144 value = strtod(string, &sp);
2146 fprintf(stderr,"Invalid stopwatch timer: %s\n", string);
2150 stopwatch_start = DOUBLE_TO_NANO_ULL(value);
2152 value = strtod(string, &sp);
2153 if (sp == string || *sp != '\0') {
2154 fprintf(stderr,"Invalid stopwatch duration time: %s\n",
2158 } else if (*sp != '\0') {
2159 fprintf(stderr,"Invalid stopwatch start timer: %s\n", string);
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);
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";
2192 static void usage(char *prog)
2194 fprintf(stderr, "Usage: %s %s %s", prog, blkparse_version, usage_str);
2197 int main(int argc, char *argv[])
2200 int i, c, ret, mode;
2201 int act_mask_tmp = 0;
2203 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
2206 i = find_mask_map(optarg);
2208 fprintf(stderr,"Invalid action mask %s\n",
2216 if ((sscanf(optarg, "%x", &i) != 1) ||
2217 !valid_act_opt(i)) {
2219 "Invalid set action mask %s/0x%x\n",
2226 if (!strcmp(optarg, "-") && !pipeline)
2228 else if (resize_devices(optarg) != 0)
2235 output_name = optarg;
2238 rb_batch = atoi(optarg);
2240 rb_batch = RB_BATCH_DEFAULT;
2243 per_process_stats = 1;
2249 per_device_and_cpu_stats = 0;
2252 if (find_stopwatch_interval(optarg) != 0)
2256 set_all_format_specs(optarg);
2259 if (add_format_spec(optarg) != 0)
2263 ppi_hash_by_pid = 0;
2269 printf("%s version %s\n", argv[0], blkparse_version);
2277 while (optind < argc) {
2278 if (!strcmp(argv[optind], "-") && !pipeline)
2280 else if (resize_devices(argv[optind]) != 0)
2285 if (!pipeline && !ndevices) {
2290 if (act_mask_tmp != 0)
2291 act_mask = act_mask_tmp;
2293 memset(&rb_sort_root, 0, sizeof(rb_sort_root));
2295 signal(SIGINT, handle_sigint);
2296 signal(SIGHUP, handle_sigint);
2297 signal(SIGTERM, handle_sigint);
2299 setlocale(LC_NUMERIC, "en_US");
2302 ofp = fdopen(STDOUT_FILENO, "w");
2307 snprintf(ofname, sizeof(ofname) - 1, "%s", output_name);
2308 ofp = fopen(ofname, "w");
2317 ofp_buffer = malloc(4096);
2318 if (setvbuf(ofp, ofp_buffer, mode, 4096)) {