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