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