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