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>
38 static char blkparse_version[] = "0.90";
45 unsigned long long events;
46 unsigned long long first_reported_time;
47 unsigned long long last_reported_time;
48 unsigned long long last_read_time;
49 struct io_stats io_stats;
50 unsigned long last_sequence;
53 struct rb_root rb_last;
54 unsigned long rb_last_entries;
56 struct rb_root rb_track;
61 unsigned long *cpu_map;
62 unsigned int cpu_map_max;
64 struct per_cpu_info *cpus;
67 struct per_process_info {
70 struct io_stats io_stats;
71 struct per_process_info *hash_next, *list_next;
77 unsigned long long longest_allocation_wait[2];
78 unsigned long long longest_dispatch_wait[2];
79 unsigned long long longest_completion_wait[2];
82 #define PPI_HASH_SHIFT (8)
83 #define PPI_HASH_SIZE (1 << PPI_HASH_SHIFT)
84 #define PPI_HASH_MASK (PPI_HASH_SIZE - 1)
85 static struct per_process_info *ppi_hash_table[PPI_HASH_SIZE];
86 static struct per_process_info *ppi_list;
87 static int ppi_list_entries;
89 #define S_OPTS "a:A:i:o:b:stqw:f:F:vVhD:"
90 static struct option l_opts[] = {
93 .has_arg = required_argument,
99 .has_arg = required_argument,
105 .has_arg = required_argument,
111 .has_arg = required_argument,
117 .has_arg = required_argument,
122 .name = "per-program-stats",
123 .has_arg = no_argument,
129 .has_arg = no_argument,
135 .has_arg = no_argument,
141 .has_arg = required_argument,
147 .has_arg = required_argument,
152 .name = "format-spec",
153 .has_arg = required_argument,
158 .name = "hash-by-name",
159 .has_arg = no_argument,
165 .has_arg = no_argument,
171 .has_arg = no_argument,
176 .name = "input-directory",
177 .has_arg = required_argument,
187 * for sorting the displayed output
190 struct blk_io_trace *bit;
191 struct rb_node rb_node;
195 static struct rb_root rb_sort_root;
196 static unsigned long rb_sort_entries;
198 static struct trace *trace_list;
203 static struct blk_io_trace *bit_alloc_list;
204 static struct trace *t_alloc_list;
207 * for tracking individual ios
210 struct rb_node rb_node;
215 unsigned long long allocation_time;
216 unsigned long long queue_time;
217 unsigned long long dispatch_time;
218 unsigned long long completion_time;
222 static struct per_dev_info *devices;
223 static char *get_dev_name(struct per_dev_info *, char *, int);
226 static char *output_name;
227 static char *input_dir;
229 static unsigned long long genesis_time;
230 static unsigned long long last_allowed_time;
231 static unsigned int smallest_seq_read;
232 static unsigned long long stopwatch_start; /* start from zero by default */
233 static unsigned long long stopwatch_end = ULONG_LONG_MAX; /* "infinity" */
235 static int per_process_stats;
236 static int per_device_and_cpu_stats = 1;
237 static int track_ios;
238 static int ppi_hash_by_pid = 1;
240 static unsigned int act_mask = -1U;
241 static int stats_printed;
243 static unsigned int t_alloc_cache;
244 static unsigned int bit_alloc_cache;
246 #define RB_BATCH_DEFAULT (512)
247 static unsigned int rb_batch = RB_BATCH_DEFAULT;
251 #define is_done() (*(volatile int *)(&done))
252 static volatile int done;
254 #define JHASH_RANDOM (0x3af5f2ee)
256 #define CPUS_PER_LONG (8 * sizeof(unsigned long))
257 #define CPU_IDX(cpu) ((cpu) / CPUS_PER_LONG)
258 #define CPU_BIT(cpu) ((cpu) & (CPUS_PER_LONG - 1))
260 static void cpu_mark_online(struct per_dev_info *pdi, unsigned int cpu)
262 if (cpu >= pdi->cpu_map_max || !pdi->cpu_map) {
263 int new_max = (cpu + CPUS_PER_LONG) & ~(CPUS_PER_LONG - 1);
264 unsigned long *map = malloc(new_max / sizeof(long));
266 memset(map, 0, new_max / sizeof(long));
269 memcpy(map, pdi->cpu_map, pdi->cpu_map_max / sizeof(long));
274 pdi->cpu_map_max = new_max;
277 pdi->cpu_map[CPU_IDX(cpu)] |= (1UL << CPU_BIT(cpu));
280 static inline void cpu_mark_offline(struct per_dev_info *pdi, int cpu)
282 pdi->cpu_map[CPU_IDX(cpu)] &= ~(1UL << CPU_BIT(cpu));
285 static inline int cpu_is_online(struct per_dev_info *pdi, int cpu)
287 return (pdi->cpu_map[CPU_IDX(cpu)] & (1UL << CPU_BIT(cpu))) != 0;
290 static inline int ppi_hash_pid(__u32 pid)
292 return jhash_1word(pid, JHASH_RANDOM) & PPI_HASH_MASK;
295 static inline int ppi_hash_name(const char *name)
297 return jhash(name, 16, JHASH_RANDOM) & PPI_HASH_MASK;
300 static inline int ppi_hash(struct per_process_info *ppi)
303 return ppi_hash_pid(ppi->pid);
305 return ppi_hash_name(ppi->name);
308 static inline void add_process_to_hash(struct per_process_info *ppi)
310 const int hash_idx = ppi_hash(ppi);
312 ppi->hash_next = ppi_hash_table[hash_idx];
313 ppi_hash_table[hash_idx] = ppi;
316 static inline void add_process_to_list(struct per_process_info *ppi)
318 ppi->list_next = ppi_list;
323 static struct per_process_info *find_process_by_name(char *name)
325 const int hash_idx = ppi_hash_name(name);
326 struct per_process_info *ppi;
328 ppi = ppi_hash_table[hash_idx];
330 if (!strcmp(ppi->name, name))
333 ppi = ppi->hash_next;
339 static struct per_process_info *find_process_by_pid(__u32 pid)
341 const int hash_idx = ppi_hash_pid(pid);
342 struct per_process_info *ppi;
344 ppi = ppi_hash_table[hash_idx];
349 ppi = ppi->hash_next;
355 static struct per_process_info *find_process(__u32 pid, char *name)
357 struct per_process_info *ppi;
360 return find_process_by_pid(pid);
362 ppi = find_process_by_name(name);
363 if (ppi && ppi->pid != pid)
364 ppi->more_than_one = 1;
369 static inline int trace_rb_insert(struct trace *t, struct rb_root *root,
372 struct rb_node **p = &root->rb_node;
373 struct rb_node *parent = NULL;
379 __t = rb_entry(parent, struct trace, rb_node);
382 if (t->bit->time < __t->bit->time) {
385 } else if (t->bit->time > __t->bit->time) {
390 if (t->bit->device < __t->bit->device)
392 else if (t->bit->device > __t->bit->device)
394 else if (t->bit->sequence < __t->bit->sequence)
396 else /* >= sequence */
400 rb_link_node(&t->rb_node, parent, p);
401 rb_insert_color(&t->rb_node, root);
405 static inline int trace_rb_insert_sort(struct trace *t)
407 if (!trace_rb_insert(t, &rb_sort_root, 1)) {
415 static inline int trace_rb_insert_last(struct per_dev_info *pdi,struct trace *t)
417 if (!trace_rb_insert(t, &pdi->rb_last, 1)) {
418 pdi->rb_last_entries++;
425 static struct trace *trace_rb_find(dev_t device, unsigned long sequence,
426 struct rb_root *root, int order)
428 struct rb_node *n = root->rb_node;
429 struct rb_node *prev = NULL;
433 __t = rb_entry(n, struct trace, rb_node);
436 if (device < __t->bit->device)
438 else if (device > __t->bit->device)
440 else if (sequence < __t->bit->sequence)
442 else if (sequence > __t->bit->sequence)
449 * hack - the list may not be sequence ordered because some
450 * events don't have sequence and time matched. so we end up
451 * being a little off in the rb lookup here, because we don't
452 * know the time we are looking for. compensate by browsing
453 * a little ahead from the last entry to find the match
458 while (((n = rb_next(prev)) != NULL) && max--) {
459 __t = rb_entry(n, struct trace, rb_node);
461 if (__t->bit->device == device &&
462 __t->bit->sequence == sequence)
472 static inline struct trace *trace_rb_find_sort(dev_t dev, unsigned long seq)
474 return trace_rb_find(dev, seq, &rb_sort_root, 1);
477 static inline struct trace *trace_rb_find_last(struct per_dev_info *pdi,
480 return trace_rb_find(pdi->dev, seq, &pdi->rb_last, 0);
483 static inline int track_rb_insert(struct per_dev_info *pdi,struct io_track *iot)
485 struct rb_node **p = &pdi->rb_track.rb_node;
486 struct rb_node *parent = NULL;
487 struct io_track *__iot;
491 __iot = rb_entry(parent, struct io_track, rb_node);
493 if (iot->sector < __iot->sector)
495 else if (iot->sector > __iot->sector)
499 "sector alias (%Lu) on device %d,%d!\n",
500 (unsigned long long) iot->sector,
501 MAJOR(pdi->dev), MINOR(pdi->dev));
506 rb_link_node(&iot->rb_node, parent, p);
507 rb_insert_color(&iot->rb_node, &pdi->rb_track);
511 static struct io_track *__find_track(struct per_dev_info *pdi, __u64 sector)
513 struct rb_node *n = pdi->rb_track.rb_node;
514 struct io_track *__iot;
517 __iot = rb_entry(n, struct io_track, rb_node);
519 if (sector < __iot->sector)
521 else if (sector > __iot->sector)
530 static struct io_track *find_track(struct per_dev_info *pdi, __u32 pid,
531 char *comm, __u64 sector)
533 struct io_track *iot;
535 iot = __find_track(pdi, sector);
537 iot = malloc(sizeof(*iot));
539 memcpy(iot->comm, comm, sizeof(iot->comm));
540 iot->sector = sector;
541 track_rb_insert(pdi, iot);
547 static void log_track_frontmerge(struct per_dev_info *pdi,
548 struct blk_io_trace *t)
550 struct io_track *iot;
555 iot = __find_track(pdi, t->sector + t_sec(t));
558 fprintf(stderr, "merge not found for (%d,%d): %llu\n",
559 MAJOR(pdi->dev), MINOR(pdi->dev),
560 (unsigned long long) t->sector + t_sec(t));
564 rb_erase(&iot->rb_node, &pdi->rb_track);
565 iot->sector -= t_sec(t);
566 track_rb_insert(pdi, iot);
569 static void log_track_getrq(struct per_dev_info *pdi, struct blk_io_trace *t)
571 struct io_track *iot;
576 iot = find_track(pdi, t->pid, t->comm, t->sector);
577 iot->allocation_time = t->time;
581 * return time between rq allocation and insertion
583 static unsigned long long log_track_insert(struct per_dev_info *pdi,
584 struct blk_io_trace *t)
586 unsigned long long elapsed;
587 struct io_track *iot;
592 iot = find_track(pdi, t->pid, t->comm, t->sector);
593 iot->queue_time = t->time;
595 if (!iot->allocation_time)
598 elapsed = iot->queue_time - iot->allocation_time;
600 if (per_process_stats) {
601 struct per_process_info *ppi = find_process(iot->pid,iot->comm);
602 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
604 if (ppi && elapsed > ppi->longest_allocation_wait[w])
605 ppi->longest_allocation_wait[w] = elapsed;
612 * return time between queue and issue
614 static unsigned long long log_track_issue(struct per_dev_info *pdi,
615 struct blk_io_trace *t)
617 unsigned long long elapsed;
618 struct io_track *iot;
622 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
625 iot = __find_track(pdi, t->sector);
628 fprintf(stderr, "issue not found for (%d,%d): %llu\n",
629 MAJOR(pdi->dev), MINOR(pdi->dev),
630 (unsigned long long) t->sector);
634 iot->dispatch_time = t->time;
635 elapsed = iot->dispatch_time - iot->queue_time;
637 if (per_process_stats) {
638 struct per_process_info *ppi = find_process(iot->pid,iot->comm);
639 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
641 if (ppi && elapsed > ppi->longest_dispatch_wait[w])
642 ppi->longest_dispatch_wait[w] = elapsed;
649 * return time between dispatch and complete
651 static unsigned long long log_track_complete(struct per_dev_info *pdi,
652 struct blk_io_trace *t)
654 unsigned long long elapsed;
655 struct io_track *iot;
659 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
662 iot = __find_track(pdi, t->sector);
665 fprintf(stderr,"complete not found for (%d,%d): %llu\n",
666 MAJOR(pdi->dev), MINOR(pdi->dev),
667 (unsigned long long) t->sector);
671 iot->completion_time = t->time;
672 elapsed = iot->completion_time - iot->dispatch_time;
674 if (per_process_stats) {
675 struct per_process_info *ppi = find_process(iot->pid,iot->comm);
676 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
678 if (ppi && elapsed > ppi->longest_completion_wait[w])
679 ppi->longest_completion_wait[w] = elapsed;
683 * kill the trace, we don't need it after completion
685 rb_erase(&iot->rb_node, &pdi->rb_track);
692 static struct io_stats *find_process_io_stats(__u32 pid, char *name)
694 struct per_process_info *ppi = find_process(pid, name);
697 ppi = malloc(sizeof(*ppi));
698 memset(ppi, 0, sizeof(*ppi));
699 memcpy(ppi->name, name, 16);
701 add_process_to_hash(ppi);
702 add_process_to_list(ppi);
705 return &ppi->io_stats;
708 static void resize_cpu_info(struct per_dev_info *pdi, int cpu)
710 struct per_cpu_info *cpus = pdi->cpus;
711 int ncpus = pdi->ncpus;
712 int new_count = cpu + 1;
716 size = new_count * sizeof(struct per_cpu_info);
717 cpus = realloc(cpus, size);
720 fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n",
721 get_dev_name(pdi, name, sizeof(name)), size);
725 new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info));
726 new_space = (new_count - ncpus) * sizeof(struct per_cpu_info);
727 memset(new_start, 0, new_space);
729 pdi->ncpus = new_count;
733 static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu)
735 struct per_cpu_info *pci;
737 if (cpu >= pdi->ncpus)
738 resize_cpu_info(pdi, cpu);
740 pci = &pdi->cpus[cpu];
746 static int resize_devices(char *name)
748 int size = (ndevices + 1) * sizeof(struct per_dev_info);
750 devices = realloc(devices, size);
752 fprintf(stderr, "Out of memory, device %s (%d)\n", name, size);
755 memset(&devices[ndevices], 0, sizeof(struct per_dev_info));
756 devices[ndevices].name = name;
761 static struct per_dev_info *get_dev_info(dev_t dev)
763 struct per_dev_info *pdi;
766 for (i = 0; i < ndevices; i++) {
768 devices[i].dev = dev;
769 if (devices[i].dev == dev)
773 if (resize_devices(NULL))
776 pdi = &devices[ndevices - 1];
778 pdi->first_reported_time = 0;
779 pdi->last_sequence = -1;
780 pdi->last_read_time = 0;
781 memset(&pdi->rb_last, 0, sizeof(pdi->rb_last));
782 pdi->rb_last_entries = 0;
786 static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size)
789 snprintf(buffer, size, "%s", pdi->name);
791 snprintf(buffer, size, "%d,%d",MAJOR(pdi->dev),MINOR(pdi->dev));
795 static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit)
797 unsigned long long this = bit->time;
798 unsigned long long last = pdi->last_reported_time;
800 pdi->backwards = (this < last) ? 'B' : ' ';
801 pdi->last_reported_time = this;
804 static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t,
809 ios->qwrite_kb += t_kb(t);
812 ios->qread_kb += t_kb(t);
816 static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci,
819 __account_m(&pci->io_stats, t, rw);
821 if (per_process_stats) {
822 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
824 __account_m(ios, t, rw);
828 static inline void __account_queue(struct io_stats *ios, struct blk_io_trace *t,
833 ios->qwrite_kb += t_kb(t);
836 ios->qread_kb += t_kb(t);
840 static inline void account_queue(struct blk_io_trace *t,
841 struct per_cpu_info *pci, int rw)
843 __account_queue(&pci->io_stats, t, rw);
845 if (per_process_stats) {
846 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
848 __account_queue(ios, t, rw);
852 static inline void __account_c(struct io_stats *ios, int rw, unsigned int bytes)
856 ios->cwrite_kb += bytes >> 10;
859 ios->cread_kb += bytes >> 10;
863 static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci,
866 __account_c(&pci->io_stats, rw, bytes);
868 if (per_process_stats) {
869 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
871 __account_c(ios, rw, bytes);
875 static inline void __account_issue(struct io_stats *ios, int rw,
880 ios->iwrite_kb += bytes >> 10;
883 ios->iread_kb += bytes >> 10;
887 static inline void account_issue(struct blk_io_trace *t,
888 struct per_cpu_info *pci, int rw)
890 __account_issue(&pci->io_stats, rw, t->bytes);
892 if (per_process_stats) {
893 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
895 __account_issue(ios, rw, t->bytes);
899 static inline void __account_unplug(struct io_stats *ios, int timer)
902 ios->timer_unplugs++;
907 static inline void account_unplug(struct blk_io_trace *t,
908 struct per_cpu_info *pci, int timer)
910 __account_unplug(&pci->io_stats, timer);
912 if (per_process_stats) {
913 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
915 __account_unplug(ios, timer);
919 static void log_complete(struct per_dev_info *pdi, struct per_cpu_info *pci,
920 struct blk_io_trace *t, char *act)
922 process_fmt(act, pci, t, log_track_complete(pdi, t), 0, NULL);
925 static void log_insert(struct per_dev_info *pdi, struct per_cpu_info *pci,
926 struct blk_io_trace *t, char *act)
928 process_fmt(act, pci, t, log_track_insert(pdi, t), 0, NULL);
931 static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t,
934 process_fmt(act, pci, t, -1, 0, NULL);
937 static void log_issue(struct per_dev_info *pdi, struct per_cpu_info *pci,
938 struct blk_io_trace *t, char *act)
940 process_fmt(act, pci, t, log_track_issue(pdi, t), 0, NULL);
943 static void log_merge(struct per_dev_info *pdi, struct per_cpu_info *pci,
944 struct blk_io_trace *t, char *act)
947 log_track_frontmerge(pdi, t);
949 process_fmt(act, pci, t, -1ULL, 0, NULL);
952 static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t,
955 process_fmt(act, pci, t, -1ULL, 0, NULL);
958 static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t,
961 process_fmt(act, pci, t, -1ULL, 0, NULL);
964 static void log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t,
967 process_fmt(act, pci, t, -1ULL, 0, NULL);
970 static void log_split(struct per_cpu_info *pci, struct blk_io_trace *t,
973 process_fmt(act, pci, t, -1ULL, 0, NULL);
976 static void log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act)
978 unsigned char *buf = (unsigned char *) t + sizeof(*t);
980 process_fmt(act, pci, t, -1ULL, t->pdu_len, buf);
983 static void dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci)
985 int act = t->action & 0xffff;
989 log_generic(pci, t, "Q");
992 log_generic(pci, t, "G");
994 case __BLK_TA_SLEEPRQ:
995 log_generic(pci, t, "S");
997 case __BLK_TA_REQUEUE:
998 log_generic(pci, t, "R");
1000 case __BLK_TA_ISSUE:
1001 log_pc(pci, t, "D");
1003 case __BLK_TA_COMPLETE:
1004 log_pc(pci, t, "C");
1006 case __BLK_TA_INSERT:
1007 log_pc(pci, t, "I");
1010 fprintf(stderr, "Bad pc action %x\n", act);
1015 static void dump_trace_fs(struct blk_io_trace *t, struct per_dev_info *pdi,
1016 struct per_cpu_info *pci)
1018 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
1019 int act = t->action & 0xffff;
1022 case __BLK_TA_QUEUE:
1023 account_queue(t, pci, w);
1024 log_queue(pci, t, "Q");
1026 case __BLK_TA_INSERT:
1027 log_insert(pdi, pci, t, "I");
1029 case __BLK_TA_BACKMERGE:
1030 account_m(t, pci, w);
1031 log_merge(pdi, pci, t, "M");
1033 case __BLK_TA_FRONTMERGE:
1034 account_m(t, pci, w);
1035 log_merge(pdi, pci, t, "F");
1037 case __BLK_TA_GETRQ:
1038 log_track_getrq(pdi, t);
1039 log_generic(pci, t, "G");
1041 case __BLK_TA_SLEEPRQ:
1042 log_generic(pci, t, "S");
1044 case __BLK_TA_REQUEUE:
1045 account_c(t, pci, w, -t->bytes);
1046 log_queue(pci, t, "R");
1048 case __BLK_TA_ISSUE:
1049 account_issue(t, pci, w);
1050 log_issue(pdi, pci, t, "D");
1052 case __BLK_TA_COMPLETE:
1053 account_c(t, pci, w, t->bytes);
1054 log_complete(pdi, pci, t, "C");
1057 log_action(pci, t, "P");
1059 case __BLK_TA_UNPLUG_IO:
1060 account_unplug(t, pci, 0);
1061 log_unplug(pci, t, "U");
1063 case __BLK_TA_UNPLUG_TIMER:
1064 account_unplug(t, pci, 1);
1065 log_unplug(pci, t, "UT");
1067 case __BLK_TA_SPLIT:
1068 log_split(pci, t, "X");
1070 case __BLK_TA_BOUNCE:
1071 log_generic(pci, t, "B");
1073 case __BLK_TA_REMAP:
1074 log_generic(pci, t, "A");
1077 fprintf(stderr, "Bad fs action %x\n", t->action);
1082 static void dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
1083 struct per_dev_info *pdi)
1085 if (t->action & BLK_TC_ACT(BLK_TC_PC))
1086 dump_trace_pc(t, pci);
1088 dump_trace_fs(t, pdi, pci);
1091 pdi->first_reported_time = t->time;
1097 * print in a proper way, not too small and not too big. if more than
1098 * 1000,000K, turn into M and so on
1100 static char *size_cnv(char *dst, unsigned long long num, int in_kb)
1102 char suff[] = { '\0', 'K', 'M', 'G', 'P' };
1108 while (num > 1000 * 1000ULL && (i < sizeof(suff) - 1)) {
1113 sprintf(dst, "%'8Lu%c", num, suff[i]);
1117 static void dump_io_stats(struct io_stats *ios, char *msg)
1119 static char x[256], y[256];
1121 fprintf(ofp, "%s\n", msg);
1123 fprintf(ofp, " Reads Queued: %s, %siB\t", size_cnv(x, ios->qreads, 0), size_cnv(y, ios->qread_kb, 1));
1124 fprintf(ofp, " Writes Queued: %s, %siB\n", size_cnv(x, ios->qwrites, 0), size_cnv(y, ios->qwrite_kb, 1));
1126 fprintf(ofp, " Read Dispatches: %s, %siB\t", size_cnv(x, ios->ireads, 0), size_cnv(y, ios->iread_kb, 1));
1127 fprintf(ofp, " Write Dispatches: %s, %siB\n", size_cnv(x, ios->iwrites, 0), size_cnv(y, ios->iwrite_kb, 1));
1128 fprintf(ofp, " Reads Completed: %s, %siB\t", size_cnv(x, ios->creads, 0), size_cnv(y, ios->cread_kb, 1));
1129 fprintf(ofp, " Writes Completed: %s, %siB\n", size_cnv(x, ios->cwrites, 0), size_cnv(y, ios->cwrite_kb, 1));
1130 fprintf(ofp, " Read Merges: %'8lu%8c\t", ios->mreads, ' ');
1131 fprintf(ofp, " Write Merges: %'8lu\n", ios->mwrites);
1132 fprintf(ofp, " IO unplugs: %'8lu%8c\t", ios->io_unplugs, ' ');
1133 fprintf(ofp, " Timer unplugs: %'8lu\n", ios->timer_unplugs);
1136 static void dump_wait_stats(struct per_process_info *ppi)
1138 unsigned long rawait = ppi->longest_allocation_wait[0] / 1000;
1139 unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000;
1140 unsigned long rcwait = ppi->longest_completion_wait[0] / 1000;
1141 unsigned long wawait = ppi->longest_allocation_wait[1] / 1000;
1142 unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000;
1143 unsigned long wcwait = ppi->longest_completion_wait[1] / 1000;
1145 fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' ');
1146 fprintf(ofp, " Allocation wait: %'8lu\n", wawait);
1147 fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' ');
1148 fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait);
1149 fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' ');
1150 fprintf(ofp, " Completion wait: %'8lu\n", wcwait);
1153 static int ppi_name_compare(const void *p1, const void *p2)
1155 struct per_process_info *ppi1 = *((struct per_process_info **) p1);
1156 struct per_process_info *ppi2 = *((struct per_process_info **) p2);
1159 res = strverscmp(ppi1->name, ppi2->name);
1161 res = ppi1->pid > ppi2->pid;
1166 static void sort_process_list(void)
1168 struct per_process_info **ppis;
1169 struct per_process_info *ppi;
1172 ppis = malloc(ppi_list_entries * sizeof(struct per_process_info *));
1177 ppi = ppi->list_next;
1180 qsort(ppis, ppi_list_entries, sizeof(ppi), ppi_name_compare);
1182 i = ppi_list_entries - 1;
1187 ppi->list_next = ppi_list;
1195 static void show_process_stats(void)
1197 struct per_process_info *ppi;
1199 sort_process_list();
1205 if (ppi->more_than_one)
1206 sprintf(name, "%s (%u, ...)", ppi->name, ppi->pid);
1208 sprintf(name, "%s (%u)", ppi->name, ppi->pid);
1210 dump_io_stats(&ppi->io_stats, name);
1211 dump_wait_stats(ppi);
1212 ppi = ppi->list_next;
1218 static void show_device_and_cpu_stats(void)
1220 struct per_dev_info *pdi;
1221 struct per_cpu_info *pci;
1222 struct io_stats total, *ios;
1223 unsigned long long rrate, wrate, msec;
1224 int i, j, pci_events;
1225 char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3];
1228 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
1230 memset(&total, 0, sizeof(total));
1236 for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) {
1240 ios = &pci->io_stats;
1241 total.qreads += ios->qreads;
1242 total.qwrites += ios->qwrites;
1243 total.creads += ios->creads;
1244 total.cwrites += ios->cwrites;
1245 total.mreads += ios->mreads;
1246 total.mwrites += ios->mwrites;
1247 total.ireads += ios->ireads;
1248 total.iwrites += ios->iwrites;
1249 total.qread_kb += ios->qread_kb;
1250 total.qwrite_kb += ios->qwrite_kb;
1251 total.cread_kb += ios->cread_kb;
1252 total.cwrite_kb += ios->cwrite_kb;
1253 total.iread_kb += ios->iread_kb;
1254 total.iwrite_kb += ios->iwrite_kb;
1255 total.timer_unplugs += ios->timer_unplugs;
1256 total.io_unplugs += ios->io_unplugs;
1258 snprintf(line, sizeof(line) - 1, "CPU%d (%s):",
1259 j, get_dev_name(pdi, name, sizeof(name)));
1260 dump_io_stats(ios, line);
1264 if (pci_events > 1) {
1266 snprintf(line, sizeof(line) - 1, "Total (%s):",
1267 get_dev_name(pdi, name, sizeof(name)));
1268 dump_io_stats(&total, line);
1272 msec = (pdi->last_reported_time - pdi->first_reported_time) / 1000000;
1274 rrate = 1000 * total.cread_kb / msec;
1275 wrate = 1000 * total.cwrite_kb / msec;
1278 fprintf(ofp, "\nThroughput (R/W): %'LuKiB/s / %'LuKiB/s\n", rrate, wrate);
1279 fprintf(ofp, "Events (%s): %'Lu entries, %'lu skips\n",
1280 get_dev_name(pdi, line, sizeof(line)), pdi->events,
1286 * struct trace and blktrace allocation cache, we do potentially
1287 * millions of mallocs for these structures while only using at most
1288 * a few thousand at the time
1290 static inline void t_free(struct trace *t)
1292 if (t_alloc_cache < 1024) {
1293 t->next = t_alloc_list;
1300 static inline struct trace *t_alloc(void)
1302 struct trace *t = t_alloc_list;
1305 t_alloc_list = t->next;
1310 return malloc(sizeof(*t));
1313 static inline void bit_free(struct blk_io_trace *bit)
1315 if (bit_alloc_cache < 1024 && !bit->pdu_len) {
1317 * abuse a 64-bit field for a next pointer for the free item
1319 bit->time = (__u64) (unsigned long) bit_alloc_list;
1320 bit_alloc_list = (struct blk_io_trace *) bit;
1326 static inline struct blk_io_trace *bit_alloc(void)
1328 struct blk_io_trace *bit = bit_alloc_list;
1331 bit_alloc_list = (struct blk_io_trace *) (unsigned long) \
1337 return malloc(sizeof(*bit));
1340 static void find_genesis(void)
1342 struct trace *t = trace_list;
1344 genesis_time = -1ULL;
1346 if (t->bit->time < genesis_time)
1347 genesis_time = t->bit->time;
1353 static inline int check_stopwatch(struct blk_io_trace *bit)
1355 if (bit->time < stopwatch_end &&
1356 bit->time >= stopwatch_start)
1363 * return youngest entry read
1365 static int sort_entries(unsigned long long *youngest)
1373 while ((t = trace_list) != NULL) {
1374 struct blk_io_trace *bit = t->bit;
1376 trace_list = t->next;
1378 bit->time -= genesis_time;
1380 if (bit->time < *youngest || !*youngest)
1381 *youngest = bit->time;
1383 if (bit->sequence < smallest_seq_read)
1384 smallest_seq_read = bit->sequence;
1386 if (check_stopwatch(bit)) {
1392 if (trace_rb_insert_sort(t))
1399 static inline void __put_trace_last(struct per_dev_info *pdi, struct trace *t)
1401 rb_erase(&t->rb_node, &pdi->rb_last);
1402 pdi->rb_last_entries--;
1408 static void put_trace(struct per_dev_info *pdi, struct trace *t)
1410 rb_erase(&t->rb_node, &rb_sort_root);
1413 trace_rb_insert_last(pdi, t);
1415 if (pdi->rb_last_entries > rb_batch * pdi->nfiles) {
1416 struct rb_node *n = rb_first(&pdi->rb_last);
1418 t = rb_entry(n, struct trace, rb_node);
1419 __put_trace_last(pdi, t);
1424 * to continue, we must have traces from all online cpus in the tree
1426 static int check_cpu_map(struct per_dev_info *pdi)
1428 unsigned long *cpu_map;
1435 * create a map of the cpus we have traces for
1437 cpu_map = malloc(pdi->cpu_map_max / sizeof(long));
1438 n = rb_first(&rb_sort_root);
1440 __t = rb_entry(n, struct trace, rb_node);
1441 cpu = __t->bit->cpu;
1443 cpu_map[CPU_IDX(cpu)] |= (1UL << CPU_BIT(cpu));
1448 * we can't continue if pdi->cpu_map has entries set that we don't
1449 * have in the sort rbtree. the opposite is not a problem, though
1452 for (i = 0; i < pdi->cpu_map_max / CPUS_PER_LONG; i++) {
1453 if (pdi->cpu_map[i] & ~(cpu_map[i])) {
1463 static int check_sequence(struct per_dev_info *pdi, struct trace *t, int force)
1465 unsigned long expected_sequence = pdi->last_sequence + 1;
1466 struct blk_io_trace *bit = t->bit;
1469 if (!expected_sequence) {
1471 * 1 should be the first entry, just allow it
1473 if (bit->sequence == 1)
1476 return check_cpu_map(pdi);
1479 if (bit->sequence == expected_sequence)
1483 * we may not have seen that sequence yet. if we are not doing
1484 * the final run, break and wait for more entries.
1486 if (expected_sequence < smallest_seq_read) {
1487 __t = trace_rb_find_last(pdi, expected_sequence);
1491 __put_trace_last(pdi, __t);
1493 } else if (!force) {
1498 fprintf(stderr, "(%d,%d): skipping %lu -> %u\n",
1499 MAJOR(pdi->dev), MINOR(pdi->dev),
1500 pdi->last_sequence, bit->sequence);
1507 static void show_entries_rb(int force)
1509 struct per_dev_info *pdi = NULL;
1510 struct per_cpu_info *pci = NULL;
1511 struct blk_io_trace *bit;
1515 while ((n = rb_first(&rb_sort_root)) != NULL) {
1516 if (is_done() && !force && !pipeline)
1519 t = rb_entry(n, struct trace, rb_node);
1522 if (!pdi || pdi->dev != bit->device)
1523 pdi = get_dev_info(bit->device);
1526 fprintf(stderr, "Unknown device ID? (%d,%d)\n",
1527 MAJOR(bit->device), MINOR(bit->device));
1531 if (check_sequence(pdi, t, force))
1534 if (!force && bit->time > last_allowed_time)
1537 pdi->last_sequence = bit->sequence;
1539 check_time(pdi, bit);
1541 if (!pci || pci->cpu != bit->cpu)
1542 pci = get_cpu_info(pdi, bit->cpu);
1546 if (bit->action & (act_mask << BLK_TC_SHIFT))
1547 dump_trace(bit, pci, pdi);
1553 static int read_data(int fd, void *buffer, int bytes, int block)
1555 int ret, bytes_left, fl;
1558 fl = fcntl(fd, F_GETFL);
1561 fcntl(fd, F_SETFL, fl | O_NONBLOCK);
1563 fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
1567 while (bytes_left > 0) {
1568 ret = read(fd, p, bytes_left);
1572 if (errno != EAGAIN)
1585 static int read_events(int fd, int always_block)
1587 struct per_dev_info *pdi = NULL;
1588 unsigned int events = 0;
1590 while (!is_done() && events < rb_batch) {
1591 struct blk_io_trace *bit;
1598 if (read_data(fd, bit, sizeof(*bit), !events || always_block)) {
1603 magic = be32_to_cpu(bit->magic);
1604 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
1605 fprintf(stderr, "Bad magic %x\n", magic);
1609 pdu_len = be16_to_cpu(bit->pdu_len);
1611 void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
1613 if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1)) {
1623 if (verify_trace(bit)) {
1629 memset(t, 0, sizeof(*t));
1632 t->next = trace_list;
1635 if (!pdi || pdi->dev != bit->device)
1636 pdi = get_dev_info(bit->device);
1638 if (bit->time > pdi->last_read_time)
1639 pdi->last_read_time = bit->time;
1647 static int do_file(void)
1649 struct per_cpu_info *pci;
1650 struct per_dev_info *pdi;
1651 int i, j, events, events_added;
1654 * first prepare all files for reading
1656 for (i = 0; i < ndevices; i++) {
1659 pdi->last_sequence = -1;
1665 pci = get_cpu_info(pdi, j);
1670 len = sprintf(pci->fname, "%s/", input_dir);
1672 snprintf(pci->fname + len, sizeof(pci->fname)-1-len,
1673 "%s.blktrace.%d", pdi->name, pci->cpu);
1674 if (stat(pci->fname, &st) < 0)
1677 pci->fd = open(pci->fname, O_RDONLY);
1684 printf("Input file %s added\n", pci->fname);
1686 cpu_mark_online(pdi, pci->cpu);
1691 * now loop over the files reading in the data
1694 unsigned long long youngest;
1697 last_allowed_time = -1ULL;
1698 smallest_seq_read = -1U;
1700 for (i = 0; i < ndevices; i++) {
1703 for (j = 0; j < pdi->nfiles; j++) {
1705 pci = get_cpu_info(pdi, j);
1710 events = read_events(pci->fd, 1);
1712 cpu_mark_offline(pdi, pci->cpu);
1718 if (pdi->last_read_time < last_allowed_time)
1719 last_allowed_time = pdi->last_read_time;
1721 events_added += events;
1725 if (sort_entries(&youngest))
1728 if (youngest > stopwatch_end)
1733 } while (events_added);
1735 if (rb_sort_entries)
1741 static int do_stdin(void)
1743 unsigned long long youngest;
1746 last_allowed_time = -1ULL;
1747 fd = dup(STDIN_FILENO);
1749 perror("dup stdin");
1753 while ((events = read_events(fd, 0)) != 0) {
1755 smallest_seq_read = -1U;
1757 if (sort_entries(&youngest))
1760 if (youngest > stopwatch_end)
1766 if (rb_sort_entries)
1773 static void show_stats(void)
1782 if (per_process_stats)
1783 show_process_stats();
1785 if (per_device_and_cpu_stats)
1786 show_device_and_cpu_stats();
1791 static void handle_sigint(__attribute__((__unused__)) int sig)
1798 * Extract start and duration times from a string, allowing
1799 * us to specify a time interval of interest within a trace.
1800 * Format: "duration" (start is zero) or "start:duration".
1802 static int find_stopwatch_interval(char *string)
1807 value = strtod(string, &sp);
1809 fprintf(stderr,"Invalid stopwatch timer: %s\n", string);
1813 stopwatch_start = DOUBLE_TO_NANO_ULL(value);
1815 value = strtod(string, &sp);
1816 if (sp == string || *sp != '\0') {
1817 fprintf(stderr,"Invalid stopwatch duration time: %s\n",
1821 } else if (*sp != '\0') {
1822 fprintf(stderr,"Invalid stopwatch start timer: %s\n", string);
1825 stopwatch_end = DOUBLE_TO_NANO_ULL(value);
1826 if (stopwatch_end <= stopwatch_start) {
1827 fprintf(stderr, "Invalid stopwatch interval: %Lu -> %Lu\n",
1828 stopwatch_start, stopwatch_end);
1835 static char usage_str[] = \
1836 "[ -i <input name> ] [-o <output name> [ -s ] [ -t ] [ -q ]\n" \
1837 "[ -w start:stop ] [ -f output format ] [ -F format spec ] [ -v] \n\n" \
1838 "\t-i Input file containing trace data, or '-' for stdin\n" \
1839 "\t-D Directory to prepend to input file names\n" \
1840 "\t-o Output file. If not given, output is stdout\n" \
1841 "\t-b stdin read batching\n" \
1842 "\t-s Show per-program io statistics\n" \
1843 "\t-n Hash processes by name, not pid\n" \
1844 "\t-t Track individual ios. Will tell you the time a request took\n" \
1845 "\t to get queued, to get dispatched, and to get completed\n" \
1846 "\t-q Quiet. Don't display any stats at the end of the trace\n" \
1847 "\t-w Only parse data between the given time interval in seconds.\n" \
1848 "\t If 'start' isn't given, blkparse defaults the start time to 0\n" \
1849 "\t-f Output format. Customize the output format. The format field\n" \
1850 "\t identifies can be found in the documentation\n" \
1851 "\t-F Format specification. Can be found in the documentation\n" \
1852 "\t-v More verbose for marginal errors\n" \
1853 "\t-V Print program version info\n\n";
1855 static void usage(char *prog)
1857 fprintf(stderr, "Usage: %s %s %s", prog, blkparse_version, usage_str);
1860 int main(int argc, char *argv[])
1863 int i, c, ret, mode;
1864 int act_mask_tmp = 0;
1866 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
1869 i = find_mask_map(optarg);
1871 fprintf(stderr,"Invalid action mask %s\n",
1879 if ((sscanf(optarg, "%x", &i) != 1) ||
1880 !valid_act_opt(i)) {
1882 "Invalid set action mask %s/0x%x\n",
1889 if (!strcmp(optarg, "-") && !pipeline)
1891 else if (resize_devices(optarg) != 0)
1898 output_name = optarg;
1901 rb_batch = atoi(optarg);
1903 rb_batch = RB_BATCH_DEFAULT;
1906 per_process_stats = 1;
1912 per_device_and_cpu_stats = 0;
1915 if (find_stopwatch_interval(optarg) != 0)
1919 set_all_format_specs(optarg);
1922 if (add_format_spec(optarg) != 0)
1926 ppi_hash_by_pid = 0;
1932 printf("%s version %s\n", argv[0], blkparse_version);
1940 while (optind < argc) {
1941 if (!strcmp(argv[optind], "-") && !pipeline)
1943 else if (resize_devices(argv[optind]) != 0)
1948 if (!pipeline && !ndevices) {
1953 if (act_mask_tmp != 0)
1954 act_mask = act_mask_tmp;
1956 memset(&rb_sort_root, 0, sizeof(rb_sort_root));
1958 signal(SIGINT, handle_sigint);
1959 signal(SIGHUP, handle_sigint);
1960 signal(SIGTERM, handle_sigint);
1962 setlocale(LC_NUMERIC, "en_US");
1965 ofp = fdopen(STDOUT_FILENO, "w");
1970 snprintf(ofname, sizeof(ofname) - 1, "%s", output_name);
1971 ofp = fopen(ofname, "w");
1980 ofp_buffer = malloc(4096);
1981 if (setvbuf(ofp, ofp_buffer, mode, 4096)) {