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