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