2 * blktrace output analysis: generate a timeline & gather statistics
4 * Copyright (C) 2006 Alan D. Brunelle <Alan.Brunelle@hp.com>
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.
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.
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
24 #define N_DEV_HASH 128
25 #define DEV_HASH(dev) ((MAJOR(dev) ^ MINOR(dev)) & (N_DEV_HASH - 1))
26 struct list_head dev_heads[N_DEV_HASH];
28 static inline void *dip_rb_mkhds(void)
30 size_t len = N_IOP_TYPES * sizeof(struct rb_root);
31 return memset(malloc(len), 0, len);
34 static void __destroy(struct rb_node *n)
37 struct io *iop = rb_entry(n, struct io, rb_node);
39 __destroy(n->rb_left);
40 __destroy(n->rb_right);
45 static void __destroy_heads(struct rb_root *roots)
49 for (i = 0; i < N_IOP_TYPES; i++)
50 __destroy(roots[i].rb_node);
55 void init_dev_heads(void)
58 for (i = 0; i < N_DEV_HASH; i++)
59 INIT_LIST_HEAD(&dev_heads[i]);
62 struct d_info *__dip_find(__u32 device)
67 __list_for_each(p, &dev_heads[DEV_HASH(device)]) {
68 dip = list_entry(p, struct d_info, hash_head);
69 if (device == dip->device)
79 struct list_head *p, *q;
81 list_for_each_safe(p, q, &all_devs) {
82 dip = list_entry(p, struct d_info, all_head);
84 __destroy_heads(dip->heads);
85 region_exit(&dip->regions);
86 seeki_exit(dip->seek_handle);
87 seeki_exit(dip->q2q_handle);
88 aqd_exit(dip->aqd_handle);
89 plat_exit(dip->q2d_plat_handle);
90 plat_exit(dip->q2c_plat_handle);
91 plat_exit(dip->d2c_plat_handle);
92 bno_dump_exit(dip->bno_dump_handle);
93 unplug_hist_exit(dip->unplug_hist_handle);
95 q2d_release(dip->q2d_priv);
102 static inline char *mkhandle(char *str, __u32 device, char *post)
104 int mjr = device >> MINORBITS;
105 int mnr = device & ((1 << MINORBITS) - 1);
107 sprintf(str, "%03d,%03d%s", mjr, mnr, post);
111 static inline FILE *open_pit(char *str)
113 FILE *fp = fopen(str, "w");
121 struct d_info *dip_add(__u32 device, struct io *iop)
123 struct d_info *dip = __dip_find(device);
128 dip = malloc(sizeof(struct d_info));
129 memset(dip, 0, sizeof(*dip));
130 dip->heads = dip_rb_mkhds();
131 region_init(&dip->regions);
132 dip->device = device;
133 dip->last_q = (__u64)-1;
134 dip->map = dev_map_find(device);
135 dip->bno_dump_handle = bno_dump_init(device);
136 dip->unplug_hist_handle = unplug_hist_init(device);
137 dip->seek_handle = seeki_init(mkhandle(str, device, "_d2d"));
138 dip->q2q_handle = seeki_init(mkhandle(str, device, "_q2q"));
139 dip->aqd_handle = aqd_init(mkhandle(str, device, "_aqd"));
140 dip->q2d_plat_handle =
141 plat_init(mkhandle(str, device, "_q2d_plat"));
142 dip->q2c_plat_handle =
143 plat_init(mkhandle(str, device, "_q2c_plat"));
144 dip->d2c_plat_handle =
145 plat_init(mkhandle(str, device, "_d2c_plat"));
147 list_add_tail(&dip->hash_head, &dev_heads[DEV_HASH(device)]);
148 list_add_tail(&dip->all_head, &all_devs);
149 dip->start_time = BIT_TIME(iop->t.time);
150 dip->pre_culling = 1;
152 dip->q2d_priv = q2d_init();
155 dip->pit_fp = open_pit(mkhandle(per_io_trees,
156 device, "_pit.dat"));
159 if (dip->pre_culling) {
160 if (iop->type == IOP_Q || iop->type == IOP_A)
161 dip->pre_culling = 0;
166 iop->linked = dip_rb_ins(dip, iop);
167 dip->end_time = BIT_TIME(iop->t.time);
172 void dip_rem(struct io *iop)
180 void dip_foreach(struct io *iop, enum iop_type type,
181 void (*fnc)(struct io *iop, struct io *this), int rm_after)
186 struct list_head *p, *q;
188 dip_rb_fe(iop->dip, type, iop, fnc, &head);
189 list_for_each_safe(p, q, &head) {
190 this = list_entry(p, struct io, f_head);
191 list_del(&this->f_head);
196 dip_rb_fe(iop->dip, type, iop, fnc, NULL);
199 void dip_foreach_list(struct io *iop, enum iop_type type, struct list_head *hd)
201 dip_rb_fe(iop->dip, type, iop, NULL, hd);
204 struct io *dip_find_sec(struct d_info *dip, enum iop_type type, __u64 sec)
206 return dip_rb_find_sec(dip, type, sec);
209 void dip_foreach_out(void (*func)(struct d_info *, void *), void *arg)
211 if (devices == NULL) {
213 __list_for_each(p, &all_devs)
214 func(list_entry(p, struct d_info, all_head), arg);
219 unsigned int mjr, mnr;
222 while (p && ((i = sscanf(p, "%u,%u", &mjr, &mnr)) == 2)) {
223 dip = __dip_find((__u32)((mjr << MINORBITS) | mnr));
231 void dip_plug(__u32 dev, double cur_time)
233 struct d_info *dip = __dip_find(dev);
235 if (!dip || dip->is_plugged) return;
238 dip->last_plug = cur_time;
241 void dip_unplug(__u32 dev, double cur_time, __u64 nios_up)
243 struct d_info *dip = __dip_find(dev);
245 if (dip && dip->is_plugged) {
247 dip->plugged_time += (cur_time - dip->last_plug);
249 dip->nios_up += nios_up;
253 void dip_unplug_tm(__u32 dev, __u64 nios_up)
255 struct d_info *dip = __dip_find(dev);
257 if (dip && dip->is_plugged) {
258 dip->n_timer_unplugs++;
259 dip->nios_upt += nios_up;