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