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;
60 struct per_cpu_info *cpus;
63 struct per_process_info {
66 struct io_stats io_stats;
67 struct per_process_info *hash_next, *list_next;
73 unsigned long long longest_allocation_wait[2];
74 unsigned long long longest_dispatch_wait[2];
75 unsigned long long longest_completion_wait[2];
78 #define PPI_HASH_SHIFT (8)
79 #define PPI_HASH_SIZE (1 << PPI_HASH_SHIFT)
80 #define PPI_HASH_MASK (PPI_HASH_SIZE - 1)
81 static struct per_process_info *ppi_hash_table[PPI_HASH_SIZE];
82 static struct per_process_info *ppi_list;
83 static int ppi_list_entries;
85 #define S_OPTS "a:A:i:o:b:stqw:f:F:vVhD:"
86 static struct option l_opts[] = {
89 .has_arg = required_argument,
95 .has_arg = required_argument,
101 .has_arg = required_argument,
107 .has_arg = required_argument,
113 .has_arg = required_argument,
118 .name = "per-program-stats",
119 .has_arg = no_argument,
125 .has_arg = no_argument,
131 .has_arg = no_argument,
137 .has_arg = required_argument,
143 .has_arg = required_argument,
148 .name = "format-spec",
149 .has_arg = required_argument,
154 .name = "hash-by-name",
155 .has_arg = no_argument,
161 .has_arg = no_argument,
167 .has_arg = no_argument,
172 .name = "input-directory",
173 .has_arg = required_argument,
183 * for sorting the displayed output
186 struct blk_io_trace *bit;
187 struct rb_node rb_node;
191 static struct rb_root rb_sort_root;
192 static unsigned long rb_sort_entries;
194 static struct trace *trace_list;
199 static struct blk_io_trace *bit_alloc_list;
200 static struct trace *t_alloc_list;
203 * for tracking individual ios
206 struct rb_node rb_node;
211 unsigned long long allocation_time;
212 unsigned long long queue_time;
213 unsigned long long dispatch_time;
214 unsigned long long completion_time;
218 static struct per_dev_info *devices;
219 static char *get_dev_name(struct per_dev_info *, char *, int);
222 static char *output_name;
223 static char *input_dir;
225 static unsigned long long genesis_time;
226 static unsigned long long last_allowed_time;
227 static unsigned int smallest_seq_read;
228 static unsigned long long stopwatch_start; /* start from zero by default */
229 static unsigned long long stopwatch_end = ULONG_LONG_MAX; /* "infinity" */
231 static int per_process_stats;
232 static int per_device_and_cpu_stats = 1;
233 static int track_ios;
234 static int ppi_hash_by_pid = 1;
236 static unsigned int act_mask = -1U;
237 static int stats_printed;
239 static unsigned int t_alloc_cache;
240 static unsigned int bit_alloc_cache;
242 #define RB_BATCH_DEFAULT (512)
243 static unsigned int rb_batch = RB_BATCH_DEFAULT;
247 #define is_done() (*(volatile int *)(&done))
248 static volatile int done;
250 #define JHASH_RANDOM (0x3af5f2ee)
252 static inline int ppi_hash_pid(__u32 pid)
254 return jhash_1word(pid, JHASH_RANDOM) & PPI_HASH_MASK;
257 static inline int ppi_hash_name(const char *name)
259 return jhash(name, 16, JHASH_RANDOM) & PPI_HASH_MASK;
262 static inline int ppi_hash(struct per_process_info *ppi)
265 return ppi_hash_pid(ppi->pid);
267 return ppi_hash_name(ppi->name);
270 static inline void add_process_to_hash(struct per_process_info *ppi)
272 const int hash_idx = ppi_hash(ppi);
274 ppi->hash_next = ppi_hash_table[hash_idx];
275 ppi_hash_table[hash_idx] = ppi;
278 static inline void add_process_to_list(struct per_process_info *ppi)
280 ppi->list_next = ppi_list;
285 static struct per_process_info *find_process_by_name(char *name)
287 const int hash_idx = ppi_hash_name(name);
288 struct per_process_info *ppi;
290 ppi = ppi_hash_table[hash_idx];
292 if (!strcmp(ppi->name, name))
295 ppi = ppi->hash_next;
301 static struct per_process_info *find_process_by_pid(__u32 pid)
303 const int hash_idx = ppi_hash_pid(pid);
304 struct per_process_info *ppi;
306 ppi = ppi_hash_table[hash_idx];
311 ppi = ppi->hash_next;
317 static struct per_process_info *find_process(__u32 pid, char *name)
319 struct per_process_info *ppi;
322 return find_process_by_pid(pid);
324 ppi = find_process_by_name(name);
325 if (ppi && ppi->pid != pid)
326 ppi->more_than_one = 1;
331 static inline int trace_rb_insert(struct trace *t, struct rb_root *root,
334 struct rb_node **p = &root->rb_node;
335 struct rb_node *parent = NULL;
341 __t = rb_entry(parent, struct trace, rb_node);
344 if (t->bit->time < __t->bit->time) {
347 } else if (t->bit->time > __t->bit->time) {
352 if (t->bit->device < __t->bit->device)
354 else if (t->bit->device > __t->bit->device)
356 else if (t->bit->sequence < __t->bit->sequence)
358 else /* >= sequence */
362 rb_link_node(&t->rb_node, parent, p);
363 rb_insert_color(&t->rb_node, root);
367 static inline int trace_rb_insert_sort(struct trace *t)
369 if (!trace_rb_insert(t, &rb_sort_root, 1)) {
377 static inline int trace_rb_insert_last(struct per_dev_info *pdi,struct trace *t)
379 if (!trace_rb_insert(t, &pdi->rb_last, 1)) {
380 pdi->rb_last_entries++;
387 static struct trace *trace_rb_find(dev_t device, unsigned long sequence,
388 struct rb_root *root, int order)
390 struct rb_node *n = root->rb_node;
391 struct rb_node *prev = NULL;
395 __t = rb_entry(n, struct trace, rb_node);
398 if (device < __t->bit->device)
400 else if (device > __t->bit->device)
402 else if (sequence < __t->bit->sequence)
404 else if (sequence > __t->bit->sequence)
411 * hack - the list may not be sequence ordered because some
412 * events don't have sequence and time matched. so we end up
413 * being a little off in the rb lookup here, because we don't
414 * know the time we are looking for. compensate by browsing
415 * a little ahead from the last entry to find the match
420 while (((n = rb_next(prev)) != NULL) && max--) {
421 __t = rb_entry(n, struct trace, rb_node);
423 if (__t->bit->device == device &&
424 __t->bit->sequence == sequence)
434 static inline struct trace *trace_rb_find_sort(dev_t dev, unsigned long seq)
436 return trace_rb_find(dev, seq, &rb_sort_root, 1);
439 static inline struct trace *trace_rb_find_last(struct per_dev_info *pdi,
442 return trace_rb_find(pdi->dev, seq, &pdi->rb_last, 0);
445 static inline int track_rb_insert(struct per_dev_info *pdi,struct io_track *iot)
447 struct rb_node **p = &pdi->rb_track.rb_node;
448 struct rb_node *parent = NULL;
449 struct io_track *__iot;
453 __iot = rb_entry(parent, struct io_track, rb_node);
455 if (iot->sector < __iot->sector)
457 else if (iot->sector > __iot->sector)
461 "sector alias (%Lu) on device %d,%d!\n",
462 (unsigned long long) iot->sector,
463 MAJOR(pdi->dev), MINOR(pdi->dev));
468 rb_link_node(&iot->rb_node, parent, p);
469 rb_insert_color(&iot->rb_node, &pdi->rb_track);
473 static struct io_track *__find_track(struct per_dev_info *pdi, __u64 sector)
475 struct rb_node *n = pdi->rb_track.rb_node;
476 struct io_track *__iot;
479 __iot = rb_entry(n, struct io_track, rb_node);
481 if (sector < __iot->sector)
483 else if (sector > __iot->sector)
492 static struct io_track *find_track(struct per_dev_info *pdi, __u32 pid,
493 char *comm, __u64 sector)
495 struct io_track *iot;
497 iot = __find_track(pdi, sector);
499 iot = malloc(sizeof(*iot));
501 memcpy(iot->comm, comm, sizeof(iot->comm));
502 iot->sector = sector;
503 track_rb_insert(pdi, iot);
509 static void log_track_frontmerge(struct per_dev_info *pdi,
510 struct blk_io_trace *t)
512 struct io_track *iot;
517 iot = __find_track(pdi, t->sector + t_sec(t));
520 fprintf(stderr, "merge not found for (%d,%d): %llu\n",
521 MAJOR(pdi->dev), MINOR(pdi->dev),
522 (unsigned long long) t->sector + t_sec(t));
526 rb_erase(&iot->rb_node, &pdi->rb_track);
527 iot->sector -= t_sec(t);
528 track_rb_insert(pdi, iot);
531 static void log_track_getrq(struct per_dev_info *pdi, struct blk_io_trace *t)
533 struct io_track *iot;
538 iot = find_track(pdi, t->pid, t->comm, t->sector);
539 iot->allocation_time = t->time;
543 * return time between rq allocation and insertion
545 static unsigned long long log_track_insert(struct per_dev_info *pdi,
546 struct blk_io_trace *t)
548 unsigned long long elapsed;
549 struct io_track *iot;
554 iot = find_track(pdi, t->pid, t->comm, t->sector);
555 iot->queue_time = t->time;
557 if (!iot->allocation_time)
560 elapsed = iot->queue_time - iot->allocation_time;
562 if (per_process_stats) {
563 struct per_process_info *ppi = find_process(iot->pid,iot->comm);
564 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
566 if (ppi && elapsed > ppi->longest_allocation_wait[w])
567 ppi->longest_allocation_wait[w] = elapsed;
574 * return time between queue and issue
576 static unsigned long long log_track_issue(struct per_dev_info *pdi,
577 struct blk_io_trace *t)
579 unsigned long long elapsed;
580 struct io_track *iot;
584 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
587 iot = __find_track(pdi, t->sector);
590 fprintf(stderr, "issue not found for (%d,%d): %llu\n",
591 MAJOR(pdi->dev), MINOR(pdi->dev),
592 (unsigned long long) t->sector);
596 iot->dispatch_time = t->time;
597 elapsed = iot->dispatch_time - iot->queue_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_dispatch_wait[w])
604 ppi->longest_dispatch_wait[w] = elapsed;
611 * return time between dispatch and complete
613 static unsigned long long log_track_complete(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,"complete not found for (%d,%d): %llu\n",
628 MAJOR(pdi->dev), MINOR(pdi->dev),
629 (unsigned long long) t->sector);
633 iot->completion_time = t->time;
634 elapsed = iot->completion_time - iot->dispatch_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_completion_wait[w])
641 ppi->longest_completion_wait[w] = elapsed;
645 * kill the trace, we don't need it after completion
647 rb_erase(&iot->rb_node, &pdi->rb_track);
654 static struct io_stats *find_process_io_stats(__u32 pid, char *name)
656 struct per_process_info *ppi = find_process(pid, name);
659 ppi = malloc(sizeof(*ppi));
660 memset(ppi, 0, sizeof(*ppi));
661 memcpy(ppi->name, name, 16);
663 add_process_to_hash(ppi);
664 add_process_to_list(ppi);
667 return &ppi->io_stats;
670 static void resize_cpu_info(struct per_dev_info *pdi, int cpu)
672 struct per_cpu_info *cpus = pdi->cpus;
673 int ncpus = pdi->ncpus;
674 int new_count = cpu + 1;
678 size = new_count * sizeof(struct per_cpu_info);
679 cpus = realloc(cpus, size);
682 fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n",
683 get_dev_name(pdi, name, sizeof(name)), size);
687 new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info));
688 new_space = (new_count - ncpus) * sizeof(struct per_cpu_info);
689 memset(new_start, 0, new_space);
691 pdi->ncpus = new_count;
695 static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu)
697 struct per_cpu_info *pci;
699 if (cpu >= pdi->ncpus)
700 resize_cpu_info(pdi, cpu);
702 pci = &pdi->cpus[cpu];
708 static int resize_devices(char *name)
710 int size = (ndevices + 1) * sizeof(struct per_dev_info);
712 devices = realloc(devices, size);
714 fprintf(stderr, "Out of memory, device %s (%d)\n", name, size);
717 memset(&devices[ndevices], 0, sizeof(struct per_dev_info));
718 devices[ndevices].name = name;
723 static struct per_dev_info *get_dev_info(dev_t dev)
725 struct per_dev_info *pdi;
728 for (i = 0; i < ndevices; i++) {
730 devices[i].dev = dev;
731 if (devices[i].dev == dev)
735 if (resize_devices(NULL))
738 pdi = &devices[ndevices - 1];
740 pdi->last_sequence = -1;
741 pdi->last_read_time = 0;
742 memset(&pdi->rb_last, 0, sizeof(pdi->rb_last));
743 pdi->rb_last_entries = 0;
747 static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size)
750 snprintf(buffer, size, "%s", pdi->name);
752 snprintf(buffer, size, "%d,%d",MAJOR(pdi->dev),MINOR(pdi->dev));
756 static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit)
758 unsigned long long this = bit->time;
759 unsigned long long last = pdi->last_reported_time;
761 pdi->backwards = (this < last) ? 'B' : ' ';
762 pdi->last_reported_time = this;
765 static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t,
770 ios->qwrite_kb += t_kb(t);
773 ios->qread_kb += t_kb(t);
777 static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci,
780 __account_m(&pci->io_stats, t, rw);
782 if (per_process_stats) {
783 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
785 __account_m(ios, t, rw);
789 static inline void __account_queue(struct io_stats *ios, struct blk_io_trace *t,
794 ios->qwrite_kb += t_kb(t);
797 ios->qread_kb += t_kb(t);
801 static inline void account_queue(struct blk_io_trace *t,
802 struct per_cpu_info *pci, int rw)
804 __account_queue(&pci->io_stats, t, rw);
806 if (per_process_stats) {
807 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
809 __account_queue(ios, t, rw);
813 static inline void __account_c(struct io_stats *ios, int rw, unsigned int bytes)
817 ios->cwrite_kb += bytes >> 10;
820 ios->cread_kb += bytes >> 10;
824 static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci,
827 __account_c(&pci->io_stats, rw, bytes);
829 if (per_process_stats) {
830 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
832 __account_c(ios, rw, bytes);
836 static inline void __account_issue(struct io_stats *ios, int rw,
841 ios->iwrite_kb += bytes >> 10;
844 ios->iread_kb += bytes >> 10;
848 static inline void account_issue(struct blk_io_trace *t,
849 struct per_cpu_info *pci, int rw)
851 __account_issue(&pci->io_stats, rw, t->bytes);
853 if (per_process_stats) {
854 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
856 __account_issue(ios, rw, t->bytes);
860 static inline void __account_unplug(struct io_stats *ios, int timer)
863 ios->timer_unplugs++;
868 static inline void account_unplug(struct blk_io_trace *t,
869 struct per_cpu_info *pci, int timer)
871 __account_unplug(&pci->io_stats, timer);
873 if (per_process_stats) {
874 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
876 __account_unplug(ios, timer);
880 static void log_complete(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_complete(pdi, t), 0, NULL);
886 static void log_insert(struct per_dev_info *pdi, struct per_cpu_info *pci,
887 struct blk_io_trace *t, char *act)
889 process_fmt(act, pci, t, log_track_insert(pdi, t), 0, NULL);
892 static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t,
895 process_fmt(act, pci, t, -1, 0, NULL);
898 static void log_issue(struct per_dev_info *pdi, struct per_cpu_info *pci,
899 struct blk_io_trace *t, char *act)
901 process_fmt(act, pci, t, log_track_issue(pdi, t), 0, NULL);
904 static void log_merge(struct per_dev_info *pdi, struct per_cpu_info *pci,
905 struct blk_io_trace *t, char *act)
908 log_track_frontmerge(pdi, t);
910 process_fmt(act, pci, t, -1ULL, 0, NULL);
913 static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t,
916 process_fmt(act, pci, t, -1ULL, 0, NULL);
919 static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t,
922 process_fmt(act, pci, t, -1ULL, 0, NULL);
925 static void log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t,
928 process_fmt(act, pci, t, -1ULL, 0, NULL);
931 static void log_split(struct per_cpu_info *pci, struct blk_io_trace *t,
934 process_fmt(act, pci, t, -1ULL, 0, NULL);
937 static void log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act)
939 unsigned char *buf = (unsigned char *) t + sizeof(*t);
941 process_fmt(act, pci, t, -1ULL, t->pdu_len, buf);
944 static void dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci)
946 int act = t->action & 0xffff;
950 log_generic(pci, t, "Q");
953 log_generic(pci, t, "G");
955 case __BLK_TA_SLEEPRQ:
956 log_generic(pci, t, "S");
958 case __BLK_TA_REQUEUE:
959 log_generic(pci, t, "R");
964 case __BLK_TA_COMPLETE:
967 case __BLK_TA_INSERT:
971 fprintf(stderr, "Bad pc action %x\n", act);
976 static void dump_trace_fs(struct blk_io_trace *t, struct per_dev_info *pdi,
977 struct per_cpu_info *pci)
979 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
980 int act = t->action & 0xffff;
984 account_queue(t, pci, w);
985 log_queue(pci, t, "Q");
987 case __BLK_TA_INSERT:
988 log_insert(pdi, pci, t, "I");
990 case __BLK_TA_BACKMERGE:
991 account_m(t, pci, w);
992 log_merge(pdi, pci, t, "M");
994 case __BLK_TA_FRONTMERGE:
995 account_m(t, pci, w);
996 log_merge(pdi, pci, t, "F");
999 log_track_getrq(pdi, t);
1000 log_generic(pci, t, "G");
1002 case __BLK_TA_SLEEPRQ:
1003 log_generic(pci, t, "S");
1005 case __BLK_TA_REQUEUE:
1006 account_c(t, pci, w, -t->bytes);
1007 log_queue(pci, t, "R");
1009 case __BLK_TA_ISSUE:
1010 account_issue(t, pci, w);
1011 log_issue(pdi, pci, t, "D");
1013 case __BLK_TA_COMPLETE:
1014 account_c(t, pci, w, t->bytes);
1015 log_complete(pdi, pci, t, "C");
1018 log_action(pci, t, "P");
1020 case __BLK_TA_UNPLUG_IO:
1021 account_unplug(t, pci, 0);
1022 log_unplug(pci, t, "U");
1024 case __BLK_TA_UNPLUG_TIMER:
1025 account_unplug(t, pci, 1);
1026 log_unplug(pci, t, "UT");
1028 case __BLK_TA_SPLIT:
1029 log_split(pci, t, "X");
1031 case __BLK_TA_BOUNCE:
1032 log_generic(pci, t, "B");
1034 case __BLK_TA_REMAP:
1035 log_generic(pci, t, "A");
1038 fprintf(stderr, "Bad fs action %x\n", t->action);
1043 static void dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
1044 struct per_dev_info *pdi)
1046 if (t->action & BLK_TC_ACT(BLK_TC_PC))
1047 dump_trace_pc(t, pci);
1049 dump_trace_fs(t, pdi, pci);
1054 static void dump_io_stats(struct io_stats *ios, char *msg)
1056 fprintf(ofp, "%s\n", msg);
1058 fprintf(ofp, " Reads Queued: %'8lu, %'8LuKiB\t", ios->qreads, ios->qread_kb);
1059 fprintf(ofp, " Writes Queued: %'8lu, %'8LuKiB\n", ios->qwrites,ios->qwrite_kb);
1061 fprintf(ofp, " Read Dispatches: %'8lu, %'8LuKiB\t", ios->ireads, ios->iread_kb);
1062 fprintf(ofp, " Write Dispatches: %'8lu, %'8LuKiB\n", ios->iwrites,ios->iwrite_kb);
1063 fprintf(ofp, " Reads Completed: %'8lu, %'8LuKiB\t", ios->creads, ios->cread_kb);
1064 fprintf(ofp, " Writes Completed: %'8lu, %'8LuKiB\n", ios->cwrites,ios->cwrite_kb);
1065 fprintf(ofp, " Read Merges: %'8lu%8c\t", ios->mreads, ' ');
1066 fprintf(ofp, " Write Merges: %'8lu\n", ios->mwrites);
1067 fprintf(ofp, " IO unplugs: %'8lu%8c\t", ios->io_unplugs, ' ');
1068 fprintf(ofp, " Timer unplugs: %'8lu\n", ios->timer_unplugs);
1071 static void dump_wait_stats(struct per_process_info *ppi)
1073 unsigned long rawait = ppi->longest_allocation_wait[0] / 1000;
1074 unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000;
1075 unsigned long rcwait = ppi->longest_completion_wait[0] / 1000;
1076 unsigned long wawait = ppi->longest_allocation_wait[1] / 1000;
1077 unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000;
1078 unsigned long wcwait = ppi->longest_completion_wait[1] / 1000;
1080 fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' ');
1081 fprintf(ofp, " Allocation wait: %'8lu\n", wawait);
1082 fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' ');
1083 fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait);
1084 fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' ');
1085 fprintf(ofp, " Completion wait: %'8lu\n", wcwait);
1088 static int ppi_name_compare(const void *p1, const void *p2)
1090 struct per_process_info *ppi1 = *((struct per_process_info **) p1);
1091 struct per_process_info *ppi2 = *((struct per_process_info **) p2);
1094 res = strverscmp(ppi1->name, ppi2->name);
1096 res = ppi1->pid > ppi2->pid;
1101 static void sort_process_list(void)
1103 struct per_process_info **ppis;
1104 struct per_process_info *ppi;
1107 ppis = malloc(ppi_list_entries * sizeof(struct per_process_info *));
1112 ppi = ppi->list_next;
1115 qsort(ppis, ppi_list_entries, sizeof(ppi), ppi_name_compare);
1117 i = ppi_list_entries - 1;
1122 ppi->list_next = ppi_list;
1130 static void show_process_stats(void)
1132 struct per_process_info *ppi;
1134 sort_process_list();
1140 if (ppi->more_than_one)
1141 sprintf(name, "%s (%u, ...)", ppi->name, ppi->pid);
1143 sprintf(name, "%s (%u)", ppi->name, ppi->pid);
1145 dump_io_stats(&ppi->io_stats, name);
1146 dump_wait_stats(ppi);
1147 ppi = ppi->list_next;
1153 static void show_device_and_cpu_stats(void)
1155 struct per_dev_info *pdi;
1156 struct per_cpu_info *pci;
1157 struct io_stats total, *ios;
1158 int i, j, pci_events;
1159 char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3];
1162 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
1164 memset(&total, 0, sizeof(total));
1170 for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) {
1174 ios = &pci->io_stats;
1175 total.qreads += ios->qreads;
1176 total.qwrites += ios->qwrites;
1177 total.creads += ios->creads;
1178 total.cwrites += ios->cwrites;
1179 total.mreads += ios->mreads;
1180 total.mwrites += ios->mwrites;
1181 total.ireads += ios->ireads;
1182 total.iwrites += ios->iwrites;
1183 total.qread_kb += ios->qread_kb;
1184 total.qwrite_kb += ios->qwrite_kb;
1185 total.cread_kb += ios->cread_kb;
1186 total.cwrite_kb += ios->cwrite_kb;
1187 total.iread_kb += ios->iread_kb;
1188 total.iwrite_kb += ios->iwrite_kb;
1189 total.timer_unplugs += ios->timer_unplugs;
1190 total.io_unplugs += ios->io_unplugs;
1192 snprintf(line, sizeof(line) - 1, "CPU%d (%s):",
1193 j, get_dev_name(pdi, name, sizeof(name)));
1194 dump_io_stats(ios, line);
1198 if (pci_events > 1) {
1200 snprintf(line, sizeof(line) - 1, "Total (%s):",
1201 get_dev_name(pdi, name, sizeof(name)));
1202 dump_io_stats(&total, line);
1205 fprintf(ofp, "\nEvents (%s): %'Lu entries, %'lu skips\n",
1206 get_dev_name(pdi, line, sizeof(line)), pdi->events,
1212 * struct trace and blktrace allocation cache, we do potentially
1213 * millions of mallocs for these structures while only using at most
1214 * a few thousand at the time
1216 static inline void t_free(struct trace *t)
1218 if (t_alloc_cache < 1024) {
1219 t->next = t_alloc_list;
1226 static inline struct trace *t_alloc(void)
1228 struct trace *t = t_alloc_list;
1231 t_alloc_list = t->next;
1236 return malloc(sizeof(*t));
1239 static inline void bit_free(struct blk_io_trace *bit)
1241 if (bit_alloc_cache < 1024) {
1243 * abuse a 64-bit field for a next pointer for the free item
1245 bit->time = (__u64) (unsigned long) bit_alloc_list;
1246 bit_alloc_list = (struct blk_io_trace *) bit;
1252 static inline struct blk_io_trace *bit_alloc(void)
1254 struct blk_io_trace *bit = bit_alloc_list;
1257 bit_alloc_list = (struct blk_io_trace *) (unsigned long) \
1263 return malloc(sizeof(*bit));
1266 static void find_genesis(void)
1268 struct trace *t = trace_list;
1270 genesis_time = -1ULL;
1272 if (t->bit->time < genesis_time)
1273 genesis_time = t->bit->time;
1279 static inline int check_stopwatch(struct blk_io_trace *bit)
1281 if (bit->time < stopwatch_end &&
1282 bit->time >= stopwatch_start)
1289 * return youngest entry read
1291 static int sort_entries(unsigned long long *youngest)
1299 while ((t = trace_list) != NULL) {
1300 struct blk_io_trace *bit = t->bit;
1302 trace_list = t->next;
1304 bit->time -= genesis_time;
1306 if (bit->time < *youngest || !*youngest)
1307 *youngest = bit->time;
1309 if (bit->sequence < smallest_seq_read)
1310 smallest_seq_read = bit->sequence;
1312 if (check_stopwatch(bit)) {
1318 if (trace_rb_insert_sort(t))
1325 static inline void __put_trace_last(struct per_dev_info *pdi, struct trace *t)
1327 rb_erase(&t->rb_node, &pdi->rb_last);
1328 pdi->rb_last_entries--;
1334 static void put_trace(struct per_dev_info *pdi, struct trace *t)
1336 rb_erase(&t->rb_node, &rb_sort_root);
1339 trace_rb_insert_last(pdi, t);
1341 if (pdi->rb_last_entries > rb_batch * pdi->nfiles) {
1342 struct rb_node *n = rb_first(&pdi->rb_last);
1344 t = rb_entry(n, struct trace, rb_node);
1345 __put_trace_last(pdi, t);
1349 static int check_sequence(struct per_dev_info *pdi, struct trace *t, int force)
1351 unsigned long expected_sequence = pdi->last_sequence + 1;
1352 struct blk_io_trace *bit = t->bit;
1355 if (!expected_sequence) {
1361 * 1 should be the first entry, just allow it
1363 if (bit->sequence == 1)
1367 * if we are starting somewhere else, check that we have
1368 * entries from all cpus in the tree before dumping one
1370 cpu_seen = malloc(pdi->nopenfiles);
1371 n = rb_first(&rb_sort_root);
1374 __t = rb_entry(n, struct trace, rb_node);
1376 if (!cpu_seen[__t->bit->cpu]) {
1377 cpu_seen[__t->bit->cpu] = 1;
1384 return cpus != pdi->nopenfiles;
1387 if (bit->sequence == expected_sequence)
1391 * we may not have seen that sequence yet. if we are not doing
1392 * the final run, break and wait for more entries.
1394 if (expected_sequence < smallest_seq_read) {
1395 __t = trace_rb_find_last(pdi, expected_sequence);
1399 __put_trace_last(pdi, __t);
1401 } else if (!force) {
1406 fprintf(stderr, "(%d,%d): skipping %lu -> %u\n",
1407 MAJOR(pdi->dev), MINOR(pdi->dev),
1408 pdi->last_sequence, bit->sequence);
1415 static void show_entries_rb(int force)
1417 struct per_dev_info *pdi = NULL;
1418 struct per_cpu_info *pci = NULL;
1419 struct blk_io_trace *bit;
1423 while ((n = rb_first(&rb_sort_root)) != NULL) {
1424 if (is_done() && !force && !pipeline)
1427 t = rb_entry(n, struct trace, rb_node);
1430 if (!pdi || pdi->dev != bit->device)
1431 pdi = get_dev_info(bit->device);
1434 fprintf(stderr, "Unknown device ID? (%d,%d)\n",
1435 MAJOR(bit->device), MINOR(bit->device));
1439 if (check_sequence(pdi, t, force))
1442 if (!force && bit->time > last_allowed_time)
1445 pdi->last_sequence = bit->sequence;
1447 check_time(pdi, bit);
1449 if (!pci || pci->cpu != bit->cpu)
1450 pci = get_cpu_info(pdi, bit->cpu);
1454 if (bit->action & (act_mask << BLK_TC_SHIFT))
1455 dump_trace(bit, pci, pdi);
1461 static int read_data(int fd, void *buffer, int bytes, int block)
1463 int ret, bytes_left, fl;
1466 fl = fcntl(fd, F_GETFL);
1469 fcntl(fd, F_SETFL, fl | O_NONBLOCK);
1471 fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
1475 while (bytes_left > 0) {
1476 ret = read(fd, p, bytes_left);
1480 if (errno != EAGAIN)
1493 static int read_events(int fd, int always_block)
1495 struct per_dev_info *pdi = NULL;
1496 unsigned int events = 0;
1498 while (!is_done() && events < rb_batch) {
1499 struct blk_io_trace *bit;
1506 if (read_data(fd, bit, sizeof(*bit), !events || always_block))
1509 magic = be32_to_cpu(bit->magic);
1510 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
1511 fprintf(stderr, "Bad magic %x\n", magic);
1515 pdu_len = be16_to_cpu(bit->pdu_len);
1517 void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
1519 if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1))
1527 if (verify_trace(bit)) {
1533 memset(t, 0, sizeof(*t));
1536 t->next = trace_list;
1539 if (!pdi || pdi->dev != bit->device)
1540 pdi = get_dev_info(bit->device);
1542 if (bit->time > pdi->last_read_time)
1543 pdi->last_read_time = bit->time;
1551 static int do_file(void)
1553 struct per_cpu_info *pci;
1554 struct per_dev_info *pdi;
1555 int i, j, events, events_added;
1558 * first prepare all files for reading
1560 for (i = 0; i < ndevices; i++) {
1563 pdi->last_sequence = -1;
1569 pci = get_cpu_info(pdi, j);
1574 len = sprintf(pci->fname, "%s/", input_dir);
1576 snprintf(pci->fname + len, sizeof(pci->fname)-1-len,
1577 "%s.blktrace.%d", pdi->name, pci->cpu);
1578 if (stat(pci->fname, &st) < 0)
1581 pci->fd = open(pci->fname, O_RDONLY);
1588 printf("Input file %s added\n", pci->fname);
1595 * now loop over the files reading in the data
1598 unsigned long long youngest;
1601 last_allowed_time = -1ULL;
1602 smallest_seq_read = -1U;
1604 for (i = 0; i < ndevices; i++) {
1607 for (j = 0; j < pdi->nfiles; j++) {
1609 pci = get_cpu_info(pdi, j);
1614 events = read_events(pci->fd, 1);
1622 if (pdi->last_read_time < last_allowed_time)
1623 last_allowed_time = pdi->last_read_time;
1625 events_added += events;
1629 if (sort_entries(&youngest))
1632 if (youngest > stopwatch_end)
1637 } while (events_added);
1639 if (rb_sort_entries)
1645 static int do_stdin(void)
1647 unsigned long long youngest;
1650 last_allowed_time = -1ULL;
1651 fd = dup(STDIN_FILENO);
1653 perror("dup stdin");
1657 while ((events = read_events(fd, 0)) != 0) {
1659 smallest_seq_read = -1U;
1661 if (sort_entries(&youngest))
1664 if (youngest > stopwatch_end)
1670 if (rb_sort_entries)
1677 static void show_stats(void)
1686 if (per_process_stats)
1687 show_process_stats();
1689 if (per_device_and_cpu_stats)
1690 show_device_and_cpu_stats();
1695 static void handle_sigint(__attribute__((__unused__)) int sig)
1702 * Extract start and duration times from a string, allowing
1703 * us to specify a time interval of interest within a trace.
1704 * Format: "duration" (start is zero) or "start:duration".
1706 static int find_stopwatch_interval(char *string)
1711 value = strtod(string, &sp);
1713 fprintf(stderr,"Invalid stopwatch timer: %s\n", string);
1717 stopwatch_start = DOUBLE_TO_NANO_ULL(value);
1719 value = strtod(string, &sp);
1720 if (sp == string || *sp != '\0') {
1721 fprintf(stderr,"Invalid stopwatch duration time: %s\n",
1725 } else if (*sp != '\0') {
1726 fprintf(stderr,"Invalid stopwatch start timer: %s\n", string);
1729 stopwatch_end = DOUBLE_TO_NANO_ULL(value);
1730 if (stopwatch_end <= stopwatch_start) {
1731 fprintf(stderr, "Invalid stopwatch interval: %Lu -> %Lu\n",
1732 stopwatch_start, stopwatch_end);
1739 static char usage_str[] = \
1740 "[ -i <input name> ] [-o <output name> [ -s ] [ -t ] [ -q ]\n" \
1741 "[ -w start:stop ] [ -f output format ] [ -F format spec ] [ -v] \n\n" \
1742 "\t-i Input file containing trace data, or '-' for stdin\n" \
1743 "\t-D Directory to prepend to input file names\n" \
1744 "\t-o Output file. If not given, output is stdout\n" \
1745 "\t-b stdin read batching\n" \
1746 "\t-s Show per-program io statistics\n" \
1747 "\t-n Hash processes by name, not pid\n" \
1748 "\t-t Track individual ios. Will tell you the time a request took\n" \
1749 "\t to get queued, to get dispatched, and to get completed\n" \
1750 "\t-q Quiet. Don't display any stats at the end of the trace\n" \
1751 "\t-w Only parse data between the given time interval in seconds.\n" \
1752 "\t If 'start' isn't given, blkparse defaults the start time to 0\n" \
1753 "\t-f Output format. Customize the output format. The format field\n" \
1754 "\t identifies can be found in the documentation\n" \
1755 "\t-F Format specification. Can be found in the documentation\n" \
1756 "\t-v More verbose for marginal errors\n" \
1757 "\t-V Print program version info\n\n";
1759 static void usage(char *prog)
1761 fprintf(stderr, "Usage: %s %s %s", prog, blkparse_version, usage_str);
1764 int main(int argc, char *argv[])
1767 int i, c, ret, mode;
1768 int act_mask_tmp = 0;
1770 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
1773 i = find_mask_map(optarg);
1775 fprintf(stderr,"Invalid action mask %s\n",
1783 if ((sscanf(optarg, "%x", &i) != 1) ||
1784 !valid_act_opt(i)) {
1786 "Invalid set action mask %s/0x%x\n",
1793 if (!strcmp(optarg, "-") && !pipeline)
1795 else if (resize_devices(optarg) != 0)
1802 output_name = optarg;
1805 rb_batch = atoi(optarg);
1807 rb_batch = RB_BATCH_DEFAULT;
1810 per_process_stats = 1;
1816 per_device_and_cpu_stats = 0;
1819 if (find_stopwatch_interval(optarg) != 0)
1823 set_all_format_specs(optarg);
1826 if (add_format_spec(optarg) != 0)
1830 ppi_hash_by_pid = 0;
1836 printf("%s version %s\n", argv[0], blkparse_version);
1844 while (optind < argc) {
1845 if (!strcmp(argv[optind], "-") && !pipeline)
1847 else if (resize_devices(argv[optind]) != 0)
1852 if (!pipeline && !ndevices) {
1857 if (act_mask_tmp != 0)
1858 act_mask = act_mask_tmp;
1860 memset(&rb_sort_root, 0, sizeof(rb_sort_root));
1862 signal(SIGINT, handle_sigint);
1863 signal(SIGHUP, handle_sigint);
1864 signal(SIGTERM, handle_sigint);
1866 setlocale(LC_NUMERIC, "en_US");
1869 ofp = fdopen(STDOUT_FILENO, "w");
1874 snprintf(ofname, sizeof(ofname) - 1, "%s", output_name);
1875 ofp = fopen(ofname, "w");
1884 ofp_buffer = malloc(4096);
1885 if (setvbuf(ofp, ofp_buffer, mode, 4096)) {