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