[PATCH] BTT patch: (2/3) per-IO stream output
[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 void init_dev_heads(void)
29 {
30         int i;
31         for (i = 0; i < N_DEV_HASH; i++)
32                 INIT_LIST_HEAD(&dev_heads[i]);
33 }
34
35 struct d_info *__dip_find(__u32 device)
36 {
37         struct list_head *p;
38         struct d_info *dip;
39
40         __list_for_each(p, &dev_heads[DEV_HASH(device)]) {
41                 dip = list_entry(p, struct d_info, hash_head);
42                 if (device == dip->device)
43                         return dip;
44         }
45
46         return NULL;
47 }
48
49 struct d_info *dip_add(__u32 device, struct io *iop)
50 {
51         struct d_info *dip = __dip_find(device);
52
53         if (dip == NULL) {
54                 dip = malloc(sizeof(struct d_info));
55                 dip->heads = dip_rb_mkhds();
56                 init_region(&dip->regions);
57                 dip->device = device;
58                 dip->last_q = (__u64)-1;
59                 dip->map = dev_map_find(device);
60                 dip->seek_handle = seeki_init(device);
61                 latency_init(dip);
62                 memset(&dip->stats, 0, sizeof(dip->stats));
63                 memset(&dip->all_stats, 0, sizeof(dip->all_stats));
64                 list_add_tail(&dip->hash_head, &dev_heads[DEV_HASH(device)]);
65                 list_add_tail(&dip->all_head, &all_devs);
66                 n_devs++;
67         }
68
69         iop->linked = dip_rb_ins(dip, iop);
70         return dip;
71 }
72
73 void dip_rem(struct io *iop)
74 {
75         dip_rb_rem(iop);
76 }
77
78 void dip_foreach(struct io *iop, enum iop_type type, 
79                  void (*fnc)(struct io *iop, struct io *this), int rm_after)
80 {
81         if (rm_after) {
82                 LIST_HEAD(head);
83                 struct io *this;
84                 struct list_head *p, *q;
85
86                 dip_rb_fe(iop->dip, type, iop, fnc, &head);
87                 list_for_each_safe(p, q, &head) {
88                         this = list_entry(p, struct io, f_head);
89                         LIST_DEL(&this->f_head);
90                         io_release(this);
91                 }
92         }
93         else
94                 dip_rb_fe(iop->dip, type, iop, fnc, NULL);
95 }
96
97 void dip_foreach_list(struct io *iop, enum iop_type type, struct list_head *hd)
98 {
99         dip_rb_fe(iop->dip, type, iop, NULL, hd);
100 }
101
102 struct io *dip_find_sec(struct d_info *dip, enum iop_type type, __u64 sec)
103 {
104         return dip_rb_find_sec(dip, type, sec);
105 }
106
107 void dip_foreach_out(void (*func)(struct d_info *, void *), void *arg)
108 {
109         if (devices == NULL) {
110                 struct list_head *p;
111                 __list_for_each(p, &all_devs)
112                         func(list_entry(p, struct d_info, all_head), arg);
113         }
114         else {
115                 int i;
116                 struct d_info *dip;
117                 unsigned int mjr, mnr;
118                 char *p = devices;
119
120                 while (p && ((i = sscanf(p, "%u,%u", &mjr, &mnr)) == 2)) {
121                         dip = __dip_find((__u32)((mjr << MINORBITS) | mnr));
122                         ASSERT(dip);
123
124                         func(dip, arg);
125
126                         p = strchr(p, ';');
127                         if (p) p++;
128                 }
129         }
130 }