btt: Missed fopen conversion to my_fopen
[blktrace.git] / btt / devs.c
CommitLineData
63eba147
JA
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>
63eba147
JA
22#include "globals.h"
23
24#define N_DEV_HASH 128
25#define DEV_HASH(dev) ((MAJOR(dev) ^ MINOR(dev)) & (N_DEV_HASH - 1))
6eb42155 26struct list_head dev_heads[N_DEV_HASH];
63eba147 27
69040794
AB
28static inline void *dip_rb_mkhds(void)
29{
30 size_t len = N_IOP_TYPES * sizeof(struct rb_root);
31 return memset(malloc(len), 0, len);
32}
33
34static void __destroy(struct rb_node *n)
35{
36 if (n) {
37 struct io *iop = rb_entry(n, struct io, rb_node);
38
39 __destroy(n->rb_left);
40 __destroy(n->rb_right);
41 io_release(iop);
42 }
43}
44
45static void __destroy_heads(struct rb_root *roots)
46{
47 int i;
48
49 for (i = 0; i < N_IOP_TYPES; i++)
50 __destroy(roots[i].rb_node);
51
52 free(roots);
53}
54
6eb42155 55void init_dev_heads(void)
63eba147
JA
56{
57 int i;
63eba147
JA
58 for (i = 0; i < N_DEV_HASH; i++)
59 INIT_LIST_HEAD(&dev_heads[i]);
60}
61
62struct d_info *__dip_find(__u32 device)
63{
63eba147 64 struct d_info *dip;
d76c5b81 65 struct list_head *p;
63eba147 66
63eba147
JA
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)
70 return dip;
71 }
72
73 return NULL;
74}
75
52481561
AB
76void __dip_exit(struct d_info *dip)
77{
78 list_del(&dip->all_head);
79 __destroy_heads(dip->heads);
80 region_exit(&dip->regions);
81 seeki_exit(dip->seek_handle);
82 seeki_exit(dip->q2q_handle);
83 aqd_exit(dip->aqd_handle);
84 plat_exit(dip->q2d_plat_handle);
85 plat_exit(dip->q2c_plat_handle);
86 plat_exit(dip->d2c_plat_handle);
87 bno_dump_exit(dip->bno_dump_handle);
88 unplug_hist_exit(dip->unplug_hist_handle);
89 if (output_all_data)
90 q2d_release(dip->q2d_priv);
91 if (dip->pit_fp)
92 fclose(dip->pit_fp);
93 free(dip);
94}
95
69040794
AB
96void dip_exit(void)
97{
69040794
AB
98 struct list_head *p, *q;
99
100 list_for_each_safe(p, q, &all_devs) {
52481561
AB
101 struct d_info *dip = list_entry(p, struct d_info, all_head);
102 __dip_exit(dip);
69040794
AB
103 }
104}
105
4c48f14e
AB
106static inline char *mkhandle(char *str, __u32 device, char *post)
107{
108 int mjr = device >> MINORBITS;
109 int mnr = device & ((1 << MINORBITS) - 1);
110
111 sprintf(str, "%03d,%03d%s", mjr, mnr, post);
112 return str;
113}
114
a22df989
AB
115static inline FILE *open_pit(char *str)
116{
99bb5ebc 117 FILE *fp = my_fopen(str, "w");
a22df989
AB
118
119 if (fp == NULL)
120 perror(str);
121
122 return fp;
123}
124
095181f2 125struct d_info *dip_add(__u32 device, struct io *iop)
63eba147
JA
126{
127 struct d_info *dip = __dip_find(device);
128
129 if (dip == NULL) {
4c48f14e
AB
130 char str[256];
131
6eb42155 132 dip = malloc(sizeof(struct d_info));
2fa934ce 133 memset(dip, 0, sizeof(*dip));
6eb42155 134 dip->heads = dip_rb_mkhds();
69040794 135 region_init(&dip->regions);
63eba147
JA
136 dip->device = device;
137 dip->last_q = (__u64)-1;
63eba147 138 dip->map = dev_map_find(device);
69040794 139 dip->bno_dump_handle = bno_dump_init(device);
fc16a815 140 dip->unplug_hist_handle = unplug_hist_init(device);
4c48f14e
AB
141 dip->seek_handle = seeki_init(mkhandle(str, device, "_d2d"));
142 dip->q2q_handle = seeki_init(mkhandle(str, device, "_q2q"));
4ae2c3c6 143 dip->aqd_handle = aqd_init(mkhandle(str, device, "_aqd"));
e47ada10
AB
144 dip->q2d_plat_handle =
145 plat_init(mkhandle(str, device, "_q2d_plat"));
2baef508
AB
146 dip->q2c_plat_handle =
147 plat_init(mkhandle(str, device, "_q2c_plat"));
148 dip->d2c_plat_handle =
149 plat_init(mkhandle(str, device, "_d2c_plat"));
b2ecdd0f 150 latency_init(dip);
63eba147 151 list_add_tail(&dip->hash_head, &dev_heads[DEV_HASH(device)]);
6eb42155 152 list_add_tail(&dip->all_head, &all_devs);
b2822cea 153 dip->start_time = BIT_TIME(iop->t.time);
11997716 154 dip->pre_culling = 1;
951ea56c
AB
155 if (output_all_data)
156 dip->q2d_priv = q2d_init();
63eba147 157 n_devs++;
a22df989
AB
158 if (per_io_trees)
159 dip->pit_fp = open_pit(mkhandle(per_io_trees,
160 device, "_pit.dat"));
63eba147
JA
161 }
162
11997716
AB
163 if (dip->pre_culling) {
164 if (iop->type == IOP_Q || iop->type == IOP_A)
165 dip->pre_culling = 0;
166 else
167 return NULL;
168 }
169
095181f2 170 iop->linked = dip_rb_ins(dip, iop);
b2822cea 171 dip->end_time = BIT_TIME(iop->t.time);
c8b0b334 172
63eba147
JA
173 return dip;
174}
6eb42155
ADB
175
176void dip_rem(struct io *iop)
177{
d76c5b81
AB
178 if (iop->linked) {
179 dip_rb_rem(iop);
180 iop->linked = 0;
181 }
6eb42155
ADB
182}
183
8b10aae0 184void dip_foreach(struct io *iop, enum iop_type type,
6eb42155
ADB
185 void (*fnc)(struct io *iop, struct io *this), int rm_after)
186{
187 if (rm_after) {
188 LIST_HEAD(head);
189 struct io *this;
190 struct list_head *p, *q;
191
192 dip_rb_fe(iop->dip, type, iop, fnc, &head);
193 list_for_each_safe(p, q, &head) {
194 this = list_entry(p, struct io, f_head);
8b10aae0 195 list_del(&this->f_head);
6eb42155
ADB
196 io_release(this);
197 }
198 }
199 else
200 dip_rb_fe(iop->dip, type, iop, fnc, NULL);
201}
202
095181f2
JA
203void dip_foreach_list(struct io *iop, enum iop_type type, struct list_head *hd)
204{
205 dip_rb_fe(iop->dip, type, iop, NULL, hd);
206}
207
6eb42155
ADB
208struct io *dip_find_sec(struct d_info *dip, enum iop_type type, __u64 sec)
209{
210 return dip_rb_find_sec(dip, type, sec);
211}
212
213void dip_foreach_out(void (*func)(struct d_info *, void *), void *arg)
214{
215 if (devices == NULL) {
216 struct list_head *p;
217 __list_for_each(p, &all_devs)
218 func(list_entry(p, struct d_info, all_head), arg);
219 }
220 else {
221 int i;
222 struct d_info *dip;
223 unsigned int mjr, mnr;
224 char *p = devices;
225
226 while (p && ((i = sscanf(p, "%u,%u", &mjr, &mnr)) == 2)) {
227 dip = __dip_find((__u32)((mjr << MINORBITS) | mnr));
6eb42155 228 func(dip, arg);
6eb42155
ADB
229 p = strchr(p, ';');
230 if (p) p++;
231 }
232 }
233}
b2822cea
AB
234
235void dip_plug(__u32 dev, double cur_time)
236{
237 struct d_info *dip = __dip_find(dev);
238
239 if (!dip || dip->is_plugged) return;
240
241 dip->is_plugged = 1;
242 dip->last_plug = cur_time;
243}
244
b70a6642 245void dip_unplug(__u32 dev, double cur_time, __u64 nios_up)
b2822cea
AB
246{
247 struct d_info *dip = __dip_find(dev);
248
00a47cd1
AB
249 if (dip && dip->is_plugged) {
250 dip->nplugs++;
251 dip->plugged_time += (cur_time - dip->last_plug);
252 dip->is_plugged = 0;
b70a6642 253 dip->nios_up += nios_up;
00a47cd1
AB
254 }
255}
b2822cea 256
b70a6642 257void dip_unplug_tm(__u32 dev, __u64 nios_up)
00a47cd1
AB
258{
259 struct d_info *dip = __dip_find(dev);
b2822cea 260
b70a6642 261 if (dip && dip->is_plugged) {
00a47cd1 262 dip->n_timer_unplugs++;
b70a6642
AB
263 dip->nios_upt += nios_up;
264 dip->nplugs_t++;
265 }
b2822cea 266}
52481561
AB
267
268void dip_cleanup(void)
269{
270 struct list_head *p, *q;
271
272 list_for_each_safe(p, q, &all_devs) {
273 struct d_info *dip = list_entry(p, struct d_info, all_head);
274
275 if (dip->n_qs == 0 && dip->n_ds == 0)
276 __dip_exit(dip);
277 }
278}