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