[PATCH] blkparse: kill old force debug print
[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>
46e6968b 32#include <limits.h>
d0ca268b 33
8fc0abbc
JA
34#include "blktrace.h"
35#include "rbtree.h"
bf0720af 36#include "jhash.h"
d0ca268b 37
52724a0e
JA
38static char blkparse_version[] = "0.90";
39
e7c9f3ff 40struct per_dev_info {
f7bd1a9b 41 dev_t dev;
e7c9f3ff
NS
42 char *name;
43
44 int backwards;
45 unsigned long long events;
46 unsigned long long last_reported_time;
287fa3d6 47 unsigned long long last_read_time;
e7c9f3ff 48 struct io_stats io_stats;
cb2a1a62 49 unsigned long last_sequence;
dcf0f7ed 50 unsigned long skips;
e7c9f3ff 51
2a1b3424
JA
52 struct rb_root rb_last;
53 unsigned long rb_last_entries;
54
f7bd1a9b
JA
55 struct rb_root rb_track;
56
73877e12 57 int nfiles;
e7c9f3ff
NS
58 int ncpus;
59 struct per_cpu_info *cpus;
60};
61
152f6476
JA
62struct per_process_info {
63 char name[16];
64 __u32 pid;
65 struct io_stats io_stats;
66 struct per_process_info *hash_next, *list_next;
715d8021 67 int more_than_one;
50adc0ba
JA
68
69 /*
70 * individual io stats
71 */
b9d40d6f
JA
72 unsigned long long longest_allocation_wait[2];
73 unsigned long long longest_dispatch_wait[2];
74 unsigned long long longest_completion_wait[2];
d0ca268b
JA
75};
76
152f6476 77#define PPI_HASH_SHIFT (8)
bf0720af
JA
78#define PPI_HASH_SIZE (1 << PPI_HASH_SHIFT)
79#define PPI_HASH_MASK (PPI_HASH_SIZE - 1)
80static struct per_process_info *ppi_hash_table[PPI_HASH_SIZE];
152f6476 81static struct per_process_info *ppi_list;
886ecf0e 82static int ppi_list_entries;
152f6476 83
7d1c0411 84#define S_OPTS "i:o:b:stqw:f:F:vnm"
d5396421
JA
85static struct option l_opts[] = {
86 {
87 .name = "input",
428683db 88 .has_arg = required_argument,
d5396421
JA
89 .flag = NULL,
90 .val = 'i'
91 },
92 {
93 .name = "output",
428683db 94 .has_arg = required_argument,
d5396421
JA
95 .flag = NULL,
96 .val = 'o'
97 },
79f19470
JA
98 {
99 .name = "batch",
428683db 100 .has_arg = required_argument,
79f19470
JA
101 .flag = NULL,
102 .val = 'b'
103 },
152f6476
JA
104 {
105 .name = "per program stats",
428683db 106 .has_arg = no_argument,
152f6476
JA
107 .flag = NULL,
108 .val = 's'
109 },
7997c5b0
JA
110 {
111 .name = "track ios",
428683db 112 .has_arg = no_argument,
7997c5b0
JA
113 .flag = NULL,
114 .val = 't'
115 },
1e1c60f1
NS
116 {
117 .name = "quiet",
428683db 118 .has_arg = no_argument,
1e1c60f1
NS
119 .flag = NULL,
120 .val = 'q'
121 },
46e6968b
NS
122 {
123 .name = "stopwatch",
428683db 124 .has_arg = required_argument,
46e6968b
NS
125 .flag = NULL,
126 .val = 'w'
127 },
ab197ca7
AB
128 {
129 .name = "format",
428683db 130 .has_arg = required_argument,
ab197ca7
AB
131 .flag = NULL,
132 .val = 'f'
133 },
134 {
135 .name = "format-spec",
428683db 136 .has_arg = required_argument,
ab197ca7
AB
137 .flag = NULL,
138 .val = 'F'
139 },
bf0720af
JA
140 {
141 .name = "hash by name",
142 .has_arg = no_argument,
143 .flag = NULL,
144 .val = 'n'
145 },
7d1c0411
JA
146 {
147 .name = "missing",
148 .has_arg = no_argument,
149 .flag = NULL,
150 .val = 'm'
151 },
52724a0e
JA
152 {
153 .name = "version",
154 .has_arg = no_argument,
155 .flag = NULL,
156 .val = 'v'
157 },
d5396421
JA
158};
159
7997c5b0
JA
160/*
161 * for sorting the displayed output
162 */
8fc0abbc
JA
163struct trace {
164 struct blk_io_trace *bit;
165 struct rb_node rb_node;
cb2a1a62 166 struct trace *next;
8fc0abbc
JA
167};
168
cb2a1a62 169static struct rb_root rb_sort_root;
a649216c
JA
170static unsigned long rb_sort_entries;
171
cb2a1a62
JA
172static struct trace *trace_list;
173
d36421e4
JA
174/*
175 * allocation cache
176 */
177static struct blk_io_trace *bit_alloc_list;
178static struct trace *t_alloc_list;
179
7997c5b0
JA
180/*
181 * for tracking individual ios
182 */
183struct io_track {
184 struct rb_node rb_node;
185
186 __u64 sector;
187 __u32 pid;
bf0720af 188 char comm[16];
95c15013 189 unsigned long long allocation_time;
7997c5b0
JA
190 unsigned long long queue_time;
191 unsigned long long dispatch_time;
192 unsigned long long completion_time;
193};
194
e7c9f3ff
NS
195static int ndevices;
196static struct per_dev_info *devices;
197static char *get_dev_name(struct per_dev_info *, char *, int);
d0ca268b 198
71d5d4c9 199FILE *ofp = NULL;
e7c9f3ff
NS
200static char *output_name;
201
202static unsigned long long genesis_time;
287fa3d6 203static unsigned long long last_allowed_time;
1c7c54aa 204static unsigned int smallest_seq_read;
46e6968b
NS
205static unsigned long long stopwatch_start; /* start from zero by default */
206static unsigned long long stopwatch_end = ULONG_LONG_MAX; /* "infinity" */
152f6476
JA
207
208static int per_process_stats;
7997c5b0 209static int track_ios;
bf0720af 210static int ppi_hash_by_pid = 1;
7d1c0411 211static int print_missing;
d0ca268b 212
1d24fc14
JA
213static unsigned int t_alloc_cache;
214static unsigned int bit_alloc_cache;
215
7d747d22 216#define RB_BATCH_DEFAULT (512)
e820abd7 217static unsigned int rb_batch = RB_BATCH_DEFAULT;
79f19470 218
e7c9f3ff
NS
219static int pipeline;
220
412819ce
JA
221#define is_done() (*(volatile int *)(&done))
222static volatile int done;
223
bf0720af
JA
224#define JHASH_RANDOM (0x3af5f2ee)
225
226static inline int ppi_hash_pid(__u32 pid)
227{
228 return jhash_1word(pid, JHASH_RANDOM) & PPI_HASH_MASK;
229}
230
231static inline int ppi_hash_name(const char *name)
152f6476 232{
bf0720af
JA
233 return jhash(name, 16, JHASH_RANDOM) & PPI_HASH_MASK;
234}
235
236static inline int ppi_hash(struct per_process_info *ppi)
237{
238 if (ppi_hash_by_pid)
239 return ppi_hash_pid(ppi->pid);
240
bf0720af 241 return ppi_hash_name(ppi->name);
152f6476
JA
242}
243
244static inline void add_process_to_hash(struct per_process_info *ppi)
245{
bf0720af 246 const int hash_idx = ppi_hash(ppi);
152f6476 247
bf0720af
JA
248 ppi->hash_next = ppi_hash_table[hash_idx];
249 ppi_hash_table[hash_idx] = ppi;
152f6476
JA
250}
251
252static inline void add_process_to_list(struct per_process_info *ppi)
253{
254 ppi->list_next = ppi_list;
255 ppi_list = ppi;
886ecf0e 256 ppi_list_entries++;
152f6476
JA
257}
258
bf0720af
JA
259static struct per_process_info *find_process_by_name(char *name)
260{
261 const int hash_idx = ppi_hash_name(name);
262 struct per_process_info *ppi;
263
264 ppi = ppi_hash_table[hash_idx];
265 while (ppi) {
266 if (!strcmp(ppi->name, name))
267 return ppi;
268
269 ppi = ppi->hash_next;
270 }
271
272 return NULL;
273}
274
152f6476
JA
275static struct per_process_info *find_process_by_pid(__u32 pid)
276{
bf0720af 277 const int hash_idx = ppi_hash_pid(pid);
152f6476
JA
278 struct per_process_info *ppi;
279
bf0720af 280 ppi = ppi_hash_table[hash_idx];
152f6476
JA
281 while (ppi) {
282 if (ppi->pid == pid)
283 return ppi;
284
285 ppi = ppi->hash_next;
286 }
287
288 return NULL;
289}
290
bf0720af
JA
291static struct per_process_info *find_process(__u32 pid, char *name)
292{
715d8021
JA
293 struct per_process_info *ppi;
294
bf0720af
JA
295 if (ppi_hash_by_pid)
296 return find_process_by_pid(pid);
297
715d8021
JA
298 ppi = find_process_by_name(name);
299 if (ppi && ppi->pid != pid)
300 ppi->more_than_one = 1;
301
302 return ppi;
bf0720af
JA
303}
304
23211fdf
JA
305static inline int trace_rb_insert(struct trace *t, struct rb_root *root,
306 int check_time)
7997c5b0 307{
2a1b3424 308 struct rb_node **p = &root->rb_node;
7997c5b0
JA
309 struct rb_node *parent = NULL;
310 struct trace *__t;
311
312 while (*p) {
313 parent = *p;
2a1b3424 314
7997c5b0
JA
315 __t = rb_entry(parent, struct trace, rb_node);
316
23211fdf
JA
317 if (check_time) {
318 if (t->bit->time < __t->bit->time) {
319 p = &(*p)->rb_left;
320 continue;
321 } else if (t->bit->time > __t->bit->time) {
322 p = &(*p)->rb_right;
323 continue;
324 }
325 }
326 if (t->bit->device < __t->bit->device)
e7c9f3ff
NS
327 p = &(*p)->rb_left;
328 else if (t->bit->device > __t->bit->device)
329 p = &(*p)->rb_right;
dcf0f7ed
JA
330 else if (t->bit->sequence < __t->bit->sequence)
331 p = &(*p)->rb_left;
332 else if (t->bit->sequence > __t->bit->sequence)
333 p = &(*p)->rb_right;
e7c9f3ff
NS
334 else if (t->bit->device == __t->bit->device) {
335 fprintf(stderr,
336 "sequence alias (%d) on device %d,%d!\n",
337 t->bit->sequence,
338 MAJOR(t->bit->device), MINOR(t->bit->device));
7997c5b0
JA
339 return 1;
340 }
341 }
342
343 rb_link_node(&t->rb_node, parent, p);
2a1b3424 344 rb_insert_color(&t->rb_node, root);
7997c5b0
JA
345 return 0;
346}
347
2a1b3424 348static inline int trace_rb_insert_sort(struct trace *t)
e3556946 349{
23211fdf 350 if (!trace_rb_insert(t, &rb_sort_root, 1)) {
2a1b3424
JA
351 rb_sort_entries++;
352 return 0;
353 }
354
355 return 1;
356}
357
358static inline int trace_rb_insert_last(struct per_dev_info *pdi,struct trace *t)
359{
23211fdf 360 if (!trace_rb_insert(t, &pdi->rb_last, 1)) {
2a1b3424
JA
361 pdi->rb_last_entries++;
362 return 0;
363 }
364
365 return 1;
366}
367
368static struct trace *trace_rb_find(dev_t device, unsigned long sequence,
369 struct rb_root *root, int order)
370{
371 struct rb_node *n = root->rb_node;
372 struct rb_node *prev = NULL;
e3556946
JA
373 struct trace *__t;
374
2a1b3424
JA
375 while (n) {
376 __t = rb_entry(n, struct trace, rb_node);
377 prev = n;
e3556946 378
0583b6a2 379 if (device < __t->bit->device)
2a1b3424 380 n = n->rb_left;
0583b6a2 381 else if (device > __t->bit->device)
2a1b3424 382 n = n->rb_right;
0583b6a2 383 else if (sequence < __t->bit->sequence)
2a1b3424 384 n = n->rb_left;
e3556946 385 else if (sequence > __t->bit->sequence)
2a1b3424 386 n = n->rb_right;
e3556946
JA
387 else
388 return __t;
389 }
390
2a1b3424
JA
391 /*
392 * hack - the list may not be sequence ordered because some
393 * events don't have sequence and time matched. so we end up
394 * being a little off in the rb lookup here, because we don't
395 * know the time we are looking for. compensate by browsing
396 * a little ahead from the last entry to find the match
397 */
398 if (order && prev) {
399 int max = 5;
400
401 while (((n = rb_next(prev)) != NULL) && max--) {
402 __t = rb_entry(n, struct trace, rb_node);
403
404 if (__t->bit->device == device &&
405 __t->bit->sequence == sequence)
406 return __t;
407
408 prev = n;
409 }
410 }
411
e3556946
JA
412 return NULL;
413}
414
2a1b3424
JA
415static inline struct trace *trace_rb_find_sort(dev_t dev, unsigned long seq)
416{
417 return trace_rb_find(dev, seq, &rb_sort_root, 1);
418}
419
420static inline struct trace *trace_rb_find_last(struct per_dev_info *pdi,
421 unsigned long seq)
422{
f7bd1a9b 423 return trace_rb_find(pdi->dev, seq, &pdi->rb_last, 0);
2a1b3424
JA
424}
425
f7bd1a9b 426static inline int track_rb_insert(struct per_dev_info *pdi,struct io_track *iot)
7997c5b0 427{
f7bd1a9b 428 struct rb_node **p = &pdi->rb_track.rb_node;
7997c5b0
JA
429 struct rb_node *parent = NULL;
430 struct io_track *__iot;
431
432 while (*p) {
433 parent = *p;
7997c5b0
JA
434 __iot = rb_entry(parent, struct io_track, rb_node);
435
f7bd1a9b 436 if (iot->sector < __iot->sector)
7997c5b0
JA
437 p = &(*p)->rb_left;
438 else if (iot->sector > __iot->sector)
439 p = &(*p)->rb_right;
440 else {
e7c9f3ff 441 fprintf(stderr,
ab197ca7
AB
442 "sector alias (%Lu) on device %d,%d!\n",
443 (unsigned long long) iot->sector,
f7bd1a9b 444 MAJOR(pdi->dev), MINOR(pdi->dev));
7997c5b0
JA
445 return 1;
446 }
447 }
448
449 rb_link_node(&iot->rb_node, parent, p);
f7bd1a9b 450 rb_insert_color(&iot->rb_node, &pdi->rb_track);
7997c5b0
JA
451 return 0;
452}
453
f7bd1a9b 454static struct io_track *__find_track(struct per_dev_info *pdi, __u64 sector)
7997c5b0 455{
f7bd1a9b 456 struct rb_node *n = pdi->rb_track.rb_node;
7997c5b0
JA
457 struct io_track *__iot;
458
2a1b3424
JA
459 while (n) {
460 __iot = rb_entry(n, struct io_track, rb_node);
7997c5b0 461
f7bd1a9b 462 if (sector < __iot->sector)
2a1b3424 463 n = n->rb_left;
7997c5b0 464 else if (sector > __iot->sector)
2a1b3424 465 n = n->rb_right;
7997c5b0
JA
466 else
467 return __iot;
468 }
469
470 return NULL;
471}
472
f7bd1a9b
JA
473static struct io_track *find_track(struct per_dev_info *pdi, __u32 pid,
474 char *comm, __u64 sector)
7997c5b0 475{
916b5501 476 struct io_track *iot;
7997c5b0 477
f7bd1a9b 478 iot = __find_track(pdi, sector);
7997c5b0
JA
479 if (!iot) {
480 iot = malloc(sizeof(*iot));
50adc0ba 481 iot->pid = pid;
bf0720af 482 memcpy(iot->comm, comm, sizeof(iot->comm));
7997c5b0 483 iot->sector = sector;
f7bd1a9b 484 track_rb_insert(pdi, iot);
7997c5b0
JA
485 }
486
487 return iot;
488}
489
f7bd1a9b
JA
490static void log_track_frontmerge(struct per_dev_info *pdi,
491 struct blk_io_trace *t)
2e3e8ded
JA
492{
493 struct io_track *iot;
494
495 if (!track_ios)
496 return;
2e3e8ded 497
f7bd1a9b 498 iot = __find_track(pdi, t->sector + (t->bytes >> 9));
cb2a1a62 499 if (!iot) {
acd70d21 500 fprintf(stderr, "merge not found for (%d,%d): %llu\n",
f7bd1a9b 501 MAJOR(pdi->dev), MINOR(pdi->dev),
85e84946 502 (unsigned long long) t->sector + (t->bytes >> 9));
cb2a1a62 503 return;
2e3e8ded 504 }
cb2a1a62 505
f7bd1a9b 506 rb_erase(&iot->rb_node, &pdi->rb_track);
a01516de 507 iot->sector -= t->bytes >> 9;
f7bd1a9b 508 track_rb_insert(pdi, iot);
2e3e8ded
JA
509}
510
f7bd1a9b 511static void log_track_getrq(struct per_dev_info *pdi, struct blk_io_trace *t)
2e3e8ded
JA
512{
513 struct io_track *iot;
514
515 if (!track_ios)
516 return;
517
f7bd1a9b 518 iot = find_track(pdi, t->pid, t->comm, t->sector);
95c15013
JA
519 iot->allocation_time = t->time;
520}
521
95c15013 522/*
b6076a9b 523 * return time between rq allocation and insertion
95c15013 524 */
f7bd1a9b
JA
525static unsigned long long log_track_insert(struct per_dev_info *pdi,
526 struct blk_io_trace *t)
95c15013 527{
50adc0ba 528 unsigned long long elapsed;
95c15013
JA
529 struct io_track *iot;
530
531 if (!track_ios)
532 return -1;
533
f7bd1a9b 534 iot = find_track(pdi, t->pid, t->comm, t->sector);
2e3e8ded 535 iot->queue_time = t->time;
acd70d21
JA
536
537 if (!iot->allocation_time)
538 return -1;
539
50adc0ba
JA
540 elapsed = iot->queue_time - iot->allocation_time;
541
542 if (per_process_stats) {
bf0720af 543 struct per_process_info *ppi = find_process(iot->pid,iot->comm);
b9d40d6f 544 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
50adc0ba 545
b9d40d6f
JA
546 if (ppi && elapsed > ppi->longest_allocation_wait[w])
547 ppi->longest_allocation_wait[w] = elapsed;
50adc0ba
JA
548 }
549
550 return elapsed;
2e3e8ded
JA
551}
552
553/*
554 * return time between queue and issue
555 */
f7bd1a9b
JA
556static unsigned long long log_track_issue(struct per_dev_info *pdi,
557 struct blk_io_trace *t)
2e3e8ded 558{
50adc0ba 559 unsigned long long elapsed;
2e3e8ded
JA
560 struct io_track *iot;
561
562 if (!track_ios)
563 return -1;
564 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
565 return -1;
566
f7bd1a9b 567 iot = __find_track(pdi, t->sector);
cb2a1a62 568 if (!iot) {
acd70d21 569 fprintf(stderr, "issue not found for (%d,%d): %llu\n",
85e84946
NS
570 MAJOR(pdi->dev), MINOR(pdi->dev),
571 (unsigned long long) t->sector);
2e3e8ded 572 return -1;
cb2a1a62 573 }
2e3e8ded
JA
574
575 iot->dispatch_time = t->time;
50adc0ba
JA
576 elapsed = iot->dispatch_time - iot->queue_time;
577
578 if (per_process_stats) {
bf0720af 579 struct per_process_info *ppi = find_process(iot->pid,iot->comm);
b9d40d6f 580 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
50adc0ba 581
b9d40d6f
JA
582 if (ppi && elapsed > ppi->longest_dispatch_wait[w])
583 ppi->longest_dispatch_wait[w] = elapsed;
50adc0ba
JA
584 }
585
586 return elapsed;
2e3e8ded
JA
587}
588
589/*
590 * return time between dispatch and complete
591 */
f7bd1a9b
JA
592static unsigned long long log_track_complete(struct per_dev_info *pdi,
593 struct blk_io_trace *t)
2e3e8ded
JA
594{
595 unsigned long long elapsed;
596 struct io_track *iot;
597
598 if (!track_ios)
599 return -1;
600 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
601 return -1;
602
f7bd1a9b 603 iot = __find_track(pdi, t->sector);
cb2a1a62 604 if (!iot) {
acd70d21 605 fprintf(stderr, "complete not found for (%d,%d): %llu\n",
85e84946
NS
606 MAJOR(pdi->dev), MINOR(pdi->dev),
607 (unsigned long long) t->sector);
2e3e8ded 608 return -1;
cb2a1a62 609 }
2e3e8ded
JA
610
611 iot->completion_time = t->time;
612 elapsed = iot->completion_time - iot->dispatch_time;
613
50adc0ba 614 if (per_process_stats) {
bf0720af 615 struct per_process_info *ppi = find_process(iot->pid,iot->comm);
b9d40d6f 616 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
50adc0ba 617
b9d40d6f
JA
618 if (ppi && elapsed > ppi->longest_completion_wait[w])
619 ppi->longest_completion_wait[w] = elapsed;
50adc0ba
JA
620 }
621
2e3e8ded
JA
622 /*
623 * kill the trace, we don't need it after completion
624 */
f7bd1a9b 625 rb_erase(&iot->rb_node, &pdi->rb_track);
2e3e8ded
JA
626 free(iot);
627
628 return elapsed;
629}
630
631
152f6476
JA
632static struct io_stats *find_process_io_stats(__u32 pid, char *name)
633{
bf0720af 634 struct per_process_info *ppi = find_process(pid, name);
152f6476
JA
635
636 if (!ppi) {
637 ppi = malloc(sizeof(*ppi));
638 memset(ppi, 0, sizeof(*ppi));
bf0720af 639 memcpy(ppi->name, name, 16);
152f6476
JA
640 ppi->pid = pid;
641 add_process_to_hash(ppi);
642 add_process_to_list(ppi);
643 }
644
645 return &ppi->io_stats;
646}
647
e7c9f3ff 648static void resize_cpu_info(struct per_dev_info *pdi, int cpu)
a718bd37 649{
e7c9f3ff
NS
650 struct per_cpu_info *cpus = pdi->cpus;
651 int ncpus = pdi->ncpus;
652 int new_count = cpu + 1;
653 int new_space, size;
a718bd37
NS
654 char *new_start;
655
e7c9f3ff
NS
656 size = new_count * sizeof(struct per_cpu_info);
657 cpus = realloc(cpus, size);
658 if (!cpus) {
659 char name[20];
660 fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n",
661 get_dev_name(pdi, name, sizeof(name)), size);
a718bd37
NS
662 exit(1);
663 }
664
e7c9f3ff
NS
665 new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info));
666 new_space = (new_count - ncpus) * sizeof(struct per_cpu_info);
a718bd37 667 memset(new_start, 0, new_space);
e7c9f3ff
NS
668
669 pdi->ncpus = new_count;
670 pdi->cpus = cpus;
671}
cb2a1a62 672
e7c9f3ff
NS
673static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu)
674{
cb2a1a62
JA
675 struct per_cpu_info *pci;
676
e7c9f3ff
NS
677 if (cpu >= pdi->ncpus)
678 resize_cpu_info(pdi, cpu);
cb2a1a62
JA
679
680 pci = &pdi->cpus[cpu];
681 pci->cpu = cpu;
682 return pci;
a718bd37
NS
683}
684
e7c9f3ff
NS
685
686static int resize_devices(char *name)
a718bd37 687{
e7c9f3ff 688 int size = (ndevices + 1) * sizeof(struct per_dev_info);
c499bf38 689
e7c9f3ff
NS
690 devices = realloc(devices, size);
691 if (!devices) {
692 fprintf(stderr, "Out of memory, device %s (%d)\n", name, size);
693 return 1;
694 }
695 memset(&devices[ndevices], 0, sizeof(struct per_dev_info));
696 devices[ndevices].name = name;
697 ndevices++;
698 return 0;
699}
a718bd37 700
f7bd1a9b 701static struct per_dev_info *get_dev_info(dev_t dev)
e7c9f3ff 702{
cb2a1a62 703 struct per_dev_info *pdi;
e7c9f3ff 704 int i;
c499bf38 705
7d747d22 706 for (i = 0; i < ndevices; i++) {
f7bd1a9b
JA
707 if (!devices[i].dev)
708 devices[i].dev = dev;
709 if (devices[i].dev == dev)
e7c9f3ff 710 return &devices[i];
7d747d22 711 }
cb2a1a62 712
2a1b3424 713 if (resize_devices(NULL))
e7c9f3ff 714 return NULL;
cb2a1a62
JA
715
716 pdi = &devices[ndevices - 1];
f7bd1a9b 717 pdi->dev = dev;
d6222db8 718 pdi->last_sequence = -1;
287fa3d6 719 pdi->last_read_time = 0;
2a1b3424
JA
720 memset(&pdi->rb_last, 0, sizeof(pdi->rb_last));
721 pdi->rb_last_entries = 0;
cb2a1a62 722 return pdi;
a718bd37
NS
723}
724
e7c9f3ff
NS
725static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size)
726{
727 if (pdi->name)
728 snprintf(buffer, size, "%s", pdi->name);
729 else
f7bd1a9b 730 snprintf(buffer, size, "%d,%d",MAJOR(pdi->dev),MINOR(pdi->dev));
e7c9f3ff
NS
731 return buffer;
732}
733
e7c9f3ff 734static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit)
cfab07eb
AB
735{
736 unsigned long long this = bit->time;
e7c9f3ff 737 unsigned long long last = pdi->last_reported_time;
cfab07eb 738
e7c9f3ff
NS
739 pdi->backwards = (this < last) ? 'B' : ' ';
740 pdi->last_reported_time = this;
cfab07eb
AB
741}
742
152f6476
JA
743static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t,
744 int rw)
d0ca268b
JA
745{
746 if (rw) {
152f6476
JA
747 ios->mwrites++;
748 ios->qwrite_kb += t->bytes >> 10;
d0ca268b 749 } else {
152f6476
JA
750 ios->mreads++;
751 ios->qread_kb += t->bytes >> 10;
752 }
753}
754
755static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci,
756 int rw)
757{
758 __account_m(&pci->io_stats, t, rw);
759
760 if (per_process_stats) {
761 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
762
763 __account_m(ios, t, rw);
d0ca268b
JA
764 }
765}
766
b6076a9b
JA
767static inline void __account_queue(struct io_stats *ios, struct blk_io_trace *t,
768 int rw)
d0ca268b
JA
769{
770 if (rw) {
152f6476
JA
771 ios->qwrites++;
772 ios->qwrite_kb += t->bytes >> 10;
d0ca268b 773 } else {
152f6476
JA
774 ios->qreads++;
775 ios->qread_kb += t->bytes >> 10;
776 }
777}
778
b6076a9b
JA
779static inline void account_queue(struct blk_io_trace *t,
780 struct per_cpu_info *pci, int rw)
152f6476 781{
b6076a9b 782 __account_queue(&pci->io_stats, t, rw);
152f6476
JA
783
784 if (per_process_stats) {
785 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
786
b6076a9b 787 __account_queue(ios, t, rw);
d0ca268b
JA
788 }
789}
790
152f6476 791static inline void __account_c(struct io_stats *ios, int rw, unsigned int bytes)
d0ca268b
JA
792{
793 if (rw) {
152f6476
JA
794 ios->cwrites++;
795 ios->cwrite_kb += bytes >> 10;
d0ca268b 796 } else {
152f6476
JA
797 ios->creads++;
798 ios->cread_kb += bytes >> 10;
799 }
800}
801
802static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci,
803 int rw, int bytes)
804{
805 __account_c(&pci->io_stats, rw, bytes);
806
807 if (per_process_stats) {
808 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
809
810 __account_c(ios, rw, bytes);
d0ca268b
JA
811 }
812}
813
b6076a9b
JA
814static inline void __account_issue(struct io_stats *ios, int rw,
815 unsigned int bytes)
afd2d7ad 816{
817 if (rw) {
152f6476
JA
818 ios->iwrites++;
819 ios->iwrite_kb += bytes >> 10;
afd2d7ad 820 } else {
152f6476
JA
821 ios->ireads++;
822 ios->iread_kb += bytes >> 10;
afd2d7ad 823 }
824}
825
b6076a9b
JA
826static inline void account_issue(struct blk_io_trace *t,
827 struct per_cpu_info *pci, int rw)
d0ca268b 828{
b6076a9b 829 __account_issue(&pci->io_stats, rw, t->bytes);
152f6476
JA
830
831 if (per_process_stats) {
832 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
d5396421 833
b6076a9b 834 __account_issue(ios, rw, t->bytes);
152f6476
JA
835 }
836}
837
06639b27
JA
838static inline void __account_unplug(struct io_stats *ios, int timer)
839{
840 if (timer)
841 ios->timer_unplugs++;
842 else
843 ios->io_unplugs++;
844}
845
846static inline void account_unplug(struct blk_io_trace *t,
847 struct per_cpu_info *pci, int timer)
848{
849 __account_unplug(&pci->io_stats, timer);
850
851 if (per_process_stats) {
852 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
853
854 __account_unplug(ios, timer);
855 }
856}
857
f7bd1a9b
JA
858static void log_complete(struct per_dev_info *pdi, struct per_cpu_info *pci,
859 struct blk_io_trace *t, char *act)
ab197ca7 860{
f7bd1a9b 861 process_fmt(act, pci, t, log_track_complete(pdi, t), 0, NULL);
ab197ca7
AB
862}
863
f7bd1a9b
JA
864static void log_insert(struct per_dev_info *pdi, struct per_cpu_info *pci,
865 struct blk_io_trace *t, char *act)
b6076a9b 866{
f7bd1a9b 867 process_fmt(act, pci, t, log_track_insert(pdi, t), 0, NULL);
b6076a9b
JA
868}
869
ab197ca7
AB
870static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t,
871 char *act)
872{
b6076a9b 873 process_fmt(act, pci, t, -1, 0, NULL);
ab197ca7 874}
2e3e8ded 875
f7bd1a9b
JA
876static void log_issue(struct per_dev_info *pdi, struct per_cpu_info *pci,
877 struct blk_io_trace *t, char *act)
ab197ca7 878{
f7bd1a9b 879 process_fmt(act, pci, t, log_track_issue(pdi, t), 0, NULL);
d0ca268b
JA
880}
881
f7bd1a9b
JA
882static void log_merge(struct per_dev_info *pdi, struct per_cpu_info *pci,
883 struct blk_io_trace *t, char *act)
d0ca268b 884{
a01516de 885 if (act[0] == 'F')
f7bd1a9b 886 log_track_frontmerge(pdi, t);
2e3e8ded 887
ab197ca7 888 process_fmt(act, pci, t, -1ULL, 0, NULL);
d0ca268b
JA
889}
890
dfe34da1 891static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t,
3639a11e 892 char *act)
dfe34da1 893{
ab197ca7 894 process_fmt(act, pci, t, -1ULL, 0, NULL);
dfe34da1
JA
895}
896
d5396421 897static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t,
3639a11e 898 char *act)
d0ca268b 899{
ab197ca7 900 process_fmt(act, pci, t, -1ULL, 0, NULL);
d0ca268b
JA
901}
902
ab197ca7 903static void log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t,
3639a11e 904 char *act)
67e14fdc 905{
ab197ca7 906 process_fmt(act, pci, t, -1ULL, 0, NULL);
67e14fdc
JA
907}
908
93f1c611
JA
909static void log_split(struct per_cpu_info *pci, struct blk_io_trace *t,
910 char *act)
911{
912 process_fmt(act, pci, t, -1ULL, 0, NULL);
913}
914
ab197ca7 915static void log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act)
d0ca268b 916{
ab197ca7 917 unsigned char *buf = (unsigned char *) t + sizeof(*t);
d0ca268b 918
ab197ca7 919 process_fmt(act, pci, t, -1ULL, t->pdu_len, buf);
d0ca268b
JA
920}
921
ff3a732c 922static void dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci)
d0ca268b 923{
56f2af81
JA
924 int act = t->action & 0xffff;
925
926 switch (act) {
d0ca268b 927 case __BLK_TA_QUEUE:
3639a11e 928 log_generic(pci, t, "Q");
d0ca268b
JA
929 break;
930 case __BLK_TA_GETRQ:
3639a11e 931 log_generic(pci, t, "G");
d0ca268b
JA
932 break;
933 case __BLK_TA_SLEEPRQ:
3639a11e 934 log_generic(pci, t, "S");
d0ca268b
JA
935 break;
936 case __BLK_TA_REQUEUE:
3639a11e 937 log_generic(pci, t, "R");
d0ca268b
JA
938 break;
939 case __BLK_TA_ISSUE:
ab197ca7 940 log_pc(pci, t, "D");
d0ca268b
JA
941 break;
942 case __BLK_TA_COMPLETE:
3639a11e 943 log_pc(pci, t, "C");
d0ca268b 944 break;
56f2af81
JA
945 case __BLK_TA_INSERT:
946 log_pc(pci, t, "I");
947 break;
d0ca268b 948 default:
56f2af81 949 fprintf(stderr, "Bad pc action %x\n", act);
87b72777 950 break;
d0ca268b 951 }
d0ca268b
JA
952}
953
f7bd1a9b
JA
954static void dump_trace_fs(struct blk_io_trace *t, struct per_dev_info *pdi,
955 struct per_cpu_info *pci)
d0ca268b
JA
956{
957 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
7997c5b0 958 int act = t->action & 0xffff;
d0ca268b 959
7997c5b0 960 switch (act) {
d0ca268b 961 case __BLK_TA_QUEUE:
b6076a9b 962 account_queue(t, pci, w);
3639a11e 963 log_queue(pci, t, "Q");
d0ca268b 964 break;
b6076a9b 965 case __BLK_TA_INSERT:
f7bd1a9b 966 log_insert(pdi, pci, t, "I");
b6076a9b 967 break;
d0ca268b 968 case __BLK_TA_BACKMERGE:
152f6476 969 account_m(t, pci, w);
f7bd1a9b 970 log_merge(pdi, pci, t, "M");
d0ca268b
JA
971 break;
972 case __BLK_TA_FRONTMERGE:
152f6476 973 account_m(t, pci, w);
f7bd1a9b 974 log_merge(pdi, pci, t, "F");
d0ca268b
JA
975 break;
976 case __BLK_TA_GETRQ:
f7bd1a9b 977 log_track_getrq(pdi, t);
3639a11e 978 log_generic(pci, t, "G");
d0ca268b
JA
979 break;
980 case __BLK_TA_SLEEPRQ:
3639a11e 981 log_generic(pci, t, "S");
d0ca268b
JA
982 break;
983 case __BLK_TA_REQUEUE:
152f6476 984 account_c(t, pci, w, -t->bytes);
3639a11e 985 log_queue(pci, t, "R");
d0ca268b
JA
986 break;
987 case __BLK_TA_ISSUE:
b6076a9b 988 account_issue(t, pci, w);
f7bd1a9b 989 log_issue(pdi, pci, t, "D");
d0ca268b
JA
990 break;
991 case __BLK_TA_COMPLETE:
152f6476 992 account_c(t, pci, w, t->bytes);
f7bd1a9b 993 log_complete(pdi, pci, t, "C");
d0ca268b 994 break;
88b1a526 995 case __BLK_TA_PLUG:
3639a11e 996 log_action(pci, t, "P");
88b1a526 997 break;
3639a11e 998 case __BLK_TA_UNPLUG_IO:
06639b27 999 account_unplug(t, pci, 0);
3639a11e
JA
1000 log_unplug(pci, t, "U");
1001 break;
1002 case __BLK_TA_UNPLUG_TIMER:
06639b27 1003 account_unplug(t, pci, 1);
3639a11e 1004 log_unplug(pci, t, "UT");
88b1a526 1005 break;
93f1c611
JA
1006 case __BLK_TA_SPLIT:
1007 log_split(pci, t, "X");
1008 break;
1009 case __BLK_TA_BOUNCE:
1010 log_generic(pci, t, "B");
1011 break;
d0ca268b
JA
1012 default:
1013 fprintf(stderr, "Bad fs action %x\n", t->action);
1f79c4a0 1014 break;
d0ca268b 1015 }
d0ca268b
JA
1016}
1017
ff3a732c
JA
1018static void dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
1019 struct per_dev_info *pdi)
d0ca268b
JA
1020{
1021 if (t->action & BLK_TC_ACT(BLK_TC_PC))
ff3a732c 1022 dump_trace_pc(t, pci);
d0ca268b 1023 else
f7bd1a9b 1024 dump_trace_fs(t, pdi, pci);
87b72777 1025
e7c9f3ff 1026 pdi->events++;
d0ca268b
JA
1027}
1028
152f6476 1029static void dump_io_stats(struct io_stats *ios, char *msg)
5c017e4b 1030{
152f6476
JA
1031 fprintf(ofp, "%s\n", msg);
1032
1033 fprintf(ofp, " Reads Queued: %'8lu, %'8LuKiB\t", ios->qreads, ios->qread_kb);
1034 fprintf(ofp, " Writes Queued: %'8lu, %'8LuKiB\n", ios->qwrites,ios->qwrite_kb);
0a6b8fc4 1035
152f6476
JA
1036 fprintf(ofp, " Read Dispatches: %'8lu, %'8LuKiB\t", ios->ireads, ios->iread_kb);
1037 fprintf(ofp, " Write Dispatches: %'8lu, %'8LuKiB\n", ios->iwrites,ios->iwrite_kb);
1038 fprintf(ofp, " Reads Completed: %'8lu, %'8LuKiB\t", ios->creads, ios->cread_kb);
1039 fprintf(ofp, " Writes Completed: %'8lu, %'8LuKiB\n", ios->cwrites,ios->cwrite_kb);
1040 fprintf(ofp, " Read Merges: %'8lu%8c\t", ios->mreads, ' ');
152f6476 1041 fprintf(ofp, " Write Merges: %'8lu\n", ios->mwrites);
06639b27
JA
1042 fprintf(ofp, " IO unplugs: %'8lu%8c\t", ios->io_unplugs, ' ');
1043 fprintf(ofp, " Timer unplugs: %'8lu\n", ios->timer_unplugs);
5c017e4b
JA
1044}
1045
50adc0ba
JA
1046static void dump_wait_stats(struct per_process_info *ppi)
1047{
b9d40d6f
JA
1048 unsigned long rawait = ppi->longest_allocation_wait[0] / 1000;
1049 unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000;
1050 unsigned long rcwait = ppi->longest_completion_wait[0] / 1000;
1051 unsigned long wawait = ppi->longest_allocation_wait[1] / 1000;
1052 unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000;
1053 unsigned long wcwait = ppi->longest_completion_wait[1] / 1000;
1054
1055 fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' ');
1056 fprintf(ofp, " Allocation wait: %'8lu\n", wawait);
1057 fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' ');
1058 fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait);
1059 fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' ');
1060 fprintf(ofp, " Completion wait: %'8lu\n", wcwait);
50adc0ba
JA
1061}
1062
886ecf0e
JA
1063static int ppi_name_compare(const void *p1, const void *p2)
1064{
1065 struct per_process_info *ppi1 = *((struct per_process_info **) p1);
1066 struct per_process_info *ppi2 = *((struct per_process_info **) p2);
1067 int res;
1068
1069 res = strverscmp(ppi1->name, ppi2->name);
1070 if (!res)
06e6f286 1071 res = ppi1->pid > ppi2->pid;
886ecf0e
JA
1072
1073 return res;
1074}
1075
1076static void sort_process_list(void)
1077{
1078 struct per_process_info **ppis;
1079 struct per_process_info *ppi;
1080 int i = 0;
1081
1082 ppis = malloc(ppi_list_entries * sizeof(struct per_process_info *));
1083
1084 ppi = ppi_list;
1085 while (ppi) {
06e6f286 1086 ppis[i++] = ppi;
886ecf0e
JA
1087 ppi = ppi->list_next;
1088 }
1089
06e6f286 1090 qsort(ppis, ppi_list_entries, sizeof(ppi), ppi_name_compare);
886ecf0e
JA
1091
1092 i = ppi_list_entries - 1;
1093 ppi_list = NULL;
1094 while (i >= 0) {
1095 ppi = ppis[i];
1096
1097 ppi->list_next = ppi_list;
1098 ppi_list = ppi;
1099 i--;
1100 }
50c38702
JA
1101
1102 free(ppis);
886ecf0e
JA
1103}
1104
152f6476
JA
1105static void show_process_stats(void)
1106{
1107 struct per_process_info *ppi;
1108
886ecf0e
JA
1109 sort_process_list();
1110
152f6476
JA
1111 ppi = ppi_list;
1112 while (ppi) {
ce8b6b4f
JA
1113 char name[64];
1114
715d8021 1115 if (ppi->more_than_one)
bf0720af 1116 sprintf(name, "%s (%u, ...)", ppi->name, ppi->pid);
715d8021
JA
1117 else
1118 sprintf(name, "%s (%u)", ppi->name, ppi->pid);
bf0720af 1119
ce8b6b4f 1120 dump_io_stats(&ppi->io_stats, name);
50adc0ba 1121 dump_wait_stats(ppi);
152f6476
JA
1122 ppi = ppi->list_next;
1123 }
1124
1125 fprintf(ofp, "\n");
1126}
1127
e7c9f3ff 1128static void show_device_and_cpu_stats(void)
d0ca268b 1129{
e7c9f3ff
NS
1130 struct per_dev_info *pdi;
1131 struct per_cpu_info *pci;
1132 struct io_stats total, *ios;
1133 int i, j, pci_events;
1134 char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3];
1135 char name[32];
1136
1137 for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
1138
1139 memset(&total, 0, sizeof(total));
1140 pci_events = 0;
1141
1142 if (i > 0)
1143 fprintf(ofp, "\n");
1144
1145 for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) {
1146 if (!pci->nelems)
1147 continue;
1148
1149 ios = &pci->io_stats;
1150 total.qreads += ios->qreads;
1151 total.qwrites += ios->qwrites;
1152 total.creads += ios->creads;
1153 total.cwrites += ios->cwrites;
1154 total.mreads += ios->mreads;
1155 total.mwrites += ios->mwrites;
1156 total.ireads += ios->ireads;
1157 total.iwrites += ios->iwrites;
1158 total.qread_kb += ios->qread_kb;
1159 total.qwrite_kb += ios->qwrite_kb;
1160 total.cread_kb += ios->cread_kb;
1161 total.cwrite_kb += ios->cwrite_kb;
1162 total.iread_kb += ios->iread_kb;
1163 total.iwrite_kb += ios->iwrite_kb;
06639b27
JA
1164 total.timer_unplugs += ios->timer_unplugs;
1165 total.io_unplugs += ios->io_unplugs;
e7c9f3ff
NS
1166
1167 snprintf(line, sizeof(line) - 1, "CPU%d (%s):",
1168 j, get_dev_name(pdi, name, sizeof(name)));
1169 dump_io_stats(ios, line);
1170 pci_events++;
1171 }
5c017e4b 1172
e7c9f3ff
NS
1173 if (pci_events > 1) {
1174 fprintf(ofp, "\n");
1175 snprintf(line, sizeof(line) - 1, "Total (%s):",
1176 get_dev_name(pdi, name, sizeof(name)));
1177 dump_io_stats(&total, line);
1178 }
d0ca268b 1179
dcf0f7ed 1180 fprintf(ofp, "\nEvents (%s): %'Lu entries, %'lu skips\n",
0aa8a62d 1181 get_dev_name(pdi, line, sizeof(line)), pdi->events,
dcf0f7ed 1182 pdi->skips);
e7c9f3ff 1183 }
d0ca268b
JA
1184}
1185
d36421e4
JA
1186/*
1187 * struct trace and blktrace allocation cache, we do potentially
1188 * millions of mallocs for these structures while only using at most
1189 * a few thousand at the time
1190 */
1191static inline void t_free(struct trace *t)
1192{
3c8413cb 1193 if (t_alloc_cache < 1024) {
1d24fc14
JA
1194 t->next = t_alloc_list;
1195 t_alloc_list = t;
3c8413cb
JA
1196 t_alloc_cache++;
1197 } else
1198 free(t);
d36421e4
JA
1199}
1200
1201static inline struct trace *t_alloc(void)
1202{
1203 struct trace *t = t_alloc_list;
1204
1205 if (t) {
1206 t_alloc_list = t->next;
3c8413cb 1207 t_alloc_cache--;
d36421e4
JA
1208 return t;
1209 }
1210
1211 return malloc(sizeof(*t));
1212}
1213
1214static inline void bit_free(struct blk_io_trace *bit)
1215{
3c8413cb 1216 if (bit_alloc_cache < 1024) {
1d24fc14
JA
1217 /*
1218 * abuse a 64-bit field for a next pointer for the free item
1219 */
1220 bit->time = (__u64) (unsigned long) bit_alloc_list;
1221 bit_alloc_list = (struct blk_io_trace *) bit;
3c8413cb
JA
1222 bit_alloc_cache++;
1223 } else
1224 free(bit);
d36421e4
JA
1225}
1226
1227static inline struct blk_io_trace *bit_alloc(void)
1228{
1229 struct blk_io_trace *bit = bit_alloc_list;
1230
1231 if (bit) {
0c39425c
NS
1232 bit_alloc_list = (struct blk_io_trace *) (unsigned long) \
1233 bit->time;
3c8413cb 1234 bit_alloc_cache--;
d36421e4
JA
1235 return bit;
1236 }
1237
1238 return malloc(sizeof(*bit));
1239}
1240
4f0ae44f
JA
1241static void find_genesis(void)
1242{
1243 struct trace *t = trace_list;
1244
1245 genesis_time = -1ULL;
1246 while (t != NULL) {
1247 if (t->bit->time < genesis_time)
1248 genesis_time = t->bit->time;
1249
1250 t = t->next;
1251 }
1252}
1253
7f4d89e6 1254static inline int check_stopwatch(struct blk_io_trace *bit)
4f0ae44f 1255{
7f4d89e6
JA
1256 if (bit->time < stopwatch_end &&
1257 bit->time >= stopwatch_start)
4f0ae44f
JA
1258 return 0;
1259
1260 return 1;
1261}
1262
53c68c88
JA
1263/*
1264 * return youngest entry read
1265 */
1266static int sort_entries(unsigned long long *youngest)
4f0ae44f
JA
1267{
1268 struct trace *t;
4f0ae44f
JA
1269
1270 if (!genesis_time)
1271 find_genesis();
1272
d6222db8 1273 *youngest = 0;
4f0ae44f
JA
1274 while ((t = trace_list) != NULL) {
1275 struct blk_io_trace *bit = t->bit;
1276
1277 trace_list = t->next;
1278
7f4d89e6 1279 bit->time -= genesis_time;
4f0ae44f 1280
d6222db8
JA
1281 if (bit->time < *youngest || !*youngest)
1282 *youngest = bit->time;
1283
7f4d89e6 1284 if (check_stopwatch(bit)) {
4f0ae44f
JA
1285 bit_free(bit);
1286 t_free(t);
1287 continue;
1288 }
1289
2a1b3424 1290 if (trace_rb_insert_sort(t))
53c68c88 1291 return -1;
1c7c54aa
JA
1292
1293 if (bit->sequence < smallest_seq_read)
1294 smallest_seq_read = bit->sequence;
4f0ae44f
JA
1295 }
1296
53c68c88 1297 return 0;
4f0ae44f
JA
1298}
1299
1c24add6
JA
1300static inline void __put_trace_last(struct per_dev_info *pdi, struct trace *t)
1301{
1302 rb_erase(&t->rb_node, &pdi->rb_last);
1303 pdi->rb_last_entries--;
1304
1305 bit_free(t->bit);
1306 t_free(t);
1307}
1308
1309static void put_trace(struct per_dev_info *pdi, struct trace *t)
2a1b3424
JA
1310{
1311 rb_erase(&t->rb_node, &rb_sort_root);
1312 rb_sort_entries--;
1313
1314 trace_rb_insert_last(pdi, t);
1315
1c24add6 1316 if (pdi->rb_last_entries > rb_batch * pdi->nfiles) {
2a1b3424
JA
1317 struct rb_node *n = rb_first(&pdi->rb_last);
1318
1319 t = rb_entry(n, struct trace, rb_node);
1c24add6 1320 __put_trace_last(pdi, t);
2a1b3424
JA
1321 }
1322}
1323
1c7c54aa
JA
1324static int check_sequence(struct per_dev_info *pdi, struct blk_io_trace *bit,
1325 int force)
2a1b3424
JA
1326{
1327 unsigned long expected_sequence = pdi->last_sequence + 1;
1c7c54aa 1328 struct trace *t;
d6222db8
JA
1329
1330 /*
1331 * first entry, always ok
1332 */
1333 if (!expected_sequence)
1334 return 0;
2a1b3424
JA
1335
1336 if (bit->sequence == expected_sequence)
1337 return 0;
1338
2a1b3424 1339 /*
1c7c54aa
JA
1340 * we may not have seen that sequence yet. if we are not doing
1341 * the final run, break and wait for more entries.
1c24add6 1342 */
1c7c54aa
JA
1343 if (expected_sequence < smallest_seq_read) {
1344 t = trace_rb_find_last(pdi, expected_sequence);
1345 if (!t)
1346 goto skip;
2a1b3424 1347
7d1c0411 1348 __put_trace_last(pdi, t);
2a1b3424 1349 return 0;
1c7c54aa 1350 } else if (!force)
2a1b3424 1351 return 1;
1c7c54aa
JA
1352 else {
1353skip:
7d1c0411
JA
1354 if (print_missing) {
1355 fprintf(stderr, "(%d,%d): skipping %lu -> %u\n",
1356 MAJOR(pdi->dev), MINOR(pdi->dev),
1357 pdi->last_sequence, bit->sequence);
1358 }
1c7c54aa
JA
1359 pdi->skips++;
1360 return 0;
1361 }
2a1b3424
JA
1362}
1363
a649216c 1364static void show_entries_rb(int force)
8fc0abbc 1365{
1f7afa72
JA
1366 struct per_dev_info *pdi = NULL;
1367 struct per_cpu_info *pci = NULL;
8fc0abbc 1368 struct blk_io_trace *bit;
3aabcd89 1369 struct rb_node *n;
8fc0abbc 1370 struct trace *t;
1f7afa72 1371
7d747d22 1372 while ((n = rb_first(&rb_sort_root)) != NULL) {
1f7afa72
JA
1373 if (done)
1374 break;
8fc0abbc
JA
1375
1376 t = rb_entry(n, struct trace, rb_node);
1377 bit = t->bit;
1378
f7bd1a9b 1379 if (!pdi || pdi->dev != bit->device)
287fa3d6 1380 pdi = get_dev_info(bit->device);
1f7afa72 1381
e7c9f3ff
NS
1382 if (!pdi) {
1383 fprintf(stderr, "Unknown device ID? (%d,%d)\n",
1384 MAJOR(bit->device), MINOR(bit->device));
1385 break;
1386 }
1f7afa72 1387
1c7c54aa 1388 if (check_sequence(pdi, bit, force))
2a1b3424 1389 break;
cb2a1a62 1390
a649216c 1391 if (!force && bit->time > last_allowed_time)
46e6968b 1392 break;
8fc0abbc 1393
be925321
JA
1394 pdi->last_sequence = bit->sequence;
1395
4f0ae44f 1396 check_time(pdi, bit);
8fc0abbc 1397
4f0ae44f
JA
1398 if (!pci || pci->cpu != bit->cpu)
1399 pci = get_cpu_info(pdi, bit->cpu);
287fa3d6 1400
4f0ae44f 1401 dump_trace(bit, pci, pdi);
87b72777 1402
2a1b3424 1403 put_trace(pdi, t);
cb2a1a62 1404 }
8fc0abbc
JA
1405}
1406
1f79c4a0
JA
1407static int read_data(int fd, void *buffer, int bytes, int block)
1408{
1409 int ret, bytes_left, fl;
1410 void *p;
1411
1412 fl = fcntl(fd, F_GETFL);
1413
1414 if (!block)
1415 fcntl(fd, F_SETFL, fl | O_NONBLOCK);
1416 else
1417 fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
1418
1419 bytes_left = bytes;
1420 p = buffer;
1421 while (bytes_left > 0) {
1422 ret = read(fd, p, bytes_left);
1423 if (!ret)
1424 return 1;
1425 else if (ret < 0) {
1426 if (errno != EAGAIN)
1427 perror("read");
a649216c 1428
1f79c4a0
JA
1429 return -1;
1430 } else {
1431 p += ret;
1432 bytes_left -= ret;
1433 }
1434 }
1435
1436 return 0;
1437}
1438
a649216c 1439static int read_events(int fd, int always_block)
cb2a1a62 1440{
287fa3d6 1441 struct per_dev_info *pdi = NULL;
e820abd7 1442 unsigned int events = 0;
7d747d22
JA
1443
1444 while (!is_done() && events < rb_batch) {
1445 struct blk_io_trace *bit;
1446 struct trace *t;
1447 int pdu_len;
1448 __u32 magic;
1449
d36421e4 1450 bit = bit_alloc();
cb2a1a62 1451
a649216c 1452 if (read_data(fd, bit, sizeof(*bit), !events || always_block))
cb2a1a62
JA
1453 break;
1454
7d747d22
JA
1455 magic = be32_to_cpu(bit->magic);
1456 if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
1457 fprintf(stderr, "Bad magic %x\n", magic);
1458 break;
1459 }
1460
1461 pdu_len = be16_to_cpu(bit->pdu_len);
1462 if (pdu_len) {
1463 void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
1464
1465 if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1))
1466 break;
1467
1468 bit = ptr;
1469 }
1470
d6222db8
JA
1471 trace_to_cpu(bit);
1472
1473 if (verify_trace(bit)) {
1474 bit_free(bit);
1475 continue;
1476 }
1477
d36421e4 1478 t = t_alloc();
cb2a1a62
JA
1479 memset(t, 0, sizeof(*t));
1480 t->bit = bit;
1481
7d747d22
JA
1482 t->next = trace_list;
1483 trace_list = t;
1f7afa72 1484
f7bd1a9b 1485 if (!pdi || pdi->dev != bit->device)
287fa3d6
JA
1486 pdi = get_dev_info(bit->device);
1487
1488 if (bit->time > pdi->last_read_time)
1489 pdi->last_read_time = bit->time;
1490
7d747d22 1491 events++;
cb2a1a62
JA
1492 }
1493
7d747d22 1494 return events;
cb2a1a62
JA
1495}
1496
d5396421 1497static int do_file(void)
d0ca268b 1498{
7d747d22 1499 struct per_cpu_info *pci;
73877e12
NS
1500 struct per_dev_info *pdi;
1501 int i, j, events, events_added;
d0ca268b 1502
7d747d22
JA
1503 /*
1504 * first prepare all files for reading
1505 */
e8741a4a 1506 for (i = 0; i < ndevices; i++) {
73877e12
NS
1507 pdi = &devices[i];
1508 pdi->nfiles = 0;
d6222db8 1509 pdi->last_sequence = -1;
73877e12 1510
74feaeb5 1511 for (j = 0;; j++) {
e7c9f3ff 1512 struct stat st;
87b72777 1513
e7c9f3ff
NS
1514 pci = get_cpu_info(pdi, j);
1515 pci->cpu = j;
7d747d22 1516 pci->fd = -1;
d0ca268b 1517
e7c9f3ff 1518 snprintf(pci->fname, sizeof(pci->fname)-1,
7d747d22 1519 "%s.blktrace.%d", pdi->name, pci->cpu);
e7c9f3ff
NS
1520 if (stat(pci->fname, &st) < 0)
1521 break;
74feaeb5
AB
1522 if (st.st_size) {
1523 pci->fd = open(pci->fname, O_RDONLY);
1524 if (pci->fd < 0) {
1525 perror(pci->fname);
1526 continue;
1527 }
e7c9f3ff
NS
1528 }
1529
7d747d22 1530 printf("Input file %s added\n", pci->fname);
73877e12 1531 pdi->nfiles++;
d0ca268b 1532 }
d5396421
JA
1533 }
1534
7d747d22
JA
1535 /*
1536 * now loop over the files reading in the data
1537 */
412819ce 1538 do {
53c68c88
JA
1539 unsigned long long youngest;
1540
7d747d22 1541 events_added = 0;
287fa3d6 1542 last_allowed_time = -1ULL;
1c7c54aa 1543 smallest_seq_read = -1U;
d5396421 1544
7d747d22 1545 for (i = 0; i < ndevices; i++) {
73877e12
NS
1546 pdi = &devices[i];
1547
1548 for (j = 0; j < pdi->nfiles; j++) {
d5396421 1549
73877e12 1550 pci = get_cpu_info(pdi, j);
d5396421 1551
7d747d22
JA
1552 if (pci->fd == -1)
1553 continue;
51128a28 1554
a649216c 1555 events = read_events(pci->fd, 1);
7d747d22
JA
1556 if (!events) {
1557 close(pci->fd);
1558 pci->fd = -1;
1559 continue;
1560 }
d5396421 1561
287fa3d6
JA
1562 if (pdi->last_read_time < last_allowed_time)
1563 last_allowed_time = pdi->last_read_time;
d5396421 1564
7d747d22
JA
1565 events_added += events;
1566 }
2ff323b0 1567 }
d5396421 1568
53c68c88
JA
1569 if (sort_entries(&youngest))
1570 break;
1571
1572 if (youngest > stopwatch_end)
287fa3d6
JA
1573 break;
1574
a649216c 1575 show_entries_rb(0);
cb2a1a62 1576
7d747d22 1577 } while (events_added);
d5396421 1578
a649216c
JA
1579 if (rb_sort_entries)
1580 show_entries_rb(1);
1581
7d747d22 1582 return 0;
412819ce 1583}
d5396421 1584
412819ce
JA
1585static int do_stdin(void)
1586{
53c68c88 1587 unsigned long long youngest;
412819ce 1588 int fd;
d5396421 1589
be925321 1590 last_allowed_time = -1ULL;
1f79c4a0 1591 fd = dup(STDIN_FILENO);
412819ce
JA
1592 do {
1593 int events;
d5396421 1594
a649216c 1595 events = read_events(fd, 0);
412819ce
JA
1596 if (!events)
1597 break;
1598
53c68c88
JA
1599 if (sort_entries(&youngest))
1600 break;
1601
1602 if (youngest > stopwatch_end)
2ff323b0
JA
1603 break;
1604
a649216c 1605 show_entries_rb(0);
d5396421
JA
1606 } while (1);
1607
a649216c
JA
1608 if (rb_sort_entries)
1609 show_entries_rb(1);
1610
d5396421 1611 close(fd);
d5396421
JA
1612 return 0;
1613}
d0ca268b 1614
1f79c4a0 1615static void flush_output(void)
412819ce 1616{
152f6476 1617 fflush(ofp);
412819ce
JA
1618}
1619
e820abd7 1620static void handle_sigint(__attribute__((__unused__)) int sig)
412819ce
JA
1621{
1622 done = 1;
1623 flush_output();
1624}
1625
46e6968b
NS
1626/*
1627 * Extract start and duration times from a string, allowing
1628 * us to specify a time interval of interest within a trace.
1629 * Format: "duration" (start is zero) or "start:duration".
1630 */
1631static int find_stopwatch_interval(char *string)
1632{
1633 double value;
1634 char *sp;
1635
1636 value = strtod(string, &sp);
1637 if (sp == string) {
1638 fprintf(stderr,"Invalid stopwatch timer: %s\n", string);
1639 return 1;
1640 }
1641 if (*sp == ':') {
1642 stopwatch_start = DOUBLE_TO_NANO_ULL(value);
1643 string = sp + 1;
1644 value = strtod(string, &sp);
1645 if (sp == string || *sp != '\0') {
1646 fprintf(stderr,"Invalid stopwatch duration time: %s\n",
1647 string);
1648 return 1;
1649 }
1650 } else if (*sp != '\0') {
1651 fprintf(stderr,"Invalid stopwatch start timer: %s\n", string);
1652 return 1;
1653 }
1b928247
JA
1654 stopwatch_end = DOUBLE_TO_NANO_ULL(value);
1655 if (stopwatch_end <= stopwatch_start) {
1656 fprintf(stderr, "Invalid stopwatch interval: %Lu -> %Lu\n",
1657 stopwatch_start, stopwatch_end);
1658 return 1;
1659 }
1660
46e6968b
NS
1661 return 0;
1662}
1663
52724a0e
JA
1664static char usage_str[] = \
1665 "[ -i <input name> ] [-o <output name> [ -s ] [ -t ] [ -q ]\n" \
1666 "[ -w start:stop ] [ -f output format ] [ -F format spec ] [ -v] \n\n" \
1667 "\t-i Input file containing trace data, or '-' for stdin\n" \
1668 "\t-o Output file. If not given, output is stdout\n" \
1669 "\t-b stdin read batching\n" \
1670 "\t-s Show per-program io statistics\n" \
bf0720af 1671 "\t-n Hash processes by name, not pid\n" \
52724a0e
JA
1672 "\t-t Track individual ios. Will tell you the time a request took\n" \
1673 "\t to get queued, to get dispatched, and to get completed\n" \
1674 "\t-q Quiet. Don't display any stats at the end of the trace\n" \
1675 "\t-w Only parse data between the given time interval in seconds.\n" \
1676 "\t If 'start' isn't given, blkparse defaults the start time to 0\n" \
1677 "\t -f Output format. Customize the output format. The format field\n" \
1678 "\t identifies can be found in the documentation\n" \
1679 "\t-F Format specification. Can be found in the documentation\n" \
7d1c0411 1680 "\t-m Print missing entries\n" \
52724a0e
JA
1681 "\t-v Print program version info\n\n";
1682
1f79c4a0
JA
1683static void usage(char *prog)
1684{
52724a0e 1685 fprintf(stderr, "Usage: %s %s %s", prog, blkparse_version, usage_str);
1f79c4a0
JA
1686}
1687
d5396421
JA
1688int main(int argc, char *argv[])
1689{
152f6476 1690 char *ofp_buffer;
a66877e6 1691 int c, ret, mode;
1e1c60f1 1692 int per_device_and_cpu_stats = 1;
d5396421
JA
1693
1694 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
1695 switch (c) {
1696 case 'i':
e7c9f3ff
NS
1697 if (!strcmp(optarg, "-") && !pipeline)
1698 pipeline = 1;
1699 else if (resize_devices(optarg) != 0)
1700 return 1;
d5396421
JA
1701 break;
1702 case 'o':
66efebf8 1703 output_name = optarg;
d5396421 1704 break;
79f19470
JA
1705 case 'b':
1706 rb_batch = atoi(optarg);
1707 if (rb_batch <= 0)
1708 rb_batch = RB_BATCH_DEFAULT;
1709 break;
152f6476
JA
1710 case 's':
1711 per_process_stats = 1;
1712 break;
7997c5b0
JA
1713 case 't':
1714 track_ios = 1;
1715 break;
1e1c60f1
NS
1716 case 'q':
1717 per_device_and_cpu_stats = 0;
1718 break;
46e6968b
NS
1719 case 'w':
1720 if (find_stopwatch_interval(optarg) != 0)
1721 return 1;
1722 break;
ab197ca7
AB
1723 case 'f':
1724 set_all_format_specs(optarg);
1725 break;
1726 case 'F':
1727 if (add_format_spec(optarg) != 0)
1728 return 1;
1729 break;
bf0720af 1730 case 'n':
715d8021 1731 ppi_hash_by_pid = 0;
bf0720af 1732 break;
7d1c0411
JA
1733 case 'm':
1734 print_missing = 1;
1735 break;
52724a0e
JA
1736 case 'v':
1737 printf("%s version %s\n", argv[0], blkparse_version);
1738 return 0;
d5396421 1739 default:
1f79c4a0 1740 usage(argv[0]);
d5396421
JA
1741 return 1;
1742 }
d0ca268b
JA
1743 }
1744
e7c9f3ff
NS
1745 while (optind < argc) {
1746 if (!strcmp(argv[optind], "-") && !pipeline)
1747 pipeline = 1;
1748 else if (resize_devices(argv[optind]) != 0)
1749 return 1;
1750 optind++;
1751 }
1752
1753 if (!pipeline && !ndevices) {
1f79c4a0 1754 usage(argv[0]);
d5396421
JA
1755 return 1;
1756 }
1757
7997c5b0 1758 memset(&rb_sort_root, 0, sizeof(rb_sort_root));
412819ce
JA
1759
1760 signal(SIGINT, handle_sigint);
1761 signal(SIGHUP, handle_sigint);
1762 signal(SIGTERM, handle_sigint);
d5396421 1763
d69db225
JA
1764 setlocale(LC_NUMERIC, "en_US");
1765
a66877e6 1766 if (!output_name) {
152f6476 1767 ofp = fdopen(STDOUT_FILENO, "w");
a66877e6
JA
1768 mode = _IOLBF;
1769 } else {
152f6476
JA
1770 char ofname[128];
1771
7a9690c0 1772 snprintf(ofname, sizeof(ofname) - 1, "%s", output_name);
152f6476 1773 ofp = fopen(ofname, "w");
a66877e6 1774 mode = _IOFBF;
152f6476
JA
1775 }
1776
1777 if (!ofp) {
1778 perror("fopen");
1779 return 1;
1780 }
1781
1782 ofp_buffer = malloc(4096);
a66877e6 1783 if (setvbuf(ofp, ofp_buffer, mode, 4096)) {
152f6476
JA
1784 perror("setvbuf");
1785 return 1;
1786 }
1787
e7c9f3ff 1788 if (pipeline)
d5396421
JA
1789 ret = do_stdin();
1790 else
1791 ret = do_file();
1792
152f6476
JA
1793 if (per_process_stats)
1794 show_process_stats();
1795
1e1c60f1
NS
1796 if (per_device_and_cpu_stats)
1797 show_device_and_cpu_stats();
152f6476 1798
412819ce 1799 flush_output();
d5396421 1800 return ret;
d0ca268b 1801}