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;
367 * this can happen if we lose events, so don't print an error
369 iot = __find_track(t->device, t->sector + (t->bytes >> 9));
371 fprintf(stderr, "failed to find mergeable event\n");
375 rb_erase(&iot->rb_node, &rb_track_root);
376 iot->sector -= t->bytes >> 9;
377 track_rb_insert(iot);
380 static void log_track_getrq(struct blk_io_trace *t)
382 struct io_track *iot;
387 iot = find_track(t->pid, t->device, t->sector);
388 iot->allocation_time = t->time;
393 * return time between rq allocation and queue
395 static unsigned long long log_track_queue(struct blk_io_trace *t)
397 unsigned long long elapsed;
398 struct io_track *iot;
403 iot = find_track(t->pid, t->device, t->sector);
404 iot->queue_time = t->time;
405 elapsed = iot->queue_time - iot->allocation_time;
407 if (per_process_stats) {
408 struct per_process_info *ppi = find_process_by_pid(iot->pid);
409 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
411 if (ppi && elapsed > ppi->longest_allocation_wait[w])
412 ppi->longest_allocation_wait[w] = elapsed;
419 * return time between queue and issue
421 static unsigned long long log_track_issue(struct blk_io_trace *t)
423 unsigned long long elapsed;
424 struct io_track *iot;
428 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
432 * this can happen if we lose events, so don't print an error
434 iot = __find_track(t->device, t->sector);
436 fprintf(stderr, "failed to find issue event\n");
440 iot->dispatch_time = t->time;
441 elapsed = iot->dispatch_time - iot->queue_time;
443 if (per_process_stats) {
444 struct per_process_info *ppi = find_process_by_pid(iot->pid);
445 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
447 if (ppi && elapsed > ppi->longest_dispatch_wait[w])
448 ppi->longest_dispatch_wait[w] = elapsed;
455 * return time between dispatch and complete
457 static unsigned long long log_track_complete(struct blk_io_trace *t)
459 unsigned long long elapsed;
460 struct io_track *iot;
464 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
467 iot = __find_track(t->device, t->sector);
469 fprintf(stderr, "failed to find complete event\n");
473 iot->completion_time = t->time;
474 elapsed = iot->completion_time - iot->dispatch_time;
476 if (per_process_stats) {
477 struct per_process_info *ppi = find_process_by_pid(iot->pid);
478 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
480 if (ppi && elapsed > ppi->longest_completion_wait[w])
481 ppi->longest_completion_wait[w] = elapsed;
485 * kill the trace, we don't need it after completion
487 rb_erase(&iot->rb_node, &rb_track_root);
494 static struct io_stats *find_process_io_stats(__u32 pid, char *name)
496 struct per_process_info *ppi = find_process_by_pid(pid);
499 ppi = malloc(sizeof(*ppi));
500 memset(ppi, 0, sizeof(*ppi));
501 strncpy(ppi->name, name, sizeof(ppi->name));
503 add_process_to_hash(ppi);
504 add_process_to_list(ppi);
507 return &ppi->io_stats;
511 static void resize_cpu_info(struct per_dev_info *pdi, int cpu)
513 struct per_cpu_info *cpus = pdi->cpus;
514 int ncpus = pdi->ncpus;
515 int new_count = cpu + 1;
519 size = new_count * sizeof(struct per_cpu_info);
520 cpus = realloc(cpus, size);
523 fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n",
524 get_dev_name(pdi, name, sizeof(name)), size);
528 new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info));
529 new_space = (new_count - ncpus) * sizeof(struct per_cpu_info);
530 memset(new_start, 0, new_space);
532 pdi->ncpus = new_count;
536 static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu)
538 struct per_cpu_info *pci;
540 if (cpu >= pdi->ncpus)
541 resize_cpu_info(pdi, cpu);
543 pci = &pdi->cpus[cpu];
549 static int resize_devices(char *name)
551 int size = (ndevices + 1) * sizeof(struct per_dev_info);
553 devices = realloc(devices, size);
555 fprintf(stderr, "Out of memory, device %s (%d)\n", name, size);
558 memset(&devices[ndevices], 0, sizeof(struct per_dev_info));
559 devices[ndevices].name = name;
564 static struct per_dev_info *get_dev_info(dev_t id)
566 struct per_dev_info *pdi;
569 for (i = 0; i < ndevices; i++)
570 if (devices[i].id == id)
573 if (resize_devices(NULL) != 0)
576 pdi = &devices[ndevices - 1];
581 static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size)
584 snprintf(buffer, size, "%s", pdi->name);
586 snprintf(buffer, size, "%d,%d", MAJOR(pdi->id), MINOR(pdi->id));
590 static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit)
592 unsigned long long this = bit->time;
593 unsigned long long last = pdi->last_reported_time;
595 pdi->backwards = (this < last) ? 'B' : ' ';
596 pdi->last_reported_time = this;
599 static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t,
604 ios->qwrite_kb += t->bytes >> 10;
607 ios->qread_kb += t->bytes >> 10;
611 static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci,
614 __account_m(&pci->io_stats, t, rw);
616 if (per_process_stats) {
617 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
619 __account_m(ios, t, rw);
623 static inline void __account_q(struct io_stats *ios, struct blk_io_trace *t,
628 ios->qwrite_kb += t->bytes >> 10;
631 ios->qread_kb += t->bytes >> 10;
635 static inline void account_q(struct blk_io_trace *t, struct per_cpu_info *pci,
638 __account_q(&pci->io_stats, t, rw);
640 if (per_process_stats) {
641 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
643 __account_q(ios, t, rw);
647 static inline void __account_c(struct io_stats *ios, int rw, unsigned int bytes)
651 ios->cwrite_kb += bytes >> 10;
654 ios->cread_kb += bytes >> 10;
658 static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci,
661 __account_c(&pci->io_stats, rw, bytes);
663 if (per_process_stats) {
664 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
666 __account_c(ios, rw, bytes);
670 static inline void __account_i(struct io_stats *ios, int rw, unsigned int bytes)
674 ios->iwrite_kb += bytes >> 10;
677 ios->iread_kb += bytes >> 10;
681 static inline void account_i(struct blk_io_trace *t, struct per_cpu_info *pci,
684 __account_i(&pci->io_stats, rw, t->bytes);
686 if (per_process_stats) {
687 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
689 __account_i(ios, rw, t->bytes);
693 static inline void __account_unplug(struct io_stats *ios, int timer)
696 ios->timer_unplugs++;
701 static inline void account_unplug(struct blk_io_trace *t,
702 struct per_cpu_info *pci, int timer)
704 __account_unplug(&pci->io_stats, timer);
706 if (per_process_stats) {
707 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
709 __account_unplug(ios, timer);
713 static void output(struct per_cpu_info *pci, char *s)
715 fprintf(ofp, "%s", s);
718 static char hstring[256];
719 static char tstring[256];
721 static inline char *setup_header(struct per_cpu_info *pci,
722 struct blk_io_trace *t, char *act)
724 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
725 int b = t->action & BLK_TC_ACT(BLK_TC_BARRIER);
726 int s = t->action & BLK_TC_ACT(BLK_TC_SYNC);
741 sprintf(hstring, "%3d,%-3d %2d %8ld %5Lu.%09Lu %5u %2s %3s",
742 MAJOR(t->device), MINOR(t->device), pci->cpu,
743 (unsigned long)t->sequence, SECONDS(t->time),
744 NANO_SECONDS(t->time), t->pid, act, rwbs);
749 static void log_complete(struct per_cpu_info *pci, struct blk_io_trace *t,
752 unsigned long long elapsed = log_track_complete(t);
754 if (elapsed != -1ULL) {
755 unsigned long usec = elapsed / 1000;
757 sprintf(tstring,"%s %Lu + %u (%8lu) [%d]\n",
758 setup_header(pci, t, act),
759 (unsigned long long)t->sector, t->bytes >> 9,
762 sprintf(tstring,"%s %Lu + %u [%d]\n", setup_header(pci, t, act),
763 (unsigned long long)t->sector, t->bytes >> 9, t->error);
766 output(pci, tstring);
769 static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t,
772 unsigned long long elapsed = log_track_queue(t);
774 if (elapsed != -1ULL) {
775 unsigned long usec = elapsed / 1000;
777 sprintf(tstring,"%s %Lu + %u (%8lu) [%s]\n",
778 setup_header(pci, t, act),
779 (unsigned long long)t->sector, t->bytes >> 9,
782 sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act),
783 (unsigned long long)t->sector, t->bytes >> 9, t->comm);
785 output(pci, tstring);
788 static void log_issue(struct per_cpu_info *pci, struct blk_io_trace *t,
791 unsigned long long elapsed = log_track_issue(t);
793 if (elapsed != -1ULL) {
794 double usec = (double) elapsed / 1000;
796 sprintf(tstring,"%s %Lu + %u (%8.2f) [%s]\n",
797 setup_header(pci, t, act),
798 (unsigned long long)t->sector, t->bytes >> 9,
801 sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act),
802 (unsigned long long)t->sector, t->bytes >> 9, t->comm);
805 output(pci, tstring);
808 static void log_merge(struct per_cpu_info *pci, struct blk_io_trace *t,
812 log_track_frontmerge(t);
814 sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act),
815 (unsigned long long)t->sector, t->bytes >> 9, t->comm);
816 output(pci, tstring);
819 static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t,
822 sprintf(tstring,"%s [%s]\n", setup_header(pci, t, act), t->comm);
823 output(pci, tstring);
826 static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t,
829 sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act),
830 (unsigned long long)t->sector, t->bytes >> 9, t->comm);
831 output(pci, tstring);
834 static int log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t,
840 len = sprintf(tstring,"%s [%s] ", setup_header(pci, t, act), t->comm);
841 depth = (__u64 *) ((char *) t + sizeof(*t));
842 sprintf(tstring + len, "%u\n", (unsigned int) be64_to_cpu(*depth));
843 output(pci, tstring);
848 static int log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act)
853 sprintf(tstring,"%s ", setup_header(pci, t, act));
854 output(pci, tstring);
856 buf = (unsigned char *) t + sizeof(*t);
857 for (i = 0; i < t->pdu_len; i++) {
858 sprintf(tstring,"%02x ", buf[i]);
859 output(pci, tstring);
863 sprintf(tstring,"[%d]\n", t->error);
864 output(pci, tstring);
866 sprintf(tstring,"[%s]\n", t->comm);
867 output(pci, tstring);
872 static int dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci)
876 switch (t->action & 0xffff) {
878 log_generic(pci, t, "Q");
881 log_generic(pci, t, "G");
883 case __BLK_TA_SLEEPRQ:
884 log_generic(pci, t, "S");
886 case __BLK_TA_REQUEUE:
887 log_generic(pci, t, "R");
890 ret = log_pc(pci, t, "D");
892 case __BLK_TA_COMPLETE:
896 fprintf(stderr, "Bad pc action %x\n", t->action);
904 static void dump_trace_fs(struct blk_io_trace *t, struct per_cpu_info *pci)
906 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
907 int act = t->action & 0xffff;
911 account_q(t, pci, w);
912 log_queue(pci, t, "Q");
914 case __BLK_TA_BACKMERGE:
915 account_m(t, pci, w);
916 log_merge(pci, t, "M");
918 case __BLK_TA_FRONTMERGE:
919 account_m(t, pci, w);
920 log_merge(pci, t, "F");
924 log_generic(pci, t, "G");
926 case __BLK_TA_SLEEPRQ:
927 log_generic(pci, t, "S");
929 case __BLK_TA_REQUEUE:
930 account_c(t, pci, w, -t->bytes);
931 log_queue(pci, t, "R");
934 account_i(t, pci, w);
935 log_issue(pci, t, "D");
937 case __BLK_TA_COMPLETE:
938 account_c(t, pci, w, t->bytes);
939 log_complete(pci, t, "C");
942 log_action(pci, t, "P");
944 case __BLK_TA_UNPLUG_IO:
945 account_unplug(t, pci, 0);
946 log_unplug(pci, t, "U");
948 case __BLK_TA_UNPLUG_TIMER:
949 account_unplug(t, pci, 1);
950 log_unplug(pci, t, "UT");
953 fprintf(stderr, "Bad fs action %x\n", t->action);
958 static int dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
959 struct per_dev_info *pdi)
963 if (t->action & BLK_TC_ACT(BLK_TC_PC))
964 ret = dump_trace_pc(t, pci);
966 dump_trace_fs(t, pci);
972 static void dump_io_stats(struct io_stats *ios, char *msg)
974 fprintf(ofp, "%s\n", msg);
976 fprintf(ofp, " Reads Queued: %'8lu, %'8LuKiB\t", ios->qreads, ios->qread_kb);
977 fprintf(ofp, " Writes Queued: %'8lu, %'8LuKiB\n", ios->qwrites,ios->qwrite_kb);
979 fprintf(ofp, " Read Dispatches: %'8lu, %'8LuKiB\t", ios->ireads, ios->iread_kb);
980 fprintf(ofp, " Write Dispatches: %'8lu, %'8LuKiB\n", ios->iwrites,ios->iwrite_kb);
981 fprintf(ofp, " Reads Completed: %'8lu, %'8LuKiB\t", ios->creads, ios->cread_kb);
982 fprintf(ofp, " Writes Completed: %'8lu, %'8LuKiB\n", ios->cwrites,ios->cwrite_kb);
983 fprintf(ofp, " Read Merges: %'8lu%8c\t", ios->mreads, ' ');
984 fprintf(ofp, " Write Merges: %'8lu\n", ios->mwrites);
985 fprintf(ofp, " IO unplugs: %'8lu%8c\t", ios->io_unplugs, ' ');
986 fprintf(ofp, " Timer unplugs: %'8lu\n", ios->timer_unplugs);
989 static void dump_wait_stats(struct per_process_info *ppi)
991 unsigned long rawait = ppi->longest_allocation_wait[0] / 1000;
992 unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000;
993 unsigned long rcwait = ppi->longest_completion_wait[0] / 1000;
994 unsigned long wawait = ppi->longest_allocation_wait[1] / 1000;
995 unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000;
996 unsigned long wcwait = ppi->longest_completion_wait[1] / 1000;
998 fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' ');
999 fprintf(ofp, " Allocation wait: %'8lu\n", wawait);
1000 fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' ');
1001 fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait);
1002 fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' ');
1003 fprintf(ofp, " Completion wait: %'8lu\n", wcwait);
1006 static void show_process_stats(void)
1008 struct per_process_info *ppi;
1012 dump_io_stats(&ppi->io_stats, ppi->name);
1013 dump_wait_stats(ppi);
1014 ppi = ppi->list_next;
1020 static void show_device_and_cpu_stats(void)
1022 struct per_dev_info *pdi;
1023 struct per_cpu_info *pci;
1024 struct io_stats total, *ios;
1025 int i, j, pci_events;
1026 char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3];
1029 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
1031 memset(&total, 0, sizeof(total));
1037 for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) {
1041 ios = &pci->io_stats;
1042 total.qreads += ios->qreads;
1043 total.qwrites += ios->qwrites;
1044 total.creads += ios->creads;
1045 total.cwrites += ios->cwrites;
1046 total.mreads += ios->mreads;
1047 total.mwrites += ios->mwrites;
1048 total.ireads += ios->ireads;
1049 total.iwrites += ios->iwrites;
1050 total.qread_kb += ios->qread_kb;
1051 total.qwrite_kb += ios->qwrite_kb;
1052 total.cread_kb += ios->cread_kb;
1053 total.cwrite_kb += ios->cwrite_kb;
1054 total.iread_kb += ios->iread_kb;
1055 total.iwrite_kb += ios->iwrite_kb;
1056 total.timer_unplugs += ios->timer_unplugs;
1057 total.io_unplugs += ios->io_unplugs;
1059 snprintf(line, sizeof(line) - 1, "CPU%d (%s):",
1060 j, get_dev_name(pdi, name, sizeof(name)));
1061 dump_io_stats(ios, line);
1065 if (pci_events > 1) {
1067 snprintf(line, sizeof(line) - 1, "Total (%s):",
1068 get_dev_name(pdi, name, sizeof(name)));
1069 dump_io_stats(&total, line);
1072 fprintf(ofp, "Events (%s): %'Lu\n",
1073 get_dev_name(pdi, line, sizeof(line)), pdi->events);
1077 static struct blk_io_trace *find_trace(void *p, unsigned long offset)
1079 unsigned long max_offset = offset;
1081 struct blk_io_trace *bit;
1084 for (off = 0; off < max_offset; off++) {
1087 magic = be32_to_cpu(bit->magic);
1088 if ((magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
1095 static int sort_entries(void)
1097 struct per_dev_info *pdi;
1098 struct per_cpu_info *pci;
1099 struct blk_io_trace *bit;
1103 while ((t = trace_list) != NULL) {
1105 trace_list = t->next;
1108 memset(&t->rb_node, 0, sizeof(t->rb_node));
1112 if (verify_trace(bit))
1114 if (trace_rb_insert(t))
1117 pdi = get_dev_info(bit->device);
1118 pci = get_cpu_info(pdi, bit->cpu);
1127 static void show_entries_rb(void)
1129 struct per_dev_info *pdi;
1130 struct blk_io_trace *bit;
1135 while ((n = rb_first(&rb_sort_root)) != NULL) {
1137 t = rb_entry(n, struct trace, rb_node);
1140 pdi = get_dev_info(bit->device);
1142 fprintf(stderr, "Unknown device ID? (%d,%d)\n",
1143 MAJOR(bit->device), MINOR(bit->device));
1147 if (cpu > pdi->ncpus) {
1148 fprintf(stderr, "Unknown CPU ID? (%d, device %d,%d)\n",
1149 cpu, MAJOR(bit->device), MINOR(bit->device));
1154 * back off displaying more info if we are out of sync
1155 * on SMP systems. to prevent stalling on lost events,
1156 * only allow an event to skip us once
1158 if (bit->sequence != (pdi->last_sequence + 1)) {
1165 pdi->last_sequence = bit->sequence;
1167 bit->time -= genesis_time;
1168 if (bit->time < stopwatch_start)
1170 if (bit->time >= stopwatch_end)
1173 check_time(pdi, bit);
1175 if (dump_trace(bit, &pdi->cpus[cpu], pdi))
1178 rb_erase(&t->rb_node, &rb_sort_root);
1184 static int read_data(int fd, void *buffer, int bytes, int block)
1186 int ret, bytes_left, fl;
1189 fl = fcntl(fd, F_GETFL);
1192 fcntl(fd, F_SETFL, fl | O_NONBLOCK);
1194 fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
1198 while (bytes_left > 0) {
1199 ret = read(fd, p, bytes_left);
1203 if (errno != EAGAIN)
1216 * Find the traces in 'tb' and add them to the list for sorting and
1219 static int find_entries(void *tb, unsigned long size)
1221 struct blk_io_trace *bit;
1225 while (tb - start <= size - sizeof(*bit)) {
1226 bit = find_trace(tb, size - (tb - start));
1230 t = malloc(sizeof(*t));
1231 memset(t, 0, sizeof(*t));
1234 t->next = trace_list;
1237 tb += sizeof(*bit) + bit->pdu_len;
1243 static int do_file(void)
1245 struct per_dev_info *pdi;
1246 int i, j, nfiles = 0, nelems;
1248 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
1249 for (j = 0;; j++, nfiles++) {
1250 struct per_cpu_info *pci;
1254 pci = get_cpu_info(pdi, j);
1257 snprintf(pci->fname, sizeof(pci->fname)-1,
1258 "%s_out.%d", pdi->name, j);
1259 if (stat(pci->fname, &st) < 0)
1264 printf("Processing %s\n", pci->fname);
1266 tb = malloc(st.st_size);
1268 fprintf(stderr, "Out of memory, skip file %s\n",
1273 pci->fd = open(pci->fname, O_RDONLY);
1280 if (read_data(pci->fd, tb, st.st_size, 1)) {
1286 if (find_entries(tb, st.st_size)) {
1291 nelems = sort_entries();
1298 printf("Completed %s (CPU%d %d, entries)\n",
1299 pci->fname, j, nelems);
1305 fprintf(stderr, "No files found\n");
1313 static int read_sort_events(int fd)
1318 struct blk_io_trace *bit;
1323 bit = malloc(sizeof(*bit));
1325 if (read_data(fd, bit, sizeof(*bit), !events))
1328 magic = be32_to_cpu(bit->magic);
1329 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
1330 fprintf(stderr, "Bad magic %x\n", magic);
1334 pdu_len = be16_to_cpu(bit->pdu_len);
1336 void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
1338 if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1))
1344 t = malloc(sizeof(*t));
1345 memset(t, 0, sizeof(*t));
1347 t->next = trace_list;
1351 } while (!is_done() && events < rb_batch);
1356 static int do_stdin(void)
1360 fd = dup(STDIN_FILENO);
1364 events = read_sort_events(fd);
1368 if (sort_entries() == -1)
1378 static void flush_output(void)
1383 static void handle_sigint(int sig)
1390 * Extract start and duration times from a string, allowing
1391 * us to specify a time interval of interest within a trace.
1392 * Format: "duration" (start is zero) or "start:duration".
1394 static int find_stopwatch_interval(char *string)
1399 value = strtod(string, &sp);
1401 fprintf(stderr,"Invalid stopwatch timer: %s\n", string);
1405 stopwatch_start = DOUBLE_TO_NANO_ULL(value);
1407 value = strtod(string, &sp);
1408 if (sp == string || *sp != '\0') {
1409 fprintf(stderr,"Invalid stopwatch duration time: %s\n",
1413 } else if (*sp != '\0') {
1414 fprintf(stderr,"Invalid stopwatch start timer: %s\n", string);
1417 stopwatch_end = stopwatch_start + DOUBLE_TO_NANO_ULL(value);
1421 static void usage(char *prog)
1423 fprintf(stderr, "Usage: %s "
1424 "[-i <name>] [-o <output>] [-s] [-w N[:n]] <name>...\n",
1428 int main(int argc, char *argv[])
1432 int per_device_and_cpu_stats = 1;
1434 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
1437 if (!strcmp(optarg, "-") && !pipeline)
1439 else if (resize_devices(optarg) != 0)
1443 output_name = optarg;
1446 rb_batch = atoi(optarg);
1448 rb_batch = RB_BATCH_DEFAULT;
1451 per_process_stats = 1;
1457 per_device_and_cpu_stats = 0;
1460 if (find_stopwatch_interval(optarg) != 0)
1469 while (optind < argc) {
1470 if (!strcmp(argv[optind], "-") && !pipeline)
1472 else if (resize_devices(argv[optind]) != 0)
1477 if (!pipeline && !ndevices) {
1482 memset(&rb_sort_root, 0, sizeof(rb_sort_root));
1483 memset(&rb_track_root, 0, sizeof(rb_track_root));
1485 signal(SIGINT, handle_sigint);
1486 signal(SIGHUP, handle_sigint);
1487 signal(SIGTERM, handle_sigint);
1489 setlocale(LC_NUMERIC, "en_US");
1492 ofp = fdopen(STDOUT_FILENO, "w");
1497 snprintf(ofname, sizeof(ofname) - 1, "%s.log", output_name);
1498 ofp = fopen(ofname, "w");
1507 ofp_buffer = malloc(4096);
1508 if (setvbuf(ofp, ofp_buffer, mode, 4096)) {
1518 if (per_process_stats)
1519 show_process_stats();
1521 if (per_device_and_cpu_stats)
1522 show_device_and_cpu_stats();