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;
101 static int ppi_list_entries;
103 #define S_OPTS "i:o:b:stqw:f:F:"
104 static struct option l_opts[] = {
107 .has_arg = required_argument,
113 .has_arg = required_argument,
119 .has_arg = required_argument,
124 .name = "per program stats",
125 .has_arg = no_argument,
131 .has_arg = no_argument,
137 .has_arg = no_argument,
143 .has_arg = required_argument,
149 .has_arg = required_argument,
154 .name = "format-spec",
155 .has_arg = required_argument,
162 * for sorting the displayed output
165 struct blk_io_trace *bit;
166 struct rb_node rb_node;
171 static struct rb_root rb_sort_root;
172 static struct rb_root rb_track_root;
174 static struct trace *trace_list;
177 * for tracking individual ios
180 struct rb_node rb_node;
185 unsigned long long allocation_time;
186 unsigned long long queue_time;
187 unsigned long long dispatch_time;
188 unsigned long long completion_time;
192 static struct per_dev_info *devices;
193 static char *get_dev_name(struct per_dev_info *, char *, int);
196 static char *output_name;
198 static unsigned long long genesis_time;
199 static unsigned long long stopwatch_start; /* start from zero by default */
200 static unsigned long long stopwatch_end = ULONG_LONG_MAX; /* "infinity" */
202 static int per_process_stats;
203 static int track_ios;
205 #define RB_BATCH_DEFAULT (1024)
206 static int rb_batch = RB_BATCH_DEFAULT;
210 #define is_done() (*(volatile int *)(&done))
211 static volatile int done;
213 static inline unsigned long hash_long(unsigned long val)
217 #elif __WORDSIZE == 64
218 val *= 0x9e37fffffffc0001UL;
220 #error unknown word size
223 return val >> (__WORDSIZE - PPI_HASH_SHIFT);
226 static inline void add_process_to_hash(struct per_process_info *ppi)
228 const int hash_idx = hash_long(ppi->pid);
230 ppi->hash_next = ppi_hash[hash_idx];
231 ppi_hash[hash_idx] = ppi;
234 static inline void add_process_to_list(struct per_process_info *ppi)
236 ppi->list_next = ppi_list;
241 static struct per_process_info *find_process_by_pid(__u32 pid)
243 const int hash_idx = hash_long(pid);
244 struct per_process_info *ppi;
246 ppi = ppi_hash[hash_idx];
251 ppi = ppi->hash_next;
257 static inline int trace_rb_insert(struct trace *t)
259 struct rb_node **p = &rb_sort_root.rb_node;
260 struct rb_node *parent = NULL;
263 if (genesis_time == 0 || t->bit->time < genesis_time)
264 genesis_time = t->bit->time;
268 __t = rb_entry(parent, struct trace, rb_node);
270 if (t->bit->time < __t->bit->time)
272 else if (t->bit->time > __t->bit->time)
274 else if (t->bit->device < __t->bit->device)
276 else if (t->bit->device > __t->bit->device)
278 else if (t->bit->sequence < __t->bit->sequence)
280 else if (t->bit->sequence > __t->bit->sequence)
282 else if (t->bit->device == __t->bit->device) {
284 "sequence alias (%d) on device %d,%d!\n",
286 MAJOR(t->bit->device), MINOR(t->bit->device));
291 rb_link_node(&t->rb_node, parent, p);
292 rb_insert_color(&t->rb_node, &rb_sort_root);
296 static inline int track_rb_insert(struct io_track *iot)
298 struct rb_node **p = &rb_track_root.rb_node;
299 struct rb_node *parent = NULL;
300 struct io_track *__iot;
305 __iot = rb_entry(parent, struct io_track, rb_node);
307 if (iot->device < __iot->device)
309 else if (iot->device > __iot->device)
311 else if (iot->sector < __iot->sector)
313 else if (iot->sector > __iot->sector)
317 "sector alias (%Lu) on device %d,%d!\n",
318 (unsigned long long) iot->sector,
319 MAJOR(iot->device), MINOR(iot->device));
324 rb_link_node(&iot->rb_node, parent, p);
325 rb_insert_color(&iot->rb_node, &rb_track_root);
329 static struct io_track *__find_track(dev_t device, __u64 sector)
331 struct rb_node **p = &rb_track_root.rb_node;
332 struct rb_node *parent = NULL;
333 struct io_track *__iot;
338 __iot = rb_entry(parent, struct io_track, rb_node);
340 if (device < __iot->device)
342 else if (device > __iot->device)
344 else if (sector < __iot->sector)
346 else if (sector > __iot->sector)
355 static struct io_track *find_track(__u32 pid, dev_t device, __u64 sector)
357 struct io_track *iot;
359 iot = __find_track(device, sector);
361 iot = malloc(sizeof(*iot));
363 iot->device = device;
364 iot->sector = sector;
365 track_rb_insert(iot);
371 static void log_track_frontmerge(struct blk_io_trace *t)
373 struct io_track *iot;
378 iot = __find_track(t->device, t->sector + (t->bytes >> 9));
380 fprintf(stderr, "failed to find mergeable event\n");
384 rb_erase(&iot->rb_node, &rb_track_root);
385 iot->sector -= t->bytes >> 9;
386 track_rb_insert(iot);
389 static void log_track_getrq(struct blk_io_trace *t)
391 struct io_track *iot;
396 iot = find_track(t->pid, t->device, t->sector);
397 iot->allocation_time = t->time;
402 * return time between rq allocation and queue
404 static unsigned long long log_track_queue(struct blk_io_trace *t)
406 unsigned long long elapsed;
407 struct io_track *iot;
412 iot = find_track(t->pid, t->device, t->sector);
413 iot->queue_time = t->time;
414 elapsed = iot->queue_time - iot->allocation_time;
416 if (per_process_stats) {
417 struct per_process_info *ppi = find_process_by_pid(iot->pid);
418 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
420 if (ppi && elapsed > ppi->longest_allocation_wait[w])
421 ppi->longest_allocation_wait[w] = elapsed;
428 * return time between queue and issue
430 static unsigned long long log_track_issue(struct blk_io_trace *t)
432 unsigned long long elapsed;
433 struct io_track *iot;
437 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
440 iot = __find_track(t->device, t->sector);
442 fprintf(stderr, "failed to find issue event\n");
446 iot->dispatch_time = t->time;
447 elapsed = iot->dispatch_time - iot->queue_time;
449 if (per_process_stats) {
450 struct per_process_info *ppi = find_process_by_pid(iot->pid);
451 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
453 if (ppi && elapsed > ppi->longest_dispatch_wait[w])
454 ppi->longest_dispatch_wait[w] = elapsed;
461 * return time between dispatch and complete
463 static unsigned long long log_track_complete(struct blk_io_trace *t)
465 unsigned long long elapsed;
466 struct io_track *iot;
470 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
473 iot = __find_track(t->device, t->sector);
475 fprintf(stderr, "failed to find complete event\n");
479 iot->completion_time = t->time;
480 elapsed = iot->completion_time - iot->dispatch_time;
482 if (per_process_stats) {
483 struct per_process_info *ppi = find_process_by_pid(iot->pid);
484 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
486 if (ppi && elapsed > ppi->longest_completion_wait[w])
487 ppi->longest_completion_wait[w] = elapsed;
491 * kill the trace, we don't need it after completion
493 rb_erase(&iot->rb_node, &rb_track_root);
500 static struct io_stats *find_process_io_stats(__u32 pid, char *name)
502 struct per_process_info *ppi = find_process_by_pid(pid);
505 ppi = malloc(sizeof(*ppi));
506 memset(ppi, 0, sizeof(*ppi));
507 strncpy(ppi->name, name, sizeof(ppi->name));
509 add_process_to_hash(ppi);
510 add_process_to_list(ppi);
513 return &ppi->io_stats;
517 static void resize_cpu_info(struct per_dev_info *pdi, int cpu)
519 struct per_cpu_info *cpus = pdi->cpus;
520 int ncpus = pdi->ncpus;
521 int new_count = cpu + 1;
525 size = new_count * sizeof(struct per_cpu_info);
526 cpus = realloc(cpus, size);
529 fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n",
530 get_dev_name(pdi, name, sizeof(name)), size);
534 new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info));
535 new_space = (new_count - ncpus) * sizeof(struct per_cpu_info);
536 memset(new_start, 0, new_space);
538 pdi->ncpus = new_count;
542 static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu)
544 struct per_cpu_info *pci;
546 if (cpu >= pdi->ncpus)
547 resize_cpu_info(pdi, cpu);
549 pci = &pdi->cpus[cpu];
555 static int resize_devices(char *name)
557 int size = (ndevices + 1) * sizeof(struct per_dev_info);
559 devices = realloc(devices, size);
561 fprintf(stderr, "Out of memory, device %s (%d)\n", name, size);
564 memset(&devices[ndevices], 0, sizeof(struct per_dev_info));
565 devices[ndevices].name = name;
570 static struct per_dev_info *get_dev_info(dev_t id)
572 struct per_dev_info *pdi;
575 for (i = 0; i < ndevices; i++)
576 if (devices[i].id == id)
579 if (resize_devices(NULL) != 0)
582 pdi = &devices[ndevices - 1];
587 static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size)
590 snprintf(buffer, size, "%s", pdi->name);
592 snprintf(buffer, size, "%d,%d", MAJOR(pdi->id), MINOR(pdi->id));
596 static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit)
598 unsigned long long this = bit->time;
599 unsigned long long last = pdi->last_reported_time;
601 pdi->backwards = (this < last) ? 'B' : ' ';
602 pdi->last_reported_time = this;
605 static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t,
610 ios->qwrite_kb += t->bytes >> 10;
613 ios->qread_kb += t->bytes >> 10;
617 static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci,
620 __account_m(&pci->io_stats, t, rw);
622 if (per_process_stats) {
623 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
625 __account_m(ios, t, rw);
629 static inline void __account_q(struct io_stats *ios, struct blk_io_trace *t,
634 ios->qwrite_kb += t->bytes >> 10;
637 ios->qread_kb += t->bytes >> 10;
641 static inline void account_q(struct blk_io_trace *t, struct per_cpu_info *pci,
644 __account_q(&pci->io_stats, t, rw);
646 if (per_process_stats) {
647 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
649 __account_q(ios, t, rw);
653 static inline void __account_c(struct io_stats *ios, int rw, unsigned int bytes)
657 ios->cwrite_kb += bytes >> 10;
660 ios->cread_kb += bytes >> 10;
664 static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci,
667 __account_c(&pci->io_stats, rw, bytes);
669 if (per_process_stats) {
670 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
672 __account_c(ios, rw, bytes);
676 static inline void __account_i(struct io_stats *ios, int rw, unsigned int bytes)
680 ios->iwrite_kb += bytes >> 10;
683 ios->iread_kb += bytes >> 10;
687 static inline void account_i(struct blk_io_trace *t, struct per_cpu_info *pci,
690 __account_i(&pci->io_stats, rw, t->bytes);
692 if (per_process_stats) {
693 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
695 __account_i(ios, rw, t->bytes);
699 static inline void __account_unplug(struct io_stats *ios, int timer)
702 ios->timer_unplugs++;
707 static inline void account_unplug(struct blk_io_trace *t,
708 struct per_cpu_info *pci, int timer)
710 __account_unplug(&pci->io_stats, timer);
712 if (per_process_stats) {
713 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
715 __account_unplug(ios, timer);
719 #define VALID_SPECS "BCDFGMPQRSTU"
720 static char *override_format[256];
721 static inline int valid_spec(int spec)
723 return strchr(VALID_SPECS, spec) != NULL;
726 static void set_all_format_specs(char *optarg)
730 for (p = VALID_SPECS; *p; p++)
731 if (override_format[(int)(*p)] == NULL)
732 override_format[(int)(*p)] = strdup(optarg);
735 static int add_format_spec(char *optarg)
737 int spec = optarg[0];
739 if (!valid_spec(spec)) {
740 fprintf(stderr,"Bad format specifier %c\n", spec);
743 if (optarg[1] != ',') {
744 fprintf(stderr,"Bad format specifier - need ',' %s\n", optarg);
748 if (*optarg == '\0') {
749 fprintf(stderr,"Bad format specifier - need fmt %s\n", optarg);
754 * Set both merges (front and back)
757 override_format['B'] = strdup(optarg);
758 override_format['M'] = strdup(optarg);
760 override_format[spec] = strdup(optarg);
765 static void print_field(char *act, struct per_cpu_info *pci,
766 struct blk_io_trace *t, unsigned long long elapsed,
767 int pdu_len, unsigned char *pdu_buf, char field,
768 int minus, int has_w, int width)
774 sprintf(format, "%%-%d", width);
776 sprintf(format, "%%%d", width);
778 sprintf(format, "%%");
782 fprintf(ofp, strcat(format, "s"), act);
785 fprintf(ofp, strcat(format, "d"), pci->cpu);
788 fprintf(ofp, strcat(format, "s"), t->comm);
793 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
794 int b = t->action & BLK_TC_ACT(BLK_TC_BARRIER);
795 int s = t->action & BLK_TC_ACT(BLK_TC_SYNC);
805 fprintf(ofp, strcat(format, "s"), rwbs);
808 case 'D': /* format width ignored */
809 fprintf(ofp,"%3d,%-3d", MAJOR(t->device), MINOR(t->device));
812 fprintf(ofp, strcat(format, "d"), t->error);
815 fprintf(ofp, strcat(format, "d"), MAJOR(t->device));
818 fprintf(ofp, strcat(format, "d"), MINOR(t->device));
821 fprintf(ofp, strcat(format, "u"), t->bytes >> 9);
824 fprintf(ofp, strcat(format, "u"), t->pid);
826 case 'P': /* format width ignored */
827 if ((pdu_len > 0) && (pdu_buf != NULL)) {
829 unsigned char *p = pdu_buf;
830 for (i = 0; i < pdu_len; i++)
831 fprintf(ofp, "%02x ", *p++);
835 fprintf(ofp, strcat(format, "ld"), t->sequence);
838 fprintf(ofp, strcat(format, "lu"), t->sector);
841 sprintf(format, "%%0%dlu", has_w ? width : 9);
842 fprintf(ofp, format, NANO_SECONDS(t->time));
845 fprintf(ofp, strcat(format, "d"), SECONDS(t->time));
848 if (elapsed == -1ULL) {
849 fprintf(stderr, "Expecting elapsed value\n");
852 fprintf(ofp, strcat(format, "llu"), elapsed / 1000);
855 __u64 *depth = (__u64 *) ((char *) t + sizeof(*t));
856 fprintf(ofp, strcat(format, "u"),
857 (unsigned int) be64_to_cpu(*depth));
861 fprintf(ofp,strcat(format, "c"), field);
866 static char *parse_field(char *act, struct per_cpu_info *pci,
867 struct blk_io_trace *t, unsigned long long elapsed,
868 int pdu_len, unsigned char *pdu_buf,
874 char *p = master_format;
883 width = (width * 10) + (*p++ - '0');
884 } while ((*p) && (isdigit(*p)));
887 print_field(act, pci, t, elapsed, pdu_len, pdu_buf, *p++,
888 minus, has_w, width);
893 static char *fmt_select(int fmt_spec, struct blk_io_trace *t,
894 unsigned long long elapsed)
897 char scratch_format[1024];
899 if (override_format[fmt_spec] != NULL)
900 return override_format[fmt_spec];
903 case 'C': /* Complete */
904 if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
905 strcpy(scratch_format, HEADER);
906 strcat(scratch_format, "%P");
908 strcpy(scratch_format, HEADER "%S + %n ");
909 if (elapsed != -1ULL)
910 strcat(scratch_format, "(%8u) ");
912 strcat(scratch_format, "[%e]\n");
913 fmt = scratch_format;
916 case 'D': /* Issue */
917 if (t->action & BLK_TC_ACT(BLK_TC_PC)) {
918 strcpy(scratch_format, HEADER);
919 strcat(scratch_format, "%P");
921 strcpy(scratch_format, HEADER "%S + %n ");
922 if (elapsed != -1ULL)
923 strcat(scratch_format, "(%8u) ");
925 strcat(scratch_format,"[%C]\n");
926 fmt = scratch_format;
929 case 'Q': /* Queue */
930 strcpy(scratch_format, HEADER "%S + %n ");
931 if (elapsed != -1ULL)
932 strcat(scratch_format, "(%8u) ");
933 strcat(scratch_format,"[%C]\n");
934 fmt = scratch_format;
937 case 'B': /* Back merge */
938 case 'F': /* Front merge */
939 case 'M': /* Front or back merge */
940 fmt = HEADER "%S + %n [%C]\n";
944 fmt = HEADER "[%C]\n";
947 case 'G': /* Get request */
948 case 'S': /* Sleep request */
949 fmt = HEADER "%S + %n [%C]\n";
952 case 'U': /* Unplug IO */
953 case 'T': /* Unplug timer */
954 fmt = HEADER "[%C] %U\n";
958 fprintf(stderr,"FATAL: Invalid format spec %c\n", fmt_spec);
966 static void process_fmt(char *act, struct per_cpu_info *pci,
967 struct blk_io_trace *t, unsigned long long elapsed,
968 int pdu_len, unsigned char *pdu_buf)
970 char *p = fmt_select(act[0], t, elapsed);
974 case '%': /* Field specifier */
977 fprintf(ofp, "%c", *p++);
979 fprintf(ofp, "%c", '%');
981 p = parse_field(act, pci, t, elapsed,
982 pdu_len, pdu_buf, p);
984 case '\\': { /* escape */
986 case 'b': fprintf(ofp, "\b"); break;
987 case 'n': fprintf(ofp, "\n"); break;
988 case 'r': fprintf(ofp, "\r"); break;
989 case 't': fprintf(ofp, "\t"); break;
992 "Invalid escape char in format %c\n",
1001 fprintf(ofp, "%c", *p++);
1007 static void log_complete(struct per_cpu_info *pci, struct blk_io_trace *t,
1010 process_fmt(act, pci, t, log_track_complete(t), 0, NULL);
1013 static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t,
1016 process_fmt(act, pci, t, log_track_queue(t), 0, NULL);
1019 static void log_issue(struct per_cpu_info *pci, struct blk_io_trace *t,
1022 process_fmt(act, pci, t, log_track_issue(t), 0, NULL);
1025 static void log_merge(struct per_cpu_info *pci, struct blk_io_trace *t,
1029 log_track_frontmerge(t);
1031 process_fmt(act, pci, t, -1ULL, 0, NULL);
1034 static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t,
1037 process_fmt(act, pci, t, -1ULL, 0, NULL);
1040 static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t,
1043 process_fmt(act, pci, t, -1ULL, 0, NULL);
1046 static void log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t,
1049 process_fmt(act, pci, t, -1ULL, 0, NULL);
1052 static void log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act)
1054 unsigned char *buf = (unsigned char *) t + sizeof(*t);
1056 process_fmt(act, pci, t, -1ULL, t->pdu_len, buf);
1059 static int dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci)
1063 switch (t->action & 0xffff) {
1064 case __BLK_TA_QUEUE:
1065 log_generic(pci, t, "Q");
1067 case __BLK_TA_GETRQ:
1068 log_generic(pci, t, "G");
1070 case __BLK_TA_SLEEPRQ:
1071 log_generic(pci, t, "S");
1073 case __BLK_TA_REQUEUE:
1074 log_generic(pci, t, "R");
1076 case __BLK_TA_ISSUE:
1077 log_pc(pci, t, "D");
1079 case __BLK_TA_COMPLETE:
1080 log_pc(pci, t, "C");
1083 fprintf(stderr, "Bad pc action %x\n", t->action);
1091 static void dump_trace_fs(struct blk_io_trace *t, struct per_cpu_info *pci)
1093 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
1094 int act = t->action & 0xffff;
1097 case __BLK_TA_QUEUE:
1098 account_q(t, pci, w);
1099 log_queue(pci, t, "Q");
1101 case __BLK_TA_BACKMERGE:
1102 account_m(t, pci, w);
1103 log_merge(pci, t, "M");
1105 case __BLK_TA_FRONTMERGE:
1106 account_m(t, pci, w);
1107 log_merge(pci, t, "F");
1109 case __BLK_TA_GETRQ:
1111 log_generic(pci, t, "G");
1113 case __BLK_TA_SLEEPRQ:
1114 log_generic(pci, t, "S");
1116 case __BLK_TA_REQUEUE:
1117 account_c(t, pci, w, -t->bytes);
1118 log_queue(pci, t, "R");
1120 case __BLK_TA_ISSUE:
1121 account_i(t, pci, w);
1122 log_issue(pci, t, "D");
1124 case __BLK_TA_COMPLETE:
1125 account_c(t, pci, w, t->bytes);
1126 log_complete(pci, t, "C");
1129 log_action(pci, t, "P");
1131 case __BLK_TA_UNPLUG_IO:
1132 account_unplug(t, pci, 0);
1133 log_unplug(pci, t, "U");
1135 case __BLK_TA_UNPLUG_TIMER:
1136 account_unplug(t, pci, 1);
1137 log_unplug(pci, t, "UT");
1140 fprintf(stderr, "Bad fs action %x\n", t->action);
1145 static int dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
1146 struct per_dev_info *pdi)
1150 if (t->action & BLK_TC_ACT(BLK_TC_PC))
1151 ret = dump_trace_pc(t, pci);
1153 dump_trace_fs(t, pci);
1159 static void dump_io_stats(struct io_stats *ios, char *msg)
1161 fprintf(ofp, "%s\n", msg);
1163 fprintf(ofp, " Reads Queued: %'8lu, %'8LuKiB\t", ios->qreads, ios->qread_kb);
1164 fprintf(ofp, " Writes Queued: %'8lu, %'8LuKiB\n", ios->qwrites,ios->qwrite_kb);
1166 fprintf(ofp, " Read Dispatches: %'8lu, %'8LuKiB\t", ios->ireads, ios->iread_kb);
1167 fprintf(ofp, " Write Dispatches: %'8lu, %'8LuKiB\n", ios->iwrites,ios->iwrite_kb);
1168 fprintf(ofp, " Reads Completed: %'8lu, %'8LuKiB\t", ios->creads, ios->cread_kb);
1169 fprintf(ofp, " Writes Completed: %'8lu, %'8LuKiB\n", ios->cwrites,ios->cwrite_kb);
1170 fprintf(ofp, " Read Merges: %'8lu%8c\t", ios->mreads, ' ');
1171 fprintf(ofp, " Write Merges: %'8lu\n", ios->mwrites);
1172 fprintf(ofp, " IO unplugs: %'8lu%8c\t", ios->io_unplugs, ' ');
1173 fprintf(ofp, " Timer unplugs: %'8lu\n", ios->timer_unplugs);
1176 static void dump_wait_stats(struct per_process_info *ppi)
1178 unsigned long rawait = ppi->longest_allocation_wait[0] / 1000;
1179 unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000;
1180 unsigned long rcwait = ppi->longest_completion_wait[0] / 1000;
1181 unsigned long wawait = ppi->longest_allocation_wait[1] / 1000;
1182 unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000;
1183 unsigned long wcwait = ppi->longest_completion_wait[1] / 1000;
1185 fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' ');
1186 fprintf(ofp, " Allocation wait: %'8lu\n", wawait);
1187 fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' ');
1188 fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait);
1189 fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' ');
1190 fprintf(ofp, " Completion wait: %'8lu\n", wcwait);
1193 static int ppi_name_compare(const void *p1, const void *p2)
1195 struct per_process_info *ppi1 = *((struct per_process_info **) p1);
1196 struct per_process_info *ppi2 = *((struct per_process_info **) p2);
1199 res = strverscmp(ppi1->name, ppi2->name);
1201 res = ppi1->pid < ppi2->pid;
1206 static void sort_process_list(void)
1208 struct per_process_info **ppis;
1209 struct per_process_info *ppi;
1212 ppis = malloc(ppi_list_entries * sizeof(struct per_process_info *));
1217 ppi = ppi->list_next;
1220 qsort(ppis, ppi_list_entries, sizeof(*ppi), ppi_name_compare);
1222 i = ppi_list_entries - 1;
1227 ppi->list_next = ppi_list;
1233 static void show_process_stats(void)
1235 struct per_process_info *ppi;
1237 sort_process_list();
1241 dump_io_stats(&ppi->io_stats, ppi->name);
1242 dump_wait_stats(ppi);
1243 ppi = ppi->list_next;
1249 static void show_device_and_cpu_stats(void)
1251 struct per_dev_info *pdi;
1252 struct per_cpu_info *pci;
1253 struct io_stats total, *ios;
1254 int i, j, pci_events;
1255 char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3];
1258 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
1260 memset(&total, 0, sizeof(total));
1266 for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) {
1270 ios = &pci->io_stats;
1271 total.qreads += ios->qreads;
1272 total.qwrites += ios->qwrites;
1273 total.creads += ios->creads;
1274 total.cwrites += ios->cwrites;
1275 total.mreads += ios->mreads;
1276 total.mwrites += ios->mwrites;
1277 total.ireads += ios->ireads;
1278 total.iwrites += ios->iwrites;
1279 total.qread_kb += ios->qread_kb;
1280 total.qwrite_kb += ios->qwrite_kb;
1281 total.cread_kb += ios->cread_kb;
1282 total.cwrite_kb += ios->cwrite_kb;
1283 total.iread_kb += ios->iread_kb;
1284 total.iwrite_kb += ios->iwrite_kb;
1285 total.timer_unplugs += ios->timer_unplugs;
1286 total.io_unplugs += ios->io_unplugs;
1288 snprintf(line, sizeof(line) - 1, "CPU%d (%s):",
1289 j, get_dev_name(pdi, name, sizeof(name)));
1290 dump_io_stats(ios, line);
1294 if (pci_events > 1) {
1296 snprintf(line, sizeof(line) - 1, "Total (%s):",
1297 get_dev_name(pdi, name, sizeof(name)));
1298 dump_io_stats(&total, line);
1301 fprintf(ofp, "\nEvents (%s): %'Lu entries, %'lu skips\n",
1302 get_dev_name(pdi, line, sizeof(line)), pdi->events,
1307 static struct blk_io_trace *find_trace(void *p, unsigned long offset)
1309 unsigned long max_offset = offset;
1311 struct blk_io_trace *bit;
1314 for (off = 0; off < max_offset; off++) {
1317 magic = be32_to_cpu(bit->magic);
1318 if ((magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
1325 static int sort_entries(void)
1327 struct per_dev_info *pdi;
1328 struct per_cpu_info *pci;
1329 struct blk_io_trace *bit;
1333 while ((t = trace_list) != NULL) {
1335 trace_list = t->next;
1338 memset(&t->rb_node, 0, sizeof(t->rb_node));
1340 if (verify_trace(bit))
1342 if (trace_rb_insert(t))
1345 pdi = get_dev_info(bit->device);
1346 pci = get_cpu_info(pdi, bit->cpu);
1355 static void show_entries_rb(int piped)
1357 struct per_dev_info *pdi;
1358 struct blk_io_trace *bit;
1363 while ((n = rb_first(&rb_sort_root)) != NULL) {
1365 t = rb_entry(n, struct trace, rb_node);
1368 pdi = get_dev_info(bit->device);
1370 fprintf(stderr, "Unknown device ID? (%d,%d)\n",
1371 MAJOR(bit->device), MINOR(bit->device));
1375 if (cpu > pdi->ncpus) {
1376 fprintf(stderr, "Unknown CPU ID? (%d, device %d,%d)\n",
1377 cpu, MAJOR(bit->device), MINOR(bit->device));
1382 * back off displaying more info if we are out of sync
1383 * on SMP systems. to prevent stalling on lost events,
1384 * only allow an event to us a few times
1386 if (bit->sequence != (pdi->last_sequence + 1)) {
1387 if (piped && t->skipped < 5) {
1391 fprintf(stderr, "skipping from %lu to %u\n", pdi->last_sequence, bit->sequence);
1396 pdi->last_sequence = bit->sequence;
1398 bit->time -= genesis_time;
1399 if (bit->time < stopwatch_start)
1401 if (bit->time >= stopwatch_end)
1404 check_time(pdi, bit);
1406 if (dump_trace(bit, &pdi->cpus[cpu], pdi))
1409 rb_erase(&t->rb_node, &rb_sort_root);
1418 static int read_data(int fd, void *buffer, int bytes, int block)
1420 int ret, bytes_left, fl;
1423 fl = fcntl(fd, F_GETFL);
1426 fcntl(fd, F_SETFL, fl | O_NONBLOCK);
1428 fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
1432 while (bytes_left > 0) {
1433 ret = read(fd, p, bytes_left);
1437 if (errno != EAGAIN)
1450 * Find the traces in 'tb' and add them to the list for sorting and
1453 static int find_entries(void *tb, unsigned long size)
1455 struct blk_io_trace *bit;
1459 while (tb - start <= size - sizeof(*bit)) {
1460 bit = find_trace(tb, size - (tb - start));
1464 t = malloc(sizeof(*t));
1465 memset(t, 0, sizeof(*t));
1469 t->next = trace_list;
1472 tb += sizeof(*bit) + bit->pdu_len;
1478 static int do_file(void)
1480 int i, j, nfiles = 0, nelems;
1482 for (i = 0; i < ndevices; i++) {
1483 for (j = 0;; j++, nfiles++) {
1484 struct per_dev_info *pdi;
1485 struct per_cpu_info *pci;
1490 pci = get_cpu_info(pdi, j);
1493 snprintf(pci->fname, sizeof(pci->fname)-1,
1494 "%s.%d", pdi->name, j);
1495 if (stat(pci->fname, &st) < 0)
1500 printf("Processing %s\n", pci->fname);
1502 tb = malloc(st.st_size);
1504 fprintf(stderr, "Out of memory, skip file %s\n",
1509 pci->fd = open(pci->fname, O_RDONLY);
1516 if (read_data(pci->fd, tb, st.st_size, 1)) {
1522 if (find_entries(tb, st.st_size)) {
1527 nelems = sort_entries();
1534 printf("Completed %s (CPU%d %d, entries)\n",
1535 pci->fname, j, nelems);
1541 fprintf(stderr, "No files found\n");
1549 static int read_sort_events(int fd)
1554 struct blk_io_trace *bit;
1559 bit = malloc(sizeof(*bit));
1561 if (read_data(fd, bit, sizeof(*bit), !events))
1564 magic = be32_to_cpu(bit->magic);
1565 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
1566 fprintf(stderr, "Bad magic %x\n", magic);
1570 pdu_len = be16_to_cpu(bit->pdu_len);
1572 void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
1574 if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1))
1580 t = malloc(sizeof(*t));
1581 memset(t, 0, sizeof(*t));
1585 t->next = trace_list;
1589 } while (!is_done() && events < rb_batch);
1594 static int do_stdin(void)
1598 fd = dup(STDIN_FILENO);
1602 events = read_sort_events(fd);
1606 if (sort_entries() == -1)
1616 static void flush_output(void)
1621 static void handle_sigint(int sig)
1628 * Extract start and duration times from a string, allowing
1629 * us to specify a time interval of interest within a trace.
1630 * Format: "duration" (start is zero) or "start:duration".
1632 static int find_stopwatch_interval(char *string)
1637 value = strtod(string, &sp);
1639 fprintf(stderr,"Invalid stopwatch timer: %s\n", string);
1643 stopwatch_start = DOUBLE_TO_NANO_ULL(value);
1645 value = strtod(string, &sp);
1646 if (sp == string || *sp != '\0') {
1647 fprintf(stderr,"Invalid stopwatch duration time: %s\n",
1651 } else if (*sp != '\0') {
1652 fprintf(stderr,"Invalid stopwatch start timer: %s\n", string);
1655 stopwatch_end = stopwatch_start + DOUBLE_TO_NANO_ULL(value);
1659 static void usage(char *prog)
1661 fprintf(stderr, "Usage: %s "
1662 "[-i <name>] [-o <output>] [-s] [-w N[:n]] <name>...\n",
1666 int main(int argc, char *argv[])
1670 int per_device_and_cpu_stats = 1;
1672 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
1675 if (!strcmp(optarg, "-") && !pipeline)
1677 else if (resize_devices(optarg) != 0)
1681 output_name = optarg;
1684 rb_batch = atoi(optarg);
1686 rb_batch = RB_BATCH_DEFAULT;
1689 per_process_stats = 1;
1695 per_device_and_cpu_stats = 0;
1698 if (find_stopwatch_interval(optarg) != 0)
1702 set_all_format_specs(optarg);
1705 if (add_format_spec(optarg) != 0)
1714 while (optind < argc) {
1715 if (!strcmp(argv[optind], "-") && !pipeline)
1717 else if (resize_devices(argv[optind]) != 0)
1722 if (!pipeline && !ndevices) {
1727 memset(&rb_sort_root, 0, sizeof(rb_sort_root));
1728 memset(&rb_track_root, 0, sizeof(rb_track_root));
1730 signal(SIGINT, handle_sigint);
1731 signal(SIGHUP, handle_sigint);
1732 signal(SIGTERM, handle_sigint);
1734 setlocale(LC_NUMERIC, "en_US");
1737 ofp = fdopen(STDOUT_FILENO, "w");
1742 snprintf(ofname, sizeof(ofname) - 1, "%s.log", output_name);
1743 ofp = fopen(ofname, "w");
1752 ofp_buffer = malloc(4096);
1753 if (setvbuf(ofp, ofp_buffer, mode, 4096)) {
1763 if (per_process_stats)
1764 show_process_stats();
1766 if (per_device_and_cpu_stats)
1767 show_device_and_cpu_stats();