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 unsigned long rb_sort_entries;
152 static struct rb_root rb_track_root;
154 static struct trace *trace_list;
159 static struct blk_io_trace *bit_alloc_list;
160 static struct trace *t_alloc_list;
163 * for tracking individual ios
166 struct rb_node rb_node;
171 unsigned long long allocation_time;
172 unsigned long long queue_time;
173 unsigned long long dispatch_time;
174 unsigned long long completion_time;
178 static struct per_dev_info *devices;
179 static char *get_dev_name(struct per_dev_info *, char *, int);
182 static char *output_name;
184 static unsigned long long genesis_time;
185 static unsigned long long last_allowed_time;
186 static unsigned long long stopwatch_start; /* start from zero by default */
187 static unsigned long long stopwatch_end = ULONG_LONG_MAX; /* "infinity" */
189 static int per_process_stats;
190 static int track_ios;
192 #define RB_BATCH_DEFAULT (512)
193 static int rb_batch = RB_BATCH_DEFAULT;
197 #define is_done() (*(volatile int *)(&done))
198 static volatile int done;
200 static inline unsigned long hash_long(unsigned long val)
204 #elif __WORDSIZE == 64
205 val *= 0x9e37fffffffc0001UL;
207 #error unknown word size
210 return val >> (__WORDSIZE - PPI_HASH_SHIFT);
213 static inline void add_process_to_hash(struct per_process_info *ppi)
215 const int hash_idx = hash_long(ppi->pid);
217 ppi->hash_next = ppi_hash[hash_idx];
218 ppi_hash[hash_idx] = ppi;
221 static inline void add_process_to_list(struct per_process_info *ppi)
223 ppi->list_next = ppi_list;
228 static struct per_process_info *find_process_by_pid(__u32 pid)
230 const int hash_idx = hash_long(pid);
231 struct per_process_info *ppi;
233 ppi = ppi_hash[hash_idx];
238 ppi = ppi->hash_next;
244 static inline int trace_rb_insert(struct trace *t)
246 struct rb_node **p = &rb_sort_root.rb_node;
247 struct rb_node *parent = NULL;
252 __t = rb_entry(parent, struct trace, rb_node);
254 if (t->bit->time < __t->bit->time)
256 else if (t->bit->time > __t->bit->time)
258 else if (t->bit->device < __t->bit->device)
260 else if (t->bit->device > __t->bit->device)
262 else if (t->bit->sequence < __t->bit->sequence)
264 else if (t->bit->sequence > __t->bit->sequence)
266 else if (t->bit->device == __t->bit->device) {
268 "sequence alias (%d) on device %d,%d!\n",
270 MAJOR(t->bit->device), MINOR(t->bit->device));
276 rb_link_node(&t->rb_node, parent, p);
277 rb_insert_color(&t->rb_node, &rb_sort_root);
281 static inline int track_rb_insert(struct io_track *iot)
283 struct rb_node **p = &rb_track_root.rb_node;
284 struct rb_node *parent = NULL;
285 struct io_track *__iot;
290 __iot = rb_entry(parent, struct io_track, rb_node);
292 if (iot->device < __iot->device)
294 else if (iot->device > __iot->device)
296 else if (iot->sector < __iot->sector)
298 else if (iot->sector > __iot->sector)
302 "sector alias (%Lu) on device %d,%d!\n",
303 (unsigned long long) iot->sector,
304 MAJOR(iot->device), MINOR(iot->device));
309 rb_link_node(&iot->rb_node, parent, p);
310 rb_insert_color(&iot->rb_node, &rb_track_root);
314 static struct io_track *__find_track(dev_t device, __u64 sector)
316 struct rb_node **p = &rb_track_root.rb_node;
317 struct rb_node *parent = NULL;
318 struct io_track *__iot;
323 __iot = rb_entry(parent, struct io_track, rb_node);
325 if (device < __iot->device)
327 else if (device > __iot->device)
329 else if (sector < __iot->sector)
331 else if (sector > __iot->sector)
340 static struct io_track *find_track(__u32 pid, dev_t device, __u64 sector)
342 struct io_track *iot;
344 iot = __find_track(device, sector);
346 iot = malloc(sizeof(*iot));
348 iot->device = device;
349 iot->sector = sector;
350 track_rb_insert(iot);
356 static void log_track_frontmerge(struct blk_io_trace *t)
358 struct io_track *iot;
363 iot = __find_track(t->device, t->sector + (t->bytes >> 9));
365 fprintf(stderr, "failed to find mergeable event\n");
369 rb_erase(&iot->rb_node, &rb_track_root);
370 iot->sector -= t->bytes >> 9;
371 track_rb_insert(iot);
374 static void log_track_getrq(struct blk_io_trace *t)
376 struct io_track *iot;
381 iot = find_track(t->pid, t->device, t->sector);
382 iot->allocation_time = t->time;
387 * return time between rq allocation and insertion
389 static unsigned long long log_track_insert(struct blk_io_trace *t)
391 unsigned long long elapsed;
392 struct io_track *iot;
397 iot = find_track(t->pid, t->device, t->sector);
398 iot->queue_time = t->time;
399 elapsed = iot->queue_time - iot->allocation_time;
401 if (per_process_stats) {
402 struct per_process_info *ppi = find_process_by_pid(iot->pid);
403 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
405 if (ppi && elapsed > ppi->longest_allocation_wait[w])
406 ppi->longest_allocation_wait[w] = elapsed;
413 * return time between queue and issue
415 static unsigned long long log_track_issue(struct blk_io_trace *t)
417 unsigned long long elapsed;
418 struct io_track *iot;
422 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
425 iot = __find_track(t->device, t->sector);
427 fprintf(stderr, "failed to find issue event\n");
431 iot->dispatch_time = t->time;
432 elapsed = iot->dispatch_time - iot->queue_time;
434 if (per_process_stats) {
435 struct per_process_info *ppi = find_process_by_pid(iot->pid);
436 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
438 if (ppi && elapsed > ppi->longest_dispatch_wait[w])
439 ppi->longest_dispatch_wait[w] = elapsed;
446 * return time between dispatch and complete
448 static unsigned long long log_track_complete(struct blk_io_trace *t)
450 unsigned long long elapsed;
451 struct io_track *iot;
455 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
458 iot = __find_track(t->device, t->sector);
460 fprintf(stderr, "failed to find complete event\n");
464 iot->completion_time = t->time;
465 elapsed = iot->completion_time - iot->dispatch_time;
467 if (per_process_stats) {
468 struct per_process_info *ppi = find_process_by_pid(iot->pid);
469 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
471 if (ppi && elapsed > ppi->longest_completion_wait[w])
472 ppi->longest_completion_wait[w] = elapsed;
476 * kill the trace, we don't need it after completion
478 rb_erase(&iot->rb_node, &rb_track_root);
485 static struct io_stats *find_process_io_stats(__u32 pid, char *name)
487 struct per_process_info *ppi = find_process_by_pid(pid);
490 ppi = malloc(sizeof(*ppi));
491 memset(ppi, 0, sizeof(*ppi));
492 strncpy(ppi->name, name, sizeof(ppi->name));
494 add_process_to_hash(ppi);
495 add_process_to_list(ppi);
498 return &ppi->io_stats;
502 static void resize_cpu_info(struct per_dev_info *pdi, int cpu)
504 struct per_cpu_info *cpus = pdi->cpus;
505 int ncpus = pdi->ncpus;
506 int new_count = cpu + 1;
510 size = new_count * sizeof(struct per_cpu_info);
511 cpus = realloc(cpus, size);
514 fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n",
515 get_dev_name(pdi, name, sizeof(name)), size);
519 new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info));
520 new_space = (new_count - ncpus) * sizeof(struct per_cpu_info);
521 memset(new_start, 0, new_space);
523 pdi->ncpus = new_count;
527 static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu)
529 struct per_cpu_info *pci;
531 if (cpu >= pdi->ncpus)
532 resize_cpu_info(pdi, cpu);
534 pci = &pdi->cpus[cpu];
540 static int resize_devices(char *name)
542 int size = (ndevices + 1) * sizeof(struct per_dev_info);
544 devices = realloc(devices, size);
546 fprintf(stderr, "Out of memory, device %s (%d)\n", name, size);
549 memset(&devices[ndevices], 0, sizeof(struct per_dev_info));
550 devices[ndevices].name = name;
555 static struct per_dev_info *get_dev_info(dev_t id)
557 struct per_dev_info *pdi;
560 for (i = 0; i < ndevices; i++) {
563 if (devices[i].id == id)
567 if (resize_devices(NULL) != 0)
570 pdi = &devices[ndevices - 1];
572 pdi->last_sequence = -1;
573 pdi->last_read_time = 0;
577 static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size)
580 snprintf(buffer, size, "%s", pdi->name);
582 snprintf(buffer, size, "%d,%d", MAJOR(pdi->id), MINOR(pdi->id));
586 static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit)
588 unsigned long long this = bit->time;
589 unsigned long long last = pdi->last_reported_time;
591 pdi->backwards = (this < last) ? 'B' : ' ';
592 pdi->last_reported_time = this;
595 static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t,
600 ios->qwrite_kb += t->bytes >> 10;
603 ios->qread_kb += t->bytes >> 10;
607 static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci,
610 __account_m(&pci->io_stats, t, rw);
612 if (per_process_stats) {
613 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
615 __account_m(ios, t, rw);
619 static inline void __account_queue(struct io_stats *ios, struct blk_io_trace *t,
624 ios->qwrite_kb += t->bytes >> 10;
627 ios->qread_kb += t->bytes >> 10;
631 static inline void account_queue(struct blk_io_trace *t,
632 struct per_cpu_info *pci, int rw)
634 __account_queue(&pci->io_stats, t, rw);
636 if (per_process_stats) {
637 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
639 __account_queue(ios, t, rw);
643 static inline void __account_c(struct io_stats *ios, int rw, unsigned int bytes)
647 ios->cwrite_kb += bytes >> 10;
650 ios->cread_kb += bytes >> 10;
654 static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci,
657 __account_c(&pci->io_stats, rw, bytes);
659 if (per_process_stats) {
660 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
662 __account_c(ios, rw, bytes);
666 static inline void __account_issue(struct io_stats *ios, int rw,
671 ios->iwrite_kb += bytes >> 10;
674 ios->iread_kb += bytes >> 10;
678 static inline void account_issue(struct blk_io_trace *t,
679 struct per_cpu_info *pci, int rw)
681 __account_issue(&pci->io_stats, rw, t->bytes);
683 if (per_process_stats) {
684 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
686 __account_issue(ios, rw, t->bytes);
690 static inline void __account_unplug(struct io_stats *ios, int timer)
693 ios->timer_unplugs++;
698 static inline void account_unplug(struct blk_io_trace *t,
699 struct per_cpu_info *pci, int timer)
701 __account_unplug(&pci->io_stats, timer);
703 if (per_process_stats) {
704 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
706 __account_unplug(ios, timer);
710 static void log_complete(struct per_cpu_info *pci, struct blk_io_trace *t,
713 process_fmt(act, pci, t, log_track_complete(t), 0, NULL);
716 static void log_insert(struct per_cpu_info *pci, struct blk_io_trace *t,
719 process_fmt(act, pci, t, log_track_insert(t), 0, NULL);
722 static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t,
725 process_fmt(act, pci, t, -1, 0, NULL);
728 static void log_issue(struct per_cpu_info *pci, struct blk_io_trace *t,
731 process_fmt(act, pci, t, log_track_issue(t), 0, NULL);
734 static void log_merge(struct per_cpu_info *pci, struct blk_io_trace *t,
738 log_track_frontmerge(t);
740 process_fmt(act, pci, t, -1ULL, 0, NULL);
743 static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t,
746 process_fmt(act, pci, t, -1ULL, 0, NULL);
749 static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t,
752 process_fmt(act, pci, t, -1ULL, 0, NULL);
755 static void log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t,
758 process_fmt(act, pci, t, -1ULL, 0, NULL);
761 static void log_split(struct per_cpu_info *pci, struct blk_io_trace *t,
764 process_fmt(act, pci, t, -1ULL, 0, NULL);
767 static void log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act)
769 unsigned char *buf = (unsigned char *) t + sizeof(*t);
771 process_fmt(act, pci, t, -1ULL, t->pdu_len, buf);
774 static void dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci)
776 int act = t->action & 0xffff;
780 log_generic(pci, t, "Q");
783 log_generic(pci, t, "G");
785 case __BLK_TA_SLEEPRQ:
786 log_generic(pci, t, "S");
788 case __BLK_TA_REQUEUE:
789 log_generic(pci, t, "R");
794 case __BLK_TA_COMPLETE:
797 case __BLK_TA_INSERT:
801 fprintf(stderr, "Bad pc action %x\n", act);
806 static void dump_trace_fs(struct blk_io_trace *t, struct per_cpu_info *pci)
808 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
809 int act = t->action & 0xffff;
813 account_queue(t, pci, w);
814 log_queue(pci, t, "Q");
816 case __BLK_TA_INSERT:
817 log_insert(pci, t, "I");
819 case __BLK_TA_BACKMERGE:
820 account_m(t, pci, w);
821 log_merge(pci, t, "M");
823 case __BLK_TA_FRONTMERGE:
824 account_m(t, pci, w);
825 log_merge(pci, t, "F");
829 log_generic(pci, t, "G");
831 case __BLK_TA_SLEEPRQ:
832 log_generic(pci, t, "S");
834 case __BLK_TA_REQUEUE:
835 account_c(t, pci, w, -t->bytes);
836 log_queue(pci, t, "R");
839 account_issue(t, pci, w);
840 log_issue(pci, t, "D");
842 case __BLK_TA_COMPLETE:
843 account_c(t, pci, w, t->bytes);
844 log_complete(pci, t, "C");
847 log_action(pci, t, "P");
849 case __BLK_TA_UNPLUG_IO:
850 account_unplug(t, pci, 0);
851 log_unplug(pci, t, "U");
853 case __BLK_TA_UNPLUG_TIMER:
854 account_unplug(t, pci, 1);
855 log_unplug(pci, t, "UT");
858 log_split(pci, t, "X");
860 case __BLK_TA_BOUNCE:
861 log_generic(pci, t, "B");
864 fprintf(stderr, "Bad fs action %x\n", t->action);
869 static void dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
870 struct per_dev_info *pdi)
872 if (t->action & BLK_TC_ACT(BLK_TC_PC))
873 dump_trace_pc(t, pci);
875 dump_trace_fs(t, pci);
880 static void dump_io_stats(struct io_stats *ios, char *msg)
882 fprintf(ofp, "%s\n", msg);
884 fprintf(ofp, " Reads Queued: %'8lu, %'8LuKiB\t", ios->qreads, ios->qread_kb);
885 fprintf(ofp, " Writes Queued: %'8lu, %'8LuKiB\n", ios->qwrites,ios->qwrite_kb);
887 fprintf(ofp, " Read Dispatches: %'8lu, %'8LuKiB\t", ios->ireads, ios->iread_kb);
888 fprintf(ofp, " Write Dispatches: %'8lu, %'8LuKiB\n", ios->iwrites,ios->iwrite_kb);
889 fprintf(ofp, " Reads Completed: %'8lu, %'8LuKiB\t", ios->creads, ios->cread_kb);
890 fprintf(ofp, " Writes Completed: %'8lu, %'8LuKiB\n", ios->cwrites,ios->cwrite_kb);
891 fprintf(ofp, " Read Merges: %'8lu%8c\t", ios->mreads, ' ');
892 fprintf(ofp, " Write Merges: %'8lu\n", ios->mwrites);
893 fprintf(ofp, " IO unplugs: %'8lu%8c\t", ios->io_unplugs, ' ');
894 fprintf(ofp, " Timer unplugs: %'8lu\n", ios->timer_unplugs);
897 static void dump_wait_stats(struct per_process_info *ppi)
899 unsigned long rawait = ppi->longest_allocation_wait[0] / 1000;
900 unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000;
901 unsigned long rcwait = ppi->longest_completion_wait[0] / 1000;
902 unsigned long wawait = ppi->longest_allocation_wait[1] / 1000;
903 unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000;
904 unsigned long wcwait = ppi->longest_completion_wait[1] / 1000;
906 fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' ');
907 fprintf(ofp, " Allocation wait: %'8lu\n", wawait);
908 fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' ');
909 fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait);
910 fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' ');
911 fprintf(ofp, " Completion wait: %'8lu\n", wcwait);
914 static int ppi_name_compare(const void *p1, const void *p2)
916 struct per_process_info *ppi1 = *((struct per_process_info **) p1);
917 struct per_process_info *ppi2 = *((struct per_process_info **) p2);
920 res = strverscmp(ppi1->name, ppi2->name);
922 res = ppi1->pid > ppi2->pid;
927 static void sort_process_list(void)
929 struct per_process_info **ppis;
930 struct per_process_info *ppi;
933 ppis = malloc(ppi_list_entries * sizeof(struct per_process_info *));
938 ppi = ppi->list_next;
941 qsort(ppis, ppi_list_entries, sizeof(ppi), ppi_name_compare);
943 i = ppi_list_entries - 1;
948 ppi->list_next = ppi_list;
956 static void show_process_stats(void)
958 struct per_process_info *ppi;
966 snprintf(name, sizeof(name)-1, "%s (%u)", ppi->name, ppi->pid);
967 dump_io_stats(&ppi->io_stats, name);
968 dump_wait_stats(ppi);
969 ppi = ppi->list_next;
975 static void show_device_and_cpu_stats(void)
977 struct per_dev_info *pdi;
978 struct per_cpu_info *pci;
979 struct io_stats total, *ios;
980 int i, j, pci_events;
981 char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3];
984 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
986 memset(&total, 0, sizeof(total));
992 for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) {
996 ios = &pci->io_stats;
997 total.qreads += ios->qreads;
998 total.qwrites += ios->qwrites;
999 total.creads += ios->creads;
1000 total.cwrites += ios->cwrites;
1001 total.mreads += ios->mreads;
1002 total.mwrites += ios->mwrites;
1003 total.ireads += ios->ireads;
1004 total.iwrites += ios->iwrites;
1005 total.qread_kb += ios->qread_kb;
1006 total.qwrite_kb += ios->qwrite_kb;
1007 total.cread_kb += ios->cread_kb;
1008 total.cwrite_kb += ios->cwrite_kb;
1009 total.iread_kb += ios->iread_kb;
1010 total.iwrite_kb += ios->iwrite_kb;
1011 total.timer_unplugs += ios->timer_unplugs;
1012 total.io_unplugs += ios->io_unplugs;
1014 snprintf(line, sizeof(line) - 1, "CPU%d (%s):",
1015 j, get_dev_name(pdi, name, sizeof(name)));
1016 dump_io_stats(ios, line);
1020 if (pci_events > 1) {
1022 snprintf(line, sizeof(line) - 1, "Total (%s):",
1023 get_dev_name(pdi, name, sizeof(name)));
1024 dump_io_stats(&total, line);
1027 fprintf(ofp, "\nEvents (%s): %'Lu entries, %'lu skips\n",
1028 get_dev_name(pdi, line, sizeof(line)), pdi->events,
1033 static int sort_entries(void)
1038 while ((t = trace_list) != NULL) {
1039 trace_list = t->next;
1041 if (verify_trace(t->bit))
1043 if (trace_rb_insert(t))
1052 * struct trace and blktrace allocation cache, we do potentially
1053 * millions of mallocs for these structures while only using at most
1054 * a few thousand at the time
1056 static inline void t_free(struct trace *t)
1058 t->next = t_alloc_list;
1062 static inline struct trace *t_alloc(void)
1064 struct trace *t = t_alloc_list;
1067 t_alloc_list = t->next;
1071 return malloc(sizeof(*t));
1074 static inline void bit_free(struct blk_io_trace *bit)
1077 * abuse a 64-bit field for a next pointer for the free item
1079 bit->time = (__u64) (unsigned long) bit_alloc_list;
1080 bit_alloc_list = (struct blk_io_trace *) bit;
1083 static inline struct blk_io_trace *bit_alloc(void)
1085 struct blk_io_trace *bit = bit_alloc_list;
1088 bit_alloc_list = (struct blk_io_trace *) (unsigned long) \
1093 return malloc(sizeof(*bit));
1096 static void show_entries_rb(int force)
1098 struct per_dev_info *pdi = NULL;
1099 struct per_cpu_info *pci = NULL;
1100 struct blk_io_trace *bit;
1104 while ((n = rb_first(&rb_sort_root)) != NULL) {
1109 t = rb_entry(n, struct trace, rb_node);
1112 if (!pdi || pdi->id != bit->device)
1113 pdi = get_dev_info(bit->device);
1116 fprintf(stderr, "Unknown device ID? (%d,%d)\n",
1117 MAJOR(bit->device), MINOR(bit->device));
1121 if (bit->cpu > pdi->ncpus) {
1122 fprintf(stderr, "Unknown CPU ID? (%d, device %d,%d)\n",
1123 bit->cpu, MAJOR(bit->device),
1124 MINOR(bit->device));
1129 * back off displaying more info if we are out of sync
1130 * on SMP systems. to prevent stalling on lost events,
1131 * only allow an event to skip us a few times
1133 if (bit->sequence != (pdi->last_sequence + 1)
1134 && pdi->last_sequence != -1 && !force) {
1135 if (t->skipped < 5) {
1139 fprintf(stderr, "skipping from %lu to %u\n", pdi->last_sequence, bit->sequence);
1144 if (bit->time >= stopwatch_end)
1147 if (!force && bit->time > last_allowed_time)
1150 pdi->last_sequence = bit->sequence;
1152 if (bit->time >= stopwatch_start) {
1153 check_time(pdi, bit);
1155 if (!pci || pci->cpu != bit->cpu)
1156 pci = get_cpu_info(pdi, bit->cpu);
1158 dump_trace(bit, pci, pdi);
1161 rb_erase(&t->rb_node, &rb_sort_root);
1168 static int read_data(int fd, void *buffer, int bytes, int block)
1170 int ret, bytes_left, fl;
1173 fl = fcntl(fd, F_GETFL);
1176 fcntl(fd, F_SETFL, fl | O_NONBLOCK);
1178 fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
1182 while (bytes_left > 0) {
1183 ret = read(fd, p, bytes_left);
1187 if (errno != EAGAIN)
1200 static int read_events(int fd, int always_block)
1202 struct per_dev_info *pdi = NULL;
1205 while (!is_done() && events < rb_batch) {
1206 struct blk_io_trace *bit;
1213 if (read_data(fd, bit, sizeof(*bit), !events || always_block))
1216 magic = be32_to_cpu(bit->magic);
1217 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
1218 fprintf(stderr, "Bad magic %x\n", magic);
1222 pdu_len = be16_to_cpu(bit->pdu_len);
1224 void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
1226 if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1))
1233 memset(t, 0, sizeof(*t));
1237 t->next = trace_list;
1240 if (genesis_time == 0 || t->bit->time < genesis_time)
1241 genesis_time = t->bit->time;
1243 bit->time -= genesis_time;
1245 if (!pdi || pdi->id != bit->device)
1246 pdi = get_dev_info(bit->device);
1248 if (bit->time > pdi->last_read_time)
1249 pdi->last_read_time = bit->time;
1257 static int do_file(void)
1259 struct per_cpu_info *pci;
1260 struct per_dev_info *pdi;
1261 int i, j, events, events_added;
1264 * first prepare all files for reading
1266 for (i = 0; i < ndevices; i++) {
1269 pdi->last_sequence = -1;
1274 pci = get_cpu_info(pdi, j);
1278 snprintf(pci->fname, sizeof(pci->fname)-1,
1279 "%s.blktrace.%d", pdi->name, pci->cpu);
1280 if (stat(pci->fname, &st) < 0)
1283 pci->fd = open(pci->fname, O_RDONLY);
1290 printf("Input file %s added\n", pci->fname);
1296 * now loop over the files reading in the data
1300 last_allowed_time = -1ULL;
1302 for (i = 0; i < ndevices; i++) {
1305 for (j = 0; j < pdi->nfiles; j++) {
1307 pci = get_cpu_info(pdi, j);
1312 events = read_events(pci->fd, 1);
1319 if (pdi->last_read_time < last_allowed_time)
1320 last_allowed_time = pdi->last_read_time;
1322 events_added += events;
1326 if (sort_entries() == -1)
1331 } while (events_added);
1333 if (rb_sort_entries)
1339 static int do_stdin(void)
1343 last_allowed_time = -1ULL;
1344 fd = dup(STDIN_FILENO);
1348 events = read_events(fd, 0);
1352 if (sort_entries() == -1)
1358 if (rb_sort_entries)
1365 static void flush_output(void)
1370 static void handle_sigint(int sig)
1377 * Extract start and duration times from a string, allowing
1378 * us to specify a time interval of interest within a trace.
1379 * Format: "duration" (start is zero) or "start:duration".
1381 static int find_stopwatch_interval(char *string)
1386 value = strtod(string, &sp);
1388 fprintf(stderr,"Invalid stopwatch timer: %s\n", string);
1392 stopwatch_start = DOUBLE_TO_NANO_ULL(value);
1394 value = strtod(string, &sp);
1395 if (sp == string || *sp != '\0') {
1396 fprintf(stderr,"Invalid stopwatch duration time: %s\n",
1400 } else if (*sp != '\0') {
1401 fprintf(stderr,"Invalid stopwatch start timer: %s\n", string);
1404 stopwatch_end = DOUBLE_TO_NANO_ULL(value);
1405 if (stopwatch_end <= stopwatch_start) {
1406 fprintf(stderr, "Invalid stopwatch interval: %Lu -> %Lu\n",
1407 stopwatch_start, stopwatch_end);
1414 static char usage_str[] = \
1415 "[ -i <input name> ] [-o <output name> [ -s ] [ -t ] [ -q ]\n" \
1416 "[ -w start:stop ] [ -f output format ] [ -F format spec ] [ -v] \n\n" \
1417 "\t-i Input file containing trace data, or '-' for stdin\n" \
1418 "\t-o Output file. If not given, output is stdout\n" \
1419 "\t-b stdin read batching\n" \
1420 "\t-s Show per-program io statistics\n" \
1421 "\t-t Track individual ios. Will tell you the time a request took\n" \
1422 "\t to get queued, to get dispatched, and to get completed\n" \
1423 "\t-q Quiet. Don't display any stats at the end of the trace\n" \
1424 "\t-w Only parse data between the given time interval in seconds.\n" \
1425 "\t If 'start' isn't given, blkparse defaults the start time to 0\n" \
1426 "\t -f Output format. Customize the output format. The format field\n" \
1427 "\t identifies can be found in the documentation\n" \
1428 "\t-F Format specification. Can be found in the documentation\n" \
1429 "\t-v Print program version info\n\n";
1431 static void usage(char *prog)
1433 fprintf(stderr, "Usage: %s %s %s", prog, blkparse_version, usage_str);
1436 int main(int argc, char *argv[])
1440 int per_device_and_cpu_stats = 1;
1442 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
1445 if (!strcmp(optarg, "-") && !pipeline)
1447 else if (resize_devices(optarg) != 0)
1451 output_name = optarg;
1454 rb_batch = atoi(optarg);
1456 rb_batch = RB_BATCH_DEFAULT;
1459 per_process_stats = 1;
1465 per_device_and_cpu_stats = 0;
1468 if (find_stopwatch_interval(optarg) != 0)
1472 set_all_format_specs(optarg);
1475 if (add_format_spec(optarg) != 0)
1479 printf("%s version %s\n", argv[0], blkparse_version);
1487 while (optind < argc) {
1488 if (!strcmp(argv[optind], "-") && !pipeline)
1490 else if (resize_devices(argv[optind]) != 0)
1495 if (!pipeline && !ndevices) {
1500 memset(&rb_sort_root, 0, sizeof(rb_sort_root));
1501 memset(&rb_track_root, 0, sizeof(rb_track_root));
1503 signal(SIGINT, handle_sigint);
1504 signal(SIGHUP, handle_sigint);
1505 signal(SIGTERM, handle_sigint);
1507 setlocale(LC_NUMERIC, "en_US");
1510 ofp = fdopen(STDOUT_FILENO, "w");
1515 snprintf(ofname, sizeof(ofname) - 1, "%s", output_name);
1516 ofp = fopen(ofname, "w");
1525 ofp_buffer = malloc(4096);
1526 if (setvbuf(ofp, ofp_buffer, mode, 4096)) {
1536 if (per_process_stats)
1537 show_process_stats();
1539 if (per_device_and_cpu_stats)
1540 show_device_and_cpu_stats();