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