2 * block queue tracing parse application
4 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
5 * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <sys/types.h>
39 static char blkparse_version[] = "1.0.0";
42 unsigned long start, end;
43 struct skip_info *prev, *next;
51 unsigned long long events;
52 unsigned long long first_reported_time;
53 unsigned long long last_reported_time;
54 unsigned long long last_read_time;
55 struct io_stats io_stats;
57 unsigned long long seq_skips;
58 unsigned int max_depth[2];
59 unsigned int cur_depth[2];
61 struct rb_root rb_track;
66 unsigned long *cpu_map;
67 unsigned int cpu_map_max;
69 struct per_cpu_info *cpus;
73 * some duplicated effort here, we can unify this hash and the ppi hash later
75 struct process_pid_map {
78 struct process_pid_map *hash_next, *list_next;
81 #define PPM_HASH_SHIFT (8)
82 #define PPM_HASH_SIZE (1 << PPM_HASH_SHIFT)
83 #define PPM_HASH_MASK (PPM_HASH_SIZE - 1)
84 static struct process_pid_map *ppm_hash_table[PPM_HASH_SIZE];
86 struct per_process_info {
87 struct process_pid_map *ppm;
88 struct io_stats io_stats;
89 struct per_process_info *hash_next, *list_next;
95 unsigned long long longest_allocation_wait[2];
96 unsigned long long longest_dispatch_wait[2];
97 unsigned long long longest_completion_wait[2];
100 #define PPI_HASH_SHIFT (8)
101 #define PPI_HASH_SIZE (1 << PPI_HASH_SHIFT)
102 #define PPI_HASH_MASK (PPI_HASH_SIZE - 1)
103 static struct per_process_info *ppi_hash_table[PPI_HASH_SIZE];
104 static struct per_process_info *ppi_list;
105 static int ppi_list_entries;
107 static struct option l_opts[] = {
110 .has_arg = required_argument,
116 .has_arg = required_argument,
122 .has_arg = required_argument,
127 .name = "input-directory",
128 .has_arg = required_argument,
133 .name = "dump-binary",
134 .has_arg = required_argument,
140 .has_arg = required_argument,
145 .name = "format-spec",
146 .has_arg = required_argument,
151 .name = "hash-by-name",
152 .has_arg = no_argument,
158 .has_arg = required_argument,
164 .has_arg = no_argument,
170 .has_arg = required_argument,
175 .name = "no-text-output",
176 .has_arg = no_argument,
182 .has_arg = no_argument,
187 .name = "per-program-stats",
188 .has_arg = no_argument,
194 .has_arg = no_argument,
200 .has_arg = required_argument,
206 .has_arg = no_argument,
212 .has_arg = no_argument,
222 * for sorting the displayed output
225 struct blk_io_trace *bit;
226 struct rb_node rb_node;
228 unsigned long read_sequence;
231 static struct rb_root rb_sort_root;
232 static unsigned long rb_sort_entries;
234 static struct trace *trace_list;
239 static struct blk_io_trace *bit_alloc_list;
240 static struct trace *t_alloc_list;
243 * for tracking individual ios
246 struct rb_node rb_node;
248 struct process_pid_map *ppm;
250 unsigned long long allocation_time;
251 unsigned long long queue_time;
252 unsigned long long dispatch_time;
253 unsigned long long completion_time;
257 static struct per_dev_info *devices;
258 static char *get_dev_name(struct per_dev_info *, char *, int);
259 static int trace_rb_insert_last(struct per_dev_info *, struct trace *);
262 static char *output_name;
263 static char *input_dir;
265 static unsigned long long genesis_time;
266 static unsigned long long last_allowed_time;
267 static unsigned long long stopwatch_start; /* start from zero by default */
268 static unsigned long long stopwatch_end = -1ULL; /* "infinity" */
269 static unsigned long read_sequence;
271 static int per_process_stats;
272 static int per_device_and_cpu_stats = 1;
273 static int track_ios;
274 static int ppi_hash_by_pid = 1;
276 static unsigned int act_mask = -1U;
277 static int stats_printed;
278 static int bin_output_msgs = 1;
279 int data_is_native = -1;
281 static FILE *dump_fp;
282 static char *dump_binary;
284 static unsigned int t_alloc_cache;
285 static unsigned int bit_alloc_cache;
287 #define RB_BATCH_DEFAULT (512)
288 static unsigned int rb_batch = RB_BATCH_DEFAULT;
291 static char *pipename;
293 static int text_output = 1;
295 #define is_done() (*(volatile int *)(&done))
296 static volatile int done;
298 struct timespec abs_start_time;
299 static unsigned long long start_timestamp;
301 static int have_drv_data = 0;
303 #define JHASH_RANDOM (0x3af5f2ee)
305 #define CPUS_PER_LONG (8 * sizeof(unsigned long))
306 #define CPU_IDX(cpu) ((cpu) / CPUS_PER_LONG)
307 #define CPU_BIT(cpu) ((cpu) & (CPUS_PER_LONG - 1))
309 static void output_binary(void *buf, int len)
312 size_t n = fwrite(buf, len, 1, dump_fp);
321 static void resize_cpu_info(struct per_dev_info *pdi, int cpu)
323 struct per_cpu_info *cpus = pdi->cpus;
324 int ncpus = pdi->ncpus;
325 int new_count = cpu + 1;
329 size = new_count * sizeof(struct per_cpu_info);
330 cpus = realloc(cpus, size);
333 fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n",
334 get_dev_name(pdi, name, sizeof(name)), size);
338 new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info));
339 new_space = (new_count - ncpus) * sizeof(struct per_cpu_info);
340 memset(new_start, 0, new_space);
342 pdi->ncpus = new_count;
345 for (new_count = 0; new_count < pdi->ncpus; new_count++) {
346 struct per_cpu_info *pci = &pdi->cpus[new_count];
350 memset(&pci->rb_last, 0, sizeof(pci->rb_last));
351 pci->rb_last_entries = 0;
352 pci->last_sequence = -1;
357 static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu)
359 struct per_cpu_info *pci;
361 if (cpu >= pdi->ncpus)
362 resize_cpu_info(pdi, cpu);
364 pci = &pdi->cpus[cpu];
370 static int resize_devices(char *name)
372 int size = (ndevices + 1) * sizeof(struct per_dev_info);
374 devices = realloc(devices, size);
376 fprintf(stderr, "Out of memory, device %s (%d)\n", name, size);
379 memset(&devices[ndevices], 0, sizeof(struct per_dev_info));
380 devices[ndevices].name = name;
385 static struct per_dev_info *get_dev_info(dev_t dev)
387 struct per_dev_info *pdi;
390 for (i = 0; i < ndevices; i++) {
392 devices[i].dev = dev;
393 if (devices[i].dev == dev)
397 if (resize_devices(NULL))
400 pdi = &devices[ndevices - 1];
402 pdi->first_reported_time = 0;
403 pdi->last_read_time = 0;
408 static void insert_skip(struct per_cpu_info *pci, unsigned long start,
411 struct skip_info *sip;
413 for (sip = pci->skips_tail; sip != NULL; sip = sip->prev) {
414 if (end == (sip->start - 1)) {
417 } else if (start == (sip->end + 1)) {
423 sip = malloc(sizeof(struct skip_info));
426 sip->prev = sip->next = NULL;
427 if (pci->skips_tail == NULL)
428 pci->skips_head = pci->skips_tail = sip;
430 sip->prev = pci->skips_tail;
431 pci->skips_tail->next = sip;
432 pci->skips_tail = sip;
436 static void remove_sip(struct per_cpu_info *pci, struct skip_info *sip)
438 if (sip->prev == NULL) {
439 if (sip->next == NULL)
440 pci->skips_head = pci->skips_tail = NULL;
442 pci->skips_head = sip->next;
443 sip->next->prev = NULL;
445 } else if (sip->next == NULL) {
446 pci->skips_tail = sip->prev;
447 sip->prev->next = NULL;
449 sip->prev->next = sip->next;
450 sip->next->prev = sip->prev;
453 sip->prev = sip->next = NULL;
457 #define IN_SKIP(sip,seq) (((sip)->start <= (seq)) && ((seq) <= sip->end))
458 static int check_current_skips(struct per_cpu_info *pci, unsigned long seq)
460 struct skip_info *sip;
462 for (sip = pci->skips_tail; sip != NULL; sip = sip->prev) {
463 if (IN_SKIP(sip, seq)) {
464 if (sip->start == seq) {
466 remove_sip(pci, sip);
469 } else if (sip->end == seq)
473 insert_skip(pci, seq + 1, sip->end);
482 static void collect_pdi_skips(struct per_dev_info *pdi)
484 struct skip_info *sip;
490 for (cpu = 0; cpu < pdi->ncpus; cpu++) {
491 struct per_cpu_info *pci = &pdi->cpus[cpu];
493 for (sip = pci->skips_head; sip != NULL; sip = sip->next) {
495 pdi->seq_skips += (sip->end - sip->start + 1);
497 fprintf(stderr,"(%d,%d): skipping %lu -> %lu\n",
498 MAJOR(pdi->dev), MINOR(pdi->dev),
499 sip->start, sip->end);
504 static void cpu_mark_online(struct per_dev_info *pdi, unsigned int cpu)
506 if (cpu >= pdi->cpu_map_max || !pdi->cpu_map) {
507 int new_max = (cpu + CPUS_PER_LONG) & ~(CPUS_PER_LONG - 1);
508 unsigned long *map = malloc(new_max / sizeof(long));
510 memset(map, 0, new_max / sizeof(long));
513 memcpy(map, pdi->cpu_map, pdi->cpu_map_max / sizeof(long));
518 pdi->cpu_map_max = new_max;
521 pdi->cpu_map[CPU_IDX(cpu)] |= (1UL << CPU_BIT(cpu));
524 static inline void cpu_mark_offline(struct per_dev_info *pdi, int cpu)
526 pdi->cpu_map[CPU_IDX(cpu)] &= ~(1UL << CPU_BIT(cpu));
529 static inline int cpu_is_online(struct per_dev_info *pdi, int cpu)
531 return (pdi->cpu_map[CPU_IDX(cpu)] & (1UL << CPU_BIT(cpu))) != 0;
534 static inline int ppm_hash_pid(pid_t pid)
536 return jhash_1word(pid, JHASH_RANDOM) & PPM_HASH_MASK;
539 static struct process_pid_map *find_ppm(pid_t pid)
541 const int hash_idx = ppm_hash_pid(pid);
542 struct process_pid_map *ppm;
544 ppm = ppm_hash_table[hash_idx];
549 ppm = ppm->hash_next;
555 static struct process_pid_map *add_ppm_hash(pid_t pid, const char *name)
557 const int hash_idx = ppm_hash_pid(pid);
558 struct process_pid_map *ppm;
562 ppm = malloc(sizeof(*ppm));
563 memset(ppm, 0, sizeof(*ppm));
565 strcpy(ppm->comm, name);
566 ppm->hash_next = ppm_hash_table[hash_idx];
567 ppm_hash_table[hash_idx] = ppm;
573 static void handle_notify(struct blk_io_trace *bit)
575 void *payload = (caddr_t) bit + sizeof(*bit);
578 switch (bit->action) {
580 add_ppm_hash(bit->pid, payload);
583 case BLK_TN_TIMESTAMP:
584 if (bit->pdu_len != sizeof(two32))
586 memcpy(two32, payload, sizeof(two32));
587 if (!data_is_native) {
588 two32[0] = be32_to_cpu(two32[0]);
589 two32[1] = be32_to_cpu(two32[1]);
591 start_timestamp = bit->time;
592 abs_start_time.tv_sec = two32[0];
593 abs_start_time.tv_nsec = two32[1];
594 if (abs_start_time.tv_nsec < 0) {
595 abs_start_time.tv_sec--;
596 abs_start_time.tv_nsec += 1000000000;
602 if (bit->pdu_len > 0) {
603 char msg[bit->pdu_len+1];
605 memcpy(msg, (char *)payload, bit->pdu_len);
606 msg[bit->pdu_len] = '\0';
609 "%3d,%-3d %2d %8s %5d.%09lu %5u %2s %3s %s\n",
610 MAJOR(bit->device), MINOR(bit->device),
611 bit->cpu, "0", (int) SECONDS(bit->time),
612 (unsigned long) NANO_SECONDS(bit->time),
618 /* Ignore unknown notify events */
623 char *find_process_name(pid_t pid)
625 struct process_pid_map *ppm = find_ppm(pid);
633 static inline int ppi_hash_pid(pid_t pid)
635 return jhash_1word(pid, JHASH_RANDOM) & PPI_HASH_MASK;
638 static inline int ppi_hash_name(const char *name)
640 return jhash(name, 16, JHASH_RANDOM) & PPI_HASH_MASK;
643 static inline int ppi_hash(struct per_process_info *ppi)
645 struct process_pid_map *ppm = ppi->ppm;
648 return ppi_hash_pid(ppm->pid);
650 return ppi_hash_name(ppm->comm);
653 static inline void add_ppi_to_hash(struct per_process_info *ppi)
655 const int hash_idx = ppi_hash(ppi);
657 ppi->hash_next = ppi_hash_table[hash_idx];
658 ppi_hash_table[hash_idx] = ppi;
661 static inline void add_ppi_to_list(struct per_process_info *ppi)
663 ppi->list_next = ppi_list;
668 static struct per_process_info *find_ppi_by_name(char *name)
670 const int hash_idx = ppi_hash_name(name);
671 struct per_process_info *ppi;
673 ppi = ppi_hash_table[hash_idx];
675 struct process_pid_map *ppm = ppi->ppm;
677 if (!strcmp(ppm->comm, name))
680 ppi = ppi->hash_next;
686 static struct per_process_info *find_ppi_by_pid(pid_t pid)
688 const int hash_idx = ppi_hash_pid(pid);
689 struct per_process_info *ppi;
691 ppi = ppi_hash_table[hash_idx];
693 struct process_pid_map *ppm = ppi->ppm;
698 ppi = ppi->hash_next;
704 static struct per_process_info *find_ppi(pid_t pid)
706 struct per_process_info *ppi;
710 return find_ppi_by_pid(pid);
712 name = find_process_name(pid);
716 ppi = find_ppi_by_name(name);
717 if (ppi && ppi->ppm->pid != pid)
718 ppi->more_than_one = 1;
724 * struct trace and blktrace allocation cache, we do potentially
725 * millions of mallocs for these structures while only using at most
726 * a few thousand at the time
728 static inline void t_free(struct trace *t)
730 if (t_alloc_cache < 1024) {
731 t->next = t_alloc_list;
738 static inline struct trace *t_alloc(void)
740 struct trace *t = t_alloc_list;
743 t_alloc_list = t->next;
748 return malloc(sizeof(*t));
751 static inline void bit_free(struct blk_io_trace *bit)
753 if (bit_alloc_cache < 1024 && !bit->pdu_len) {
755 * abuse a 64-bit field for a next pointer for the free item
757 bit->time = (__u64) (unsigned long) bit_alloc_list;
758 bit_alloc_list = (struct blk_io_trace *) bit;
764 static inline struct blk_io_trace *bit_alloc(void)
766 struct blk_io_trace *bit = bit_alloc_list;
769 bit_alloc_list = (struct blk_io_trace *) (unsigned long) \
775 return malloc(sizeof(*bit));
778 static inline void __put_trace_last(struct per_dev_info *pdi, struct trace *t)
780 struct per_cpu_info *pci = get_cpu_info(pdi, t->bit->cpu);
782 rb_erase(&t->rb_node, &pci->rb_last);
783 pci->rb_last_entries--;
789 static void put_trace(struct per_dev_info *pdi, struct trace *t)
791 rb_erase(&t->rb_node, &rb_sort_root);
794 trace_rb_insert_last(pdi, t);
797 static inline int trace_rb_insert(struct trace *t, struct rb_root *root)
799 struct rb_node **p = &root->rb_node;
800 struct rb_node *parent = NULL;
806 __t = rb_entry(parent, struct trace, rb_node);
808 if (t->bit->time < __t->bit->time)
810 else if (t->bit->time > __t->bit->time)
812 else if (t->bit->device < __t->bit->device)
814 else if (t->bit->device > __t->bit->device)
816 else if (t->bit->sequence < __t->bit->sequence)
818 else /* >= sequence */
822 rb_link_node(&t->rb_node, parent, p);
823 rb_insert_color(&t->rb_node, root);
827 static inline int trace_rb_insert_sort(struct trace *t)
829 if (!trace_rb_insert(t, &rb_sort_root)) {
837 static int trace_rb_insert_last(struct per_dev_info *pdi, struct trace *t)
839 struct per_cpu_info *pci = get_cpu_info(pdi, t->bit->cpu);
841 if (trace_rb_insert(t, &pci->rb_last))
844 pci->rb_last_entries++;
846 if (pci->rb_last_entries > rb_batch * pdi->nfiles) {
847 struct rb_node *n = rb_first(&pci->rb_last);
849 t = rb_entry(n, struct trace, rb_node);
850 __put_trace_last(pdi, t);
856 static struct trace *trace_rb_find(dev_t device, unsigned long sequence,
857 struct rb_root *root, int order)
859 struct rb_node *n = root->rb_node;
860 struct rb_node *prev = NULL;
864 __t = rb_entry(n, struct trace, rb_node);
867 if (device < __t->bit->device)
869 else if (device > __t->bit->device)
871 else if (sequence < __t->bit->sequence)
873 else if (sequence > __t->bit->sequence)
880 * hack - the list may not be sequence ordered because some
881 * events don't have sequence and time matched. so we end up
882 * being a little off in the rb lookup here, because we don't
883 * know the time we are looking for. compensate by browsing
884 * a little ahead from the last entry to find the match
889 while (((n = rb_next(prev)) != NULL) && max--) {
890 __t = rb_entry(n, struct trace, rb_node);
892 if (__t->bit->device == device &&
893 __t->bit->sequence == sequence)
903 static inline struct trace *trace_rb_find_last(struct per_dev_info *pdi,
904 struct per_cpu_info *pci,
907 return trace_rb_find(pdi->dev, seq, &pci->rb_last, 0);
910 static inline int track_rb_insert(struct per_dev_info *pdi,struct io_track *iot)
912 struct rb_node **p = &pdi->rb_track.rb_node;
913 struct rb_node *parent = NULL;
914 struct io_track *__iot;
918 __iot = rb_entry(parent, struct io_track, rb_node);
920 if (iot->sector < __iot->sector)
922 else if (iot->sector > __iot->sector)
926 "sector alias (%Lu) on device %d,%d!\n",
927 (unsigned long long) iot->sector,
928 MAJOR(pdi->dev), MINOR(pdi->dev));
933 rb_link_node(&iot->rb_node, parent, p);
934 rb_insert_color(&iot->rb_node, &pdi->rb_track);
938 static struct io_track *__find_track(struct per_dev_info *pdi, __u64 sector)
940 struct rb_node *n = pdi->rb_track.rb_node;
941 struct io_track *__iot;
944 __iot = rb_entry(n, struct io_track, rb_node);
946 if (sector < __iot->sector)
948 else if (sector > __iot->sector)
957 static struct io_track *find_track(struct per_dev_info *pdi, pid_t pid,
960 struct io_track *iot;
962 iot = __find_track(pdi, sector);
964 iot = malloc(sizeof(*iot));
965 iot->ppm = find_ppm(pid);
967 iot->ppm = add_ppm_hash(pid, "unknown");
968 iot->sector = sector;
969 track_rb_insert(pdi, iot);
975 static void log_track_frontmerge(struct per_dev_info *pdi,
976 struct blk_io_trace *t)
978 struct io_track *iot;
983 iot = __find_track(pdi, t->sector + t_sec(t));
986 fprintf(stderr, "merge not found for (%d,%d): %llu\n",
987 MAJOR(pdi->dev), MINOR(pdi->dev),
988 (unsigned long long) t->sector + t_sec(t));
992 rb_erase(&iot->rb_node, &pdi->rb_track);
993 iot->sector -= t_sec(t);
994 track_rb_insert(pdi, iot);
997 static void log_track_getrq(struct per_dev_info *pdi, struct blk_io_trace *t)
999 struct io_track *iot;
1004 iot = find_track(pdi, t->pid, t->sector);
1005 iot->allocation_time = t->time;
1008 static inline int is_remapper(struct per_dev_info *pdi)
1010 int major = MAJOR(pdi->dev);
1012 return (major == 253 || major == 9);
1016 * for md/dm setups, the interesting cycle is Q -> C. So track queueing
1017 * time here, as dispatch time
1019 static void log_track_queue(struct per_dev_info *pdi, struct blk_io_trace *t)
1021 struct io_track *iot;
1025 if (!is_remapper(pdi))
1028 iot = find_track(pdi, t->pid, t->sector);
1029 iot->dispatch_time = t->time;
1033 * return time between rq allocation and insertion
1035 static unsigned long long log_track_insert(struct per_dev_info *pdi,
1036 struct blk_io_trace *t)
1038 unsigned long long elapsed;
1039 struct io_track *iot;
1044 iot = find_track(pdi, t->pid, t->sector);
1045 iot->queue_time = t->time;
1047 if (!iot->allocation_time)
1050 elapsed = iot->queue_time - iot->allocation_time;
1052 if (per_process_stats) {
1053 struct per_process_info *ppi = find_ppi(iot->ppm->pid);
1054 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
1056 if (ppi && elapsed > ppi->longest_allocation_wait[w])
1057 ppi->longest_allocation_wait[w] = elapsed;
1064 * return time between queue and issue
1066 static unsigned long long log_track_issue(struct per_dev_info *pdi,
1067 struct blk_io_trace *t)
1069 unsigned long long elapsed;
1070 struct io_track *iot;
1074 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
1077 iot = __find_track(pdi, t->sector);
1080 fprintf(stderr, "issue not found for (%d,%d): %llu\n",
1081 MAJOR(pdi->dev), MINOR(pdi->dev),
1082 (unsigned long long) t->sector);
1086 iot->dispatch_time = t->time;
1087 elapsed = iot->dispatch_time - iot->queue_time;
1089 if (per_process_stats) {
1090 struct per_process_info *ppi = find_ppi(iot->ppm->pid);
1091 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
1093 if (ppi && elapsed > ppi->longest_dispatch_wait[w])
1094 ppi->longest_dispatch_wait[w] = elapsed;
1101 * return time between dispatch and complete
1103 static unsigned long long log_track_complete(struct per_dev_info *pdi,
1104 struct blk_io_trace *t)
1106 unsigned long long elapsed;
1107 struct io_track *iot;
1112 iot = __find_track(pdi, t->sector);
1115 fprintf(stderr,"complete not found for (%d,%d): %llu\n",
1116 MAJOR(pdi->dev), MINOR(pdi->dev),
1117 (unsigned long long) t->sector);
1121 iot->completion_time = t->time;
1122 elapsed = iot->completion_time - iot->dispatch_time;
1124 if (per_process_stats) {
1125 struct per_process_info *ppi = find_ppi(iot->ppm->pid);
1126 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
1128 if (ppi && elapsed > ppi->longest_completion_wait[w])
1129 ppi->longest_completion_wait[w] = elapsed;
1133 * kill the trace, we don't need it after completion
1135 rb_erase(&iot->rb_node, &pdi->rb_track);
1142 static struct io_stats *find_process_io_stats(pid_t pid)
1144 struct per_process_info *ppi = find_ppi(pid);
1147 ppi = malloc(sizeof(*ppi));
1148 memset(ppi, 0, sizeof(*ppi));
1149 ppi->ppm = find_ppm(pid);
1151 ppi->ppm = add_ppm_hash(pid, "unknown");
1152 add_ppi_to_hash(ppi);
1153 add_ppi_to_list(ppi);
1156 return &ppi->io_stats;
1159 static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size)
1162 snprintf(buffer, size, "%s", pdi->name);
1164 snprintf(buffer, size, "%d,%d",MAJOR(pdi->dev),MINOR(pdi->dev));
1168 static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit)
1170 unsigned long long this = bit->time;
1171 unsigned long long last = pdi->last_reported_time;
1173 pdi->backwards = (this < last) ? 'B' : ' ';
1174 pdi->last_reported_time = this;
1177 static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t,
1182 ios->mwrite_kb += t_kb(t);
1185 ios->mread_kb += t_kb(t);
1189 static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci,
1192 __account_m(&pci->io_stats, t, rw);
1194 if (per_process_stats) {
1195 struct io_stats *ios = find_process_io_stats(t->pid);
1197 __account_m(ios, t, rw);
1201 static inline void __account_pc_queue(struct io_stats *ios,
1202 struct blk_io_trace *t, int rw)
1206 ios->qwrite_kb_pc += t_kb(t);
1209 ios->qread_kb += t_kb(t);
1213 static inline void account_pc_queue(struct blk_io_trace *t,
1214 struct per_cpu_info *pci, int rw)
1216 __account_pc_queue(&pci->io_stats, t, rw);
1218 if (per_process_stats) {
1219 struct io_stats *ios = find_process_io_stats(t->pid);
1221 __account_pc_queue(ios, t, rw);
1225 static inline void __account_pc_issue(struct io_stats *ios, int rw,
1230 ios->iwrite_kb_pc += bytes >> 10;
1233 ios->iread_kb_pc += bytes >> 10;
1237 static inline void account_pc_issue(struct blk_io_trace *t,
1238 struct per_cpu_info *pci, int rw)
1240 __account_pc_issue(&pci->io_stats, rw, t->bytes);
1242 if (per_process_stats) {
1243 struct io_stats *ios = find_process_io_stats(t->pid);
1245 __account_pc_issue(ios, rw, t->bytes);
1249 static inline void __account_pc_requeue(struct io_stats *ios,
1250 struct blk_io_trace *t, int rw)
1254 ios->iwrite_kb_pc -= t_kb(t);
1257 ios->iread_kb_pc -= t_kb(t);
1261 static inline void account_pc_requeue(struct blk_io_trace *t,
1262 struct per_cpu_info *pci, int rw)
1264 __account_pc_requeue(&pci->io_stats, t, rw);
1266 if (per_process_stats) {
1267 struct io_stats *ios = find_process_io_stats(t->pid);
1269 __account_pc_requeue(ios, t, rw);
1273 static inline void __account_pc_c(struct io_stats *ios, int rw)
1281 static inline void account_pc_c(struct blk_io_trace *t,
1282 struct per_cpu_info *pci, int rw)
1284 __account_pc_c(&pci->io_stats, rw);
1286 if (per_process_stats) {
1287 struct io_stats *ios = find_process_io_stats(t->pid);
1289 __account_pc_c(ios, rw);
1293 static inline void __account_queue(struct io_stats *ios, struct blk_io_trace *t,
1298 ios->qwrite_kb += t_kb(t);
1301 ios->qread_kb += t_kb(t);
1305 static inline void account_queue(struct blk_io_trace *t,
1306 struct per_cpu_info *pci, int rw)
1308 __account_queue(&pci->io_stats, t, rw);
1310 if (per_process_stats) {
1311 struct io_stats *ios = find_process_io_stats(t->pid);
1313 __account_queue(ios, t, rw);
1317 static inline void __account_c(struct io_stats *ios, int rw, int bytes)
1321 ios->cwrite_kb += bytes >> 10;
1324 ios->cread_kb += bytes >> 10;
1328 static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci,
1331 __account_c(&pci->io_stats, rw, bytes);
1333 if (per_process_stats) {
1334 struct io_stats *ios = find_process_io_stats(t->pid);
1336 __account_c(ios, rw, bytes);
1340 static inline void __account_issue(struct io_stats *ios, int rw,
1345 ios->iwrite_kb += bytes >> 10;
1348 ios->iread_kb += bytes >> 10;
1352 static inline void account_issue(struct blk_io_trace *t,
1353 struct per_cpu_info *pci, int rw)
1355 __account_issue(&pci->io_stats, rw, t->bytes);
1357 if (per_process_stats) {
1358 struct io_stats *ios = find_process_io_stats(t->pid);
1360 __account_issue(ios, rw, t->bytes);
1364 static inline void __account_unplug(struct io_stats *ios, int timer)
1367 ios->timer_unplugs++;
1372 static inline void account_unplug(struct blk_io_trace *t,
1373 struct per_cpu_info *pci, int timer)
1375 __account_unplug(&pci->io_stats, timer);
1377 if (per_process_stats) {
1378 struct io_stats *ios = find_process_io_stats(t->pid);
1380 __account_unplug(ios, timer);
1384 static inline void __account_requeue(struct io_stats *ios,
1385 struct blk_io_trace *t, int rw)
1389 ios->iwrite_kb -= t_kb(t);
1392 ios->iread_kb -= t_kb(t);
1396 static inline void account_requeue(struct blk_io_trace *t,
1397 struct per_cpu_info *pci, int rw)
1399 __account_requeue(&pci->io_stats, t, rw);
1401 if (per_process_stats) {
1402 struct io_stats *ios = find_process_io_stats(t->pid);
1404 __account_requeue(ios, t, rw);
1408 static void log_complete(struct per_dev_info *pdi, struct per_cpu_info *pci,
1409 struct blk_io_trace *t, char *act)
1411 process_fmt(act, pci, t, log_track_complete(pdi, t), 0, NULL);
1414 static void log_insert(struct per_dev_info *pdi, struct per_cpu_info *pci,
1415 struct blk_io_trace *t, char *act)
1417 process_fmt(act, pci, t, log_track_insert(pdi, t), 0, NULL);
1420 static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t,
1423 process_fmt(act, pci, t, -1, 0, NULL);
1426 static void log_issue(struct per_dev_info *pdi, struct per_cpu_info *pci,
1427 struct blk_io_trace *t, char *act)
1429 process_fmt(act, pci, t, log_track_issue(pdi, t), 0, NULL);
1432 static void log_merge(struct per_dev_info *pdi, struct per_cpu_info *pci,
1433 struct blk_io_trace *t, char *act)
1436 log_track_frontmerge(pdi, t);
1438 process_fmt(act, pci, t, -1ULL, 0, NULL);
1441 static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t,
1444 process_fmt(act, pci, t, -1ULL, 0, NULL);
1447 static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t,
1450 process_fmt(act, pci, t, -1ULL, 0, NULL);
1453 static void log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t,
1456 process_fmt(act, pci, t, -1ULL, 0, NULL);
1459 static void log_split(struct per_cpu_info *pci, struct blk_io_trace *t,
1462 process_fmt(act, pci, t, -1ULL, 0, NULL);
1465 static void log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act)
1467 unsigned char *buf = (unsigned char *) t + sizeof(*t);
1469 process_fmt(act, pci, t, -1ULL, t->pdu_len, buf);
1472 static void dump_trace_pc(struct blk_io_trace *t, struct per_dev_info *pdi,
1473 struct per_cpu_info *pci)
1475 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
1476 int act = t->action & 0xffff;
1479 case __BLK_TA_QUEUE:
1480 log_generic(pci, t, "Q");
1481 account_pc_queue(t, pci, w);
1483 case __BLK_TA_GETRQ:
1484 log_generic(pci, t, "G");
1486 case __BLK_TA_SLEEPRQ:
1487 log_generic(pci, t, "S");
1489 case __BLK_TA_REQUEUE:
1491 * can happen if we miss traces, don't let it go
1494 if (pdi->cur_depth[w])
1495 pdi->cur_depth[w]--;
1496 account_pc_requeue(t, pci, w);
1497 log_generic(pci, t, "R");
1499 case __BLK_TA_ISSUE:
1500 account_pc_issue(t, pci, w);
1501 pdi->cur_depth[w]++;
1502 if (pdi->cur_depth[w] > pdi->max_depth[w])
1503 pdi->max_depth[w] = pdi->cur_depth[w];
1504 log_pc(pci, t, "D");
1506 case __BLK_TA_COMPLETE:
1507 if (pdi->cur_depth[w])
1508 pdi->cur_depth[w]--;
1509 log_pc(pci, t, "C");
1510 account_pc_c(t, pci, w);
1512 case __BLK_TA_INSERT:
1513 log_pc(pci, t, "I");
1516 fprintf(stderr, "Bad pc action %x\n", act);
1521 static void dump_trace_fs(struct blk_io_trace *t, struct per_dev_info *pdi,
1522 struct per_cpu_info *pci)
1524 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
1525 int act = t->action & 0xffff;
1528 case __BLK_TA_QUEUE:
1529 log_track_queue(pdi, t);
1530 account_queue(t, pci, w);
1531 log_queue(pci, t, "Q");
1533 case __BLK_TA_INSERT:
1534 log_insert(pdi, pci, t, "I");
1536 case __BLK_TA_BACKMERGE:
1537 account_m(t, pci, w);
1538 log_merge(pdi, pci, t, "M");
1540 case __BLK_TA_FRONTMERGE:
1541 account_m(t, pci, w);
1542 log_merge(pdi, pci, t, "F");
1544 case __BLK_TA_GETRQ:
1545 log_track_getrq(pdi, t);
1546 log_generic(pci, t, "G");
1548 case __BLK_TA_SLEEPRQ:
1549 log_generic(pci, t, "S");
1551 case __BLK_TA_REQUEUE:
1553 * can happen if we miss traces, don't let it go
1556 if (pdi->cur_depth[w])
1557 pdi->cur_depth[w]--;
1558 account_requeue(t, pci, w);
1559 log_queue(pci, t, "R");
1561 case __BLK_TA_ISSUE:
1562 account_issue(t, pci, w);
1563 pdi->cur_depth[w]++;
1564 if (pdi->cur_depth[w] > pdi->max_depth[w])
1565 pdi->max_depth[w] = pdi->cur_depth[w];
1566 log_issue(pdi, pci, t, "D");
1568 case __BLK_TA_COMPLETE:
1569 if (pdi->cur_depth[w])
1570 pdi->cur_depth[w]--;
1571 account_c(t, pci, w, t->bytes);
1572 log_complete(pdi, pci, t, "C");
1575 log_action(pci, t, "P");
1577 case __BLK_TA_UNPLUG_IO:
1578 account_unplug(t, pci, 0);
1579 log_unplug(pci, t, "U");
1581 case __BLK_TA_UNPLUG_TIMER:
1582 account_unplug(t, pci, 1);
1583 log_unplug(pci, t, "UT");
1585 case __BLK_TA_SPLIT:
1586 log_split(pci, t, "X");
1588 case __BLK_TA_BOUNCE:
1589 log_generic(pci, t, "B");
1591 case __BLK_TA_REMAP:
1592 log_generic(pci, t, "A");
1594 case __BLK_TA_DRV_DATA:
1596 /* dump to binary file only */
1599 fprintf(stderr, "Bad fs action %x\n", t->action);
1604 static void dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
1605 struct per_dev_info *pdi)
1608 if (t->action == BLK_TN_MESSAGE)
1610 else if (t->action & BLK_TC_ACT(BLK_TC_PC))
1611 dump_trace_pc(t, pdi, pci);
1613 dump_trace_fs(t, pdi, pci);
1617 pdi->first_reported_time = t->time;
1621 if (bin_output_msgs ||
1622 !(t->action & BLK_TC_ACT(BLK_TC_NOTIFY) &&
1623 t->action == BLK_TN_MESSAGE))
1624 output_binary(t, sizeof(*t) + t->pdu_len);
1628 * print in a proper way, not too small and not too big. if more than
1629 * 1000,000K, turn into M and so on
1631 static char *size_cnv(char *dst, unsigned long long num, int in_kb)
1633 char suff[] = { '\0', 'K', 'M', 'G', 'P' };
1639 while (num > 1000 * 1000ULL && (i < sizeof(suff) - 1)) {
1644 sprintf(dst, "%'8Lu%c", num, suff[i]);
1648 static void dump_io_stats(struct per_dev_info *pdi, struct io_stats *ios,
1651 static char x[256], y[256];
1653 fprintf(ofp, "%s\n", msg);
1655 fprintf(ofp, " Reads Queued: %s, %siB\t", size_cnv(x, ios->qreads, 0), size_cnv(y, ios->qread_kb, 1));
1656 fprintf(ofp, " Writes Queued: %s, %siB\n", size_cnv(x, ios->qwrites, 0), size_cnv(y, ios->qwrite_kb, 1));
1657 fprintf(ofp, " Read Dispatches: %s, %siB\t", size_cnv(x, ios->ireads, 0), size_cnv(y, ios->iread_kb, 1));
1658 fprintf(ofp, " Write Dispatches: %s, %siB\n", size_cnv(x, ios->iwrites, 0), size_cnv(y, ios->iwrite_kb, 1));
1659 fprintf(ofp, " Reads Requeued: %s\t\t", size_cnv(x, ios->rrqueue, 0));
1660 fprintf(ofp, " Writes Requeued: %s\n", size_cnv(x, ios->wrqueue, 0));
1661 fprintf(ofp, " Reads Completed: %s, %siB\t", size_cnv(x, ios->creads, 0), size_cnv(y, ios->cread_kb, 1));
1662 fprintf(ofp, " Writes Completed: %s, %siB\n", size_cnv(x, ios->cwrites, 0), size_cnv(y, ios->cwrite_kb, 1));
1663 fprintf(ofp, " Read Merges: %s, %siB\t", size_cnv(x, ios->mreads, 0), size_cnv(y, ios->mread_kb, 1));
1664 fprintf(ofp, " Write Merges: %s, %siB\n", size_cnv(x, ios->mwrites, 0), size_cnv(y, ios->mwrite_kb, 1));
1666 fprintf(ofp, " Read depth: %'8u%8c\t", pdi->max_depth[0], ' ');
1667 fprintf(ofp, " Write depth: %'8u\n", pdi->max_depth[1]);
1669 if (ios->qreads_pc || ios->qwrites_pc || ios->ireads_pc || ios->iwrites_pc ||
1670 ios->rrqueue_pc || ios->wrqueue_pc || ios->creads_pc || ios->cwrites_pc) {
1671 fprintf(ofp, " PC Reads Queued: %s, %siB\t", size_cnv(x, ios->qreads_pc, 0), size_cnv(y, ios->qread_kb_pc, 1));
1672 fprintf(ofp, " PC Writes Queued: %s, %siB\n", size_cnv(x, ios->qwrites_pc, 0), size_cnv(y, ios->qwrite_kb_pc, 1));
1673 fprintf(ofp, " PC Read Disp.: %s, %siB\t", size_cnv(x, ios->ireads_pc, 0), size_cnv(y, ios->iread_kb_pc, 1));
1674 fprintf(ofp, " PC Write Disp.: %s, %siB\n", size_cnv(x, ios->iwrites_pc, 0), size_cnv(y, ios->iwrite_kb_pc, 1));
1675 fprintf(ofp, " PC Reads Req.: %s\t\t", size_cnv(x, ios->rrqueue_pc, 0));
1676 fprintf(ofp, " PC Writes Req.: %s\n", size_cnv(x, ios->wrqueue_pc, 0));
1677 fprintf(ofp, " PC Reads Compl.: %s\t\t", size_cnv(x, ios->creads_pc, 0));
1678 fprintf(ofp, " PC Writes Compl.: %s\n", size_cnv(x, ios->cwrites, 0));
1680 fprintf(ofp, " IO unplugs: %'8lu%8c\t", ios->io_unplugs, ' ');
1681 fprintf(ofp, " Timer unplugs: %'8lu\n", ios->timer_unplugs);
1684 static void dump_wait_stats(struct per_process_info *ppi)
1686 unsigned long rawait = ppi->longest_allocation_wait[0] / 1000;
1687 unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000;
1688 unsigned long rcwait = ppi->longest_completion_wait[0] / 1000;
1689 unsigned long wawait = ppi->longest_allocation_wait[1] / 1000;
1690 unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000;
1691 unsigned long wcwait = ppi->longest_completion_wait[1] / 1000;
1693 fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' ');
1694 fprintf(ofp, " Allocation wait: %'8lu\n", wawait);
1695 fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' ');
1696 fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait);
1697 fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' ');
1698 fprintf(ofp, " Completion wait: %'8lu\n", wcwait);
1701 static int ppi_name_compare(const void *p1, const void *p2)
1703 struct per_process_info *ppi1 = *((struct per_process_info **) p1);
1704 struct per_process_info *ppi2 = *((struct per_process_info **) p2);
1707 res = strverscmp(ppi1->ppm->comm, ppi2->ppm->comm);
1709 res = ppi1->ppm->pid > ppi2->ppm->pid;
1714 static void sort_process_list(void)
1716 struct per_process_info **ppis;
1717 struct per_process_info *ppi;
1720 ppis = malloc(ppi_list_entries * sizeof(struct per_process_info *));
1725 ppi = ppi->list_next;
1728 qsort(ppis, ppi_list_entries, sizeof(ppi), ppi_name_compare);
1730 i = ppi_list_entries - 1;
1735 ppi->list_next = ppi_list;
1743 static void show_process_stats(void)
1745 struct per_process_info *ppi;
1747 sort_process_list();
1751 struct process_pid_map *ppm = ppi->ppm;
1754 if (ppi->more_than_one)
1755 sprintf(name, "%s (%u, ...)", ppm->comm, ppm->pid);
1757 sprintf(name, "%s (%u)", ppm->comm, ppm->pid);
1759 dump_io_stats(NULL, &ppi->io_stats, name);
1760 dump_wait_stats(ppi);
1761 ppi = ppi->list_next;
1767 static void show_device_and_cpu_stats(void)
1769 struct per_dev_info *pdi;
1770 struct per_cpu_info *pci;
1771 struct io_stats total, *ios;
1772 unsigned long long rrate, wrate, msec;
1773 int i, j, pci_events;
1774 char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3];
1778 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
1780 memset(&total, 0, sizeof(total));
1786 for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) {
1790 ios = &pci->io_stats;
1791 total.qreads += ios->qreads;
1792 total.qwrites += ios->qwrites;
1793 total.creads += ios->creads;
1794 total.cwrites += ios->cwrites;
1795 total.mreads += ios->mreads;
1796 total.mwrites += ios->mwrites;
1797 total.ireads += ios->ireads;
1798 total.iwrites += ios->iwrites;
1799 total.rrqueue += ios->rrqueue;
1800 total.wrqueue += ios->wrqueue;
1801 total.qread_kb += ios->qread_kb;
1802 total.qwrite_kb += ios->qwrite_kb;
1803 total.cread_kb += ios->cread_kb;
1804 total.cwrite_kb += ios->cwrite_kb;
1805 total.iread_kb += ios->iread_kb;
1806 total.iwrite_kb += ios->iwrite_kb;
1807 total.mread_kb += ios->mread_kb;
1808 total.mwrite_kb += ios->mwrite_kb;
1810 total.qreads_pc += ios->qreads_pc;
1811 total.qwrites_pc += ios->qwrites_pc;
1812 total.creads_pc += ios->creads_pc;
1813 total.cwrites_pc += ios->cwrites_pc;
1814 total.ireads_pc += ios->ireads_pc;
1815 total.iwrites_pc += ios->iwrites_pc;
1816 total.rrqueue_pc += ios->rrqueue_pc;
1817 total.wrqueue_pc += ios->wrqueue_pc;
1818 total.qread_kb_pc += ios->qread_kb_pc;
1819 total.qwrite_kb_pc += ios->qwrite_kb_pc;
1820 total.iread_kb_pc += ios->iread_kb_pc;
1821 total.iwrite_kb_pc += ios->iwrite_kb_pc;
1823 total.timer_unplugs += ios->timer_unplugs;
1824 total.io_unplugs += ios->io_unplugs;
1826 snprintf(line, sizeof(line) - 1, "CPU%d (%s):",
1827 j, get_dev_name(pdi, name, sizeof(name)));
1828 dump_io_stats(pdi, ios, line);
1832 if (pci_events > 1) {
1834 snprintf(line, sizeof(line) - 1, "Total (%s):",
1835 get_dev_name(pdi, name, sizeof(name)));
1836 dump_io_stats(NULL, &total, line);
1840 msec = (pdi->last_reported_time - pdi->first_reported_time) / 1000000;
1842 rrate = 1000 * total.cread_kb / msec;
1843 wrate = 1000 * total.cwrite_kb / msec;
1846 fprintf(ofp, "\nThroughput (R/W): %'LuKiB/s / %'LuKiB/s\n",
1848 fprintf(ofp, "Events (%s): %'Lu entries\n",
1849 get_dev_name(pdi, line, sizeof(line)), pdi->events);
1851 collect_pdi_skips(pdi);
1852 if (!pdi->skips && !pdi->events)
1855 ratio = 100.0 * ((double)pdi->seq_skips /
1856 (double)(pdi->events + pdi->seq_skips));
1857 fprintf(ofp, "Skips: %'lu forward (%'llu - %5.1lf%%)\n",
1858 pdi->skips, pdi->seq_skips, ratio);
1862 static void find_genesis(void)
1864 struct trace *t = trace_list;
1866 genesis_time = -1ULL;
1868 if (t->bit->time < genesis_time)
1869 genesis_time = t->bit->time;
1874 /* The time stamp record will usually be the first
1875 * record in the trace, but not always.
1878 && start_timestamp != genesis_time) {
1879 long delta = genesis_time - start_timestamp;
1881 abs_start_time.tv_sec += SECONDS(delta);
1882 abs_start_time.tv_nsec += NANO_SECONDS(delta);
1883 if (abs_start_time.tv_nsec < 0) {
1884 abs_start_time.tv_nsec += 1000000000;
1885 abs_start_time.tv_sec -= 1;
1887 if (abs_start_time.tv_nsec > 1000000000) {
1888 abs_start_time.tv_nsec -= 1000000000;
1889 abs_start_time.tv_sec += 1;
1894 static inline int check_stopwatch(struct blk_io_trace *bit)
1896 if (bit->time < stopwatch_end &&
1897 bit->time >= stopwatch_start)
1904 * return youngest entry read
1906 static int sort_entries(unsigned long long *youngest)
1908 struct per_dev_info *pdi = NULL;
1909 struct per_cpu_info *pci = NULL;
1916 while ((t = trace_list) != NULL) {
1917 struct blk_io_trace *bit = t->bit;
1919 trace_list = t->next;
1921 bit->time -= genesis_time;
1923 if (bit->time < *youngest || !*youngest)
1924 *youngest = bit->time;
1926 if (!pdi || pdi->dev != bit->device) {
1927 pdi = get_dev_info(bit->device);
1931 if (!pci || pci->cpu != bit->cpu)
1932 pci = get_cpu_info(pdi, bit->cpu);
1934 if (bit->sequence < pci->smallest_seq_read)
1935 pci->smallest_seq_read = bit->sequence;
1937 if (check_stopwatch(bit)) {
1943 if (trace_rb_insert_sort(t))
1951 * to continue, we must have traces from all online cpus in the tree
1953 static int check_cpu_map(struct per_dev_info *pdi)
1955 unsigned long *cpu_map;
1962 * create a map of the cpus we have traces for
1964 cpu_map = malloc(pdi->cpu_map_max / sizeof(long));
1965 n = rb_first(&rb_sort_root);
1967 __t = rb_entry(n, struct trace, rb_node);
1968 cpu = __t->bit->cpu;
1970 cpu_map[CPU_IDX(cpu)] |= (1UL << CPU_BIT(cpu));
1975 * we can't continue if pdi->cpu_map has entries set that we don't
1976 * have in the sort rbtree. the opposite is not a problem, though
1979 for (i = 0; i < pdi->cpu_map_max / CPUS_PER_LONG; i++) {
1980 if (pdi->cpu_map[i] & ~(cpu_map[i])) {
1990 static int check_sequence(struct per_dev_info *pdi, struct trace *t, int force)
1992 struct blk_io_trace *bit = t->bit;
1993 unsigned long expected_sequence;
1994 struct per_cpu_info *pci;
1997 pci = get_cpu_info(pdi, bit->cpu);
1998 expected_sequence = pci->last_sequence + 1;
2000 if (!expected_sequence) {
2002 * 1 should be the first entry, just allow it
2004 if (bit->sequence == 1)
2006 if (bit->sequence == pci->smallest_seq_read)
2009 return check_cpu_map(pdi);
2012 if (bit->sequence == expected_sequence)
2016 * we may not have seen that sequence yet. if we are not doing
2017 * the final run, break and wait for more entries.
2019 if (expected_sequence < pci->smallest_seq_read) {
2020 __t = trace_rb_find_last(pdi, pci, expected_sequence);
2024 __put_trace_last(pdi, __t);
2026 } else if (!force) {
2030 if (check_current_skips(pci, bit->sequence))
2033 if (expected_sequence < bit->sequence)
2034 insert_skip(pci, expected_sequence, bit->sequence - 1);
2039 static void show_entries_rb(int force)
2041 struct per_dev_info *pdi = NULL;
2042 struct per_cpu_info *pci = NULL;
2043 struct blk_io_trace *bit;
2047 while ((n = rb_first(&rb_sort_root)) != NULL) {
2048 if (is_done() && !force && !pipeline)
2051 t = rb_entry(n, struct trace, rb_node);
2054 if (read_sequence - t->read_sequence < 1 && !force)
2057 if (!pdi || pdi->dev != bit->device) {
2058 pdi = get_dev_info(bit->device);
2063 fprintf(stderr, "Unknown device ID? (%d,%d)\n",
2064 MAJOR(bit->device), MINOR(bit->device));
2068 if (check_sequence(pdi, t, force))
2071 if (!force && bit->time > last_allowed_time)
2074 check_time(pdi, bit);
2076 if (!pci || pci->cpu != bit->cpu)
2077 pci = get_cpu_info(pdi, bit->cpu);
2079 pci->last_sequence = bit->sequence;
2083 if (bit->action & (act_mask << BLK_TC_SHIFT))
2084 dump_trace(bit, pci, pdi);
2090 static int read_data(int fd, void *buffer, int bytes, int block, int *fdblock)
2092 int ret, bytes_left, fl;
2095 if (block != *fdblock) {
2096 fl = fcntl(fd, F_GETFL);
2100 fcntl(fd, F_SETFL, fl | O_NONBLOCK);
2103 fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
2109 while (bytes_left > 0) {
2110 ret = read(fd, p, bytes_left);
2114 if (errno != EAGAIN) {
2120 * never do partial reads. we can return if we
2121 * didn't read anything and we should not block,
2122 * otherwise wait for data
2124 if ((bytes_left == bytes) && !block)
2138 static inline __u16 get_pdulen(struct blk_io_trace *bit)
2141 return bit->pdu_len;
2143 return __bswap_16(bit->pdu_len);
2146 static inline __u32 get_magic(struct blk_io_trace *bit)
2151 return __bswap_32(bit->magic);
2154 static int read_events(int fd, int always_block, int *fdblock)
2156 struct per_dev_info *pdi = NULL;
2157 unsigned int events = 0;
2159 while (!is_done() && events < rb_batch) {
2160 struct blk_io_trace *bit;
2162 int pdu_len, should_block, ret;
2167 should_block = !events || always_block;
2169 ret = read_data(fd, bit, sizeof(*bit), should_block, fdblock);
2172 if (!events && ret < 0)
2178 * look at first trace to check whether we need to convert
2179 * data in the future
2181 if (data_is_native == -1 && check_data_endianness(bit->magic))
2184 magic = get_magic(bit);
2185 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
2186 fprintf(stderr, "Bad magic %x\n", magic);
2190 pdu_len = get_pdulen(bit);
2192 void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
2194 if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1, fdblock)) {
2204 if (verify_trace(bit)) {
2210 * not a real trace, so grab and handle it here
2212 if (bit->action & BLK_TC_ACT(BLK_TC_NOTIFY) && bit->action != BLK_TN_MESSAGE) {
2214 output_binary(bit, sizeof(*bit) + bit->pdu_len);
2219 memset(t, 0, sizeof(*t));
2221 t->read_sequence = read_sequence;
2223 t->next = trace_list;
2226 if (!pdi || pdi->dev != bit->device)
2227 pdi = get_dev_info(bit->device);
2229 if (bit->time > pdi->last_read_time)
2230 pdi->last_read_time = bit->time;
2239 * Managing input streams
2243 struct ms_stream *next;
2244 struct trace *first, *last;
2245 struct per_dev_info *pdi;
2249 #define MS_HASH(d, c) ((MAJOR(d) & 0xff) ^ (MINOR(d) & 0xff) ^ (cpu & 0xff))
2251 struct ms_stream *ms_head;
2252 struct ms_stream *ms_hash[256];
2254 static void ms_sort(struct ms_stream *msp);
2255 static int ms_prime(struct ms_stream *msp);
2257 static inline struct trace *ms_peek(struct ms_stream *msp)
2259 return (msp == NULL) ? NULL : msp->first;
2262 static inline __u64 ms_peek_time(struct ms_stream *msp)
2264 return ms_peek(msp)->bit->time;
2267 static inline void ms_resort(struct ms_stream *msp)
2269 if (msp->next && ms_peek_time(msp) > ms_peek_time(msp->next)) {
2270 ms_head = msp->next;
2276 static inline void ms_deq(struct ms_stream *msp)
2278 msp->first = msp->first->next;
2281 if (!ms_prime(msp)) {
2282 ms_head = msp->next;
2291 static void ms_sort(struct ms_stream *msp)
2293 __u64 msp_t = ms_peek_time(msp);
2294 struct ms_stream *this_msp = ms_head;
2296 if (this_msp == NULL)
2298 else if (msp_t < ms_peek_time(this_msp)) {
2299 msp->next = this_msp;
2303 while (this_msp->next && ms_peek_time(this_msp->next) < msp_t)
2304 this_msp = this_msp->next;
2306 msp->next = this_msp->next;
2307 this_msp->next = msp;
2311 static int ms_prime(struct ms_stream *msp)
2316 struct per_dev_info *pdi = msp->pdi;
2317 struct per_cpu_info *pci = get_cpu_info(pdi, msp->cpu);
2318 struct blk_io_trace *bit = NULL;
2319 int ret, pdu_len, ndone = 0;
2321 for (i = 0; !is_done() && pci->fd >= 0 && i < rb_batch; i++) {
2323 ret = read_data(pci->fd, bit, sizeof(*bit), 1, &pci->fdblock);
2327 if (data_is_native == -1 && check_data_endianness(bit->magic))
2330 magic = get_magic(bit);
2331 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
2332 fprintf(stderr, "Bad magic %x\n", magic);
2337 pdu_len = get_pdulen(bit);
2339 void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
2340 ret = read_data(pci->fd, ptr + sizeof(*bit), pdu_len,
2352 if (verify_trace(bit))
2355 if (bit->action & BLK_TC_ACT(BLK_TC_NOTIFY) && bit->action != BLK_TN_MESSAGE) {
2357 output_binary(bit, sizeof(*bit) + bit->pdu_len);
2364 if (bit->time > pdi->last_read_time)
2365 pdi->last_read_time = bit->time;
2368 memset(t, 0, sizeof(*t));
2371 if (msp->first == NULL)
2372 msp->first = msp->last = t;
2374 msp->last->next = t;
2384 if (bit) bit_free(bit);
2386 cpu_mark_offline(pdi, pci->cpu);
2393 static struct ms_stream *ms_alloc(struct per_dev_info *pdi, int cpu)
2395 struct ms_stream *msp = malloc(sizeof(*msp));
2398 msp->first = msp->last = NULL;
2408 static int setup_file(struct per_dev_info *pdi, int cpu)
2413 struct per_cpu_info *pci = get_cpu_info(pdi, cpu);
2418 p = strdup(pdi->name);
2420 if (strcmp(dname, ".")) {
2422 p = strdup(pdi->name);
2423 strcpy(pdi->name, basename(p));
2428 len = sprintf(pci->fname, "%s/", input_dir);
2430 snprintf(pci->fname + len, sizeof(pci->fname)-1-len,
2431 "%s.blktrace.%d", pdi->name, pci->cpu);
2432 if (stat(pci->fname, &st) < 0)
2437 pci->fd = open(pci->fname, O_RDONLY);
2443 printf("Input file %s added\n", pci->fname);
2444 cpu_mark_online(pdi, pci->cpu);
2447 ms_alloc(pdi, pci->cpu);
2452 static int handle(struct ms_stream *msp)
2455 struct per_dev_info *pdi;
2456 struct per_cpu_info *pci;
2457 struct blk_io_trace *bit;
2463 pci = get_cpu_info(pdi, msp->cpu);
2465 bit->time -= genesis_time;
2467 if (t->bit->time > stopwatch_end)
2470 pdi->last_reported_time = bit->time;
2471 if ((bit->action & (act_mask << BLK_TC_SHIFT))&&
2472 t->bit->time >= stopwatch_start)
2473 dump_trace(bit, pci, pdi);
2478 trace_rb_insert_last(pdi, t);
2488 * Check if we need to sanitize the name. We allow 'foo', or if foo.blktrace.X
2489 * is given, then strip back down to 'foo' to avoid missing files.
2491 static int name_fixup(char *name)
2498 b = strstr(name, ".blktrace.");
2505 static int do_file(void)
2508 struct per_dev_info *pdi;
2511 * first prepare all files for reading
2513 for (i = 0; i < ndevices; i++) {
2515 ret = name_fixup(pdi->name);
2519 for (cpu = 0; setup_file(pdi, cpu); cpu++)
2524 * Get the initial time stamp
2527 genesis_time = ms_peek_time(ms_head);
2530 * Keep processing traces while any are left
2532 while (!is_done() && ms_head && handle(ms_head))
2538 static void do_pipe(int fd)
2540 unsigned long long youngest;
2541 int events, fdblock;
2543 last_allowed_time = -1ULL;
2545 while ((events = read_events(fd, 0, &fdblock)) > 0) {
2549 smallest_seq_read = -1U;
2552 if (sort_entries(&youngest))
2555 if (youngest > stopwatch_end)
2561 if (rb_sort_entries)
2565 static int do_fifo(void)
2569 if (!strcmp(pipename, "-"))
2570 fd = dup(STDIN_FILENO);
2572 fd = open(pipename, O_RDONLY);
2575 perror("dup stdin");
2584 static void show_stats(void)
2593 if (per_process_stats)
2594 show_process_stats();
2596 if (per_device_and_cpu_stats)
2597 show_device_and_cpu_stats();
2602 static void handle_sigint(__attribute__((__unused__)) int sig)
2608 * Extract start and duration times from a string, allowing
2609 * us to specify a time interval of interest within a trace.
2610 * Format: "duration" (start is zero) or "start:duration".
2612 static int find_stopwatch_interval(char *string)
2617 value = strtod(string, &sp);
2619 fprintf(stderr,"Invalid stopwatch timer: %s\n", string);
2623 stopwatch_start = DOUBLE_TO_NANO_ULL(value);
2625 value = strtod(string, &sp);
2626 if (sp == string || *sp != '\0') {
2627 fprintf(stderr,"Invalid stopwatch duration time: %s\n",
2631 } else if (*sp != '\0') {
2632 fprintf(stderr,"Invalid stopwatch start timer: %s\n", string);
2635 stopwatch_end = DOUBLE_TO_NANO_ULL(value);
2636 if (stopwatch_end <= stopwatch_start) {
2637 fprintf(stderr, "Invalid stopwatch interval: %Lu -> %Lu\n",
2638 stopwatch_start, stopwatch_end);
2645 static int is_pipe(const char *str)
2649 if (!strcmp(str, "-"))
2651 if (!stat(str, &st) && S_ISFIFO(st.st_mode))
2657 #define S_OPTS "a:A:b:D:d:f:F:hi:o:Oqstw:vVM"
2658 static char usage_str[] = "\n\n" \
2659 "-i <file> | --input=<file>\n" \
2660 "[ -a <action field> | --act-mask=<action field> ]\n" \
2661 "[ -A <action mask> | --set-mask=<action mask> ]\n" \
2662 "[ -b <traces> | --batch=<traces> ]\n" \
2663 "[ -d <file> | --dump-binary=<file> ]\n" \
2664 "[ -D <dir> | --input-directory=<dir> ]\n" \
2665 "[ -f <format> | --format=<format> ]\n" \
2666 "[ -F <spec> | --format-spec=<spec> ]\n" \
2667 "[ -h | --hash-by-name ]\n" \
2668 "[ -o <file> | --output=<file> ]\n" \
2669 "[ -O | --no-text-output ]\n" \
2670 "[ -q | --quiet ]\n" \
2671 "[ -s | --per-program-stats ]\n" \
2672 "[ -t | --track-ios ]\n" \
2673 "[ -w <time> | --stopwatch=<time> ]\n" \
2674 "[ -M | --no-msgs\n" \
2675 "[ -v | --verbose ]\n" \
2676 "[ -V | --version ]\n\n" \
2677 "\t-b stdin read batching\n" \
2678 "\t-d Output file. If specified, binary data is written to file\n" \
2679 "\t-D Directory to prepend to input file names\n" \
2680 "\t-f Output format. Customize the output format. The format field\n" \
2681 "\t identifies can be found in the documentation\n" \
2682 "\t-F Format specification. Can be found in the documentation\n" \
2683 "\t-h Hash processes by name, not pid\n" \
2684 "\t-i Input file containing trace data, or '-' for stdin\n" \
2685 "\t-o Output file. If not given, output is stdout\n" \
2686 "\t-O Do NOT output text data\n" \
2687 "\t-q Quiet. Don't display any stats at the end of the trace\n" \
2688 "\t-s Show per-program io statistics\n" \
2689 "\t-t Track individual ios. Will tell you the time a request took\n" \
2690 "\t to get queued, to get dispatched, and to get completed\n" \
2691 "\t-w Only parse data between the given time interval in seconds.\n" \
2692 "\t If 'start' isn't given, blkparse defaults the start time to 0\n" \
2693 "\t-M Do not output messages to binary file\n" \
2694 "\t-v More verbose for marginal errors\n" \
2695 "\t-V Print program version info\n\n";
2697 static void usage(char *prog)
2699 fprintf(stderr, "Usage: %s %s %s", prog, blkparse_version, usage_str);
2702 int main(int argc, char *argv[])
2704 int i, c, ret, mode;
2705 int act_mask_tmp = 0;
2706 char *ofp_buffer = NULL;
2707 char *bin_ofp_buffer = NULL;
2709 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
2712 i = find_mask_map(optarg);
2714 fprintf(stderr,"Invalid action mask %s\n",
2722 if ((sscanf(optarg, "%x", &i) != 1) ||
2723 !valid_act_opt(i)) {
2725 "Invalid set action mask %s/0x%x\n",
2732 if (is_pipe(optarg) && !pipeline) {
2734 pipename = strdup(optarg);
2735 } else if (resize_devices(optarg) != 0)
2742 output_name = optarg;
2748 rb_batch = atoi(optarg);
2750 rb_batch = RB_BATCH_DEFAULT;
2753 per_process_stats = 1;
2759 per_device_and_cpu_stats = 0;
2762 if (find_stopwatch_interval(optarg) != 0)
2766 set_all_format_specs(optarg);
2769 if (add_format_spec(optarg) != 0)
2773 ppi_hash_by_pid = 0;
2779 printf("%s version %s\n", argv[0], blkparse_version);
2782 dump_binary = optarg;
2785 bin_output_msgs = 0;
2793 while (optind < argc) {
2794 if (is_pipe(argv[optind]) && !pipeline) {
2796 pipename = strdup(argv[optind]);
2797 } else if (resize_devices(argv[optind]) != 0)
2802 if (!pipeline && !ndevices) {
2807 if (act_mask_tmp != 0)
2808 act_mask = act_mask_tmp;
2810 memset(&rb_sort_root, 0, sizeof(rb_sort_root));
2812 signal(SIGINT, handle_sigint);
2813 signal(SIGHUP, handle_sigint);
2814 signal(SIGTERM, handle_sigint);
2816 setlocale(LC_NUMERIC, "en_US");
2820 ofp = fdopen(STDOUT_FILENO, "w");
2825 snprintf(ofname, sizeof(ofname) - 1, "%s", output_name);
2826 ofp = fopen(ofname, "w");
2835 ofp_buffer = malloc(4096);
2836 if (setvbuf(ofp, ofp_buffer, mode, 4096)) {
2843 dump_fp = fopen(dump_binary, "w");
2845 perror(dump_binary);
2849 bin_ofp_buffer = malloc(128 * 1024);
2850 if (setvbuf(dump_fp, bin_ofp_buffer, _IOFBF, 128 * 1024)) {
2851 perror("setvbuf binary");
2864 if (have_drv_data && !dump_binary)
2865 printf("\ndiscarded traces containing low-level device driver "
2866 "specific data (only available in binary output)\n");
2872 if (bin_ofp_buffer) {
2874 free(bin_ofp_buffer);