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>
38 static char blkparse_version[] = "0.99";
41 unsigned long start, end;
42 struct skip_info *prev, *next;
50 unsigned long long events;
51 unsigned long long first_reported_time;
52 unsigned long long last_reported_time;
53 unsigned long long last_read_time;
54 struct io_stats io_stats;
55 unsigned long last_sequence;
56 unsigned long skips, nskips;
57 unsigned long long seq_skips, seq_nskips;
59 struct rb_root rb_last;
60 unsigned long rb_last_entries;
62 struct rb_root rb_track;
67 unsigned long *cpu_map;
68 unsigned int cpu_map_max;
70 struct per_cpu_info *cpus;
71 struct skip_info *skips_head;
72 struct skip_info *skips_tail;
75 struct per_process_info {
78 struct io_stats io_stats;
79 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 #define PPI_HASH_SIZE (1 << PPI_HASH_SHIFT)
92 #define PPI_HASH_MASK (PPI_HASH_SIZE - 1)
93 static struct per_process_info *ppi_hash_table[PPI_HASH_SIZE];
94 static struct per_process_info *ppi_list;
95 static int ppi_list_entries;
97 #define S_OPTS "a:A:i:o:b:stqw:f:F:vVhD:"
98 static struct option l_opts[] = {
101 .has_arg = required_argument,
107 .has_arg = required_argument,
113 .has_arg = required_argument,
119 .has_arg = required_argument,
125 .has_arg = required_argument,
130 .name = "per-program-stats",
131 .has_arg = no_argument,
137 .has_arg = no_argument,
143 .has_arg = no_argument,
149 .has_arg = required_argument,
155 .has_arg = required_argument,
160 .name = "format-spec",
161 .has_arg = required_argument,
166 .name = "hash-by-name",
167 .has_arg = no_argument,
173 .has_arg = no_argument,
179 .has_arg = no_argument,
184 .name = "input-directory",
185 .has_arg = required_argument,
195 * for sorting the displayed output
198 struct blk_io_trace *bit;
199 struct rb_node rb_node;
203 static struct rb_root rb_sort_root;
204 static unsigned long rb_sort_entries;
206 static struct trace *trace_list;
211 static struct blk_io_trace *bit_alloc_list;
212 static struct trace *t_alloc_list;
215 * for tracking individual ios
218 struct rb_node rb_node;
223 unsigned long long allocation_time;
224 unsigned long long queue_time;
225 unsigned long long dispatch_time;
226 unsigned long long completion_time;
230 static struct per_dev_info *devices;
231 static char *get_dev_name(struct per_dev_info *, char *, int);
234 static char *output_name;
235 static char *input_dir;
237 static unsigned long long genesis_time;
238 static unsigned long long last_allowed_time;
239 static unsigned int smallest_seq_read;
240 static unsigned long long stopwatch_start; /* start from zero by default */
241 static unsigned long long stopwatch_end = -1ULL; /* "infinity" */
243 static int per_process_stats;
244 static int per_device_and_cpu_stats = 1;
245 static int track_ios;
246 static int ppi_hash_by_pid = 1;
248 static unsigned int act_mask = -1U;
249 static int stats_printed;
251 static unsigned int t_alloc_cache;
252 static unsigned int bit_alloc_cache;
254 #define RB_BATCH_DEFAULT (512)
255 static unsigned int rb_batch = RB_BATCH_DEFAULT;
259 #define is_done() (*(volatile int *)(&done))
260 static volatile int done;
262 #define JHASH_RANDOM (0x3af5f2ee)
264 #define CPUS_PER_LONG (8 * sizeof(unsigned long))
265 #define CPU_IDX(cpu) ((cpu) / CPUS_PER_LONG)
266 #define CPU_BIT(cpu) ((cpu) & (CPUS_PER_LONG - 1))
268 static void insert_skip(struct per_dev_info *pdi, unsigned long start,
271 struct skip_info *sip;
273 for (sip = pdi->skips_tail; sip != NULL; sip = sip->prev) {
274 if (end == (sip->start - 1)) {
277 } else if (start == (sip->end + 1)) {
283 sip = malloc(sizeof(struct skip_info));
286 sip->prev = sip->next = NULL;
287 if (pdi->skips_tail == NULL)
288 pdi->skips_head = pdi->skips_tail = sip;
290 sip->prev = pdi->skips_tail;
291 pdi->skips_tail->next = sip;
292 pdi->skips_tail = sip;
296 static void remove_sip(struct per_dev_info *pdi, struct skip_info *sip)
298 if (sip->prev == NULL) {
299 if (sip->next == NULL)
300 pdi->skips_head = pdi->skips_tail = NULL;
302 pdi->skips_head = sip->next;
303 sip->next->prev = NULL;
305 } else if (sip->next == NULL) {
306 pdi->skips_tail = sip->prev;
307 sip->prev->next = NULL;
309 sip->prev->next = sip->next;
310 sip->next->prev = sip->prev;
313 sip->prev = sip->next = NULL;
317 #define IN_SKIP(sip,seq) (((sip)->start <= (seq)) && ((seq) <= sip->end))
318 static int check_current_skips(struct per_dev_info *pdi, unsigned long seq)
320 struct skip_info *sip;
322 for (sip = pdi->skips_tail; sip != NULL; sip = sip->prev) {
323 if (IN_SKIP(sip,seq)) {
324 if (sip->start == seq) {
329 } else if (sip->end == seq)
333 insert_skip(pdi,seq+1,sip->end);
341 static void collect_pdi_skips(struct per_dev_info *pdi)
343 struct skip_info *sip;
347 for (sip = pdi->skips_head; sip != NULL; sip = sip->next) {
349 pdi->seq_skips += (sip->end - sip->start + 1);
351 fprintf(stderr, "(%d,%d): skipping %lu -> %lu\n",
352 MAJOR(pdi->dev), MINOR(pdi->dev),
353 sip->start, sip->end);
357 static void cpu_mark_online(struct per_dev_info *pdi, unsigned int cpu)
359 if (cpu >= pdi->cpu_map_max || !pdi->cpu_map) {
360 int new_max = (cpu + CPUS_PER_LONG) & ~(CPUS_PER_LONG - 1);
361 unsigned long *map = malloc(new_max / sizeof(long));
363 memset(map, 0, new_max / sizeof(long));
366 memcpy(map, pdi->cpu_map, pdi->cpu_map_max / sizeof(long));
371 pdi->cpu_map_max = new_max;
374 pdi->cpu_map[CPU_IDX(cpu)] |= (1UL << CPU_BIT(cpu));
377 static inline void cpu_mark_offline(struct per_dev_info *pdi, int cpu)
379 pdi->cpu_map[CPU_IDX(cpu)] &= ~(1UL << CPU_BIT(cpu));
382 static inline int cpu_is_online(struct per_dev_info *pdi, int cpu)
384 return (pdi->cpu_map[CPU_IDX(cpu)] & (1UL << CPU_BIT(cpu))) != 0;
387 static inline int ppi_hash_pid(__u32 pid)
389 return jhash_1word(pid, JHASH_RANDOM) & PPI_HASH_MASK;
392 static inline int ppi_hash_name(const char *name)
394 return jhash(name, 16, JHASH_RANDOM) & PPI_HASH_MASK;
397 static inline int ppi_hash(struct per_process_info *ppi)
400 return ppi_hash_pid(ppi->pid);
402 return ppi_hash_name(ppi->name);
405 static inline void add_process_to_hash(struct per_process_info *ppi)
407 const int hash_idx = ppi_hash(ppi);
409 ppi->hash_next = ppi_hash_table[hash_idx];
410 ppi_hash_table[hash_idx] = ppi;
413 static inline void add_process_to_list(struct per_process_info *ppi)
415 ppi->list_next = ppi_list;
420 static struct per_process_info *find_process_by_name(char *name)
422 const int hash_idx = ppi_hash_name(name);
423 struct per_process_info *ppi;
425 ppi = ppi_hash_table[hash_idx];
427 if (!strcmp(ppi->name, name))
430 ppi = ppi->hash_next;
436 static struct per_process_info *find_process_by_pid(__u32 pid)
438 const int hash_idx = ppi_hash_pid(pid);
439 struct per_process_info *ppi;
441 ppi = ppi_hash_table[hash_idx];
446 ppi = ppi->hash_next;
452 static struct per_process_info *find_process(__u32 pid, char *name)
454 struct per_process_info *ppi;
457 return find_process_by_pid(pid);
459 ppi = find_process_by_name(name);
460 if (ppi && ppi->pid != pid)
461 ppi->more_than_one = 1;
466 static inline int trace_rb_insert(struct trace *t, struct rb_root *root,
469 struct rb_node **p = &root->rb_node;
470 struct rb_node *parent = NULL;
476 __t = rb_entry(parent, struct trace, rb_node);
479 if (t->bit->time < __t->bit->time) {
482 } else if (t->bit->time > __t->bit->time) {
487 if (t->bit->device < __t->bit->device)
489 else if (t->bit->device > __t->bit->device)
491 else if (t->bit->sequence < __t->bit->sequence)
493 else /* >= sequence */
497 rb_link_node(&t->rb_node, parent, p);
498 rb_insert_color(&t->rb_node, root);
502 static inline int trace_rb_insert_sort(struct trace *t)
504 if (!trace_rb_insert(t, &rb_sort_root, 1)) {
512 static inline int trace_rb_insert_last(struct per_dev_info *pdi,struct trace *t)
514 if (!trace_rb_insert(t, &pdi->rb_last, 1)) {
515 pdi->rb_last_entries++;
522 static struct trace *trace_rb_find(dev_t device, unsigned long sequence,
523 struct rb_root *root, int order)
525 struct rb_node *n = root->rb_node;
526 struct rb_node *prev = NULL;
530 __t = rb_entry(n, struct trace, rb_node);
533 if (device < __t->bit->device)
535 else if (device > __t->bit->device)
537 else if (sequence < __t->bit->sequence)
539 else if (sequence > __t->bit->sequence)
546 * hack - the list may not be sequence ordered because some
547 * events don't have sequence and time matched. so we end up
548 * being a little off in the rb lookup here, because we don't
549 * know the time we are looking for. compensate by browsing
550 * a little ahead from the last entry to find the match
555 while (((n = rb_next(prev)) != NULL) && max--) {
556 __t = rb_entry(n, struct trace, rb_node);
558 if (__t->bit->device == device &&
559 __t->bit->sequence == sequence)
569 static inline struct trace *trace_rb_find_sort(dev_t dev, unsigned long seq)
571 return trace_rb_find(dev, seq, &rb_sort_root, 1);
574 static inline struct trace *trace_rb_find_last(struct per_dev_info *pdi,
577 return trace_rb_find(pdi->dev, seq, &pdi->rb_last, 0);
580 static inline int track_rb_insert(struct per_dev_info *pdi,struct io_track *iot)
582 struct rb_node **p = &pdi->rb_track.rb_node;
583 struct rb_node *parent = NULL;
584 struct io_track *__iot;
588 __iot = rb_entry(parent, struct io_track, rb_node);
590 if (iot->sector < __iot->sector)
592 else if (iot->sector > __iot->sector)
596 "sector alias (%Lu) on device %d,%d!\n",
597 (unsigned long long) iot->sector,
598 MAJOR(pdi->dev), MINOR(pdi->dev));
603 rb_link_node(&iot->rb_node, parent, p);
604 rb_insert_color(&iot->rb_node, &pdi->rb_track);
608 static struct io_track *__find_track(struct per_dev_info *pdi, __u64 sector)
610 struct rb_node *n = pdi->rb_track.rb_node;
611 struct io_track *__iot;
614 __iot = rb_entry(n, struct io_track, rb_node);
616 if (sector < __iot->sector)
618 else if (sector > __iot->sector)
627 static struct io_track *find_track(struct per_dev_info *pdi, __u32 pid,
628 char *comm, __u64 sector)
630 struct io_track *iot;
632 iot = __find_track(pdi, sector);
634 iot = malloc(sizeof(*iot));
636 memcpy(iot->comm, comm, sizeof(iot->comm));
637 iot->sector = sector;
638 track_rb_insert(pdi, iot);
644 static void log_track_frontmerge(struct per_dev_info *pdi,
645 struct blk_io_trace *t)
647 struct io_track *iot;
652 iot = __find_track(pdi, t->sector + t_sec(t));
655 fprintf(stderr, "merge not found for (%d,%d): %llu\n",
656 MAJOR(pdi->dev), MINOR(pdi->dev),
657 (unsigned long long) t->sector + t_sec(t));
661 rb_erase(&iot->rb_node, &pdi->rb_track);
662 iot->sector -= t_sec(t);
663 track_rb_insert(pdi, iot);
666 static void log_track_getrq(struct per_dev_info *pdi, struct blk_io_trace *t)
668 struct io_track *iot;
673 iot = find_track(pdi, t->pid, t->comm, t->sector);
674 iot->allocation_time = t->time;
678 * return time between rq allocation and insertion
680 static unsigned long long log_track_insert(struct per_dev_info *pdi,
681 struct blk_io_trace *t)
683 unsigned long long elapsed;
684 struct io_track *iot;
689 iot = find_track(pdi, t->pid, t->comm, t->sector);
690 iot->queue_time = t->time;
692 if (!iot->allocation_time)
695 elapsed = iot->queue_time - iot->allocation_time;
697 if (per_process_stats) {
698 struct per_process_info *ppi = find_process(iot->pid,iot->comm);
699 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
701 if (ppi && elapsed > ppi->longest_allocation_wait[w])
702 ppi->longest_allocation_wait[w] = elapsed;
709 * return time between queue and issue
711 static unsigned long long log_track_issue(struct per_dev_info *pdi,
712 struct blk_io_trace *t)
714 unsigned long long elapsed;
715 struct io_track *iot;
719 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
722 iot = __find_track(pdi, t->sector);
725 fprintf(stderr, "issue not found for (%d,%d): %llu\n",
726 MAJOR(pdi->dev), MINOR(pdi->dev),
727 (unsigned long long) t->sector);
731 iot->dispatch_time = t->time;
732 elapsed = iot->dispatch_time - iot->queue_time;
734 if (per_process_stats) {
735 struct per_process_info *ppi = find_process(iot->pid,iot->comm);
736 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
738 if (ppi && elapsed > ppi->longest_dispatch_wait[w])
739 ppi->longest_dispatch_wait[w] = elapsed;
746 * return time between dispatch and complete
748 static unsigned long long log_track_complete(struct per_dev_info *pdi,
749 struct blk_io_trace *t)
751 unsigned long long elapsed;
752 struct io_track *iot;
756 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
759 iot = __find_track(pdi, t->sector);
762 fprintf(stderr,"complete not found for (%d,%d): %llu\n",
763 MAJOR(pdi->dev), MINOR(pdi->dev),
764 (unsigned long long) t->sector);
768 iot->completion_time = t->time;
769 elapsed = iot->completion_time - iot->dispatch_time;
771 if (per_process_stats) {
772 struct per_process_info *ppi = find_process(iot->pid,iot->comm);
773 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
775 if (ppi && elapsed > ppi->longest_completion_wait[w])
776 ppi->longest_completion_wait[w] = elapsed;
780 * kill the trace, we don't need it after completion
782 rb_erase(&iot->rb_node, &pdi->rb_track);
789 static struct io_stats *find_process_io_stats(__u32 pid, char *name)
791 struct per_process_info *ppi = find_process(pid, name);
794 ppi = malloc(sizeof(*ppi));
795 memset(ppi, 0, sizeof(*ppi));
796 memcpy(ppi->name, name, 16);
798 add_process_to_hash(ppi);
799 add_process_to_list(ppi);
802 return &ppi->io_stats;
805 static void resize_cpu_info(struct per_dev_info *pdi, int cpu)
807 struct per_cpu_info *cpus = pdi->cpus;
808 int ncpus = pdi->ncpus;
809 int new_count = cpu + 1;
813 size = new_count * sizeof(struct per_cpu_info);
814 cpus = realloc(cpus, size);
817 fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n",
818 get_dev_name(pdi, name, sizeof(name)), size);
822 new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info));
823 new_space = (new_count - ncpus) * sizeof(struct per_cpu_info);
824 memset(new_start, 0, new_space);
826 pdi->ncpus = new_count;
830 static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu)
832 struct per_cpu_info *pci;
834 if (cpu >= pdi->ncpus)
835 resize_cpu_info(pdi, cpu);
837 pci = &pdi->cpus[cpu];
843 static int resize_devices(char *name)
845 int size = (ndevices + 1) * sizeof(struct per_dev_info);
847 devices = realloc(devices, size);
849 fprintf(stderr, "Out of memory, device %s (%d)\n", name, size);
852 memset(&devices[ndevices], 0, sizeof(struct per_dev_info));
853 devices[ndevices].name = name;
858 static struct per_dev_info *get_dev_info(dev_t dev)
860 struct per_dev_info *pdi;
863 for (i = 0; i < ndevices; i++) {
865 devices[i].dev = dev;
866 if (devices[i].dev == dev)
870 if (resize_devices(NULL))
873 pdi = &devices[ndevices - 1];
875 pdi->first_reported_time = 0;
876 pdi->last_sequence = -1;
877 pdi->last_read_time = 0;
878 memset(&pdi->rb_last, 0, sizeof(pdi->rb_last));
879 pdi->rb_last_entries = 0;
881 pdi->skips_head = pdi->skips_tail = NULL;
886 static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size)
889 snprintf(buffer, size, "%s", pdi->name);
891 snprintf(buffer, size, "%d,%d",MAJOR(pdi->dev),MINOR(pdi->dev));
895 static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit)
897 unsigned long long this = bit->time;
898 unsigned long long last = pdi->last_reported_time;
900 pdi->backwards = (this < last) ? 'B' : ' ';
901 pdi->last_reported_time = this;
904 static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t,
909 ios->qwrite_kb += t_kb(t);
912 ios->qread_kb += t_kb(t);
916 static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci,
919 __account_m(&pci->io_stats, t, rw);
921 if (per_process_stats) {
922 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
924 __account_m(ios, t, rw);
928 static inline void __account_queue(struct io_stats *ios, struct blk_io_trace *t,
933 ios->qwrite_kb += t_kb(t);
936 ios->qread_kb += t_kb(t);
940 static inline void account_queue(struct blk_io_trace *t,
941 struct per_cpu_info *pci, int rw)
943 __account_queue(&pci->io_stats, t, rw);
945 if (per_process_stats) {
946 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
948 __account_queue(ios, t, rw);
952 static inline void __account_c(struct io_stats *ios, int rw, int bytes)
956 ios->cwrite_kb += bytes >> 10;
959 ios->cread_kb += bytes >> 10;
963 static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci,
966 __account_c(&pci->io_stats, rw, bytes);
968 if (per_process_stats) {
969 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
971 __account_c(ios, rw, bytes);
975 static inline void __account_issue(struct io_stats *ios, int rw,
980 ios->iwrite_kb += bytes >> 10;
983 ios->iread_kb += bytes >> 10;
987 static inline void account_issue(struct blk_io_trace *t,
988 struct per_cpu_info *pci, int rw)
990 __account_issue(&pci->io_stats, rw, t->bytes);
992 if (per_process_stats) {
993 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
995 __account_issue(ios, rw, t->bytes);
999 static inline void __account_unplug(struct io_stats *ios, int timer)
1002 ios->timer_unplugs++;
1007 static inline void account_unplug(struct blk_io_trace *t,
1008 struct per_cpu_info *pci, int timer)
1010 __account_unplug(&pci->io_stats, timer);
1012 if (per_process_stats) {
1013 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
1015 __account_unplug(ios, timer);
1019 static inline void __account_requeue(struct io_stats *ios,
1020 struct blk_io_trace *t, int rw)
1024 ios->iwrite_kb -= t_kb(t);
1027 ios->iread_kb -= t_kb(t);
1031 static inline void account_requeue(struct blk_io_trace *t,
1032 struct per_cpu_info *pci, int rw)
1034 __account_requeue(&pci->io_stats, t, rw);
1036 if (per_process_stats) {
1037 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
1039 __account_requeue(ios, t, rw);
1043 static void log_complete(struct per_dev_info *pdi, struct per_cpu_info *pci,
1044 struct blk_io_trace *t, char *act)
1046 process_fmt(act, pci, t, log_track_complete(pdi, t), 0, NULL);
1049 static void log_insert(struct per_dev_info *pdi, struct per_cpu_info *pci,
1050 struct blk_io_trace *t, char *act)
1052 process_fmt(act, pci, t, log_track_insert(pdi, t), 0, NULL);
1055 static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t,
1058 process_fmt(act, pci, t, -1, 0, NULL);
1061 static void log_issue(struct per_dev_info *pdi, struct per_cpu_info *pci,
1062 struct blk_io_trace *t, char *act)
1064 process_fmt(act, pci, t, log_track_issue(pdi, t), 0, NULL);
1067 static void log_merge(struct per_dev_info *pdi, struct per_cpu_info *pci,
1068 struct blk_io_trace *t, char *act)
1071 log_track_frontmerge(pdi, t);
1073 process_fmt(act, pci, t, -1ULL, 0, NULL);
1076 static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t,
1079 process_fmt(act, pci, t, -1ULL, 0, NULL);
1082 static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t,
1085 process_fmt(act, pci, t, -1ULL, 0, NULL);
1088 static void log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t,
1091 process_fmt(act, pci, t, -1ULL, 0, NULL);
1094 static void log_split(struct per_cpu_info *pci, struct blk_io_trace *t,
1097 process_fmt(act, pci, t, -1ULL, 0, NULL);
1100 static void log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act)
1102 unsigned char *buf = (unsigned char *) t + sizeof(*t);
1104 process_fmt(act, pci, t, -1ULL, t->pdu_len, buf);
1107 static void dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci)
1109 int act = t->action & 0xffff;
1112 case __BLK_TA_QUEUE:
1113 log_generic(pci, t, "Q");
1115 case __BLK_TA_GETRQ:
1116 log_generic(pci, t, "G");
1118 case __BLK_TA_SLEEPRQ:
1119 log_generic(pci, t, "S");
1121 case __BLK_TA_REQUEUE:
1122 log_generic(pci, t, "R");
1124 case __BLK_TA_ISSUE:
1125 log_pc(pci, t, "D");
1127 case __BLK_TA_COMPLETE:
1128 log_pc(pci, t, "C");
1130 case __BLK_TA_INSERT:
1131 log_pc(pci, t, "I");
1134 fprintf(stderr, "Bad pc action %x\n", act);
1139 static void dump_trace_fs(struct blk_io_trace *t, struct per_dev_info *pdi,
1140 struct per_cpu_info *pci)
1142 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
1143 int act = t->action & 0xffff;
1146 case __BLK_TA_QUEUE:
1147 account_queue(t, pci, w);
1148 log_queue(pci, t, "Q");
1150 case __BLK_TA_INSERT:
1151 log_insert(pdi, pci, t, "I");
1153 case __BLK_TA_BACKMERGE:
1154 account_m(t, pci, w);
1155 log_merge(pdi, pci, t, "M");
1157 case __BLK_TA_FRONTMERGE:
1158 account_m(t, pci, w);
1159 log_merge(pdi, pci, t, "F");
1161 case __BLK_TA_GETRQ:
1162 log_track_getrq(pdi, t);
1163 log_generic(pci, t, "G");
1165 case __BLK_TA_SLEEPRQ:
1166 log_generic(pci, t, "S");
1168 case __BLK_TA_REQUEUE:
1169 account_requeue(t, pci, w);
1170 log_queue(pci, t, "R");
1172 case __BLK_TA_ISSUE:
1173 account_issue(t, pci, w);
1174 log_issue(pdi, pci, t, "D");
1176 case __BLK_TA_COMPLETE:
1177 account_c(t, pci, w, t->bytes);
1178 log_complete(pdi, pci, t, "C");
1181 log_action(pci, t, "P");
1183 case __BLK_TA_UNPLUG_IO:
1184 account_unplug(t, pci, 0);
1185 log_unplug(pci, t, "U");
1187 case __BLK_TA_UNPLUG_TIMER:
1188 account_unplug(t, pci, 1);
1189 log_unplug(pci, t, "UT");
1191 case __BLK_TA_SPLIT:
1192 log_split(pci, t, "X");
1194 case __BLK_TA_BOUNCE:
1195 log_generic(pci, t, "B");
1197 case __BLK_TA_REMAP:
1198 log_generic(pci, t, "A");
1201 fprintf(stderr, "Bad fs action %x\n", t->action);
1206 static void dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
1207 struct per_dev_info *pdi)
1209 if (t->action & BLK_TC_ACT(BLK_TC_PC))
1210 dump_trace_pc(t, pci);
1212 dump_trace_fs(t, pdi, pci);
1215 pdi->first_reported_time = t->time;
1221 * print in a proper way, not too small and not too big. if more than
1222 * 1000,000K, turn into M and so on
1224 static char *size_cnv(char *dst, unsigned long long num, int in_kb)
1226 char suff[] = { '\0', 'K', 'M', 'G', 'P' };
1232 while (num > 1000 * 1000ULL && (i < sizeof(suff) - 1)) {
1237 sprintf(dst, "%'8Lu%c", num, suff[i]);
1241 static void dump_io_stats(struct io_stats *ios, char *msg)
1243 static char x[256], y[256];
1245 fprintf(ofp, "%s\n", msg);
1247 fprintf(ofp, " Reads Queued: %s, %siB\t", size_cnv(x, ios->qreads, 0), size_cnv(y, ios->qread_kb, 1));
1248 fprintf(ofp, " Writes Queued: %s, %siB\n", size_cnv(x, ios->qwrites, 0), size_cnv(y, ios->qwrite_kb, 1));
1250 fprintf(ofp, " Read Dispatches: %s, %siB\t", size_cnv(x, ios->ireads, 0), size_cnv(y, ios->iread_kb, 1));
1251 fprintf(ofp, " Write Dispatches: %s, %siB\n", size_cnv(x, ios->iwrites, 0), size_cnv(y, ios->iwrite_kb, 1));
1252 fprintf(ofp, " Reads Requeued: %s\t\t", size_cnv(x, ios->rrqueue, 0));
1253 fprintf(ofp, " Writes Requeued: %s\n", size_cnv(x, ios->wrqueue, 0));
1254 fprintf(ofp, " Reads Completed: %s, %siB\t", size_cnv(x, ios->creads, 0), size_cnv(y, ios->cread_kb, 1));
1255 fprintf(ofp, " Writes Completed: %s, %siB\n", size_cnv(x, ios->cwrites, 0), size_cnv(y, ios->cwrite_kb, 1));
1256 fprintf(ofp, " Read Merges: %'8lu%8c\t", ios->mreads, ' ');
1257 fprintf(ofp, " Write Merges: %'8lu\n", ios->mwrites);
1258 fprintf(ofp, " IO unplugs: %'8lu%8c\t", ios->io_unplugs, ' ');
1259 fprintf(ofp, " Timer unplugs: %'8lu\n", ios->timer_unplugs);
1262 static void dump_wait_stats(struct per_process_info *ppi)
1264 unsigned long rawait = ppi->longest_allocation_wait[0] / 1000;
1265 unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000;
1266 unsigned long rcwait = ppi->longest_completion_wait[0] / 1000;
1267 unsigned long wawait = ppi->longest_allocation_wait[1] / 1000;
1268 unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000;
1269 unsigned long wcwait = ppi->longest_completion_wait[1] / 1000;
1271 fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' ');
1272 fprintf(ofp, " Allocation wait: %'8lu\n", wawait);
1273 fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' ');
1274 fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait);
1275 fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' ');
1276 fprintf(ofp, " Completion wait: %'8lu\n", wcwait);
1279 static int ppi_name_compare(const void *p1, const void *p2)
1281 struct per_process_info *ppi1 = *((struct per_process_info **) p1);
1282 struct per_process_info *ppi2 = *((struct per_process_info **) p2);
1285 res = strverscmp(ppi1->name, ppi2->name);
1287 res = ppi1->pid > ppi2->pid;
1292 static void sort_process_list(void)
1294 struct per_process_info **ppis;
1295 struct per_process_info *ppi;
1298 ppis = malloc(ppi_list_entries * sizeof(struct per_process_info *));
1303 ppi = ppi->list_next;
1306 qsort(ppis, ppi_list_entries, sizeof(ppi), ppi_name_compare);
1308 i = ppi_list_entries - 1;
1313 ppi->list_next = ppi_list;
1321 static void show_process_stats(void)
1323 struct per_process_info *ppi;
1325 sort_process_list();
1331 if (ppi->more_than_one)
1332 sprintf(name, "%s (%u, ...)", ppi->name, ppi->pid);
1334 sprintf(name, "%s (%u)", ppi->name, ppi->pid);
1336 dump_io_stats(&ppi->io_stats, name);
1337 dump_wait_stats(ppi);
1338 ppi = ppi->list_next;
1344 static void show_device_and_cpu_stats(void)
1346 struct per_dev_info *pdi;
1347 struct per_cpu_info *pci;
1348 struct io_stats total, *ios;
1349 unsigned long long rrate, wrate, msec;
1350 int i, j, pci_events;
1351 char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3];
1354 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
1356 memset(&total, 0, sizeof(total));
1362 for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) {
1366 ios = &pci->io_stats;
1367 total.qreads += ios->qreads;
1368 total.qwrites += ios->qwrites;
1369 total.creads += ios->creads;
1370 total.cwrites += ios->cwrites;
1371 total.mreads += ios->mreads;
1372 total.mwrites += ios->mwrites;
1373 total.ireads += ios->ireads;
1374 total.iwrites += ios->iwrites;
1375 total.rrqueue += ios->rrqueue;
1376 total.wrqueue += ios->wrqueue;
1377 total.qread_kb += ios->qread_kb;
1378 total.qwrite_kb += ios->qwrite_kb;
1379 total.cread_kb += ios->cread_kb;
1380 total.cwrite_kb += ios->cwrite_kb;
1381 total.iread_kb += ios->iread_kb;
1382 total.iwrite_kb += ios->iwrite_kb;
1383 total.timer_unplugs += ios->timer_unplugs;
1384 total.io_unplugs += ios->io_unplugs;
1386 snprintf(line, sizeof(line) - 1, "CPU%d (%s):",
1387 j, get_dev_name(pdi, name, sizeof(name)));
1388 dump_io_stats(ios, line);
1392 if (pci_events > 1) {
1394 snprintf(line, sizeof(line) - 1, "Total (%s):",
1395 get_dev_name(pdi, name, sizeof(name)));
1396 dump_io_stats(&total, line);
1400 msec = (pdi->last_reported_time - pdi->first_reported_time) / 1000000;
1402 rrate = 1000 * total.cread_kb / msec;
1403 wrate = 1000 * total.cwrite_kb / msec;
1406 fprintf(ofp, "\nThroughput (R/W): %'LuKiB/s / %'LuKiB/s\n",
1408 fprintf(ofp, "Events (%s): %'Lu entries\n",
1409 get_dev_name(pdi, line, sizeof(line)), pdi->events);
1411 collect_pdi_skips(pdi);
1412 fprintf(ofp, "Skips: %'lu forward (%'llu - %5.1lf%%)\n",
1413 pdi->skips,pdi->seq_skips,
1414 100.0 * ((double)pdi->seq_skips /
1415 (double)(pdi->events + pdi->seq_skips)));
1420 * struct trace and blktrace allocation cache, we do potentially
1421 * millions of mallocs for these structures while only using at most
1422 * a few thousand at the time
1424 static inline void t_free(struct trace *t)
1426 if (t_alloc_cache < 1024) {
1427 t->next = t_alloc_list;
1434 static inline struct trace *t_alloc(void)
1436 struct trace *t = t_alloc_list;
1439 t_alloc_list = t->next;
1444 return malloc(sizeof(*t));
1447 static inline void bit_free(struct blk_io_trace *bit)
1449 if (bit_alloc_cache < 1024 && !bit->pdu_len) {
1451 * abuse a 64-bit field for a next pointer for the free item
1453 bit->time = (__u64) (unsigned long) bit_alloc_list;
1454 bit_alloc_list = (struct blk_io_trace *) bit;
1460 static inline struct blk_io_trace *bit_alloc(void)
1462 struct blk_io_trace *bit = bit_alloc_list;
1465 bit_alloc_list = (struct blk_io_trace *) (unsigned long) \
1471 return malloc(sizeof(*bit));
1474 static void find_genesis(void)
1476 struct trace *t = trace_list;
1478 genesis_time = -1ULL;
1480 if (t->bit->time < genesis_time)
1481 genesis_time = t->bit->time;
1487 static inline int check_stopwatch(struct blk_io_trace *bit)
1489 if (bit->time < stopwatch_end &&
1490 bit->time >= stopwatch_start)
1497 * return youngest entry read
1499 static int sort_entries(unsigned long long *youngest)
1507 while ((t = trace_list) != NULL) {
1508 struct blk_io_trace *bit = t->bit;
1510 trace_list = t->next;
1512 bit->time -= genesis_time;
1514 if (bit->time < *youngest || !*youngest)
1515 *youngest = bit->time;
1517 if (bit->sequence < smallest_seq_read)
1518 smallest_seq_read = bit->sequence;
1520 if (check_stopwatch(bit)) {
1526 if (trace_rb_insert_sort(t))
1533 static inline void __put_trace_last(struct per_dev_info *pdi, struct trace *t)
1535 rb_erase(&t->rb_node, &pdi->rb_last);
1536 pdi->rb_last_entries--;
1542 static void put_trace(struct per_dev_info *pdi, struct trace *t)
1544 rb_erase(&t->rb_node, &rb_sort_root);
1547 trace_rb_insert_last(pdi, t);
1549 if (pdi->rb_last_entries > rb_batch * pdi->nfiles) {
1550 struct rb_node *n = rb_first(&pdi->rb_last);
1552 t = rb_entry(n, struct trace, rb_node);
1553 __put_trace_last(pdi, t);
1558 * to continue, we must have traces from all online cpus in the tree
1560 static int check_cpu_map(struct per_dev_info *pdi)
1562 unsigned long *cpu_map;
1569 * create a map of the cpus we have traces for
1571 cpu_map = malloc(pdi->cpu_map_max / sizeof(long));
1572 n = rb_first(&rb_sort_root);
1574 __t = rb_entry(n, struct trace, rb_node);
1575 cpu = __t->bit->cpu;
1577 cpu_map[CPU_IDX(cpu)] |= (1UL << CPU_BIT(cpu));
1582 * we can't continue if pdi->cpu_map has entries set that we don't
1583 * have in the sort rbtree. the opposite is not a problem, though
1586 for (i = 0; i < pdi->cpu_map_max / CPUS_PER_LONG; i++) {
1587 if (pdi->cpu_map[i] & ~(cpu_map[i])) {
1597 static int check_sequence(struct per_dev_info *pdi, struct trace *t, int force)
1599 unsigned long expected_sequence = pdi->last_sequence + 1;
1600 struct blk_io_trace *bit = t->bit;
1603 if (!expected_sequence) {
1605 * 1 should be the first entry, just allow it
1607 if (bit->sequence == 1)
1610 return check_cpu_map(pdi);
1613 if (bit->sequence == expected_sequence)
1617 * we may not have seen that sequence yet. if we are not doing
1618 * the final run, break and wait for more entries.
1620 if (expected_sequence < smallest_seq_read) {
1621 __t = trace_rb_find_last(pdi, expected_sequence);
1625 __put_trace_last(pdi, __t);
1627 } else if (!force) {
1631 if (check_current_skips(pdi,bit->sequence))
1634 insert_skip(pdi,expected_sequence,bit->sequence-1);
1639 static void show_entries_rb(int force)
1641 struct per_dev_info *pdi = NULL;
1642 struct per_cpu_info *pci = NULL;
1643 struct blk_io_trace *bit;
1647 while ((n = rb_first(&rb_sort_root)) != NULL) {
1648 if (is_done() && !force && !pipeline)
1651 t = rb_entry(n, struct trace, rb_node);
1654 if (!pdi || pdi->dev != bit->device)
1655 pdi = get_dev_info(bit->device);
1658 fprintf(stderr, "Unknown device ID? (%d,%d)\n",
1659 MAJOR(bit->device), MINOR(bit->device));
1663 if (check_sequence(pdi, t, force))
1666 if (!force && bit->time > last_allowed_time)
1669 pdi->last_sequence = bit->sequence;
1671 check_time(pdi, bit);
1673 if (!pci || pci->cpu != bit->cpu)
1674 pci = get_cpu_info(pdi, bit->cpu);
1678 if (bit->action & (act_mask << BLK_TC_SHIFT))
1679 dump_trace(bit, pci, pdi);
1685 static int read_data(int fd, void *buffer, int bytes, int block)
1687 int ret, bytes_left, fl;
1690 fl = fcntl(fd, F_GETFL);
1693 fcntl(fd, F_SETFL, fl | O_NONBLOCK);
1695 fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
1699 while (bytes_left > 0) {
1700 ret = read(fd, p, bytes_left);
1704 if (errno != EAGAIN)
1717 static int read_events(int fd, int always_block)
1719 struct per_dev_info *pdi = NULL;
1720 unsigned int events = 0;
1722 while (!is_done() && events < rb_batch) {
1723 struct blk_io_trace *bit;
1730 if (read_data(fd, bit, sizeof(*bit), !events || always_block)) {
1735 magic = be32_to_cpu(bit->magic);
1736 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
1737 fprintf(stderr, "Bad magic %x\n", magic);
1741 pdu_len = be16_to_cpu(bit->pdu_len);
1743 void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
1745 if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1)) {
1755 if (verify_trace(bit)) {
1761 memset(t, 0, sizeof(*t));
1764 t->next = trace_list;
1767 if (!pdi || pdi->dev != bit->device)
1768 pdi = get_dev_info(bit->device);
1770 if (bit->time > pdi->last_read_time)
1771 pdi->last_read_time = bit->time;
1779 static int do_file(void)
1781 struct per_cpu_info *pci;
1782 struct per_dev_info *pdi;
1783 int i, j, events, events_added;
1786 * first prepare all files for reading
1788 for (i = 0; i < ndevices; i++) {
1791 pdi->last_sequence = -1;
1798 pci = get_cpu_info(pdi, j);
1802 p = strdup(pdi->name);
1804 if (strcmp(dname, ".")) {
1806 p = strdup(pdi->name);
1807 strcpy(pdi->name, basename(p));
1812 len = sprintf(pci->fname, "%s/", input_dir);
1814 snprintf(pci->fname + len, sizeof(pci->fname)-1-len,
1815 "%s.blktrace.%d", pdi->name, pci->cpu);
1816 if (stat(pci->fname, &st) < 0)
1819 pci->fd = open(pci->fname, O_RDONLY);
1826 printf("Input file %s added\n", pci->fname);
1828 cpu_mark_online(pdi, pci->cpu);
1833 * now loop over the files reading in the data
1836 unsigned long long youngest;
1839 last_allowed_time = -1ULL;
1840 smallest_seq_read = -1U;
1842 for (i = 0; i < ndevices; i++) {
1845 for (j = 0; j < pdi->nfiles; j++) {
1847 pci = get_cpu_info(pdi, j);
1852 events = read_events(pci->fd, 1);
1854 cpu_mark_offline(pdi, pci->cpu);
1860 if (pdi->last_read_time < last_allowed_time)
1861 last_allowed_time = pdi->last_read_time;
1863 events_added += events;
1867 if (sort_entries(&youngest))
1870 if (youngest > stopwatch_end)
1875 } while (events_added);
1877 if (rb_sort_entries)
1883 static int do_stdin(void)
1885 unsigned long long youngest;
1888 last_allowed_time = -1ULL;
1889 fd = dup(STDIN_FILENO);
1891 perror("dup stdin");
1895 while ((events = read_events(fd, 0)) != 0) {
1897 smallest_seq_read = -1U;
1899 if (sort_entries(&youngest))
1902 if (youngest > stopwatch_end)
1908 if (rb_sort_entries)
1915 static void show_stats(void)
1924 if (per_process_stats)
1925 show_process_stats();
1927 if (per_device_and_cpu_stats)
1928 show_device_and_cpu_stats();
1933 static void handle_sigint(__attribute__((__unused__)) int sig)
1940 * Extract start and duration times from a string, allowing
1941 * us to specify a time interval of interest within a trace.
1942 * Format: "duration" (start is zero) or "start:duration".
1944 static int find_stopwatch_interval(char *string)
1949 value = strtod(string, &sp);
1951 fprintf(stderr,"Invalid stopwatch timer: %s\n", string);
1955 stopwatch_start = DOUBLE_TO_NANO_ULL(value);
1957 value = strtod(string, &sp);
1958 if (sp == string || *sp != '\0') {
1959 fprintf(stderr,"Invalid stopwatch duration time: %s\n",
1963 } else if (*sp != '\0') {
1964 fprintf(stderr,"Invalid stopwatch start timer: %s\n", string);
1967 stopwatch_end = DOUBLE_TO_NANO_ULL(value);
1968 if (stopwatch_end <= stopwatch_start) {
1969 fprintf(stderr, "Invalid stopwatch interval: %Lu -> %Lu\n",
1970 stopwatch_start, stopwatch_end);
1977 static char usage_str[] = \
1978 "[ -i <input name> ] [-o <output name> [ -s ] [ -t ] [ -q ]\n" \
1979 "[ -w start:stop ] [ -f output format ] [ -F format spec ] [ -v] \n\n" \
1980 "\t-i Input file containing trace data, or '-' for stdin\n" \
1981 "\t-D Directory to prepend to input file names\n" \
1982 "\t-o Output file. If not given, output is stdout\n" \
1983 "\t-b stdin read batching\n" \
1984 "\t-s Show per-program io statistics\n" \
1985 "\t-h Hash processes by name, not pid\n" \
1986 "\t-t Track individual ios. Will tell you the time a request took\n" \
1987 "\t to get queued, to get dispatched, and to get completed\n" \
1988 "\t-q Quiet. Don't display any stats at the end of the trace\n" \
1989 "\t-w Only parse data between the given time interval in seconds.\n" \
1990 "\t If 'start' isn't given, blkparse defaults the start time to 0\n" \
1991 "\t-f Output format. Customize the output format. The format field\n" \
1992 "\t identifies can be found in the documentation\n" \
1993 "\t-F Format specification. Can be found in the documentation\n" \
1994 "\t-v More verbose for marginal errors\n" \
1995 "\t-V Print program version info\n\n";
1997 static void usage(char *prog)
1999 fprintf(stderr, "Usage: %s %s %s", prog, blkparse_version, usage_str);
2002 int main(int argc, char *argv[])
2005 int i, c, ret, mode;
2006 int act_mask_tmp = 0;
2008 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
2011 i = find_mask_map(optarg);
2013 fprintf(stderr,"Invalid action mask %s\n",
2021 if ((sscanf(optarg, "%x", &i) != 1) ||
2022 !valid_act_opt(i)) {
2024 "Invalid set action mask %s/0x%x\n",
2031 if (!strcmp(optarg, "-") && !pipeline)
2033 else if (resize_devices(optarg) != 0)
2040 output_name = optarg;
2043 rb_batch = atoi(optarg);
2045 rb_batch = RB_BATCH_DEFAULT;
2048 per_process_stats = 1;
2054 per_device_and_cpu_stats = 0;
2057 if (find_stopwatch_interval(optarg) != 0)
2061 set_all_format_specs(optarg);
2064 if (add_format_spec(optarg) != 0)
2068 ppi_hash_by_pid = 0;
2074 printf("%s version %s\n", argv[0], blkparse_version);
2082 while (optind < argc) {
2083 if (!strcmp(argv[optind], "-") && !pipeline)
2085 else if (resize_devices(argv[optind]) != 0)
2090 if (!pipeline && !ndevices) {
2095 if (act_mask_tmp != 0)
2096 act_mask = act_mask_tmp;
2098 memset(&rb_sort_root, 0, sizeof(rb_sort_root));
2100 signal(SIGINT, handle_sigint);
2101 signal(SIGHUP, handle_sigint);
2102 signal(SIGTERM, handle_sigint);
2104 setlocale(LC_NUMERIC, "en_US");
2107 ofp = fdopen(STDOUT_FILENO, "w");
2112 snprintf(ofname, sizeof(ofname) - 1, "%s", output_name);
2113 ofp = fopen(ofname, "w");
2122 ofp_buffer = malloc(4096);
2123 if (setvbuf(ofp, ofp_buffer, mode, 4096)) {