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