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;
163 static struct rb_root rb_sort_root;
164 static unsigned long rb_sort_entries;
166 static struct trace *trace_list;
171 static struct blk_io_trace *bit_alloc_list;
172 static struct trace *t_alloc_list;
175 * for tracking individual ios
178 struct rb_node rb_node;
183 unsigned long long allocation_time;
184 unsigned long long queue_time;
185 unsigned long long dispatch_time;
186 unsigned long long completion_time;
190 static struct per_dev_info *devices;
191 static char *get_dev_name(struct per_dev_info *, char *, int);
194 static char *output_name;
196 static unsigned long long genesis_time;
197 static unsigned long long last_allowed_time;
198 static unsigned long long stopwatch_start; /* start from zero by default */
199 static unsigned long long stopwatch_end = ULONG_LONG_MAX; /* "infinity" */
201 static int per_process_stats;
202 static int track_ios;
203 static int ppi_hash_by_pid = 1;
205 #define RB_BATCH_DEFAULT (512)
206 static int rb_batch = RB_BATCH_DEFAULT;
210 #define is_done() (*(volatile int *)(&done))
211 static volatile int done;
213 #define JHASH_RANDOM (0x3af5f2ee)
215 static inline int ppi_hash_pid(__u32 pid)
217 return jhash_1word(pid, JHASH_RANDOM) & PPI_HASH_MASK;
220 static inline int ppi_hash_name(const char *name)
222 return jhash(name, 16, JHASH_RANDOM) & PPI_HASH_MASK;
225 static inline int ppi_hash(struct per_process_info *ppi)
228 return ppi_hash_pid(ppi->pid);
230 return ppi_hash_name(ppi->name);
233 static inline void add_process_to_hash(struct per_process_info *ppi)
235 const int hash_idx = ppi_hash(ppi);
237 ppi->hash_next = ppi_hash_table[hash_idx];
238 ppi_hash_table[hash_idx] = ppi;
241 static inline void add_process_to_list(struct per_process_info *ppi)
243 ppi->list_next = ppi_list;
248 static struct per_process_info *find_process_by_name(char *name)
250 const int hash_idx = ppi_hash_name(name);
251 struct per_process_info *ppi;
253 ppi = ppi_hash_table[hash_idx];
255 if (!strcmp(ppi->name, name))
258 ppi = ppi->hash_next;
264 static struct per_process_info *find_process_by_pid(__u32 pid)
266 const int hash_idx = ppi_hash_pid(pid);
267 struct per_process_info *ppi;
269 ppi = ppi_hash_table[hash_idx];
274 ppi = ppi->hash_next;
280 static struct per_process_info *find_process(__u32 pid, char *name)
282 struct per_process_info *ppi;
285 return find_process_by_pid(pid);
287 ppi = find_process_by_name(name);
288 if (ppi && ppi->pid != pid)
289 ppi->more_than_one = 1;
294 static inline int trace_rb_insert(struct trace *t, struct rb_root *root)
296 struct rb_node **p = &root->rb_node;
297 struct rb_node *parent = NULL;
303 __t = rb_entry(parent, struct trace, rb_node);
305 if (t->bit->time < __t->bit->time)
307 else if (t->bit->time > __t->bit->time)
309 else if (t->bit->device < __t->bit->device)
311 else if (t->bit->device > __t->bit->device)
313 else if (t->bit->sequence < __t->bit->sequence)
315 else if (t->bit->sequence > __t->bit->sequence)
317 else if (t->bit->device == __t->bit->device) {
319 "sequence alias (%d) on device %d,%d!\n",
321 MAJOR(t->bit->device), MINOR(t->bit->device));
326 rb_link_node(&t->rb_node, parent, p);
327 rb_insert_color(&t->rb_node, root);
331 static inline int trace_rb_insert_sort(struct trace *t)
333 if (!trace_rb_insert(t, &rb_sort_root)) {
341 static inline int trace_rb_insert_last(struct per_dev_info *pdi,struct trace *t)
343 if (!trace_rb_insert(t, &pdi->rb_last)) {
344 pdi->rb_last_entries++;
351 static struct trace *trace_rb_find(dev_t device, unsigned long sequence,
352 struct rb_root *root, int order)
354 struct rb_node *n = root->rb_node;
355 struct rb_node *prev = NULL;
359 __t = rb_entry(n, struct trace, rb_node);
362 if (device < __t->bit->device)
364 else if (device > __t->bit->device)
366 else if (sequence < __t->bit->sequence)
368 else if (sequence > __t->bit->sequence)
375 * hack - the list may not be sequence ordered because some
376 * events don't have sequence and time matched. so we end up
377 * being a little off in the rb lookup here, because we don't
378 * know the time we are looking for. compensate by browsing
379 * a little ahead from the last entry to find the match
384 while (((n = rb_next(prev)) != NULL) && max--) {
385 __t = rb_entry(n, struct trace, rb_node);
387 if (__t->bit->device == device &&
388 __t->bit->sequence == sequence)
398 static inline struct trace *trace_rb_find_sort(dev_t dev, unsigned long seq)
400 return trace_rb_find(dev, seq, &rb_sort_root, 1);
403 static inline struct trace *trace_rb_find_last(struct per_dev_info *pdi,
406 return trace_rb_find(pdi->dev, seq, &pdi->rb_last, 0);
409 static inline int track_rb_insert(struct per_dev_info *pdi,struct io_track *iot)
411 struct rb_node **p = &pdi->rb_track.rb_node;
412 struct rb_node *parent = NULL;
413 struct io_track *__iot;
417 __iot = rb_entry(parent, struct io_track, rb_node);
419 if (iot->sector < __iot->sector)
421 else if (iot->sector > __iot->sector)
425 "sector alias (%Lu) on device %d,%d!\n",
426 (unsigned long long) iot->sector,
427 MAJOR(pdi->dev), MINOR(pdi->dev));
432 rb_link_node(&iot->rb_node, parent, p);
433 rb_insert_color(&iot->rb_node, &pdi->rb_track);
437 static struct io_track *__find_track(struct per_dev_info *pdi, __u64 sector)
439 struct rb_node *n = pdi->rb_track.rb_node;
440 struct io_track *__iot;
443 __iot = rb_entry(n, struct io_track, rb_node);
445 if (sector < __iot->sector)
447 else if (sector > __iot->sector)
456 static struct io_track *find_track(struct per_dev_info *pdi, __u32 pid,
457 char *comm, __u64 sector)
459 struct io_track *iot;
461 iot = __find_track(pdi, sector);
463 iot = malloc(sizeof(*iot));
465 memcpy(iot->comm, comm, sizeof(iot->comm));
466 iot->sector = sector;
467 track_rb_insert(pdi, iot);
473 static void log_track_frontmerge(struct per_dev_info *pdi,
474 struct blk_io_trace *t)
476 struct io_track *iot;
481 iot = __find_track(pdi, t->sector + (t->bytes >> 9));
483 fprintf(stderr, "merge not found for (%d,%d): %llu\n",
484 MAJOR(pdi->dev), MINOR(pdi->dev),
485 t->sector + (t->bytes >> 9));
489 rb_erase(&iot->rb_node, &pdi->rb_track);
490 iot->sector -= t->bytes >> 9;
491 track_rb_insert(pdi, iot);
494 static void log_track_getrq(struct per_dev_info *pdi, struct blk_io_trace *t)
496 struct io_track *iot;
501 iot = find_track(pdi, t->pid, t->comm, t->sector);
502 iot->allocation_time = t->time;
506 * return time between rq allocation and insertion
508 static unsigned long long log_track_insert(struct per_dev_info *pdi,
509 struct blk_io_trace *t)
511 unsigned long long elapsed;
512 struct io_track *iot;
517 iot = find_track(pdi, t->pid, t->comm, t->sector);
518 iot->queue_time = t->time;
520 if (!iot->allocation_time)
523 elapsed = iot->queue_time - iot->allocation_time;
525 if (per_process_stats) {
526 struct per_process_info *ppi = find_process(iot->pid,iot->comm);
527 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
529 if (ppi && elapsed > ppi->longest_allocation_wait[w])
530 ppi->longest_allocation_wait[w] = elapsed;
537 * return time between queue and issue
539 static unsigned long long log_track_issue(struct per_dev_info *pdi,
540 struct blk_io_trace *t)
542 unsigned long long elapsed;
543 struct io_track *iot;
547 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
550 iot = __find_track(pdi, t->sector);
552 fprintf(stderr, "issue not found for (%d,%d): %llu\n",
553 MAJOR(pdi->dev), MINOR(pdi->dev), t->sector);
557 iot->dispatch_time = t->time;
558 elapsed = iot->dispatch_time - iot->queue_time;
560 if (per_process_stats) {
561 struct per_process_info *ppi = find_process(iot->pid,iot->comm);
562 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
564 if (ppi && elapsed > ppi->longest_dispatch_wait[w])
565 ppi->longest_dispatch_wait[w] = elapsed;
572 * return time between dispatch and complete
574 static unsigned long long log_track_complete(struct per_dev_info *pdi,
575 struct blk_io_trace *t)
577 unsigned long long elapsed;
578 struct io_track *iot;
582 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
585 iot = __find_track(pdi, t->sector);
587 fprintf(stderr, "complete not found for (%d,%d): %llu\n",
588 MAJOR(pdi->dev), MINOR(pdi->dev), t->sector);
592 iot->completion_time = t->time;
593 elapsed = iot->completion_time - iot->dispatch_time;
595 if (per_process_stats) {
596 struct per_process_info *ppi = find_process(iot->pid,iot->comm);
597 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
599 if (ppi && elapsed > ppi->longest_completion_wait[w])
600 ppi->longest_completion_wait[w] = elapsed;
604 * kill the trace, we don't need it after completion
606 rb_erase(&iot->rb_node, &pdi->rb_track);
613 static struct io_stats *find_process_io_stats(__u32 pid, char *name)
615 struct per_process_info *ppi = find_process(pid, name);
618 ppi = malloc(sizeof(*ppi));
619 memset(ppi, 0, sizeof(*ppi));
620 memcpy(ppi->name, name, 16);
622 add_process_to_hash(ppi);
623 add_process_to_list(ppi);
626 return &ppi->io_stats;
629 static void resize_cpu_info(struct per_dev_info *pdi, int cpu)
631 struct per_cpu_info *cpus = pdi->cpus;
632 int ncpus = pdi->ncpus;
633 int new_count = cpu + 1;
637 size = new_count * sizeof(struct per_cpu_info);
638 cpus = realloc(cpus, size);
641 fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n",
642 get_dev_name(pdi, name, sizeof(name)), size);
646 new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info));
647 new_space = (new_count - ncpus) * sizeof(struct per_cpu_info);
648 memset(new_start, 0, new_space);
650 pdi->ncpus = new_count;
654 static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu)
656 struct per_cpu_info *pci;
658 if (cpu >= pdi->ncpus)
659 resize_cpu_info(pdi, cpu);
661 pci = &pdi->cpus[cpu];
667 static int resize_devices(char *name)
669 int size = (ndevices + 1) * sizeof(struct per_dev_info);
671 devices = realloc(devices, size);
673 fprintf(stderr, "Out of memory, device %s (%d)\n", name, size);
676 memset(&devices[ndevices], 0, sizeof(struct per_dev_info));
677 devices[ndevices].name = name;
682 static struct per_dev_info *get_dev_info(dev_t dev)
684 struct per_dev_info *pdi;
687 for (i = 0; i < ndevices; i++) {
689 devices[i].dev = dev;
690 if (devices[i].dev == dev)
694 if (resize_devices(NULL))
697 pdi = &devices[ndevices - 1];
699 pdi->last_sequence = -1;
700 pdi->last_read_time = 0;
701 memset(&pdi->rb_last, 0, sizeof(pdi->rb_last));
702 pdi->rb_last_entries = 0;
706 static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size)
709 snprintf(buffer, size, "%s", pdi->name);
711 snprintf(buffer, size, "%d,%d",MAJOR(pdi->dev),MINOR(pdi->dev));
715 static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit)
717 unsigned long long this = bit->time;
718 unsigned long long last = pdi->last_reported_time;
720 pdi->backwards = (this < last) ? 'B' : ' ';
721 pdi->last_reported_time = this;
724 static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t,
729 ios->qwrite_kb += t->bytes >> 10;
732 ios->qread_kb += t->bytes >> 10;
736 static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci,
739 __account_m(&pci->io_stats, t, rw);
741 if (per_process_stats) {
742 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
744 __account_m(ios, t, rw);
748 static inline void __account_queue(struct io_stats *ios, struct blk_io_trace *t,
753 ios->qwrite_kb += t->bytes >> 10;
756 ios->qread_kb += t->bytes >> 10;
760 static inline void account_queue(struct blk_io_trace *t,
761 struct per_cpu_info *pci, int rw)
763 __account_queue(&pci->io_stats, t, rw);
765 if (per_process_stats) {
766 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
768 __account_queue(ios, t, rw);
772 static inline void __account_c(struct io_stats *ios, int rw, unsigned int bytes)
776 ios->cwrite_kb += bytes >> 10;
779 ios->cread_kb += bytes >> 10;
783 static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci,
786 __account_c(&pci->io_stats, rw, bytes);
788 if (per_process_stats) {
789 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
791 __account_c(ios, rw, bytes);
795 static inline void __account_issue(struct io_stats *ios, int rw,
800 ios->iwrite_kb += bytes >> 10;
803 ios->iread_kb += bytes >> 10;
807 static inline void account_issue(struct blk_io_trace *t,
808 struct per_cpu_info *pci, int rw)
810 __account_issue(&pci->io_stats, rw, t->bytes);
812 if (per_process_stats) {
813 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
815 __account_issue(ios, rw, t->bytes);
819 static inline void __account_unplug(struct io_stats *ios, int timer)
822 ios->timer_unplugs++;
827 static inline void account_unplug(struct blk_io_trace *t,
828 struct per_cpu_info *pci, int timer)
830 __account_unplug(&pci->io_stats, timer);
832 if (per_process_stats) {
833 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
835 __account_unplug(ios, timer);
839 static void log_complete(struct per_dev_info *pdi, struct per_cpu_info *pci,
840 struct blk_io_trace *t, char *act)
842 process_fmt(act, pci, t, log_track_complete(pdi, t), 0, NULL);
845 static void log_insert(struct per_dev_info *pdi, struct per_cpu_info *pci,
846 struct blk_io_trace *t, char *act)
848 process_fmt(act, pci, t, log_track_insert(pdi, t), 0, NULL);
851 static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t,
854 process_fmt(act, pci, t, -1, 0, NULL);
857 static void log_issue(struct per_dev_info *pdi, struct per_cpu_info *pci,
858 struct blk_io_trace *t, char *act)
860 process_fmt(act, pci, t, log_track_issue(pdi, t), 0, NULL);
863 static void log_merge(struct per_dev_info *pdi, struct per_cpu_info *pci,
864 struct blk_io_trace *t, char *act)
867 log_track_frontmerge(pdi, t);
869 process_fmt(act, pci, t, -1ULL, 0, NULL);
872 static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t,
875 process_fmt(act, pci, t, -1ULL, 0, NULL);
878 static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t,
881 process_fmt(act, pci, t, -1ULL, 0, NULL);
884 static void log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t,
887 process_fmt(act, pci, t, -1ULL, 0, NULL);
890 static void log_split(struct per_cpu_info *pci, struct blk_io_trace *t,
893 process_fmt(act, pci, t, -1ULL, 0, NULL);
896 static void log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act)
898 unsigned char *buf = (unsigned char *) t + sizeof(*t);
900 process_fmt(act, pci, t, -1ULL, t->pdu_len, buf);
903 static void dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci)
905 int act = t->action & 0xffff;
909 log_generic(pci, t, "Q");
912 log_generic(pci, t, "G");
914 case __BLK_TA_SLEEPRQ:
915 log_generic(pci, t, "S");
917 case __BLK_TA_REQUEUE:
918 log_generic(pci, t, "R");
923 case __BLK_TA_COMPLETE:
926 case __BLK_TA_INSERT:
930 fprintf(stderr, "Bad pc action %x\n", act);
935 static void dump_trace_fs(struct blk_io_trace *t, struct per_dev_info *pdi,
936 struct per_cpu_info *pci)
938 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
939 int act = t->action & 0xffff;
943 account_queue(t, pci, w);
944 log_queue(pci, t, "Q");
946 case __BLK_TA_INSERT:
947 log_insert(pdi, pci, t, "I");
949 case __BLK_TA_BACKMERGE:
950 account_m(t, pci, w);
951 log_merge(pdi, pci, t, "M");
953 case __BLK_TA_FRONTMERGE:
954 account_m(t, pci, w);
955 log_merge(pdi, pci, t, "F");
958 log_track_getrq(pdi, t);
959 log_generic(pci, t, "G");
961 case __BLK_TA_SLEEPRQ:
962 log_generic(pci, t, "S");
964 case __BLK_TA_REQUEUE:
965 account_c(t, pci, w, -t->bytes);
966 log_queue(pci, t, "R");
969 account_issue(t, pci, w);
970 log_issue(pdi, pci, t, "D");
972 case __BLK_TA_COMPLETE:
973 account_c(t, pci, w, t->bytes);
974 log_complete(pdi, pci, t, "C");
977 log_action(pci, t, "P");
979 case __BLK_TA_UNPLUG_IO:
980 account_unplug(t, pci, 0);
981 log_unplug(pci, t, "U");
983 case __BLK_TA_UNPLUG_TIMER:
984 account_unplug(t, pci, 1);
985 log_unplug(pci, t, "UT");
988 log_split(pci, t, "X");
990 case __BLK_TA_BOUNCE:
991 log_generic(pci, t, "B");
994 fprintf(stderr, "Bad fs action %x\n", t->action);
999 static void dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
1000 struct per_dev_info *pdi)
1002 if (t->action & BLK_TC_ACT(BLK_TC_PC))
1003 dump_trace_pc(t, pci);
1005 dump_trace_fs(t, pdi, pci);
1010 static void dump_io_stats(struct io_stats *ios, char *msg)
1012 fprintf(ofp, "%s\n", msg);
1014 fprintf(ofp, " Reads Queued: %'8lu, %'8LuKiB\t", ios->qreads, ios->qread_kb);
1015 fprintf(ofp, " Writes Queued: %'8lu, %'8LuKiB\n", ios->qwrites,ios->qwrite_kb);
1017 fprintf(ofp, " Read Dispatches: %'8lu, %'8LuKiB\t", ios->ireads, ios->iread_kb);
1018 fprintf(ofp, " Write Dispatches: %'8lu, %'8LuKiB\n", ios->iwrites,ios->iwrite_kb);
1019 fprintf(ofp, " Reads Completed: %'8lu, %'8LuKiB\t", ios->creads, ios->cread_kb);
1020 fprintf(ofp, " Writes Completed: %'8lu, %'8LuKiB\n", ios->cwrites,ios->cwrite_kb);
1021 fprintf(ofp, " Read Merges: %'8lu%8c\t", ios->mreads, ' ');
1022 fprintf(ofp, " Write Merges: %'8lu\n", ios->mwrites);
1023 fprintf(ofp, " IO unplugs: %'8lu%8c\t", ios->io_unplugs, ' ');
1024 fprintf(ofp, " Timer unplugs: %'8lu\n", ios->timer_unplugs);
1027 static void dump_wait_stats(struct per_process_info *ppi)
1029 unsigned long rawait = ppi->longest_allocation_wait[0] / 1000;
1030 unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000;
1031 unsigned long rcwait = ppi->longest_completion_wait[0] / 1000;
1032 unsigned long wawait = ppi->longest_allocation_wait[1] / 1000;
1033 unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000;
1034 unsigned long wcwait = ppi->longest_completion_wait[1] / 1000;
1036 fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' ');
1037 fprintf(ofp, " Allocation wait: %'8lu\n", wawait);
1038 fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' ');
1039 fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait);
1040 fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' ');
1041 fprintf(ofp, " Completion wait: %'8lu\n", wcwait);
1044 static int ppi_name_compare(const void *p1, const void *p2)
1046 struct per_process_info *ppi1 = *((struct per_process_info **) p1);
1047 struct per_process_info *ppi2 = *((struct per_process_info **) p2);
1050 res = strverscmp(ppi1->name, ppi2->name);
1052 res = ppi1->pid > ppi2->pid;
1057 static void sort_process_list(void)
1059 struct per_process_info **ppis;
1060 struct per_process_info *ppi;
1063 ppis = malloc(ppi_list_entries * sizeof(struct per_process_info *));
1068 ppi = ppi->list_next;
1071 qsort(ppis, ppi_list_entries, sizeof(ppi), ppi_name_compare);
1073 i = ppi_list_entries - 1;
1078 ppi->list_next = ppi_list;
1086 static void show_process_stats(void)
1088 struct per_process_info *ppi;
1090 sort_process_list();
1096 if (ppi->more_than_one)
1097 sprintf(name, "%s (%u, ...)", ppi->name, ppi->pid);
1099 sprintf(name, "%s (%u)", ppi->name, ppi->pid);
1101 dump_io_stats(&ppi->io_stats, name);
1102 dump_wait_stats(ppi);
1103 ppi = ppi->list_next;
1109 static void show_device_and_cpu_stats(void)
1111 struct per_dev_info *pdi;
1112 struct per_cpu_info *pci;
1113 struct io_stats total, *ios;
1114 int i, j, pci_events;
1115 char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3];
1118 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
1120 memset(&total, 0, sizeof(total));
1126 for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) {
1130 ios = &pci->io_stats;
1131 total.qreads += ios->qreads;
1132 total.qwrites += ios->qwrites;
1133 total.creads += ios->creads;
1134 total.cwrites += ios->cwrites;
1135 total.mreads += ios->mreads;
1136 total.mwrites += ios->mwrites;
1137 total.ireads += ios->ireads;
1138 total.iwrites += ios->iwrites;
1139 total.qread_kb += ios->qread_kb;
1140 total.qwrite_kb += ios->qwrite_kb;
1141 total.cread_kb += ios->cread_kb;
1142 total.cwrite_kb += ios->cwrite_kb;
1143 total.iread_kb += ios->iread_kb;
1144 total.iwrite_kb += ios->iwrite_kb;
1145 total.timer_unplugs += ios->timer_unplugs;
1146 total.io_unplugs += ios->io_unplugs;
1148 snprintf(line, sizeof(line) - 1, "CPU%d (%s):",
1149 j, get_dev_name(pdi, name, sizeof(name)));
1150 dump_io_stats(ios, line);
1154 if (pci_events > 1) {
1156 snprintf(line, sizeof(line) - 1, "Total (%s):",
1157 get_dev_name(pdi, name, sizeof(name)));
1158 dump_io_stats(&total, line);
1161 fprintf(ofp, "\nEvents (%s): %'Lu entries, %'lu skips\n",
1162 get_dev_name(pdi, line, sizeof(line)), pdi->events,
1168 * struct trace and blktrace allocation cache, we do potentially
1169 * millions of mallocs for these structures while only using at most
1170 * a few thousand at the time
1172 static inline void t_free(struct trace *t)
1174 t->next = t_alloc_list;
1178 static inline struct trace *t_alloc(void)
1180 struct trace *t = t_alloc_list;
1183 t_alloc_list = t->next;
1187 return malloc(sizeof(*t));
1190 static inline void bit_free(struct blk_io_trace *bit)
1193 * abuse a 64-bit field for a next pointer for the free item
1195 bit->time = (__u64) (unsigned long) bit_alloc_list;
1196 bit_alloc_list = (struct blk_io_trace *) bit;
1199 static inline struct blk_io_trace *bit_alloc(void)
1201 struct blk_io_trace *bit = bit_alloc_list;
1204 bit_alloc_list = (struct blk_io_trace *) (unsigned long) \
1209 return malloc(sizeof(*bit));
1212 static void find_genesis(void)
1214 struct trace *t = trace_list;
1216 genesis_time = -1ULL;
1218 if (t->bit->time < genesis_time)
1219 genesis_time = t->bit->time;
1225 static inline int check_stopwatch(struct blk_io_trace *bit)
1227 if (bit->time < stopwatch_end &&
1228 bit->time >= stopwatch_start)
1235 * return youngest entry read
1237 static int sort_entries(unsigned long long *youngest)
1245 while ((t = trace_list) != NULL) {
1246 struct blk_io_trace *bit = t->bit;
1248 trace_list = t->next;
1250 bit->time -= genesis_time;
1252 if (bit->time < *youngest || !*youngest)
1253 *youngest = bit->time;
1255 if (check_stopwatch(bit)) {
1261 if (trace_rb_insert_sort(t))
1268 static inline void put_trace(struct per_dev_info *pdi, struct trace *t)
1270 rb_erase(&t->rb_node, &rb_sort_root);
1273 trace_rb_insert_last(pdi, t);
1275 if (pdi->rb_last_entries > 1024) {
1276 struct rb_node *n = rb_first(&pdi->rb_last);
1278 t = rb_entry(n, struct trace, rb_node);
1279 rb_erase(n, &pdi->rb_last);
1280 pdi->rb_last_entries--;
1287 static int check_sequence(struct per_dev_info *pdi, struct blk_io_trace *bit,
1290 unsigned long expected_sequence = pdi->last_sequence + 1;
1294 * first entry, always ok
1296 if (!expected_sequence)
1299 if (bit->sequence == expected_sequence)
1302 if (bit->sequence < expected_sequence &&
1303 bit->time > pdi->last_reported_time)
1307 * the wanted sequence is really there, continue
1308 * because this means that the log time is earlier
1309 * on the trace we have now */
1310 t = trace_rb_find_sort(pdi->dev, expected_sequence);
1314 t = trace_rb_find_last(pdi, expected_sequence);
1319 * unless this is the last run, break and wait for more entries
1324 fprintf(stderr, "(%d,%d): skipping %lu -> %u\n", MAJOR(pdi->dev),
1325 MINOR(pdi->dev), pdi->last_sequence, bit->sequence);
1330 static void show_entries_rb(int force)
1332 struct per_dev_info *pdi = NULL;
1333 struct per_cpu_info *pci = NULL;
1334 struct blk_io_trace *bit;
1338 while ((n = rb_first(&rb_sort_root)) != NULL) {
1342 t = rb_entry(n, struct trace, rb_node);
1345 if (!pdi || pdi->dev != bit->device)
1346 pdi = get_dev_info(bit->device);
1349 fprintf(stderr, "Unknown device ID? (%d,%d)\n",
1350 MAJOR(bit->device), MINOR(bit->device));
1354 if (check_sequence(pdi, bit, force))
1357 if (!force && bit->time > last_allowed_time)
1360 pdi->last_sequence = bit->sequence;
1362 check_time(pdi, bit);
1364 if (!pci || pci->cpu != bit->cpu)
1365 pci = get_cpu_info(pdi, bit->cpu);
1367 dump_trace(bit, pci, pdi);
1373 static int read_data(int fd, void *buffer, int bytes, int block)
1375 int ret, bytes_left, fl;
1378 fl = fcntl(fd, F_GETFL);
1381 fcntl(fd, F_SETFL, fl | O_NONBLOCK);
1383 fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
1387 while (bytes_left > 0) {
1388 ret = read(fd, p, bytes_left);
1392 if (errno != EAGAIN)
1405 static int read_events(int fd, int always_block)
1407 struct per_dev_info *pdi = NULL;
1410 while (!is_done() && events < rb_batch) {
1411 struct blk_io_trace *bit;
1418 if (read_data(fd, bit, sizeof(*bit), !events || always_block))
1421 magic = be32_to_cpu(bit->magic);
1422 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
1423 fprintf(stderr, "Bad magic %x\n", magic);
1427 pdu_len = be16_to_cpu(bit->pdu_len);
1429 void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
1431 if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1))
1439 if (verify_trace(bit)) {
1445 memset(t, 0, sizeof(*t));
1448 t->next = trace_list;
1451 if (!pdi || pdi->dev != bit->device)
1452 pdi = get_dev_info(bit->device);
1454 if (bit->time > pdi->last_read_time)
1455 pdi->last_read_time = bit->time;
1463 static int do_file(void)
1465 struct per_cpu_info *pci;
1466 struct per_dev_info *pdi;
1467 int i, j, events, events_added;
1470 * first prepare all files for reading
1472 for (i = 0; i < ndevices; i++) {
1475 pdi->last_sequence = -1;
1480 pci = get_cpu_info(pdi, j);
1484 snprintf(pci->fname, sizeof(pci->fname)-1,
1485 "%s.blktrace.%d", pdi->name, pci->cpu);
1486 if (stat(pci->fname, &st) < 0)
1489 pci->fd = open(pci->fname, O_RDONLY);
1496 printf("Input file %s added\n", pci->fname);
1502 * now loop over the files reading in the data
1505 unsigned long long youngest;
1508 last_allowed_time = -1ULL;
1510 for (i = 0; i < ndevices; i++) {
1513 for (j = 0; j < pdi->nfiles; j++) {
1515 pci = get_cpu_info(pdi, j);
1520 events = read_events(pci->fd, 1);
1527 if (pdi->last_read_time < last_allowed_time)
1528 last_allowed_time = pdi->last_read_time;
1530 events_added += events;
1534 if (sort_entries(&youngest))
1537 if (youngest > stopwatch_end)
1542 } while (events_added);
1544 if (rb_sort_entries)
1550 static int do_stdin(void)
1552 unsigned long long youngest;
1555 last_allowed_time = -1ULL;
1556 fd = dup(STDIN_FILENO);
1560 events = read_events(fd, 0);
1564 if (sort_entries(&youngest))
1567 if (youngest > stopwatch_end)
1573 if (rb_sort_entries)
1580 static void flush_output(void)
1585 static void handle_sigint(int sig)
1592 * Extract start and duration times from a string, allowing
1593 * us to specify a time interval of interest within a trace.
1594 * Format: "duration" (start is zero) or "start:duration".
1596 static int find_stopwatch_interval(char *string)
1601 value = strtod(string, &sp);
1603 fprintf(stderr,"Invalid stopwatch timer: %s\n", string);
1607 stopwatch_start = DOUBLE_TO_NANO_ULL(value);
1609 value = strtod(string, &sp);
1610 if (sp == string || *sp != '\0') {
1611 fprintf(stderr,"Invalid stopwatch duration time: %s\n",
1615 } else if (*sp != '\0') {
1616 fprintf(stderr,"Invalid stopwatch start timer: %s\n", string);
1619 stopwatch_end = DOUBLE_TO_NANO_ULL(value);
1620 if (stopwatch_end <= stopwatch_start) {
1621 fprintf(stderr, "Invalid stopwatch interval: %Lu -> %Lu\n",
1622 stopwatch_start, stopwatch_end);
1629 static char usage_str[] = \
1630 "[ -i <input name> ] [-o <output name> [ -s ] [ -t ] [ -q ]\n" \
1631 "[ -w start:stop ] [ -f output format ] [ -F format spec ] [ -v] \n\n" \
1632 "\t-i Input file containing trace data, or '-' for stdin\n" \
1633 "\t-o Output file. If not given, output is stdout\n" \
1634 "\t-b stdin read batching\n" \
1635 "\t-s Show per-program io statistics\n" \
1636 "\t-n Hash processes by name, not pid\n" \
1637 "\t-t Track individual ios. Will tell you the time a request took\n" \
1638 "\t to get queued, to get dispatched, and to get completed\n" \
1639 "\t-q Quiet. Don't display any stats at the end of the trace\n" \
1640 "\t-w Only parse data between the given time interval in seconds.\n" \
1641 "\t If 'start' isn't given, blkparse defaults the start time to 0\n" \
1642 "\t -f Output format. Customize the output format. The format field\n" \
1643 "\t identifies can be found in the documentation\n" \
1644 "\t-F Format specification. Can be found in the documentation\n" \
1645 "\t-v Print program version info\n\n";
1647 static void usage(char *prog)
1649 fprintf(stderr, "Usage: %s %s %s", prog, blkparse_version, usage_str);
1652 int main(int argc, char *argv[])
1656 int per_device_and_cpu_stats = 1;
1658 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
1661 if (!strcmp(optarg, "-") && !pipeline)
1663 else if (resize_devices(optarg) != 0)
1667 output_name = optarg;
1670 rb_batch = atoi(optarg);
1672 rb_batch = RB_BATCH_DEFAULT;
1675 per_process_stats = 1;
1681 per_device_and_cpu_stats = 0;
1684 if (find_stopwatch_interval(optarg) != 0)
1688 set_all_format_specs(optarg);
1691 if (add_format_spec(optarg) != 0)
1695 ppi_hash_by_pid = 0;
1698 printf("%s version %s\n", argv[0], blkparse_version);
1706 while (optind < argc) {
1707 if (!strcmp(argv[optind], "-") && !pipeline)
1709 else if (resize_devices(argv[optind]) != 0)
1714 if (!pipeline && !ndevices) {
1719 memset(&rb_sort_root, 0, sizeof(rb_sort_root));
1721 signal(SIGINT, handle_sigint);
1722 signal(SIGHUP, handle_sigint);
1723 signal(SIGTERM, handle_sigint);
1725 setlocale(LC_NUMERIC, "en_US");
1728 ofp = fdopen(STDOUT_FILENO, "w");
1733 snprintf(ofname, sizeof(ofname) - 1, "%s", output_name);
1734 ofp = fopen(ofname, "w");
1743 ofp_buffer = malloc(4096);
1744 if (setvbuf(ofp, ofp_buffer, mode, 4096)) {
1754 if (per_process_stats)
1755 show_process_stats();
1757 if (per_device_and_cpu_stats)
1758 show_device_and_cpu_stats();