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