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";
40 #define SECONDS(x) ((unsigned long long)(x) / 1000000000)
41 #define NANO_SECONDS(x) ((unsigned long long)(x) % 1000000000)
42 #define DOUBLE_TO_NANO_ULL(d) ((unsigned long long)((d) * 1000000000))
45 #define MINORMASK ((1U << MINORBITS) - 1)
46 #define MAJOR(dev) ((unsigned int) ((dev) >> MINORBITS))
47 #define MINOR(dev) ((unsigned int) ((dev) & MINORMASK))
49 #define min(a, b) ((a) < (b) ? (a) : (b))
51 #define HEADER "%D %2c %8s %5T.%9t %5p %2a %3d "
54 unsigned long qreads, qwrites, creads, cwrites, mreads, mwrites;
55 unsigned long ireads, iwrites;
56 unsigned long long qread_kb, qwrite_kb, cread_kb, cwrite_kb;
57 unsigned long long iread_kb, iwrite_kb;
58 unsigned long io_unplugs, timer_unplugs;
68 struct io_stats io_stats;
76 unsigned long long events;
77 unsigned long long last_reported_time;
78 unsigned long long last_read_time;
79 struct io_stats io_stats;
80 unsigned long last_sequence;
85 struct per_cpu_info *cpus;
88 struct per_process_info {
91 struct io_stats io_stats;
92 struct per_process_info *hash_next, *list_next;
97 unsigned long long longest_allocation_wait[2];
98 unsigned long long longest_dispatch_wait[2];
99 unsigned long long longest_completion_wait[2];
102 #define PPI_HASH_SHIFT (8)
103 static struct per_process_info *ppi_hash[1 << PPI_HASH_SHIFT];
104 static struct per_process_info *ppi_list;
105 static int ppi_list_entries;
107 #define S_OPTS "i:o:b:stqw:f:F:v"
108 static struct option l_opts[] = {
111 .has_arg = required_argument,
117 .has_arg = required_argument,
123 .has_arg = required_argument,
128 .name = "per program stats",
129 .has_arg = no_argument,
135 .has_arg = no_argument,
141 .has_arg = no_argument,
147 .has_arg = required_argument,
153 .has_arg = required_argument,
158 .name = "format-spec",
159 .has_arg = required_argument,
165 .has_arg = no_argument,
172 * for sorting the displayed output
175 struct blk_io_trace *bit;
176 struct rb_node rb_node;
181 static struct rb_root rb_sort_root;
182 static struct rb_root rb_track_root;
184 static struct trace *trace_list;
189 static struct blk_io_trace *bit_alloc_list;
190 static struct trace *t_alloc_list;
193 * for tracking individual ios
196 struct rb_node rb_node;
201 unsigned long long allocation_time;
202 unsigned long long queue_time;
203 unsigned long long dispatch_time;
204 unsigned long long completion_time;
208 static struct per_dev_info *devices;
209 static char *get_dev_name(struct per_dev_info *, char *, int);
212 static char *output_name;
214 static unsigned long long genesis_time;
215 static unsigned long long last_allowed_time;
216 static unsigned long long stopwatch_start; /* start from zero by default */
217 static unsigned long long stopwatch_end = ULONG_LONG_MAX; /* "infinity" */
219 static int per_process_stats;
220 static int track_ios;
222 #define RB_BATCH_DEFAULT (512)
223 static int rb_batch = RB_BATCH_DEFAULT;
227 #define is_done() (*(volatile int *)(&done))
228 static volatile int done;
230 static inline unsigned long hash_long(unsigned long val)
234 #elif __WORDSIZE == 64
235 val *= 0x9e37fffffffc0001UL;
237 #error unknown word size
240 return val >> (__WORDSIZE - PPI_HASH_SHIFT);
243 static inline void add_process_to_hash(struct per_process_info *ppi)
245 const int hash_idx = hash_long(ppi->pid);
247 ppi->hash_next = ppi_hash[hash_idx];
248 ppi_hash[hash_idx] = ppi;
251 static inline void add_process_to_list(struct per_process_info *ppi)
253 ppi->list_next = ppi_list;
258 static struct per_process_info *find_process_by_pid(__u32 pid)
260 const int hash_idx = hash_long(pid);
261 struct per_process_info *ppi;
263 ppi = ppi_hash[hash_idx];
268 ppi = ppi->hash_next;
274 static inline int trace_rb_insert(struct trace *t)
276 struct rb_node **p = &rb_sort_root.rb_node;
277 struct rb_node *parent = NULL;
282 __t = rb_entry(parent, struct trace, rb_node);
284 if (t->bit->time < __t->bit->time)
286 else if (t->bit->time > __t->bit->time)
288 else if (t->bit->device < __t->bit->device)
290 else if (t->bit->device > __t->bit->device)
292 else if (t->bit->sequence < __t->bit->sequence)
294 else if (t->bit->sequence > __t->bit->sequence)
296 else if (t->bit->device == __t->bit->device) {
298 "sequence alias (%d) on device %d,%d!\n",
300 MAJOR(t->bit->device), MINOR(t->bit->device));
305 rb_link_node(&t->rb_node, parent, p);
306 rb_insert_color(&t->rb_node, &rb_sort_root);
310 static inline int track_rb_insert(struct io_track *iot)
312 struct rb_node **p = &rb_track_root.rb_node;
313 struct rb_node *parent = NULL;
314 struct io_track *__iot;
319 __iot = rb_entry(parent, struct io_track, rb_node);
321 if (iot->device < __iot->device)
323 else if (iot->device > __iot->device)
325 else if (iot->sector < __iot->sector)
327 else if (iot->sector > __iot->sector)
331 "sector alias (%Lu) on device %d,%d!\n",
332 (unsigned long long) iot->sector,
333 MAJOR(iot->device), MINOR(iot->device));
338 rb_link_node(&iot->rb_node, parent, p);
339 rb_insert_color(&iot->rb_node, &rb_track_root);
343 static struct io_track *__find_track(dev_t device, __u64 sector)
345 struct rb_node **p = &rb_track_root.rb_node;
346 struct rb_node *parent = NULL;
347 struct io_track *__iot;
352 __iot = rb_entry(parent, struct io_track, rb_node);
354 if (device < __iot->device)
356 else if (device > __iot->device)
358 else if (sector < __iot->sector)
360 else if (sector > __iot->sector)
369 static struct io_track *find_track(__u32 pid, dev_t device, __u64 sector)
371 struct io_track *iot;
373 iot = __find_track(device, sector);
375 iot = malloc(sizeof(*iot));
377 iot->device = device;
378 iot->sector = sector;
379 track_rb_insert(iot);
385 static void log_track_frontmerge(struct blk_io_trace *t)
387 struct io_track *iot;
392 iot = __find_track(t->device, t->sector + (t->bytes >> 9));
394 fprintf(stderr, "failed to find mergeable event\n");
398 rb_erase(&iot->rb_node, &rb_track_root);
399 iot->sector -= t->bytes >> 9;
400 track_rb_insert(iot);
403 static void log_track_getrq(struct blk_io_trace *t)
405 struct io_track *iot;
410 iot = find_track(t->pid, t->device, t->sector);
411 iot->allocation_time = t->time;
416 * return time between rq allocation and insertion
418 static unsigned long long log_track_insert(struct blk_io_trace *t)
420 unsigned long long elapsed;
421 struct io_track *iot;
426 iot = find_track(t->pid, t->device, t->sector);
427 iot->queue_time = t->time;
428 elapsed = iot->queue_time - iot->allocation_time;
430 if (per_process_stats) {
431 struct per_process_info *ppi = find_process_by_pid(iot->pid);
432 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
434 if (ppi && elapsed > ppi->longest_allocation_wait[w])
435 ppi->longest_allocation_wait[w] = elapsed;
442 * return time between queue and issue
444 static unsigned long long log_track_issue(struct blk_io_trace *t)
446 unsigned long long elapsed;
447 struct io_track *iot;
451 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
454 iot = __find_track(t->device, t->sector);
456 fprintf(stderr, "failed to find issue event\n");
460 iot->dispatch_time = t->time;
461 elapsed = iot->dispatch_time - iot->queue_time;
463 if (per_process_stats) {
464 struct per_process_info *ppi = find_process_by_pid(iot->pid);
465 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
467 if (ppi && elapsed > ppi->longest_dispatch_wait[w])
468 ppi->longest_dispatch_wait[w] = elapsed;
475 * return time between dispatch and complete
477 static unsigned long long log_track_complete(struct blk_io_trace *t)
479 unsigned long long elapsed;
480 struct io_track *iot;
484 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
487 iot = __find_track(t->device, t->sector);
489 fprintf(stderr, "failed to find complete event\n");
493 iot->completion_time = t->time;
494 elapsed = iot->completion_time - iot->dispatch_time;
496 if (per_process_stats) {
497 struct per_process_info *ppi = find_process_by_pid(iot->pid);
498 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
500 if (ppi && elapsed > ppi->longest_completion_wait[w])
501 ppi->longest_completion_wait[w] = elapsed;
505 * kill the trace, we don't need it after completion
507 rb_erase(&iot->rb_node, &rb_track_root);
514 static struct io_stats *find_process_io_stats(__u32 pid, char *name)
516 struct per_process_info *ppi = find_process_by_pid(pid);
519 ppi = malloc(sizeof(*ppi));
520 memset(ppi, 0, sizeof(*ppi));
521 strncpy(ppi->name, name, sizeof(ppi->name));
523 add_process_to_hash(ppi);
524 add_process_to_list(ppi);
527 return &ppi->io_stats;
531 static void resize_cpu_info(struct per_dev_info *pdi, int cpu)
533 struct per_cpu_info *cpus = pdi->cpus;
534 int ncpus = pdi->ncpus;
535 int new_count = cpu + 1;
539 size = new_count * sizeof(struct per_cpu_info);
540 cpus = realloc(cpus, size);
543 fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n",
544 get_dev_name(pdi, name, sizeof(name)), size);
548 new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info));
549 new_space = (new_count - ncpus) * sizeof(struct per_cpu_info);
550 memset(new_start, 0, new_space);
552 pdi->ncpus = new_count;
556 static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu)
558 struct per_cpu_info *pci;
560 if (cpu >= pdi->ncpus)
561 resize_cpu_info(pdi, cpu);
563 pci = &pdi->cpus[cpu];
569 static int resize_devices(char *name)
571 int size = (ndevices + 1) * sizeof(struct per_dev_info);
573 devices = realloc(devices, size);
575 fprintf(stderr, "Out of memory, device %s (%d)\n", name, size);
578 memset(&devices[ndevices], 0, sizeof(struct per_dev_info));
579 devices[ndevices].name = name;
584 static struct per_dev_info *get_dev_info(dev_t id)
586 struct per_dev_info *pdi;
589 for (i = 0; i < ndevices; i++) {
592 if (devices[i].id == id)
596 if (resize_devices(NULL) != 0)
599 pdi = &devices[ndevices - 1];
601 pdi->last_sequence = -1;
602 pdi->last_read_time = 0;
606 static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size)
609 snprintf(buffer, size, "%s", pdi->name);
611 snprintf(buffer, size, "%d,%d", MAJOR(pdi->id), MINOR(pdi->id));
615 static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit)
617 unsigned long long this = bit->time;
618 unsigned long long last = pdi->last_reported_time;
620 pdi->backwards = (this < last) ? 'B' : ' ';
621 pdi->last_reported_time = this;
624 static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t,
629 ios->qwrite_kb += t->bytes >> 10;
632 ios->qread_kb += t->bytes >> 10;
636 static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci,
639 __account_m(&pci->io_stats, t, rw);
641 if (per_process_stats) {
642 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
644 __account_m(ios, t, rw);
648 static inline void __account_queue(struct io_stats *ios, struct blk_io_trace *t,
653 ios->qwrite_kb += t->bytes >> 10;
656 ios->qread_kb += t->bytes >> 10;
660 static inline void account_queue(struct blk_io_trace *t,
661 struct per_cpu_info *pci, int rw)
663 __account_queue(&pci->io_stats, t, rw);
665 if (per_process_stats) {
666 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
668 __account_queue(ios, t, rw);
672 static inline void __account_c(struct io_stats *ios, int rw, unsigned int bytes)
676 ios->cwrite_kb += bytes >> 10;
679 ios->cread_kb += bytes >> 10;
683 static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci,
686 __account_c(&pci->io_stats, rw, bytes);
688 if (per_process_stats) {
689 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
691 __account_c(ios, rw, bytes);
695 static inline void __account_issue(struct io_stats *ios, int rw,
700 ios->iwrite_kb += bytes >> 10;
703 ios->iread_kb += bytes >> 10;
707 static inline void account_issue(struct blk_io_trace *t,
708 struct per_cpu_info *pci, int rw)
710 __account_issue(&pci->io_stats, rw, t->bytes);
712 if (per_process_stats) {
713 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
715 __account_issue(ios, rw, t->bytes);
719 static inline void __account_unplug(struct io_stats *ios, int timer)
722 ios->timer_unplugs++;
727 static inline void account_unplug(struct blk_io_trace *t,
728 struct per_cpu_info *pci, int timer)
730 __account_unplug(&pci->io_stats, timer);
732 if (per_process_stats) {
733 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
735 __account_unplug(ios, timer);
739 #define VALID_SPECS "BCDFGMPQRSTU"
740 static char *override_format[256];
741 static inline int valid_spec(int spec)
743 return strchr(VALID_SPECS, spec) != NULL;
746 static void set_all_format_specs(char *optarg)
750 for (p = VALID_SPECS; *p; p++)
751 if (override_format[(int)(*p)] == NULL)
752 override_format[(int)(*p)] = strdup(optarg);
755 static int add_format_spec(char *optarg)
757 int spec = optarg[0];
759 if (!valid_spec(spec)) {
760 fprintf(stderr,"Bad format specifier %c\n", spec);
763 if (optarg[1] != ',') {
764 fprintf(stderr,"Bad format specifier - need ',' %s\n", optarg);
768 if (*optarg == '\0') {
769 fprintf(stderr,"Bad format specifier - need fmt %s\n", optarg);
774 * Set both merges (front and back)
777 override_format['B'] = strdup(optarg);
778 override_format['M'] = strdup(optarg);
780 override_format[spec] = strdup(optarg);
785 static void print_field(char *act, struct per_cpu_info *pci,
786 struct blk_io_trace *t, unsigned long long elapsed,
787 int pdu_len, unsigned char *pdu_buf, char field,
788 int minus, int has_w, int width)
794 sprintf(format, "%%-%d", width);
796 sprintf(format, "%%%d", width);
798 sprintf(format, "%%");
802 fprintf(ofp, strcat(format, "s"), act);
805 fprintf(ofp, strcat(format, "d"), pci->cpu);
808 fprintf(ofp, strcat(format, "s"), t->comm);
813 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
814 int b = t->action & BLK_TC_ACT(BLK_TC_BARRIER);
815 int s = t->action & BLK_TC_ACT(BLK_TC_SYNC);
825 fprintf(ofp, strcat(format, "s"), rwbs);
828 case 'D': /* format width ignored */
829 fprintf(ofp,"%3d,%-3d", MAJOR(t->device), MINOR(t->device));
832 fprintf(ofp, strcat(format, "d"), t->error);
835 fprintf(ofp, strcat(format, "d"), MAJOR(t->device));
838 fprintf(ofp, strcat(format, "d"), MINOR(t->device));
841 fprintf(ofp, strcat(format, "u"), t->bytes >> 9);
844 fprintf(ofp, strcat(format, "u"), t->pid);
846 case 'P': /* format width ignored */
847 if ((pdu_len > 0) && (pdu_buf != NULL)) {
849 unsigned char *p = pdu_buf;
850 for (i = 0; i < pdu_len; i++) {
854 fprintf(ofp, "%02x", *p++);
859 fprintf(ofp, strcat(format, "ld"), t->sequence);
862 fprintf(ofp, strcat(format, "lu"), t->sector);
865 sprintf(format, "%%0%dlu", has_w ? width : 9);
866 fprintf(ofp, format, NANO_SECONDS(t->time));
869 fprintf(ofp, strcat(format, "d"), SECONDS(t->time));
872 if (elapsed == -1ULL) {
873 fprintf(stderr, "Expecting elapsed value\n");
876 fprintf(ofp, strcat(format, "llu"), elapsed / 1000);
879 __u64 *depth = (__u64 *) ((char *) t + sizeof(*t));
880 fprintf(ofp, strcat(format, "u"),
881 (unsigned int) be64_to_cpu(*depth));
885 fprintf(ofp,strcat(format, "c"), field);
890 static char *parse_field(char *act, struct per_cpu_info *pci,
891 struct blk_io_trace *t, unsigned long long elapsed,
892 int pdu_len, unsigned char *pdu_buf,
898 char *p = master_format;
907 width = (width * 10) + (*p++ - '0');
908 } while ((*p) && (isdigit(*p)));
911 print_field(act, pci, t, elapsed, pdu_len, pdu_buf, *p++,
912 minus, has_w, width);
917 static char *fmt_select(int fmt_spec, struct blk_io_trace *t,
918 unsigned long long elapsed)
921 static char scratch_format[1024];
923 if (override_format[fmt_spec] != NULL)
924 return override_format[fmt_spec];
927 case 'C': /* Complete */
928 if (t->action & BLK_TC_ACT(BLK_TC_PC))
929 strcpy(scratch_format, HEADER "(%P) [%e]\n");
931 if (elapsed != -1ULL) {
932 strcpy(scratch_format,
933 HEADER "%S +%n (%8u) [%e]\n");
935 strcpy(scratch_format, HEADER "%S + %n [%e]\n");
937 fmt = scratch_format;
940 case 'D': /* Issue */
941 if (t->action & BLK_TC_ACT(BLK_TC_PC))
942 strcpy(scratch_format, HEADER "%n (%P) [%C]\n");
944 if (elapsed != -1ULL) {
945 strcpy(scratch_format,
946 HEADER "%S + %n (%8u) [%C]\n");
948 strcpy(scratch_format, HEADER "%S + %n [%C]\n");
950 fmt = scratch_format;
953 case 'I': /* Insert */
954 if (t->action & BLK_TC_ACT(BLK_TC_PC))
955 strcpy(scratch_format, HEADER "%n (%P) [%C]\n");
957 if (elapsed != -1ULL) {
958 strcpy(scratch_format,
959 HEADER "%S + %n (%8u) [%C]\n");
961 strcpy(scratch_format, HEADER "%S + %n [%C]\n");
963 fmt = scratch_format;
966 case 'Q': /* Queue */
967 case 'W': /* Bounce */
968 if (elapsed != -1ULL) {
969 strcpy(scratch_format, HEADER "%S + %n (%8u) [%C]\n");
971 strcpy(scratch_format, HEADER "%S + %n [%C]\n");
972 fmt = scratch_format;
975 case 'B': /* Back merge */
976 case 'F': /* Front merge */
977 case 'M': /* Front or back merge */
978 fmt = HEADER "%S + %n [%C]\n";
982 fmt = HEADER "[%C]\n";
985 case 'G': /* Get request */
986 case 'S': /* Sleep request */
987 fmt = HEADER "%S + %n [%C]\n";
990 case 'U': /* Unplug IO */
991 case 'T': /* Unplug timer */
992 fmt = HEADER "[%C] %U\n";
995 case 'X': /* Split */
996 strcpy(scratch_format, HEADER "%S / %U [%C]\n");
997 fmt = scratch_format;
1001 fprintf(stderr,"FATAL: Invalid format spec %c\n", fmt_spec);
1009 static void process_fmt(char *act, struct per_cpu_info *pci,
1010 struct blk_io_trace *t, unsigned long long elapsed,
1011 int pdu_len, unsigned char *pdu_buf)
1013 char *p = fmt_select(act[0], t, elapsed);
1017 case '%': /* Field specifier */
1020 fprintf(ofp, "%c", *p++);
1022 fprintf(ofp, "%c", '%');
1024 p = parse_field(act, pci, t, elapsed,
1025 pdu_len, pdu_buf, p);
1027 case '\\': { /* escape */
1029 case 'b': fprintf(ofp, "\b"); break;
1030 case 'n': fprintf(ofp, "\n"); break;
1031 case 'r': fprintf(ofp, "\r"); break;
1032 case 't': fprintf(ofp, "\t"); break;
1035 "Invalid escape char in format %c\n",
1044 fprintf(ofp, "%c", *p++);
1050 static void log_complete(struct per_cpu_info *pci, struct blk_io_trace *t,
1053 process_fmt(act, pci, t, log_track_complete(t), 0, NULL);
1056 static void log_insert(struct per_cpu_info *pci, struct blk_io_trace *t,
1059 process_fmt(act, pci, t, log_track_insert(t), 0, NULL);
1062 static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t,
1065 process_fmt(act, pci, t, -1, 0, NULL);
1068 static void log_issue(struct per_cpu_info *pci, struct blk_io_trace *t,
1071 process_fmt(act, pci, t, log_track_issue(t), 0, NULL);
1074 static void log_merge(struct per_cpu_info *pci, struct blk_io_trace *t,
1078 log_track_frontmerge(t);
1080 process_fmt(act, pci, t, -1ULL, 0, NULL);
1083 static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t,
1086 process_fmt(act, pci, t, -1ULL, 0, NULL);
1089 static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t,
1092 process_fmt(act, pci, t, -1ULL, 0, NULL);
1095 static void log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t,
1098 process_fmt(act, pci, t, -1ULL, 0, NULL);
1101 static void log_split(struct per_cpu_info *pci, struct blk_io_trace *t,
1104 process_fmt(act, pci, t, -1ULL, 0, NULL);
1107 static void log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act)
1109 unsigned char *buf = (unsigned char *) t + sizeof(*t);
1111 process_fmt(act, pci, t, -1ULL, t->pdu_len, buf);
1114 static void dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci)
1116 int act = t->action & 0xffff;
1119 case __BLK_TA_QUEUE:
1120 log_generic(pci, t, "Q");
1122 case __BLK_TA_GETRQ:
1123 log_generic(pci, t, "G");
1125 case __BLK_TA_SLEEPRQ:
1126 log_generic(pci, t, "S");
1128 case __BLK_TA_REQUEUE:
1129 log_generic(pci, t, "R");
1131 case __BLK_TA_ISSUE:
1132 log_pc(pci, t, "D");
1134 case __BLK_TA_COMPLETE:
1135 log_pc(pci, t, "C");
1137 case __BLK_TA_INSERT:
1138 log_pc(pci, t, "I");
1141 fprintf(stderr, "Bad pc action %x\n", act);
1146 static void dump_trace_fs(struct blk_io_trace *t, struct per_cpu_info *pci)
1148 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
1149 int act = t->action & 0xffff;
1152 case __BLK_TA_QUEUE:
1153 account_queue(t, pci, w);
1154 log_queue(pci, t, "Q");
1156 case __BLK_TA_INSERT:
1157 log_insert(pci, t, "I");
1159 case __BLK_TA_BACKMERGE:
1160 account_m(t, pci, w);
1161 log_merge(pci, t, "M");
1163 case __BLK_TA_FRONTMERGE:
1164 account_m(t, pci, w);
1165 log_merge(pci, t, "F");
1167 case __BLK_TA_GETRQ:
1169 log_generic(pci, t, "G");
1171 case __BLK_TA_SLEEPRQ:
1172 log_generic(pci, t, "S");
1174 case __BLK_TA_REQUEUE:
1175 account_c(t, pci, w, -t->bytes);
1176 log_queue(pci, t, "R");
1178 case __BLK_TA_ISSUE:
1179 account_issue(t, pci, w);
1180 log_issue(pci, t, "D");
1182 case __BLK_TA_COMPLETE:
1183 account_c(t, pci, w, t->bytes);
1184 log_complete(pci, t, "C");
1187 log_action(pci, t, "P");
1189 case __BLK_TA_UNPLUG_IO:
1190 account_unplug(t, pci, 0);
1191 log_unplug(pci, t, "U");
1193 case __BLK_TA_UNPLUG_TIMER:
1194 account_unplug(t, pci, 1);
1195 log_unplug(pci, t, "UT");
1197 case __BLK_TA_SPLIT:
1198 log_split(pci, t, "X");
1200 case __BLK_TA_BOUNCE:
1201 log_generic(pci, t, "B");
1204 fprintf(stderr, "Bad fs action %x\n", t->action);
1209 static void dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
1210 struct per_dev_info *pdi)
1212 if (t->action & BLK_TC_ACT(BLK_TC_PC))
1213 dump_trace_pc(t, pci);
1215 dump_trace_fs(t, pci);
1220 static void dump_io_stats(struct io_stats *ios, char *msg)
1222 fprintf(ofp, "%s\n", msg);
1224 fprintf(ofp, " Reads Queued: %'8lu, %'8LuKiB\t", ios->qreads, ios->qread_kb);
1225 fprintf(ofp, " Writes Queued: %'8lu, %'8LuKiB\n", ios->qwrites,ios->qwrite_kb);
1227 fprintf(ofp, " Read Dispatches: %'8lu, %'8LuKiB\t", ios->ireads, ios->iread_kb);
1228 fprintf(ofp, " Write Dispatches: %'8lu, %'8LuKiB\n", ios->iwrites,ios->iwrite_kb);
1229 fprintf(ofp, " Reads Completed: %'8lu, %'8LuKiB\t", ios->creads, ios->cread_kb);
1230 fprintf(ofp, " Writes Completed: %'8lu, %'8LuKiB\n", ios->cwrites,ios->cwrite_kb);
1231 fprintf(ofp, " Read Merges: %'8lu%8c\t", ios->mreads, ' ');
1232 fprintf(ofp, " Write Merges: %'8lu\n", ios->mwrites);
1233 fprintf(ofp, " IO unplugs: %'8lu%8c\t", ios->io_unplugs, ' ');
1234 fprintf(ofp, " Timer unplugs: %'8lu\n", ios->timer_unplugs);
1237 static void dump_wait_stats(struct per_process_info *ppi)
1239 unsigned long rawait = ppi->longest_allocation_wait[0] / 1000;
1240 unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000;
1241 unsigned long rcwait = ppi->longest_completion_wait[0] / 1000;
1242 unsigned long wawait = ppi->longest_allocation_wait[1] / 1000;
1243 unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000;
1244 unsigned long wcwait = ppi->longest_completion_wait[1] / 1000;
1246 fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' ');
1247 fprintf(ofp, " Allocation wait: %'8lu\n", wawait);
1248 fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' ');
1249 fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait);
1250 fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' ');
1251 fprintf(ofp, " Completion wait: %'8lu\n", wcwait);
1254 static int ppi_name_compare(const void *p1, const void *p2)
1256 struct per_process_info *ppi1 = *((struct per_process_info **) p1);
1257 struct per_process_info *ppi2 = *((struct per_process_info **) p2);
1260 res = strverscmp(ppi1->name, ppi2->name);
1262 res = ppi1->pid > ppi2->pid;
1267 static void sort_process_list(void)
1269 struct per_process_info **ppis;
1270 struct per_process_info *ppi;
1273 ppis = malloc(ppi_list_entries * sizeof(struct per_process_info *));
1278 ppi = ppi->list_next;
1281 qsort(ppis, ppi_list_entries, sizeof(ppi), ppi_name_compare);
1283 i = ppi_list_entries - 1;
1288 ppi->list_next = ppi_list;
1296 static void show_process_stats(void)
1298 struct per_process_info *ppi;
1300 sort_process_list();
1306 snprintf(name, sizeof(name)-1, "%s (%u)", ppi->name, ppi->pid);
1307 dump_io_stats(&ppi->io_stats, name);
1308 dump_wait_stats(ppi);
1309 ppi = ppi->list_next;
1315 static void show_device_and_cpu_stats(void)
1317 struct per_dev_info *pdi;
1318 struct per_cpu_info *pci;
1319 struct io_stats total, *ios;
1320 int i, j, pci_events;
1321 char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3];
1324 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
1326 memset(&total, 0, sizeof(total));
1332 for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) {
1336 ios = &pci->io_stats;
1337 total.qreads += ios->qreads;
1338 total.qwrites += ios->qwrites;
1339 total.creads += ios->creads;
1340 total.cwrites += ios->cwrites;
1341 total.mreads += ios->mreads;
1342 total.mwrites += ios->mwrites;
1343 total.ireads += ios->ireads;
1344 total.iwrites += ios->iwrites;
1345 total.qread_kb += ios->qread_kb;
1346 total.qwrite_kb += ios->qwrite_kb;
1347 total.cread_kb += ios->cread_kb;
1348 total.cwrite_kb += ios->cwrite_kb;
1349 total.iread_kb += ios->iread_kb;
1350 total.iwrite_kb += ios->iwrite_kb;
1351 total.timer_unplugs += ios->timer_unplugs;
1352 total.io_unplugs += ios->io_unplugs;
1354 snprintf(line, sizeof(line) - 1, "CPU%d (%s):",
1355 j, get_dev_name(pdi, name, sizeof(name)));
1356 dump_io_stats(ios, line);
1360 if (pci_events > 1) {
1362 snprintf(line, sizeof(line) - 1, "Total (%s):",
1363 get_dev_name(pdi, name, sizeof(name)));
1364 dump_io_stats(&total, line);
1367 fprintf(ofp, "\nEvents (%s): %'Lu entries, %'lu skips\n",
1368 get_dev_name(pdi, line, sizeof(line)), pdi->events,
1373 static int sort_entries(void)
1378 while ((t = trace_list) != NULL) {
1379 trace_list = t->next;
1381 if (verify_trace(t->bit))
1383 if (trace_rb_insert(t))
1392 * struct trace and blktrace allocation cache, we do potentially
1393 * millions of mallocs for these structures while only using at most
1394 * a few thousand at the time
1396 static inline void t_free(struct trace *t)
1398 t->next = t_alloc_list;
1402 static inline struct trace *t_alloc(void)
1404 struct trace *t = t_alloc_list;
1407 t_alloc_list = t->next;
1411 return malloc(sizeof(*t));
1414 static inline void bit_free(struct blk_io_trace *bit)
1417 * abuse a 64-bit field for a next pointer for the free item
1419 bit->time = (__u64) (unsigned long) bit_alloc_list;
1420 bit_alloc_list = (struct blk_io_trace *) bit;
1423 static inline struct blk_io_trace *bit_alloc(void)
1425 struct blk_io_trace *bit = bit_alloc_list;
1428 bit_alloc_list = (struct blk_io_trace *) (unsigned long) \
1433 return malloc(sizeof(*bit));
1436 static void show_entries_rb(void)
1438 struct per_dev_info *pdi = NULL;
1439 struct per_cpu_info *pci = NULL;
1440 struct blk_io_trace *bit;
1444 while ((n = rb_first(&rb_sort_root)) != NULL) {
1449 t = rb_entry(n, struct trace, rb_node);
1452 if (!pdi || pdi->id != bit->device)
1453 pdi = get_dev_info(bit->device);
1456 fprintf(stderr, "Unknown device ID? (%d,%d)\n",
1457 MAJOR(bit->device), MINOR(bit->device));
1461 if (bit->cpu > pdi->ncpus) {
1462 fprintf(stderr, "Unknown CPU ID? (%d, device %d,%d)\n",
1463 bit->cpu, MAJOR(bit->device),
1464 MINOR(bit->device));
1469 * back off displaying more info if we are out of sync
1470 * on SMP systems. to prevent stalling on lost events,
1471 * only allow an event to skip us a few times
1473 if (bit->sequence != (pdi->last_sequence + 1)
1474 && pdi->last_sequence != -1) {
1475 if (t->skipped < 5) {
1479 fprintf(stderr, "skipping from %lu to %u\n", pdi->last_sequence, bit->sequence);
1484 pdi->last_sequence = bit->sequence;
1486 if (bit->time >= stopwatch_end || bit->time > last_allowed_time)
1489 if (bit->time >= stopwatch_start) {
1490 check_time(pdi, bit);
1492 if (!pci || pci->cpu != bit->cpu)
1493 pci = get_cpu_info(pdi, bit->cpu);
1495 dump_trace(bit, pci, pdi);
1498 rb_erase(&t->rb_node, &rb_sort_root);
1504 static int read_data(int fd, void *buffer, int bytes, int block)
1506 int ret, bytes_left, fl;
1509 fl = fcntl(fd, F_GETFL);
1512 fcntl(fd, F_SETFL, fl | O_NONBLOCK);
1514 fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
1518 while (bytes_left > 0) {
1519 ret = read(fd, p, bytes_left);
1523 if (errno != EAGAIN)
1535 static int read_events(int fd)
1537 struct per_dev_info *pdi = NULL;
1540 while (!is_done() && events < rb_batch) {
1541 struct blk_io_trace *bit;
1548 if (read_data(fd, bit, sizeof(*bit), !events))
1551 magic = be32_to_cpu(bit->magic);
1552 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
1553 fprintf(stderr, "Bad magic %x\n", magic);
1557 pdu_len = be16_to_cpu(bit->pdu_len);
1559 void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
1561 if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1))
1568 memset(t, 0, sizeof(*t));
1572 t->next = trace_list;
1575 if (genesis_time == 0 || t->bit->time < genesis_time)
1576 genesis_time = t->bit->time;
1578 bit->time -= genesis_time;
1580 if (!pdi || pdi->id != bit->device)
1581 pdi = get_dev_info(bit->device);
1583 if (bit->time > pdi->last_read_time)
1584 pdi->last_read_time = bit->time;
1592 static int do_file(void)
1594 struct per_cpu_info *pci;
1595 struct per_dev_info *pdi;
1596 int i, j, events, events_added;
1599 * first prepare all files for reading
1601 for (i = 0; i < ndevices; i++) {
1604 pdi->last_sequence = -1;
1609 pci = get_cpu_info(pdi, j);
1613 snprintf(pci->fname, sizeof(pci->fname)-1,
1614 "%s.blktrace.%d", pdi->name, pci->cpu);
1615 if (stat(pci->fname, &st) < 0)
1618 pci->fd = open(pci->fname, O_RDONLY);
1625 printf("Input file %s added\n", pci->fname);
1631 * now loop over the files reading in the data
1635 last_allowed_time = -1ULL;
1637 for (i = 0; i < ndevices; i++) {
1640 for (j = 0; j < pdi->nfiles; j++) {
1642 pci = get_cpu_info(pdi, j);
1647 events = read_events(pci->fd);
1654 if (pdi->last_read_time < last_allowed_time)
1655 last_allowed_time = pdi->last_read_time;
1657 events_added += events;
1661 if (sort_entries() == -1)
1666 } while (events_added);
1671 static int do_stdin(void)
1675 fd = dup(STDIN_FILENO);
1679 events = read_events(fd);
1683 if (sort_entries() == -1)
1693 static void flush_output(void)
1698 static void handle_sigint(int sig)
1705 * Extract start and duration times from a string, allowing
1706 * us to specify a time interval of interest within a trace.
1707 * Format: "duration" (start is zero) or "start:duration".
1709 static int find_stopwatch_interval(char *string)
1714 value = strtod(string, &sp);
1716 fprintf(stderr,"Invalid stopwatch timer: %s\n", string);
1720 stopwatch_start = DOUBLE_TO_NANO_ULL(value);
1722 value = strtod(string, &sp);
1723 if (sp == string || *sp != '\0') {
1724 fprintf(stderr,"Invalid stopwatch duration time: %s\n",
1728 } else if (*sp != '\0') {
1729 fprintf(stderr,"Invalid stopwatch start timer: %s\n", string);
1732 stopwatch_end = DOUBLE_TO_NANO_ULL(value);
1733 if (stopwatch_end <= stopwatch_start) {
1734 fprintf(stderr, "Invalid stopwatch interval: %Lu -> %Lu\n",
1735 stopwatch_start, stopwatch_end);
1742 static char usage_str[] = \
1743 "[ -i <input name> ] [-o <output name> [ -s ] [ -t ] [ -q ]\n" \
1744 "[ -w start:stop ] [ -f output format ] [ -F format spec ] [ -v] \n\n" \
1745 "\t-i Input file containing trace data, or '-' for stdin\n" \
1746 "\t-o Output file. If not given, output is stdout\n" \
1747 "\t-b stdin read batching\n" \
1748 "\t-s Show per-program io statistics\n" \
1749 "\t-t Track individual ios. Will tell you the time a request took\n" \
1750 "\t to get queued, to get dispatched, and to get completed\n" \
1751 "\t-q Quiet. Don't display any stats at the end of the trace\n" \
1752 "\t-w Only parse data between the given time interval in seconds.\n" \
1753 "\t If 'start' isn't given, blkparse defaults the start time to 0\n" \
1754 "\t -f Output format. Customize the output format. The format field\n" \
1755 "\t identifies can be found in the documentation\n" \
1756 "\t-F Format specification. Can be found in the documentation\n" \
1757 "\t-v Print program version info\n\n";
1759 static void usage(char *prog)
1761 fprintf(stderr, "Usage: %s %s %s", prog, blkparse_version, usage_str);
1764 int main(int argc, char *argv[])
1768 int per_device_and_cpu_stats = 1;
1770 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
1773 if (!strcmp(optarg, "-") && !pipeline)
1775 else if (resize_devices(optarg) != 0)
1779 output_name = optarg;
1782 rb_batch = atoi(optarg);
1784 rb_batch = RB_BATCH_DEFAULT;
1787 per_process_stats = 1;
1793 per_device_and_cpu_stats = 0;
1796 if (find_stopwatch_interval(optarg) != 0)
1800 set_all_format_specs(optarg);
1803 if (add_format_spec(optarg) != 0)
1807 printf("%s version %s\n", argv[0], blkparse_version);
1815 while (optind < argc) {
1816 if (!strcmp(argv[optind], "-") && !pipeline)
1818 else if (resize_devices(argv[optind]) != 0)
1823 if (!pipeline && !ndevices) {
1828 memset(&rb_sort_root, 0, sizeof(rb_sort_root));
1829 memset(&rb_track_root, 0, sizeof(rb_track_root));
1831 signal(SIGINT, handle_sigint);
1832 signal(SIGHUP, handle_sigint);
1833 signal(SIGTERM, handle_sigint);
1835 setlocale(LC_NUMERIC, "en_US");
1838 ofp = fdopen(STDOUT_FILENO, "w");
1843 snprintf(ofname, sizeof(ofname) - 1, "%s", output_name);
1844 ofp = fopen(ofname, "w");
1853 ofp_buffer = malloc(4096);
1854 if (setvbuf(ofp, ofp_buffer, mode, 4096)) {
1864 if (per_process_stats)
1865 show_process_stats();
1867 if (per_device_and_cpu_stats)
1868 show_device_and_cpu_stats();