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;
58 unsigned int max_depth[2];
59 unsigned int cur_depth[2];
61 struct rb_root rb_last;
62 unsigned long rb_last_entries;
64 struct rb_root rb_track;
69 unsigned long *cpu_map;
70 unsigned int cpu_map_max;
72 struct per_cpu_info *cpus;
73 struct skip_info *skips_head;
74 struct skip_info *skips_tail;
77 struct per_process_info {
80 struct io_stats io_stats;
81 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 #define PPI_HASH_SIZE (1 << PPI_HASH_SHIFT)
94 #define PPI_HASH_MASK (PPI_HASH_SIZE - 1)
95 static struct per_process_info *ppi_hash_table[PPI_HASH_SIZE];
96 static struct per_process_info *ppi_list;
97 static int ppi_list_entries;
99 #define S_OPTS "a:A:i:o:b:stqw:f:F:vVhD:"
100 static struct option l_opts[] = {
103 .has_arg = required_argument,
109 .has_arg = required_argument,
115 .has_arg = required_argument,
121 .has_arg = required_argument,
127 .has_arg = required_argument,
132 .name = "per-program-stats",
133 .has_arg = no_argument,
139 .has_arg = no_argument,
145 .has_arg = no_argument,
151 .has_arg = required_argument,
157 .has_arg = required_argument,
162 .name = "format-spec",
163 .has_arg = required_argument,
168 .name = "hash-by-name",
169 .has_arg = no_argument,
175 .has_arg = no_argument,
181 .has_arg = no_argument,
186 .name = "input-directory",
187 .has_arg = required_argument,
197 * for sorting the displayed output
200 struct blk_io_trace *bit;
201 struct rb_node rb_node;
205 static struct rb_root rb_sort_root;
206 static unsigned long rb_sort_entries;
208 static struct trace *trace_list;
213 static struct blk_io_trace *bit_alloc_list;
214 static struct trace *t_alloc_list;
217 * for tracking individual ios
220 struct rb_node rb_node;
225 unsigned long long allocation_time;
226 unsigned long long queue_time;
227 unsigned long long dispatch_time;
228 unsigned long long completion_time;
232 static struct per_dev_info *devices;
233 static char *get_dev_name(struct per_dev_info *, char *, int);
236 static char *output_name;
237 static char *input_dir;
239 static unsigned long long genesis_time;
240 static unsigned long long last_allowed_time;
241 static unsigned int smallest_seq_read;
242 static unsigned long long stopwatch_start; /* start from zero by default */
243 static unsigned long long stopwatch_end = -1ULL; /* "infinity" */
245 static int per_process_stats;
246 static int per_device_and_cpu_stats = 1;
247 static int track_ios;
248 static int ppi_hash_by_pid = 1;
250 static unsigned int act_mask = -1U;
251 static int stats_printed;
253 static unsigned int t_alloc_cache;
254 static unsigned int bit_alloc_cache;
256 #define RB_BATCH_DEFAULT (512)
257 static unsigned int rb_batch = RB_BATCH_DEFAULT;
261 #define is_done() (*(volatile int *)(&done))
262 static volatile int done;
264 #define JHASH_RANDOM (0x3af5f2ee)
266 #define CPUS_PER_LONG (8 * sizeof(unsigned long))
267 #define CPU_IDX(cpu) ((cpu) / CPUS_PER_LONG)
268 #define CPU_BIT(cpu) ((cpu) & (CPUS_PER_LONG - 1))
270 static void insert_skip(struct per_dev_info *pdi, unsigned long start,
273 struct skip_info *sip;
275 for (sip = pdi->skips_tail; sip != NULL; sip = sip->prev) {
276 if (end == (sip->start - 1)) {
279 } else if (start == (sip->end + 1)) {
285 sip = malloc(sizeof(struct skip_info));
288 sip->prev = sip->next = NULL;
289 if (pdi->skips_tail == NULL)
290 pdi->skips_head = pdi->skips_tail = sip;
292 sip->prev = pdi->skips_tail;
293 pdi->skips_tail->next = sip;
294 pdi->skips_tail = sip;
298 static void remove_sip(struct per_dev_info *pdi, struct skip_info *sip)
300 if (sip->prev == NULL) {
301 if (sip->next == NULL)
302 pdi->skips_head = pdi->skips_tail = NULL;
304 pdi->skips_head = sip->next;
305 sip->next->prev = NULL;
307 } else if (sip->next == NULL) {
308 pdi->skips_tail = sip->prev;
309 sip->prev->next = NULL;
311 sip->prev->next = sip->next;
312 sip->next->prev = sip->prev;
315 sip->prev = sip->next = NULL;
319 #define IN_SKIP(sip,seq) (((sip)->start <= (seq)) && ((seq) <= sip->end))
320 static int check_current_skips(struct per_dev_info *pdi, unsigned long seq)
322 struct skip_info *sip;
324 for (sip = pdi->skips_tail; sip != NULL; sip = sip->prev) {
325 if (IN_SKIP(sip,seq)) {
326 if (sip->start == seq) {
331 } else if (sip->end == seq)
335 insert_skip(pdi,seq+1,sip->end);
343 static void collect_pdi_skips(struct per_dev_info *pdi)
345 struct skip_info *sip;
349 for (sip = pdi->skips_head; sip != NULL; sip = sip->next) {
351 pdi->seq_skips += (sip->end - sip->start + 1);
353 fprintf(stderr, "(%d,%d): skipping %lu -> %lu\n",
354 MAJOR(pdi->dev), MINOR(pdi->dev),
355 sip->start, sip->end);
359 static void cpu_mark_online(struct per_dev_info *pdi, unsigned int cpu)
361 if (cpu >= pdi->cpu_map_max || !pdi->cpu_map) {
362 int new_max = (cpu + CPUS_PER_LONG) & ~(CPUS_PER_LONG - 1);
363 unsigned long *map = malloc(new_max / sizeof(long));
365 memset(map, 0, new_max / sizeof(long));
368 memcpy(map, pdi->cpu_map, pdi->cpu_map_max / sizeof(long));
373 pdi->cpu_map_max = new_max;
376 pdi->cpu_map[CPU_IDX(cpu)] |= (1UL << CPU_BIT(cpu));
379 static inline void cpu_mark_offline(struct per_dev_info *pdi, int cpu)
381 pdi->cpu_map[CPU_IDX(cpu)] &= ~(1UL << CPU_BIT(cpu));
384 static inline int cpu_is_online(struct per_dev_info *pdi, int cpu)
386 return (pdi->cpu_map[CPU_IDX(cpu)] & (1UL << CPU_BIT(cpu))) != 0;
389 static inline int ppi_hash_pid(__u32 pid)
391 return jhash_1word(pid, JHASH_RANDOM) & PPI_HASH_MASK;
394 static inline int ppi_hash_name(const char *name)
396 return jhash(name, 16, JHASH_RANDOM) & PPI_HASH_MASK;
399 static inline int ppi_hash(struct per_process_info *ppi)
402 return ppi_hash_pid(ppi->pid);
404 return ppi_hash_name(ppi->name);
407 static inline void add_process_to_hash(struct per_process_info *ppi)
409 const int hash_idx = ppi_hash(ppi);
411 ppi->hash_next = ppi_hash_table[hash_idx];
412 ppi_hash_table[hash_idx] = ppi;
415 static inline void add_process_to_list(struct per_process_info *ppi)
417 ppi->list_next = ppi_list;
422 static struct per_process_info *find_process_by_name(char *name)
424 const int hash_idx = ppi_hash_name(name);
425 struct per_process_info *ppi;
427 ppi = ppi_hash_table[hash_idx];
429 if (!strcmp(ppi->name, name))
432 ppi = ppi->hash_next;
438 static struct per_process_info *find_process_by_pid(__u32 pid)
440 const int hash_idx = ppi_hash_pid(pid);
441 struct per_process_info *ppi;
443 ppi = ppi_hash_table[hash_idx];
448 ppi = ppi->hash_next;
454 static struct per_process_info *find_process(__u32 pid, char *name)
456 struct per_process_info *ppi;
459 return find_process_by_pid(pid);
461 ppi = find_process_by_name(name);
462 if (ppi && ppi->pid != pid)
463 ppi->more_than_one = 1;
468 static inline int trace_rb_insert(struct trace *t, struct rb_root *root)
470 struct rb_node **p = &root->rb_node;
471 struct rb_node *parent = NULL;
477 __t = rb_entry(parent, struct trace, rb_node);
479 if (t->bit->time < __t->bit->time)
481 else if (t->bit->time > __t->bit->time)
483 else if (t->bit->device < __t->bit->device)
485 else if (t->bit->device > __t->bit->device)
487 else if (t->bit->sequence < __t->bit->sequence)
489 else /* >= sequence */
493 rb_link_node(&t->rb_node, parent, p);
494 rb_insert_color(&t->rb_node, root);
498 static inline int trace_rb_insert_sort(struct trace *t)
500 if (!trace_rb_insert(t, &rb_sort_root)) {
508 static inline int trace_rb_insert_last(struct per_dev_info *pdi,struct trace *t)
510 if (!trace_rb_insert(t, &pdi->rb_last)) {
511 pdi->rb_last_entries++;
518 static struct trace *trace_rb_find(dev_t device, unsigned long sequence,
519 struct rb_root *root, int order)
521 struct rb_node *n = root->rb_node;
522 struct rb_node *prev = NULL;
526 __t = rb_entry(n, struct trace, rb_node);
529 if (device < __t->bit->device)
531 else if (device > __t->bit->device)
533 else if (sequence < __t->bit->sequence)
535 else if (sequence > __t->bit->sequence)
542 * hack - the list may not be sequence ordered because some
543 * events don't have sequence and time matched. so we end up
544 * being a little off in the rb lookup here, because we don't
545 * know the time we are looking for. compensate by browsing
546 * a little ahead from the last entry to find the match
551 while (((n = rb_next(prev)) != NULL) && max--) {
552 __t = rb_entry(n, struct trace, rb_node);
554 if (__t->bit->device == device &&
555 __t->bit->sequence == sequence)
565 static inline struct trace *trace_rb_find_sort(dev_t dev, unsigned long seq)
567 return trace_rb_find(dev, seq, &rb_sort_root, 1);
570 static inline struct trace *trace_rb_find_last(struct per_dev_info *pdi,
573 return trace_rb_find(pdi->dev, seq, &pdi->rb_last, 0);
576 static inline int track_rb_insert(struct per_dev_info *pdi,struct io_track *iot)
578 struct rb_node **p = &pdi->rb_track.rb_node;
579 struct rb_node *parent = NULL;
580 struct io_track *__iot;
584 __iot = rb_entry(parent, struct io_track, rb_node);
586 if (iot->sector < __iot->sector)
588 else if (iot->sector > __iot->sector)
592 "sector alias (%Lu) on device %d,%d!\n",
593 (unsigned long long) iot->sector,
594 MAJOR(pdi->dev), MINOR(pdi->dev));
599 rb_link_node(&iot->rb_node, parent, p);
600 rb_insert_color(&iot->rb_node, &pdi->rb_track);
604 static struct io_track *__find_track(struct per_dev_info *pdi, __u64 sector)
606 struct rb_node *n = pdi->rb_track.rb_node;
607 struct io_track *__iot;
610 __iot = rb_entry(n, struct io_track, rb_node);
612 if (sector < __iot->sector)
614 else if (sector > __iot->sector)
623 static struct io_track *find_track(struct per_dev_info *pdi, __u32 pid,
624 char *comm, __u64 sector)
626 struct io_track *iot;
628 iot = __find_track(pdi, sector);
630 iot = malloc(sizeof(*iot));
632 memcpy(iot->comm, comm, sizeof(iot->comm));
633 iot->sector = sector;
634 track_rb_insert(pdi, iot);
640 static void log_track_frontmerge(struct per_dev_info *pdi,
641 struct blk_io_trace *t)
643 struct io_track *iot;
648 iot = __find_track(pdi, t->sector + t_sec(t));
651 fprintf(stderr, "merge not found for (%d,%d): %llu\n",
652 MAJOR(pdi->dev), MINOR(pdi->dev),
653 (unsigned long long) t->sector + t_sec(t));
657 rb_erase(&iot->rb_node, &pdi->rb_track);
658 iot->sector -= t_sec(t);
659 track_rb_insert(pdi, iot);
662 static void log_track_getrq(struct per_dev_info *pdi, struct blk_io_trace *t)
664 struct io_track *iot;
669 iot = find_track(pdi, t->pid, t->comm, t->sector);
670 iot->allocation_time = t->time;
674 * return time between rq allocation and insertion
676 static unsigned long long log_track_insert(struct per_dev_info *pdi,
677 struct blk_io_trace *t)
679 unsigned long long elapsed;
680 struct io_track *iot;
685 iot = find_track(pdi, t->pid, t->comm, t->sector);
686 iot->queue_time = t->time;
688 if (!iot->allocation_time)
691 elapsed = iot->queue_time - iot->allocation_time;
693 if (per_process_stats) {
694 struct per_process_info *ppi = find_process(iot->pid,iot->comm);
695 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
697 if (ppi && elapsed > ppi->longest_allocation_wait[w])
698 ppi->longest_allocation_wait[w] = elapsed;
705 * return time between queue and issue
707 static unsigned long long log_track_issue(struct per_dev_info *pdi,
708 struct blk_io_trace *t)
710 unsigned long long elapsed;
711 struct io_track *iot;
715 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
718 iot = __find_track(pdi, t->sector);
721 fprintf(stderr, "issue not found for (%d,%d): %llu\n",
722 MAJOR(pdi->dev), MINOR(pdi->dev),
723 (unsigned long long) t->sector);
727 iot->dispatch_time = t->time;
728 elapsed = iot->dispatch_time - iot->queue_time;
730 if (per_process_stats) {
731 struct per_process_info *ppi = find_process(iot->pid,iot->comm);
732 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
734 if (ppi && elapsed > ppi->longest_dispatch_wait[w])
735 ppi->longest_dispatch_wait[w] = elapsed;
742 * return time between dispatch and complete
744 static unsigned long long log_track_complete(struct per_dev_info *pdi,
745 struct blk_io_trace *t)
747 unsigned long long elapsed;
748 struct io_track *iot;
752 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
755 iot = __find_track(pdi, t->sector);
758 fprintf(stderr,"complete not found for (%d,%d): %llu\n",
759 MAJOR(pdi->dev), MINOR(pdi->dev),
760 (unsigned long long) t->sector);
764 iot->completion_time = t->time;
765 elapsed = iot->completion_time - iot->dispatch_time;
767 if (per_process_stats) {
768 struct per_process_info *ppi = find_process(iot->pid,iot->comm);
769 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
771 if (ppi && elapsed > ppi->longest_completion_wait[w])
772 ppi->longest_completion_wait[w] = elapsed;
776 * kill the trace, we don't need it after completion
778 rb_erase(&iot->rb_node, &pdi->rb_track);
785 static struct io_stats *find_process_io_stats(__u32 pid, char *name)
787 struct per_process_info *ppi = find_process(pid, name);
790 ppi = malloc(sizeof(*ppi));
791 memset(ppi, 0, sizeof(*ppi));
792 memcpy(ppi->name, name, 16);
794 add_process_to_hash(ppi);
795 add_process_to_list(ppi);
798 return &ppi->io_stats;
801 static void resize_cpu_info(struct per_dev_info *pdi, int cpu)
803 struct per_cpu_info *cpus = pdi->cpus;
804 int ncpus = pdi->ncpus;
805 int new_count = cpu + 1;
809 size = new_count * sizeof(struct per_cpu_info);
810 cpus = realloc(cpus, size);
813 fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n",
814 get_dev_name(pdi, name, sizeof(name)), size);
818 new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info));
819 new_space = (new_count - ncpus) * sizeof(struct per_cpu_info);
820 memset(new_start, 0, new_space);
822 pdi->ncpus = new_count;
825 for (new_count = 0; new_count < pdi->ncpus; new_count++)
826 if (!pdi->cpus[new_count].fd)
827 pdi->cpus[new_count].fd = -1;
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)) != 0;
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 pdi->cur_depth[w]--;
1170 account_requeue(t, pci, w);
1171 log_queue(pci, t, "R");
1173 case __BLK_TA_ISSUE:
1174 account_issue(t, pci, w);
1175 pdi->cur_depth[w]++;
1176 if (pdi->cur_depth[w] > pdi->max_depth[w])
1177 pdi->max_depth[w] = pdi->cur_depth[w];
1178 log_issue(pdi, pci, t, "D");
1180 case __BLK_TA_COMPLETE:
1181 pdi->cur_depth[w]--;
1182 account_c(t, pci, w, t->bytes);
1183 log_complete(pdi, pci, t, "C");
1186 log_action(pci, t, "P");
1188 case __BLK_TA_UNPLUG_IO:
1189 account_unplug(t, pci, 0);
1190 log_unplug(pci, t, "U");
1192 case __BLK_TA_UNPLUG_TIMER:
1193 account_unplug(t, pci, 1);
1194 log_unplug(pci, t, "UT");
1196 case __BLK_TA_SPLIT:
1197 log_split(pci, t, "X");
1199 case __BLK_TA_BOUNCE:
1200 log_generic(pci, t, "B");
1202 case __BLK_TA_REMAP:
1203 log_generic(pci, t, "A");
1206 fprintf(stderr, "Bad fs action %x\n", t->action);
1211 static void dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
1212 struct per_dev_info *pdi)
1214 if (t->action & BLK_TC_ACT(BLK_TC_PC))
1215 dump_trace_pc(t, pci);
1217 dump_trace_fs(t, pdi, pci);
1220 pdi->first_reported_time = t->time;
1226 * print in a proper way, not too small and not too big. if more than
1227 * 1000,000K, turn into M and so on
1229 static char *size_cnv(char *dst, unsigned long long num, int in_kb)
1231 char suff[] = { '\0', 'K', 'M', 'G', 'P' };
1237 while (num > 1000 * 1000ULL && (i < sizeof(suff) - 1)) {
1242 sprintf(dst, "%'8Lu%c", num, suff[i]);
1246 static void dump_io_stats(struct per_dev_info *pdi, struct io_stats *ios,
1249 static char x[256], y[256];
1251 fprintf(ofp, "%s\n", msg);
1253 fprintf(ofp, " Reads Queued: %s, %siB\t", size_cnv(x, ios->qreads, 0), size_cnv(y, ios->qread_kb, 1));
1254 fprintf(ofp, " Writes Queued: %s, %siB\n", size_cnv(x, ios->qwrites, 0), size_cnv(y, ios->qwrite_kb, 1));
1256 fprintf(ofp, " Read Dispatches: %s, %siB\t", size_cnv(x, ios->ireads, 0), size_cnv(y, ios->iread_kb, 1));
1257 fprintf(ofp, " Write Dispatches: %s, %siB\n", size_cnv(x, ios->iwrites, 0), size_cnv(y, ios->iwrite_kb, 1));
1258 fprintf(ofp, " Reads Requeued: %s\t\t", size_cnv(x, ios->rrqueue, 0));
1259 fprintf(ofp, " Writes Requeued: %s\n", size_cnv(x, ios->wrqueue, 0));
1260 fprintf(ofp, " Reads Completed: %s, %siB\t", size_cnv(x, ios->creads, 0), size_cnv(y, ios->cread_kb, 1));
1261 fprintf(ofp, " Writes Completed: %s, %siB\n", size_cnv(x, ios->cwrites, 0), size_cnv(y, ios->cwrite_kb, 1));
1262 fprintf(ofp, " Read Merges: %'8lu%8c\t", ios->mreads, ' ');
1263 fprintf(ofp, " Write Merges: %'8lu\n", ios->mwrites);
1265 fprintf(ofp, " Read depth: %'8u%8c\t", pdi->max_depth[0], ' ');
1266 fprintf(ofp, " Write depth: %'8u\n", pdi->max_depth[1]);
1268 fprintf(ofp, " IO unplugs: %'8lu%8c\t", ios->io_unplugs, ' ');
1269 fprintf(ofp, " Timer unplugs: %'8lu\n", ios->timer_unplugs);
1272 static void dump_wait_stats(struct per_process_info *ppi)
1274 unsigned long rawait = ppi->longest_allocation_wait[0] / 1000;
1275 unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000;
1276 unsigned long rcwait = ppi->longest_completion_wait[0] / 1000;
1277 unsigned long wawait = ppi->longest_allocation_wait[1] / 1000;
1278 unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000;
1279 unsigned long wcwait = ppi->longest_completion_wait[1] / 1000;
1281 fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' ');
1282 fprintf(ofp, " Allocation wait: %'8lu\n", wawait);
1283 fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' ');
1284 fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait);
1285 fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' ');
1286 fprintf(ofp, " Completion wait: %'8lu\n", wcwait);
1289 static int ppi_name_compare(const void *p1, const void *p2)
1291 struct per_process_info *ppi1 = *((struct per_process_info **) p1);
1292 struct per_process_info *ppi2 = *((struct per_process_info **) p2);
1295 res = strverscmp(ppi1->name, ppi2->name);
1297 res = ppi1->pid > ppi2->pid;
1302 static void sort_process_list(void)
1304 struct per_process_info **ppis;
1305 struct per_process_info *ppi;
1308 ppis = malloc(ppi_list_entries * sizeof(struct per_process_info *));
1313 ppi = ppi->list_next;
1316 qsort(ppis, ppi_list_entries, sizeof(ppi), ppi_name_compare);
1318 i = ppi_list_entries - 1;
1323 ppi->list_next = ppi_list;
1331 static void show_process_stats(void)
1333 struct per_process_info *ppi;
1335 sort_process_list();
1341 if (ppi->more_than_one)
1342 sprintf(name, "%s (%u, ...)", ppi->name, ppi->pid);
1344 sprintf(name, "%s (%u)", ppi->name, ppi->pid);
1346 dump_io_stats(NULL, &ppi->io_stats, name);
1347 dump_wait_stats(ppi);
1348 ppi = ppi->list_next;
1354 static void show_device_and_cpu_stats(void)
1356 struct per_dev_info *pdi;
1357 struct per_cpu_info *pci;
1358 struct io_stats total, *ios;
1359 unsigned long long rrate, wrate, msec;
1360 int i, j, pci_events;
1361 char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3];
1364 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
1366 memset(&total, 0, sizeof(total));
1372 for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) {
1376 ios = &pci->io_stats;
1377 total.qreads += ios->qreads;
1378 total.qwrites += ios->qwrites;
1379 total.creads += ios->creads;
1380 total.cwrites += ios->cwrites;
1381 total.mreads += ios->mreads;
1382 total.mwrites += ios->mwrites;
1383 total.ireads += ios->ireads;
1384 total.iwrites += ios->iwrites;
1385 total.rrqueue += ios->rrqueue;
1386 total.wrqueue += ios->wrqueue;
1387 total.qread_kb += ios->qread_kb;
1388 total.qwrite_kb += ios->qwrite_kb;
1389 total.cread_kb += ios->cread_kb;
1390 total.cwrite_kb += ios->cwrite_kb;
1391 total.iread_kb += ios->iread_kb;
1392 total.iwrite_kb += ios->iwrite_kb;
1393 total.timer_unplugs += ios->timer_unplugs;
1394 total.io_unplugs += ios->io_unplugs;
1396 snprintf(line, sizeof(line) - 1, "CPU%d (%s):",
1397 j, get_dev_name(pdi, name, sizeof(name)));
1398 dump_io_stats(pdi, ios, line);
1402 if (pci_events > 1) {
1404 snprintf(line, sizeof(line) - 1, "Total (%s):",
1405 get_dev_name(pdi, name, sizeof(name)));
1406 dump_io_stats(NULL, &total, line);
1410 msec = (pdi->last_reported_time - pdi->first_reported_time) / 1000000;
1412 rrate = 1000 * total.cread_kb / msec;
1413 wrate = 1000 * total.cwrite_kb / msec;
1416 fprintf(ofp, "\nThroughput (R/W): %'LuKiB/s / %'LuKiB/s\n",
1418 fprintf(ofp, "Events (%s): %'Lu entries\n",
1419 get_dev_name(pdi, line, sizeof(line)), pdi->events);
1421 collect_pdi_skips(pdi);
1422 fprintf(ofp, "Skips: %'lu forward (%'llu - %5.1lf%%)\n",
1423 pdi->skips,pdi->seq_skips,
1424 100.0 * ((double)pdi->seq_skips /
1425 (double)(pdi->events + pdi->seq_skips)));
1430 * struct trace and blktrace allocation cache, we do potentially
1431 * millions of mallocs for these structures while only using at most
1432 * a few thousand at the time
1434 static inline void t_free(struct trace *t)
1436 if (t_alloc_cache < 1024) {
1437 t->next = t_alloc_list;
1444 static inline struct trace *t_alloc(void)
1446 struct trace *t = t_alloc_list;
1449 t_alloc_list = t->next;
1454 return malloc(sizeof(*t));
1457 static inline void bit_free(struct blk_io_trace *bit)
1459 if (bit_alloc_cache < 1024 && !bit->pdu_len) {
1461 * abuse a 64-bit field for a next pointer for the free item
1463 bit->time = (__u64) (unsigned long) bit_alloc_list;
1464 bit_alloc_list = (struct blk_io_trace *) bit;
1470 static inline struct blk_io_trace *bit_alloc(void)
1472 struct blk_io_trace *bit = bit_alloc_list;
1475 bit_alloc_list = (struct blk_io_trace *) (unsigned long) \
1481 return malloc(sizeof(*bit));
1484 static void find_genesis(void)
1486 struct trace *t = trace_list;
1488 genesis_time = -1ULL;
1490 if (t->bit->time < genesis_time)
1491 genesis_time = t->bit->time;
1497 static inline int check_stopwatch(struct blk_io_trace *bit)
1499 if (bit->time < stopwatch_end &&
1500 bit->time >= stopwatch_start)
1507 * return youngest entry read
1509 static int sort_entries(unsigned long long *youngest)
1517 while ((t = trace_list) != NULL) {
1518 struct blk_io_trace *bit = t->bit;
1520 trace_list = t->next;
1522 bit->time -= genesis_time;
1524 if (bit->time < *youngest || !*youngest)
1525 *youngest = bit->time;
1527 if (bit->sequence < smallest_seq_read)
1528 smallest_seq_read = bit->sequence;
1530 if (check_stopwatch(bit)) {
1536 if (trace_rb_insert_sort(t))
1543 static inline void __put_trace_last(struct per_dev_info *pdi, struct trace *t)
1545 rb_erase(&t->rb_node, &pdi->rb_last);
1546 pdi->rb_last_entries--;
1552 static void put_trace(struct per_dev_info *pdi, struct trace *t)
1554 rb_erase(&t->rb_node, &rb_sort_root);
1557 trace_rb_insert_last(pdi, t);
1559 if (pdi->rb_last_entries > rb_batch * pdi->nfiles) {
1560 struct rb_node *n = rb_first(&pdi->rb_last);
1562 t = rb_entry(n, struct trace, rb_node);
1563 __put_trace_last(pdi, t);
1568 * to continue, we must have traces from all online cpus in the tree
1570 static int check_cpu_map(struct per_dev_info *pdi)
1572 unsigned long *cpu_map;
1579 * create a map of the cpus we have traces for
1581 cpu_map = malloc(pdi->cpu_map_max / sizeof(long));
1582 n = rb_first(&rb_sort_root);
1584 __t = rb_entry(n, struct trace, rb_node);
1585 cpu = __t->bit->cpu;
1587 cpu_map[CPU_IDX(cpu)] |= (1UL << CPU_BIT(cpu));
1592 * we can't continue if pdi->cpu_map has entries set that we don't
1593 * have in the sort rbtree. the opposite is not a problem, though
1596 for (i = 0; i < pdi->cpu_map_max / CPUS_PER_LONG; i++) {
1597 if (pdi->cpu_map[i] & ~(cpu_map[i])) {
1607 static int check_sequence(struct per_dev_info *pdi, struct trace *t, int force)
1609 unsigned long expected_sequence = pdi->last_sequence + 1;
1610 struct blk_io_trace *bit = t->bit;
1613 if (!expected_sequence) {
1615 * 1 should be the first entry, just allow it
1617 if (bit->sequence == 1)
1619 if (bit->sequence == smallest_seq_read)
1622 return check_cpu_map(pdi);
1625 if (bit->sequence == expected_sequence)
1629 * we may not have seen that sequence yet. if we are not doing
1630 * the final run, break and wait for more entries.
1632 if (expected_sequence < smallest_seq_read) {
1633 __t = trace_rb_find_last(pdi, expected_sequence);
1637 __put_trace_last(pdi, __t);
1639 } else if (!force) {
1643 if (check_current_skips(pdi,bit->sequence))
1646 if (expected_sequence < bit->sequence)
1647 insert_skip(pdi, expected_sequence, bit->sequence - 1);
1652 static void show_entries_rb(int force)
1654 struct per_dev_info *pdi = NULL;
1655 struct per_cpu_info *pci = NULL;
1656 struct blk_io_trace *bit;
1660 while ((n = rb_first(&rb_sort_root)) != NULL) {
1661 if (is_done() && !force && !pipeline)
1664 t = rb_entry(n, struct trace, rb_node);
1667 if (!pdi || pdi->dev != bit->device)
1668 pdi = get_dev_info(bit->device);
1671 fprintf(stderr, "Unknown device ID? (%d,%d)\n",
1672 MAJOR(bit->device), MINOR(bit->device));
1676 if (check_sequence(pdi, t, force))
1679 if (!force && bit->time > last_allowed_time)
1682 pdi->last_sequence = bit->sequence;
1684 check_time(pdi, bit);
1686 if (!pci || pci->cpu != bit->cpu)
1687 pci = get_cpu_info(pdi, bit->cpu);
1691 if (bit->action & (act_mask << BLK_TC_SHIFT))
1692 dump_trace(bit, pci, pdi);
1698 static int read_data(int fd, void *buffer, int bytes, int block, int *fdblock)
1700 int ret, bytes_left, fl;
1703 if (block != *fdblock) {
1704 fl = fcntl(fd, F_GETFL);
1708 fcntl(fd, F_SETFL, fl | O_NONBLOCK);
1711 fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
1717 while (bytes_left > 0) {
1718 ret = read(fd, p, bytes_left);
1722 if (errno != EAGAIN) {
1728 * never do partial reads. we can return if we
1729 * didn't read anything and we should not block,
1730 * otherwise wait for data
1732 if ((bytes_left == bytes) && !block)
1746 static int read_events(int fd, int always_block, int *fdblock)
1748 struct per_dev_info *pdi = NULL;
1749 unsigned int events = 0;
1751 while (!is_done() && events < rb_batch) {
1752 struct blk_io_trace *bit;
1754 int pdu_len, should_block, ret;
1759 should_block = !events || always_block;
1761 ret = read_data(fd, bit, sizeof(*bit), should_block, fdblock);
1764 if (!events && ret < 0)
1769 magic = be32_to_cpu(bit->magic);
1770 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
1771 fprintf(stderr, "Bad magic %x\n", magic);
1775 pdu_len = be16_to_cpu(bit->pdu_len);
1777 void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
1779 if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1, fdblock)) {
1789 if (verify_trace(bit)) {
1795 memset(t, 0, sizeof(*t));
1798 t->next = trace_list;
1801 if (!pdi || pdi->dev != bit->device)
1802 pdi = get_dev_info(bit->device);
1804 if (bit->time > pdi->last_read_time)
1805 pdi->last_read_time = bit->time;
1813 static int do_file(void)
1815 struct per_cpu_info *pci;
1816 struct per_dev_info *pdi;
1817 int i, j, events, events_added;
1820 * first prepare all files for reading
1822 for (i = 0; i < ndevices; i++) {
1825 pdi->last_sequence = -1;
1832 pci = get_cpu_info(pdi, j);
1837 p = strdup(pdi->name);
1839 if (strcmp(dname, ".")) {
1841 p = strdup(pdi->name);
1842 strcpy(pdi->name, basename(p));
1847 len = sprintf(pci->fname, "%s/", input_dir);
1849 snprintf(pci->fname + len, sizeof(pci->fname)-1-len,
1850 "%s.blktrace.%d", pdi->name, pci->cpu);
1851 if (stat(pci->fname, &st) < 0)
1854 pci->fd = open(pci->fname, O_RDONLY);
1861 printf("Input file %s added\n", pci->fname);
1863 cpu_mark_online(pdi, pci->cpu);
1868 * now loop over the files reading in the data
1871 unsigned long long youngest;
1874 last_allowed_time = -1ULL;
1875 smallest_seq_read = -1U;
1877 for (i = 0; i < ndevices; i++) {
1880 for (j = 0; j < pdi->nfiles; j++) {
1882 pci = get_cpu_info(pdi, j);
1887 events = read_events(pci->fd, 1, &pci->fdblock);
1889 cpu_mark_offline(pdi, pci->cpu);
1895 if (pdi->last_read_time < last_allowed_time)
1896 last_allowed_time = pdi->last_read_time;
1898 events_added += events;
1902 if (sort_entries(&youngest))
1905 if (youngest > stopwatch_end)
1910 } while (events_added);
1912 if (rb_sort_entries)
1918 static int do_stdin(void)
1920 unsigned long long youngest;
1921 int fd, events, fdblock;
1923 last_allowed_time = -1ULL;
1924 fd = dup(STDIN_FILENO);
1926 perror("dup stdin");
1931 while ((events = read_events(fd, 0, &fdblock)) > 0) {
1933 smallest_seq_read = -1U;
1935 if (sort_entries(&youngest))
1938 if (youngest > stopwatch_end)
1944 if (rb_sort_entries)
1951 static void show_stats(void)
1960 if (per_process_stats)
1961 show_process_stats();
1963 if (per_device_and_cpu_stats)
1964 show_device_and_cpu_stats();
1969 static void handle_sigint(__attribute__((__unused__)) int sig)
1975 * Extract start and duration times from a string, allowing
1976 * us to specify a time interval of interest within a trace.
1977 * Format: "duration" (start is zero) or "start:duration".
1979 static int find_stopwatch_interval(char *string)
1984 value = strtod(string, &sp);
1986 fprintf(stderr,"Invalid stopwatch timer: %s\n", string);
1990 stopwatch_start = DOUBLE_TO_NANO_ULL(value);
1992 value = strtod(string, &sp);
1993 if (sp == string || *sp != '\0') {
1994 fprintf(stderr,"Invalid stopwatch duration time: %s\n",
1998 } else if (*sp != '\0') {
1999 fprintf(stderr,"Invalid stopwatch start timer: %s\n", string);
2002 stopwatch_end = DOUBLE_TO_NANO_ULL(value);
2003 if (stopwatch_end <= stopwatch_start) {
2004 fprintf(stderr, "Invalid stopwatch interval: %Lu -> %Lu\n",
2005 stopwatch_start, stopwatch_end);
2012 static char usage_str[] = \
2013 "[ -i <input name> ] [-o <output name> [ -s ] [ -t ] [ -q ]\n" \
2014 "[ -w start:stop ] [ -f output format ] [ -F format spec ] [ -v] \n\n" \
2015 "\t-i Input file containing trace data, or '-' for stdin\n" \
2016 "\t-D Directory to prepend to input file names\n" \
2017 "\t-o Output file. If not given, output is stdout\n" \
2018 "\t-b stdin read batching\n" \
2019 "\t-s Show per-program io statistics\n" \
2020 "\t-h Hash processes by name, not pid\n" \
2021 "\t-t Track individual ios. Will tell you the time a request took\n" \
2022 "\t to get queued, to get dispatched, and to get completed\n" \
2023 "\t-q Quiet. Don't display any stats at the end of the trace\n" \
2024 "\t-w Only parse data between the given time interval in seconds.\n" \
2025 "\t If 'start' isn't given, blkparse defaults the start time to 0\n" \
2026 "\t-f Output format. Customize the output format. The format field\n" \
2027 "\t identifies can be found in the documentation\n" \
2028 "\t-F Format specification. Can be found in the documentation\n" \
2029 "\t-v More verbose for marginal errors\n" \
2030 "\t-V Print program version info\n\n";
2032 static void usage(char *prog)
2034 fprintf(stderr, "Usage: %s %s %s", prog, blkparse_version, usage_str);
2037 int main(int argc, char *argv[])
2040 int i, c, ret, mode;
2041 int act_mask_tmp = 0;
2043 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
2046 i = find_mask_map(optarg);
2048 fprintf(stderr,"Invalid action mask %s\n",
2056 if ((sscanf(optarg, "%x", &i) != 1) ||
2057 !valid_act_opt(i)) {
2059 "Invalid set action mask %s/0x%x\n",
2066 if (!strcmp(optarg, "-") && !pipeline)
2068 else if (resize_devices(optarg) != 0)
2075 output_name = optarg;
2078 rb_batch = atoi(optarg);
2080 rb_batch = RB_BATCH_DEFAULT;
2083 per_process_stats = 1;
2089 per_device_and_cpu_stats = 0;
2092 if (find_stopwatch_interval(optarg) != 0)
2096 set_all_format_specs(optarg);
2099 if (add_format_spec(optarg) != 0)
2103 ppi_hash_by_pid = 0;
2109 printf("%s version %s\n", argv[0], blkparse_version);
2117 while (optind < argc) {
2118 if (!strcmp(argv[optind], "-") && !pipeline)
2120 else if (resize_devices(argv[optind]) != 0)
2125 if (!pipeline && !ndevices) {
2130 if (act_mask_tmp != 0)
2131 act_mask = act_mask_tmp;
2133 memset(&rb_sort_root, 0, sizeof(rb_sort_root));
2135 signal(SIGINT, handle_sigint);
2136 signal(SIGHUP, handle_sigint);
2137 signal(SIGTERM, handle_sigint);
2139 setlocale(LC_NUMERIC, "en_US");
2142 ofp = fdopen(STDOUT_FILENO, "w");
2147 snprintf(ofname, sizeof(ofname) - 1, "%s", output_name);
2148 ofp = fopen(ofname, "w");
2157 ofp_buffer = malloc(4096);
2158 if (setvbuf(ofp, ofp_buffer, mode, 4096)) {