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