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 #define SECONDS(x) ((unsigned long long)(x) / 1000000000)
39 #define NANO_SECONDS(x) ((unsigned long long)(x) % 1000000000)
40 #define DOUBLE_TO_NANO_ULL(d) ((unsigned long long)((d) * 1000000000))
43 #define MINORMASK ((1U << MINORBITS) - 1)
44 #define MAJOR(dev) ((unsigned int) ((dev) >> MINORBITS))
45 #define MINOR(dev) ((unsigned int) ((dev) & MINORMASK))
47 #define min(a, b) ((a) < (b) ? (a) : (b))
49 #define HEADER "%D %2c %8s %5T.%9t %5p %2a %3d "
52 unsigned long qreads, qwrites, creads, cwrites, mreads, mwrites;
53 unsigned long ireads, iwrites;
54 unsigned long long qread_kb, qwrite_kb, cread_kb, cwrite_kb;
55 unsigned long long iread_kb, iwrite_kb;
56 unsigned long io_unplugs, timer_unplugs;
66 struct io_stats io_stats;
74 unsigned long long events;
75 unsigned long long last_reported_time;
76 struct io_stats io_stats;
77 unsigned long last_sequence;
81 struct per_cpu_info *cpus;
84 struct per_process_info {
87 struct io_stats io_stats;
88 struct per_process_info *hash_next, *list_next;
93 unsigned long long longest_allocation_wait[2];
94 unsigned long long longest_dispatch_wait[2];
95 unsigned long long longest_completion_wait[2];
98 #define PPI_HASH_SHIFT (8)
99 static struct per_process_info *ppi_hash[1 << PPI_HASH_SHIFT];
100 static struct per_process_info *ppi_list;
102 #define S_OPTS "i:o:b:stqw:f:F:"
103 static struct option l_opts[] = {
106 .has_arg = required_argument,
112 .has_arg = required_argument,
118 .has_arg = required_argument,
123 .name = "per program stats",
124 .has_arg = no_argument,
130 .has_arg = no_argument,
136 .has_arg = no_argument,
142 .has_arg = required_argument,
148 .has_arg = required_argument,
153 .name = "format-spec",
154 .has_arg = required_argument,
161 * for sorting the displayed output
164 struct blk_io_trace *bit;
165 struct rb_node rb_node;
170 static struct rb_root rb_sort_root;
171 static struct rb_root rb_track_root;
173 static struct trace *trace_list;
176 * for tracking individual ios
179 struct rb_node rb_node;
184 unsigned long long allocation_time;
185 unsigned long long queue_time;
186 unsigned long long dispatch_time;
187 unsigned long long completion_time;
191 static struct per_dev_info *devices;
192 static char *get_dev_name(struct per_dev_info *, char *, int);
195 static char *output_name;
197 static unsigned long long genesis_time;
198 static unsigned long long stopwatch_start; /* start from zero by default */
199 static unsigned long long stopwatch_end = ULONG_LONG_MAX; /* "infinity" */
201 static int per_process_stats;
202 static int track_ios;
204 #define RB_BATCH_DEFAULT (1024)
205 static int rb_batch = RB_BATCH_DEFAULT;
209 #define is_done() (*(volatile int *)(&done))
210 static volatile int done;
212 static inline unsigned long hash_long(unsigned long val)
216 #elif __WORDSIZE == 64
217 val *= 0x9e37fffffffc0001UL;
219 #error unknown word size
222 return val >> (__WORDSIZE - PPI_HASH_SHIFT);
225 static inline void add_process_to_hash(struct per_process_info *ppi)
227 const int hash_idx = hash_long(ppi->pid);
229 ppi->hash_next = ppi_hash[hash_idx];
230 ppi_hash[hash_idx] = ppi;
233 static inline void add_process_to_list(struct per_process_info *ppi)
235 ppi->list_next = ppi_list;
239 static struct per_process_info *find_process_by_pid(__u32 pid)
241 const int hash_idx = hash_long(pid);
242 struct per_process_info *ppi;
244 ppi = ppi_hash[hash_idx];
249 ppi = ppi->hash_next;
255 static inline int trace_rb_insert(struct trace *t)
257 struct rb_node **p = &rb_sort_root.rb_node;
258 struct rb_node *parent = NULL;
261 if (genesis_time == 0 || t->bit->time < genesis_time)
262 genesis_time = t->bit->time;
266 __t = rb_entry(parent, struct trace, rb_node);
268 if (t->bit->time < __t->bit->time)
270 else if (t->bit->time > __t->bit->time)
272 else if (t->bit->device < __t->bit->device)
274 else if (t->bit->device > __t->bit->device)
276 else if (t->bit->sequence < __t->bit->sequence)
278 else if (t->bit->sequence > __t->bit->sequence)
280 else if (t->bit->device == __t->bit->device) {
282 "sequence alias (%d) on device %d,%d!\n",
284 MAJOR(t->bit->device), MINOR(t->bit->device));
289 rb_link_node(&t->rb_node, parent, p);
290 rb_insert_color(&t->rb_node, &rb_sort_root);
294 static inline int track_rb_insert(struct io_track *iot)
296 struct rb_node **p = &rb_track_root.rb_node;
297 struct rb_node *parent = NULL;
298 struct io_track *__iot;
303 __iot = rb_entry(parent, struct io_track, rb_node);
305 if (iot->device < __iot->device)
307 else if (iot->device > __iot->device)
309 else if (iot->sector < __iot->sector)
311 else if (iot->sector > __iot->sector)
315 "sector alias (%Lu) on device %d,%d!\n",
316 (unsigned long long) iot->sector,
317 MAJOR(iot->device), MINOR(iot->device));
322 rb_link_node(&iot->rb_node, parent, p);
323 rb_insert_color(&iot->rb_node, &rb_track_root);
327 static struct io_track *__find_track(dev_t device, __u64 sector)
329 struct rb_node **p = &rb_track_root.rb_node;
330 struct rb_node *parent = NULL;
331 struct io_track *__iot;
336 __iot = rb_entry(parent, struct io_track, rb_node);
338 if (device < __iot->device)
340 else if (device > __iot->device)
342 else if (sector < __iot->sector)
344 else if (sector > __iot->sector)
353 static struct io_track *find_track(__u32 pid, dev_t device, __u64 sector)
355 struct io_track *iot;
357 iot = __find_track(device, sector);
359 iot = malloc(sizeof(*iot));
361 iot->device = device;
362 iot->sector = sector;
363 track_rb_insert(iot);
369 static void log_track_frontmerge(struct blk_io_trace *t)
371 struct io_track *iot;
376 iot = __find_track(t->device, t->sector + (t->bytes >> 9));
378 fprintf(stderr, "failed to find mergeable event\n");
382 rb_erase(&iot->rb_node, &rb_track_root);
383 iot->sector -= t->bytes >> 9;
384 track_rb_insert(iot);
387 static void log_track_getrq(struct blk_io_trace *t)
389 struct io_track *iot;
394 iot = find_track(t->pid, t->device, t->sector);
395 iot->allocation_time = t->time;
400 * return time between rq allocation and queue
402 static unsigned long long log_track_queue(struct blk_io_trace *t)
404 unsigned long long elapsed;
405 struct io_track *iot;
410 iot = find_track(t->pid, t->device, t->sector);
411 iot->queue_time = t->time;
412 elapsed = iot->queue_time - iot->allocation_time;
414 if (per_process_stats) {
415 struct per_process_info *ppi = find_process_by_pid(iot->pid);
416 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
418 if (ppi && elapsed > ppi->longest_allocation_wait[w])
419 ppi->longest_allocation_wait[w] = elapsed;
426 * return time between queue and issue
428 static unsigned long long log_track_issue(struct blk_io_trace *t)
430 unsigned long long elapsed;
431 struct io_track *iot;
435 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
438 iot = __find_track(t->device, t->sector);
440 fprintf(stderr, "failed to find issue event\n");
444 iot->dispatch_time = t->time;
445 elapsed = iot->dispatch_time - iot->queue_time;
447 if (per_process_stats) {
448 struct per_process_info *ppi = find_process_by_pid(iot->pid);
449 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
451 if (ppi && elapsed > ppi->longest_dispatch_wait[w])
452 ppi->longest_dispatch_wait[w] = elapsed;
459 * return time between dispatch and complete
461 static unsigned long long log_track_complete(struct blk_io_trace *t)
463 unsigned long long elapsed;
464 struct io_track *iot;
468 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
471 iot = __find_track(t->device, t->sector);
473 fprintf(stderr, "failed to find complete event\n");
477 iot->completion_time = t->time;
478 elapsed = iot->completion_time - iot->dispatch_time;
480 if (per_process_stats) {
481 struct per_process_info *ppi = find_process_by_pid(iot->pid);
482 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
484 if (ppi && elapsed > ppi->longest_completion_wait[w])
485 ppi->longest_completion_wait[w] = elapsed;
489 * kill the trace, we don't need it after completion
491 rb_erase(&iot->rb_node, &rb_track_root);
498 static struct io_stats *find_process_io_stats(__u32 pid, char *name)
500 struct per_process_info *ppi = find_process_by_pid(pid);
503 ppi = malloc(sizeof(*ppi));
504 memset(ppi, 0, sizeof(*ppi));
505 strncpy(ppi->name, name, sizeof(ppi->name));
507 add_process_to_hash(ppi);
508 add_process_to_list(ppi);
511 return &ppi->io_stats;
515 static void resize_cpu_info(struct per_dev_info *pdi, int cpu)
517 struct per_cpu_info *cpus = pdi->cpus;
518 int ncpus = pdi->ncpus;
519 int new_count = cpu + 1;
523 size = new_count * sizeof(struct per_cpu_info);
524 cpus = realloc(cpus, size);
527 fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n",
528 get_dev_name(pdi, name, sizeof(name)), size);
532 new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info));
533 new_space = (new_count - ncpus) * sizeof(struct per_cpu_info);
534 memset(new_start, 0, new_space);
536 pdi->ncpus = new_count;
540 static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu)
542 struct per_cpu_info *pci;
544 if (cpu >= pdi->ncpus)
545 resize_cpu_info(pdi, cpu);
547 pci = &pdi->cpus[cpu];
553 static int resize_devices(char *name)
555 int size = (ndevices + 1) * sizeof(struct per_dev_info);
557 devices = realloc(devices, size);
559 fprintf(stderr, "Out of memory, device %s (%d)\n", name, size);
562 memset(&devices[ndevices], 0, sizeof(struct per_dev_info));
563 devices[ndevices].name = name;
568 static struct per_dev_info *get_dev_info(dev_t id)
570 struct per_dev_info *pdi;
573 for (i = 0; i < ndevices; i++)
574 if (devices[i].id == id)
577 if (resize_devices(NULL) != 0)
580 pdi = &devices[ndevices - 1];
585 static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size)
588 snprintf(buffer, size, "%s", pdi->name);
590 snprintf(buffer, size, "%d,%d", MAJOR(pdi->id), MINOR(pdi->id));
594 static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit)
596 unsigned long long this = bit->time;
597 unsigned long long last = pdi->last_reported_time;
599 pdi->backwards = (this < last) ? 'B' : ' ';
600 pdi->last_reported_time = this;
603 static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t,
608 ios->qwrite_kb += t->bytes >> 10;
611 ios->qread_kb += t->bytes >> 10;
615 static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci,
618 __account_m(&pci->io_stats, t, rw);
620 if (per_process_stats) {
621 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
623 __account_m(ios, t, rw);
627 static inline void __account_q(struct io_stats *ios, struct blk_io_trace *t,
632 ios->qwrite_kb += t->bytes >> 10;
635 ios->qread_kb += t->bytes >> 10;
639 static inline void account_q(struct blk_io_trace *t, struct per_cpu_info *pci,
642 __account_q(&pci->io_stats, t, rw);
644 if (per_process_stats) {
645 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
647 __account_q(ios, t, rw);
651 static inline void __account_c(struct io_stats *ios, int rw, unsigned int bytes)
655 ios->cwrite_kb += bytes >> 10;
658 ios->cread_kb += bytes >> 10;
662 static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci,
665 __account_c(&pci->io_stats, rw, bytes);
667 if (per_process_stats) {
668 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
670 __account_c(ios, rw, bytes);
674 static inline void __account_i(struct io_stats *ios, int rw, unsigned int bytes)
678 ios->iwrite_kb += bytes >> 10;
681 ios->iread_kb += bytes >> 10;
685 static inline void account_i(struct blk_io_trace *t, struct per_cpu_info *pci,
688 __account_i(&pci->io_stats, rw, t->bytes);
690 if (per_process_stats) {
691 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
693 __account_i(ios, rw, t->bytes);
697 static inline void __account_unplug(struct io_stats *ios, int timer)
700 ios->timer_unplugs++;
705 static inline void account_unplug(struct blk_io_trace *t,
706 struct per_cpu_info *pci, int timer)
708 __account_unplug(&pci->io_stats, timer);
710 if (per_process_stats) {
711 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
713 __account_unplug(ios, timer);
717 #define VALID_SPECS "BCDFGMPQRSTU"
718 static char *override_format[256];
719 static inline int valid_spec(int spec)
721 return strchr(VALID_SPECS, spec) != NULL;
724 static void set_all_format_specs(char *optarg)
728 for (p = VALID_SPECS; *p; p++)
729 if (override_format[(int)(*p)] == NULL)
730 override_format[(int)(*p)] = strdup(optarg);
733 static int add_format_spec(char *optarg)
735 int spec = optarg[0];
737 if (!valid_spec(spec)) {
738 fprintf(stderr,"Bad format specifier %c\n", spec);
741 if (optarg[1] != ',') {
742 fprintf(stderr,"Bad format specifier - need ',' %s\n", optarg);
746 if (*optarg == '\0') {
747 fprintf(stderr,"Bad format specifier - need fmt %s\n", optarg);
752 * Set both merges (front and back)
755 override_format['B'] = strdup(optarg);
756 override_format['M'] = strdup(optarg);
758 override_format[spec] = strdup(optarg);
763 static void print_field(char *act, struct per_cpu_info *pci,
764 struct blk_io_trace *t, unsigned long long elapsed,
765 int pdu_len, unsigned char *pdu_buf, char field,
766 int minus, int has_w, int width)
772 sprintf(format, "%%-%d", width);
774 sprintf(format, "%%%d", width);
776 sprintf(format, "%%");
780 fprintf(ofp, strcat(format, "s"), act);
783 fprintf(ofp, strcat(format, "d"), pci->cpu);
786 fprintf(ofp, strcat(format, "s"), t->comm);
791 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
792 int b = t->action & BLK_TC_ACT(BLK_TC_BARRIER);
793 int s = t->action & BLK_TC_ACT(BLK_TC_SYNC);
803 fprintf(ofp, strcat(format, "s"), rwbs);
806 case 'D': /* format width ignored */
807 fprintf(ofp,"%3d,%-3d", MAJOR(t->device), MINOR(t->device));
810 fprintf(ofp, strcat(format, "d"), t->error);
813 fprintf(ofp, strcat(format, "d"), MAJOR(t->device));
816 fprintf(ofp, strcat(format, "d"), MINOR(t->device));
819 fprintf(ofp, strcat(format, "u"), t->bytes >> 9);
822 fprintf(ofp, strcat(format, "u"), t->pid);
824 case 'P': /* format width ignored */
825 if ((pdu_len > 0) && (pdu_buf != NULL)) {
827 unsigned char *p = pdu_buf;
828 for (i = 0; i < pdu_len; i++)
829 fprintf(ofp, "%02x ", *p++);
833 fprintf(ofp, strcat(format, "ld"), t->sequence);
836 fprintf(ofp, strcat(format, "lu"), t->sector);
839 sprintf(format, "%%0%dlu", has_w ? width : 9);
840 fprintf(ofp, format, NANO_SECONDS(t->time));
843 fprintf(ofp, strcat(format, "d"), SECONDS(t->time));
846 if (elapsed == -1ULL) {
847 fprintf(stderr, "Expecting elapsed value\n");
850 fprintf(ofp, strcat(format, "llu"), elapsed / 1000);
853 __u64 *depth = (__u64 *) ((char *) t + sizeof(*t));
854 fprintf(ofp, strcat(format, "u"),
855 (unsigned int) be64_to_cpu(*depth));
859 fprintf(ofp,strcat(format, "c"), field);
864 static char *parse_field(char *act, struct per_cpu_info *pci,
865 struct blk_io_trace *t, unsigned long long elapsed,
866 int pdu_len, unsigned char *pdu_buf,
872 char *p = master_format;
881 width = (width * 10) + (*p++ - '0');
882 } while ((*p) && (isdigit(*p)));
885 print_field(act, pci, t, elapsed, pdu_len, pdu_buf, *p++,
886 minus, has_w, width);
891 static char *fmt_select(int fmt_spec, struct blk_io_trace *t,
892 unsigned long long elapsed)
895 char scratch_format[1024];
897 if (override_format[fmt_spec] != NULL)
898 return override_format[fmt_spec];
901 case 'C': /* Complete */
902 if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
903 strcpy(scratch_format, HEADER);
904 strcat(scratch_format, "%P");
906 strcpy(scratch_format, HEADER "%S + %n ");
907 if (elapsed != -1ULL)
908 strcat(scratch_format, "(%8u) ");
910 strcat(scratch_format, "[%e]\n");
911 fmt = scratch_format;
914 case 'D': /* Issue */
915 if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
916 strcpy(scratch_format, HEADER);
917 strcat(scratch_format, "%P");
919 strcpy(scratch_format, HEADER "%S + %n ");
920 if (elapsed != -1ULL)
921 strcat(scratch_format, "(%8u) ");
923 strcat(scratch_format,"[%C]\n");
924 fmt = scratch_format;
927 case 'Q': /* Queue */
928 strcpy(scratch_format, HEADER "%S + %n ");
929 if (elapsed != -1ULL)
930 strcat(scratch_format, "(%8u) ");
931 strcat(scratch_format,"[%C]\n");
932 fmt = scratch_format;
935 case 'B': /* Back merge */
936 case 'F': /* Front merge */
937 case 'M': /* Front or back merge */
938 fmt = HEADER "%S + %n [%C]\n";
942 fmt = HEADER "[%C]\n";
945 case 'G': /* Get request */
946 case 'S': /* Sleep request */
947 fmt = HEADER "%S + %n [%C]\n";
950 case 'U': /* Unplug IO */
951 case 'T': /* Unplug timer */
952 fmt = HEADER "[%C] %U\n";
956 fprintf(stderr,"FATAL: Invalid format spec %c\n", fmt_spec);
964 static void process_fmt(char *act, struct per_cpu_info *pci,
965 struct blk_io_trace *t, unsigned long long elapsed,
966 int pdu_len, unsigned char *pdu_buf)
968 char *p = fmt_select(act[0], t, elapsed);
972 case '%': /* Field specifier */
975 fprintf(ofp, "%c", *p++);
977 fprintf(ofp, "%c", '%');
979 p = parse_field(act, pci, t, elapsed,
980 pdu_len, pdu_buf, p);
982 case '\\': { /* escape */
984 case 'b': fprintf(ofp, "\b"); break;
985 case 'n': fprintf(ofp, "\n"); break;
986 case 'r': fprintf(ofp, "\r"); break;
987 case 't': fprintf(ofp, "\t"); break;
990 "Invalid escape char in format %c\n",
999 fprintf(ofp, "%c", *p++);
1005 static void log_complete(struct per_cpu_info *pci, struct blk_io_trace *t,
1008 process_fmt(act, pci, t, log_track_complete(t), 0, NULL);
1011 static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t,
1014 process_fmt(act, pci, t, log_track_queue(t), 0, NULL);
1017 static void log_issue(struct per_cpu_info *pci, struct blk_io_trace *t,
1020 process_fmt(act, pci, t, log_track_issue(t), 0, NULL);
1023 static void log_merge(struct per_cpu_info *pci, struct blk_io_trace *t,
1027 log_track_frontmerge(t);
1029 process_fmt(act, pci, t, -1ULL, 0, NULL);
1032 static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t,
1035 process_fmt(act, pci, t, -1ULL, 0, NULL);
1038 static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t,
1041 process_fmt(act, pci, t, -1ULL, 0, NULL);
1044 static void log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t,
1047 process_fmt(act, pci, t, -1ULL, 0, NULL);
1050 static void log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act)
1052 unsigned char *buf = (unsigned char *) t + sizeof(*t);
1054 process_fmt(act, pci, t, -1ULL, t->pdu_len, buf);
1057 static int dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci)
1061 switch (t->action & 0xffff) {
1062 case __BLK_TA_QUEUE:
1063 log_generic(pci, t, "Q");
1065 case __BLK_TA_GETRQ:
1066 log_generic(pci, t, "G");
1068 case __BLK_TA_SLEEPRQ:
1069 log_generic(pci, t, "S");
1071 case __BLK_TA_REQUEUE:
1072 log_generic(pci, t, "R");
1074 case __BLK_TA_ISSUE:
1075 log_pc(pci, t, "D");
1077 case __BLK_TA_COMPLETE:
1078 log_pc(pci, t, "C");
1081 fprintf(stderr, "Bad pc action %x\n", t->action);
1089 static void dump_trace_fs(struct blk_io_trace *t, struct per_cpu_info *pci)
1091 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
1092 int act = t->action & 0xffff;
1095 case __BLK_TA_QUEUE:
1096 account_q(t, pci, w);
1097 log_queue(pci, t, "Q");
1099 case __BLK_TA_BACKMERGE:
1100 account_m(t, pci, w);
1101 log_merge(pci, t, "M");
1103 case __BLK_TA_FRONTMERGE:
1104 account_m(t, pci, w);
1105 log_merge(pci, t, "F");
1107 case __BLK_TA_GETRQ:
1109 log_generic(pci, t, "G");
1111 case __BLK_TA_SLEEPRQ:
1112 log_generic(pci, t, "S");
1114 case __BLK_TA_REQUEUE:
1115 account_c(t, pci, w, -t->bytes);
1116 log_queue(pci, t, "R");
1118 case __BLK_TA_ISSUE:
1119 account_i(t, pci, w);
1120 log_issue(pci, t, "D");
1122 case __BLK_TA_COMPLETE:
1123 account_c(t, pci, w, t->bytes);
1124 log_complete(pci, t, "C");
1127 log_action(pci, t, "P");
1129 case __BLK_TA_UNPLUG_IO:
1130 account_unplug(t, pci, 0);
1131 log_unplug(pci, t, "U");
1133 case __BLK_TA_UNPLUG_TIMER:
1134 account_unplug(t, pci, 1);
1135 log_unplug(pci, t, "UT");
1138 fprintf(stderr, "Bad fs action %x\n", t->action);
1143 static int dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
1144 struct per_dev_info *pdi)
1148 if (t->action & BLK_TC_ACT(BLK_TC_PC))
1149 ret = dump_trace_pc(t, pci);
1151 dump_trace_fs(t, pci);
1157 static void dump_io_stats(struct io_stats *ios, char *msg)
1159 fprintf(ofp, "%s\n", msg);
1161 fprintf(ofp, " Reads Queued: %'8lu, %'8LuKiB\t", ios->qreads, ios->qread_kb);
1162 fprintf(ofp, " Writes Queued: %'8lu, %'8LuKiB\n", ios->qwrites,ios->qwrite_kb);
1164 fprintf(ofp, " Read Dispatches: %'8lu, %'8LuKiB\t", ios->ireads, ios->iread_kb);
1165 fprintf(ofp, " Write Dispatches: %'8lu, %'8LuKiB\n", ios->iwrites,ios->iwrite_kb);
1166 fprintf(ofp, " Reads Completed: %'8lu, %'8LuKiB\t", ios->creads, ios->cread_kb);
1167 fprintf(ofp, " Writes Completed: %'8lu, %'8LuKiB\n", ios->cwrites,ios->cwrite_kb);
1168 fprintf(ofp, " Read Merges: %'8lu%8c\t", ios->mreads, ' ');
1169 fprintf(ofp, " Write Merges: %'8lu\n", ios->mwrites);
1170 fprintf(ofp, " IO unplugs: %'8lu%8c\t", ios->io_unplugs, ' ');
1171 fprintf(ofp, " Timer unplugs: %'8lu\n", ios->timer_unplugs);
1174 static void dump_wait_stats(struct per_process_info *ppi)
1176 unsigned long rawait = ppi->longest_allocation_wait[0] / 1000;
1177 unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000;
1178 unsigned long rcwait = ppi->longest_completion_wait[0] / 1000;
1179 unsigned long wawait = ppi->longest_allocation_wait[1] / 1000;
1180 unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000;
1181 unsigned long wcwait = ppi->longest_completion_wait[1] / 1000;
1183 fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' ');
1184 fprintf(ofp, " Allocation wait: %'8lu\n", wawait);
1185 fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' ');
1186 fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait);
1187 fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' ');
1188 fprintf(ofp, " Completion wait: %'8lu\n", wcwait);
1191 static void show_process_stats(void)
1193 struct per_process_info *ppi;
1197 dump_io_stats(&ppi->io_stats, ppi->name);
1198 dump_wait_stats(ppi);
1199 ppi = ppi->list_next;
1205 static void show_device_and_cpu_stats(void)
1207 struct per_dev_info *pdi;
1208 struct per_cpu_info *pci;
1209 struct io_stats total, *ios;
1210 int i, j, pci_events;
1211 char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3];
1214 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
1216 memset(&total, 0, sizeof(total));
1222 for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) {
1226 ios = &pci->io_stats;
1227 total.qreads += ios->qreads;
1228 total.qwrites += ios->qwrites;
1229 total.creads += ios->creads;
1230 total.cwrites += ios->cwrites;
1231 total.mreads += ios->mreads;
1232 total.mwrites += ios->mwrites;
1233 total.ireads += ios->ireads;
1234 total.iwrites += ios->iwrites;
1235 total.qread_kb += ios->qread_kb;
1236 total.qwrite_kb += ios->qwrite_kb;
1237 total.cread_kb += ios->cread_kb;
1238 total.cwrite_kb += ios->cwrite_kb;
1239 total.iread_kb += ios->iread_kb;
1240 total.iwrite_kb += ios->iwrite_kb;
1241 total.timer_unplugs += ios->timer_unplugs;
1242 total.io_unplugs += ios->io_unplugs;
1244 snprintf(line, sizeof(line) - 1, "CPU%d (%s):",
1245 j, get_dev_name(pdi, name, sizeof(name)));
1246 dump_io_stats(ios, line);
1250 if (pci_events > 1) {
1252 snprintf(line, sizeof(line) - 1, "Total (%s):",
1253 get_dev_name(pdi, name, sizeof(name)));
1254 dump_io_stats(&total, line);
1257 fprintf(ofp, "\nEvents (%s): %'Lu entries, %'lu skips\n",
1258 get_dev_name(pdi, line, sizeof(line)), pdi->events,
1263 static struct blk_io_trace *find_trace(void *p, unsigned long offset)
1265 unsigned long max_offset = offset;
1267 struct blk_io_trace *bit;
1270 for (off = 0; off < max_offset; off++) {
1273 magic = be32_to_cpu(bit->magic);
1274 if ((magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
1281 static int sort_entries(void)
1283 struct per_dev_info *pdi;
1284 struct per_cpu_info *pci;
1285 struct blk_io_trace *bit;
1289 while ((t = trace_list) != NULL) {
1291 trace_list = t->next;
1294 memset(&t->rb_node, 0, sizeof(t->rb_node));
1296 if (verify_trace(bit))
1298 if (trace_rb_insert(t))
1301 pdi = get_dev_info(bit->device);
1302 pci = get_cpu_info(pdi, bit->cpu);
1311 static void show_entries_rb(int piped)
1313 struct per_dev_info *pdi;
1314 struct blk_io_trace *bit;
1319 while ((n = rb_first(&rb_sort_root)) != NULL) {
1321 t = rb_entry(n, struct trace, rb_node);
1324 pdi = get_dev_info(bit->device);
1326 fprintf(stderr, "Unknown device ID? (%d,%d)\n",
1327 MAJOR(bit->device), MINOR(bit->device));
1331 if (cpu > pdi->ncpus) {
1332 fprintf(stderr, "Unknown CPU ID? (%d, device %d,%d)\n",
1333 cpu, MAJOR(bit->device), MINOR(bit->device));
1338 * back off displaying more info if we are out of sync
1339 * on SMP systems. to prevent stalling on lost events,
1340 * only allow an event to us a few times
1342 if (bit->sequence != (pdi->last_sequence + 1)) {
1343 if (piped && t->skipped < 5) {
1347 fprintf(stderr, "skipping from %lu to %u\n", pdi->last_sequence, bit->sequence);
1352 pdi->last_sequence = bit->sequence;
1354 bit->time -= genesis_time;
1355 if (bit->time < stopwatch_start)
1357 if (bit->time >= stopwatch_end)
1360 check_time(pdi, bit);
1362 if (dump_trace(bit, &pdi->cpus[cpu], pdi))
1365 rb_erase(&t->rb_node, &rb_sort_root);
1374 static int read_data(int fd, void *buffer, int bytes, int block)
1376 int ret, bytes_left, fl;
1379 fl = fcntl(fd, F_GETFL);
1382 fcntl(fd, F_SETFL, fl | O_NONBLOCK);
1384 fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
1388 while (bytes_left > 0) {
1389 ret = read(fd, p, bytes_left);
1393 if (errno != EAGAIN)
1406 * Find the traces in 'tb' and add them to the list for sorting and
1409 static int find_entries(void *tb, unsigned long size)
1411 struct blk_io_trace *bit;
1415 while (tb - start <= size - sizeof(*bit)) {
1416 bit = find_trace(tb, size - (tb - start));
1420 t = malloc(sizeof(*t));
1421 memset(t, 0, sizeof(*t));
1425 t->next = trace_list;
1428 tb += sizeof(*bit) + bit->pdu_len;
1434 static int do_file(void)
1436 int i, j, nfiles = 0, nelems;
1438 for (i = 0; i < ndevices; i++) {
1439 for (j = 0;; j++, nfiles++) {
1440 struct per_dev_info *pdi;
1441 struct per_cpu_info *pci;
1446 pci = get_cpu_info(pdi, j);
1449 snprintf(pci->fname, sizeof(pci->fname)-1,
1450 "%s.%d", pdi->name, j);
1451 if (stat(pci->fname, &st) < 0)
1456 printf("Processing %s\n", pci->fname);
1458 tb = malloc(st.st_size);
1460 fprintf(stderr, "Out of memory, skip file %s\n",
1465 pci->fd = open(pci->fname, O_RDONLY);
1472 if (read_data(pci->fd, tb, st.st_size, 1)) {
1478 if (find_entries(tb, st.st_size)) {
1483 nelems = sort_entries();
1490 printf("Completed %s (CPU%d %d, entries)\n",
1491 pci->fname, j, nelems);
1497 fprintf(stderr, "No files found\n");
1505 static int read_sort_events(int fd)
1510 struct blk_io_trace *bit;
1515 bit = malloc(sizeof(*bit));
1517 if (read_data(fd, bit, sizeof(*bit), !events))
1520 magic = be32_to_cpu(bit->magic);
1521 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
1522 fprintf(stderr, "Bad magic %x\n", magic);
1526 pdu_len = be16_to_cpu(bit->pdu_len);
1528 void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
1530 if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1))
1536 t = malloc(sizeof(*t));
1537 memset(t, 0, sizeof(*t));
1541 t->next = trace_list;
1545 } while (!is_done() && events < rb_batch);
1550 static int do_stdin(void)
1554 fd = dup(STDIN_FILENO);
1558 events = read_sort_events(fd);
1562 if (sort_entries() == -1)
1572 static void flush_output(void)
1577 static void handle_sigint(int sig)
1584 * Extract start and duration times from a string, allowing
1585 * us to specify a time interval of interest within a trace.
1586 * Format: "duration" (start is zero) or "start:duration".
1588 static int find_stopwatch_interval(char *string)
1593 value = strtod(string, &sp);
1595 fprintf(stderr,"Invalid stopwatch timer: %s\n", string);
1599 stopwatch_start = DOUBLE_TO_NANO_ULL(value);
1601 value = strtod(string, &sp);
1602 if (sp == string || *sp != '\0') {
1603 fprintf(stderr,"Invalid stopwatch duration time: %s\n",
1607 } else if (*sp != '\0') {
1608 fprintf(stderr,"Invalid stopwatch start timer: %s\n", string);
1611 stopwatch_end = stopwatch_start + DOUBLE_TO_NANO_ULL(value);
1615 static void usage(char *prog)
1617 fprintf(stderr, "Usage: %s "
1618 "[-i <name>] [-o <output>] [-s] [-w N[:n]] <name>...\n",
1622 int main(int argc, char *argv[])
1626 int per_device_and_cpu_stats = 1;
1628 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
1631 if (!strcmp(optarg, "-") && !pipeline)
1633 else if (resize_devices(optarg) != 0)
1637 output_name = optarg;
1640 rb_batch = atoi(optarg);
1642 rb_batch = RB_BATCH_DEFAULT;
1645 per_process_stats = 1;
1651 per_device_and_cpu_stats = 0;
1654 if (find_stopwatch_interval(optarg) != 0)
1658 set_all_format_specs(optarg);
1661 if (add_format_spec(optarg) != 0)
1670 while (optind < argc) {
1671 if (!strcmp(argv[optind], "-") && !pipeline)
1673 else if (resize_devices(argv[optind]) != 0)
1678 if (!pipeline && !ndevices) {
1683 memset(&rb_sort_root, 0, sizeof(rb_sort_root));
1684 memset(&rb_track_root, 0, sizeof(rb_track_root));
1686 signal(SIGINT, handle_sigint);
1687 signal(SIGHUP, handle_sigint);
1688 signal(SIGTERM, handle_sigint);
1690 setlocale(LC_NUMERIC, "en_US");
1693 ofp = fdopen(STDOUT_FILENO, "w");
1698 snprintf(ofname, sizeof(ofname) - 1, "%s.log", output_name);
1699 ofp = fopen(ofname, "w");
1708 ofp_buffer = malloc(4096);
1709 if (setvbuf(ofp, ofp_buffer, mode, 4096)) {
1719 if (per_process_stats)
1720 show_process_stats();
1722 if (per_device_and_cpu_stats)
1723 show_device_and_cpu_stats();