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)
76 void __dip_exit(struct d_info *dip)
78 list_del(&dip->all_head);
79 __destroy_heads(dip->heads);
80 region_exit(&dip->regions);
81 seeki_free(dip->seek_handle);
82 seeki_free(dip->q2q_handle);
83 aqd_free(dip->aqd_handle);
84 plat_free(dip->q2d_plat_handle);
85 plat_free(dip->q2c_plat_handle);
86 plat_free(dip->d2c_plat_handle);
87 bno_dump_free(dip->bno_dump_handle);
88 unplug_hist_free(dip->up_hist_handle);
89 rstat_free(dip->rstat_handle);
91 q2d_free(dip->q2d_priv);
99 struct list_head *p, *q;
101 list_for_each_safe(p, q, &all_devs) {
102 struct d_info *dip = list_entry(p, struct d_info, all_head);
107 static inline FILE *open_pit(struct d_info *dip)
112 sprintf(str, "%s_pit.dat", dip->dip_name);
113 if ((fp = my_fopen(str, "w")) == NULL)
119 struct d_info *dip_alloc(__u32 device, struct io *iop)
121 struct d_info *dip = __dip_find(device);
124 dip = malloc(sizeof(struct d_info));
125 memset(dip, 0, sizeof(*dip));
126 dip->device = device;
127 dip->devmap = dev_map_find(device);
128 dip->last_q = (__u64)-1;
129 dip->heads = dip_rb_mkhds();
130 region_init(&dip->regions);
131 dip->start_time = BIT_TIME(iop->t.time);
132 dip->pre_culling = 1;
134 mkhandle(dip, dip->dip_name, 256);
137 dip->aqd_handle = aqd_alloc(dip);
138 dip->bno_dump_handle = bno_dump_alloc(dip);
139 dip->up_hist_handle = unplug_hist_alloc(dip);
140 dip->seek_handle = seeki_alloc(dip, "_d2d");
141 dip->q2q_handle = seeki_alloc(dip, "_q2q");
142 dip->q2d_plat_handle = plat_alloc(dip, "_q2d");
143 dip->q2c_plat_handle = plat_alloc(dip, "_q2c");
144 dip->d2c_plat_handle = plat_alloc(dip, "_d2c");
145 dip->rstat_handle = rstat_alloc(dip);
148 dip->pit_fp = open_pit(dip);
151 dip->q2d_priv = q2d_alloc();
153 list_add_tail(&dip->hash_head, &dev_heads[DEV_HASH(device)]);
154 list_add_tail(&dip->all_head, &all_devs);
158 if (dip->pre_culling) {
159 if (iop->type == IOP_Q || iop->type == IOP_A)
160 dip->pre_culling = 0;
165 iop->linked = dip_rb_ins(dip, iop);
166 dip->end_time = BIT_TIME(iop->t.time);
171 void iop_rem_dip(struct io *iop)
179 void dip_foreach(struct io *iop, enum iop_type type,
180 void (*fnc)(struct io *iop, struct io *this), int rm_after)
185 struct list_head *p, *q;
187 dip_rb_fe(iop->dip, type, iop, fnc, &head);
188 list_for_each_safe(p, q, &head) {
189 this = list_entry(p, struct io, f_head);
190 list_del(&this->f_head);
194 dip_rb_fe(iop->dip, type, iop, fnc, NULL);
197 void dip_foreach_list(struct io *iop, enum iop_type type, struct list_head *hd)
199 dip_rb_fe(iop->dip, type, iop, NULL, hd);
202 struct io *dip_find_sec(struct d_info *dip, enum iop_type type, __u64 sec)
204 return dip_rb_find_sec(dip, type, sec);
207 void dip_foreach_out(void (*func)(struct d_info *, void *), void *arg)
209 if (devices == NULL) {
211 __list_for_each(p, &all_devs)
212 func(list_entry(p, struct d_info, all_head), arg);
216 unsigned int mjr, mnr;
219 while (p && ((i = sscanf(p, "%u,%u", &mjr, &mnr)) == 2)) {
220 dip = __dip_find((__u32)((mjr << MINORBITS) | mnr));
228 void dip_plug(__u32 dev, double cur_time)
230 struct d_info *dip = __dip_find(dev);
232 if (dip && !dip->is_plugged) {
234 dip->last_plug = cur_time;
238 static inline void unplug(struct d_info *dip, double cur_time)
241 dip->plugged_time += (cur_time - dip->last_plug);
244 void dip_unplug(__u32 dev, double cur_time, __u64 nios_up)
246 struct d_info *dip = __dip_find(dev);
248 if (dip && dip->is_plugged) {
250 dip->nios_up += nios_up;
251 unplug(dip, cur_time);
255 void dip_unplug_tm(__u32 dev, double cur_time, __u64 nios_up)
257 struct d_info *dip = __dip_find(dev);
259 if (dip && dip->is_plugged) {
260 dip->nios_upt += nios_up;
262 unplug(dip, cur_time);
266 void dip_cleanup(void)
268 struct list_head *p, *q;
270 list_for_each_safe(p, q, &all_devs) {
271 struct d_info *dip = list_entry(p, struct d_info, all_head);
273 if (dip->n_qs == 0 && dip->n_ds == 0)