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