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.1";
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:d:"
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,
199 .name = "dump-binary",
200 .has_arg = required_argument,
210 * for sorting the displayed output
213 struct blk_io_trace *bit;
214 struct rb_node rb_node;
216 unsigned long read_sequence;
219 static struct rb_root rb_sort_root;
220 static unsigned long rb_sort_entries;
222 static struct trace *trace_list;
227 static struct blk_io_trace *bit_alloc_list;
228 static struct trace *t_alloc_list;
231 * for tracking individual ios
234 struct rb_node rb_node;
236 struct process_pid_map *ppm;
238 unsigned long long allocation_time;
239 unsigned long long queue_time;
240 unsigned long long dispatch_time;
241 unsigned long long completion_time;
245 static struct per_dev_info *devices;
246 static char *get_dev_name(struct per_dev_info *, char *, int);
247 static int trace_rb_insert_last(struct per_dev_info *, struct trace *);
250 static char *output_name;
251 static char *input_dir;
253 static unsigned long long genesis_time;
254 static unsigned long long last_allowed_time;
255 static unsigned long long stopwatch_start; /* start from zero by default */
256 static unsigned long long stopwatch_end = -1ULL; /* "infinity" */
257 static unsigned long read_sequence;
259 static int per_process_stats;
260 static int per_device_and_cpu_stats = 1;
261 static int track_ios;
262 static int ppi_hash_by_pid = 1;
264 static unsigned int act_mask = -1U;
265 static int stats_printed;
266 int data_is_native = -1;
269 static char *dump_binary;
271 static unsigned int t_alloc_cache;
272 static unsigned int bit_alloc_cache;
274 #define RB_BATCH_DEFAULT (512)
275 static unsigned int rb_batch = RB_BATCH_DEFAULT;
279 #define is_done() (*(volatile int *)(&done))
280 static volatile int done;
282 #define JHASH_RANDOM (0x3af5f2ee)
284 #define CPUS_PER_LONG (8 * sizeof(unsigned long))
285 #define CPU_IDX(cpu) ((cpu) / CPUS_PER_LONG)
286 #define CPU_BIT(cpu) ((cpu) & (CPUS_PER_LONG - 1))
288 static void output_binary(void *buf, int len)
291 int n = write(dump_fd, buf, len);
300 static void resize_cpu_info(struct per_dev_info *pdi, int cpu)
302 struct per_cpu_info *cpus = pdi->cpus;
303 int ncpus = pdi->ncpus;
304 int new_count = cpu + 1;
308 size = new_count * sizeof(struct per_cpu_info);
309 cpus = realloc(cpus, size);
312 fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n",
313 get_dev_name(pdi, name, sizeof(name)), size);
317 new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info));
318 new_space = (new_count - ncpus) * sizeof(struct per_cpu_info);
319 memset(new_start, 0, new_space);
321 pdi->ncpus = new_count;
324 for (new_count = 0; new_count < pdi->ncpus; new_count++) {
325 struct per_cpu_info *pci = &pdi->cpus[new_count];
329 memset(&pci->rb_last, 0, sizeof(pci->rb_last));
330 pci->rb_last_entries = 0;
331 pci->last_sequence = -1;
336 static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu)
338 struct per_cpu_info *pci;
340 if (cpu >= pdi->ncpus)
341 resize_cpu_info(pdi, cpu);
343 pci = &pdi->cpus[cpu];
349 static int resize_devices(char *name)
351 int size = (ndevices + 1) * sizeof(struct per_dev_info);
353 devices = realloc(devices, size);
355 fprintf(stderr, "Out of memory, device %s (%d)\n", name, size);
358 memset(&devices[ndevices], 0, sizeof(struct per_dev_info));
359 devices[ndevices].name = name;
364 static struct per_dev_info *get_dev_info(dev_t dev)
366 struct per_dev_info *pdi;
369 for (i = 0; i < ndevices; i++) {
371 devices[i].dev = dev;
372 if (devices[i].dev == dev)
376 if (resize_devices(NULL))
379 pdi = &devices[ndevices - 1];
381 pdi->first_reported_time = 0;
382 pdi->last_read_time = 0;
387 static void insert_skip(struct per_cpu_info *pci, unsigned long start,
390 struct skip_info *sip;
392 for (sip = pci->skips_tail; sip != NULL; sip = sip->prev) {
393 if (end == (sip->start - 1)) {
396 } else if (start == (sip->end + 1)) {
402 sip = malloc(sizeof(struct skip_info));
405 sip->prev = sip->next = NULL;
406 if (pci->skips_tail == NULL)
407 pci->skips_head = pci->skips_tail = sip;
409 sip->prev = pci->skips_tail;
410 pci->skips_tail->next = sip;
411 pci->skips_tail = sip;
415 static void remove_sip(struct per_cpu_info *pci, struct skip_info *sip)
417 if (sip->prev == NULL) {
418 if (sip->next == NULL)
419 pci->skips_head = pci->skips_tail = NULL;
421 pci->skips_head = sip->next;
422 sip->next->prev = NULL;
424 } else if (sip->next == NULL) {
425 pci->skips_tail = sip->prev;
426 sip->prev->next = NULL;
428 sip->prev->next = sip->next;
429 sip->next->prev = sip->prev;
432 sip->prev = sip->next = NULL;
436 #define IN_SKIP(sip,seq) (((sip)->start <= (seq)) && ((seq) <= sip->end))
437 static int check_current_skips(struct per_cpu_info *pci, unsigned long seq)
439 struct skip_info *sip;
441 for (sip = pci->skips_tail; sip != NULL; sip = sip->prev) {
442 if (IN_SKIP(sip, seq)) {
443 if (sip->start == seq) {
445 remove_sip(pci, sip);
448 } else if (sip->end == seq)
452 insert_skip(pci, seq + 1, sip->end);
461 static void collect_pdi_skips(struct per_dev_info *pdi)
463 struct skip_info *sip;
469 for (cpu = 0; cpu < pdi->ncpus; cpu++) {
470 struct per_cpu_info *pci = &pdi->cpus[cpu];
472 for (sip = pci->skips_head; sip != NULL; sip = sip->next) {
474 pdi->seq_skips += (sip->end - sip->start + 1);
476 fprintf(stderr,"(%d,%d): skipping %lu -> %lu\n",
477 MAJOR(pdi->dev), MINOR(pdi->dev),
478 sip->start, sip->end);
483 static void cpu_mark_online(struct per_dev_info *pdi, unsigned int cpu)
485 if (cpu >= pdi->cpu_map_max || !pdi->cpu_map) {
486 int new_max = (cpu + CPUS_PER_LONG) & ~(CPUS_PER_LONG - 1);
487 unsigned long *map = malloc(new_max / sizeof(long));
489 memset(map, 0, new_max / sizeof(long));
492 memcpy(map, pdi->cpu_map, pdi->cpu_map_max / sizeof(long));
497 pdi->cpu_map_max = new_max;
500 pdi->cpu_map[CPU_IDX(cpu)] |= (1UL << CPU_BIT(cpu));
503 static inline void cpu_mark_offline(struct per_dev_info *pdi, int cpu)
505 pdi->cpu_map[CPU_IDX(cpu)] &= ~(1UL << CPU_BIT(cpu));
508 static inline int cpu_is_online(struct per_dev_info *pdi, int cpu)
510 return (pdi->cpu_map[CPU_IDX(cpu)] & (1UL << CPU_BIT(cpu))) != 0;
513 static inline int ppm_hash_pid(pid_t pid)
515 return jhash_1word(pid, JHASH_RANDOM) & PPM_HASH_MASK;
518 static struct process_pid_map *find_ppm(pid_t pid)
520 const int hash_idx = ppm_hash_pid(pid);
521 struct process_pid_map *ppm;
523 ppm = ppm_hash_table[hash_idx];
528 ppm = ppm->hash_next;
534 static void add_ppm_hash(pid_t pid, const char *name)
536 const int hash_idx = ppm_hash_pid(pid);
537 struct process_pid_map *ppm;
541 ppm = malloc(sizeof(*ppm));
542 memset(ppm, 0, sizeof(*ppm));
544 strcpy(ppm->comm, name);
545 ppm->hash_next = ppm_hash_table[hash_idx];
546 ppm_hash_table[hash_idx] = ppm;
550 char *find_process_name(pid_t pid)
552 struct process_pid_map *ppm = find_ppm(pid);
560 static inline int ppi_hash_pid(pid_t pid)
562 return jhash_1word(pid, JHASH_RANDOM) & PPI_HASH_MASK;
565 static inline int ppi_hash_name(const char *name)
567 return jhash(name, 16, JHASH_RANDOM) & PPI_HASH_MASK;
570 static inline int ppi_hash(struct per_process_info *ppi)
572 struct process_pid_map *ppm = ppi->ppm;
575 return ppi_hash_pid(ppm->pid);
577 return ppi_hash_name(ppm->comm);
580 static inline void add_ppi_to_hash(struct per_process_info *ppi)
582 const int hash_idx = ppi_hash(ppi);
584 ppi->hash_next = ppi_hash_table[hash_idx];
585 ppi_hash_table[hash_idx] = ppi;
588 static inline void add_ppi_to_list(struct per_process_info *ppi)
590 ppi->list_next = ppi_list;
595 static struct per_process_info *find_ppi_by_name(char *name)
597 const int hash_idx = ppi_hash_name(name);
598 struct per_process_info *ppi;
600 ppi = ppi_hash_table[hash_idx];
602 struct process_pid_map *ppm = ppi->ppm;
604 if (!strcmp(ppm->comm, name))
607 ppi = ppi->hash_next;
613 static struct per_process_info *find_ppi_by_pid(pid_t pid)
615 const int hash_idx = ppi_hash_pid(pid);
616 struct per_process_info *ppi;
618 ppi = ppi_hash_table[hash_idx];
620 struct process_pid_map *ppm = ppi->ppm;
625 ppi = ppi->hash_next;
631 static struct per_process_info *find_ppi(pid_t pid)
633 struct per_process_info *ppi;
637 return find_ppi_by_pid(pid);
639 name = find_process_name(pid);
643 ppi = find_ppi_by_name(name);
644 if (ppi && ppi->ppm->pid != pid)
645 ppi->more_than_one = 1;
651 * struct trace and blktrace allocation cache, we do potentially
652 * millions of mallocs for these structures while only using at most
653 * a few thousand at the time
655 static inline void t_free(struct trace *t)
657 if (t_alloc_cache < 1024) {
658 t->next = t_alloc_list;
665 static inline struct trace *t_alloc(void)
667 struct trace *t = t_alloc_list;
670 t_alloc_list = t->next;
675 return malloc(sizeof(*t));
678 static inline void bit_free(struct blk_io_trace *bit)
680 if (bit_alloc_cache < 1024 && !bit->pdu_len) {
682 * abuse a 64-bit field for a next pointer for the free item
684 bit->time = (__u64) (unsigned long) bit_alloc_list;
685 bit_alloc_list = (struct blk_io_trace *) bit;
691 static inline struct blk_io_trace *bit_alloc(void)
693 struct blk_io_trace *bit = bit_alloc_list;
696 bit_alloc_list = (struct blk_io_trace *) (unsigned long) \
702 return malloc(sizeof(*bit));
705 static inline void __put_trace_last(struct per_dev_info *pdi, struct trace *t)
707 struct per_cpu_info *pci = get_cpu_info(pdi, t->bit->cpu);
709 rb_erase(&t->rb_node, &pci->rb_last);
710 pci->rb_last_entries--;
716 static void put_trace(struct per_dev_info *pdi, struct trace *t)
718 rb_erase(&t->rb_node, &rb_sort_root);
721 trace_rb_insert_last(pdi, t);
724 static inline int trace_rb_insert(struct trace *t, struct rb_root *root)
726 struct rb_node **p = &root->rb_node;
727 struct rb_node *parent = NULL;
733 __t = rb_entry(parent, struct trace, rb_node);
735 if (t->bit->time < __t->bit->time)
737 else if (t->bit->time > __t->bit->time)
739 else if (t->bit->device < __t->bit->device)
741 else if (t->bit->device > __t->bit->device)
743 else if (t->bit->sequence < __t->bit->sequence)
745 else /* >= sequence */
749 rb_link_node(&t->rb_node, parent, p);
750 rb_insert_color(&t->rb_node, root);
754 static inline int trace_rb_insert_sort(struct trace *t)
756 if (!trace_rb_insert(t, &rb_sort_root)) {
764 static int trace_rb_insert_last(struct per_dev_info *pdi, struct trace *t)
766 struct per_cpu_info *pci = get_cpu_info(pdi, t->bit->cpu);
768 if (trace_rb_insert(t, &pci->rb_last))
771 pci->rb_last_entries++;
773 if (pci->rb_last_entries > rb_batch * pdi->nfiles) {
774 struct rb_node *n = rb_first(&pci->rb_last);
776 t = rb_entry(n, struct trace, rb_node);
777 __put_trace_last(pdi, t);
783 static struct trace *trace_rb_find(dev_t device, unsigned long sequence,
784 struct rb_root *root, int order)
786 struct rb_node *n = root->rb_node;
787 struct rb_node *prev = NULL;
791 __t = rb_entry(n, struct trace, rb_node);
794 if (device < __t->bit->device)
796 else if (device > __t->bit->device)
798 else if (sequence < __t->bit->sequence)
800 else if (sequence > __t->bit->sequence)
807 * hack - the list may not be sequence ordered because some
808 * events don't have sequence and time matched. so we end up
809 * being a little off in the rb lookup here, because we don't
810 * know the time we are looking for. compensate by browsing
811 * a little ahead from the last entry to find the match
816 while (((n = rb_next(prev)) != NULL) && max--) {
817 __t = rb_entry(n, struct trace, rb_node);
819 if (__t->bit->device == device &&
820 __t->bit->sequence == sequence)
830 static inline struct trace *trace_rb_find_last(struct per_dev_info *pdi,
831 struct per_cpu_info *pci,
834 return trace_rb_find(pdi->dev, seq, &pci->rb_last, 0);
837 static inline int track_rb_insert(struct per_dev_info *pdi,struct io_track *iot)
839 struct rb_node **p = &pdi->rb_track.rb_node;
840 struct rb_node *parent = NULL;
841 struct io_track *__iot;
845 __iot = rb_entry(parent, struct io_track, rb_node);
847 if (iot->sector < __iot->sector)
849 else if (iot->sector > __iot->sector)
853 "sector alias (%Lu) on device %d,%d!\n",
854 (unsigned long long) iot->sector,
855 MAJOR(pdi->dev), MINOR(pdi->dev));
860 rb_link_node(&iot->rb_node, parent, p);
861 rb_insert_color(&iot->rb_node, &pdi->rb_track);
865 static struct io_track *__find_track(struct per_dev_info *pdi, __u64 sector)
867 struct rb_node *n = pdi->rb_track.rb_node;
868 struct io_track *__iot;
871 __iot = rb_entry(n, struct io_track, rb_node);
873 if (sector < __iot->sector)
875 else if (sector > __iot->sector)
884 static struct io_track *find_track(struct per_dev_info *pdi, pid_t pid,
887 struct io_track *iot;
889 iot = __find_track(pdi, sector);
891 iot = malloc(sizeof(*iot));
892 iot->ppm = find_ppm(pid);
893 iot->sector = sector;
894 track_rb_insert(pdi, iot);
900 static void log_track_frontmerge(struct per_dev_info *pdi,
901 struct blk_io_trace *t)
903 struct io_track *iot;
908 iot = __find_track(pdi, t->sector + t_sec(t));
911 fprintf(stderr, "merge not found for (%d,%d): %llu\n",
912 MAJOR(pdi->dev), MINOR(pdi->dev),
913 (unsigned long long) t->sector + t_sec(t));
917 rb_erase(&iot->rb_node, &pdi->rb_track);
918 iot->sector -= t_sec(t);
919 track_rb_insert(pdi, iot);
922 static void log_track_getrq(struct per_dev_info *pdi, struct blk_io_trace *t)
924 struct io_track *iot;
929 iot = find_track(pdi, t->pid, t->sector);
930 iot->allocation_time = t->time;
933 static inline int is_remapper(struct per_dev_info *pdi)
935 int major = MAJOR(pdi->dev);
937 return (major == 253 || major == 9);
941 * for md/dm setups, the interesting cycle is Q -> C. So track queueing
942 * time here, as dispatch time
944 static void log_track_queue(struct per_dev_info *pdi, struct blk_io_trace *t)
946 struct io_track *iot;
950 if (!is_remapper(pdi))
953 iot = find_track(pdi, t->pid, t->sector);
954 iot->dispatch_time = t->time;
958 * return time between rq allocation and insertion
960 static unsigned long long log_track_insert(struct per_dev_info *pdi,
961 struct blk_io_trace *t)
963 unsigned long long elapsed;
964 struct io_track *iot;
969 iot = find_track(pdi, t->pid, t->sector);
970 iot->queue_time = t->time;
972 if (!iot->allocation_time)
975 elapsed = iot->queue_time - iot->allocation_time;
977 if (per_process_stats) {
978 struct per_process_info *ppi = find_ppi(iot->ppm->pid);
979 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
981 if (ppi && elapsed > ppi->longest_allocation_wait[w])
982 ppi->longest_allocation_wait[w] = elapsed;
989 * return time between queue and issue
991 static unsigned long long log_track_issue(struct per_dev_info *pdi,
992 struct blk_io_trace *t)
994 unsigned long long elapsed;
995 struct io_track *iot;
999 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
1002 iot = __find_track(pdi, t->sector);
1005 fprintf(stderr, "issue not found for (%d,%d): %llu\n",
1006 MAJOR(pdi->dev), MINOR(pdi->dev),
1007 (unsigned long long) t->sector);
1011 iot->dispatch_time = t->time;
1012 elapsed = iot->dispatch_time - iot->queue_time;
1014 if (per_process_stats) {
1015 struct per_process_info *ppi = find_ppi(iot->ppm->pid);
1016 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
1018 if (ppi && elapsed > ppi->longest_dispatch_wait[w])
1019 ppi->longest_dispatch_wait[w] = elapsed;
1026 * return time between dispatch and complete
1028 static unsigned long long log_track_complete(struct per_dev_info *pdi,
1029 struct blk_io_trace *t)
1031 unsigned long long elapsed;
1032 struct io_track *iot;
1037 iot = __find_track(pdi, t->sector);
1040 fprintf(stderr,"complete not found for (%d,%d): %llu\n",
1041 MAJOR(pdi->dev), MINOR(pdi->dev),
1042 (unsigned long long) t->sector);
1046 iot->completion_time = t->time;
1047 elapsed = iot->completion_time - iot->dispatch_time;
1049 if (per_process_stats) {
1050 struct per_process_info *ppi = find_ppi(iot->ppm->pid);
1051 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
1053 if (ppi && elapsed > ppi->longest_completion_wait[w])
1054 ppi->longest_completion_wait[w] = elapsed;
1058 * kill the trace, we don't need it after completion
1060 rb_erase(&iot->rb_node, &pdi->rb_track);
1067 static struct io_stats *find_process_io_stats(pid_t pid)
1069 struct per_process_info *ppi = find_ppi(pid);
1072 ppi = malloc(sizeof(*ppi));
1073 memset(ppi, 0, sizeof(*ppi));
1074 ppi->ppm = find_ppm(pid);
1075 add_ppi_to_hash(ppi);
1076 add_ppi_to_list(ppi);
1079 return &ppi->io_stats;
1082 static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size)
1085 snprintf(buffer, size, "%s", pdi->name);
1087 snprintf(buffer, size, "%d,%d",MAJOR(pdi->dev),MINOR(pdi->dev));
1091 static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit)
1093 unsigned long long this = bit->time;
1094 unsigned long long last = pdi->last_reported_time;
1096 pdi->backwards = (this < last) ? 'B' : ' ';
1097 pdi->last_reported_time = this;
1100 static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t,
1105 ios->qwrite_kb += t_kb(t);
1108 ios->qread_kb += t_kb(t);
1112 static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci,
1115 __account_m(&pci->io_stats, t, rw);
1117 if (per_process_stats) {
1118 struct io_stats *ios = find_process_io_stats(t->pid);
1120 __account_m(ios, t, rw);
1124 static inline void __account_queue(struct io_stats *ios, struct blk_io_trace *t,
1129 ios->qwrite_kb += t_kb(t);
1132 ios->qread_kb += t_kb(t);
1136 static inline void account_queue(struct blk_io_trace *t,
1137 struct per_cpu_info *pci, int rw)
1139 __account_queue(&pci->io_stats, t, rw);
1141 if (per_process_stats) {
1142 struct io_stats *ios = find_process_io_stats(t->pid);
1144 __account_queue(ios, t, rw);
1148 static inline void __account_c(struct io_stats *ios, int rw, int bytes)
1152 ios->cwrite_kb += bytes >> 10;
1155 ios->cread_kb += bytes >> 10;
1159 static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci,
1162 __account_c(&pci->io_stats, rw, bytes);
1164 if (per_process_stats) {
1165 struct io_stats *ios = find_process_io_stats(t->pid);
1167 __account_c(ios, rw, bytes);
1171 static inline void __account_issue(struct io_stats *ios, int rw,
1176 ios->iwrite_kb += bytes >> 10;
1179 ios->iread_kb += bytes >> 10;
1183 static inline void account_issue(struct blk_io_trace *t,
1184 struct per_cpu_info *pci, int rw)
1186 __account_issue(&pci->io_stats, rw, t->bytes);
1188 if (per_process_stats) {
1189 struct io_stats *ios = find_process_io_stats(t->pid);
1191 __account_issue(ios, rw, t->bytes);
1195 static inline void __account_unplug(struct io_stats *ios, int timer)
1198 ios->timer_unplugs++;
1203 static inline void account_unplug(struct blk_io_trace *t,
1204 struct per_cpu_info *pci, int timer)
1206 __account_unplug(&pci->io_stats, timer);
1208 if (per_process_stats) {
1209 struct io_stats *ios = find_process_io_stats(t->pid);
1211 __account_unplug(ios, timer);
1215 static inline void __account_requeue(struct io_stats *ios,
1216 struct blk_io_trace *t, int rw)
1220 ios->iwrite_kb -= t_kb(t);
1223 ios->iread_kb -= t_kb(t);
1227 static inline void account_requeue(struct blk_io_trace *t,
1228 struct per_cpu_info *pci, int rw)
1230 __account_requeue(&pci->io_stats, t, rw);
1232 if (per_process_stats) {
1233 struct io_stats *ios = find_process_io_stats(t->pid);
1235 __account_requeue(ios, t, rw);
1239 static void log_complete(struct per_dev_info *pdi, struct per_cpu_info *pci,
1240 struct blk_io_trace *t, char *act)
1242 process_fmt(act, pci, t, log_track_complete(pdi, t), 0, NULL);
1245 static void log_insert(struct per_dev_info *pdi, struct per_cpu_info *pci,
1246 struct blk_io_trace *t, char *act)
1248 process_fmt(act, pci, t, log_track_insert(pdi, t), 0, NULL);
1251 static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t,
1254 process_fmt(act, pci, t, -1, 0, NULL);
1257 static void log_issue(struct per_dev_info *pdi, struct per_cpu_info *pci,
1258 struct blk_io_trace *t, char *act)
1260 process_fmt(act, pci, t, log_track_issue(pdi, t), 0, NULL);
1263 static void log_merge(struct per_dev_info *pdi, struct per_cpu_info *pci,
1264 struct blk_io_trace *t, char *act)
1267 log_track_frontmerge(pdi, t);
1269 process_fmt(act, pci, t, -1ULL, 0, NULL);
1272 static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t,
1275 process_fmt(act, pci, t, -1ULL, 0, NULL);
1278 static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t,
1281 process_fmt(act, pci, t, -1ULL, 0, NULL);
1284 static void log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t,
1287 process_fmt(act, pci, t, -1ULL, 0, NULL);
1290 static void log_split(struct per_cpu_info *pci, struct blk_io_trace *t,
1293 process_fmt(act, pci, t, -1ULL, 0, NULL);
1296 static void log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act)
1298 unsigned char *buf = (unsigned char *) t + sizeof(*t);
1300 process_fmt(act, pci, t, -1ULL, t->pdu_len, buf);
1303 static void dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci)
1305 int act = t->action & 0xffff;
1308 case __BLK_TA_QUEUE:
1309 log_generic(pci, t, "Q");
1311 case __BLK_TA_GETRQ:
1312 log_generic(pci, t, "G");
1314 case __BLK_TA_SLEEPRQ:
1315 log_generic(pci, t, "S");
1317 case __BLK_TA_REQUEUE:
1318 log_generic(pci, t, "R");
1320 case __BLK_TA_ISSUE:
1321 log_pc(pci, t, "D");
1323 case __BLK_TA_COMPLETE:
1324 log_pc(pci, t, "C");
1326 case __BLK_TA_INSERT:
1327 log_pc(pci, t, "I");
1330 fprintf(stderr, "Bad pc action %x\n", act);
1335 static void dump_trace_fs(struct blk_io_trace *t, struct per_dev_info *pdi,
1336 struct per_cpu_info *pci)
1338 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
1339 int act = t->action & 0xffff;
1342 case __BLK_TA_QUEUE:
1343 log_track_queue(pdi, t);
1344 account_queue(t, pci, w);
1345 log_queue(pci, t, "Q");
1347 case __BLK_TA_INSERT:
1348 log_insert(pdi, pci, t, "I");
1350 case __BLK_TA_BACKMERGE:
1351 account_m(t, pci, w);
1352 log_merge(pdi, pci, t, "M");
1354 case __BLK_TA_FRONTMERGE:
1355 account_m(t, pci, w);
1356 log_merge(pdi, pci, t, "F");
1358 case __BLK_TA_GETRQ:
1359 log_track_getrq(pdi, t);
1360 log_generic(pci, t, "G");
1362 case __BLK_TA_SLEEPRQ:
1363 log_generic(pci, t, "S");
1365 case __BLK_TA_REQUEUE:
1367 * can happen if we miss traces, don't let it go
1370 if (pdi->cur_depth[w])
1371 pdi->cur_depth[w]--;
1372 account_requeue(t, pci, w);
1373 log_queue(pci, t, "R");
1375 case __BLK_TA_ISSUE:
1376 account_issue(t, pci, w);
1377 pdi->cur_depth[w]++;
1378 if (pdi->cur_depth[w] > pdi->max_depth[w])
1379 pdi->max_depth[w] = pdi->cur_depth[w];
1380 log_issue(pdi, pci, t, "D");
1382 case __BLK_TA_COMPLETE:
1383 if (pdi->cur_depth[w])
1384 pdi->cur_depth[w]--;
1385 account_c(t, pci, w, t->bytes);
1386 log_complete(pdi, pci, t, "C");
1389 log_action(pci, t, "P");
1391 case __BLK_TA_UNPLUG_IO:
1392 account_unplug(t, pci, 0);
1393 log_unplug(pci, t, "U");
1395 case __BLK_TA_UNPLUG_TIMER:
1396 account_unplug(t, pci, 1);
1397 log_unplug(pci, t, "UT");
1399 case __BLK_TA_SPLIT:
1400 log_split(pci, t, "X");
1402 case __BLK_TA_BOUNCE:
1403 log_generic(pci, t, "B");
1405 case __BLK_TA_REMAP:
1406 log_generic(pci, t, "A");
1409 fprintf(stderr, "Bad fs action %x\n", t->action);
1414 static void dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
1415 struct per_dev_info *pdi)
1417 if (t->action & BLK_TC_ACT(BLK_TC_PC))
1418 dump_trace_pc(t, pci);
1420 dump_trace_fs(t, pdi, pci);
1423 pdi->first_reported_time = t->time;
1427 output_binary(t, sizeof(*t) + t->pdu_len);
1431 * print in a proper way, not too small and not too big. if more than
1432 * 1000,000K, turn into M and so on
1434 static char *size_cnv(char *dst, unsigned long long num, int in_kb)
1436 char suff[] = { '\0', 'K', 'M', 'G', 'P' };
1442 while (num > 1000 * 1000ULL && (i < sizeof(suff) - 1)) {
1447 sprintf(dst, "%'8Lu%c", num, suff[i]);
1451 static void dump_io_stats(struct per_dev_info *pdi, struct io_stats *ios,
1454 static char x[256], y[256];
1456 fprintf(ofp, "%s\n", msg);
1458 fprintf(ofp, " Reads Queued: %s, %siB\t", size_cnv(x, ios->qreads, 0), size_cnv(y, ios->qread_kb, 1));
1459 fprintf(ofp, " Writes Queued: %s, %siB\n", size_cnv(x, ios->qwrites, 0), size_cnv(y, ios->qwrite_kb, 1));
1461 fprintf(ofp, " Read Dispatches: %s, %siB\t", size_cnv(x, ios->ireads, 0), size_cnv(y, ios->iread_kb, 1));
1462 fprintf(ofp, " Write Dispatches: %s, %siB\n", size_cnv(x, ios->iwrites, 0), size_cnv(y, ios->iwrite_kb, 1));
1463 fprintf(ofp, " Reads Requeued: %s\t\t", size_cnv(x, ios->rrqueue, 0));
1464 fprintf(ofp, " Writes Requeued: %s\n", size_cnv(x, ios->wrqueue, 0));
1465 fprintf(ofp, " Reads Completed: %s, %siB\t", size_cnv(x, ios->creads, 0), size_cnv(y, ios->cread_kb, 1));
1466 fprintf(ofp, " Writes Completed: %s, %siB\n", size_cnv(x, ios->cwrites, 0), size_cnv(y, ios->cwrite_kb, 1));
1467 fprintf(ofp, " Read Merges: %'8lu%8c\t", ios->mreads, ' ');
1468 fprintf(ofp, " Write Merges: %'8lu\n", ios->mwrites);
1470 fprintf(ofp, " Read depth: %'8u%8c\t", pdi->max_depth[0], ' ');
1471 fprintf(ofp, " Write depth: %'8u\n", pdi->max_depth[1]);
1473 fprintf(ofp, " IO unplugs: %'8lu%8c\t", ios->io_unplugs, ' ');
1474 fprintf(ofp, " Timer unplugs: %'8lu\n", ios->timer_unplugs);
1477 static void dump_wait_stats(struct per_process_info *ppi)
1479 unsigned long rawait = ppi->longest_allocation_wait[0] / 1000;
1480 unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000;
1481 unsigned long rcwait = ppi->longest_completion_wait[0] / 1000;
1482 unsigned long wawait = ppi->longest_allocation_wait[1] / 1000;
1483 unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000;
1484 unsigned long wcwait = ppi->longest_completion_wait[1] / 1000;
1486 fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' ');
1487 fprintf(ofp, " Allocation wait: %'8lu\n", wawait);
1488 fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' ');
1489 fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait);
1490 fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' ');
1491 fprintf(ofp, " Completion wait: %'8lu\n", wcwait);
1494 static int ppi_name_compare(const void *p1, const void *p2)
1496 struct per_process_info *ppi1 = *((struct per_process_info **) p1);
1497 struct per_process_info *ppi2 = *((struct per_process_info **) p2);
1500 res = strverscmp(ppi1->ppm->comm, ppi2->ppm->comm);
1502 res = ppi1->ppm->pid > ppi2->ppm->pid;
1507 static void sort_process_list(void)
1509 struct per_process_info **ppis;
1510 struct per_process_info *ppi;
1513 ppis = malloc(ppi_list_entries * sizeof(struct per_process_info *));
1518 ppi = ppi->list_next;
1521 qsort(ppis, ppi_list_entries, sizeof(ppi), ppi_name_compare);
1523 i = ppi_list_entries - 1;
1528 ppi->list_next = ppi_list;
1536 static void show_process_stats(void)
1538 struct per_process_info *ppi;
1540 sort_process_list();
1544 struct process_pid_map *ppm = ppi->ppm;
1547 if (ppi->more_than_one)
1548 sprintf(name, "%s (%u, ...)", ppm->comm, ppm->pid);
1550 sprintf(name, "%s (%u)", ppm->comm, ppm->pid);
1552 dump_io_stats(NULL, &ppi->io_stats, name);
1553 dump_wait_stats(ppi);
1554 ppi = ppi->list_next;
1560 static void show_device_and_cpu_stats(void)
1562 struct per_dev_info *pdi;
1563 struct per_cpu_info *pci;
1564 struct io_stats total, *ios;
1565 unsigned long long rrate, wrate, msec;
1566 int i, j, pci_events;
1567 char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3];
1570 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
1572 memset(&total, 0, sizeof(total));
1578 for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) {
1582 ios = &pci->io_stats;
1583 total.qreads += ios->qreads;
1584 total.qwrites += ios->qwrites;
1585 total.creads += ios->creads;
1586 total.cwrites += ios->cwrites;
1587 total.mreads += ios->mreads;
1588 total.mwrites += ios->mwrites;
1589 total.ireads += ios->ireads;
1590 total.iwrites += ios->iwrites;
1591 total.rrqueue += ios->rrqueue;
1592 total.wrqueue += ios->wrqueue;
1593 total.qread_kb += ios->qread_kb;
1594 total.qwrite_kb += ios->qwrite_kb;
1595 total.cread_kb += ios->cread_kb;
1596 total.cwrite_kb += ios->cwrite_kb;
1597 total.iread_kb += ios->iread_kb;
1598 total.iwrite_kb += ios->iwrite_kb;
1599 total.timer_unplugs += ios->timer_unplugs;
1600 total.io_unplugs += ios->io_unplugs;
1602 snprintf(line, sizeof(line) - 1, "CPU%d (%s):",
1603 j, get_dev_name(pdi, name, sizeof(name)));
1604 dump_io_stats(pdi, ios, line);
1608 if (pci_events > 1) {
1610 snprintf(line, sizeof(line) - 1, "Total (%s):",
1611 get_dev_name(pdi, name, sizeof(name)));
1612 dump_io_stats(NULL, &total, line);
1616 msec = (pdi->last_reported_time - pdi->first_reported_time) / 1000000;
1618 rrate = 1000 * total.cread_kb / msec;
1619 wrate = 1000 * total.cwrite_kb / msec;
1622 fprintf(ofp, "\nThroughput (R/W): %'LuKiB/s / %'LuKiB/s\n",
1624 fprintf(ofp, "Events (%s): %'Lu entries\n",
1625 get_dev_name(pdi, line, sizeof(line)), pdi->events);
1627 collect_pdi_skips(pdi);
1628 fprintf(ofp, "Skips: %'lu forward (%'llu - %5.1lf%%)\n",
1629 pdi->skips,pdi->seq_skips,
1630 100.0 * ((double)pdi->seq_skips /
1631 (double)(pdi->events + pdi->seq_skips)));
1635 static void find_genesis(void)
1637 struct trace *t = trace_list;
1639 genesis_time = -1ULL;
1641 if (t->bit->time < genesis_time)
1642 genesis_time = t->bit->time;
1648 static inline int check_stopwatch(struct blk_io_trace *bit)
1650 if (bit->time < stopwatch_end &&
1651 bit->time >= stopwatch_start)
1658 * return youngest entry read
1660 static int sort_entries(unsigned long long *youngest)
1662 struct per_dev_info *pdi = NULL;
1663 struct per_cpu_info *pci = NULL;
1670 while ((t = trace_list) != NULL) {
1671 struct blk_io_trace *bit = t->bit;
1673 trace_list = t->next;
1675 bit->time -= genesis_time;
1677 if (bit->time < *youngest || !*youngest)
1678 *youngest = bit->time;
1680 if (!pdi || pdi->dev != bit->device) {
1681 pdi = get_dev_info(bit->device);
1685 if (!pci || pci->cpu != bit->cpu)
1686 pci = get_cpu_info(pdi, bit->cpu);
1688 if (bit->sequence < pci->smallest_seq_read)
1689 pci->smallest_seq_read = bit->sequence;
1691 if (check_stopwatch(bit)) {
1697 if (trace_rb_insert_sort(t))
1705 * to continue, we must have traces from all online cpus in the tree
1707 static int check_cpu_map(struct per_dev_info *pdi)
1709 unsigned long *cpu_map;
1716 * create a map of the cpus we have traces for
1718 cpu_map = malloc(pdi->cpu_map_max / sizeof(long));
1719 n = rb_first(&rb_sort_root);
1721 __t = rb_entry(n, struct trace, rb_node);
1722 cpu = __t->bit->cpu;
1724 cpu_map[CPU_IDX(cpu)] |= (1UL << CPU_BIT(cpu));
1729 * we can't continue if pdi->cpu_map has entries set that we don't
1730 * have in the sort rbtree. the opposite is not a problem, though
1733 for (i = 0; i < pdi->cpu_map_max / CPUS_PER_LONG; i++) {
1734 if (pdi->cpu_map[i] & ~(cpu_map[i])) {
1744 static int check_sequence(struct per_dev_info *pdi, struct trace *t, int force)
1746 struct blk_io_trace *bit = t->bit;
1747 unsigned long expected_sequence;
1748 struct per_cpu_info *pci;
1751 pci = get_cpu_info(pdi, bit->cpu);
1752 expected_sequence = pci->last_sequence + 1;
1754 if (!expected_sequence) {
1756 * 1 should be the first entry, just allow it
1758 if (bit->sequence == 1)
1760 if (bit->sequence == pci->smallest_seq_read)
1763 return check_cpu_map(pdi);
1766 if (bit->sequence == expected_sequence)
1770 * we may not have seen that sequence yet. if we are not doing
1771 * the final run, break and wait for more entries.
1773 if (expected_sequence < pci->smallest_seq_read) {
1774 __t = trace_rb_find_last(pdi, pci, expected_sequence);
1778 __put_trace_last(pdi, __t);
1780 } else if (!force) {
1784 if (check_current_skips(pci, bit->sequence))
1787 if (expected_sequence < bit->sequence)
1788 insert_skip(pci, expected_sequence, bit->sequence - 1);
1793 static void show_entries_rb(int force)
1795 struct per_dev_info *pdi = NULL;
1796 struct per_cpu_info *pci = NULL;
1797 struct blk_io_trace *bit;
1801 while ((n = rb_first(&rb_sort_root)) != NULL) {
1802 if (is_done() && !force && !pipeline)
1805 t = rb_entry(n, struct trace, rb_node);
1808 if (read_sequence - t->read_sequence < 1 && !force)
1811 if (!pdi || pdi->dev != bit->device) {
1812 pdi = get_dev_info(bit->device);
1817 fprintf(stderr, "Unknown device ID? (%d,%d)\n",
1818 MAJOR(bit->device), MINOR(bit->device));
1822 if (check_sequence(pdi, t, force))
1825 if (!force && bit->time > last_allowed_time)
1828 check_time(pdi, bit);
1830 if (!pci || pci->cpu != bit->cpu)
1831 pci = get_cpu_info(pdi, bit->cpu);
1833 pci->last_sequence = bit->sequence;
1837 if (bit->action & (act_mask << BLK_TC_SHIFT))
1838 dump_trace(bit, pci, pdi);
1844 static int read_data(int fd, void *buffer, int bytes, int block, int *fdblock)
1846 int ret, bytes_left, fl;
1849 if (block != *fdblock) {
1850 fl = fcntl(fd, F_GETFL);
1854 fcntl(fd, F_SETFL, fl | O_NONBLOCK);
1857 fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
1863 while (bytes_left > 0) {
1864 ret = read(fd, p, bytes_left);
1868 if (errno != EAGAIN) {
1874 * never do partial reads. we can return if we
1875 * didn't read anything and we should not block,
1876 * otherwise wait for data
1878 if ((bytes_left == bytes) && !block)
1892 static inline __u16 get_pdulen(struct blk_io_trace *bit)
1895 return bit->pdu_len;
1897 return __bswap_16(bit->pdu_len);
1900 static inline __u32 get_magic(struct blk_io_trace *bit)
1905 return __bswap_32(bit->magic);
1908 static int read_events(int fd, int always_block, int *fdblock)
1910 struct per_dev_info *pdi = NULL;
1911 unsigned int events = 0;
1913 while (!is_done() && events < rb_batch) {
1914 struct blk_io_trace *bit;
1916 int pdu_len, should_block, ret;
1921 should_block = !events || always_block;
1923 ret = read_data(fd, bit, sizeof(*bit), should_block, fdblock);
1926 if (!events && ret < 0)
1932 * look at first trace to check whether we need to convert
1933 * data in the future
1935 if (data_is_native == -1 && check_data_endianness(bit->magic))
1938 magic = get_magic(bit);
1939 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
1940 fprintf(stderr, "Bad magic %x\n", magic);
1944 pdu_len = get_pdulen(bit);
1946 void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
1948 if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1, fdblock)) {
1958 if (verify_trace(bit)) {
1964 * not a real trace, so grab and handle it here
1966 if (bit->action & BLK_TC_ACT(BLK_TC_NOTIFY)) {
1967 add_ppm_hash(bit->pid, (char *) bit + sizeof(*bit));
1968 output_binary(bit, sizeof(*bit) + bit->pdu_len);
1973 memset(t, 0, sizeof(*t));
1975 t->read_sequence = read_sequence;
1977 t->next = trace_list;
1980 if (!pdi || pdi->dev != bit->device)
1981 pdi = get_dev_info(bit->device);
1983 if (bit->time > pdi->last_read_time)
1984 pdi->last_read_time = bit->time;
1992 static int do_file(void)
1994 struct per_cpu_info *pci;
1995 struct per_dev_info *pdi;
1996 int i, j, events, events_added;
1999 * first prepare all files for reading
2001 for (i = 0; i < ndevices; i++) {
2010 pci = get_cpu_info(pdi, j);
2015 p = strdup(pdi->name);
2017 if (strcmp(dname, ".")) {
2019 p = strdup(pdi->name);
2020 strcpy(pdi->name, basename(p));
2025 len = sprintf(pci->fname, "%s/", input_dir);
2027 snprintf(pci->fname + len, sizeof(pci->fname)-1-len,
2028 "%s.blktrace.%d", pdi->name, pci->cpu);
2029 if (stat(pci->fname, &st) < 0)
2032 pci->fd = open(pci->fname, O_RDONLY);
2039 printf("Input file %s added\n", pci->fname);
2041 cpu_mark_online(pdi, pci->cpu);
2046 * now loop over the files reading in the data
2049 unsigned long long youngest;
2052 last_allowed_time = -1ULL;
2055 for (i = 0; i < ndevices; i++) {
2057 pdi->last_read_time = -1ULL;
2059 for (j = 0; j < pdi->nfiles; j++) {
2061 pci = get_cpu_info(pdi, j);
2066 pci->smallest_seq_read = -1;
2068 events = read_events(pci->fd, 1, &pci->fdblock);
2070 cpu_mark_offline(pdi, pci->cpu);
2076 if (pdi->last_read_time < last_allowed_time)
2077 last_allowed_time = pdi->last_read_time;
2079 events_added += events;
2083 if (sort_entries(&youngest))
2086 if (youngest > stopwatch_end)
2091 } while (events_added);
2093 if (rb_sort_entries)
2099 static int do_stdin(void)
2101 unsigned long long youngest;
2102 int fd, events, fdblock;
2104 last_allowed_time = -1ULL;
2105 fd = dup(STDIN_FILENO);
2107 perror("dup stdin");
2112 while ((events = read_events(fd, 0, &fdblock)) > 0) {
2116 smallest_seq_read = -1U;
2119 if (sort_entries(&youngest))
2122 if (youngest > stopwatch_end)
2128 if (rb_sort_entries)
2135 static void show_stats(void)
2144 if (per_process_stats)
2145 show_process_stats();
2147 if (per_device_and_cpu_stats)
2148 show_device_and_cpu_stats();
2153 static void handle_sigint(__attribute__((__unused__)) int sig)
2159 * Extract start and duration times from a string, allowing
2160 * us to specify a time interval of interest within a trace.
2161 * Format: "duration" (start is zero) or "start:duration".
2163 static int find_stopwatch_interval(char *string)
2168 value = strtod(string, &sp);
2170 fprintf(stderr,"Invalid stopwatch timer: %s\n", string);
2174 stopwatch_start = DOUBLE_TO_NANO_ULL(value);
2176 value = strtod(string, &sp);
2177 if (sp == string || *sp != '\0') {
2178 fprintf(stderr,"Invalid stopwatch duration time: %s\n",
2182 } else if (*sp != '\0') {
2183 fprintf(stderr,"Invalid stopwatch start timer: %s\n", string);
2186 stopwatch_end = DOUBLE_TO_NANO_ULL(value);
2187 if (stopwatch_end <= stopwatch_start) {
2188 fprintf(stderr, "Invalid stopwatch interval: %Lu -> %Lu\n",
2189 stopwatch_start, stopwatch_end);
2196 static char usage_str[] = \
2197 "[ -i <input name> ] [-o <output name> [ -s ] [ -t ] [ -q ]\n" \
2198 "[ -w start:stop ] [ -f output format ] [ -F format spec ] [ -v] \n\n" \
2199 "\t-i Input file containing trace data, or '-' for stdin\n" \
2200 "\t-D Directory to prepend to input file names\n" \
2201 "\t-o Output file. If not given, output is stdout\n" \
2202 "\t-d Output file. If specified, binary data is written to file\n" \
2203 "\t-b stdin read batching\n" \
2204 "\t-s Show per-program io statistics\n" \
2205 "\t-h Hash processes by name, not pid\n" \
2206 "\t-t Track individual ios. Will tell you the time a request took\n" \
2207 "\t to get queued, to get dispatched, and to get completed\n" \
2208 "\t-q Quiet. Don't display any stats at the end of the trace\n" \
2209 "\t-w Only parse data between the given time interval in seconds.\n" \
2210 "\t If 'start' isn't given, blkparse defaults the start time to 0\n" \
2211 "\t-f Output format. Customize the output format. The format field\n" \
2212 "\t identifies can be found in the documentation\n" \
2213 "\t-F Format specification. Can be found in the documentation\n" \
2214 "\t-v More verbose for marginal errors\n" \
2215 "\t-V Print program version info\n\n";
2217 static void usage(char *prog)
2219 fprintf(stderr, "Usage: %s %s %s", prog, blkparse_version, usage_str);
2222 int main(int argc, char *argv[])
2225 int i, c, ret, mode;
2226 int act_mask_tmp = 0;
2228 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
2231 i = find_mask_map(optarg);
2233 fprintf(stderr,"Invalid action mask %s\n",
2241 if ((sscanf(optarg, "%x", &i) != 1) ||
2242 !valid_act_opt(i)) {
2244 "Invalid set action mask %s/0x%x\n",
2251 if (!strcmp(optarg, "-") && !pipeline)
2253 else if (resize_devices(optarg) != 0)
2260 output_name = optarg;
2263 rb_batch = atoi(optarg);
2265 rb_batch = RB_BATCH_DEFAULT;
2268 per_process_stats = 1;
2274 per_device_and_cpu_stats = 0;
2277 if (find_stopwatch_interval(optarg) != 0)
2281 set_all_format_specs(optarg);
2284 if (add_format_spec(optarg) != 0)
2288 ppi_hash_by_pid = 0;
2294 printf("%s version %s\n", argv[0], blkparse_version);
2297 dump_binary = optarg;
2305 while (optind < argc) {
2306 if (!strcmp(argv[optind], "-") && !pipeline)
2308 else if (resize_devices(argv[optind]) != 0)
2313 if (!pipeline && !ndevices) {
2318 if (act_mask_tmp != 0)
2319 act_mask = act_mask_tmp;
2321 memset(&rb_sort_root, 0, sizeof(rb_sort_root));
2323 signal(SIGINT, handle_sigint);
2324 signal(SIGHUP, handle_sigint);
2325 signal(SIGTERM, handle_sigint);
2327 setlocale(LC_NUMERIC, "en_US");
2330 ofp = fdopen(STDOUT_FILENO, "w");
2335 snprintf(ofname, sizeof(ofname) - 1, "%s", output_name);
2336 ofp = fopen(ofname, "w");
2345 ofp_buffer = malloc(4096);
2346 if (setvbuf(ofp, ofp_buffer, mode, 4096)) {
2352 dump_fd = creat(dump_binary, 0666);
2354 perror(dump_binary);