[PATCH] Added iostat-style statistics to btt
[blktrace.git] / btt / bt_timeline.c
1 /*
2  * blktrace output analysis: generate a timeline & gather statistics
3  *
4  * Copyright (C) 2006 Alan D. Brunelle <Alan.Brunelle@hp.com>
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  */
21 #include <stdio.h>
22 #include <string.h>
23 #include <unistd.h>
24
25 #include "globals.h"
26
27 char bt_timeline_version[] = "0.99";
28
29 char *devices = NULL;
30 char *exes = NULL;
31 char *input_name = NULL;
32 char *output_name = NULL;
33 char *seek_name = NULL;
34 double range_delta = 0.1;
35 FILE *ranges_ofp, *avgs_ofp;
36 int is_lvm = -1;
37 int verbose = 0;
38 int ifd;
39 unsigned long n_traces, n_io_allocs, n_io_frees;
40 struct avgs_info all_avgs;
41 __u64 last_q = (__u64)-1;
42 unsigned int pending_xs;
43
44 unsigned int n_devs;
45 LIST_HEAD(all_devs);
46 LIST_HEAD(all_ios);
47 LIST_HEAD(all_procs);
48 struct my_mem *free_ios = NULL;
49 struct my_mem *free_bits = NULL;
50
51 struct region_info all_regions = {
52         .qranges = LIST_HEAD_INIT(all_regions.qranges),
53         .cranges = LIST_HEAD_INIT(all_regions.cranges),
54         .qr_cur = NULL,
55         .cr_cur = NULL
56 };
57
58 char iop_map[] = { 'Q', 'X', 'A', 'M', 'I', 'D', 'C', 'Y' };
59
60 struct blk_io_trace *convert_to_cpu(struct blk_io_trace *t);
61 int process(void);
62
63 int main(int argc, char *argv[])
64 {
65         handle_args(argc, argv);
66
67         cy_init();
68         iostat_init();
69         if (process() || output_avgs(avgs_ofp) || output_ranges(ranges_ofp))
70                 return 1;
71
72         return 0;
73 }
74
75 int process(void)
76 {
77         int ret = 0;
78         struct blk_io_trace *t;
79         struct io *iop = IO_ZALLOC();
80
81         while (!do_read(ifd, &iop->t, sizeof(iop->t))) {
82                 t = convert_to_cpu(&iop->t);
83                 if (t->pdu_len > 0) {
84                         iop->pdu = malloc(t->pdu_len);
85                         if (do_read(ifd, iop->pdu, t->pdu_len)) {
86                                 free(iop->pdu);
87                                 ret = 1;
88                                 break;
89                         }
90                 }
91                 add_trace(iop);
92                 iop = IO_ZALLOC();
93         }
94         IO_FREE(iop);
95
96         if (iostat_ofp) {
97                 fprintf(iostat_ofp, "\n");
98                 iostat_dump_stats(iostat_last_stamp, 1);
99         }
100
101         if (verbose)
102                 printf("\n%10lu traces, %10lu mallocs %1lu frees\n",
103                                        n_traces, n_io_allocs, n_io_frees);
104
105         cy_shutdown();
106         return ret;
107 }