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>
37 #define SECONDS(x) ((unsigned long long)(x) / 1000000000)
38 #define NANO_SECONDS(x) ((unsigned long long)(x) % 1000000000)
39 #define DOUBLE_TO_NANO_ULL(d) ((unsigned long long)((d) * 1000000000))
42 #define MINORMASK ((1U << MINORBITS) - 1)
43 #define MAJOR(dev) ((unsigned int) ((dev) >> MINORBITS))
44 #define MINOR(dev) ((unsigned int) ((dev) & MINORMASK))
46 #define min(a, b) ((a) < (b) ? (a) : (b))
49 unsigned long qreads, qwrites, creads, cwrites, mreads, mwrites;
50 unsigned long ireads, iwrites;
51 unsigned long long qread_kb, qwrite_kb, cread_kb, cwrite_kb;
52 unsigned long long iread_kb, iwrite_kb;
53 unsigned long io_unplugs, timer_unplugs;
63 struct io_stats io_stats;
71 unsigned long long events;
72 unsigned long long last_reported_time;
73 struct io_stats io_stats;
74 unsigned long last_sequence;
77 struct per_cpu_info *cpus;
80 struct per_process_info {
83 struct io_stats io_stats;
84 struct per_process_info *hash_next, *list_next;
89 unsigned long long longest_allocation_wait[2];
90 unsigned long long longest_dispatch_wait[2];
91 unsigned long long longest_completion_wait[2];
94 #define PPI_HASH_SHIFT (8)
95 static struct per_process_info *ppi_hash[1 << PPI_HASH_SHIFT];
96 static struct per_process_info *ppi_list;
98 #define S_OPTS "i:o:b:stqw:"
99 static struct option l_opts[] = {
119 .name = "per program stats",
151 * for sorting the displayed output
154 struct blk_io_trace *bit;
155 struct rb_node rb_node;
160 static struct rb_root rb_sort_root;
161 static struct rb_root rb_track_root;
163 static struct trace *trace_list;
166 * for tracking individual ios
169 struct rb_node rb_node;
174 unsigned long long allocation_time;
175 unsigned long long queue_time;
176 unsigned long long dispatch_time;
177 unsigned long long completion_time;
181 static struct per_dev_info *devices;
182 static char *get_dev_name(struct per_dev_info *, char *, int);
185 static char *output_name;
187 static unsigned long long genesis_time;
188 static unsigned long long stopwatch_start; /* start from zero by default */
189 static unsigned long long stopwatch_end = ULONG_LONG_MAX; /* "infinity" */
191 static int per_process_stats;
192 static int track_ios;
194 #define RB_BATCH_DEFAULT (1024)
195 static int rb_batch = RB_BATCH_DEFAULT;
199 #define is_done() (*(volatile int *)(&done))
200 static volatile int done;
202 static inline unsigned long hash_long(unsigned long val)
206 #elif __WORDSIZE == 64
207 val *= 0x9e37fffffffc0001UL;
209 #error unknown word size
212 return val >> (__WORDSIZE - PPI_HASH_SHIFT);
215 static inline void add_process_to_hash(struct per_process_info *ppi)
217 const int hash_idx = hash_long(ppi->pid);
219 ppi->hash_next = ppi_hash[hash_idx];
220 ppi_hash[hash_idx] = ppi;
223 static inline void add_process_to_list(struct per_process_info *ppi)
225 ppi->list_next = ppi_list;
229 static struct per_process_info *find_process_by_pid(__u32 pid)
231 const int hash_idx = hash_long(pid);
232 struct per_process_info *ppi;
234 ppi = ppi_hash[hash_idx];
239 ppi = ppi->hash_next;
245 static inline int trace_rb_insert(struct trace *t)
247 struct rb_node **p = &rb_sort_root.rb_node;
248 struct rb_node *parent = NULL;
251 if (genesis_time == 0 || t->bit->time < genesis_time)
252 genesis_time = t->bit->time;
256 __t = rb_entry(parent, struct trace, rb_node);
258 if (t->bit->time < __t->bit->time)
260 else if (t->bit->time > __t->bit->time)
262 else if (t->bit->device < __t->bit->device)
264 else if (t->bit->device > __t->bit->device)
266 else if (t->bit->sequence < __t->bit->sequence)
268 else if (t->bit->sequence > __t->bit->sequence)
270 else if (t->bit->device == __t->bit->device) {
272 "sequence alias (%d) on device %d,%d!\n",
274 MAJOR(t->bit->device), MINOR(t->bit->device));
279 rb_link_node(&t->rb_node, parent, p);
280 rb_insert_color(&t->rb_node, &rb_sort_root);
284 static inline int track_rb_insert(struct io_track *iot)
286 struct rb_node **p = &rb_track_root.rb_node;
287 struct rb_node *parent = NULL;
288 struct io_track *__iot;
293 __iot = rb_entry(parent, struct io_track, rb_node);
295 if (iot->device < __iot->device)
297 else if (iot->device > __iot->device)
299 else if (iot->sector < __iot->sector)
301 else if (iot->sector > __iot->sector)
305 "sector alias (%llu) on device %d,%d!\n",
307 MAJOR(iot->device), MINOR(iot->device));
312 rb_link_node(&iot->rb_node, parent, p);
313 rb_insert_color(&iot->rb_node, &rb_track_root);
317 static struct io_track *__find_track(dev_t device, __u64 sector)
319 struct rb_node **p = &rb_track_root.rb_node;
320 struct rb_node *parent = NULL;
321 struct io_track *__iot;
326 __iot = rb_entry(parent, struct io_track, rb_node);
328 if (device < __iot->device)
330 else if (device > __iot->device)
332 else if (sector < __iot->sector)
334 else if (sector > __iot->sector)
343 static struct io_track *find_track(__u32 pid, dev_t device, __u64 sector)
345 struct io_track *iot;
347 iot = __find_track(device, sector);
349 iot = malloc(sizeof(*iot));
351 iot->device = device;
352 iot->sector = sector;
353 track_rb_insert(iot);
359 static void log_track_frontmerge(struct blk_io_trace *t)
361 struct io_track *iot;
366 iot = __find_track(t->device, t->sector + (t->bytes >> 9));
368 fprintf(stderr, "failed to find mergeable event\n");
372 rb_erase(&iot->rb_node, &rb_track_root);
373 iot->sector -= t->bytes >> 9;
374 track_rb_insert(iot);
377 static void log_track_getrq(struct blk_io_trace *t)
379 struct io_track *iot;
384 iot = find_track(t->pid, t->device, t->sector);
385 iot->allocation_time = t->time;
390 * return time between rq allocation and queue
392 static unsigned long long log_track_queue(struct blk_io_trace *t)
394 unsigned long long elapsed;
395 struct io_track *iot;
400 iot = find_track(t->pid, t->device, t->sector);
401 iot->queue_time = t->time;
402 elapsed = iot->queue_time - iot->allocation_time;
404 if (per_process_stats) {
405 struct per_process_info *ppi = find_process_by_pid(iot->pid);
406 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
408 if (ppi && elapsed > ppi->longest_allocation_wait[w])
409 ppi->longest_allocation_wait[w] = elapsed;
416 * return time between queue and issue
418 static unsigned long long log_track_issue(struct blk_io_trace *t)
420 unsigned long long elapsed;
421 struct io_track *iot;
425 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
428 iot = __find_track(t->device, t->sector);
430 fprintf(stderr, "failed to find issue event\n");
434 iot->dispatch_time = t->time;
435 elapsed = iot->dispatch_time - iot->queue_time;
437 if (per_process_stats) {
438 struct per_process_info *ppi = find_process_by_pid(iot->pid);
439 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
441 if (ppi && elapsed > ppi->longest_dispatch_wait[w])
442 ppi->longest_dispatch_wait[w] = elapsed;
449 * return time between dispatch and complete
451 static unsigned long long log_track_complete(struct blk_io_trace *t)
453 unsigned long long elapsed;
454 struct io_track *iot;
458 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
461 iot = __find_track(t->device, t->sector);
463 fprintf(stderr, "failed to find complete event\n");
467 iot->completion_time = t->time;
468 elapsed = iot->completion_time - iot->dispatch_time;
470 if (per_process_stats) {
471 struct per_process_info *ppi = find_process_by_pid(iot->pid);
472 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
474 if (ppi && elapsed > ppi->longest_completion_wait[w])
475 ppi->longest_completion_wait[w] = elapsed;
479 * kill the trace, we don't need it after completion
481 rb_erase(&iot->rb_node, &rb_track_root);
488 static struct io_stats *find_process_io_stats(__u32 pid, char *name)
490 struct per_process_info *ppi = find_process_by_pid(pid);
493 ppi = malloc(sizeof(*ppi));
494 memset(ppi, 0, sizeof(*ppi));
495 strncpy(ppi->name, name, sizeof(ppi->name));
497 add_process_to_hash(ppi);
498 add_process_to_list(ppi);
501 return &ppi->io_stats;
505 static void resize_cpu_info(struct per_dev_info *pdi, int cpu)
507 struct per_cpu_info *cpus = pdi->cpus;
508 int ncpus = pdi->ncpus;
509 int new_count = cpu + 1;
513 size = new_count * sizeof(struct per_cpu_info);
514 cpus = realloc(cpus, size);
517 fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n",
518 get_dev_name(pdi, name, sizeof(name)), size);
522 new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info));
523 new_space = (new_count - ncpus) * sizeof(struct per_cpu_info);
524 memset(new_start, 0, new_space);
526 pdi->ncpus = new_count;
530 static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu)
532 struct per_cpu_info *pci;
534 if (cpu >= pdi->ncpus)
535 resize_cpu_info(pdi, cpu);
537 pci = &pdi->cpus[cpu];
543 static int resize_devices(char *name)
545 int size = (ndevices + 1) * sizeof(struct per_dev_info);
547 devices = realloc(devices, size);
549 fprintf(stderr, "Out of memory, device %s (%d)\n", name, size);
552 memset(&devices[ndevices], 0, sizeof(struct per_dev_info));
553 devices[ndevices].name = name;
558 static struct per_dev_info *get_dev_info(dev_t id)
560 struct per_dev_info *pdi;
563 for (i = 0; i < ndevices; i++)
564 if (devices[i].id == id)
567 if (resize_devices(NULL) != 0)
570 pdi = &devices[ndevices - 1];
575 static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size)
578 snprintf(buffer, size, "%s", pdi->name);
580 snprintf(buffer, size, "%d,%d", MAJOR(pdi->id), MINOR(pdi->id));
584 static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit)
586 unsigned long long this = bit->time;
587 unsigned long long last = pdi->last_reported_time;
589 pdi->backwards = (this < last) ? 'B' : ' ';
590 pdi->last_reported_time = this;
593 static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t,
598 ios->qwrite_kb += t->bytes >> 10;
601 ios->qread_kb += t->bytes >> 10;
605 static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci,
608 __account_m(&pci->io_stats, t, rw);
610 if (per_process_stats) {
611 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
613 __account_m(ios, t, rw);
617 static inline void __account_q(struct io_stats *ios, struct blk_io_trace *t,
622 ios->qwrite_kb += t->bytes >> 10;
625 ios->qread_kb += t->bytes >> 10;
629 static inline void account_q(struct blk_io_trace *t, struct per_cpu_info *pci,
632 __account_q(&pci->io_stats, t, rw);
634 if (per_process_stats) {
635 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
637 __account_q(ios, t, rw);
641 static inline void __account_c(struct io_stats *ios, int rw, unsigned int bytes)
645 ios->cwrite_kb += bytes >> 10;
648 ios->cread_kb += bytes >> 10;
652 static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci,
655 __account_c(&pci->io_stats, rw, bytes);
657 if (per_process_stats) {
658 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
660 __account_c(ios, rw, bytes);
664 static inline void __account_i(struct io_stats *ios, int rw, unsigned int bytes)
668 ios->iwrite_kb += bytes >> 10;
671 ios->iread_kb += bytes >> 10;
675 static inline void account_i(struct blk_io_trace *t, struct per_cpu_info *pci,
678 __account_i(&pci->io_stats, rw, t->bytes);
680 if (per_process_stats) {
681 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
683 __account_i(ios, rw, t->bytes);
687 static inline void __account_unplug(struct io_stats *ios, int timer)
690 ios->timer_unplugs++;
695 static inline void account_unplug(struct blk_io_trace *t,
696 struct per_cpu_info *pci, int timer)
698 __account_unplug(&pci->io_stats, timer);
700 if (per_process_stats) {
701 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
703 __account_unplug(ios, timer);
707 static void output(struct per_cpu_info *pci, char *s)
709 fprintf(ofp, "%s", s);
712 static char hstring[256];
713 static char tstring[256];
715 static inline char *setup_header(struct per_cpu_info *pci,
716 struct blk_io_trace *t, char *act)
718 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
719 int b = t->action & BLK_TC_ACT(BLK_TC_BARRIER);
720 int s = t->action & BLK_TC_ACT(BLK_TC_SYNC);
735 sprintf(hstring, "%3d,%-3d %2d %8ld %5Lu.%09Lu %5u %2s %3s",
736 MAJOR(t->device), MINOR(t->device), pci->cpu,
737 (unsigned long)t->sequence, SECONDS(t->time),
738 NANO_SECONDS(t->time), t->pid, act, rwbs);
743 static void log_complete(struct per_cpu_info *pci, struct blk_io_trace *t,
746 unsigned long long elapsed = log_track_complete(t);
748 if (elapsed != -1ULL) {
749 unsigned long usec = elapsed / 1000;
751 sprintf(tstring,"%s %Lu + %u (%8lu) [%d]\n",
752 setup_header(pci, t, act),
753 (unsigned long long)t->sector, t->bytes >> 9,
756 sprintf(tstring,"%s %Lu + %u [%d]\n", setup_header(pci, t, act),
757 (unsigned long long)t->sector, t->bytes >> 9, t->error);
760 output(pci, tstring);
763 static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t,
766 unsigned long long elapsed = log_track_queue(t);
768 if (elapsed != -1ULL) {
769 unsigned long usec = elapsed / 1000;
771 sprintf(tstring,"%s %Lu + %u (%8lu) [%s]\n",
772 setup_header(pci, t, act),
773 (unsigned long long)t->sector, t->bytes >> 9,
776 sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act),
777 (unsigned long long)t->sector, t->bytes >> 9, t->comm);
779 output(pci, tstring);
782 static void log_issue(struct per_cpu_info *pci, struct blk_io_trace *t,
785 unsigned long long elapsed = log_track_issue(t);
787 if (elapsed != -1ULL) {
788 double usec = (double) elapsed / 1000;
790 sprintf(tstring,"%s %Lu + %u (%8.2f) [%s]\n",
791 setup_header(pci, t, act),
792 (unsigned long long)t->sector, t->bytes >> 9,
795 sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act),
796 (unsigned long long)t->sector, t->bytes >> 9, t->comm);
799 output(pci, tstring);
802 static void log_merge(struct per_cpu_info *pci, struct blk_io_trace *t,
806 log_track_frontmerge(t);
808 sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act),
809 (unsigned long long)t->sector, t->bytes >> 9, t->comm);
810 output(pci, tstring);
813 static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t,
816 sprintf(tstring,"%s [%s]\n", setup_header(pci, t, act), t->comm);
817 output(pci, tstring);
820 static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t,
823 sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act),
824 (unsigned long long)t->sector, t->bytes >> 9, t->comm);
825 output(pci, tstring);
828 static int log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t,
834 len = sprintf(tstring,"%s [%s] ", setup_header(pci, t, act), t->comm);
835 depth = (__u64 *) ((char *) t + sizeof(*t));
836 sprintf(tstring + len, "%u\n", (unsigned int) be64_to_cpu(*depth));
837 output(pci, tstring);
842 static int log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act)
847 sprintf(tstring,"%s ", setup_header(pci, t, act));
848 output(pci, tstring);
850 buf = (unsigned char *) t + sizeof(*t);
851 for (i = 0; i < t->pdu_len; i++) {
852 sprintf(tstring,"%02x ", buf[i]);
853 output(pci, tstring);
857 sprintf(tstring,"[%d]\n", t->error);
858 output(pci, tstring);
860 sprintf(tstring,"[%s]\n", t->comm);
861 output(pci, tstring);
866 static int dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci)
870 switch (t->action & 0xffff) {
872 log_generic(pci, t, "Q");
875 log_generic(pci, t, "G");
877 case __BLK_TA_SLEEPRQ:
878 log_generic(pci, t, "S");
880 case __BLK_TA_REQUEUE:
881 log_generic(pci, t, "R");
884 ret = log_pc(pci, t, "D");
886 case __BLK_TA_COMPLETE:
890 fprintf(stderr, "Bad pc action %x\n", t->action);
898 static void dump_trace_fs(struct blk_io_trace *t, struct per_cpu_info *pci)
900 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
901 int act = t->action & 0xffff;
905 account_q(t, pci, w);
906 log_queue(pci, t, "Q");
908 case __BLK_TA_BACKMERGE:
909 account_m(t, pci, w);
910 log_merge(pci, t, "M");
912 case __BLK_TA_FRONTMERGE:
913 account_m(t, pci, w);
914 log_merge(pci, t, "F");
918 log_generic(pci, t, "G");
920 case __BLK_TA_SLEEPRQ:
921 log_generic(pci, t, "S");
923 case __BLK_TA_REQUEUE:
924 account_c(t, pci, w, -t->bytes);
925 log_queue(pci, t, "R");
928 account_i(t, pci, w);
929 log_issue(pci, t, "D");
931 case __BLK_TA_COMPLETE:
932 account_c(t, pci, w, t->bytes);
933 log_complete(pci, t, "C");
936 log_action(pci, t, "P");
938 case __BLK_TA_UNPLUG_IO:
939 account_unplug(t, pci, 0);
940 log_unplug(pci, t, "U");
942 case __BLK_TA_UNPLUG_TIMER:
943 account_unplug(t, pci, 1);
944 log_unplug(pci, t, "UT");
947 fprintf(stderr, "Bad fs action %x\n", t->action);
952 static int dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
953 struct per_dev_info *pdi)
957 if (t->action & BLK_TC_ACT(BLK_TC_PC))
958 ret = dump_trace_pc(t, pci);
960 dump_trace_fs(t, pci);
966 static void dump_io_stats(struct io_stats *ios, char *msg)
968 fprintf(ofp, "%s\n", msg);
970 fprintf(ofp, " Reads Queued: %'8lu, %'8LuKiB\t", ios->qreads, ios->qread_kb);
971 fprintf(ofp, " Writes Queued: %'8lu, %'8LuKiB\n", ios->qwrites,ios->qwrite_kb);
973 fprintf(ofp, " Read Dispatches: %'8lu, %'8LuKiB\t", ios->ireads, ios->iread_kb);
974 fprintf(ofp, " Write Dispatches: %'8lu, %'8LuKiB\n", ios->iwrites,ios->iwrite_kb);
975 fprintf(ofp, " Reads Completed: %'8lu, %'8LuKiB\t", ios->creads, ios->cread_kb);
976 fprintf(ofp, " Writes Completed: %'8lu, %'8LuKiB\n", ios->cwrites,ios->cwrite_kb);
977 fprintf(ofp, " Read Merges: %'8lu%8c\t", ios->mreads, ' ');
978 fprintf(ofp, " Write Merges: %'8lu\n", ios->mwrites);
979 fprintf(ofp, " IO unplugs: %'8lu%8c\t", ios->io_unplugs, ' ');
980 fprintf(ofp, " Timer unplugs: %'8lu\n", ios->timer_unplugs);
983 static void dump_wait_stats(struct per_process_info *ppi)
985 unsigned long rawait = ppi->longest_allocation_wait[0] / 1000;
986 unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000;
987 unsigned long rcwait = ppi->longest_completion_wait[0] / 1000;
988 unsigned long wawait = ppi->longest_allocation_wait[1] / 1000;
989 unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000;
990 unsigned long wcwait = ppi->longest_completion_wait[1] / 1000;
992 fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' ');
993 fprintf(ofp, " Allocation wait: %'8lu\n", wawait);
994 fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' ');
995 fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait);
996 fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' ');
997 fprintf(ofp, " Completion wait: %'8lu\n", wcwait);
1000 static void show_process_stats(void)
1002 struct per_process_info *ppi;
1006 dump_io_stats(&ppi->io_stats, ppi->name);
1007 dump_wait_stats(ppi);
1008 ppi = ppi->list_next;
1014 static void show_device_and_cpu_stats(void)
1016 struct per_dev_info *pdi;
1017 struct per_cpu_info *pci;
1018 struct io_stats total, *ios;
1019 int i, j, pci_events;
1020 char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3];
1023 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
1025 memset(&total, 0, sizeof(total));
1031 for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) {
1035 ios = &pci->io_stats;
1036 total.qreads += ios->qreads;
1037 total.qwrites += ios->qwrites;
1038 total.creads += ios->creads;
1039 total.cwrites += ios->cwrites;
1040 total.mreads += ios->mreads;
1041 total.mwrites += ios->mwrites;
1042 total.ireads += ios->ireads;
1043 total.iwrites += ios->iwrites;
1044 total.qread_kb += ios->qread_kb;
1045 total.qwrite_kb += ios->qwrite_kb;
1046 total.cread_kb += ios->cread_kb;
1047 total.cwrite_kb += ios->cwrite_kb;
1048 total.iread_kb += ios->iread_kb;
1049 total.iwrite_kb += ios->iwrite_kb;
1050 total.timer_unplugs += ios->timer_unplugs;
1051 total.io_unplugs += ios->io_unplugs;
1053 snprintf(line, sizeof(line) - 1, "CPU%d (%s):",
1054 j, get_dev_name(pdi, name, sizeof(name)));
1055 dump_io_stats(ios, line);
1059 if (pci_events > 1) {
1061 snprintf(line, sizeof(line) - 1, "Total (%s):",
1062 get_dev_name(pdi, name, sizeof(name)));
1063 dump_io_stats(&total, line);
1066 fprintf(ofp, "Events (%s): %'Lu\n",
1067 get_dev_name(pdi, line, sizeof(line)), pdi->events);
1071 static struct blk_io_trace *find_trace(void *p, unsigned long offset)
1073 unsigned long max_offset = offset;
1075 struct blk_io_trace *bit;
1078 for (off = 0; off < max_offset; off++) {
1081 magic = be32_to_cpu(bit->magic);
1082 if ((magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
1089 static int sort_entries(void)
1091 struct per_dev_info *pdi;
1092 struct per_cpu_info *pci;
1093 struct blk_io_trace *bit;
1097 while ((t = trace_list) != NULL) {
1099 trace_list = t->next;
1102 memset(&t->rb_node, 0, sizeof(t->rb_node));
1106 if (verify_trace(bit))
1108 if (trace_rb_insert(t))
1111 pdi = get_dev_info(bit->device);
1112 pci = get_cpu_info(pdi, bit->cpu);
1121 static void show_entries_rb(void)
1123 struct per_dev_info *pdi;
1124 struct blk_io_trace *bit;
1129 while ((n = rb_first(&rb_sort_root)) != NULL) {
1131 t = rb_entry(n, struct trace, rb_node);
1134 pdi = get_dev_info(bit->device);
1136 fprintf(stderr, "Unknown device ID? (%d,%d)\n",
1137 MAJOR(bit->device), MINOR(bit->device));
1141 if (cpu > pdi->ncpus) {
1142 fprintf(stderr, "Unknown CPU ID? (%d, device %d,%d)\n",
1143 cpu, MAJOR(bit->device), MINOR(bit->device));
1148 * back off displaying more info if we are out of sync
1149 * on SMP systems. to prevent stalling on lost events,
1150 * only allow an event to skip us once
1152 if (bit->sequence != (pdi->last_sequence + 1)) {
1159 pdi->last_sequence = bit->sequence;
1161 bit->time -= genesis_time;
1162 if (bit->time < stopwatch_start)
1164 if (bit->time >= stopwatch_end)
1167 check_time(pdi, bit);
1169 if (dump_trace(bit, &pdi->cpus[cpu], pdi))
1172 rb_erase(&t->rb_node, &rb_sort_root);
1178 static int read_data(int fd, void *buffer, int bytes, int block)
1180 int ret, bytes_left, fl;
1183 fl = fcntl(fd, F_GETFL);
1186 fcntl(fd, F_SETFL, fl | O_NONBLOCK);
1188 fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
1192 while (bytes_left > 0) {
1193 ret = read(fd, p, bytes_left);
1197 if (errno != EAGAIN)
1210 * Find the traces in 'tb' and add them to the list for sorting and
1213 static int find_entries(void *tb, unsigned long size)
1215 struct blk_io_trace *bit;
1219 while (tb - start <= size - sizeof(*bit)) {
1220 bit = find_trace(tb, size - (tb - start));
1224 t = malloc(sizeof(*t));
1225 memset(t, 0, sizeof(*t));
1228 t->next = trace_list;
1231 tb += sizeof(*bit) + bit->pdu_len;
1237 static int do_file(void)
1239 struct per_dev_info *pdi;
1240 int i, j, nfiles = 0, nelems;
1242 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
1243 for (j = 0;; j++, nfiles++) {
1244 struct per_cpu_info *pci;
1248 pci = get_cpu_info(pdi, j);
1251 snprintf(pci->fname, sizeof(pci->fname)-1,
1252 "%s_out.%d", pdi->name, j);
1253 if (stat(pci->fname, &st) < 0)
1258 printf("Processing %s\n", pci->fname);
1260 tb = malloc(st.st_size);
1262 fprintf(stderr, "Out of memory, skip file %s\n",
1267 pci->fd = open(pci->fname, O_RDONLY);
1274 if (read_data(pci->fd, tb, st.st_size, 1)) {
1280 if (find_entries(tb, st.st_size)) {
1285 nelems = sort_entries();
1292 printf("Completed %s (CPU%d %d, entries)\n",
1293 pci->fname, j, nelems);
1299 fprintf(stderr, "No files found\n");
1307 static int read_sort_events(int fd)
1312 struct blk_io_trace *bit;
1317 bit = malloc(sizeof(*bit));
1319 if (read_data(fd, bit, sizeof(*bit), !events))
1322 magic = be32_to_cpu(bit->magic);
1323 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
1324 fprintf(stderr, "Bad magic %x\n", magic);
1328 pdu_len = be16_to_cpu(bit->pdu_len);
1330 void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
1332 if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1))
1338 t = malloc(sizeof(*t));
1339 memset(t, 0, sizeof(*t));
1341 t->next = trace_list;
1345 } while (!is_done() && events < rb_batch);
1350 static int do_stdin(void)
1354 fd = dup(STDIN_FILENO);
1358 events = read_sort_events(fd);
1362 if (sort_entries() == -1)
1372 static void flush_output(void)
1377 static void handle_sigint(int sig)
1384 * Extract start and duration times from a string, allowing
1385 * us to specify a time interval of interest within a trace.
1386 * Format: "duration" (start is zero) or "start:duration".
1388 static int find_stopwatch_interval(char *string)
1393 value = strtod(string, &sp);
1395 fprintf(stderr,"Invalid stopwatch timer: %s\n", string);
1399 stopwatch_start = DOUBLE_TO_NANO_ULL(value);
1401 value = strtod(string, &sp);
1402 if (sp == string || *sp != '\0') {
1403 fprintf(stderr,"Invalid stopwatch duration time: %s\n",
1407 } else if (*sp != '\0') {
1408 fprintf(stderr,"Invalid stopwatch start timer: %s\n", string);
1411 stopwatch_end = stopwatch_start + DOUBLE_TO_NANO_ULL(value);
1415 static void usage(char *prog)
1417 fprintf(stderr, "Usage: %s "
1418 "[-i <name>] [-o <output>] [-s] [-w N[:n]] <name>...\n",
1422 int main(int argc, char *argv[])
1426 int per_device_and_cpu_stats = 1;
1428 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
1431 if (!strcmp(optarg, "-") && !pipeline)
1433 else if (resize_devices(optarg) != 0)
1437 output_name = optarg;
1440 rb_batch = atoi(optarg);
1442 rb_batch = RB_BATCH_DEFAULT;
1445 per_process_stats = 1;
1451 per_device_and_cpu_stats = 0;
1454 if (find_stopwatch_interval(optarg) != 0)
1463 while (optind < argc) {
1464 if (!strcmp(argv[optind], "-") && !pipeline)
1466 else if (resize_devices(argv[optind]) != 0)
1471 if (!pipeline && !ndevices) {
1476 memset(&rb_sort_root, 0, sizeof(rb_sort_root));
1477 memset(&rb_track_root, 0, sizeof(rb_track_root));
1479 signal(SIGINT, handle_sigint);
1480 signal(SIGHUP, handle_sigint);
1481 signal(SIGTERM, handle_sigint);
1483 setlocale(LC_NUMERIC, "en_US");
1486 ofp = fdopen(STDOUT_FILENO, "w");
1491 snprintf(ofname, sizeof(ofname) - 1, "%s.log", output_name);
1492 ofp = fopen(ofname, "w");
1501 ofp_buffer = malloc(4096);
1502 if (setvbuf(ofp, ofp_buffer, mode, 4096)) {
1512 if (per_process_stats)
1513 show_process_stats();
1515 if (per_device_and_cpu_stats)
1516 show_device_and_cpu_stats();