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