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;
66 unsigned long long longest_allocation_wait[2];
67 unsigned long long longest_dispatch_wait[2];
68 unsigned long long longest_completion_wait[2];
71 #define PPI_HASH_SHIFT (8)
72 #define PPI_HASH_SIZE (1 << PPI_HASH_SHIFT)
73 #define PPI_HASH_MASK (PPI_HASH_SIZE - 1)
74 static struct per_process_info *ppi_hash_table[PPI_HASH_SIZE];
75 static struct per_process_info *ppi_list;
76 static int ppi_list_entries;
78 #define S_OPTS "i:o:b:stqw:f:F:vn"
79 static struct option l_opts[] = {
82 .has_arg = required_argument,
88 .has_arg = required_argument,
94 .has_arg = required_argument,
99 .name = "per program stats",
100 .has_arg = no_argument,
106 .has_arg = no_argument,
112 .has_arg = no_argument,
118 .has_arg = required_argument,
124 .has_arg = required_argument,
129 .name = "format-spec",
130 .has_arg = required_argument,
135 .name = "hash by name",
136 .has_arg = no_argument,
142 .has_arg = no_argument,
149 * for sorting the displayed output
152 struct blk_io_trace *bit;
153 struct rb_node rb_node;
158 static struct rb_root rb_sort_root;
159 static unsigned long rb_sort_entries;
161 static struct rb_root rb_track_root;
163 static struct trace *trace_list;
168 static struct blk_io_trace *bit_alloc_list;
169 static struct trace *t_alloc_list;
172 * for tracking individual ios
175 struct rb_node rb_node;
181 unsigned long long allocation_time;
182 unsigned long long queue_time;
183 unsigned long long dispatch_time;
184 unsigned long long completion_time;
188 static struct per_dev_info *devices;
189 static char *get_dev_name(struct per_dev_info *, char *, int);
192 static char *output_name;
194 static unsigned long long genesis_time;
195 static unsigned long long last_allowed_time;
196 static unsigned long long stopwatch_start; /* start from zero by default */
197 static unsigned long long stopwatch_end = ULONG_LONG_MAX; /* "infinity" */
199 static int per_process_stats;
200 static int track_ios;
201 static int ppi_hash_by_pid = 1;
203 #define RB_BATCH_DEFAULT (512)
204 static int rb_batch = RB_BATCH_DEFAULT;
208 #define is_done() (*(volatile int *)(&done))
209 static volatile int done;
211 #define JHASH_RANDOM (0x3af5f2ee)
213 static inline int ppi_hash_pid(__u32 pid)
215 return jhash_1word(pid, JHASH_RANDOM) & PPI_HASH_MASK;
218 static inline int ppi_hash_name(const char *name)
220 return jhash(name, 16, JHASH_RANDOM) & PPI_HASH_MASK;
223 static inline int ppi_hash(struct per_process_info *ppi)
226 return ppi_hash_pid(ppi->pid);
228 if (ppi->name[0] == 0)
229 fprintf(stderr, "bad\n");
231 return ppi_hash_name(ppi->name);
234 static inline void add_process_to_hash(struct per_process_info *ppi)
236 const int hash_idx = ppi_hash(ppi);
238 ppi->hash_next = ppi_hash_table[hash_idx];
239 ppi_hash_table[hash_idx] = ppi;
242 static inline void add_process_to_list(struct per_process_info *ppi)
244 ppi->list_next = ppi_list;
249 static struct per_process_info *find_process_by_name(char *name)
251 const int hash_idx = ppi_hash_name(name);
252 struct per_process_info *ppi;
254 ppi = ppi_hash_table[hash_idx];
256 if (!strcmp(ppi->name, name))
259 ppi = ppi->hash_next;
265 static struct per_process_info *find_process_by_pid(__u32 pid)
267 const int hash_idx = ppi_hash_pid(pid);
268 struct per_process_info *ppi;
270 ppi = ppi_hash_table[hash_idx];
275 ppi = ppi->hash_next;
281 static struct per_process_info *find_process(__u32 pid, char *name)
284 return find_process_by_pid(pid);
286 return find_process_by_name(name);
289 static inline int trace_rb_insert(struct trace *t)
291 struct rb_node **p = &rb_sort_root.rb_node;
292 struct rb_node *parent = NULL;
297 __t = rb_entry(parent, struct trace, rb_node);
299 if (t->bit->time < __t->bit->time)
301 else if (t->bit->time > __t->bit->time)
303 else if (t->bit->device < __t->bit->device)
305 else if (t->bit->device > __t->bit->device)
307 else if (t->bit->sequence < __t->bit->sequence)
309 else if (t->bit->sequence > __t->bit->sequence)
311 else if (t->bit->device == __t->bit->device) {
313 "sequence alias (%d) on device %d,%d!\n",
315 MAJOR(t->bit->device), MINOR(t->bit->device));
321 rb_link_node(&t->rb_node, parent, p);
322 rb_insert_color(&t->rb_node, &rb_sort_root);
326 static struct trace *trace_rb_find(dev_t device, unsigned long sequence)
328 struct rb_node **p = &rb_sort_root.rb_node;
329 struct rb_node *parent = NULL;
334 __t = rb_entry(parent, struct trace, rb_node);
336 if (device < __t->bit->device)
338 else if (device > __t->bit->device)
340 else if (sequence < __t->bit->sequence)
342 else if (sequence > __t->bit->sequence)
351 static inline int track_rb_insert(struct io_track *iot)
353 struct rb_node **p = &rb_track_root.rb_node;
354 struct rb_node *parent = NULL;
355 struct io_track *__iot;
360 __iot = rb_entry(parent, struct io_track, rb_node);
362 if (iot->device < __iot->device)
364 else if (iot->device > __iot->device)
366 else if (iot->sector < __iot->sector)
368 else if (iot->sector > __iot->sector)
372 "sector alias (%Lu) on device %d,%d!\n",
373 (unsigned long long) iot->sector,
374 MAJOR(iot->device), MINOR(iot->device));
379 rb_link_node(&iot->rb_node, parent, p);
380 rb_insert_color(&iot->rb_node, &rb_track_root);
384 static struct io_track *__find_track(dev_t device, __u64 sector)
386 struct rb_node **p = &rb_track_root.rb_node;
387 struct rb_node *parent = NULL;
388 struct io_track *__iot;
393 __iot = rb_entry(parent, struct io_track, rb_node);
395 if (device < __iot->device)
397 else if (device > __iot->device)
399 else if (sector < __iot->sector)
401 else if (sector > __iot->sector)
410 static struct io_track *find_track(__u32 pid, char *comm, dev_t device,
413 struct io_track *iot;
415 iot = __find_track(device, sector);
417 iot = malloc(sizeof(*iot));
419 memcpy(iot->comm, comm, sizeof(iot->comm));
420 iot->device = device;
421 iot->sector = sector;
422 track_rb_insert(iot);
428 static void log_track_frontmerge(struct blk_io_trace *t)
430 struct io_track *iot;
435 iot = __find_track(t->device, t->sector + (t->bytes >> 9));
437 fprintf(stderr, "failed to find mergeable event\n");
441 rb_erase(&iot->rb_node, &rb_track_root);
442 iot->sector -= t->bytes >> 9;
443 track_rb_insert(iot);
446 static void log_track_getrq(struct blk_io_trace *t)
448 struct io_track *iot;
453 iot = find_track(t->pid, t->comm, t->device, t->sector);
454 iot->allocation_time = t->time;
459 * return time between rq allocation and insertion
461 static unsigned long long log_track_insert(struct blk_io_trace *t)
463 unsigned long long elapsed;
464 struct io_track *iot;
469 iot = find_track(t->pid, t->comm, t->device, t->sector);
470 iot->queue_time = t->time;
471 elapsed = iot->queue_time - iot->allocation_time;
473 if (per_process_stats) {
474 struct per_process_info *ppi = find_process(iot->pid,iot->comm);
475 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
477 if (ppi && elapsed > ppi->longest_allocation_wait[w])
478 ppi->longest_allocation_wait[w] = elapsed;
485 * return time between queue and issue
487 static unsigned long long log_track_issue(struct blk_io_trace *t)
489 unsigned long long elapsed;
490 struct io_track *iot;
494 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
497 iot = __find_track(t->device, t->sector);
499 fprintf(stderr, "failed to find issue event\n");
503 iot->dispatch_time = t->time;
504 elapsed = iot->dispatch_time - iot->queue_time;
506 if (per_process_stats) {
507 struct per_process_info *ppi = find_process(iot->pid,iot->comm);
508 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
510 if (ppi && elapsed > ppi->longest_dispatch_wait[w])
511 ppi->longest_dispatch_wait[w] = elapsed;
518 * return time between dispatch and complete
520 static unsigned long long log_track_complete(struct blk_io_trace *t)
522 unsigned long long elapsed;
523 struct io_track *iot;
527 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
530 iot = __find_track(t->device, t->sector);
532 fprintf(stderr, "failed to find complete event\n");
536 iot->completion_time = t->time;
537 elapsed = iot->completion_time - iot->dispatch_time;
539 if (per_process_stats) {
540 struct per_process_info *ppi = find_process(iot->pid,iot->comm);
541 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
543 if (ppi && elapsed > ppi->longest_completion_wait[w])
544 ppi->longest_completion_wait[w] = elapsed;
548 * kill the trace, we don't need it after completion
550 rb_erase(&iot->rb_node, &rb_track_root);
557 static struct io_stats *find_process_io_stats(__u32 pid, char *name)
559 struct per_process_info *ppi = find_process(pid, name);
562 ppi = malloc(sizeof(*ppi));
563 memset(ppi, 0, sizeof(*ppi));
564 memcpy(ppi->name, name, 16);
566 add_process_to_hash(ppi);
567 add_process_to_list(ppi);
570 return &ppi->io_stats;
573 static void resize_cpu_info(struct per_dev_info *pdi, int cpu)
575 struct per_cpu_info *cpus = pdi->cpus;
576 int ncpus = pdi->ncpus;
577 int new_count = cpu + 1;
581 size = new_count * sizeof(struct per_cpu_info);
582 cpus = realloc(cpus, size);
585 fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n",
586 get_dev_name(pdi, name, sizeof(name)), size);
590 new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info));
591 new_space = (new_count - ncpus) * sizeof(struct per_cpu_info);
592 memset(new_start, 0, new_space);
594 pdi->ncpus = new_count;
598 static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu)
600 struct per_cpu_info *pci;
602 if (cpu >= pdi->ncpus)
603 resize_cpu_info(pdi, cpu);
605 pci = &pdi->cpus[cpu];
611 static int resize_devices(char *name)
613 int size = (ndevices + 1) * sizeof(struct per_dev_info);
615 devices = realloc(devices, size);
617 fprintf(stderr, "Out of memory, device %s (%d)\n", name, size);
620 memset(&devices[ndevices], 0, sizeof(struct per_dev_info));
621 devices[ndevices].name = name;
626 static struct per_dev_info *get_dev_info(dev_t id)
628 struct per_dev_info *pdi;
631 for (i = 0; i < ndevices; i++) {
634 if (devices[i].id == id)
638 if (resize_devices(NULL) != 0)
641 pdi = &devices[ndevices - 1];
643 pdi->last_sequence = 0;
644 pdi->last_read_time = 0;
648 static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size)
651 snprintf(buffer, size, "%s", pdi->name);
653 snprintf(buffer, size, "%d,%d", MAJOR(pdi->id), MINOR(pdi->id));
657 static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit)
659 unsigned long long this = bit->time;
660 unsigned long long last = pdi->last_reported_time;
662 pdi->backwards = (this < last) ? 'B' : ' ';
663 pdi->last_reported_time = this;
666 static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t,
671 ios->qwrite_kb += t->bytes >> 10;
674 ios->qread_kb += t->bytes >> 10;
678 static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci,
681 __account_m(&pci->io_stats, t, rw);
683 if (per_process_stats) {
684 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
686 __account_m(ios, t, rw);
690 static inline void __account_queue(struct io_stats *ios, struct blk_io_trace *t,
695 ios->qwrite_kb += t->bytes >> 10;
698 ios->qread_kb += t->bytes >> 10;
702 static inline void account_queue(struct blk_io_trace *t,
703 struct per_cpu_info *pci, int rw)
705 __account_queue(&pci->io_stats, t, rw);
707 if (per_process_stats) {
708 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
710 __account_queue(ios, t, rw);
714 static inline void __account_c(struct io_stats *ios, int rw, unsigned int bytes)
718 ios->cwrite_kb += bytes >> 10;
721 ios->cread_kb += bytes >> 10;
725 static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci,
728 __account_c(&pci->io_stats, rw, bytes);
730 if (per_process_stats) {
731 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
733 __account_c(ios, rw, bytes);
737 static inline void __account_issue(struct io_stats *ios, int rw,
742 ios->iwrite_kb += bytes >> 10;
745 ios->iread_kb += bytes >> 10;
749 static inline void account_issue(struct blk_io_trace *t,
750 struct per_cpu_info *pci, int rw)
752 __account_issue(&pci->io_stats, rw, t->bytes);
754 if (per_process_stats) {
755 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
757 __account_issue(ios, rw, t->bytes);
761 static inline void __account_unplug(struct io_stats *ios, int timer)
764 ios->timer_unplugs++;
769 static inline void account_unplug(struct blk_io_trace *t,
770 struct per_cpu_info *pci, int timer)
772 __account_unplug(&pci->io_stats, timer);
774 if (per_process_stats) {
775 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
777 __account_unplug(ios, timer);
781 static void log_complete(struct per_cpu_info *pci, struct blk_io_trace *t,
784 process_fmt(act, pci, t, log_track_complete(t), 0, NULL);
787 static void log_insert(struct per_cpu_info *pci, struct blk_io_trace *t,
790 process_fmt(act, pci, t, log_track_insert(t), 0, NULL);
793 static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t,
796 process_fmt(act, pci, t, -1, 0, NULL);
799 static void log_issue(struct per_cpu_info *pci, struct blk_io_trace *t,
802 process_fmt(act, pci, t, log_track_issue(t), 0, NULL);
805 static void log_merge(struct per_cpu_info *pci, struct blk_io_trace *t,
809 log_track_frontmerge(t);
811 process_fmt(act, pci, t, -1ULL, 0, NULL);
814 static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t,
817 process_fmt(act, pci, t, -1ULL, 0, NULL);
820 static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t,
823 process_fmt(act, pci, t, -1ULL, 0, NULL);
826 static void log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t,
829 process_fmt(act, pci, t, -1ULL, 0, NULL);
832 static void log_split(struct per_cpu_info *pci, struct blk_io_trace *t,
835 process_fmt(act, pci, t, -1ULL, 0, NULL);
838 static void log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act)
840 unsigned char *buf = (unsigned char *) t + sizeof(*t);
842 process_fmt(act, pci, t, -1ULL, t->pdu_len, buf);
845 static void dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci)
847 int act = t->action & 0xffff;
851 log_generic(pci, t, "Q");
854 log_generic(pci, t, "G");
856 case __BLK_TA_SLEEPRQ:
857 log_generic(pci, t, "S");
859 case __BLK_TA_REQUEUE:
860 log_generic(pci, t, "R");
865 case __BLK_TA_COMPLETE:
868 case __BLK_TA_INSERT:
872 fprintf(stderr, "Bad pc action %x\n", act);
877 static void dump_trace_fs(struct blk_io_trace *t, struct per_cpu_info *pci)
879 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
880 int act = t->action & 0xffff;
884 account_queue(t, pci, w);
885 log_queue(pci, t, "Q");
887 case __BLK_TA_INSERT:
888 log_insert(pci, t, "I");
890 case __BLK_TA_BACKMERGE:
891 account_m(t, pci, w);
892 log_merge(pci, t, "M");
894 case __BLK_TA_FRONTMERGE:
895 account_m(t, pci, w);
896 log_merge(pci, t, "F");
900 log_generic(pci, t, "G");
902 case __BLK_TA_SLEEPRQ:
903 log_generic(pci, t, "S");
905 case __BLK_TA_REQUEUE:
906 account_c(t, pci, w, -t->bytes);
907 log_queue(pci, t, "R");
910 account_issue(t, pci, w);
911 log_issue(pci, t, "D");
913 case __BLK_TA_COMPLETE:
914 account_c(t, pci, w, t->bytes);
915 log_complete(pci, t, "C");
918 log_action(pci, t, "P");
920 case __BLK_TA_UNPLUG_IO:
921 account_unplug(t, pci, 0);
922 log_unplug(pci, t, "U");
924 case __BLK_TA_UNPLUG_TIMER:
925 account_unplug(t, pci, 1);
926 log_unplug(pci, t, "UT");
929 log_split(pci, t, "X");
931 case __BLK_TA_BOUNCE:
932 log_generic(pci, t, "B");
935 fprintf(stderr, "Bad fs action %x\n", t->action);
940 static void dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
941 struct per_dev_info *pdi)
943 if (t->action & BLK_TC_ACT(BLK_TC_PC))
944 dump_trace_pc(t, pci);
946 dump_trace_fs(t, pci);
951 static void dump_io_stats(struct io_stats *ios, char *msg)
953 fprintf(ofp, "%s\n", msg);
955 fprintf(ofp, " Reads Queued: %'8lu, %'8LuKiB\t", ios->qreads, ios->qread_kb);
956 fprintf(ofp, " Writes Queued: %'8lu, %'8LuKiB\n", ios->qwrites,ios->qwrite_kb);
958 fprintf(ofp, " Read Dispatches: %'8lu, %'8LuKiB\t", ios->ireads, ios->iread_kb);
959 fprintf(ofp, " Write Dispatches: %'8lu, %'8LuKiB\n", ios->iwrites,ios->iwrite_kb);
960 fprintf(ofp, " Reads Completed: %'8lu, %'8LuKiB\t", ios->creads, ios->cread_kb);
961 fprintf(ofp, " Writes Completed: %'8lu, %'8LuKiB\n", ios->cwrites,ios->cwrite_kb);
962 fprintf(ofp, " Read Merges: %'8lu%8c\t", ios->mreads, ' ');
963 fprintf(ofp, " Write Merges: %'8lu\n", ios->mwrites);
964 fprintf(ofp, " IO unplugs: %'8lu%8c\t", ios->io_unplugs, ' ');
965 fprintf(ofp, " Timer unplugs: %'8lu\n", ios->timer_unplugs);
968 static void dump_wait_stats(struct per_process_info *ppi)
970 unsigned long rawait = ppi->longest_allocation_wait[0] / 1000;
971 unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000;
972 unsigned long rcwait = ppi->longest_completion_wait[0] / 1000;
973 unsigned long wawait = ppi->longest_allocation_wait[1] / 1000;
974 unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000;
975 unsigned long wcwait = ppi->longest_completion_wait[1] / 1000;
977 fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' ');
978 fprintf(ofp, " Allocation wait: %'8lu\n", wawait);
979 fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' ');
980 fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait);
981 fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' ');
982 fprintf(ofp, " Completion wait: %'8lu\n", wcwait);
985 static int ppi_name_compare(const void *p1, const void *p2)
987 struct per_process_info *ppi1 = *((struct per_process_info **) p1);
988 struct per_process_info *ppi2 = *((struct per_process_info **) p2);
991 res = strverscmp(ppi1->name, ppi2->name);
993 res = ppi1->pid > ppi2->pid;
998 static void sort_process_list(void)
1000 struct per_process_info **ppis;
1001 struct per_process_info *ppi;
1004 ppis = malloc(ppi_list_entries * sizeof(struct per_process_info *));
1009 ppi = ppi->list_next;
1012 qsort(ppis, ppi_list_entries, sizeof(ppi), ppi_name_compare);
1014 i = ppi_list_entries - 1;
1019 ppi->list_next = ppi_list;
1027 static void show_process_stats(void)
1029 struct per_process_info *ppi;
1031 sort_process_list();
1037 if (ppi_hash_by_pid)
1038 sprintf(name, "%s (%u)", ppi->name, ppi->pid);
1040 sprintf(name, "%s (%u, ...)", ppi->name, ppi->pid);
1042 dump_io_stats(&ppi->io_stats, name);
1043 dump_wait_stats(ppi);
1044 ppi = ppi->list_next;
1050 static void show_device_and_cpu_stats(void)
1052 struct per_dev_info *pdi;
1053 struct per_cpu_info *pci;
1054 struct io_stats total, *ios;
1055 int i, j, pci_events;
1056 char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3];
1059 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
1061 memset(&total, 0, sizeof(total));
1067 for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) {
1071 ios = &pci->io_stats;
1072 total.qreads += ios->qreads;
1073 total.qwrites += ios->qwrites;
1074 total.creads += ios->creads;
1075 total.cwrites += ios->cwrites;
1076 total.mreads += ios->mreads;
1077 total.mwrites += ios->mwrites;
1078 total.ireads += ios->ireads;
1079 total.iwrites += ios->iwrites;
1080 total.qread_kb += ios->qread_kb;
1081 total.qwrite_kb += ios->qwrite_kb;
1082 total.cread_kb += ios->cread_kb;
1083 total.cwrite_kb += ios->cwrite_kb;
1084 total.iread_kb += ios->iread_kb;
1085 total.iwrite_kb += ios->iwrite_kb;
1086 total.timer_unplugs += ios->timer_unplugs;
1087 total.io_unplugs += ios->io_unplugs;
1089 snprintf(line, sizeof(line) - 1, "CPU%d (%s):",
1090 j, get_dev_name(pdi, name, sizeof(name)));
1091 dump_io_stats(ios, line);
1095 if (pci_events > 1) {
1097 snprintf(line, sizeof(line) - 1, "Total (%s):",
1098 get_dev_name(pdi, name, sizeof(name)));
1099 dump_io_stats(&total, line);
1102 fprintf(ofp, "\nEvents (%s): %'Lu entries, %'lu skips\n",
1103 get_dev_name(pdi, line, sizeof(line)), pdi->events,
1108 static void find_genesis(void)
1110 struct trace *t = trace_list;
1112 genesis_time = -1ULL;
1114 if (t->bit->time < genesis_time)
1115 genesis_time = t->bit->time;
1121 static int sort_entries(void)
1129 while ((t = trace_list) != NULL) {
1130 trace_list = t->next;
1132 if (verify_trace(t->bit))
1135 t->bit->time -= genesis_time;
1137 if (trace_rb_insert(t))
1146 * struct trace and blktrace allocation cache, we do potentially
1147 * millions of mallocs for these structures while only using at most
1148 * a few thousand at the time
1150 static inline void t_free(struct trace *t)
1152 t->next = t_alloc_list;
1156 static inline struct trace *t_alloc(void)
1158 struct trace *t = t_alloc_list;
1161 t_alloc_list = t->next;
1165 return malloc(sizeof(*t));
1168 static inline void bit_free(struct blk_io_trace *bit)
1171 * abuse a 64-bit field for a next pointer for the free item
1173 bit->time = (__u64) (unsigned long) bit_alloc_list;
1174 bit_alloc_list = (struct blk_io_trace *) bit;
1177 static inline struct blk_io_trace *bit_alloc(void)
1179 struct blk_io_trace *bit = bit_alloc_list;
1182 bit_alloc_list = (struct blk_io_trace *) (unsigned long) \
1187 return malloc(sizeof(*bit));
1190 static void show_entries_rb(int force)
1192 struct per_dev_info *pdi = NULL;
1193 struct per_cpu_info *pci = NULL;
1194 struct blk_io_trace *bit;
1198 while ((n = rb_first(&rb_sort_root)) != NULL) {
1203 t = rb_entry(n, struct trace, rb_node);
1206 if (!pdi || pdi->id != bit->device)
1207 pdi = get_dev_info(bit->device);
1210 fprintf(stderr, "Unknown device ID? (%d,%d)\n",
1211 MAJOR(bit->device), MINOR(bit->device));
1215 if (bit->cpu > pdi->ncpus) {
1216 fprintf(stderr, "Unknown CPU ID? (%d, device %d,%d)\n",
1217 bit->cpu, MAJOR(bit->device),
1218 MINOR(bit->device));
1223 * back off displaying more info if we are out of sync
1224 * on SMP systems. to prevent stalling on lost events,
1225 * only allow an event to skip us a few times
1227 if (bit->sequence > (pdi->last_sequence + 1) && !force) {
1231 * the wanted sequence is really there, continue
1232 * because this means that the log time is earlier
1233 * on the trace we have now
1235 __t = trace_rb_find(pdi->id, pdi->last_sequence + 1);
1239 if (t->skipped < 5) {
1247 if (bit->time >= stopwatch_end)
1250 if (!force && bit->time > last_allowed_time)
1253 pdi->last_sequence = bit->sequence;
1255 if (bit->time >= stopwatch_start) {
1256 check_time(pdi, bit);
1258 if (!pci || pci->cpu != bit->cpu)
1259 pci = get_cpu_info(pdi, bit->cpu);
1261 dump_trace(bit, pci, pdi);
1264 rb_erase(&t->rb_node, &rb_sort_root);
1271 static int read_data(int fd, void *buffer, int bytes, int block)
1273 int ret, bytes_left, fl;
1276 fl = fcntl(fd, F_GETFL);
1279 fcntl(fd, F_SETFL, fl | O_NONBLOCK);
1281 fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
1285 while (bytes_left > 0) {
1286 ret = read(fd, p, bytes_left);
1290 if (errno != EAGAIN)
1303 static int read_events(int fd, int always_block)
1305 struct per_dev_info *pdi = NULL;
1308 while (!is_done() && events < rb_batch) {
1309 struct blk_io_trace *bit;
1316 if (read_data(fd, bit, sizeof(*bit), !events || always_block))
1319 magic = be32_to_cpu(bit->magic);
1320 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
1321 fprintf(stderr, "Bad magic %x\n", magic);
1325 pdu_len = be16_to_cpu(bit->pdu_len);
1327 void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
1329 if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1))
1336 memset(t, 0, sizeof(*t));
1340 t->next = trace_list;
1343 if (!pdi || pdi->id != bit->device)
1344 pdi = get_dev_info(bit->device);
1346 if (bit->time > pdi->last_read_time)
1347 pdi->last_read_time = bit->time;
1355 static int do_file(void)
1357 struct per_cpu_info *pci;
1358 struct per_dev_info *pdi;
1359 int i, j, events, events_added;
1362 * first prepare all files for reading
1364 for (i = 0; i < ndevices; i++) {
1367 pdi->last_sequence = 0;
1372 pci = get_cpu_info(pdi, j);
1376 snprintf(pci->fname, sizeof(pci->fname)-1,
1377 "%s.blktrace.%d", pdi->name, pci->cpu);
1378 if (stat(pci->fname, &st) < 0)
1381 pci->fd = open(pci->fname, O_RDONLY);
1388 printf("Input file %s added\n", pci->fname);
1394 * now loop over the files reading in the data
1398 last_allowed_time = -1ULL;
1400 for (i = 0; i < ndevices; i++) {
1403 for (j = 0; j < pdi->nfiles; j++) {
1405 pci = get_cpu_info(pdi, j);
1410 events = read_events(pci->fd, 1);
1417 if (pdi->last_read_time < last_allowed_time)
1418 last_allowed_time = pdi->last_read_time;
1420 events_added += events;
1424 if (sort_entries() == -1)
1429 } while (events_added);
1431 if (rb_sort_entries)
1437 static int do_stdin(void)
1441 last_allowed_time = -1ULL;
1442 fd = dup(STDIN_FILENO);
1446 events = read_events(fd, 0);
1450 if (sort_entries() == -1)
1456 if (rb_sort_entries)
1463 static void flush_output(void)
1468 static void handle_sigint(int sig)
1475 * Extract start and duration times from a string, allowing
1476 * us to specify a time interval of interest within a trace.
1477 * Format: "duration" (start is zero) or "start:duration".
1479 static int find_stopwatch_interval(char *string)
1484 value = strtod(string, &sp);
1486 fprintf(stderr,"Invalid stopwatch timer: %s\n", string);
1490 stopwatch_start = DOUBLE_TO_NANO_ULL(value);
1492 value = strtod(string, &sp);
1493 if (sp == string || *sp != '\0') {
1494 fprintf(stderr,"Invalid stopwatch duration time: %s\n",
1498 } else if (*sp != '\0') {
1499 fprintf(stderr,"Invalid stopwatch start timer: %s\n", string);
1502 stopwatch_end = DOUBLE_TO_NANO_ULL(value);
1503 if (stopwatch_end <= stopwatch_start) {
1504 fprintf(stderr, "Invalid stopwatch interval: %Lu -> %Lu\n",
1505 stopwatch_start, stopwatch_end);
1512 static char usage_str[] = \
1513 "[ -i <input name> ] [-o <output name> [ -s ] [ -t ] [ -q ]\n" \
1514 "[ -w start:stop ] [ -f output format ] [ -F format spec ] [ -v] \n\n" \
1515 "\t-i Input file containing trace data, or '-' for stdin\n" \
1516 "\t-o Output file. If not given, output is stdout\n" \
1517 "\t-b stdin read batching\n" \
1518 "\t-s Show per-program io statistics\n" \
1519 "\t-n Hash processes by name, not pid\n" \
1520 "\t-t Track individual ios. Will tell you the time a request took\n" \
1521 "\t to get queued, to get dispatched, and to get completed\n" \
1522 "\t-q Quiet. Don't display any stats at the end of the trace\n" \
1523 "\t-w Only parse data between the given time interval in seconds.\n" \
1524 "\t If 'start' isn't given, blkparse defaults the start time to 0\n" \
1525 "\t -f Output format. Customize the output format. The format field\n" \
1526 "\t identifies can be found in the documentation\n" \
1527 "\t-F Format specification. Can be found in the documentation\n" \
1528 "\t-v Print program version info\n\n";
1530 static void usage(char *prog)
1532 fprintf(stderr, "Usage: %s %s %s", prog, blkparse_version, usage_str);
1535 int main(int argc, char *argv[])
1539 int per_device_and_cpu_stats = 1;
1541 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
1544 if (!strcmp(optarg, "-") && !pipeline)
1546 else if (resize_devices(optarg) != 0)
1550 output_name = optarg;
1553 rb_batch = atoi(optarg);
1555 rb_batch = RB_BATCH_DEFAULT;
1558 per_process_stats = 1;
1564 per_device_and_cpu_stats = 0;
1567 if (find_stopwatch_interval(optarg) != 0)
1571 set_all_format_specs(optarg);
1574 if (add_format_spec(optarg) != 0)
1578 ppi_hash_by_pid = 1;
1581 printf("%s version %s\n", argv[0], blkparse_version);
1589 while (optind < argc) {
1590 if (!strcmp(argv[optind], "-") && !pipeline)
1592 else if (resize_devices(argv[optind]) != 0)
1597 if (!pipeline && !ndevices) {
1602 memset(&rb_sort_root, 0, sizeof(rb_sort_root));
1603 memset(&rb_track_root, 0, sizeof(rb_track_root));
1605 signal(SIGINT, handle_sigint);
1606 signal(SIGHUP, handle_sigint);
1607 signal(SIGTERM, handle_sigint);
1609 setlocale(LC_NUMERIC, "en_US");
1612 ofp = fdopen(STDOUT_FILENO, "w");
1617 snprintf(ofname, sizeof(ofname) - 1, "%s", output_name);
1618 ofp = fopen(ofname, "w");
1627 ofp_buffer = malloc(4096);
1628 if (setvbuf(ofp, ofp_buffer, mode, 4096)) {
1638 if (per_process_stats)
1639 show_process_stats();
1641 if (per_device_and_cpu_stats)
1642 show_device_and_cpu_stats();