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