[PATCH] blkparse: fix quit on stdin reads
[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>
d0ca268b 32
8fc0abbc
JA
33#include "blktrace.h"
34#include "rbtree.h"
d0ca268b 35
2e3e8ded
JA
36#define SECONDS(x) ((unsigned long long)(x) / 1000000000)
37#define NANO_SECONDS(x) ((unsigned long long)(x) % 1000000000)
cfab07eb 38
d5396421
JA
39static int backwards;
40static unsigned long long genesis_time, last_reported_time;
41
152f6476
JA
42struct io_stats {
43 unsigned long qreads, qwrites, creads, cwrites, mreads, mwrites;
44 unsigned long ireads, iwrites;
45 unsigned long long qread_kb, qwrite_kb, cread_kb, cwrite_kb;
46 unsigned long long iread_kb, iwrite_kb;
47};
48
d5396421 49struct per_cpu_info {
d0ca268b
JA
50 int cpu;
51 int nelems;
d0ca268b
JA
52
53 int fd;
87b72777 54 char fname[128];
d0ca268b 55
152f6476
JA
56 struct io_stats io_stats;
57};
8fc0abbc 58
152f6476
JA
59struct per_process_info {
60 char name[16];
61 __u32 pid;
62 struct io_stats io_stats;
63 struct per_process_info *hash_next, *list_next;
50adc0ba
JA
64
65 /*
66 * individual io stats
67 */
b9d40d6f
JA
68 unsigned long long longest_allocation_wait[2];
69 unsigned long long longest_dispatch_wait[2];
70 unsigned long long longest_completion_wait[2];
d0ca268b
JA
71};
72
152f6476
JA
73#define PPI_HASH_SHIFT (8)
74static struct per_process_info *ppi_hash[1 << PPI_HASH_SHIFT];
75static struct per_process_info *ppi_list;
76
7997c5b0 77#define S_OPTS "i:o:b:st"
d5396421
JA
78static struct option l_opts[] = {
79 {
80 .name = "input",
81 .has_arg = 1,
82 .flag = NULL,
83 .val = 'i'
84 },
85 {
86 .name = "output",
87 .has_arg = 1,
88 .flag = NULL,
89 .val = 'o'
90 },
79f19470
JA
91 {
92 .name = "batch",
93 .has_arg = 1,
94 .flag = NULL,
95 .val = 'b'
96 },
152f6476
JA
97 {
98 .name = "per program stats",
99 .has_arg = 0,
100 .flag = NULL,
101 .val = 's'
102 },
7997c5b0
JA
103 {
104 .name = "track ios",
105 .has_arg = 0,
106 .flag = NULL,
107 .val = 't'
108 },
d5396421
JA
109 {
110 .name = NULL,
111 .has_arg = 0,
112 .flag = NULL,
113 .val = 0
114 }
115};
116
7997c5b0
JA
117static struct rb_root rb_sort_root;
118static struct rb_root rb_track_root;
8fc0abbc 119
7997c5b0
JA
120/*
121 * for sorting the displayed output
122 */
8fc0abbc
JA
123struct trace {
124 struct blk_io_trace *bit;
125 struct rb_node rb_node;
126};
127
7997c5b0
JA
128/*
129 * for tracking individual ios
130 */
131struct io_track {
132 struct rb_node rb_node;
133
134 __u64 sector;
135 __u32 pid;
95c15013 136 unsigned long long allocation_time;
7997c5b0
JA
137 unsigned long long queue_time;
138 unsigned long long dispatch_time;
139 unsigned long long completion_time;
140};
141
a718bd37
NS
142static int max_cpus;
143static struct per_cpu_info *per_cpu_info;
d0ca268b 144
87b72777
JA
145static unsigned long long events;
146
d5396421 147static char *dev, *output_name;
152f6476
JA
148static FILE *ofp;
149
150static int per_process_stats;
7997c5b0 151static int track_ios;
d0ca268b 152
79f19470
JA
153#define RB_BATCH_DEFAULT (1024)
154static int rb_batch = RB_BATCH_DEFAULT;
155
412819ce
JA
156#define is_done() (*(volatile int *)(&done))
157static volatile int done;
158
152f6476
JA
159static inline unsigned long hash_long(unsigned long val)
160{
16ef714e
JA
161#if __WORDSIZE == 32
162 val *= 0x9e370001UL;
163#elif __WORDSIZE == 64
164 val *= 0x9e37fffffffc0001UL;
165#else
166#error unknown word size
167#endif
168
169 return val >> (__WORDSIZE - PPI_HASH_SHIFT);
152f6476
JA
170}
171
172static inline void add_process_to_hash(struct per_process_info *ppi)
173{
174 const int hash_idx = hash_long(ppi->pid);
175
176 ppi->hash_next = ppi_hash[hash_idx];
177 ppi_hash[hash_idx] = ppi;
178}
179
180static inline void add_process_to_list(struct per_process_info *ppi)
181{
182 ppi->list_next = ppi_list;
183 ppi_list = ppi;
184}
185
186static struct per_process_info *find_process_by_pid(__u32 pid)
187{
188 const int hash_idx = hash_long(pid);
189 struct per_process_info *ppi;
190
191 ppi = ppi_hash[hash_idx];
192 while (ppi) {
193 if (ppi->pid == pid)
194 return ppi;
195
196 ppi = ppi->hash_next;
197 }
198
199 return NULL;
200}
201
7997c5b0
JA
202static inline int trace_rb_insert(struct trace *t)
203{
204 struct rb_node **p = &rb_sort_root.rb_node;
205 struct rb_node *parent = NULL;
206 struct trace *__t;
207
208 while (*p) {
209 parent = *p;
210 __t = rb_entry(parent, struct trace, rb_node);
211
212 if (t->bit->sequence < __t->bit->sequence)
213 p = &(*p)->rb_left;
214 else if (t->bit->sequence > __t->bit->sequence)
215 p = &(*p)->rb_right;
216 else {
217 fprintf(stderr, "sequence alias!\n");
218 return 1;
219 }
220 }
221
222 rb_link_node(&t->rb_node, parent, p);
223 rb_insert_color(&t->rb_node, &rb_sort_root);
224 return 0;
225}
226
227static inline int track_rb_insert(struct io_track *iot)
228{
229 struct rb_node **p = &rb_track_root.rb_node;
230 struct rb_node *parent = NULL;
231 struct io_track *__iot;
232
233 while (*p) {
234 parent = *p;
235
236 __iot = rb_entry(parent, struct io_track, rb_node);
237
238 if (iot->sector < __iot->sector)
239 p = &(*p)->rb_left;
240 else if (iot->sector > __iot->sector)
241 p = &(*p)->rb_right;
242 else {
243 fprintf(stderr, "sequence alias!\n");
244 return 1;
245 }
246 }
247
248 rb_link_node(&iot->rb_node, parent, p);
249 rb_insert_color(&iot->rb_node, &rb_track_root);
250 return 0;
251}
252
253static struct io_track *__find_track(__u64 sector)
254{
255 struct rb_node **p = &rb_track_root.rb_node;
256 struct rb_node *parent = NULL;
257 struct io_track *__iot;
258
259 while (*p) {
260 parent = *p;
261
262 __iot = rb_entry(parent, struct io_track, rb_node);
263
264 if (sector < __iot->sector)
265 p = &(*p)->rb_left;
266 else if (sector > __iot->sector)
267 p = &(*p)->rb_right;
268 else
269 return __iot;
270 }
271
272 return NULL;
273}
274
50adc0ba 275static struct io_track *find_track(__u32 pid, __u64 sector)
7997c5b0
JA
276{
277 struct io_track *iot = __find_track(sector);
278
279 iot = __find_track(sector);
280 if (!iot) {
281 iot = malloc(sizeof(*iot));
50adc0ba 282 iot->pid = pid;
7997c5b0
JA
283 iot->sector = sector;
284 track_rb_insert(iot);
285 }
286
287 return iot;
288}
289
2e3e8ded
JA
290static void log_track_merge(struct blk_io_trace *t)
291{
292 struct io_track *iot;
293
294 if (!track_ios)
295 return;
296 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
297 return;
298
299 iot = __find_track(t->sector - (t->bytes >> 10));
300 if (!iot) {
301 fprintf(stderr, "Trying to merge on non-existing request\n");
302 return;
303 }
304
305 rb_erase(&iot->rb_node, &rb_track_root);
306 iot->sector -= t->bytes >> 10;
307 track_rb_insert(iot);
308}
309
95c15013 310static void log_track_getrq(struct blk_io_trace *t)
2e3e8ded
JA
311{
312 struct io_track *iot;
313
314 if (!track_ios)
315 return;
316
50adc0ba 317 iot = find_track(t->pid, t->sector);
95c15013
JA
318 iot->allocation_time = t->time;
319}
320
321
322/*
323 * return time between rq allocation and queue
324 */
325static unsigned long long log_track_queue(struct blk_io_trace *t)
326{
50adc0ba 327 unsigned long long elapsed;
95c15013
JA
328 struct io_track *iot;
329
330 if (!track_ios)
331 return -1;
332
50adc0ba 333 iot = find_track(t->pid, t->sector);
2e3e8ded 334 iot->queue_time = t->time;
50adc0ba
JA
335 elapsed = iot->queue_time - iot->allocation_time;
336
337 if (per_process_stats) {
338 struct per_process_info *ppi = find_process_by_pid(iot->pid);
b9d40d6f 339 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
50adc0ba 340
b9d40d6f
JA
341 if (ppi && elapsed > ppi->longest_allocation_wait[w])
342 ppi->longest_allocation_wait[w] = elapsed;
50adc0ba
JA
343 }
344
345 return elapsed;
2e3e8ded
JA
346}
347
348/*
349 * return time between queue and issue
350 */
351static unsigned long long log_track_issue(struct blk_io_trace *t)
352{
50adc0ba 353 unsigned long long elapsed;
2e3e8ded
JA
354 struct io_track *iot;
355
356 if (!track_ios)
357 return -1;
358 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
359 return -1;
360
361 iot = __find_track(t->sector);
362 if (!iot) {
363 fprintf(stderr, "Trying to issue on non-existing request\n");
364 return -1;
365 }
366
367 iot->dispatch_time = t->time;
50adc0ba
JA
368 elapsed = iot->dispatch_time - iot->queue_time;
369
370 if (per_process_stats) {
371 struct per_process_info *ppi = find_process_by_pid(iot->pid);
b9d40d6f 372 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
50adc0ba 373
b9d40d6f
JA
374 if (ppi && elapsed > ppi->longest_dispatch_wait[w])
375 ppi->longest_dispatch_wait[w] = elapsed;
50adc0ba
JA
376 }
377
378 return elapsed;
2e3e8ded
JA
379}
380
381/*
382 * return time between dispatch and complete
383 */
384static unsigned long long log_track_complete(struct blk_io_trace *t)
385{
386 unsigned long long elapsed;
387 struct io_track *iot;
388
389 if (!track_ios)
390 return -1;
391 if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
392 return -1;
393
394 iot = __find_track(t->sector);
395 if (!iot) {
396 fprintf(stderr, "Trying to dispatch on non-existing request\n");
397 return -1;
398 }
399
400 iot->completion_time = t->time;
401 elapsed = iot->completion_time - iot->dispatch_time;
402
50adc0ba
JA
403 if (per_process_stats) {
404 struct per_process_info *ppi = find_process_by_pid(iot->pid);
b9d40d6f 405 int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
50adc0ba 406
b9d40d6f
JA
407 if (ppi && elapsed > ppi->longest_completion_wait[w])
408 ppi->longest_completion_wait[w] = elapsed;
50adc0ba
JA
409 }
410
2e3e8ded
JA
411 /*
412 * kill the trace, we don't need it after completion
413 */
414 rb_erase(&iot->rb_node, &rb_track_root);
415 free(iot);
416
417 return elapsed;
418}
419
420
152f6476
JA
421static struct io_stats *find_process_io_stats(__u32 pid, char *name)
422{
423 struct per_process_info *ppi = find_process_by_pid(pid);
424
425 if (!ppi) {
426 ppi = malloc(sizeof(*ppi));
427 memset(ppi, 0, sizeof(*ppi));
428 strncpy(ppi->name, name, sizeof(ppi->name));
429 ppi->pid = pid;
430 add_process_to_hash(ppi);
431 add_process_to_list(ppi);
432 }
433
434 return &ppi->io_stats;
435}
436
a718bd37
NS
437static void resize_cpu_info(int cpuid)
438{
439 int new_space, new_max = cpuid + 1;
440 char *new_start;
441
442 per_cpu_info = realloc(per_cpu_info, new_max * sizeof(*per_cpu_info));
443 if (!per_cpu_info) {
444 fprintf(stderr, "Cannot allocate CPU info -- %d x %d bytes\n",
445 new_max, (int) sizeof(*per_cpu_info));
446 exit(1);
447 }
448
449 new_start = (char *)per_cpu_info + (max_cpus * sizeof(*per_cpu_info));
450 new_space = (new_max - max_cpus) * sizeof(*per_cpu_info);
451 memset(new_start, 0, new_space);
452 max_cpus = new_max;
453}
454
455static struct per_cpu_info *get_cpu_info(int cpu)
456{
c499bf38
JA
457 struct per_cpu_info *pci;
458
a718bd37
NS
459 if (cpu >= max_cpus)
460 resize_cpu_info(cpu);
461
c499bf38
JA
462 /*
463 * ->cpu might already be set, but just set it unconditionally
464 */
465 pci = &per_cpu_info[cpu];
466 pci->cpu = cpu;
467
468 return pci;
a718bd37
NS
469}
470
cfab07eb
AB
471static inline void check_time(struct blk_io_trace *bit)
472{
473 unsigned long long this = bit->time;
474 unsigned long long last = last_reported_time;
475
476 backwards = (this < last) ? 'B' : ' ';
477 last_reported_time = this;
478}
479
152f6476
JA
480static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t,
481 int rw)
d0ca268b
JA
482{
483 if (rw) {
152f6476
JA
484 ios->mwrites++;
485 ios->qwrite_kb += t->bytes >> 10;
d0ca268b 486 } else {
152f6476
JA
487 ios->mreads++;
488 ios->qread_kb += t->bytes >> 10;
489 }
490}
491
492static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci,
493 int rw)
494{
495 __account_m(&pci->io_stats, t, rw);
496
497 if (per_process_stats) {
498 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
499
500 __account_m(ios, t, rw);
d0ca268b
JA
501 }
502}
503
152f6476
JA
504static inline void __account_q(struct io_stats *ios, struct blk_io_trace *t,
505 int rw)
d0ca268b
JA
506{
507 if (rw) {
152f6476
JA
508 ios->qwrites++;
509 ios->qwrite_kb += t->bytes >> 10;
d0ca268b 510 } else {
152f6476
JA
511 ios->qreads++;
512 ios->qread_kb += t->bytes >> 10;
513 }
514}
515
516static inline void account_q(struct blk_io_trace *t, struct per_cpu_info *pci,
517 int rw)
518{
519 __account_q(&pci->io_stats, t, rw);
520
521 if (per_process_stats) {
522 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
523
524 __account_q(ios, t, rw);
d0ca268b
JA
525 }
526}
527
152f6476 528static inline void __account_c(struct io_stats *ios, int rw, unsigned int bytes)
d0ca268b
JA
529{
530 if (rw) {
152f6476
JA
531 ios->cwrites++;
532 ios->cwrite_kb += bytes >> 10;
d0ca268b 533 } else {
152f6476
JA
534 ios->creads++;
535 ios->cread_kb += bytes >> 10;
536 }
537}
538
539static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci,
540 int rw, int bytes)
541{
542 __account_c(&pci->io_stats, rw, bytes);
543
544 if (per_process_stats) {
545 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
546
547 __account_c(ios, rw, bytes);
d0ca268b
JA
548 }
549}
550
152f6476 551static inline void __account_i(struct io_stats *ios, int rw, unsigned int bytes)
afd2d7ad 552{
553 if (rw) {
152f6476
JA
554 ios->iwrites++;
555 ios->iwrite_kb += bytes >> 10;
afd2d7ad 556 } else {
152f6476
JA
557 ios->ireads++;
558 ios->iread_kb += bytes >> 10;
afd2d7ad 559 }
560}
561
152f6476
JA
562static inline void account_i(struct blk_io_trace *t, struct per_cpu_info *pci,
563 int rw)
d0ca268b 564{
152f6476
JA
565 __account_i(&pci->io_stats, rw, t->bytes);
566
567 if (per_process_stats) {
568 struct io_stats *ios = find_process_io_stats(t->pid, t->comm);
d5396421 569
152f6476
JA
570 __account_i(ios, rw, t->bytes);
571 }
572}
573
574static void output(struct per_cpu_info *pci, char *s)
575{
576 fprintf(ofp, "%s", s);
d0ca268b
JA
577}
578
3aabcd89
JA
579static char hstring[256];
580static char tstring[256];
d0ca268b 581
d5396421
JA
582static inline char *setup_header(struct per_cpu_info *pci,
583 struct blk_io_trace *t, char act)
d0ca268b
JA
584{
585 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
586 int b = t->action & BLK_TC_ACT(BLK_TC_BARRIER);
587 int s = t->action & BLK_TC_ACT(BLK_TC_SYNC);
588 char rwbs[4];
589 int i = 0;
590
591 if (w)
592 rwbs[i++] = 'W';
593 else
594 rwbs[i++] = 'R';
595 if (b)
596 rwbs[i++] = 'B';
597 if (s)
598 rwbs[i++] = 'S';
599
600 rwbs[i] = '\0';
601
555d3a31 602 sprintf(hstring, "%2d %8ld %5Lu.%09Lu %5u %c %3s",
d5396421 603 pci->cpu,
cfab07eb
AB
604 (unsigned long)t->sequence, SECONDS(t->time),
605 NANO_SECONDS(t->time), t->pid, act, rwbs);
d0ca268b
JA
606
607 return hstring;
608}
609
d5396421
JA
610static void log_complete(struct per_cpu_info *pci, struct blk_io_trace *t,
611 char act)
d0ca268b 612{
2e3e8ded
JA
613 unsigned long long elapsed = log_track_complete(t);
614
615 if (elapsed != -1ULL) {
b9d40d6f 616 unsigned long usec = elapsed / 1000;
2e3e8ded 617
b9d40d6f 618 sprintf(tstring,"%s %Lu + %u (%8lu) [%d]\n",
2e3e8ded
JA
619 setup_header(pci, t, act),
620 (unsigned long long)t->sector, t->bytes >> 9,
621 usec, t->error);
622 } else {
623 sprintf(tstring,"%s %Lu + %u [%d]\n", setup_header(pci, t, act),
624 (unsigned long long)t->sector, t->bytes >> 9, t->error);
625 }
626
d5396421 627 output(pci, tstring);
d0ca268b
JA
628}
629
d5396421
JA
630static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t,
631 char act)
d0ca268b 632{
95c15013 633 unsigned long long elapsed = log_track_queue(t);
2e3e8ded 634
95c15013 635 if (elapsed != -1ULL) {
b9d40d6f 636 unsigned long usec = elapsed / 1000;
95c15013 637
b9d40d6f 638 sprintf(tstring,"%s %Lu + %u (%8lu) [%s]\n",
95c15013
JA
639 setup_header(pci, t, act),
640 (unsigned long long)t->sector, t->bytes >> 9,
641 usec, t->comm);
642 } else {
643 sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act),
644 (unsigned long long)t->sector, t->bytes >> 9, t->comm);
645 }
d5396421 646 output(pci, tstring);
d0ca268b
JA
647}
648
d5396421
JA
649static void log_issue(struct per_cpu_info *pci, struct blk_io_trace *t,
650 char act)
d0ca268b 651{
2e3e8ded
JA
652 unsigned long long elapsed = log_track_issue(t);
653
654 if (elapsed != -1ULL) {
655 double usec = (double) elapsed / 1000;
656
555d3a31 657 sprintf(tstring,"%s %Lu + %u (%8.2f) [%s]\n",
2e3e8ded
JA
658 setup_header(pci, t, act),
659 (unsigned long long)t->sector, t->bytes >> 9,
660 usec, t->comm);
661 } else {
662 sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act),
663 (unsigned long long)t->sector, t->bytes >> 9, t->comm);
664 }
665
d5396421 666 output(pci, tstring);
d0ca268b
JA
667}
668
d5396421
JA
669static void log_merge(struct per_cpu_info *pci, struct blk_io_trace *t,
670 char act)
d0ca268b 671{
2e3e8ded
JA
672 log_track_merge(t);
673
984c63b7 674 sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act),
2955af9d 675 (unsigned long long)t->sector, t->bytes >> 9, t->comm);
d5396421 676 output(pci, tstring);
d0ca268b
JA
677}
678
d5396421
JA
679static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t,
680 char act)
d0ca268b 681{
2955af9d
NS
682 sprintf(tstring,"%s %Lu + %u [%s]\n", setup_header(pci, t, act),
683 (unsigned long long)t->sector, t->bytes >> 9, t->comm);
d5396421 684 output(pci, tstring);
d0ca268b
JA
685}
686
d5396421 687static int log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char act)
d0ca268b 688{
87b72777
JA
689 unsigned char *buf;
690 int i;
d0ca268b 691
d5396421
JA
692 sprintf(tstring,"%s ", setup_header(pci, t, act));
693 output(pci, tstring);
d0ca268b 694
87b72777 695 buf = (unsigned char *) t + sizeof(*t);
d0ca268b
JA
696 for (i = 0; i < t->pdu_len; i++) {
697 sprintf(tstring,"%02x ", buf[i]);
d5396421 698 output(pci, tstring);
d0ca268b
JA
699 }
700
701 if (act == 'C') {
2955af9d
NS
702 sprintf(tstring,"[%d]\n", t->error);
703 output(pci, tstring);
704 } else {
705 sprintf(tstring,"[%s]\n", t->comm);
d5396421 706 output(pci, tstring);
d0ca268b 707 }
87b72777 708 return 0;
d0ca268b
JA
709}
710
d5396421 711static int dump_trace_pc(struct blk_io_trace *t, struct per_cpu_info *pci)
d0ca268b 712{
87b72777
JA
713 int ret = 0;
714
d0ca268b
JA
715 switch (t->action & 0xffff) {
716 case __BLK_TA_QUEUE:
d5396421 717 log_generic(pci, t, 'Q');
d0ca268b
JA
718 break;
719 case __BLK_TA_GETRQ:
d5396421 720 log_generic(pci, t, 'G');
d0ca268b
JA
721 break;
722 case __BLK_TA_SLEEPRQ:
d5396421 723 log_generic(pci, t, 'S');
d0ca268b
JA
724 break;
725 case __BLK_TA_REQUEUE:
d5396421 726 log_generic(pci, t, 'R');
d0ca268b
JA
727 break;
728 case __BLK_TA_ISSUE:
d5396421 729 ret = log_pc(pci, t, 'D');
d0ca268b
JA
730 break;
731 case __BLK_TA_COMPLETE:
d5396421 732 log_pc(pci, t, 'C');
d0ca268b
JA
733 break;
734 default:
735 fprintf(stderr, "Bad pc action %x\n", t->action);
87b72777
JA
736 ret = 1;
737 break;
d0ca268b
JA
738 }
739
87b72777 740 return ret;
d0ca268b
JA
741}
742
d5396421 743static void dump_trace_fs(struct blk_io_trace *t, struct per_cpu_info *pci)
d0ca268b
JA
744{
745 int w = t->action & BLK_TC_ACT(BLK_TC_WRITE);
7997c5b0 746 int act = t->action & 0xffff;
d0ca268b 747
7997c5b0 748 switch (act) {
d0ca268b 749 case __BLK_TA_QUEUE:
152f6476 750 account_q(t, pci, w);
d5396421 751 log_queue(pci, t, 'Q');
d0ca268b
JA
752 break;
753 case __BLK_TA_BACKMERGE:
152f6476 754 account_m(t, pci, w);
d5396421 755 log_merge(pci, t, 'M');
d0ca268b
JA
756 break;
757 case __BLK_TA_FRONTMERGE:
152f6476 758 account_m(t, pci, w);
d5396421 759 log_merge(pci, t, 'F');
d0ca268b
JA
760 break;
761 case __BLK_TA_GETRQ:
95c15013 762 log_track_getrq(t);
d5396421 763 log_generic(pci, t, 'G');
d0ca268b
JA
764 break;
765 case __BLK_TA_SLEEPRQ:
d5396421 766 log_generic(pci, t, 'S');
d0ca268b
JA
767 break;
768 case __BLK_TA_REQUEUE:
152f6476 769 account_c(t, pci, w, -t->bytes);
d5396421 770 log_queue(pci, t, 'R');
d0ca268b
JA
771 break;
772 case __BLK_TA_ISSUE:
152f6476 773 account_i(t, pci, w);
d5396421 774 log_issue(pci, t, 'D');
d0ca268b
JA
775 break;
776 case __BLK_TA_COMPLETE:
152f6476 777 account_c(t, pci, w, t->bytes);
d5396421 778 log_complete(pci, t, 'C');
d0ca268b
JA
779 break;
780 default:
781 fprintf(stderr, "Bad fs action %x\n", t->action);
1f79c4a0 782 break;
d0ca268b 783 }
d0ca268b
JA
784}
785
d5396421 786static int dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci)
d0ca268b 787{
87b72777
JA
788 int ret = 0;
789
d0ca268b 790 if (t->action & BLK_TC_ACT(BLK_TC_PC))
d5396421 791 ret = dump_trace_pc(t, pci);
d0ca268b 792 else
d5396421 793 dump_trace_fs(t, pci);
87b72777
JA
794
795 events++;
796 return ret;
d0ca268b
JA
797}
798
152f6476 799static void dump_io_stats(struct io_stats *ios, char *msg)
5c017e4b 800{
152f6476
JA
801 fprintf(ofp, "%s\n", msg);
802
803 fprintf(ofp, " Reads Queued: %'8lu, %'8LuKiB\t", ios->qreads, ios->qread_kb);
804 fprintf(ofp, " Writes Queued: %'8lu, %'8LuKiB\n", ios->qwrites,ios->qwrite_kb);
0a6b8fc4 805
152f6476
JA
806 fprintf(ofp, " Read Dispatches: %'8lu, %'8LuKiB\t", ios->ireads, ios->iread_kb);
807 fprintf(ofp, " Write Dispatches: %'8lu, %'8LuKiB\n", ios->iwrites,ios->iwrite_kb);
808 fprintf(ofp, " Reads Completed: %'8lu, %'8LuKiB\t", ios->creads, ios->cread_kb);
809 fprintf(ofp, " Writes Completed: %'8lu, %'8LuKiB\n", ios->cwrites,ios->cwrite_kb);
810 fprintf(ofp, " Read Merges: %'8lu%8c\t", ios->mreads, ' ');
0a6b8fc4 811
152f6476 812 fprintf(ofp, " Write Merges: %'8lu\n", ios->mwrites);
5c017e4b
JA
813}
814
50adc0ba
JA
815static void dump_wait_stats(struct per_process_info *ppi)
816{
b9d40d6f
JA
817 unsigned long rawait = ppi->longest_allocation_wait[0] / 1000;
818 unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000;
819 unsigned long rcwait = ppi->longest_completion_wait[0] / 1000;
820 unsigned long wawait = ppi->longest_allocation_wait[1] / 1000;
821 unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000;
822 unsigned long wcwait = ppi->longest_completion_wait[1] / 1000;
823
824 fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' ');
825 fprintf(ofp, " Allocation wait: %'8lu\n", wawait);
826 fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' ');
827 fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait);
828 fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' ');
829 fprintf(ofp, " Completion wait: %'8lu\n", wcwait);
50adc0ba
JA
830}
831
152f6476
JA
832static void show_process_stats(void)
833{
834 struct per_process_info *ppi;
835
836 ppi = ppi_list;
837 while (ppi) {
838 dump_io_stats(&ppi->io_stats, ppi->name);
50adc0ba 839 dump_wait_stats(ppi);
152f6476
JA
840 ppi = ppi->list_next;
841 }
842
843 fprintf(ofp, "\n");
844}
845
846static void show_cpu_stats(void)
d0ca268b 847{
d5396421 848 struct per_cpu_info foo, *pci;
152f6476 849 struct io_stats *ios;
412819ce 850 int i, pci_events = 0;
5c017e4b
JA
851
852 memset(&foo, 0, sizeof(foo));
d0ca268b 853
a718bd37 854 for (i = 0; i < max_cpus; i++) {
152f6476
JA
855 char cpu[8];
856
d5396421 857 pci = &per_cpu_info[i];
152f6476 858 ios = &pci->io_stats;
5c017e4b 859
d5396421 860 if (!pci->nelems)
afd2d7ad 861 continue;
862
152f6476
JA
863 foo.io_stats.qreads += ios->qreads;
864 foo.io_stats.qwrites += ios->qwrites;
865 foo.io_stats.creads += ios->creads;
866 foo.io_stats.cwrites += ios->cwrites;
867 foo.io_stats.mreads += ios->mreads;
868 foo.io_stats.mwrites += ios->mwrites;
11e51068
NS
869 foo.io_stats.ireads += ios->ireads;
870 foo.io_stats.iwrites += ios->iwrites;
152f6476
JA
871 foo.io_stats.qread_kb += ios->qread_kb;
872 foo.io_stats.qwrite_kb += ios->qwrite_kb;
873 foo.io_stats.cread_kb += ios->cread_kb;
874 foo.io_stats.cwrite_kb += ios->cwrite_kb;
11e51068
NS
875 foo.io_stats.iread_kb += ios->iread_kb;
876 foo.io_stats.iwrite_kb += ios->iwrite_kb;
152f6476
JA
877
878 snprintf(cpu, sizeof(cpu) - 1, "CPU%d:", i);
879 dump_io_stats(ios, cpu);
412819ce 880 pci_events++;
5c017e4b
JA
881 }
882
412819ce 883 if (pci_events > 1) {
152f6476
JA
884 fprintf(ofp, "\n");
885 dump_io_stats(&foo.io_stats, "Total:");
5c017e4b 886 }
d0ca268b 887
152f6476 888 fprintf(ofp, "\nEvents: %'Lu\n", events);
d0ca268b
JA
889}
890
2ff323b0
JA
891#define min(a, b) ((a) < (b) ? (a) : (b))
892
893static struct blk_io_trace *find_trace(void *p, unsigned long offset, int nr)
894{
895 unsigned long max_offset = min(offset,nr * sizeof(struct blk_io_trace));
896 unsigned long off;
897 struct blk_io_trace *bit;
898 __u32 magic;
899
900 for (off = 0; off < max_offset; off++) {
901 bit = p + off;
902
903 magic = be32_to_cpu(bit->magic);
904 if ((magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
905 return bit;
906 }
907
908 return NULL;
909}
910
412819ce 911static int sort_entries(void *traces, unsigned long offset, int nr)
8fc0abbc 912{
412819ce 913 struct per_cpu_info *pci;
8fc0abbc
JA
914 struct blk_io_trace *bit;
915 struct trace *t;
916 void *start = traces;
917 int nelems = 0;
918
6fe4709e 919 while (traces - start <= offset - sizeof(*bit)) {
412819ce
JA
920 if (!nr)
921 break;
922
2ff323b0
JA
923 bit = find_trace(traces, offset - (traces - start), nr);
924 if (!bit)
925 break;
6fe4709e 926
8fc0abbc
JA
927 t = malloc(sizeof(*t));
928 t->bit = bit;
929 memset(&t->rb_node, 0, sizeof(t->rb_node));
930
6fe4709e
JA
931 trace_to_cpu(bit);
932
66fa7233
JA
933 if (verify_trace(bit))
934 break;
935
a718bd37 936 pci = get_cpu_info(bit->cpu);
412819ce
JA
937 pci->nelems++;
938
8fc0abbc
JA
939 if (trace_rb_insert(t))
940 return -1;
941
942 traces += sizeof(*bit) + bit->pdu_len;
943 nelems++;
412819ce 944 nr--;
6fe4709e 945 }
8fc0abbc
JA
946
947 return nelems;
948}
949
412819ce
JA
950static void free_entries_rb(void)
951{
952 struct rb_node *n;
953
7997c5b0 954 while ((n = rb_first(&rb_sort_root)) != NULL) {
412819ce
JA
955 struct trace *t = rb_entry(n, struct trace, rb_node);
956
7997c5b0 957 rb_erase(&t->rb_node, &rb_sort_root);
412819ce
JA
958 free(t);
959 }
960}
961
d5396421 962static void show_entries_rb(void)
8fc0abbc 963{
8fc0abbc 964 struct blk_io_trace *bit;
3aabcd89 965 struct rb_node *n;
8fc0abbc
JA
966 struct trace *t;
967 int cpu;
968
7997c5b0 969 n = rb_first(&rb_sort_root);
3aabcd89
JA
970 if (!n)
971 return;
8fc0abbc 972
3aabcd89 973 do {
8fc0abbc
JA
974 t = rb_entry(n, struct trace, rb_node);
975 bit = t->bit;
976
d5396421 977 cpu = bit->cpu;
87b72777 978 if (cpu > max_cpus) {
8fc0abbc 979 fprintf(stderr, "CPU number too large (%d)\n", cpu);
87b72777 980 break;
8fc0abbc
JA
981 }
982
cfab07eb
AB
983 if (genesis_time == 0)
984 genesis_time = bit->time;
985 bit->time -= genesis_time;
8fc0abbc 986
cfab07eb 987 check_time(bit);
8fc0abbc 988
d5396421 989 if (dump_trace(bit, &per_cpu_info[cpu]))
87b72777
JA
990 break;
991
8fc0abbc
JA
992 } while ((n = rb_next(n)) != NULL);
993}
994
1f79c4a0
JA
995static int read_data(int fd, void *buffer, int bytes, int block)
996{
997 int ret, bytes_left, fl;
998 void *p;
999
1000 fl = fcntl(fd, F_GETFL);
1001
1002 if (!block)
1003 fcntl(fd, F_SETFL, fl | O_NONBLOCK);
1004 else
1005 fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
1006
1007 bytes_left = bytes;
1008 p = buffer;
1009 while (bytes_left > 0) {
1010 ret = read(fd, p, bytes_left);
1011 if (!ret)
1012 return 1;
1013 else if (ret < 0) {
1014 if (errno != EAGAIN)
1015 perror("read");
1016 return -1;
1017 } else {
1018 p += ret;
1019 bytes_left -= ret;
1020 }
1021 }
1022
1023 return 0;
1024}
1025
d5396421 1026static int do_file(void)
d0ca268b 1027{
a718bd37 1028 int i, nfiles;
d0ca268b 1029
a718bd37
NS
1030 for (i = 0, nfiles = 0;; i++, nfiles++) {
1031 struct per_cpu_info *pci;
87b72777
JA
1032 struct stat st;
1033 void *tb;
1034
a718bd37 1035 pci = get_cpu_info(i);
d0ca268b 1036
d5396421
JA
1037 snprintf(pci->fname, sizeof(pci->fname)-1,"%s_out.%d", dev, i);
1038 if (stat(pci->fname, &st) < 0)
d0ca268b 1039 break;
afd2d7ad 1040 if (!st.st_size)
1041 continue;
d0ca268b 1042
d5396421 1043 printf("Processing %s\n", pci->fname);
8fc0abbc 1044
87b72777 1045 tb = malloc(st.st_size);
8fc0abbc 1046
d5396421
JA
1047 pci->fd = open(pci->fname, O_RDONLY);
1048 if (pci->fd < 0) {
1049 perror(pci->fname);
3aabcd89 1050 break;
d0ca268b 1051 }
afd2d7ad 1052
1f79c4a0 1053 if (read_data(pci->fd, tb, st.st_size, 1))
3aabcd89 1054 break;
d0ca268b 1055
a718bd37 1056 if (sort_entries(tb, st.st_size, ~0U) == -1)
3aabcd89 1057 break;
d0ca268b 1058
d5396421 1059 close(pci->fd);
d5396421
JA
1060 printf("\t%2d %10s %15d\n", i, pci->fname, pci->nelems);
1061
1062 }
1063
1064 if (!nfiles) {
1065 fprintf(stderr, "No files found\n");
1066 return 1;
1067 }
1068
1069 show_entries_rb();
d5396421
JA
1070 return 0;
1071}
1072
2ff323b0 1073static void resize_buffer(void **buffer, long *size, long offset)
d5396421 1074{
2ff323b0 1075 long old_size = *size;
d5396421 1076
2ff323b0
JA
1077 *size *= 2;
1078 *buffer = realloc(*buffer, *size);
1079 memset(*buffer + offset, 0, *size - old_size);
412819ce 1080}
d5396421 1081
0a94916b 1082static int read_sort_events(int fd, void **buffer, long *max_offset)
412819ce 1083{
0a94916b 1084 long offset;
412819ce 1085 int events;
d5396421 1086
0a94916b
JA
1087 if (*max_offset == 0) {
1088 *max_offset = 128 * sizeof(struct blk_io_trace);
1089 *buffer = malloc(*max_offset);
1090 }
1091
412819ce
JA
1092 events = 0;
1093 offset = 0;
d5396421 1094
412819ce
JA
1095 do {
1096 struct blk_io_trace *t;
1097 int pdu_len;
d5396421 1098
0a94916b
JA
1099 if (*max_offset - offset < sizeof(*t))
1100 resize_buffer(buffer, max_offset, offset);
d5396421 1101
c80c18a7
JA
1102 if (read_data(fd, *buffer + offset, sizeof(*t), !events))
1103 break;
d5396421 1104
412819ce
JA
1105 t = *buffer + offset;
1106 offset += sizeof(*t);
d5396421 1107
412819ce 1108 pdu_len = be16_to_cpu(t->pdu_len);
2ff323b0 1109 if (pdu_len) {
0a94916b
JA
1110 if (*max_offset - offset < pdu_len)
1111 resize_buffer(buffer, max_offset, offset);
d5396421 1112
2ff323b0
JA
1113 if (read_data(fd, *buffer + offset, pdu_len, 1))
1114 break;
d5396421 1115
2ff323b0
JA
1116 offset += pdu_len;
1117 }
d5396421 1118
412819ce 1119 events++;
79f19470 1120 } while (!is_done() && events < rb_batch);
d5396421 1121
412819ce
JA
1122 return events;
1123}
d5396421 1124
412819ce
JA
1125static int do_stdin(void)
1126{
1127 int fd;
1128 void *ptr;
0a94916b 1129 long max_offset;
d5396421 1130
1f79c4a0 1131 fd = dup(STDIN_FILENO);
0a94916b 1132 max_offset = 0;
412819ce
JA
1133 do {
1134 int events;
d5396421 1135
0a94916b 1136 events = read_sort_events(fd, &ptr, &max_offset);
412819ce
JA
1137 if (!events)
1138 break;
1139
2ff323b0
JA
1140 if (sort_entries(ptr, ~0UL, events) == -1)
1141 break;
1142
412819ce
JA
1143 show_entries_rb();
1144 free_entries_rb();
d5396421
JA
1145 } while (1);
1146
0a94916b 1147 free(ptr);
d5396421 1148 close(fd);
d5396421
JA
1149 return 0;
1150}
d0ca268b 1151
1f79c4a0 1152static void flush_output(void)
412819ce 1153{
152f6476 1154 fflush(ofp);
412819ce
JA
1155}
1156
1f79c4a0 1157static void handle_sigint(int sig)
412819ce
JA
1158{
1159 done = 1;
1160 flush_output();
1161}
1162
1f79c4a0
JA
1163static void usage(char *prog)
1164{
152f6476 1165 fprintf(stderr, "Usage: %s -i <name> [-o <output>][-s]\n", prog);
1f79c4a0
JA
1166}
1167
d5396421
JA
1168int main(int argc, char *argv[])
1169{
152f6476 1170 char *ofp_buffer;
a66877e6 1171 int c, ret, mode;
d5396421
JA
1172
1173 while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
1174 switch (c) {
1175 case 'i':
66efebf8 1176 dev = optarg;
d5396421
JA
1177 break;
1178 case 'o':
66efebf8 1179 output_name = optarg;
d5396421 1180 break;
79f19470
JA
1181 case 'b':
1182 rb_batch = atoi(optarg);
1183 if (rb_batch <= 0)
1184 rb_batch = RB_BATCH_DEFAULT;
1185 break;
152f6476
JA
1186 case 's':
1187 per_process_stats = 1;
1188 break;
7997c5b0
JA
1189 case 't':
1190 track_ios = 1;
1191 break;
d5396421 1192 default:
1f79c4a0 1193 usage(argv[0]);
d5396421
JA
1194 return 1;
1195 }
d0ca268b
JA
1196 }
1197
d5396421 1198 if (!dev) {
1f79c4a0 1199 usage(argv[0]);
d5396421
JA
1200 return 1;
1201 }
1202
7997c5b0
JA
1203 memset(&rb_sort_root, 0, sizeof(rb_sort_root));
1204 memset(&rb_track_root, 0, sizeof(rb_track_root));
412819ce
JA
1205
1206 signal(SIGINT, handle_sigint);
1207 signal(SIGHUP, handle_sigint);
1208 signal(SIGTERM, handle_sigint);
d5396421 1209
d69db225
JA
1210 setlocale(LC_NUMERIC, "en_US");
1211
a66877e6 1212 if (!output_name) {
152f6476 1213 ofp = fdopen(STDOUT_FILENO, "w");
a66877e6
JA
1214 mode = _IOLBF;
1215 } else {
152f6476
JA
1216 char ofname[128];
1217
1218 snprintf(ofname, sizeof(ofname) - 1, "%s.log", output_name);
1219 ofp = fopen(ofname, "w");
a66877e6 1220 mode = _IOFBF;
152f6476
JA
1221 }
1222
1223 if (!ofp) {
1224 perror("fopen");
1225 return 1;
1226 }
1227
1228 ofp_buffer = malloc(4096);
a66877e6 1229 if (setvbuf(ofp, ofp_buffer, mode, 4096)) {
152f6476
JA
1230 perror("setvbuf");
1231 return 1;
1232 }
1233
d5396421
JA
1234 if (!strcmp(dev, "-"))
1235 ret = do_stdin();
1236 else
1237 ret = do_file();
1238
152f6476
JA
1239 if (per_process_stats)
1240 show_process_stats();
1241
1242 show_cpu_stats();
1243
412819ce 1244 flush_output();
d5396421 1245 return ret;
d0ca268b 1246}