Merge branch 'fix-m' into add-P
[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
69040794
AB
76void dip_exit(void)
77{
78 struct d_info *dip;
79 struct list_head *p, *q;
80
81 list_for_each_safe(p, q, &all_devs) {
82 dip = list_entry(p, struct d_info, all_head);
83
84 __destroy_heads(dip->heads);
85 region_exit(&dip->regions);
86 seeki_exit(dip->seek_handle);
4c48f14e 87 seeki_exit(dip->q2q_handle);
2baef508 88 aqd_exit(dip->aqd_handle);
e47ada10 89 plat_exit(dip->q2d_plat_handle);
2baef508
AB
90 plat_exit(dip->q2c_plat_handle);
91 plat_exit(dip->d2c_plat_handle);
69040794 92 bno_dump_exit(dip->bno_dump_handle);
fc16a815 93 unplug_hist_exit(dip->unplug_hist_handle);
951ea56c
AB
94 if (output_all_data)
95 q2d_release(dip->q2d_priv);
69040794
AB
96 free(dip);
97 }
98}
99
4c48f14e
AB
100static inline char *mkhandle(char *str, __u32 device, char *post)
101{
102 int mjr = device >> MINORBITS;
103 int mnr = device & ((1 << MINORBITS) - 1);
104
105 sprintf(str, "%03d,%03d%s", mjr, mnr, post);
106 return str;
107}
108
095181f2 109struct d_info *dip_add(__u32 device, struct io *iop)
63eba147
JA
110{
111 struct d_info *dip = __dip_find(device);
112
113 if (dip == NULL) {
4c48f14e
AB
114 char str[256];
115
6eb42155 116 dip = malloc(sizeof(struct d_info));
2fa934ce 117 memset(dip, 0, sizeof(*dip));
6eb42155 118 dip->heads = dip_rb_mkhds();
69040794 119 region_init(&dip->regions);
63eba147
JA
120 dip->device = device;
121 dip->last_q = (__u64)-1;
63eba147 122 dip->map = dev_map_find(device);
69040794 123 dip->bno_dump_handle = bno_dump_init(device);
fc16a815 124 dip->unplug_hist_handle = unplug_hist_init(device);
4c48f14e
AB
125 dip->seek_handle = seeki_init(mkhandle(str, device, "_d2d"));
126 dip->q2q_handle = seeki_init(mkhandle(str, device, "_q2q"));
4ae2c3c6 127 dip->aqd_handle = aqd_init(mkhandle(str, device, "_aqd"));
e47ada10
AB
128 dip->q2d_plat_handle =
129 plat_init(mkhandle(str, device, "_q2d_plat"));
2baef508
AB
130 dip->q2c_plat_handle =
131 plat_init(mkhandle(str, device, "_q2c_plat"));
132 dip->d2c_plat_handle =
133 plat_init(mkhandle(str, device, "_d2c_plat"));
b2ecdd0f 134 latency_init(dip);
63eba147 135 list_add_tail(&dip->hash_head, &dev_heads[DEV_HASH(device)]);
6eb42155 136 list_add_tail(&dip->all_head, &all_devs);
b2822cea 137 dip->start_time = BIT_TIME(iop->t.time);
11997716 138 dip->pre_culling = 1;
951ea56c
AB
139 if (output_all_data)
140 dip->q2d_priv = q2d_init();
63eba147
JA
141 n_devs++;
142 }
143
11997716
AB
144 if (dip->pre_culling) {
145 if (iop->type == IOP_Q || iop->type == IOP_A)
146 dip->pre_culling = 0;
147 else
148 return NULL;
149 }
150
095181f2 151 iop->linked = dip_rb_ins(dip, iop);
b2822cea 152 dip->end_time = BIT_TIME(iop->t.time);
c8b0b334 153
63eba147
JA
154 return dip;
155}
6eb42155
ADB
156
157void dip_rem(struct io *iop)
158{
d76c5b81
AB
159 if (iop->linked) {
160 dip_rb_rem(iop);
161 iop->linked = 0;
162 }
6eb42155
ADB
163}
164
8b10aae0 165void dip_foreach(struct io *iop, enum iop_type type,
6eb42155
ADB
166 void (*fnc)(struct io *iop, struct io *this), int rm_after)
167{
168 if (rm_after) {
169 LIST_HEAD(head);
170 struct io *this;
171 struct list_head *p, *q;
172
173 dip_rb_fe(iop->dip, type, iop, fnc, &head);
174 list_for_each_safe(p, q, &head) {
175 this = list_entry(p, struct io, f_head);
8b10aae0 176 list_del(&this->f_head);
6eb42155
ADB
177 io_release(this);
178 }
179 }
180 else
181 dip_rb_fe(iop->dip, type, iop, fnc, NULL);
182}
183
095181f2
JA
184void dip_foreach_list(struct io *iop, enum iop_type type, struct list_head *hd)
185{
186 dip_rb_fe(iop->dip, type, iop, NULL, hd);
187}
188
6eb42155
ADB
189struct io *dip_find_sec(struct d_info *dip, enum iop_type type, __u64 sec)
190{
191 return dip_rb_find_sec(dip, type, sec);
192}
193
194void dip_foreach_out(void (*func)(struct d_info *, void *), void *arg)
195{
196 if (devices == NULL) {
197 struct list_head *p;
198 __list_for_each(p, &all_devs)
199 func(list_entry(p, struct d_info, all_head), arg);
200 }
201 else {
202 int i;
203 struct d_info *dip;
204 unsigned int mjr, mnr;
205 char *p = devices;
206
207 while (p && ((i = sscanf(p, "%u,%u", &mjr, &mnr)) == 2)) {
208 dip = __dip_find((__u32)((mjr << MINORBITS) | mnr));
6eb42155 209 func(dip, arg);
6eb42155
ADB
210 p = strchr(p, ';');
211 if (p) p++;
212 }
213 }
214}
b2822cea
AB
215
216void dip_plug(__u32 dev, double cur_time)
217{
218 struct d_info *dip = __dip_find(dev);
219
220 if (!dip || dip->is_plugged) return;
221
222 dip->is_plugged = 1;
223 dip->last_plug = cur_time;
224}
225
b70a6642 226void dip_unplug(__u32 dev, double cur_time, __u64 nios_up)
b2822cea
AB
227{
228 struct d_info *dip = __dip_find(dev);
229
00a47cd1
AB
230 if (dip && dip->is_plugged) {
231 dip->nplugs++;
232 dip->plugged_time += (cur_time - dip->last_plug);
233 dip->is_plugged = 0;
b70a6642 234 dip->nios_up += nios_up;
00a47cd1
AB
235 }
236}
b2822cea 237
b70a6642 238void dip_unplug_tm(__u32 dev, __u64 nios_up)
00a47cd1
AB
239{
240 struct d_info *dip = __dip_find(dev);
b2822cea 241
b70a6642 242 if (dip && dip->is_plugged) {
00a47cd1 243 dip->n_timer_unplugs++;
b70a6642
AB
244 dip->nios_upt += nios_up;
245 dip->nplugs_t++;
246 }
b2822cea 247}