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