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:vVnD:"
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 per_device_and_cpu_stats = 1;
232 static int track_ios;
233 static int ppi_hash_by_pid = 1;
235 static unsigned int act_mask = -1U;
236 static int stats_printed;
238 static unsigned int t_alloc_cache;
239 static unsigned int bit_alloc_cache;
241 #define RB_BATCH_DEFAULT (512)
242 static unsigned int rb_batch = RB_BATCH_DEFAULT;
246 #define is_done() (*(volatile int *)(&done))
247 static volatile int done;
249 #define JHASH_RANDOM (0x3af5f2ee)
251 static inline int ppi_hash_pid(__u32 pid)
253 return jhash_1word(pid, JHASH_RANDOM) & PPI_HASH_MASK;
256 static inline int ppi_hash_name(const char *name)
258 return jhash(name, 16, JHASH_RANDOM) & PPI_HASH_MASK;
261 static inline int ppi_hash(struct per_process_info *ppi)
264 return ppi_hash_pid(ppi->pid);
266 return ppi_hash_name(ppi->name);
269 static inline void add_process_to_hash(struct per_process_info *ppi)
271 const int hash_idx = ppi_hash(ppi);
273 ppi->hash_next = ppi_hash_table[hash_idx];
274 ppi_hash_table[hash_idx] = ppi;
277 static inline void add_process_to_list(struct per_process_info *ppi)
279 ppi->list_next = ppi_list;
284 static struct per_process_info *find_process_by_name(char *name)
286 const int hash_idx = ppi_hash_name(name);
287 struct per_process_info *ppi;
289 ppi = ppi_hash_table[hash_idx];
291 if (!strcmp(ppi->name, name))
294 ppi = ppi->hash_next;
300 static struct per_process_info *find_process_by_pid(__u32 pid)
302 const int hash_idx = ppi_hash_pid(pid);
303 struct per_process_info *ppi;
305 ppi = ppi_hash_table[hash_idx];
310 ppi = ppi->hash_next;
316 static struct per_process_info *find_process(__u32 pid, char *name)
318 struct per_process_info *ppi;
321 return find_process_by_pid(pid);
323 ppi = find_process_by_name(name);
324 if (ppi && ppi->pid != pid)
325 ppi->more_than_one = 1;
330 static inline int trace_rb_insert(struct trace *t, struct rb_root *root,
333 struct rb_node **p = &root->rb_node;
334 struct rb_node *parent = NULL;
340 __t = rb_entry(parent, struct trace, rb_node);
343 if (t->bit->time < __t->bit->time) {
346 } else if (t->bit->time > __t->bit->time) {
351 if (t->bit->device < __t->bit->device)
353 else if (t->bit->device > __t->bit->device)
355 else if (t->bit->sequence < __t->bit->sequence)
357 else /* >= sequence */
361 rb_link_node(&t->rb_node, parent, p);
362 rb_insert_color(&t->rb_node, root);
366 static inline int trace_rb_insert_sort(struct trace *t)
368 if (!trace_rb_insert(t, &rb_sort_root, 1)) {
376 static inline int trace_rb_insert_last(struct per_dev_info *pdi,struct trace *t)
378 if (!trace_rb_insert(t, &pdi->rb_last, 1)) {
379 pdi->rb_last_entries++;
386 static struct trace *trace_rb_find(dev_t device, unsigned long sequence,
387 struct rb_root *root, int order)
389 struct rb_node *n = root->rb_node;
390 struct rb_node *prev = NULL;
394 __t = rb_entry(n, struct trace, rb_node);
397 if (device < __t->bit->device)
399 else if (device > __t->bit->device)
401 else if (sequence < __t->bit->sequence)
403 else if (sequence > __t->bit->sequence)
410 * hack - the list may not be sequence ordered because some
411 * events don't have sequence and time matched. so we end up
412 * being a little off in the rb lookup here, because we don't
413 * know the time we are looking for. compensate by browsing
414 * a little ahead from the last entry to find the match
419 while (((n = rb_next(prev)) != NULL) && max--) {
420 __t = rb_entry(n, struct trace, rb_node);
422 if (__t->bit->device == device &&
423 __t->bit->sequence == sequence)
433 static inline struct trace *trace_rb_find_sort(dev_t dev, unsigned long seq)
435 return trace_rb_find(dev, seq, &rb_sort_root, 1);
438 static inline struct trace *trace_rb_find_last(struct per_dev_info *pdi,
441 return trace_rb_find(pdi->dev, seq, &pdi->rb_last, 0);
444 static inline int track_rb_insert(struct per_dev_info *pdi,struct io_track *iot)
446 struct rb_node **p = &pdi->rb_track.rb_node;
447 struct rb_node *parent = NULL;
448 struct io_track *__iot;
452 __iot = rb_entry(parent, struct io_track, rb_node);
454 if (iot->sector < __iot->sector)
456 else if (iot->sector > __iot->sector)
460 "sector alias (%Lu) on device %d,%d!\n",
461 (unsigned long long) iot->sector,
462 MAJOR(pdi->dev), MINOR(pdi->dev));
467 rb_link_node(&iot->rb_node, parent, p);
468 rb_insert_color(&iot->rb_node, &pdi->rb_track);
472 static struct io_track *__find_track(struct per_dev_info *pdi, __u64 sector)
474 struct rb_node *n = pdi->rb_track.rb_node;
475 struct io_track *__iot;
478 __iot = rb_entry(n, struct io_track, rb_node);
480 if (sector < __iot->sector)
482 else if (sector > __iot->sector)
491 static struct io_track *find_track(struct per_dev_info *pdi, __u32 pid,
492 char *comm, __u64 sector)
494 struct io_track *iot;
496 iot = __find_track(pdi, sector);
498 iot = malloc(sizeof(*iot));
500 memcpy(iot->comm, comm, sizeof(iot->comm));
501 iot->sector = sector;
502 track_rb_insert(pdi, iot);
508 static void log_track_frontmerge(struct per_dev_info *pdi,
509 struct blk_io_trace *t)
511 struct io_track *iot;
516 iot = __find_track(pdi, t->sector + t_sec(t));
519 fprintf(stderr, "merge not found for (%d,%d): %llu\n",
520 MAJOR(pdi->dev), MINOR(pdi->dev),
521 (unsigned long long) t->sector + t_sec(t));
525 rb_erase(&iot->rb_node, &pdi->rb_track);
526 iot->sector -= t_sec(t);
527 track_rb_insert(pdi, iot);
530 static void log_track_getrq(struct per_dev_info *pdi, struct blk_io_trace *t)
532 struct io_track *iot;
537 iot = find_track(pdi, t->pid, t->comm, t->sector);
538 iot->allocation_time = t->time;
542 * return time between rq allocation and insertion
544 static unsigned long long log_track_insert(struct per_dev_info *pdi,
545 struct blk_io_trace *t)
547 unsigned long long elapsed;
548 struct io_track *iot;
553 iot = find_track(pdi, t->pid, t->comm, t->sector);
554 iot->queue_time = t->time;
556 if (!iot->allocation_time)
559 elapsed = iot->queue_time - iot->allocation_time;
561 if (per_process_stats) {
562 struct per_process_info *ppi = find_process(iot->pid,iot->comm);
563 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
565 if (ppi && elapsed > ppi->longest_allocation_wait[w])
566 ppi->longest_allocation_wait[w] = elapsed;
573 * return time between queue and issue
575 static unsigned long long log_track_issue(struct per_dev_info *pdi,
576 struct blk_io_trace *t)
578 unsigned long long elapsed;
579 struct io_track *iot;
583 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
586 iot = __find_track(pdi, t->sector);
589 fprintf(stderr, "issue not found for (%d,%d): %llu\n",
590 MAJOR(pdi->dev), MINOR(pdi->dev),
591 (unsigned long long) t->sector);
595 iot->dispatch_time = t->time;
596 elapsed = iot->dispatch_time - iot->queue_time;
598 if (per_process_stats) {
599 struct per_process_info *ppi = find_process(iot->pid,iot->comm);
600 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
602 if (ppi && elapsed > ppi->longest_dispatch_wait[w])
603 ppi->longest_dispatch_wait[w] = elapsed;
610 * return time between dispatch and complete
612 static unsigned long long log_track_complete(struct per_dev_info *pdi,
613 struct blk_io_trace *t)
615 unsigned long long elapsed;
616 struct io_track *iot;
620 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
623 iot = __find_track(pdi, t->sector);
626 fprintf(stderr,"complete not found for (%d,%d): %llu\n",
627 MAJOR(pdi->dev), MINOR(pdi->dev),
628 (unsigned long long) t->sector);
632 iot->completion_time = t->time;
633 elapsed = iot->completion_time - iot->dispatch_time;
635 if (per_process_stats) {
636 struct per_process_info *ppi = find_process(iot->pid,iot->comm);
637 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
639 if (ppi && elapsed > ppi->longest_completion_wait[w])
640 ppi->longest_completion_wait[w] = elapsed;
644 * kill the trace, we don't need it after completion
646 rb_erase(&iot->rb_node, &pdi->rb_track);
653 static struct io_stats *find_process_io_stats(__u32 pid, char *name)
655 struct per_process_info *ppi = find_process(pid, name);
658 ppi = malloc(sizeof(*ppi));
659 memset(ppi, 0, sizeof(*ppi));
660 memcpy(ppi->name, name, 16);
662 add_process_to_hash(ppi);
663 add_process_to_list(ppi);
666 return &ppi->io_stats;
669 static void resize_cpu_info(struct per_dev_info *pdi, int cpu)
671 struct per_cpu_info *cpus = pdi->cpus;
672 int ncpus = pdi->ncpus;
673 int new_count = cpu + 1;
677 size = new_count * sizeof(struct per_cpu_info);
678 cpus = realloc(cpus, size);
681 fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n",
682 get_dev_name(pdi, name, sizeof(name)), size);
686 new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info));
687 new_space = (new_count - ncpus) * sizeof(struct per_cpu_info);
688 memset(new_start, 0, new_space);
690 pdi->ncpus = new_count;
694 static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu)
696 struct per_cpu_info *pci;
698 if (cpu >= pdi->ncpus)
699 resize_cpu_info(pdi, cpu);
701 pci = &pdi->cpus[cpu];
707 static int resize_devices(char *name)
709 int size = (ndevices + 1) * sizeof(struct per_dev_info);
711 devices = realloc(devices, size);
713 fprintf(stderr, "Out of memory, device %s (%d)\n", name, size);
716 memset(&devices[ndevices], 0, sizeof(struct per_dev_info));
717 devices[ndevices].name = name;
722 static struct per_dev_info *get_dev_info(dev_t dev)
724 struct per_dev_info *pdi;
727 for (i = 0; i < ndevices; i++) {
729 devices[i].dev = dev;
730 if (devices[i].dev == dev)
734 if (resize_devices(NULL))
737 pdi = &devices[ndevices - 1];
739 pdi->last_sequence = -1;
740 pdi->last_read_time = 0;
741 memset(&pdi->rb_last, 0, sizeof(pdi->rb_last));
742 pdi->rb_last_entries = 0;
746 static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size)
749 snprintf(buffer, size, "%s", pdi->name);
751 snprintf(buffer, size, "%d,%d",MAJOR(pdi->dev),MINOR(pdi->dev));
755 static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit)
757 unsigned long long this = bit->time;
758 unsigned long long last = pdi->last_reported_time;
760 pdi->backwards = (this < last) ? 'B' : ' ';
761 pdi->last_reported_time = this;
764 static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t,
769 ios->qwrite_kb += t_kb(t);
772 ios->qread_kb += t_kb(t);
776 static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci,
779 __account_m(&pci->io_stats, t, rw);
781 if (per_process_stats) {
782 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
784 __account_m(ios, t, rw);
788 static inline void __account_queue(struct io_stats *ios, struct blk_io_trace *t,
793 ios->qwrite_kb += t_kb(t);
796 ios->qread_kb += t_kb(t);
800 static inline void account_queue(struct blk_io_trace *t,
801 struct per_cpu_info *pci, int rw)
803 __account_queue(&pci->io_stats, t, rw);
805 if (per_process_stats) {
806 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
808 __account_queue(ios, t, rw);
812 static inline void __account_c(struct io_stats *ios, int rw, unsigned int bytes)
816 ios->cwrite_kb += bytes >> 10;
819 ios->cread_kb += bytes >> 10;
823 static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci,
826 __account_c(&pci->io_stats, rw, bytes);
828 if (per_process_stats) {
829 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
831 __account_c(ios, rw, bytes);
835 static inline void __account_issue(struct io_stats *ios, int rw,
840 ios->iwrite_kb += bytes >> 10;
843 ios->iread_kb += bytes >> 10;
847 static inline void account_issue(struct blk_io_trace *t,
848 struct per_cpu_info *pci, int rw)
850 __account_issue(&pci->io_stats, rw, t->bytes);
852 if (per_process_stats) {
853 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
855 __account_issue(ios, rw, t->bytes);
859 static inline void __account_unplug(struct io_stats *ios, int timer)
862 ios->timer_unplugs++;
867 static inline void account_unplug(struct blk_io_trace *t,
868 struct per_cpu_info *pci, int timer)
870 __account_unplug(&pci->io_stats, timer);
872 if (per_process_stats) {
873 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
875 __account_unplug(ios, timer);
879 static void log_complete(struct per_dev_info *pdi, struct per_cpu_info *pci,
880 struct blk_io_trace *t, char *act)
882 process_fmt(act, pci, t, log_track_complete(pdi, t), 0, NULL);
885 static void log_insert(struct per_dev_info *pdi, struct per_cpu_info *pci,
886 struct blk_io_trace *t, char *act)
888 process_fmt(act, pci, t, log_track_insert(pdi, t), 0, NULL);
891 static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t,
894 process_fmt(act, pci, t, -1, 0, NULL);
897 static void log_issue(struct per_dev_info *pdi, struct per_cpu_info *pci,
898 struct blk_io_trace *t, char *act)
900 process_fmt(act, pci, t, log_track_issue(pdi, t), 0, NULL);
903 static void log_merge(struct per_dev_info *pdi, struct per_cpu_info *pci,
904 struct blk_io_trace *t, char *act)
907 log_track_frontmerge(pdi, t);
909 process_fmt(act, pci, t, -1ULL, 0, NULL);
912 static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t,
915 process_fmt(act, pci, t, -1ULL, 0, NULL);
918 static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t,
921 process_fmt(act, pci, t, -1ULL, 0, NULL);
924 static void log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t,
927 process_fmt(act, pci, t, -1ULL, 0, NULL);
930 static void log_split(struct per_cpu_info *pci, struct blk_io_trace *t,
933 process_fmt(act, pci, t, -1ULL, 0, NULL);
936 static void log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act)
938 unsigned char *buf = (unsigned char *) t + sizeof(*t);
940 process_fmt(act, pci, t, -1ULL, t->pdu_len, buf);
943 static void dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci)
945 int act = t->action & 0xffff;
949 log_generic(pci, t, "Q");
952 log_generic(pci, t, "G");
954 case __BLK_TA_SLEEPRQ:
955 log_generic(pci, t, "S");
957 case __BLK_TA_REQUEUE:
958 log_generic(pci, t, "R");
963 case __BLK_TA_COMPLETE:
966 case __BLK_TA_INSERT:
970 fprintf(stderr, "Bad pc action %x\n", act);
975 static void dump_trace_fs(struct blk_io_trace *t, struct per_dev_info *pdi,
976 struct per_cpu_info *pci)
978 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
979 int act = t->action & 0xffff;
983 account_queue(t, pci, w);
984 log_queue(pci, t, "Q");
986 case __BLK_TA_INSERT:
987 log_insert(pdi, pci, t, "I");
989 case __BLK_TA_BACKMERGE:
990 account_m(t, pci, w);
991 log_merge(pdi, pci, t, "M");
993 case __BLK_TA_FRONTMERGE:
994 account_m(t, pci, w);
995 log_merge(pdi, pci, t, "F");
998 log_track_getrq(pdi, t);
999 log_generic(pci, t, "G");
1001 case __BLK_TA_SLEEPRQ:
1002 log_generic(pci, t, "S");
1004 case __BLK_TA_REQUEUE:
1005 account_c(t, pci, w, -t->bytes);
1006 log_queue(pci, t, "R");
1008 case __BLK_TA_ISSUE:
1009 account_issue(t, pci, w);
1010 log_issue(pdi, pci, t, "D");
1012 case __BLK_TA_COMPLETE:
1013 account_c(t, pci, w, t->bytes);
1014 log_complete(pdi, pci, t, "C");
1017 log_action(pci, t, "P");
1019 case __BLK_TA_UNPLUG_IO:
1020 account_unplug(t, pci, 0);
1021 log_unplug(pci, t, "U");
1023 case __BLK_TA_UNPLUG_TIMER:
1024 account_unplug(t, pci, 1);
1025 log_unplug(pci, t, "UT");
1027 case __BLK_TA_SPLIT:
1028 log_split(pci, t, "X");
1030 case __BLK_TA_BOUNCE:
1031 log_generic(pci, t, "B");
1033 case __BLK_TA_REMAP:
1034 log_generic(pci, t, "A");
1037 fprintf(stderr, "Bad fs action %x\n", t->action);
1042 static void dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
1043 struct per_dev_info *pdi)
1045 if (t->action & BLK_TC_ACT(BLK_TC_PC))
1046 dump_trace_pc(t, pci);
1048 dump_trace_fs(t, pdi, pci);
1053 static void dump_io_stats(struct io_stats *ios, char *msg)
1055 fprintf(ofp, "%s\n", msg);
1057 fprintf(ofp, " Reads Queued: %'8lu, %'8LuKiB\t", ios->qreads, ios->qread_kb);
1058 fprintf(ofp, " Writes Queued: %'8lu, %'8LuKiB\n", ios->qwrites,ios->qwrite_kb);
1060 fprintf(ofp, " Read Dispatches: %'8lu, %'8LuKiB\t", ios->ireads, ios->iread_kb);
1061 fprintf(ofp, " Write Dispatches: %'8lu, %'8LuKiB\n", ios->iwrites,ios->iwrite_kb);
1062 fprintf(ofp, " Reads Completed: %'8lu, %'8LuKiB\t", ios->creads, ios->cread_kb);
1063 fprintf(ofp, " Writes Completed: %'8lu, %'8LuKiB\n", ios->cwrites,ios->cwrite_kb);
1064 fprintf(ofp, " Read Merges: %'8lu%8c\t", ios->mreads, ' ');
1065 fprintf(ofp, " Write Merges: %'8lu\n", ios->mwrites);
1066 fprintf(ofp, " IO unplugs: %'8lu%8c\t", ios->io_unplugs, ' ');
1067 fprintf(ofp, " Timer unplugs: %'8lu\n", ios->timer_unplugs);
1070 static void dump_wait_stats(struct per_process_info *ppi)
1072 unsigned long rawait = ppi->longest_allocation_wait[0] / 1000;
1073 unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000;
1074 unsigned long rcwait = ppi->longest_completion_wait[0] / 1000;
1075 unsigned long wawait = ppi->longest_allocation_wait[1] / 1000;
1076 unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000;
1077 unsigned long wcwait = ppi->longest_completion_wait[1] / 1000;
1079 fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' ');
1080 fprintf(ofp, " Allocation wait: %'8lu\n", wawait);
1081 fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' ');
1082 fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait);
1083 fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' ');
1084 fprintf(ofp, " Completion wait: %'8lu\n", wcwait);
1087 static int ppi_name_compare(const void *p1, const void *p2)
1089 struct per_process_info *ppi1 = *((struct per_process_info **) p1);
1090 struct per_process_info *ppi2 = *((struct per_process_info **) p2);
1093 res = strverscmp(ppi1->name, ppi2->name);
1095 res = ppi1->pid > ppi2->pid;
1100 static void sort_process_list(void)
1102 struct per_process_info **ppis;
1103 struct per_process_info *ppi;
1106 ppis = malloc(ppi_list_entries * sizeof(struct per_process_info *));
1111 ppi = ppi->list_next;
1114 qsort(ppis, ppi_list_entries, sizeof(ppi), ppi_name_compare);
1116 i = ppi_list_entries - 1;
1121 ppi->list_next = ppi_list;
1129 static void show_process_stats(void)
1131 struct per_process_info *ppi;
1133 sort_process_list();
1139 if (ppi->more_than_one)
1140 sprintf(name, "%s (%u, ...)", ppi->name, ppi->pid);
1142 sprintf(name, "%s (%u)", ppi->name, ppi->pid);
1144 dump_io_stats(&ppi->io_stats, name);
1145 dump_wait_stats(ppi);
1146 ppi = ppi->list_next;
1152 static void show_device_and_cpu_stats(void)
1154 struct per_dev_info *pdi;
1155 struct per_cpu_info *pci;
1156 struct io_stats total, *ios;
1157 int i, j, pci_events;
1158 char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3];
1161 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
1163 memset(&total, 0, sizeof(total));
1169 for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) {
1173 ios = &pci->io_stats;
1174 total.qreads += ios->qreads;
1175 total.qwrites += ios->qwrites;
1176 total.creads += ios->creads;
1177 total.cwrites += ios->cwrites;
1178 total.mreads += ios->mreads;
1179 total.mwrites += ios->mwrites;
1180 total.ireads += ios->ireads;
1181 total.iwrites += ios->iwrites;
1182 total.qread_kb += ios->qread_kb;
1183 total.qwrite_kb += ios->qwrite_kb;
1184 total.cread_kb += ios->cread_kb;
1185 total.cwrite_kb += ios->cwrite_kb;
1186 total.iread_kb += ios->iread_kb;
1187 total.iwrite_kb += ios->iwrite_kb;
1188 total.timer_unplugs += ios->timer_unplugs;
1189 total.io_unplugs += ios->io_unplugs;
1191 snprintf(line, sizeof(line) - 1, "CPU%d (%s):",
1192 j, get_dev_name(pdi, name, sizeof(name)));
1193 dump_io_stats(ios, line);
1197 if (pci_events > 1) {
1199 snprintf(line, sizeof(line) - 1, "Total (%s):",
1200 get_dev_name(pdi, name, sizeof(name)));
1201 dump_io_stats(&total, line);
1204 fprintf(ofp, "\nEvents (%s): %'Lu entries, %'lu skips\n",
1205 get_dev_name(pdi, line, sizeof(line)), pdi->events,
1211 * struct trace and blktrace allocation cache, we do potentially
1212 * millions of mallocs for these structures while only using at most
1213 * a few thousand at the time
1215 static inline void t_free(struct trace *t)
1217 if (t_alloc_cache < 1024) {
1218 t->next = t_alloc_list;
1225 static inline struct trace *t_alloc(void)
1227 struct trace *t = t_alloc_list;
1230 t_alloc_list = t->next;
1235 return malloc(sizeof(*t));
1238 static inline void bit_free(struct blk_io_trace *bit)
1240 if (bit_alloc_cache < 1024) {
1242 * abuse a 64-bit field for a next pointer for the free item
1244 bit->time = (__u64) (unsigned long) bit_alloc_list;
1245 bit_alloc_list = (struct blk_io_trace *) bit;
1251 static inline struct blk_io_trace *bit_alloc(void)
1253 struct blk_io_trace *bit = bit_alloc_list;
1256 bit_alloc_list = (struct blk_io_trace *) (unsigned long) \
1262 return malloc(sizeof(*bit));
1265 static void find_genesis(void)
1267 struct trace *t = trace_list;
1269 genesis_time = -1ULL;
1271 if (t->bit->time < genesis_time)
1272 genesis_time = t->bit->time;
1278 static inline int check_stopwatch(struct blk_io_trace *bit)
1280 if (bit->time < stopwatch_end &&
1281 bit->time >= stopwatch_start)
1288 * return youngest entry read
1290 static int sort_entries(unsigned long long *youngest)
1298 while ((t = trace_list) != NULL) {
1299 struct blk_io_trace *bit = t->bit;
1301 trace_list = t->next;
1303 bit->time -= genesis_time;
1305 if (bit->time < *youngest || !*youngest)
1306 *youngest = bit->time;
1308 if (check_stopwatch(bit)) {
1314 if (trace_rb_insert_sort(t))
1317 if (bit->sequence < smallest_seq_read)
1318 smallest_seq_read = bit->sequence;
1324 static inline void __put_trace_last(struct per_dev_info *pdi, struct trace *t)
1326 rb_erase(&t->rb_node, &pdi->rb_last);
1327 pdi->rb_last_entries--;
1333 static void put_trace(struct per_dev_info *pdi, struct trace *t)
1335 rb_erase(&t->rb_node, &rb_sort_root);
1338 trace_rb_insert_last(pdi, t);
1340 if (pdi->rb_last_entries > rb_batch * pdi->nfiles) {
1341 struct rb_node *n = rb_first(&pdi->rb_last);
1343 t = rb_entry(n, struct trace, rb_node);
1344 __put_trace_last(pdi, t);
1348 static int check_sequence(struct per_dev_info *pdi, struct trace *t, int force)
1350 unsigned long expected_sequence = pdi->last_sequence + 1;
1351 struct blk_io_trace *bit = t->bit;
1355 * first entry, always ok
1357 if (!expected_sequence)
1360 if (bit->sequence == expected_sequence)
1364 * we may not have seen that sequence yet. if we are not doing
1365 * the final run, break and wait for more entries.
1367 if (expected_sequence < smallest_seq_read) {
1368 __t = trace_rb_find_last(pdi, expected_sequence);
1372 __put_trace_last(pdi, __t);
1374 } else if (!force) {
1379 fprintf(stderr, "(%d,%d): skipping %lu -> %u\n",
1380 MAJOR(pdi->dev), MINOR(pdi->dev),
1381 pdi->last_sequence, bit->sequence);
1388 static void show_entries_rb(int force)
1390 struct per_dev_info *pdi = NULL;
1391 struct per_cpu_info *pci = NULL;
1392 struct blk_io_trace *bit;
1396 while ((n = rb_first(&rb_sort_root)) != NULL) {
1397 if (is_done() && !force && !pipeline)
1400 t = rb_entry(n, struct trace, rb_node);
1403 if (!pdi || pdi->dev != bit->device)
1404 pdi = get_dev_info(bit->device);
1407 fprintf(stderr, "Unknown device ID? (%d,%d)\n",
1408 MAJOR(bit->device), MINOR(bit->device));
1412 if (check_sequence(pdi, t, force))
1415 if (!force && bit->time > last_allowed_time)
1418 pdi->last_sequence = bit->sequence;
1420 check_time(pdi, bit);
1422 if (!pci || pci->cpu != bit->cpu)
1423 pci = get_cpu_info(pdi, bit->cpu);
1427 if (bit->action & (act_mask << BLK_TC_SHIFT))
1428 dump_trace(bit, pci, pdi);
1434 static int read_data(int fd, void *buffer, int bytes, int block)
1436 int ret, bytes_left, fl;
1439 fl = fcntl(fd, F_GETFL);
1442 fcntl(fd, F_SETFL, fl | O_NONBLOCK);
1444 fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
1448 while (bytes_left > 0) {
1449 ret = read(fd, p, bytes_left);
1453 if (errno != EAGAIN)
1466 static int read_events(int fd, int always_block)
1468 struct per_dev_info *pdi = NULL;
1469 unsigned int events = 0;
1471 while (!is_done() && events < rb_batch) {
1472 struct blk_io_trace *bit;
1479 if (read_data(fd, bit, sizeof(*bit), !events || always_block))
1482 magic = be32_to_cpu(bit->magic);
1483 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
1484 fprintf(stderr, "Bad magic %x\n", magic);
1488 pdu_len = be16_to_cpu(bit->pdu_len);
1490 void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
1492 if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1))
1500 if (verify_trace(bit)) {
1506 memset(t, 0, sizeof(*t));
1509 t->next = trace_list;
1512 if (!pdi || pdi->dev != bit->device)
1513 pdi = get_dev_info(bit->device);
1515 if (bit->time > pdi->last_read_time)
1516 pdi->last_read_time = bit->time;
1524 static int do_file(void)
1526 struct per_cpu_info *pci;
1527 struct per_dev_info *pdi;
1528 int i, j, events, events_added;
1531 * first prepare all files for reading
1533 for (i = 0; i < ndevices; i++) {
1536 pdi->last_sequence = -1;
1542 pci = get_cpu_info(pdi, j);
1547 len = sprintf(pci->fname, "%s/", input_dir);
1549 snprintf(pci->fname + len, sizeof(pci->fname)-1-len,
1550 "%s.blktrace.%d", pdi->name, pci->cpu);
1551 if (stat(pci->fname, &st) < 0)
1554 pci->fd = open(pci->fname, O_RDONLY);
1561 printf("Input file %s added\n", pci->fname);
1567 * now loop over the files reading in the data
1570 unsigned long long youngest;
1573 last_allowed_time = -1ULL;
1574 smallest_seq_read = -1U;
1576 for (i = 0; i < ndevices; i++) {
1579 for (j = 0; j < pdi->nfiles; j++) {
1581 pci = get_cpu_info(pdi, j);
1586 events = read_events(pci->fd, 1);
1593 if (pdi->last_read_time < last_allowed_time)
1594 last_allowed_time = pdi->last_read_time;
1596 events_added += events;
1600 if (sort_entries(&youngest))
1603 if (youngest > stopwatch_end)
1608 } while (events_added);
1610 if (rb_sort_entries)
1616 static int do_stdin(void)
1618 unsigned long long youngest;
1621 last_allowed_time = -1ULL;
1622 fd = dup(STDIN_FILENO);
1624 perror("dup stdin");
1628 while ((events = read_events(fd, 0)) != 0) {
1630 smallest_seq_read = -1U;
1632 if (sort_entries(&youngest))
1635 if (youngest > stopwatch_end)
1641 if (rb_sort_entries)
1648 static void show_stats(void)
1657 if (per_process_stats)
1658 show_process_stats();
1660 if (per_device_and_cpu_stats)
1661 show_device_and_cpu_stats();
1666 static void handle_sigint(__attribute__((__unused__)) int sig)
1673 * Extract start and duration times from a string, allowing
1674 * us to specify a time interval of interest within a trace.
1675 * Format: "duration" (start is zero) or "start:duration".
1677 static int find_stopwatch_interval(char *string)
1682 value = strtod(string, &sp);
1684 fprintf(stderr,"Invalid stopwatch timer: %s\n", string);
1688 stopwatch_start = DOUBLE_TO_NANO_ULL(value);
1690 value = strtod(string, &sp);
1691 if (sp == string || *sp != '\0') {
1692 fprintf(stderr,"Invalid stopwatch duration time: %s\n",
1696 } else if (*sp != '\0') {
1697 fprintf(stderr,"Invalid stopwatch start timer: %s\n", string);
1700 stopwatch_end = DOUBLE_TO_NANO_ULL(value);
1701 if (stopwatch_end <= stopwatch_start) {
1702 fprintf(stderr, "Invalid stopwatch interval: %Lu -> %Lu\n",
1703 stopwatch_start, stopwatch_end);
1710 static char usage_str[] = \
1711 "[ -i <input name> ] [-o <output name> [ -s ] [ -t ] [ -q ]\n" \
1712 "[ -w start:stop ] [ -f output format ] [ -F format spec ] [ -v] \n\n" \
1713 "\t-i Input file containing trace data, or '-' for stdin\n" \
1714 "\t-D Directory to prepend to input file names\n" \
1715 "\t-o Output file. If not given, output is stdout\n" \
1716 "\t-b stdin read batching\n" \
1717 "\t-s Show per-program io statistics\n" \
1718 "\t-n Hash processes by name, not pid\n" \
1719 "\t-t Track individual ios. Will tell you the time a request took\n" \
1720 "\t to get queued, to get dispatched, and to get completed\n" \
1721 "\t-q Quiet. Don't display any stats at the end of the trace\n" \
1722 "\t-w Only parse data between the given time interval in seconds.\n" \
1723 "\t If 'start' isn't given, blkparse defaults the start time to 0\n" \
1724 "\t -f Output format. Customize the output format. The format field\n" \
1725 "\t identifies can be found in the documentation\n" \
1726 "\t-F Format specification. Can be found in the documentation\n" \
1727 "\t-v More verbose for marginal errors\n" \
1728 "\t-V Print program version info\n\n";
1730 static void usage(char *prog)
1732 fprintf(stderr, "Usage: %s %s %s", prog, blkparse_version, usage_str);
1735 int main(int argc, char *argv[])
1738 int i, c, ret, mode;
1739 int act_mask_tmp = 0;
1741 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
1744 i = find_mask_map(optarg);
1746 fprintf(stderr,"Invalid action mask %s\n",
1754 if ((sscanf(optarg, "%x", &i) != 1) ||
1755 !valid_act_opt(i)) {
1757 "Invalid set action mask %s/0x%x\n",
1764 if (!strcmp(optarg, "-") && !pipeline)
1766 else if (resize_devices(optarg) != 0)
1773 output_name = optarg;
1776 rb_batch = atoi(optarg);
1778 rb_batch = RB_BATCH_DEFAULT;
1781 per_process_stats = 1;
1787 per_device_and_cpu_stats = 0;
1790 if (find_stopwatch_interval(optarg) != 0)
1794 set_all_format_specs(optarg);
1797 if (add_format_spec(optarg) != 0)
1801 ppi_hash_by_pid = 0;
1807 printf("%s version %s\n", argv[0], blkparse_version);
1815 while (optind < argc) {
1816 if (!strcmp(argv[optind], "-") && !pipeline)
1818 else if (resize_devices(argv[optind]) != 0)
1823 if (!pipeline && !ndevices) {
1828 if (act_mask_tmp != 0)
1829 act_mask = act_mask_tmp;
1831 memset(&rb_sort_root, 0, sizeof(rb_sort_root));
1833 signal(SIGINT, handle_sigint);
1834 signal(SIGHUP, handle_sigint);
1835 signal(SIGTERM, handle_sigint);
1837 setlocale(LC_NUMERIC, "en_US");
1840 ofp = fdopen(STDOUT_FILENO, "w");
1845 snprintf(ofname, sizeof(ofname) - 1, "%s", output_name);
1846 ofp = fopen(ofname, "w");
1855 ofp_buffer = malloc(4096);
1856 if (setvbuf(ofp, ofp_buffer, mode, 4096)) {