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;
185 * for tracking individual ios
188 struct rb_node rb_node;
193 unsigned long long allocation_time;
194 unsigned long long queue_time;
195 unsigned long long dispatch_time;
196 unsigned long long completion_time;
200 static struct per_dev_info *devices;
201 static char *get_dev_name(struct per_dev_info *, char *, int);
204 static char *output_name;
206 static unsigned long long genesis_time;
207 static unsigned long long stopwatch_start; /* start from zero by default */
208 static unsigned long long stopwatch_end = ULONG_LONG_MAX; /* "infinity" */
210 static int per_process_stats;
211 static int track_ios;
213 #define RB_BATCH_DEFAULT (1024)
214 static int rb_batch = RB_BATCH_DEFAULT;
218 #define is_done() (*(volatile int *)(&done))
219 static volatile int done;
221 static inline unsigned long hash_long(unsigned long val)
225 #elif __WORDSIZE == 64
226 val *= 0x9e37fffffffc0001UL;
228 #error unknown word size
231 return val >> (__WORDSIZE - PPI_HASH_SHIFT);
234 static inline void add_process_to_hash(struct per_process_info *ppi)
236 const int hash_idx = hash_long(ppi->pid);
238 ppi->hash_next = ppi_hash[hash_idx];
239 ppi_hash[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_pid(__u32 pid)
251 const int hash_idx = hash_long(pid);
252 struct per_process_info *ppi;
254 ppi = ppi_hash[hash_idx];
259 ppi = ppi->hash_next;
265 static inline int trace_rb_insert(struct trace *t)
267 struct rb_node **p = &rb_sort_root.rb_node;
268 struct rb_node *parent = NULL;
271 if (genesis_time == 0 || t->bit->time < genesis_time)
272 genesis_time = t->bit->time;
276 __t = rb_entry(parent, struct trace, rb_node);
278 if (t->bit->time < __t->bit->time)
280 else if (t->bit->time > __t->bit->time)
282 else if (t->bit->device < __t->bit->device)
284 else if (t->bit->device > __t->bit->device)
286 else if (t->bit->sequence < __t->bit->sequence)
288 else if (t->bit->sequence > __t->bit->sequence)
290 else if (t->bit->device == __t->bit->device) {
292 "sequence alias (%d) on device %d,%d!\n",
294 MAJOR(t->bit->device), MINOR(t->bit->device));
299 rb_link_node(&t->rb_node, parent, p);
300 rb_insert_color(&t->rb_node, &rb_sort_root);
304 static inline int track_rb_insert(struct io_track *iot)
306 struct rb_node **p = &rb_track_root.rb_node;
307 struct rb_node *parent = NULL;
308 struct io_track *__iot;
313 __iot = rb_entry(parent, struct io_track, rb_node);
315 if (iot->device < __iot->device)
317 else if (iot->device > __iot->device)
319 else if (iot->sector < __iot->sector)
321 else if (iot->sector > __iot->sector)
325 "sector alias (%Lu) on device %d,%d!\n",
326 (unsigned long long) iot->sector,
327 MAJOR(iot->device), MINOR(iot->device));
332 rb_link_node(&iot->rb_node, parent, p);
333 rb_insert_color(&iot->rb_node, &rb_track_root);
337 static struct io_track *__find_track(dev_t device, __u64 sector)
339 struct rb_node **p = &rb_track_root.rb_node;
340 struct rb_node *parent = NULL;
341 struct io_track *__iot;
346 __iot = rb_entry(parent, struct io_track, rb_node);
348 if (device < __iot->device)
350 else if (device > __iot->device)
352 else if (sector < __iot->sector)
354 else if (sector > __iot->sector)
363 static struct io_track *find_track(__u32 pid, dev_t device, __u64 sector)
365 struct io_track *iot;
367 iot = __find_track(device, sector);
369 iot = malloc(sizeof(*iot));
371 iot->device = device;
372 iot->sector = sector;
373 track_rb_insert(iot);
379 static void log_track_frontmerge(struct blk_io_trace *t)
381 struct io_track *iot;
386 iot = __find_track(t->device, t->sector + (t->bytes >> 9));
388 fprintf(stderr, "failed to find mergeable event\n");
392 rb_erase(&iot->rb_node, &rb_track_root);
393 iot->sector -= t->bytes >> 9;
394 track_rb_insert(iot);
397 static void log_track_getrq(struct blk_io_trace *t)
399 struct io_track *iot;
404 iot = find_track(t->pid, t->device, t->sector);
405 iot->allocation_time = t->time;
410 * return time between rq allocation and insertion
412 static unsigned long long log_track_insert(struct blk_io_trace *t)
414 unsigned long long elapsed;
415 struct io_track *iot;
420 iot = find_track(t->pid, t->device, t->sector);
421 iot->queue_time = t->time;
422 elapsed = iot->queue_time - iot->allocation_time;
424 if (per_process_stats) {
425 struct per_process_info *ppi = find_process_by_pid(iot->pid);
426 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
428 if (ppi && elapsed > ppi->longest_allocation_wait[w])
429 ppi->longest_allocation_wait[w] = elapsed;
436 * return time between queue and issue
438 static unsigned long long log_track_issue(struct blk_io_trace *t)
440 unsigned long long elapsed;
441 struct io_track *iot;
445 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
448 iot = __find_track(t->device, t->sector);
450 fprintf(stderr, "failed to find issue event\n");
454 iot->dispatch_time = t->time;
455 elapsed = iot->dispatch_time - iot->queue_time;
457 if (per_process_stats) {
458 struct per_process_info *ppi = find_process_by_pid(iot->pid);
459 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
461 if (ppi && elapsed > ppi->longest_dispatch_wait[w])
462 ppi->longest_dispatch_wait[w] = elapsed;
469 * return time between dispatch and complete
471 static unsigned long long log_track_complete(struct blk_io_trace *t)
473 unsigned long long elapsed;
474 struct io_track *iot;
478 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
481 iot = __find_track(t->device, t->sector);
483 fprintf(stderr, "failed to find complete event\n");
487 iot->completion_time = t->time;
488 elapsed = iot->completion_time - iot->dispatch_time;
490 if (per_process_stats) {
491 struct per_process_info *ppi = find_process_by_pid(iot->pid);
492 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
494 if (ppi && elapsed > ppi->longest_completion_wait[w])
495 ppi->longest_completion_wait[w] = elapsed;
499 * kill the trace, we don't need it after completion
501 rb_erase(&iot->rb_node, &rb_track_root);
508 static struct io_stats *find_process_io_stats(__u32 pid, char *name)
510 struct per_process_info *ppi = find_process_by_pid(pid);
513 ppi = malloc(sizeof(*ppi));
514 memset(ppi, 0, sizeof(*ppi));
515 strncpy(ppi->name, name, sizeof(ppi->name));
517 add_process_to_hash(ppi);
518 add_process_to_list(ppi);
521 return &ppi->io_stats;
525 static void resize_cpu_info(struct per_dev_info *pdi, int cpu)
527 struct per_cpu_info *cpus = pdi->cpus;
528 int ncpus = pdi->ncpus;
529 int new_count = cpu + 1;
533 size = new_count * sizeof(struct per_cpu_info);
534 cpus = realloc(cpus, size);
537 fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n",
538 get_dev_name(pdi, name, sizeof(name)), size);
542 new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info));
543 new_space = (new_count - ncpus) * sizeof(struct per_cpu_info);
544 memset(new_start, 0, new_space);
546 pdi->ncpus = new_count;
550 static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu)
552 struct per_cpu_info *pci;
554 if (cpu >= pdi->ncpus)
555 resize_cpu_info(pdi, cpu);
557 pci = &pdi->cpus[cpu];
563 static int resize_devices(char *name)
565 int size = (ndevices + 1) * sizeof(struct per_dev_info);
567 devices = realloc(devices, size);
569 fprintf(stderr, "Out of memory, device %s (%d)\n", name, size);
572 memset(&devices[ndevices], 0, sizeof(struct per_dev_info));
573 devices[ndevices].name = name;
578 static struct per_dev_info *get_dev_info(dev_t id)
580 struct per_dev_info *pdi;
583 for (i = 0; i < ndevices; i++)
584 if (devices[i].id == id)
587 if (resize_devices(NULL) != 0)
590 pdi = &devices[ndevices - 1];
592 pdi->last_sequence = -1;
596 static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size)
599 snprintf(buffer, size, "%s", pdi->name);
601 snprintf(buffer, size, "%d,%d", MAJOR(pdi->id), MINOR(pdi->id));
605 static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit)
607 unsigned long long this = bit->time;
608 unsigned long long last = pdi->last_reported_time;
610 pdi->backwards = (this < last) ? 'B' : ' ';
611 pdi->last_reported_time = this;
614 static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t,
619 ios->qwrite_kb += t->bytes >> 10;
622 ios->qread_kb += t->bytes >> 10;
626 static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci,
629 __account_m(&pci->io_stats, t, rw);
631 if (per_process_stats) {
632 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
634 __account_m(ios, t, rw);
638 static inline void __account_queue(struct io_stats *ios, struct blk_io_trace *t,
643 ios->qwrite_kb += t->bytes >> 10;
646 ios->qread_kb += t->bytes >> 10;
650 static inline void account_queue(struct blk_io_trace *t,
651 struct per_cpu_info *pci, int rw)
653 __account_queue(&pci->io_stats, t, rw);
655 if (per_process_stats) {
656 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
658 __account_queue(ios, t, rw);
662 static inline void __account_c(struct io_stats *ios, int rw, unsigned int bytes)
666 ios->cwrite_kb += bytes >> 10;
669 ios->cread_kb += bytes >> 10;
673 static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci,
676 __account_c(&pci->io_stats, rw, bytes);
678 if (per_process_stats) {
679 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
681 __account_c(ios, rw, bytes);
685 static inline void __account_issue(struct io_stats *ios, int rw,
690 ios->iwrite_kb += bytes >> 10;
693 ios->iread_kb += bytes >> 10;
697 static inline void account_issue(struct blk_io_trace *t,
698 struct per_cpu_info *pci, int rw)
700 __account_issue(&pci->io_stats, rw, t->bytes);
702 if (per_process_stats) {
703 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
705 __account_issue(ios, rw, t->bytes);
709 static inline void __account_unplug(struct io_stats *ios, int timer)
712 ios->timer_unplugs++;
717 static inline void account_unplug(struct blk_io_trace *t,
718 struct per_cpu_info *pci, int timer)
720 __account_unplug(&pci->io_stats, timer);
722 if (per_process_stats) {
723 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
725 __account_unplug(ios, timer);
729 #define VALID_SPECS "BCDFGMPQRSTU"
730 static char *override_format[256];
731 static inline int valid_spec(int spec)
733 return strchr(VALID_SPECS, spec) != NULL;
736 static void set_all_format_specs(char *optarg)
740 for (p = VALID_SPECS; *p; p++)
741 if (override_format[(int)(*p)] == NULL)
742 override_format[(int)(*p)] = strdup(optarg);
745 static int add_format_spec(char *optarg)
747 int spec = optarg[0];
749 if (!valid_spec(spec)) {
750 fprintf(stderr,"Bad format specifier %c\n", spec);
753 if (optarg[1] != ',') {
754 fprintf(stderr,"Bad format specifier - need ',' %s\n", optarg);
758 if (*optarg == '\0') {
759 fprintf(stderr,"Bad format specifier - need fmt %s\n", optarg);
764 * Set both merges (front and back)
767 override_format['B'] = strdup(optarg);
768 override_format['M'] = strdup(optarg);
770 override_format[spec] = strdup(optarg);
775 static void print_field(char *act, struct per_cpu_info *pci,
776 struct blk_io_trace *t, unsigned long long elapsed,
777 int pdu_len, unsigned char *pdu_buf, char field,
778 int minus, int has_w, int width)
784 sprintf(format, "%%-%d", width);
786 sprintf(format, "%%%d", width);
788 sprintf(format, "%%");
792 fprintf(ofp, strcat(format, "s"), act);
795 fprintf(ofp, strcat(format, "d"), pci->cpu);
798 fprintf(ofp, strcat(format, "s"), t->comm);
803 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
804 int b = t->action & BLK_TC_ACT(BLK_TC_BARRIER);
805 int s = t->action & BLK_TC_ACT(BLK_TC_SYNC);
815 fprintf(ofp, strcat(format, "s"), rwbs);
818 case 'D': /* format width ignored */
819 fprintf(ofp,"%3d,%-3d", MAJOR(t->device), MINOR(t->device));
822 fprintf(ofp, strcat(format, "d"), t->error);
825 fprintf(ofp, strcat(format, "d"), MAJOR(t->device));
828 fprintf(ofp, strcat(format, "d"), MINOR(t->device));
831 fprintf(ofp, strcat(format, "u"), t->bytes >> 9);
834 fprintf(ofp, strcat(format, "u"), t->pid);
836 case 'P': /* format width ignored */
837 if ((pdu_len > 0) && (pdu_buf != NULL)) {
839 unsigned char *p = pdu_buf;
840 for (i = 0; i < pdu_len; i++) {
844 fprintf(ofp, "%02x", *p++);
849 fprintf(ofp, strcat(format, "ld"), t->sequence);
852 fprintf(ofp, strcat(format, "lu"), t->sector);
855 sprintf(format, "%%0%dlu", has_w ? width : 9);
856 fprintf(ofp, format, NANO_SECONDS(t->time));
859 fprintf(ofp, strcat(format, "d"), SECONDS(t->time));
862 if (elapsed == -1ULL) {
863 fprintf(stderr, "Expecting elapsed value\n");
866 fprintf(ofp, strcat(format, "llu"), elapsed / 1000);
869 __u64 *depth = (__u64 *) ((char *) t + sizeof(*t));
870 fprintf(ofp, strcat(format, "u"),
871 (unsigned int) be64_to_cpu(*depth));
875 fprintf(ofp,strcat(format, "c"), field);
880 static char *parse_field(char *act, struct per_cpu_info *pci,
881 struct blk_io_trace *t, unsigned long long elapsed,
882 int pdu_len, unsigned char *pdu_buf,
888 char *p = master_format;
897 width = (width * 10) + (*p++ - '0');
898 } while ((*p) && (isdigit(*p)));
901 print_field(act, pci, t, elapsed, pdu_len, pdu_buf, *p++,
902 minus, has_w, width);
907 static char *fmt_select(int fmt_spec, struct blk_io_trace *t,
908 unsigned long long elapsed)
911 char scratch_format[1024];
913 if (override_format[fmt_spec] != NULL)
914 return override_format[fmt_spec];
917 case 'C': /* Complete */
918 if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
919 strcpy(scratch_format, HEADER);
920 strcat(scratch_format, "(%P) ");
922 strcpy(scratch_format, HEADER "%S + %n ");
923 if (elapsed != -1ULL)
924 strcat(scratch_format, "(%8u) ");
926 strcat(scratch_format, "[%e]\n");
927 fmt = scratch_format;
930 case 'D': /* Issue */
931 if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
932 strcpy(scratch_format, HEADER);
933 strcat(scratch_format, "%n (%P) ");
935 strcpy(scratch_format, HEADER "%S + %n ");
936 if (elapsed != -1ULL)
937 strcat(scratch_format, "(%8u) ");
939 strcat(scratch_format,"[%C]\n");
940 fmt = scratch_format;
943 case 'I': /* Insert */
944 if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
945 strcpy(scratch_format, HEADER);
946 strcat(scratch_format, "%n (%P) ");
948 strcpy(scratch_format, HEADER "%S + %n ");
949 if (elapsed != -1ULL)
950 strcat(scratch_format, "(%8u) ");
952 strcat(scratch_format,"[%C]\n");
953 fmt = scratch_format;
956 case 'Q': /* Queue */
957 case 'W': /* Bounce */
958 strcpy(scratch_format, HEADER "%S + %n ");
959 if (elapsed != -1ULL)
960 strcat(scratch_format, "(%8u) ");
961 strcat(scratch_format,"[%C]\n");
962 fmt = scratch_format;
965 case 'B': /* Back merge */
966 case 'F': /* Front merge */
967 case 'M': /* Front or back merge */
968 fmt = HEADER "%S + %n [%C]\n";
972 fmt = HEADER "[%C]\n";
975 case 'G': /* Get request */
976 case 'S': /* Sleep request */
977 fmt = HEADER "%S + %n [%C]\n";
980 case 'U': /* Unplug IO */
981 case 'T': /* Unplug timer */
982 fmt = HEADER "[%C] %U\n";
985 case 'X': /* Split */
986 strcpy(scratch_format, HEADER "%S / %U ");
987 strcat(scratch_format,"[%C]\n");
988 fmt = scratch_format;
992 fprintf(stderr,"FATAL: Invalid format spec %c\n", fmt_spec);
1000 static void process_fmt(char *act, struct per_cpu_info *pci,
1001 struct blk_io_trace *t, unsigned long long elapsed,
1002 int pdu_len, unsigned char *pdu_buf)
1004 char *p = fmt_select(act[0], t, elapsed);
1008 case '%': /* Field specifier */
1011 fprintf(ofp, "%c", *p++);
1013 fprintf(ofp, "%c", '%');
1015 p = parse_field(act, pci, t, elapsed,
1016 pdu_len, pdu_buf, p);
1018 case '\\': { /* escape */
1020 case 'b': fprintf(ofp, "\b"); break;
1021 case 'n': fprintf(ofp, "\n"); break;
1022 case 'r': fprintf(ofp, "\r"); break;
1023 case 't': fprintf(ofp, "\t"); break;
1026 "Invalid escape char in format %c\n",
1035 fprintf(ofp, "%c", *p++);
1041 static void log_complete(struct per_cpu_info *pci, struct blk_io_trace *t,
1044 process_fmt(act, pci, t, log_track_complete(t), 0, NULL);
1047 static void log_insert(struct per_cpu_info *pci, struct blk_io_trace *t,
1050 process_fmt(act, pci, t, log_track_insert(t), 0, NULL);
1053 static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t,
1056 process_fmt(act, pci, t, -1, 0, NULL);
1059 static void log_issue(struct per_cpu_info *pci, struct blk_io_trace *t,
1062 process_fmt(act, pci, t, log_track_issue(t), 0, NULL);
1065 static void log_merge(struct per_cpu_info *pci, struct blk_io_trace *t,
1069 log_track_frontmerge(t);
1071 process_fmt(act, pci, t, -1ULL, 0, NULL);
1074 static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t,
1077 process_fmt(act, pci, t, -1ULL, 0, NULL);
1080 static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t,
1083 process_fmt(act, pci, t, -1ULL, 0, NULL);
1086 static void log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t,
1089 process_fmt(act, pci, t, -1ULL, 0, NULL);
1092 static void log_split(struct per_cpu_info *pci, struct blk_io_trace *t,
1095 process_fmt(act, pci, t, -1ULL, 0, NULL);
1098 static void log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act)
1100 unsigned char *buf = (unsigned char *) t + sizeof(*t);
1102 process_fmt(act, pci, t, -1ULL, t->pdu_len, buf);
1105 static void dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci)
1107 int act = t->action & 0xffff;
1110 case __BLK_TA_QUEUE:
1111 log_generic(pci, t, "Q");
1113 case __BLK_TA_GETRQ:
1114 log_generic(pci, t, "G");
1116 case __BLK_TA_SLEEPRQ:
1117 log_generic(pci, t, "S");
1119 case __BLK_TA_REQUEUE:
1120 log_generic(pci, t, "R");
1122 case __BLK_TA_ISSUE:
1123 log_pc(pci, t, "D");
1125 case __BLK_TA_COMPLETE:
1126 log_pc(pci, t, "C");
1128 case __BLK_TA_INSERT:
1129 log_pc(pci, t, "I");
1132 fprintf(stderr, "Bad pc action %x\n", act);
1137 static void dump_trace_fs(struct blk_io_trace *t, struct per_cpu_info *pci)
1139 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
1140 int act = t->action & 0xffff;
1143 case __BLK_TA_QUEUE:
1144 account_queue(t, pci, w);
1145 log_queue(pci, t, "Q");
1147 case __BLK_TA_INSERT:
1148 log_insert(pci, t, "I");
1150 case __BLK_TA_BACKMERGE:
1151 account_m(t, pci, w);
1152 log_merge(pci, t, "M");
1154 case __BLK_TA_FRONTMERGE:
1155 account_m(t, pci, w);
1156 log_merge(pci, t, "F");
1158 case __BLK_TA_GETRQ:
1160 log_generic(pci, t, "G");
1162 case __BLK_TA_SLEEPRQ:
1163 log_generic(pci, t, "S");
1165 case __BLK_TA_REQUEUE:
1166 account_c(t, pci, w, -t->bytes);
1167 log_queue(pci, t, "R");
1169 case __BLK_TA_ISSUE:
1170 account_issue(t, pci, w);
1171 log_issue(pci, t, "D");
1173 case __BLK_TA_COMPLETE:
1174 account_c(t, pci, w, t->bytes);
1175 log_complete(pci, t, "C");
1178 log_action(pci, t, "P");
1180 case __BLK_TA_UNPLUG_IO:
1181 account_unplug(t, pci, 0);
1182 log_unplug(pci, t, "U");
1184 case __BLK_TA_UNPLUG_TIMER:
1185 account_unplug(t, pci, 1);
1186 log_unplug(pci, t, "UT");
1188 case __BLK_TA_SPLIT:
1189 log_split(pci, t, "X");
1191 case __BLK_TA_BOUNCE:
1192 log_generic(pci, t, "B");
1195 fprintf(stderr, "Bad fs action %x\n", t->action);
1200 static void dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
1201 struct per_dev_info *pdi)
1203 if (t->action & BLK_TC_ACT(BLK_TC_PC))
1204 dump_trace_pc(t, pci);
1206 dump_trace_fs(t, pci);
1211 static void dump_io_stats(struct io_stats *ios, char *msg)
1213 fprintf(ofp, "%s\n", msg);
1215 fprintf(ofp, " Reads Queued: %'8lu, %'8LuKiB\t", ios->qreads, ios->qread_kb);
1216 fprintf(ofp, " Writes Queued: %'8lu, %'8LuKiB\n", ios->qwrites,ios->qwrite_kb);
1218 fprintf(ofp, " Read Dispatches: %'8lu, %'8LuKiB\t", ios->ireads, ios->iread_kb);
1219 fprintf(ofp, " Write Dispatches: %'8lu, %'8LuKiB\n", ios->iwrites,ios->iwrite_kb);
1220 fprintf(ofp, " Reads Completed: %'8lu, %'8LuKiB\t", ios->creads, ios->cread_kb);
1221 fprintf(ofp, " Writes Completed: %'8lu, %'8LuKiB\n", ios->cwrites,ios->cwrite_kb);
1222 fprintf(ofp, " Read Merges: %'8lu%8c\t", ios->mreads, ' ');
1223 fprintf(ofp, " Write Merges: %'8lu\n", ios->mwrites);
1224 fprintf(ofp, " IO unplugs: %'8lu%8c\t", ios->io_unplugs, ' ');
1225 fprintf(ofp, " Timer unplugs: %'8lu\n", ios->timer_unplugs);
1228 static void dump_wait_stats(struct per_process_info *ppi)
1230 unsigned long rawait = ppi->longest_allocation_wait[0] / 1000;
1231 unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000;
1232 unsigned long rcwait = ppi->longest_completion_wait[0] / 1000;
1233 unsigned long wawait = ppi->longest_allocation_wait[1] / 1000;
1234 unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000;
1235 unsigned long wcwait = ppi->longest_completion_wait[1] / 1000;
1237 fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' ');
1238 fprintf(ofp, " Allocation wait: %'8lu\n", wawait);
1239 fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' ');
1240 fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait);
1241 fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' ');
1242 fprintf(ofp, " Completion wait: %'8lu\n", wcwait);
1245 static int ppi_name_compare(const void *p1, const void *p2)
1247 struct per_process_info *ppi1 = *((struct per_process_info **) p1);
1248 struct per_process_info *ppi2 = *((struct per_process_info **) p2);
1251 res = strverscmp(ppi1->name, ppi2->name);
1253 res = ppi1->pid > ppi2->pid;
1258 static void sort_process_list(void)
1260 struct per_process_info **ppis;
1261 struct per_process_info *ppi;
1264 ppis = malloc(ppi_list_entries * sizeof(struct per_process_info *));
1269 ppi = ppi->list_next;
1272 qsort(ppis, ppi_list_entries, sizeof(ppi), ppi_name_compare);
1274 i = ppi_list_entries - 1;
1279 ppi->list_next = ppi_list;
1287 static void show_process_stats(void)
1289 struct per_process_info *ppi;
1291 sort_process_list();
1297 snprintf(name, sizeof(name)-1, "%s (%u)", ppi->name, ppi->pid);
1298 dump_io_stats(&ppi->io_stats, name);
1299 dump_wait_stats(ppi);
1300 ppi = ppi->list_next;
1306 static void show_device_and_cpu_stats(void)
1308 struct per_dev_info *pdi;
1309 struct per_cpu_info *pci;
1310 struct io_stats total, *ios;
1311 int i, j, pci_events;
1312 char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3];
1315 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
1317 memset(&total, 0, sizeof(total));
1323 for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) {
1327 ios = &pci->io_stats;
1328 total.qreads += ios->qreads;
1329 total.qwrites += ios->qwrites;
1330 total.creads += ios->creads;
1331 total.cwrites += ios->cwrites;
1332 total.mreads += ios->mreads;
1333 total.mwrites += ios->mwrites;
1334 total.ireads += ios->ireads;
1335 total.iwrites += ios->iwrites;
1336 total.qread_kb += ios->qread_kb;
1337 total.qwrite_kb += ios->qwrite_kb;
1338 total.cread_kb += ios->cread_kb;
1339 total.cwrite_kb += ios->cwrite_kb;
1340 total.iread_kb += ios->iread_kb;
1341 total.iwrite_kb += ios->iwrite_kb;
1342 total.timer_unplugs += ios->timer_unplugs;
1343 total.io_unplugs += ios->io_unplugs;
1345 snprintf(line, sizeof(line) - 1, "CPU%d (%s):",
1346 j, get_dev_name(pdi, name, sizeof(name)));
1347 dump_io_stats(ios, line);
1351 if (pci_events > 1) {
1353 snprintf(line, sizeof(line) - 1, "Total (%s):",
1354 get_dev_name(pdi, name, sizeof(name)));
1355 dump_io_stats(&total, line);
1358 fprintf(ofp, "\nEvents (%s): %'Lu entries, %'lu skips\n",
1359 get_dev_name(pdi, line, sizeof(line)), pdi->events,
1364 static struct blk_io_trace *find_trace(void *p, unsigned long offset)
1366 unsigned long max_offset = offset;
1368 struct blk_io_trace *bit;
1371 for (off = 0; off < max_offset; off++) {
1374 magic = be32_to_cpu(bit->magic);
1375 if ((magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
1382 static inline int verify_and_add_trace(struct trace *t)
1384 if (verify_trace(t->bit))
1386 if (trace_rb_insert(t))
1392 static int sort_entries(void)
1397 while ((t = trace_list) != NULL) {
1398 trace_list = t->next;
1399 verify_and_add_trace(t);
1406 static void show_entries_rb(int piped)
1408 struct per_dev_info *pdi = NULL;
1409 struct per_cpu_info *pci = NULL;
1410 struct blk_io_trace *bit;
1416 n = rb_first(&rb_sort_root);
1422 t = rb_entry(n, struct trace, rb_node);
1425 if (!pdi || device != bit->device) {
1426 device = bit->device;
1427 pdi = get_dev_info(device);
1431 fprintf(stderr, "Unknown device ID? (%d,%d)\n",
1432 MAJOR(bit->device), MINOR(bit->device));
1436 if (!pci || cpu != bit->cpu) {
1438 pci = get_cpu_info(pdi, cpu);
1442 * back off displaying more info if we are out of sync
1443 * on SMP systems. to prevent stalling on lost events,
1444 * only allow an event to us a few times
1446 if (bit->sequence != (pdi->last_sequence + 1)
1447 && pdi->last_sequence != -1) {
1448 if (piped && t->skipped < 5) {
1452 fprintf(stderr, "skipping from %lu to %u\n", pdi->last_sequence, bit->sequence);
1457 pdi->last_sequence = bit->sequence;
1459 bit->time -= genesis_time;
1460 if (bit->time >= stopwatch_end)
1463 if (bit->time >= stopwatch_start) {
1464 check_time(pdi, bit);
1466 dump_trace(bit, pci, pdi);
1470 rb_erase(&t->rb_node, &rb_sort_root);
1473 n = rb_first(&rb_sort_root);
1479 static int read_data(int fd, void *buffer, int bytes, int block)
1481 int ret, bytes_left, fl;
1484 fl = fcntl(fd, F_GETFL);
1487 fcntl(fd, F_SETFL, fl | O_NONBLOCK);
1489 fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
1493 while (bytes_left > 0) {
1494 ret = read(fd, p, bytes_left);
1498 if (errno != EAGAIN)
1511 * Find the traces in 'tb' and add them to the list for sorting and
1514 static int find_sort_entries(void *tb, unsigned long size)
1516 struct blk_io_trace *bit;
1521 while (tb - start <= size - sizeof(*bit)) {
1522 bit = find_trace(tb, size - (tb - start));
1526 t = malloc(sizeof(*t));
1527 memset(t, 0, sizeof(*t));
1532 verify_and_add_trace(t);
1534 tb += sizeof(*bit) + bit->pdu_len;
1541 static int do_file(void)
1543 int i, j, nfiles = 0;
1545 for (i = 0; i < ndevices; i++) {
1546 for (j = 0;; j++, nfiles++) {
1547 struct per_dev_info *pdi;
1548 struct per_cpu_info *pci;
1553 pdi->last_sequence = -1;
1554 pci = get_cpu_info(pdi, j);
1557 snprintf(pci->fname, sizeof(pci->fname)-1,
1558 "%s.blktrace.%d", pdi->name, j);
1559 if (stat(pci->fname, &st) < 0)
1564 printf("Processing %s\n", pci->fname);
1566 tb = malloc(st.st_size);
1568 fprintf(stderr, "Out of memory, skip file %s\n",
1573 pci->fd = open(pci->fname, O_RDONLY);
1580 if (read_data(pci->fd, tb, st.st_size, 1)) {
1586 pci->nelems = find_sort_entries(tb, st.st_size);
1588 printf("Completed %s (CPU%d %d, entries)\n",
1589 pci->fname, j, pci->nelems);
1595 fprintf(stderr, "No files found\n");
1603 static int read_sort_events(int fd)
1608 struct blk_io_trace *bit;
1613 bit = malloc(sizeof(*bit));
1615 if (read_data(fd, bit, sizeof(*bit), !events))
1618 magic = be32_to_cpu(bit->magic);
1619 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
1620 fprintf(stderr, "Bad magic %x\n", magic);
1624 pdu_len = be16_to_cpu(bit->pdu_len);
1626 void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
1628 if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1))
1634 t = malloc(sizeof(*t));
1635 memset(t, 0, sizeof(*t));
1639 t->next = trace_list;
1643 } while (!is_done() && events < rb_batch);
1648 static int do_stdin(void)
1652 fd = dup(STDIN_FILENO);
1656 events = read_sort_events(fd);
1660 if (sort_entries() == -1)
1670 static void flush_output(void)
1675 static void handle_sigint(int sig)
1682 * Extract start and duration times from a string, allowing
1683 * us to specify a time interval of interest within a trace.
1684 * Format: "duration" (start is zero) or "start:duration".
1686 static int find_stopwatch_interval(char *string)
1691 value = strtod(string, &sp);
1693 fprintf(stderr,"Invalid stopwatch timer: %s\n", string);
1697 stopwatch_start = DOUBLE_TO_NANO_ULL(value);
1699 value = strtod(string, &sp);
1700 if (sp == string || *sp != '\0') {
1701 fprintf(stderr,"Invalid stopwatch duration time: %s\n",
1705 } else if (*sp != '\0') {
1706 fprintf(stderr,"Invalid stopwatch start timer: %s\n", string);
1709 stopwatch_end = DOUBLE_TO_NANO_ULL(value);
1710 if (stopwatch_end <= stopwatch_start) {
1711 fprintf(stderr, "Invalid stopwatch interval: %Lu -> %Lu\n",
1712 stopwatch_start, stopwatch_end);
1719 static char usage_str[] = \
1720 "[ -i <input name> ] [-o <output name> [ -s ] [ -t ] [ -q ]\n" \
1721 "[ -w start:stop ] [ -f output format ] [ -F format spec ] [ -v] \n\n" \
1722 "\t-i Input file containing trace data, or '-' for stdin\n" \
1723 "\t-o Output file. If not given, output is stdout\n" \
1724 "\t-b stdin read batching\n" \
1725 "\t-s Show per-program io statistics\n" \
1726 "\t-t Track individual ios. Will tell you the time a request took\n" \
1727 "\t to get queued, to get dispatched, and to get completed\n" \
1728 "\t-q Quiet. Don't display any stats at the end of the trace\n" \
1729 "\t-w Only parse data between the given time interval in seconds.\n" \
1730 "\t If 'start' isn't given, blkparse defaults the start time to 0\n" \
1731 "\t -f Output format. Customize the output format. The format field\n" \
1732 "\t identifies can be found in the documentation\n" \
1733 "\t-F Format specification. Can be found in the documentation\n" \
1734 "\t-v Print program version info\n\n";
1736 static void usage(char *prog)
1738 fprintf(stderr, "Usage: %s %s %s", prog, blkparse_version, usage_str);
1741 int main(int argc, char *argv[])
1745 int per_device_and_cpu_stats = 1;
1747 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
1750 if (!strcmp(optarg, "-") && !pipeline)
1752 else if (resize_devices(optarg) != 0)
1756 output_name = optarg;
1759 rb_batch = atoi(optarg);
1761 rb_batch = RB_BATCH_DEFAULT;
1764 per_process_stats = 1;
1770 per_device_and_cpu_stats = 0;
1773 if (find_stopwatch_interval(optarg) != 0)
1777 set_all_format_specs(optarg);
1780 if (add_format_spec(optarg) != 0)
1784 printf("%s version %s\n", argv[0], blkparse_version);
1792 while (optind < argc) {
1793 if (!strcmp(argv[optind], "-") && !pipeline)
1795 else if (resize_devices(argv[optind]) != 0)
1800 if (!pipeline && !ndevices) {
1805 memset(&rb_sort_root, 0, sizeof(rb_sort_root));
1806 memset(&rb_track_root, 0, sizeof(rb_track_root));
1808 signal(SIGINT, handle_sigint);
1809 signal(SIGHUP, handle_sigint);
1810 signal(SIGTERM, handle_sigint);
1812 setlocale(LC_NUMERIC, "en_US");
1815 ofp = fdopen(STDOUT_FILENO, "w");
1820 snprintf(ofname, sizeof(ofname) - 1, "%s.log", output_name);
1821 ofp = fopen(ofname, "w");
1830 ofp_buffer = malloc(4096);
1831 if (setvbuf(ofp, ofp_buffer, mode, 4096)) {
1841 if (per_process_stats)
1842 show_process_stats();
1844 if (per_device_and_cpu_stats)
1845 show_device_and_cpu_stats();