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>
40 static char blkparse_version[] = "1.2.0";
43 unsigned long start, end;
44 struct skip_info *prev, *next;
52 unsigned long long events;
53 unsigned long long first_reported_time;
54 unsigned long long last_reported_time;
55 unsigned long long last_read_time;
56 struct io_stats io_stats;
58 unsigned long long seq_skips;
59 unsigned int max_depth[2];
60 unsigned int cur_depth[2];
62 struct rb_root rb_track;
67 unsigned long *cpu_map;
68 unsigned int cpu_map_max;
70 struct per_cpu_info *cpus;
74 * some duplicated effort here, we can unify this hash and the ppi hash later
76 struct process_pid_map {
79 struct process_pid_map *hash_next, *list_next;
82 #define PPM_HASH_SHIFT (8)
83 #define PPM_HASH_SIZE (1 << PPM_HASH_SHIFT)
84 #define PPM_HASH_MASK (PPM_HASH_SIZE - 1)
85 static struct process_pid_map *ppm_hash_table[PPM_HASH_SIZE];
87 struct per_process_info {
88 struct process_pid_map *ppm;
89 struct io_stats io_stats;
90 struct per_process_info *hash_next, *list_next;
96 unsigned long long longest_allocation_wait[2];
97 unsigned long long longest_dispatch_wait[2];
98 unsigned long long longest_completion_wait[2];
101 #define PPI_HASH_SHIFT (8)
102 #define PPI_HASH_SIZE (1 << PPI_HASH_SHIFT)
103 #define PPI_HASH_MASK (PPI_HASH_SIZE - 1)
106 SORT_PROG_EVENT_N, /* Program Name */
107 SORT_PROG_EVENT_QKB, /* KB: Queued read and write */
108 SORT_PROG_EVENT_RKB, /* KB: Queued Read */
109 SORT_PROG_EVENT_WKB, /* KB: Queued Write */
110 SORT_PROG_EVENT_CKB, /* KB: Complete */
111 SORT_PROG_EVENT_QIO, /* IO: Queued read and write */
112 SORT_PROG_EVENT_RIO, /* IO: Queued Read */
113 SORT_PROG_EVENT_WIO, /* IO: Queued Write */
114 SORT_PROG_EVENT_CIO, /* IO: Complete */
117 static struct per_process_info *ppi_hash_table[PPI_HASH_SIZE];
118 static struct per_process_info *ppi_list;
119 static int ppi_list_entries;
121 static struct option l_opts[] = {
124 .has_arg = required_argument,
130 .has_arg = required_argument,
136 .has_arg = required_argument,
141 .name = "input-directory",
142 .has_arg = required_argument,
147 .name = "dump-binary",
148 .has_arg = required_argument,
154 .has_arg = required_argument,
159 .name = "format-spec",
160 .has_arg = required_argument,
165 .name = "hash-by-name",
166 .has_arg = no_argument,
172 .has_arg = required_argument,
178 .has_arg = no_argument,
184 .has_arg = required_argument,
189 .name = "no-text-output",
190 .has_arg = no_argument,
196 .has_arg = no_argument,
201 .name = "per-program-stats",
202 .has_arg = no_argument,
207 .name = "sort-program-stats",
208 .has_arg = required_argument,
214 .has_arg = no_argument,
220 .has_arg = required_argument,
226 .has_arg = no_argument,
232 .has_arg = no_argument,
242 * for sorting the displayed output
245 struct blk_io_trace *bit;
246 struct rb_node rb_node;
248 unsigned long read_sequence;
251 static struct rb_root rb_sort_root;
252 static unsigned long rb_sort_entries;
254 static struct trace *trace_list;
259 static struct blk_io_trace *bit_alloc_list;
260 static struct trace *t_alloc_list;
263 * for tracking individual ios
266 struct rb_node rb_node;
268 struct process_pid_map *ppm;
270 unsigned long long allocation_time;
271 unsigned long long queue_time;
272 unsigned long long dispatch_time;
273 unsigned long long completion_time;
277 static struct per_dev_info *devices;
278 static char *get_dev_name(struct per_dev_info *, char *, int);
279 static int trace_rb_insert_last(struct per_dev_info *, struct trace *);
282 static char *output_name;
283 static char *input_dir;
285 static unsigned long long genesis_time;
286 static unsigned long long last_allowed_time;
287 static unsigned long long stopwatch_start; /* start from zero by default */
288 static unsigned long long stopwatch_end = -1ULL; /* "infinity" */
289 static unsigned long read_sequence;
291 static int per_process_stats;
292 static int per_process_stats_event = SORT_PROG_EVENT_N;
293 static int per_device_and_cpu_stats = 1;
294 static int track_ios;
295 static int ppi_hash_by_pid = 1;
297 static unsigned int act_mask = -1U;
298 static int stats_printed;
299 static int bin_output_msgs = 1;
300 int data_is_native = -1;
302 static FILE *dump_fp;
303 static char *dump_binary;
305 static unsigned int t_alloc_cache;
306 static unsigned int bit_alloc_cache;
308 #define RB_BATCH_DEFAULT (512)
309 static unsigned int rb_batch = RB_BATCH_DEFAULT;
312 static char *pipename;
314 static int text_output = 1;
316 #define is_done() (*(volatile int *)(&done))
317 static volatile int done;
319 struct timespec abs_start_time;
320 static unsigned long long start_timestamp;
322 static int have_drv_data = 0;
324 #define JHASH_RANDOM (0x3af5f2ee)
326 #define CPUS_PER_LONG (8 * sizeof(unsigned long))
327 #define CPU_IDX(cpu) ((cpu) / CPUS_PER_LONG)
328 #define CPU_BIT(cpu) ((cpu) & (CPUS_PER_LONG - 1))
330 static void io_warn_unless(struct blk_io_trace *t, int condition,
331 const char *fmt, ...)
338 printf("(%d,%d) request %llu + %u: ",
339 MAJOR(t->device), MINOR(t->device),
340 t->sector, t->bytes);
341 vfprintf(stderr, fmt, ap);
345 static void output_binary(void *buf, int len)
348 size_t n = fwrite(buf, len, 1, dump_fp);
357 static void resize_cpu_info(struct per_dev_info *pdi, int cpu)
359 struct per_cpu_info *cpus = pdi->cpus;
360 int ncpus = pdi->ncpus;
361 int new_count = cpu + 1;
365 size = new_count * sizeof(struct per_cpu_info);
366 cpus = realloc(cpus, size);
369 fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n",
370 get_dev_name(pdi, name, sizeof(name)), size);
374 new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info));
375 new_space = (new_count - ncpus) * sizeof(struct per_cpu_info);
376 memset(new_start, 0, new_space);
378 pdi->ncpus = new_count;
381 for (new_count = 0; new_count < pdi->ncpus; new_count++) {
382 struct per_cpu_info *pci = &pdi->cpus[new_count];
386 memset(&pci->rb_last, 0, sizeof(pci->rb_last));
387 pci->rb_last_entries = 0;
388 pci->last_sequence = -1;
393 static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu)
395 struct per_cpu_info *pci;
397 if (cpu >= pdi->ncpus)
398 resize_cpu_info(pdi, cpu);
400 pci = &pdi->cpus[cpu];
406 static int resize_devices(char *name)
408 int size = (ndevices + 1) * sizeof(struct per_dev_info);
410 devices = realloc(devices, size);
412 fprintf(stderr, "Out of memory, device %s (%d)\n", name, size);
415 memset(&devices[ndevices], 0, sizeof(struct per_dev_info));
416 devices[ndevices].name = name;
421 static struct per_dev_info *get_dev_info(dev_t dev)
423 struct per_dev_info *pdi;
426 for (i = 0; i < ndevices; i++) {
428 devices[i].dev = dev;
429 if (devices[i].dev == dev)
433 if (resize_devices(NULL))
436 pdi = &devices[ndevices - 1];
438 pdi->first_reported_time = 0;
439 pdi->last_read_time = 0;
444 static void insert_skip(struct per_cpu_info *pci, unsigned long start,
447 struct skip_info *sip;
449 for (sip = pci->skips_tail; sip != NULL; sip = sip->prev) {
450 if (end == (sip->start - 1)) {
453 } else if (start == (sip->end + 1)) {
459 sip = malloc(sizeof(struct skip_info));
462 sip->prev = sip->next = NULL;
463 if (pci->skips_tail == NULL)
464 pci->skips_head = pci->skips_tail = sip;
466 sip->prev = pci->skips_tail;
467 pci->skips_tail->next = sip;
468 pci->skips_tail = sip;
472 static void remove_sip(struct per_cpu_info *pci, struct skip_info *sip)
474 if (sip->prev == NULL) {
475 if (sip->next == NULL)
476 pci->skips_head = pci->skips_tail = NULL;
478 pci->skips_head = sip->next;
479 sip->next->prev = NULL;
481 } else if (sip->next == NULL) {
482 pci->skips_tail = sip->prev;
483 sip->prev->next = NULL;
485 sip->prev->next = sip->next;
486 sip->next->prev = sip->prev;
489 sip->prev = sip->next = NULL;
493 #define IN_SKIP(sip,seq) (((sip)->start <= (seq)) && ((seq) <= sip->end))
494 static int check_current_skips(struct per_cpu_info *pci, unsigned long seq)
496 struct skip_info *sip;
498 for (sip = pci->skips_tail; sip != NULL; sip = sip->prev) {
499 if (IN_SKIP(sip, seq)) {
500 if (sip->start == seq) {
502 remove_sip(pci, sip);
505 } else if (sip->end == seq)
509 insert_skip(pci, seq + 1, sip->end);
518 static void collect_pdi_skips(struct per_dev_info *pdi)
520 struct skip_info *sip;
526 for (cpu = 0; cpu < pdi->ncpus; cpu++) {
527 struct per_cpu_info *pci = &pdi->cpus[cpu];
529 for (sip = pci->skips_head; sip != NULL; sip = sip->next) {
531 pdi->seq_skips += (sip->end - sip->start + 1);
533 fprintf(stderr,"(%d,%d): skipping %lu -> %lu\n",
534 MAJOR(pdi->dev), MINOR(pdi->dev),
535 sip->start, sip->end);
540 static void cpu_mark_online(struct per_dev_info *pdi, unsigned int cpu)
542 if (cpu >= pdi->cpu_map_max || !pdi->cpu_map) {
543 int new_max = (cpu + CPUS_PER_LONG) & ~(CPUS_PER_LONG - 1);
544 unsigned long *map = malloc(new_max / sizeof(long));
546 memset(map, 0, new_max / sizeof(long));
549 memcpy(map, pdi->cpu_map, pdi->cpu_map_max / sizeof(long));
554 pdi->cpu_map_max = new_max;
557 pdi->cpu_map[CPU_IDX(cpu)] |= (1UL << CPU_BIT(cpu));
560 static inline void cpu_mark_offline(struct per_dev_info *pdi, int cpu)
562 pdi->cpu_map[CPU_IDX(cpu)] &= ~(1UL << CPU_BIT(cpu));
565 static inline int cpu_is_online(struct per_dev_info *pdi, int cpu)
567 return (pdi->cpu_map[CPU_IDX(cpu)] & (1UL << CPU_BIT(cpu))) != 0;
570 static inline int ppm_hash_pid(pid_t pid)
572 return jhash_1word(pid, JHASH_RANDOM) & PPM_HASH_MASK;
575 static struct process_pid_map *find_ppm(pid_t pid)
577 const int hash_idx = ppm_hash_pid(pid);
578 struct process_pid_map *ppm;
580 ppm = ppm_hash_table[hash_idx];
585 ppm = ppm->hash_next;
591 static struct process_pid_map *add_ppm_hash(pid_t pid, const char *name)
593 const int hash_idx = ppm_hash_pid(pid);
594 struct process_pid_map *ppm;
598 ppm = malloc(sizeof(*ppm));
599 memset(ppm, 0, sizeof(*ppm));
601 memset(ppm->comm, 0, sizeof(ppm->comm));
602 strncpy(ppm->comm, name, sizeof(ppm->comm));
603 ppm->comm[sizeof(ppm->comm) - 1] = '\0';
604 ppm->hash_next = ppm_hash_table[hash_idx];
605 ppm_hash_table[hash_idx] = ppm;
611 static void handle_notify(struct blk_io_trace *bit)
613 void *payload = (caddr_t) bit + sizeof(*bit);
616 switch (bit->action) {
618 add_ppm_hash(bit->pid, payload);
621 case BLK_TN_TIMESTAMP:
622 if (bit->pdu_len != sizeof(two32))
624 memcpy(two32, payload, sizeof(two32));
625 if (!data_is_native) {
626 two32[0] = be32_to_cpu(two32[0]);
627 two32[1] = be32_to_cpu(two32[1]);
629 start_timestamp = bit->time;
630 abs_start_time.tv_sec = two32[0];
631 abs_start_time.tv_nsec = two32[1];
632 if (abs_start_time.tv_nsec < 0) {
633 abs_start_time.tv_sec--;
634 abs_start_time.tv_nsec += 1000000000;
640 if (bit->pdu_len > 0) {
641 char msg[bit->pdu_len+1];
643 memcpy(msg, (char *)payload, bit->pdu_len);
644 msg[bit->pdu_len] = '\0';
647 "%3d,%-3d %2d %8s %5d.%09lu %5u %2s %3s %s\n",
648 MAJOR(bit->device), MINOR(bit->device),
649 bit->cpu, "0", (int) SECONDS(bit->time),
650 (unsigned long) NANO_SECONDS(bit->time),
656 /* Ignore unknown notify events */
661 char *find_process_name(pid_t pid)
663 struct process_pid_map *ppm = find_ppm(pid);
671 static inline int ppi_hash_pid(pid_t pid)
673 return jhash_1word(pid, JHASH_RANDOM) & PPI_HASH_MASK;
676 static inline int ppi_hash_name(const char *name)
678 return jhash(name, 16, JHASH_RANDOM) & PPI_HASH_MASK;
681 static inline int ppi_hash(struct per_process_info *ppi)
683 struct process_pid_map *ppm = ppi->ppm;
686 return ppi_hash_pid(ppm->pid);
688 return ppi_hash_name(ppm->comm);
691 static inline void add_ppi_to_hash(struct per_process_info *ppi)
693 const int hash_idx = ppi_hash(ppi);
695 ppi->hash_next = ppi_hash_table[hash_idx];
696 ppi_hash_table[hash_idx] = ppi;
699 static inline void add_ppi_to_list(struct per_process_info *ppi)
701 ppi->list_next = ppi_list;
706 static struct per_process_info *find_ppi_by_name(char *name)
708 const int hash_idx = ppi_hash_name(name);
709 struct per_process_info *ppi;
711 ppi = ppi_hash_table[hash_idx];
713 struct process_pid_map *ppm = ppi->ppm;
715 if (!strcmp(ppm->comm, name))
718 ppi = ppi->hash_next;
724 static struct per_process_info *find_ppi_by_pid(pid_t pid)
726 const int hash_idx = ppi_hash_pid(pid);
727 struct per_process_info *ppi;
729 ppi = ppi_hash_table[hash_idx];
731 struct process_pid_map *ppm = ppi->ppm;
736 ppi = ppi->hash_next;
742 static struct per_process_info *find_ppi(pid_t pid)
744 struct per_process_info *ppi;
748 return find_ppi_by_pid(pid);
750 name = find_process_name(pid);
754 ppi = find_ppi_by_name(name);
755 if (ppi && ppi->ppm->pid != pid)
756 ppi->more_than_one = 1;
762 * struct trace and blktrace allocation cache, we do potentially
763 * millions of mallocs for these structures while only using at most
764 * a few thousand at the time
766 static inline void t_free(struct trace *t)
768 if (t_alloc_cache < 1024) {
769 t->next = t_alloc_list;
776 static inline struct trace *t_alloc(void)
778 struct trace *t = t_alloc_list;
781 t_alloc_list = t->next;
786 return malloc(sizeof(*t));
789 static inline void bit_free(struct blk_io_trace *bit)
791 if (bit_alloc_cache < 1024 && !bit->pdu_len) {
793 * abuse a 64-bit field for a next pointer for the free item
795 bit->time = (__u64) (unsigned long) bit_alloc_list;
796 bit_alloc_list = (struct blk_io_trace *) bit;
802 static inline struct blk_io_trace *bit_alloc(void)
804 struct blk_io_trace *bit = bit_alloc_list;
807 bit_alloc_list = (struct blk_io_trace *) (unsigned long) \
813 return malloc(sizeof(*bit));
816 static inline void __put_trace_last(struct per_dev_info *pdi, struct trace *t)
818 struct per_cpu_info *pci = get_cpu_info(pdi, t->bit->cpu);
820 rb_erase(&t->rb_node, &pci->rb_last);
821 pci->rb_last_entries--;
827 static void put_trace(struct per_dev_info *pdi, struct trace *t)
829 rb_erase(&t->rb_node, &rb_sort_root);
832 trace_rb_insert_last(pdi, t);
835 static inline int trace_rb_insert(struct trace *t, struct rb_root *root)
837 struct rb_node **p = &root->rb_node;
838 struct rb_node *parent = NULL;
844 __t = rb_entry(parent, struct trace, rb_node);
846 if (t->bit->time < __t->bit->time)
848 else if (t->bit->time > __t->bit->time)
850 else if (t->bit->device < __t->bit->device)
852 else if (t->bit->device > __t->bit->device)
854 else if (t->bit->sequence < __t->bit->sequence)
856 else /* >= sequence */
860 rb_link_node(&t->rb_node, parent, p);
861 rb_insert_color(&t->rb_node, root);
865 static inline int trace_rb_insert_sort(struct trace *t)
867 if (!trace_rb_insert(t, &rb_sort_root)) {
875 static int trace_rb_insert_last(struct per_dev_info *pdi, struct trace *t)
877 struct per_cpu_info *pci = get_cpu_info(pdi, t->bit->cpu);
879 if (trace_rb_insert(t, &pci->rb_last))
882 pci->rb_last_entries++;
884 if (pci->rb_last_entries > rb_batch * pdi->nfiles) {
885 struct rb_node *n = rb_first(&pci->rb_last);
887 t = rb_entry(n, struct trace, rb_node);
888 __put_trace_last(pdi, t);
894 static struct trace *trace_rb_find(dev_t device, unsigned long sequence,
895 struct rb_root *root, int order)
897 struct rb_node *n = root->rb_node;
898 struct rb_node *prev = NULL;
902 __t = rb_entry(n, struct trace, rb_node);
905 if (device < __t->bit->device)
907 else if (device > __t->bit->device)
909 else if (sequence < __t->bit->sequence)
911 else if (sequence > __t->bit->sequence)
918 * hack - the list may not be sequence ordered because some
919 * events don't have sequence and time matched. so we end up
920 * being a little off in the rb lookup here, because we don't
921 * know the time we are looking for. compensate by browsing
922 * a little ahead from the last entry to find the match
927 while (((n = rb_next(prev)) != NULL) && max--) {
928 __t = rb_entry(n, struct trace, rb_node);
930 if (__t->bit->device == device &&
931 __t->bit->sequence == sequence)
941 static inline struct trace *trace_rb_find_last(struct per_dev_info *pdi,
942 struct per_cpu_info *pci,
945 return trace_rb_find(pdi->dev, seq, &pci->rb_last, 0);
948 static inline int track_rb_insert(struct per_dev_info *pdi,struct io_track *iot)
950 struct rb_node **p = &pdi->rb_track.rb_node;
951 struct rb_node *parent = NULL;
952 struct io_track *__iot;
956 __iot = rb_entry(parent, struct io_track, rb_node);
958 if (iot->sector < __iot->sector)
960 else if (iot->sector > __iot->sector)
964 "sector alias (%Lu) on device %d,%d!\n",
965 (unsigned long long) iot->sector,
966 MAJOR(pdi->dev), MINOR(pdi->dev));
971 rb_link_node(&iot->rb_node, parent, p);
972 rb_insert_color(&iot->rb_node, &pdi->rb_track);
976 static struct io_track *__find_track(struct per_dev_info *pdi, __u64 sector)
978 struct rb_node *n = pdi->rb_track.rb_node;
979 struct io_track *__iot;
982 __iot = rb_entry(n, struct io_track, rb_node);
984 if (sector < __iot->sector)
986 else if (sector > __iot->sector)
995 static struct io_track *find_track(struct per_dev_info *pdi, pid_t pid,
998 struct io_track *iot;
1000 iot = __find_track(pdi, sector);
1002 iot = malloc(sizeof(*iot));
1003 iot->ppm = find_ppm(pid);
1005 iot->ppm = add_ppm_hash(pid, "unknown");
1006 iot->sector = sector;
1007 iot->allocation_time = -1ULL;
1008 iot->queue_time = -1ULL;
1009 iot->dispatch_time = -1ULL;
1010 iot->completion_time = -1ULL;
1011 track_rb_insert(pdi, iot);
1017 static void log_track_frontmerge(struct per_dev_info *pdi,
1018 struct blk_io_trace *t)
1020 struct io_track *iot;
1025 iot = __find_track(pdi, t->sector + t_sec(t));
1028 fprintf(stderr, "merge not found for (%d,%d): %llu\n",
1029 MAJOR(t->device), MINOR(t->device),
1030 (unsigned long long) t->sector + t_sec(t));
1034 rb_erase(&iot->rb_node, &pdi->rb_track);
1035 iot->sector -= t_sec(t);
1036 track_rb_insert(pdi, iot);
1039 static void log_track_getrq(struct per_dev_info *pdi, struct blk_io_trace *t)
1041 struct io_track *iot;
1046 iot = find_track(pdi, t->pid, t->sector);
1047 io_warn_unless(t, iot->allocation_time == -1ULL,
1048 "confused about %s time", "allocation");
1049 iot->allocation_time = t->time;
1053 * for md/dm setups, the interesting cycle is Q -> C. So track queueing
1054 * time here, as dispatch time
1056 static void log_track_queue(struct per_dev_info *pdi, struct blk_io_trace *t)
1058 struct io_track *iot;
1063 iot = find_track(pdi, t->pid, t->sector);
1064 io_warn_unless(t, iot->dispatch_time == -1ULL,
1065 "confused about %s time", "dispatch");
1066 iot->dispatch_time = t->time;
1070 * return time between rq allocation and insertion
1072 static unsigned long long log_track_insert(struct per_dev_info *pdi,
1073 struct blk_io_trace *t)
1075 unsigned long long elapsed;
1076 struct io_track *iot;
1081 iot = find_track(pdi, t->pid, t->sector);
1082 io_warn_unless(t, iot->queue_time == -1ULL,
1083 "confused about %s time", "queue");
1084 iot->queue_time = t->time;
1086 if (iot->allocation_time == -1ULL)
1089 elapsed = iot->queue_time - iot->allocation_time;
1091 if (per_process_stats) {
1092 struct per_process_info *ppi = find_ppi(iot->ppm->pid);
1093 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
1095 if (ppi && elapsed > ppi->longest_allocation_wait[w])
1096 ppi->longest_allocation_wait[w] = elapsed;
1103 * return time between queue and issue
1105 static unsigned long long log_track_issue(struct per_dev_info *pdi,
1106 struct blk_io_trace *t)
1108 unsigned long long elapsed = -1ULL;
1109 struct io_track *iot;
1113 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
1116 iot = __find_track(pdi, t->sector);
1119 fprintf(stderr, "issue not found for (%d,%d): %llu\n",
1120 MAJOR(t->device), MINOR(t->device),
1121 (unsigned long long) t->sector);
1125 io_warn_unless(t, iot->dispatch_time == -1ULL,
1126 "confused about %s time", "dispatch");
1127 iot->dispatch_time = t->time;
1128 if (iot->queue_time != -1ULL)
1129 elapsed = iot->dispatch_time - iot->queue_time;
1131 if (elapsed != -1ULL && per_process_stats) {
1132 struct per_process_info *ppi = find_ppi(iot->ppm->pid);
1133 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
1135 if (ppi && elapsed > ppi->longest_dispatch_wait[w])
1136 ppi->longest_dispatch_wait[w] = elapsed;
1143 * return time between dispatch and complete
1145 static unsigned long long log_track_complete(struct per_dev_info *pdi,
1146 struct blk_io_trace *t)
1148 unsigned long long elapsed = -1ULL;
1149 struct io_track *iot;
1154 iot = __find_track(pdi, t->sector);
1157 fprintf(stderr,"complete not found for (%d,%d): %llu\n",
1158 MAJOR(t->device), MINOR(t->device),
1159 (unsigned long long) t->sector);
1163 io_warn_unless(t, iot->completion_time == -1ULL,
1164 "confused about %s time", "completion");
1165 iot->completion_time = t->time;
1166 if (iot->dispatch_time != -1ULL)
1167 elapsed = iot->completion_time - iot->dispatch_time;
1169 if (elapsed != -1ULL && per_process_stats) {
1170 struct per_process_info *ppi = find_ppi(iot->ppm->pid);
1171 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
1173 if (ppi && elapsed > ppi->longest_completion_wait[w])
1174 ppi->longest_completion_wait[w] = elapsed;
1178 * kill the trace, we don't need it after completion
1180 rb_erase(&iot->rb_node, &pdi->rb_track);
1187 static struct io_stats *find_process_io_stats(pid_t pid)
1189 struct per_process_info *ppi = find_ppi(pid);
1192 ppi = malloc(sizeof(*ppi));
1193 memset(ppi, 0, sizeof(*ppi));
1194 ppi->ppm = find_ppm(pid);
1196 ppi->ppm = add_ppm_hash(pid, "unknown");
1197 add_ppi_to_hash(ppi);
1198 add_ppi_to_list(ppi);
1201 return &ppi->io_stats;
1204 static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size)
1207 snprintf(buffer, size, "%s", pdi->name);
1209 snprintf(buffer, size, "%d,%d",MAJOR(pdi->dev),MINOR(pdi->dev));
1213 static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit)
1215 unsigned long long this = bit->time;
1216 unsigned long long last = pdi->last_reported_time;
1218 pdi->backwards = (this < last) ? 'B' : ' ';
1219 pdi->last_reported_time = this;
1222 static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t,
1227 ios->mwrite_kb += t_kb(t);
1228 ios->mwrite_b += t_b(t);
1231 ios->mread_kb += t_kb(t);
1232 ios->mread_b += t_b(t);
1236 static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci,
1239 __account_m(&pci->io_stats, t, rw);
1241 if (per_process_stats) {
1242 struct io_stats *ios = find_process_io_stats(t->pid);
1244 __account_m(ios, t, rw);
1248 static inline void __account_pc_queue(struct io_stats *ios,
1249 struct blk_io_trace *t, int rw)
1253 ios->qwrite_kb_pc += t_kb(t);
1254 ios->qwrite_b_pc += t_b(t);
1257 ios->qread_kb += t_kb(t);
1258 ios->qread_b_pc += t_b(t);
1262 static inline void account_pc_queue(struct blk_io_trace *t,
1263 struct per_cpu_info *pci, int rw)
1265 __account_pc_queue(&pci->io_stats, t, rw);
1267 if (per_process_stats) {
1268 struct io_stats *ios = find_process_io_stats(t->pid);
1270 __account_pc_queue(ios, t, rw);
1274 static inline void __account_pc_issue(struct io_stats *ios, int rw,
1279 ios->iwrite_kb_pc += bytes >> 10;
1280 ios->iwrite_b_pc += bytes & 1023;
1283 ios->iread_kb_pc += bytes >> 10;
1284 ios->iread_b_pc += bytes & 1023;
1288 static inline void account_pc_issue(struct blk_io_trace *t,
1289 struct per_cpu_info *pci, int rw)
1291 __account_pc_issue(&pci->io_stats, rw, t->bytes);
1293 if (per_process_stats) {
1294 struct io_stats *ios = find_process_io_stats(t->pid);
1296 __account_pc_issue(ios, rw, t->bytes);
1300 static inline void __account_pc_requeue(struct io_stats *ios,
1301 struct blk_io_trace *t, int rw)
1305 ios->iwrite_kb_pc -= t_kb(t);
1306 ios->iwrite_b_pc -= t_b(t);
1309 ios->iread_kb_pc -= t_kb(t);
1310 ios->iread_b_pc -= t_b(t);
1314 static inline void account_pc_requeue(struct blk_io_trace *t,
1315 struct per_cpu_info *pci, int rw)
1317 __account_pc_requeue(&pci->io_stats, t, rw);
1319 if (per_process_stats) {
1320 struct io_stats *ios = find_process_io_stats(t->pid);
1322 __account_pc_requeue(ios, t, rw);
1326 static inline void __account_pc_c(struct io_stats *ios, int rw)
1334 static inline void account_pc_c(struct blk_io_trace *t,
1335 struct per_cpu_info *pci, int rw)
1337 __account_pc_c(&pci->io_stats, rw);
1339 if (per_process_stats) {
1340 struct io_stats *ios = find_process_io_stats(t->pid);
1342 __account_pc_c(ios, rw);
1346 static inline void __account_queue(struct io_stats *ios, struct blk_io_trace *t,
1351 ios->qwrite_kb += t_kb(t);
1352 ios->qwrite_b += t_b(t);
1355 ios->qread_kb += t_kb(t);
1356 ios->qread_b += t_b(t);
1360 static inline void account_queue(struct blk_io_trace *t,
1361 struct per_cpu_info *pci, int rw)
1363 __account_queue(&pci->io_stats, t, rw);
1365 if (per_process_stats) {
1366 struct io_stats *ios = find_process_io_stats(t->pid);
1368 __account_queue(ios, t, rw);
1372 static inline void __account_c(struct io_stats *ios, int rw, int bytes)
1376 ios->cwrite_kb += bytes >> 10;
1377 ios->cwrite_b += bytes & 1023;
1380 ios->cread_kb += bytes >> 10;
1381 ios->cread_b += bytes & 1023;
1385 static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci,
1388 __account_c(&pci->io_stats, rw, bytes);
1390 if (per_process_stats) {
1391 struct io_stats *ios = find_process_io_stats(t->pid);
1393 __account_c(ios, rw, bytes);
1397 static inline void __account_issue(struct io_stats *ios, int rw,
1402 ios->iwrite_kb += bytes >> 10;
1403 ios->iwrite_b += bytes & 1023;
1406 ios->iread_kb += bytes >> 10;
1407 ios->iread_b += bytes & 1023;
1411 static inline void account_issue(struct blk_io_trace *t,
1412 struct per_cpu_info *pci, int rw)
1414 __account_issue(&pci->io_stats, rw, t->bytes);
1416 if (per_process_stats) {
1417 struct io_stats *ios = find_process_io_stats(t->pid);
1419 __account_issue(ios, rw, t->bytes);
1423 static inline void __account_unplug(struct io_stats *ios, int timer)
1426 ios->timer_unplugs++;
1431 static inline void account_unplug(struct blk_io_trace *t,
1432 struct per_cpu_info *pci, int timer)
1434 __account_unplug(&pci->io_stats, timer);
1436 if (per_process_stats) {
1437 struct io_stats *ios = find_process_io_stats(t->pid);
1439 __account_unplug(ios, timer);
1443 static inline void __account_requeue(struct io_stats *ios,
1444 struct blk_io_trace *t, int rw)
1448 ios->iwrite_kb -= t_kb(t);
1449 ios->iwrite_b -= t_b(t);
1452 ios->iread_kb -= t_kb(t);
1453 ios->iread_b -= t_b(t);
1457 static inline void account_requeue(struct blk_io_trace *t,
1458 struct per_cpu_info *pci, int rw)
1460 __account_requeue(&pci->io_stats, t, rw);
1462 if (per_process_stats) {
1463 struct io_stats *ios = find_process_io_stats(t->pid);
1465 __account_requeue(ios, t, rw);
1469 static void log_complete(struct per_dev_info *pdi, struct per_cpu_info *pci,
1470 struct blk_io_trace *t, char *act)
1472 process_fmt(act, pci, t, log_track_complete(pdi, t), 0, NULL);
1475 static void log_insert(struct per_dev_info *pdi, struct per_cpu_info *pci,
1476 struct blk_io_trace *t, char *act)
1478 process_fmt(act, pci, t, log_track_insert(pdi, t), 0, NULL);
1481 static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t,
1484 process_fmt(act, pci, t, -1, 0, NULL);
1487 static void log_issue(struct per_dev_info *pdi, struct per_cpu_info *pci,
1488 struct blk_io_trace *t, char *act)
1490 process_fmt(act, pci, t, log_track_issue(pdi, t), 0, NULL);
1493 static void log_merge(struct per_dev_info *pdi, struct per_cpu_info *pci,
1494 struct blk_io_trace *t, char *act)
1497 log_track_frontmerge(pdi, t);
1499 process_fmt(act, pci, t, -1ULL, 0, NULL);
1502 static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t,
1505 process_fmt(act, pci, t, -1ULL, 0, NULL);
1508 static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t,
1511 process_fmt(act, pci, t, -1ULL, 0, NULL);
1514 static void log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t,
1517 process_fmt(act, pci, t, -1ULL, 0, NULL);
1520 static void log_split(struct per_cpu_info *pci, struct blk_io_trace *t,
1523 process_fmt(act, pci, t, -1ULL, 0, NULL);
1526 static void log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act)
1528 unsigned char *buf = (unsigned char *) t + sizeof(*t);
1530 process_fmt(act, pci, t, -1ULL, t->pdu_len, buf);
1533 static void dump_trace_pc(struct blk_io_trace *t, struct per_dev_info *pdi,
1534 struct per_cpu_info *pci)
1536 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
1537 int act = t->action & 0xffff;
1540 case __BLK_TA_QUEUE:
1541 log_generic(pci, t, "Q");
1542 account_pc_queue(t, pci, w);
1544 case __BLK_TA_GETRQ:
1545 log_generic(pci, t, "G");
1547 case __BLK_TA_SLEEPRQ:
1548 log_generic(pci, t, "S");
1550 case __BLK_TA_REQUEUE:
1552 * can happen if we miss traces, don't let it go
1555 if (pdi->cur_depth[w])
1556 pdi->cur_depth[w]--;
1557 account_pc_requeue(t, pci, w);
1558 log_generic(pci, t, "R");
1560 case __BLK_TA_ISSUE:
1561 account_pc_issue(t, pci, w);
1562 pdi->cur_depth[w]++;
1563 if (pdi->cur_depth[w] > pdi->max_depth[w])
1564 pdi->max_depth[w] = pdi->cur_depth[w];
1565 log_pc(pci, t, "D");
1567 case __BLK_TA_COMPLETE:
1568 if (pdi->cur_depth[w])
1569 pdi->cur_depth[w]--;
1570 log_pc(pci, t, "C");
1571 account_pc_c(t, pci, w);
1573 case __BLK_TA_INSERT:
1574 log_pc(pci, t, "I");
1577 fprintf(stderr, "Bad pc action %x\n", act);
1582 static void dump_trace_fs(struct blk_io_trace *t, struct per_dev_info *pdi,
1583 struct per_cpu_info *pci)
1585 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
1586 int act = t->action & 0xffff;
1589 case __BLK_TA_QUEUE:
1590 log_track_queue(pdi, t);
1591 account_queue(t, pci, w);
1592 log_queue(pci, t, "Q");
1594 case __BLK_TA_INSERT:
1595 log_insert(pdi, pci, t, "I");
1597 case __BLK_TA_BACKMERGE:
1598 account_m(t, pci, w);
1599 log_merge(pdi, pci, t, "M");
1601 case __BLK_TA_FRONTMERGE:
1602 account_m(t, pci, w);
1603 log_merge(pdi, pci, t, "F");
1605 case __BLK_TA_GETRQ:
1606 log_track_getrq(pdi, t);
1607 log_generic(pci, t, "G");
1609 case __BLK_TA_SLEEPRQ:
1610 log_generic(pci, t, "S");
1612 case __BLK_TA_REQUEUE:
1614 * can happen if we miss traces, don't let it go
1617 if (pdi->cur_depth[w])
1618 pdi->cur_depth[w]--;
1619 account_requeue(t, pci, w);
1620 log_queue(pci, t, "R");
1622 case __BLK_TA_ISSUE:
1623 account_issue(t, pci, w);
1624 pdi->cur_depth[w]++;
1625 if (pdi->cur_depth[w] > pdi->max_depth[w])
1626 pdi->max_depth[w] = pdi->cur_depth[w];
1627 log_issue(pdi, pci, t, "D");
1629 case __BLK_TA_COMPLETE:
1630 if (pdi->cur_depth[w])
1631 pdi->cur_depth[w]--;
1632 account_c(t, pci, w, t->bytes);
1633 log_complete(pdi, pci, t, "C");
1636 log_action(pci, t, "P");
1638 case __BLK_TA_UNPLUG_IO:
1639 account_unplug(t, pci, 0);
1640 log_unplug(pci, t, "U");
1642 case __BLK_TA_UNPLUG_TIMER:
1643 account_unplug(t, pci, 1);
1644 log_unplug(pci, t, "UT");
1646 case __BLK_TA_SPLIT:
1647 log_split(pci, t, "X");
1649 case __BLK_TA_BOUNCE:
1650 log_generic(pci, t, "B");
1652 case __BLK_TA_REMAP:
1653 log_generic(pci, t, "A");
1655 case __BLK_TA_DRV_DATA:
1657 /* dump to binary file only */
1660 fprintf(stderr, "Bad fs action %x\n", t->action);
1665 static void dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
1666 struct per_dev_info *pdi)
1669 if (t->action == BLK_TN_MESSAGE)
1671 else if (t->action & BLK_TC_ACT(BLK_TC_PC))
1672 dump_trace_pc(t, pdi, pci);
1674 dump_trace_fs(t, pdi, pci);
1678 pdi->first_reported_time = t->time;
1682 if (bin_output_msgs ||
1683 !(t->action & BLK_TC_ACT(BLK_TC_NOTIFY) &&
1684 t->action == BLK_TN_MESSAGE))
1685 output_binary(t, sizeof(*t) + t->pdu_len);
1689 * print in a proper way, not too small and not too big. if more than
1690 * 1000,000K, turn into M and so on
1692 static char *size_cnv(char *dst, unsigned long long num, int in_kb)
1694 char suff[] = { '\0', 'K', 'M', 'G', 'P' };
1700 while (num > 1000 * 1000ULL && (i < sizeof(suff) - 1)) {
1705 sprintf(dst, "%'8Lu%c", num, suff[i]);
1709 static void dump_io_stats(struct per_dev_info *pdi, struct io_stats *ios,
1712 static char x[256], y[256];
1714 fprintf(ofp, "%s\n", msg);
1716 fprintf(ofp, " Reads Queued: %s, %siB\t",
1717 size_cnv(x, ios->qreads, 0),
1718 size_cnv(y, ios->qread_kb + (ios->qread_b>>10), 1));
1719 fprintf(ofp, " Writes Queued: %s, %siB\n",
1720 size_cnv(x, ios->qwrites, 0),
1721 size_cnv(y, ios->qwrite_kb + (ios->qwrite_b>>10), 1));
1722 fprintf(ofp, " Read Dispatches: %s, %siB\t",
1723 size_cnv(x, ios->ireads, 0),
1724 size_cnv(y, ios->iread_kb + (ios->iread_b>>10), 1));
1725 fprintf(ofp, " Write Dispatches: %s, %siB\n",
1726 size_cnv(x, ios->iwrites, 0),
1727 size_cnv(y, ios->iwrite_kb + (ios->iwrite_b>>10), 1));
1728 fprintf(ofp, " Reads Requeued: %s\t\t", size_cnv(x, ios->rrqueue, 0));
1729 fprintf(ofp, " Writes Requeued: %s\n", size_cnv(x, ios->wrqueue, 0));
1730 fprintf(ofp, " Reads Completed: %s, %siB\t",
1731 size_cnv(x, ios->creads, 0),
1732 size_cnv(y, ios->cread_kb + (ios->cread_b>>10), 1));
1733 fprintf(ofp, " Writes Completed: %s, %siB\n",
1734 size_cnv(x, ios->cwrites, 0),
1735 size_cnv(y, ios->cwrite_kb + (ios->cwrite_b>>10), 1));
1736 fprintf(ofp, " Read Merges: %s, %siB\t",
1737 size_cnv(x, ios->mreads, 0),
1738 size_cnv(y, ios->mread_kb + (ios->mread_b>>10), 1));
1739 fprintf(ofp, " Write Merges: %s, %siB\n",
1740 size_cnv(x, ios->mwrites, 0),
1741 size_cnv(y, ios->mwrite_kb + (ios->mwrite_b>>10), 1));
1743 fprintf(ofp, " Read depth: %'8u%8c\t", pdi->max_depth[0], ' ');
1744 fprintf(ofp, " Write depth: %'8u\n", pdi->max_depth[1]);
1746 if (ios->qreads_pc || ios->qwrites_pc || ios->ireads_pc || ios->iwrites_pc ||
1747 ios->rrqueue_pc || ios->wrqueue_pc || ios->creads_pc || ios->cwrites_pc) {
1748 fprintf(ofp, " PC Reads Queued: %s, %siB\t",
1749 size_cnv(x, ios->qreads_pc, 0),
1751 ios->qread_kb_pc + (ios->qread_b_pc>>10), 1));
1752 fprintf(ofp, " PC Writes Queued: %s, %siB\n",
1753 size_cnv(x, ios->qwrites_pc, 0),
1755 ios->qwrite_kb_pc + (ios->qwrite_b_pc>>10), 1));
1756 fprintf(ofp, " PC Read Disp.: %s, %siB\t",
1757 size_cnv(x, ios->ireads_pc, 0),
1759 ios->iread_kb_pc + (ios->iread_b_pc>>10), 1));
1760 fprintf(ofp, " PC Write Disp.: %s, %siB\n",
1761 size_cnv(x, ios->iwrites_pc, 0),
1763 ios->iwrite_kb_pc + (ios->iwrite_b_pc>>10),
1765 fprintf(ofp, " PC Reads Req.: %s\t\t", size_cnv(x, ios->rrqueue_pc, 0));
1766 fprintf(ofp, " PC Writes Req.: %s\n", size_cnv(x, ios->wrqueue_pc, 0));
1767 fprintf(ofp, " PC Reads Compl.: %s\t\t", size_cnv(x, ios->creads_pc, 0));
1768 fprintf(ofp, " PC Writes Compl.: %s\n", size_cnv(x, ios->cwrites_pc, 0));
1770 fprintf(ofp, " IO unplugs: %'8lu%8c\t", ios->io_unplugs, ' ');
1771 fprintf(ofp, " Timer unplugs: %'8lu\n", ios->timer_unplugs);
1774 static void dump_wait_stats(struct per_process_info *ppi)
1776 unsigned long rawait = ppi->longest_allocation_wait[0] / 1000;
1777 unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000;
1778 unsigned long rcwait = ppi->longest_completion_wait[0] / 1000;
1779 unsigned long wawait = ppi->longest_allocation_wait[1] / 1000;
1780 unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000;
1781 unsigned long wcwait = ppi->longest_completion_wait[1] / 1000;
1783 fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' ');
1784 fprintf(ofp, " Allocation wait: %'8lu\n", wawait);
1785 fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' ');
1786 fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait);
1787 fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' ');
1788 fprintf(ofp, " Completion wait: %'8lu\n", wcwait);
1791 static int ppi_name_compare(const void *p1, const void *p2)
1793 struct per_process_info *ppi1 = *((struct per_process_info **) p1);
1794 struct per_process_info *ppi2 = *((struct per_process_info **) p2);
1797 res = strverscmp(ppi1->ppm->comm, ppi2->ppm->comm);
1799 res = ppi1->ppm->pid > ppi2->ppm->pid;
1804 static int ppi_event_compare(const void *p1, const void *p2)
1806 struct per_process_info *ppi1 = *((struct per_process_info **) p1);
1807 struct per_process_info *ppi2 = *((struct per_process_info **) p2);
1808 struct io_stats *ios1 = &ppi1->io_stats;
1809 struct io_stats *ios2 = &ppi2->io_stats;
1810 unsigned long io1, io2;
1811 unsigned long long kb1,kb2;
1817 switch (per_process_stats_event) {
1818 case SORT_PROG_EVENT_QKB: /* KB: Queued read and write */
1819 kb1 = ios1->qwrite_kb + (ios1->qwrite_b>>10) +
1820 ios1->qread_kb + (ios1->qread_b>>10);
1821 kb2 = ios2->qwrite_kb + (ios2->qwrite_b>>10) +
1822 ios2->qread_kb + (ios2->qread_b>>10);
1824 case SORT_PROG_EVENT_RKB: /* KB: Queued Read */
1825 kb1 = ios1->qread_kb + (ios1->qread_b>>10);
1826 kb2 = ios2->qread_kb + (ios2->qread_b>>10);
1828 case SORT_PROG_EVENT_WKB: /* KB: Queued Write */
1829 kb1 = ios1->qwrite_kb + (ios1->qwrite_b>>10);
1830 kb2 = ios2->qwrite_kb + (ios2->qwrite_b>>10);
1832 case SORT_PROG_EVENT_CKB: /* KB: Complete */
1833 kb1 = ios1->cwrite_kb + (ios1->cwrite_b>>10) +
1834 ios1->cread_kb + (ios1->cread_b>>10);
1835 kb2 = ios2->cwrite_kb + (ios2->cwrite_b>>10) +
1836 ios2->cread_kb + (ios2->cread_b>>10);
1838 case SORT_PROG_EVENT_QIO: /* IO: Queued read and write */
1840 io1 = ios1->qreads + ios1->qwrites;
1841 io2 = ios2->qreads + ios2->qwrites;
1843 case SORT_PROG_EVENT_RIO: /* IO: Queued Read */
1848 case SORT_PROG_EVENT_WIO: /* IO: Queued Write */
1850 io1 = ios1->qwrites;
1851 io2 = ios2->qwrites;
1853 case SORT_PROG_EVENT_CIO: /* IO: Complete */
1855 io1 = ios1->creads + ios1->cwrites;
1856 io2 = ios2->creads + ios2->cwrites;
1865 else if (kb1 == kb2)
1873 else if (io1 == io2)
1878 static int ppi_compare(const void *p1, const void *p2)
1880 if (per_process_stats_event == SORT_PROG_EVENT_N)
1881 return ppi_name_compare(p1, p2);
1883 return ppi_event_compare(p1, p2);
1886 static void sort_process_list(void)
1888 struct per_process_info **ppis;
1889 struct per_process_info *ppi;
1892 ppis = malloc(ppi_list_entries * sizeof(struct per_process_info *));
1897 ppi = ppi->list_next;
1900 qsort(ppis, ppi_list_entries, sizeof(ppi), ppi_compare);
1902 i = ppi_list_entries - 1;
1907 ppi->list_next = ppi_list;
1915 static void show_process_stats(void)
1917 struct per_process_info *ppi;
1919 sort_process_list();
1923 struct process_pid_map *ppm = ppi->ppm;
1926 if (ppi->more_than_one)
1927 sprintf(name, "%s (%u, ...)", ppm->comm, ppm->pid);
1929 sprintf(name, "%s (%u)", ppm->comm, ppm->pid);
1931 dump_io_stats(NULL, &ppi->io_stats, name);
1932 dump_wait_stats(ppi);
1933 ppi = ppi->list_next;
1939 static void show_device_and_cpu_stats(void)
1941 struct per_dev_info *pdi;
1942 struct per_cpu_info *pci;
1943 struct io_stats total, *ios;
1944 unsigned long long rrate, wrate, msec;
1945 int i, j, pci_events;
1946 char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3];
1950 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
1952 memset(&total, 0, sizeof(total));
1958 for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) {
1962 ios = &pci->io_stats;
1963 total.qreads += ios->qreads;
1964 total.qwrites += ios->qwrites;
1965 total.creads += ios->creads;
1966 total.cwrites += ios->cwrites;
1967 total.mreads += ios->mreads;
1968 total.mwrites += ios->mwrites;
1969 total.ireads += ios->ireads;
1970 total.iwrites += ios->iwrites;
1971 total.rrqueue += ios->rrqueue;
1972 total.wrqueue += ios->wrqueue;
1973 total.qread_kb += ios->qread_kb;
1974 total.qwrite_kb += ios->qwrite_kb;
1975 total.cread_kb += ios->cread_kb;
1976 total.cwrite_kb += ios->cwrite_kb;
1977 total.iread_kb += ios->iread_kb;
1978 total.iwrite_kb += ios->iwrite_kb;
1979 total.mread_kb += ios->mread_kb;
1980 total.mwrite_kb += ios->mwrite_kb;
1981 total.qread_b += ios->qread_b;
1982 total.qwrite_b += ios->qwrite_b;
1983 total.cread_b += ios->cread_b;
1984 total.cwrite_b += ios->cwrite_b;
1985 total.iread_b += ios->iread_b;
1986 total.iwrite_b += ios->iwrite_b;
1987 total.mread_b += ios->mread_b;
1988 total.mwrite_b += ios->mwrite_b;
1990 total.qreads_pc += ios->qreads_pc;
1991 total.qwrites_pc += ios->qwrites_pc;
1992 total.creads_pc += ios->creads_pc;
1993 total.cwrites_pc += ios->cwrites_pc;
1994 total.ireads_pc += ios->ireads_pc;
1995 total.iwrites_pc += ios->iwrites_pc;
1996 total.rrqueue_pc += ios->rrqueue_pc;
1997 total.wrqueue_pc += ios->wrqueue_pc;
1998 total.qread_kb_pc += ios->qread_kb_pc;
1999 total.qwrite_kb_pc += ios->qwrite_kb_pc;
2000 total.iread_kb_pc += ios->iread_kb_pc;
2001 total.iwrite_kb_pc += ios->iwrite_kb_pc;
2002 total.qread_b_pc += ios->qread_b_pc;
2003 total.qwrite_b_pc += ios->qwrite_b_pc;
2004 total.iread_b_pc += ios->iread_b_pc;
2005 total.iwrite_b_pc += ios->iwrite_b_pc;
2007 total.timer_unplugs += ios->timer_unplugs;
2008 total.io_unplugs += ios->io_unplugs;
2010 snprintf(line, sizeof(line) - 1, "CPU%d (%s):",
2011 j, get_dev_name(pdi, name, sizeof(name)));
2012 dump_io_stats(pdi, ios, line);
2016 if (pci_events > 1) {
2018 snprintf(line, sizeof(line) - 1, "Total (%s):",
2019 get_dev_name(pdi, name, sizeof(name)));
2020 dump_io_stats(NULL, &total, line);
2024 msec = (pdi->last_reported_time - pdi->first_reported_time) / 1000000;
2026 rrate = ((1000 * total.cread_kb) + total.cread_b) /
2028 wrate = ((1000 * total.cwrite_kb) + total.cwrite_b) /
2032 fprintf(ofp, "\nThroughput (R/W): %'LuKiB/s / %'LuKiB/s\n",
2034 fprintf(ofp, "Events (%s): %'Lu entries\n",
2035 get_dev_name(pdi, line, sizeof(line)), pdi->events);
2037 collect_pdi_skips(pdi);
2038 if (!pdi->skips && !pdi->events)
2041 ratio = 100.0 * ((double)pdi->seq_skips /
2042 (double)(pdi->events + pdi->seq_skips));
2043 fprintf(ofp, "Skips: %'lu forward (%'llu - %5.1lf%%)\n",
2044 pdi->skips, pdi->seq_skips, ratio);
2048 static void correct_abs_start_time(void)
2050 long delta = genesis_time - start_timestamp;
2052 abs_start_time.tv_sec += SECONDS(delta);
2053 abs_start_time.tv_nsec += NANO_SECONDS(delta);
2054 if (abs_start_time.tv_nsec < 0) {
2055 abs_start_time.tv_nsec += 1000000000;
2056 abs_start_time.tv_sec -= 1;
2058 if (abs_start_time.tv_nsec > 1000000000) {
2059 abs_start_time.tv_nsec -= 1000000000;
2060 abs_start_time.tv_sec += 1;
2064 static void find_genesis(void)
2066 struct trace *t = trace_list;
2068 genesis_time = -1ULL;
2070 if (t->bit->time < genesis_time)
2071 genesis_time = t->bit->time;
2076 /* The time stamp record will usually be the first
2077 * record in the trace, but not always.
2080 && start_timestamp != genesis_time) {
2081 correct_abs_start_time();
2085 static inline int check_stopwatch(struct blk_io_trace *bit)
2087 if (bit->time < stopwatch_end &&
2088 bit->time >= stopwatch_start)
2095 * return youngest entry read
2097 static int sort_entries(unsigned long long *youngest)
2099 struct per_dev_info *pdi = NULL;
2100 struct per_cpu_info *pci = NULL;
2107 while ((t = trace_list) != NULL) {
2108 struct blk_io_trace *bit = t->bit;
2110 trace_list = t->next;
2112 bit->time -= genesis_time;
2114 if (bit->time < *youngest || !*youngest)
2115 *youngest = bit->time;
2117 if (!pdi || pdi->dev != bit->device) {
2118 pdi = get_dev_info(bit->device);
2122 if (!pci || pci->cpu != bit->cpu)
2123 pci = get_cpu_info(pdi, bit->cpu);
2125 if (bit->sequence < pci->smallest_seq_read)
2126 pci->smallest_seq_read = bit->sequence;
2128 if (check_stopwatch(bit)) {
2134 if (trace_rb_insert_sort(t))
2142 * to continue, we must have traces from all online cpus in the tree
2144 static int check_cpu_map(struct per_dev_info *pdi)
2146 unsigned long *cpu_map;
2153 * create a map of the cpus we have traces for
2155 cpu_map = malloc(pdi->cpu_map_max / sizeof(long));
2156 memset(cpu_map, 0, sizeof(*cpu_map));
2157 n = rb_first(&rb_sort_root);
2159 __t = rb_entry(n, struct trace, rb_node);
2160 cpu = __t->bit->cpu;
2162 cpu_map[CPU_IDX(cpu)] |= (1UL << CPU_BIT(cpu));
2167 * we can't continue if pdi->cpu_map has entries set that we don't
2168 * have in the sort rbtree. the opposite is not a problem, though
2171 for (i = 0; i < pdi->cpu_map_max / CPUS_PER_LONG; i++) {
2172 if (pdi->cpu_map[i] & ~(cpu_map[i])) {
2182 static int check_sequence(struct per_dev_info *pdi, struct trace *t, int force)
2184 struct blk_io_trace *bit = t->bit;
2185 unsigned long expected_sequence;
2186 struct per_cpu_info *pci;
2189 pci = get_cpu_info(pdi, bit->cpu);
2190 expected_sequence = pci->last_sequence + 1;
2192 if (!expected_sequence) {
2194 * 1 should be the first entry, just allow it
2196 if (bit->sequence == 1)
2198 if (bit->sequence == pci->smallest_seq_read)
2201 return check_cpu_map(pdi);
2204 if (bit->sequence == expected_sequence)
2208 * we may not have seen that sequence yet. if we are not doing
2209 * the final run, break and wait for more entries.
2211 if (expected_sequence < pci->smallest_seq_read) {
2212 __t = trace_rb_find_last(pdi, pci, expected_sequence);
2216 __put_trace_last(pdi, __t);
2218 } else if (!force) {
2222 if (check_current_skips(pci, bit->sequence))
2225 if (expected_sequence < bit->sequence)
2226 insert_skip(pci, expected_sequence, bit->sequence - 1);
2231 static void show_entries_rb(int force)
2233 struct per_dev_info *pdi = NULL;
2234 struct per_cpu_info *pci = NULL;
2235 struct blk_io_trace *bit;
2239 while ((n = rb_first(&rb_sort_root)) != NULL) {
2240 if (is_done() && !force && !pipeline)
2243 t = rb_entry(n, struct trace, rb_node);
2246 if (read_sequence - t->read_sequence < 1 && !force)
2249 if (!pdi || pdi->dev != bit->device) {
2250 pdi = get_dev_info(bit->device);
2255 fprintf(stderr, "Unknown device ID? (%d,%d)\n",
2256 MAJOR(bit->device), MINOR(bit->device));
2260 if (!(bit->action == BLK_TN_MESSAGE) &&
2261 check_sequence(pdi, t, force))
2264 if (!force && bit->time > last_allowed_time)
2267 check_time(pdi, bit);
2269 if (!pci || pci->cpu != bit->cpu)
2270 pci = get_cpu_info(pdi, bit->cpu);
2272 if (!(bit->action == BLK_TN_MESSAGE))
2273 pci->last_sequence = bit->sequence;
2277 if (bit->action & (act_mask << BLK_TC_SHIFT))
2278 dump_trace(bit, pci, pdi);
2284 static int read_data(int fd, void *buffer, int bytes, int block, int *fdblock)
2286 int ret, bytes_left, fl;
2289 if (block != *fdblock) {
2290 fl = fcntl(fd, F_GETFL);
2294 fcntl(fd, F_SETFL, fl | O_NONBLOCK);
2297 fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
2303 while (bytes_left > 0) {
2304 ret = read(fd, p, bytes_left);
2308 if (errno != EAGAIN) {
2314 * never do partial reads. we can return if we
2315 * didn't read anything and we should not block,
2316 * otherwise wait for data
2318 if ((bytes_left == bytes) && !block)
2332 static inline __u16 get_pdulen(struct blk_io_trace *bit)
2335 return bit->pdu_len;
2337 return __bswap_16(bit->pdu_len);
2340 static inline __u32 get_magic(struct blk_io_trace *bit)
2345 return __bswap_32(bit->magic);
2348 static int read_events(int fd, int always_block, int *fdblock)
2350 struct per_dev_info *pdi = NULL;
2351 unsigned int events = 0;
2353 while (!is_done() && events < rb_batch) {
2354 struct blk_io_trace *bit;
2356 int pdu_len, should_block, ret;
2361 should_block = !events || always_block;
2363 ret = read_data(fd, bit, sizeof(*bit), should_block, fdblock);
2366 if (!events && ret < 0)
2372 * look at first trace to check whether we need to convert
2373 * data in the future
2375 if (data_is_native == -1 && check_data_endianness(bit->magic))
2378 magic = get_magic(bit);
2379 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
2380 fprintf(stderr, "Bad magic %x\n", magic);
2384 pdu_len = get_pdulen(bit);
2386 void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
2388 if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1, fdblock)) {
2398 if (verify_trace(bit)) {
2404 * not a real trace, so grab and handle it here
2406 if (bit->action & BLK_TC_ACT(BLK_TC_NOTIFY) && bit->action != BLK_TN_MESSAGE) {
2408 output_binary(bit, sizeof(*bit) + bit->pdu_len);
2413 memset(t, 0, sizeof(*t));
2415 t->read_sequence = read_sequence;
2417 t->next = trace_list;
2420 if (!pdi || pdi->dev != bit->device)
2421 pdi = get_dev_info(bit->device);
2423 if (bit->time > pdi->last_read_time)
2424 pdi->last_read_time = bit->time;
2433 * Managing input streams
2437 struct ms_stream *next;
2438 struct trace *first, *last;
2439 struct per_dev_info *pdi;
2443 #define MS_HASH(d, c) ((MAJOR(d) & 0xff) ^ (MINOR(d) & 0xff) ^ (cpu & 0xff))
2445 struct ms_stream *ms_head;
2446 struct ms_stream *ms_hash[256];
2448 static void ms_sort(struct ms_stream *msp);
2449 static int ms_prime(struct ms_stream *msp);
2451 static inline struct trace *ms_peek(struct ms_stream *msp)
2453 return (msp == NULL) ? NULL : msp->first;
2456 static inline __u64 ms_peek_time(struct ms_stream *msp)
2458 return ms_peek(msp)->bit->time;
2461 static inline void ms_resort(struct ms_stream *msp)
2463 if (msp->next && ms_peek_time(msp) > ms_peek_time(msp->next)) {
2464 ms_head = msp->next;
2470 static inline void ms_deq(struct ms_stream *msp)
2472 msp->first = msp->first->next;
2475 if (!ms_prime(msp)) {
2476 ms_head = msp->next;
2485 static void ms_sort(struct ms_stream *msp)
2487 __u64 msp_t = ms_peek_time(msp);
2488 struct ms_stream *this_msp = ms_head;
2490 if (this_msp == NULL)
2492 else if (msp_t < ms_peek_time(this_msp)) {
2493 msp->next = this_msp;
2497 while (this_msp->next && ms_peek_time(this_msp->next) < msp_t)
2498 this_msp = this_msp->next;
2500 msp->next = this_msp->next;
2501 this_msp->next = msp;
2505 static int ms_prime(struct ms_stream *msp)
2510 struct per_dev_info *pdi = msp->pdi;
2511 struct per_cpu_info *pci = get_cpu_info(pdi, msp->cpu);
2512 struct blk_io_trace *bit = NULL;
2513 int ret, pdu_len, ndone = 0;
2515 for (i = 0; !is_done() && pci->fd >= 0 && i < rb_batch; i++) {
2517 ret = read_data(pci->fd, bit, sizeof(*bit), 1, &pci->fdblock);
2521 if (data_is_native == -1 && check_data_endianness(bit->magic))
2524 magic = get_magic(bit);
2525 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
2526 fprintf(stderr, "Bad magic %x\n", magic);
2531 pdu_len = get_pdulen(bit);
2533 void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
2534 ret = read_data(pci->fd, ptr + sizeof(*bit), pdu_len,
2546 if (verify_trace(bit))
2549 if (bit->cpu != pci->cpu) {
2550 fprintf(stderr, "cpu %d trace info has error cpu %d\n",
2551 pci->cpu, bit->cpu);
2555 if (bit->action & BLK_TC_ACT(BLK_TC_NOTIFY) && bit->action != BLK_TN_MESSAGE) {
2557 output_binary(bit, sizeof(*bit) + bit->pdu_len);
2564 if (bit->time > pdi->last_read_time)
2565 pdi->last_read_time = bit->time;
2568 memset(t, 0, sizeof(*t));
2571 if (msp->first == NULL)
2572 msp->first = msp->last = t;
2574 msp->last->next = t;
2584 if (bit) bit_free(bit);
2586 cpu_mark_offline(pdi, pci->cpu);
2593 static struct ms_stream *ms_alloc(struct per_dev_info *pdi, int cpu)
2595 struct ms_stream *msp = malloc(sizeof(*msp));
2598 msp->first = msp->last = NULL;
2608 static int setup_file(struct per_dev_info *pdi, int cpu)
2613 struct per_cpu_info *pci = get_cpu_info(pdi, cpu);
2618 p = strdup(pdi->name);
2620 if (strcmp(dname, ".")) {
2622 p = strdup(pdi->name);
2623 strcpy(pdi->name, basename(p));
2628 len = sprintf(pci->fname, "%s/", input_dir);
2630 snprintf(pci->fname + len, sizeof(pci->fname)-1-len,
2631 "%s.blktrace.%d", pdi->name, pci->cpu);
2632 if (stat(pci->fname, &st) < 0)
2637 pci->fd = open(pci->fname, O_RDONLY);
2643 printf("Input file %s added\n", pci->fname);
2644 cpu_mark_online(pdi, pci->cpu);
2647 ms_alloc(pdi, pci->cpu);
2652 static int handle(struct ms_stream *msp)
2655 struct per_dev_info *pdi;
2656 struct per_cpu_info *pci;
2657 struct blk_io_trace *bit;
2663 pci = get_cpu_info(pdi, msp->cpu);
2665 bit->time -= genesis_time;
2667 if (t->bit->time > stopwatch_end)
2670 pdi->last_reported_time = bit->time;
2671 if ((bit->action & (act_mask << BLK_TC_SHIFT))&&
2672 t->bit->time >= stopwatch_start)
2673 dump_trace(bit, pci, pdi);
2678 trace_rb_insert_last(pdi, t);
2688 * Check if we need to sanitize the name. We allow 'foo', or if foo.blktrace.X
2689 * is given, then strip back down to 'foo' to avoid missing files.
2691 static int name_fixup(char *name)
2698 b = strstr(name, ".blktrace.");
2705 static int do_file(void)
2708 struct per_dev_info *pdi;
2711 * first prepare all files for reading
2713 for (i = 0; i < ndevices; i++) {
2715 ret = name_fixup(pdi->name);
2719 for (cpu = 0; setup_file(pdi, cpu); cpu++)
2723 fprintf(stderr,"No input files found for %s\n",
2730 * Get the initial time stamp
2733 genesis_time = ms_peek_time(ms_head);
2736 * Correct abs_start_time if necessary
2739 && start_timestamp != genesis_time) {
2740 correct_abs_start_time();
2744 * Keep processing traces while any are left
2746 while (!is_done() && ms_head && handle(ms_head))
2752 static void do_pipe(int fd)
2754 unsigned long long youngest;
2755 int events, fdblock;
2757 last_allowed_time = -1ULL;
2759 while ((events = read_events(fd, 0, &fdblock)) > 0) {
2763 smallest_seq_read = -1U;
2766 if (sort_entries(&youngest))
2769 if (youngest > stopwatch_end)
2775 if (rb_sort_entries)
2779 static int do_fifo(void)
2783 if (!strcmp(pipename, "-"))
2784 fd = dup(STDIN_FILENO);
2786 fd = open(pipename, O_RDONLY);
2789 perror("dup stdin");
2798 static void show_stats(void)
2807 if (per_process_stats)
2808 show_process_stats();
2810 if (per_device_and_cpu_stats)
2811 show_device_and_cpu_stats();
2816 static void handle_sigint(__attribute__((__unused__)) int sig)
2822 * Extract start and duration times from a string, allowing
2823 * us to specify a time interval of interest within a trace.
2824 * Format: "duration" (start is zero) or "start:duration".
2826 static int find_stopwatch_interval(char *string)
2831 value = strtod(string, &sp);
2833 fprintf(stderr,"Invalid stopwatch timer: %s\n", string);
2837 stopwatch_start = DOUBLE_TO_NANO_ULL(value);
2839 value = strtod(string, &sp);
2840 if (sp == string || *sp != '\0') {
2841 fprintf(stderr,"Invalid stopwatch duration time: %s\n",
2845 } else if (*sp != '\0') {
2846 fprintf(stderr,"Invalid stopwatch start timer: %s\n", string);
2849 stopwatch_end = DOUBLE_TO_NANO_ULL(value);
2850 if (stopwatch_end <= stopwatch_start) {
2851 fprintf(stderr, "Invalid stopwatch interval: %Lu -> %Lu\n",
2852 stopwatch_start, stopwatch_end);
2859 static int is_pipe(const char *str)
2863 if (!strcmp(str, "-"))
2865 if (!stat(str, &st) && S_ISFIFO(st.st_mode))
2871 static int get_program_sort_event(const char *str)
2877 per_process_stats_event = SORT_PROG_EVENT_N;
2880 per_process_stats_event = SORT_PROG_EVENT_QKB;
2883 per_process_stats_event = SORT_PROG_EVENT_QIO;
2886 per_process_stats_event = SORT_PROG_EVENT_RKB;
2889 per_process_stats_event = SORT_PROG_EVENT_RIO;
2892 per_process_stats_event = SORT_PROG_EVENT_WKB;
2895 per_process_stats_event = SORT_PROG_EVENT_WIO;
2898 per_process_stats_event = SORT_PROG_EVENT_CKB;
2901 per_process_stats_event = SORT_PROG_EVENT_CIO;
2910 #define S_OPTS "a:A:b:D:d:f:F:hi:o:OqsS:tw:vVM"
2911 static char usage_str[] = "\n\n" \
2912 "-i <file> | --input=<file>\n" \
2913 "[ -a <action field> | --act-mask=<action field> ]\n" \
2914 "[ -A <action mask> | --set-mask=<action mask> ]\n" \
2915 "[ -b <traces> | --batch=<traces> ]\n" \
2916 "[ -d <file> | --dump-binary=<file> ]\n" \
2917 "[ -D <dir> | --input-directory=<dir> ]\n" \
2918 "[ -f <format> | --format=<format> ]\n" \
2919 "[ -F <spec> | --format-spec=<spec> ]\n" \
2920 "[ -h | --hash-by-name ]\n" \
2921 "[ -o <file> | --output=<file> ]\n" \
2922 "[ -O | --no-text-output ]\n" \
2923 "[ -q | --quiet ]\n" \
2924 "[ -s | --per-program-stats ]\n" \
2925 "[ -S <event> | --sort-program-stats=<event> ]\n" \
2926 "[ -t | --track-ios ]\n" \
2927 "[ -w <time> | --stopwatch=<time> ]\n" \
2928 "[ -M | --no-msgs\n" \
2929 "[ -v | --verbose ]\n" \
2930 "[ -V | --version ]\n\n" \
2931 "\t-a Only trace specified actions. See documentation\n" \
2932 "\t-A Give trace mask as a single value. See documentation\n" \
2933 "\t-b stdin read batching\n" \
2934 "\t-d Output file. If specified, binary data is written to file\n" \
2935 "\t-D Directory to prepend to input file names\n" \
2936 "\t-f Output format. Customize the output format. The format field\n" \
2937 "\t identifies can be found in the documentation\n" \
2938 "\t-F Format specification. Can be found in the documentation\n" \
2939 "\t-h Hash processes by name, not pid\n" \
2940 "\t-i Input file containing trace data, or '-' for stdin\n" \
2941 "\t-o Output file. If not given, output is stdout\n" \
2942 "\t-O Do NOT output text data\n" \
2943 "\t-q Quiet. Don't display any stats at the end of the trace\n" \
2944 "\t-s Show per-program io statistics\n" \
2945 "\t-S Show per-program io statistics sorted by N/Q/q/R/r/W/w/C/c\n" \
2946 "\t N:Name, Q/q:Queued(read & write), R/r:Queued Read, W/w:Queued Write, C/c:Complete.\n" \
2947 "\t Sort programs by how much data(KB): Q,R,W,C.\n" \
2948 "\t Sort programs by how many IO operations: q,r,w,c.\n" \
2949 "\t if -S was used, the -s parameter will be ignored.\n" \
2950 "\t-t Track individual ios. Will tell you the time a request took\n" \
2951 "\t to get queued, to get dispatched, and to get completed\n" \
2952 "\t-w Only parse data between the given time interval in seconds.\n" \
2953 "\t If 'start' isn't given, blkparse defaults the start time to 0\n" \
2954 "\t-M Do not output messages to binary file\n" \
2955 "\t-v More verbose for marginal errors\n" \
2956 "\t-V Print program version info\n\n";
2958 static void usage(char *prog)
2960 fprintf(stderr, "Usage: %s %s", prog, usage_str);
2963 int main(int argc, char *argv[])
2965 int i, c, ret, mode;
2966 int act_mask_tmp = 0;
2967 char *ofp_buffer = NULL;
2968 char *bin_ofp_buffer = NULL;
2970 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
2973 i = find_mask_map(optarg);
2975 fprintf(stderr,"Invalid action mask %s\n",
2983 if ((sscanf(optarg, "%x", &i) != 1) ||
2984 !valid_act_opt(i)) {
2986 "Invalid set action mask %s/0x%x\n",
2993 if (is_pipe(optarg) && !pipeline) {
2995 pipename = strdup(optarg);
2996 } else if (resize_devices(optarg) != 0)
3003 output_name = optarg;
3009 rb_batch = atoi(optarg);
3011 rb_batch = RB_BATCH_DEFAULT;
3014 per_process_stats = 1;
3017 per_process_stats = 1;
3018 if (get_program_sort_event(optarg))
3025 per_device_and_cpu_stats = 0;
3028 if (find_stopwatch_interval(optarg) != 0)
3032 set_all_format_specs(optarg);
3035 if (add_format_spec(optarg) != 0)
3039 ppi_hash_by_pid = 0;
3045 printf("%s version %s\n", argv[0], blkparse_version);
3048 dump_binary = optarg;
3051 bin_output_msgs = 0;
3059 while (optind < argc) {
3060 if (is_pipe(argv[optind]) && !pipeline) {
3062 pipename = strdup(argv[optind]);
3063 } else if (resize_devices(argv[optind]) != 0)
3068 if (!pipeline && !ndevices) {
3073 if (act_mask_tmp != 0)
3074 act_mask = act_mask_tmp;
3076 memset(&rb_sort_root, 0, sizeof(rb_sort_root));
3078 signal(SIGINT, handle_sigint);
3079 signal(SIGHUP, handle_sigint);
3080 signal(SIGTERM, handle_sigint);
3082 setlocale(LC_NUMERIC, "en_US");
3086 ofp = fdopen(STDOUT_FILENO, "w");
3089 char ofname[PATH_MAX];
3091 snprintf(ofname, sizeof(ofname) - 1, "%s", output_name);
3092 ofp = fopen(ofname, "w");
3101 ofp_buffer = malloc(4096);
3102 if (setvbuf(ofp, ofp_buffer, mode, 4096)) {
3109 if (!strcmp(dump_binary, "-"))
3112 dump_fp = fopen(dump_binary, "w");
3114 perror(dump_binary);
3119 bin_ofp_buffer = malloc(128 * 1024);
3120 if (setvbuf(dump_fp, bin_ofp_buffer, _IOFBF, 128 * 1024)) {
3121 perror("setvbuf binary");
3134 if (have_drv_data && !dump_binary)
3135 printf("\ndiscarded traces containing low-level device driver "
3136 "specific data (only available in binary output)\n");
3142 if (bin_ofp_buffer) {
3144 free(bin_ofp_buffer);