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>
36 #define SECONDS(x) ((unsigned long long)(x) / 1000000000)
37 #define NANO_SECONDS(x) ((unsigned long long)(x) % 1000000000)
40 #define MINORMASK ((1U << MINORBITS) - 1)
41 #define MAJOR(dev) ((unsigned int) ((dev) >> MINORBITS))
42 #define MINOR(dev) ((unsigned int) ((dev) & MINORMASK))
44 #define min(a, b) ((a) < (b) ? (a) : (b))
47 unsigned long qreads, qwrites, creads, cwrites, mreads, mwrites;
48 unsigned long ireads, iwrites;
49 unsigned long long qread_kb, qwrite_kb, cread_kb, cwrite_kb;
50 unsigned long long iread_kb, iwrite_kb;
60 struct io_stats io_stats;
68 unsigned long long events;
69 unsigned long long last_reported_time;
70 struct io_stats io_stats;
73 struct per_cpu_info *cpus;
76 struct per_process_info {
79 struct io_stats io_stats;
80 struct per_process_info *hash_next, *list_next;
85 unsigned long long longest_allocation_wait[2];
86 unsigned long long longest_dispatch_wait[2];
87 unsigned long long longest_completion_wait[2];
90 #define PPI_HASH_SHIFT (8)
91 static struct per_process_info *ppi_hash[1 << PPI_HASH_SHIFT];
92 static struct per_process_info *ppi_list;
94 #define S_OPTS "i:o:b:st"
95 static struct option l_opts[] = {
115 .name = "per program stats",
134 static struct rb_root rb_sort_root;
135 static struct rb_root rb_track_root;
138 * for sorting the displayed output
141 struct blk_io_trace *bit;
142 struct rb_node rb_node;
146 * for tracking individual ios
149 struct rb_node rb_node;
154 unsigned long long allocation_time;
155 unsigned long long queue_time;
156 unsigned long long dispatch_time;
157 unsigned long long completion_time;
161 static struct per_dev_info *devices;
162 static char *get_dev_name(struct per_dev_info *, char *, int);
165 static char *output_name;
167 static unsigned long long genesis_time;
169 static int per_process_stats;
170 static int track_ios;
172 #define RB_BATCH_DEFAULT (1024)
173 static int rb_batch = RB_BATCH_DEFAULT;
177 #define is_done() (*(volatile int *)(&done))
178 static volatile int done;
180 static inline unsigned long hash_long(unsigned long val)
184 #elif __WORDSIZE == 64
185 val *= 0x9e37fffffffc0001UL;
187 #error unknown word size
190 return val >> (__WORDSIZE - PPI_HASH_SHIFT);
193 static inline void add_process_to_hash(struct per_process_info *ppi)
195 const int hash_idx = hash_long(ppi->pid);
197 ppi->hash_next = ppi_hash[hash_idx];
198 ppi_hash[hash_idx] = ppi;
201 static inline void add_process_to_list(struct per_process_info *ppi)
203 ppi->list_next = ppi_list;
207 static struct per_process_info *find_process_by_pid(__u32 pid)
209 const int hash_idx = hash_long(pid);
210 struct per_process_info *ppi;
212 ppi = ppi_hash[hash_idx];
217 ppi = ppi->hash_next;
223 static inline int trace_rb_insert(struct trace *t)
225 struct rb_node **p = &rb_sort_root.rb_node;
226 struct rb_node *parent = NULL;
229 if (genesis_time == 0 || t->bit->time < genesis_time)
230 genesis_time = t->bit->time;
234 __t = rb_entry(parent, struct trace, rb_node);
236 if (t->bit->time < __t->bit->time)
238 else if (t->bit->time > __t->bit->time)
240 else if (t->bit->device < __t->bit->device)
242 else if (t->bit->device > __t->bit->device)
244 else if (t->bit->sequence < __t->bit->sequence)
246 else if (t->bit->sequence > __t->bit->sequence)
248 else if (t->bit->device == __t->bit->device) {
250 "sequence alias (%d) on device %d,%d!\n",
252 MAJOR(t->bit->device), MINOR(t->bit->device));
257 rb_link_node(&t->rb_node, parent, p);
258 rb_insert_color(&t->rb_node, &rb_sort_root);
262 static inline int track_rb_insert(struct io_track *iot)
264 struct rb_node **p = &rb_track_root.rb_node;
265 struct rb_node *parent = NULL;
266 struct io_track *__iot;
271 __iot = rb_entry(parent, struct io_track, rb_node);
273 if (iot->device < __iot->device)
275 else if (iot->device > __iot->device)
277 else if (iot->sector < __iot->sector)
279 else if (iot->sector > __iot->sector)
283 "sector alias (%llu) on device %d,%d!\n",
285 MAJOR(iot->device), MINOR(iot->device));
290 rb_link_node(&iot->rb_node, parent, p);
291 rb_insert_color(&iot->rb_node, &rb_track_root);
295 static struct io_track *__find_track(dev_t device, __u64 sector)
297 struct rb_node **p = &rb_track_root.rb_node;
298 struct rb_node *parent = NULL;
299 struct io_track *__iot;
304 __iot = rb_entry(parent, struct io_track, rb_node);
306 if (device < __iot->device)
308 else if (device > __iot->device)
310 else if (sector < __iot->sector)
312 else if (sector > __iot->sector)
321 static struct io_track *find_track(__u32 pid, dev_t device, __u64 sector)
323 struct io_track *iot;
325 iot = __find_track(device, sector);
327 iot = malloc(sizeof(*iot));
329 iot->device = device;
330 iot->sector = sector;
331 track_rb_insert(iot);
337 static void log_track_merge(struct blk_io_trace *t)
339 struct io_track *iot;
343 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
346 iot = __find_track(t->device, t->sector - (t->bytes >> 10));
348 fprintf(stderr, "Trying to merge on non-existing request\n");
352 rb_erase(&iot->rb_node, &rb_track_root);
353 iot->sector -= t->bytes >> 10;
354 track_rb_insert(iot);
357 static void log_track_getrq(struct blk_io_trace *t)
359 struct io_track *iot;
364 iot = find_track(t->pid, t->device, t->sector);
365 iot->allocation_time = t->time;
370 * return time between rq allocation and queue
372 static unsigned long long log_track_queue(struct blk_io_trace *t)
374 unsigned long long elapsed;
375 struct io_track *iot;
380 iot = find_track(t->pid, t->device, t->sector);
381 iot->queue_time = t->time;
382 elapsed = iot->queue_time - iot->allocation_time;
384 if (per_process_stats) {
385 struct per_process_info *ppi = find_process_by_pid(iot->pid);
386 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
388 if (ppi && elapsed > ppi->longest_allocation_wait[w])
389 ppi->longest_allocation_wait[w] = elapsed;
396 * return time between queue and issue
398 static unsigned long long log_track_issue(struct blk_io_trace *t)
400 unsigned long long elapsed;
401 struct io_track *iot;
405 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
408 iot = __find_track(t->device, t->sector);
410 fprintf(stderr, "Trying to issue on non-existing request\n");
414 iot->dispatch_time = t->time;
415 elapsed = iot->dispatch_time - iot->queue_time;
417 if (per_process_stats) {
418 struct per_process_info *ppi = find_process_by_pid(iot->pid);
419 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
421 if (ppi && elapsed > ppi->longest_dispatch_wait[w])
422 ppi->longest_dispatch_wait[w] = elapsed;
429 * return time between dispatch and complete
431 static unsigned long long log_track_complete(struct blk_io_trace *t)
433 unsigned long long elapsed;
434 struct io_track *iot;
438 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
441 iot = __find_track(t->device, t->sector);
443 fprintf(stderr, "Trying to dispatch on non-existing request\n");
447 iot->completion_time = t->time;
448 elapsed = iot->completion_time - iot->dispatch_time;
450 if (per_process_stats) {
451 struct per_process_info *ppi = find_process_by_pid(iot->pid);
452 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
454 if (ppi && elapsed > ppi->longest_completion_wait[w])
455 ppi->longest_completion_wait[w] = elapsed;
459 * kill the trace, we don't need it after completion
461 rb_erase(&iot->rb_node, &rb_track_root);
468 static struct io_stats *find_process_io_stats(__u32 pid, char *name)
470 struct per_process_info *ppi = find_process_by_pid(pid);
473 ppi = malloc(sizeof(*ppi));
474 memset(ppi, 0, sizeof(*ppi));
475 strncpy(ppi->name, name, sizeof(ppi->name));
477 add_process_to_hash(ppi);
478 add_process_to_list(ppi);
481 return &ppi->io_stats;
485 static void resize_cpu_info(struct per_dev_info *pdi, int cpu)
487 struct per_cpu_info *cpus = pdi->cpus;
488 int ncpus = pdi->ncpus;
489 int new_count = cpu + 1;
493 size = new_count * sizeof(struct per_cpu_info);
494 cpus = realloc(cpus, size);
497 fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n",
498 get_dev_name(pdi, name, sizeof(name)), size);
502 new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info));
503 new_space = (new_count - ncpus) * sizeof(struct per_cpu_info);
504 memset(new_start, 0, new_space);
506 pdi->ncpus = new_count;
510 static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu)
512 if (cpu >= pdi->ncpus)
513 resize_cpu_info(pdi, cpu);
514 return &pdi->cpus[cpu];
518 static int resize_devices(char *name)
520 int size = (ndevices + 1) * sizeof(struct per_dev_info);
522 devices = realloc(devices, size);
524 fprintf(stderr, "Out of memory, device %s (%d)\n", name, size);
527 memset(&devices[ndevices], 0, sizeof(struct per_dev_info));
528 devices[ndevices].name = name;
533 static struct per_dev_info *get_dev_info(dev_t id, int create)
537 for (i = 0; i < ndevices; i++)
538 if (devices[i].id == id)
542 if (resize_devices(NULL) != 0)
544 return &devices[ndevices-1];
547 static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size)
550 snprintf(buffer, size, "%s", pdi->name);
552 snprintf(buffer, size, "%d,%d", MAJOR(pdi->id), MINOR(pdi->id));
557 static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit)
559 unsigned long long this = bit->time;
560 unsigned long long last = pdi->last_reported_time;
562 pdi->backwards = (this < last) ? 'B' : ' ';
563 pdi->last_reported_time = this;
567 static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t,
572 ios->qwrite_kb += t->bytes >> 10;
575 ios->qread_kb += t->bytes >> 10;
579 static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci,
582 __account_m(&pci->io_stats, t, rw);
584 if (per_process_stats) {
585 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
587 __account_m(ios, t, rw);
591 static inline void __account_q(struct io_stats *ios, struct blk_io_trace *t,
596 ios->qwrite_kb += t->bytes >> 10;
599 ios->qread_kb += t->bytes >> 10;
603 static inline void account_q(struct blk_io_trace *t, struct per_cpu_info *pci,
606 __account_q(&pci->io_stats, t, rw);
608 if (per_process_stats) {
609 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
611 __account_q(ios, t, rw);
615 static inline void __account_c(struct io_stats *ios, int rw, unsigned int bytes)
619 ios->cwrite_kb += bytes >> 10;
622 ios->cread_kb += bytes >> 10;
626 static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci,
629 __account_c(&pci->io_stats, rw, bytes);
631 if (per_process_stats) {
632 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
634 __account_c(ios, rw, bytes);
638 static inline void __account_i(struct io_stats *ios, int rw, unsigned int bytes)
642 ios->iwrite_kb += bytes >> 10;
645 ios->iread_kb += bytes >> 10;
649 static inline void account_i(struct blk_io_trace *t, struct per_cpu_info *pci,
652 __account_i(&pci->io_stats, rw, t->bytes);
654 if (per_process_stats) {
655 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
657 __account_i(ios, rw, t->bytes);
661 static void output(struct per_cpu_info *pci, char *s)
663 fprintf(ofp, "%s", s);
666 static char hstring[256];
667 static char tstring[256];
669 static inline char *setup_header(struct per_cpu_info *pci,
670 struct blk_io_trace *t, char act)
672 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
673 int b = t->action & BLK_TC_ACT(BLK_TC_BARRIER);
674 int s = t->action & BLK_TC_ACT(BLK_TC_SYNC);
689 sprintf(hstring, "%3d,%-3d %2d %8ld %5Lu.%09Lu %5u %c %3s",
690 MAJOR(t->device), MINOR(t->device), pci->cpu,
691 (unsigned long)t->sequence, SECONDS(t->time),
692 NANO_SECONDS(t->time), t->pid, act, rwbs);
697 static void log_complete(struct per_cpu_info *pci, struct blk_io_trace *t,
700 unsigned long long elapsed = log_track_complete(t);
702 if (elapsed != -1ULL) {
703 unsigned long usec = elapsed / 1000;
705 sprintf(tstring,"%s %Lu + %u (%8lu) [%d]\n",
706 setup_header(pci, t, act),
707 (unsigned long long)t->sector, t->bytes >> 9,
710 sprintf(tstring,"%s %Lu + %u [%d]\n", setup_header(pci, t, act),
711 (unsigned long long)t->sector, t->bytes >> 9, t->error);
714 output(pci, tstring);
717 static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t,
720 unsigned long long elapsed = log_track_queue(t);
722 if (elapsed != -1ULL) {
723 unsigned long usec = elapsed / 1000;
725 sprintf(tstring,"%s %Lu + %u (%8lu) [%s]\n",
726 setup_header(pci, t, act),
727 (unsigned long long)t->sector, t->bytes >> 9,
730 sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act),
731 (unsigned long long)t->sector, t->bytes >> 9, t->comm);
733 output(pci, tstring);
736 static void log_issue(struct per_cpu_info *pci, struct blk_io_trace *t,
739 unsigned long long elapsed = log_track_issue(t);
741 if (elapsed != -1ULL) {
742 double usec = (double) elapsed / 1000;
744 sprintf(tstring,"%s %Lu + %u (%8.2f) [%s]\n",
745 setup_header(pci, t, act),
746 (unsigned long long)t->sector, t->bytes >> 9,
749 sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act),
750 (unsigned long long)t->sector, t->bytes >> 9, t->comm);
753 output(pci, tstring);
756 static void log_merge(struct per_cpu_info *pci, struct blk_io_trace *t,
761 sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act),
762 (unsigned long long)t->sector, t->bytes >> 9, t->comm);
763 output(pci, tstring);
766 static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t,
769 sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act),
770 (unsigned long long)t->sector, t->bytes >> 9, t->comm);
771 output(pci, tstring);
774 static int log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char act)
779 sprintf(tstring,"%s ", setup_header(pci, t, act));
780 output(pci, tstring);
782 buf = (unsigned char *) t + sizeof(*t);
783 for (i = 0; i < t->pdu_len; i++) {
784 sprintf(tstring,"%02x ", buf[i]);
785 output(pci, tstring);
789 sprintf(tstring,"[%d]\n", t->error);
790 output(pci, tstring);
792 sprintf(tstring,"[%s]\n", t->comm);
793 output(pci, tstring);
798 static int dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci)
802 switch (t->action & 0xffff) {
804 log_generic(pci, t, 'Q');
807 log_generic(pci, t, 'G');
809 case __BLK_TA_SLEEPRQ:
810 log_generic(pci, t, 'S');
812 case __BLK_TA_REQUEUE:
813 log_generic(pci, t, 'R');
816 ret = log_pc(pci, t, 'D');
818 case __BLK_TA_COMPLETE:
822 fprintf(stderr, "Bad pc action %x\n", t->action);
830 static void dump_trace_fs(struct blk_io_trace *t, struct per_cpu_info *pci)
832 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
833 int act = t->action & 0xffff;
837 account_q(t, pci, w);
838 log_queue(pci, t, 'Q');
840 case __BLK_TA_BACKMERGE:
841 account_m(t, pci, w);
842 log_merge(pci, t, 'M');
844 case __BLK_TA_FRONTMERGE:
845 account_m(t, pci, w);
846 log_merge(pci, t, 'F');
850 log_generic(pci, t, 'G');
852 case __BLK_TA_SLEEPRQ:
853 log_generic(pci, t, 'S');
855 case __BLK_TA_REQUEUE:
856 account_c(t, pci, w, -t->bytes);
857 log_queue(pci, t, 'R');
860 account_i(t, pci, w);
861 log_issue(pci, t, 'D');
863 case __BLK_TA_COMPLETE:
864 account_c(t, pci, w, t->bytes);
865 log_complete(pci, t, 'C');
868 fprintf(stderr, "Bad fs action %x\n", t->action);
873 static int dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
874 struct per_dev_info *pdi)
878 if (t->action & BLK_TC_ACT(BLK_TC_PC))
879 ret = dump_trace_pc(t, pci);
881 dump_trace_fs(t, pci);
887 static void dump_io_stats(struct io_stats *ios, char *msg)
889 fprintf(ofp, "%s\n", msg);
891 fprintf(ofp, " Reads Queued: %'8lu, %'8LuKiB\t", ios->qreads, ios->qread_kb);
892 fprintf(ofp, " Writes Queued: %'8lu, %'8LuKiB\n", ios->qwrites,ios->qwrite_kb);
894 fprintf(ofp, " Read Dispatches: %'8lu, %'8LuKiB\t", ios->ireads, ios->iread_kb);
895 fprintf(ofp, " Write Dispatches: %'8lu, %'8LuKiB\n", ios->iwrites,ios->iwrite_kb);
896 fprintf(ofp, " Reads Completed: %'8lu, %'8LuKiB\t", ios->creads, ios->cread_kb);
897 fprintf(ofp, " Writes Completed: %'8lu, %'8LuKiB\n", ios->cwrites,ios->cwrite_kb);
898 fprintf(ofp, " Read Merges: %'8lu%8c\t", ios->mreads, ' ');
900 fprintf(ofp, " Write Merges: %'8lu\n", ios->mwrites);
903 static void dump_wait_stats(struct per_process_info *ppi)
905 unsigned long rawait = ppi->longest_allocation_wait[0] / 1000;
906 unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000;
907 unsigned long rcwait = ppi->longest_completion_wait[0] / 1000;
908 unsigned long wawait = ppi->longest_allocation_wait[1] / 1000;
909 unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000;
910 unsigned long wcwait = ppi->longest_completion_wait[1] / 1000;
912 fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' ');
913 fprintf(ofp, " Allocation wait: %'8lu\n", wawait);
914 fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' ');
915 fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait);
916 fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' ');
917 fprintf(ofp, " Completion wait: %'8lu\n", wcwait);
920 static void show_process_stats(void)
922 struct per_process_info *ppi;
926 dump_io_stats(&ppi->io_stats, ppi->name);
927 dump_wait_stats(ppi);
928 ppi = ppi->list_next;
934 static void show_device_and_cpu_stats(void)
936 struct per_dev_info *pdi;
937 struct per_cpu_info *pci;
938 struct io_stats total, *ios;
939 int i, j, pci_events;
940 char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3];
943 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
945 memset(&total, 0, sizeof(total));
951 for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) {
955 ios = &pci->io_stats;
956 total.qreads += ios->qreads;
957 total.qwrites += ios->qwrites;
958 total.creads += ios->creads;
959 total.cwrites += ios->cwrites;
960 total.mreads += ios->mreads;
961 total.mwrites += ios->mwrites;
962 total.ireads += ios->ireads;
963 total.iwrites += ios->iwrites;
964 total.qread_kb += ios->qread_kb;
965 total.qwrite_kb += ios->qwrite_kb;
966 total.cread_kb += ios->cread_kb;
967 total.cwrite_kb += ios->cwrite_kb;
968 total.iread_kb += ios->iread_kb;
969 total.iwrite_kb += ios->iwrite_kb;
971 snprintf(line, sizeof(line) - 1, "CPU%d (%s):",
972 j, get_dev_name(pdi, name, sizeof(name)));
973 dump_io_stats(ios, line);
977 if (pci_events > 1) {
979 snprintf(line, sizeof(line) - 1, "Total (%s):",
980 get_dev_name(pdi, name, sizeof(name)));
981 dump_io_stats(&total, line);
984 fprintf(ofp, "Events (%s): %'Lu\n",
985 get_dev_name(pdi, line, sizeof(line)), pdi->events);
989 static struct blk_io_trace *find_trace(void *p, unsigned long offset, int nr)
991 unsigned long max_offset = min(offset,nr * sizeof(struct blk_io_trace));
993 struct blk_io_trace *bit;
996 for (off = 0; off < max_offset; off++) {
999 magic = be32_to_cpu(bit->magic);
1000 if ((magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
1007 static int sort_entries(void *traces, unsigned long offset, int nr,
1008 struct per_dev_info *fpdi, struct per_cpu_info *fpci)
1010 struct per_dev_info *pdi;
1011 struct per_cpu_info *pci;
1012 struct blk_io_trace *bit;
1014 void *start = traces;
1016 while (traces - start <= offset - sizeof(*bit)) {
1020 bit = find_trace(traces, offset - (traces - start), nr);
1024 t = malloc(sizeof(*t));
1026 fprintf(stderr, "Out of memory, seq %d on dev %d,%d\n",
1028 MAJOR(bit->device), MINOR(bit->device));
1032 memset(&t->rb_node, 0, sizeof(t->rb_node));
1036 if (verify_trace(bit)) {
1041 pdi = fpdi ? fpdi : get_dev_info(bit->device, 1);
1042 pdi->id = bit->device;
1043 pci = fpci ? fpci : get_cpu_info(pdi, bit->cpu);
1044 pci->cpu = bit->cpu;
1047 if (trace_rb_insert(t)) {
1052 traces += sizeof(*bit) + bit->pdu_len;
1059 static void free_entries_rb(void)
1063 while ((n = rb_first(&rb_sort_root)) != NULL) {
1064 struct trace *t = rb_entry(n, struct trace, rb_node);
1066 rb_erase(&t->rb_node, &rb_sort_root);
1071 static void show_entries_rb(void)
1073 struct per_dev_info *pdi;
1074 struct blk_io_trace *bit;
1079 n = rb_first(&rb_sort_root);
1084 t = rb_entry(n, struct trace, rb_node);
1087 pdi = get_dev_info(bit->device, 0);
1089 fprintf(stderr, "Unknown device ID? (%d,%d)\n",
1090 MAJOR(bit->device), MINOR(bit->device));
1094 if (cpu > pdi->ncpus) {
1095 fprintf(stderr, "Unknown CPU ID? (%d, device %d,%d)\n",
1096 cpu, MAJOR(bit->device), MINOR(bit->device));
1100 bit->time -= genesis_time;
1102 check_time(pdi, bit);
1104 if (dump_trace(bit, &pdi->cpus[cpu], pdi))
1107 } while ((n = rb_next(n)) != NULL);
1110 static int read_data(int fd, void *buffer, int bytes, int block)
1112 int ret, bytes_left, fl;
1115 fl = fcntl(fd, F_GETFL);
1118 fcntl(fd, F_SETFL, fl | O_NONBLOCK);
1120 fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
1124 while (bytes_left > 0) {
1125 ret = read(fd, p, bytes_left);
1129 if (errno != EAGAIN)
1141 static int do_file(void)
1143 struct per_dev_info *pdi;
1144 int i, j, nfiles = 0;
1146 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
1147 for (j = 0;; j++, nfiles++) {
1148 struct per_cpu_info *pci;
1152 pci = get_cpu_info(pdi, j);
1155 snprintf(pci->fname, sizeof(pci->fname)-1,
1156 "%s_out.%d", pdi->name, j);
1157 if (stat(pci->fname, &st) < 0)
1162 printf("Processing %s\n", pci->fname);
1164 tb = malloc(st.st_size);
1166 fprintf(stderr, "Out of memory, skip file %s\n",
1171 pci->fd = open(pci->fname, O_RDONLY);
1178 if (read_data(pci->fd, tb, st.st_size, 1)) {
1184 if (sort_entries(tb, st.st_size, ~0U, pdi, pci) == -1) {
1190 printf("Completed %s (CPU%d %d, entries)\n",
1191 pci->fname, j, pci->nelems);
1197 fprintf(stderr, "No files found\n");
1205 static void resize_buffer(void **buffer, long *size, long offset)
1207 long old_size = *size;
1210 *size = 64 * sizeof(struct blk_io_trace);
1213 *buffer = realloc(*buffer, *size);
1216 memset(*buffer + offset, 0, *size - old_size);
1219 static int read_sort_events(int fd, void **buffer, long *max_offset)
1224 events = offset = 0;
1226 struct blk_io_trace *t;
1230 if (*max_offset - offset < sizeof(*t))
1231 resize_buffer(buffer, max_offset, offset);
1233 if (read_data(fd, *buffer + offset, sizeof(*t), !events))
1236 t = *buffer + offset;
1237 offset += sizeof(*t);
1239 magic = be32_to_cpu(t->magic);
1240 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
1241 fprintf(stderr, "Bad magic %x\n", magic);
1245 pdu_len = be16_to_cpu(t->pdu_len);
1247 if (*max_offset - offset <= pdu_len)
1248 resize_buffer(buffer, max_offset, offset);
1250 if (read_data(fd, *buffer + offset, pdu_len, 1))
1257 } while (!is_done() && events < rb_batch);
1262 static int do_stdin(void)
1268 fd = dup(STDIN_FILENO);
1273 events = read_sort_events(fd, &ptr, &max_offset);
1277 if (sort_entries(ptr, ~0UL, events, NULL, NULL) == -1)
1291 static void flush_output(void)
1296 static void handle_sigint(int sig)
1302 static void usage(char *prog)
1304 fprintf(stderr, "Usage: %s [-i <name>] [-o <output>] [-s] <name>...\n",
1308 int main(int argc, char *argv[])
1313 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
1316 if (!strcmp(optarg, "-") && !pipeline)
1318 else if (resize_devices(optarg) != 0)
1322 output_name = optarg;
1325 rb_batch = atoi(optarg);
1327 rb_batch = RB_BATCH_DEFAULT;
1330 per_process_stats = 1;
1341 while (optind < argc) {
1342 if (!strcmp(argv[optind], "-") && !pipeline)
1344 else if (resize_devices(argv[optind]) != 0)
1349 if (!pipeline && !ndevices) {
1354 memset(&rb_sort_root, 0, sizeof(rb_sort_root));
1355 memset(&rb_track_root, 0, sizeof(rb_track_root));
1357 signal(SIGINT, handle_sigint);
1358 signal(SIGHUP, handle_sigint);
1359 signal(SIGTERM, handle_sigint);
1361 setlocale(LC_NUMERIC, "en_US");
1364 ofp = fdopen(STDOUT_FILENO, "w");
1369 snprintf(ofname, sizeof(ofname) - 1, "%s.log", output_name);
1370 ofp = fopen(ofname, "w");
1379 ofp_buffer = malloc(4096);
1380 if (setvbuf(ofp, ofp_buffer, mode, 4096)) {
1390 if (per_process_stats)
1391 show_process_stats();
1393 show_device_and_cpu_stats();