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 "i:o:b:stqw:f:F:vnm"
85 static struct option l_opts[] = {
88 .has_arg = required_argument,
94 .has_arg = required_argument,
100 .has_arg = required_argument,
105 .name = "per program stats",
106 .has_arg = no_argument,
112 .has_arg = no_argument,
118 .has_arg = no_argument,
124 .has_arg = required_argument,
130 .has_arg = required_argument,
135 .name = "format-spec",
136 .has_arg = required_argument,
141 .name = "hash by name",
142 .has_arg = no_argument,
148 .has_arg = no_argument,
154 .has_arg = no_argument,
161 * for sorting the displayed output
164 struct blk_io_trace *bit;
165 struct rb_node rb_node;
169 static struct rb_root rb_sort_root;
170 static unsigned long rb_sort_entries;
172 static struct trace *trace_list;
177 static struct blk_io_trace *bit_alloc_list;
178 static struct trace *t_alloc_list;
181 * for tracking individual ios
184 struct rb_node rb_node;
189 unsigned long long allocation_time;
190 unsigned long long queue_time;
191 unsigned long long dispatch_time;
192 unsigned long long completion_time;
196 static struct per_dev_info *devices;
197 static char *get_dev_name(struct per_dev_info *, char *, int);
200 static char *output_name;
202 static unsigned long long genesis_time;
203 static unsigned long long last_allowed_time;
204 static unsigned int smallest_seq_read;
205 static unsigned long long stopwatch_start; /* start from zero by default */
206 static unsigned long long stopwatch_end = ULONG_LONG_MAX; /* "infinity" */
208 static int per_process_stats;
209 static int track_ios;
210 static int ppi_hash_by_pid = 1;
211 static int print_missing;
213 static unsigned int t_alloc_cache;
214 static unsigned int bit_alloc_cache;
216 #define RB_BATCH_DEFAULT (512)
217 static unsigned int rb_batch = RB_BATCH_DEFAULT;
221 #define is_done() (*(volatile int *)(&done))
222 static volatile int done;
224 #define JHASH_RANDOM (0x3af5f2ee)
226 static inline int ppi_hash_pid(__u32 pid)
228 return jhash_1word(pid, JHASH_RANDOM) & PPI_HASH_MASK;
231 static inline int ppi_hash_name(const char *name)
233 return jhash(name, 16, JHASH_RANDOM) & PPI_HASH_MASK;
236 static inline int ppi_hash(struct per_process_info *ppi)
239 return ppi_hash_pid(ppi->pid);
241 return ppi_hash_name(ppi->name);
244 static inline void add_process_to_hash(struct per_process_info *ppi)
246 const int hash_idx = ppi_hash(ppi);
248 ppi->hash_next = ppi_hash_table[hash_idx];
249 ppi_hash_table[hash_idx] = ppi;
252 static inline void add_process_to_list(struct per_process_info *ppi)
254 ppi->list_next = ppi_list;
259 static struct per_process_info *find_process_by_name(char *name)
261 const int hash_idx = ppi_hash_name(name);
262 struct per_process_info *ppi;
264 ppi = ppi_hash_table[hash_idx];
266 if (!strcmp(ppi->name, name))
269 ppi = ppi->hash_next;
275 static struct per_process_info *find_process_by_pid(__u32 pid)
277 const int hash_idx = ppi_hash_pid(pid);
278 struct per_process_info *ppi;
280 ppi = ppi_hash_table[hash_idx];
285 ppi = ppi->hash_next;
291 static struct per_process_info *find_process(__u32 pid, char *name)
293 struct per_process_info *ppi;
296 return find_process_by_pid(pid);
298 ppi = find_process_by_name(name);
299 if (ppi && ppi->pid != pid)
300 ppi->more_than_one = 1;
305 static inline int trace_rb_insert(struct trace *t, struct rb_root *root,
308 struct rb_node **p = &root->rb_node;
309 struct rb_node *parent = NULL;
315 __t = rb_entry(parent, struct trace, rb_node);
318 if (t->bit->time < __t->bit->time) {
321 } else if (t->bit->time > __t->bit->time) {
326 if (t->bit->device < __t->bit->device)
328 else if (t->bit->device > __t->bit->device)
330 else if (t->bit->sequence < __t->bit->sequence)
332 else if (t->bit->sequence > __t->bit->sequence)
334 else if (t->bit->device == __t->bit->device) {
336 "sequence alias (%d) on device %d,%d!\n",
338 MAJOR(t->bit->device), MINOR(t->bit->device));
343 rb_link_node(&t->rb_node, parent, p);
344 rb_insert_color(&t->rb_node, root);
348 static inline int trace_rb_insert_sort(struct trace *t)
350 if (!trace_rb_insert(t, &rb_sort_root, 1)) {
358 static inline int trace_rb_insert_last(struct per_dev_info *pdi,struct trace *t)
360 if (!trace_rb_insert(t, &pdi->rb_last, 1)) {
361 pdi->rb_last_entries++;
368 static struct trace *trace_rb_find(dev_t device, unsigned long sequence,
369 struct rb_root *root, int order)
371 struct rb_node *n = root->rb_node;
372 struct rb_node *prev = NULL;
376 __t = rb_entry(n, struct trace, rb_node);
379 if (device < __t->bit->device)
381 else if (device > __t->bit->device)
383 else if (sequence < __t->bit->sequence)
385 else if (sequence > __t->bit->sequence)
392 * hack - the list may not be sequence ordered because some
393 * events don't have sequence and time matched. so we end up
394 * being a little off in the rb lookup here, because we don't
395 * know the time we are looking for. compensate by browsing
396 * a little ahead from the last entry to find the match
401 while (((n = rb_next(prev)) != NULL) && max--) {
402 __t = rb_entry(n, struct trace, rb_node);
404 if (__t->bit->device == device &&
405 __t->bit->sequence == sequence)
415 static inline struct trace *trace_rb_find_sort(dev_t dev, unsigned long seq)
417 return trace_rb_find(dev, seq, &rb_sort_root, 1);
420 static inline struct trace *trace_rb_find_last(struct per_dev_info *pdi,
423 return trace_rb_find(pdi->dev, seq, &pdi->rb_last, 0);
426 static inline int track_rb_insert(struct per_dev_info *pdi,struct io_track *iot)
428 struct rb_node **p = &pdi->rb_track.rb_node;
429 struct rb_node *parent = NULL;
430 struct io_track *__iot;
434 __iot = rb_entry(parent, struct io_track, rb_node);
436 if (iot->sector < __iot->sector)
438 else if (iot->sector > __iot->sector)
442 "sector alias (%Lu) on device %d,%d!\n",
443 (unsigned long long) iot->sector,
444 MAJOR(pdi->dev), MINOR(pdi->dev));
449 rb_link_node(&iot->rb_node, parent, p);
450 rb_insert_color(&iot->rb_node, &pdi->rb_track);
454 static struct io_track *__find_track(struct per_dev_info *pdi, __u64 sector)
456 struct rb_node *n = pdi->rb_track.rb_node;
457 struct io_track *__iot;
460 __iot = rb_entry(n, struct io_track, rb_node);
462 if (sector < __iot->sector)
464 else if (sector > __iot->sector)
473 static struct io_track *find_track(struct per_dev_info *pdi, __u32 pid,
474 char *comm, __u64 sector)
476 struct io_track *iot;
478 iot = __find_track(pdi, sector);
480 iot = malloc(sizeof(*iot));
482 memcpy(iot->comm, comm, sizeof(iot->comm));
483 iot->sector = sector;
484 track_rb_insert(pdi, iot);
490 static void log_track_frontmerge(struct per_dev_info *pdi,
491 struct blk_io_trace *t)
493 struct io_track *iot;
498 iot = __find_track(pdi, t->sector + (t->bytes >> 9));
500 fprintf(stderr, "merge not found for (%d,%d): %llu\n",
501 MAJOR(pdi->dev), MINOR(pdi->dev),
502 (unsigned long long) t->sector + (t->bytes >> 9));
506 rb_erase(&iot->rb_node, &pdi->rb_track);
507 iot->sector -= t->bytes >> 9;
508 track_rb_insert(pdi, iot);
511 static void log_track_getrq(struct per_dev_info *pdi, struct blk_io_trace *t)
513 struct io_track *iot;
518 iot = find_track(pdi, t->pid, t->comm, t->sector);
519 iot->allocation_time = t->time;
523 * return time between rq allocation and insertion
525 static unsigned long long log_track_insert(struct per_dev_info *pdi,
526 struct blk_io_trace *t)
528 unsigned long long elapsed;
529 struct io_track *iot;
534 iot = find_track(pdi, t->pid, t->comm, t->sector);
535 iot->queue_time = t->time;
537 if (!iot->allocation_time)
540 elapsed = iot->queue_time - iot->allocation_time;
542 if (per_process_stats) {
543 struct per_process_info *ppi = find_process(iot->pid,iot->comm);
544 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
546 if (ppi && elapsed > ppi->longest_allocation_wait[w])
547 ppi->longest_allocation_wait[w] = elapsed;
554 * return time between queue and issue
556 static unsigned long long log_track_issue(struct per_dev_info *pdi,
557 struct blk_io_trace *t)
559 unsigned long long elapsed;
560 struct io_track *iot;
564 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
567 iot = __find_track(pdi, t->sector);
569 fprintf(stderr, "issue not found for (%d,%d): %llu\n",
570 MAJOR(pdi->dev), MINOR(pdi->dev),
571 (unsigned long long) t->sector);
575 iot->dispatch_time = t->time;
576 elapsed = iot->dispatch_time - iot->queue_time;
578 if (per_process_stats) {
579 struct per_process_info *ppi = find_process(iot->pid,iot->comm);
580 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
582 if (ppi && elapsed > ppi->longest_dispatch_wait[w])
583 ppi->longest_dispatch_wait[w] = elapsed;
590 * return time between dispatch and complete
592 static unsigned long long log_track_complete(struct per_dev_info *pdi,
593 struct blk_io_trace *t)
595 unsigned long long elapsed;
596 struct io_track *iot;
600 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
603 iot = __find_track(pdi, t->sector);
605 fprintf(stderr, "complete not found for (%d,%d): %llu\n",
606 MAJOR(pdi->dev), MINOR(pdi->dev),
607 (unsigned long long) t->sector);
611 iot->completion_time = t->time;
612 elapsed = iot->completion_time - iot->dispatch_time;
614 if (per_process_stats) {
615 struct per_process_info *ppi = find_process(iot->pid,iot->comm);
616 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
618 if (ppi && elapsed > ppi->longest_completion_wait[w])
619 ppi->longest_completion_wait[w] = elapsed;
623 * kill the trace, we don't need it after completion
625 rb_erase(&iot->rb_node, &pdi->rb_track);
632 static struct io_stats *find_process_io_stats(__u32 pid, char *name)
634 struct per_process_info *ppi = find_process(pid, name);
637 ppi = malloc(sizeof(*ppi));
638 memset(ppi, 0, sizeof(*ppi));
639 memcpy(ppi->name, name, 16);
641 add_process_to_hash(ppi);
642 add_process_to_list(ppi);
645 return &ppi->io_stats;
648 static void resize_cpu_info(struct per_dev_info *pdi, int cpu)
650 struct per_cpu_info *cpus = pdi->cpus;
651 int ncpus = pdi->ncpus;
652 int new_count = cpu + 1;
656 size = new_count * sizeof(struct per_cpu_info);
657 cpus = realloc(cpus, size);
660 fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n",
661 get_dev_name(pdi, name, sizeof(name)), size);
665 new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info));
666 new_space = (new_count - ncpus) * sizeof(struct per_cpu_info);
667 memset(new_start, 0, new_space);
669 pdi->ncpus = new_count;
673 static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu)
675 struct per_cpu_info *pci;
677 if (cpu >= pdi->ncpus)
678 resize_cpu_info(pdi, cpu);
680 pci = &pdi->cpus[cpu];
686 static int resize_devices(char *name)
688 int size = (ndevices + 1) * sizeof(struct per_dev_info);
690 devices = realloc(devices, size);
692 fprintf(stderr, "Out of memory, device %s (%d)\n", name, size);
695 memset(&devices[ndevices], 0, sizeof(struct per_dev_info));
696 devices[ndevices].name = name;
701 static struct per_dev_info *get_dev_info(dev_t dev)
703 struct per_dev_info *pdi;
706 for (i = 0; i < ndevices; i++) {
708 devices[i].dev = dev;
709 if (devices[i].dev == dev)
713 if (resize_devices(NULL))
716 pdi = &devices[ndevices - 1];
718 pdi->last_sequence = -1;
719 pdi->last_read_time = 0;
720 memset(&pdi->rb_last, 0, sizeof(pdi->rb_last));
721 pdi->rb_last_entries = 0;
725 static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size)
728 snprintf(buffer, size, "%s", pdi->name);
730 snprintf(buffer, size, "%d,%d",MAJOR(pdi->dev),MINOR(pdi->dev));
734 static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit)
736 unsigned long long this = bit->time;
737 unsigned long long last = pdi->last_reported_time;
739 pdi->backwards = (this < last) ? 'B' : ' ';
740 pdi->last_reported_time = this;
743 static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t,
748 ios->qwrite_kb += t->bytes >> 10;
751 ios->qread_kb += t->bytes >> 10;
755 static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci,
758 __account_m(&pci->io_stats, t, rw);
760 if (per_process_stats) {
761 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
763 __account_m(ios, t, rw);
767 static inline void __account_queue(struct io_stats *ios, struct blk_io_trace *t,
772 ios->qwrite_kb += t->bytes >> 10;
775 ios->qread_kb += t->bytes >> 10;
779 static inline void account_queue(struct blk_io_trace *t,
780 struct per_cpu_info *pci, int rw)
782 __account_queue(&pci->io_stats, t, rw);
784 if (per_process_stats) {
785 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
787 __account_queue(ios, t, rw);
791 static inline void __account_c(struct io_stats *ios, int rw, unsigned int bytes)
795 ios->cwrite_kb += bytes >> 10;
798 ios->cread_kb += bytes >> 10;
802 static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci,
805 __account_c(&pci->io_stats, rw, bytes);
807 if (per_process_stats) {
808 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
810 __account_c(ios, rw, bytes);
814 static inline void __account_issue(struct io_stats *ios, int rw,
819 ios->iwrite_kb += bytes >> 10;
822 ios->iread_kb += bytes >> 10;
826 static inline void account_issue(struct blk_io_trace *t,
827 struct per_cpu_info *pci, int rw)
829 __account_issue(&pci->io_stats, rw, t->bytes);
831 if (per_process_stats) {
832 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
834 __account_issue(ios, rw, t->bytes);
838 static inline void __account_unplug(struct io_stats *ios, int timer)
841 ios->timer_unplugs++;
846 static inline void account_unplug(struct blk_io_trace *t,
847 struct per_cpu_info *pci, int timer)
849 __account_unplug(&pci->io_stats, timer);
851 if (per_process_stats) {
852 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
854 __account_unplug(ios, timer);
858 static void log_complete(struct per_dev_info *pdi, struct per_cpu_info *pci,
859 struct blk_io_trace *t, char *act)
861 process_fmt(act, pci, t, log_track_complete(pdi, t), 0, NULL);
864 static void log_insert(struct per_dev_info *pdi, struct per_cpu_info *pci,
865 struct blk_io_trace *t, char *act)
867 process_fmt(act, pci, t, log_track_insert(pdi, t), 0, NULL);
870 static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t,
873 process_fmt(act, pci, t, -1, 0, NULL);
876 static void log_issue(struct per_dev_info *pdi, struct per_cpu_info *pci,
877 struct blk_io_trace *t, char *act)
879 process_fmt(act, pci, t, log_track_issue(pdi, t), 0, NULL);
882 static void log_merge(struct per_dev_info *pdi, struct per_cpu_info *pci,
883 struct blk_io_trace *t, char *act)
886 log_track_frontmerge(pdi, t);
888 process_fmt(act, pci, t, -1ULL, 0, NULL);
891 static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t,
894 process_fmt(act, pci, t, -1ULL, 0, NULL);
897 static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t,
900 process_fmt(act, pci, t, -1ULL, 0, NULL);
903 static void log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t,
906 process_fmt(act, pci, t, -1ULL, 0, NULL);
909 static void log_split(struct per_cpu_info *pci, struct blk_io_trace *t,
912 process_fmt(act, pci, t, -1ULL, 0, NULL);
915 static void log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act)
917 unsigned char *buf = (unsigned char *) t + sizeof(*t);
919 process_fmt(act, pci, t, -1ULL, t->pdu_len, buf);
922 static void dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci)
924 int act = t->action & 0xffff;
928 log_generic(pci, t, "Q");
931 log_generic(pci, t, "G");
933 case __BLK_TA_SLEEPRQ:
934 log_generic(pci, t, "S");
936 case __BLK_TA_REQUEUE:
937 log_generic(pci, t, "R");
942 case __BLK_TA_COMPLETE:
945 case __BLK_TA_INSERT:
949 fprintf(stderr, "Bad pc action %x\n", act);
954 static void dump_trace_fs(struct blk_io_trace *t, struct per_dev_info *pdi,
955 struct per_cpu_info *pci)
957 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
958 int act = t->action & 0xffff;
962 account_queue(t, pci, w);
963 log_queue(pci, t, "Q");
965 case __BLK_TA_INSERT:
966 log_insert(pdi, pci, t, "I");
968 case __BLK_TA_BACKMERGE:
969 account_m(t, pci, w);
970 log_merge(pdi, pci, t, "M");
972 case __BLK_TA_FRONTMERGE:
973 account_m(t, pci, w);
974 log_merge(pdi, pci, t, "F");
977 log_track_getrq(pdi, t);
978 log_generic(pci, t, "G");
980 case __BLK_TA_SLEEPRQ:
981 log_generic(pci, t, "S");
983 case __BLK_TA_REQUEUE:
984 account_c(t, pci, w, -t->bytes);
985 log_queue(pci, t, "R");
988 account_issue(t, pci, w);
989 log_issue(pdi, pci, t, "D");
991 case __BLK_TA_COMPLETE:
992 account_c(t, pci, w, t->bytes);
993 log_complete(pdi, pci, t, "C");
996 log_action(pci, t, "P");
998 case __BLK_TA_UNPLUG_IO:
999 account_unplug(t, pci, 0);
1000 log_unplug(pci, t, "U");
1002 case __BLK_TA_UNPLUG_TIMER:
1003 account_unplug(t, pci, 1);
1004 log_unplug(pci, t, "UT");
1006 case __BLK_TA_SPLIT:
1007 log_split(pci, t, "X");
1009 case __BLK_TA_BOUNCE:
1010 log_generic(pci, t, "B");
1013 fprintf(stderr, "Bad fs action %x\n", t->action);
1018 static void dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
1019 struct per_dev_info *pdi)
1021 if (t->action & BLK_TC_ACT(BLK_TC_PC))
1022 dump_trace_pc(t, pci);
1024 dump_trace_fs(t, pdi, pci);
1029 static void dump_io_stats(struct io_stats *ios, char *msg)
1031 fprintf(ofp, "%s\n", msg);
1033 fprintf(ofp, " Reads Queued: %'8lu, %'8LuKiB\t", ios->qreads, ios->qread_kb);
1034 fprintf(ofp, " Writes Queued: %'8lu, %'8LuKiB\n", ios->qwrites,ios->qwrite_kb);
1036 fprintf(ofp, " Read Dispatches: %'8lu, %'8LuKiB\t", ios->ireads, ios->iread_kb);
1037 fprintf(ofp, " Write Dispatches: %'8lu, %'8LuKiB\n", ios->iwrites,ios->iwrite_kb);
1038 fprintf(ofp, " Reads Completed: %'8lu, %'8LuKiB\t", ios->creads, ios->cread_kb);
1039 fprintf(ofp, " Writes Completed: %'8lu, %'8LuKiB\n", ios->cwrites,ios->cwrite_kb);
1040 fprintf(ofp, " Read Merges: %'8lu%8c\t", ios->mreads, ' ');
1041 fprintf(ofp, " Write Merges: %'8lu\n", ios->mwrites);
1042 fprintf(ofp, " IO unplugs: %'8lu%8c\t", ios->io_unplugs, ' ');
1043 fprintf(ofp, " Timer unplugs: %'8lu\n", ios->timer_unplugs);
1046 static void dump_wait_stats(struct per_process_info *ppi)
1048 unsigned long rawait = ppi->longest_allocation_wait[0] / 1000;
1049 unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000;
1050 unsigned long rcwait = ppi->longest_completion_wait[0] / 1000;
1051 unsigned long wawait = ppi->longest_allocation_wait[1] / 1000;
1052 unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000;
1053 unsigned long wcwait = ppi->longest_completion_wait[1] / 1000;
1055 fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' ');
1056 fprintf(ofp, " Allocation wait: %'8lu\n", wawait);
1057 fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' ');
1058 fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait);
1059 fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' ');
1060 fprintf(ofp, " Completion wait: %'8lu\n", wcwait);
1063 static int ppi_name_compare(const void *p1, const void *p2)
1065 struct per_process_info *ppi1 = *((struct per_process_info **) p1);
1066 struct per_process_info *ppi2 = *((struct per_process_info **) p2);
1069 res = strverscmp(ppi1->name, ppi2->name);
1071 res = ppi1->pid > ppi2->pid;
1076 static void sort_process_list(void)
1078 struct per_process_info **ppis;
1079 struct per_process_info *ppi;
1082 ppis = malloc(ppi_list_entries * sizeof(struct per_process_info *));
1087 ppi = ppi->list_next;
1090 qsort(ppis, ppi_list_entries, sizeof(ppi), ppi_name_compare);
1092 i = ppi_list_entries - 1;
1097 ppi->list_next = ppi_list;
1105 static void show_process_stats(void)
1107 struct per_process_info *ppi;
1109 sort_process_list();
1115 if (ppi->more_than_one)
1116 sprintf(name, "%s (%u, ...)", ppi->name, ppi->pid);
1118 sprintf(name, "%s (%u)", ppi->name, ppi->pid);
1120 dump_io_stats(&ppi->io_stats, name);
1121 dump_wait_stats(ppi);
1122 ppi = ppi->list_next;
1128 static void show_device_and_cpu_stats(void)
1130 struct per_dev_info *pdi;
1131 struct per_cpu_info *pci;
1132 struct io_stats total, *ios;
1133 int i, j, pci_events;
1134 char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3];
1137 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
1139 memset(&total, 0, sizeof(total));
1145 for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) {
1149 ios = &pci->io_stats;
1150 total.qreads += ios->qreads;
1151 total.qwrites += ios->qwrites;
1152 total.creads += ios->creads;
1153 total.cwrites += ios->cwrites;
1154 total.mreads += ios->mreads;
1155 total.mwrites += ios->mwrites;
1156 total.ireads += ios->ireads;
1157 total.iwrites += ios->iwrites;
1158 total.qread_kb += ios->qread_kb;
1159 total.qwrite_kb += ios->qwrite_kb;
1160 total.cread_kb += ios->cread_kb;
1161 total.cwrite_kb += ios->cwrite_kb;
1162 total.iread_kb += ios->iread_kb;
1163 total.iwrite_kb += ios->iwrite_kb;
1164 total.timer_unplugs += ios->timer_unplugs;
1165 total.io_unplugs += ios->io_unplugs;
1167 snprintf(line, sizeof(line) - 1, "CPU%d (%s):",
1168 j, get_dev_name(pdi, name, sizeof(name)));
1169 dump_io_stats(ios, line);
1173 if (pci_events > 1) {
1175 snprintf(line, sizeof(line) - 1, "Total (%s):",
1176 get_dev_name(pdi, name, sizeof(name)));
1177 dump_io_stats(&total, line);
1180 fprintf(ofp, "\nEvents (%s): %'Lu entries, %'lu skips\n",
1181 get_dev_name(pdi, line, sizeof(line)), pdi->events,
1187 * struct trace and blktrace allocation cache, we do potentially
1188 * millions of mallocs for these structures while only using at most
1189 * a few thousand at the time
1191 static inline void t_free(struct trace *t)
1193 if (t_alloc_cache < 1024) {
1194 t->next = t_alloc_list;
1201 static inline struct trace *t_alloc(void)
1203 struct trace *t = t_alloc_list;
1206 t_alloc_list = t->next;
1211 return malloc(sizeof(*t));
1214 static inline void bit_free(struct blk_io_trace *bit)
1216 if (bit_alloc_cache < 1024) {
1218 * abuse a 64-bit field for a next pointer for the free item
1220 bit->time = (__u64) (unsigned long) bit_alloc_list;
1221 bit_alloc_list = (struct blk_io_trace *) bit;
1227 static inline struct blk_io_trace *bit_alloc(void)
1229 struct blk_io_trace *bit = bit_alloc_list;
1232 bit_alloc_list = (struct blk_io_trace *) (unsigned long) \
1238 return malloc(sizeof(*bit));
1241 static void find_genesis(void)
1243 struct trace *t = trace_list;
1245 genesis_time = -1ULL;
1247 if (t->bit->time < genesis_time)
1248 genesis_time = t->bit->time;
1254 static inline int check_stopwatch(struct blk_io_trace *bit)
1256 if (bit->time < stopwatch_end &&
1257 bit->time >= stopwatch_start)
1264 * return youngest entry read
1266 static int sort_entries(unsigned long long *youngest)
1274 while ((t = trace_list) != NULL) {
1275 struct blk_io_trace *bit = t->bit;
1277 trace_list = t->next;
1279 bit->time -= genesis_time;
1281 if (bit->time < *youngest || !*youngest)
1282 *youngest = bit->time;
1284 if (check_stopwatch(bit)) {
1290 if (trace_rb_insert_sort(t))
1293 if (bit->sequence < smallest_seq_read)
1294 smallest_seq_read = bit->sequence;
1300 static inline void __put_trace_last(struct per_dev_info *pdi, struct trace *t)
1302 rb_erase(&t->rb_node, &pdi->rb_last);
1303 pdi->rb_last_entries--;
1309 static void put_trace(struct per_dev_info *pdi, struct trace *t)
1311 rb_erase(&t->rb_node, &rb_sort_root);
1314 trace_rb_insert_last(pdi, t);
1316 if (pdi->rb_last_entries > rb_batch * pdi->nfiles) {
1317 struct rb_node *n = rb_first(&pdi->rb_last);
1319 t = rb_entry(n, struct trace, rb_node);
1320 __put_trace_last(pdi, t);
1324 static int check_sequence(struct per_dev_info *pdi, struct blk_io_trace *bit,
1327 unsigned long expected_sequence = pdi->last_sequence + 1;
1331 * first entry, always ok
1333 if (!expected_sequence)
1336 if (bit->sequence == expected_sequence)
1340 * we may not have seen that sequence yet. if we are not doing
1341 * the final run, break and wait for more entries.
1343 if (expected_sequence < smallest_seq_read) {
1344 t = trace_rb_find_last(pdi, expected_sequence);
1348 __put_trace_last(pdi, t);
1354 if (print_missing) {
1355 fprintf(stderr, "(%d,%d): skipping %lu -> %u\n",
1356 MAJOR(pdi->dev), MINOR(pdi->dev),
1357 pdi->last_sequence, bit->sequence);
1364 static void show_entries_rb(int force)
1366 struct per_dev_info *pdi = NULL;
1367 struct per_cpu_info *pci = NULL;
1368 struct blk_io_trace *bit;
1373 n = rb_first(&rb_sort_root);
1374 t = rb_entry(n, struct trace, rb_node);
1375 fprintf(stderr, "first force %u\n", t->bit->sequence);
1378 while ((n = rb_first(&rb_sort_root)) != NULL) {
1382 t = rb_entry(n, struct trace, rb_node);
1385 if (!pdi || pdi->dev != bit->device)
1386 pdi = get_dev_info(bit->device);
1389 fprintf(stderr, "Unknown device ID? (%d,%d)\n",
1390 MAJOR(bit->device), MINOR(bit->device));
1394 if (check_sequence(pdi, bit, force))
1397 if (!force && bit->time > last_allowed_time)
1400 pdi->last_sequence = bit->sequence;
1402 check_time(pdi, bit);
1404 if (!pci || pci->cpu != bit->cpu)
1405 pci = get_cpu_info(pdi, bit->cpu);
1407 dump_trace(bit, pci, pdi);
1413 static int read_data(int fd, void *buffer, int bytes, int block)
1415 int ret, bytes_left, fl;
1418 fl = fcntl(fd, F_GETFL);
1421 fcntl(fd, F_SETFL, fl | O_NONBLOCK);
1423 fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
1427 while (bytes_left > 0) {
1428 ret = read(fd, p, bytes_left);
1432 if (errno != EAGAIN)
1445 static int read_events(int fd, int always_block)
1447 struct per_dev_info *pdi = NULL;
1448 unsigned int events = 0;
1450 while (!is_done() && events < rb_batch) {
1451 struct blk_io_trace *bit;
1458 if (read_data(fd, bit, sizeof(*bit), !events || always_block))
1461 magic = be32_to_cpu(bit->magic);
1462 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
1463 fprintf(stderr, "Bad magic %x\n", magic);
1467 pdu_len = be16_to_cpu(bit->pdu_len);
1469 void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
1471 if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1))
1479 if (verify_trace(bit)) {
1485 memset(t, 0, sizeof(*t));
1488 t->next = trace_list;
1491 if (!pdi || pdi->dev != bit->device)
1492 pdi = get_dev_info(bit->device);
1494 if (bit->time > pdi->last_read_time)
1495 pdi->last_read_time = bit->time;
1503 static int do_file(void)
1505 struct per_cpu_info *pci;
1506 struct per_dev_info *pdi;
1507 int i, j, events, events_added;
1510 * first prepare all files for reading
1512 for (i = 0; i < ndevices; i++) {
1515 pdi->last_sequence = -1;
1520 pci = get_cpu_info(pdi, j);
1524 snprintf(pci->fname, sizeof(pci->fname)-1,
1525 "%s.blktrace.%d", pdi->name, pci->cpu);
1526 if (stat(pci->fname, &st) < 0)
1529 pci->fd = open(pci->fname, O_RDONLY);
1536 printf("Input file %s added\n", pci->fname);
1542 * now loop over the files reading in the data
1545 unsigned long long youngest;
1548 last_allowed_time = -1ULL;
1549 smallest_seq_read = -1U;
1551 for (i = 0; i < ndevices; i++) {
1554 for (j = 0; j < pdi->nfiles; j++) {
1556 pci = get_cpu_info(pdi, j);
1561 events = read_events(pci->fd, 1);
1568 if (pdi->last_read_time < last_allowed_time)
1569 last_allowed_time = pdi->last_read_time;
1571 events_added += events;
1575 if (sort_entries(&youngest))
1578 if (youngest > stopwatch_end)
1583 } while (events_added);
1585 if (rb_sort_entries)
1591 static int do_stdin(void)
1593 unsigned long long youngest;
1596 last_allowed_time = -1ULL;
1597 fd = dup(STDIN_FILENO);
1601 events = read_events(fd, 0);
1605 if (sort_entries(&youngest))
1608 if (youngest > stopwatch_end)
1614 if (rb_sort_entries)
1621 static void flush_output(void)
1626 static void handle_sigint(__attribute__((__unused__)) int sig)
1633 * Extract start and duration times from a string, allowing
1634 * us to specify a time interval of interest within a trace.
1635 * Format: "duration" (start is zero) or "start:duration".
1637 static int find_stopwatch_interval(char *string)
1642 value = strtod(string, &sp);
1644 fprintf(stderr,"Invalid stopwatch timer: %s\n", string);
1648 stopwatch_start = DOUBLE_TO_NANO_ULL(value);
1650 value = strtod(string, &sp);
1651 if (sp == string || *sp != '\0') {
1652 fprintf(stderr,"Invalid stopwatch duration time: %s\n",
1656 } else if (*sp != '\0') {
1657 fprintf(stderr,"Invalid stopwatch start timer: %s\n", string);
1660 stopwatch_end = DOUBLE_TO_NANO_ULL(value);
1661 if (stopwatch_end <= stopwatch_start) {
1662 fprintf(stderr, "Invalid stopwatch interval: %Lu -> %Lu\n",
1663 stopwatch_start, stopwatch_end);
1670 static char usage_str[] = \
1671 "[ -i <input name> ] [-o <output name> [ -s ] [ -t ] [ -q ]\n" \
1672 "[ -w start:stop ] [ -f output format ] [ -F format spec ] [ -v] \n\n" \
1673 "\t-i Input file containing trace data, or '-' for stdin\n" \
1674 "\t-o Output file. If not given, output is stdout\n" \
1675 "\t-b stdin read batching\n" \
1676 "\t-s Show per-program io statistics\n" \
1677 "\t-n Hash processes by name, not pid\n" \
1678 "\t-t Track individual ios. Will tell you the time a request took\n" \
1679 "\t to get queued, to get dispatched, and to get completed\n" \
1680 "\t-q Quiet. Don't display any stats at the end of the trace\n" \
1681 "\t-w Only parse data between the given time interval in seconds.\n" \
1682 "\t If 'start' isn't given, blkparse defaults the start time to 0\n" \
1683 "\t -f Output format. Customize the output format. The format field\n" \
1684 "\t identifies can be found in the documentation\n" \
1685 "\t-F Format specification. Can be found in the documentation\n" \
1686 "\t-m Print missing entries\n" \
1687 "\t-v Print program version info\n\n";
1689 static void usage(char *prog)
1691 fprintf(stderr, "Usage: %s %s %s", prog, blkparse_version, usage_str);
1694 int main(int argc, char *argv[])
1698 int per_device_and_cpu_stats = 1;
1700 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
1703 if (!strcmp(optarg, "-") && !pipeline)
1705 else if (resize_devices(optarg) != 0)
1709 output_name = optarg;
1712 rb_batch = atoi(optarg);
1714 rb_batch = RB_BATCH_DEFAULT;
1717 per_process_stats = 1;
1723 per_device_and_cpu_stats = 0;
1726 if (find_stopwatch_interval(optarg) != 0)
1730 set_all_format_specs(optarg);
1733 if (add_format_spec(optarg) != 0)
1737 ppi_hash_by_pid = 0;
1743 printf("%s version %s\n", argv[0], blkparse_version);
1751 while (optind < argc) {
1752 if (!strcmp(argv[optind], "-") && !pipeline)
1754 else if (resize_devices(argv[optind]) != 0)
1759 if (!pipeline && !ndevices) {
1764 memset(&rb_sort_root, 0, sizeof(rb_sort_root));
1766 signal(SIGINT, handle_sigint);
1767 signal(SIGHUP, handle_sigint);
1768 signal(SIGTERM, handle_sigint);
1770 setlocale(LC_NUMERIC, "en_US");
1773 ofp = fdopen(STDOUT_FILENO, "w");
1778 snprintf(ofname, sizeof(ofname) - 1, "%s", output_name);
1779 ofp = fopen(ofname, "w");
1788 ofp_buffer = malloc(4096);
1789 if (setvbuf(ofp, ofp_buffer, mode, 4096)) {
1799 if (per_process_stats)
1800 show_process_stats();
1802 if (per_device_and_cpu_stats)
1803 show_device_and_cpu_stats();