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 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;
59 struct per_cpu_info *cpus;
62 struct per_process_info {
65 struct io_stats io_stats;
66 struct per_process_info *hash_next, *list_next;
72 unsigned long long longest_allocation_wait[2];
73 unsigned long long longest_dispatch_wait[2];
74 unsigned long long longest_completion_wait[2];
77 #define PPI_HASH_SHIFT (8)
78 #define PPI_HASH_SIZE (1 << PPI_HASH_SHIFT)
79 #define PPI_HASH_MASK (PPI_HASH_SIZE - 1)
80 static struct per_process_info *ppi_hash_table[PPI_HASH_SIZE];
81 static struct per_process_info *ppi_list;
82 static int ppi_list_entries;
84 #define S_OPTS "a:A:i:o:b:stqw:f:F:vnmD:"
85 static struct option l_opts[] = {
88 .has_arg = required_argument,
94 .has_arg = required_argument,
100 .has_arg = required_argument,
106 .has_arg = required_argument,
112 .has_arg = required_argument,
117 .name = "per-program-stats",
118 .has_arg = no_argument,
124 .has_arg = no_argument,
130 .has_arg = no_argument,
136 .has_arg = required_argument,
142 .has_arg = required_argument,
147 .name = "format-spec",
148 .has_arg = required_argument,
153 .name = "hash-by-name",
154 .has_arg = no_argument,
160 .has_arg = no_argument,
166 .has_arg = no_argument,
171 .name = "input-directory",
172 .has_arg = required_argument,
182 * for sorting the displayed output
185 struct blk_io_trace *bit;
186 struct rb_node rb_node;
190 static struct rb_root rb_sort_root;
191 static unsigned long rb_sort_entries;
193 static struct trace *trace_list;
198 static struct blk_io_trace *bit_alloc_list;
199 static struct trace *t_alloc_list;
202 * for tracking individual ios
205 struct rb_node rb_node;
210 unsigned long long allocation_time;
211 unsigned long long queue_time;
212 unsigned long long dispatch_time;
213 unsigned long long completion_time;
217 static struct per_dev_info *devices;
218 static char *get_dev_name(struct per_dev_info *, char *, int);
221 static char *output_name;
222 static char *input_dir;
224 static unsigned long long genesis_time;
225 static unsigned long long last_allowed_time;
226 static unsigned int smallest_seq_read;
227 static unsigned long long stopwatch_start; /* start from zero by default */
228 static unsigned long long stopwatch_end = ULONG_LONG_MAX; /* "infinity" */
230 static int per_process_stats;
231 static int track_ios;
232 static int ppi_hash_by_pid = 1;
233 static int print_missing;
234 static unsigned int act_mask = -1U;
236 static unsigned int t_alloc_cache;
237 static unsigned int bit_alloc_cache;
239 #define RB_BATCH_DEFAULT (512)
240 static unsigned int rb_batch = RB_BATCH_DEFAULT;
244 #define is_done() (*(volatile int *)(&done))
245 static volatile int done;
247 #define JHASH_RANDOM (0x3af5f2ee)
249 static inline int ppi_hash_pid(__u32 pid)
251 return jhash_1word(pid, JHASH_RANDOM) & PPI_HASH_MASK;
254 static inline int ppi_hash_name(const char *name)
256 return jhash(name, 16, JHASH_RANDOM) & PPI_HASH_MASK;
259 static inline int ppi_hash(struct per_process_info *ppi)
262 return ppi_hash_pid(ppi->pid);
264 return ppi_hash_name(ppi->name);
267 static inline void add_process_to_hash(struct per_process_info *ppi)
269 const int hash_idx = ppi_hash(ppi);
271 ppi->hash_next = ppi_hash_table[hash_idx];
272 ppi_hash_table[hash_idx] = ppi;
275 static inline void add_process_to_list(struct per_process_info *ppi)
277 ppi->list_next = ppi_list;
282 static struct per_process_info *find_process_by_name(char *name)
284 const int hash_idx = ppi_hash_name(name);
285 struct per_process_info *ppi;
287 ppi = ppi_hash_table[hash_idx];
289 if (!strcmp(ppi->name, name))
292 ppi = ppi->hash_next;
298 static struct per_process_info *find_process_by_pid(__u32 pid)
300 const int hash_idx = ppi_hash_pid(pid);
301 struct per_process_info *ppi;
303 ppi = ppi_hash_table[hash_idx];
308 ppi = ppi->hash_next;
314 static struct per_process_info *find_process(__u32 pid, char *name)
316 struct per_process_info *ppi;
319 return find_process_by_pid(pid);
321 ppi = find_process_by_name(name);
322 if (ppi && ppi->pid != pid)
323 ppi->more_than_one = 1;
328 static inline int trace_rb_insert(struct trace *t, struct rb_root *root,
331 struct rb_node **p = &root->rb_node;
332 struct rb_node *parent = NULL;
338 __t = rb_entry(parent, struct trace, rb_node);
341 if (t->bit->time < __t->bit->time) {
344 } else if (t->bit->time > __t->bit->time) {
349 if (t->bit->device < __t->bit->device)
351 else if (t->bit->device > __t->bit->device)
353 else if (t->bit->sequence < __t->bit->sequence)
355 else /* >= sequence */
359 rb_link_node(&t->rb_node, parent, p);
360 rb_insert_color(&t->rb_node, root);
364 static inline int trace_rb_insert_sort(struct trace *t)
366 if (!trace_rb_insert(t, &rb_sort_root, 1)) {
374 static inline int trace_rb_insert_last(struct per_dev_info *pdi,struct trace *t)
376 if (!trace_rb_insert(t, &pdi->rb_last, 1)) {
377 pdi->rb_last_entries++;
384 static struct trace *trace_rb_find(dev_t device, unsigned long sequence,
385 struct rb_root *root, int order)
387 struct rb_node *n = root->rb_node;
388 struct rb_node *prev = NULL;
392 __t = rb_entry(n, struct trace, rb_node);
395 if (device < __t->bit->device)
397 else if (device > __t->bit->device)
399 else if (sequence < __t->bit->sequence)
401 else if (sequence > __t->bit->sequence)
408 * hack - the list may not be sequence ordered because some
409 * events don't have sequence and time matched. so we end up
410 * being a little off in the rb lookup here, because we don't
411 * know the time we are looking for. compensate by browsing
412 * a little ahead from the last entry to find the match
417 while (((n = rb_next(prev)) != NULL) && max--) {
418 __t = rb_entry(n, struct trace, rb_node);
420 if (__t->bit->device == device &&
421 __t->bit->sequence == sequence)
431 static inline struct trace *trace_rb_find_sort(dev_t dev, unsigned long seq)
433 return trace_rb_find(dev, seq, &rb_sort_root, 1);
436 static inline struct trace *trace_rb_find_last(struct per_dev_info *pdi,
439 return trace_rb_find(pdi->dev, seq, &pdi->rb_last, 0);
442 static inline int track_rb_insert(struct per_dev_info *pdi,struct io_track *iot)
444 struct rb_node **p = &pdi->rb_track.rb_node;
445 struct rb_node *parent = NULL;
446 struct io_track *__iot;
450 __iot = rb_entry(parent, struct io_track, rb_node);
452 if (iot->sector < __iot->sector)
454 else if (iot->sector > __iot->sector)
458 "sector alias (%Lu) on device %d,%d!\n",
459 (unsigned long long) iot->sector,
460 MAJOR(pdi->dev), MINOR(pdi->dev));
465 rb_link_node(&iot->rb_node, parent, p);
466 rb_insert_color(&iot->rb_node, &pdi->rb_track);
470 static struct io_track *__find_track(struct per_dev_info *pdi, __u64 sector)
472 struct rb_node *n = pdi->rb_track.rb_node;
473 struct io_track *__iot;
476 __iot = rb_entry(n, struct io_track, rb_node);
478 if (sector < __iot->sector)
480 else if (sector > __iot->sector)
489 static struct io_track *find_track(struct per_dev_info *pdi, __u32 pid,
490 char *comm, __u64 sector)
492 struct io_track *iot;
494 iot = __find_track(pdi, sector);
496 iot = malloc(sizeof(*iot));
498 memcpy(iot->comm, comm, sizeof(iot->comm));
499 iot->sector = sector;
500 track_rb_insert(pdi, iot);
506 static void log_track_frontmerge(struct per_dev_info *pdi,
507 struct blk_io_trace *t)
509 struct io_track *iot;
514 iot = __find_track(pdi, t->sector + (t->bytes >> 9));
516 fprintf(stderr, "merge not found for (%d,%d): %llu\n",
517 MAJOR(pdi->dev), MINOR(pdi->dev),
518 (unsigned long long) t->sector + (t->bytes >> 9));
522 rb_erase(&iot->rb_node, &pdi->rb_track);
523 iot->sector -= t->bytes >> 9;
524 track_rb_insert(pdi, iot);
527 static void log_track_getrq(struct per_dev_info *pdi, struct blk_io_trace *t)
529 struct io_track *iot;
534 iot = find_track(pdi, t->pid, t->comm, t->sector);
535 iot->allocation_time = t->time;
539 * return time between rq allocation and insertion
541 static unsigned long long log_track_insert(struct per_dev_info *pdi,
542 struct blk_io_trace *t)
544 unsigned long long elapsed;
545 struct io_track *iot;
550 iot = find_track(pdi, t->pid, t->comm, t->sector);
551 iot->queue_time = t->time;
553 if (!iot->allocation_time)
556 elapsed = iot->queue_time - iot->allocation_time;
558 if (per_process_stats) {
559 struct per_process_info *ppi = find_process(iot->pid,iot->comm);
560 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
562 if (ppi && elapsed > ppi->longest_allocation_wait[w])
563 ppi->longest_allocation_wait[w] = elapsed;
570 * return time between queue and issue
572 static unsigned long long log_track_issue(struct per_dev_info *pdi,
573 struct blk_io_trace *t)
575 unsigned long long elapsed;
576 struct io_track *iot;
580 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
583 iot = __find_track(pdi, t->sector);
585 fprintf(stderr, "issue not found for (%d,%d): %llu\n",
586 MAJOR(pdi->dev), MINOR(pdi->dev),
587 (unsigned long long) t->sector);
591 iot->dispatch_time = t->time;
592 elapsed = iot->dispatch_time - iot->queue_time;
594 if (per_process_stats) {
595 struct per_process_info *ppi = find_process(iot->pid,iot->comm);
596 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
598 if (ppi && elapsed > ppi->longest_dispatch_wait[w])
599 ppi->longest_dispatch_wait[w] = elapsed;
606 * return time between dispatch and complete
608 static unsigned long long log_track_complete(struct per_dev_info *pdi,
609 struct blk_io_trace *t)
611 unsigned long long elapsed;
612 struct io_track *iot;
616 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
619 iot = __find_track(pdi, t->sector);
621 fprintf(stderr, "complete not found for (%d,%d): %llu\n",
622 MAJOR(pdi->dev), MINOR(pdi->dev),
623 (unsigned long long) t->sector);
627 iot->completion_time = t->time;
628 elapsed = iot->completion_time - iot->dispatch_time;
630 if (per_process_stats) {
631 struct per_process_info *ppi = find_process(iot->pid,iot->comm);
632 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
634 if (ppi && elapsed > ppi->longest_completion_wait[w])
635 ppi->longest_completion_wait[w] = elapsed;
639 * kill the trace, we don't need it after completion
641 rb_erase(&iot->rb_node, &pdi->rb_track);
648 static struct io_stats *find_process_io_stats(__u32 pid, char *name)
650 struct per_process_info *ppi = find_process(pid, name);
653 ppi = malloc(sizeof(*ppi));
654 memset(ppi, 0, sizeof(*ppi));
655 memcpy(ppi->name, name, 16);
657 add_process_to_hash(ppi);
658 add_process_to_list(ppi);
661 return &ppi->io_stats;
664 static void resize_cpu_info(struct per_dev_info *pdi, int cpu)
666 struct per_cpu_info *cpus = pdi->cpus;
667 int ncpus = pdi->ncpus;
668 int new_count = cpu + 1;
672 size = new_count * sizeof(struct per_cpu_info);
673 cpus = realloc(cpus, size);
676 fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n",
677 get_dev_name(pdi, name, sizeof(name)), size);
681 new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info));
682 new_space = (new_count - ncpus) * sizeof(struct per_cpu_info);
683 memset(new_start, 0, new_space);
685 pdi->ncpus = new_count;
689 static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu)
691 struct per_cpu_info *pci;
693 if (cpu >= pdi->ncpus)
694 resize_cpu_info(pdi, cpu);
696 pci = &pdi->cpus[cpu];
702 static int resize_devices(char *name)
704 int size = (ndevices + 1) * sizeof(struct per_dev_info);
706 devices = realloc(devices, size);
708 fprintf(stderr, "Out of memory, device %s (%d)\n", name, size);
711 memset(&devices[ndevices], 0, sizeof(struct per_dev_info));
712 devices[ndevices].name = name;
717 static struct per_dev_info *get_dev_info(dev_t dev)
719 struct per_dev_info *pdi;
722 for (i = 0; i < ndevices; i++) {
724 devices[i].dev = dev;
725 if (devices[i].dev == dev)
729 if (resize_devices(NULL))
732 pdi = &devices[ndevices - 1];
734 pdi->last_sequence = -1;
735 pdi->last_read_time = 0;
736 memset(&pdi->rb_last, 0, sizeof(pdi->rb_last));
737 pdi->rb_last_entries = 0;
741 static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size)
744 snprintf(buffer, size, "%s", pdi->name);
746 snprintf(buffer, size, "%d,%d",MAJOR(pdi->dev),MINOR(pdi->dev));
750 static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit)
752 unsigned long long this = bit->time;
753 unsigned long long last = pdi->last_reported_time;
755 pdi->backwards = (this < last) ? 'B' : ' ';
756 pdi->last_reported_time = this;
759 static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t,
764 ios->qwrite_kb += t->bytes >> 10;
767 ios->qread_kb += t->bytes >> 10;
771 static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci,
774 __account_m(&pci->io_stats, t, rw);
776 if (per_process_stats) {
777 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
779 __account_m(ios, t, rw);
783 static inline void __account_queue(struct io_stats *ios, struct blk_io_trace *t,
788 ios->qwrite_kb += t->bytes >> 10;
791 ios->qread_kb += t->bytes >> 10;
795 static inline void account_queue(struct blk_io_trace *t,
796 struct per_cpu_info *pci, int rw)
798 __account_queue(&pci->io_stats, t, rw);
800 if (per_process_stats) {
801 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
803 __account_queue(ios, t, rw);
807 static inline void __account_c(struct io_stats *ios, int rw, unsigned int bytes)
811 ios->cwrite_kb += bytes >> 10;
814 ios->cread_kb += bytes >> 10;
818 static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci,
821 __account_c(&pci->io_stats, rw, bytes);
823 if (per_process_stats) {
824 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
826 __account_c(ios, rw, bytes);
830 static inline void __account_issue(struct io_stats *ios, int rw,
835 ios->iwrite_kb += bytes >> 10;
838 ios->iread_kb += bytes >> 10;
842 static inline void account_issue(struct blk_io_trace *t,
843 struct per_cpu_info *pci, int rw)
845 __account_issue(&pci->io_stats, rw, t->bytes);
847 if (per_process_stats) {
848 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
850 __account_issue(ios, rw, t->bytes);
854 static inline void __account_unplug(struct io_stats *ios, int timer)
857 ios->timer_unplugs++;
862 static inline void account_unplug(struct blk_io_trace *t,
863 struct per_cpu_info *pci, int timer)
865 __account_unplug(&pci->io_stats, timer);
867 if (per_process_stats) {
868 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
870 __account_unplug(ios, timer);
874 static void log_complete(struct per_dev_info *pdi, struct per_cpu_info *pci,
875 struct blk_io_trace *t, char *act)
877 process_fmt(act, pci, t, log_track_complete(pdi, t), 0, NULL);
880 static void log_insert(struct per_dev_info *pdi, struct per_cpu_info *pci,
881 struct blk_io_trace *t, char *act)
883 process_fmt(act, pci, t, log_track_insert(pdi, t), 0, NULL);
886 static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t,
889 process_fmt(act, pci, t, -1, 0, NULL);
892 static void log_issue(struct per_dev_info *pdi, struct per_cpu_info *pci,
893 struct blk_io_trace *t, char *act)
895 process_fmt(act, pci, t, log_track_issue(pdi, t), 0, NULL);
898 static void log_merge(struct per_dev_info *pdi, struct per_cpu_info *pci,
899 struct blk_io_trace *t, char *act)
902 log_track_frontmerge(pdi, t);
904 process_fmt(act, pci, t, -1ULL, 0, NULL);
907 static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t,
910 process_fmt(act, pci, t, -1ULL, 0, NULL);
913 static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t,
916 process_fmt(act, pci, t, -1ULL, 0, NULL);
919 static void log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t,
922 process_fmt(act, pci, t, -1ULL, 0, NULL);
925 static void log_split(struct per_cpu_info *pci, struct blk_io_trace *t,
928 process_fmt(act, pci, t, -1ULL, 0, NULL);
931 static void log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act)
933 unsigned char *buf = (unsigned char *) t + sizeof(*t);
935 process_fmt(act, pci, t, -1ULL, t->pdu_len, buf);
938 static void dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci)
940 int act = t->action & 0xffff;
944 log_generic(pci, t, "Q");
947 log_generic(pci, t, "G");
949 case __BLK_TA_SLEEPRQ:
950 log_generic(pci, t, "S");
952 case __BLK_TA_REQUEUE:
953 log_generic(pci, t, "R");
958 case __BLK_TA_COMPLETE:
961 case __BLK_TA_INSERT:
965 fprintf(stderr, "Bad pc action %x\n", act);
970 static void dump_trace_fs(struct blk_io_trace *t, struct per_dev_info *pdi,
971 struct per_cpu_info *pci)
973 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
974 int act = t->action & 0xffff;
978 account_queue(t, pci, w);
979 log_queue(pci, t, "Q");
981 case __BLK_TA_INSERT:
982 log_insert(pdi, pci, t, "I");
984 case __BLK_TA_BACKMERGE:
985 account_m(t, pci, w);
986 log_merge(pdi, pci, t, "M");
988 case __BLK_TA_FRONTMERGE:
989 account_m(t, pci, w);
990 log_merge(pdi, pci, t, "F");
993 log_track_getrq(pdi, t);
994 log_generic(pci, t, "G");
996 case __BLK_TA_SLEEPRQ:
997 log_generic(pci, t, "S");
999 case __BLK_TA_REQUEUE:
1000 account_c(t, pci, w, -t->bytes);
1001 log_queue(pci, t, "R");
1003 case __BLK_TA_ISSUE:
1004 account_issue(t, pci, w);
1005 log_issue(pdi, pci, t, "D");
1007 case __BLK_TA_COMPLETE:
1008 account_c(t, pci, w, t->bytes);
1009 log_complete(pdi, pci, t, "C");
1012 log_action(pci, t, "P");
1014 case __BLK_TA_UNPLUG_IO:
1015 account_unplug(t, pci, 0);
1016 log_unplug(pci, t, "U");
1018 case __BLK_TA_UNPLUG_TIMER:
1019 account_unplug(t, pci, 1);
1020 log_unplug(pci, t, "UT");
1022 case __BLK_TA_SPLIT:
1023 log_split(pci, t, "X");
1025 case __BLK_TA_BOUNCE:
1026 log_generic(pci, t, "B");
1029 fprintf(stderr, "Bad fs action %x\n", t->action);
1034 static void dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
1035 struct per_dev_info *pdi)
1037 if (t->action & BLK_TC_ACT(BLK_TC_PC))
1038 dump_trace_pc(t, pci);
1040 dump_trace_fs(t, pdi, pci);
1045 static void dump_io_stats(struct io_stats *ios, char *msg)
1047 fprintf(ofp, "%s\n", msg);
1049 fprintf(ofp, " Reads Queued: %'8lu, %'8LuKiB\t", ios->qreads, ios->qread_kb);
1050 fprintf(ofp, " Writes Queued: %'8lu, %'8LuKiB\n", ios->qwrites,ios->qwrite_kb);
1052 fprintf(ofp, " Read Dispatches: %'8lu, %'8LuKiB\t", ios->ireads, ios->iread_kb);
1053 fprintf(ofp, " Write Dispatches: %'8lu, %'8LuKiB\n", ios->iwrites,ios->iwrite_kb);
1054 fprintf(ofp, " Reads Completed: %'8lu, %'8LuKiB\t", ios->creads, ios->cread_kb);
1055 fprintf(ofp, " Writes Completed: %'8lu, %'8LuKiB\n", ios->cwrites,ios->cwrite_kb);
1056 fprintf(ofp, " Read Merges: %'8lu%8c\t", ios->mreads, ' ');
1057 fprintf(ofp, " Write Merges: %'8lu\n", ios->mwrites);
1058 fprintf(ofp, " IO unplugs: %'8lu%8c\t", ios->io_unplugs, ' ');
1059 fprintf(ofp, " Timer unplugs: %'8lu\n", ios->timer_unplugs);
1062 static void dump_wait_stats(struct per_process_info *ppi)
1064 unsigned long rawait = ppi->longest_allocation_wait[0] / 1000;
1065 unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000;
1066 unsigned long rcwait = ppi->longest_completion_wait[0] / 1000;
1067 unsigned long wawait = ppi->longest_allocation_wait[1] / 1000;
1068 unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000;
1069 unsigned long wcwait = ppi->longest_completion_wait[1] / 1000;
1071 fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' ');
1072 fprintf(ofp, " Allocation wait: %'8lu\n", wawait);
1073 fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' ');
1074 fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait);
1075 fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' ');
1076 fprintf(ofp, " Completion wait: %'8lu\n", wcwait);
1079 static int ppi_name_compare(const void *p1, const void *p2)
1081 struct per_process_info *ppi1 = *((struct per_process_info **) p1);
1082 struct per_process_info *ppi2 = *((struct per_process_info **) p2);
1085 res = strverscmp(ppi1->name, ppi2->name);
1087 res = ppi1->pid > ppi2->pid;
1092 static void sort_process_list(void)
1094 struct per_process_info **ppis;
1095 struct per_process_info *ppi;
1098 ppis = malloc(ppi_list_entries * sizeof(struct per_process_info *));
1103 ppi = ppi->list_next;
1106 qsort(ppis, ppi_list_entries, sizeof(ppi), ppi_name_compare);
1108 i = ppi_list_entries - 1;
1113 ppi->list_next = ppi_list;
1121 static void show_process_stats(void)
1123 struct per_process_info *ppi;
1125 sort_process_list();
1131 if (ppi->more_than_one)
1132 sprintf(name, "%s (%u, ...)", ppi->name, ppi->pid);
1134 sprintf(name, "%s (%u)", ppi->name, ppi->pid);
1136 dump_io_stats(&ppi->io_stats, name);
1137 dump_wait_stats(ppi);
1138 ppi = ppi->list_next;
1144 static void show_device_and_cpu_stats(void)
1146 struct per_dev_info *pdi;
1147 struct per_cpu_info *pci;
1148 struct io_stats total, *ios;
1149 int i, j, pci_events;
1150 char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3];
1153 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
1155 memset(&total, 0, sizeof(total));
1161 for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) {
1165 ios = &pci->io_stats;
1166 total.qreads += ios->qreads;
1167 total.qwrites += ios->qwrites;
1168 total.creads += ios->creads;
1169 total.cwrites += ios->cwrites;
1170 total.mreads += ios->mreads;
1171 total.mwrites += ios->mwrites;
1172 total.ireads += ios->ireads;
1173 total.iwrites += ios->iwrites;
1174 total.qread_kb += ios->qread_kb;
1175 total.qwrite_kb += ios->qwrite_kb;
1176 total.cread_kb += ios->cread_kb;
1177 total.cwrite_kb += ios->cwrite_kb;
1178 total.iread_kb += ios->iread_kb;
1179 total.iwrite_kb += ios->iwrite_kb;
1180 total.timer_unplugs += ios->timer_unplugs;
1181 total.io_unplugs += ios->io_unplugs;
1183 snprintf(line, sizeof(line) - 1, "CPU%d (%s):",
1184 j, get_dev_name(pdi, name, sizeof(name)));
1185 dump_io_stats(ios, line);
1189 if (pci_events > 1) {
1191 snprintf(line, sizeof(line) - 1, "Total (%s):",
1192 get_dev_name(pdi, name, sizeof(name)));
1193 dump_io_stats(&total, line);
1196 fprintf(ofp, "\nEvents (%s): %'Lu entries, %'lu skips\n",
1197 get_dev_name(pdi, line, sizeof(line)), pdi->events,
1203 * struct trace and blktrace allocation cache, we do potentially
1204 * millions of mallocs for these structures while only using at most
1205 * a few thousand at the time
1207 static inline void t_free(struct trace *t)
1209 if (t_alloc_cache < 1024) {
1210 t->next = t_alloc_list;
1217 static inline struct trace *t_alloc(void)
1219 struct trace *t = t_alloc_list;
1222 t_alloc_list = t->next;
1227 return malloc(sizeof(*t));
1230 static inline void bit_free(struct blk_io_trace *bit)
1232 if (bit_alloc_cache < 1024) {
1234 * abuse a 64-bit field for a next pointer for the free item
1236 bit->time = (__u64) (unsigned long) bit_alloc_list;
1237 bit_alloc_list = (struct blk_io_trace *) bit;
1243 static inline struct blk_io_trace *bit_alloc(void)
1245 struct blk_io_trace *bit = bit_alloc_list;
1248 bit_alloc_list = (struct blk_io_trace *) (unsigned long) \
1254 return malloc(sizeof(*bit));
1257 static void find_genesis(void)
1259 struct trace *t = trace_list;
1261 genesis_time = -1ULL;
1263 if (t->bit->time < genesis_time)
1264 genesis_time = t->bit->time;
1270 static inline int check_stopwatch(struct blk_io_trace *bit)
1272 if (bit->time < stopwatch_end &&
1273 bit->time >= stopwatch_start)
1280 * return youngest entry read
1282 static int sort_entries(unsigned long long *youngest)
1290 while ((t = trace_list) != NULL) {
1291 struct blk_io_trace *bit = t->bit;
1293 trace_list = t->next;
1295 bit->time -= genesis_time;
1297 if (bit->time < *youngest || !*youngest)
1298 *youngest = bit->time;
1300 if (check_stopwatch(bit)) {
1306 if (trace_rb_insert_sort(t))
1309 if (bit->sequence < smallest_seq_read)
1310 smallest_seq_read = bit->sequence;
1316 static inline void __put_trace_last(struct per_dev_info *pdi, struct trace *t)
1318 rb_erase(&t->rb_node, &pdi->rb_last);
1319 pdi->rb_last_entries--;
1325 static void put_trace(struct per_dev_info *pdi, struct trace *t)
1327 rb_erase(&t->rb_node, &rb_sort_root);
1330 trace_rb_insert_last(pdi, t);
1332 if (pdi->rb_last_entries > rb_batch * pdi->nfiles) {
1333 struct rb_node *n = rb_first(&pdi->rb_last);
1335 t = rb_entry(n, struct trace, rb_node);
1336 __put_trace_last(pdi, t);
1340 static int check_sequence(struct per_dev_info *pdi, struct blk_io_trace *bit)
1342 unsigned long expected_sequence = pdi->last_sequence + 1;
1346 * first entry, always ok
1348 if (!expected_sequence)
1351 if (bit->sequence == expected_sequence)
1355 * we may not have seen that sequence yet. if we are not doing
1356 * the final run, break and wait for more entries.
1358 if (expected_sequence < smallest_seq_read) {
1359 t = trace_rb_find_last(pdi, expected_sequence);
1363 __put_trace_last(pdi, t);
1367 if (print_missing) {
1368 fprintf(stderr, "(%d,%d): skipping %lu -> %u\n",
1369 MAJOR(pdi->dev), MINOR(pdi->dev),
1370 pdi->last_sequence, bit->sequence);
1377 static void show_entries_rb(int force)
1379 struct per_dev_info *pdi = NULL;
1380 struct per_cpu_info *pci = NULL;
1381 struct blk_io_trace *bit;
1385 while ((n = rb_first(&rb_sort_root)) != NULL) {
1386 if (is_done() && !force && !pipeline)
1389 t = rb_entry(n, struct trace, rb_node);
1392 if (!pdi || pdi->dev != bit->device)
1393 pdi = get_dev_info(bit->device);
1396 fprintf(stderr, "Unknown device ID? (%d,%d)\n",
1397 MAJOR(bit->device), MINOR(bit->device));
1402 if (check_sequence(pdi, bit))
1405 if (bit->time > last_allowed_time)
1409 pdi->last_sequence = bit->sequence;
1411 check_time(pdi, bit);
1413 if (!pci || pci->cpu != bit->cpu)
1414 pci = get_cpu_info(pdi, bit->cpu);
1416 if (bit->action & (act_mask << BLK_TC_SHIFT))
1417 dump_trace(bit, pci, pdi);
1423 static int read_data(int fd, void *buffer, int bytes, int block)
1425 int ret, bytes_left, fl;
1428 fl = fcntl(fd, F_GETFL);
1431 fcntl(fd, F_SETFL, fl | O_NONBLOCK);
1433 fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
1437 while (bytes_left > 0) {
1438 ret = read(fd, p, bytes_left);
1442 if (errno != EAGAIN)
1455 static int read_events(int fd, int always_block)
1457 struct per_dev_info *pdi = NULL;
1458 unsigned int events = 0;
1460 while (!is_done() && events < rb_batch) {
1461 struct blk_io_trace *bit;
1468 if (read_data(fd, bit, sizeof(*bit), !events || always_block))
1471 magic = be32_to_cpu(bit->magic);
1472 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
1473 fprintf(stderr, "Bad magic %x\n", magic);
1477 pdu_len = be16_to_cpu(bit->pdu_len);
1479 void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
1481 if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1))
1489 if (verify_trace(bit)) {
1495 memset(t, 0, sizeof(*t));
1498 t->next = trace_list;
1501 if (!pdi || pdi->dev != bit->device)
1502 pdi = get_dev_info(bit->device);
1504 if (bit->time > pdi->last_read_time)
1505 pdi->last_read_time = bit->time;
1513 static int do_file(void)
1515 struct per_cpu_info *pci;
1516 struct per_dev_info *pdi;
1517 int i, j, events, events_added;
1520 * first prepare all files for reading
1522 for (i = 0; i < ndevices; i++) {
1525 pdi->last_sequence = -1;
1531 pci = get_cpu_info(pdi, j);
1536 len = sprintf(pci->fname, "%s/", input_dir);
1538 snprintf(pci->fname + len, sizeof(pci->fname)-1-len,
1539 "%s.blktrace.%d", pdi->name, pci->cpu);
1540 if (stat(pci->fname, &st) < 0)
1543 pci->fd = open(pci->fname, O_RDONLY);
1550 printf("Input file %s added\n", pci->fname);
1556 * now loop over the files reading in the data
1559 unsigned long long youngest;
1562 last_allowed_time = -1ULL;
1563 smallest_seq_read = -1U;
1565 for (i = 0; i < ndevices; i++) {
1568 for (j = 0; j < pdi->nfiles; j++) {
1570 pci = get_cpu_info(pdi, j);
1575 events = read_events(pci->fd, 1);
1582 if (pdi->last_read_time < last_allowed_time)
1583 last_allowed_time = pdi->last_read_time;
1585 events_added += events;
1589 if (sort_entries(&youngest))
1592 if (youngest > stopwatch_end)
1597 } while (events_added);
1599 if (rb_sort_entries)
1605 static int do_stdin(void)
1607 unsigned long long youngest;
1608 int fd, events, loops;
1610 last_allowed_time = -1ULL;
1611 fd = dup(STDIN_FILENO);
1613 perror("dup stdin");
1618 while ((events = read_events(fd, 0)) != 0) {
1620 smallest_seq_read = -1U;
1622 if (sort_entries(&youngest))
1625 if (youngest > stopwatch_end)
1632 if (rb_sort_entries)
1639 static void flush_output(void)
1644 static void handle_sigint(__attribute__((__unused__)) int sig)
1651 * Extract start and duration times from a string, allowing
1652 * us to specify a time interval of interest within a trace.
1653 * Format: "duration" (start is zero) or "start:duration".
1655 static int find_stopwatch_interval(char *string)
1660 value = strtod(string, &sp);
1662 fprintf(stderr,"Invalid stopwatch timer: %s\n", string);
1666 stopwatch_start = DOUBLE_TO_NANO_ULL(value);
1668 value = strtod(string, &sp);
1669 if (sp == string || *sp != '\0') {
1670 fprintf(stderr,"Invalid stopwatch duration time: %s\n",
1674 } else if (*sp != '\0') {
1675 fprintf(stderr,"Invalid stopwatch start timer: %s\n", string);
1678 stopwatch_end = DOUBLE_TO_NANO_ULL(value);
1679 if (stopwatch_end <= stopwatch_start) {
1680 fprintf(stderr, "Invalid stopwatch interval: %Lu -> %Lu\n",
1681 stopwatch_start, stopwatch_end);
1688 static char usage_str[] = \
1689 "[ -i <input name> ] [-o <output name> [ -s ] [ -t ] [ -q ]\n" \
1690 "[ -w start:stop ] [ -f output format ] [ -F format spec ] [ -v] \n\n" \
1691 "\t-i Input file containing trace data, or '-' for stdin\n" \
1692 "\t-D Directory to prepend to input file names\n" \
1693 "\t-o Output file. If not given, output is stdout\n" \
1694 "\t-b stdin read batching\n" \
1695 "\t-s Show per-program io statistics\n" \
1696 "\t-n Hash processes by name, not pid\n" \
1697 "\t-t Track individual ios. Will tell you the time a request took\n" \
1698 "\t to get queued, to get dispatched, and to get completed\n" \
1699 "\t-q Quiet. Don't display any stats at the end of the trace\n" \
1700 "\t-w Only parse data between the given time interval in seconds.\n" \
1701 "\t If 'start' isn't given, blkparse defaults the start time to 0\n" \
1702 "\t -f Output format. Customize the output format. The format field\n" \
1703 "\t identifies can be found in the documentation\n" \
1704 "\t-F Format specification. Can be found in the documentation\n" \
1705 "\t-m Print missing entries\n" \
1706 "\t-v Print program version info\n\n";
1708 static void usage(char *prog)
1710 fprintf(stderr, "Usage: %s %s %s", prog, blkparse_version, usage_str);
1713 int main(int argc, char *argv[])
1716 int i, c, ret, mode;
1717 int per_device_and_cpu_stats = 1;
1718 int act_mask_tmp = 0;
1720 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
1723 i = find_mask_map(optarg);
1725 fprintf(stderr,"Invalid action mask %s\n",
1733 if ((sscanf(optarg, "%x", &i) != 1) ||
1734 !valid_act_opt(i)) {
1736 "Invalid set action mask %s/0x%x\n",
1743 if (!strcmp(optarg, "-") && !pipeline)
1745 else if (resize_devices(optarg) != 0)
1752 output_name = optarg;
1755 rb_batch = atoi(optarg);
1757 rb_batch = RB_BATCH_DEFAULT;
1760 per_process_stats = 1;
1766 per_device_and_cpu_stats = 0;
1769 if (find_stopwatch_interval(optarg) != 0)
1773 set_all_format_specs(optarg);
1776 if (add_format_spec(optarg) != 0)
1780 ppi_hash_by_pid = 0;
1786 printf("%s version %s\n", argv[0], blkparse_version);
1794 while (optind < argc) {
1795 if (!strcmp(argv[optind], "-") && !pipeline)
1797 else if (resize_devices(argv[optind]) != 0)
1802 if (!pipeline && !ndevices) {
1807 if (act_mask_tmp != 0)
1808 act_mask = act_mask_tmp;
1810 memset(&rb_sort_root, 0, sizeof(rb_sort_root));
1812 signal(SIGINT, handle_sigint);
1813 signal(SIGHUP, handle_sigint);
1814 signal(SIGTERM, handle_sigint);
1816 setlocale(LC_NUMERIC, "en_US");
1819 ofp = fdopen(STDOUT_FILENO, "w");
1824 snprintf(ofname, sizeof(ofname) - 1, "%s", output_name);
1825 ofp = fopen(ofname, "w");
1834 ofp_buffer = malloc(4096);
1835 if (setvbuf(ofp, ofp_buffer, mode, 4096)) {
1845 if (per_process_stats)
1846 show_process_stats();
1848 if (per_device_and_cpu_stats)
1849 show_device_and_cpu_stats();