[PATCH] kernel: update to work with latest Linus -git tree
[blktrace.git] / blkparse.c
CommitLineData
d956a2cd
JA
1/*
2 * block queue tracing parse application
3 *
4 * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
5 *
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.
10 *
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.
15 *
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
19 *
20 */
d0ca268b
JA
21#include <sys/types.h>
22#include <sys/stat.h>
23#include <unistd.h>
24#include <stdio.h>
25#include <fcntl.h>
26#include <stdlib.h>
8fc0abbc 27#include <string.h>
d5396421 28#include <getopt.h>
412819ce
JA
29#include <errno.h>
30#include <signal.h>
d69db225 31#include <locale.h>
6e0073ed 32#include <libgen.h>
d0ca268b 33
8fc0abbc
JA
34#include "blktrace.h"
35#include "rbtree.h"
bf0720af 36#include "jhash.h"
d0ca268b 37
13d928f0 38static char blkparse_version[] = "0.99";
52724a0e 39
492da111
AB
40struct skip_info {
41 unsigned long start, end;
42 struct skip_info *prev, *next;
43};
44
e7c9f3ff 45struct per_dev_info {
f7bd1a9b 46 dev_t dev;
e7c9f3ff
NS
47 char *name;
48
49 int backwards;
50 unsigned long long events;
20ed6177 51 unsigned long long first_reported_time;
e7c9f3ff 52 unsigned long long last_reported_time;
287fa3d6 53 unsigned long long last_read_time;
e7c9f3ff 54 struct io_stats io_stats;
dce0f678
AB
55 unsigned long skips, nskips;
56 unsigned long long seq_skips, seq_nskips;
649c7b66
JA
57 unsigned int max_depth[2];
58 unsigned int cur_depth[2];
e7c9f3ff 59
f7bd1a9b
JA
60 struct rb_root rb_track;
61
73877e12 62 int nfiles;
e7c9f3ff 63 int ncpus;
824c2b39
JA
64
65 unsigned long *cpu_map;
66 unsigned int cpu_map_max;
67
e7c9f3ff
NS
68 struct per_cpu_info *cpus;
69};
70
152f6476
JA
71struct per_process_info {
72 char name[16];
73 __u32 pid;
74 struct io_stats io_stats;
75 struct per_process_info *hash_next, *list_next;
715d8021 76 int more_than_one;
50adc0ba
JA
77
78 /*
79 * individual io stats
80 */
b9d40d6f
JA
81 unsigned long long longest_allocation_wait[2];
82 unsigned long long longest_dispatch_wait[2];
83 unsigned long long longest_completion_wait[2];
d0ca268b
JA
84};
85
152f6476 86#define PPI_HASH_SHIFT (8)
bf0720af
JA
87#define PPI_HASH_SIZE (1 << PPI_HASH_SHIFT)
88#define PPI_HASH_MASK (PPI_HASH_SIZE - 1)
89static struct per_process_info *ppi_hash_table[PPI_HASH_SIZE];
152f6476 90static struct per_process_info *ppi_list;
886ecf0e 91static int ppi_list_entries;
152f6476 92
bfc70ad5
JA
93/*
94 * some duplicated effort here, we can unify this hash and the ppi hash later
95 */
96struct process_pid_map {
97 pid_t pid;
98 char comm[16];
99 struct process_pid_map *hash_next, *list_next;
100};
101
102#define PPM_HASH_SHIFT (8)
103#define PPM_HASH_SIZE (1 << PPM_HASH_SHIFT)
104#define PPM_HASH_MASK (PPM_HASH_SIZE - 1)
105static struct process_pid_map *ppm_hash_table[PPI_HASH_SIZE];
106
d915dee6 107#define S_OPTS "a:A:i:o:b:stqw:f:F:vVhD:"
d5396421 108static struct option l_opts[] = {
98f8386b
AB
109 {
110 .name = "act-mask",
111 .has_arg = required_argument,
112 .flag = NULL,
113 .val = 'a'
114 },
115 {
116 .name = "set-mask",
117 .has_arg = required_argument,
118 .flag = NULL,
119 .val = 'A'
120 },
d5396421
JA
121 {
122 .name = "input",
428683db 123 .has_arg = required_argument,
d5396421
JA
124 .flag = NULL,
125 .val = 'i'
126 },
127 {
128 .name = "output",
428683db 129 .has_arg = required_argument,
d5396421
JA
130 .flag = NULL,
131 .val = 'o'
132 },
79f19470
JA
133 {
134 .name = "batch",
428683db 135 .has_arg = required_argument,
79f19470
JA
136 .flag = NULL,
137 .val = 'b'
138 },
152f6476 139 {
3f65c585 140 .name = "per-program-stats",
428683db 141 .has_arg = no_argument,
152f6476
JA
142 .flag = NULL,
143 .val = 's'
144 },
7997c5b0 145 {
3f65c585 146 .name = "track-ios",
428683db 147 .has_arg = no_argument,
7997c5b0
JA
148 .flag = NULL,
149 .val = 't'
150 },
1e1c60f1
NS
151 {
152 .name = "quiet",
428683db 153 .has_arg = no_argument,
1e1c60f1
NS
154 .flag = NULL,
155 .val = 'q'
156 },
46e6968b
NS
157 {
158 .name = "stopwatch",
428683db 159 .has_arg = required_argument,
46e6968b
NS
160 .flag = NULL,
161 .val = 'w'
162 },
ab197ca7
AB
163 {
164 .name = "format",
428683db 165 .has_arg = required_argument,
ab197ca7
AB
166 .flag = NULL,
167 .val = 'f'
168 },
169 {
170 .name = "format-spec",
428683db 171 .has_arg = required_argument,
ab197ca7
AB
172 .flag = NULL,
173 .val = 'F'
174 },
bf0720af 175 {
3f65c585 176 .name = "hash-by-name",
bf0720af
JA
177 .has_arg = no_argument,
178 .flag = NULL,
d915dee6 179 .val = 'h'
bf0720af 180 },
7d1c0411 181 {
57ea8602 182 .name = "verbose",
7d1c0411
JA
183 .has_arg = no_argument,
184 .flag = NULL,
57ea8602 185 .val = 'v'
7d1c0411 186 },
52724a0e
JA
187 {
188 .name = "version",
189 .has_arg = no_argument,
190 .flag = NULL,
57ea8602 191 .val = 'V'
52724a0e 192 },
d1d7f15f 193 {
3f65c585 194 .name = "input-directory",
d1d7f15f
JA
195 .has_arg = required_argument,
196 .flag = NULL,
197 .val = 'D'
198 },
71ef8b7c
JA
199 {
200 .name = NULL,
201 }
d5396421
JA
202};
203
7997c5b0
JA
204/*
205 * for sorting the displayed output
206 */
8fc0abbc
JA
207struct trace {
208 struct blk_io_trace *bit;
209 struct rb_node rb_node;
cb2a1a62 210 struct trace *next;
a43c1c17 211 unsigned long read_sequence;
8fc0abbc
JA
212};
213
cb2a1a62 214static struct rb_root rb_sort_root;
a649216c
JA
215static unsigned long rb_sort_entries;
216
cb2a1a62
JA
217static struct trace *trace_list;
218
d36421e4
JA
219/*
220 * allocation cache
221 */
222static struct blk_io_trace *bit_alloc_list;
223static struct trace *t_alloc_list;
224
7997c5b0
JA
225/*
226 * for tracking individual ios
227 */
228struct io_track {
229 struct rb_node rb_node;
230
231 __u64 sector;
232 __u32 pid;
bf0720af 233 char comm[16];
95c15013 234 unsigned long long allocation_time;
7997c5b0
JA
235 unsigned long long queue_time;
236 unsigned long long dispatch_time;
237 unsigned long long completion_time;
238};
239
e7c9f3ff
NS
240static int ndevices;
241static struct per_dev_info *devices;
242static char *get_dev_name(struct per_dev_info *, char *, int);
210824c3 243static int trace_rb_insert_last(struct per_dev_info *, struct trace *);
d0ca268b 244
71d5d4c9 245FILE *ofp = NULL;
e7c9f3ff 246static char *output_name;
d1d7f15f 247static char *input_dir;
e7c9f3ff
NS
248
249static unsigned long long genesis_time;
287fa3d6 250static unsigned long long last_allowed_time;
46e6968b 251static unsigned long long stopwatch_start; /* start from zero by default */
bc171579 252static unsigned long long stopwatch_end = -1ULL; /* "infinity" */
a43c1c17 253static unsigned long read_sequence;
152f6476
JA
254
255static int per_process_stats;
cbc927b6 256static int per_device_and_cpu_stats = 1;
7997c5b0 257static int track_ios;
bf0720af 258static int ppi_hash_by_pid = 1;
57ea8602 259static int verbose;
98f8386b 260static unsigned int act_mask = -1U;
cbc927b6 261static int stats_printed;
86368eb5 262int data_is_native = -1;
d0ca268b 263
1d24fc14
JA
264static unsigned int t_alloc_cache;
265static unsigned int bit_alloc_cache;
266
7d747d22 267#define RB_BATCH_DEFAULT (512)
e820abd7 268static unsigned int rb_batch = RB_BATCH_DEFAULT;
79f19470 269
e7c9f3ff
NS
270static int pipeline;
271
412819ce
JA
272#define is_done() (*(volatile int *)(&done))
273static volatile int done;
274
bf0720af
JA
275#define JHASH_RANDOM (0x3af5f2ee)
276
824c2b39
JA
277#define CPUS_PER_LONG (8 * sizeof(unsigned long))
278#define CPU_IDX(cpu) ((cpu) / CPUS_PER_LONG)
279#define CPU_BIT(cpu) ((cpu) & (CPUS_PER_LONG - 1))
280
210824c3
JA
281static void resize_cpu_info(struct per_dev_info *pdi, int cpu)
282{
283 struct per_cpu_info *cpus = pdi->cpus;
284 int ncpus = pdi->ncpus;
285 int new_count = cpu + 1;
286 int new_space, size;
287 char *new_start;
288
289 size = new_count * sizeof(struct per_cpu_info);
290 cpus = realloc(cpus, size);
291 if (!cpus) {
292 char name[20];
293 fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n",
294 get_dev_name(pdi, name, sizeof(name)), size);
295 exit(1);
296 }
297
298 new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info));
299 new_space = (new_count - ncpus) * sizeof(struct per_cpu_info);
300 memset(new_start, 0, new_space);
301
302 pdi->ncpus = new_count;
303 pdi->cpus = cpus;
304
305 for (new_count = 0; new_count < pdi->ncpus; new_count++) {
306 struct per_cpu_info *pci = &pdi->cpus[new_count];
307
308 if (!pci->fd) {
309 pci->fd = -1;
310 memset(&pci->rb_last, 0, sizeof(pci->rb_last));
311 pci->rb_last_entries = 0;
312 pci->last_sequence = -1;
313 }
314 }
315}
316
317static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu)
318{
319 struct per_cpu_info *pci;
320
321 if (cpu >= pdi->ncpus)
322 resize_cpu_info(pdi, cpu);
323
324 pci = &pdi->cpus[cpu];
325 pci->cpu = cpu;
326 return pci;
327}
328
329
330static int resize_devices(char *name)
331{
332 int size = (ndevices + 1) * sizeof(struct per_dev_info);
333
334 devices = realloc(devices, size);
335 if (!devices) {
336 fprintf(stderr, "Out of memory, device %s (%d)\n", name, size);
337 return 1;
338 }
339 memset(&devices[ndevices], 0, sizeof(struct per_dev_info));
340 devices[ndevices].name = name;
341 ndevices++;
342 return 0;
343}
344
345static struct per_dev_info *get_dev_info(dev_t dev)
346{
347 struct per_dev_info *pdi;
348 int i;
349
350 for (i = 0; i < ndevices; i++) {
351 if (!devices[i].dev)
352 devices[i].dev = dev;
353 if (devices[i].dev == dev)
354 return &devices[i];
355 }
356
357 if (resize_devices(NULL))
358 return NULL;
359
360 pdi = &devices[ndevices - 1];
361 pdi->dev = dev;
362 pdi->first_reported_time = 0;
363 pdi->last_read_time = 0;
210824c3
JA
364
365 return pdi;
366}
367
66930177 368static void insert_skip(struct per_cpu_info *pci, unsigned long start,
492da111
AB
369 unsigned long end)
370{
371 struct skip_info *sip;
372
66930177 373 for (sip = pci->skips_tail; sip != NULL; sip = sip->prev) {
492da111
AB
374 if (end == (sip->start - 1)) {
375 sip->start = start;
376 return;
377 } else if (start == (sip->end + 1)) {
378 sip->end = end;
379 return;
380 }
381 }
382
383 sip = malloc(sizeof(struct skip_info));
384 sip->start = start;
385 sip->end = end;
386 sip->prev = sip->next = NULL;
66930177
JA
387 if (pci->skips_tail == NULL)
388 pci->skips_head = pci->skips_tail = sip;
492da111 389 else {
66930177
JA
390 sip->prev = pci->skips_tail;
391 pci->skips_tail->next = sip;
392 pci->skips_tail = sip;
492da111
AB
393 }
394}
395
66930177 396static void remove_sip(struct per_cpu_info *pci, struct skip_info *sip)
492da111
AB
397{
398 if (sip->prev == NULL) {
399 if (sip->next == NULL)
66930177 400 pci->skips_head = pci->skips_tail = NULL;
492da111 401 else {
66930177 402 pci->skips_head = sip->next;
492da111
AB
403 sip->next->prev = NULL;
404 }
405 } else if (sip->next == NULL) {
66930177 406 pci->skips_tail = sip->prev;
492da111
AB
407 sip->prev->next = NULL;
408 } else {
409 sip->prev->next = sip->next;
410 sip->next->prev = sip->prev;
411 }
412
413 sip->prev = sip->next = NULL;
414 free(sip);
415}
416
417#define IN_SKIP(sip,seq) (((sip)->start <= (seq)) && ((seq) <= sip->end))
66930177 418static int check_current_skips(struct per_cpu_info *pci, unsigned long seq)
492da111
AB
419{
420 struct skip_info *sip;
421
66930177
JA
422 for (sip = pci->skips_tail; sip != NULL; sip = sip->prev) {
423 if (IN_SKIP(sip, seq)) {
492da111
AB
424 if (sip->start == seq) {
425 if (sip->end == seq)
66930177 426 remove_sip(pci, sip);
492da111
AB
427 else
428 sip->start += 1;
429 } else if (sip->end == seq)
430 sip->end -= 1;
431 else {
432 sip->end = seq - 1;
66930177 433 insert_skip(pci, seq + 1, sip->end);
492da111
AB
434 }
435 return 1;
436 }
437 }
66930177 438
492da111
AB
439 return 0;
440}
441
442static void collect_pdi_skips(struct per_dev_info *pdi)
443{
444 struct skip_info *sip;
66930177 445 int cpu;
492da111
AB
446
447 pdi->skips = 0;
448 pdi->seq_skips = 0;
66930177
JA
449
450 for (cpu = 0; cpu < pdi->ncpus; cpu++) {
451 struct per_cpu_info *pci = &pdi->cpus[cpu];
452
453 for (sip = pci->skips_head; sip != NULL; sip = sip->next) {
454 pdi->skips++;
455 pdi->seq_skips += (sip->end - sip->start + 1);
456 if (verbose)
457 fprintf(stderr,"(%d,%d): skipping %lu -> %lu\n",
458 MAJOR(pdi->dev), MINOR(pdi->dev),
459 sip->start, sip->end);
460 }
492da111
AB
461 }
462}
463
824c2b39
JA
464static void cpu_mark_online(struct per_dev_info *pdi, unsigned int cpu)
465{
466 if (cpu >= pdi->cpu_map_max || !pdi->cpu_map) {
467 int new_max = (cpu + CPUS_PER_LONG) & ~(CPUS_PER_LONG - 1);
468 unsigned long *map = malloc(new_max / sizeof(long));
469
470 memset(map, 0, new_max / sizeof(long));
471
472 if (pdi->cpu_map) {
473 memcpy(map, pdi->cpu_map, pdi->cpu_map_max / sizeof(long));
474 free(pdi->cpu_map);
475 }
476
477 pdi->cpu_map = map;
478 pdi->cpu_map_max = new_max;
479 }
480
481 pdi->cpu_map[CPU_IDX(cpu)] |= (1UL << CPU_BIT(cpu));
482}
483
484static inline void cpu_mark_offline(struct per_dev_info *pdi, int cpu)
485{
486 pdi->cpu_map[CPU_IDX(cpu)] &= ~(1UL << CPU_BIT(cpu));
487}
488
489static inline int cpu_is_online(struct per_dev_info *pdi, int cpu)
490{
491 return (pdi->cpu_map[CPU_IDX(cpu)] & (1UL << CPU_BIT(cpu))) != 0;
492}
493
bfc70ad5
JA
494static inline int ppm_hash_pid(pid_t pid)
495{
496 return jhash_1word(pid, JHASH_RANDOM) & PPM_HASH_MASK;
497}
498
499static struct process_pid_map *find_ppm(pid_t pid)
500{
501 const int hash_idx = ppm_hash_pid(pid);
502 struct process_pid_map *ppm;
503
504 ppm = ppm_hash_table[hash_idx];
505 while (ppm) {
506 if (ppm->pid == pid)
507 return ppm;
508
509 ppm = ppm->hash_next;
510 }
511
512 return NULL;
513}
514
515static void add_ppm_hash(pid_t pid, const char *name)
516{
517 const int hash_idx = ppm_hash_pid(pid);
518 struct process_pid_map *ppm;
519
520 ppm = find_ppm(pid);
521 if (ppm) {
522 fprintf(stderr, "Hmm ppm hash already exists %s/%d, this %s/%d\n", ppm->comm, ppm->pid, name, pid);
523 return;
524 }
525
526 ppm = malloc(sizeof(*ppm));
527 memset(ppm, 0, sizeof(*ppm));
528 ppm->pid = pid;
529 strcpy(ppm->comm, name);
530 ppm->hash_next = ppm_hash_table[hash_idx];
531 ppm_hash_table[hash_idx] = ppm;
532}
533
534char *find_process_name(pid_t pid)
535{
536 struct process_pid_map *ppm = find_ppm(pid);
537
538 if (ppm)
539 return ppm->comm;
540
541 return NULL;
542}
543
bf0720af
JA
544static inline int ppi_hash_pid(__u32 pid)
545{
546 return jhash_1word(pid, JHASH_RANDOM) & PPI_HASH_MASK;
547}
548
549static inline int ppi_hash_name(const char *name)
152f6476 550{
bf0720af
JA
551 return jhash(name, 16, JHASH_RANDOM) & PPI_HASH_MASK;
552}
553
554static inline int ppi_hash(struct per_process_info *ppi)
555{
556 if (ppi_hash_by_pid)
557 return ppi_hash_pid(ppi->pid);
558
bf0720af 559 return ppi_hash_name(ppi->name);
152f6476
JA
560}
561
bfc70ad5 562static inline void add_ppi_to_hash(struct per_process_info *ppi)
152f6476 563{
bf0720af 564 const int hash_idx = ppi_hash(ppi);
152f6476 565
bf0720af
JA
566 ppi->hash_next = ppi_hash_table[hash_idx];
567 ppi_hash_table[hash_idx] = ppi;
152f6476
JA
568}
569
bfc70ad5 570static inline void add_ppi_to_list(struct per_process_info *ppi)
152f6476
JA
571{
572 ppi->list_next = ppi_list;
573 ppi_list = ppi;
886ecf0e 574 ppi_list_entries++;
152f6476
JA
575}
576
bfc70ad5 577static struct per_process_info *find_ppi_by_name(char *name)
bf0720af
JA
578{
579 const int hash_idx = ppi_hash_name(name);
580 struct per_process_info *ppi;
581
582 ppi = ppi_hash_table[hash_idx];
583 while (ppi) {
584 if (!strcmp(ppi->name, name))
585 return ppi;
586
587 ppi = ppi->hash_next;
588 }
589
590 return NULL;
591}
592
bfc70ad5 593static struct per_process_info *find_ppi_by_pid(__u32 pid)
152f6476 594{
bf0720af 595 const int hash_idx = ppi_hash_pid(pid);
152f6476
JA
596 struct per_process_info *ppi;
597
bf0720af 598 ppi = ppi_hash_table[hash_idx];
152f6476
JA
599 while (ppi) {
600 if (ppi->pid == pid)
601 return ppi;
602
603 ppi = ppi->hash_next;
604 }
605
606 return NULL;
607}
608
bfc70ad5 609static struct per_process_info *find_ppi(__u32 pid)
bf0720af 610{
715d8021 611 struct per_process_info *ppi;
bfc70ad5 612 char *name;
715d8021 613
bf0720af 614 if (ppi_hash_by_pid)
bfc70ad5
JA
615 return find_ppi_by_pid(pid);
616
617 name = find_process_name(pid);
618 if (!name)
619 return NULL;
bf0720af 620
bfc70ad5 621 ppi = find_ppi_by_name(name);
715d8021
JA
622 if (ppi && ppi->pid != pid)
623 ppi->more_than_one = 1;
624
625 return ppi;
bf0720af
JA
626}
627
210824c3
JA
628/*
629 * struct trace and blktrace allocation cache, we do potentially
630 * millions of mallocs for these structures while only using at most
631 * a few thousand at the time
632 */
633static inline void t_free(struct trace *t)
634{
635 if (t_alloc_cache < 1024) {
636 t->next = t_alloc_list;
637 t_alloc_list = t;
638 t_alloc_cache++;
639 } else
640 free(t);
641}
642
643static inline struct trace *t_alloc(void)
644{
645 struct trace *t = t_alloc_list;
646
647 if (t) {
648 t_alloc_list = t->next;
649 t_alloc_cache--;
650 return t;
651 }
652
653 return malloc(sizeof(*t));
654}
655
656static inline void bit_free(struct blk_io_trace *bit)
657{
658 if (bit_alloc_cache < 1024 && !bit->pdu_len) {
659 /*
660 * abuse a 64-bit field for a next pointer for the free item
661 */
662 bit->time = (__u64) (unsigned long) bit_alloc_list;
663 bit_alloc_list = (struct blk_io_trace *) bit;
664 bit_alloc_cache++;
665 } else
666 free(bit);
667}
668
669static inline struct blk_io_trace *bit_alloc(void)
670{
671 struct blk_io_trace *bit = bit_alloc_list;
672
673 if (bit) {
674 bit_alloc_list = (struct blk_io_trace *) (unsigned long) \
675 bit->time;
676 bit_alloc_cache--;
677 return bit;
678 }
679
680 return malloc(sizeof(*bit));
681}
682
683static inline void __put_trace_last(struct per_dev_info *pdi, struct trace *t)
684{
685 struct per_cpu_info *pci = get_cpu_info(pdi, t->bit->cpu);
686
687 rb_erase(&t->rb_node, &pci->rb_last);
688 pci->rb_last_entries--;
689
690 bit_free(t->bit);
691 t_free(t);
692}
693
694static void put_trace(struct per_dev_info *pdi, struct trace *t)
695{
696 rb_erase(&t->rb_node, &rb_sort_root);
697 rb_sort_entries--;
698
699 trace_rb_insert_last(pdi, t);
700}
701
89482da6 702static inline int trace_rb_insert(struct trace *t, struct rb_root *root)
7997c5b0 703{
2a1b3424 704 struct rb_node **p = &root->rb_node;
7997c5b0
JA
705 struct rb_node *parent = NULL;
706 struct trace *__t;
707
708 while (*p) {
709 parent = *p;
2a1b3424 710
7997c5b0
JA
711 __t = rb_entry(parent, struct trace, rb_node);
712
89482da6
JA
713 if (t->bit->time < __t->bit->time)
714 p = &(*p)->rb_left;
715 else if (t->bit->time > __t->bit->time)
716 p = &(*p)->rb_right;
717 else if (t->bit->device < __t->bit->device)
e7c9f3ff
NS
718 p = &(*p)->rb_left;
719 else if (t->bit->device > __t->bit->device)
720 p = &(*p)->rb_right;
dcf0f7ed
JA
721 else if (t->bit->sequence < __t->bit->sequence)
722 p = &(*p)->rb_left;
0b07f23e 723 else /* >= sequence */
dcf0f7ed 724 p = &(*p)->rb_right;
7997c5b0
JA
725 }
726
727 rb_link_node(&t->rb_node, parent, p);
2a1b3424 728 rb_insert_color(&t->rb_node, root);
7997c5b0
JA
729 return 0;
730}
731
2a1b3424 732static inline int trace_rb_insert_sort(struct trace *t)
e3556946 733{
89482da6 734 if (!trace_rb_insert(t, &rb_sort_root)) {
2a1b3424
JA
735 rb_sort_entries++;
736 return 0;
737 }
738
739 return 1;
740}
741
210824c3 742static int trace_rb_insert_last(struct per_dev_info *pdi, struct trace *t)
2a1b3424 743{
210824c3
JA
744 struct per_cpu_info *pci = get_cpu_info(pdi, t->bit->cpu);
745
746 if (trace_rb_insert(t, &pci->rb_last))
747 return 1;
748
749 pci->rb_last_entries++;
750
751 if (pci->rb_last_entries > rb_batch * pdi->nfiles) {
752 struct rb_node *n = rb_first(&pci->rb_last);
753
754 t = rb_entry(n, struct trace, rb_node);
755 __put_trace_last(pdi, t);
2a1b3424
JA
756 }
757
210824c3 758 return 0;
2a1b3424
JA
759}
760
761static struct trace *trace_rb_find(dev_t device, unsigned long sequence,
762 struct rb_root *root, int order)
763{
764 struct rb_node *n = root->rb_node;
765 struct rb_node *prev = NULL;
e3556946
JA
766 struct trace *__t;
767
2a1b3424
JA
768 while (n) {
769 __t = rb_entry(n, struct trace, rb_node);
770 prev = n;
e3556946 771
0583b6a2 772 if (device < __t->bit->device)
2a1b3424 773 n = n->rb_left;
0583b6a2 774 else if (device > __t->bit->device)
2a1b3424 775 n = n->rb_right;
0583b6a2 776 else if (sequence < __t->bit->sequence)
2a1b3424 777 n = n->rb_left;
e3556946 778 else if (sequence > __t->bit->sequence)
2a1b3424 779 n = n->rb_right;
e3556946
JA
780 else
781 return __t;
782 }
783
2a1b3424
JA
784 /*
785 * hack - the list may not be sequence ordered because some
786 * events don't have sequence and time matched. so we end up
787 * being a little off in the rb lookup here, because we don't
788 * know the time we are looking for. compensate by browsing
789 * a little ahead from the last entry to find the match
790 */
791 if (order && prev) {
792 int max = 5;
793
794 while (((n = rb_next(prev)) != NULL) && max--) {
795 __t = rb_entry(n, struct trace, rb_node);
492da111 796
2a1b3424
JA
797 if (__t->bit->device == device &&
798 __t->bit->sequence == sequence)
799 return __t;
800
801 prev = n;
802 }
803 }
492da111 804
e3556946
JA
805 return NULL;
806}
807
2a1b3424 808static inline struct trace *trace_rb_find_last(struct per_dev_info *pdi,
210824c3 809 struct per_cpu_info *pci,
2a1b3424
JA
810 unsigned long seq)
811{
210824c3 812 return trace_rb_find(pdi->dev, seq, &pci->rb_last, 0);
2a1b3424
JA
813}
814
f7bd1a9b 815static inline int track_rb_insert(struct per_dev_info *pdi,struct io_track *iot)
7997c5b0 816{
f7bd1a9b 817 struct rb_node **p = &pdi->rb_track.rb_node;
7997c5b0
JA
818 struct rb_node *parent = NULL;
819 struct io_track *__iot;
820
821 while (*p) {
822 parent = *p;
7997c5b0
JA
823 __iot = rb_entry(parent, struct io_track, rb_node);
824
f7bd1a9b 825 if (iot->sector < __iot->sector)
7997c5b0
JA
826 p = &(*p)->rb_left;
827 else if (iot->sector > __iot->sector)
828 p = &(*p)->rb_right;
829 else {
e7c9f3ff 830 fprintf(stderr,
ab197ca7
AB
831 "sector alias (%Lu) on device %d,%d!\n",
832 (unsigned long long) iot->sector,
f7bd1a9b 833 MAJOR(pdi->dev), MINOR(pdi->dev));
7997c5b0
JA
834 return 1;
835 }
836 }
837
838 rb_link_node(&iot->rb_node, parent, p);
f7bd1a9b 839 rb_insert_color(&iot->rb_node, &pdi->rb_track);
7997c5b0
JA
840 return 0;
841}
842
f7bd1a9b 843static struct io_track *__find_track(struct per_dev_info *pdi, __u64 sector)
7997c5b0 844{
f7bd1a9b 845 struct rb_node *n = pdi->rb_track.rb_node;
7997c5b0
JA
846 struct io_track *__iot;
847
2a1b3424
JA
848 while (n) {
849 __iot = rb_entry(n, struct io_track, rb_node);
7997c5b0 850
f7bd1a9b 851 if (sector < __iot->sector)
2a1b3424 852 n = n->rb_left;
7997c5b0 853 else if (sector > __iot->sector)
2a1b3424 854 n = n->rb_right;
7997c5b0
JA
855 else
856 return __iot;
857 }
858
859 return NULL;
860}
861
f7bd1a9b 862static struct io_track *find_track(struct per_dev_info *pdi, __u32 pid,
bfc70ad5 863 __u64 sector)
7997c5b0 864{
916b5501 865 struct io_track *iot;
7997c5b0 866
f7bd1a9b 867 iot = __find_track(pdi, sector);
7997c5b0 868 if (!iot) {
bfc70ad5
JA
869 char *name = find_process_name(pid);
870
7997c5b0 871 iot = malloc(sizeof(*iot));
50adc0ba 872 iot->pid = pid;
bfc70ad5
JA
873 if (name)
874 memcpy(iot->comm, name, sizeof(iot->comm));
7997c5b0 875 iot->sector = sector;
f7bd1a9b 876 track_rb_insert(pdi, iot);
7997c5b0
JA
877 }
878
879 return iot;
880}
881
f7bd1a9b
JA
882static void log_track_frontmerge(struct per_dev_info *pdi,
883 struct blk_io_trace *t)
2e3e8ded
JA
884{
885 struct io_track *iot;
886
887 if (!track_ios)
888 return;
2e3e8ded 889
ae957cbc 890 iot = __find_track(pdi, t->sector + t_sec(t));
cb2a1a62 891 if (!iot) {
57ea8602
JA
892 if (verbose)
893 fprintf(stderr, "merge not found for (%d,%d): %llu\n",
894 MAJOR(pdi->dev), MINOR(pdi->dev),
895 (unsigned long long) t->sector + t_sec(t));
cb2a1a62 896 return;
2e3e8ded 897 }
cb2a1a62 898
f7bd1a9b 899 rb_erase(&iot->rb_node, &pdi->rb_track);
ae957cbc 900 iot->sector -= t_sec(t);
f7bd1a9b 901 track_rb_insert(pdi, iot);
2e3e8ded
JA
902}
903
f7bd1a9b 904static void log_track_getrq(struct per_dev_info *pdi, struct blk_io_trace *t)
2e3e8ded
JA
905{
906 struct io_track *iot;
907
908 if (!track_ios)
909 return;
910
bfc70ad5 911 iot = find_track(pdi, t->pid, t->sector);
95c15013
JA
912 iot->allocation_time = t->time;
913}
914
753f9091
JA
915static inline int is_remapper(struct per_dev_info *pdi)
916{
917 int major = MAJOR(pdi->dev);
918
919 return (major == 253 || major == 9);
920}
921
922/*
923 * for md/dm setups, the interesting cycle is Q -> C. So track queueing
924 * time here, as dispatch time
925 */
926static void log_track_queue(struct per_dev_info *pdi, struct blk_io_trace *t)
927{
928 struct io_track *iot;
929
930 if (!track_ios)
931 return;
932 if (!is_remapper(pdi))
933 return;
934
bfc70ad5 935 iot = find_track(pdi, t->pid, t->sector);
753f9091
JA
936 iot->dispatch_time = t->time;
937}
938
95c15013 939/*
b6076a9b 940 * return time between rq allocation and insertion
95c15013 941 */
f7bd1a9b
JA
942static unsigned long long log_track_insert(struct per_dev_info *pdi,
943 struct blk_io_trace *t)
95c15013 944{
50adc0ba 945 unsigned long long elapsed;
95c15013
JA
946 struct io_track *iot;
947
948 if (!track_ios)
949 return -1;
950
bfc70ad5 951 iot = find_track(pdi, t->pid, t->sector);
2e3e8ded 952 iot->queue_time = t->time;
acd70d21
JA
953
954 if (!iot->allocation_time)
955 return -1;
956
50adc0ba
JA
957 elapsed = iot->queue_time - iot->allocation_time;
958
959 if (per_process_stats) {
bfc70ad5 960 struct per_process_info *ppi = find_ppi(iot->pid);
b9d40d6f 961 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
50adc0ba 962
b9d40d6f
JA
963 if (ppi && elapsed > ppi->longest_allocation_wait[w])
964 ppi->longest_allocation_wait[w] = elapsed;
50adc0ba
JA
965 }
966
967 return elapsed;
2e3e8ded
JA
968}
969
970/*
971 * return time between queue and issue
972 */
f7bd1a9b
JA
973static unsigned long long log_track_issue(struct per_dev_info *pdi,
974 struct blk_io_trace *t)
2e3e8ded 975{
50adc0ba 976 unsigned long long elapsed;
2e3e8ded
JA
977 struct io_track *iot;
978
979 if (!track_ios)
980 return -1;
981 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
982 return -1;
983
f7bd1a9b 984 iot = __find_track(pdi, t->sector);
cb2a1a62 985 if (!iot) {
57ea8602
JA
986 if (verbose)
987 fprintf(stderr, "issue not found for (%d,%d): %llu\n",
988 MAJOR(pdi->dev), MINOR(pdi->dev),
989 (unsigned long long) t->sector);
2e3e8ded 990 return -1;
cb2a1a62 991 }
2e3e8ded
JA
992
993 iot->dispatch_time = t->time;
50adc0ba
JA
994 elapsed = iot->dispatch_time - iot->queue_time;
995
996 if (per_process_stats) {
bfc70ad5 997 struct per_process_info *ppi = find_ppi(iot->pid);
b9d40d6f 998 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
50adc0ba 999
b9d40d6f
JA
1000 if (ppi && elapsed > ppi->longest_dispatch_wait[w])
1001 ppi->longest_dispatch_wait[w] = elapsed;
50adc0ba
JA
1002 }
1003
1004 return elapsed;
2e3e8ded
JA
1005}
1006
1007/*
1008 * return time between dispatch and complete
1009 */
f7bd1a9b
JA
1010static unsigned long long log_track_complete(struct per_dev_info *pdi,
1011 struct blk_io_trace *t)
2e3e8ded
JA
1012{
1013 unsigned long long elapsed;
1014 struct io_track *iot;
1015
1016 if (!track_ios)
1017 return -1;
2e3e8ded 1018
f7bd1a9b 1019 iot = __find_track(pdi, t->sector);
cb2a1a62 1020 if (!iot) {
57ea8602
JA
1021 if (verbose)
1022 fprintf(stderr,"complete not found for (%d,%d): %llu\n",
1023 MAJOR(pdi->dev), MINOR(pdi->dev),
1024 (unsigned long long) t->sector);
2e3e8ded 1025 return -1;
cb2a1a62 1026 }
2e3e8ded
JA
1027
1028 iot->completion_time = t->time;
1029 elapsed = iot->completion_time - iot->dispatch_time;
1030
50adc0ba 1031 if (per_process_stats) {
bfc70ad5 1032 struct per_process_info *ppi = find_ppi(iot->pid);
b9d40d6f 1033 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
50adc0ba 1034
b9d40d6f
JA
1035 if (ppi && elapsed > ppi->longest_completion_wait[w])
1036 ppi->longest_completion_wait[w] = elapsed;
50adc0ba
JA
1037 }
1038
2e3e8ded
JA
1039 /*
1040 * kill the trace, we don't need it after completion
1041 */
f7bd1a9b 1042 rb_erase(&iot->rb_node, &pdi->rb_track);
2e3e8ded
JA
1043 free(iot);
1044
1045 return elapsed;
1046}
1047
1048
bfc70ad5 1049static struct io_stats *find_process_io_stats(__u32 pid)
152f6476 1050{
bfc70ad5 1051 struct per_process_info *ppi = find_ppi(pid);
152f6476
JA
1052
1053 if (!ppi) {
bfc70ad5
JA
1054 char *name = find_process_name(pid);
1055
152f6476
JA
1056 ppi = malloc(sizeof(*ppi));
1057 memset(ppi, 0, sizeof(*ppi));
bfc70ad5
JA
1058 if (name)
1059 memcpy(ppi->name, name, 16);
152f6476 1060 ppi->pid = pid;
bfc70ad5
JA
1061 add_ppi_to_hash(ppi);
1062 add_ppi_to_list(ppi);
152f6476
JA
1063 }
1064
1065 return &ppi->io_stats;
1066}
1067
e7c9f3ff
NS
1068static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size)
1069{
1070 if (pdi->name)
1071 snprintf(buffer, size, "%s", pdi->name);
1072 else
f7bd1a9b 1073 snprintf(buffer, size, "%d,%d",MAJOR(pdi->dev),MINOR(pdi->dev));
e7c9f3ff
NS
1074 return buffer;
1075}
1076
e7c9f3ff 1077static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit)
cfab07eb
AB
1078{
1079 unsigned long long this = bit->time;
e7c9f3ff 1080 unsigned long long last = pdi->last_reported_time;
cfab07eb 1081
e7c9f3ff
NS
1082 pdi->backwards = (this < last) ? 'B' : ' ';
1083 pdi->last_reported_time = this;
cfab07eb
AB
1084}
1085
152f6476
JA
1086static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t,
1087 int rw)
d0ca268b
JA
1088{
1089 if (rw) {
152f6476 1090 ios->mwrites++;
ae957cbc 1091 ios->qwrite_kb += t_kb(t);
d0ca268b 1092 } else {
152f6476 1093 ios->mreads++;
ae957cbc 1094 ios->qread_kb += t_kb(t);
152f6476
JA
1095 }
1096}
1097
1098static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci,
1099 int rw)
1100{
1101 __account_m(&pci->io_stats, t, rw);
1102
1103 if (per_process_stats) {
bfc70ad5 1104 struct io_stats *ios = find_process_io_stats(t->pid);
152f6476
JA
1105
1106 __account_m(ios, t, rw);
d0ca268b
JA
1107 }
1108}
1109
b6076a9b
JA
1110static inline void __account_queue(struct io_stats *ios, struct blk_io_trace *t,
1111 int rw)
d0ca268b
JA
1112{
1113 if (rw) {
152f6476 1114 ios->qwrites++;
ae957cbc 1115 ios->qwrite_kb += t_kb(t);
d0ca268b 1116 } else {
152f6476 1117 ios->qreads++;
ae957cbc 1118 ios->qread_kb += t_kb(t);
152f6476
JA
1119 }
1120}
1121
b6076a9b
JA
1122static inline void account_queue(struct blk_io_trace *t,
1123 struct per_cpu_info *pci, int rw)
152f6476 1124{
b6076a9b 1125 __account_queue(&pci->io_stats, t, rw);
152f6476
JA
1126
1127 if (per_process_stats) {
bfc70ad5 1128 struct io_stats *ios = find_process_io_stats(t->pid);
152f6476 1129
b6076a9b 1130 __account_queue(ios, t, rw);
d0ca268b
JA
1131 }
1132}
1133
e21dc4dd 1134static inline void __account_c(struct io_stats *ios, int rw, int bytes)
d0ca268b
JA
1135{
1136 if (rw) {
152f6476
JA
1137 ios->cwrites++;
1138 ios->cwrite_kb += bytes >> 10;
d0ca268b 1139 } else {
152f6476
JA
1140 ios->creads++;
1141 ios->cread_kb += bytes >> 10;
1142 }
1143}
1144
1145static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci,
1146 int rw, int bytes)
1147{
1148 __account_c(&pci->io_stats, rw, bytes);
1149
1150 if (per_process_stats) {
bfc70ad5 1151 struct io_stats *ios = find_process_io_stats(t->pid);
152f6476
JA
1152
1153 __account_c(ios, rw, bytes);
d0ca268b
JA
1154 }
1155}
1156
b6076a9b
JA
1157static inline void __account_issue(struct io_stats *ios, int rw,
1158 unsigned int bytes)
afd2d7ad 1159{
1160 if (rw) {
152f6476
JA
1161 ios->iwrites++;
1162 ios->iwrite_kb += bytes >> 10;
afd2d7ad 1163 } else {
152f6476
JA
1164 ios->ireads++;
1165 ios->iread_kb += bytes >> 10;
afd2d7ad 1166 }
1167}
1168
b6076a9b
JA
1169static inline void account_issue(struct blk_io_trace *t,
1170 struct per_cpu_info *pci, int rw)
d0ca268b 1171{
b6076a9b 1172 __account_issue(&pci->io_stats, rw, t->bytes);
152f6476
JA
1173
1174 if (per_process_stats) {
bfc70ad5 1175 struct io_stats *ios = find_process_io_stats(t->pid);
d5396421 1176
b6076a9b 1177 __account_issue(ios, rw, t->bytes);
152f6476
JA
1178 }
1179}
1180
06639b27
JA
1181static inline void __account_unplug(struct io_stats *ios, int timer)
1182{
1183 if (timer)
1184 ios->timer_unplugs++;
1185 else
1186 ios->io_unplugs++;
1187}
1188
1189static inline void account_unplug(struct blk_io_trace *t,
1190 struct per_cpu_info *pci, int timer)
1191{
1192 __account_unplug(&pci->io_stats, timer);
1193
1194 if (per_process_stats) {
bfc70ad5 1195 struct io_stats *ios = find_process_io_stats(t->pid);
06639b27
JA
1196
1197 __account_unplug(ios, timer);
1198 }
1199}
1200
4054070a
JA
1201static inline void __account_requeue(struct io_stats *ios,
1202 struct blk_io_trace *t, int rw)
1203{
1204 if (rw) {
1205 ios->wrqueue++;
1206 ios->iwrite_kb -= t_kb(t);
1207 } else {
1208 ios->rrqueue++;
1209 ios->iread_kb -= t_kb(t);
1210 }
1211}
1212
1213static inline void account_requeue(struct blk_io_trace *t,
1214 struct per_cpu_info *pci, int rw)
1215{
1216 __account_requeue(&pci->io_stats, t, rw);
1217
1218 if (per_process_stats) {
bfc70ad5 1219 struct io_stats *ios = find_process_io_stats(t->pid);
4054070a
JA
1220
1221 __account_requeue(ios, t, rw);
1222 }
1223}
1224
f7bd1a9b
JA
1225static void log_complete(struct per_dev_info *pdi, struct per_cpu_info *pci,
1226 struct blk_io_trace *t, char *act)
ab197ca7 1227{
f7bd1a9b 1228 process_fmt(act, pci, t, log_track_complete(pdi, t), 0, NULL);
ab197ca7
AB
1229}
1230
f7bd1a9b
JA
1231static void log_insert(struct per_dev_info *pdi, struct per_cpu_info *pci,
1232 struct blk_io_trace *t, char *act)
b6076a9b 1233{
f7bd1a9b 1234 process_fmt(act, pci, t, log_track_insert(pdi, t), 0, NULL);
b6076a9b
JA
1235}
1236
ab197ca7
AB
1237static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t,
1238 char *act)
1239{
b6076a9b 1240 process_fmt(act, pci, t, -1, 0, NULL);
ab197ca7 1241}
2e3e8ded 1242
f7bd1a9b
JA
1243static void log_issue(struct per_dev_info *pdi, struct per_cpu_info *pci,
1244 struct blk_io_trace *t, char *act)
ab197ca7 1245{
f7bd1a9b 1246 process_fmt(act, pci, t, log_track_issue(pdi, t), 0, NULL);
d0ca268b
JA
1247}
1248
f7bd1a9b
JA
1249static void log_merge(struct per_dev_info *pdi, struct per_cpu_info *pci,
1250 struct blk_io_trace *t, char *act)
d0ca268b 1251{
a01516de 1252 if (act[0] == 'F')
f7bd1a9b 1253 log_track_frontmerge(pdi, t);
2e3e8ded 1254
ab197ca7 1255 process_fmt(act, pci, t, -1ULL, 0, NULL);
d0ca268b
JA
1256}
1257
dfe34da1 1258static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t,
3639a11e 1259 char *act)
dfe34da1 1260{
ab197ca7 1261 process_fmt(act, pci, t, -1ULL, 0, NULL);
dfe34da1
JA
1262}
1263
d5396421 1264static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t,
3639a11e 1265 char *act)
d0ca268b 1266{
ab197ca7 1267 process_fmt(act, pci, t, -1ULL, 0, NULL);
d0ca268b
JA
1268}
1269
ab197ca7 1270static void log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t,
3639a11e 1271 char *act)
67e14fdc 1272{
ab197ca7 1273 process_fmt(act, pci, t, -1ULL, 0, NULL);
67e14fdc
JA
1274}
1275
93f1c611
JA
1276static void log_split(struct per_cpu_info *pci, struct blk_io_trace *t,
1277 char *act)
1278{
1279 process_fmt(act, pci, t, -1ULL, 0, NULL);
1280}
1281
ab197ca7 1282static void log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act)
d0ca268b 1283{
ab197ca7 1284 unsigned char *buf = (unsigned char *) t + sizeof(*t);
d0ca268b 1285
ab197ca7 1286 process_fmt(act, pci, t, -1ULL, t->pdu_len, buf);
d0ca268b
JA
1287}
1288
ff3a732c 1289static void dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci)
d0ca268b 1290{
56f2af81
JA
1291 int act = t->action & 0xffff;
1292
1293 switch (act) {
d0ca268b 1294 case __BLK_TA_QUEUE:
3639a11e 1295 log_generic(pci, t, "Q");
d0ca268b
JA
1296 break;
1297 case __BLK_TA_GETRQ:
3639a11e 1298 log_generic(pci, t, "G");
d0ca268b
JA
1299 break;
1300 case __BLK_TA_SLEEPRQ:
3639a11e 1301 log_generic(pci, t, "S");
d0ca268b
JA
1302 break;
1303 case __BLK_TA_REQUEUE:
3639a11e 1304 log_generic(pci, t, "R");
d0ca268b
JA
1305 break;
1306 case __BLK_TA_ISSUE:
ab197ca7 1307 log_pc(pci, t, "D");
d0ca268b
JA
1308 break;
1309 case __BLK_TA_COMPLETE:
3639a11e 1310 log_pc(pci, t, "C");
d0ca268b 1311 break;
56f2af81
JA
1312 case __BLK_TA_INSERT:
1313 log_pc(pci, t, "I");
1314 break;
d0ca268b 1315 default:
56f2af81 1316 fprintf(stderr, "Bad pc action %x\n", act);
87b72777 1317 break;
d0ca268b 1318 }
d0ca268b
JA
1319}
1320
f7bd1a9b
JA
1321static void dump_trace_fs(struct blk_io_trace *t, struct per_dev_info *pdi,
1322 struct per_cpu_info *pci)
d0ca268b 1323{
649c7b66 1324 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
7997c5b0 1325 int act = t->action & 0xffff;
d0ca268b 1326
7997c5b0 1327 switch (act) {
d0ca268b 1328 case __BLK_TA_QUEUE:
753f9091 1329 log_track_queue(pdi, t);
b6076a9b 1330 account_queue(t, pci, w);
3639a11e 1331 log_queue(pci, t, "Q");
d0ca268b 1332 break;
b6076a9b 1333 case __BLK_TA_INSERT:
f7bd1a9b 1334 log_insert(pdi, pci, t, "I");
b6076a9b 1335 break;
d0ca268b 1336 case __BLK_TA_BACKMERGE:
152f6476 1337 account_m(t, pci, w);
f7bd1a9b 1338 log_merge(pdi, pci, t, "M");
d0ca268b
JA
1339 break;
1340 case __BLK_TA_FRONTMERGE:
152f6476 1341 account_m(t, pci, w);
f7bd1a9b 1342 log_merge(pdi, pci, t, "F");
d0ca268b
JA
1343 break;
1344 case __BLK_TA_GETRQ:
f7bd1a9b 1345 log_track_getrq(pdi, t);
3639a11e 1346 log_generic(pci, t, "G");
d0ca268b
JA
1347 break;
1348 case __BLK_TA_SLEEPRQ:
3639a11e 1349 log_generic(pci, t, "S");
d0ca268b
JA
1350 break;
1351 case __BLK_TA_REQUEUE:
65f2deb5
JA
1352 /*
1353 * can happen if we miss traces, don't let it go
1354 * below zero
1355 */
1356 if (pdi->cur_depth[w])
1357 pdi->cur_depth[w]--;
4054070a 1358 account_requeue(t, pci, w);
3639a11e 1359 log_queue(pci, t, "R");
d0ca268b
JA
1360 break;
1361 case __BLK_TA_ISSUE:
b6076a9b 1362 account_issue(t, pci, w);
649c7b66
JA
1363 pdi->cur_depth[w]++;
1364 if (pdi->cur_depth[w] > pdi->max_depth[w])
1365 pdi->max_depth[w] = pdi->cur_depth[w];
f7bd1a9b 1366 log_issue(pdi, pci, t, "D");
d0ca268b
JA
1367 break;
1368 case __BLK_TA_COMPLETE:
65f2deb5
JA
1369 if (pdi->cur_depth[w])
1370 pdi->cur_depth[w]--;
152f6476 1371 account_c(t, pci, w, t->bytes);
f7bd1a9b 1372 log_complete(pdi, pci, t, "C");
d0ca268b 1373 break;
88b1a526 1374 case __BLK_TA_PLUG:
3639a11e 1375 log_action(pci, t, "P");
88b1a526 1376 break;
3639a11e 1377 case __BLK_TA_UNPLUG_IO:
06639b27 1378 account_unplug(t, pci, 0);
3639a11e
JA
1379 log_unplug(pci, t, "U");
1380 break;
1381 case __BLK_TA_UNPLUG_TIMER:
06639b27 1382 account_unplug(t, pci, 1);
3639a11e 1383 log_unplug(pci, t, "UT");
88b1a526 1384 break;
93f1c611
JA
1385 case __BLK_TA_SPLIT:
1386 log_split(pci, t, "X");
1387 break;
1388 case __BLK_TA_BOUNCE:
1389 log_generic(pci, t, "B");
1390 break;
a8f30e64
JA
1391 case __BLK_TA_REMAP:
1392 log_generic(pci, t, "A");
1393 break;
d0ca268b
JA
1394 default:
1395 fprintf(stderr, "Bad fs action %x\n", t->action);
1f79c4a0 1396 break;
d0ca268b 1397 }
d0ca268b
JA
1398}
1399
ff3a732c
JA
1400static void dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
1401 struct per_dev_info *pdi)
d0ca268b
JA
1402{
1403 if (t->action & BLK_TC_ACT(BLK_TC_PC))
ff3a732c 1404 dump_trace_pc(t, pci);
d0ca268b 1405 else
f7bd1a9b 1406 dump_trace_fs(t, pdi, pci);
87b72777 1407
20ed6177
JA
1408 if (!pdi->events)
1409 pdi->first_reported_time = t->time;
1410
e7c9f3ff 1411 pdi->events++;
d0ca268b
JA
1412}
1413
4c523165
JA
1414/*
1415 * print in a proper way, not too small and not too big. if more than
1416 * 1000,000K, turn into M and so on
1417 */
1418static char *size_cnv(char *dst, unsigned long long num, int in_kb)
1419{
da19e768 1420 char suff[] = { '\0', 'K', 'M', 'G', 'P' };
0dc3602c 1421 unsigned int i = 0;
4c523165
JA
1422
1423 if (in_kb)
1424 i++;
1425
0dc3602c 1426 while (num > 1000 * 1000ULL && (i < sizeof(suff) - 1)) {
4c523165
JA
1427 i++;
1428 num /= 1000;
1429 }
1430
1431 sprintf(dst, "%'8Lu%c", num, suff[i]);
1432 return dst;
1433}
1434
649c7b66
JA
1435static void dump_io_stats(struct per_dev_info *pdi, struct io_stats *ios,
1436 char *msg)
5c017e4b 1437{
4c523165
JA
1438 static char x[256], y[256];
1439
152f6476
JA
1440 fprintf(ofp, "%s\n", msg);
1441
4c523165
JA
1442 fprintf(ofp, " Reads Queued: %s, %siB\t", size_cnv(x, ios->qreads, 0), size_cnv(y, ios->qread_kb, 1));
1443 fprintf(ofp, " Writes Queued: %s, %siB\n", size_cnv(x, ios->qwrites, 0), size_cnv(y, ios->qwrite_kb, 1));
0a6b8fc4 1444
4c523165
JA
1445 fprintf(ofp, " Read Dispatches: %s, %siB\t", size_cnv(x, ios->ireads, 0), size_cnv(y, ios->iread_kb, 1));
1446 fprintf(ofp, " Write Dispatches: %s, %siB\n", size_cnv(x, ios->iwrites, 0), size_cnv(y, ios->iwrite_kb, 1));
4054070a
JA
1447 fprintf(ofp, " Reads Requeued: %s\t\t", size_cnv(x, ios->rrqueue, 0));
1448 fprintf(ofp, " Writes Requeued: %s\n", size_cnv(x, ios->wrqueue, 0));
4c523165
JA
1449 fprintf(ofp, " Reads Completed: %s, %siB\t", size_cnv(x, ios->creads, 0), size_cnv(y, ios->cread_kb, 1));
1450 fprintf(ofp, " Writes Completed: %s, %siB\n", size_cnv(x, ios->cwrites, 0), size_cnv(y, ios->cwrite_kb, 1));
152f6476 1451 fprintf(ofp, " Read Merges: %'8lu%8c\t", ios->mreads, ' ');
152f6476 1452 fprintf(ofp, " Write Merges: %'8lu\n", ios->mwrites);
649c7b66
JA
1453 if (pdi) {
1454 fprintf(ofp, " Read depth: %'8u%8c\t", pdi->max_depth[0], ' ');
1455 fprintf(ofp, " Write depth: %'8u\n", pdi->max_depth[1]);
1456 }
06639b27
JA
1457 fprintf(ofp, " IO unplugs: %'8lu%8c\t", ios->io_unplugs, ' ');
1458 fprintf(ofp, " Timer unplugs: %'8lu\n", ios->timer_unplugs);
5c017e4b
JA
1459}
1460
50adc0ba
JA
1461static void dump_wait_stats(struct per_process_info *ppi)
1462{
b9d40d6f
JA
1463 unsigned long rawait = ppi->longest_allocation_wait[0] / 1000;
1464 unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000;
1465 unsigned long rcwait = ppi->longest_completion_wait[0] / 1000;
1466 unsigned long wawait = ppi->longest_allocation_wait[1] / 1000;
1467 unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000;
1468 unsigned long wcwait = ppi->longest_completion_wait[1] / 1000;
1469
1470 fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' ');
1471 fprintf(ofp, " Allocation wait: %'8lu\n", wawait);
1472 fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' ');
1473 fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait);
1474 fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' ');
1475 fprintf(ofp, " Completion wait: %'8lu\n", wcwait);
50adc0ba
JA
1476}
1477
886ecf0e
JA
1478static int ppi_name_compare(const void *p1, const void *p2)
1479{
1480 struct per_process_info *ppi1 = *((struct per_process_info **) p1);
1481 struct per_process_info *ppi2 = *((struct per_process_info **) p2);
1482 int res;
1483
1484 res = strverscmp(ppi1->name, ppi2->name);
1485 if (!res)
06e6f286 1486 res = ppi1->pid > ppi2->pid;
886ecf0e
JA
1487
1488 return res;
1489}
1490
1491static void sort_process_list(void)
1492{
1493 struct per_process_info **ppis;
1494 struct per_process_info *ppi;
1495 int i = 0;
1496
1497 ppis = malloc(ppi_list_entries * sizeof(struct per_process_info *));
1498
1499 ppi = ppi_list;
1500 while (ppi) {
06e6f286 1501 ppis[i++] = ppi;
886ecf0e
JA
1502 ppi = ppi->list_next;
1503 }
1504
06e6f286 1505 qsort(ppis, ppi_list_entries, sizeof(ppi), ppi_name_compare);
886ecf0e
JA
1506
1507 i = ppi_list_entries - 1;
1508 ppi_list = NULL;
1509 while (i >= 0) {
1510 ppi = ppis[i];
1511
1512 ppi->list_next = ppi_list;
1513 ppi_list = ppi;
1514 i--;
1515 }
50c38702
JA
1516
1517 free(ppis);
886ecf0e
JA
1518}
1519
152f6476
JA
1520static void show_process_stats(void)
1521{
1522 struct per_process_info *ppi;
1523
886ecf0e
JA
1524 sort_process_list();
1525
152f6476
JA
1526 ppi = ppi_list;
1527 while (ppi) {
ce8b6b4f
JA
1528 char name[64];
1529
715d8021 1530 if (ppi->more_than_one)
bf0720af 1531 sprintf(name, "%s (%u, ...)", ppi->name, ppi->pid);
715d8021
JA
1532 else
1533 sprintf(name, "%s (%u)", ppi->name, ppi->pid);
bf0720af 1534
649c7b66 1535 dump_io_stats(NULL, &ppi->io_stats, name);
50adc0ba 1536 dump_wait_stats(ppi);
152f6476
JA
1537 ppi = ppi->list_next;
1538 }
1539
1540 fprintf(ofp, "\n");
1541}
1542
e7c9f3ff 1543static void show_device_and_cpu_stats(void)
d0ca268b 1544{
e7c9f3ff
NS
1545 struct per_dev_info *pdi;
1546 struct per_cpu_info *pci;
1547 struct io_stats total, *ios;
20ed6177 1548 unsigned long long rrate, wrate, msec;
e7c9f3ff
NS
1549 int i, j, pci_events;
1550 char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3];
1551 char name[32];
1552
1553 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
1554
1555 memset(&total, 0, sizeof(total));
1556 pci_events = 0;
1557
1558 if (i > 0)
1559 fprintf(ofp, "\n");
1560
1561 for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) {
1562 if (!pci->nelems)
1563 continue;
1564
1565 ios = &pci->io_stats;
1566 total.qreads += ios->qreads;
1567 total.qwrites += ios->qwrites;
1568 total.creads += ios->creads;
1569 total.cwrites += ios->cwrites;
1570 total.mreads += ios->mreads;
1571 total.mwrites += ios->mwrites;
1572 total.ireads += ios->ireads;
1573 total.iwrites += ios->iwrites;
4054070a
JA
1574 total.rrqueue += ios->rrqueue;
1575 total.wrqueue += ios->wrqueue;
e7c9f3ff
NS
1576 total.qread_kb += ios->qread_kb;
1577 total.qwrite_kb += ios->qwrite_kb;
1578 total.cread_kb += ios->cread_kb;
1579 total.cwrite_kb += ios->cwrite_kb;
1580 total.iread_kb += ios->iread_kb;
1581 total.iwrite_kb += ios->iwrite_kb;
06639b27
JA
1582 total.timer_unplugs += ios->timer_unplugs;
1583 total.io_unplugs += ios->io_unplugs;
e7c9f3ff
NS
1584
1585 snprintf(line, sizeof(line) - 1, "CPU%d (%s):",
1586 j, get_dev_name(pdi, name, sizeof(name)));
649c7b66 1587 dump_io_stats(pdi, ios, line);
e7c9f3ff
NS
1588 pci_events++;
1589 }
5c017e4b 1590
e7c9f3ff
NS
1591 if (pci_events > 1) {
1592 fprintf(ofp, "\n");
1593 snprintf(line, sizeof(line) - 1, "Total (%s):",
1594 get_dev_name(pdi, name, sizeof(name)));
649c7b66 1595 dump_io_stats(NULL, &total, line);
e7c9f3ff 1596 }
d0ca268b 1597
20ed6177 1598 wrate = rrate = 0;
20ed6177
JA
1599 msec = (pdi->last_reported_time - pdi->first_reported_time) / 1000000;
1600 if (msec) {
1601 rrate = 1000 * total.cread_kb / msec;
1602 wrate = 1000 * total.cwrite_kb / msec;
1603 }
1604
dce0f678
AB
1605 fprintf(ofp, "\nThroughput (R/W): %'LuKiB/s / %'LuKiB/s\n",
1606 rrate, wrate);
1607 fprintf(ofp, "Events (%s): %'Lu entries\n",
1608 get_dev_name(pdi, line, sizeof(line)), pdi->events);
492da111
AB
1609
1610 collect_pdi_skips(pdi);
1611 fprintf(ofp, "Skips: %'lu forward (%'llu - %5.1lf%%)\n",
dce0f678
AB
1612 pdi->skips,pdi->seq_skips,
1613 100.0 * ((double)pdi->seq_skips /
dce0f678 1614 (double)(pdi->events + pdi->seq_skips)));
e7c9f3ff 1615 }
d0ca268b
JA
1616}
1617
4f0ae44f
JA
1618static void find_genesis(void)
1619{
1620 struct trace *t = trace_list;
1621
1622 genesis_time = -1ULL;
1623 while (t != NULL) {
1624 if (t->bit->time < genesis_time)
1625 genesis_time = t->bit->time;
1626
1627 t = t->next;
1628 }
1629}
1630
7f4d89e6 1631static inline int check_stopwatch(struct blk_io_trace *bit)
4f0ae44f 1632{
7f4d89e6
JA
1633 if (bit->time < stopwatch_end &&
1634 bit->time >= stopwatch_start)
4f0ae44f
JA
1635 return 0;
1636
1637 return 1;
1638}
1639
53c68c88
JA
1640/*
1641 * return youngest entry read
1642 */
1643static int sort_entries(unsigned long long *youngest)
4f0ae44f 1644{
210824c3
JA
1645 struct per_dev_info *pdi = NULL;
1646 struct per_cpu_info *pci = NULL;
4f0ae44f 1647 struct trace *t;
4f0ae44f
JA
1648
1649 if (!genesis_time)
1650 find_genesis();
1651
d6222db8 1652 *youngest = 0;
4f0ae44f
JA
1653 while ((t = trace_list) != NULL) {
1654 struct blk_io_trace *bit = t->bit;
1655
1656 trace_list = t->next;
1657
7f4d89e6 1658 bit->time -= genesis_time;
4f0ae44f 1659
d6222db8
JA
1660 if (bit->time < *youngest || !*youngest)
1661 *youngest = bit->time;
1662
210824c3
JA
1663 if (!pdi || pdi->dev != bit->device) {
1664 pdi = get_dev_info(bit->device);
1665 pci = NULL;
1666 }
1667
1668 if (!pci || pci->cpu != bit->cpu)
1669 pci = get_cpu_info(pdi, bit->cpu);
1670
1671 if (bit->sequence < pci->smallest_seq_read)
1672 pci->smallest_seq_read = bit->sequence;
774a1a10 1673
7f4d89e6 1674 if (check_stopwatch(bit)) {
4f0ae44f
JA
1675 bit_free(bit);
1676 t_free(t);
1677 continue;
1678 }
1679
2a1b3424 1680 if (trace_rb_insert_sort(t))
53c68c88 1681 return -1;
4f0ae44f
JA
1682 }
1683
53c68c88 1684 return 0;
4f0ae44f
JA
1685}
1686
824c2b39
JA
1687/*
1688 * to continue, we must have traces from all online cpus in the tree
1689 */
1690static int check_cpu_map(struct per_dev_info *pdi)
1691{
1692 unsigned long *cpu_map;
1693 struct rb_node *n;
1694 struct trace *__t;
1695 unsigned int i;
1696 int ret, cpu;
1697
1698 /*
1699 * create a map of the cpus we have traces for
1700 */
1701 cpu_map = malloc(pdi->cpu_map_max / sizeof(long));
1702 n = rb_first(&rb_sort_root);
1703 while (n) {
1704 __t = rb_entry(n, struct trace, rb_node);
1705 cpu = __t->bit->cpu;
1706
1707 cpu_map[CPU_IDX(cpu)] |= (1UL << CPU_BIT(cpu));
1708 n = rb_next(n);
1709 }
1710
1711 /*
b1c8e614
JA
1712 * we can't continue if pdi->cpu_map has entries set that we don't
1713 * have in the sort rbtree. the opposite is not a problem, though
824c2b39
JA
1714 */
1715 ret = 0;
1716 for (i = 0; i < pdi->cpu_map_max / CPUS_PER_LONG; i++) {
1717 if (pdi->cpu_map[i] & ~(cpu_map[i])) {
1718 ret = 1;
1719 break;
1720 }
1721 }
1722
1723 free(cpu_map);
1724 return ret;
1725}
1726
a141a7cd 1727static int check_sequence(struct per_dev_info *pdi, struct trace *t, int force)
2a1b3424 1728{
1ca323a5 1729 struct blk_io_trace *bit = t->bit;
210824c3
JA
1730 unsigned long expected_sequence;
1731 struct per_cpu_info *pci;
1ca323a5 1732 struct trace *__t;
492da111 1733
210824c3
JA
1734 pci = get_cpu_info(pdi, bit->cpu);
1735 expected_sequence = pci->last_sequence + 1;
1736
774a1a10 1737 if (!expected_sequence) {
774a1a10
JA
1738 /*
1739 * 1 should be the first entry, just allow it
1740 */
1741 if (bit->sequence == 1)
1742 return 0;
210824c3 1743 if (bit->sequence == pci->smallest_seq_read)
79ee9704 1744 return 0;
774a1a10 1745
824c2b39 1746 return check_cpu_map(pdi);
774a1a10 1747 }
2a1b3424
JA
1748
1749 if (bit->sequence == expected_sequence)
1750 return 0;
1751
2a1b3424 1752 /*
1c7c54aa
JA
1753 * we may not have seen that sequence yet. if we are not doing
1754 * the final run, break and wait for more entries.
1c24add6 1755 */
210824c3
JA
1756 if (expected_sequence < pci->smallest_seq_read) {
1757 __t = trace_rb_find_last(pdi, pci, expected_sequence);
1ca323a5 1758 if (!__t)
1c7c54aa 1759 goto skip;
2a1b3424 1760
1ca323a5 1761 __put_trace_last(pdi, __t);
2a1b3424 1762 return 0;
a141a7cd
JA
1763 } else if (!force) {
1764 return 1;
0b07f23e 1765 } else {
1c7c54aa 1766skip:
66930177 1767 if (check_current_skips(pci, bit->sequence))
492da111
AB
1768 return 0;
1769
965eca2d 1770 if (expected_sequence < bit->sequence)
66930177 1771 insert_skip(pci, expected_sequence, bit->sequence - 1);
1c7c54aa
JA
1772 return 0;
1773 }
2a1b3424
JA
1774}
1775
a649216c 1776static void show_entries_rb(int force)
8fc0abbc 1777{
1f7afa72
JA
1778 struct per_dev_info *pdi = NULL;
1779 struct per_cpu_info *pci = NULL;
8fc0abbc 1780 struct blk_io_trace *bit;
3aabcd89 1781 struct rb_node *n;
8fc0abbc 1782 struct trace *t;
1f7afa72 1783
7d747d22 1784 while ((n = rb_first(&rb_sort_root)) != NULL) {
dd90748f 1785 if (is_done() && !force && !pipeline)
1f7afa72 1786 break;
8fc0abbc
JA
1787
1788 t = rb_entry(n, struct trace, rb_node);
1789 bit = t->bit;
1790
a43c1c17
JA
1791 if (read_sequence - t->read_sequence < 1 && !force)
1792 break;
1793
210824c3 1794 if (!pdi || pdi->dev != bit->device) {
287fa3d6 1795 pdi = get_dev_info(bit->device);
210824c3
JA
1796 pci = NULL;
1797 }
1f7afa72 1798
e7c9f3ff
NS
1799 if (!pdi) {
1800 fprintf(stderr, "Unknown device ID? (%d,%d)\n",
1801 MAJOR(bit->device), MINOR(bit->device));
1802 break;
1803 }
1f7afa72 1804
a141a7cd
JA
1805 if (check_sequence(pdi, t, force))
1806 break;
cb2a1a62 1807
a141a7cd
JA
1808 if (!force && bit->time > last_allowed_time)
1809 break;
8fc0abbc 1810
4f0ae44f 1811 check_time(pdi, bit);
8fc0abbc 1812
4f0ae44f
JA
1813 if (!pci || pci->cpu != bit->cpu)
1814 pci = get_cpu_info(pdi, bit->cpu);
287fa3d6 1815
210824c3
JA
1816 pci->last_sequence = bit->sequence;
1817
cbc927b6
JA
1818 pci->nelems++;
1819
66930177 1820 if (bit->action & (act_mask << BLK_TC_SHIFT))
98f8386b 1821 dump_trace(bit, pci, pdi);
87b72777 1822
2a1b3424 1823 put_trace(pdi, t);
cb2a1a62 1824 }
8fc0abbc
JA
1825}
1826
c0e0dbc2 1827static int read_data(int fd, void *buffer, int bytes, int block, int *fdblock)
1f79c4a0
JA
1828{
1829 int ret, bytes_left, fl;
1830 void *p;
1831
c0e0dbc2
JA
1832 if (block != *fdblock) {
1833 fl = fcntl(fd, F_GETFL);
1f79c4a0 1834
c0e0dbc2
JA
1835 if (!block) {
1836 *fdblock = 0;
1837 fcntl(fd, F_SETFL, fl | O_NONBLOCK);
1838 } else {
1839 *fdblock = 1;
1840 fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
1841 }
1842 }
1f79c4a0
JA
1843
1844 bytes_left = bytes;
1845 p = buffer;
1846 while (bytes_left > 0) {
1847 ret = read(fd, p, bytes_left);
1848 if (!ret)
1849 return 1;
1850 else if (ret < 0) {
db7e0552 1851 if (errno != EAGAIN) {
1f79c4a0 1852 perror("read");
db7e0552
JA
1853 return -1;
1854 }
a649216c 1855
5c0f40f7
JA
1856 /*
1857 * never do partial reads. we can return if we
1858 * didn't read anything and we should not block,
1859 * otherwise wait for data
1860 */
1861 if ((bytes_left == bytes) && !block)
1862 return 1;
1863
1864 usleep(10);
1865 continue;
1f79c4a0
JA
1866 } else {
1867 p += ret;
1868 bytes_left -= ret;
1869 }
1870 }
1871
1872 return 0;
1873}
1874
017d1660
JA
1875static inline __u16 get_pdulen(struct blk_io_trace *bit)
1876{
1877 if (data_is_native)
1878 return bit->pdu_len;
1879
1880 return __bswap_16(bit->pdu_len);
1881}
1882
1883static inline __u32 get_magic(struct blk_io_trace *bit)
1884{
1885 if (data_is_native)
1886 return bit->magic;
1887
1888 return __bswap_32(bit->magic);
1889}
1890
c0e0dbc2 1891static int read_events(int fd, int always_block, int *fdblock)
cb2a1a62 1892{
287fa3d6 1893 struct per_dev_info *pdi = NULL;
e820abd7 1894 unsigned int events = 0;
7d747d22
JA
1895
1896 while (!is_done() && events < rb_batch) {
1897 struct blk_io_trace *bit;
1898 struct trace *t;
db7e0552 1899 int pdu_len, should_block, ret;
7d747d22
JA
1900 __u32 magic;
1901
d36421e4 1902 bit = bit_alloc();
cb2a1a62 1903
c0e0dbc2
JA
1904 should_block = !events || always_block;
1905
db7e0552
JA
1906 ret = read_data(fd, bit, sizeof(*bit), should_block, fdblock);
1907 if (ret) {
eb9bd4e9 1908 bit_free(bit);
db7e0552
JA
1909 if (!events && ret < 0)
1910 events = ret;
cb2a1a62 1911 break;
eb9bd4e9 1912 }
cb2a1a62 1913
017d1660
JA
1914 /*
1915 * look at first trace to check whether we need to convert
1916 * data in the future
1917 */
1918 if (data_is_native == -1 && check_data_endianness(bit))
1919 break;
1920
1921 magic = get_magic(bit);
7d747d22
JA
1922 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
1923 fprintf(stderr, "Bad magic %x\n", magic);
1924 break;
1925 }
1926
017d1660 1927 pdu_len = get_pdulen(bit);
7d747d22
JA
1928 if (pdu_len) {
1929 void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
1930
c0e0dbc2 1931 if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1, fdblock)) {
eb9bd4e9 1932 bit_free(ptr);
7d747d22 1933 break;
eb9bd4e9 1934 }
7d747d22
JA
1935
1936 bit = ptr;
1937 }
1938
d6222db8
JA
1939 trace_to_cpu(bit);
1940
1941 if (verify_trace(bit)) {
1942 bit_free(bit);
1943 continue;
1944 }
1945
bfc70ad5
JA
1946 /*
1947 * not a real trace, so grab and handle it here
1948 */
1949 if (bit->action & BLK_TC_ACT(BLK_TC_NOTIFY)) {
1950 add_ppm_hash(bit->pid, (char *) bit + sizeof(*bit));
1951 continue;
1952 }
1953
d36421e4 1954 t = t_alloc();
cb2a1a62
JA
1955 memset(t, 0, sizeof(*t));
1956 t->bit = bit;
a43c1c17 1957 t->read_sequence = read_sequence;
cb2a1a62 1958
7d747d22
JA
1959 t->next = trace_list;
1960 trace_list = t;
1f7afa72 1961
f7bd1a9b 1962 if (!pdi || pdi->dev != bit->device)
287fa3d6
JA
1963 pdi = get_dev_info(bit->device);
1964
1965 if (bit->time > pdi->last_read_time)
1966 pdi->last_read_time = bit->time;
1967
7d747d22 1968 events++;
cb2a1a62
JA
1969 }
1970
7d747d22 1971 return events;
cb2a1a62
JA
1972}
1973
d5396421 1974static int do_file(void)
d0ca268b 1975{
7d747d22 1976 struct per_cpu_info *pci;
73877e12
NS
1977 struct per_dev_info *pdi;
1978 int i, j, events, events_added;
d0ca268b 1979
7d747d22
JA
1980 /*
1981 * first prepare all files for reading
1982 */
e8741a4a 1983 for (i = 0; i < ndevices; i++) {
73877e12
NS
1984 pdi = &devices[i];
1985 pdi->nfiles = 0;
73877e12 1986
74feaeb5 1987 for (j = 0;; j++) {
e7c9f3ff 1988 struct stat st;
d1d7f15f 1989 int len = 0;
6e0073ed 1990 char *p, *dname;
87b72777 1991
e7c9f3ff
NS
1992 pci = get_cpu_info(pdi, j);
1993 pci->cpu = j;
7d747d22 1994 pci->fd = -1;
c0e0dbc2 1995 pci->fdblock = -1;
6e0073ed
JA
1996
1997 p = strdup(pdi->name);
1998 dname = dirname(p);
1999 if (strcmp(dname, ".")) {
2000 input_dir = dname;
2001 p = strdup(pdi->name);
2002 strcpy(pdi->name, basename(p));
2003 }
2004 free(p);
d0ca268b 2005
d1d7f15f
JA
2006 if (input_dir)
2007 len = sprintf(pci->fname, "%s/", input_dir);
2008
2009 snprintf(pci->fname + len, sizeof(pci->fname)-1-len,
7d747d22 2010 "%s.blktrace.%d", pdi->name, pci->cpu);
e7c9f3ff
NS
2011 if (stat(pci->fname, &st) < 0)
2012 break;
74feaeb5
AB
2013 if (st.st_size) {
2014 pci->fd = open(pci->fname, O_RDONLY);
2015 if (pci->fd < 0) {
2016 perror(pci->fname);
2017 continue;
2018 }
e7c9f3ff
NS
2019 }
2020
7d747d22 2021 printf("Input file %s added\n", pci->fname);
73877e12 2022 pdi->nfiles++;
824c2b39 2023 cpu_mark_online(pdi, pci->cpu);
d0ca268b 2024 }
d5396421
JA
2025 }
2026
7d747d22
JA
2027 /*
2028 * now loop over the files reading in the data
2029 */
412819ce 2030 do {
53c68c88
JA
2031 unsigned long long youngest;
2032
7d747d22 2033 events_added = 0;
287fa3d6 2034 last_allowed_time = -1ULL;
a43c1c17 2035 read_sequence++;
d5396421 2036
7d747d22 2037 for (i = 0; i < ndevices; i++) {
73877e12 2038 pdi = &devices[i];
66930177 2039 pdi->last_read_time = -1ULL;
73877e12
NS
2040
2041 for (j = 0; j < pdi->nfiles; j++) {
d5396421 2042
73877e12 2043 pci = get_cpu_info(pdi, j);
d5396421 2044
7d747d22
JA
2045 if (pci->fd == -1)
2046 continue;
51128a28 2047
210824c3
JA
2048 pci->smallest_seq_read = -1;
2049
c0e0dbc2 2050 events = read_events(pci->fd, 1, &pci->fdblock);
db7e0552 2051 if (events <= 0) {
824c2b39 2052 cpu_mark_offline(pdi, pci->cpu);
7d747d22
JA
2053 close(pci->fd);
2054 pci->fd = -1;
2055 continue;
2056 }
d5396421 2057
287fa3d6
JA
2058 if (pdi->last_read_time < last_allowed_time)
2059 last_allowed_time = pdi->last_read_time;
d5396421 2060
7d747d22
JA
2061 events_added += events;
2062 }
2ff323b0 2063 }
d5396421 2064
53c68c88
JA
2065 if (sort_entries(&youngest))
2066 break;
2067
2068 if (youngest > stopwatch_end)
287fa3d6
JA
2069 break;
2070
a649216c 2071 show_entries_rb(0);
cb2a1a62 2072
7d747d22 2073 } while (events_added);
d5396421 2074
a649216c
JA
2075 if (rb_sort_entries)
2076 show_entries_rb(1);
2077
7d747d22 2078 return 0;
412819ce 2079}
d5396421 2080
412819ce
JA
2081static int do_stdin(void)
2082{
53c68c88 2083 unsigned long long youngest;
c0e0dbc2 2084 int fd, events, fdblock;
d5396421 2085
be925321 2086 last_allowed_time = -1ULL;
1f79c4a0 2087 fd = dup(STDIN_FILENO);
0b07f23e
JA
2088 if (fd == -1) {
2089 perror("dup stdin");
2090 return -1;
2091 }
d5396421 2092
c0e0dbc2 2093 fdblock = -1;
db7e0552 2094 while ((events = read_events(fd, 0, &fdblock)) > 0) {
4ab42801 2095 read_sequence++;
412819ce 2096
210824c3 2097#if 0
0b07f23e 2098 smallest_seq_read = -1U;
210824c3 2099#endif
0b07f23e 2100
53c68c88
JA
2101 if (sort_entries(&youngest))
2102 break;
2103
2104 if (youngest > stopwatch_end)
2ff323b0
JA
2105 break;
2106
763d936e 2107 show_entries_rb(0);
0b07f23e 2108 }
d5396421 2109
a649216c
JA
2110 if (rb_sort_entries)
2111 show_entries_rb(1);
2112
d5396421 2113 close(fd);
d5396421
JA
2114 return 0;
2115}
d0ca268b 2116
cbc927b6 2117static void show_stats(void)
412819ce 2118{
cbc927b6
JA
2119 if (!ofp)
2120 return;
2121 if (stats_printed)
2122 return;
2123
2124 stats_printed = 1;
2125
2126 if (per_process_stats)
2127 show_process_stats();
2128
2129 if (per_device_and_cpu_stats)
2130 show_device_and_cpu_stats();
2131
152f6476 2132 fflush(ofp);
412819ce
JA
2133}
2134
e820abd7 2135static void handle_sigint(__attribute__((__unused__)) int sig)
412819ce
JA
2136{
2137 done = 1;
412819ce
JA
2138}
2139
46e6968b
NS
2140/*
2141 * Extract start and duration times from a string, allowing
2142 * us to specify a time interval of interest within a trace.
2143 * Format: "duration" (start is zero) or "start:duration".
2144 */
2145static int find_stopwatch_interval(char *string)
2146{
2147 double value;
2148 char *sp;
2149
2150 value = strtod(string, &sp);
2151 if (sp == string) {
2152 fprintf(stderr,"Invalid stopwatch timer: %s\n", string);
2153 return 1;
2154 }
2155 if (*sp == ':') {
2156 stopwatch_start = DOUBLE_TO_NANO_ULL(value);
2157 string = sp + 1;
2158 value = strtod(string, &sp);
2159 if (sp == string || *sp != '\0') {
2160 fprintf(stderr,"Invalid stopwatch duration time: %s\n",
2161 string);
2162 return 1;
2163 }
2164 } else if (*sp != '\0') {
2165 fprintf(stderr,"Invalid stopwatch start timer: %s\n", string);
2166 return 1;
2167 }
1b928247
JA
2168 stopwatch_end = DOUBLE_TO_NANO_ULL(value);
2169 if (stopwatch_end <= stopwatch_start) {
2170 fprintf(stderr, "Invalid stopwatch interval: %Lu -> %Lu\n",
2171 stopwatch_start, stopwatch_end);
2172 return 1;
2173 }
2174
46e6968b
NS
2175 return 0;
2176}
2177
52724a0e
JA
2178static char usage_str[] = \
2179 "[ -i <input name> ] [-o <output name> [ -s ] [ -t ] [ -q ]\n" \
2180 "[ -w start:stop ] [ -f output format ] [ -F format spec ] [ -v] \n\n" \
2181 "\t-i Input file containing trace data, or '-' for stdin\n" \
d1d7f15f 2182 "\t-D Directory to prepend to input file names\n" \
52724a0e
JA
2183 "\t-o Output file. If not given, output is stdout\n" \
2184 "\t-b stdin read batching\n" \
2185 "\t-s Show per-program io statistics\n" \
ac740d98 2186 "\t-h Hash processes by name, not pid\n" \
52724a0e
JA
2187 "\t-t Track individual ios. Will tell you the time a request took\n" \
2188 "\t to get queued, to get dispatched, and to get completed\n" \
2189 "\t-q Quiet. Don't display any stats at the end of the trace\n" \
2190 "\t-w Only parse data between the given time interval in seconds.\n" \
2191 "\t If 'start' isn't given, blkparse defaults the start time to 0\n" \
d915dee6
JA
2192 "\t-f Output format. Customize the output format. The format field\n" \
2193 "\t identifies can be found in the documentation\n" \
52724a0e 2194 "\t-F Format specification. Can be found in the documentation\n" \
57ea8602
JA
2195 "\t-v More verbose for marginal errors\n" \
2196 "\t-V Print program version info\n\n";
52724a0e 2197
1f79c4a0
JA
2198static void usage(char *prog)
2199{
52724a0e 2200 fprintf(stderr, "Usage: %s %s %s", prog, blkparse_version, usage_str);
1f79c4a0
JA
2201}
2202
d5396421
JA
2203int main(int argc, char *argv[])
2204{
152f6476 2205 char *ofp_buffer;
98f8386b 2206 int i, c, ret, mode;
98f8386b 2207 int act_mask_tmp = 0;
d5396421
JA
2208
2209 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
2210 switch (c) {
98f8386b
AB
2211 case 'a':
2212 i = find_mask_map(optarg);
2213 if (i < 0) {
2214 fprintf(stderr,"Invalid action mask %s\n",
2215 optarg);
2216 return 1;
2217 }
2218 act_mask_tmp |= i;
2219 break;
2220
2221 case 'A':
2222 if ((sscanf(optarg, "%x", &i) != 1) ||
2223 !valid_act_opt(i)) {
2224 fprintf(stderr,
2225 "Invalid set action mask %s/0x%x\n",
2226 optarg, i);
2227 return 1;
2228 }
2229 act_mask_tmp = i;
2230 break;
d5396421 2231 case 'i':
e7c9f3ff
NS
2232 if (!strcmp(optarg, "-") && !pipeline)
2233 pipeline = 1;
2234 else if (resize_devices(optarg) != 0)
2235 return 1;
d5396421 2236 break;
d1d7f15f
JA
2237 case 'D':
2238 input_dir = optarg;
2239 break;
d5396421 2240 case 'o':
66efebf8 2241 output_name = optarg;
d5396421 2242 break;
79f19470
JA
2243 case 'b':
2244 rb_batch = atoi(optarg);
2245 if (rb_batch <= 0)
2246 rb_batch = RB_BATCH_DEFAULT;
2247 break;
152f6476
JA
2248 case 's':
2249 per_process_stats = 1;
2250 break;
7997c5b0
JA
2251 case 't':
2252 track_ios = 1;
2253 break;
1e1c60f1
NS
2254 case 'q':
2255 per_device_and_cpu_stats = 0;
2256 break;
46e6968b
NS
2257 case 'w':
2258 if (find_stopwatch_interval(optarg) != 0)
2259 return 1;
2260 break;
ab197ca7
AB
2261 case 'f':
2262 set_all_format_specs(optarg);
2263 break;
2264 case 'F':
2265 if (add_format_spec(optarg) != 0)
2266 return 1;
2267 break;
d915dee6 2268 case 'h':
715d8021 2269 ppi_hash_by_pid = 0;
bf0720af 2270 break;
52724a0e 2271 case 'v':
57ea8602
JA
2272 verbose++;
2273 break;
2274 case 'V':
52724a0e
JA
2275 printf("%s version %s\n", argv[0], blkparse_version);
2276 return 0;
d5396421 2277 default:
1f79c4a0 2278 usage(argv[0]);
d5396421
JA
2279 return 1;
2280 }
d0ca268b
JA
2281 }
2282
e7c9f3ff
NS
2283 while (optind < argc) {
2284 if (!strcmp(argv[optind], "-") && !pipeline)
2285 pipeline = 1;
2286 else if (resize_devices(argv[optind]) != 0)
2287 return 1;
2288 optind++;
2289 }
2290
2291 if (!pipeline && !ndevices) {
1f79c4a0 2292 usage(argv[0]);
d5396421
JA
2293 return 1;
2294 }
2295
98f8386b
AB
2296 if (act_mask_tmp != 0)
2297 act_mask = act_mask_tmp;
2298
7997c5b0 2299 memset(&rb_sort_root, 0, sizeof(rb_sort_root));
412819ce
JA
2300
2301 signal(SIGINT, handle_sigint);
2302 signal(SIGHUP, handle_sigint);
2303 signal(SIGTERM, handle_sigint);
d5396421 2304
d69db225
JA
2305 setlocale(LC_NUMERIC, "en_US");
2306
a66877e6 2307 if (!output_name) {
152f6476 2308 ofp = fdopen(STDOUT_FILENO, "w");
a66877e6
JA
2309 mode = _IOLBF;
2310 } else {
152f6476
JA
2311 char ofname[128];
2312
7a9690c0 2313 snprintf(ofname, sizeof(ofname) - 1, "%s", output_name);
152f6476 2314 ofp = fopen(ofname, "w");
a66877e6 2315 mode = _IOFBF;
152f6476
JA
2316 }
2317
2318 if (!ofp) {
2319 perror("fopen");
2320 return 1;
2321 }
2322
2323 ofp_buffer = malloc(4096);
a66877e6 2324 if (setvbuf(ofp, ofp_buffer, mode, 4096)) {
152f6476
JA
2325 perror("setvbuf");
2326 return 1;
2327 }
2328
e7c9f3ff 2329 if (pipeline)
d5396421
JA
2330 ret = do_stdin();
2331 else
2332 ret = do_file();
2333
cbc927b6 2334 show_stats();
eb9bd4e9 2335 free(ofp_buffer);
d5396421 2336 return ret;
d0ca268b 2337}