Cleanups: Fixed IOPs in btt left over at end of run
[blktrace.git] / btt / globals.h
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 <time.h>
24
25 #include "blktrace.h"
26 #include "rbtree.h"
27 #include "list.h"
28
29 /*
30  * 0 == 1 blk
31  * 1 == 2 blks
32  * ...
33  * 1022 == 1023 blks
34  * 1023 == 1024 blks
35  * 1024 == > 1024 blks
36  */
37 #define N_HIST_BKTS     1025
38
39 #define BIT_TIME(t)     ((double)SECONDS(t) + ((double)NANO_SECONDS(t) / 1.0e9))
40
41 #define BIT_START(iop)  ((iop)->t.sector)
42 #define BIT_END(iop)    ((iop)->t.sector + ((iop)->t.bytes >> 9))
43 #define IOP_READ(iop)   ((iop)->t.action & BLK_TC_ACT(BLK_TC_READ))
44 #define IOP_RW(iop)     (IOP_READ(iop) ? 1 : 0)
45
46 #define TO_SEC(nanosec) ((double)(nanosec) / 1.0e9)
47 #define TO_MSEC(nanosec) (1000.0 * TO_SEC(nanosec))
48
49 enum iop_type {
50         IOP_Q = 0,
51         IOP_X = 1,
52         IOP_A = 2,
53         IOP_G = 3,
54         IOP_M = 4,
55         IOP_D = 5,
56         IOP_C = 6,
57         IOP_R = 7,
58         IOP_I = 8
59 };
60 #define N_IOP_TYPES     (IOP_I + 1)
61
62 struct file_info {
63         struct file_info *next;
64         FILE *ofp;
65         char *oname;
66 };
67
68 struct mode {
69         int most_seeks, nmds;
70         long long *modes;
71 };
72
73 struct io;
74 struct io_list {
75         struct list_head head;
76         struct io *iop;
77         int cy_users;
78 };
79
80 struct avg_info {
81         __u64 min, max, total;
82         double avg;
83         int n;
84 };
85
86 struct avgs_info {
87         struct avg_info q2q_dm;
88         struct avg_info q2a_dm;
89         struct avg_info q2c_dm;
90
91         struct avg_info q2q;
92         struct avg_info q2a;
93         struct avg_info q2g;
94         struct avg_info g2i;
95         struct avg_info q2m;
96         struct avg_info i2d;
97         struct avg_info m2d;
98         struct avg_info d2c;
99         struct avg_info q2c;
100
101         struct avg_info blks;           /* Blocks transferred */
102 };
103
104 struct range_info {
105         struct list_head head;          /* on: qranges OR cranges */
106         __u64 start, end;
107 };
108
109 struct region_info {
110         struct list_head qranges;
111         struct list_head cranges;
112 };
113
114 struct p_info {
115         struct region_info regions;
116         struct avgs_info avgs;
117         __u64 last_q;
118         __u32 pid;
119         char *name;
120 };
121
122 struct devmap {
123         struct devmap *next;
124         unsigned int host, bus, target, lun, irq, cpu;
125         char model[64];
126         char device[32], node[32], pci[32], devno[32];
127 };
128
129 struct stats {
130         __u64 rqm[2], ios[2], sec[2], wait, svctm;
131         double last_qu_change, last_dev_change, tot_qusz, idle_time;
132         int cur_qusz, cur_dev;
133 };
134
135 struct stats_t {
136         double n;
137         double rqm_s[2], ios_s[2], sec_s[2];
138         double avgrq_sz, avgqu_sz, await, svctm, p_util;
139 };
140
141 struct d_info {
142         struct list_head all_head, hash_head;
143         void *heads;
144         struct region_info regions;
145         struct devmap *map;
146         void *q2q_handle, *seek_handle, *bno_dump_handle, *unplug_hist_handle;
147         void *q2d_priv;
148         FILE *d2c_ofp, *q2c_ofp;
149         struct avgs_info avgs;
150         struct stats stats, all_stats;
151         __u64 last_q, n_qs, n_ds;
152         __u64 n_act_q, t_act_q; /* # currently active when Q comes in */
153         __u32 device;
154
155         int pre_culling;
156         int is_plugged, nplugs, nplugs_t, n_timer_unplugs;
157         __u64 nios_up, nios_upt;
158         double start_time, last_plug, plugged_time, end_time;
159 };
160
161 struct io {
162         struct rb_node rb_node;
163         struct list_head f_head, a_head;
164         struct d_info *dip;
165         struct p_info *pip;
166         void *pdu;
167         __u64 bytes_left, g_time, i_time, m_time, d_time, c_time, d_sec, c_sec;
168         __u32 d_nsec, c_nsec;
169
170         struct blk_io_trace t;
171
172         int linked;
173         enum iop_type type;
174 };
175
176 /* bt_timeline.c */
177
178 extern char bt_timeline_version[], *devices, *exes, *input_name, *output_name;
179 extern char *seek_name, *iostat_name, *d2c_name, *q2c_name, *per_io_name;
180 extern char *bno_dump_name, *unplug_hist_name;
181 extern double range_delta;
182 extern FILE *ranges_ofp, *avgs_ofp, *iostat_ofp, *per_io_ofp;
183 extern int verbose, done, time_bounded, output_all_data, seek_absolute;
184 extern unsigned int n_devs;
185 extern unsigned long n_traces;
186 extern struct list_head all_devs, all_procs;
187 extern struct avgs_info all_avgs;
188 extern __u64 last_q;
189 extern struct region_info all_regions;
190 extern struct list_head all_ios, free_ios;
191 extern __u64 iostat_interval, iostat_last_stamp;
192 extern time_t genesis, last_vtrace;
193 extern double t_astart, t_aend;
194 extern __u64 q_histo[N_HIST_BKTS], d_histo[N_HIST_BKTS];
195
196 /* args.c */
197 void handle_args(int argc, char *argv[]);
198 void clean_args();
199
200 /* devmap.c */
201 int dev_map_read(char *fname);
202 struct devmap *dev_map_find(__u32 device);
203 void dev_map_exit(void);
204
205 /* devs.c */
206 void init_dev_heads(void);
207 struct d_info *dip_add(__u32 device, struct io *iop);
208 void dip_rem(struct io *iop);
209 struct d_info *__dip_find(__u32 device);
210 void dip_foreach_list(struct io *iop, enum iop_type type, struct list_head *hd);
211 void dip_foreach(struct io *iop, enum iop_type type,
212                  void (*fnc)(struct io *iop, struct io *this), int rm_after);
213 struct io *dip_find_sec(struct d_info *dip, enum iop_type type, __u64 sec);
214 void dip_foreach_out(void (*func)(struct d_info *, void *), void *arg);
215 void dip_plug(__u32 dev, double cur_time);
216 void dip_unplug(__u32 dev, double cur_time, __u64 nio_ups);
217 void dip_unplug_tm(__u32 dev, __u64 nio_ups);
218 void dip_exit(void);
219
220 /* dip_rb.c */
221 int rb_insert(struct rb_root *root, struct io *iop);
222 struct io *rb_find_sec(struct rb_root *root, __u64 sec);
223 void rb_foreach(struct rb_node *n, struct io *iop,
224                       void (*fnc)(struct io *iop, struct io *this),
225                       struct list_head *head);
226
227 /* iostat.c */
228 void iostat_init(void);
229 void iostat_getrq(struct io *iop);
230 void iostat_merge(struct io *iop);
231 void iostat_issue(struct io *iop);
232 void iostat_unissue(struct io *iop);
233 void iostat_complete(struct io *d_iop, struct io *c_iop);
234 void iostat_check_time(__u64 stamp);
235 void iostat_dump_stats(__u64 stamp, int all);
236
237 /* latency.c */
238 void latency_init(struct d_info *dip);
239 void latency_clean(void);
240 void latency_d2c(struct d_info *dip, __u64 tstamp, __u64 latency);
241 void latency_q2c(struct d_info *dip, __u64 tstamp, __u64 latency);
242
243 /* misc.c */
244 int in_devices(struct blk_io_trace *t);
245 void add_file(struct file_info **fipp, FILE *fp, char *oname);
246 void clean_files(struct file_info **fipp);
247 void add_buf(void *buf);
248 void clean_bufs(void);
249 void dbg_ping(void);
250
251 /* mmap.c */
252 void setup_ifile(char *fname);
253 void cleanup_ifile(void);
254 int next_trace(struct blk_io_trace *t, void **pdu);
255
256 /* output.c */
257 int output_avgs(FILE *ofp);
258 int output_ranges(FILE *ofp);
259 char *make_dev_hdr(char *pad, size_t len, struct d_info *dip);
260
261 /* proc.c */
262 void add_process(__u32 pid, char *name);
263 struct p_info *find_process(__u32 pid, char *name);
264 void pip_update_q(struct io *iop);
265 void pip_foreach_out(void (*f)(struct p_info *, void *), void *arg);
266 void pip_exit(void);
267
268 /* bno_dump.c */
269 void *bno_dump_init(__u32 device);
270 void bno_dump_exit(void *param);
271 void bno_dump_add(void *handle, struct io *iop);
272 void bno_dump_clean(void);
273
274 /* q2d.c */
275 void q2d_histo_add(void *priv, __u64 q2d);
276 void *q2d_init(void);
277 void q2d_release(void *priv);
278 void q2d_display_header(FILE *fp);
279 void q2d_display_dashes(FILE *fp);
280 void q2d_display(FILE *fp, void *priv);
281 int q2d_ok(void *priv);
282 void q2d_acc(void *a1, void *a2);
283
284 /* seek.c */
285 void *seeki_init(char *str);
286 void seeki_exit(void *param);
287 void seek_clean(void);
288 void seeki_add(void *handle, struct io *iop);
289 double seeki_mean(void *handle);
290 long long seeki_nseeks(void *handle);
291 long long seeki_median(void *handle);
292 int seeki_mode(void *handle, struct mode *mp);
293
294 /* trace.c */
295 void add_trace(struct io *iop);
296
297 /* trace_complete.c */
298 void trace_complete(struct io *c_iop);
299
300 /* trace_im.c */
301 void run_im(struct io *im_iop, struct io *d_iop, struct io *c_iop);
302 void run_unim(struct io *im_iop, struct io *d_iop, struct io *c_iop);
303 int ready_im(struct io *im_iop, struct io *c_iop);
304 void trace_insert(struct io *i_iop);
305 void trace_merge(struct io *m_iop);
306 void trace_getrq(struct io *g_iop);
307
308 /* trace_issue.c */
309 void run_issue(struct io *d_iop, struct io *u_iop, struct io *c_iop);
310 void run_unissue(struct io *d_iop, struct io *u_iop, struct io *c_iop);
311 int ready_issue(struct io *d_iop, struct io *c_iop);
312 void trace_issue(struct io *d_iop);
313
314 /* trace_plug.c */
315 __u64 get_nio_up(struct io *u_iop);
316 void trace_plug(struct io *p_iop);
317 void trace_unplug_io(struct io *u_iop);
318 void trace_unplug_timer(struct io *u_iop);
319
320 /* trace_queue.c */
321 void run_queue(struct io *q_iop, struct io *u_iop, struct io *c_iop);
322 int ready_queue(struct io *q_iop, struct io *c_iop);
323 void trace_queue(struct io *q_iop);
324
325 /* trace_remap.c */
326 void run_remap(struct io *a_iop, struct io *u_iop, struct io *c_iop);
327 int ready_remap(struct io *a_iop, struct io *c_iop);
328 void trace_remap(struct io *a_iop);
329
330 /* trace_requeue.c */
331 void trace_requeue(struct io *r_iop);
332
333 /* unplug_hist.c */
334 void *unplug_hist_init(__u32 device);
335 void unplug_hist_exit(void *arg);
336 void unplug_hist_add(struct io *u_iop);
337
338 #include "inlines.h"