Add in unplug IO count histogram
[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 #include <sys/time.h>
25 #include <time.h>
26 #include "globals.h"
27
28 char bt_timeline_version[] = "0.99.1";
29
30 char *devices, *exes, *input_name, *output_name, *seek_name, *bno_dump_name;
31 char *d2c_name, *q2c_name, *per_io_name, *unplug_hist_name;
32 FILE *ranges_ofp, *avgs_ofp, *per_io_ofp;
33 int verbose, done, time_bounded, output_all_data, seek_absolute;
34 double t_astart, t_aend;
35 unsigned long n_traces;
36 struct avgs_info all_avgs;
37 unsigned int n_devs;
38 time_t genesis, last_vtrace;
39 LIST_HEAD(all_devs);
40 LIST_HEAD(all_procs);
41 LIST_HEAD(free_ios);
42 LIST_HEAD(free_bilinks);
43 LIST_HEAD(rmhd);
44 LIST_HEAD(retries);
45 __u64 q_histo[N_HIST_BKTS], d_histo[N_HIST_BKTS];
46
47 double range_delta = 0.1;
48 __u64 last_q = (__u64)-1;
49 __u64 next_retry_check = 0;
50
51 struct region_info all_regions = {
52         .qranges = LIST_HEAD_INIT(all_regions.qranges),
53         .cranges = LIST_HEAD_INIT(all_regions.cranges),
54 };
55
56 #if defined(DEBUG)
57         int rb_tree_size;
58 #endif
59
60 #if defined(COUNT_IOS)
61 unsigned long nios_reused, nios_alloced, nios_freed;
62 LIST_HEAD(cios);
63 #endif
64
65 int process(void);
66
67 int main(int argc, char *argv[])
68 {
69         handle_args(argc, argv);
70
71         init_dev_heads();
72         iostat_init();
73         if (process() || output_avgs(avgs_ofp) || output_ranges(ranges_ofp))
74                 return 1;
75
76         if (iostat_ofp) {
77                 fprintf(iostat_ofp, "\n");
78                 iostat_dump_stats(iostat_last_stamp, 1);
79         }
80
81         if (ranges_ofp != stdout) 
82                 fclose(ranges_ofp);
83         if (avgs_ofp != stdout) 
84                 fclose(avgs_ofp);
85
86         seek_clean();
87         latency_clean();
88         bno_dump_clean();
89         dev_map_exit();
90         dip_exit();
91         pip_exit();
92         io_free_all();
93         bilink_free_all();
94         region_exit(&all_regions);
95
96         free(input_name);
97         if (output_name) free(output_name);
98
99         clean_bufs();
100
101         return 0;
102 }
103
104 static inline double tv2dbl(struct timeval *tv)
105 {
106         return (double)tv->tv_sec + (((double)tv->tv_usec) / (1000.0 * 1000.0));
107 }
108
109 int process(void)
110 {
111         int ret = 0;
112         struct io *iop = io_alloc();
113         struct timeval tvs, tve;
114
115         genesis = last_vtrace = time(NULL);
116         gettimeofday(&tvs, NULL);
117         while (!done && next_trace(&iop->t, &iop->pdu)) {
118                 add_trace(iop);
119                 iop = io_alloc();
120         }
121
122         io_release(iop);
123         do_retries(0);
124         gettimeofday(&tve, NULL);
125
126         if (verbose) {
127                 double tps, dt_input = tv2dbl(&tve) - tv2dbl(&tvs);
128                 
129                 tps = (double)n_traces / dt_input;
130                 printf("\r                                        "
131                        "                                        \r");
132                 printf("%10lu traces @ %.1lf Ktps in %.6lf seconds\n",
133                         n_traces, tps/1000.0,
134                         dt_input);
135
136 #               if defined(DEBUG)
137                         printf("\ttree = |%d|\n", rb_tree_size);
138                         if (rb_tree_size > 0)
139                                 dump_rb_trees();
140 #               endif
141
142 #               if defined(COUNT_IOS)
143                         {
144                                 struct io *_iop;
145                                 struct list_head *_p;
146                                 FILE *_ofp = fopen("cios.txt", "w");
147                                 printf("(%ld + %ld) = %ld - %ld = %ld\n", 
148                                         nios_alloced, nios_reused, 
149                                         nios_alloced + nios_reused, 
150                                         nios_freed, 
151                                         (nios_alloced + nios_reused) 
152                                                                 - nios_freed);
153
154                                 __list_for_each(_p, &cios) {
155                                         _iop = list_entry(_p, struct io, 
156                                                                 cio_head);
157                                         __dump_iop(_ofp, _iop, 0);
158                                 }
159                                 fclose(_ofp);
160                         }
161 #               endif
162         }
163
164         return ret;
165 }