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