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