Add in unplug IO count histogram
[blktrace.git] / btt / devs.c
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>
22 #include "globals.h"
23
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];
27
28 static 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
34 static 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
45 static 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
55 #if defined(DEBUG)
56 void __dump_rb_node(struct rb_node *n)
57 {
58         struct io *iop = rb_entry(n, struct io, rb_node);
59
60         dbg_ping();
61         if (iop->type == IOP_A)
62                 __dump_iop2(stdout, iop, bilink_first_down(iop, NULL));
63         else
64                 __dump_iop(stdout, iop, 0);
65         if (n->rb_left)
66                 __dump_rb_node(n->rb_left);
67         if (n->rb_right)
68                 __dump_rb_node(n->rb_right);
69 }
70
71 void __dump_rb_tree(struct d_info *dip, enum iop_type type)
72 {
73         struct rb_root *roots = dip->heads;
74         struct rb_root *root = &roots[type];
75         struct rb_node *n = root->rb_node;
76
77         if (n) {
78                 printf("\tIOP_%c\n", type2c(type));
79                 __dump_rb_node(n);
80         }
81 }
82
83 void dump_rb_trees(void)
84 {
85         int i;
86         enum iop_type type;
87         struct d_info *dip;
88         struct list_head *p;
89
90         for (i = 0; i < N_DEV_HASH; i++) {
91                 __list_for_each(p, &dev_heads[i]) {
92                         dip = list_entry(p, struct d_info, hash_head);
93                         printf("Trees for %3d,%-3d\n", MAJOR(dip->device),
94                                MINOR(dip->device));
95                         for (type = IOP_Q; type < N_IOP_TYPES; type++) {
96                                 if (type != IOP_L)
97                                         __dump_rb_tree(dip, type);
98                         }
99                 }
100         }
101 }
102 #endif
103
104 void init_dev_heads(void)
105 {
106         int i;
107         for (i = 0; i < N_DEV_HASH; i++)
108                 INIT_LIST_HEAD(&dev_heads[i]);
109 }
110
111 struct d_info *__dip_find(__u32 device)
112 {
113         struct d_info *dip;
114         struct list_head *p;
115
116         __list_for_each(p, &dev_heads[DEV_HASH(device)]) {
117                 dip = list_entry(p, struct d_info, hash_head);
118                 if (device == dip->device)
119                         return dip;
120         }
121
122         return NULL;
123 }
124
125 void dip_exit(void)
126 {
127         struct d_info *dip;
128         struct list_head *p, *q;
129
130         list_for_each_safe(p, q, &all_devs) {
131                 dip = list_entry(p, struct d_info, all_head);
132
133                 __destroy_heads(dip->heads);
134                 region_exit(&dip->regions);
135                 seeki_exit(dip->seek_handle);
136                 bno_dump_exit(dip->bno_dump_handle);
137                 unplug_hist_exit(dip->unplug_hist_handle);
138                 free(dip);
139         }
140 }
141
142 struct d_info *dip_add(__u32 device, struct io *iop)
143 {
144         struct d_info *dip = __dip_find(device);
145
146         if (dip == NULL) {
147                 dip = malloc(sizeof(struct d_info));
148                 memset(dip, 0, sizeof(*dip));
149                 dip->heads = dip_rb_mkhds();
150                 region_init(&dip->regions);
151                 dip->device = device;
152                 dip->last_q = (__u64)-1;
153                 dip->map = dev_map_find(device);
154                 dip->seek_handle = seeki_init(device);
155                 dip->bno_dump_handle = bno_dump_init(device);
156                 dip->unplug_hist_handle = unplug_hist_init(device);
157                 latency_init(dip);
158                 list_add_tail(&dip->hash_head, &dev_heads[DEV_HASH(device)]);
159                 list_add_tail(&dip->all_head, &all_devs);
160                 dip->start_time = BIT_TIME(iop->t.time);
161                 dip->pre_culling = 1;
162                 n_devs++;
163         }
164
165         if (dip->pre_culling) {
166                 if (iop->type == IOP_Q || iop->type == IOP_A)
167                         dip->pre_culling = 0;
168                 else
169                         return NULL;
170         }
171
172         iop->linked = dip_rb_ins(dip, iop);
173         dip->end_time = BIT_TIME(iop->t.time);
174
175 #       if defined(DEBUG)
176                 if (iop->linked) 
177                         rb_tree_size++;
178 #       endif
179
180         return dip;
181 }
182
183 void dip_rem(struct io *iop)
184 {
185         if (iop->linked) {
186                 dip_rb_rem(iop);
187                 iop->linked = 0;
188         }
189 }
190
191 void dip_foreach(struct io *iop, enum iop_type type, 
192                  void (*fnc)(struct io *iop, struct io *this), int rm_after)
193 {
194         if (rm_after) {
195                 LIST_HEAD(head);
196                 struct io *this;
197                 struct list_head *p, *q;
198
199                 dip_rb_fe(iop->dip, type, iop, fnc, &head);
200                 list_for_each_safe(p, q, &head) {
201                         this = list_entry(p, struct io, f_head);
202                         LIST_DEL(&this->f_head);
203                         io_release(this);
204                 }
205         }
206         else
207                 dip_rb_fe(iop->dip, type, iop, fnc, NULL);
208 }
209
210 void dip_foreach_list(struct io *iop, enum iop_type type, struct list_head *hd)
211 {
212         dip_rb_fe(iop->dip, type, iop, NULL, hd);
213 }
214
215 struct io *dip_find_sec(struct d_info *dip, enum iop_type type, __u64 sec)
216 {
217         return dip_rb_find_sec(dip, type, sec);
218 }
219
220 void dip_foreach_out(void (*func)(struct d_info *, void *), void *arg)
221 {
222         if (devices == NULL) {
223                 struct list_head *p;
224                 __list_for_each(p, &all_devs)
225                         func(list_entry(p, struct d_info, all_head), arg);
226         }
227         else {
228                 int i;
229                 struct d_info *dip;
230                 unsigned int mjr, mnr;
231                 char *p = devices;
232
233                 while (p && ((i = sscanf(p, "%u,%u", &mjr, &mnr)) == 2)) {
234                         dip = __dip_find((__u32)((mjr << MINORBITS) | mnr));
235                         ASSERT(dip);
236
237                         func(dip, arg);
238
239                         p = strchr(p, ';');
240                         if (p) p++;
241                 }
242         }
243 }
244
245 void dip_plug(__u32 dev, double cur_time)
246 {
247         struct d_info *dip = __dip_find(dev);
248
249         if (!dip || dip->is_plugged) return;
250
251         dip->is_plugged = 1;
252         dip->last_plug = cur_time;
253 }
254
255 void dip_unplug(__u32 dev, double cur_time, int is_timer)
256 {
257         struct d_info *dip = __dip_find(dev);
258
259         if (!dip || !dip->is_plugged) return;
260
261         dip->nplugs++;
262         if (is_timer) dip->n_timer_unplugs++;
263
264         dip->plugged_time += (cur_time - dip->last_plug);
265         dip->is_plugged = 0;
266 }