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