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 struct trace *trace_rb_find(dev_t device, unsigned long sequence)
283 struct rb_node **p = &rb_sort_root.rb_node;
284 struct rb_node *parent = NULL;
289 __t = rb_entry(parent, struct trace, rb_node);
291 if (device < __t->bit->device)
293 else if (device > __t->bit->device)
295 else if (sequence < __t->bit->sequence)
297 else if (sequence > __t->bit->sequence)
306 static inline int track_rb_insert(struct io_track *iot)
308 struct rb_node **p = &rb_track_root.rb_node;
309 struct rb_node *parent = NULL;
310 struct io_track *__iot;
315 __iot = rb_entry(parent, struct io_track, rb_node);
317 if (iot->device < __iot->device)
319 else if (iot->device > __iot->device)
321 else if (iot->sector < __iot->sector)
323 else if (iot->sector > __iot->sector)
327 "sector alias (%Lu) on device %d,%d!\n",
328 (unsigned long long) iot->sector,
329 MAJOR(iot->device), MINOR(iot->device));
334 rb_link_node(&iot->rb_node, parent, p);
335 rb_insert_color(&iot->rb_node, &rb_track_root);
339 static struct io_track *__find_track(dev_t device, __u64 sector)
341 struct rb_node **p = &rb_track_root.rb_node;
342 struct rb_node *parent = NULL;
343 struct io_track *__iot;
348 __iot = rb_entry(parent, struct io_track, rb_node);
350 if (device < __iot->device)
352 else if (device > __iot->device)
354 else if (sector < __iot->sector)
356 else if (sector > __iot->sector)
365 static struct io_track *find_track(__u32 pid, dev_t device, __u64 sector)
367 struct io_track *iot;
369 iot = __find_track(device, sector);
371 iot = malloc(sizeof(*iot));
373 iot->device = device;
374 iot->sector = sector;
375 track_rb_insert(iot);
381 static void log_track_frontmerge(struct blk_io_trace *t)
383 struct io_track *iot;
388 iot = __find_track(t->device, t->sector + (t->bytes >> 9));
390 fprintf(stderr, "failed to find mergeable event\n");
394 rb_erase(&iot->rb_node, &rb_track_root);
395 iot->sector -= t->bytes >> 9;
396 track_rb_insert(iot);
399 static void log_track_getrq(struct blk_io_trace *t)
401 struct io_track *iot;
406 iot = find_track(t->pid, t->device, t->sector);
407 iot->allocation_time = t->time;
412 * return time between rq allocation and insertion
414 static unsigned long long log_track_insert(struct blk_io_trace *t)
416 unsigned long long elapsed;
417 struct io_track *iot;
422 iot = find_track(t->pid, t->device, t->sector);
423 iot->queue_time = t->time;
424 elapsed = iot->queue_time - iot->allocation_time;
426 if (per_process_stats) {
427 struct per_process_info *ppi = find_process_by_pid(iot->pid);
428 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
430 if (ppi && elapsed > ppi->longest_allocation_wait[w])
431 ppi->longest_allocation_wait[w] = elapsed;
438 * return time between queue and issue
440 static unsigned long long log_track_issue(struct blk_io_trace *t)
442 unsigned long long elapsed;
443 struct io_track *iot;
447 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
450 iot = __find_track(t->device, t->sector);
452 fprintf(stderr, "failed to find issue event\n");
456 iot->dispatch_time = t->time;
457 elapsed = iot->dispatch_time - iot->queue_time;
459 if (per_process_stats) {
460 struct per_process_info *ppi = find_process_by_pid(iot->pid);
461 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
463 if (ppi && elapsed > ppi->longest_dispatch_wait[w])
464 ppi->longest_dispatch_wait[w] = elapsed;
471 * return time between dispatch and complete
473 static unsigned long long log_track_complete(struct blk_io_trace *t)
475 unsigned long long elapsed;
476 struct io_track *iot;
480 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
483 iot = __find_track(t->device, t->sector);
485 fprintf(stderr, "failed to find complete event\n");
489 iot->completion_time = t->time;
490 elapsed = iot->completion_time - iot->dispatch_time;
492 if (per_process_stats) {
493 struct per_process_info *ppi = find_process_by_pid(iot->pid);
494 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
496 if (ppi && elapsed > ppi->longest_completion_wait[w])
497 ppi->longest_completion_wait[w] = elapsed;
501 * kill the trace, we don't need it after completion
503 rb_erase(&iot->rb_node, &rb_track_root);
510 static struct io_stats *find_process_io_stats(__u32 pid, char *name)
512 struct per_process_info *ppi = find_process_by_pid(pid);
515 ppi = malloc(sizeof(*ppi));
516 memset(ppi, 0, sizeof(*ppi));
517 strncpy(ppi->name, name, sizeof(ppi->name));
519 add_process_to_hash(ppi);
520 add_process_to_list(ppi);
523 return &ppi->io_stats;
527 static void resize_cpu_info(struct per_dev_info *pdi, int cpu)
529 struct per_cpu_info *cpus = pdi->cpus;
530 int ncpus = pdi->ncpus;
531 int new_count = cpu + 1;
535 size = new_count * sizeof(struct per_cpu_info);
536 cpus = realloc(cpus, size);
539 fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n",
540 get_dev_name(pdi, name, sizeof(name)), size);
544 new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info));
545 new_space = (new_count - ncpus) * sizeof(struct per_cpu_info);
546 memset(new_start, 0, new_space);
548 pdi->ncpus = new_count;
552 static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu)
554 struct per_cpu_info *pci;
556 if (cpu >= pdi->ncpus)
557 resize_cpu_info(pdi, cpu);
559 pci = &pdi->cpus[cpu];
565 static int resize_devices(char *name)
567 int size = (ndevices + 1) * sizeof(struct per_dev_info);
569 devices = realloc(devices, size);
571 fprintf(stderr, "Out of memory, device %s (%d)\n", name, size);
574 memset(&devices[ndevices], 0, sizeof(struct per_dev_info));
575 devices[ndevices].name = name;
580 static struct per_dev_info *get_dev_info(dev_t id)
582 struct per_dev_info *pdi;
585 for (i = 0; i < ndevices; i++) {
588 if (devices[i].id == id)
592 if (resize_devices(NULL) != 0)
595 pdi = &devices[ndevices - 1];
597 pdi->last_sequence = 0;
598 pdi->last_read_time = 0;
602 static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size)
605 snprintf(buffer, size, "%s", pdi->name);
607 snprintf(buffer, size, "%d,%d", MAJOR(pdi->id), MINOR(pdi->id));
611 static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit)
613 unsigned long long this = bit->time;
614 unsigned long long last = pdi->last_reported_time;
616 pdi->backwards = (this < last) ? 'B' : ' ';
617 pdi->last_reported_time = this;
620 static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t,
625 ios->qwrite_kb += t->bytes >> 10;
628 ios->qread_kb += t->bytes >> 10;
632 static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci,
635 __account_m(&pci->io_stats, t, rw);
637 if (per_process_stats) {
638 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
640 __account_m(ios, t, rw);
644 static inline void __account_queue(struct io_stats *ios, struct blk_io_trace *t,
649 ios->qwrite_kb += t->bytes >> 10;
652 ios->qread_kb += t->bytes >> 10;
656 static inline void account_queue(struct blk_io_trace *t,
657 struct per_cpu_info *pci, int rw)
659 __account_queue(&pci->io_stats, t, rw);
661 if (per_process_stats) {
662 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
664 __account_queue(ios, t, rw);
668 static inline void __account_c(struct io_stats *ios, int rw, unsigned int bytes)
672 ios->cwrite_kb += bytes >> 10;
675 ios->cread_kb += bytes >> 10;
679 static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci,
682 __account_c(&pci->io_stats, rw, bytes);
684 if (per_process_stats) {
685 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
687 __account_c(ios, rw, bytes);
691 static inline void __account_issue(struct io_stats *ios, int rw,
696 ios->iwrite_kb += bytes >> 10;
699 ios->iread_kb += bytes >> 10;
703 static inline void account_issue(struct blk_io_trace *t,
704 struct per_cpu_info *pci, int rw)
706 __account_issue(&pci->io_stats, rw, t->bytes);
708 if (per_process_stats) {
709 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
711 __account_issue(ios, rw, t->bytes);
715 static inline void __account_unplug(struct io_stats *ios, int timer)
718 ios->timer_unplugs++;
723 static inline void account_unplug(struct blk_io_trace *t,
724 struct per_cpu_info *pci, int timer)
726 __account_unplug(&pci->io_stats, timer);
728 if (per_process_stats) {
729 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
731 __account_unplug(ios, timer);
735 static void log_complete(struct per_cpu_info *pci, struct blk_io_trace *t,
738 process_fmt(act, pci, t, log_track_complete(t), 0, NULL);
741 static void log_insert(struct per_cpu_info *pci, struct blk_io_trace *t,
744 process_fmt(act, pci, t, log_track_insert(t), 0, NULL);
747 static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t,
750 process_fmt(act, pci, t, -1, 0, NULL);
753 static void log_issue(struct per_cpu_info *pci, struct blk_io_trace *t,
756 process_fmt(act, pci, t, log_track_issue(t), 0, NULL);
759 static void log_merge(struct per_cpu_info *pci, struct blk_io_trace *t,
763 log_track_frontmerge(t);
765 process_fmt(act, pci, t, -1ULL, 0, NULL);
768 static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t,
771 process_fmt(act, pci, t, -1ULL, 0, NULL);
774 static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t,
777 process_fmt(act, pci, t, -1ULL, 0, NULL);
780 static void log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t,
783 process_fmt(act, pci, t, -1ULL, 0, NULL);
786 static void log_split(struct per_cpu_info *pci, struct blk_io_trace *t,
789 process_fmt(act, pci, t, -1ULL, 0, NULL);
792 static void log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act)
794 unsigned char *buf = (unsigned char *) t + sizeof(*t);
796 process_fmt(act, pci, t, -1ULL, t->pdu_len, buf);
799 static void dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci)
801 int act = t->action & 0xffff;
805 log_generic(pci, t, "Q");
808 log_generic(pci, t, "G");
810 case __BLK_TA_SLEEPRQ:
811 log_generic(pci, t, "S");
813 case __BLK_TA_REQUEUE:
814 log_generic(pci, t, "R");
819 case __BLK_TA_COMPLETE:
822 case __BLK_TA_INSERT:
826 fprintf(stderr, "Bad pc action %x\n", act);
831 static void dump_trace_fs(struct blk_io_trace *t, struct per_cpu_info *pci)
833 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
834 int act = t->action & 0xffff;
838 account_queue(t, pci, w);
839 log_queue(pci, t, "Q");
841 case __BLK_TA_INSERT:
842 log_insert(pci, t, "I");
844 case __BLK_TA_BACKMERGE:
845 account_m(t, pci, w);
846 log_merge(pci, t, "M");
848 case __BLK_TA_FRONTMERGE:
849 account_m(t, pci, w);
850 log_merge(pci, t, "F");
854 log_generic(pci, t, "G");
856 case __BLK_TA_SLEEPRQ:
857 log_generic(pci, t, "S");
859 case __BLK_TA_REQUEUE:
860 account_c(t, pci, w, -t->bytes);
861 log_queue(pci, t, "R");
864 account_issue(t, pci, w);
865 log_issue(pci, t, "D");
867 case __BLK_TA_COMPLETE:
868 account_c(t, pci, w, t->bytes);
869 log_complete(pci, t, "C");
872 log_action(pci, t, "P");
874 case __BLK_TA_UNPLUG_IO:
875 account_unplug(t, pci, 0);
876 log_unplug(pci, t, "U");
878 case __BLK_TA_UNPLUG_TIMER:
879 account_unplug(t, pci, 1);
880 log_unplug(pci, t, "UT");
883 log_split(pci, t, "X");
885 case __BLK_TA_BOUNCE:
886 log_generic(pci, t, "B");
889 fprintf(stderr, "Bad fs action %x\n", t->action);
894 static void dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
895 struct per_dev_info *pdi)
897 if (t->action & BLK_TC_ACT(BLK_TC_PC))
898 dump_trace_pc(t, pci);
900 dump_trace_fs(t, pci);
905 static void dump_io_stats(struct io_stats *ios, char *msg)
907 fprintf(ofp, "%s\n", msg);
909 fprintf(ofp, " Reads Queued: %'8lu, %'8LuKiB\t", ios->qreads, ios->qread_kb);
910 fprintf(ofp, " Writes Queued: %'8lu, %'8LuKiB\n", ios->qwrites,ios->qwrite_kb);
912 fprintf(ofp, " Read Dispatches: %'8lu, %'8LuKiB\t", ios->ireads, ios->iread_kb);
913 fprintf(ofp, " Write Dispatches: %'8lu, %'8LuKiB\n", ios->iwrites,ios->iwrite_kb);
914 fprintf(ofp, " Reads Completed: %'8lu, %'8LuKiB\t", ios->creads, ios->cread_kb);
915 fprintf(ofp, " Writes Completed: %'8lu, %'8LuKiB\n", ios->cwrites,ios->cwrite_kb);
916 fprintf(ofp, " Read Merges: %'8lu%8c\t", ios->mreads, ' ');
917 fprintf(ofp, " Write Merges: %'8lu\n", ios->mwrites);
918 fprintf(ofp, " IO unplugs: %'8lu%8c\t", ios->io_unplugs, ' ');
919 fprintf(ofp, " Timer unplugs: %'8lu\n", ios->timer_unplugs);
922 static void dump_wait_stats(struct per_process_info *ppi)
924 unsigned long rawait = ppi->longest_allocation_wait[0] / 1000;
925 unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000;
926 unsigned long rcwait = ppi->longest_completion_wait[0] / 1000;
927 unsigned long wawait = ppi->longest_allocation_wait[1] / 1000;
928 unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000;
929 unsigned long wcwait = ppi->longest_completion_wait[1] / 1000;
931 fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' ');
932 fprintf(ofp, " Allocation wait: %'8lu\n", wawait);
933 fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' ');
934 fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait);
935 fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' ');
936 fprintf(ofp, " Completion wait: %'8lu\n", wcwait);
939 static int ppi_name_compare(const void *p1, const void *p2)
941 struct per_process_info *ppi1 = *((struct per_process_info **) p1);
942 struct per_process_info *ppi2 = *((struct per_process_info **) p2);
945 res = strverscmp(ppi1->name, ppi2->name);
947 res = ppi1->pid > ppi2->pid;
952 static void sort_process_list(void)
954 struct per_process_info **ppis;
955 struct per_process_info *ppi;
958 ppis = malloc(ppi_list_entries * sizeof(struct per_process_info *));
963 ppi = ppi->list_next;
966 qsort(ppis, ppi_list_entries, sizeof(ppi), ppi_name_compare);
968 i = ppi_list_entries - 1;
973 ppi->list_next = ppi_list;
981 static void show_process_stats(void)
983 struct per_process_info *ppi;
991 snprintf(name, sizeof(name)-1, "%s (%u)", ppi->name, ppi->pid);
992 dump_io_stats(&ppi->io_stats, name);
993 dump_wait_stats(ppi);
994 ppi = ppi->list_next;
1000 static void show_device_and_cpu_stats(void)
1002 struct per_dev_info *pdi;
1003 struct per_cpu_info *pci;
1004 struct io_stats total, *ios;
1005 int i, j, pci_events;
1006 char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3];
1009 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
1011 memset(&total, 0, sizeof(total));
1017 for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) {
1021 ios = &pci->io_stats;
1022 total.qreads += ios->qreads;
1023 total.qwrites += ios->qwrites;
1024 total.creads += ios->creads;
1025 total.cwrites += ios->cwrites;
1026 total.mreads += ios->mreads;
1027 total.mwrites += ios->mwrites;
1028 total.ireads += ios->ireads;
1029 total.iwrites += ios->iwrites;
1030 total.qread_kb += ios->qread_kb;
1031 total.qwrite_kb += ios->qwrite_kb;
1032 total.cread_kb += ios->cread_kb;
1033 total.cwrite_kb += ios->cwrite_kb;
1034 total.iread_kb += ios->iread_kb;
1035 total.iwrite_kb += ios->iwrite_kb;
1036 total.timer_unplugs += ios->timer_unplugs;
1037 total.io_unplugs += ios->io_unplugs;
1039 snprintf(line, sizeof(line) - 1, "CPU%d (%s):",
1040 j, get_dev_name(pdi, name, sizeof(name)));
1041 dump_io_stats(ios, line);
1045 if (pci_events > 1) {
1047 snprintf(line, sizeof(line) - 1, "Total (%s):",
1048 get_dev_name(pdi, name, sizeof(name)));
1049 dump_io_stats(&total, line);
1052 fprintf(ofp, "\nEvents (%s): %'Lu entries, %'lu skips\n",
1053 get_dev_name(pdi, line, sizeof(line)), pdi->events,
1058 static void find_genesis(void)
1060 struct trace *t = trace_list;
1062 genesis_time = -1ULL;
1064 if (t->bit->time < genesis_time)
1065 genesis_time = t->bit->time;
1071 static int sort_entries(void)
1079 while ((t = trace_list) != NULL) {
1080 trace_list = t->next;
1082 if (verify_trace(t->bit))
1085 t->bit->time -= genesis_time;
1087 if (trace_rb_insert(t))
1096 * struct trace and blktrace allocation cache, we do potentially
1097 * millions of mallocs for these structures while only using at most
1098 * a few thousand at the time
1100 static inline void t_free(struct trace *t)
1102 t->next = t_alloc_list;
1106 static inline struct trace *t_alloc(void)
1108 struct trace *t = t_alloc_list;
1111 t_alloc_list = t->next;
1115 return malloc(sizeof(*t));
1118 static inline void bit_free(struct blk_io_trace *bit)
1121 * abuse a 64-bit field for a next pointer for the free item
1123 bit->time = (__u64) (unsigned long) bit_alloc_list;
1124 bit_alloc_list = (struct blk_io_trace *) bit;
1127 static inline struct blk_io_trace *bit_alloc(void)
1129 struct blk_io_trace *bit = bit_alloc_list;
1132 bit_alloc_list = (struct blk_io_trace *) (unsigned long) \
1137 return malloc(sizeof(*bit));
1140 static void show_entries_rb(int force)
1142 struct per_dev_info *pdi = NULL;
1143 struct per_cpu_info *pci = NULL;
1144 struct blk_io_trace *bit;
1148 while ((n = rb_first(&rb_sort_root)) != NULL) {
1153 t = rb_entry(n, struct trace, rb_node);
1156 if (!pdi || pdi->id != bit->device)
1157 pdi = get_dev_info(bit->device);
1160 fprintf(stderr, "Unknown device ID? (%d,%d)\n",
1161 MAJOR(bit->device), MINOR(bit->device));
1165 if (bit->cpu > pdi->ncpus) {
1166 fprintf(stderr, "Unknown CPU ID? (%d, device %d,%d)\n",
1167 bit->cpu, MAJOR(bit->device),
1168 MINOR(bit->device));
1173 * back off displaying more info if we are out of sync
1174 * on SMP systems. to prevent stalling on lost events,
1175 * only allow an event to skip us a few times
1177 if (bit->sequence > (pdi->last_sequence + 1) && !force) {
1181 * the wanted sequence is really there, continue
1182 * because this means that the log time is earlier
1183 * on the trace we have now
1185 __t = trace_rb_find(pdi->id, pdi->last_sequence + 1);
1189 if (t->skipped < 5) {
1197 if (bit->time >= stopwatch_end)
1200 if (!force && bit->time > last_allowed_time)
1203 pdi->last_sequence = bit->sequence;
1205 if (bit->time >= stopwatch_start) {
1206 check_time(pdi, bit);
1208 if (!pci || pci->cpu != bit->cpu)
1209 pci = get_cpu_info(pdi, bit->cpu);
1211 dump_trace(bit, pci, pdi);
1214 rb_erase(&t->rb_node, &rb_sort_root);
1221 static int read_data(int fd, void *buffer, int bytes, int block)
1223 int ret, bytes_left, fl;
1226 fl = fcntl(fd, F_GETFL);
1229 fcntl(fd, F_SETFL, fl | O_NONBLOCK);
1231 fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
1235 while (bytes_left > 0) {
1236 ret = read(fd, p, bytes_left);
1240 if (errno != EAGAIN)
1253 static int read_events(int fd, int always_block)
1255 struct per_dev_info *pdi = NULL;
1258 while (!is_done() && events < rb_batch) {
1259 struct blk_io_trace *bit;
1266 if (read_data(fd, bit, sizeof(*bit), !events || always_block))
1269 magic = be32_to_cpu(bit->magic);
1270 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
1271 fprintf(stderr, "Bad magic %x\n", magic);
1275 pdu_len = be16_to_cpu(bit->pdu_len);
1277 void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
1279 if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1))
1286 memset(t, 0, sizeof(*t));
1290 t->next = trace_list;
1293 if (!pdi || pdi->id != bit->device)
1294 pdi = get_dev_info(bit->device);
1296 if (bit->time > pdi->last_read_time)
1297 pdi->last_read_time = bit->time;
1305 static int do_file(void)
1307 struct per_cpu_info *pci;
1308 struct per_dev_info *pdi;
1309 int i, j, events, events_added;
1312 * first prepare all files for reading
1314 for (i = 0; i < ndevices; i++) {
1317 pdi->last_sequence = 0;
1322 pci = get_cpu_info(pdi, j);
1326 snprintf(pci->fname, sizeof(pci->fname)-1,
1327 "%s.blktrace.%d", pdi->name, pci->cpu);
1328 if (stat(pci->fname, &st) < 0)
1331 pci->fd = open(pci->fname, O_RDONLY);
1338 printf("Input file %s added\n", pci->fname);
1344 * now loop over the files reading in the data
1348 last_allowed_time = -1ULL;
1350 for (i = 0; i < ndevices; i++) {
1353 for (j = 0; j < pdi->nfiles; j++) {
1355 pci = get_cpu_info(pdi, j);
1360 events = read_events(pci->fd, 1);
1367 if (pdi->last_read_time < last_allowed_time)
1368 last_allowed_time = pdi->last_read_time;
1370 events_added += events;
1374 if (sort_entries() == -1)
1379 } while (events_added);
1381 if (rb_sort_entries)
1387 static int do_stdin(void)
1391 last_allowed_time = -1ULL;
1392 fd = dup(STDIN_FILENO);
1396 events = read_events(fd, 0);
1400 if (sort_entries() == -1)
1406 if (rb_sort_entries)
1413 static void flush_output(void)
1418 static void handle_sigint(int sig)
1425 * Extract start and duration times from a string, allowing
1426 * us to specify a time interval of interest within a trace.
1427 * Format: "duration" (start is zero) or "start:duration".
1429 static int find_stopwatch_interval(char *string)
1434 value = strtod(string, &sp);
1436 fprintf(stderr,"Invalid stopwatch timer: %s\n", string);
1440 stopwatch_start = DOUBLE_TO_NANO_ULL(value);
1442 value = strtod(string, &sp);
1443 if (sp == string || *sp != '\0') {
1444 fprintf(stderr,"Invalid stopwatch duration time: %s\n",
1448 } else if (*sp != '\0') {
1449 fprintf(stderr,"Invalid stopwatch start timer: %s\n", string);
1452 stopwatch_end = DOUBLE_TO_NANO_ULL(value);
1453 if (stopwatch_end <= stopwatch_start) {
1454 fprintf(stderr, "Invalid stopwatch interval: %Lu -> %Lu\n",
1455 stopwatch_start, stopwatch_end);
1462 static char usage_str[] = \
1463 "[ -i <input name> ] [-o <output name> [ -s ] [ -t ] [ -q ]\n" \
1464 "[ -w start:stop ] [ -f output format ] [ -F format spec ] [ -v] \n\n" \
1465 "\t-i Input file containing trace data, or '-' for stdin\n" \
1466 "\t-o Output file. If not given, output is stdout\n" \
1467 "\t-b stdin read batching\n" \
1468 "\t-s Show per-program io statistics\n" \
1469 "\t-t Track individual ios. Will tell you the time a request took\n" \
1470 "\t to get queued, to get dispatched, and to get completed\n" \
1471 "\t-q Quiet. Don't display any stats at the end of the trace\n" \
1472 "\t-w Only parse data between the given time interval in seconds.\n" \
1473 "\t If 'start' isn't given, blkparse defaults the start time to 0\n" \
1474 "\t -f Output format. Customize the output format. The format field\n" \
1475 "\t identifies can be found in the documentation\n" \
1476 "\t-F Format specification. Can be found in the documentation\n" \
1477 "\t-v Print program version info\n\n";
1479 static void usage(char *prog)
1481 fprintf(stderr, "Usage: %s %s %s", prog, blkparse_version, usage_str);
1484 int main(int argc, char *argv[])
1488 int per_device_and_cpu_stats = 1;
1490 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
1493 if (!strcmp(optarg, "-") && !pipeline)
1495 else if (resize_devices(optarg) != 0)
1499 output_name = optarg;
1502 rb_batch = atoi(optarg);
1504 rb_batch = RB_BATCH_DEFAULT;
1507 per_process_stats = 1;
1513 per_device_and_cpu_stats = 0;
1516 if (find_stopwatch_interval(optarg) != 0)
1520 set_all_format_specs(optarg);
1523 if (add_format_spec(optarg) != 0)
1527 printf("%s version %s\n", argv[0], blkparse_version);
1535 while (optind < argc) {
1536 if (!strcmp(argv[optind], "-") && !pipeline)
1538 else if (resize_devices(argv[optind]) != 0)
1543 if (!pipeline && !ndevices) {
1548 memset(&rb_sort_root, 0, sizeof(rb_sort_root));
1549 memset(&rb_track_root, 0, sizeof(rb_track_root));
1551 signal(SIGINT, handle_sigint);
1552 signal(SIGHUP, handle_sigint);
1553 signal(SIGTERM, handle_sigint);
1555 setlocale(LC_NUMERIC, "en_US");
1558 ofp = fdopen(STDOUT_FILENO, "w");
1563 snprintf(ofname, sizeof(ofname) - 1, "%s", output_name);
1564 ofp = fopen(ofname, "w");
1573 ofp_buffer = malloc(4096);
1574 if (setvbuf(ofp, ofp_buffer, mode, 4096)) {
1584 if (per_process_stats)
1585 show_process_stats();
1587 if (per_device_and_cpu_stats)
1588 show_device_and_cpu_stats();