Fixed plug/unplug logic in btt
[blktrace.git] / btt / seek.c
CommitLineData
5225e788
AB
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 */
5bebfd16 21#include <float.h>
5225e788
AB
22#include "globals.h"
23
24struct seek_bkt {
6eb42155 25 struct rb_node rb_node;
5225e788
AB
26 long long sectors;
27 int nseeks;
28};
29
ccf6d55e
AB
30/* Seeks per second */
31struct sps_bkt {
32 double t_start, t_last;
33 unsigned long nseeks;
34};
35
5225e788 36struct seeki {
ccf6d55e 37 FILE *rfp, *wfp, *cfp, *sps_fp;
6eb42155 38 struct rb_root root;
ccf6d55e 39 struct sps_bkt sps;
6eb42155 40 long long tot_seeks;
5225e788
AB
41 double total_sectors;
42 long long last_start, last_end;
43};
44
4c48f14e 45static FILE *seek_open(char *str, char rw)
5225e788
AB
46{
47 FILE *fp;
48 char *oname;
5225e788
AB
49
50 if (seek_name == NULL) return NULL;
51
4c48f14e
AB
52 oname = malloc(strlen(seek_name) + strlen(str) + 32);
53 sprintf(oname, "%s_%s_%c.dat", seek_name, str, rw);
99bb5ebc 54 if ((fp = my_fopen(oname, "w")) == NULL)
5225e788 55 perror(oname);
b2ecdd0f 56 else
c053af42 57 add_file(fp, oname);
5225e788
AB
58
59 return fp;
60}
61
6eb42155
ADB
62static void __insert(struct rb_root *root, long long sectors)
63{
64 struct seek_bkt *sbp;
65 struct rb_node *parent = NULL;
66 struct rb_node **p = &root->rb_node;
67
68 while (*p) {
69 parent = *p;
70 sbp = rb_entry(parent, struct seek_bkt, rb_node);
71 if (sectors < sbp->sectors)
72 p = &(*p)->rb_left;
73 else if (sectors > sbp->sectors)
74 p = &(*p)->rb_right;
75 else {
76 sbp->nseeks++;
77 return;
78 }
79 }
80
81 sbp = malloc(sizeof(struct seek_bkt));
82 sbp->nseeks = 1;
83 sbp->sectors = sectors;
84
85 rb_link_node(&sbp->rb_node, parent, p);
86 rb_insert_color(&sbp->rb_node, root);
87}
88
69040794
AB
89static void __destroy(struct rb_node *n)
90{
91 if (n) {
92 struct seek_bkt *sbp = rb_entry(n, struct seek_bkt, rb_node);
93
94 __destroy(n->rb_left);
95 __destroy(n->rb_right);
96 free(sbp);
97 }
98}
99
ccf6d55e
AB
100static void sps_emit(struct seeki *sip)
101{
102 double tstamp, s_p_s;
103 struct sps_bkt *sps = &sip->sps;
5bebfd16 104 double delta = sps->t_last - sps->t_start;
ccf6d55e 105
5bebfd16
AB
106 if ((sps->nseeks == 1) || (delta < DBL_EPSILON)) {
107 s_p_s = (double)(sps->nseeks);
ccf6d55e 108 tstamp = sps->t_start;
c053af42 109 } else {
ccf6d55e
AB
110
111 s_p_s = (double)(sps->nseeks) / delta;
112 tstamp = sps->t_start + (delta / 2);
113 }
114
115 fprintf(sip->sps_fp, "%15.9lf %.2lf\n", sps->t_start, s_p_s);
116
117 sps->t_start = 0;
118 sps->nseeks = 0;
119}
120
121static void sps_add(struct seeki *sip, double t)
122{
123 if (sip->sps_fp) {
124 struct sps_bkt *sps = &sip->sps;
125
126 if (sps->nseeks != 0 && ((t - sps->t_start) >= 1.0))
127 sps_emit(sip);
128
129 sps->t_last = t;
130 if (sps->nseeks == 0) {
131 sps->t_start = t;
132 sps->nseeks = 1;
c053af42 133 } else
ccf6d55e
AB
134 sps->nseeks++;
135 }
136}
137
c053af42
AB
138static int __median(struct rb_node *n, long long sofar, long long target,
139 long long *rvp)
b2ecdd0f 140{
c053af42
AB
141 struct seek_bkt *sbp;
142
143 sbp = rb_entry(n, struct seek_bkt, rb_node);
144 if ((sofar + sbp->nseeks) >= target) {
145 *rvp = sbp->sectors;
146 return 1;
147 }
148
149 if (n->rb_left && __median(n->rb_left, sofar, target, rvp))
150 return 1;
151
152 if (n->rb_right && __median(n->rb_right, sofar, target, rvp))
153 return 1;
154
155 return 0;
156
157}
158
159static void __mode(struct rb_node *n, struct mode *mp)
160{
161 struct seek_bkt *sbp;
162
163 if (n->rb_left)
164 __mode(n->rb_left, mp);
165 if (n->rb_right)
166 __mode(n->rb_right, mp);
167
168 sbp = rb_entry(n, struct seek_bkt, rb_node);
169 if (mp->modes == NULL) {
170 mp->modes = malloc(sizeof(long long));
171 mp->nmds = 0;
172 } else if (sbp->nseeks > mp->most_seeks)
173 mp->nmds = 0;
174 else if (sbp->nseeks == mp->most_seeks)
175 mp->modes = realloc(mp->modes, (mp->nmds + 1) *
176 sizeof(long long));
177 else
178 return;
179
180 mp->most_seeks = sbp->nseeks;
181 mp->modes[mp->nmds++] = sbp->sectors;
b2ecdd0f
ADB
182}
183
5225e788
AB
184long long seek_dist(struct seeki *sip, struct io *iop)
185{
186 long long dist;
187 long long start = BIT_START(iop), end = BIT_END(iop);
188
69040794 189 if (seek_absolute)
5225e788 190 dist = start - sip->last_end;
69040794
AB
191 else {
192 /* Some overlap means no seek */
193 if (((sip->last_start <= start) && (start <= sip->last_end)) ||
194 ((sip->last_start <= end) && (end <= sip->last_end)))
195 dist = 0;
196 else if (start > sip->last_end)
197 dist = start - sip->last_end;
198 else
199 dist = start - sip->last_start;
200
201 }
5225e788
AB
202
203 sip->last_start = start;
204 sip->last_end = end;
205 return dist;
206}
207
c053af42 208void *seeki_alloc(char *str)
5225e788
AB
209{
210 struct seeki *sip = malloc(sizeof(struct seeki));
211
4c48f14e
AB
212 sip->rfp = seek_open(str, 'r');
213 sip->wfp = seek_open(str, 'w');
214 sip->cfp = seek_open(str, 'c');
6eb42155 215 sip->tot_seeks = 0;
5225e788
AB
216 sip->total_sectors = 0.0;
217 sip->last_start = sip->last_end = 0;
6eb42155 218 memset(&sip->root, 0, sizeof(sip->root));
5225e788 219
ccf6d55e
AB
220 if (sps_name) {
221 char *oname;
222
223 memset(&sip->sps, 0, sizeof(sip->sps));
224
225 oname = malloc(strlen(sps_name) + strlen(str) + 32);
226 sprintf(oname, "%s_%s.dat", sps_name, str);
99bb5ebc 227 if ((sip->sps_fp = my_fopen(oname, "w")) == NULL)
ccf6d55e
AB
228 perror(oname);
229 else
c053af42
AB
230 add_file(sip->sps_fp, oname);
231 } else
ccf6d55e
AB
232 sip->sps_fp = NULL;
233
5225e788
AB
234 return sip;
235}
236
c053af42 237void seeki_free(void *param)
69040794
AB
238{
239 struct seeki *sip = param;
240
ccf6d55e
AB
241 if (sip->sps_fp && sip->sps.nseeks != 0)
242 sps_emit(sip);
243
69040794
AB
244 /*
245 * Associated files are cleaned up by seek_clean
246 */
247 __destroy(sip->root.rb_node);
248 free(sip);
249}
250
5225e788
AB
251void seeki_add(void *handle, struct io *iop)
252{
5225e788 253 struct seeki *sip = handle;
69040794 254 char rw = IOP_READ(iop) ? 'r' : 'w';
5225e788 255 long long dist = seek_dist(sip, iop);
69040794 256 double tstamp = BIT_TIME(iop->t.time);
5225e788
AB
257 FILE *fp = IOP_READ(iop) ? sip->rfp : sip->wfp;
258
259 if (fp)
69040794
AB
260 fprintf(fp, "%15.9lf %13lld %c\n", tstamp, dist, rw);
261 if (sip->cfp)
262 fprintf(sip->cfp, "%15.9lf %13lld %c\n", tstamp, dist, rw);
5225e788
AB
263
264 dist = llabs(dist);
6eb42155 265 sip->tot_seeks++;
5225e788 266 sip->total_sectors += dist;
6eb42155 267 __insert(&sip->root, dist);
ccf6d55e
AB
268
269 sps_add(sip, tstamp);
5225e788
AB
270}
271
272long long seeki_nseeks(void *handle)
273{
6eb42155 274 return ((struct seeki *)handle)->tot_seeks;
5225e788
AB
275}
276
277double seeki_mean(void *handle)
278{
279 struct seeki *sip = handle;
6eb42155
ADB
280 return sip->total_sectors / sip->tot_seeks;
281}
282
5225e788
AB
283long long seeki_median(void *handle)
284{
6eb42155 285 long long rval = 0LL;
5225e788 286 struct seeki *sip = handle;
5225e788 287
6eb42155 288 if (sip->root.rb_node)
8b10aae0 289 (void)__median(sip->root.rb_node, 0LL, sip->tot_seeks / 2,
6eb42155 290 &rval);
5225e788 291
6eb42155 292 return rval;
5225e788
AB
293}
294
6eb42155 295int seeki_mode(void *handle, struct mode *mp)
5225e788 296{
5225e788 297 struct seeki *sip = handle;
6eb42155
ADB
298 struct rb_root *root = &sip->root;
299
300 memset(mp, 0, sizeof(struct mode));
8b10aae0 301 if (root->rb_node)
6eb42155 302 __mode(root->rb_node, mp);
5225e788 303
6eb42155 304 return mp->nmds;
5225e788 305}