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>
37 static char blkparse_version[] = "0.90";
44 unsigned long long events;
45 unsigned long long last_reported_time;
46 unsigned long long last_read_time;
47 struct io_stats io_stats;
48 unsigned long last_sequence;
53 struct per_cpu_info *cpus;
56 struct per_process_info {
59 struct io_stats io_stats;
60 struct per_process_info *hash_next, *list_next;
65 unsigned long long longest_allocation_wait[2];
66 unsigned long long longest_dispatch_wait[2];
67 unsigned long long longest_completion_wait[2];
70 #define PPI_HASH_SHIFT (8)
71 static struct per_process_info *ppi_hash[1 << PPI_HASH_SHIFT];
72 static struct per_process_info *ppi_list;
73 static int ppi_list_entries;
75 #define S_OPTS "i:o:b:stqw:f:F:v"
76 static struct option l_opts[] = {
79 .has_arg = required_argument,
85 .has_arg = required_argument,
91 .has_arg = required_argument,
96 .name = "per program stats",
97 .has_arg = no_argument,
103 .has_arg = no_argument,
109 .has_arg = no_argument,
115 .has_arg = required_argument,
121 .has_arg = required_argument,
126 .name = "format-spec",
127 .has_arg = required_argument,
133 .has_arg = no_argument,
140 * for sorting the displayed output
143 struct blk_io_trace *bit;
144 struct rb_node rb_node;
149 static struct rb_root rb_sort_root;
150 static struct rb_root rb_track_root;
152 static struct trace *trace_list;
157 static struct blk_io_trace *bit_alloc_list;
158 static struct trace *t_alloc_list;
161 * for tracking individual ios
164 struct rb_node rb_node;
169 unsigned long long allocation_time;
170 unsigned long long queue_time;
171 unsigned long long dispatch_time;
172 unsigned long long completion_time;
176 static struct per_dev_info *devices;
177 static char *get_dev_name(struct per_dev_info *, char *, int);
180 static char *output_name;
182 static unsigned long long genesis_time;
183 static unsigned long long last_allowed_time;
184 static unsigned long long stopwatch_start; /* start from zero by default */
185 static unsigned long long stopwatch_end = ULONG_LONG_MAX; /* "infinity" */
187 static int per_process_stats;
188 static int track_ios;
190 #define RB_BATCH_DEFAULT (512)
191 static int rb_batch = RB_BATCH_DEFAULT;
195 #define is_done() (*(volatile int *)(&done))
196 static volatile int done;
198 static inline unsigned long hash_long(unsigned long val)
202 #elif __WORDSIZE == 64
203 val *= 0x9e37fffffffc0001UL;
205 #error unknown word size
208 return val >> (__WORDSIZE - PPI_HASH_SHIFT);
211 static inline void add_process_to_hash(struct per_process_info *ppi)
213 const int hash_idx = hash_long(ppi->pid);
215 ppi->hash_next = ppi_hash[hash_idx];
216 ppi_hash[hash_idx] = ppi;
219 static inline void add_process_to_list(struct per_process_info *ppi)
221 ppi->list_next = ppi_list;
226 static struct per_process_info *find_process_by_pid(__u32 pid)
228 const int hash_idx = hash_long(pid);
229 struct per_process_info *ppi;
231 ppi = ppi_hash[hash_idx];
236 ppi = ppi->hash_next;
242 static inline int trace_rb_insert(struct trace *t)
244 struct rb_node **p = &rb_sort_root.rb_node;
245 struct rb_node *parent = NULL;
250 __t = rb_entry(parent, struct trace, rb_node);
252 if (t->bit->time < __t->bit->time)
254 else if (t->bit->time > __t->bit->time)
256 else if (t->bit->device < __t->bit->device)
258 else if (t->bit->device > __t->bit->device)
260 else if (t->bit->sequence < __t->bit->sequence)
262 else if (t->bit->sequence > __t->bit->sequence)
264 else if (t->bit->device == __t->bit->device) {
266 "sequence alias (%d) on device %d,%d!\n",
268 MAJOR(t->bit->device), MINOR(t->bit->device));
273 rb_link_node(&t->rb_node, parent, p);
274 rb_insert_color(&t->rb_node, &rb_sort_root);
278 static inline int track_rb_insert(struct io_track *iot)
280 struct rb_node **p = &rb_track_root.rb_node;
281 struct rb_node *parent = NULL;
282 struct io_track *__iot;
287 __iot = rb_entry(parent, struct io_track, rb_node);
289 if (iot->device < __iot->device)
291 else if (iot->device > __iot->device)
293 else if (iot->sector < __iot->sector)
295 else if (iot->sector > __iot->sector)
299 "sector alias (%Lu) on device %d,%d!\n",
300 (unsigned long long) iot->sector,
301 MAJOR(iot->device), MINOR(iot->device));
306 rb_link_node(&iot->rb_node, parent, p);
307 rb_insert_color(&iot->rb_node, &rb_track_root);
311 static struct io_track *__find_track(dev_t device, __u64 sector)
313 struct rb_node **p = &rb_track_root.rb_node;
314 struct rb_node *parent = NULL;
315 struct io_track *__iot;
320 __iot = rb_entry(parent, struct io_track, rb_node);
322 if (device < __iot->device)
324 else if (device > __iot->device)
326 else if (sector < __iot->sector)
328 else if (sector > __iot->sector)
337 static struct io_track *find_track(__u32 pid, dev_t device, __u64 sector)
339 struct io_track *iot;
341 iot = __find_track(device, sector);
343 iot = malloc(sizeof(*iot));
345 iot->device = device;
346 iot->sector = sector;
347 track_rb_insert(iot);
353 static void log_track_frontmerge(struct blk_io_trace *t)
355 struct io_track *iot;
360 iot = __find_track(t->device, t->sector + (t->bytes >> 9));
362 fprintf(stderr, "failed to find mergeable event\n");
366 rb_erase(&iot->rb_node, &rb_track_root);
367 iot->sector -= t->bytes >> 9;
368 track_rb_insert(iot);
371 static void log_track_getrq(struct blk_io_trace *t)
373 struct io_track *iot;
378 iot = find_track(t->pid, t->device, t->sector);
379 iot->allocation_time = t->time;
384 * return time between rq allocation and insertion
386 static unsigned long long log_track_insert(struct blk_io_trace *t)
388 unsigned long long elapsed;
389 struct io_track *iot;
394 iot = find_track(t->pid, t->device, t->sector);
395 iot->queue_time = t->time;
396 elapsed = iot->queue_time - iot->allocation_time;
398 if (per_process_stats) {
399 struct per_process_info *ppi = find_process_by_pid(iot->pid);
400 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
402 if (ppi && elapsed > ppi->longest_allocation_wait[w])
403 ppi->longest_allocation_wait[w] = elapsed;
410 * return time between queue and issue
412 static unsigned long long log_track_issue(struct blk_io_trace *t)
414 unsigned long long elapsed;
415 struct io_track *iot;
419 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
422 iot = __find_track(t->device, t->sector);
424 fprintf(stderr, "failed to find issue event\n");
428 iot->dispatch_time = t->time;
429 elapsed = iot->dispatch_time - iot->queue_time;
431 if (per_process_stats) {
432 struct per_process_info *ppi = find_process_by_pid(iot->pid);
433 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
435 if (ppi && elapsed > ppi->longest_dispatch_wait[w])
436 ppi->longest_dispatch_wait[w] = elapsed;
443 * return time between dispatch and complete
445 static unsigned long long log_track_complete(struct blk_io_trace *t)
447 unsigned long long elapsed;
448 struct io_track *iot;
452 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
455 iot = __find_track(t->device, t->sector);
457 fprintf(stderr, "failed to find complete event\n");
461 iot->completion_time = t->time;
462 elapsed = iot->completion_time - iot->dispatch_time;
464 if (per_process_stats) {
465 struct per_process_info *ppi = find_process_by_pid(iot->pid);
466 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
468 if (ppi && elapsed > ppi->longest_completion_wait[w])
469 ppi->longest_completion_wait[w] = elapsed;
473 * kill the trace, we don't need it after completion
475 rb_erase(&iot->rb_node, &rb_track_root);
482 static struct io_stats *find_process_io_stats(__u32 pid, char *name)
484 struct per_process_info *ppi = find_process_by_pid(pid);
487 ppi = malloc(sizeof(*ppi));
488 memset(ppi, 0, sizeof(*ppi));
489 strncpy(ppi->name, name, sizeof(ppi->name));
491 add_process_to_hash(ppi);
492 add_process_to_list(ppi);
495 return &ppi->io_stats;
499 static void resize_cpu_info(struct per_dev_info *pdi, int cpu)
501 struct per_cpu_info *cpus = pdi->cpus;
502 int ncpus = pdi->ncpus;
503 int new_count = cpu + 1;
507 size = new_count * sizeof(struct per_cpu_info);
508 cpus = realloc(cpus, size);
511 fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n",
512 get_dev_name(pdi, name, sizeof(name)), size);
516 new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info));
517 new_space = (new_count - ncpus) * sizeof(struct per_cpu_info);
518 memset(new_start, 0, new_space);
520 pdi->ncpus = new_count;
524 static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu)
526 struct per_cpu_info *pci;
528 if (cpu >= pdi->ncpus)
529 resize_cpu_info(pdi, cpu);
531 pci = &pdi->cpus[cpu];
537 static int resize_devices(char *name)
539 int size = (ndevices + 1) * sizeof(struct per_dev_info);
541 devices = realloc(devices, size);
543 fprintf(stderr, "Out of memory, device %s (%d)\n", name, size);
546 memset(&devices[ndevices], 0, sizeof(struct per_dev_info));
547 devices[ndevices].name = name;
552 static struct per_dev_info *get_dev_info(dev_t id)
554 struct per_dev_info *pdi;
557 for (i = 0; i < ndevices; i++) {
560 if (devices[i].id == id)
564 if (resize_devices(NULL) != 0)
567 pdi = &devices[ndevices - 1];
569 pdi->last_sequence = -1;
570 pdi->last_read_time = 0;
574 static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size)
577 snprintf(buffer, size, "%s", pdi->name);
579 snprintf(buffer, size, "%d,%d", MAJOR(pdi->id), MINOR(pdi->id));
583 static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit)
585 unsigned long long this = bit->time;
586 unsigned long long last = pdi->last_reported_time;
588 pdi->backwards = (this < last) ? 'B' : ' ';
589 pdi->last_reported_time = this;
592 static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t,
597 ios->qwrite_kb += t->bytes >> 10;
600 ios->qread_kb += t->bytes >> 10;
604 static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci,
607 __account_m(&pci->io_stats, t, rw);
609 if (per_process_stats) {
610 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
612 __account_m(ios, t, rw);
616 static inline void __account_queue(struct io_stats *ios, struct blk_io_trace *t,
621 ios->qwrite_kb += t->bytes >> 10;
624 ios->qread_kb += t->bytes >> 10;
628 static inline void account_queue(struct blk_io_trace *t,
629 struct per_cpu_info *pci, int rw)
631 __account_queue(&pci->io_stats, t, rw);
633 if (per_process_stats) {
634 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
636 __account_queue(ios, t, rw);
640 static inline void __account_c(struct io_stats *ios, int rw, unsigned int bytes)
644 ios->cwrite_kb += bytes >> 10;
647 ios->cread_kb += bytes >> 10;
651 static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci,
654 __account_c(&pci->io_stats, rw, bytes);
656 if (per_process_stats) {
657 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
659 __account_c(ios, rw, bytes);
663 static inline void __account_issue(struct io_stats *ios, int rw,
668 ios->iwrite_kb += bytes >> 10;
671 ios->iread_kb += bytes >> 10;
675 static inline void account_issue(struct blk_io_trace *t,
676 struct per_cpu_info *pci, int rw)
678 __account_issue(&pci->io_stats, rw, t->bytes);
680 if (per_process_stats) {
681 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
683 __account_issue(ios, rw, t->bytes);
687 static inline void __account_unplug(struct io_stats *ios, int timer)
690 ios->timer_unplugs++;
695 static inline void account_unplug(struct blk_io_trace *t,
696 struct per_cpu_info *pci, int timer)
698 __account_unplug(&pci->io_stats, timer);
700 if (per_process_stats) {
701 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
703 __account_unplug(ios, timer);
707 static void log_complete(struct per_cpu_info *pci, struct blk_io_trace *t,
710 process_fmt(act, pci, t, log_track_complete(t), 0, NULL);
713 static void log_insert(struct per_cpu_info *pci, struct blk_io_trace *t,
716 process_fmt(act, pci, t, log_track_insert(t), 0, NULL);
719 static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t,
722 process_fmt(act, pci, t, -1, 0, NULL);
725 static void log_issue(struct per_cpu_info *pci, struct blk_io_trace *t,
728 process_fmt(act, pci, t, log_track_issue(t), 0, NULL);
731 static void log_merge(struct per_cpu_info *pci, struct blk_io_trace *t,
735 log_track_frontmerge(t);
737 process_fmt(act, pci, t, -1ULL, 0, NULL);
740 static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t,
743 process_fmt(act, pci, t, -1ULL, 0, NULL);
746 static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t,
749 process_fmt(act, pci, t, -1ULL, 0, NULL);
752 static void log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t,
755 process_fmt(act, pci, t, -1ULL, 0, NULL);
758 static void log_split(struct per_cpu_info *pci, struct blk_io_trace *t,
761 process_fmt(act, pci, t, -1ULL, 0, NULL);
764 static void log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act)
766 unsigned char *buf = (unsigned char *) t + sizeof(*t);
768 process_fmt(act, pci, t, -1ULL, t->pdu_len, buf);
771 static void dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci)
773 int act = t->action & 0xffff;
777 log_generic(pci, t, "Q");
780 log_generic(pci, t, "G");
782 case __BLK_TA_SLEEPRQ:
783 log_generic(pci, t, "S");
785 case __BLK_TA_REQUEUE:
786 log_generic(pci, t, "R");
791 case __BLK_TA_COMPLETE:
794 case __BLK_TA_INSERT:
798 fprintf(stderr, "Bad pc action %x\n", act);
803 static void dump_trace_fs(struct blk_io_trace *t, struct per_cpu_info *pci)
805 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
806 int act = t->action & 0xffff;
810 account_queue(t, pci, w);
811 log_queue(pci, t, "Q");
813 case __BLK_TA_INSERT:
814 log_insert(pci, t, "I");
816 case __BLK_TA_BACKMERGE:
817 account_m(t, pci, w);
818 log_merge(pci, t, "M");
820 case __BLK_TA_FRONTMERGE:
821 account_m(t, pci, w);
822 log_merge(pci, t, "F");
826 log_generic(pci, t, "G");
828 case __BLK_TA_SLEEPRQ:
829 log_generic(pci, t, "S");
831 case __BLK_TA_REQUEUE:
832 account_c(t, pci, w, -t->bytes);
833 log_queue(pci, t, "R");
836 account_issue(t, pci, w);
837 log_issue(pci, t, "D");
839 case __BLK_TA_COMPLETE:
840 account_c(t, pci, w, t->bytes);
841 log_complete(pci, t, "C");
844 log_action(pci, t, "P");
846 case __BLK_TA_UNPLUG_IO:
847 account_unplug(t, pci, 0);
848 log_unplug(pci, t, "U");
850 case __BLK_TA_UNPLUG_TIMER:
851 account_unplug(t, pci, 1);
852 log_unplug(pci, t, "UT");
855 log_split(pci, t, "X");
857 case __BLK_TA_BOUNCE:
858 log_generic(pci, t, "B");
861 fprintf(stderr, "Bad fs action %x\n", t->action);
866 static void dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
867 struct per_dev_info *pdi)
869 if (t->action & BLK_TC_ACT(BLK_TC_PC))
870 dump_trace_pc(t, pci);
872 dump_trace_fs(t, pci);
877 static void dump_io_stats(struct io_stats *ios, char *msg)
879 fprintf(ofp, "%s\n", msg);
881 fprintf(ofp, " Reads Queued: %'8lu, %'8LuKiB\t", ios->qreads, ios->qread_kb);
882 fprintf(ofp, " Writes Queued: %'8lu, %'8LuKiB\n", ios->qwrites,ios->qwrite_kb);
884 fprintf(ofp, " Read Dispatches: %'8lu, %'8LuKiB\t", ios->ireads, ios->iread_kb);
885 fprintf(ofp, " Write Dispatches: %'8lu, %'8LuKiB\n", ios->iwrites,ios->iwrite_kb);
886 fprintf(ofp, " Reads Completed: %'8lu, %'8LuKiB\t", ios->creads, ios->cread_kb);
887 fprintf(ofp, " Writes Completed: %'8lu, %'8LuKiB\n", ios->cwrites,ios->cwrite_kb);
888 fprintf(ofp, " Read Merges: %'8lu%8c\t", ios->mreads, ' ');
889 fprintf(ofp, " Write Merges: %'8lu\n", ios->mwrites);
890 fprintf(ofp, " IO unplugs: %'8lu%8c\t", ios->io_unplugs, ' ');
891 fprintf(ofp, " Timer unplugs: %'8lu\n", ios->timer_unplugs);
894 static void dump_wait_stats(struct per_process_info *ppi)
896 unsigned long rawait = ppi->longest_allocation_wait[0] / 1000;
897 unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000;
898 unsigned long rcwait = ppi->longest_completion_wait[0] / 1000;
899 unsigned long wawait = ppi->longest_allocation_wait[1] / 1000;
900 unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000;
901 unsigned long wcwait = ppi->longest_completion_wait[1] / 1000;
903 fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' ');
904 fprintf(ofp, " Allocation wait: %'8lu\n", wawait);
905 fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' ');
906 fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait);
907 fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' ');
908 fprintf(ofp, " Completion wait: %'8lu\n", wcwait);
911 static int ppi_name_compare(const void *p1, const void *p2)
913 struct per_process_info *ppi1 = *((struct per_process_info **) p1);
914 struct per_process_info *ppi2 = *((struct per_process_info **) p2);
917 res = strverscmp(ppi1->name, ppi2->name);
919 res = ppi1->pid > ppi2->pid;
924 static void sort_process_list(void)
926 struct per_process_info **ppis;
927 struct per_process_info *ppi;
930 ppis = malloc(ppi_list_entries * sizeof(struct per_process_info *));
935 ppi = ppi->list_next;
938 qsort(ppis, ppi_list_entries, sizeof(ppi), ppi_name_compare);
940 i = ppi_list_entries - 1;
945 ppi->list_next = ppi_list;
953 static void show_process_stats(void)
955 struct per_process_info *ppi;
963 snprintf(name, sizeof(name)-1, "%s (%u)", ppi->name, ppi->pid);
964 dump_io_stats(&ppi->io_stats, name);
965 dump_wait_stats(ppi);
966 ppi = ppi->list_next;
972 static void show_device_and_cpu_stats(void)
974 struct per_dev_info *pdi;
975 struct per_cpu_info *pci;
976 struct io_stats total, *ios;
977 int i, j, pci_events;
978 char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3];
981 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
983 memset(&total, 0, sizeof(total));
989 for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) {
993 ios = &pci->io_stats;
994 total.qreads += ios->qreads;
995 total.qwrites += ios->qwrites;
996 total.creads += ios->creads;
997 total.cwrites += ios->cwrites;
998 total.mreads += ios->mreads;
999 total.mwrites += ios->mwrites;
1000 total.ireads += ios->ireads;
1001 total.iwrites += ios->iwrites;
1002 total.qread_kb += ios->qread_kb;
1003 total.qwrite_kb += ios->qwrite_kb;
1004 total.cread_kb += ios->cread_kb;
1005 total.cwrite_kb += ios->cwrite_kb;
1006 total.iread_kb += ios->iread_kb;
1007 total.iwrite_kb += ios->iwrite_kb;
1008 total.timer_unplugs += ios->timer_unplugs;
1009 total.io_unplugs += ios->io_unplugs;
1011 snprintf(line, sizeof(line) - 1, "CPU%d (%s):",
1012 j, get_dev_name(pdi, name, sizeof(name)));
1013 dump_io_stats(ios, line);
1017 if (pci_events > 1) {
1019 snprintf(line, sizeof(line) - 1, "Total (%s):",
1020 get_dev_name(pdi, name, sizeof(name)));
1021 dump_io_stats(&total, line);
1024 fprintf(ofp, "\nEvents (%s): %'Lu entries, %'lu skips\n",
1025 get_dev_name(pdi, line, sizeof(line)), pdi->events,
1030 static int sort_entries(void)
1035 while ((t = trace_list) != NULL) {
1036 trace_list = t->next;
1038 if (verify_trace(t->bit))
1040 if (trace_rb_insert(t))
1049 * struct trace and blktrace allocation cache, we do potentially
1050 * millions of mallocs for these structures while only using at most
1051 * a few thousand at the time
1053 static inline void t_free(struct trace *t)
1055 t->next = t_alloc_list;
1059 static inline struct trace *t_alloc(void)
1061 struct trace *t = t_alloc_list;
1064 t_alloc_list = t->next;
1068 return malloc(sizeof(*t));
1071 static inline void bit_free(struct blk_io_trace *bit)
1074 * abuse a 64-bit field for a next pointer for the free item
1076 bit->time = (__u64) (unsigned long) bit_alloc_list;
1077 bit_alloc_list = (struct blk_io_trace *) bit;
1080 static inline struct blk_io_trace *bit_alloc(void)
1082 struct blk_io_trace *bit = bit_alloc_list;
1085 bit_alloc_list = (struct blk_io_trace *) (unsigned long) \
1090 return malloc(sizeof(*bit));
1093 static void show_entries_rb(void)
1095 struct per_dev_info *pdi = NULL;
1096 struct per_cpu_info *pci = NULL;
1097 struct blk_io_trace *bit;
1101 while ((n = rb_first(&rb_sort_root)) != NULL) {
1106 t = rb_entry(n, struct trace, rb_node);
1109 if (!pdi || pdi->id != bit->device)
1110 pdi = get_dev_info(bit->device);
1113 fprintf(stderr, "Unknown device ID? (%d,%d)\n",
1114 MAJOR(bit->device), MINOR(bit->device));
1118 if (bit->cpu > pdi->ncpus) {
1119 fprintf(stderr, "Unknown CPU ID? (%d, device %d,%d)\n",
1120 bit->cpu, MAJOR(bit->device),
1121 MINOR(bit->device));
1126 * back off displaying more info if we are out of sync
1127 * on SMP systems. to prevent stalling on lost events,
1128 * only allow an event to skip us a few times
1130 if (bit->sequence != (pdi->last_sequence + 1)
1131 && pdi->last_sequence != -1) {
1132 if (t->skipped < 5) {
1136 fprintf(stderr, "skipping from %lu to %u\n", pdi->last_sequence, bit->sequence);
1141 if (bit->time >= stopwatch_end || bit->time > last_allowed_time)
1144 pdi->last_sequence = bit->sequence;
1146 if (bit->time >= stopwatch_start) {
1147 check_time(pdi, bit);
1149 if (!pci || pci->cpu != bit->cpu)
1150 pci = get_cpu_info(pdi, bit->cpu);
1152 dump_trace(bit, pci, pdi);
1155 rb_erase(&t->rb_node, &rb_sort_root);
1161 static int read_data(int fd, void *buffer, int bytes, int block)
1163 int ret, bytes_left, fl;
1166 fl = fcntl(fd, F_GETFL);
1169 fcntl(fd, F_SETFL, fl | O_NONBLOCK);
1171 fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
1175 while (bytes_left > 0) {
1176 ret = read(fd, p, bytes_left);
1180 if (errno != EAGAIN)
1192 static int read_events(int fd)
1194 struct per_dev_info *pdi = NULL;
1197 while (!is_done() && events < rb_batch) {
1198 struct blk_io_trace *bit;
1205 if (read_data(fd, bit, sizeof(*bit), !events))
1208 magic = be32_to_cpu(bit->magic);
1209 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
1210 fprintf(stderr, "Bad magic %x\n", magic);
1214 pdu_len = be16_to_cpu(bit->pdu_len);
1216 void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
1218 if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1))
1225 memset(t, 0, sizeof(*t));
1229 t->next = trace_list;
1232 if (genesis_time == 0 || t->bit->time < genesis_time)
1233 genesis_time = t->bit->time;
1235 bit->time -= genesis_time;
1237 if (!pdi || pdi->id != bit->device)
1238 pdi = get_dev_info(bit->device);
1240 if (bit->time > pdi->last_read_time)
1241 pdi->last_read_time = bit->time;
1249 static int do_file(void)
1251 struct per_cpu_info *pci;
1252 struct per_dev_info *pdi;
1253 int i, j, events, events_added;
1256 * first prepare all files for reading
1258 for (i = 0; i < ndevices; i++) {
1261 pdi->last_sequence = -1;
1266 pci = get_cpu_info(pdi, j);
1270 snprintf(pci->fname, sizeof(pci->fname)-1,
1271 "%s.blktrace.%d", pdi->name, pci->cpu);
1272 if (stat(pci->fname, &st) < 0)
1275 pci->fd = open(pci->fname, O_RDONLY);
1282 printf("Input file %s added\n", pci->fname);
1288 * now loop over the files reading in the data
1292 last_allowed_time = -1ULL;
1294 for (i = 0; i < ndevices; i++) {
1297 for (j = 0; j < pdi->nfiles; j++) {
1299 pci = get_cpu_info(pdi, j);
1304 events = read_events(pci->fd);
1311 if (pdi->last_read_time < last_allowed_time)
1312 last_allowed_time = pdi->last_read_time;
1314 events_added += events;
1318 if (sort_entries() == -1)
1323 } while (events_added);
1328 static int do_stdin(void)
1332 last_allowed_time = -1ULL;
1333 fd = dup(STDIN_FILENO);
1337 events = read_events(fd);
1341 if (sort_entries() == -1)
1351 static void flush_output(void)
1356 static void handle_sigint(int sig)
1363 * Extract start and duration times from a string, allowing
1364 * us to specify a time interval of interest within a trace.
1365 * Format: "duration" (start is zero) or "start:duration".
1367 static int find_stopwatch_interval(char *string)
1372 value = strtod(string, &sp);
1374 fprintf(stderr,"Invalid stopwatch timer: %s\n", string);
1378 stopwatch_start = DOUBLE_TO_NANO_ULL(value);
1380 value = strtod(string, &sp);
1381 if (sp == string || *sp != '\0') {
1382 fprintf(stderr,"Invalid stopwatch duration time: %s\n",
1386 } else if (*sp != '\0') {
1387 fprintf(stderr,"Invalid stopwatch start timer: %s\n", string);
1390 stopwatch_end = DOUBLE_TO_NANO_ULL(value);
1391 if (stopwatch_end <= stopwatch_start) {
1392 fprintf(stderr, "Invalid stopwatch interval: %Lu -> %Lu\n",
1393 stopwatch_start, stopwatch_end);
1400 static char usage_str[] = \
1401 "[ -i <input name> ] [-o <output name> [ -s ] [ -t ] [ -q ]\n" \
1402 "[ -w start:stop ] [ -f output format ] [ -F format spec ] [ -v] \n\n" \
1403 "\t-i Input file containing trace data, or '-' for stdin\n" \
1404 "\t-o Output file. If not given, output is stdout\n" \
1405 "\t-b stdin read batching\n" \
1406 "\t-s Show per-program io statistics\n" \
1407 "\t-t Track individual ios. Will tell you the time a request took\n" \
1408 "\t to get queued, to get dispatched, and to get completed\n" \
1409 "\t-q Quiet. Don't display any stats at the end of the trace\n" \
1410 "\t-w Only parse data between the given time interval in seconds.\n" \
1411 "\t If 'start' isn't given, blkparse defaults the start time to 0\n" \
1412 "\t -f Output format. Customize the output format. The format field\n" \
1413 "\t identifies can be found in the documentation\n" \
1414 "\t-F Format specification. Can be found in the documentation\n" \
1415 "\t-v Print program version info\n\n";
1417 static void usage(char *prog)
1419 fprintf(stderr, "Usage: %s %s %s", prog, blkparse_version, usage_str);
1422 int main(int argc, char *argv[])
1426 int per_device_and_cpu_stats = 1;
1428 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
1431 if (!strcmp(optarg, "-") && !pipeline)
1433 else if (resize_devices(optarg) != 0)
1437 output_name = optarg;
1440 rb_batch = atoi(optarg);
1442 rb_batch = RB_BATCH_DEFAULT;
1445 per_process_stats = 1;
1451 per_device_and_cpu_stats = 0;
1454 if (find_stopwatch_interval(optarg) != 0)
1458 set_all_format_specs(optarg);
1461 if (add_format_spec(optarg) != 0)
1465 printf("%s version %s\n", argv[0], blkparse_version);
1473 while (optind < argc) {
1474 if (!strcmp(argv[optind], "-") && !pipeline)
1476 else if (resize_devices(argv[optind]) != 0)
1481 if (!pipeline && !ndevices) {
1486 memset(&rb_sort_root, 0, sizeof(rb_sort_root));
1487 memset(&rb_track_root, 0, sizeof(rb_track_root));
1489 signal(SIGINT, handle_sigint);
1490 signal(SIGHUP, handle_sigint);
1491 signal(SIGTERM, handle_sigint);
1493 setlocale(LC_NUMERIC, "en_US");
1496 ofp = fdopen(STDOUT_FILENO, "w");
1501 snprintf(ofname, sizeof(ofname) - 1, "%s", output_name);
1502 ofp = fopen(ofname, "w");
1511 ofp_buffer = malloc(4096);
1512 if (setvbuf(ofp, ofp_buffer, mode, 4096)) {
1522 if (per_process_stats)
1523 show_process_stats();
1525 if (per_device_and_cpu_stats)
1526 show_device_and_cpu_stats();