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 struct io_stats io_stats;
79 unsigned long last_sequence;
83 struct per_cpu_info *cpus;
86 struct per_process_info {
89 struct io_stats io_stats;
90 struct per_process_info *hash_next, *list_next;
95 unsigned long long longest_allocation_wait[2];
96 unsigned long long longest_dispatch_wait[2];
97 unsigned long long longest_completion_wait[2];
100 #define PPI_HASH_SHIFT (8)
101 static struct per_process_info *ppi_hash[1 << PPI_HASH_SHIFT];
102 static struct per_process_info *ppi_list;
103 static int ppi_list_entries;
105 #define S_OPTS "i:o:b:stqw:f:F:v"
106 static struct option l_opts[] = {
109 .has_arg = required_argument,
115 .has_arg = required_argument,
121 .has_arg = required_argument,
126 .name = "per program stats",
127 .has_arg = no_argument,
133 .has_arg = no_argument,
139 .has_arg = no_argument,
145 .has_arg = required_argument,
151 .has_arg = required_argument,
156 .name = "format-spec",
157 .has_arg = required_argument,
163 .has_arg = no_argument,
170 * for sorting the displayed output
173 struct blk_io_trace *bit;
174 struct rb_node rb_node;
179 static struct rb_root rb_sort_root;
180 static struct rb_root rb_track_root;
182 static struct trace *trace_list;
187 static struct blk_io_trace *bit_alloc_list;
188 static struct trace *t_alloc_list;
191 * for tracking individual ios
194 struct rb_node rb_node;
199 unsigned long long allocation_time;
200 unsigned long long queue_time;
201 unsigned long long dispatch_time;
202 unsigned long long completion_time;
206 static struct per_dev_info *devices;
207 static char *get_dev_name(struct per_dev_info *, char *, int);
210 static char *output_name;
212 static unsigned long long genesis_time;
213 static unsigned long long stopwatch_start; /* start from zero by default */
214 static unsigned long long stopwatch_end = ULONG_LONG_MAX; /* "infinity" */
216 static int per_process_stats;
217 static int track_ios;
219 #define RB_BATCH_DEFAULT (512)
220 static int rb_batch = RB_BATCH_DEFAULT;
224 #define is_done() (*(volatile int *)(&done))
225 static volatile int done;
227 static inline unsigned long hash_long(unsigned long val)
231 #elif __WORDSIZE == 64
232 val *= 0x9e37fffffffc0001UL;
234 #error unknown word size
237 return val >> (__WORDSIZE - PPI_HASH_SHIFT);
240 static inline void add_process_to_hash(struct per_process_info *ppi)
242 const int hash_idx = hash_long(ppi->pid);
244 ppi->hash_next = ppi_hash[hash_idx];
245 ppi_hash[hash_idx] = ppi;
248 static inline void add_process_to_list(struct per_process_info *ppi)
250 ppi->list_next = ppi_list;
255 static struct per_process_info *find_process_by_pid(__u32 pid)
257 const int hash_idx = hash_long(pid);
258 struct per_process_info *ppi;
260 ppi = ppi_hash[hash_idx];
265 ppi = ppi->hash_next;
271 static inline int trace_rb_insert(struct trace *t)
273 struct rb_node **p = &rb_sort_root.rb_node;
274 struct rb_node *parent = NULL;
277 if (genesis_time == 0 || t->bit->time < genesis_time)
278 genesis_time = t->bit->time;
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;
605 static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size)
608 snprintf(buffer, size, "%s", pdi->name);
610 snprintf(buffer, size, "%d,%d", MAJOR(pdi->id), MINOR(pdi->id));
614 static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit)
616 unsigned long long this = bit->time;
617 unsigned long long last = pdi->last_reported_time;
619 pdi->backwards = (this < last) ? 'B' : ' ';
620 pdi->last_reported_time = this;
623 static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t,
628 ios->qwrite_kb += t->bytes >> 10;
631 ios->qread_kb += t->bytes >> 10;
635 static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci,
638 __account_m(&pci->io_stats, t, rw);
640 if (per_process_stats) {
641 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
643 __account_m(ios, t, rw);
647 static inline void __account_queue(struct io_stats *ios, struct blk_io_trace *t,
652 ios->qwrite_kb += t->bytes >> 10;
655 ios->qread_kb += t->bytes >> 10;
659 static inline void account_queue(struct blk_io_trace *t,
660 struct per_cpu_info *pci, int rw)
662 __account_queue(&pci->io_stats, t, rw);
664 if (per_process_stats) {
665 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
667 __account_queue(ios, t, rw);
671 static inline void __account_c(struct io_stats *ios, int rw, unsigned int bytes)
675 ios->cwrite_kb += bytes >> 10;
678 ios->cread_kb += bytes >> 10;
682 static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci,
685 __account_c(&pci->io_stats, rw, bytes);
687 if (per_process_stats) {
688 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
690 __account_c(ios, rw, bytes);
694 static inline void __account_issue(struct io_stats *ios, int rw,
699 ios->iwrite_kb += bytes >> 10;
702 ios->iread_kb += bytes >> 10;
706 static inline void account_issue(struct blk_io_trace *t,
707 struct per_cpu_info *pci, int rw)
709 __account_issue(&pci->io_stats, rw, t->bytes);
711 if (per_process_stats) {
712 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
714 __account_issue(ios, rw, t->bytes);
718 static inline void __account_unplug(struct io_stats *ios, int timer)
721 ios->timer_unplugs++;
726 static inline void account_unplug(struct blk_io_trace *t,
727 struct per_cpu_info *pci, int timer)
729 __account_unplug(&pci->io_stats, timer);
731 if (per_process_stats) {
732 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
734 __account_unplug(ios, timer);
738 #define VALID_SPECS "BCDFGMPQRSTU"
739 static char *override_format[256];
740 static inline int valid_spec(int spec)
742 return strchr(VALID_SPECS, spec) != NULL;
745 static void set_all_format_specs(char *optarg)
749 for (p = VALID_SPECS; *p; p++)
750 if (override_format[(int)(*p)] == NULL)
751 override_format[(int)(*p)] = strdup(optarg);
754 static int add_format_spec(char *optarg)
756 int spec = optarg[0];
758 if (!valid_spec(spec)) {
759 fprintf(stderr,"Bad format specifier %c\n", spec);
762 if (optarg[1] != ',') {
763 fprintf(stderr,"Bad format specifier - need ',' %s\n", optarg);
767 if (*optarg == '\0') {
768 fprintf(stderr,"Bad format specifier - need fmt %s\n", optarg);
773 * Set both merges (front and back)
776 override_format['B'] = strdup(optarg);
777 override_format['M'] = strdup(optarg);
779 override_format[spec] = strdup(optarg);
784 static void print_field(char *act, struct per_cpu_info *pci,
785 struct blk_io_trace *t, unsigned long long elapsed,
786 int pdu_len, unsigned char *pdu_buf, char field,
787 int minus, int has_w, int width)
793 sprintf(format, "%%-%d", width);
795 sprintf(format, "%%%d", width);
797 sprintf(format, "%%");
801 fprintf(ofp, strcat(format, "s"), act);
804 fprintf(ofp, strcat(format, "d"), pci->cpu);
807 fprintf(ofp, strcat(format, "s"), t->comm);
812 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
813 int b = t->action & BLK_TC_ACT(BLK_TC_BARRIER);
814 int s = t->action & BLK_TC_ACT(BLK_TC_SYNC);
824 fprintf(ofp, strcat(format, "s"), rwbs);
827 case 'D': /* format width ignored */
828 fprintf(ofp,"%3d,%-3d", MAJOR(t->device), MINOR(t->device));
831 fprintf(ofp, strcat(format, "d"), t->error);
834 fprintf(ofp, strcat(format, "d"), MAJOR(t->device));
837 fprintf(ofp, strcat(format, "d"), MINOR(t->device));
840 fprintf(ofp, strcat(format, "u"), t->bytes >> 9);
843 fprintf(ofp, strcat(format, "u"), t->pid);
845 case 'P': /* format width ignored */
846 if ((pdu_len > 0) && (pdu_buf != NULL)) {
848 unsigned char *p = pdu_buf;
849 for (i = 0; i < pdu_len; i++) {
853 fprintf(ofp, "%02x", *p++);
858 fprintf(ofp, strcat(format, "ld"), t->sequence);
861 fprintf(ofp, strcat(format, "lu"), t->sector);
864 sprintf(format, "%%0%dlu", has_w ? width : 9);
865 fprintf(ofp, format, NANO_SECONDS(t->time));
868 fprintf(ofp, strcat(format, "d"), SECONDS(t->time));
871 if (elapsed == -1ULL) {
872 fprintf(stderr, "Expecting elapsed value\n");
875 fprintf(ofp, strcat(format, "llu"), elapsed / 1000);
878 __u64 *depth = (__u64 *) ((char *) t + sizeof(*t));
879 fprintf(ofp, strcat(format, "u"),
880 (unsigned int) be64_to_cpu(*depth));
884 fprintf(ofp,strcat(format, "c"), field);
889 static char *parse_field(char *act, struct per_cpu_info *pci,
890 struct blk_io_trace *t, unsigned long long elapsed,
891 int pdu_len, unsigned char *pdu_buf,
897 char *p = master_format;
906 width = (width * 10) + (*p++ - '0');
907 } while ((*p) && (isdigit(*p)));
910 print_field(act, pci, t, elapsed, pdu_len, pdu_buf, *p++,
911 minus, has_w, width);
916 static char *fmt_select(int fmt_spec, struct blk_io_trace *t,
917 unsigned long long elapsed)
920 char scratch_format[1024];
922 if (override_format[fmt_spec] != NULL)
923 return override_format[fmt_spec];
926 case 'C': /* Complete */
927 if (t->action & BLK_TC_ACT(BLK_TC_PC))
928 strcpy(scratch_format, HEADER "(%P) [%e]\n");
930 if (elapsed != -1ULL) {
931 strcpy(scratch_format,
932 HEADER "%S +%n (%8u) [%e]\n");
934 strcpy(scratch_format, HEADER "%S + %n [%e]\n");
936 fmt = scratch_format;
939 case 'D': /* Issue */
940 if (t->action & BLK_TC_ACT(BLK_TC_PC))
941 strcpy(scratch_format, HEADER "%n (%P) [%C]\n");
943 if (elapsed != -1ULL) {
944 strcpy(scratch_format,
945 HEADER "%S + %n (%8u) [%C]\n");
947 strcpy(scratch_format, HEADER "%S + %n [%C]\n");
949 fmt = scratch_format;
952 case 'I': /* Insert */
953 if (t->action & BLK_TC_ACT(BLK_TC_PC))
954 strcpy(scratch_format, HEADER "%n (%P) [%C]\n");
956 if (elapsed != -1ULL) {
957 strcpy(scratch_format,
958 HEADER "%S + %n (%8u) [%C]\n");
960 strcpy(scratch_format, HEADER "%S + %n [%C]\n");
962 fmt = scratch_format;
965 case 'Q': /* Queue */
966 case 'W': /* Bounce */
967 if (elapsed != -1ULL) {
968 strcpy(scratch_format, HEADER "%S + %n (%8u) [%C]\n");
970 strcpy(scratch_format, HEADER "%S + %n [%C]\n");
971 fmt = scratch_format;
974 case 'B': /* Back merge */
975 case 'F': /* Front merge */
976 case 'M': /* Front or back merge */
977 fmt = HEADER "%S + %n [%C]\n";
981 fmt = HEADER "[%C]\n";
984 case 'G': /* Get request */
985 case 'S': /* Sleep request */
986 fmt = HEADER "%S + %n [%C]\n";
989 case 'U': /* Unplug IO */
990 case 'T': /* Unplug timer */
991 fmt = HEADER "[%C] %U\n";
994 case 'X': /* Split */
995 strcpy(scratch_format, HEADER "%S / %U [%C]\n");
996 fmt = scratch_format;
1000 fprintf(stderr,"FATAL: Invalid format spec %c\n", fmt_spec);
1008 static void process_fmt(char *act, struct per_cpu_info *pci,
1009 struct blk_io_trace *t, unsigned long long elapsed,
1010 int pdu_len, unsigned char *pdu_buf)
1012 char *p = fmt_select(act[0], t, elapsed);
1016 case '%': /* Field specifier */
1019 fprintf(ofp, "%c", *p++);
1021 fprintf(ofp, "%c", '%');
1023 p = parse_field(act, pci, t, elapsed,
1024 pdu_len, pdu_buf, p);
1026 case '\\': { /* escape */
1028 case 'b': fprintf(ofp, "\b"); break;
1029 case 'n': fprintf(ofp, "\n"); break;
1030 case 'r': fprintf(ofp, "\r"); break;
1031 case 't': fprintf(ofp, "\t"); break;
1034 "Invalid escape char in format %c\n",
1043 fprintf(ofp, "%c", *p++);
1049 static void log_complete(struct per_cpu_info *pci, struct blk_io_trace *t,
1052 process_fmt(act, pci, t, log_track_complete(t), 0, NULL);
1055 static void log_insert(struct per_cpu_info *pci, struct blk_io_trace *t,
1058 process_fmt(act, pci, t, log_track_insert(t), 0, NULL);
1061 static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t,
1064 process_fmt(act, pci, t, -1, 0, NULL);
1067 static void log_issue(struct per_cpu_info *pci, struct blk_io_trace *t,
1070 process_fmt(act, pci, t, log_track_issue(t), 0, NULL);
1073 static void log_merge(struct per_cpu_info *pci, struct blk_io_trace *t,
1077 log_track_frontmerge(t);
1079 process_fmt(act, pci, t, -1ULL, 0, NULL);
1082 static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t,
1085 process_fmt(act, pci, t, -1ULL, 0, NULL);
1088 static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t,
1091 process_fmt(act, pci, t, -1ULL, 0, NULL);
1094 static void log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t,
1097 process_fmt(act, pci, t, -1ULL, 0, NULL);
1100 static void log_split(struct per_cpu_info *pci, struct blk_io_trace *t,
1103 process_fmt(act, pci, t, -1ULL, 0, NULL);
1106 static void log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act)
1108 unsigned char *buf = (unsigned char *) t + sizeof(*t);
1110 process_fmt(act, pci, t, -1ULL, t->pdu_len, buf);
1113 static void dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci)
1115 int act = t->action & 0xffff;
1118 case __BLK_TA_QUEUE:
1119 log_generic(pci, t, "Q");
1121 case __BLK_TA_GETRQ:
1122 log_generic(pci, t, "G");
1124 case __BLK_TA_SLEEPRQ:
1125 log_generic(pci, t, "S");
1127 case __BLK_TA_REQUEUE:
1128 log_generic(pci, t, "R");
1130 case __BLK_TA_ISSUE:
1131 log_pc(pci, t, "D");
1133 case __BLK_TA_COMPLETE:
1134 log_pc(pci, t, "C");
1136 case __BLK_TA_INSERT:
1137 log_pc(pci, t, "I");
1140 fprintf(stderr, "Bad pc action %x\n", act);
1145 static void dump_trace_fs(struct blk_io_trace *t, struct per_cpu_info *pci)
1147 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
1148 int act = t->action & 0xffff;
1151 case __BLK_TA_QUEUE:
1152 account_queue(t, pci, w);
1153 log_queue(pci, t, "Q");
1155 case __BLK_TA_INSERT:
1156 log_insert(pci, t, "I");
1158 case __BLK_TA_BACKMERGE:
1159 account_m(t, pci, w);
1160 log_merge(pci, t, "M");
1162 case __BLK_TA_FRONTMERGE:
1163 account_m(t, pci, w);
1164 log_merge(pci, t, "F");
1166 case __BLK_TA_GETRQ:
1168 log_generic(pci, t, "G");
1170 case __BLK_TA_SLEEPRQ:
1171 log_generic(pci, t, "S");
1173 case __BLK_TA_REQUEUE:
1174 account_c(t, pci, w, -t->bytes);
1175 log_queue(pci, t, "R");
1177 case __BLK_TA_ISSUE:
1178 account_issue(t, pci, w);
1179 log_issue(pci, t, "D");
1181 case __BLK_TA_COMPLETE:
1182 account_c(t, pci, w, t->bytes);
1183 log_complete(pci, t, "C");
1186 log_action(pci, t, "P");
1188 case __BLK_TA_UNPLUG_IO:
1189 account_unplug(t, pci, 0);
1190 log_unplug(pci, t, "U");
1192 case __BLK_TA_UNPLUG_TIMER:
1193 account_unplug(t, pci, 1);
1194 log_unplug(pci, t, "UT");
1196 case __BLK_TA_SPLIT:
1197 log_split(pci, t, "X");
1199 case __BLK_TA_BOUNCE:
1200 log_generic(pci, t, "B");
1203 fprintf(stderr, "Bad fs action %x\n", t->action);
1208 static void dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
1209 struct per_dev_info *pdi)
1211 if (t->action & BLK_TC_ACT(BLK_TC_PC))
1212 dump_trace_pc(t, pci);
1214 dump_trace_fs(t, pci);
1219 static void dump_io_stats(struct io_stats *ios, char *msg)
1221 fprintf(ofp, "%s\n", msg);
1223 fprintf(ofp, " Reads Queued: %'8lu, %'8LuKiB\t", ios->qreads, ios->qread_kb);
1224 fprintf(ofp, " Writes Queued: %'8lu, %'8LuKiB\n", ios->qwrites,ios->qwrite_kb);
1226 fprintf(ofp, " Read Dispatches: %'8lu, %'8LuKiB\t", ios->ireads, ios->iread_kb);
1227 fprintf(ofp, " Write Dispatches: %'8lu, %'8LuKiB\n", ios->iwrites,ios->iwrite_kb);
1228 fprintf(ofp, " Reads Completed: %'8lu, %'8LuKiB\t", ios->creads, ios->cread_kb);
1229 fprintf(ofp, " Writes Completed: %'8lu, %'8LuKiB\n", ios->cwrites,ios->cwrite_kb);
1230 fprintf(ofp, " Read Merges: %'8lu%8c\t", ios->mreads, ' ');
1231 fprintf(ofp, " Write Merges: %'8lu\n", ios->mwrites);
1232 fprintf(ofp, " IO unplugs: %'8lu%8c\t", ios->io_unplugs, ' ');
1233 fprintf(ofp, " Timer unplugs: %'8lu\n", ios->timer_unplugs);
1236 static void dump_wait_stats(struct per_process_info *ppi)
1238 unsigned long rawait = ppi->longest_allocation_wait[0] / 1000;
1239 unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000;
1240 unsigned long rcwait = ppi->longest_completion_wait[0] / 1000;
1241 unsigned long wawait = ppi->longest_allocation_wait[1] / 1000;
1242 unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000;
1243 unsigned long wcwait = ppi->longest_completion_wait[1] / 1000;
1245 fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' ');
1246 fprintf(ofp, " Allocation wait: %'8lu\n", wawait);
1247 fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' ');
1248 fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait);
1249 fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' ');
1250 fprintf(ofp, " Completion wait: %'8lu\n", wcwait);
1253 static int ppi_name_compare(const void *p1, const void *p2)
1255 struct per_process_info *ppi1 = *((struct per_process_info **) p1);
1256 struct per_process_info *ppi2 = *((struct per_process_info **) p2);
1259 res = strverscmp(ppi1->name, ppi2->name);
1261 res = ppi1->pid > ppi2->pid;
1266 static void sort_process_list(void)
1268 struct per_process_info **ppis;
1269 struct per_process_info *ppi;
1272 ppis = malloc(ppi_list_entries * sizeof(struct per_process_info *));
1277 ppi = ppi->list_next;
1280 qsort(ppis, ppi_list_entries, sizeof(ppi), ppi_name_compare);
1282 i = ppi_list_entries - 1;
1287 ppi->list_next = ppi_list;
1295 static void show_process_stats(void)
1297 struct per_process_info *ppi;
1299 sort_process_list();
1305 snprintf(name, sizeof(name)-1, "%s (%u)", ppi->name, ppi->pid);
1306 dump_io_stats(&ppi->io_stats, name);
1307 dump_wait_stats(ppi);
1308 ppi = ppi->list_next;
1314 static void show_device_and_cpu_stats(void)
1316 struct per_dev_info *pdi;
1317 struct per_cpu_info *pci;
1318 struct io_stats total, *ios;
1319 int i, j, pci_events;
1320 char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3];
1323 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
1325 memset(&total, 0, sizeof(total));
1331 for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) {
1335 ios = &pci->io_stats;
1336 total.qreads += ios->qreads;
1337 total.qwrites += ios->qwrites;
1338 total.creads += ios->creads;
1339 total.cwrites += ios->cwrites;
1340 total.mreads += ios->mreads;
1341 total.mwrites += ios->mwrites;
1342 total.ireads += ios->ireads;
1343 total.iwrites += ios->iwrites;
1344 total.qread_kb += ios->qread_kb;
1345 total.qwrite_kb += ios->qwrite_kb;
1346 total.cread_kb += ios->cread_kb;
1347 total.cwrite_kb += ios->cwrite_kb;
1348 total.iread_kb += ios->iread_kb;
1349 total.iwrite_kb += ios->iwrite_kb;
1350 total.timer_unplugs += ios->timer_unplugs;
1351 total.io_unplugs += ios->io_unplugs;
1353 snprintf(line, sizeof(line) - 1, "CPU%d (%s):",
1354 j, get_dev_name(pdi, name, sizeof(name)));
1355 dump_io_stats(ios, line);
1359 if (pci_events > 1) {
1361 snprintf(line, sizeof(line) - 1, "Total (%s):",
1362 get_dev_name(pdi, name, sizeof(name)));
1363 dump_io_stats(&total, line);
1366 fprintf(ofp, "\nEvents (%s): %'Lu entries, %'lu skips\n",
1367 get_dev_name(pdi, line, sizeof(line)), pdi->events,
1372 static int sort_entries(void)
1377 while ((t = trace_list) != NULL) {
1378 trace_list = t->next;
1380 if (verify_trace(t->bit))
1382 if (trace_rb_insert(t))
1391 * struct trace and blktrace allocation cache, we do potentially
1392 * millions of mallocs for these structures while only using at most
1393 * a few thousand at the time
1395 static inline void t_free(struct trace *t)
1397 t->next = t_alloc_list;
1401 static inline struct trace *t_alloc(void)
1403 struct trace *t = t_alloc_list;
1406 t_alloc_list = t->next;
1410 return malloc(sizeof(*t));
1413 static inline void bit_free(struct blk_io_trace *bit)
1416 * abuse a 64-bit field for a next pointer for the free item
1418 bit->time = (__u64) bit_alloc_list;
1419 bit_alloc_list = (struct blk_io_trace *) bit;
1422 static inline struct blk_io_trace *bit_alloc(void)
1424 struct blk_io_trace *bit = bit_alloc_list;
1427 bit_alloc_list = (struct blk_io_trace *) bit->time;
1431 return malloc(sizeof(*bit));
1434 static void show_entries_rb(void)
1436 struct per_dev_info *pdi = NULL;
1437 struct per_cpu_info *pci = NULL;
1438 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 || device != bit->device) {
1453 device = bit->device;
1454 pdi = get_dev_info(device);
1458 fprintf(stderr, "Unknown device ID? (%d,%d)\n",
1459 MAJOR(bit->device), MINOR(bit->device));
1463 if (bit->cpu > pdi->ncpus) {
1464 fprintf(stderr, "Unknown CPU ID? (%d, device %d,%d)\n",
1465 bit->cpu, MAJOR(bit->device),
1466 MINOR(bit->device));
1470 if (!pci || cpu != bit->cpu) {
1472 pci = get_cpu_info(pdi, cpu);
1476 * back off displaying more info if we are out of sync
1477 * on SMP systems. to prevent stalling on lost events,
1478 * only allow an event to us a few times
1480 if (bit->sequence != (pdi->last_sequence + 1)
1481 && pdi->last_sequence != -1) {
1482 if (t->skipped < 5) {
1486 fprintf(stderr, "skipping from %lu to %u\n", pdi->last_sequence, bit->sequence);
1491 pdi->last_sequence = bit->sequence;
1493 bit->time -= genesis_time;
1494 if (bit->time >= stopwatch_end)
1497 if (bit->time >= stopwatch_start) {
1498 check_time(pdi, bit);
1500 dump_trace(bit, pci, pdi);
1503 rb_erase(&t->rb_node, &rb_sort_root);
1509 static int read_data(int fd, void *buffer, int bytes, int block)
1511 int ret, bytes_left, fl;
1514 fl = fcntl(fd, F_GETFL);
1517 fcntl(fd, F_SETFL, fl | O_NONBLOCK);
1519 fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
1523 while (bytes_left > 0) {
1524 ret = read(fd, p, bytes_left);
1528 if (errno != EAGAIN)
1540 static int read_sort_events(int fd)
1544 while (!is_done() && events < rb_batch) {
1545 struct blk_io_trace *bit;
1552 if (read_data(fd, bit, sizeof(*bit), !events))
1555 magic = be32_to_cpu(bit->magic);
1556 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
1557 fprintf(stderr, "Bad magic %x\n", magic);
1561 pdu_len = be16_to_cpu(bit->pdu_len);
1563 void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
1565 if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1))
1572 memset(t, 0, sizeof(*t));
1576 t->next = trace_list;
1585 static int do_file(void)
1587 struct per_cpu_info *pci;
1588 int i, j, *nfiles, events, events_added;
1590 nfiles = malloc(ndevices * sizeof(int));
1593 * first prepare all files for reading
1595 for (i = 0; i < ndevices; i++) {
1598 struct per_dev_info *pdi;
1602 pdi->last_sequence = -1;
1603 pci = get_cpu_info(pdi, j);
1607 snprintf(pci->fname, sizeof(pci->fname)-1,
1608 "%s.blktrace.%d", pdi->name, pci->cpu);
1609 if (stat(pci->fname, &st) < 0)
1612 pci->fd = open(pci->fname, O_RDONLY);
1619 printf("Input file %s added\n", pci->fname);
1625 * now loop over the files reading in the data
1630 for (i = 0; i < ndevices; i++) {
1631 for (j = 0; j < nfiles[i]; j++) {
1633 pci = get_cpu_info(&devices[i], j);
1638 events = read_sort_events(pci->fd);
1645 if (sort_entries() == -1)
1648 events_added += events;
1654 } while (events_added);
1659 static int do_stdin(void)
1663 fd = dup(STDIN_FILENO);
1667 events = read_sort_events(fd);
1671 if (sort_entries() == -1)
1681 static void flush_output(void)
1686 static void handle_sigint(int sig)
1693 * Extract start and duration times from a string, allowing
1694 * us to specify a time interval of interest within a trace.
1695 * Format: "duration" (start is zero) or "start:duration".
1697 static int find_stopwatch_interval(char *string)
1702 value = strtod(string, &sp);
1704 fprintf(stderr,"Invalid stopwatch timer: %s\n", string);
1708 stopwatch_start = DOUBLE_TO_NANO_ULL(value);
1710 value = strtod(string, &sp);
1711 if (sp == string || *sp != '\0') {
1712 fprintf(stderr,"Invalid stopwatch duration time: %s\n",
1716 } else if (*sp != '\0') {
1717 fprintf(stderr,"Invalid stopwatch start timer: %s\n", string);
1720 stopwatch_end = DOUBLE_TO_NANO_ULL(value);
1721 if (stopwatch_end <= stopwatch_start) {
1722 fprintf(stderr, "Invalid stopwatch interval: %Lu -> %Lu\n",
1723 stopwatch_start, stopwatch_end);
1730 static char usage_str[] = \
1731 "[ -i <input name> ] [-o <output name> [ -s ] [ -t ] [ -q ]\n" \
1732 "[ -w start:stop ] [ -f output format ] [ -F format spec ] [ -v] \n\n" \
1733 "\t-i Input file containing trace data, or '-' for stdin\n" \
1734 "\t-o Output file. If not given, output is stdout\n" \
1735 "\t-b stdin read batching\n" \
1736 "\t-s Show per-program io statistics\n" \
1737 "\t-t Track individual ios. Will tell you the time a request took\n" \
1738 "\t to get queued, to get dispatched, and to get completed\n" \
1739 "\t-q Quiet. Don't display any stats at the end of the trace\n" \
1740 "\t-w Only parse data between the given time interval in seconds.\n" \
1741 "\t If 'start' isn't given, blkparse defaults the start time to 0\n" \
1742 "\t -f Output format. Customize the output format. The format field\n" \
1743 "\t identifies can be found in the documentation\n" \
1744 "\t-F Format specification. Can be found in the documentation\n" \
1745 "\t-v Print program version info\n\n";
1747 static void usage(char *prog)
1749 fprintf(stderr, "Usage: %s %s %s", prog, blkparse_version, usage_str);
1752 int main(int argc, char *argv[])
1756 int per_device_and_cpu_stats = 1;
1758 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
1761 if (!strcmp(optarg, "-") && !pipeline)
1763 else if (resize_devices(optarg) != 0)
1767 output_name = optarg;
1770 rb_batch = atoi(optarg);
1772 rb_batch = RB_BATCH_DEFAULT;
1775 per_process_stats = 1;
1781 per_device_and_cpu_stats = 0;
1784 if (find_stopwatch_interval(optarg) != 0)
1788 set_all_format_specs(optarg);
1791 if (add_format_spec(optarg) != 0)
1795 printf("%s version %s\n", argv[0], blkparse_version);
1803 while (optind < argc) {
1804 if (!strcmp(argv[optind], "-") && !pipeline)
1806 else if (resize_devices(argv[optind]) != 0)
1811 if (!pipeline && !ndevices) {
1816 memset(&rb_sort_root, 0, sizeof(rb_sort_root));
1817 memset(&rb_track_root, 0, sizeof(rb_track_root));
1819 signal(SIGINT, handle_sigint);
1820 signal(SIGHUP, handle_sigint);
1821 signal(SIGTERM, handle_sigint);
1823 setlocale(LC_NUMERIC, "en_US");
1826 ofp = fdopen(STDOUT_FILENO, "w");
1831 snprintf(ofname, sizeof(ofname) - 1, "%s.log", output_name);
1832 ofp = fopen(ofname, "w");
1841 ofp_buffer = malloc(4096);
1842 if (setvbuf(ofp, ofp_buffer, mode, 4096)) {
1852 if (per_process_stats)
1853 show_process_stats();
1855 if (per_device_and_cpu_stats)
1856 show_device_and_cpu_stats();