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:vn"
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,
155 * for sorting the displayed output
158 struct blk_io_trace *bit;
159 struct rb_node rb_node;
161 unsigned int skipped;
164 static struct rb_root rb_sort_root;
165 static unsigned long rb_sort_entries;
167 static struct trace *trace_list;
172 static struct blk_io_trace *bit_alloc_list;
173 static struct trace *t_alloc_list;
176 * for tracking individual ios
179 struct rb_node rb_node;
184 unsigned long long allocation_time;
185 unsigned long long queue_time;
186 unsigned long long dispatch_time;
187 unsigned long long completion_time;
191 static struct per_dev_info *devices;
192 static char *get_dev_name(struct per_dev_info *, char *, int);
195 static char *output_name;
197 static unsigned long long genesis_time;
198 static unsigned long long last_allowed_time;
199 static unsigned long long stopwatch_start; /* start from zero by default */
200 static unsigned long long stopwatch_end = ULONG_LONG_MAX; /* "infinity" */
202 static int per_process_stats;
203 static int track_ios;
204 static int ppi_hash_by_pid = 1;
206 static unsigned long last_skipped;
207 static unsigned int skip_runs;
209 static unsigned int t_alloc_cache;
210 static unsigned int bit_alloc_cache;
212 #define RB_BATCH_DEFAULT (512)
213 static unsigned int rb_batch = RB_BATCH_DEFAULT;
217 #define is_done() (*(volatile int *)(&done))
218 static volatile int done;
220 #define JHASH_RANDOM (0x3af5f2ee)
222 static inline int ppi_hash_pid(__u32 pid)
224 return jhash_1word(pid, JHASH_RANDOM) & PPI_HASH_MASK;
227 static inline int ppi_hash_name(const char *name)
229 return jhash(name, 16, JHASH_RANDOM) & PPI_HASH_MASK;
232 static inline int ppi_hash(struct per_process_info *ppi)
235 return ppi_hash_pid(ppi->pid);
237 return ppi_hash_name(ppi->name);
240 static inline void add_process_to_hash(struct per_process_info *ppi)
242 const int hash_idx = ppi_hash(ppi);
244 ppi->hash_next = ppi_hash_table[hash_idx];
245 ppi_hash_table[hash_idx] = ppi;
248 static inline void add_process_to_list(struct per_process_info *ppi)
250 ppi->list_next = ppi_list;
255 static struct per_process_info *find_process_by_name(char *name)
257 const int hash_idx = ppi_hash_name(name);
258 struct per_process_info *ppi;
260 ppi = ppi_hash_table[hash_idx];
262 if (!strcmp(ppi->name, name))
265 ppi = ppi->hash_next;
271 static struct per_process_info *find_process_by_pid(__u32 pid)
273 const int hash_idx = ppi_hash_pid(pid);
274 struct per_process_info *ppi;
276 ppi = ppi_hash_table[hash_idx];
281 ppi = ppi->hash_next;
287 static struct per_process_info *find_process(__u32 pid, char *name)
289 struct per_process_info *ppi;
292 return find_process_by_pid(pid);
294 ppi = find_process_by_name(name);
295 if (ppi && ppi->pid != pid)
296 ppi->more_than_one = 1;
301 static inline int trace_rb_insert(struct trace *t, struct rb_root *root,
304 struct rb_node **p = &root->rb_node;
305 struct rb_node *parent = NULL;
311 __t = rb_entry(parent, struct trace, rb_node);
314 if (t->bit->time < __t->bit->time) {
317 } else if (t->bit->time > __t->bit->time) {
322 if (t->bit->device < __t->bit->device)
324 else if (t->bit->device > __t->bit->device)
326 else if (t->bit->sequence < __t->bit->sequence)
328 else if (t->bit->sequence > __t->bit->sequence)
330 else if (t->bit->device == __t->bit->device) {
332 "sequence alias (%d) on device %d,%d!\n",
334 MAJOR(t->bit->device), MINOR(t->bit->device));
339 rb_link_node(&t->rb_node, parent, p);
340 rb_insert_color(&t->rb_node, root);
344 static inline int trace_rb_insert_sort(struct trace *t)
346 if (!trace_rb_insert(t, &rb_sort_root, 1)) {
354 static inline int trace_rb_insert_last(struct per_dev_info *pdi,struct trace *t)
356 if (!trace_rb_insert(t, &pdi->rb_last, 1)) {
357 pdi->rb_last_entries++;
364 static struct trace *trace_rb_find(dev_t device, unsigned long sequence,
365 struct rb_root *root, int order)
367 struct rb_node *n = root->rb_node;
368 struct rb_node *prev = NULL;
372 __t = rb_entry(n, struct trace, rb_node);
375 if (device < __t->bit->device)
377 else if (device > __t->bit->device)
379 else if (sequence < __t->bit->sequence)
381 else if (sequence > __t->bit->sequence)
388 * hack - the list may not be sequence ordered because some
389 * events don't have sequence and time matched. so we end up
390 * being a little off in the rb lookup here, because we don't
391 * know the time we are looking for. compensate by browsing
392 * a little ahead from the last entry to find the match
397 while (((n = rb_next(prev)) != NULL) && max--) {
398 __t = rb_entry(n, struct trace, rb_node);
400 if (__t->bit->device == device &&
401 __t->bit->sequence == sequence)
411 static inline struct trace *trace_rb_find_sort(dev_t dev, unsigned long seq)
413 return trace_rb_find(dev, seq, &rb_sort_root, 1);
416 static inline struct trace *trace_rb_find_last(struct per_dev_info *pdi,
419 return trace_rb_find(pdi->dev, seq, &pdi->rb_last, 0);
422 static inline int track_rb_insert(struct per_dev_info *pdi,struct io_track *iot)
424 struct rb_node **p = &pdi->rb_track.rb_node;
425 struct rb_node *parent = NULL;
426 struct io_track *__iot;
430 __iot = rb_entry(parent, struct io_track, rb_node);
432 if (iot->sector < __iot->sector)
434 else if (iot->sector > __iot->sector)
438 "sector alias (%Lu) on device %d,%d!\n",
439 (unsigned long long) iot->sector,
440 MAJOR(pdi->dev), MINOR(pdi->dev));
445 rb_link_node(&iot->rb_node, parent, p);
446 rb_insert_color(&iot->rb_node, &pdi->rb_track);
450 static struct io_track *__find_track(struct per_dev_info *pdi, __u64 sector)
452 struct rb_node *n = pdi->rb_track.rb_node;
453 struct io_track *__iot;
456 __iot = rb_entry(n, struct io_track, rb_node);
458 if (sector < __iot->sector)
460 else if (sector > __iot->sector)
469 static struct io_track *find_track(struct per_dev_info *pdi, __u32 pid,
470 char *comm, __u64 sector)
472 struct io_track *iot;
474 iot = __find_track(pdi, sector);
476 iot = malloc(sizeof(*iot));
478 memcpy(iot->comm, comm, sizeof(iot->comm));
479 iot->sector = sector;
480 track_rb_insert(pdi, iot);
486 static void log_track_frontmerge(struct per_dev_info *pdi,
487 struct blk_io_trace *t)
489 struct io_track *iot;
494 iot = __find_track(pdi, t->sector + (t->bytes >> 9));
496 fprintf(stderr, "merge not found for (%d,%d): %llu\n",
497 MAJOR(pdi->dev), MINOR(pdi->dev),
498 (unsigned long long) t->sector + (t->bytes >> 9));
502 rb_erase(&iot->rb_node, &pdi->rb_track);
503 iot->sector -= t->bytes >> 9;
504 track_rb_insert(pdi, iot);
507 static void log_track_getrq(struct per_dev_info *pdi, struct blk_io_trace *t)
509 struct io_track *iot;
514 iot = find_track(pdi, t->pid, t->comm, t->sector);
515 iot->allocation_time = t->time;
519 * return time between rq allocation and insertion
521 static unsigned long long log_track_insert(struct per_dev_info *pdi,
522 struct blk_io_trace *t)
524 unsigned long long elapsed;
525 struct io_track *iot;
530 iot = find_track(pdi, t->pid, t->comm, t->sector);
531 iot->queue_time = t->time;
533 if (!iot->allocation_time)
536 elapsed = iot->queue_time - iot->allocation_time;
538 if (per_process_stats) {
539 struct per_process_info *ppi = find_process(iot->pid,iot->comm);
540 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
542 if (ppi && elapsed > ppi->longest_allocation_wait[w])
543 ppi->longest_allocation_wait[w] = elapsed;
550 * return time between queue and issue
552 static unsigned long long log_track_issue(struct per_dev_info *pdi,
553 struct blk_io_trace *t)
555 unsigned long long elapsed;
556 struct io_track *iot;
560 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
563 iot = __find_track(pdi, t->sector);
565 fprintf(stderr, "issue not found for (%d,%d): %llu\n",
566 MAJOR(pdi->dev), MINOR(pdi->dev),
567 (unsigned long long) t->sector);
571 iot->dispatch_time = t->time;
572 elapsed = iot->dispatch_time - iot->queue_time;
574 if (per_process_stats) {
575 struct per_process_info *ppi = find_process(iot->pid,iot->comm);
576 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
578 if (ppi && elapsed > ppi->longest_dispatch_wait[w])
579 ppi->longest_dispatch_wait[w] = elapsed;
586 * return time between dispatch and complete
588 static unsigned long long log_track_complete(struct per_dev_info *pdi,
589 struct blk_io_trace *t)
591 unsigned long long elapsed;
592 struct io_track *iot;
596 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
599 iot = __find_track(pdi, t->sector);
601 fprintf(stderr, "complete not found for (%d,%d): %llu\n",
602 MAJOR(pdi->dev), MINOR(pdi->dev),
603 (unsigned long long) t->sector);
607 iot->completion_time = t->time;
608 elapsed = iot->completion_time - iot->dispatch_time;
610 if (per_process_stats) {
611 struct per_process_info *ppi = find_process(iot->pid,iot->comm);
612 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
614 if (ppi && elapsed > ppi->longest_completion_wait[w])
615 ppi->longest_completion_wait[w] = elapsed;
619 * kill the trace, we don't need it after completion
621 rb_erase(&iot->rb_node, &pdi->rb_track);
628 static struct io_stats *find_process_io_stats(__u32 pid, char *name)
630 struct per_process_info *ppi = find_process(pid, name);
633 ppi = malloc(sizeof(*ppi));
634 memset(ppi, 0, sizeof(*ppi));
635 memcpy(ppi->name, name, 16);
637 add_process_to_hash(ppi);
638 add_process_to_list(ppi);
641 return &ppi->io_stats;
644 static void resize_cpu_info(struct per_dev_info *pdi, int cpu)
646 struct per_cpu_info *cpus = pdi->cpus;
647 int ncpus = pdi->ncpus;
648 int new_count = cpu + 1;
652 size = new_count * sizeof(struct per_cpu_info);
653 cpus = realloc(cpus, size);
656 fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n",
657 get_dev_name(pdi, name, sizeof(name)), size);
661 new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info));
662 new_space = (new_count - ncpus) * sizeof(struct per_cpu_info);
663 memset(new_start, 0, new_space);
665 pdi->ncpus = new_count;
669 static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu)
671 struct per_cpu_info *pci;
673 if (cpu >= pdi->ncpus)
674 resize_cpu_info(pdi, cpu);
676 pci = &pdi->cpus[cpu];
682 static int resize_devices(char *name)
684 int size = (ndevices + 1) * sizeof(struct per_dev_info);
686 devices = realloc(devices, size);
688 fprintf(stderr, "Out of memory, device %s (%d)\n", name, size);
691 memset(&devices[ndevices], 0, sizeof(struct per_dev_info));
692 devices[ndevices].name = name;
697 static struct per_dev_info *get_dev_info(dev_t dev)
699 struct per_dev_info *pdi;
702 for (i = 0; i < ndevices; i++) {
704 devices[i].dev = dev;
705 if (devices[i].dev == dev)
709 if (resize_devices(NULL))
712 pdi = &devices[ndevices - 1];
714 pdi->last_sequence = -1;
715 pdi->last_read_time = 0;
716 memset(&pdi->rb_last, 0, sizeof(pdi->rb_last));
717 pdi->rb_last_entries = 0;
721 static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size)
724 snprintf(buffer, size, "%s", pdi->name);
726 snprintf(buffer, size, "%d,%d",MAJOR(pdi->dev),MINOR(pdi->dev));
730 static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit)
732 unsigned long long this = bit->time;
733 unsigned long long last = pdi->last_reported_time;
735 pdi->backwards = (this < last) ? 'B' : ' ';
736 pdi->last_reported_time = this;
739 static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t,
744 ios->qwrite_kb += t->bytes >> 10;
747 ios->qread_kb += t->bytes >> 10;
751 static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci,
754 __account_m(&pci->io_stats, t, rw);
756 if (per_process_stats) {
757 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
759 __account_m(ios, t, rw);
763 static inline void __account_queue(struct io_stats *ios, struct blk_io_trace *t,
768 ios->qwrite_kb += t->bytes >> 10;
771 ios->qread_kb += t->bytes >> 10;
775 static inline void account_queue(struct blk_io_trace *t,
776 struct per_cpu_info *pci, int rw)
778 __account_queue(&pci->io_stats, t, rw);
780 if (per_process_stats) {
781 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
783 __account_queue(ios, t, rw);
787 static inline void __account_c(struct io_stats *ios, int rw, unsigned int bytes)
791 ios->cwrite_kb += bytes >> 10;
794 ios->cread_kb += bytes >> 10;
798 static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci,
801 __account_c(&pci->io_stats, rw, bytes);
803 if (per_process_stats) {
804 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
806 __account_c(ios, rw, bytes);
810 static inline void __account_issue(struct io_stats *ios, int rw,
815 ios->iwrite_kb += bytes >> 10;
818 ios->iread_kb += bytes >> 10;
822 static inline void account_issue(struct blk_io_trace *t,
823 struct per_cpu_info *pci, int rw)
825 __account_issue(&pci->io_stats, rw, t->bytes);
827 if (per_process_stats) {
828 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
830 __account_issue(ios, rw, t->bytes);
834 static inline void __account_unplug(struct io_stats *ios, int timer)
837 ios->timer_unplugs++;
842 static inline void account_unplug(struct blk_io_trace *t,
843 struct per_cpu_info *pci, int timer)
845 __account_unplug(&pci->io_stats, timer);
847 if (per_process_stats) {
848 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
850 __account_unplug(ios, timer);
854 static void log_complete(struct per_dev_info *pdi, struct per_cpu_info *pci,
855 struct blk_io_trace *t, char *act)
857 process_fmt(act, pci, t, log_track_complete(pdi, t), 0, NULL);
860 static void log_insert(struct per_dev_info *pdi, struct per_cpu_info *pci,
861 struct blk_io_trace *t, char *act)
863 process_fmt(act, pci, t, log_track_insert(pdi, t), 0, NULL);
866 static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t,
869 process_fmt(act, pci, t, -1, 0, NULL);
872 static void log_issue(struct per_dev_info *pdi, struct per_cpu_info *pci,
873 struct blk_io_trace *t, char *act)
875 process_fmt(act, pci, t, log_track_issue(pdi, t), 0, NULL);
878 static void log_merge(struct per_dev_info *pdi, struct per_cpu_info *pci,
879 struct blk_io_trace *t, char *act)
882 log_track_frontmerge(pdi, t);
884 process_fmt(act, pci, t, -1ULL, 0, NULL);
887 static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t,
890 process_fmt(act, pci, t, -1ULL, 0, NULL);
893 static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t,
896 process_fmt(act, pci, t, -1ULL, 0, NULL);
899 static void log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t,
902 process_fmt(act, pci, t, -1ULL, 0, NULL);
905 static void log_split(struct per_cpu_info *pci, struct blk_io_trace *t,
908 process_fmt(act, pci, t, -1ULL, 0, NULL);
911 static void log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act)
913 unsigned char *buf = (unsigned char *) t + sizeof(*t);
915 process_fmt(act, pci, t, -1ULL, t->pdu_len, buf);
918 static void dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci)
920 int act = t->action & 0xffff;
924 log_generic(pci, t, "Q");
927 log_generic(pci, t, "G");
929 case __BLK_TA_SLEEPRQ:
930 log_generic(pci, t, "S");
932 case __BLK_TA_REQUEUE:
933 log_generic(pci, t, "R");
938 case __BLK_TA_COMPLETE:
941 case __BLK_TA_INSERT:
945 fprintf(stderr, "Bad pc action %x\n", act);
950 static void dump_trace_fs(struct blk_io_trace *t, struct per_dev_info *pdi,
951 struct per_cpu_info *pci)
953 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
954 int act = t->action & 0xffff;
958 account_queue(t, pci, w);
959 log_queue(pci, t, "Q");
961 case __BLK_TA_INSERT:
962 log_insert(pdi, pci, t, "I");
964 case __BLK_TA_BACKMERGE:
965 account_m(t, pci, w);
966 log_merge(pdi, pci, t, "M");
968 case __BLK_TA_FRONTMERGE:
969 account_m(t, pci, w);
970 log_merge(pdi, pci, t, "F");
973 log_track_getrq(pdi, t);
974 log_generic(pci, t, "G");
976 case __BLK_TA_SLEEPRQ:
977 log_generic(pci, t, "S");
979 case __BLK_TA_REQUEUE:
980 account_c(t, pci, w, -t->bytes);
981 log_queue(pci, t, "R");
984 account_issue(t, pci, w);
985 log_issue(pdi, pci, t, "D");
987 case __BLK_TA_COMPLETE:
988 account_c(t, pci, w, t->bytes);
989 log_complete(pdi, pci, t, "C");
992 log_action(pci, t, "P");
994 case __BLK_TA_UNPLUG_IO:
995 account_unplug(t, pci, 0);
996 log_unplug(pci, t, "U");
998 case __BLK_TA_UNPLUG_TIMER:
999 account_unplug(t, pci, 1);
1000 log_unplug(pci, t, "UT");
1002 case __BLK_TA_SPLIT:
1003 log_split(pci, t, "X");
1005 case __BLK_TA_BOUNCE:
1006 log_generic(pci, t, "B");
1009 fprintf(stderr, "Bad fs action %x\n", t->action);
1014 static void dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
1015 struct per_dev_info *pdi)
1017 if (t->action & BLK_TC_ACT(BLK_TC_PC))
1018 dump_trace_pc(t, pci);
1020 dump_trace_fs(t, pdi, pci);
1025 static void dump_io_stats(struct io_stats *ios, char *msg)
1027 fprintf(ofp, "%s\n", msg);
1029 fprintf(ofp, " Reads Queued: %'8lu, %'8LuKiB\t", ios->qreads, ios->qread_kb);
1030 fprintf(ofp, " Writes Queued: %'8lu, %'8LuKiB\n", ios->qwrites,ios->qwrite_kb);
1032 fprintf(ofp, " Read Dispatches: %'8lu, %'8LuKiB\t", ios->ireads, ios->iread_kb);
1033 fprintf(ofp, " Write Dispatches: %'8lu, %'8LuKiB\n", ios->iwrites,ios->iwrite_kb);
1034 fprintf(ofp, " Reads Completed: %'8lu, %'8LuKiB\t", ios->creads, ios->cread_kb);
1035 fprintf(ofp, " Writes Completed: %'8lu, %'8LuKiB\n", ios->cwrites,ios->cwrite_kb);
1036 fprintf(ofp, " Read Merges: %'8lu%8c\t", ios->mreads, ' ');
1037 fprintf(ofp, " Write Merges: %'8lu\n", ios->mwrites);
1038 fprintf(ofp, " IO unplugs: %'8lu%8c\t", ios->io_unplugs, ' ');
1039 fprintf(ofp, " Timer unplugs: %'8lu\n", ios->timer_unplugs);
1042 static void dump_wait_stats(struct per_process_info *ppi)
1044 unsigned long rawait = ppi->longest_allocation_wait[0] / 1000;
1045 unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000;
1046 unsigned long rcwait = ppi->longest_completion_wait[0] / 1000;
1047 unsigned long wawait = ppi->longest_allocation_wait[1] / 1000;
1048 unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000;
1049 unsigned long wcwait = ppi->longest_completion_wait[1] / 1000;
1051 fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' ');
1052 fprintf(ofp, " Allocation wait: %'8lu\n", wawait);
1053 fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' ');
1054 fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait);
1055 fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' ');
1056 fprintf(ofp, " Completion wait: %'8lu\n", wcwait);
1059 static int ppi_name_compare(const void *p1, const void *p2)
1061 struct per_process_info *ppi1 = *((struct per_process_info **) p1);
1062 struct per_process_info *ppi2 = *((struct per_process_info **) p2);
1065 res = strverscmp(ppi1->name, ppi2->name);
1067 res = ppi1->pid > ppi2->pid;
1072 static void sort_process_list(void)
1074 struct per_process_info **ppis;
1075 struct per_process_info *ppi;
1078 ppis = malloc(ppi_list_entries * sizeof(struct per_process_info *));
1083 ppi = ppi->list_next;
1086 qsort(ppis, ppi_list_entries, sizeof(ppi), ppi_name_compare);
1088 i = ppi_list_entries - 1;
1093 ppi->list_next = ppi_list;
1101 static void show_process_stats(void)
1103 struct per_process_info *ppi;
1105 sort_process_list();
1111 if (ppi->more_than_one)
1112 sprintf(name, "%s (%u, ...)", ppi->name, ppi->pid);
1114 sprintf(name, "%s (%u)", ppi->name, ppi->pid);
1116 dump_io_stats(&ppi->io_stats, name);
1117 dump_wait_stats(ppi);
1118 ppi = ppi->list_next;
1124 static void show_device_and_cpu_stats(void)
1126 struct per_dev_info *pdi;
1127 struct per_cpu_info *pci;
1128 struct io_stats total, *ios;
1129 int i, j, pci_events;
1130 char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3];
1133 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
1135 memset(&total, 0, sizeof(total));
1141 for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) {
1145 ios = &pci->io_stats;
1146 total.qreads += ios->qreads;
1147 total.qwrites += ios->qwrites;
1148 total.creads += ios->creads;
1149 total.cwrites += ios->cwrites;
1150 total.mreads += ios->mreads;
1151 total.mwrites += ios->mwrites;
1152 total.ireads += ios->ireads;
1153 total.iwrites += ios->iwrites;
1154 total.qread_kb += ios->qread_kb;
1155 total.qwrite_kb += ios->qwrite_kb;
1156 total.cread_kb += ios->cread_kb;
1157 total.cwrite_kb += ios->cwrite_kb;
1158 total.iread_kb += ios->iread_kb;
1159 total.iwrite_kb += ios->iwrite_kb;
1160 total.timer_unplugs += ios->timer_unplugs;
1161 total.io_unplugs += ios->io_unplugs;
1163 snprintf(line, sizeof(line) - 1, "CPU%d (%s):",
1164 j, get_dev_name(pdi, name, sizeof(name)));
1165 dump_io_stats(ios, line);
1169 if (pci_events > 1) {
1171 snprintf(line, sizeof(line) - 1, "Total (%s):",
1172 get_dev_name(pdi, name, sizeof(name)));
1173 dump_io_stats(&total, line);
1176 fprintf(ofp, "\nEvents (%s): %'Lu entries, %'lu skips\n",
1177 get_dev_name(pdi, line, sizeof(line)), pdi->events,
1183 * struct trace and blktrace allocation cache, we do potentially
1184 * millions of mallocs for these structures while only using at most
1185 * a few thousand at the time
1187 static inline void t_free(struct trace *t)
1189 if (t_alloc_cache < 1024) {
1190 t->next = t_alloc_list;
1197 static inline struct trace *t_alloc(void)
1199 struct trace *t = t_alloc_list;
1202 t_alloc_list = t->next;
1207 return malloc(sizeof(*t));
1210 static inline void bit_free(struct blk_io_trace *bit)
1212 if (bit_alloc_cache < 1024) {
1214 * abuse a 64-bit field for a next pointer for the free item
1216 bit->time = (__u64) (unsigned long) bit_alloc_list;
1217 bit_alloc_list = (struct blk_io_trace *) bit;
1223 static inline struct blk_io_trace *bit_alloc(void)
1225 struct blk_io_trace *bit = bit_alloc_list;
1228 bit_alloc_list = (struct blk_io_trace *) (unsigned long) \
1234 return malloc(sizeof(*bit));
1237 static void find_genesis(void)
1239 struct trace *t = trace_list;
1241 genesis_time = -1ULL;
1243 if (t->bit->time < genesis_time)
1244 genesis_time = t->bit->time;
1250 static inline int check_stopwatch(struct blk_io_trace *bit)
1252 if (bit->time < stopwatch_end &&
1253 bit->time >= stopwatch_start)
1260 * return youngest entry read
1262 static int sort_entries(unsigned long long *youngest)
1270 while ((t = trace_list) != NULL) {
1271 struct blk_io_trace *bit = t->bit;
1273 trace_list = t->next;
1275 bit->time -= genesis_time;
1277 if (bit->time < *youngest || !*youngest)
1278 *youngest = bit->time;
1280 if (check_stopwatch(bit)) {
1286 if (trace_rb_insert_sort(t))
1293 static inline void __put_trace_last(struct per_dev_info *pdi, struct trace *t)
1295 rb_erase(&t->rb_node, &pdi->rb_last);
1296 pdi->rb_last_entries--;
1302 static void put_trace(struct per_dev_info *pdi, struct trace *t)
1304 rb_erase(&t->rb_node, &rb_sort_root);
1307 trace_rb_insert_last(pdi, t);
1309 if (pdi->rb_last_entries > rb_batch * pdi->nfiles) {
1310 struct rb_node *n = rb_first(&pdi->rb_last);
1312 t = rb_entry(n, struct trace, rb_node);
1313 __put_trace_last(pdi, t);
1317 static int check_sequence(struct per_dev_info *pdi, struct trace *t, int force)
1319 unsigned long expected_sequence = pdi->last_sequence + 1;
1320 struct blk_io_trace *bit = t->bit;
1324 * first entry, always ok
1326 if (!expected_sequence)
1329 if (bit->sequence == expected_sequence)
1332 if (bit->sequence < expected_sequence &&
1333 bit->time > pdi->last_reported_time)
1337 * the wanted sequence is really there, continue
1338 * because this means that the log time is earlier
1339 * on the trace we have now
1341 __t = trace_rb_find_sort(pdi->dev, expected_sequence);
1346 * we already displayed this entry, it's ok to continue.
1347 * kill old entry now
1349 __t = trace_rb_find_last(pdi, expected_sequence);
1351 __put_trace_last(pdi, __t);
1355 if (last_skipped == expected_sequence)
1358 last_skipped = expected_sequence;
1361 * unless this is the last run, break and wait for more entries
1363 if (!force && t->skipped <= skip_runs)
1366 fprintf(stderr, "(%d,%d): skipping %lu -> %u\n", MAJOR(pdi->dev),
1367 MINOR(pdi->dev), pdi->last_sequence, bit->sequence);
1372 static void show_entries_rb(int force)
1374 struct per_dev_info *pdi = NULL;
1375 struct per_cpu_info *pci = NULL;
1376 struct blk_io_trace *bit;
1381 n = rb_first(&rb_sort_root);
1382 t = rb_entry(n, struct trace, rb_node);
1383 fprintf(stderr, "first force %u\n", t->bit->sequence);
1386 while ((n = rb_first(&rb_sort_root)) != NULL) {
1390 t = rb_entry(n, struct trace, rb_node);
1393 if (!pdi || pdi->dev != bit->device)
1394 pdi = get_dev_info(bit->device);
1397 fprintf(stderr, "Unknown device ID? (%d,%d)\n",
1398 MAJOR(bit->device), MINOR(bit->device));
1402 if (check_sequence(pdi, t, force))
1405 if (!force && bit->time > last_allowed_time)
1408 pdi->last_sequence = bit->sequence;
1410 check_time(pdi, bit);
1412 if (!pci || pci->cpu != bit->cpu)
1413 pci = get_cpu_info(pdi, bit->cpu);
1415 dump_trace(bit, pci, pdi);
1421 static int read_data(int fd, void *buffer, int bytes, int block)
1423 int ret, bytes_left, fl;
1426 fl = fcntl(fd, F_GETFL);
1429 fcntl(fd, F_SETFL, fl | O_NONBLOCK);
1431 fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
1435 while (bytes_left > 0) {
1436 ret = read(fd, p, bytes_left);
1440 if (errno != EAGAIN)
1453 static int read_events(int fd, int always_block)
1455 struct per_dev_info *pdi = NULL;
1456 unsigned int events = 0;
1458 while (!is_done() && events < rb_batch) {
1459 struct blk_io_trace *bit;
1466 if (read_data(fd, bit, sizeof(*bit), !events || always_block))
1469 magic = be32_to_cpu(bit->magic);
1470 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
1471 fprintf(stderr, "Bad magic %x\n", magic);
1475 pdu_len = be16_to_cpu(bit->pdu_len);
1477 void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
1479 if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1))
1487 if (verify_trace(bit)) {
1493 memset(t, 0, sizeof(*t));
1496 t->next = trace_list;
1499 if (!pdi || pdi->dev != bit->device)
1500 pdi = get_dev_info(bit->device);
1502 if (bit->time > pdi->last_read_time)
1503 pdi->last_read_time = bit->time;
1511 static int do_file(void)
1513 struct per_cpu_info *pci;
1514 struct per_dev_info *pdi;
1515 int i, j, events, events_added;
1520 * first prepare all files for reading
1522 for (i = 0; i < ndevices; i++) {
1525 pdi->last_sequence = -1;
1530 pci = get_cpu_info(pdi, j);
1534 snprintf(pci->fname, sizeof(pci->fname)-1,
1535 "%s.blktrace.%d", pdi->name, pci->cpu);
1536 if (stat(pci->fname, &st) < 0)
1539 pci->fd = open(pci->fname, O_RDONLY);
1546 printf("Input file %s added\n", pci->fname);
1552 * now loop over the files reading in the data
1555 unsigned long long youngest;
1558 last_allowed_time = -1ULL;
1560 for (i = 0; i < ndevices; i++) {
1563 for (j = 0; j < pdi->nfiles; j++) {
1565 pci = get_cpu_info(pdi, j);
1570 events = read_events(pci->fd, 1);
1577 if (pdi->last_read_time < last_allowed_time)
1578 last_allowed_time = pdi->last_read_time;
1580 events_added += events;
1584 if (sort_entries(&youngest))
1587 if (youngest > stopwatch_end)
1592 } while (events_added);
1594 if (rb_sort_entries)
1600 static int do_stdin(void)
1602 unsigned long long youngest;
1606 last_allowed_time = -1ULL;
1607 fd = dup(STDIN_FILENO);
1611 events = read_events(fd, 0);
1615 if (sort_entries(&youngest))
1618 if (youngest > stopwatch_end)
1624 if (rb_sort_entries)
1631 static void flush_output(void)
1636 static void handle_sigint(__attribute__((__unused__)) int sig)
1643 * Extract start and duration times from a string, allowing
1644 * us to specify a time interval of interest within a trace.
1645 * Format: "duration" (start is zero) or "start:duration".
1647 static int find_stopwatch_interval(char *string)
1652 value = strtod(string, &sp);
1654 fprintf(stderr,"Invalid stopwatch timer: %s\n", string);
1658 stopwatch_start = DOUBLE_TO_NANO_ULL(value);
1660 value = strtod(string, &sp);
1661 if (sp == string || *sp != '\0') {
1662 fprintf(stderr,"Invalid stopwatch duration time: %s\n",
1666 } else if (*sp != '\0') {
1667 fprintf(stderr,"Invalid stopwatch start timer: %s\n", string);
1670 stopwatch_end = DOUBLE_TO_NANO_ULL(value);
1671 if (stopwatch_end <= stopwatch_start) {
1672 fprintf(stderr, "Invalid stopwatch interval: %Lu -> %Lu\n",
1673 stopwatch_start, stopwatch_end);
1680 static char usage_str[] = \
1681 "[ -i <input name> ] [-o <output name> [ -s ] [ -t ] [ -q ]\n" \
1682 "[ -w start:stop ] [ -f output format ] [ -F format spec ] [ -v] \n\n" \
1683 "\t-i Input file containing trace data, or '-' for stdin\n" \
1684 "\t-o Output file. If not given, output is stdout\n" \
1685 "\t-b stdin read batching\n" \
1686 "\t-s Show per-program io statistics\n" \
1687 "\t-n Hash processes by name, not pid\n" \
1688 "\t-t Track individual ios. Will tell you the time a request took\n" \
1689 "\t to get queued, to get dispatched, and to get completed\n" \
1690 "\t-q Quiet. Don't display any stats at the end of the trace\n" \
1691 "\t-w Only parse data between the given time interval in seconds.\n" \
1692 "\t If 'start' isn't given, blkparse defaults the start time to 0\n" \
1693 "\t -f Output format. Customize the output format. The format field\n" \
1694 "\t identifies can be found in the documentation\n" \
1695 "\t-F Format specification. Can be found in the documentation\n" \
1696 "\t-v Print program version info\n\n";
1698 static void usage(char *prog)
1700 fprintf(stderr, "Usage: %s %s %s", prog, blkparse_version, usage_str);
1703 int main(int argc, char *argv[])
1707 int per_device_and_cpu_stats = 1;
1709 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
1712 if (!strcmp(optarg, "-") && !pipeline)
1714 else if (resize_devices(optarg) != 0)
1718 output_name = optarg;
1721 rb_batch = atoi(optarg);
1723 rb_batch = RB_BATCH_DEFAULT;
1726 per_process_stats = 1;
1732 per_device_and_cpu_stats = 0;
1735 if (find_stopwatch_interval(optarg) != 0)
1739 set_all_format_specs(optarg);
1742 if (add_format_spec(optarg) != 0)
1746 ppi_hash_by_pid = 0;
1749 printf("%s version %s\n", argv[0], blkparse_version);
1757 while (optind < argc) {
1758 if (!strcmp(argv[optind], "-") && !pipeline)
1760 else if (resize_devices(argv[optind]) != 0)
1765 if (!pipeline && !ndevices) {
1770 memset(&rb_sort_root, 0, sizeof(rb_sort_root));
1772 signal(SIGINT, handle_sigint);
1773 signal(SIGHUP, handle_sigint);
1774 signal(SIGTERM, handle_sigint);
1776 setlocale(LC_NUMERIC, "en_US");
1779 ofp = fdopen(STDOUT_FILENO, "w");
1784 snprintf(ofname, sizeof(ofname) - 1, "%s", output_name);
1785 ofp = fopen(ofname, "w");
1794 ofp_buffer = malloc(4096);
1795 if (setvbuf(ofp, ofp_buffer, mode, 4096)) {
1805 if (per_process_stats)
1806 show_process_stats();
1808 if (per_device_and_cpu_stats)
1809 show_device_and_cpu_stats();