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