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.2";
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;
286 static int text_output = 1;
288 #define is_done() (*(volatile int *)(&done))
289 static volatile int done;
291 struct timespec abs_start_time;
292 static unsigned long long start_timestamp;
294 #define JHASH_RANDOM (0x3af5f2ee)
296 #define CPUS_PER_LONG (8 * sizeof(unsigned long))
297 #define CPU_IDX(cpu) ((cpu) / CPUS_PER_LONG)
298 #define CPU_BIT(cpu) ((cpu) & (CPUS_PER_LONG - 1))
300 static void output_binary(void *buf, int len)
303 size_t n = fwrite(buf, len, 1, dump_fp);
312 static void resize_cpu_info(struct per_dev_info *pdi, int cpu)
314 struct per_cpu_info *cpus = pdi->cpus;
315 int ncpus = pdi->ncpus;
316 int new_count = cpu + 1;
320 size = new_count * sizeof(struct per_cpu_info);
321 cpus = realloc(cpus, size);
324 fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n",
325 get_dev_name(pdi, name, sizeof(name)), size);
329 new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info));
330 new_space = (new_count - ncpus) * sizeof(struct per_cpu_info);
331 memset(new_start, 0, new_space);
333 pdi->ncpus = new_count;
336 for (new_count = 0; new_count < pdi->ncpus; new_count++) {
337 struct per_cpu_info *pci = &pdi->cpus[new_count];
341 memset(&pci->rb_last, 0, sizeof(pci->rb_last));
342 pci->rb_last_entries = 0;
343 pci->last_sequence = -1;
348 static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu)
350 struct per_cpu_info *pci;
352 if (cpu >= pdi->ncpus)
353 resize_cpu_info(pdi, cpu);
355 pci = &pdi->cpus[cpu];
361 static int resize_devices(char *name)
363 int size = (ndevices + 1) * sizeof(struct per_dev_info);
365 devices = realloc(devices, size);
367 fprintf(stderr, "Out of memory, device %s (%d)\n", name, size);
370 memset(&devices[ndevices], 0, sizeof(struct per_dev_info));
371 devices[ndevices].name = name;
376 static struct per_dev_info *get_dev_info(dev_t dev)
378 struct per_dev_info *pdi;
381 for (i = 0; i < ndevices; i++) {
383 devices[i].dev = dev;
384 if (devices[i].dev == dev)
388 if (resize_devices(NULL))
391 pdi = &devices[ndevices - 1];
393 pdi->first_reported_time = 0;
394 pdi->last_read_time = 0;
399 static void insert_skip(struct per_cpu_info *pci, unsigned long start,
402 struct skip_info *sip;
404 for (sip = pci->skips_tail; sip != NULL; sip = sip->prev) {
405 if (end == (sip->start - 1)) {
408 } else if (start == (sip->end + 1)) {
414 sip = malloc(sizeof(struct skip_info));
417 sip->prev = sip->next = NULL;
418 if (pci->skips_tail == NULL)
419 pci->skips_head = pci->skips_tail = sip;
421 sip->prev = pci->skips_tail;
422 pci->skips_tail->next = sip;
423 pci->skips_tail = sip;
427 static void remove_sip(struct per_cpu_info *pci, struct skip_info *sip)
429 if (sip->prev == NULL) {
430 if (sip->next == NULL)
431 pci->skips_head = pci->skips_tail = NULL;
433 pci->skips_head = sip->next;
434 sip->next->prev = NULL;
436 } else if (sip->next == NULL) {
437 pci->skips_tail = sip->prev;
438 sip->prev->next = NULL;
440 sip->prev->next = sip->next;
441 sip->next->prev = sip->prev;
444 sip->prev = sip->next = NULL;
448 #define IN_SKIP(sip,seq) (((sip)->start <= (seq)) && ((seq) <= sip->end))
449 static int check_current_skips(struct per_cpu_info *pci, unsigned long seq)
451 struct skip_info *sip;
453 for (sip = pci->skips_tail; sip != NULL; sip = sip->prev) {
454 if (IN_SKIP(sip, seq)) {
455 if (sip->start == seq) {
457 remove_sip(pci, sip);
460 } else if (sip->end == seq)
464 insert_skip(pci, seq + 1, sip->end);
473 static void collect_pdi_skips(struct per_dev_info *pdi)
475 struct skip_info *sip;
481 for (cpu = 0; cpu < pdi->ncpus; cpu++) {
482 struct per_cpu_info *pci = &pdi->cpus[cpu];
484 for (sip = pci->skips_head; sip != NULL; sip = sip->next) {
486 pdi->seq_skips += (sip->end - sip->start + 1);
488 fprintf(stderr,"(%d,%d): skipping %lu -> %lu\n",
489 MAJOR(pdi->dev), MINOR(pdi->dev),
490 sip->start, sip->end);
495 static void cpu_mark_online(struct per_dev_info *pdi, unsigned int cpu)
497 if (cpu >= pdi->cpu_map_max || !pdi->cpu_map) {
498 int new_max = (cpu + CPUS_PER_LONG) & ~(CPUS_PER_LONG - 1);
499 unsigned long *map = malloc(new_max / sizeof(long));
501 memset(map, 0, new_max / sizeof(long));
504 memcpy(map, pdi->cpu_map, pdi->cpu_map_max / sizeof(long));
509 pdi->cpu_map_max = new_max;
512 pdi->cpu_map[CPU_IDX(cpu)] |= (1UL << CPU_BIT(cpu));
515 static inline void cpu_mark_offline(struct per_dev_info *pdi, int cpu)
517 pdi->cpu_map[CPU_IDX(cpu)] &= ~(1UL << CPU_BIT(cpu));
520 static inline int cpu_is_online(struct per_dev_info *pdi, int cpu)
522 return (pdi->cpu_map[CPU_IDX(cpu)] & (1UL << CPU_BIT(cpu))) != 0;
525 static inline int ppm_hash_pid(pid_t pid)
527 return jhash_1word(pid, JHASH_RANDOM) & PPM_HASH_MASK;
530 static struct process_pid_map *find_ppm(pid_t pid)
532 const int hash_idx = ppm_hash_pid(pid);
533 struct process_pid_map *ppm;
535 ppm = ppm_hash_table[hash_idx];
540 ppm = ppm->hash_next;
546 static void add_ppm_hash(pid_t pid, const char *name)
548 const int hash_idx = ppm_hash_pid(pid);
549 struct process_pid_map *ppm;
553 ppm = malloc(sizeof(*ppm));
554 memset(ppm, 0, sizeof(*ppm));
556 strcpy(ppm->comm, name);
557 ppm->hash_next = ppm_hash_table[hash_idx];
558 ppm_hash_table[hash_idx] = ppm;
562 static void handle_notify(struct blk_io_trace *bit)
564 void *payload = (caddr_t) bit + sizeof(*bit);
567 switch (bit->action) {
569 add_ppm_hash(bit->pid, payload);
572 case BLK_TN_TIMESTAMP:
573 if (bit->pdu_len != sizeof(two32))
575 memcpy(two32, payload, sizeof(two32));
576 if (!data_is_native) {
577 two32[0] = be32_to_cpu(two32[0]);
578 two32[1] = be32_to_cpu(two32[1]);
580 start_timestamp = bit->time;
581 abs_start_time.tv_sec = two32[0];
582 abs_start_time.tv_nsec = two32[1];
583 if (abs_start_time.tv_nsec < 0) {
584 abs_start_time.tv_sec--;
585 abs_start_time.tv_nsec += 1000000000;
591 /* Ignore unknown notify events */
596 char *find_process_name(pid_t pid)
598 struct process_pid_map *ppm = find_ppm(pid);
606 static inline int ppi_hash_pid(pid_t pid)
608 return jhash_1word(pid, JHASH_RANDOM) & PPI_HASH_MASK;
611 static inline int ppi_hash_name(const char *name)
613 return jhash(name, 16, JHASH_RANDOM) & PPI_HASH_MASK;
616 static inline int ppi_hash(struct per_process_info *ppi)
618 struct process_pid_map *ppm = ppi->ppm;
621 return ppi_hash_pid(ppm->pid);
623 return ppi_hash_name(ppm->comm);
626 static inline void add_ppi_to_hash(struct per_process_info *ppi)
628 const int hash_idx = ppi_hash(ppi);
630 ppi->hash_next = ppi_hash_table[hash_idx];
631 ppi_hash_table[hash_idx] = ppi;
634 static inline void add_ppi_to_list(struct per_process_info *ppi)
636 ppi->list_next = ppi_list;
641 static struct per_process_info *find_ppi_by_name(char *name)
643 const int hash_idx = ppi_hash_name(name);
644 struct per_process_info *ppi;
646 ppi = ppi_hash_table[hash_idx];
648 struct process_pid_map *ppm = ppi->ppm;
650 if (!strcmp(ppm->comm, name))
653 ppi = ppi->hash_next;
659 static struct per_process_info *find_ppi_by_pid(pid_t pid)
661 const int hash_idx = ppi_hash_pid(pid);
662 struct per_process_info *ppi;
664 ppi = ppi_hash_table[hash_idx];
666 struct process_pid_map *ppm = ppi->ppm;
671 ppi = ppi->hash_next;
677 static struct per_process_info *find_ppi(pid_t pid)
679 struct per_process_info *ppi;
683 return find_ppi_by_pid(pid);
685 name = find_process_name(pid);
689 ppi = find_ppi_by_name(name);
690 if (ppi && ppi->ppm->pid != pid)
691 ppi->more_than_one = 1;
697 * struct trace and blktrace allocation cache, we do potentially
698 * millions of mallocs for these structures while only using at most
699 * a few thousand at the time
701 static inline void t_free(struct trace *t)
703 if (t_alloc_cache < 1024) {
704 t->next = t_alloc_list;
711 static inline struct trace *t_alloc(void)
713 struct trace *t = t_alloc_list;
716 t_alloc_list = t->next;
721 return malloc(sizeof(*t));
724 static inline void bit_free(struct blk_io_trace *bit)
726 if (bit_alloc_cache < 1024 && !bit->pdu_len) {
728 * abuse a 64-bit field for a next pointer for the free item
730 bit->time = (__u64) (unsigned long) bit_alloc_list;
731 bit_alloc_list = (struct blk_io_trace *) bit;
737 static inline struct blk_io_trace *bit_alloc(void)
739 struct blk_io_trace *bit = bit_alloc_list;
742 bit_alloc_list = (struct blk_io_trace *) (unsigned long) \
748 return malloc(sizeof(*bit));
751 static inline void __put_trace_last(struct per_dev_info *pdi, struct trace *t)
753 struct per_cpu_info *pci = get_cpu_info(pdi, t->bit->cpu);
755 rb_erase(&t->rb_node, &pci->rb_last);
756 pci->rb_last_entries--;
762 static void put_trace(struct per_dev_info *pdi, struct trace *t)
764 rb_erase(&t->rb_node, &rb_sort_root);
767 trace_rb_insert_last(pdi, t);
770 static inline int trace_rb_insert(struct trace *t, struct rb_root *root)
772 struct rb_node **p = &root->rb_node;
773 struct rb_node *parent = NULL;
779 __t = rb_entry(parent, struct trace, rb_node);
781 if (t->bit->time < __t->bit->time)
783 else if (t->bit->time > __t->bit->time)
785 else if (t->bit->device < __t->bit->device)
787 else if (t->bit->device > __t->bit->device)
789 else if (t->bit->sequence < __t->bit->sequence)
791 else /* >= sequence */
795 rb_link_node(&t->rb_node, parent, p);
796 rb_insert_color(&t->rb_node, root);
800 static inline int trace_rb_insert_sort(struct trace *t)
802 if (!trace_rb_insert(t, &rb_sort_root)) {
810 static int trace_rb_insert_last(struct per_dev_info *pdi, struct trace *t)
812 struct per_cpu_info *pci = get_cpu_info(pdi, t->bit->cpu);
814 if (trace_rb_insert(t, &pci->rb_last))
817 pci->rb_last_entries++;
819 if (pci->rb_last_entries > rb_batch * pdi->nfiles) {
820 struct rb_node *n = rb_first(&pci->rb_last);
822 t = rb_entry(n, struct trace, rb_node);
823 __put_trace_last(pdi, t);
829 static struct trace *trace_rb_find(dev_t device, unsigned long sequence,
830 struct rb_root *root, int order)
832 struct rb_node *n = root->rb_node;
833 struct rb_node *prev = NULL;
837 __t = rb_entry(n, struct trace, rb_node);
840 if (device < __t->bit->device)
842 else if (device > __t->bit->device)
844 else if (sequence < __t->bit->sequence)
846 else if (sequence > __t->bit->sequence)
853 * hack - the list may not be sequence ordered because some
854 * events don't have sequence and time matched. so we end up
855 * being a little off in the rb lookup here, because we don't
856 * know the time we are looking for. compensate by browsing
857 * a little ahead from the last entry to find the match
862 while (((n = rb_next(prev)) != NULL) && max--) {
863 __t = rb_entry(n, struct trace, rb_node);
865 if (__t->bit->device == device &&
866 __t->bit->sequence == sequence)
876 static inline struct trace *trace_rb_find_last(struct per_dev_info *pdi,
877 struct per_cpu_info *pci,
880 return trace_rb_find(pdi->dev, seq, &pci->rb_last, 0);
883 static inline int track_rb_insert(struct per_dev_info *pdi,struct io_track *iot)
885 struct rb_node **p = &pdi->rb_track.rb_node;
886 struct rb_node *parent = NULL;
887 struct io_track *__iot;
891 __iot = rb_entry(parent, struct io_track, rb_node);
893 if (iot->sector < __iot->sector)
895 else if (iot->sector > __iot->sector)
899 "sector alias (%Lu) on device %d,%d!\n",
900 (unsigned long long) iot->sector,
901 MAJOR(pdi->dev), MINOR(pdi->dev));
906 rb_link_node(&iot->rb_node, parent, p);
907 rb_insert_color(&iot->rb_node, &pdi->rb_track);
911 static struct io_track *__find_track(struct per_dev_info *pdi, __u64 sector)
913 struct rb_node *n = pdi->rb_track.rb_node;
914 struct io_track *__iot;
917 __iot = rb_entry(n, struct io_track, rb_node);
919 if (sector < __iot->sector)
921 else if (sector > __iot->sector)
930 static struct io_track *find_track(struct per_dev_info *pdi, pid_t pid,
933 struct io_track *iot;
935 iot = __find_track(pdi, sector);
937 iot = malloc(sizeof(*iot));
938 iot->ppm = find_ppm(pid);
939 iot->sector = sector;
940 track_rb_insert(pdi, iot);
946 static void log_track_frontmerge(struct per_dev_info *pdi,
947 struct blk_io_trace *t)
949 struct io_track *iot;
954 iot = __find_track(pdi, t->sector + t_sec(t));
957 fprintf(stderr, "merge not found for (%d,%d): %llu\n",
958 MAJOR(pdi->dev), MINOR(pdi->dev),
959 (unsigned long long) t->sector + t_sec(t));
963 rb_erase(&iot->rb_node, &pdi->rb_track);
964 iot->sector -= t_sec(t);
965 track_rb_insert(pdi, iot);
968 static void log_track_getrq(struct per_dev_info *pdi, struct blk_io_trace *t)
970 struct io_track *iot;
975 iot = find_track(pdi, t->pid, t->sector);
976 iot->allocation_time = t->time;
979 static inline int is_remapper(struct per_dev_info *pdi)
981 int major = MAJOR(pdi->dev);
983 return (major == 253 || major == 9);
987 * for md/dm setups, the interesting cycle is Q -> C. So track queueing
988 * time here, as dispatch time
990 static void log_track_queue(struct per_dev_info *pdi, struct blk_io_trace *t)
992 struct io_track *iot;
996 if (!is_remapper(pdi))
999 iot = find_track(pdi, t->pid, t->sector);
1000 iot->dispatch_time = t->time;
1004 * return time between rq allocation and insertion
1006 static unsigned long long log_track_insert(struct per_dev_info *pdi,
1007 struct blk_io_trace *t)
1009 unsigned long long elapsed;
1010 struct io_track *iot;
1015 iot = find_track(pdi, t->pid, t->sector);
1016 iot->queue_time = t->time;
1018 if (!iot->allocation_time)
1021 elapsed = iot->queue_time - iot->allocation_time;
1023 if (per_process_stats) {
1024 struct per_process_info *ppi = find_ppi(iot->ppm->pid);
1025 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
1027 if (ppi && elapsed > ppi->longest_allocation_wait[w])
1028 ppi->longest_allocation_wait[w] = elapsed;
1035 * return time between queue and issue
1037 static unsigned long long log_track_issue(struct per_dev_info *pdi,
1038 struct blk_io_trace *t)
1040 unsigned long long elapsed;
1041 struct io_track *iot;
1045 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
1048 iot = __find_track(pdi, t->sector);
1051 fprintf(stderr, "issue not found for (%d,%d): %llu\n",
1052 MAJOR(pdi->dev), MINOR(pdi->dev),
1053 (unsigned long long) t->sector);
1057 iot->dispatch_time = t->time;
1058 elapsed = iot->dispatch_time - iot->queue_time;
1060 if (per_process_stats) {
1061 struct per_process_info *ppi = find_ppi(iot->ppm->pid);
1062 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
1064 if (ppi && elapsed > ppi->longest_dispatch_wait[w])
1065 ppi->longest_dispatch_wait[w] = elapsed;
1072 * return time between dispatch and complete
1074 static unsigned long long log_track_complete(struct per_dev_info *pdi,
1075 struct blk_io_trace *t)
1077 unsigned long long elapsed;
1078 struct io_track *iot;
1083 iot = __find_track(pdi, t->sector);
1086 fprintf(stderr,"complete not found for (%d,%d): %llu\n",
1087 MAJOR(pdi->dev), MINOR(pdi->dev),
1088 (unsigned long long) t->sector);
1092 iot->completion_time = t->time;
1093 elapsed = iot->completion_time - iot->dispatch_time;
1095 if (per_process_stats) {
1096 struct per_process_info *ppi = find_ppi(iot->ppm->pid);
1097 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
1099 if (ppi && elapsed > ppi->longest_completion_wait[w])
1100 ppi->longest_completion_wait[w] = elapsed;
1104 * kill the trace, we don't need it after completion
1106 rb_erase(&iot->rb_node, &pdi->rb_track);
1113 static struct io_stats *find_process_io_stats(pid_t pid)
1115 struct per_process_info *ppi = find_ppi(pid);
1118 ppi = malloc(sizeof(*ppi));
1119 memset(ppi, 0, sizeof(*ppi));
1120 ppi->ppm = find_ppm(pid);
1121 add_ppi_to_hash(ppi);
1122 add_ppi_to_list(ppi);
1125 return &ppi->io_stats;
1128 static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size)
1131 snprintf(buffer, size, "%s", pdi->name);
1133 snprintf(buffer, size, "%d,%d",MAJOR(pdi->dev),MINOR(pdi->dev));
1137 static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit)
1139 unsigned long long this = bit->time;
1140 unsigned long long last = pdi->last_reported_time;
1142 pdi->backwards = (this < last) ? 'B' : ' ';
1143 pdi->last_reported_time = this;
1146 static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t,
1151 ios->qwrite_kb += t_kb(t);
1154 ios->qread_kb += t_kb(t);
1158 static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci,
1161 __account_m(&pci->io_stats, t, rw);
1163 if (per_process_stats) {
1164 struct io_stats *ios = find_process_io_stats(t->pid);
1166 __account_m(ios, t, rw);
1170 static inline void __account_queue(struct io_stats *ios, struct blk_io_trace *t,
1175 ios->qwrite_kb += t_kb(t);
1178 ios->qread_kb += t_kb(t);
1182 static inline void account_queue(struct blk_io_trace *t,
1183 struct per_cpu_info *pci, int rw)
1185 __account_queue(&pci->io_stats, t, rw);
1187 if (per_process_stats) {
1188 struct io_stats *ios = find_process_io_stats(t->pid);
1190 __account_queue(ios, t, rw);
1194 static inline void __account_c(struct io_stats *ios, int rw, int bytes)
1198 ios->cwrite_kb += bytes >> 10;
1201 ios->cread_kb += bytes >> 10;
1205 static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci,
1208 __account_c(&pci->io_stats, rw, bytes);
1210 if (per_process_stats) {
1211 struct io_stats *ios = find_process_io_stats(t->pid);
1213 __account_c(ios, rw, bytes);
1217 static inline void __account_issue(struct io_stats *ios, int rw,
1222 ios->iwrite_kb += bytes >> 10;
1225 ios->iread_kb += bytes >> 10;
1229 static inline void account_issue(struct blk_io_trace *t,
1230 struct per_cpu_info *pci, int rw)
1232 __account_issue(&pci->io_stats, rw, t->bytes);
1234 if (per_process_stats) {
1235 struct io_stats *ios = find_process_io_stats(t->pid);
1237 __account_issue(ios, rw, t->bytes);
1241 static inline void __account_unplug(struct io_stats *ios, int timer)
1244 ios->timer_unplugs++;
1249 static inline void account_unplug(struct blk_io_trace *t,
1250 struct per_cpu_info *pci, int timer)
1252 __account_unplug(&pci->io_stats, timer);
1254 if (per_process_stats) {
1255 struct io_stats *ios = find_process_io_stats(t->pid);
1257 __account_unplug(ios, timer);
1261 static inline void __account_requeue(struct io_stats *ios,
1262 struct blk_io_trace *t, int rw)
1266 ios->iwrite_kb -= t_kb(t);
1269 ios->iread_kb -= t_kb(t);
1273 static inline void account_requeue(struct blk_io_trace *t,
1274 struct per_cpu_info *pci, int rw)
1276 __account_requeue(&pci->io_stats, t, rw);
1278 if (per_process_stats) {
1279 struct io_stats *ios = find_process_io_stats(t->pid);
1281 __account_requeue(ios, t, rw);
1285 static void log_complete(struct per_dev_info *pdi, struct per_cpu_info *pci,
1286 struct blk_io_trace *t, char *act)
1288 process_fmt(act, pci, t, log_track_complete(pdi, t), 0, NULL);
1291 static void log_insert(struct per_dev_info *pdi, struct per_cpu_info *pci,
1292 struct blk_io_trace *t, char *act)
1294 process_fmt(act, pci, t, log_track_insert(pdi, t), 0, NULL);
1297 static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t,
1300 process_fmt(act, pci, t, -1, 0, NULL);
1303 static void log_issue(struct per_dev_info *pdi, struct per_cpu_info *pci,
1304 struct blk_io_trace *t, char *act)
1306 process_fmt(act, pci, t, log_track_issue(pdi, t), 0, NULL);
1309 static void log_merge(struct per_dev_info *pdi, struct per_cpu_info *pci,
1310 struct blk_io_trace *t, char *act)
1313 log_track_frontmerge(pdi, t);
1315 process_fmt(act, pci, t, -1ULL, 0, NULL);
1318 static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t,
1321 process_fmt(act, pci, t, -1ULL, 0, NULL);
1324 static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t,
1327 process_fmt(act, pci, t, -1ULL, 0, NULL);
1330 static void log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t,
1333 process_fmt(act, pci, t, -1ULL, 0, NULL);
1336 static void log_split(struct per_cpu_info *pci, struct blk_io_trace *t,
1339 process_fmt(act, pci, t, -1ULL, 0, NULL);
1342 static void log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act)
1344 unsigned char *buf = (unsigned char *) t + sizeof(*t);
1346 process_fmt(act, pci, t, -1ULL, t->pdu_len, buf);
1349 static void dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci)
1351 int act = t->action & 0xffff;
1354 case __BLK_TA_QUEUE:
1355 log_generic(pci, t, "Q");
1357 case __BLK_TA_GETRQ:
1358 log_generic(pci, t, "G");
1360 case __BLK_TA_SLEEPRQ:
1361 log_generic(pci, t, "S");
1363 case __BLK_TA_REQUEUE:
1364 log_generic(pci, t, "R");
1366 case __BLK_TA_ISSUE:
1367 log_pc(pci, t, "D");
1369 case __BLK_TA_COMPLETE:
1370 log_pc(pci, t, "C");
1372 case __BLK_TA_INSERT:
1373 log_pc(pci, t, "I");
1376 fprintf(stderr, "Bad pc action %x\n", act);
1381 static void dump_trace_fs(struct blk_io_trace *t, struct per_dev_info *pdi,
1382 struct per_cpu_info *pci)
1384 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
1385 int act = t->action & 0xffff;
1388 case __BLK_TA_QUEUE:
1389 log_track_queue(pdi, t);
1390 account_queue(t, pci, w);
1391 log_queue(pci, t, "Q");
1393 case __BLK_TA_INSERT:
1394 log_insert(pdi, pci, t, "I");
1396 case __BLK_TA_BACKMERGE:
1397 account_m(t, pci, w);
1398 log_merge(pdi, pci, t, "M");
1400 case __BLK_TA_FRONTMERGE:
1401 account_m(t, pci, w);
1402 log_merge(pdi, pci, t, "F");
1404 case __BLK_TA_GETRQ:
1405 log_track_getrq(pdi, t);
1406 log_generic(pci, t, "G");
1408 case __BLK_TA_SLEEPRQ:
1409 log_generic(pci, t, "S");
1411 case __BLK_TA_REQUEUE:
1413 * can happen if we miss traces, don't let it go
1416 if (pdi->cur_depth[w])
1417 pdi->cur_depth[w]--;
1418 account_requeue(t, pci, w);
1419 log_queue(pci, t, "R");
1421 case __BLK_TA_ISSUE:
1422 account_issue(t, pci, w);
1423 pdi->cur_depth[w]++;
1424 if (pdi->cur_depth[w] > pdi->max_depth[w])
1425 pdi->max_depth[w] = pdi->cur_depth[w];
1426 log_issue(pdi, pci, t, "D");
1428 case __BLK_TA_COMPLETE:
1429 if (pdi->cur_depth[w])
1430 pdi->cur_depth[w]--;
1431 account_c(t, pci, w, t->bytes);
1432 log_complete(pdi, pci, t, "C");
1435 log_action(pci, t, "P");
1437 case __BLK_TA_UNPLUG_IO:
1438 account_unplug(t, pci, 0);
1439 log_unplug(pci, t, "U");
1441 case __BLK_TA_UNPLUG_TIMER:
1442 account_unplug(t, pci, 1);
1443 log_unplug(pci, t, "UT");
1445 case __BLK_TA_SPLIT:
1446 log_split(pci, t, "X");
1448 case __BLK_TA_BOUNCE:
1449 log_generic(pci, t, "B");
1451 case __BLK_TA_REMAP:
1452 log_generic(pci, t, "A");
1455 fprintf(stderr, "Bad fs action %x\n", t->action);
1460 static void dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
1461 struct per_dev_info *pdi)
1464 if (t->action & BLK_TC_ACT(BLK_TC_PC))
1465 dump_trace_pc(t, pci);
1467 dump_trace_fs(t, pdi, pci);
1471 pdi->first_reported_time = t->time;
1475 output_binary(t, sizeof(*t) + t->pdu_len);
1479 * print in a proper way, not too small and not too big. if more than
1480 * 1000,000K, turn into M and so on
1482 static char *size_cnv(char *dst, unsigned long long num, int in_kb)
1484 char suff[] = { '\0', 'K', 'M', 'G', 'P' };
1490 while (num > 1000 * 1000ULL && (i < sizeof(suff) - 1)) {
1495 sprintf(dst, "%'8Lu%c", num, suff[i]);
1499 static void dump_io_stats(struct per_dev_info *pdi, struct io_stats *ios,
1502 static char x[256], y[256];
1504 fprintf(ofp, "%s\n", msg);
1506 fprintf(ofp, " Reads Queued: %s, %siB\t", size_cnv(x, ios->qreads, 0), size_cnv(y, ios->qread_kb, 1));
1507 fprintf(ofp, " Writes Queued: %s, %siB\n", size_cnv(x, ios->qwrites, 0), size_cnv(y, ios->qwrite_kb, 1));
1509 fprintf(ofp, " Read Dispatches: %s, %siB\t", size_cnv(x, ios->ireads, 0), size_cnv(y, ios->iread_kb, 1));
1510 fprintf(ofp, " Write Dispatches: %s, %siB\n", size_cnv(x, ios->iwrites, 0), size_cnv(y, ios->iwrite_kb, 1));
1511 fprintf(ofp, " Reads Requeued: %s\t\t", size_cnv(x, ios->rrqueue, 0));
1512 fprintf(ofp, " Writes Requeued: %s\n", size_cnv(x, ios->wrqueue, 0));
1513 fprintf(ofp, " Reads Completed: %s, %siB\t", size_cnv(x, ios->creads, 0), size_cnv(y, ios->cread_kb, 1));
1514 fprintf(ofp, " Writes Completed: %s, %siB\n", size_cnv(x, ios->cwrites, 0), size_cnv(y, ios->cwrite_kb, 1));
1515 fprintf(ofp, " Read Merges: %'8lu%8c\t", ios->mreads, ' ');
1516 fprintf(ofp, " Write Merges: %'8lu\n", ios->mwrites);
1518 fprintf(ofp, " Read depth: %'8u%8c\t", pdi->max_depth[0], ' ');
1519 fprintf(ofp, " Write depth: %'8u\n", pdi->max_depth[1]);
1521 fprintf(ofp, " IO unplugs: %'8lu%8c\t", ios->io_unplugs, ' ');
1522 fprintf(ofp, " Timer unplugs: %'8lu\n", ios->timer_unplugs);
1525 static void dump_wait_stats(struct per_process_info *ppi)
1527 unsigned long rawait = ppi->longest_allocation_wait[0] / 1000;
1528 unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000;
1529 unsigned long rcwait = ppi->longest_completion_wait[0] / 1000;
1530 unsigned long wawait = ppi->longest_allocation_wait[1] / 1000;
1531 unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000;
1532 unsigned long wcwait = ppi->longest_completion_wait[1] / 1000;
1534 fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' ');
1535 fprintf(ofp, " Allocation wait: %'8lu\n", wawait);
1536 fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' ');
1537 fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait);
1538 fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' ');
1539 fprintf(ofp, " Completion wait: %'8lu\n", wcwait);
1542 static int ppi_name_compare(const void *p1, const void *p2)
1544 struct per_process_info *ppi1 = *((struct per_process_info **) p1);
1545 struct per_process_info *ppi2 = *((struct per_process_info **) p2);
1548 res = strverscmp(ppi1->ppm->comm, ppi2->ppm->comm);
1550 res = ppi1->ppm->pid > ppi2->ppm->pid;
1555 static void sort_process_list(void)
1557 struct per_process_info **ppis;
1558 struct per_process_info *ppi;
1561 ppis = malloc(ppi_list_entries * sizeof(struct per_process_info *));
1566 ppi = ppi->list_next;
1569 qsort(ppis, ppi_list_entries, sizeof(ppi), ppi_name_compare);
1571 i = ppi_list_entries - 1;
1576 ppi->list_next = ppi_list;
1584 static void show_process_stats(void)
1586 struct per_process_info *ppi;
1588 sort_process_list();
1592 struct process_pid_map *ppm = ppi->ppm;
1595 if (ppi->more_than_one)
1596 sprintf(name, "%s (%u, ...)", ppm->comm, ppm->pid);
1598 sprintf(name, "%s (%u)", ppm->comm, ppm->pid);
1600 dump_io_stats(NULL, &ppi->io_stats, name);
1601 dump_wait_stats(ppi);
1602 ppi = ppi->list_next;
1608 static void show_device_and_cpu_stats(void)
1610 struct per_dev_info *pdi;
1611 struct per_cpu_info *pci;
1612 struct io_stats total, *ios;
1613 unsigned long long rrate, wrate, msec;
1614 int i, j, pci_events;
1615 char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3];
1618 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
1620 memset(&total, 0, sizeof(total));
1626 for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) {
1630 ios = &pci->io_stats;
1631 total.qreads += ios->qreads;
1632 total.qwrites += ios->qwrites;
1633 total.creads += ios->creads;
1634 total.cwrites += ios->cwrites;
1635 total.mreads += ios->mreads;
1636 total.mwrites += ios->mwrites;
1637 total.ireads += ios->ireads;
1638 total.iwrites += ios->iwrites;
1639 total.rrqueue += ios->rrqueue;
1640 total.wrqueue += ios->wrqueue;
1641 total.qread_kb += ios->qread_kb;
1642 total.qwrite_kb += ios->qwrite_kb;
1643 total.cread_kb += ios->cread_kb;
1644 total.cwrite_kb += ios->cwrite_kb;
1645 total.iread_kb += ios->iread_kb;
1646 total.iwrite_kb += ios->iwrite_kb;
1647 total.timer_unplugs += ios->timer_unplugs;
1648 total.io_unplugs += ios->io_unplugs;
1650 snprintf(line, sizeof(line) - 1, "CPU%d (%s):",
1651 j, get_dev_name(pdi, name, sizeof(name)));
1652 dump_io_stats(pdi, ios, line);
1656 if (pci_events > 1) {
1658 snprintf(line, sizeof(line) - 1, "Total (%s):",
1659 get_dev_name(pdi, name, sizeof(name)));
1660 dump_io_stats(NULL, &total, line);
1664 msec = (pdi->last_reported_time - pdi->first_reported_time) / 1000000;
1666 rrate = 1000 * total.cread_kb / msec;
1667 wrate = 1000 * total.cwrite_kb / msec;
1670 fprintf(ofp, "\nThroughput (R/W): %'LuKiB/s / %'LuKiB/s\n",
1672 fprintf(ofp, "Events (%s): %'Lu entries\n",
1673 get_dev_name(pdi, line, sizeof(line)), pdi->events);
1675 collect_pdi_skips(pdi);
1676 fprintf(ofp, "Skips: %'lu forward (%'llu - %5.1lf%%)\n",
1677 pdi->skips,pdi->seq_skips,
1678 100.0 * ((double)pdi->seq_skips /
1679 (double)(pdi->events + pdi->seq_skips)));
1683 static void find_genesis(void)
1685 struct trace *t = trace_list;
1687 genesis_time = -1ULL;
1689 if (t->bit->time < genesis_time)
1690 genesis_time = t->bit->time;
1695 /* The time stamp record will usually be the first
1696 * record in the trace, but not always.
1699 && start_timestamp != genesis_time) {
1700 long delta = genesis_time - start_timestamp;
1702 abs_start_time.tv_sec += SECONDS(delta);
1703 abs_start_time.tv_nsec += NANO_SECONDS(delta);
1704 if (abs_start_time.tv_nsec < 0) {
1705 abs_start_time.tv_nsec += 1000000000;
1706 abs_start_time.tv_sec -= 1;
1708 if (abs_start_time.tv_nsec > 1000000000) {
1709 abs_start_time.tv_nsec -= 1000000000;
1710 abs_start_time.tv_sec += 1;
1715 static inline int check_stopwatch(struct blk_io_trace *bit)
1717 if (bit->time < stopwatch_end &&
1718 bit->time >= stopwatch_start)
1725 * return youngest entry read
1727 static int sort_entries(unsigned long long *youngest)
1729 struct per_dev_info *pdi = NULL;
1730 struct per_cpu_info *pci = NULL;
1737 while ((t = trace_list) != NULL) {
1738 struct blk_io_trace *bit = t->bit;
1740 trace_list = t->next;
1742 bit->time -= genesis_time;
1744 if (bit->time < *youngest || !*youngest)
1745 *youngest = bit->time;
1747 if (!pdi || pdi->dev != bit->device) {
1748 pdi = get_dev_info(bit->device);
1752 if (!pci || pci->cpu != bit->cpu)
1753 pci = get_cpu_info(pdi, bit->cpu);
1755 if (bit->sequence < pci->smallest_seq_read)
1756 pci->smallest_seq_read = bit->sequence;
1758 if (check_stopwatch(bit)) {
1764 if (trace_rb_insert_sort(t))
1772 * to continue, we must have traces from all online cpus in the tree
1774 static int check_cpu_map(struct per_dev_info *pdi)
1776 unsigned long *cpu_map;
1783 * create a map of the cpus we have traces for
1785 cpu_map = malloc(pdi->cpu_map_max / sizeof(long));
1786 n = rb_first(&rb_sort_root);
1788 __t = rb_entry(n, struct trace, rb_node);
1789 cpu = __t->bit->cpu;
1791 cpu_map[CPU_IDX(cpu)] |= (1UL << CPU_BIT(cpu));
1796 * we can't continue if pdi->cpu_map has entries set that we don't
1797 * have in the sort rbtree. the opposite is not a problem, though
1800 for (i = 0; i < pdi->cpu_map_max / CPUS_PER_LONG; i++) {
1801 if (pdi->cpu_map[i] & ~(cpu_map[i])) {
1811 static int check_sequence(struct per_dev_info *pdi, struct trace *t, int force)
1813 struct blk_io_trace *bit = t->bit;
1814 unsigned long expected_sequence;
1815 struct per_cpu_info *pci;
1818 pci = get_cpu_info(pdi, bit->cpu);
1819 expected_sequence = pci->last_sequence + 1;
1821 if (!expected_sequence) {
1823 * 1 should be the first entry, just allow it
1825 if (bit->sequence == 1)
1827 if (bit->sequence == pci->smallest_seq_read)
1830 return check_cpu_map(pdi);
1833 if (bit->sequence == expected_sequence)
1837 * we may not have seen that sequence yet. if we are not doing
1838 * the final run, break and wait for more entries.
1840 if (expected_sequence < pci->smallest_seq_read) {
1841 __t = trace_rb_find_last(pdi, pci, expected_sequence);
1845 __put_trace_last(pdi, __t);
1847 } else if (!force) {
1851 if (check_current_skips(pci, bit->sequence))
1854 if (expected_sequence < bit->sequence)
1855 insert_skip(pci, expected_sequence, bit->sequence - 1);
1860 static void show_entries_rb(int force)
1862 struct per_dev_info *pdi = NULL;
1863 struct per_cpu_info *pci = NULL;
1864 struct blk_io_trace *bit;
1868 while ((n = rb_first(&rb_sort_root)) != NULL) {
1869 if (is_done() && !force && !pipeline)
1872 t = rb_entry(n, struct trace, rb_node);
1875 if (read_sequence - t->read_sequence < 1 && !force)
1878 if (!pdi || pdi->dev != bit->device) {
1879 pdi = get_dev_info(bit->device);
1884 fprintf(stderr, "Unknown device ID? (%d,%d)\n",
1885 MAJOR(bit->device), MINOR(bit->device));
1889 if (check_sequence(pdi, t, force))
1892 if (!force && bit->time > last_allowed_time)
1895 check_time(pdi, bit);
1897 if (!pci || pci->cpu != bit->cpu)
1898 pci = get_cpu_info(pdi, bit->cpu);
1900 pci->last_sequence = bit->sequence;
1904 if (bit->action & (act_mask << BLK_TC_SHIFT))
1905 dump_trace(bit, pci, pdi);
1911 static int read_data(int fd, void *buffer, int bytes, int block, int *fdblock)
1913 int ret, bytes_left, fl;
1916 if (block != *fdblock) {
1917 fl = fcntl(fd, F_GETFL);
1921 fcntl(fd, F_SETFL, fl | O_NONBLOCK);
1924 fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
1930 while (bytes_left > 0) {
1931 ret = read(fd, p, bytes_left);
1935 if (errno != EAGAIN) {
1941 * never do partial reads. we can return if we
1942 * didn't read anything and we should not block,
1943 * otherwise wait for data
1945 if ((bytes_left == bytes) && !block)
1959 static inline __u16 get_pdulen(struct blk_io_trace *bit)
1962 return bit->pdu_len;
1964 return __bswap_16(bit->pdu_len);
1967 static inline __u32 get_magic(struct blk_io_trace *bit)
1972 return __bswap_32(bit->magic);
1975 static int read_events(int fd, int always_block, int *fdblock)
1977 struct per_dev_info *pdi = NULL;
1978 unsigned int events = 0;
1980 while (!is_done() && events < rb_batch) {
1981 struct blk_io_trace *bit;
1983 int pdu_len, should_block, ret;
1988 should_block = !events || always_block;
1990 ret = read_data(fd, bit, sizeof(*bit), should_block, fdblock);
1993 if (!events && ret < 0)
1999 * look at first trace to check whether we need to convert
2000 * data in the future
2002 if (data_is_native == -1 && check_data_endianness(bit->magic))
2005 magic = get_magic(bit);
2006 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
2007 fprintf(stderr, "Bad magic %x\n", magic);
2011 pdu_len = get_pdulen(bit);
2013 void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
2015 if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1, fdblock)) {
2025 if (verify_trace(bit)) {
2031 * not a real trace, so grab and handle it here
2033 if (bit->action & BLK_TC_ACT(BLK_TC_NOTIFY)) {
2035 output_binary(bit, sizeof(*bit) + bit->pdu_len);
2040 memset(t, 0, sizeof(*t));
2042 t->read_sequence = read_sequence;
2044 t->next = trace_list;
2047 if (!pdi || pdi->dev != bit->device)
2048 pdi = get_dev_info(bit->device);
2050 if (bit->time > pdi->last_read_time)
2051 pdi->last_read_time = bit->time;
2060 * Managing input streams
2064 struct ms_stream *next;
2065 struct trace *first, *last;
2066 struct per_dev_info *pdi;
2070 #define MS_HASH(d, c) ((MAJOR(d) & 0xff) ^ (MINOR(d) & 0xff) ^ (cpu & 0xff))
2072 struct ms_stream *ms_head;
2073 struct ms_stream *ms_hash[256];
2075 static void ms_sort(struct ms_stream *msp);
2076 static int ms_prime(struct ms_stream *msp);
2078 static inline struct trace *ms_peek(struct ms_stream *msp)
2080 return (msp == NULL) ? NULL : msp->first;
2083 static inline __u64 ms_peek_time(struct ms_stream *msp)
2085 return ms_peek(msp)->bit->time;
2088 static inline void ms_resort(struct ms_stream *msp)
2090 if (msp->next && ms_peek_time(msp) > ms_peek_time(msp->next)) {
2091 ms_head = msp->next;
2097 static inline void ms_deq(struct ms_stream *msp)
2099 msp->first = msp->first->next;
2102 if (!ms_prime(msp)) {
2103 ms_head = msp->next;
2112 static void ms_sort(struct ms_stream *msp)
2114 __u64 msp_t = ms_peek_time(msp);
2115 struct ms_stream *this_msp = ms_head;
2117 if (this_msp == NULL)
2119 else if (msp_t < ms_peek_time(this_msp)) {
2120 msp->next = this_msp;
2124 while (this_msp->next && ms_peek_time(this_msp->next) < msp_t)
2125 this_msp = this_msp->next;
2127 msp->next = this_msp->next;
2128 this_msp->next = msp;
2132 static int ms_prime(struct ms_stream *msp)
2137 struct per_dev_info *pdi = msp->pdi;
2138 struct per_cpu_info *pci = get_cpu_info(pdi, msp->cpu);
2139 struct blk_io_trace *bit = NULL;
2140 int ret, pdu_len, ndone = 0;
2142 for (i = 0; !is_done() && pci->fd >= 0 && i < rb_batch; i++) {
2144 ret = read_data(pci->fd, bit, sizeof(*bit), 1, &pci->fdblock);
2148 if (data_is_native == -1 && check_data_endianness(bit->magic))
2151 magic = get_magic(bit);
2152 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
2153 fprintf(stderr, "Bad magic %x\n", magic);
2158 pdu_len = get_pdulen(bit);
2160 void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
2161 ret = read_data(pci->fd, ptr + sizeof(*bit), pdu_len,
2172 if (verify_trace(bit))
2175 if (bit->action & BLK_TC_ACT(BLK_TC_NOTIFY)) {
2177 output_binary(bit, sizeof(*bit) + bit->pdu_len);
2184 if (bit->time > pdi->last_read_time)
2185 pdi->last_read_time = bit->time;
2188 memset(t, 0, sizeof(*t));
2191 if (msp->first == NULL)
2192 msp->first = msp->last = t;
2194 msp->last->next = t;
2204 if (bit) bit_free(bit);
2206 cpu_mark_offline(pdi, pci->cpu);
2213 static struct ms_stream *ms_alloc(struct per_dev_info *pdi, int cpu)
2215 struct ms_stream *msp = malloc(sizeof(*msp));
2218 msp->first = msp->last = NULL;
2228 static int setup_file(struct per_dev_info *pdi, int cpu)
2233 struct per_cpu_info *pci = get_cpu_info(pdi, cpu);
2238 p = strdup(pdi->name);
2240 if (strcmp(dname, ".")) {
2242 p = strdup(pdi->name);
2243 strcpy(pdi->name, basename(p));
2248 len = sprintf(pci->fname, "%s/", input_dir);
2250 snprintf(pci->fname + len, sizeof(pci->fname)-1-len,
2251 "%s.blktrace.%d", pdi->name, pci->cpu);
2252 if (stat(pci->fname, &st) < 0)
2257 pci->fd = open(pci->fname, O_RDONLY);
2263 printf("Input file %s added\n", pci->fname);
2264 cpu_mark_online(pdi, pci->cpu);
2267 ms_alloc(pdi, pci->cpu);
2272 static int handle(struct ms_stream *msp)
2275 struct per_dev_info *pdi;
2276 struct per_cpu_info *pci;
2277 struct blk_io_trace *bit;
2280 if (t->bit->time > stopwatch_end)
2285 pci = get_cpu_info(pdi, msp->cpu);
2288 bit->time -= genesis_time;
2289 pdi->last_reported_time = bit->time;
2290 if (bit->action & (act_mask << BLK_TC_SHIFT))
2291 dump_trace(bit, pci, pdi);
2296 trace_rb_insert_last(pdi, t);
2305 static int do_file(void)
2308 struct per_dev_info *pdi;
2311 * first prepare all files for reading
2313 for (i = 0; i < ndevices; i++) {
2315 for (cpu = 0; setup_file(pdi, cpu); cpu++)
2320 * Get the initial time stamp
2323 genesis_time = ms_peek_time(ms_head);
2326 * Keep processing traces while any are left
2328 while (!is_done() && ms_head && handle(ms_head))
2334 static int do_stdin(void)
2336 unsigned long long youngest;
2337 int fd, events, fdblock;
2339 last_allowed_time = -1ULL;
2340 fd = dup(STDIN_FILENO);
2342 perror("dup stdin");
2347 while ((events = read_events(fd, 0, &fdblock)) > 0) {
2351 smallest_seq_read = -1U;
2354 if (sort_entries(&youngest))
2357 if (youngest > stopwatch_end)
2363 if (rb_sort_entries)
2370 static void show_stats(void)
2379 if (per_process_stats)
2380 show_process_stats();
2382 if (per_device_and_cpu_stats)
2383 show_device_and_cpu_stats();
2388 static void handle_sigint(__attribute__((__unused__)) int sig)
2394 * Extract start and duration times from a string, allowing
2395 * us to specify a time interval of interest within a trace.
2396 * Format: "duration" (start is zero) or "start:duration".
2398 static int find_stopwatch_interval(char *string)
2403 value = strtod(string, &sp);
2405 fprintf(stderr,"Invalid stopwatch timer: %s\n", string);
2409 stopwatch_start = DOUBLE_TO_NANO_ULL(value);
2411 value = strtod(string, &sp);
2412 if (sp == string || *sp != '\0') {
2413 fprintf(stderr,"Invalid stopwatch duration time: %s\n",
2417 } else if (*sp != '\0') {
2418 fprintf(stderr,"Invalid stopwatch start timer: %s\n", string);
2421 stopwatch_end = DOUBLE_TO_NANO_ULL(value);
2422 if (stopwatch_end <= stopwatch_start) {
2423 fprintf(stderr, "Invalid stopwatch interval: %Lu -> %Lu\n",
2424 stopwatch_start, stopwatch_end);
2431 #define S_OPTS "a:A:b:D:d:f:F:hi:o:Oqstw:vV"
2432 static char usage_str[] = "\n\n" \
2433 "-i <file> | --input=<file>\n" \
2434 "[ -a <action field> | --act-mask=<action field> ]\n" \
2435 "[ -A <action mask> | --set-mask=<action mask> ]\n" \
2436 "[ -b <traces> | --batch=<traces> ]\n" \
2437 "[ -d <file> | --dump-binary=<file> ]\n" \
2438 "[ -D <dir> | --input-directory=<dir> ]\n" \
2439 "[ -f <format> | --format=<format> ]\n" \
2440 "[ -F <spec> | --format-spec=<spec> ]\n" \
2441 "[ -h | --hash-by-name ]\n" \
2442 "[ -o <file> | --output=<file> ]\n" \
2443 "[ -O | --no-text-output ]\n" \
2444 "[ -q | --quiet ]\n" \
2445 "[ -s | --per-program-stats ]\n" \
2446 "[ -t | --track-ios ]\n" \
2447 "[ -w <time> | --stopwatch=<time> ]\n" \
2448 "[ -v | --verbose ]\n" \
2449 "[ -V | --version ]\n\n" \
2450 "\t-b stdin read batching\n" \
2451 "\t-d Output file. If specified, binary data is written to file\n" \
2452 "\t-D Directory to prepend to input file names\n" \
2453 "\t-f Output format. Customize the output format. The format field\n" \
2454 "\t identifies can be found in the documentation\n" \
2455 "\t-F Format specification. Can be found in the documentation\n" \
2456 "\t-h Hash processes by name, not pid\n" \
2457 "\t-i Input file containing trace data, or '-' for stdin\n" \
2458 "\t-o Output file. If not given, output is stdout\n" \
2459 "\t-O Do NOT output text data\n" \
2460 "\t-q Quiet. Don't display any stats at the end of the trace\n" \
2461 "\t-s Show per-program io statistics\n" \
2462 "\t-t Track individual ios. Will tell you the time a request took\n" \
2463 "\t to get queued, to get dispatched, and to get completed\n" \
2464 "\t-w Only parse data between the given time interval in seconds.\n" \
2465 "\t If 'start' isn't given, blkparse defaults the start time to 0\n" \
2466 "\t-v More verbose for marginal errors\n" \
2467 "\t-V Print program version info\n\n";
2469 static void usage(char *prog)
2471 fprintf(stderr, "Usage: %s %s %s", prog, blkparse_version, usage_str);
2474 int main(int argc, char *argv[])
2476 int i, c, ret, mode;
2477 int act_mask_tmp = 0;
2478 char *ofp_buffer = NULL;
2479 char *bin_ofp_buffer = NULL;
2481 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
2484 i = find_mask_map(optarg);
2486 fprintf(stderr,"Invalid action mask %s\n",
2494 if ((sscanf(optarg, "%x", &i) != 1) ||
2495 !valid_act_opt(i)) {
2497 "Invalid set action mask %s/0x%x\n",
2504 if (!strcmp(optarg, "-") && !pipeline)
2506 else if (resize_devices(optarg) != 0)
2513 output_name = optarg;
2519 rb_batch = atoi(optarg);
2521 rb_batch = RB_BATCH_DEFAULT;
2524 per_process_stats = 1;
2530 per_device_and_cpu_stats = 0;
2533 if (find_stopwatch_interval(optarg) != 0)
2537 set_all_format_specs(optarg);
2540 if (add_format_spec(optarg) != 0)
2544 ppi_hash_by_pid = 0;
2550 printf("%s version %s\n", argv[0], blkparse_version);
2553 dump_binary = optarg;
2561 while (optind < argc) {
2562 if (!strcmp(argv[optind], "-") && !pipeline)
2564 else if (resize_devices(argv[optind]) != 0)
2569 if (!pipeline && !ndevices) {
2574 if (act_mask_tmp != 0)
2575 act_mask = act_mask_tmp;
2577 memset(&rb_sort_root, 0, sizeof(rb_sort_root));
2579 signal(SIGINT, handle_sigint);
2580 signal(SIGHUP, handle_sigint);
2581 signal(SIGTERM, handle_sigint);
2583 setlocale(LC_NUMERIC, "en_US");
2587 ofp = fdopen(STDOUT_FILENO, "w");
2592 snprintf(ofname, sizeof(ofname) - 1, "%s", output_name);
2593 ofp = fopen(ofname, "w");
2602 ofp_buffer = malloc(4096);
2603 if (setvbuf(ofp, ofp_buffer, mode, 4096)) {
2610 dump_fp = fopen(dump_binary, "w");
2612 perror(dump_binary);
2616 bin_ofp_buffer = malloc(128 * 1024);
2617 if (setvbuf(dump_fp, bin_ofp_buffer, _IOFBF, 128 * 1024)) {
2618 perror("setvbuf binary");
2633 if (bin_ofp_buffer) {
2635 free(bin_ofp_buffer);