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;
78 struct per_cpu_info *cpus;
81 struct per_process_info {
84 struct io_stats io_stats;
85 struct per_process_info *hash_next, *list_next;
90 unsigned long long longest_allocation_wait[2];
91 unsigned long long longest_dispatch_wait[2];
92 unsigned long long longest_completion_wait[2];
95 #define PPI_HASH_SHIFT (8)
96 static struct per_process_info *ppi_hash[1 << PPI_HASH_SHIFT];
97 static struct per_process_info *ppi_list;
99 #define S_OPTS "i:o:b:stqw:"
100 static struct option l_opts[] = {
120 .name = "per program stats",
152 * for sorting the displayed output
155 struct blk_io_trace *bit;
156 struct rb_node rb_node;
161 static struct rb_root rb_sort_root;
162 static struct rb_root rb_track_root;
164 static struct trace *trace_list;
167 * for tracking individual ios
170 struct rb_node rb_node;
175 unsigned long long allocation_time;
176 unsigned long long queue_time;
177 unsigned long long dispatch_time;
178 unsigned long long completion_time;
182 static struct per_dev_info *devices;
183 static char *get_dev_name(struct per_dev_info *, char *, int);
186 static char *output_name;
188 static unsigned long long genesis_time;
189 static unsigned long long stopwatch_start; /* start from zero by default */
190 static unsigned long long stopwatch_end = ULONG_LONG_MAX; /* "infinity" */
192 static int per_process_stats;
193 static int track_ios;
195 #define RB_BATCH_DEFAULT (1024)
196 static int rb_batch = RB_BATCH_DEFAULT;
200 #define is_done() (*(volatile int *)(&done))
201 static volatile int done;
203 static inline unsigned long hash_long(unsigned long val)
207 #elif __WORDSIZE == 64
208 val *= 0x9e37fffffffc0001UL;
210 #error unknown word size
213 return val >> (__WORDSIZE - PPI_HASH_SHIFT);
216 static inline void add_process_to_hash(struct per_process_info *ppi)
218 const int hash_idx = hash_long(ppi->pid);
220 ppi->hash_next = ppi_hash[hash_idx];
221 ppi_hash[hash_idx] = ppi;
224 static inline void add_process_to_list(struct per_process_info *ppi)
226 ppi->list_next = ppi_list;
230 static struct per_process_info *find_process_by_pid(__u32 pid)
232 const int hash_idx = hash_long(pid);
233 struct per_process_info *ppi;
235 ppi = ppi_hash[hash_idx];
240 ppi = ppi->hash_next;
246 static inline int trace_rb_insert(struct trace *t)
248 struct rb_node **p = &rb_sort_root.rb_node;
249 struct rb_node *parent = NULL;
252 if (genesis_time == 0 || t->bit->time < genesis_time)
253 genesis_time = t->bit->time;
257 __t = rb_entry(parent, struct trace, rb_node);
259 if (t->bit->time < __t->bit->time)
261 else if (t->bit->time > __t->bit->time)
263 else if (t->bit->device < __t->bit->device)
265 else if (t->bit->device > __t->bit->device)
267 else if (t->bit->sequence < __t->bit->sequence)
269 else if (t->bit->sequence > __t->bit->sequence)
271 else if (t->bit->device == __t->bit->device) {
273 "sequence alias (%d) on device %d,%d!\n",
275 MAJOR(t->bit->device), MINOR(t->bit->device));
280 rb_link_node(&t->rb_node, parent, p);
281 rb_insert_color(&t->rb_node, &rb_sort_root);
285 static inline int track_rb_insert(struct io_track *iot)
287 struct rb_node **p = &rb_track_root.rb_node;
288 struct rb_node *parent = NULL;
289 struct io_track *__iot;
294 __iot = rb_entry(parent, struct io_track, rb_node);
296 if (iot->device < __iot->device)
298 else if (iot->device > __iot->device)
300 else if (iot->sector < __iot->sector)
302 else if (iot->sector > __iot->sector)
306 "sector alias (%llu) on device %d,%d!\n",
308 MAJOR(iot->device), MINOR(iot->device));
313 rb_link_node(&iot->rb_node, parent, p);
314 rb_insert_color(&iot->rb_node, &rb_track_root);
318 static struct io_track *__find_track(dev_t device, __u64 sector)
320 struct rb_node **p = &rb_track_root.rb_node;
321 struct rb_node *parent = NULL;
322 struct io_track *__iot;
327 __iot = rb_entry(parent, struct io_track, rb_node);
329 if (device < __iot->device)
331 else if (device > __iot->device)
333 else if (sector < __iot->sector)
335 else if (sector > __iot->sector)
344 static struct io_track *find_track(__u32 pid, dev_t device, __u64 sector)
346 struct io_track *iot;
348 iot = __find_track(device, sector);
350 iot = malloc(sizeof(*iot));
352 iot->device = device;
353 iot->sector = sector;
354 track_rb_insert(iot);
360 static void log_track_frontmerge(struct blk_io_trace *t)
362 struct io_track *iot;
367 iot = __find_track(t->device, t->sector + (t->bytes >> 9));
369 fprintf(stderr, "failed to find mergeable event\n");
373 rb_erase(&iot->rb_node, &rb_track_root);
374 iot->sector -= t->bytes >> 9;
375 track_rb_insert(iot);
378 static void log_track_getrq(struct blk_io_trace *t)
380 struct io_track *iot;
385 iot = find_track(t->pid, t->device, t->sector);
386 iot->allocation_time = t->time;
391 * return time between rq allocation and queue
393 static unsigned long long log_track_queue(struct blk_io_trace *t)
395 unsigned long long elapsed;
396 struct io_track *iot;
401 iot = find_track(t->pid, t->device, t->sector);
402 iot->queue_time = t->time;
403 elapsed = iot->queue_time - iot->allocation_time;
405 if (per_process_stats) {
406 struct per_process_info *ppi = find_process_by_pid(iot->pid);
407 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
409 if (ppi && elapsed > ppi->longest_allocation_wait[w])
410 ppi->longest_allocation_wait[w] = elapsed;
417 * return time between queue and issue
419 static unsigned long long log_track_issue(struct blk_io_trace *t)
421 unsigned long long elapsed;
422 struct io_track *iot;
426 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
429 iot = __find_track(t->device, t->sector);
431 fprintf(stderr, "failed to find issue event\n");
435 iot->dispatch_time = t->time;
436 elapsed = iot->dispatch_time - iot->queue_time;
438 if (per_process_stats) {
439 struct per_process_info *ppi = find_process_by_pid(iot->pid);
440 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
442 if (ppi && elapsed > ppi->longest_dispatch_wait[w])
443 ppi->longest_dispatch_wait[w] = elapsed;
450 * return time between dispatch and complete
452 static unsigned long long log_track_complete(struct blk_io_trace *t)
454 unsigned long long elapsed;
455 struct io_track *iot;
459 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
462 iot = __find_track(t->device, t->sector);
464 fprintf(stderr, "failed to find complete event\n");
468 iot->completion_time = t->time;
469 elapsed = iot->completion_time - iot->dispatch_time;
471 if (per_process_stats) {
472 struct per_process_info *ppi = find_process_by_pid(iot->pid);
473 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
475 if (ppi && elapsed > ppi->longest_completion_wait[w])
476 ppi->longest_completion_wait[w] = elapsed;
480 * kill the trace, we don't need it after completion
482 rb_erase(&iot->rb_node, &rb_track_root);
489 static struct io_stats *find_process_io_stats(__u32 pid, char *name)
491 struct per_process_info *ppi = find_process_by_pid(pid);
494 ppi = malloc(sizeof(*ppi));
495 memset(ppi, 0, sizeof(*ppi));
496 strncpy(ppi->name, name, sizeof(ppi->name));
498 add_process_to_hash(ppi);
499 add_process_to_list(ppi);
502 return &ppi->io_stats;
506 static void resize_cpu_info(struct per_dev_info *pdi, int cpu)
508 struct per_cpu_info *cpus = pdi->cpus;
509 int ncpus = pdi->ncpus;
510 int new_count = cpu + 1;
514 size = new_count * sizeof(struct per_cpu_info);
515 cpus = realloc(cpus, size);
518 fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n",
519 get_dev_name(pdi, name, sizeof(name)), size);
523 new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info));
524 new_space = (new_count - ncpus) * sizeof(struct per_cpu_info);
525 memset(new_start, 0, new_space);
527 pdi->ncpus = new_count;
531 static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu)
533 struct per_cpu_info *pci;
535 if (cpu >= pdi->ncpus)
536 resize_cpu_info(pdi, cpu);
538 pci = &pdi->cpus[cpu];
544 static int resize_devices(char *name)
546 int size = (ndevices + 1) * sizeof(struct per_dev_info);
548 devices = realloc(devices, size);
550 fprintf(stderr, "Out of memory, device %s (%d)\n", name, size);
553 memset(&devices[ndevices], 0, sizeof(struct per_dev_info));
554 devices[ndevices].name = name;
559 static struct per_dev_info *get_dev_info(dev_t id)
561 struct per_dev_info *pdi;
564 for (i = 0; i < ndevices; i++)
565 if (devices[i].id == id)
568 if (resize_devices(NULL) != 0)
571 pdi = &devices[ndevices - 1];
576 static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size)
579 snprintf(buffer, size, "%s", pdi->name);
581 snprintf(buffer, size, "%d,%d", MAJOR(pdi->id), MINOR(pdi->id));
585 static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit)
587 unsigned long long this = bit->time;
588 unsigned long long last = pdi->last_reported_time;
590 pdi->backwards = (this < last) ? 'B' : ' ';
591 pdi->last_reported_time = this;
594 static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t,
599 ios->qwrite_kb += t->bytes >> 10;
602 ios->qread_kb += t->bytes >> 10;
606 static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci,
609 __account_m(&pci->io_stats, t, rw);
611 if (per_process_stats) {
612 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
614 __account_m(ios, t, rw);
618 static inline void __account_q(struct io_stats *ios, struct blk_io_trace *t,
623 ios->qwrite_kb += t->bytes >> 10;
626 ios->qread_kb += t->bytes >> 10;
630 static inline void account_q(struct blk_io_trace *t, struct per_cpu_info *pci,
633 __account_q(&pci->io_stats, t, rw);
635 if (per_process_stats) {
636 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
638 __account_q(ios, t, rw);
642 static inline void __account_c(struct io_stats *ios, int rw, unsigned int bytes)
646 ios->cwrite_kb += bytes >> 10;
649 ios->cread_kb += bytes >> 10;
653 static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci,
656 __account_c(&pci->io_stats, rw, bytes);
658 if (per_process_stats) {
659 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
661 __account_c(ios, rw, bytes);
665 static inline void __account_i(struct io_stats *ios, int rw, unsigned int bytes)
669 ios->iwrite_kb += bytes >> 10;
672 ios->iread_kb += bytes >> 10;
676 static inline void account_i(struct blk_io_trace *t, struct per_cpu_info *pci,
679 __account_i(&pci->io_stats, rw, t->bytes);
681 if (per_process_stats) {
682 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
684 __account_i(ios, rw, t->bytes);
688 static inline void __account_unplug(struct io_stats *ios, int timer)
691 ios->timer_unplugs++;
696 static inline void account_unplug(struct blk_io_trace *t,
697 struct per_cpu_info *pci, int timer)
699 __account_unplug(&pci->io_stats, timer);
701 if (per_process_stats) {
702 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
704 __account_unplug(ios, timer);
708 static void output(struct per_cpu_info *pci, char *s)
710 fprintf(ofp, "%s", s);
713 static char hstring[256];
714 static char tstring[256];
716 static inline char *setup_header(struct per_cpu_info *pci,
717 struct blk_io_trace *t, char *act)
719 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
720 int b = t->action & BLK_TC_ACT(BLK_TC_BARRIER);
721 int s = t->action & BLK_TC_ACT(BLK_TC_SYNC);
736 sprintf(hstring, "%3d,%-3d %2d %8ld %5Lu.%09Lu %5u %2s %3s",
737 MAJOR(t->device), MINOR(t->device), pci->cpu,
738 (unsigned long)t->sequence, SECONDS(t->time),
739 NANO_SECONDS(t->time), t->pid, act, rwbs);
744 static void log_complete(struct per_cpu_info *pci, struct blk_io_trace *t,
747 unsigned long long elapsed = log_track_complete(t);
749 if (elapsed != -1ULL) {
750 unsigned long usec = elapsed / 1000;
752 sprintf(tstring,"%s %Lu + %u (%8lu) [%d]\n",
753 setup_header(pci, t, act),
754 (unsigned long long)t->sector, t->bytes >> 9,
757 sprintf(tstring,"%s %Lu + %u [%d]\n", setup_header(pci, t, act),
758 (unsigned long long)t->sector, t->bytes >> 9, t->error);
761 output(pci, tstring);
764 static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t,
767 unsigned long long elapsed = log_track_queue(t);
769 if (elapsed != -1ULL) {
770 unsigned long usec = elapsed / 1000;
772 sprintf(tstring,"%s %Lu + %u (%8lu) [%s]\n",
773 setup_header(pci, t, act),
774 (unsigned long long)t->sector, t->bytes >> 9,
777 sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act),
778 (unsigned long long)t->sector, t->bytes >> 9, t->comm);
780 output(pci, tstring);
783 static void log_issue(struct per_cpu_info *pci, struct blk_io_trace *t,
786 unsigned long long elapsed = log_track_issue(t);
788 if (elapsed != -1ULL) {
789 double usec = (double) elapsed / 1000;
791 sprintf(tstring,"%s %Lu + %u (%8.2f) [%s]\n",
792 setup_header(pci, t, act),
793 (unsigned long long)t->sector, t->bytes >> 9,
796 sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act),
797 (unsigned long long)t->sector, t->bytes >> 9, t->comm);
800 output(pci, tstring);
803 static void log_merge(struct per_cpu_info *pci, struct blk_io_trace *t,
807 log_track_frontmerge(t);
809 sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act),
810 (unsigned long long)t->sector, t->bytes >> 9, t->comm);
811 output(pci, tstring);
814 static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t,
817 sprintf(tstring,"%s [%s]\n", setup_header(pci, t, act), t->comm);
818 output(pci, tstring);
821 static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t,
824 sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act),
825 (unsigned long long)t->sector, t->bytes >> 9, t->comm);
826 output(pci, tstring);
829 static int log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t,
835 len = sprintf(tstring,"%s [%s] ", setup_header(pci, t, act), t->comm);
836 depth = (__u64 *) ((char *) t + sizeof(*t));
837 sprintf(tstring + len, "%u\n", (unsigned int) be64_to_cpu(*depth));
838 output(pci, tstring);
843 static int log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act)
848 sprintf(tstring,"%s ", setup_header(pci, t, act));
849 output(pci, tstring);
851 buf = (unsigned char *) t + sizeof(*t);
852 for (i = 0; i < t->pdu_len; i++) {
853 sprintf(tstring,"%02x ", buf[i]);
854 output(pci, tstring);
858 sprintf(tstring,"[%d]\n", t->error);
859 output(pci, tstring);
861 sprintf(tstring,"[%s]\n", t->comm);
862 output(pci, tstring);
867 static int dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci)
871 switch (t->action & 0xffff) {
873 log_generic(pci, t, "Q");
876 log_generic(pci, t, "G");
878 case __BLK_TA_SLEEPRQ:
879 log_generic(pci, t, "S");
881 case __BLK_TA_REQUEUE:
882 log_generic(pci, t, "R");
885 ret = log_pc(pci, t, "D");
887 case __BLK_TA_COMPLETE:
891 fprintf(stderr, "Bad pc action %x\n", t->action);
899 static void dump_trace_fs(struct blk_io_trace *t, struct per_cpu_info *pci)
901 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
902 int act = t->action & 0xffff;
906 account_q(t, pci, w);
907 log_queue(pci, t, "Q");
909 case __BLK_TA_BACKMERGE:
910 account_m(t, pci, w);
911 log_merge(pci, t, "M");
913 case __BLK_TA_FRONTMERGE:
914 account_m(t, pci, w);
915 log_merge(pci, t, "F");
919 log_generic(pci, t, "G");
921 case __BLK_TA_SLEEPRQ:
922 log_generic(pci, t, "S");
924 case __BLK_TA_REQUEUE:
925 account_c(t, pci, w, -t->bytes);
926 log_queue(pci, t, "R");
929 account_i(t, pci, w);
930 log_issue(pci, t, "D");
932 case __BLK_TA_COMPLETE:
933 account_c(t, pci, w, t->bytes);
934 log_complete(pci, t, "C");
937 log_action(pci, t, "P");
939 case __BLK_TA_UNPLUG_IO:
940 account_unplug(t, pci, 0);
941 log_unplug(pci, t, "U");
943 case __BLK_TA_UNPLUG_TIMER:
944 account_unplug(t, pci, 1);
945 log_unplug(pci, t, "UT");
948 fprintf(stderr, "Bad fs action %x\n", t->action);
953 static int dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
954 struct per_dev_info *pdi)
958 if (t->action & BLK_TC_ACT(BLK_TC_PC))
959 ret = dump_trace_pc(t, pci);
961 dump_trace_fs(t, pci);
967 static void dump_io_stats(struct io_stats *ios, char *msg)
969 fprintf(ofp, "%s\n", msg);
971 fprintf(ofp, " Reads Queued: %'8lu, %'8LuKiB\t", ios->qreads, ios->qread_kb);
972 fprintf(ofp, " Writes Queued: %'8lu, %'8LuKiB\n", ios->qwrites,ios->qwrite_kb);
974 fprintf(ofp, " Read Dispatches: %'8lu, %'8LuKiB\t", ios->ireads, ios->iread_kb);
975 fprintf(ofp, " Write Dispatches: %'8lu, %'8LuKiB\n", ios->iwrites,ios->iwrite_kb);
976 fprintf(ofp, " Reads Completed: %'8lu, %'8LuKiB\t", ios->creads, ios->cread_kb);
977 fprintf(ofp, " Writes Completed: %'8lu, %'8LuKiB\n", ios->cwrites,ios->cwrite_kb);
978 fprintf(ofp, " Read Merges: %'8lu%8c\t", ios->mreads, ' ');
979 fprintf(ofp, " Write Merges: %'8lu\n", ios->mwrites);
980 fprintf(ofp, " IO unplugs: %'8lu%8c\t", ios->io_unplugs, ' ');
981 fprintf(ofp, " Timer unplugs: %'8lu\n", ios->timer_unplugs);
984 static void dump_wait_stats(struct per_process_info *ppi)
986 unsigned long rawait = ppi->longest_allocation_wait[0] / 1000;
987 unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000;
988 unsigned long rcwait = ppi->longest_completion_wait[0] / 1000;
989 unsigned long wawait = ppi->longest_allocation_wait[1] / 1000;
990 unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000;
991 unsigned long wcwait = ppi->longest_completion_wait[1] / 1000;
993 fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' ');
994 fprintf(ofp, " Allocation wait: %'8lu\n", wawait);
995 fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' ');
996 fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait);
997 fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' ');
998 fprintf(ofp, " Completion wait: %'8lu\n", wcwait);
1001 static void show_process_stats(void)
1003 struct per_process_info *ppi;
1007 dump_io_stats(&ppi->io_stats, ppi->name);
1008 dump_wait_stats(ppi);
1009 ppi = ppi->list_next;
1015 static void show_device_and_cpu_stats(void)
1017 struct per_dev_info *pdi;
1018 struct per_cpu_info *pci;
1019 struct io_stats total, *ios;
1020 int i, j, pci_events;
1021 char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3];
1024 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
1026 memset(&total, 0, sizeof(total));
1032 for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) {
1036 ios = &pci->io_stats;
1037 total.qreads += ios->qreads;
1038 total.qwrites += ios->qwrites;
1039 total.creads += ios->creads;
1040 total.cwrites += ios->cwrites;
1041 total.mreads += ios->mreads;
1042 total.mwrites += ios->mwrites;
1043 total.ireads += ios->ireads;
1044 total.iwrites += ios->iwrites;
1045 total.qread_kb += ios->qread_kb;
1046 total.qwrite_kb += ios->qwrite_kb;
1047 total.cread_kb += ios->cread_kb;
1048 total.cwrite_kb += ios->cwrite_kb;
1049 total.iread_kb += ios->iread_kb;
1050 total.iwrite_kb += ios->iwrite_kb;
1051 total.timer_unplugs += ios->timer_unplugs;
1052 total.io_unplugs += ios->io_unplugs;
1054 snprintf(line, sizeof(line) - 1, "CPU%d (%s):",
1055 j, get_dev_name(pdi, name, sizeof(name)));
1056 dump_io_stats(ios, line);
1060 if (pci_events > 1) {
1062 snprintf(line, sizeof(line) - 1, "Total (%s):",
1063 get_dev_name(pdi, name, sizeof(name)));
1064 dump_io_stats(&total, line);
1067 fprintf(ofp, "\nEvents (%s): %'Lu entries, %'lu skips\n",
1068 get_dev_name(pdi, line, sizeof(line)), pdi->events,
1073 static struct blk_io_trace *find_trace(void *p, unsigned long offset)
1075 unsigned long max_offset = offset;
1077 struct blk_io_trace *bit;
1080 for (off = 0; off < max_offset; off++) {
1083 magic = be32_to_cpu(bit->magic);
1084 if ((magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
1091 static int sort_entries(void)
1093 struct per_dev_info *pdi;
1094 struct per_cpu_info *pci;
1095 struct blk_io_trace *bit;
1099 while ((t = trace_list) != NULL) {
1101 trace_list = t->next;
1104 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(int piped)
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 us a few times
1152 if (bit->sequence != (pdi->last_sequence + 1)) {
1153 if (piped && t->skipped < 5) {
1157 fprintf(stderr, "skipping from %lu to %u\n", pdi->last_sequence, bit->sequence);
1162 pdi->last_sequence = bit->sequence;
1164 bit->time -= genesis_time;
1165 if (bit->time < stopwatch_start)
1167 if (bit->time >= stopwatch_end)
1170 check_time(pdi, bit);
1172 if (dump_trace(bit, &pdi->cpus[cpu], pdi))
1175 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));
1235 t->next = trace_list;
1238 tb += sizeof(*bit) + bit->pdu_len;
1244 static int do_file(void)
1246 int i, j, nfiles = 0, nelems;
1248 for (i = 0; i < ndevices; i++) {
1249 for (j = 0;; j++, nfiles++) {
1250 struct per_dev_info *pdi;
1251 struct per_cpu_info *pci;
1256 pci = get_cpu_info(pdi, j);
1259 snprintf(pci->fname, sizeof(pci->fname)-1,
1260 "%s_out.%d", pdi->name, j);
1261 if (stat(pci->fname, &st) < 0)
1266 printf("Processing %s\n", pci->fname);
1268 tb = malloc(st.st_size);
1270 fprintf(stderr, "Out of memory, skip file %s\n",
1275 pci->fd = open(pci->fname, O_RDONLY);
1282 if (read_data(pci->fd, tb, st.st_size, 1)) {
1288 if (find_entries(tb, st.st_size)) {
1293 nelems = sort_entries();
1300 printf("Completed %s (CPU%d %d, entries)\n",
1301 pci->fname, j, nelems);
1307 fprintf(stderr, "No files found\n");
1315 static int read_sort_events(int fd)
1320 struct blk_io_trace *bit;
1325 bit = malloc(sizeof(*bit));
1327 if (read_data(fd, bit, sizeof(*bit), !events))
1330 magic = be32_to_cpu(bit->magic);
1331 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
1332 fprintf(stderr, "Bad magic %x\n", magic);
1336 pdu_len = be16_to_cpu(bit->pdu_len);
1338 void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
1340 if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1))
1346 t = malloc(sizeof(*t));
1347 memset(t, 0, sizeof(*t));
1351 t->next = trace_list;
1355 } while (!is_done() && events < rb_batch);
1360 static int do_stdin(void)
1364 fd = dup(STDIN_FILENO);
1368 events = read_sort_events(fd);
1372 if (sort_entries() == -1)
1382 static void flush_output(void)
1387 static void handle_sigint(int sig)
1394 * Extract start and duration times from a string, allowing
1395 * us to specify a time interval of interest within a trace.
1396 * Format: "duration" (start is zero) or "start:duration".
1398 static int find_stopwatch_interval(char *string)
1403 value = strtod(string, &sp);
1405 fprintf(stderr,"Invalid stopwatch timer: %s\n", string);
1409 stopwatch_start = DOUBLE_TO_NANO_ULL(value);
1411 value = strtod(string, &sp);
1412 if (sp == string || *sp != '\0') {
1413 fprintf(stderr,"Invalid stopwatch duration time: %s\n",
1417 } else if (*sp != '\0') {
1418 fprintf(stderr,"Invalid stopwatch start timer: %s\n", string);
1421 stopwatch_end = stopwatch_start + DOUBLE_TO_NANO_ULL(value);
1425 static void usage(char *prog)
1427 fprintf(stderr, "Usage: %s "
1428 "[-i <name>] [-o <output>] [-s] [-w N[:n]] <name>...\n",
1432 int main(int argc, char *argv[])
1436 int per_device_and_cpu_stats = 1;
1438 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
1441 if (!strcmp(optarg, "-") && !pipeline)
1443 else if (resize_devices(optarg) != 0)
1447 output_name = optarg;
1450 rb_batch = atoi(optarg);
1452 rb_batch = RB_BATCH_DEFAULT;
1455 per_process_stats = 1;
1461 per_device_and_cpu_stats = 0;
1464 if (find_stopwatch_interval(optarg) != 0)
1473 while (optind < argc) {
1474 if (!strcmp(argv[optind], "-") && !pipeline)
1476 else if (resize_devices(argv[optind]) != 0)
1481 if (!pipeline && !ndevices) {
1486 memset(&rb_sort_root, 0, sizeof(rb_sort_root));
1487 memset(&rb_track_root, 0, sizeof(rb_track_root));
1489 signal(SIGINT, handle_sigint);
1490 signal(SIGHUP, handle_sigint);
1491 signal(SIGTERM, handle_sigint);
1493 setlocale(LC_NUMERIC, "en_US");
1496 ofp = fdopen(STDOUT_FILENO, "w");
1501 snprintf(ofname, sizeof(ofname) - 1, "%s.log", output_name);
1502 ofp = fopen(ofname, "w");
1511 ofp_buffer = malloc(4096);
1512 if (setvbuf(ofp, ofp_buffer, mode, 4096)) {
1522 if (per_process_stats)
1523 show_process_stats();
1525 if (per_device_and_cpu_stats)
1526 show_device_and_cpu_stats();