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