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;
62 struct io_stats io_stats;
70 unsigned long long events;
71 unsigned long long last_reported_time;
72 struct io_stats io_stats;
75 struct per_cpu_info *cpus;
78 struct per_process_info {
81 struct io_stats io_stats;
82 struct per_process_info *hash_next, *list_next;
87 unsigned long long longest_allocation_wait[2];
88 unsigned long long longest_dispatch_wait[2];
89 unsigned long long longest_completion_wait[2];
92 #define PPI_HASH_SHIFT (8)
93 static struct per_process_info *ppi_hash[1 << PPI_HASH_SHIFT];
94 static struct per_process_info *ppi_list;
96 #define S_OPTS "i:o:b:stqw:"
97 static struct option l_opts[] = {
117 .name = "per program stats",
148 static struct rb_root rb_sort_root;
149 static struct rb_root rb_track_root;
152 * for sorting the displayed output
155 struct blk_io_trace *bit;
156 struct rb_node rb_node;
160 * for tracking individual ios
163 struct rb_node rb_node;
168 unsigned long long allocation_time;
169 unsigned long long queue_time;
170 unsigned long long dispatch_time;
171 unsigned long long completion_time;
175 static struct per_dev_info *devices;
176 static char *get_dev_name(struct per_dev_info *, char *, int);
179 static char *output_name;
181 static unsigned long long genesis_time;
182 static unsigned long long stopwatch_start; /* start from zero by default */
183 static unsigned long long stopwatch_end = ULONG_LONG_MAX; /* "infinity" */
185 static int per_process_stats;
186 static int track_ios;
188 #define RB_BATCH_DEFAULT (1024)
189 static int rb_batch = RB_BATCH_DEFAULT;
193 #define is_done() (*(volatile int *)(&done))
194 static volatile int done;
196 static inline unsigned long hash_long(unsigned long val)
200 #elif __WORDSIZE == 64
201 val *= 0x9e37fffffffc0001UL;
203 #error unknown word size
206 return val >> (__WORDSIZE - PPI_HASH_SHIFT);
209 static inline void add_process_to_hash(struct per_process_info *ppi)
211 const int hash_idx = hash_long(ppi->pid);
213 ppi->hash_next = ppi_hash[hash_idx];
214 ppi_hash[hash_idx] = ppi;
217 static inline void add_process_to_list(struct per_process_info *ppi)
219 ppi->list_next = ppi_list;
223 static struct per_process_info *find_process_by_pid(__u32 pid)
225 const int hash_idx = hash_long(pid);
226 struct per_process_info *ppi;
228 ppi = ppi_hash[hash_idx];
233 ppi = ppi->hash_next;
239 static inline int trace_rb_insert(struct trace *t)
241 struct rb_node **p = &rb_sort_root.rb_node;
242 struct rb_node *parent = NULL;
245 if (genesis_time == 0 || t->bit->time < genesis_time)
246 genesis_time = t->bit->time;
250 __t = rb_entry(parent, struct trace, rb_node);
252 if (t->bit->time < __t->bit->time)
254 else if (t->bit->time > __t->bit->time)
256 else if (t->bit->device < __t->bit->device)
258 else if (t->bit->device > __t->bit->device)
260 else if (t->bit->sequence < __t->bit->sequence)
262 else if (t->bit->sequence > __t->bit->sequence)
264 else if (t->bit->device == __t->bit->device) {
266 "sequence alias (%d) on device %d,%d!\n",
268 MAJOR(t->bit->device), MINOR(t->bit->device));
273 rb_link_node(&t->rb_node, parent, p);
274 rb_insert_color(&t->rb_node, &rb_sort_root);
278 static inline int track_rb_insert(struct io_track *iot)
280 struct rb_node **p = &rb_track_root.rb_node;
281 struct rb_node *parent = NULL;
282 struct io_track *__iot;
287 __iot = rb_entry(parent, struct io_track, rb_node);
289 if (iot->device < __iot->device)
291 else if (iot->device > __iot->device)
293 else if (iot->sector < __iot->sector)
295 else if (iot->sector > __iot->sector)
299 "sector alias (%llu) on device %d,%d!\n",
301 MAJOR(iot->device), MINOR(iot->device));
306 rb_link_node(&iot->rb_node, parent, p);
307 rb_insert_color(&iot->rb_node, &rb_track_root);
311 static struct io_track *__find_track(dev_t device, __u64 sector)
313 struct rb_node **p = &rb_track_root.rb_node;
314 struct rb_node *parent = NULL;
315 struct io_track *__iot;
320 __iot = rb_entry(parent, struct io_track, rb_node);
322 if (device < __iot->device)
324 else if (device > __iot->device)
326 else if (sector < __iot->sector)
328 else if (sector > __iot->sector)
337 static struct io_track *find_track(__u32 pid, dev_t device, __u64 sector)
339 struct io_track *iot;
341 iot = __find_track(device, sector);
343 iot = malloc(sizeof(*iot));
345 iot->device = device;
346 iot->sector = sector;
347 track_rb_insert(iot);
353 static void log_track_merge(struct blk_io_trace *t)
355 struct io_track *iot;
359 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
362 iot = __find_track(t->device, t->sector - (t->bytes >> 10));
364 fprintf(stderr, "Trying to merge on non-existing request\n");
368 rb_erase(&iot->rb_node, &rb_track_root);
369 iot->sector -= t->bytes >> 10;
370 track_rb_insert(iot);
373 static void log_track_getrq(struct blk_io_trace *t)
375 struct io_track *iot;
380 iot = find_track(t->pid, t->device, t->sector);
381 iot->allocation_time = t->time;
386 * return time between rq allocation and queue
388 static unsigned long long log_track_queue(struct blk_io_trace *t)
390 unsigned long long elapsed;
391 struct io_track *iot;
396 iot = find_track(t->pid, t->device, t->sector);
397 iot->queue_time = t->time;
398 elapsed = iot->queue_time - iot->allocation_time;
400 if (per_process_stats) {
401 struct per_process_info *ppi = find_process_by_pid(iot->pid);
402 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
404 if (ppi && elapsed > ppi->longest_allocation_wait[w])
405 ppi->longest_allocation_wait[w] = elapsed;
412 * return time between queue and issue
414 static unsigned long long log_track_issue(struct blk_io_trace *t)
416 unsigned long long elapsed;
417 struct io_track *iot;
421 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
424 iot = __find_track(t->device, t->sector);
426 fprintf(stderr, "Trying to issue on non-existing request\n");
430 iot->dispatch_time = t->time;
431 elapsed = iot->dispatch_time - iot->queue_time;
433 if (per_process_stats) {
434 struct per_process_info *ppi = find_process_by_pid(iot->pid);
435 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
437 if (ppi && elapsed > ppi->longest_dispatch_wait[w])
438 ppi->longest_dispatch_wait[w] = elapsed;
445 * return time between dispatch and complete
447 static unsigned long long log_track_complete(struct blk_io_trace *t)
449 unsigned long long elapsed;
450 struct io_track *iot;
454 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
457 iot = __find_track(t->device, t->sector);
459 fprintf(stderr, "Trying to dispatch on non-existing request\n");
463 iot->completion_time = t->time;
464 elapsed = iot->completion_time - iot->dispatch_time;
466 if (per_process_stats) {
467 struct per_process_info *ppi = find_process_by_pid(iot->pid);
468 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
470 if (ppi && elapsed > ppi->longest_completion_wait[w])
471 ppi->longest_completion_wait[w] = elapsed;
475 * kill the trace, we don't need it after completion
477 rb_erase(&iot->rb_node, &rb_track_root);
484 static struct io_stats *find_process_io_stats(__u32 pid, char *name)
486 struct per_process_info *ppi = find_process_by_pid(pid);
489 ppi = malloc(sizeof(*ppi));
490 memset(ppi, 0, sizeof(*ppi));
491 strncpy(ppi->name, name, sizeof(ppi->name));
493 add_process_to_hash(ppi);
494 add_process_to_list(ppi);
497 return &ppi->io_stats;
501 static void resize_cpu_info(struct per_dev_info *pdi, int cpu)
503 struct per_cpu_info *cpus = pdi->cpus;
504 int ncpus = pdi->ncpus;
505 int new_count = cpu + 1;
509 size = new_count * sizeof(struct per_cpu_info);
510 cpus = realloc(cpus, size);
513 fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n",
514 get_dev_name(pdi, name, sizeof(name)), size);
518 new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info));
519 new_space = (new_count - ncpus) * sizeof(struct per_cpu_info);
520 memset(new_start, 0, new_space);
522 pdi->ncpus = new_count;
526 static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu)
528 if (cpu >= pdi->ncpus)
529 resize_cpu_info(pdi, cpu);
530 return &pdi->cpus[cpu];
534 static int resize_devices(char *name)
536 int size = (ndevices + 1) * sizeof(struct per_dev_info);
538 devices = realloc(devices, size);
540 fprintf(stderr, "Out of memory, device %s (%d)\n", name, size);
543 memset(&devices[ndevices], 0, sizeof(struct per_dev_info));
544 devices[ndevices].name = name;
549 static struct per_dev_info *get_dev_info(dev_t id, int create)
553 for (i = 0; i < ndevices; i++)
554 if (devices[i].id == id)
558 if (resize_devices(NULL) != 0)
560 return &devices[ndevices-1];
563 static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size)
566 snprintf(buffer, size, "%s", pdi->name);
568 snprintf(buffer, size, "%d,%d", MAJOR(pdi->id), MINOR(pdi->id));
573 static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit)
575 unsigned long long this = bit->time;
576 unsigned long long last = pdi->last_reported_time;
578 pdi->backwards = (this < last) ? 'B' : ' ';
579 pdi->last_reported_time = this;
583 static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t,
588 ios->qwrite_kb += t->bytes >> 10;
591 ios->qread_kb += t->bytes >> 10;
595 static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci,
598 __account_m(&pci->io_stats, t, rw);
600 if (per_process_stats) {
601 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
603 __account_m(ios, t, rw);
607 static inline void __account_q(struct io_stats *ios, struct blk_io_trace *t,
612 ios->qwrite_kb += t->bytes >> 10;
615 ios->qread_kb += t->bytes >> 10;
619 static inline void account_q(struct blk_io_trace *t, struct per_cpu_info *pci,
622 __account_q(&pci->io_stats, t, rw);
624 if (per_process_stats) {
625 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
627 __account_q(ios, t, rw);
631 static inline void __account_c(struct io_stats *ios, int rw, unsigned int bytes)
635 ios->cwrite_kb += bytes >> 10;
638 ios->cread_kb += bytes >> 10;
642 static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci,
645 __account_c(&pci->io_stats, rw, bytes);
647 if (per_process_stats) {
648 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
650 __account_c(ios, rw, bytes);
654 static inline void __account_i(struct io_stats *ios, int rw, unsigned int bytes)
658 ios->iwrite_kb += bytes >> 10;
661 ios->iread_kb += bytes >> 10;
665 static inline void account_i(struct blk_io_trace *t, struct per_cpu_info *pci,
668 __account_i(&pci->io_stats, rw, t->bytes);
670 if (per_process_stats) {
671 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
673 __account_i(ios, rw, t->bytes);
677 static void output(struct per_cpu_info *pci, char *s)
679 fprintf(ofp, "%s", s);
682 static char hstring[256];
683 static char tstring[256];
685 static inline char *setup_header(struct per_cpu_info *pci,
686 struct blk_io_trace *t, char act)
688 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
689 int b = t->action & BLK_TC_ACT(BLK_TC_BARRIER);
690 int s = t->action & BLK_TC_ACT(BLK_TC_SYNC);
705 sprintf(hstring, "%3d,%-3d %2d %8ld %5Lu.%09Lu %5u %c %3s",
706 MAJOR(t->device), MINOR(t->device), pci->cpu,
707 (unsigned long)t->sequence, SECONDS(t->time),
708 NANO_SECONDS(t->time), t->pid, act, rwbs);
713 static void log_complete(struct per_cpu_info *pci, struct blk_io_trace *t,
716 unsigned long long elapsed = log_track_complete(t);
718 if (elapsed != -1ULL) {
719 unsigned long usec = elapsed / 1000;
721 sprintf(tstring,"%s %Lu + %u (%8lu) [%d]\n",
722 setup_header(pci, t, act),
723 (unsigned long long)t->sector, t->bytes >> 9,
726 sprintf(tstring,"%s %Lu + %u [%d]\n", setup_header(pci, t, act),
727 (unsigned long long)t->sector, t->bytes >> 9, t->error);
730 output(pci, tstring);
733 static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t,
736 unsigned long long elapsed = log_track_queue(t);
738 if (elapsed != -1ULL) {
739 unsigned long usec = elapsed / 1000;
741 sprintf(tstring,"%s %Lu + %u (%8lu) [%s]\n",
742 setup_header(pci, t, act),
743 (unsigned long long)t->sector, t->bytes >> 9,
746 sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act),
747 (unsigned long long)t->sector, t->bytes >> 9, t->comm);
749 output(pci, tstring);
752 static void log_issue(struct per_cpu_info *pci, struct blk_io_trace *t,
755 unsigned long long elapsed = log_track_issue(t);
757 if (elapsed != -1ULL) {
758 double usec = (double) elapsed / 1000;
760 sprintf(tstring,"%s %Lu + %u (%8.2f) [%s]\n",
761 setup_header(pci, t, act),
762 (unsigned long long)t->sector, t->bytes >> 9,
765 sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act),
766 (unsigned long long)t->sector, t->bytes >> 9, t->comm);
769 output(pci, tstring);
772 static void log_merge(struct per_cpu_info *pci, struct blk_io_trace *t,
777 sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act),
778 (unsigned long long)t->sector, t->bytes >> 9, t->comm);
779 output(pci, tstring);
782 static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t,
785 sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act),
786 (unsigned long long)t->sector, t->bytes >> 9, t->comm);
787 output(pci, tstring);
790 static int log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char act)
795 sprintf(tstring,"%s ", setup_header(pci, t, act));
796 output(pci, tstring);
798 buf = (unsigned char *) t + sizeof(*t);
799 for (i = 0; i < t->pdu_len; i++) {
800 sprintf(tstring,"%02x ", buf[i]);
801 output(pci, tstring);
805 sprintf(tstring,"[%d]\n", t->error);
806 output(pci, tstring);
808 sprintf(tstring,"[%s]\n", t->comm);
809 output(pci, tstring);
814 static int dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci)
818 switch (t->action & 0xffff) {
820 log_generic(pci, t, 'Q');
823 log_generic(pci, t, 'G');
825 case __BLK_TA_SLEEPRQ:
826 log_generic(pci, t, 'S');
828 case __BLK_TA_REQUEUE:
829 log_generic(pci, t, 'R');
832 ret = log_pc(pci, t, 'D');
834 case __BLK_TA_COMPLETE:
838 fprintf(stderr, "Bad pc action %x\n", t->action);
846 static void dump_trace_fs(struct blk_io_trace *t, struct per_cpu_info *pci)
848 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
849 int act = t->action & 0xffff;
853 account_q(t, pci, w);
854 log_queue(pci, t, 'Q');
856 case __BLK_TA_BACKMERGE:
857 account_m(t, pci, w);
858 log_merge(pci, t, 'M');
860 case __BLK_TA_FRONTMERGE:
861 account_m(t, pci, w);
862 log_merge(pci, t, 'F');
866 log_generic(pci, t, 'G');
868 case __BLK_TA_SLEEPRQ:
869 log_generic(pci, t, 'S');
871 case __BLK_TA_REQUEUE:
872 account_c(t, pci, w, -t->bytes);
873 log_queue(pci, t, 'R');
876 account_i(t, pci, w);
877 log_issue(pci, t, 'D');
879 case __BLK_TA_COMPLETE:
880 account_c(t, pci, w, t->bytes);
881 log_complete(pci, t, 'C');
884 log_generic(pci, t, 'P');
886 case __BLK_TA_UNPLUG:
887 log_generic(pci, t, 'U');
890 fprintf(stderr, "Bad fs action %x\n", t->action);
895 static int dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
896 struct per_dev_info *pdi)
900 if (t->action & BLK_TC_ACT(BLK_TC_PC))
901 ret = dump_trace_pc(t, pci);
903 dump_trace_fs(t, pci);
909 static void dump_io_stats(struct io_stats *ios, char *msg)
911 fprintf(ofp, "%s\n", msg);
913 fprintf(ofp, " Reads Queued: %'8lu, %'8LuKiB\t", ios->qreads, ios->qread_kb);
914 fprintf(ofp, " Writes Queued: %'8lu, %'8LuKiB\n", ios->qwrites,ios->qwrite_kb);
916 fprintf(ofp, " Read Dispatches: %'8lu, %'8LuKiB\t", ios->ireads, ios->iread_kb);
917 fprintf(ofp, " Write Dispatches: %'8lu, %'8LuKiB\n", ios->iwrites,ios->iwrite_kb);
918 fprintf(ofp, " Reads Completed: %'8lu, %'8LuKiB\t", ios->creads, ios->cread_kb);
919 fprintf(ofp, " Writes Completed: %'8lu, %'8LuKiB\n", ios->cwrites,ios->cwrite_kb);
920 fprintf(ofp, " Read Merges: %'8lu%8c\t", ios->mreads, ' ');
922 fprintf(ofp, " Write Merges: %'8lu\n", ios->mwrites);
925 static void dump_wait_stats(struct per_process_info *ppi)
927 unsigned long rawait = ppi->longest_allocation_wait[0] / 1000;
928 unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000;
929 unsigned long rcwait = ppi->longest_completion_wait[0] / 1000;
930 unsigned long wawait = ppi->longest_allocation_wait[1] / 1000;
931 unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000;
932 unsigned long wcwait = ppi->longest_completion_wait[1] / 1000;
934 fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' ');
935 fprintf(ofp, " Allocation wait: %'8lu\n", wawait);
936 fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' ');
937 fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait);
938 fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' ');
939 fprintf(ofp, " Completion wait: %'8lu\n", wcwait);
942 static void show_process_stats(void)
944 struct per_process_info *ppi;
948 dump_io_stats(&ppi->io_stats, ppi->name);
949 dump_wait_stats(ppi);
950 ppi = ppi->list_next;
956 static void show_device_and_cpu_stats(void)
958 struct per_dev_info *pdi;
959 struct per_cpu_info *pci;
960 struct io_stats total, *ios;
961 int i, j, pci_events;
962 char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3];
965 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
967 memset(&total, 0, sizeof(total));
973 for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) {
977 ios = &pci->io_stats;
978 total.qreads += ios->qreads;
979 total.qwrites += ios->qwrites;
980 total.creads += ios->creads;
981 total.cwrites += ios->cwrites;
982 total.mreads += ios->mreads;
983 total.mwrites += ios->mwrites;
984 total.ireads += ios->ireads;
985 total.iwrites += ios->iwrites;
986 total.qread_kb += ios->qread_kb;
987 total.qwrite_kb += ios->qwrite_kb;
988 total.cread_kb += ios->cread_kb;
989 total.cwrite_kb += ios->cwrite_kb;
990 total.iread_kb += ios->iread_kb;
991 total.iwrite_kb += ios->iwrite_kb;
993 snprintf(line, sizeof(line) - 1, "CPU%d (%s):",
994 j, get_dev_name(pdi, name, sizeof(name)));
995 dump_io_stats(ios, line);
999 if (pci_events > 1) {
1001 snprintf(line, sizeof(line) - 1, "Total (%s):",
1002 get_dev_name(pdi, name, sizeof(name)));
1003 dump_io_stats(&total, line);
1006 fprintf(ofp, "Events (%s): %'Lu\n",
1007 get_dev_name(pdi, line, sizeof(line)), pdi->events);
1011 static struct blk_io_trace *find_trace(void *p, unsigned long offset, int nr)
1013 unsigned long max_offset = min(offset,nr * sizeof(struct blk_io_trace));
1015 struct blk_io_trace *bit;
1018 for (off = 0; off < max_offset; off++) {
1021 magic = be32_to_cpu(bit->magic);
1022 if ((magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
1029 static int sort_entries(void *traces, unsigned long offset, int nr,
1030 struct per_dev_info *fpdi, struct per_cpu_info *fpci)
1032 struct per_dev_info *pdi;
1033 struct per_cpu_info *pci;
1034 struct blk_io_trace *bit;
1036 void *start = traces;
1038 while (traces - start <= offset - sizeof(*bit)) {
1042 bit = find_trace(traces, offset - (traces - start), nr);
1046 t = malloc(sizeof(*t));
1048 fprintf(stderr, "Out of memory, seq %d on dev %d,%d\n",
1050 MAJOR(bit->device), MINOR(bit->device));
1054 memset(&t->rb_node, 0, sizeof(t->rb_node));
1058 if (verify_trace(bit)) {
1063 pdi = fpdi ? fpdi : get_dev_info(bit->device, 1);
1064 pdi->id = bit->device;
1065 pci = fpci ? fpci : get_cpu_info(pdi, bit->cpu);
1066 pci->cpu = bit->cpu;
1069 if (trace_rb_insert(t)) {
1074 traces += sizeof(*bit) + bit->pdu_len;
1081 static void free_entries_rb(void)
1085 while ((n = rb_first(&rb_sort_root)) != NULL) {
1086 struct trace *t = rb_entry(n, struct trace, rb_node);
1088 rb_erase(&t->rb_node, &rb_sort_root);
1093 static void show_entries_rb(void)
1095 struct per_dev_info *pdi;
1096 struct blk_io_trace *bit;
1101 n = rb_first(&rb_sort_root);
1106 t = rb_entry(n, struct trace, rb_node);
1109 pdi = get_dev_info(bit->device, 0);
1111 fprintf(stderr, "Unknown device ID? (%d,%d)\n",
1112 MAJOR(bit->device), MINOR(bit->device));
1116 if (cpu > pdi->ncpus) {
1117 fprintf(stderr, "Unknown CPU ID? (%d, device %d,%d)\n",
1118 cpu, MAJOR(bit->device), MINOR(bit->device));
1122 bit->time -= genesis_time;
1123 if (bit->time < stopwatch_start)
1125 if (bit->time >= stopwatch_end)
1128 check_time(pdi, bit);
1130 if (dump_trace(bit, &pdi->cpus[cpu], pdi))
1133 } while ((n = rb_next(n)) != NULL);
1136 static int read_data(int fd, void *buffer, int bytes, int block)
1138 int ret, bytes_left, fl;
1141 fl = fcntl(fd, F_GETFL);
1144 fcntl(fd, F_SETFL, fl | O_NONBLOCK);
1146 fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
1150 while (bytes_left > 0) {
1151 ret = read(fd, p, bytes_left);
1155 if (errno != EAGAIN)
1167 static int do_file(void)
1169 struct per_dev_info *pdi;
1170 int i, j, nfiles = 0;
1172 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
1173 for (j = 0;; j++, nfiles++) {
1174 struct per_cpu_info *pci;
1178 pci = get_cpu_info(pdi, j);
1181 snprintf(pci->fname, sizeof(pci->fname)-1,
1182 "%s_out.%d", pdi->name, j);
1183 if (stat(pci->fname, &st) < 0)
1188 printf("Processing %s\n", pci->fname);
1190 tb = malloc(st.st_size);
1192 fprintf(stderr, "Out of memory, skip file %s\n",
1197 pci->fd = open(pci->fname, O_RDONLY);
1204 if (read_data(pci->fd, tb, st.st_size, 1)) {
1210 if (sort_entries(tb, st.st_size, ~0U, pdi, pci) == -1) {
1216 printf("Completed %s (CPU%d %d, entries)\n",
1217 pci->fname, j, pci->nelems);
1223 fprintf(stderr, "No files found\n");
1231 static void resize_buffer(void **buffer, long *size, long offset)
1233 long old_size = *size;
1236 *size = 64 * sizeof(struct blk_io_trace);
1239 *buffer = realloc(*buffer, *size);
1242 memset(*buffer + offset, 0, *size - old_size);
1245 static int read_sort_events(int fd, void **buffer, long *max_offset)
1250 events = offset = 0;
1252 struct blk_io_trace *t;
1256 if (*max_offset - offset < sizeof(*t))
1257 resize_buffer(buffer, max_offset, offset);
1259 if (read_data(fd, *buffer + offset, sizeof(*t), !events))
1262 t = *buffer + offset;
1263 offset += sizeof(*t);
1265 magic = be32_to_cpu(t->magic);
1266 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
1267 fprintf(stderr, "Bad magic %x\n", magic);
1271 pdu_len = be16_to_cpu(t->pdu_len);
1273 if (*max_offset - offset <= pdu_len)
1274 resize_buffer(buffer, max_offset, offset);
1276 if (read_data(fd, *buffer + offset, pdu_len, 1))
1283 } while (!is_done() && events < rb_batch);
1288 static int do_stdin(void)
1294 fd = dup(STDIN_FILENO);
1299 events = read_sort_events(fd, &ptr, &max_offset);
1303 if (sort_entries(ptr, ~0UL, events, NULL, NULL) == -1)
1317 static void flush_output(void)
1322 static void handle_sigint(int sig)
1329 * Extract start and duration times from a string, allowing
1330 * us to specify a time interval of interest within a trace.
1331 * Format: "duration" (start is zero) or "start:duration".
1333 static int find_stopwatch_interval(char *string)
1338 value = strtod(string, &sp);
1340 fprintf(stderr,"Invalid stopwatch timer: %s\n", string);
1344 stopwatch_start = DOUBLE_TO_NANO_ULL(value);
1346 value = strtod(string, &sp);
1347 if (sp == string || *sp != '\0') {
1348 fprintf(stderr,"Invalid stopwatch duration time: %s\n",
1352 } else if (*sp != '\0') {
1353 fprintf(stderr,"Invalid stopwatch start timer: %s\n", string);
1356 stopwatch_end = stopwatch_start + DOUBLE_TO_NANO_ULL(value);
1360 static void usage(char *prog)
1362 fprintf(stderr, "Usage: %s "
1363 "[-i <name>] [-o <output>] [-s] [-w N[:n]] <name>...\n",
1367 int main(int argc, char *argv[])
1371 int per_device_and_cpu_stats = 1;
1373 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
1376 if (!strcmp(optarg, "-") && !pipeline)
1378 else if (resize_devices(optarg) != 0)
1382 output_name = optarg;
1385 rb_batch = atoi(optarg);
1387 rb_batch = RB_BATCH_DEFAULT;
1390 per_process_stats = 1;
1396 per_device_and_cpu_stats = 0;
1399 if (find_stopwatch_interval(optarg) != 0)
1408 while (optind < argc) {
1409 if (!strcmp(argv[optind], "-") && !pipeline)
1411 else if (resize_devices(argv[optind]) != 0)
1416 if (!pipeline && !ndevices) {
1421 memset(&rb_sort_root, 0, sizeof(rb_sort_root));
1422 memset(&rb_track_root, 0, sizeof(rb_track_root));
1424 signal(SIGINT, handle_sigint);
1425 signal(SIGHUP, handle_sigint);
1426 signal(SIGTERM, handle_sigint);
1428 setlocale(LC_NUMERIC, "en_US");
1431 ofp = fdopen(STDOUT_FILENO, "w");
1436 snprintf(ofname, sizeof(ofname) - 1, "%s.log", output_name);
1437 ofp = fopen(ofname, "w");
1446 ofp_buffer = malloc(4096);
1447 if (setvbuf(ofp, ofp_buffer, mode, 4096)) {
1457 if (per_process_stats)
1458 show_process_stats();
1460 if (per_device_and_cpu_stats)
1461 show_device_and_cpu_stats();