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;
54 struct per_cpu_info *cpus;
57 struct per_process_info {
60 struct io_stats io_stats;
61 struct per_process_info *hash_next, *list_next;
67 unsigned long long longest_allocation_wait[2];
68 unsigned long long longest_dispatch_wait[2];
69 unsigned long long longest_completion_wait[2];
72 #define PPI_HASH_SHIFT (8)
73 #define PPI_HASH_SIZE (1 << PPI_HASH_SHIFT)
74 #define PPI_HASH_MASK (PPI_HASH_SIZE - 1)
75 static struct per_process_info *ppi_hash_table[PPI_HASH_SIZE];
76 static struct per_process_info *ppi_list;
77 static int ppi_list_entries;
79 #define S_OPTS "i:o:b:stqw:f:F:vn"
80 static struct option l_opts[] = {
83 .has_arg = required_argument,
89 .has_arg = required_argument,
95 .has_arg = required_argument,
100 .name = "per program stats",
101 .has_arg = no_argument,
107 .has_arg = no_argument,
113 .has_arg = no_argument,
119 .has_arg = required_argument,
125 .has_arg = required_argument,
130 .name = "format-spec",
131 .has_arg = required_argument,
136 .name = "hash by name",
137 .has_arg = no_argument,
143 .has_arg = no_argument,
150 * for sorting the displayed output
153 struct blk_io_trace *bit;
154 struct rb_node rb_node;
159 static struct rb_root rb_sort_root;
160 static unsigned long rb_sort_entries;
162 static struct rb_root rb_track_root;
164 static struct trace *trace_list;
169 static struct blk_io_trace *bit_alloc_list;
170 static struct trace *t_alloc_list;
173 * for tracking individual ios
176 struct rb_node rb_node;
182 unsigned long long allocation_time;
183 unsigned long long queue_time;
184 unsigned long long dispatch_time;
185 unsigned long long completion_time;
189 static struct per_dev_info *devices;
190 static char *get_dev_name(struct per_dev_info *, char *, int);
193 static char *output_name;
195 static unsigned long long genesis_time;
196 static unsigned long long last_allowed_time;
197 static unsigned long long stopwatch_start; /* start from zero by default */
198 static unsigned long long stopwatch_end = ULONG_LONG_MAX; /* "infinity" */
200 static int per_process_stats;
201 static int track_ios;
202 static int ppi_hash_by_pid = 1;
204 #define RB_BATCH_DEFAULT (512)
205 static int rb_batch = RB_BATCH_DEFAULT;
209 #define is_done() (*(volatile int *)(&done))
210 static volatile int done;
212 #define JHASH_RANDOM (0x3af5f2ee)
214 static inline int ppi_hash_pid(__u32 pid)
216 return jhash_1word(pid, JHASH_RANDOM) & PPI_HASH_MASK;
219 static inline int ppi_hash_name(const char *name)
221 return jhash(name, 16, JHASH_RANDOM) & PPI_HASH_MASK;
224 static inline int ppi_hash(struct per_process_info *ppi)
227 return ppi_hash_pid(ppi->pid);
229 return ppi_hash_name(ppi->name);
232 static inline void add_process_to_hash(struct per_process_info *ppi)
234 const int hash_idx = ppi_hash(ppi);
236 ppi->hash_next = ppi_hash_table[hash_idx];
237 ppi_hash_table[hash_idx] = ppi;
240 static inline void add_process_to_list(struct per_process_info *ppi)
242 ppi->list_next = ppi_list;
247 static struct per_process_info *find_process_by_name(char *name)
249 const int hash_idx = ppi_hash_name(name);
250 struct per_process_info *ppi;
252 ppi = ppi_hash_table[hash_idx];
254 if (!strcmp(ppi->name, name))
257 ppi = ppi->hash_next;
263 static struct per_process_info *find_process_by_pid(__u32 pid)
265 const int hash_idx = ppi_hash_pid(pid);
266 struct per_process_info *ppi;
268 ppi = ppi_hash_table[hash_idx];
273 ppi = ppi->hash_next;
279 static struct per_process_info *find_process(__u32 pid, char *name)
281 struct per_process_info *ppi;
284 return find_process_by_pid(pid);
286 ppi = find_process_by_name(name);
287 if (ppi && ppi->pid != pid)
288 ppi->more_than_one = 1;
293 static inline int trace_rb_insert(struct trace *t)
295 struct rb_node **p = &rb_sort_root.rb_node;
296 struct rb_node *parent = NULL;
301 __t = rb_entry(parent, struct trace, rb_node);
303 if (t->bit->time < __t->bit->time)
305 else if (t->bit->time > __t->bit->time)
307 else if (t->bit->device < __t->bit->device)
309 else if (t->bit->device > __t->bit->device)
311 else if (t->bit->sequence < __t->bit->sequence)
313 else if (t->bit->sequence > __t->bit->sequence)
315 else if (t->bit->device == __t->bit->device) {
317 "sequence alias (%d) on device %d,%d!\n",
319 MAJOR(t->bit->device), MINOR(t->bit->device));
325 rb_link_node(&t->rb_node, parent, p);
326 rb_insert_color(&t->rb_node, &rb_sort_root);
330 static struct trace *trace_rb_find(dev_t device, unsigned long sequence)
332 struct rb_node **p = &rb_sort_root.rb_node;
333 struct rb_node *parent = NULL;
338 __t = rb_entry(parent, struct trace, rb_node);
340 if (device < __t->bit->device)
342 else if (device > __t->bit->device)
344 else if (sequence < __t->bit->sequence)
346 else if (sequence > __t->bit->sequence)
355 static inline int track_rb_insert(struct io_track *iot)
357 struct rb_node **p = &rb_track_root.rb_node;
358 struct rb_node *parent = NULL;
359 struct io_track *__iot;
364 __iot = rb_entry(parent, struct io_track, rb_node);
366 if (iot->device < __iot->device)
368 else if (iot->device > __iot->device)
370 else if (iot->sector < __iot->sector)
372 else if (iot->sector > __iot->sector)
376 "sector alias (%Lu) on device %d,%d!\n",
377 (unsigned long long) iot->sector,
378 MAJOR(iot->device), MINOR(iot->device));
383 rb_link_node(&iot->rb_node, parent, p);
384 rb_insert_color(&iot->rb_node, &rb_track_root);
388 static struct io_track *__find_track(dev_t device, __u64 sector)
390 struct rb_node **p = &rb_track_root.rb_node;
391 struct rb_node *parent = NULL;
392 struct io_track *__iot;
397 __iot = rb_entry(parent, struct io_track, rb_node);
399 if (device < __iot->device)
401 else if (device > __iot->device)
403 else if (sector < __iot->sector)
405 else if (sector > __iot->sector)
414 static struct io_track *find_track(__u32 pid, char *comm, dev_t device,
417 struct io_track *iot;
419 iot = __find_track(device, sector);
421 iot = malloc(sizeof(*iot));
423 memcpy(iot->comm, comm, sizeof(iot->comm));
424 iot->device = device;
425 iot->sector = sector;
426 track_rb_insert(iot);
432 static void log_track_frontmerge(struct blk_io_trace *t)
434 struct io_track *iot;
439 iot = __find_track(t->device, t->sector + (t->bytes >> 9));
441 fprintf(stderr, "failed to find mergeable event\n");
445 rb_erase(&iot->rb_node, &rb_track_root);
446 iot->sector -= t->bytes >> 9;
447 track_rb_insert(iot);
450 static void log_track_getrq(struct blk_io_trace *t)
452 struct io_track *iot;
457 iot = find_track(t->pid, t->comm, t->device, t->sector);
458 iot->allocation_time = t->time;
463 * return time between rq allocation and insertion
465 static unsigned long long log_track_insert(struct blk_io_trace *t)
467 unsigned long long elapsed;
468 struct io_track *iot;
473 iot = find_track(t->pid, t->comm, t->device, t->sector);
474 iot->queue_time = t->time;
475 elapsed = iot->queue_time - iot->allocation_time;
477 if (per_process_stats) {
478 struct per_process_info *ppi = find_process(iot->pid,iot->comm);
479 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
481 if (ppi && elapsed > ppi->longest_allocation_wait[w])
482 ppi->longest_allocation_wait[w] = elapsed;
489 * return time between queue and issue
491 static unsigned long long log_track_issue(struct blk_io_trace *t)
493 unsigned long long elapsed;
494 struct io_track *iot;
498 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
501 iot = __find_track(t->device, t->sector);
503 fprintf(stderr, "failed to find issue event\n");
507 iot->dispatch_time = t->time;
508 elapsed = iot->dispatch_time - iot->queue_time;
510 if (per_process_stats) {
511 struct per_process_info *ppi = find_process(iot->pid,iot->comm);
512 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
514 if (ppi && elapsed > ppi->longest_dispatch_wait[w])
515 ppi->longest_dispatch_wait[w] = elapsed;
522 * return time between dispatch and complete
524 static unsigned long long log_track_complete(struct blk_io_trace *t)
526 unsigned long long elapsed;
527 struct io_track *iot;
531 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
534 iot = __find_track(t->device, t->sector);
536 fprintf(stderr, "failed to find complete event\n");
540 iot->completion_time = t->time;
541 elapsed = iot->completion_time - iot->dispatch_time;
543 if (per_process_stats) {
544 struct per_process_info *ppi = find_process(iot->pid,iot->comm);
545 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
547 if (ppi && elapsed > ppi->longest_completion_wait[w])
548 ppi->longest_completion_wait[w] = elapsed;
552 * kill the trace, we don't need it after completion
554 rb_erase(&iot->rb_node, &rb_track_root);
561 static struct io_stats *find_process_io_stats(__u32 pid, char *name)
563 struct per_process_info *ppi = find_process(pid, name);
566 ppi = malloc(sizeof(*ppi));
567 memset(ppi, 0, sizeof(*ppi));
568 memcpy(ppi->name, name, 16);
570 add_process_to_hash(ppi);
571 add_process_to_list(ppi);
574 return &ppi->io_stats;
577 static void resize_cpu_info(struct per_dev_info *pdi, int cpu)
579 struct per_cpu_info *cpus = pdi->cpus;
580 int ncpus = pdi->ncpus;
581 int new_count = cpu + 1;
585 size = new_count * sizeof(struct per_cpu_info);
586 cpus = realloc(cpus, size);
589 fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n",
590 get_dev_name(pdi, name, sizeof(name)), size);
594 new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info));
595 new_space = (new_count - ncpus) * sizeof(struct per_cpu_info);
596 memset(new_start, 0, new_space);
598 pdi->ncpus = new_count;
602 static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu)
604 struct per_cpu_info *pci;
606 if (cpu >= pdi->ncpus)
607 resize_cpu_info(pdi, cpu);
609 pci = &pdi->cpus[cpu];
615 static int resize_devices(char *name)
617 int size = (ndevices + 1) * sizeof(struct per_dev_info);
619 devices = realloc(devices, size);
621 fprintf(stderr, "Out of memory, device %s (%d)\n", name, size);
624 memset(&devices[ndevices], 0, sizeof(struct per_dev_info));
625 devices[ndevices].name = name;
630 static struct per_dev_info *get_dev_info(dev_t id)
632 struct per_dev_info *pdi;
635 for (i = 0; i < ndevices; i++) {
638 if (devices[i].id == id)
642 if (resize_devices(NULL) != 0)
645 pdi = &devices[ndevices - 1];
647 pdi->last_sequence = 0;
648 pdi->last_read_time = 0;
652 static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size)
655 snprintf(buffer, size, "%s", pdi->name);
657 snprintf(buffer, size, "%d,%d", MAJOR(pdi->id), MINOR(pdi->id));
661 static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit)
663 unsigned long long this = bit->time;
664 unsigned long long last = pdi->last_reported_time;
666 pdi->backwards = (this < last) ? 'B' : ' ';
667 pdi->last_reported_time = this;
670 static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t,
675 ios->qwrite_kb += t->bytes >> 10;
678 ios->qread_kb += t->bytes >> 10;
682 static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci,
685 __account_m(&pci->io_stats, t, rw);
687 if (per_process_stats) {
688 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
690 __account_m(ios, t, rw);
694 static inline void __account_queue(struct io_stats *ios, struct blk_io_trace *t,
699 ios->qwrite_kb += t->bytes >> 10;
702 ios->qread_kb += t->bytes >> 10;
706 static inline void account_queue(struct blk_io_trace *t,
707 struct per_cpu_info *pci, int rw)
709 __account_queue(&pci->io_stats, t, rw);
711 if (per_process_stats) {
712 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
714 __account_queue(ios, t, rw);
718 static inline void __account_c(struct io_stats *ios, int rw, unsigned int bytes)
722 ios->cwrite_kb += bytes >> 10;
725 ios->cread_kb += bytes >> 10;
729 static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci,
732 __account_c(&pci->io_stats, rw, bytes);
734 if (per_process_stats) {
735 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
737 __account_c(ios, rw, bytes);
741 static inline void __account_issue(struct io_stats *ios, int rw,
746 ios->iwrite_kb += bytes >> 10;
749 ios->iread_kb += bytes >> 10;
753 static inline void account_issue(struct blk_io_trace *t,
754 struct per_cpu_info *pci, int rw)
756 __account_issue(&pci->io_stats, rw, t->bytes);
758 if (per_process_stats) {
759 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
761 __account_issue(ios, rw, t->bytes);
765 static inline void __account_unplug(struct io_stats *ios, int timer)
768 ios->timer_unplugs++;
773 static inline void account_unplug(struct blk_io_trace *t,
774 struct per_cpu_info *pci, int timer)
776 __account_unplug(&pci->io_stats, timer);
778 if (per_process_stats) {
779 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
781 __account_unplug(ios, timer);
785 static void log_complete(struct per_cpu_info *pci, struct blk_io_trace *t,
788 process_fmt(act, pci, t, log_track_complete(t), 0, NULL);
791 static void log_insert(struct per_cpu_info *pci, struct blk_io_trace *t,
794 process_fmt(act, pci, t, log_track_insert(t), 0, NULL);
797 static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t,
800 process_fmt(act, pci, t, -1, 0, NULL);
803 static void log_issue(struct per_cpu_info *pci, struct blk_io_trace *t,
806 process_fmt(act, pci, t, log_track_issue(t), 0, NULL);
809 static void log_merge(struct per_cpu_info *pci, struct blk_io_trace *t,
813 log_track_frontmerge(t);
815 process_fmt(act, pci, t, -1ULL, 0, NULL);
818 static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t,
821 process_fmt(act, pci, t, -1ULL, 0, NULL);
824 static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t,
827 process_fmt(act, pci, t, -1ULL, 0, NULL);
830 static void log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t,
833 process_fmt(act, pci, t, -1ULL, 0, NULL);
836 static void log_split(struct per_cpu_info *pci, struct blk_io_trace *t,
839 process_fmt(act, pci, t, -1ULL, 0, NULL);
842 static void log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act)
844 unsigned char *buf = (unsigned char *) t + sizeof(*t);
846 process_fmt(act, pci, t, -1ULL, t->pdu_len, buf);
849 static void dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci)
851 int act = t->action & 0xffff;
855 log_generic(pci, t, "Q");
858 log_generic(pci, t, "G");
860 case __BLK_TA_SLEEPRQ:
861 log_generic(pci, t, "S");
863 case __BLK_TA_REQUEUE:
864 log_generic(pci, t, "R");
869 case __BLK_TA_COMPLETE:
872 case __BLK_TA_INSERT:
876 fprintf(stderr, "Bad pc action %x\n", act);
881 static void dump_trace_fs(struct blk_io_trace *t, struct per_cpu_info *pci)
883 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
884 int act = t->action & 0xffff;
888 account_queue(t, pci, w);
889 log_queue(pci, t, "Q");
891 case __BLK_TA_INSERT:
892 log_insert(pci, t, "I");
894 case __BLK_TA_BACKMERGE:
895 account_m(t, pci, w);
896 log_merge(pci, t, "M");
898 case __BLK_TA_FRONTMERGE:
899 account_m(t, pci, w);
900 log_merge(pci, t, "F");
904 log_generic(pci, t, "G");
906 case __BLK_TA_SLEEPRQ:
907 log_generic(pci, t, "S");
909 case __BLK_TA_REQUEUE:
910 account_c(t, pci, w, -t->bytes);
911 log_queue(pci, t, "R");
914 account_issue(t, pci, w);
915 log_issue(pci, t, "D");
917 case __BLK_TA_COMPLETE:
918 account_c(t, pci, w, t->bytes);
919 log_complete(pci, t, "C");
922 log_action(pci, t, "P");
924 case __BLK_TA_UNPLUG_IO:
925 account_unplug(t, pci, 0);
926 log_unplug(pci, t, "U");
928 case __BLK_TA_UNPLUG_TIMER:
929 account_unplug(t, pci, 1);
930 log_unplug(pci, t, "UT");
933 log_split(pci, t, "X");
935 case __BLK_TA_BOUNCE:
936 log_generic(pci, t, "B");
939 fprintf(stderr, "Bad fs action %x\n", t->action);
944 static void dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
945 struct per_dev_info *pdi)
947 if (t->action & BLK_TC_ACT(BLK_TC_PC))
948 dump_trace_pc(t, pci);
950 dump_trace_fs(t, pci);
955 static void dump_io_stats(struct io_stats *ios, char *msg)
957 fprintf(ofp, "%s\n", msg);
959 fprintf(ofp, " Reads Queued: %'8lu, %'8LuKiB\t", ios->qreads, ios->qread_kb);
960 fprintf(ofp, " Writes Queued: %'8lu, %'8LuKiB\n", ios->qwrites,ios->qwrite_kb);
962 fprintf(ofp, " Read Dispatches: %'8lu, %'8LuKiB\t", ios->ireads, ios->iread_kb);
963 fprintf(ofp, " Write Dispatches: %'8lu, %'8LuKiB\n", ios->iwrites,ios->iwrite_kb);
964 fprintf(ofp, " Reads Completed: %'8lu, %'8LuKiB\t", ios->creads, ios->cread_kb);
965 fprintf(ofp, " Writes Completed: %'8lu, %'8LuKiB\n", ios->cwrites,ios->cwrite_kb);
966 fprintf(ofp, " Read Merges: %'8lu%8c\t", ios->mreads, ' ');
967 fprintf(ofp, " Write Merges: %'8lu\n", ios->mwrites);
968 fprintf(ofp, " IO unplugs: %'8lu%8c\t", ios->io_unplugs, ' ');
969 fprintf(ofp, " Timer unplugs: %'8lu\n", ios->timer_unplugs);
972 static void dump_wait_stats(struct per_process_info *ppi)
974 unsigned long rawait = ppi->longest_allocation_wait[0] / 1000;
975 unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000;
976 unsigned long rcwait = ppi->longest_completion_wait[0] / 1000;
977 unsigned long wawait = ppi->longest_allocation_wait[1] / 1000;
978 unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000;
979 unsigned long wcwait = ppi->longest_completion_wait[1] / 1000;
981 fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' ');
982 fprintf(ofp, " Allocation wait: %'8lu\n", wawait);
983 fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' ');
984 fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait);
985 fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' ');
986 fprintf(ofp, " Completion wait: %'8lu\n", wcwait);
989 static int ppi_name_compare(const void *p1, const void *p2)
991 struct per_process_info *ppi1 = *((struct per_process_info **) p1);
992 struct per_process_info *ppi2 = *((struct per_process_info **) p2);
995 res = strverscmp(ppi1->name, ppi2->name);
997 res = ppi1->pid > ppi2->pid;
1002 static void sort_process_list(void)
1004 struct per_process_info **ppis;
1005 struct per_process_info *ppi;
1008 ppis = malloc(ppi_list_entries * sizeof(struct per_process_info *));
1013 ppi = ppi->list_next;
1016 qsort(ppis, ppi_list_entries, sizeof(ppi), ppi_name_compare);
1018 i = ppi_list_entries - 1;
1023 ppi->list_next = ppi_list;
1031 static void show_process_stats(void)
1033 struct per_process_info *ppi;
1035 sort_process_list();
1041 if (ppi->more_than_one)
1042 sprintf(name, "%s (%u, ...)", ppi->name, ppi->pid);
1044 sprintf(name, "%s (%u)", ppi->name, ppi->pid);
1046 dump_io_stats(&ppi->io_stats, name);
1047 dump_wait_stats(ppi);
1048 ppi = ppi->list_next;
1054 static void show_device_and_cpu_stats(void)
1056 struct per_dev_info *pdi;
1057 struct per_cpu_info *pci;
1058 struct io_stats total, *ios;
1059 int i, j, pci_events;
1060 char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3];
1063 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
1065 memset(&total, 0, sizeof(total));
1071 for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) {
1075 ios = &pci->io_stats;
1076 total.qreads += ios->qreads;
1077 total.qwrites += ios->qwrites;
1078 total.creads += ios->creads;
1079 total.cwrites += ios->cwrites;
1080 total.mreads += ios->mreads;
1081 total.mwrites += ios->mwrites;
1082 total.ireads += ios->ireads;
1083 total.iwrites += ios->iwrites;
1084 total.qread_kb += ios->qread_kb;
1085 total.qwrite_kb += ios->qwrite_kb;
1086 total.cread_kb += ios->cread_kb;
1087 total.cwrite_kb += ios->cwrite_kb;
1088 total.iread_kb += ios->iread_kb;
1089 total.iwrite_kb += ios->iwrite_kb;
1090 total.timer_unplugs += ios->timer_unplugs;
1091 total.io_unplugs += ios->io_unplugs;
1093 snprintf(line, sizeof(line) - 1, "CPU%d (%s):",
1094 j, get_dev_name(pdi, name, sizeof(name)));
1095 dump_io_stats(ios, line);
1099 if (pci_events > 1) {
1101 snprintf(line, sizeof(line) - 1, "Total (%s):",
1102 get_dev_name(pdi, name, sizeof(name)));
1103 dump_io_stats(&total, line);
1106 fprintf(ofp, "\nEvents (%s): %'Lu entries, %'lu skips\n",
1107 get_dev_name(pdi, line, sizeof(line)), pdi->events,
1112 static void find_genesis(void)
1114 struct trace *t = trace_list;
1116 genesis_time = -1ULL;
1118 if (t->bit->time < genesis_time)
1119 genesis_time = t->bit->time;
1125 static int sort_entries(void)
1133 while ((t = trace_list) != NULL) {
1134 trace_list = t->next;
1136 if (verify_trace(t->bit))
1139 t->bit->time -= genesis_time;
1141 if (trace_rb_insert(t))
1150 * struct trace and blktrace allocation cache, we do potentially
1151 * millions of mallocs for these structures while only using at most
1152 * a few thousand at the time
1154 static inline void t_free(struct trace *t)
1156 t->next = t_alloc_list;
1160 static inline struct trace *t_alloc(void)
1162 struct trace *t = t_alloc_list;
1165 t_alloc_list = t->next;
1169 return malloc(sizeof(*t));
1172 static inline void bit_free(struct blk_io_trace *bit)
1175 * abuse a 64-bit field for a next pointer for the free item
1177 bit->time = (__u64) (unsigned long) bit_alloc_list;
1178 bit_alloc_list = (struct blk_io_trace *) bit;
1181 static inline struct blk_io_trace *bit_alloc(void)
1183 struct blk_io_trace *bit = bit_alloc_list;
1186 bit_alloc_list = (struct blk_io_trace *) (unsigned long) \
1191 return malloc(sizeof(*bit));
1194 static void show_entries_rb(int force)
1196 struct per_dev_info *pdi = NULL;
1197 struct per_cpu_info *pci = NULL;
1198 struct blk_io_trace *bit;
1202 while ((n = rb_first(&rb_sort_root)) != NULL) {
1207 t = rb_entry(n, struct trace, rb_node);
1210 if (!pdi || pdi->id != bit->device)
1211 pdi = get_dev_info(bit->device);
1214 fprintf(stderr, "Unknown device ID? (%d,%d)\n",
1215 MAJOR(bit->device), MINOR(bit->device));
1219 if (bit->cpu > pdi->ncpus) {
1220 fprintf(stderr, "Unknown CPU ID? (%d, device %d,%d)\n",
1221 bit->cpu, MAJOR(bit->device),
1222 MINOR(bit->device));
1227 * back off displaying more info if we are out of sync
1228 * on SMP systems. to prevent stalling on lost events,
1229 * only allow an event to skip us a few times
1231 if (bit->sequence > (pdi->last_sequence + 1) && !force) {
1235 * the wanted sequence is really there, continue
1236 * because this means that the log time is earlier
1237 * on the trace we have now
1239 __t = trace_rb_find(pdi->id, pdi->last_sequence + 1);
1243 if (t->skipped < 5) {
1251 if (bit->time >= stopwatch_end)
1254 if (!force && bit->time > last_allowed_time)
1257 pdi->last_sequence = bit->sequence;
1259 if (bit->time >= stopwatch_start) {
1260 check_time(pdi, bit);
1262 if (!pci || pci->cpu != bit->cpu)
1263 pci = get_cpu_info(pdi, bit->cpu);
1265 dump_trace(bit, pci, pdi);
1268 rb_erase(&t->rb_node, &rb_sort_root);
1275 static int read_data(int fd, void *buffer, int bytes, int block)
1277 int ret, bytes_left, fl;
1280 fl = fcntl(fd, F_GETFL);
1283 fcntl(fd, F_SETFL, fl | O_NONBLOCK);
1285 fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
1289 while (bytes_left > 0) {
1290 ret = read(fd, p, bytes_left);
1294 if (errno != EAGAIN)
1307 static int read_events(int fd, int always_block)
1309 struct per_dev_info *pdi = NULL;
1312 while (!is_done() && events < rb_batch) {
1313 struct blk_io_trace *bit;
1320 if (read_data(fd, bit, sizeof(*bit), !events || always_block))
1323 magic = be32_to_cpu(bit->magic);
1324 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
1325 fprintf(stderr, "Bad magic %x\n", magic);
1329 pdu_len = be16_to_cpu(bit->pdu_len);
1331 void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
1333 if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1))
1340 memset(t, 0, sizeof(*t));
1344 t->next = trace_list;
1347 if (!pdi || pdi->id != bit->device)
1348 pdi = get_dev_info(bit->device);
1350 if (bit->time > pdi->last_read_time)
1351 pdi->last_read_time = bit->time;
1359 static int do_file(void)
1361 struct per_cpu_info *pci;
1362 struct per_dev_info *pdi;
1363 int i, j, events, events_added;
1366 * first prepare all files for reading
1368 for (i = 0; i < ndevices; i++) {
1371 pdi->last_sequence = 0;
1376 pci = get_cpu_info(pdi, j);
1380 snprintf(pci->fname, sizeof(pci->fname)-1,
1381 "%s.blktrace.%d", pdi->name, pci->cpu);
1382 if (stat(pci->fname, &st) < 0)
1385 pci->fd = open(pci->fname, O_RDONLY);
1392 printf("Input file %s added\n", pci->fname);
1398 * now loop over the files reading in the data
1402 last_allowed_time = -1ULL;
1404 for (i = 0; i < ndevices; i++) {
1407 for (j = 0; j < pdi->nfiles; j++) {
1409 pci = get_cpu_info(pdi, j);
1414 events = read_events(pci->fd, 1);
1421 if (pdi->last_read_time < last_allowed_time)
1422 last_allowed_time = pdi->last_read_time;
1424 events_added += events;
1428 if (sort_entries() == -1)
1433 } while (events_added);
1435 if (rb_sort_entries)
1441 static int do_stdin(void)
1445 last_allowed_time = -1ULL;
1446 fd = dup(STDIN_FILENO);
1450 events = read_events(fd, 0);
1454 if (sort_entries() == -1)
1460 if (rb_sort_entries)
1467 static void flush_output(void)
1472 static void handle_sigint(int sig)
1479 * Extract start and duration times from a string, allowing
1480 * us to specify a time interval of interest within a trace.
1481 * Format: "duration" (start is zero) or "start:duration".
1483 static int find_stopwatch_interval(char *string)
1488 value = strtod(string, &sp);
1490 fprintf(stderr,"Invalid stopwatch timer: %s\n", string);
1494 stopwatch_start = DOUBLE_TO_NANO_ULL(value);
1496 value = strtod(string, &sp);
1497 if (sp == string || *sp != '\0') {
1498 fprintf(stderr,"Invalid stopwatch duration time: %s\n",
1502 } else if (*sp != '\0') {
1503 fprintf(stderr,"Invalid stopwatch start timer: %s\n", string);
1506 stopwatch_end = DOUBLE_TO_NANO_ULL(value);
1507 if (stopwatch_end <= stopwatch_start) {
1508 fprintf(stderr, "Invalid stopwatch interval: %Lu -> %Lu\n",
1509 stopwatch_start, stopwatch_end);
1516 static char usage_str[] = \
1517 "[ -i <input name> ] [-o <output name> [ -s ] [ -t ] [ -q ]\n" \
1518 "[ -w start:stop ] [ -f output format ] [ -F format spec ] [ -v] \n\n" \
1519 "\t-i Input file containing trace data, or '-' for stdin\n" \
1520 "\t-o Output file. If not given, output is stdout\n" \
1521 "\t-b stdin read batching\n" \
1522 "\t-s Show per-program io statistics\n" \
1523 "\t-n Hash processes by name, not pid\n" \
1524 "\t-t Track individual ios. Will tell you the time a request took\n" \
1525 "\t to get queued, to get dispatched, and to get completed\n" \
1526 "\t-q Quiet. Don't display any stats at the end of the trace\n" \
1527 "\t-w Only parse data between the given time interval in seconds.\n" \
1528 "\t If 'start' isn't given, blkparse defaults the start time to 0\n" \
1529 "\t -f Output format. Customize the output format. The format field\n" \
1530 "\t identifies can be found in the documentation\n" \
1531 "\t-F Format specification. Can be found in the documentation\n" \
1532 "\t-v Print program version info\n\n";
1534 static void usage(char *prog)
1536 fprintf(stderr, "Usage: %s %s %s", prog, blkparse_version, usage_str);
1539 int main(int argc, char *argv[])
1543 int per_device_and_cpu_stats = 1;
1545 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
1548 if (!strcmp(optarg, "-") && !pipeline)
1550 else if (resize_devices(optarg) != 0)
1554 output_name = optarg;
1557 rb_batch = atoi(optarg);
1559 rb_batch = RB_BATCH_DEFAULT;
1562 per_process_stats = 1;
1568 per_device_and_cpu_stats = 0;
1571 if (find_stopwatch_interval(optarg) != 0)
1575 set_all_format_specs(optarg);
1578 if (add_format_spec(optarg) != 0)
1582 ppi_hash_by_pid = 0;
1585 printf("%s version %s\n", argv[0], blkparse_version);
1593 while (optind < argc) {
1594 if (!strcmp(argv[optind], "-") && !pipeline)
1596 else if (resize_devices(argv[optind]) != 0)
1601 if (!pipeline && !ndevices) {
1606 memset(&rb_sort_root, 0, sizeof(rb_sort_root));
1607 memset(&rb_track_root, 0, sizeof(rb_track_root));
1609 signal(SIGINT, handle_sigint);
1610 signal(SIGHUP, handle_sigint);
1611 signal(SIGTERM, handle_sigint);
1613 setlocale(LC_NUMERIC, "en_US");
1616 ofp = fdopen(STDOUT_FILENO, "w");
1621 snprintf(ofname, sizeof(ofname) - 1, "%s", output_name);
1622 ofp = fopen(ofname, "w");
1631 ofp_buffer = malloc(4096);
1632 if (setvbuf(ofp, ofp_buffer, mode, 4096)) {
1642 if (per_process_stats)
1643 show_process_stats();
1645 if (per_device_and_cpu_stats)
1646 show_device_and_cpu_stats();