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:stq"
95 static struct option l_opts[] = {
115 .name = "per program stats",
140 static struct rb_root rb_sort_root;
141 static struct rb_root rb_track_root;
144 * for sorting the displayed output
147 struct blk_io_trace *bit;
148 struct rb_node rb_node;
152 * for tracking individual ios
155 struct rb_node rb_node;
160 unsigned long long allocation_time;
161 unsigned long long queue_time;
162 unsigned long long dispatch_time;
163 unsigned long long completion_time;
167 static struct per_dev_info *devices;
168 static char *get_dev_name(struct per_dev_info *, char *, int);
171 static char *output_name;
173 static unsigned long long genesis_time;
175 static int per_process_stats;
176 static int track_ios;
178 #define RB_BATCH_DEFAULT (1024)
179 static int rb_batch = RB_BATCH_DEFAULT;
183 #define is_done() (*(volatile int *)(&done))
184 static volatile int done;
186 static inline unsigned long hash_long(unsigned long val)
190 #elif __WORDSIZE == 64
191 val *= 0x9e37fffffffc0001UL;
193 #error unknown word size
196 return val >> (__WORDSIZE - PPI_HASH_SHIFT);
199 static inline void add_process_to_hash(struct per_process_info *ppi)
201 const int hash_idx = hash_long(ppi->pid);
203 ppi->hash_next = ppi_hash[hash_idx];
204 ppi_hash[hash_idx] = ppi;
207 static inline void add_process_to_list(struct per_process_info *ppi)
209 ppi->list_next = ppi_list;
213 static struct per_process_info *find_process_by_pid(__u32 pid)
215 const int hash_idx = hash_long(pid);
216 struct per_process_info *ppi;
218 ppi = ppi_hash[hash_idx];
223 ppi = ppi->hash_next;
229 static inline int trace_rb_insert(struct trace *t)
231 struct rb_node **p = &rb_sort_root.rb_node;
232 struct rb_node *parent = NULL;
235 if (genesis_time == 0 || t->bit->time < genesis_time)
236 genesis_time = t->bit->time;
240 __t = rb_entry(parent, struct trace, rb_node);
242 if (t->bit->time < __t->bit->time)
244 else if (t->bit->time > __t->bit->time)
246 else if (t->bit->device < __t->bit->device)
248 else if (t->bit->device > __t->bit->device)
250 else if (t->bit->sequence < __t->bit->sequence)
252 else if (t->bit->sequence > __t->bit->sequence)
254 else if (t->bit->device == __t->bit->device) {
256 "sequence alias (%d) on device %d,%d!\n",
258 MAJOR(t->bit->device), MINOR(t->bit->device));
263 rb_link_node(&t->rb_node, parent, p);
264 rb_insert_color(&t->rb_node, &rb_sort_root);
268 static inline int track_rb_insert(struct io_track *iot)
270 struct rb_node **p = &rb_track_root.rb_node;
271 struct rb_node *parent = NULL;
272 struct io_track *__iot;
277 __iot = rb_entry(parent, struct io_track, rb_node);
279 if (iot->device < __iot->device)
281 else if (iot->device > __iot->device)
283 else if (iot->sector < __iot->sector)
285 else if (iot->sector > __iot->sector)
289 "sector alias (%llu) on device %d,%d!\n",
291 MAJOR(iot->device), MINOR(iot->device));
296 rb_link_node(&iot->rb_node, parent, p);
297 rb_insert_color(&iot->rb_node, &rb_track_root);
301 static struct io_track *__find_track(dev_t device, __u64 sector)
303 struct rb_node **p = &rb_track_root.rb_node;
304 struct rb_node *parent = NULL;
305 struct io_track *__iot;
310 __iot = rb_entry(parent, struct io_track, rb_node);
312 if (device < __iot->device)
314 else if (device > __iot->device)
316 else if (sector < __iot->sector)
318 else if (sector > __iot->sector)
327 static struct io_track *find_track(__u32 pid, dev_t device, __u64 sector)
329 struct io_track *iot;
331 iot = __find_track(device, sector);
333 iot = malloc(sizeof(*iot));
335 iot->device = device;
336 iot->sector = sector;
337 track_rb_insert(iot);
343 static void log_track_merge(struct blk_io_trace *t)
345 struct io_track *iot;
349 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
352 iot = __find_track(t->device, t->sector - (t->bytes >> 10));
354 fprintf(stderr, "Trying to merge on non-existing request\n");
358 rb_erase(&iot->rb_node, &rb_track_root);
359 iot->sector -= t->bytes >> 10;
360 track_rb_insert(iot);
363 static void log_track_getrq(struct blk_io_trace *t)
365 struct io_track *iot;
370 iot = find_track(t->pid, t->device, t->sector);
371 iot->allocation_time = t->time;
376 * return time between rq allocation and queue
378 static unsigned long long log_track_queue(struct blk_io_trace *t)
380 unsigned long long elapsed;
381 struct io_track *iot;
386 iot = find_track(t->pid, t->device, t->sector);
387 iot->queue_time = t->time;
388 elapsed = iot->queue_time - iot->allocation_time;
390 if (per_process_stats) {
391 struct per_process_info *ppi = find_process_by_pid(iot->pid);
392 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
394 if (ppi && elapsed > ppi->longest_allocation_wait[w])
395 ppi->longest_allocation_wait[w] = elapsed;
402 * return time between queue and issue
404 static unsigned long long log_track_issue(struct blk_io_trace *t)
406 unsigned long long elapsed;
407 struct io_track *iot;
411 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
414 iot = __find_track(t->device, t->sector);
416 fprintf(stderr, "Trying to issue on non-existing request\n");
420 iot->dispatch_time = t->time;
421 elapsed = iot->dispatch_time - iot->queue_time;
423 if (per_process_stats) {
424 struct per_process_info *ppi = find_process_by_pid(iot->pid);
425 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
427 if (ppi && elapsed > ppi->longest_dispatch_wait[w])
428 ppi->longest_dispatch_wait[w] = elapsed;
435 * return time between dispatch and complete
437 static unsigned long long log_track_complete(struct blk_io_trace *t)
439 unsigned long long elapsed;
440 struct io_track *iot;
444 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
447 iot = __find_track(t->device, t->sector);
449 fprintf(stderr, "Trying to dispatch on non-existing request\n");
453 iot->completion_time = t->time;
454 elapsed = iot->completion_time - iot->dispatch_time;
456 if (per_process_stats) {
457 struct per_process_info *ppi = find_process_by_pid(iot->pid);
458 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
460 if (ppi && elapsed > ppi->longest_completion_wait[w])
461 ppi->longest_completion_wait[w] = elapsed;
465 * kill the trace, we don't need it after completion
467 rb_erase(&iot->rb_node, &rb_track_root);
474 static struct io_stats *find_process_io_stats(__u32 pid, char *name)
476 struct per_process_info *ppi = find_process_by_pid(pid);
479 ppi = malloc(sizeof(*ppi));
480 memset(ppi, 0, sizeof(*ppi));
481 strncpy(ppi->name, name, sizeof(ppi->name));
483 add_process_to_hash(ppi);
484 add_process_to_list(ppi);
487 return &ppi->io_stats;
491 static void resize_cpu_info(struct per_dev_info *pdi, int cpu)
493 struct per_cpu_info *cpus = pdi->cpus;
494 int ncpus = pdi->ncpus;
495 int new_count = cpu + 1;
499 size = new_count * sizeof(struct per_cpu_info);
500 cpus = realloc(cpus, size);
503 fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n",
504 get_dev_name(pdi, name, sizeof(name)), size);
508 new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info));
509 new_space = (new_count - ncpus) * sizeof(struct per_cpu_info);
510 memset(new_start, 0, new_space);
512 pdi->ncpus = new_count;
516 static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu)
518 if (cpu >= pdi->ncpus)
519 resize_cpu_info(pdi, cpu);
520 return &pdi->cpus[cpu];
524 static int resize_devices(char *name)
526 int size = (ndevices + 1) * sizeof(struct per_dev_info);
528 devices = realloc(devices, size);
530 fprintf(stderr, "Out of memory, device %s (%d)\n", name, size);
533 memset(&devices[ndevices], 0, sizeof(struct per_dev_info));
534 devices[ndevices].name = name;
539 static struct per_dev_info *get_dev_info(dev_t id, int create)
543 for (i = 0; i < ndevices; i++)
544 if (devices[i].id == id)
548 if (resize_devices(NULL) != 0)
550 return &devices[ndevices-1];
553 static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size)
556 snprintf(buffer, size, "%s", pdi->name);
558 snprintf(buffer, size, "%d,%d", MAJOR(pdi->id), MINOR(pdi->id));
563 static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit)
565 unsigned long long this = bit->time;
566 unsigned long long last = pdi->last_reported_time;
568 pdi->backwards = (this < last) ? 'B' : ' ';
569 pdi->last_reported_time = this;
573 static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t,
578 ios->qwrite_kb += t->bytes >> 10;
581 ios->qread_kb += t->bytes >> 10;
585 static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci,
588 __account_m(&pci->io_stats, t, rw);
590 if (per_process_stats) {
591 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
593 __account_m(ios, t, rw);
597 static inline void __account_q(struct io_stats *ios, struct blk_io_trace *t,
602 ios->qwrite_kb += t->bytes >> 10;
605 ios->qread_kb += t->bytes >> 10;
609 static inline void account_q(struct blk_io_trace *t, struct per_cpu_info *pci,
612 __account_q(&pci->io_stats, t, rw);
614 if (per_process_stats) {
615 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
617 __account_q(ios, t, rw);
621 static inline void __account_c(struct io_stats *ios, int rw, unsigned int bytes)
625 ios->cwrite_kb += bytes >> 10;
628 ios->cread_kb += bytes >> 10;
632 static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci,
635 __account_c(&pci->io_stats, rw, bytes);
637 if (per_process_stats) {
638 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
640 __account_c(ios, rw, bytes);
644 static inline void __account_i(struct io_stats *ios, int rw, unsigned int bytes)
648 ios->iwrite_kb += bytes >> 10;
651 ios->iread_kb += bytes >> 10;
655 static inline void account_i(struct blk_io_trace *t, struct per_cpu_info *pci,
658 __account_i(&pci->io_stats, rw, t->bytes);
660 if (per_process_stats) {
661 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
663 __account_i(ios, rw, t->bytes);
667 static void output(struct per_cpu_info *pci, char *s)
669 fprintf(ofp, "%s", s);
672 static char hstring[256];
673 static char tstring[256];
675 static inline char *setup_header(struct per_cpu_info *pci,
676 struct blk_io_trace *t, char act)
678 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
679 int b = t->action & BLK_TC_ACT(BLK_TC_BARRIER);
680 int s = t->action & BLK_TC_ACT(BLK_TC_SYNC);
695 sprintf(hstring, "%3d,%-3d %2d %8ld %5Lu.%09Lu %5u %c %3s",
696 MAJOR(t->device), MINOR(t->device), pci->cpu,
697 (unsigned long)t->sequence, SECONDS(t->time),
698 NANO_SECONDS(t->time), t->pid, act, rwbs);
703 static void log_complete(struct per_cpu_info *pci, struct blk_io_trace *t,
706 unsigned long long elapsed = log_track_complete(t);
708 if (elapsed != -1ULL) {
709 unsigned long usec = elapsed / 1000;
711 sprintf(tstring,"%s %Lu + %u (%8lu) [%d]\n",
712 setup_header(pci, t, act),
713 (unsigned long long)t->sector, t->bytes >> 9,
716 sprintf(tstring,"%s %Lu + %u [%d]\n", setup_header(pci, t, act),
717 (unsigned long long)t->sector, t->bytes >> 9, t->error);
720 output(pci, tstring);
723 static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t,
726 unsigned long long elapsed = log_track_queue(t);
728 if (elapsed != -1ULL) {
729 unsigned long usec = elapsed / 1000;
731 sprintf(tstring,"%s %Lu + %u (%8lu) [%s]\n",
732 setup_header(pci, t, act),
733 (unsigned long long)t->sector, t->bytes >> 9,
736 sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act),
737 (unsigned long long)t->sector, t->bytes >> 9, t->comm);
739 output(pci, tstring);
742 static void log_issue(struct per_cpu_info *pci, struct blk_io_trace *t,
745 unsigned long long elapsed = log_track_issue(t);
747 if (elapsed != -1ULL) {
748 double usec = (double) elapsed / 1000;
750 sprintf(tstring,"%s %Lu + %u (%8.2f) [%s]\n",
751 setup_header(pci, t, act),
752 (unsigned long long)t->sector, t->bytes >> 9,
755 sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act),
756 (unsigned long long)t->sector, t->bytes >> 9, t->comm);
759 output(pci, tstring);
762 static void log_merge(struct per_cpu_info *pci, struct blk_io_trace *t,
767 sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act),
768 (unsigned long long)t->sector, t->bytes >> 9, t->comm);
769 output(pci, tstring);
772 static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t,
775 sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act),
776 (unsigned long long)t->sector, t->bytes >> 9, t->comm);
777 output(pci, tstring);
780 static int log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char act)
785 sprintf(tstring,"%s ", setup_header(pci, t, act));
786 output(pci, tstring);
788 buf = (unsigned char *) t + sizeof(*t);
789 for (i = 0; i < t->pdu_len; i++) {
790 sprintf(tstring,"%02x ", buf[i]);
791 output(pci, tstring);
795 sprintf(tstring,"[%d]\n", t->error);
796 output(pci, tstring);
798 sprintf(tstring,"[%s]\n", t->comm);
799 output(pci, tstring);
804 static int dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci)
808 switch (t->action & 0xffff) {
810 log_generic(pci, t, 'Q');
813 log_generic(pci, t, 'G');
815 case __BLK_TA_SLEEPRQ:
816 log_generic(pci, t, 'S');
818 case __BLK_TA_REQUEUE:
819 log_generic(pci, t, 'R');
822 ret = log_pc(pci, t, 'D');
824 case __BLK_TA_COMPLETE:
828 fprintf(stderr, "Bad pc action %x\n", t->action);
836 static void dump_trace_fs(struct blk_io_trace *t, struct per_cpu_info *pci)
838 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
839 int act = t->action & 0xffff;
843 account_q(t, pci, w);
844 log_queue(pci, t, 'Q');
846 case __BLK_TA_BACKMERGE:
847 account_m(t, pci, w);
848 log_merge(pci, t, 'M');
850 case __BLK_TA_FRONTMERGE:
851 account_m(t, pci, w);
852 log_merge(pci, t, 'F');
856 log_generic(pci, t, 'G');
858 case __BLK_TA_SLEEPRQ:
859 log_generic(pci, t, 'S');
861 case __BLK_TA_REQUEUE:
862 account_c(t, pci, w, -t->bytes);
863 log_queue(pci, t, 'R');
866 account_i(t, pci, w);
867 log_issue(pci, t, 'D');
869 case __BLK_TA_COMPLETE:
870 account_c(t, pci, w, t->bytes);
871 log_complete(pci, t, 'C');
874 fprintf(stderr, "Bad fs action %x\n", t->action);
879 static int dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
880 struct per_dev_info *pdi)
884 if (t->action & BLK_TC_ACT(BLK_TC_PC))
885 ret = dump_trace_pc(t, pci);
887 dump_trace_fs(t, pci);
893 static void dump_io_stats(struct io_stats *ios, char *msg)
895 fprintf(ofp, "%s\n", msg);
897 fprintf(ofp, " Reads Queued: %'8lu, %'8LuKiB\t", ios->qreads, ios->qread_kb);
898 fprintf(ofp, " Writes Queued: %'8lu, %'8LuKiB\n", ios->qwrites,ios->qwrite_kb);
900 fprintf(ofp, " Read Dispatches: %'8lu, %'8LuKiB\t", ios->ireads, ios->iread_kb);
901 fprintf(ofp, " Write Dispatches: %'8lu, %'8LuKiB\n", ios->iwrites,ios->iwrite_kb);
902 fprintf(ofp, " Reads Completed: %'8lu, %'8LuKiB\t", ios->creads, ios->cread_kb);
903 fprintf(ofp, " Writes Completed: %'8lu, %'8LuKiB\n", ios->cwrites,ios->cwrite_kb);
904 fprintf(ofp, " Read Merges: %'8lu%8c\t", ios->mreads, ' ');
906 fprintf(ofp, " Write Merges: %'8lu\n", ios->mwrites);
909 static void dump_wait_stats(struct per_process_info *ppi)
911 unsigned long rawait = ppi->longest_allocation_wait[0] / 1000;
912 unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000;
913 unsigned long rcwait = ppi->longest_completion_wait[0] / 1000;
914 unsigned long wawait = ppi->longest_allocation_wait[1] / 1000;
915 unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000;
916 unsigned long wcwait = ppi->longest_completion_wait[1] / 1000;
918 fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' ');
919 fprintf(ofp, " Allocation wait: %'8lu\n", wawait);
920 fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' ');
921 fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait);
922 fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' ');
923 fprintf(ofp, " Completion wait: %'8lu\n", wcwait);
926 static void show_process_stats(void)
928 struct per_process_info *ppi;
932 dump_io_stats(&ppi->io_stats, ppi->name);
933 dump_wait_stats(ppi);
934 ppi = ppi->list_next;
940 static void show_device_and_cpu_stats(void)
942 struct per_dev_info *pdi;
943 struct per_cpu_info *pci;
944 struct io_stats total, *ios;
945 int i, j, pci_events;
946 char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3];
949 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
951 memset(&total, 0, sizeof(total));
957 for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) {
961 ios = &pci->io_stats;
962 total.qreads += ios->qreads;
963 total.qwrites += ios->qwrites;
964 total.creads += ios->creads;
965 total.cwrites += ios->cwrites;
966 total.mreads += ios->mreads;
967 total.mwrites += ios->mwrites;
968 total.ireads += ios->ireads;
969 total.iwrites += ios->iwrites;
970 total.qread_kb += ios->qread_kb;
971 total.qwrite_kb += ios->qwrite_kb;
972 total.cread_kb += ios->cread_kb;
973 total.cwrite_kb += ios->cwrite_kb;
974 total.iread_kb += ios->iread_kb;
975 total.iwrite_kb += ios->iwrite_kb;
977 snprintf(line, sizeof(line) - 1, "CPU%d (%s):",
978 j, get_dev_name(pdi, name, sizeof(name)));
979 dump_io_stats(ios, line);
983 if (pci_events > 1) {
985 snprintf(line, sizeof(line) - 1, "Total (%s):",
986 get_dev_name(pdi, name, sizeof(name)));
987 dump_io_stats(&total, line);
990 fprintf(ofp, "Events (%s): %'Lu\n",
991 get_dev_name(pdi, line, sizeof(line)), pdi->events);
995 static struct blk_io_trace *find_trace(void *p, unsigned long offset, int nr)
997 unsigned long max_offset = min(offset,nr * sizeof(struct blk_io_trace));
999 struct blk_io_trace *bit;
1002 for (off = 0; off < max_offset; off++) {
1005 magic = be32_to_cpu(bit->magic);
1006 if ((magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
1013 static int sort_entries(void *traces, unsigned long offset, int nr,
1014 struct per_dev_info *fpdi, struct per_cpu_info *fpci)
1016 struct per_dev_info *pdi;
1017 struct per_cpu_info *pci;
1018 struct blk_io_trace *bit;
1020 void *start = traces;
1022 while (traces - start <= offset - sizeof(*bit)) {
1026 bit = find_trace(traces, offset - (traces - start), nr);
1030 t = malloc(sizeof(*t));
1032 fprintf(stderr, "Out of memory, seq %d on dev %d,%d\n",
1034 MAJOR(bit->device), MINOR(bit->device));
1038 memset(&t->rb_node, 0, sizeof(t->rb_node));
1042 if (verify_trace(bit)) {
1047 pdi = fpdi ? fpdi : get_dev_info(bit->device, 1);
1048 pdi->id = bit->device;
1049 pci = fpci ? fpci : get_cpu_info(pdi, bit->cpu);
1050 pci->cpu = bit->cpu;
1053 if (trace_rb_insert(t)) {
1058 traces += sizeof(*bit) + bit->pdu_len;
1065 static void free_entries_rb(void)
1069 while ((n = rb_first(&rb_sort_root)) != NULL) {
1070 struct trace *t = rb_entry(n, struct trace, rb_node);
1072 rb_erase(&t->rb_node, &rb_sort_root);
1077 static void show_entries_rb(void)
1079 struct per_dev_info *pdi;
1080 struct blk_io_trace *bit;
1085 n = rb_first(&rb_sort_root);
1090 t = rb_entry(n, struct trace, rb_node);
1093 pdi = get_dev_info(bit->device, 0);
1095 fprintf(stderr, "Unknown device ID? (%d,%d)\n",
1096 MAJOR(bit->device), MINOR(bit->device));
1100 if (cpu > pdi->ncpus) {
1101 fprintf(stderr, "Unknown CPU ID? (%d, device %d,%d)\n",
1102 cpu, MAJOR(bit->device), MINOR(bit->device));
1106 bit->time -= genesis_time;
1108 check_time(pdi, bit);
1110 if (dump_trace(bit, &pdi->cpus[cpu], pdi))
1113 } while ((n = rb_next(n)) != NULL);
1116 static int read_data(int fd, void *buffer, int bytes, int block)
1118 int ret, bytes_left, fl;
1121 fl = fcntl(fd, F_GETFL);
1124 fcntl(fd, F_SETFL, fl | O_NONBLOCK);
1126 fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
1130 while (bytes_left > 0) {
1131 ret = read(fd, p, bytes_left);
1135 if (errno != EAGAIN)
1147 static int do_file(void)
1149 struct per_dev_info *pdi;
1150 int i, j, nfiles = 0;
1152 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
1153 for (j = 0;; j++, nfiles++) {
1154 struct per_cpu_info *pci;
1158 pci = get_cpu_info(pdi, j);
1161 snprintf(pci->fname, sizeof(pci->fname)-1,
1162 "%s_out.%d", pdi->name, j);
1163 if (stat(pci->fname, &st) < 0)
1168 printf("Processing %s\n", pci->fname);
1170 tb = malloc(st.st_size);
1172 fprintf(stderr, "Out of memory, skip file %s\n",
1177 pci->fd = open(pci->fname, O_RDONLY);
1184 if (read_data(pci->fd, tb, st.st_size, 1)) {
1190 if (sort_entries(tb, st.st_size, ~0U, pdi, pci) == -1) {
1196 printf("Completed %s (CPU%d %d, entries)\n",
1197 pci->fname, j, pci->nelems);
1203 fprintf(stderr, "No files found\n");
1211 static void resize_buffer(void **buffer, long *size, long offset)
1213 long old_size = *size;
1216 *size = 64 * sizeof(struct blk_io_trace);
1219 *buffer = realloc(*buffer, *size);
1222 memset(*buffer + offset, 0, *size - old_size);
1225 static int read_sort_events(int fd, void **buffer, long *max_offset)
1230 events = offset = 0;
1232 struct blk_io_trace *t;
1236 if (*max_offset - offset < sizeof(*t))
1237 resize_buffer(buffer, max_offset, offset);
1239 if (read_data(fd, *buffer + offset, sizeof(*t), !events))
1242 t = *buffer + offset;
1243 offset += sizeof(*t);
1245 magic = be32_to_cpu(t->magic);
1246 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
1247 fprintf(stderr, "Bad magic %x\n", magic);
1251 pdu_len = be16_to_cpu(t->pdu_len);
1253 if (*max_offset - offset <= pdu_len)
1254 resize_buffer(buffer, max_offset, offset);
1256 if (read_data(fd, *buffer + offset, pdu_len, 1))
1263 } while (!is_done() && events < rb_batch);
1268 static int do_stdin(void)
1274 fd = dup(STDIN_FILENO);
1279 events = read_sort_events(fd, &ptr, &max_offset);
1283 if (sort_entries(ptr, ~0UL, events, NULL, NULL) == -1)
1297 static void flush_output(void)
1302 static void handle_sigint(int sig)
1308 static void usage(char *prog)
1310 fprintf(stderr, "Usage: %s [-i <name>] [-o <output>] [-s] <name>...\n",
1314 int main(int argc, char *argv[])
1318 int per_device_and_cpu_stats = 1;
1320 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
1323 if (!strcmp(optarg, "-") && !pipeline)
1325 else if (resize_devices(optarg) != 0)
1329 output_name = optarg;
1332 rb_batch = atoi(optarg);
1334 rb_batch = RB_BATCH_DEFAULT;
1337 per_process_stats = 1;
1343 per_device_and_cpu_stats = 0;
1351 while (optind < argc) {
1352 if (!strcmp(argv[optind], "-") && !pipeline)
1354 else if (resize_devices(argv[optind]) != 0)
1359 if (!pipeline && !ndevices) {
1364 memset(&rb_sort_root, 0, sizeof(rb_sort_root));
1365 memset(&rb_track_root, 0, sizeof(rb_track_root));
1367 signal(SIGINT, handle_sigint);
1368 signal(SIGHUP, handle_sigint);
1369 signal(SIGTERM, handle_sigint);
1371 setlocale(LC_NUMERIC, "en_US");
1374 ofp = fdopen(STDOUT_FILENO, "w");
1379 snprintf(ofname, sizeof(ofname) - 1, "%s.log", output_name);
1380 ofp = fopen(ofname, "w");
1389 ofp_buffer = malloc(4096);
1390 if (setvbuf(ofp, ofp_buffer, mode, 4096)) {
1400 if (per_process_stats)
1401 show_process_stats();
1403 if (per_device_and_cpu_stats)
1404 show_device_and_cpu_stats();