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.3";
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;
275 static FILE *dump_fp;
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;
285 static char *pipename;
287 static int text_output = 1;
289 #define is_done() (*(volatile int *)(&done))
290 static volatile int done;
292 struct timespec abs_start_time;
293 static unsigned long long start_timestamp;
295 #define JHASH_RANDOM (0x3af5f2ee)
297 #define CPUS_PER_LONG (8 * sizeof(unsigned long))
298 #define CPU_IDX(cpu) ((cpu) / CPUS_PER_LONG)
299 #define CPU_BIT(cpu) ((cpu) & (CPUS_PER_LONG - 1))
301 static void output_binary(void *buf, int len)
304 size_t n = fwrite(buf, len, 1, dump_fp);
313 static void resize_cpu_info(struct per_dev_info *pdi, int cpu)
315 struct per_cpu_info *cpus = pdi->cpus;
316 int ncpus = pdi->ncpus;
317 int new_count = cpu + 1;
321 size = new_count * sizeof(struct per_cpu_info);
322 cpus = realloc(cpus, size);
325 fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n",
326 get_dev_name(pdi, name, sizeof(name)), size);
330 new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info));
331 new_space = (new_count - ncpus) * sizeof(struct per_cpu_info);
332 memset(new_start, 0, new_space);
334 pdi->ncpus = new_count;
337 for (new_count = 0; new_count < pdi->ncpus; new_count++) {
338 struct per_cpu_info *pci = &pdi->cpus[new_count];
342 memset(&pci->rb_last, 0, sizeof(pci->rb_last));
343 pci->rb_last_entries = 0;
344 pci->last_sequence = -1;
349 static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu)
351 struct per_cpu_info *pci;
353 if (cpu >= pdi->ncpus)
354 resize_cpu_info(pdi, cpu);
356 pci = &pdi->cpus[cpu];
362 static int resize_devices(char *name)
364 int size = (ndevices + 1) * sizeof(struct per_dev_info);
366 devices = realloc(devices, size);
368 fprintf(stderr, "Out of memory, device %s (%d)\n", name, size);
371 memset(&devices[ndevices], 0, sizeof(struct per_dev_info));
372 devices[ndevices].name = name;
377 static struct per_dev_info *get_dev_info(dev_t dev)
379 struct per_dev_info *pdi;
382 for (i = 0; i < ndevices; i++) {
384 devices[i].dev = dev;
385 if (devices[i].dev == dev)
389 if (resize_devices(NULL))
392 pdi = &devices[ndevices - 1];
394 pdi->first_reported_time = 0;
395 pdi->last_read_time = 0;
400 static void insert_skip(struct per_cpu_info *pci, unsigned long start,
403 struct skip_info *sip;
405 for (sip = pci->skips_tail; sip != NULL; sip = sip->prev) {
406 if (end == (sip->start - 1)) {
409 } else if (start == (sip->end + 1)) {
415 sip = malloc(sizeof(struct skip_info));
418 sip->prev = sip->next = NULL;
419 if (pci->skips_tail == NULL)
420 pci->skips_head = pci->skips_tail = sip;
422 sip->prev = pci->skips_tail;
423 pci->skips_tail->next = sip;
424 pci->skips_tail = sip;
428 static void remove_sip(struct per_cpu_info *pci, struct skip_info *sip)
430 if (sip->prev == NULL) {
431 if (sip->next == NULL)
432 pci->skips_head = pci->skips_tail = NULL;
434 pci->skips_head = sip->next;
435 sip->next->prev = NULL;
437 } else if (sip->next == NULL) {
438 pci->skips_tail = sip->prev;
439 sip->prev->next = NULL;
441 sip->prev->next = sip->next;
442 sip->next->prev = sip->prev;
445 sip->prev = sip->next = NULL;
449 #define IN_SKIP(sip,seq) (((sip)->start <= (seq)) && ((seq) <= sip->end))
450 static int check_current_skips(struct per_cpu_info *pci, unsigned long seq)
452 struct skip_info *sip;
454 for (sip = pci->skips_tail; sip != NULL; sip = sip->prev) {
455 if (IN_SKIP(sip, seq)) {
456 if (sip->start == seq) {
458 remove_sip(pci, sip);
461 } else if (sip->end == seq)
465 insert_skip(pci, seq + 1, sip->end);
474 static void collect_pdi_skips(struct per_dev_info *pdi)
476 struct skip_info *sip;
482 for (cpu = 0; cpu < pdi->ncpus; cpu++) {
483 struct per_cpu_info *pci = &pdi->cpus[cpu];
485 for (sip = pci->skips_head; sip != NULL; sip = sip->next) {
487 pdi->seq_skips += (sip->end - sip->start + 1);
489 fprintf(stderr,"(%d,%d): skipping %lu -> %lu\n",
490 MAJOR(pdi->dev), MINOR(pdi->dev),
491 sip->start, sip->end);
496 static void cpu_mark_online(struct per_dev_info *pdi, unsigned int cpu)
498 if (cpu >= pdi->cpu_map_max || !pdi->cpu_map) {
499 int new_max = (cpu + CPUS_PER_LONG) & ~(CPUS_PER_LONG - 1);
500 unsigned long *map = malloc(new_max / sizeof(long));
502 memset(map, 0, new_max / sizeof(long));
505 memcpy(map, pdi->cpu_map, pdi->cpu_map_max / sizeof(long));
510 pdi->cpu_map_max = new_max;
513 pdi->cpu_map[CPU_IDX(cpu)] |= (1UL << CPU_BIT(cpu));
516 static inline void cpu_mark_offline(struct per_dev_info *pdi, int cpu)
518 pdi->cpu_map[CPU_IDX(cpu)] &= ~(1UL << CPU_BIT(cpu));
521 static inline int cpu_is_online(struct per_dev_info *pdi, int cpu)
523 return (pdi->cpu_map[CPU_IDX(cpu)] & (1UL << CPU_BIT(cpu))) != 0;
526 static inline int ppm_hash_pid(pid_t pid)
528 return jhash_1word(pid, JHASH_RANDOM) & PPM_HASH_MASK;
531 static struct process_pid_map *find_ppm(pid_t pid)
533 const int hash_idx = ppm_hash_pid(pid);
534 struct process_pid_map *ppm;
536 ppm = ppm_hash_table[hash_idx];
541 ppm = ppm->hash_next;
547 static void add_ppm_hash(pid_t pid, const char *name)
549 const int hash_idx = ppm_hash_pid(pid);
550 struct process_pid_map *ppm;
554 ppm = malloc(sizeof(*ppm));
555 memset(ppm, 0, sizeof(*ppm));
557 strcpy(ppm->comm, name);
558 ppm->hash_next = ppm_hash_table[hash_idx];
559 ppm_hash_table[hash_idx] = ppm;
563 static void handle_notify(struct blk_io_trace *bit)
565 void *payload = (caddr_t) bit + sizeof(*bit);
568 switch (bit->action) {
570 add_ppm_hash(bit->pid, payload);
573 case BLK_TN_TIMESTAMP:
574 if (bit->pdu_len != sizeof(two32))
576 memcpy(two32, payload, sizeof(two32));
577 if (!data_is_native) {
578 two32[0] = be32_to_cpu(two32[0]);
579 two32[1] = be32_to_cpu(two32[1]);
581 start_timestamp = bit->time;
582 abs_start_time.tv_sec = two32[0];
583 abs_start_time.tv_nsec = two32[1];
584 if (abs_start_time.tv_nsec < 0) {
585 abs_start_time.tv_sec--;
586 abs_start_time.tv_nsec += 1000000000;
592 /* Ignore unknown notify events */
597 char *find_process_name(pid_t pid)
599 struct process_pid_map *ppm = find_ppm(pid);
607 static inline int ppi_hash_pid(pid_t pid)
609 return jhash_1word(pid, JHASH_RANDOM) & PPI_HASH_MASK;
612 static inline int ppi_hash_name(const char *name)
614 return jhash(name, 16, JHASH_RANDOM) & PPI_HASH_MASK;
617 static inline int ppi_hash(struct per_process_info *ppi)
619 struct process_pid_map *ppm = ppi->ppm;
622 return ppi_hash_pid(ppm->pid);
624 return ppi_hash_name(ppm->comm);
627 static inline void add_ppi_to_hash(struct per_process_info *ppi)
629 const int hash_idx = ppi_hash(ppi);
631 ppi->hash_next = ppi_hash_table[hash_idx];
632 ppi_hash_table[hash_idx] = ppi;
635 static inline void add_ppi_to_list(struct per_process_info *ppi)
637 ppi->list_next = ppi_list;
642 static struct per_process_info *find_ppi_by_name(char *name)
644 const int hash_idx = ppi_hash_name(name);
645 struct per_process_info *ppi;
647 ppi = ppi_hash_table[hash_idx];
649 struct process_pid_map *ppm = ppi->ppm;
651 if (!strcmp(ppm->comm, name))
654 ppi = ppi->hash_next;
660 static struct per_process_info *find_ppi_by_pid(pid_t pid)
662 const int hash_idx = ppi_hash_pid(pid);
663 struct per_process_info *ppi;
665 ppi = ppi_hash_table[hash_idx];
667 struct process_pid_map *ppm = ppi->ppm;
672 ppi = ppi->hash_next;
678 static struct per_process_info *find_ppi(pid_t pid)
680 struct per_process_info *ppi;
684 return find_ppi_by_pid(pid);
686 name = find_process_name(pid);
690 ppi = find_ppi_by_name(name);
691 if (ppi && ppi->ppm->pid != pid)
692 ppi->more_than_one = 1;
698 * struct trace and blktrace allocation cache, we do potentially
699 * millions of mallocs for these structures while only using at most
700 * a few thousand at the time
702 static inline void t_free(struct trace *t)
704 if (t_alloc_cache < 1024) {
705 t->next = t_alloc_list;
712 static inline struct trace *t_alloc(void)
714 struct trace *t = t_alloc_list;
717 t_alloc_list = t->next;
722 return malloc(sizeof(*t));
725 static inline void bit_free(struct blk_io_trace *bit)
727 if (bit_alloc_cache < 1024 && !bit->pdu_len) {
729 * abuse a 64-bit field for a next pointer for the free item
731 bit->time = (__u64) (unsigned long) bit_alloc_list;
732 bit_alloc_list = (struct blk_io_trace *) bit;
738 static inline struct blk_io_trace *bit_alloc(void)
740 struct blk_io_trace *bit = bit_alloc_list;
743 bit_alloc_list = (struct blk_io_trace *) (unsigned long) \
749 return malloc(sizeof(*bit));
752 static inline void __put_trace_last(struct per_dev_info *pdi, struct trace *t)
754 struct per_cpu_info *pci = get_cpu_info(pdi, t->bit->cpu);
756 rb_erase(&t->rb_node, &pci->rb_last);
757 pci->rb_last_entries--;
763 static void put_trace(struct per_dev_info *pdi, struct trace *t)
765 rb_erase(&t->rb_node, &rb_sort_root);
768 trace_rb_insert_last(pdi, t);
771 static inline int trace_rb_insert(struct trace *t, struct rb_root *root)
773 struct rb_node **p = &root->rb_node;
774 struct rb_node *parent = NULL;
780 __t = rb_entry(parent, struct trace, rb_node);
782 if (t->bit->time < __t->bit->time)
784 else if (t->bit->time > __t->bit->time)
786 else if (t->bit->device < __t->bit->device)
788 else if (t->bit->device > __t->bit->device)
790 else if (t->bit->sequence < __t->bit->sequence)
792 else /* >= sequence */
796 rb_link_node(&t->rb_node, parent, p);
797 rb_insert_color(&t->rb_node, root);
801 static inline int trace_rb_insert_sort(struct trace *t)
803 if (!trace_rb_insert(t, &rb_sort_root)) {
811 static int trace_rb_insert_last(struct per_dev_info *pdi, struct trace *t)
813 struct per_cpu_info *pci = get_cpu_info(pdi, t->bit->cpu);
815 if (trace_rb_insert(t, &pci->rb_last))
818 pci->rb_last_entries++;
820 if (pci->rb_last_entries > rb_batch * pdi->nfiles) {
821 struct rb_node *n = rb_first(&pci->rb_last);
823 t = rb_entry(n, struct trace, rb_node);
824 __put_trace_last(pdi, t);
830 static struct trace *trace_rb_find(dev_t device, unsigned long sequence,
831 struct rb_root *root, int order)
833 struct rb_node *n = root->rb_node;
834 struct rb_node *prev = NULL;
838 __t = rb_entry(n, struct trace, rb_node);
841 if (device < __t->bit->device)
843 else if (device > __t->bit->device)
845 else if (sequence < __t->bit->sequence)
847 else if (sequence > __t->bit->sequence)
854 * hack - the list may not be sequence ordered because some
855 * events don't have sequence and time matched. so we end up
856 * being a little off in the rb lookup here, because we don't
857 * know the time we are looking for. compensate by browsing
858 * a little ahead from the last entry to find the match
863 while (((n = rb_next(prev)) != NULL) && max--) {
864 __t = rb_entry(n, struct trace, rb_node);
866 if (__t->bit->device == device &&
867 __t->bit->sequence == sequence)
877 static inline struct trace *trace_rb_find_last(struct per_dev_info *pdi,
878 struct per_cpu_info *pci,
881 return trace_rb_find(pdi->dev, seq, &pci->rb_last, 0);
884 static inline int track_rb_insert(struct per_dev_info *pdi,struct io_track *iot)
886 struct rb_node **p = &pdi->rb_track.rb_node;
887 struct rb_node *parent = NULL;
888 struct io_track *__iot;
892 __iot = rb_entry(parent, struct io_track, rb_node);
894 if (iot->sector < __iot->sector)
896 else if (iot->sector > __iot->sector)
900 "sector alias (%Lu) on device %d,%d!\n",
901 (unsigned long long) iot->sector,
902 MAJOR(pdi->dev), MINOR(pdi->dev));
907 rb_link_node(&iot->rb_node, parent, p);
908 rb_insert_color(&iot->rb_node, &pdi->rb_track);
912 static struct io_track *__find_track(struct per_dev_info *pdi, __u64 sector)
914 struct rb_node *n = pdi->rb_track.rb_node;
915 struct io_track *__iot;
918 __iot = rb_entry(n, struct io_track, rb_node);
920 if (sector < __iot->sector)
922 else if (sector > __iot->sector)
931 static struct io_track *find_track(struct per_dev_info *pdi, pid_t pid,
934 struct io_track *iot;
936 iot = __find_track(pdi, sector);
938 iot = malloc(sizeof(*iot));
939 iot->ppm = find_ppm(pid);
940 iot->sector = sector;
941 track_rb_insert(pdi, iot);
947 static void log_track_frontmerge(struct per_dev_info *pdi,
948 struct blk_io_trace *t)
950 struct io_track *iot;
955 iot = __find_track(pdi, t->sector + t_sec(t));
958 fprintf(stderr, "merge not found for (%d,%d): %llu\n",
959 MAJOR(pdi->dev), MINOR(pdi->dev),
960 (unsigned long long) t->sector + t_sec(t));
964 rb_erase(&iot->rb_node, &pdi->rb_track);
965 iot->sector -= t_sec(t);
966 track_rb_insert(pdi, iot);
969 static void log_track_getrq(struct per_dev_info *pdi, struct blk_io_trace *t)
971 struct io_track *iot;
976 iot = find_track(pdi, t->pid, t->sector);
977 iot->allocation_time = t->time;
980 static inline int is_remapper(struct per_dev_info *pdi)
982 int major = MAJOR(pdi->dev);
984 return (major == 253 || major == 9);
988 * for md/dm setups, the interesting cycle is Q -> C. So track queueing
989 * time here, as dispatch time
991 static void log_track_queue(struct per_dev_info *pdi, struct blk_io_trace *t)
993 struct io_track *iot;
997 if (!is_remapper(pdi))
1000 iot = find_track(pdi, t->pid, t->sector);
1001 iot->dispatch_time = t->time;
1005 * return time between rq allocation and insertion
1007 static unsigned long long log_track_insert(struct per_dev_info *pdi,
1008 struct blk_io_trace *t)
1010 unsigned long long elapsed;
1011 struct io_track *iot;
1016 iot = find_track(pdi, t->pid, t->sector);
1017 iot->queue_time = t->time;
1019 if (!iot->allocation_time)
1022 elapsed = iot->queue_time - iot->allocation_time;
1024 if (per_process_stats) {
1025 struct per_process_info *ppi = find_ppi(iot->ppm->pid);
1026 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
1028 if (ppi && elapsed > ppi->longest_allocation_wait[w])
1029 ppi->longest_allocation_wait[w] = elapsed;
1036 * return time between queue and issue
1038 static unsigned long long log_track_issue(struct per_dev_info *pdi,
1039 struct blk_io_trace *t)
1041 unsigned long long elapsed;
1042 struct io_track *iot;
1046 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
1049 iot = __find_track(pdi, t->sector);
1052 fprintf(stderr, "issue not found for (%d,%d): %llu\n",
1053 MAJOR(pdi->dev), MINOR(pdi->dev),
1054 (unsigned long long) t->sector);
1058 iot->dispatch_time = t->time;
1059 elapsed = iot->dispatch_time - iot->queue_time;
1061 if (per_process_stats) {
1062 struct per_process_info *ppi = find_ppi(iot->ppm->pid);
1063 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
1065 if (ppi && elapsed > ppi->longest_dispatch_wait[w])
1066 ppi->longest_dispatch_wait[w] = elapsed;
1073 * return time between dispatch and complete
1075 static unsigned long long log_track_complete(struct per_dev_info *pdi,
1076 struct blk_io_trace *t)
1078 unsigned long long elapsed;
1079 struct io_track *iot;
1084 iot = __find_track(pdi, t->sector);
1087 fprintf(stderr,"complete not found for (%d,%d): %llu\n",
1088 MAJOR(pdi->dev), MINOR(pdi->dev),
1089 (unsigned long long) t->sector);
1093 iot->completion_time = t->time;
1094 elapsed = iot->completion_time - iot->dispatch_time;
1096 if (per_process_stats) {
1097 struct per_process_info *ppi = find_ppi(iot->ppm->pid);
1098 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
1100 if (ppi && elapsed > ppi->longest_completion_wait[w])
1101 ppi->longest_completion_wait[w] = elapsed;
1105 * kill the trace, we don't need it after completion
1107 rb_erase(&iot->rb_node, &pdi->rb_track);
1114 static struct io_stats *find_process_io_stats(pid_t pid)
1116 struct per_process_info *ppi = find_ppi(pid);
1119 ppi = malloc(sizeof(*ppi));
1120 memset(ppi, 0, sizeof(*ppi));
1121 ppi->ppm = find_ppm(pid);
1122 add_ppi_to_hash(ppi);
1123 add_ppi_to_list(ppi);
1126 return &ppi->io_stats;
1129 static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size)
1132 snprintf(buffer, size, "%s", pdi->name);
1134 snprintf(buffer, size, "%d,%d",MAJOR(pdi->dev),MINOR(pdi->dev));
1138 static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit)
1140 unsigned long long this = bit->time;
1141 unsigned long long last = pdi->last_reported_time;
1143 pdi->backwards = (this < last) ? 'B' : ' ';
1144 pdi->last_reported_time = this;
1147 static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t,
1152 ios->mwrite_kb += t_kb(t);
1155 ios->mread_kb += t_kb(t);
1159 static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci,
1162 __account_m(&pci->io_stats, t, rw);
1164 if (per_process_stats) {
1165 struct io_stats *ios = find_process_io_stats(t->pid);
1167 __account_m(ios, t, rw);
1171 static inline void __account_pc_queue(struct io_stats *ios,
1172 struct blk_io_trace *t, int rw)
1176 ios->qwrite_kb_pc += t_kb(t);
1179 ios->qread_kb += t_kb(t);
1183 static inline void account_pc_queue(struct blk_io_trace *t,
1184 struct per_cpu_info *pci, int rw)
1186 __account_pc_queue(&pci->io_stats, t, rw);
1188 if (per_process_stats) {
1189 struct io_stats *ios = find_process_io_stats(t->pid);
1191 __account_pc_queue(ios, t, rw);
1195 static inline void __account_pc_issue(struct io_stats *ios, int rw,
1200 ios->iwrite_kb_pc += bytes >> 10;
1203 ios->iread_kb_pc += bytes >> 10;
1207 static inline void account_pc_issue(struct blk_io_trace *t,
1208 struct per_cpu_info *pci, int rw)
1210 __account_pc_issue(&pci->io_stats, rw, t->bytes);
1212 if (per_process_stats) {
1213 struct io_stats *ios = find_process_io_stats(t->pid);
1215 __account_pc_issue(ios, rw, t->bytes);
1219 static inline void __account_pc_requeue(struct io_stats *ios,
1220 struct blk_io_trace *t, int rw)
1224 ios->iwrite_kb_pc -= t_kb(t);
1227 ios->iread_kb_pc -= t_kb(t);
1231 static inline void account_pc_requeue(struct blk_io_trace *t,
1232 struct per_cpu_info *pci, int rw)
1234 __account_pc_requeue(&pci->io_stats, t, rw);
1236 if (per_process_stats) {
1237 struct io_stats *ios = find_process_io_stats(t->pid);
1239 __account_pc_requeue(ios, t, rw);
1243 static inline void __account_pc_c(struct io_stats *ios, int rw)
1251 static inline void account_pc_c(struct blk_io_trace *t,
1252 struct per_cpu_info *pci, int rw)
1254 __account_pc_c(&pci->io_stats, rw);
1256 if (per_process_stats) {
1257 struct io_stats *ios = find_process_io_stats(t->pid);
1259 __account_pc_c(ios, rw);
1263 static inline void __account_queue(struct io_stats *ios, struct blk_io_trace *t,
1268 ios->qwrite_kb += t_kb(t);
1271 ios->qread_kb += t_kb(t);
1275 static inline void account_queue(struct blk_io_trace *t,
1276 struct per_cpu_info *pci, int rw)
1278 __account_queue(&pci->io_stats, t, rw);
1280 if (per_process_stats) {
1281 struct io_stats *ios = find_process_io_stats(t->pid);
1283 __account_queue(ios, t, rw);
1287 static inline void __account_c(struct io_stats *ios, int rw, int bytes)
1291 ios->cwrite_kb += bytes >> 10;
1294 ios->cread_kb += bytes >> 10;
1298 static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci,
1301 __account_c(&pci->io_stats, rw, bytes);
1303 if (per_process_stats) {
1304 struct io_stats *ios = find_process_io_stats(t->pid);
1306 __account_c(ios, rw, bytes);
1310 static inline void __account_issue(struct io_stats *ios, int rw,
1315 ios->iwrite_kb += bytes >> 10;
1318 ios->iread_kb += bytes >> 10;
1322 static inline void account_issue(struct blk_io_trace *t,
1323 struct per_cpu_info *pci, int rw)
1325 __account_issue(&pci->io_stats, rw, t->bytes);
1327 if (per_process_stats) {
1328 struct io_stats *ios = find_process_io_stats(t->pid);
1330 __account_issue(ios, rw, t->bytes);
1334 static inline void __account_unplug(struct io_stats *ios, int timer)
1337 ios->timer_unplugs++;
1342 static inline void account_unplug(struct blk_io_trace *t,
1343 struct per_cpu_info *pci, int timer)
1345 __account_unplug(&pci->io_stats, timer);
1347 if (per_process_stats) {
1348 struct io_stats *ios = find_process_io_stats(t->pid);
1350 __account_unplug(ios, timer);
1354 static inline void __account_requeue(struct io_stats *ios,
1355 struct blk_io_trace *t, int rw)
1359 ios->iwrite_kb -= t_kb(t);
1362 ios->iread_kb -= t_kb(t);
1366 static inline void account_requeue(struct blk_io_trace *t,
1367 struct per_cpu_info *pci, int rw)
1369 __account_requeue(&pci->io_stats, t, rw);
1371 if (per_process_stats) {
1372 struct io_stats *ios = find_process_io_stats(t->pid);
1374 __account_requeue(ios, t, rw);
1378 static void log_complete(struct per_dev_info *pdi, struct per_cpu_info *pci,
1379 struct blk_io_trace *t, char *act)
1381 process_fmt(act, pci, t, log_track_complete(pdi, t), 0, NULL);
1384 static void log_insert(struct per_dev_info *pdi, struct per_cpu_info *pci,
1385 struct blk_io_trace *t, char *act)
1387 process_fmt(act, pci, t, log_track_insert(pdi, t), 0, NULL);
1390 static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t,
1393 process_fmt(act, pci, t, -1, 0, NULL);
1396 static void log_issue(struct per_dev_info *pdi, struct per_cpu_info *pci,
1397 struct blk_io_trace *t, char *act)
1399 process_fmt(act, pci, t, log_track_issue(pdi, t), 0, NULL);
1402 static void log_merge(struct per_dev_info *pdi, struct per_cpu_info *pci,
1403 struct blk_io_trace *t, char *act)
1406 log_track_frontmerge(pdi, t);
1408 process_fmt(act, pci, t, -1ULL, 0, NULL);
1411 static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t,
1414 process_fmt(act, pci, t, -1ULL, 0, NULL);
1417 static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t,
1420 process_fmt(act, pci, t, -1ULL, 0, NULL);
1423 static void log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t,
1426 process_fmt(act, pci, t, -1ULL, 0, NULL);
1429 static void log_split(struct per_cpu_info *pci, struct blk_io_trace *t,
1432 process_fmt(act, pci, t, -1ULL, 0, NULL);
1435 static void log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act)
1437 unsigned char *buf = (unsigned char *) t + sizeof(*t);
1439 process_fmt(act, pci, t, -1ULL, t->pdu_len, buf);
1442 static void dump_trace_pc(struct blk_io_trace *t, struct per_dev_info *pdi,
1443 struct per_cpu_info *pci)
1445 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
1446 int act = t->action & 0xffff;
1449 case __BLK_TA_QUEUE:
1450 log_generic(pci, t, "Q");
1451 account_pc_queue(t, pci, w);
1453 case __BLK_TA_GETRQ:
1454 log_generic(pci, t, "G");
1456 case __BLK_TA_SLEEPRQ:
1457 log_generic(pci, t, "S");
1459 case __BLK_TA_REQUEUE:
1461 * can happen if we miss traces, don't let it go
1464 if (pdi->cur_depth[w])
1465 pdi->cur_depth[w]--;
1466 account_pc_requeue(t, pci, w);
1467 log_generic(pci, t, "R");
1469 case __BLK_TA_ISSUE:
1470 account_pc_issue(t, pci, w);
1471 pdi->cur_depth[w]++;
1472 if (pdi->cur_depth[w] > pdi->max_depth[w])
1473 pdi->max_depth[w] = pdi->cur_depth[w];
1474 log_pc(pci, t, "D");
1476 case __BLK_TA_COMPLETE:
1477 if (pdi->cur_depth[w])
1478 pdi->cur_depth[w]--;
1479 log_pc(pci, t, "C");
1480 account_pc_c(t, pci, w);
1482 case __BLK_TA_INSERT:
1483 log_pc(pci, t, "I");
1486 fprintf(stderr, "Bad pc action %x\n", act);
1491 static void dump_trace_fs(struct blk_io_trace *t, struct per_dev_info *pdi,
1492 struct per_cpu_info *pci)
1494 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
1495 int act = t->action & 0xffff;
1498 case __BLK_TA_QUEUE:
1499 log_track_queue(pdi, t);
1500 account_queue(t, pci, w);
1501 log_queue(pci, t, "Q");
1503 case __BLK_TA_INSERT:
1504 log_insert(pdi, pci, t, "I");
1506 case __BLK_TA_BACKMERGE:
1507 account_m(t, pci, w);
1508 log_merge(pdi, pci, t, "M");
1510 case __BLK_TA_FRONTMERGE:
1511 account_m(t, pci, w);
1512 log_merge(pdi, pci, t, "F");
1514 case __BLK_TA_GETRQ:
1515 log_track_getrq(pdi, t);
1516 log_generic(pci, t, "G");
1518 case __BLK_TA_SLEEPRQ:
1519 log_generic(pci, t, "S");
1521 case __BLK_TA_REQUEUE:
1523 * can happen if we miss traces, don't let it go
1526 if (pdi->cur_depth[w])
1527 pdi->cur_depth[w]--;
1528 account_requeue(t, pci, w);
1529 log_queue(pci, t, "R");
1531 case __BLK_TA_ISSUE:
1532 account_issue(t, pci, w);
1533 pdi->cur_depth[w]++;
1534 if (pdi->cur_depth[w] > pdi->max_depth[w])
1535 pdi->max_depth[w] = pdi->cur_depth[w];
1536 log_issue(pdi, pci, t, "D");
1538 case __BLK_TA_COMPLETE:
1539 if (pdi->cur_depth[w])
1540 pdi->cur_depth[w]--;
1541 account_c(t, pci, w, t->bytes);
1542 log_complete(pdi, pci, t, "C");
1545 log_action(pci, t, "P");
1547 case __BLK_TA_UNPLUG_IO:
1548 account_unplug(t, pci, 0);
1549 log_unplug(pci, t, "U");
1551 case __BLK_TA_UNPLUG_TIMER:
1552 account_unplug(t, pci, 1);
1553 log_unplug(pci, t, "UT");
1555 case __BLK_TA_SPLIT:
1556 log_split(pci, t, "X");
1558 case __BLK_TA_BOUNCE:
1559 log_generic(pci, t, "B");
1561 case __BLK_TA_REMAP:
1562 log_generic(pci, t, "A");
1565 fprintf(stderr, "Bad fs action %x\n", t->action);
1570 static void dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
1571 struct per_dev_info *pdi)
1574 if (t->action & BLK_TC_ACT(BLK_TC_PC))
1575 dump_trace_pc(t, pdi, pci);
1577 dump_trace_fs(t, pdi, pci);
1581 pdi->first_reported_time = t->time;
1585 output_binary(t, sizeof(*t) + t->pdu_len);
1589 * print in a proper way, not too small and not too big. if more than
1590 * 1000,000K, turn into M and so on
1592 static char *size_cnv(char *dst, unsigned long long num, int in_kb)
1594 char suff[] = { '\0', 'K', 'M', 'G', 'P' };
1600 while (num > 1000 * 1000ULL && (i < sizeof(suff) - 1)) {
1605 sprintf(dst, "%'8Lu%c", num, suff[i]);
1609 static void dump_io_stats(struct per_dev_info *pdi, struct io_stats *ios,
1612 static char x[256], y[256];
1614 fprintf(ofp, "%s\n", msg);
1616 fprintf(ofp, " Reads Queued: %s, %siB\t", size_cnv(x, ios->qreads, 0), size_cnv(y, ios->qread_kb, 1));
1617 fprintf(ofp, " Writes Queued: %s, %siB\n", size_cnv(x, ios->qwrites, 0), size_cnv(y, ios->qwrite_kb, 1));
1618 fprintf(ofp, " Read Dispatches: %s, %siB\t", size_cnv(x, ios->ireads, 0), size_cnv(y, ios->iread_kb, 1));
1619 fprintf(ofp, " Write Dispatches: %s, %siB\n", size_cnv(x, ios->iwrites, 0), size_cnv(y, ios->iwrite_kb, 1));
1620 fprintf(ofp, " Reads Requeued: %s\t\t", size_cnv(x, ios->rrqueue, 0));
1621 fprintf(ofp, " Writes Requeued: %s\n", size_cnv(x, ios->wrqueue, 0));
1622 fprintf(ofp, " Reads Completed: %s, %siB\t", size_cnv(x, ios->creads, 0), size_cnv(y, ios->cread_kb, 1));
1623 fprintf(ofp, " Writes Completed: %s, %siB\n", size_cnv(x, ios->cwrites, 0), size_cnv(y, ios->cwrite_kb, 1));
1624 fprintf(ofp, " Read Merges: %s, %siB\t", size_cnv(x, ios->mreads, 0), size_cnv(y, ios->mread_kb, 1));
1625 fprintf(ofp, " Write Merges: %s, %siB\n", size_cnv(x, ios->mwrites, 0), size_cnv(y, ios->mwrite_kb, 1));
1627 fprintf(ofp, " Read depth: %'8u%8c\t", pdi->max_depth[0], ' ');
1628 fprintf(ofp, " Write depth: %'8u\n", pdi->max_depth[1]);
1630 if (ios->qreads_pc || ios->qwrites_pc || ios->ireads_pc || ios->iwrites_pc ||
1631 ios->rrqueue_pc || ios->wrqueue_pc || ios->creads_pc || ios->cwrites_pc) {
1632 fprintf(ofp, " PC Reads Queued: %s, %siB\t", size_cnv(x, ios->qreads_pc, 0), size_cnv(y, ios->qread_kb_pc, 1));
1633 fprintf(ofp, " PC Writes Queued: %s, %siB\n", size_cnv(x, ios->qwrites_pc, 0), size_cnv(y, ios->qwrite_kb_pc, 1));
1634 fprintf(ofp, " PC Read Disp.: %s, %siB\t", size_cnv(x, ios->ireads_pc, 0), size_cnv(y, ios->iread_kb_pc, 1));
1635 fprintf(ofp, " PC Write Disp.: %s, %siB\n", size_cnv(x, ios->iwrites_pc, 0), size_cnv(y, ios->iwrite_kb_pc, 1));
1636 fprintf(ofp, " PC Reads Req.: %s\t\t", size_cnv(x, ios->rrqueue_pc, 0));
1637 fprintf(ofp, " PC Writes Req.: %s\n", size_cnv(x, ios->wrqueue_pc, 0));
1638 fprintf(ofp, " PC Reads Compl.: %s\t\t", size_cnv(x, ios->creads_pc, 0));
1639 fprintf(ofp, " PC Writes Compl.: %s\n", size_cnv(x, ios->cwrites, 0));
1641 fprintf(ofp, " IO unplugs: %'8lu%8c\t", ios->io_unplugs, ' ');
1642 fprintf(ofp, " Timer unplugs: %'8lu\n", ios->timer_unplugs);
1645 static void dump_wait_stats(struct per_process_info *ppi)
1647 unsigned long rawait = ppi->longest_allocation_wait[0] / 1000;
1648 unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000;
1649 unsigned long rcwait = ppi->longest_completion_wait[0] / 1000;
1650 unsigned long wawait = ppi->longest_allocation_wait[1] / 1000;
1651 unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000;
1652 unsigned long wcwait = ppi->longest_completion_wait[1] / 1000;
1654 fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' ');
1655 fprintf(ofp, " Allocation wait: %'8lu\n", wawait);
1656 fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' ');
1657 fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait);
1658 fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' ');
1659 fprintf(ofp, " Completion wait: %'8lu\n", wcwait);
1662 static int ppi_name_compare(const void *p1, const void *p2)
1664 struct per_process_info *ppi1 = *((struct per_process_info **) p1);
1665 struct per_process_info *ppi2 = *((struct per_process_info **) p2);
1668 res = strverscmp(ppi1->ppm->comm, ppi2->ppm->comm);
1670 res = ppi1->ppm->pid > ppi2->ppm->pid;
1675 static void sort_process_list(void)
1677 struct per_process_info **ppis;
1678 struct per_process_info *ppi;
1681 ppis = malloc(ppi_list_entries * sizeof(struct per_process_info *));
1686 ppi = ppi->list_next;
1689 qsort(ppis, ppi_list_entries, sizeof(ppi), ppi_name_compare);
1691 i = ppi_list_entries - 1;
1696 ppi->list_next = ppi_list;
1704 static void show_process_stats(void)
1706 struct per_process_info *ppi;
1708 sort_process_list();
1712 struct process_pid_map *ppm = ppi->ppm;
1715 if (ppi->more_than_one)
1716 sprintf(name, "%s (%u, ...)", ppm->comm, ppm->pid);
1718 sprintf(name, "%s (%u)", ppm->comm, ppm->pid);
1720 dump_io_stats(NULL, &ppi->io_stats, name);
1721 dump_wait_stats(ppi);
1722 ppi = ppi->list_next;
1728 static void show_device_and_cpu_stats(void)
1730 struct per_dev_info *pdi;
1731 struct per_cpu_info *pci;
1732 struct io_stats total, *ios;
1733 unsigned long long rrate, wrate, msec;
1734 int i, j, pci_events;
1735 char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3];
1739 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
1741 memset(&total, 0, sizeof(total));
1747 for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) {
1751 ios = &pci->io_stats;
1752 total.qreads += ios->qreads;
1753 total.qwrites += ios->qwrites;
1754 total.creads += ios->creads;
1755 total.cwrites += ios->cwrites;
1756 total.mreads += ios->mreads;
1757 total.mwrites += ios->mwrites;
1758 total.ireads += ios->ireads;
1759 total.iwrites += ios->iwrites;
1760 total.rrqueue += ios->rrqueue;
1761 total.wrqueue += ios->wrqueue;
1762 total.qread_kb += ios->qread_kb;
1763 total.qwrite_kb += ios->qwrite_kb;
1764 total.cread_kb += ios->cread_kb;
1765 total.cwrite_kb += ios->cwrite_kb;
1766 total.iread_kb += ios->iread_kb;
1767 total.iwrite_kb += ios->iwrite_kb;
1768 total.mread_kb += ios->mread_kb;
1769 total.mwrite_kb += ios->mwrite_kb;
1771 total.qreads_pc += ios->qreads_pc;
1772 total.qwrites_pc += ios->qwrites_pc;
1773 total.creads_pc += ios->creads_pc;
1774 total.cwrites_pc += ios->cwrites_pc;
1775 total.ireads_pc += ios->ireads_pc;
1776 total.iwrites_pc += ios->iwrites_pc;
1777 total.rrqueue_pc += ios->rrqueue_pc;
1778 total.wrqueue_pc += ios->wrqueue_pc;
1779 total.qread_kb_pc += ios->qread_kb_pc;
1780 total.qwrite_kb_pc += ios->qwrite_kb_pc;
1781 total.iread_kb_pc += ios->iread_kb_pc;
1782 total.iwrite_kb_pc += ios->iwrite_kb_pc;
1784 total.timer_unplugs += ios->timer_unplugs;
1785 total.io_unplugs += ios->io_unplugs;
1787 snprintf(line, sizeof(line) - 1, "CPU%d (%s):",
1788 j, get_dev_name(pdi, name, sizeof(name)));
1789 dump_io_stats(pdi, ios, line);
1793 if (pci_events > 1) {
1795 snprintf(line, sizeof(line) - 1, "Total (%s):",
1796 get_dev_name(pdi, name, sizeof(name)));
1797 dump_io_stats(NULL, &total, line);
1801 msec = (pdi->last_reported_time - pdi->first_reported_time) / 1000000;
1803 rrate = 1000 * total.cread_kb / msec;
1804 wrate = 1000 * total.cwrite_kb / msec;
1807 fprintf(ofp, "\nThroughput (R/W): %'LuKiB/s / %'LuKiB/s\n",
1809 fprintf(ofp, "Events (%s): %'Lu entries\n",
1810 get_dev_name(pdi, line, sizeof(line)), pdi->events);
1812 collect_pdi_skips(pdi);
1813 if (!pdi->skips && !pdi->events)
1816 ratio = 100.0 * ((double)pdi->seq_skips /
1817 (double)(pdi->events + pdi->seq_skips));
1818 fprintf(ofp, "Skips: %'lu forward (%'llu - %5.1lf%%)\n",
1819 pdi->skips, pdi->seq_skips, ratio);
1823 static void find_genesis(void)
1825 struct trace *t = trace_list;
1827 genesis_time = -1ULL;
1829 if (t->bit->time < genesis_time)
1830 genesis_time = t->bit->time;
1835 /* The time stamp record will usually be the first
1836 * record in the trace, but not always.
1839 && start_timestamp != genesis_time) {
1840 long delta = genesis_time - start_timestamp;
1842 abs_start_time.tv_sec += SECONDS(delta);
1843 abs_start_time.tv_nsec += NANO_SECONDS(delta);
1844 if (abs_start_time.tv_nsec < 0) {
1845 abs_start_time.tv_nsec += 1000000000;
1846 abs_start_time.tv_sec -= 1;
1848 if (abs_start_time.tv_nsec > 1000000000) {
1849 abs_start_time.tv_nsec -= 1000000000;
1850 abs_start_time.tv_sec += 1;
1855 static inline int check_stopwatch(struct blk_io_trace *bit)
1857 if (bit->time < stopwatch_end &&
1858 bit->time >= stopwatch_start)
1865 * return youngest entry read
1867 static int sort_entries(unsigned long long *youngest)
1869 struct per_dev_info *pdi = NULL;
1870 struct per_cpu_info *pci = NULL;
1877 while ((t = trace_list) != NULL) {
1878 struct blk_io_trace *bit = t->bit;
1880 trace_list = t->next;
1882 bit->time -= genesis_time;
1884 if (bit->time < *youngest || !*youngest)
1885 *youngest = bit->time;
1887 if (!pdi || pdi->dev != bit->device) {
1888 pdi = get_dev_info(bit->device);
1892 if (!pci || pci->cpu != bit->cpu)
1893 pci = get_cpu_info(pdi, bit->cpu);
1895 if (bit->sequence < pci->smallest_seq_read)
1896 pci->smallest_seq_read = bit->sequence;
1898 if (check_stopwatch(bit)) {
1904 if (trace_rb_insert_sort(t))
1912 * to continue, we must have traces from all online cpus in the tree
1914 static int check_cpu_map(struct per_dev_info *pdi)
1916 unsigned long *cpu_map;
1923 * create a map of the cpus we have traces for
1925 cpu_map = malloc(pdi->cpu_map_max / sizeof(long));
1926 n = rb_first(&rb_sort_root);
1928 __t = rb_entry(n, struct trace, rb_node);
1929 cpu = __t->bit->cpu;
1931 cpu_map[CPU_IDX(cpu)] |= (1UL << CPU_BIT(cpu));
1936 * we can't continue if pdi->cpu_map has entries set that we don't
1937 * have in the sort rbtree. the opposite is not a problem, though
1940 for (i = 0; i < pdi->cpu_map_max / CPUS_PER_LONG; i++) {
1941 if (pdi->cpu_map[i] & ~(cpu_map[i])) {
1951 static int check_sequence(struct per_dev_info *pdi, struct trace *t, int force)
1953 struct blk_io_trace *bit = t->bit;
1954 unsigned long expected_sequence;
1955 struct per_cpu_info *pci;
1958 pci = get_cpu_info(pdi, bit->cpu);
1959 expected_sequence = pci->last_sequence + 1;
1961 if (!expected_sequence) {
1963 * 1 should be the first entry, just allow it
1965 if (bit->sequence == 1)
1967 if (bit->sequence == pci->smallest_seq_read)
1970 return check_cpu_map(pdi);
1973 if (bit->sequence == expected_sequence)
1977 * we may not have seen that sequence yet. if we are not doing
1978 * the final run, break and wait for more entries.
1980 if (expected_sequence < pci->smallest_seq_read) {
1981 __t = trace_rb_find_last(pdi, pci, expected_sequence);
1985 __put_trace_last(pdi, __t);
1987 } else if (!force) {
1991 if (check_current_skips(pci, bit->sequence))
1994 if (expected_sequence < bit->sequence)
1995 insert_skip(pci, expected_sequence, bit->sequence - 1);
2000 static void show_entries_rb(int force)
2002 struct per_dev_info *pdi = NULL;
2003 struct per_cpu_info *pci = NULL;
2004 struct blk_io_trace *bit;
2008 while ((n = rb_first(&rb_sort_root)) != NULL) {
2009 if (is_done() && !force && !pipeline)
2012 t = rb_entry(n, struct trace, rb_node);
2015 if (read_sequence - t->read_sequence < 1 && !force)
2018 if (!pdi || pdi->dev != bit->device) {
2019 pdi = get_dev_info(bit->device);
2024 fprintf(stderr, "Unknown device ID? (%d,%d)\n",
2025 MAJOR(bit->device), MINOR(bit->device));
2029 if (check_sequence(pdi, t, force))
2032 if (!force && bit->time > last_allowed_time)
2035 check_time(pdi, bit);
2037 if (!pci || pci->cpu != bit->cpu)
2038 pci = get_cpu_info(pdi, bit->cpu);
2040 pci->last_sequence = bit->sequence;
2044 if (bit->action & (act_mask << BLK_TC_SHIFT))
2045 dump_trace(bit, pci, pdi);
2051 static int read_data(int fd, void *buffer, int bytes, int block, int *fdblock)
2053 int ret, bytes_left, fl;
2056 if (block != *fdblock) {
2057 fl = fcntl(fd, F_GETFL);
2061 fcntl(fd, F_SETFL, fl | O_NONBLOCK);
2064 fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
2070 while (bytes_left > 0) {
2071 ret = read(fd, p, bytes_left);
2075 if (errno != EAGAIN) {
2081 * never do partial reads. we can return if we
2082 * didn't read anything and we should not block,
2083 * otherwise wait for data
2085 if ((bytes_left == bytes) && !block)
2099 static inline __u16 get_pdulen(struct blk_io_trace *bit)
2102 return bit->pdu_len;
2104 return __bswap_16(bit->pdu_len);
2107 static inline __u32 get_magic(struct blk_io_trace *bit)
2112 return __bswap_32(bit->magic);
2115 static int read_events(int fd, int always_block, int *fdblock)
2117 struct per_dev_info *pdi = NULL;
2118 unsigned int events = 0;
2120 while (!is_done() && events < rb_batch) {
2121 struct blk_io_trace *bit;
2123 int pdu_len, should_block, ret;
2128 should_block = !events || always_block;
2130 ret = read_data(fd, bit, sizeof(*bit), should_block, fdblock);
2133 if (!events && ret < 0)
2139 * look at first trace to check whether we need to convert
2140 * data in the future
2142 if (data_is_native == -1 && check_data_endianness(bit->magic))
2145 magic = get_magic(bit);
2146 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
2147 fprintf(stderr, "Bad magic %x\n", magic);
2151 pdu_len = get_pdulen(bit);
2153 void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
2155 if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1, fdblock)) {
2165 if (verify_trace(bit)) {
2171 * not a real trace, so grab and handle it here
2173 if (bit->action & BLK_TC_ACT(BLK_TC_NOTIFY)) {
2175 output_binary(bit, sizeof(*bit) + bit->pdu_len);
2180 memset(t, 0, sizeof(*t));
2182 t->read_sequence = read_sequence;
2184 t->next = trace_list;
2187 if (!pdi || pdi->dev != bit->device)
2188 pdi = get_dev_info(bit->device);
2190 if (bit->time > pdi->last_read_time)
2191 pdi->last_read_time = bit->time;
2200 * Managing input streams
2204 struct ms_stream *next;
2205 struct trace *first, *last;
2206 struct per_dev_info *pdi;
2210 #define MS_HASH(d, c) ((MAJOR(d) & 0xff) ^ (MINOR(d) & 0xff) ^ (cpu & 0xff))
2212 struct ms_stream *ms_head;
2213 struct ms_stream *ms_hash[256];
2215 static void ms_sort(struct ms_stream *msp);
2216 static int ms_prime(struct ms_stream *msp);
2218 static inline struct trace *ms_peek(struct ms_stream *msp)
2220 return (msp == NULL) ? NULL : msp->first;
2223 static inline __u64 ms_peek_time(struct ms_stream *msp)
2225 return ms_peek(msp)->bit->time;
2228 static inline void ms_resort(struct ms_stream *msp)
2230 if (msp->next && ms_peek_time(msp) > ms_peek_time(msp->next)) {
2231 ms_head = msp->next;
2237 static inline void ms_deq(struct ms_stream *msp)
2239 msp->first = msp->first->next;
2242 if (!ms_prime(msp)) {
2243 ms_head = msp->next;
2252 static void ms_sort(struct ms_stream *msp)
2254 __u64 msp_t = ms_peek_time(msp);
2255 struct ms_stream *this_msp = ms_head;
2257 if (this_msp == NULL)
2259 else if (msp_t < ms_peek_time(this_msp)) {
2260 msp->next = this_msp;
2264 while (this_msp->next && ms_peek_time(this_msp->next) < msp_t)
2265 this_msp = this_msp->next;
2267 msp->next = this_msp->next;
2268 this_msp->next = msp;
2272 static int ms_prime(struct ms_stream *msp)
2277 struct per_dev_info *pdi = msp->pdi;
2278 struct per_cpu_info *pci = get_cpu_info(pdi, msp->cpu);
2279 struct blk_io_trace *bit = NULL;
2280 int ret, pdu_len, ndone = 0;
2282 for (i = 0; !is_done() && pci->fd >= 0 && i < rb_batch; i++) {
2284 ret = read_data(pci->fd, bit, sizeof(*bit), 1, &pci->fdblock);
2288 if (data_is_native == -1 && check_data_endianness(bit->magic))
2291 magic = get_magic(bit);
2292 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
2293 fprintf(stderr, "Bad magic %x\n", magic);
2298 pdu_len = get_pdulen(bit);
2300 void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
2301 ret = read_data(pci->fd, ptr + sizeof(*bit), pdu_len,
2313 if (verify_trace(bit))
2316 if (bit->action & BLK_TC_ACT(BLK_TC_NOTIFY)) {
2318 output_binary(bit, sizeof(*bit) + bit->pdu_len);
2325 if (bit->time > pdi->last_read_time)
2326 pdi->last_read_time = bit->time;
2329 memset(t, 0, sizeof(*t));
2332 if (msp->first == NULL)
2333 msp->first = msp->last = t;
2335 msp->last->next = t;
2345 if (bit) bit_free(bit);
2347 cpu_mark_offline(pdi, pci->cpu);
2354 static struct ms_stream *ms_alloc(struct per_dev_info *pdi, int cpu)
2356 struct ms_stream *msp = malloc(sizeof(*msp));
2359 msp->first = msp->last = NULL;
2369 static int setup_file(struct per_dev_info *pdi, int cpu)
2374 struct per_cpu_info *pci = get_cpu_info(pdi, cpu);
2379 p = strdup(pdi->name);
2381 if (strcmp(dname, ".")) {
2383 p = strdup(pdi->name);
2384 strcpy(pdi->name, basename(p));
2389 len = sprintf(pci->fname, "%s/", input_dir);
2391 snprintf(pci->fname + len, sizeof(pci->fname)-1-len,
2392 "%s.blktrace.%d", pdi->name, pci->cpu);
2393 if (stat(pci->fname, &st) < 0)
2398 pci->fd = open(pci->fname, O_RDONLY);
2404 printf("Input file %s added\n", pci->fname);
2405 cpu_mark_online(pdi, pci->cpu);
2408 ms_alloc(pdi, pci->cpu);
2413 static int handle(struct ms_stream *msp)
2416 struct per_dev_info *pdi;
2417 struct per_cpu_info *pci;
2418 struct blk_io_trace *bit;
2421 if (t->bit->time > stopwatch_end)
2426 pci = get_cpu_info(pdi, msp->cpu);
2429 bit->time -= genesis_time;
2430 pdi->last_reported_time = bit->time;
2431 if (bit->action & (act_mask << BLK_TC_SHIFT))
2432 dump_trace(bit, pci, pdi);
2437 trace_rb_insert_last(pdi, t);
2447 * Check if we need to sanitize the name. We allow 'foo', or if foo.blktrace.X
2448 * is given, then strip back down to 'foo' to avoid missing files.
2450 static int name_fixup(char *name)
2457 b = strstr(name, ".blktrace.");
2464 static int do_file(void)
2467 struct per_dev_info *pdi;
2470 * first prepare all files for reading
2472 for (i = 0; i < ndevices; i++) {
2474 ret = name_fixup(pdi->name);
2478 for (cpu = 0; setup_file(pdi, cpu); cpu++)
2483 * Get the initial time stamp
2486 genesis_time = ms_peek_time(ms_head);
2489 * Keep processing traces while any are left
2491 while (!is_done() && ms_head && handle(ms_head))
2497 static void do_pipe(int fd)
2499 unsigned long long youngest;
2500 int events, fdblock;
2502 last_allowed_time = -1ULL;
2504 while ((events = read_events(fd, 0, &fdblock)) > 0) {
2508 smallest_seq_read = -1U;
2511 if (sort_entries(&youngest))
2514 if (youngest > stopwatch_end)
2520 if (rb_sort_entries)
2524 static int do_fifo(void)
2528 if (!strcmp(pipename, "-"))
2529 fd = dup(STDIN_FILENO);
2531 fd = open(pipename, O_RDONLY);
2534 perror("dup stdin");
2543 static void show_stats(void)
2552 if (per_process_stats)
2553 show_process_stats();
2555 if (per_device_and_cpu_stats)
2556 show_device_and_cpu_stats();
2561 static void handle_sigint(__attribute__((__unused__)) int sig)
2567 * Extract start and duration times from a string, allowing
2568 * us to specify a time interval of interest within a trace.
2569 * Format: "duration" (start is zero) or "start:duration".
2571 static int find_stopwatch_interval(char *string)
2576 value = strtod(string, &sp);
2578 fprintf(stderr,"Invalid stopwatch timer: %s\n", string);
2582 stopwatch_start = DOUBLE_TO_NANO_ULL(value);
2584 value = strtod(string, &sp);
2585 if (sp == string || *sp != '\0') {
2586 fprintf(stderr,"Invalid stopwatch duration time: %s\n",
2590 } else if (*sp != '\0') {
2591 fprintf(stderr,"Invalid stopwatch start timer: %s\n", string);
2594 stopwatch_end = DOUBLE_TO_NANO_ULL(value);
2595 if (stopwatch_end <= stopwatch_start) {
2596 fprintf(stderr, "Invalid stopwatch interval: %Lu -> %Lu\n",
2597 stopwatch_start, stopwatch_end);
2604 static int is_pipe(const char *str)
2608 if (!strcmp(str, "-"))
2610 if (!stat(str, &st) && S_ISFIFO(st.st_mode))
2616 #define S_OPTS "a:A:b:D:d:f:F:hi:o:Oqstw:vV"
2617 static char usage_str[] = "\n\n" \
2618 "-i <file> | --input=<file>\n" \
2619 "[ -a <action field> | --act-mask=<action field> ]\n" \
2620 "[ -A <action mask> | --set-mask=<action mask> ]\n" \
2621 "[ -b <traces> | --batch=<traces> ]\n" \
2622 "[ -d <file> | --dump-binary=<file> ]\n" \
2623 "[ -D <dir> | --input-directory=<dir> ]\n" \
2624 "[ -f <format> | --format=<format> ]\n" \
2625 "[ -F <spec> | --format-spec=<spec> ]\n" \
2626 "[ -h | --hash-by-name ]\n" \
2627 "[ -o <file> | --output=<file> ]\n" \
2628 "[ -O | --no-text-output ]\n" \
2629 "[ -q | --quiet ]\n" \
2630 "[ -s | --per-program-stats ]\n" \
2631 "[ -t | --track-ios ]\n" \
2632 "[ -w <time> | --stopwatch=<time> ]\n" \
2633 "[ -v | --verbose ]\n" \
2634 "[ -V | --version ]\n\n" \
2635 "\t-b stdin read batching\n" \
2636 "\t-d Output file. If specified, binary data is written to file\n" \
2637 "\t-D Directory to prepend to input file names\n" \
2638 "\t-f Output format. Customize the output format. The format field\n" \
2639 "\t identifies can be found in the documentation\n" \
2640 "\t-F Format specification. Can be found in the documentation\n" \
2641 "\t-h Hash processes by name, not pid\n" \
2642 "\t-i Input file containing trace data, or '-' for stdin\n" \
2643 "\t-o Output file. If not given, output is stdout\n" \
2644 "\t-O Do NOT output text data\n" \
2645 "\t-q Quiet. Don't display any stats at the end of the trace\n" \
2646 "\t-s Show per-program io statistics\n" \
2647 "\t-t Track individual ios. Will tell you the time a request took\n" \
2648 "\t to get queued, to get dispatched, and to get completed\n" \
2649 "\t-w Only parse data between the given time interval in seconds.\n" \
2650 "\t If 'start' isn't given, blkparse defaults the start time to 0\n" \
2651 "\t-v More verbose for marginal errors\n" \
2652 "\t-V Print program version info\n\n";
2654 static void usage(char *prog)
2656 fprintf(stderr, "Usage: %s %s %s", prog, blkparse_version, usage_str);
2659 int main(int argc, char *argv[])
2661 int i, c, ret, mode;
2662 int act_mask_tmp = 0;
2663 char *ofp_buffer = NULL;
2664 char *bin_ofp_buffer = NULL;
2666 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
2669 i = find_mask_map(optarg);
2671 fprintf(stderr,"Invalid action mask %s\n",
2679 if ((sscanf(optarg, "%x", &i) != 1) ||
2680 !valid_act_opt(i)) {
2682 "Invalid set action mask %s/0x%x\n",
2689 if (is_pipe(optarg) && !pipeline) {
2691 pipename = strdup(optarg);
2692 } else if (resize_devices(optarg) != 0)
2699 output_name = optarg;
2705 rb_batch = atoi(optarg);
2707 rb_batch = RB_BATCH_DEFAULT;
2710 per_process_stats = 1;
2716 per_device_and_cpu_stats = 0;
2719 if (find_stopwatch_interval(optarg) != 0)
2723 set_all_format_specs(optarg);
2726 if (add_format_spec(optarg) != 0)
2730 ppi_hash_by_pid = 0;
2736 printf("%s version %s\n", argv[0], blkparse_version);
2739 dump_binary = optarg;
2747 while (optind < argc) {
2748 if (is_pipe(argv[optind]) && !pipeline) {
2750 pipename = strdup(argv[optind]);
2751 } else if (resize_devices(argv[optind]) != 0)
2756 if (!pipeline && !ndevices) {
2761 if (act_mask_tmp != 0)
2762 act_mask = act_mask_tmp;
2764 memset(&rb_sort_root, 0, sizeof(rb_sort_root));
2766 signal(SIGINT, handle_sigint);
2767 signal(SIGHUP, handle_sigint);
2768 signal(SIGTERM, handle_sigint);
2770 setlocale(LC_NUMERIC, "en_US");
2774 ofp = fdopen(STDOUT_FILENO, "w");
2779 snprintf(ofname, sizeof(ofname) - 1, "%s", output_name);
2780 ofp = fopen(ofname, "w");
2789 ofp_buffer = malloc(4096);
2790 if (setvbuf(ofp, ofp_buffer, mode, 4096)) {
2797 dump_fp = fopen(dump_binary, "w");
2799 perror(dump_binary);
2803 bin_ofp_buffer = malloc(128 * 1024);
2804 if (setvbuf(dump_fp, bin_ofp_buffer, _IOFBF, 128 * 1024)) {
2805 perror("setvbuf binary");
2822 if (bin_ofp_buffer) {
2824 free(bin_ofp_buffer);