2 * block queue tracing parse application
4 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <sys/types.h>
37 static char blkparse_version[] = "0.90";
44 unsigned long long events;
45 unsigned long long first_reported_time;
46 unsigned long long last_reported_time;
47 unsigned long long last_read_time;
48 struct io_stats io_stats;
49 unsigned long last_sequence;
52 struct rb_root rb_last;
53 unsigned long rb_last_entries;
55 struct rb_root rb_track;
60 unsigned long *cpu_map;
61 unsigned int cpu_map_max;
63 struct per_cpu_info *cpus;
66 struct per_process_info {
69 struct io_stats io_stats;
70 struct per_process_info *hash_next, *list_next;
76 unsigned long long longest_allocation_wait[2];
77 unsigned long long longest_dispatch_wait[2];
78 unsigned long long longest_completion_wait[2];
81 #define PPI_HASH_SHIFT (8)
82 #define PPI_HASH_SIZE (1 << PPI_HASH_SHIFT)
83 #define PPI_HASH_MASK (PPI_HASH_SIZE - 1)
84 static struct per_process_info *ppi_hash_table[PPI_HASH_SIZE];
85 static struct per_process_info *ppi_list;
86 static int ppi_list_entries;
88 #define S_OPTS "a:A:i:o:b:stqw:f:F:vVhD:"
89 static struct option l_opts[] = {
92 .has_arg = required_argument,
98 .has_arg = required_argument,
104 .has_arg = required_argument,
110 .has_arg = required_argument,
116 .has_arg = required_argument,
121 .name = "per-program-stats",
122 .has_arg = no_argument,
128 .has_arg = no_argument,
134 .has_arg = no_argument,
140 .has_arg = required_argument,
146 .has_arg = required_argument,
151 .name = "format-spec",
152 .has_arg = required_argument,
157 .name = "hash-by-name",
158 .has_arg = no_argument,
164 .has_arg = no_argument,
170 .has_arg = no_argument,
175 .name = "input-directory",
176 .has_arg = required_argument,
186 * for sorting the displayed output
189 struct blk_io_trace *bit;
190 struct rb_node rb_node;
194 static struct rb_root rb_sort_root;
195 static unsigned long rb_sort_entries;
197 static struct trace *trace_list;
202 static struct blk_io_trace *bit_alloc_list;
203 static struct trace *t_alloc_list;
206 * for tracking individual ios
209 struct rb_node rb_node;
214 unsigned long long allocation_time;
215 unsigned long long queue_time;
216 unsigned long long dispatch_time;
217 unsigned long long completion_time;
221 static struct per_dev_info *devices;
222 static char *get_dev_name(struct per_dev_info *, char *, int);
225 static char *output_name;
226 static char *input_dir;
228 static unsigned long long genesis_time;
229 static unsigned long long last_allowed_time;
230 static unsigned int smallest_seq_read;
231 static unsigned long long stopwatch_start; /* start from zero by default */
232 static unsigned long long stopwatch_end = -1ULL; /* "infinity" */
234 static int per_process_stats;
235 static int per_device_and_cpu_stats = 1;
236 static int track_ios;
237 static int ppi_hash_by_pid = 1;
239 static unsigned int act_mask = -1U;
240 static int stats_printed;
242 static unsigned int t_alloc_cache;
243 static unsigned int bit_alloc_cache;
245 #define RB_BATCH_DEFAULT (512)
246 static unsigned int rb_batch = RB_BATCH_DEFAULT;
250 #define is_done() (*(volatile int *)(&done))
251 static volatile int done;
253 #define JHASH_RANDOM (0x3af5f2ee)
255 #define CPUS_PER_LONG (8 * sizeof(unsigned long))
256 #define CPU_IDX(cpu) ((cpu) / CPUS_PER_LONG)
257 #define CPU_BIT(cpu) ((cpu) & (CPUS_PER_LONG - 1))
259 static void cpu_mark_online(struct per_dev_info *pdi, unsigned int cpu)
261 if (cpu >= pdi->cpu_map_max || !pdi->cpu_map) {
262 int new_max = (cpu + CPUS_PER_LONG) & ~(CPUS_PER_LONG - 1);
263 unsigned long *map = malloc(new_max / sizeof(long));
265 memset(map, 0, new_max / sizeof(long));
268 memcpy(map, pdi->cpu_map, pdi->cpu_map_max / sizeof(long));
273 pdi->cpu_map_max = new_max;
276 pdi->cpu_map[CPU_IDX(cpu)] |= (1UL << CPU_BIT(cpu));
279 static inline void cpu_mark_offline(struct per_dev_info *pdi, int cpu)
281 pdi->cpu_map[CPU_IDX(cpu)] &= ~(1UL << CPU_BIT(cpu));
284 static inline int cpu_is_online(struct per_dev_info *pdi, int cpu)
286 return (pdi->cpu_map[CPU_IDX(cpu)] & (1UL << CPU_BIT(cpu))) != 0;
289 static inline int ppi_hash_pid(__u32 pid)
291 return jhash_1word(pid, JHASH_RANDOM) & PPI_HASH_MASK;
294 static inline int ppi_hash_name(const char *name)
296 return jhash(name, 16, JHASH_RANDOM) & PPI_HASH_MASK;
299 static inline int ppi_hash(struct per_process_info *ppi)
302 return ppi_hash_pid(ppi->pid);
304 return ppi_hash_name(ppi->name);
307 static inline void add_process_to_hash(struct per_process_info *ppi)
309 const int hash_idx = ppi_hash(ppi);
311 ppi->hash_next = ppi_hash_table[hash_idx];
312 ppi_hash_table[hash_idx] = ppi;
315 static inline void add_process_to_list(struct per_process_info *ppi)
317 ppi->list_next = ppi_list;
322 static struct per_process_info *find_process_by_name(char *name)
324 const int hash_idx = ppi_hash_name(name);
325 struct per_process_info *ppi;
327 ppi = ppi_hash_table[hash_idx];
329 if (!strcmp(ppi->name, name))
332 ppi = ppi->hash_next;
338 static struct per_process_info *find_process_by_pid(__u32 pid)
340 const int hash_idx = ppi_hash_pid(pid);
341 struct per_process_info *ppi;
343 ppi = ppi_hash_table[hash_idx];
348 ppi = ppi->hash_next;
354 static struct per_process_info *find_process(__u32 pid, char *name)
356 struct per_process_info *ppi;
359 return find_process_by_pid(pid);
361 ppi = find_process_by_name(name);
362 if (ppi && ppi->pid != pid)
363 ppi->more_than_one = 1;
368 static inline int trace_rb_insert(struct trace *t, struct rb_root *root,
371 struct rb_node **p = &root->rb_node;
372 struct rb_node *parent = NULL;
378 __t = rb_entry(parent, struct trace, rb_node);
381 if (t->bit->time < __t->bit->time) {
384 } else if (t->bit->time > __t->bit->time) {
389 if (t->bit->device < __t->bit->device)
391 else if (t->bit->device > __t->bit->device)
393 else if (t->bit->sequence < __t->bit->sequence)
395 else /* >= sequence */
399 rb_link_node(&t->rb_node, parent, p);
400 rb_insert_color(&t->rb_node, root);
404 static inline int trace_rb_insert_sort(struct trace *t)
406 if (!trace_rb_insert(t, &rb_sort_root, 1)) {
414 static inline int trace_rb_insert_last(struct per_dev_info *pdi,struct trace *t)
416 if (!trace_rb_insert(t, &pdi->rb_last, 1)) {
417 pdi->rb_last_entries++;
424 static struct trace *trace_rb_find(dev_t device, unsigned long sequence,
425 struct rb_root *root, int order)
427 struct rb_node *n = root->rb_node;
428 struct rb_node *prev = NULL;
432 __t = rb_entry(n, struct trace, rb_node);
435 if (device < __t->bit->device)
437 else if (device > __t->bit->device)
439 else if (sequence < __t->bit->sequence)
441 else if (sequence > __t->bit->sequence)
448 * hack - the list may not be sequence ordered because some
449 * events don't have sequence and time matched. so we end up
450 * being a little off in the rb lookup here, because we don't
451 * know the time we are looking for. compensate by browsing
452 * a little ahead from the last entry to find the match
457 while (((n = rb_next(prev)) != NULL) && max--) {
458 __t = rb_entry(n, struct trace, rb_node);
460 if (__t->bit->device == device &&
461 __t->bit->sequence == sequence)
471 static inline struct trace *trace_rb_find_sort(dev_t dev, unsigned long seq)
473 return trace_rb_find(dev, seq, &rb_sort_root, 1);
476 static inline struct trace *trace_rb_find_last(struct per_dev_info *pdi,
479 return trace_rb_find(pdi->dev, seq, &pdi->rb_last, 0);
482 static inline int track_rb_insert(struct per_dev_info *pdi,struct io_track *iot)
484 struct rb_node **p = &pdi->rb_track.rb_node;
485 struct rb_node *parent = NULL;
486 struct io_track *__iot;
490 __iot = rb_entry(parent, struct io_track, rb_node);
492 if (iot->sector < __iot->sector)
494 else if (iot->sector > __iot->sector)
498 "sector alias (%Lu) on device %d,%d!\n",
499 (unsigned long long) iot->sector,
500 MAJOR(pdi->dev), MINOR(pdi->dev));
505 rb_link_node(&iot->rb_node, parent, p);
506 rb_insert_color(&iot->rb_node, &pdi->rb_track);
510 static struct io_track *__find_track(struct per_dev_info *pdi, __u64 sector)
512 struct rb_node *n = pdi->rb_track.rb_node;
513 struct io_track *__iot;
516 __iot = rb_entry(n, struct io_track, rb_node);
518 if (sector < __iot->sector)
520 else if (sector > __iot->sector)
529 static struct io_track *find_track(struct per_dev_info *pdi, __u32 pid,
530 char *comm, __u64 sector)
532 struct io_track *iot;
534 iot = __find_track(pdi, sector);
536 iot = malloc(sizeof(*iot));
538 memcpy(iot->comm, comm, sizeof(iot->comm));
539 iot->sector = sector;
540 track_rb_insert(pdi, iot);
546 static void log_track_frontmerge(struct per_dev_info *pdi,
547 struct blk_io_trace *t)
549 struct io_track *iot;
554 iot = __find_track(pdi, t->sector + t_sec(t));
557 fprintf(stderr, "merge not found for (%d,%d): %llu\n",
558 MAJOR(pdi->dev), MINOR(pdi->dev),
559 (unsigned long long) t->sector + t_sec(t));
563 rb_erase(&iot->rb_node, &pdi->rb_track);
564 iot->sector -= t_sec(t);
565 track_rb_insert(pdi, iot);
568 static void log_track_getrq(struct per_dev_info *pdi, struct blk_io_trace *t)
570 struct io_track *iot;
575 iot = find_track(pdi, t->pid, t->comm, t->sector);
576 iot->allocation_time = t->time;
580 * return time between rq allocation and insertion
582 static unsigned long long log_track_insert(struct per_dev_info *pdi,
583 struct blk_io_trace *t)
585 unsigned long long elapsed;
586 struct io_track *iot;
591 iot = find_track(pdi, t->pid, t->comm, t->sector);
592 iot->queue_time = t->time;
594 if (!iot->allocation_time)
597 elapsed = iot->queue_time - iot->allocation_time;
599 if (per_process_stats) {
600 struct per_process_info *ppi = find_process(iot->pid,iot->comm);
601 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
603 if (ppi && elapsed > ppi->longest_allocation_wait[w])
604 ppi->longest_allocation_wait[w] = elapsed;
611 * return time between queue and issue
613 static unsigned long long log_track_issue(struct per_dev_info *pdi,
614 struct blk_io_trace *t)
616 unsigned long long elapsed;
617 struct io_track *iot;
621 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
624 iot = __find_track(pdi, t->sector);
627 fprintf(stderr, "issue not found for (%d,%d): %llu\n",
628 MAJOR(pdi->dev), MINOR(pdi->dev),
629 (unsigned long long) t->sector);
633 iot->dispatch_time = t->time;
634 elapsed = iot->dispatch_time - iot->queue_time;
636 if (per_process_stats) {
637 struct per_process_info *ppi = find_process(iot->pid,iot->comm);
638 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
640 if (ppi && elapsed > ppi->longest_dispatch_wait[w])
641 ppi->longest_dispatch_wait[w] = elapsed;
648 * return time between dispatch and complete
650 static unsigned long long log_track_complete(struct per_dev_info *pdi,
651 struct blk_io_trace *t)
653 unsigned long long elapsed;
654 struct io_track *iot;
658 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
661 iot = __find_track(pdi, t->sector);
664 fprintf(stderr,"complete not found for (%d,%d): %llu\n",
665 MAJOR(pdi->dev), MINOR(pdi->dev),
666 (unsigned long long) t->sector);
670 iot->completion_time = t->time;
671 elapsed = iot->completion_time - iot->dispatch_time;
673 if (per_process_stats) {
674 struct per_process_info *ppi = find_process(iot->pid,iot->comm);
675 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
677 if (ppi && elapsed > ppi->longest_completion_wait[w])
678 ppi->longest_completion_wait[w] = elapsed;
682 * kill the trace, we don't need it after completion
684 rb_erase(&iot->rb_node, &pdi->rb_track);
691 static struct io_stats *find_process_io_stats(__u32 pid, char *name)
693 struct per_process_info *ppi = find_process(pid, name);
696 ppi = malloc(sizeof(*ppi));
697 memset(ppi, 0, sizeof(*ppi));
698 memcpy(ppi->name, name, 16);
700 add_process_to_hash(ppi);
701 add_process_to_list(ppi);
704 return &ppi->io_stats;
707 static void resize_cpu_info(struct per_dev_info *pdi, int cpu)
709 struct per_cpu_info *cpus = pdi->cpus;
710 int ncpus = pdi->ncpus;
711 int new_count = cpu + 1;
715 size = new_count * sizeof(struct per_cpu_info);
716 cpus = realloc(cpus, size);
719 fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n",
720 get_dev_name(pdi, name, sizeof(name)), size);
724 new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info));
725 new_space = (new_count - ncpus) * sizeof(struct per_cpu_info);
726 memset(new_start, 0, new_space);
728 pdi->ncpus = new_count;
732 static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu)
734 struct per_cpu_info *pci;
736 if (cpu >= pdi->ncpus)
737 resize_cpu_info(pdi, cpu);
739 pci = &pdi->cpus[cpu];
745 static int resize_devices(char *name)
747 int size = (ndevices + 1) * sizeof(struct per_dev_info);
749 devices = realloc(devices, size);
751 fprintf(stderr, "Out of memory, device %s (%d)\n", name, size);
754 memset(&devices[ndevices], 0, sizeof(struct per_dev_info));
755 devices[ndevices].name = name;
760 static struct per_dev_info *get_dev_info(dev_t dev)
762 struct per_dev_info *pdi;
765 for (i = 0; i < ndevices; i++) {
767 devices[i].dev = dev;
768 if (devices[i].dev == dev)
772 if (resize_devices(NULL))
775 pdi = &devices[ndevices - 1];
777 pdi->first_reported_time = 0;
778 pdi->last_sequence = -1;
779 pdi->last_read_time = 0;
780 memset(&pdi->rb_last, 0, sizeof(pdi->rb_last));
781 pdi->rb_last_entries = 0;
785 static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size)
788 snprintf(buffer, size, "%s", pdi->name);
790 snprintf(buffer, size, "%d,%d",MAJOR(pdi->dev),MINOR(pdi->dev));
794 static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit)
796 unsigned long long this = bit->time;
797 unsigned long long last = pdi->last_reported_time;
799 pdi->backwards = (this < last) ? 'B' : ' ';
800 pdi->last_reported_time = this;
803 static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t,
808 ios->qwrite_kb += t_kb(t);
811 ios->qread_kb += t_kb(t);
815 static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci,
818 __account_m(&pci->io_stats, t, rw);
820 if (per_process_stats) {
821 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
823 __account_m(ios, t, rw);
827 static inline void __account_queue(struct io_stats *ios, struct blk_io_trace *t,
832 ios->qwrite_kb += t_kb(t);
835 ios->qread_kb += t_kb(t);
839 static inline void account_queue(struct blk_io_trace *t,
840 struct per_cpu_info *pci, int rw)
842 __account_queue(&pci->io_stats, t, rw);
844 if (per_process_stats) {
845 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
847 __account_queue(ios, t, rw);
851 static inline void __account_c(struct io_stats *ios, int rw, unsigned int bytes)
855 ios->cwrite_kb += bytes >> 10;
858 ios->cread_kb += bytes >> 10;
862 static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci,
865 __account_c(&pci->io_stats, rw, bytes);
867 if (per_process_stats) {
868 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
870 __account_c(ios, rw, bytes);
874 static inline void __account_issue(struct io_stats *ios, int rw,
879 ios->iwrite_kb += bytes >> 10;
882 ios->iread_kb += bytes >> 10;
886 static inline void account_issue(struct blk_io_trace *t,
887 struct per_cpu_info *pci, int rw)
889 __account_issue(&pci->io_stats, rw, t->bytes);
891 if (per_process_stats) {
892 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
894 __account_issue(ios, rw, t->bytes);
898 static inline void __account_unplug(struct io_stats *ios, int timer)
901 ios->timer_unplugs++;
906 static inline void account_unplug(struct blk_io_trace *t,
907 struct per_cpu_info *pci, int timer)
909 __account_unplug(&pci->io_stats, timer);
911 if (per_process_stats) {
912 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
914 __account_unplug(ios, timer);
918 static void log_complete(struct per_dev_info *pdi, struct per_cpu_info *pci,
919 struct blk_io_trace *t, char *act)
921 process_fmt(act, pci, t, log_track_complete(pdi, t), 0, NULL);
924 static void log_insert(struct per_dev_info *pdi, struct per_cpu_info *pci,
925 struct blk_io_trace *t, char *act)
927 process_fmt(act, pci, t, log_track_insert(pdi, t), 0, NULL);
930 static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t,
933 process_fmt(act, pci, t, -1, 0, NULL);
936 static void log_issue(struct per_dev_info *pdi, struct per_cpu_info *pci,
937 struct blk_io_trace *t, char *act)
939 process_fmt(act, pci, t, log_track_issue(pdi, t), 0, NULL);
942 static void log_merge(struct per_dev_info *pdi, struct per_cpu_info *pci,
943 struct blk_io_trace *t, char *act)
946 log_track_frontmerge(pdi, t);
948 process_fmt(act, pci, t, -1ULL, 0, NULL);
951 static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t,
954 process_fmt(act, pci, t, -1ULL, 0, NULL);
957 static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t,
960 process_fmt(act, pci, t, -1ULL, 0, NULL);
963 static void log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t,
966 process_fmt(act, pci, t, -1ULL, 0, NULL);
969 static void log_split(struct per_cpu_info *pci, struct blk_io_trace *t,
972 process_fmt(act, pci, t, -1ULL, 0, NULL);
975 static void log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act)
977 unsigned char *buf = (unsigned char *) t + sizeof(*t);
979 process_fmt(act, pci, t, -1ULL, t->pdu_len, buf);
982 static void dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci)
984 int act = t->action & 0xffff;
988 log_generic(pci, t, "Q");
991 log_generic(pci, t, "G");
993 case __BLK_TA_SLEEPRQ:
994 log_generic(pci, t, "S");
996 case __BLK_TA_REQUEUE:
997 log_generic(pci, t, "R");
1000 log_pc(pci, t, "D");
1002 case __BLK_TA_COMPLETE:
1003 log_pc(pci, t, "C");
1005 case __BLK_TA_INSERT:
1006 log_pc(pci, t, "I");
1009 fprintf(stderr, "Bad pc action %x\n", act);
1014 static void dump_trace_fs(struct blk_io_trace *t, struct per_dev_info *pdi,
1015 struct per_cpu_info *pci)
1017 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
1018 int act = t->action & 0xffff;
1021 case __BLK_TA_QUEUE:
1022 account_queue(t, pci, w);
1023 log_queue(pci, t, "Q");
1025 case __BLK_TA_INSERT:
1026 log_insert(pdi, pci, t, "I");
1028 case __BLK_TA_BACKMERGE:
1029 account_m(t, pci, w);
1030 log_merge(pdi, pci, t, "M");
1032 case __BLK_TA_FRONTMERGE:
1033 account_m(t, pci, w);
1034 log_merge(pdi, pci, t, "F");
1036 case __BLK_TA_GETRQ:
1037 log_track_getrq(pdi, t);
1038 log_generic(pci, t, "G");
1040 case __BLK_TA_SLEEPRQ:
1041 log_generic(pci, t, "S");
1043 case __BLK_TA_REQUEUE:
1044 account_c(t, pci, w, -t->bytes);
1045 log_queue(pci, t, "R");
1047 case __BLK_TA_ISSUE:
1048 account_issue(t, pci, w);
1049 log_issue(pdi, pci, t, "D");
1051 case __BLK_TA_COMPLETE:
1052 account_c(t, pci, w, t->bytes);
1053 log_complete(pdi, pci, t, "C");
1056 log_action(pci, t, "P");
1058 case __BLK_TA_UNPLUG_IO:
1059 account_unplug(t, pci, 0);
1060 log_unplug(pci, t, "U");
1062 case __BLK_TA_UNPLUG_TIMER:
1063 account_unplug(t, pci, 1);
1064 log_unplug(pci, t, "UT");
1066 case __BLK_TA_SPLIT:
1067 log_split(pci, t, "X");
1069 case __BLK_TA_BOUNCE:
1070 log_generic(pci, t, "B");
1072 case __BLK_TA_REMAP:
1073 log_generic(pci, t, "A");
1076 fprintf(stderr, "Bad fs action %x\n", t->action);
1081 static void dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
1082 struct per_dev_info *pdi)
1084 if (t->action & BLK_TC_ACT(BLK_TC_PC))
1085 dump_trace_pc(t, pci);
1087 dump_trace_fs(t, pdi, pci);
1090 pdi->first_reported_time = t->time;
1096 * print in a proper way, not too small and not too big. if more than
1097 * 1000,000K, turn into M and so on
1099 static char *size_cnv(char *dst, unsigned long long num, int in_kb)
1101 char suff[] = { '\0', 'K', 'M', 'G', 'P' };
1107 while (num > 1000 * 1000ULL && (i < sizeof(suff) - 1)) {
1112 sprintf(dst, "%'8Lu%c", num, suff[i]);
1116 static void dump_io_stats(struct io_stats *ios, char *msg)
1118 static char x[256], y[256];
1120 fprintf(ofp, "%s\n", msg);
1122 fprintf(ofp, " Reads Queued: %s, %siB\t", size_cnv(x, ios->qreads, 0), size_cnv(y, ios->qread_kb, 1));
1123 fprintf(ofp, " Writes Queued: %s, %siB\n", size_cnv(x, ios->qwrites, 0), size_cnv(y, ios->qwrite_kb, 1));
1125 fprintf(ofp, " Read Dispatches: %s, %siB\t", size_cnv(x, ios->ireads, 0), size_cnv(y, ios->iread_kb, 1));
1126 fprintf(ofp, " Write Dispatches: %s, %siB\n", size_cnv(x, ios->iwrites, 0), size_cnv(y, ios->iwrite_kb, 1));
1127 fprintf(ofp, " Reads Completed: %s, %siB\t", size_cnv(x, ios->creads, 0), size_cnv(y, ios->cread_kb, 1));
1128 fprintf(ofp, " Writes Completed: %s, %siB\n", size_cnv(x, ios->cwrites, 0), size_cnv(y, ios->cwrite_kb, 1));
1129 fprintf(ofp, " Read Merges: %'8lu%8c\t", ios->mreads, ' ');
1130 fprintf(ofp, " Write Merges: %'8lu\n", ios->mwrites);
1131 fprintf(ofp, " IO unplugs: %'8lu%8c\t", ios->io_unplugs, ' ');
1132 fprintf(ofp, " Timer unplugs: %'8lu\n", ios->timer_unplugs);
1135 static void dump_wait_stats(struct per_process_info *ppi)
1137 unsigned long rawait = ppi->longest_allocation_wait[0] / 1000;
1138 unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000;
1139 unsigned long rcwait = ppi->longest_completion_wait[0] / 1000;
1140 unsigned long wawait = ppi->longest_allocation_wait[1] / 1000;
1141 unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000;
1142 unsigned long wcwait = ppi->longest_completion_wait[1] / 1000;
1144 fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' ');
1145 fprintf(ofp, " Allocation wait: %'8lu\n", wawait);
1146 fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' ');
1147 fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait);
1148 fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' ');
1149 fprintf(ofp, " Completion wait: %'8lu\n", wcwait);
1152 static int ppi_name_compare(const void *p1, const void *p2)
1154 struct per_process_info *ppi1 = *((struct per_process_info **) p1);
1155 struct per_process_info *ppi2 = *((struct per_process_info **) p2);
1158 res = strverscmp(ppi1->name, ppi2->name);
1160 res = ppi1->pid > ppi2->pid;
1165 static void sort_process_list(void)
1167 struct per_process_info **ppis;
1168 struct per_process_info *ppi;
1171 ppis = malloc(ppi_list_entries * sizeof(struct per_process_info *));
1176 ppi = ppi->list_next;
1179 qsort(ppis, ppi_list_entries, sizeof(ppi), ppi_name_compare);
1181 i = ppi_list_entries - 1;
1186 ppi->list_next = ppi_list;
1194 static void show_process_stats(void)
1196 struct per_process_info *ppi;
1198 sort_process_list();
1204 if (ppi->more_than_one)
1205 sprintf(name, "%s (%u, ...)", ppi->name, ppi->pid);
1207 sprintf(name, "%s (%u)", ppi->name, ppi->pid);
1209 dump_io_stats(&ppi->io_stats, name);
1210 dump_wait_stats(ppi);
1211 ppi = ppi->list_next;
1217 static void show_device_and_cpu_stats(void)
1219 struct per_dev_info *pdi;
1220 struct per_cpu_info *pci;
1221 struct io_stats total, *ios;
1222 unsigned long long rrate, wrate, msec;
1223 int i, j, pci_events;
1224 char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3];
1227 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
1229 memset(&total, 0, sizeof(total));
1235 for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) {
1239 ios = &pci->io_stats;
1240 total.qreads += ios->qreads;
1241 total.qwrites += ios->qwrites;
1242 total.creads += ios->creads;
1243 total.cwrites += ios->cwrites;
1244 total.mreads += ios->mreads;
1245 total.mwrites += ios->mwrites;
1246 total.ireads += ios->ireads;
1247 total.iwrites += ios->iwrites;
1248 total.qread_kb += ios->qread_kb;
1249 total.qwrite_kb += ios->qwrite_kb;
1250 total.cread_kb += ios->cread_kb;
1251 total.cwrite_kb += ios->cwrite_kb;
1252 total.iread_kb += ios->iread_kb;
1253 total.iwrite_kb += ios->iwrite_kb;
1254 total.timer_unplugs += ios->timer_unplugs;
1255 total.io_unplugs += ios->io_unplugs;
1257 snprintf(line, sizeof(line) - 1, "CPU%d (%s):",
1258 j, get_dev_name(pdi, name, sizeof(name)));
1259 dump_io_stats(ios, line);
1263 if (pci_events > 1) {
1265 snprintf(line, sizeof(line) - 1, "Total (%s):",
1266 get_dev_name(pdi, name, sizeof(name)));
1267 dump_io_stats(&total, line);
1271 msec = (pdi->last_reported_time - pdi->first_reported_time) / 1000000;
1273 rrate = 1000 * total.cread_kb / msec;
1274 wrate = 1000 * total.cwrite_kb / msec;
1277 fprintf(ofp, "\nThroughput (R/W): %'LuKiB/s / %'LuKiB/s\n", rrate, wrate);
1278 fprintf(ofp, "Events (%s): %'Lu entries, %'lu skips\n",
1279 get_dev_name(pdi, line, sizeof(line)), pdi->events,
1285 * struct trace and blktrace allocation cache, we do potentially
1286 * millions of mallocs for these structures while only using at most
1287 * a few thousand at the time
1289 static inline void t_free(struct trace *t)
1291 if (t_alloc_cache < 1024) {
1292 t->next = t_alloc_list;
1299 static inline struct trace *t_alloc(void)
1301 struct trace *t = t_alloc_list;
1304 t_alloc_list = t->next;
1309 return malloc(sizeof(*t));
1312 static inline void bit_free(struct blk_io_trace *bit)
1314 if (bit_alloc_cache < 1024 && !bit->pdu_len) {
1316 * abuse a 64-bit field for a next pointer for the free item
1318 bit->time = (__u64) (unsigned long) bit_alloc_list;
1319 bit_alloc_list = (struct blk_io_trace *) bit;
1325 static inline struct blk_io_trace *bit_alloc(void)
1327 struct blk_io_trace *bit = bit_alloc_list;
1330 bit_alloc_list = (struct blk_io_trace *) (unsigned long) \
1336 return malloc(sizeof(*bit));
1339 static void find_genesis(void)
1341 struct trace *t = trace_list;
1343 genesis_time = -1ULL;
1345 if (t->bit->time < genesis_time)
1346 genesis_time = t->bit->time;
1352 static inline int check_stopwatch(struct blk_io_trace *bit)
1354 if (bit->time < stopwatch_end &&
1355 bit->time >= stopwatch_start)
1362 * return youngest entry read
1364 static int sort_entries(unsigned long long *youngest)
1372 while ((t = trace_list) != NULL) {
1373 struct blk_io_trace *bit = t->bit;
1375 trace_list = t->next;
1377 bit->time -= genesis_time;
1379 if (bit->time < *youngest || !*youngest)
1380 *youngest = bit->time;
1382 if (bit->sequence < smallest_seq_read)
1383 smallest_seq_read = bit->sequence;
1385 if (check_stopwatch(bit)) {
1391 if (trace_rb_insert_sort(t))
1398 static inline void __put_trace_last(struct per_dev_info *pdi, struct trace *t)
1400 rb_erase(&t->rb_node, &pdi->rb_last);
1401 pdi->rb_last_entries--;
1407 static void put_trace(struct per_dev_info *pdi, struct trace *t)
1409 rb_erase(&t->rb_node, &rb_sort_root);
1412 trace_rb_insert_last(pdi, t);
1414 if (pdi->rb_last_entries > rb_batch * pdi->nfiles) {
1415 struct rb_node *n = rb_first(&pdi->rb_last);
1417 t = rb_entry(n, struct trace, rb_node);
1418 __put_trace_last(pdi, t);
1423 * to continue, we must have traces from all online cpus in the tree
1425 static int check_cpu_map(struct per_dev_info *pdi)
1427 unsigned long *cpu_map;
1434 * create a map of the cpus we have traces for
1436 cpu_map = malloc(pdi->cpu_map_max / sizeof(long));
1437 n = rb_first(&rb_sort_root);
1439 __t = rb_entry(n, struct trace, rb_node);
1440 cpu = __t->bit->cpu;
1442 cpu_map[CPU_IDX(cpu)] |= (1UL << CPU_BIT(cpu));
1447 * we can't continue if pdi->cpu_map has entries set that we don't
1448 * have in the sort rbtree. the opposite is not a problem, though
1451 for (i = 0; i < pdi->cpu_map_max / CPUS_PER_LONG; i++) {
1452 if (pdi->cpu_map[i] & ~(cpu_map[i])) {
1462 static int check_sequence(struct per_dev_info *pdi, struct trace *t, int force)
1464 unsigned long expected_sequence = pdi->last_sequence + 1;
1465 struct blk_io_trace *bit = t->bit;
1468 if (!expected_sequence) {
1470 * 1 should be the first entry, just allow it
1472 if (bit->sequence == 1)
1475 return check_cpu_map(pdi);
1478 if (bit->sequence == expected_sequence)
1482 * we may not have seen that sequence yet. if we are not doing
1483 * the final run, break and wait for more entries.
1485 if (expected_sequence < smallest_seq_read) {
1486 __t = trace_rb_find_last(pdi, expected_sequence);
1490 __put_trace_last(pdi, __t);
1492 } else if (!force) {
1497 fprintf(stderr, "(%d,%d): skipping %lu -> %u\n",
1498 MAJOR(pdi->dev), MINOR(pdi->dev),
1499 pdi->last_sequence, bit->sequence);
1506 static void show_entries_rb(int force)
1508 struct per_dev_info *pdi = NULL;
1509 struct per_cpu_info *pci = NULL;
1510 struct blk_io_trace *bit;
1514 while ((n = rb_first(&rb_sort_root)) != NULL) {
1515 if (is_done() && !force && !pipeline)
1518 t = rb_entry(n, struct trace, rb_node);
1521 if (!pdi || pdi->dev != bit->device)
1522 pdi = get_dev_info(bit->device);
1525 fprintf(stderr, "Unknown device ID? (%d,%d)\n",
1526 MAJOR(bit->device), MINOR(bit->device));
1530 if (check_sequence(pdi, t, force))
1533 if (!force && bit->time > last_allowed_time)
1536 pdi->last_sequence = bit->sequence;
1538 check_time(pdi, bit);
1540 if (!pci || pci->cpu != bit->cpu)
1541 pci = get_cpu_info(pdi, bit->cpu);
1545 if (bit->action & (act_mask << BLK_TC_SHIFT))
1546 dump_trace(bit, pci, pdi);
1552 static int read_data(int fd, void *buffer, int bytes, int block)
1554 int ret, bytes_left, fl;
1557 fl = fcntl(fd, F_GETFL);
1560 fcntl(fd, F_SETFL, fl | O_NONBLOCK);
1562 fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
1566 while (bytes_left > 0) {
1567 ret = read(fd, p, bytes_left);
1571 if (errno != EAGAIN)
1584 static int read_events(int fd, int always_block)
1586 struct per_dev_info *pdi = NULL;
1587 unsigned int events = 0;
1589 while (!is_done() && events < rb_batch) {
1590 struct blk_io_trace *bit;
1597 if (read_data(fd, bit, sizeof(*bit), !events || always_block)) {
1602 magic = be32_to_cpu(bit->magic);
1603 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
1604 fprintf(stderr, "Bad magic %x\n", magic);
1608 pdu_len = be16_to_cpu(bit->pdu_len);
1610 void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
1612 if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1)) {
1622 if (verify_trace(bit)) {
1628 memset(t, 0, sizeof(*t));
1631 t->next = trace_list;
1634 if (!pdi || pdi->dev != bit->device)
1635 pdi = get_dev_info(bit->device);
1637 if (bit->time > pdi->last_read_time)
1638 pdi->last_read_time = bit->time;
1646 static int do_file(void)
1648 struct per_cpu_info *pci;
1649 struct per_dev_info *pdi;
1650 int i, j, events, events_added;
1653 * first prepare all files for reading
1655 for (i = 0; i < ndevices; i++) {
1658 pdi->last_sequence = -1;
1664 pci = get_cpu_info(pdi, j);
1669 len = sprintf(pci->fname, "%s/", input_dir);
1671 snprintf(pci->fname + len, sizeof(pci->fname)-1-len,
1672 "%s.blktrace.%d", pdi->name, pci->cpu);
1673 if (stat(pci->fname, &st) < 0)
1676 pci->fd = open(pci->fname, O_RDONLY);
1683 printf("Input file %s added\n", pci->fname);
1685 cpu_mark_online(pdi, pci->cpu);
1690 * now loop over the files reading in the data
1693 unsigned long long youngest;
1696 last_allowed_time = -1ULL;
1697 smallest_seq_read = -1U;
1699 for (i = 0; i < ndevices; i++) {
1702 for (j = 0; j < pdi->nfiles; j++) {
1704 pci = get_cpu_info(pdi, j);
1709 events = read_events(pci->fd, 1);
1711 cpu_mark_offline(pdi, pci->cpu);
1717 if (pdi->last_read_time < last_allowed_time)
1718 last_allowed_time = pdi->last_read_time;
1720 events_added += events;
1724 if (sort_entries(&youngest))
1727 if (youngest > stopwatch_end)
1732 } while (events_added);
1734 if (rb_sort_entries)
1740 static int do_stdin(void)
1742 unsigned long long youngest;
1745 last_allowed_time = -1ULL;
1746 fd = dup(STDIN_FILENO);
1748 perror("dup stdin");
1752 while ((events = read_events(fd, 0)) != 0) {
1754 smallest_seq_read = -1U;
1756 if (sort_entries(&youngest))
1759 if (youngest > stopwatch_end)
1765 if (rb_sort_entries)
1772 static void show_stats(void)
1781 if (per_process_stats)
1782 show_process_stats();
1784 if (per_device_and_cpu_stats)
1785 show_device_and_cpu_stats();
1790 static void handle_sigint(__attribute__((__unused__)) int sig)
1797 * Extract start and duration times from a string, allowing
1798 * us to specify a time interval of interest within a trace.
1799 * Format: "duration" (start is zero) or "start:duration".
1801 static int find_stopwatch_interval(char *string)
1806 value = strtod(string, &sp);
1808 fprintf(stderr,"Invalid stopwatch timer: %s\n", string);
1812 stopwatch_start = DOUBLE_TO_NANO_ULL(value);
1814 value = strtod(string, &sp);
1815 if (sp == string || *sp != '\0') {
1816 fprintf(stderr,"Invalid stopwatch duration time: %s\n",
1820 } else if (*sp != '\0') {
1821 fprintf(stderr,"Invalid stopwatch start timer: %s\n", string);
1824 stopwatch_end = DOUBLE_TO_NANO_ULL(value);
1825 if (stopwatch_end <= stopwatch_start) {
1826 fprintf(stderr, "Invalid stopwatch interval: %Lu -> %Lu\n",
1827 stopwatch_start, stopwatch_end);
1834 static char usage_str[] = \
1835 "[ -i <input name> ] [-o <output name> [ -s ] [ -t ] [ -q ]\n" \
1836 "[ -w start:stop ] [ -f output format ] [ -F format spec ] [ -v] \n\n" \
1837 "\t-i Input file containing trace data, or '-' for stdin\n" \
1838 "\t-D Directory to prepend to input file names\n" \
1839 "\t-o Output file. If not given, output is stdout\n" \
1840 "\t-b stdin read batching\n" \
1841 "\t-s Show per-program io statistics\n" \
1842 "\t-n Hash processes by name, not pid\n" \
1843 "\t-t Track individual ios. Will tell you the time a request took\n" \
1844 "\t to get queued, to get dispatched, and to get completed\n" \
1845 "\t-q Quiet. Don't display any stats at the end of the trace\n" \
1846 "\t-w Only parse data between the given time interval in seconds.\n" \
1847 "\t If 'start' isn't given, blkparse defaults the start time to 0\n" \
1848 "\t-f Output format. Customize the output format. The format field\n" \
1849 "\t identifies can be found in the documentation\n" \
1850 "\t-F Format specification. Can be found in the documentation\n" \
1851 "\t-v More verbose for marginal errors\n" \
1852 "\t-V Print program version info\n\n";
1854 static void usage(char *prog)
1856 fprintf(stderr, "Usage: %s %s %s", prog, blkparse_version, usage_str);
1859 int main(int argc, char *argv[])
1862 int i, c, ret, mode;
1863 int act_mask_tmp = 0;
1865 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
1868 i = find_mask_map(optarg);
1870 fprintf(stderr,"Invalid action mask %s\n",
1878 if ((sscanf(optarg, "%x", &i) != 1) ||
1879 !valid_act_opt(i)) {
1881 "Invalid set action mask %s/0x%x\n",
1888 if (!strcmp(optarg, "-") && !pipeline)
1890 else if (resize_devices(optarg) != 0)
1897 output_name = optarg;
1900 rb_batch = atoi(optarg);
1902 rb_batch = RB_BATCH_DEFAULT;
1905 per_process_stats = 1;
1911 per_device_and_cpu_stats = 0;
1914 if (find_stopwatch_interval(optarg) != 0)
1918 set_all_format_specs(optarg);
1921 if (add_format_spec(optarg) != 0)
1925 ppi_hash_by_pid = 0;
1931 printf("%s version %s\n", argv[0], blkparse_version);
1939 while (optind < argc) {
1940 if (!strcmp(argv[optind], "-") && !pipeline)
1942 else if (resize_devices(argv[optind]) != 0)
1947 if (!pipeline && !ndevices) {
1952 if (act_mask_tmp != 0)
1953 act_mask = act_mask_tmp;
1955 memset(&rb_sort_root, 0, sizeof(rb_sort_root));
1957 signal(SIGINT, handle_sigint);
1958 signal(SIGHUP, handle_sigint);
1959 signal(SIGTERM, handle_sigint);
1961 setlocale(LC_NUMERIC, "en_US");
1964 ofp = fdopen(STDOUT_FILENO, "w");
1969 snprintf(ofname, sizeof(ofname) - 1, "%s", output_name);
1970 ofp = fopen(ofname, "w");
1979 ofp_buffer = malloc(4096);
1980 if (setvbuf(ofp, ofp_buffer, mode, 4096)) {