2 * block queue tracing parse application
4 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
5 * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <sys/types.h>
39 static char blkparse_version[] = "0.99.1";
42 unsigned long start, end;
43 struct skip_info *prev, *next;
51 unsigned long long events;
52 unsigned long long first_reported_time;
53 unsigned long long last_reported_time;
54 unsigned long long last_read_time;
55 struct io_stats io_stats;
57 unsigned long long seq_skips;
58 unsigned int max_depth[2];
59 unsigned int cur_depth[2];
61 struct rb_root rb_track;
66 unsigned long *cpu_map;
67 unsigned int cpu_map_max;
69 struct per_cpu_info *cpus;
73 * some duplicated effort here, we can unify this hash and the ppi hash later
75 struct process_pid_map {
78 struct process_pid_map *hash_next, *list_next;
81 #define PPM_HASH_SHIFT (8)
82 #define PPM_HASH_SIZE (1 << PPM_HASH_SHIFT)
83 #define PPM_HASH_MASK (PPM_HASH_SIZE - 1)
84 static struct process_pid_map *ppm_hash_table[PPM_HASH_SIZE];
86 struct per_process_info {
87 struct process_pid_map *ppm;
88 struct io_stats io_stats;
89 struct per_process_info *hash_next, *list_next;
95 unsigned long long longest_allocation_wait[2];
96 unsigned long long longest_dispatch_wait[2];
97 unsigned long long longest_completion_wait[2];
100 #define PPI_HASH_SHIFT (8)
101 #define PPI_HASH_SIZE (1 << PPI_HASH_SHIFT)
102 #define PPI_HASH_MASK (PPI_HASH_SIZE - 1)
103 static struct per_process_info *ppi_hash_table[PPI_HASH_SIZE];
104 static struct per_process_info *ppi_list;
105 static int ppi_list_entries;
107 #define S_OPTS "a:A:b:D:d:f:F:hi:o:Oqstw:vV"
108 static struct option l_opts[] = {
111 .has_arg = required_argument,
117 .has_arg = required_argument,
123 .has_arg = required_argument,
128 .name = "input-directory",
129 .has_arg = required_argument,
134 .name = "dump-binary",
135 .has_arg = required_argument,
141 .has_arg = required_argument,
146 .name = "format-spec",
147 .has_arg = required_argument,
152 .name = "hash-by-name",
153 .has_arg = no_argument,
159 .has_arg = required_argument,
165 .has_arg = required_argument,
170 .name = "no-text-output",
171 .has_arg = no_argument,
177 .has_arg = no_argument,
182 .name = "per-program-stats",
183 .has_arg = no_argument,
189 .has_arg = no_argument,
195 .has_arg = required_argument,
201 .has_arg = no_argument,
207 .has_arg = no_argument,
217 * for sorting the displayed output
220 struct blk_io_trace *bit;
221 struct rb_node rb_node;
223 unsigned long read_sequence;
226 static struct rb_root rb_sort_root;
227 static unsigned long rb_sort_entries;
229 static struct trace *trace_list;
234 static struct blk_io_trace *bit_alloc_list;
235 static struct trace *t_alloc_list;
238 * for tracking individual ios
241 struct rb_node rb_node;
243 struct process_pid_map *ppm;
245 unsigned long long allocation_time;
246 unsigned long long queue_time;
247 unsigned long long dispatch_time;
248 unsigned long long completion_time;
252 static struct per_dev_info *devices;
253 static char *get_dev_name(struct per_dev_info *, char *, int);
254 static int trace_rb_insert_last(struct per_dev_info *, struct trace *);
257 static char *output_name;
258 static char *input_dir;
260 static unsigned long long genesis_time;
261 static unsigned long long last_allowed_time;
262 static unsigned long long stopwatch_start; /* start from zero by default */
263 static unsigned long long stopwatch_end = -1ULL; /* "infinity" */
264 static unsigned long read_sequence;
266 static int per_process_stats;
267 static int per_device_and_cpu_stats = 1;
268 static int track_ios;
269 static int ppi_hash_by_pid = 1;
271 static unsigned int act_mask = -1U;
272 static int stats_printed;
273 int data_is_native = -1;
276 static char *dump_binary;
278 static unsigned int t_alloc_cache;
279 static unsigned int bit_alloc_cache;
281 #define RB_BATCH_DEFAULT (512)
282 static unsigned int rb_batch = RB_BATCH_DEFAULT;
286 static int text_output = 1;
288 #define is_done() (*(volatile int *)(&done))
289 static volatile int done;
291 #define JHASH_RANDOM (0x3af5f2ee)
293 #define CPUS_PER_LONG (8 * sizeof(unsigned long))
294 #define CPU_IDX(cpu) ((cpu) / CPUS_PER_LONG)
295 #define CPU_BIT(cpu) ((cpu) & (CPUS_PER_LONG - 1))
297 static void output_binary(void *buf, int len)
300 int n = write(dump_fd, buf, len);
309 static void resize_cpu_info(struct per_dev_info *pdi, int cpu)
311 struct per_cpu_info *cpus = pdi->cpus;
312 int ncpus = pdi->ncpus;
313 int new_count = cpu + 1;
317 size = new_count * sizeof(struct per_cpu_info);
318 cpus = realloc(cpus, size);
321 fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n",
322 get_dev_name(pdi, name, sizeof(name)), size);
326 new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info));
327 new_space = (new_count - ncpus) * sizeof(struct per_cpu_info);
328 memset(new_start, 0, new_space);
330 pdi->ncpus = new_count;
333 for (new_count = 0; new_count < pdi->ncpus; new_count++) {
334 struct per_cpu_info *pci = &pdi->cpus[new_count];
338 memset(&pci->rb_last, 0, sizeof(pci->rb_last));
339 pci->rb_last_entries = 0;
340 pci->last_sequence = -1;
345 static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu)
347 struct per_cpu_info *pci;
349 if (cpu >= pdi->ncpus)
350 resize_cpu_info(pdi, cpu);
352 pci = &pdi->cpus[cpu];
358 static int resize_devices(char *name)
360 int size = (ndevices + 1) * sizeof(struct per_dev_info);
362 devices = realloc(devices, size);
364 fprintf(stderr, "Out of memory, device %s (%d)\n", name, size);
367 memset(&devices[ndevices], 0, sizeof(struct per_dev_info));
368 devices[ndevices].name = name;
373 static struct per_dev_info *get_dev_info(dev_t dev)
375 struct per_dev_info *pdi;
378 for (i = 0; i < ndevices; i++) {
380 devices[i].dev = dev;
381 if (devices[i].dev == dev)
385 if (resize_devices(NULL))
388 pdi = &devices[ndevices - 1];
390 pdi->first_reported_time = 0;
391 pdi->last_read_time = 0;
396 static void insert_skip(struct per_cpu_info *pci, unsigned long start,
399 struct skip_info *sip;
401 for (sip = pci->skips_tail; sip != NULL; sip = sip->prev) {
402 if (end == (sip->start - 1)) {
405 } else if (start == (sip->end + 1)) {
411 sip = malloc(sizeof(struct skip_info));
414 sip->prev = sip->next = NULL;
415 if (pci->skips_tail == NULL)
416 pci->skips_head = pci->skips_tail = sip;
418 sip->prev = pci->skips_tail;
419 pci->skips_tail->next = sip;
420 pci->skips_tail = sip;
424 static void remove_sip(struct per_cpu_info *pci, struct skip_info *sip)
426 if (sip->prev == NULL) {
427 if (sip->next == NULL)
428 pci->skips_head = pci->skips_tail = NULL;
430 pci->skips_head = sip->next;
431 sip->next->prev = NULL;
433 } else if (sip->next == NULL) {
434 pci->skips_tail = sip->prev;
435 sip->prev->next = NULL;
437 sip->prev->next = sip->next;
438 sip->next->prev = sip->prev;
441 sip->prev = sip->next = NULL;
445 #define IN_SKIP(sip,seq) (((sip)->start <= (seq)) && ((seq) <= sip->end))
446 static int check_current_skips(struct per_cpu_info *pci, unsigned long seq)
448 struct skip_info *sip;
450 for (sip = pci->skips_tail; sip != NULL; sip = sip->prev) {
451 if (IN_SKIP(sip, seq)) {
452 if (sip->start == seq) {
454 remove_sip(pci, sip);
457 } else if (sip->end == seq)
461 insert_skip(pci, seq + 1, sip->end);
470 static void collect_pdi_skips(struct per_dev_info *pdi)
472 struct skip_info *sip;
478 for (cpu = 0; cpu < pdi->ncpus; cpu++) {
479 struct per_cpu_info *pci = &pdi->cpus[cpu];
481 for (sip = pci->skips_head; sip != NULL; sip = sip->next) {
483 pdi->seq_skips += (sip->end - sip->start + 1);
485 fprintf(stderr,"(%d,%d): skipping %lu -> %lu\n",
486 MAJOR(pdi->dev), MINOR(pdi->dev),
487 sip->start, sip->end);
492 static void cpu_mark_online(struct per_dev_info *pdi, unsigned int cpu)
494 if (cpu >= pdi->cpu_map_max || !pdi->cpu_map) {
495 int new_max = (cpu + CPUS_PER_LONG) & ~(CPUS_PER_LONG - 1);
496 unsigned long *map = malloc(new_max / sizeof(long));
498 memset(map, 0, new_max / sizeof(long));
501 memcpy(map, pdi->cpu_map, pdi->cpu_map_max / sizeof(long));
506 pdi->cpu_map_max = new_max;
509 pdi->cpu_map[CPU_IDX(cpu)] |= (1UL << CPU_BIT(cpu));
512 static inline void cpu_mark_offline(struct per_dev_info *pdi, int cpu)
514 pdi->cpu_map[CPU_IDX(cpu)] &= ~(1UL << CPU_BIT(cpu));
517 static inline int cpu_is_online(struct per_dev_info *pdi, int cpu)
519 return (pdi->cpu_map[CPU_IDX(cpu)] & (1UL << CPU_BIT(cpu))) != 0;
522 static inline int ppm_hash_pid(pid_t pid)
524 return jhash_1word(pid, JHASH_RANDOM) & PPM_HASH_MASK;
527 static struct process_pid_map *find_ppm(pid_t pid)
529 const int hash_idx = ppm_hash_pid(pid);
530 struct process_pid_map *ppm;
532 ppm = ppm_hash_table[hash_idx];
537 ppm = ppm->hash_next;
543 static void add_ppm_hash(pid_t pid, const char *name)
545 const int hash_idx = ppm_hash_pid(pid);
546 struct process_pid_map *ppm;
550 ppm = malloc(sizeof(*ppm));
551 memset(ppm, 0, sizeof(*ppm));
553 strcpy(ppm->comm, name);
554 ppm->hash_next = ppm_hash_table[hash_idx];
555 ppm_hash_table[hash_idx] = ppm;
559 char *find_process_name(pid_t pid)
561 struct process_pid_map *ppm = find_ppm(pid);
569 static inline int ppi_hash_pid(pid_t pid)
571 return jhash_1word(pid, JHASH_RANDOM) & PPI_HASH_MASK;
574 static inline int ppi_hash_name(const char *name)
576 return jhash(name, 16, JHASH_RANDOM) & PPI_HASH_MASK;
579 static inline int ppi_hash(struct per_process_info *ppi)
581 struct process_pid_map *ppm = ppi->ppm;
584 return ppi_hash_pid(ppm->pid);
586 return ppi_hash_name(ppm->comm);
589 static inline void add_ppi_to_hash(struct per_process_info *ppi)
591 const int hash_idx = ppi_hash(ppi);
593 ppi->hash_next = ppi_hash_table[hash_idx];
594 ppi_hash_table[hash_idx] = ppi;
597 static inline void add_ppi_to_list(struct per_process_info *ppi)
599 ppi->list_next = ppi_list;
604 static struct per_process_info *find_ppi_by_name(char *name)
606 const int hash_idx = ppi_hash_name(name);
607 struct per_process_info *ppi;
609 ppi = ppi_hash_table[hash_idx];
611 struct process_pid_map *ppm = ppi->ppm;
613 if (!strcmp(ppm->comm, name))
616 ppi = ppi->hash_next;
622 static struct per_process_info *find_ppi_by_pid(pid_t pid)
624 const int hash_idx = ppi_hash_pid(pid);
625 struct per_process_info *ppi;
627 ppi = ppi_hash_table[hash_idx];
629 struct process_pid_map *ppm = ppi->ppm;
634 ppi = ppi->hash_next;
640 static struct per_process_info *find_ppi(pid_t pid)
642 struct per_process_info *ppi;
646 return find_ppi_by_pid(pid);
648 name = find_process_name(pid);
652 ppi = find_ppi_by_name(name);
653 if (ppi && ppi->ppm->pid != pid)
654 ppi->more_than_one = 1;
660 * struct trace and blktrace allocation cache, we do potentially
661 * millions of mallocs for these structures while only using at most
662 * a few thousand at the time
664 static inline void t_free(struct trace *t)
666 if (t_alloc_cache < 1024) {
667 t->next = t_alloc_list;
674 static inline struct trace *t_alloc(void)
676 struct trace *t = t_alloc_list;
679 t_alloc_list = t->next;
684 return malloc(sizeof(*t));
687 static inline void bit_free(struct blk_io_trace *bit)
689 if (bit_alloc_cache < 1024 && !bit->pdu_len) {
691 * abuse a 64-bit field for a next pointer for the free item
693 bit->time = (__u64) (unsigned long) bit_alloc_list;
694 bit_alloc_list = (struct blk_io_trace *) bit;
700 static inline struct blk_io_trace *bit_alloc(void)
702 struct blk_io_trace *bit = bit_alloc_list;
705 bit_alloc_list = (struct blk_io_trace *) (unsigned long) \
711 return malloc(sizeof(*bit));
714 static inline void __put_trace_last(struct per_dev_info *pdi, struct trace *t)
716 struct per_cpu_info *pci = get_cpu_info(pdi, t->bit->cpu);
718 rb_erase(&t->rb_node, &pci->rb_last);
719 pci->rb_last_entries--;
725 static void put_trace(struct per_dev_info *pdi, struct trace *t)
727 rb_erase(&t->rb_node, &rb_sort_root);
730 trace_rb_insert_last(pdi, t);
733 static inline int trace_rb_insert(struct trace *t, struct rb_root *root)
735 struct rb_node **p = &root->rb_node;
736 struct rb_node *parent = NULL;
742 __t = rb_entry(parent, struct trace, rb_node);
744 if (t->bit->time < __t->bit->time)
746 else if (t->bit->time > __t->bit->time)
748 else if (t->bit->device < __t->bit->device)
750 else if (t->bit->device > __t->bit->device)
752 else if (t->bit->sequence < __t->bit->sequence)
754 else /* >= sequence */
758 rb_link_node(&t->rb_node, parent, p);
759 rb_insert_color(&t->rb_node, root);
763 static inline int trace_rb_insert_sort(struct trace *t)
765 if (!trace_rb_insert(t, &rb_sort_root)) {
773 static int trace_rb_insert_last(struct per_dev_info *pdi, struct trace *t)
775 struct per_cpu_info *pci = get_cpu_info(pdi, t->bit->cpu);
777 if (trace_rb_insert(t, &pci->rb_last))
780 pci->rb_last_entries++;
782 if (pci->rb_last_entries > rb_batch * pdi->nfiles) {
783 struct rb_node *n = rb_first(&pci->rb_last);
785 t = rb_entry(n, struct trace, rb_node);
786 __put_trace_last(pdi, t);
792 static struct trace *trace_rb_find(dev_t device, unsigned long sequence,
793 struct rb_root *root, int order)
795 struct rb_node *n = root->rb_node;
796 struct rb_node *prev = NULL;
800 __t = rb_entry(n, struct trace, rb_node);
803 if (device < __t->bit->device)
805 else if (device > __t->bit->device)
807 else if (sequence < __t->bit->sequence)
809 else if (sequence > __t->bit->sequence)
816 * hack - the list may not be sequence ordered because some
817 * events don't have sequence and time matched. so we end up
818 * being a little off in the rb lookup here, because we don't
819 * know the time we are looking for. compensate by browsing
820 * a little ahead from the last entry to find the match
825 while (((n = rb_next(prev)) != NULL) && max--) {
826 __t = rb_entry(n, struct trace, rb_node);
828 if (__t->bit->device == device &&
829 __t->bit->sequence == sequence)
839 static inline struct trace *trace_rb_find_last(struct per_dev_info *pdi,
840 struct per_cpu_info *pci,
843 return trace_rb_find(pdi->dev, seq, &pci->rb_last, 0);
846 static inline int track_rb_insert(struct per_dev_info *pdi,struct io_track *iot)
848 struct rb_node **p = &pdi->rb_track.rb_node;
849 struct rb_node *parent = NULL;
850 struct io_track *__iot;
854 __iot = rb_entry(parent, struct io_track, rb_node);
856 if (iot->sector < __iot->sector)
858 else if (iot->sector > __iot->sector)
862 "sector alias (%Lu) on device %d,%d!\n",
863 (unsigned long long) iot->sector,
864 MAJOR(pdi->dev), MINOR(pdi->dev));
869 rb_link_node(&iot->rb_node, parent, p);
870 rb_insert_color(&iot->rb_node, &pdi->rb_track);
874 static struct io_track *__find_track(struct per_dev_info *pdi, __u64 sector)
876 struct rb_node *n = pdi->rb_track.rb_node;
877 struct io_track *__iot;
880 __iot = rb_entry(n, struct io_track, rb_node);
882 if (sector < __iot->sector)
884 else if (sector > __iot->sector)
893 static struct io_track *find_track(struct per_dev_info *pdi, pid_t pid,
896 struct io_track *iot;
898 iot = __find_track(pdi, sector);
900 iot = malloc(sizeof(*iot));
901 iot->ppm = find_ppm(pid);
902 iot->sector = sector;
903 track_rb_insert(pdi, iot);
909 static void log_track_frontmerge(struct per_dev_info *pdi,
910 struct blk_io_trace *t)
912 struct io_track *iot;
917 iot = __find_track(pdi, t->sector + t_sec(t));
920 fprintf(stderr, "merge not found for (%d,%d): %llu\n",
921 MAJOR(pdi->dev), MINOR(pdi->dev),
922 (unsigned long long) t->sector + t_sec(t));
926 rb_erase(&iot->rb_node, &pdi->rb_track);
927 iot->sector -= t_sec(t);
928 track_rb_insert(pdi, iot);
931 static void log_track_getrq(struct per_dev_info *pdi, struct blk_io_trace *t)
933 struct io_track *iot;
938 iot = find_track(pdi, t->pid, t->sector);
939 iot->allocation_time = t->time;
942 static inline int is_remapper(struct per_dev_info *pdi)
944 int major = MAJOR(pdi->dev);
946 return (major == 253 || major == 9);
950 * for md/dm setups, the interesting cycle is Q -> C. So track queueing
951 * time here, as dispatch time
953 static void log_track_queue(struct per_dev_info *pdi, struct blk_io_trace *t)
955 struct io_track *iot;
959 if (!is_remapper(pdi))
962 iot = find_track(pdi, t->pid, t->sector);
963 iot->dispatch_time = t->time;
967 * return time between rq allocation and insertion
969 static unsigned long long log_track_insert(struct per_dev_info *pdi,
970 struct blk_io_trace *t)
972 unsigned long long elapsed;
973 struct io_track *iot;
978 iot = find_track(pdi, t->pid, t->sector);
979 iot->queue_time = t->time;
981 if (!iot->allocation_time)
984 elapsed = iot->queue_time - iot->allocation_time;
986 if (per_process_stats) {
987 struct per_process_info *ppi = find_ppi(iot->ppm->pid);
988 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
990 if (ppi && elapsed > ppi->longest_allocation_wait[w])
991 ppi->longest_allocation_wait[w] = elapsed;
998 * return time between queue and issue
1000 static unsigned long long log_track_issue(struct per_dev_info *pdi,
1001 struct blk_io_trace *t)
1003 unsigned long long elapsed;
1004 struct io_track *iot;
1008 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
1011 iot = __find_track(pdi, t->sector);
1014 fprintf(stderr, "issue not found for (%d,%d): %llu\n",
1015 MAJOR(pdi->dev), MINOR(pdi->dev),
1016 (unsigned long long) t->sector);
1020 iot->dispatch_time = t->time;
1021 elapsed = iot->dispatch_time - iot->queue_time;
1023 if (per_process_stats) {
1024 struct per_process_info *ppi = find_ppi(iot->ppm->pid);
1025 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
1027 if (ppi && elapsed > ppi->longest_dispatch_wait[w])
1028 ppi->longest_dispatch_wait[w] = elapsed;
1035 * return time between dispatch and complete
1037 static unsigned long long log_track_complete(struct per_dev_info *pdi,
1038 struct blk_io_trace *t)
1040 unsigned long long elapsed;
1041 struct io_track *iot;
1046 iot = __find_track(pdi, t->sector);
1049 fprintf(stderr,"complete not found for (%d,%d): %llu\n",
1050 MAJOR(pdi->dev), MINOR(pdi->dev),
1051 (unsigned long long) t->sector);
1055 iot->completion_time = t->time;
1056 elapsed = iot->completion_time - iot->dispatch_time;
1058 if (per_process_stats) {
1059 struct per_process_info *ppi = find_ppi(iot->ppm->pid);
1060 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
1062 if (ppi && elapsed > ppi->longest_completion_wait[w])
1063 ppi->longest_completion_wait[w] = elapsed;
1067 * kill the trace, we don't need it after completion
1069 rb_erase(&iot->rb_node, &pdi->rb_track);
1076 static struct io_stats *find_process_io_stats(pid_t pid)
1078 struct per_process_info *ppi = find_ppi(pid);
1081 ppi = malloc(sizeof(*ppi));
1082 memset(ppi, 0, sizeof(*ppi));
1083 ppi->ppm = find_ppm(pid);
1084 add_ppi_to_hash(ppi);
1085 add_ppi_to_list(ppi);
1088 return &ppi->io_stats;
1091 static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size)
1094 snprintf(buffer, size, "%s", pdi->name);
1096 snprintf(buffer, size, "%d,%d",MAJOR(pdi->dev),MINOR(pdi->dev));
1100 static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit)
1102 unsigned long long this = bit->time;
1103 unsigned long long last = pdi->last_reported_time;
1105 pdi->backwards = (this < last) ? 'B' : ' ';
1106 pdi->last_reported_time = this;
1109 static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t,
1114 ios->qwrite_kb += t_kb(t);
1117 ios->qread_kb += t_kb(t);
1121 static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci,
1124 __account_m(&pci->io_stats, t, rw);
1126 if (per_process_stats) {
1127 struct io_stats *ios = find_process_io_stats(t->pid);
1129 __account_m(ios, t, rw);
1133 static inline void __account_queue(struct io_stats *ios, struct blk_io_trace *t,
1138 ios->qwrite_kb += t_kb(t);
1141 ios->qread_kb += t_kb(t);
1145 static inline void account_queue(struct blk_io_trace *t,
1146 struct per_cpu_info *pci, int rw)
1148 __account_queue(&pci->io_stats, t, rw);
1150 if (per_process_stats) {
1151 struct io_stats *ios = find_process_io_stats(t->pid);
1153 __account_queue(ios, t, rw);
1157 static inline void __account_c(struct io_stats *ios, int rw, int bytes)
1161 ios->cwrite_kb += bytes >> 10;
1164 ios->cread_kb += bytes >> 10;
1168 static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci,
1171 __account_c(&pci->io_stats, rw, bytes);
1173 if (per_process_stats) {
1174 struct io_stats *ios = find_process_io_stats(t->pid);
1176 __account_c(ios, rw, bytes);
1180 static inline void __account_issue(struct io_stats *ios, int rw,
1185 ios->iwrite_kb += bytes >> 10;
1188 ios->iread_kb += bytes >> 10;
1192 static inline void account_issue(struct blk_io_trace *t,
1193 struct per_cpu_info *pci, int rw)
1195 __account_issue(&pci->io_stats, rw, t->bytes);
1197 if (per_process_stats) {
1198 struct io_stats *ios = find_process_io_stats(t->pid);
1200 __account_issue(ios, rw, t->bytes);
1204 static inline void __account_unplug(struct io_stats *ios, int timer)
1207 ios->timer_unplugs++;
1212 static inline void account_unplug(struct blk_io_trace *t,
1213 struct per_cpu_info *pci, int timer)
1215 __account_unplug(&pci->io_stats, timer);
1217 if (per_process_stats) {
1218 struct io_stats *ios = find_process_io_stats(t->pid);
1220 __account_unplug(ios, timer);
1224 static inline void __account_requeue(struct io_stats *ios,
1225 struct blk_io_trace *t, int rw)
1229 ios->iwrite_kb -= t_kb(t);
1232 ios->iread_kb -= t_kb(t);
1236 static inline void account_requeue(struct blk_io_trace *t,
1237 struct per_cpu_info *pci, int rw)
1239 __account_requeue(&pci->io_stats, t, rw);
1241 if (per_process_stats) {
1242 struct io_stats *ios = find_process_io_stats(t->pid);
1244 __account_requeue(ios, t, rw);
1248 static void log_complete(struct per_dev_info *pdi, struct per_cpu_info *pci,
1249 struct blk_io_trace *t, char *act)
1251 process_fmt(act, pci, t, log_track_complete(pdi, t), 0, NULL);
1254 static void log_insert(struct per_dev_info *pdi, struct per_cpu_info *pci,
1255 struct blk_io_trace *t, char *act)
1257 process_fmt(act, pci, t, log_track_insert(pdi, t), 0, NULL);
1260 static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t,
1263 process_fmt(act, pci, t, -1, 0, NULL);
1266 static void log_issue(struct per_dev_info *pdi, struct per_cpu_info *pci,
1267 struct blk_io_trace *t, char *act)
1269 process_fmt(act, pci, t, log_track_issue(pdi, t), 0, NULL);
1272 static void log_merge(struct per_dev_info *pdi, struct per_cpu_info *pci,
1273 struct blk_io_trace *t, char *act)
1276 log_track_frontmerge(pdi, t);
1278 process_fmt(act, pci, t, -1ULL, 0, NULL);
1281 static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t,
1284 process_fmt(act, pci, t, -1ULL, 0, NULL);
1287 static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t,
1290 process_fmt(act, pci, t, -1ULL, 0, NULL);
1293 static void log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t,
1296 process_fmt(act, pci, t, -1ULL, 0, NULL);
1299 static void log_split(struct per_cpu_info *pci, struct blk_io_trace *t,
1302 process_fmt(act, pci, t, -1ULL, 0, NULL);
1305 static void log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act)
1307 unsigned char *buf = (unsigned char *) t + sizeof(*t);
1309 process_fmt(act, pci, t, -1ULL, t->pdu_len, buf);
1312 static void dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci)
1314 int act = t->action & 0xffff;
1317 case __BLK_TA_QUEUE:
1318 log_generic(pci, t, "Q");
1320 case __BLK_TA_GETRQ:
1321 log_generic(pci, t, "G");
1323 case __BLK_TA_SLEEPRQ:
1324 log_generic(pci, t, "S");
1326 case __BLK_TA_REQUEUE:
1327 log_generic(pci, t, "R");
1329 case __BLK_TA_ISSUE:
1330 log_pc(pci, t, "D");
1332 case __BLK_TA_COMPLETE:
1333 log_pc(pci, t, "C");
1335 case __BLK_TA_INSERT:
1336 log_pc(pci, t, "I");
1339 fprintf(stderr, "Bad pc action %x\n", act);
1344 static void dump_trace_fs(struct blk_io_trace *t, struct per_dev_info *pdi,
1345 struct per_cpu_info *pci)
1347 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
1348 int act = t->action & 0xffff;
1351 case __BLK_TA_QUEUE:
1352 log_track_queue(pdi, t);
1353 account_queue(t, pci, w);
1354 log_queue(pci, t, "Q");
1356 case __BLK_TA_INSERT:
1357 log_insert(pdi, pci, t, "I");
1359 case __BLK_TA_BACKMERGE:
1360 account_m(t, pci, w);
1361 log_merge(pdi, pci, t, "M");
1363 case __BLK_TA_FRONTMERGE:
1364 account_m(t, pci, w);
1365 log_merge(pdi, pci, t, "F");
1367 case __BLK_TA_GETRQ:
1368 log_track_getrq(pdi, t);
1369 log_generic(pci, t, "G");
1371 case __BLK_TA_SLEEPRQ:
1372 log_generic(pci, t, "S");
1374 case __BLK_TA_REQUEUE:
1376 * can happen if we miss traces, don't let it go
1379 if (pdi->cur_depth[w])
1380 pdi->cur_depth[w]--;
1381 account_requeue(t, pci, w);
1382 log_queue(pci, t, "R");
1384 case __BLK_TA_ISSUE:
1385 account_issue(t, pci, w);
1386 pdi->cur_depth[w]++;
1387 if (pdi->cur_depth[w] > pdi->max_depth[w])
1388 pdi->max_depth[w] = pdi->cur_depth[w];
1389 log_issue(pdi, pci, t, "D");
1391 case __BLK_TA_COMPLETE:
1392 if (pdi->cur_depth[w])
1393 pdi->cur_depth[w]--;
1394 account_c(t, pci, w, t->bytes);
1395 log_complete(pdi, pci, t, "C");
1398 log_action(pci, t, "P");
1400 case __BLK_TA_UNPLUG_IO:
1401 account_unplug(t, pci, 0);
1402 log_unplug(pci, t, "U");
1404 case __BLK_TA_UNPLUG_TIMER:
1405 account_unplug(t, pci, 1);
1406 log_unplug(pci, t, "UT");
1408 case __BLK_TA_SPLIT:
1409 log_split(pci, t, "X");
1411 case __BLK_TA_BOUNCE:
1412 log_generic(pci, t, "B");
1414 case __BLK_TA_REMAP:
1415 log_generic(pci, t, "A");
1418 fprintf(stderr, "Bad fs action %x\n", t->action);
1423 static void dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
1424 struct per_dev_info *pdi)
1427 if (t->action & BLK_TC_ACT(BLK_TC_PC))
1428 dump_trace_pc(t, pci);
1430 dump_trace_fs(t, pdi, pci);
1434 pdi->first_reported_time = t->time;
1438 output_binary(t, sizeof(*t) + t->pdu_len);
1442 * print in a proper way, not too small and not too big. if more than
1443 * 1000,000K, turn into M and so on
1445 static char *size_cnv(char *dst, unsigned long long num, int in_kb)
1447 char suff[] = { '\0', 'K', 'M', 'G', 'P' };
1453 while (num > 1000 * 1000ULL && (i < sizeof(suff) - 1)) {
1458 sprintf(dst, "%'8Lu%c", num, suff[i]);
1462 static void dump_io_stats(struct per_dev_info *pdi, struct io_stats *ios,
1465 static char x[256], y[256];
1467 fprintf(ofp, "%s\n", msg);
1469 fprintf(ofp, " Reads Queued: %s, %siB\t", size_cnv(x, ios->qreads, 0), size_cnv(y, ios->qread_kb, 1));
1470 fprintf(ofp, " Writes Queued: %s, %siB\n", size_cnv(x, ios->qwrites, 0), size_cnv(y, ios->qwrite_kb, 1));
1472 fprintf(ofp, " Read Dispatches: %s, %siB\t", size_cnv(x, ios->ireads, 0), size_cnv(y, ios->iread_kb, 1));
1473 fprintf(ofp, " Write Dispatches: %s, %siB\n", size_cnv(x, ios->iwrites, 0), size_cnv(y, ios->iwrite_kb, 1));
1474 fprintf(ofp, " Reads Requeued: %s\t\t", size_cnv(x, ios->rrqueue, 0));
1475 fprintf(ofp, " Writes Requeued: %s\n", size_cnv(x, ios->wrqueue, 0));
1476 fprintf(ofp, " Reads Completed: %s, %siB\t", size_cnv(x, ios->creads, 0), size_cnv(y, ios->cread_kb, 1));
1477 fprintf(ofp, " Writes Completed: %s, %siB\n", size_cnv(x, ios->cwrites, 0), size_cnv(y, ios->cwrite_kb, 1));
1478 fprintf(ofp, " Read Merges: %'8lu%8c\t", ios->mreads, ' ');
1479 fprintf(ofp, " Write Merges: %'8lu\n", ios->mwrites);
1481 fprintf(ofp, " Read depth: %'8u%8c\t", pdi->max_depth[0], ' ');
1482 fprintf(ofp, " Write depth: %'8u\n", pdi->max_depth[1]);
1484 fprintf(ofp, " IO unplugs: %'8lu%8c\t", ios->io_unplugs, ' ');
1485 fprintf(ofp, " Timer unplugs: %'8lu\n", ios->timer_unplugs);
1488 static void dump_wait_stats(struct per_process_info *ppi)
1490 unsigned long rawait = ppi->longest_allocation_wait[0] / 1000;
1491 unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000;
1492 unsigned long rcwait = ppi->longest_completion_wait[0] / 1000;
1493 unsigned long wawait = ppi->longest_allocation_wait[1] / 1000;
1494 unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000;
1495 unsigned long wcwait = ppi->longest_completion_wait[1] / 1000;
1497 fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' ');
1498 fprintf(ofp, " Allocation wait: %'8lu\n", wawait);
1499 fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' ');
1500 fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait);
1501 fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' ');
1502 fprintf(ofp, " Completion wait: %'8lu\n", wcwait);
1505 static int ppi_name_compare(const void *p1, const void *p2)
1507 struct per_process_info *ppi1 = *((struct per_process_info **) p1);
1508 struct per_process_info *ppi2 = *((struct per_process_info **) p2);
1511 res = strverscmp(ppi1->ppm->comm, ppi2->ppm->comm);
1513 res = ppi1->ppm->pid > ppi2->ppm->pid;
1518 static void sort_process_list(void)
1520 struct per_process_info **ppis;
1521 struct per_process_info *ppi;
1524 ppis = malloc(ppi_list_entries * sizeof(struct per_process_info *));
1529 ppi = ppi->list_next;
1532 qsort(ppis, ppi_list_entries, sizeof(ppi), ppi_name_compare);
1534 i = ppi_list_entries - 1;
1539 ppi->list_next = ppi_list;
1547 static void show_process_stats(void)
1549 struct per_process_info *ppi;
1551 sort_process_list();
1555 struct process_pid_map *ppm = ppi->ppm;
1558 if (ppi->more_than_one)
1559 sprintf(name, "%s (%u, ...)", ppm->comm, ppm->pid);
1561 sprintf(name, "%s (%u)", ppm->comm, ppm->pid);
1563 dump_io_stats(NULL, &ppi->io_stats, name);
1564 dump_wait_stats(ppi);
1565 ppi = ppi->list_next;
1571 static void show_device_and_cpu_stats(void)
1573 struct per_dev_info *pdi;
1574 struct per_cpu_info *pci;
1575 struct io_stats total, *ios;
1576 unsigned long long rrate, wrate, msec;
1577 int i, j, pci_events;
1578 char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3];
1581 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
1583 memset(&total, 0, sizeof(total));
1589 for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) {
1593 ios = &pci->io_stats;
1594 total.qreads += ios->qreads;
1595 total.qwrites += ios->qwrites;
1596 total.creads += ios->creads;
1597 total.cwrites += ios->cwrites;
1598 total.mreads += ios->mreads;
1599 total.mwrites += ios->mwrites;
1600 total.ireads += ios->ireads;
1601 total.iwrites += ios->iwrites;
1602 total.rrqueue += ios->rrqueue;
1603 total.wrqueue += ios->wrqueue;
1604 total.qread_kb += ios->qread_kb;
1605 total.qwrite_kb += ios->qwrite_kb;
1606 total.cread_kb += ios->cread_kb;
1607 total.cwrite_kb += ios->cwrite_kb;
1608 total.iread_kb += ios->iread_kb;
1609 total.iwrite_kb += ios->iwrite_kb;
1610 total.timer_unplugs += ios->timer_unplugs;
1611 total.io_unplugs += ios->io_unplugs;
1613 snprintf(line, sizeof(line) - 1, "CPU%d (%s):",
1614 j, get_dev_name(pdi, name, sizeof(name)));
1615 dump_io_stats(pdi, ios, line);
1619 if (pci_events > 1) {
1621 snprintf(line, sizeof(line) - 1, "Total (%s):",
1622 get_dev_name(pdi, name, sizeof(name)));
1623 dump_io_stats(NULL, &total, line);
1627 msec = (pdi->last_reported_time - pdi->first_reported_time) / 1000000;
1629 rrate = 1000 * total.cread_kb / msec;
1630 wrate = 1000 * total.cwrite_kb / msec;
1633 fprintf(ofp, "\nThroughput (R/W): %'LuKiB/s / %'LuKiB/s\n",
1635 fprintf(ofp, "Events (%s): %'Lu entries\n",
1636 get_dev_name(pdi, line, sizeof(line)), pdi->events);
1638 collect_pdi_skips(pdi);
1639 fprintf(ofp, "Skips: %'lu forward (%'llu - %5.1lf%%)\n",
1640 pdi->skips,pdi->seq_skips,
1641 100.0 * ((double)pdi->seq_skips /
1642 (double)(pdi->events + pdi->seq_skips)));
1646 static void find_genesis(void)
1648 struct trace *t = trace_list;
1650 genesis_time = -1ULL;
1652 if (t->bit->time < genesis_time)
1653 genesis_time = t->bit->time;
1659 static inline int check_stopwatch(struct blk_io_trace *bit)
1661 if (bit->time < stopwatch_end &&
1662 bit->time >= stopwatch_start)
1669 * return youngest entry read
1671 static int sort_entries(unsigned long long *youngest)
1673 struct per_dev_info *pdi = NULL;
1674 struct per_cpu_info *pci = NULL;
1681 while ((t = trace_list) != NULL) {
1682 struct blk_io_trace *bit = t->bit;
1684 trace_list = t->next;
1686 bit->time -= genesis_time;
1688 if (bit->time < *youngest || !*youngest)
1689 *youngest = bit->time;
1691 if (!pdi || pdi->dev != bit->device) {
1692 pdi = get_dev_info(bit->device);
1696 if (!pci || pci->cpu != bit->cpu)
1697 pci = get_cpu_info(pdi, bit->cpu);
1699 if (bit->sequence < pci->smallest_seq_read)
1700 pci->smallest_seq_read = bit->sequence;
1702 if (check_stopwatch(bit)) {
1708 if (trace_rb_insert_sort(t))
1716 * to continue, we must have traces from all online cpus in the tree
1718 static int check_cpu_map(struct per_dev_info *pdi)
1720 unsigned long *cpu_map;
1727 * create a map of the cpus we have traces for
1729 cpu_map = malloc(pdi->cpu_map_max / sizeof(long));
1730 n = rb_first(&rb_sort_root);
1732 __t = rb_entry(n, struct trace, rb_node);
1733 cpu = __t->bit->cpu;
1735 cpu_map[CPU_IDX(cpu)] |= (1UL << CPU_BIT(cpu));
1740 * we can't continue if pdi->cpu_map has entries set that we don't
1741 * have in the sort rbtree. the opposite is not a problem, though
1744 for (i = 0; i < pdi->cpu_map_max / CPUS_PER_LONG; i++) {
1745 if (pdi->cpu_map[i] & ~(cpu_map[i])) {
1755 static int check_sequence(struct per_dev_info *pdi, struct trace *t, int force)
1757 struct blk_io_trace *bit = t->bit;
1758 unsigned long expected_sequence;
1759 struct per_cpu_info *pci;
1762 pci = get_cpu_info(pdi, bit->cpu);
1763 expected_sequence = pci->last_sequence + 1;
1765 if (!expected_sequence) {
1767 * 1 should be the first entry, just allow it
1769 if (bit->sequence == 1)
1771 if (bit->sequence == pci->smallest_seq_read)
1774 return check_cpu_map(pdi);
1777 if (bit->sequence == expected_sequence)
1781 * we may not have seen that sequence yet. if we are not doing
1782 * the final run, break and wait for more entries.
1784 if (expected_sequence < pci->smallest_seq_read) {
1785 __t = trace_rb_find_last(pdi, pci, expected_sequence);
1789 __put_trace_last(pdi, __t);
1791 } else if (!force) {
1795 if (check_current_skips(pci, bit->sequence))
1798 if (expected_sequence < bit->sequence)
1799 insert_skip(pci, expected_sequence, bit->sequence - 1);
1804 static void show_entries_rb(int force)
1806 struct per_dev_info *pdi = NULL;
1807 struct per_cpu_info *pci = NULL;
1808 struct blk_io_trace *bit;
1812 while ((n = rb_first(&rb_sort_root)) != NULL) {
1813 if (is_done() && !force && !pipeline)
1816 t = rb_entry(n, struct trace, rb_node);
1819 if (read_sequence - t->read_sequence < 1 && !force)
1822 if (!pdi || pdi->dev != bit->device) {
1823 pdi = get_dev_info(bit->device);
1828 fprintf(stderr, "Unknown device ID? (%d,%d)\n",
1829 MAJOR(bit->device), MINOR(bit->device));
1833 if (check_sequence(pdi, t, force))
1836 if (!force && bit->time > last_allowed_time)
1839 check_time(pdi, bit);
1841 if (!pci || pci->cpu != bit->cpu)
1842 pci = get_cpu_info(pdi, bit->cpu);
1844 pci->last_sequence = bit->sequence;
1848 if (bit->action & (act_mask << BLK_TC_SHIFT))
1849 dump_trace(bit, pci, pdi);
1855 static int read_data(int fd, void *buffer, int bytes, int block, int *fdblock)
1857 int ret, bytes_left, fl;
1860 if (block != *fdblock) {
1861 fl = fcntl(fd, F_GETFL);
1865 fcntl(fd, F_SETFL, fl | O_NONBLOCK);
1868 fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
1874 while (bytes_left > 0) {
1875 ret = read(fd, p, bytes_left);
1879 if (errno != EAGAIN) {
1885 * never do partial reads. we can return if we
1886 * didn't read anything and we should not block,
1887 * otherwise wait for data
1889 if ((bytes_left == bytes) && !block)
1903 static inline __u16 get_pdulen(struct blk_io_trace *bit)
1906 return bit->pdu_len;
1908 return __bswap_16(bit->pdu_len);
1911 static inline __u32 get_magic(struct blk_io_trace *bit)
1916 return __bswap_32(bit->magic);
1919 static int read_events(int fd, int always_block, int *fdblock)
1921 struct per_dev_info *pdi = NULL;
1922 unsigned int events = 0;
1924 while (!is_done() && events < rb_batch) {
1925 struct blk_io_trace *bit;
1927 int pdu_len, should_block, ret;
1932 should_block = !events || always_block;
1934 ret = read_data(fd, bit, sizeof(*bit), should_block, fdblock);
1937 if (!events && ret < 0)
1943 * look at first trace to check whether we need to convert
1944 * data in the future
1946 if (data_is_native == -1 && check_data_endianness(bit->magic))
1949 magic = get_magic(bit);
1950 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
1951 fprintf(stderr, "Bad magic %x\n", magic);
1955 pdu_len = get_pdulen(bit);
1957 void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
1959 if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1, fdblock)) {
1969 if (verify_trace(bit)) {
1975 * not a real trace, so grab and handle it here
1977 if (bit->action & BLK_TC_ACT(BLK_TC_NOTIFY)) {
1978 add_ppm_hash(bit->pid, (char *) bit + sizeof(*bit));
1979 output_binary(bit, sizeof(*bit) + bit->pdu_len);
1984 memset(t, 0, sizeof(*t));
1986 t->read_sequence = read_sequence;
1988 t->next = trace_list;
1991 if (!pdi || pdi->dev != bit->device)
1992 pdi = get_dev_info(bit->device);
1994 if (bit->time > pdi->last_read_time)
1995 pdi->last_read_time = bit->time;
2003 static int do_file(void)
2005 struct per_cpu_info *pci;
2006 struct per_dev_info *pdi;
2007 int i, j, events, events_added;
2010 * first prepare all files for reading
2012 for (i = 0; i < ndevices; i++) {
2021 pci = get_cpu_info(pdi, j);
2026 p = strdup(pdi->name);
2028 if (strcmp(dname, ".")) {
2030 p = strdup(pdi->name);
2031 strcpy(pdi->name, basename(p));
2036 len = sprintf(pci->fname, "%s/", input_dir);
2038 snprintf(pci->fname + len, sizeof(pci->fname)-1-len,
2039 "%s.blktrace.%d", pdi->name, pci->cpu);
2040 if (stat(pci->fname, &st) < 0)
2043 pci->fd = open(pci->fname, O_RDONLY);
2050 printf("Input file %s added\n", pci->fname);
2052 cpu_mark_online(pdi, pci->cpu);
2057 * now loop over the files reading in the data
2060 unsigned long long youngest;
2063 last_allowed_time = -1ULL;
2066 for (i = 0; i < ndevices; i++) {
2068 pdi->last_read_time = -1ULL;
2070 for (j = 0; j < pdi->nfiles; j++) {
2072 pci = get_cpu_info(pdi, j);
2077 pci->smallest_seq_read = -1;
2079 events = read_events(pci->fd, 1, &pci->fdblock);
2081 cpu_mark_offline(pdi, pci->cpu);
2087 if (pdi->last_read_time < last_allowed_time)
2088 last_allowed_time = pdi->last_read_time;
2090 events_added += events;
2094 if (sort_entries(&youngest))
2097 if (youngest > stopwatch_end)
2102 } while (events_added);
2104 if (rb_sort_entries)
2110 static int do_stdin(void)
2112 unsigned long long youngest;
2113 int fd, events, fdblock;
2115 last_allowed_time = -1ULL;
2116 fd = dup(STDIN_FILENO);
2118 perror("dup stdin");
2123 while ((events = read_events(fd, 0, &fdblock)) > 0) {
2127 smallest_seq_read = -1U;
2130 if (sort_entries(&youngest))
2133 if (youngest > stopwatch_end)
2139 if (rb_sort_entries)
2146 static void show_stats(void)
2155 if (per_process_stats)
2156 show_process_stats();
2158 if (per_device_and_cpu_stats)
2159 show_device_and_cpu_stats();
2164 static void handle_sigint(__attribute__((__unused__)) int sig)
2170 * Extract start and duration times from a string, allowing
2171 * us to specify a time interval of interest within a trace.
2172 * Format: "duration" (start is zero) or "start:duration".
2174 static int find_stopwatch_interval(char *string)
2179 value = strtod(string, &sp);
2181 fprintf(stderr,"Invalid stopwatch timer: %s\n", string);
2185 stopwatch_start = DOUBLE_TO_NANO_ULL(value);
2187 value = strtod(string, &sp);
2188 if (sp == string || *sp != '\0') {
2189 fprintf(stderr,"Invalid stopwatch duration time: %s\n",
2193 } else if (*sp != '\0') {
2194 fprintf(stderr,"Invalid stopwatch start timer: %s\n", string);
2197 stopwatch_end = DOUBLE_TO_NANO_ULL(value);
2198 if (stopwatch_end <= stopwatch_start) {
2199 fprintf(stderr, "Invalid stopwatch interval: %Lu -> %Lu\n",
2200 stopwatch_start, stopwatch_end);
2207 #define S_OPTS "a:A:b:D:d:f:F:hi:o:Oqstw:vV"
2208 static char usage_str[] = "\n\n" \
2209 "-i <file> | --input=<file>\n" \
2210 "[ -a <action field> | --act-mask=<action field> ]\n" \
2211 "[ -A <action mask> | --set-mask=<action mask> ]\n" \
2212 "[ -b <traces> | --batch=<traces> ]\n" \
2213 "[ -d <file> | --dump-binary=<file> ]\n" \
2214 "[ -D <dir> | --input-directory=<dir> ]\n" \
2215 "[ -f <format> | --format=<format> ]\n" \
2216 "[ -F <spec> | --format-spec=<spec> ]\n" \
2217 "[ -h | --hash-by-name ]\n" \
2218 "[ -o <file> | --output=<file> ]\n" \
2219 "[ -O | --no-text-output ]\n" \
2220 "[ -q | --quiet ]\n" \
2221 "[ -s | --per-program-stats ]\n" \
2222 "[ -t | --track-ios ]\n" \
2223 "[ -w <time> | --stopwatch=<time> ]\n" \
2224 "[ -v | --verbose ]\n" \
2225 "[ -V | --version ]\n\n" \
2226 "\t-b stdin read batching\n" \
2227 "\t-d Output file. If specified, binary data is written to file\n" \
2228 "\t-D Directory to prepend to input file names\n" \
2229 "\t-f Output format. Customize the output format. The format field\n" \
2230 "\t identifies can be found in the documentation\n" \
2231 "\t-F Format specification. Can be found in the documentation\n" \
2232 "\t-h Hash processes by name, not pid\n" \
2233 "\t-i Input file containing trace data, or '-' for stdin\n" \
2234 "\t-o Output file. If not given, output is stdout\n" \
2235 "\t-O Do NOT output text data\n" \
2236 "\t-q Quiet. Don't display any stats at the end of the trace\n" \
2237 "\t-s Show per-program io statistics\n" \
2238 "\t-t Track individual ios. Will tell you the time a request took\n" \
2239 "\t to get queued, to get dispatched, and to get completed\n" \
2240 "\t-w Only parse data between the given time interval in seconds.\n" \
2241 "\t If 'start' isn't given, blkparse defaults the start time to 0\n" \
2242 "\t-v More verbose for marginal errors\n" \
2243 "\t-V Print program version info\n\n";
2245 static void usage(char *prog)
2247 fprintf(stderr, "Usage: %s %s %s", prog, blkparse_version, usage_str);
2250 int main(int argc, char *argv[])
2252 int i, c, ret, mode;
2253 int act_mask_tmp = 0;
2254 char *ofp_buffer = NULL;
2256 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
2259 i = find_mask_map(optarg);
2261 fprintf(stderr,"Invalid action mask %s\n",
2269 if ((sscanf(optarg, "%x", &i) != 1) ||
2270 !valid_act_opt(i)) {
2272 "Invalid set action mask %s/0x%x\n",
2279 if (!strcmp(optarg, "-") && !pipeline)
2281 else if (resize_devices(optarg) != 0)
2288 output_name = optarg;
2294 rb_batch = atoi(optarg);
2296 rb_batch = RB_BATCH_DEFAULT;
2299 per_process_stats = 1;
2305 per_device_and_cpu_stats = 0;
2308 if (find_stopwatch_interval(optarg) != 0)
2312 set_all_format_specs(optarg);
2315 if (add_format_spec(optarg) != 0)
2319 ppi_hash_by_pid = 0;
2325 printf("%s version %s\n", argv[0], blkparse_version);
2328 dump_binary = optarg;
2336 while (optind < argc) {
2337 if (!strcmp(argv[optind], "-") && !pipeline)
2339 else if (resize_devices(argv[optind]) != 0)
2344 if (!pipeline && !ndevices) {
2349 if (act_mask_tmp != 0)
2350 act_mask = act_mask_tmp;
2352 memset(&rb_sort_root, 0, sizeof(rb_sort_root));
2354 signal(SIGINT, handle_sigint);
2355 signal(SIGHUP, handle_sigint);
2356 signal(SIGTERM, handle_sigint);
2358 setlocale(LC_NUMERIC, "en_US");
2362 ofp = fdopen(STDOUT_FILENO, "w");
2367 snprintf(ofname, sizeof(ofname) - 1, "%s", output_name);
2368 ofp = fopen(ofname, "w");
2377 ofp_buffer = malloc(4096);
2378 if (setvbuf(ofp, ofp_buffer, mode, 4096)) {
2385 dump_fd = creat(dump_binary, 0666);
2387 perror(dump_binary);