2 * blktrace output analysis: generate a timeline & gather statistics
4 * Copyright (C) 2006 Alan D. Brunelle <Alan.Brunelle@hp.com>
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.
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.
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
23 static struct file_info *seek_files = NULL;
26 struct rb_node rb_node;
31 /* Seeks per second */
33 double t_start, t_last;
38 FILE *rfp, *wfp, *cfp, *sps_fp;
43 long long last_start, last_end;
46 static FILE *seek_open(char *str, char rw)
51 if (seek_name == NULL) return NULL;
53 oname = malloc(strlen(seek_name) + strlen(str) + 32);
54 sprintf(oname, "%s_%s_%c.dat", seek_name, str, rw);
55 if ((fp = fopen(oname, "w")) == NULL)
58 add_file(&seek_files, fp, oname);
63 static void __insert(struct rb_root *root, long long sectors)
66 struct rb_node *parent = NULL;
67 struct rb_node **p = &root->rb_node;
71 sbp = rb_entry(parent, struct seek_bkt, rb_node);
72 if (sectors < sbp->sectors)
74 else if (sectors > sbp->sectors)
82 sbp = malloc(sizeof(struct seek_bkt));
84 sbp->sectors = sectors;
86 rb_link_node(&sbp->rb_node, parent, p);
87 rb_insert_color(&sbp->rb_node, root);
90 static void __destroy(struct rb_node *n)
93 struct seek_bkt *sbp = rb_entry(n, struct seek_bkt, rb_node);
95 __destroy(n->rb_left);
96 __destroy(n->rb_right);
101 static void sps_emit(struct seeki *sip)
103 double tstamp, s_p_s;
104 struct sps_bkt *sps = &sip->sps;
106 if (sps->nseeks == 1) {
108 tstamp = sps->t_start;
111 double delta = sps->t_last - sps->t_start;
113 s_p_s = (double)(sps->nseeks) / delta;
114 tstamp = sps->t_start + (delta / 2);
117 fprintf(sip->sps_fp, "%15.9lf %.2lf\n", sps->t_start, s_p_s);
123 static void sps_add(struct seeki *sip, double t)
126 struct sps_bkt *sps = &sip->sps;
128 if (sps->nseeks != 0 && ((t - sps->t_start) >= 1.0))
132 if (sps->nseeks == 0) {
141 void seek_clean(void)
143 clean_files(&seek_files);
146 long long seek_dist(struct seeki *sip, struct io *iop)
149 long long start = BIT_START(iop), end = BIT_END(iop);
152 dist = start - sip->last_end;
154 /* Some overlap means no seek */
155 if (((sip->last_start <= start) && (start <= sip->last_end)) ||
156 ((sip->last_start <= end) && (end <= sip->last_end)))
158 else if (start > sip->last_end)
159 dist = start - sip->last_end;
161 dist = start - sip->last_start;
165 sip->last_start = start;
170 void *seeki_init(char *str)
172 struct seeki *sip = malloc(sizeof(struct seeki));
174 sip->rfp = seek_open(str, 'r');
175 sip->wfp = seek_open(str, 'w');
176 sip->cfp = seek_open(str, 'c');
178 sip->total_sectors = 0.0;
179 sip->last_start = sip->last_end = 0;
180 memset(&sip->root, 0, sizeof(sip->root));
185 memset(&sip->sps, 0, sizeof(sip->sps));
187 oname = malloc(strlen(sps_name) + strlen(str) + 32);
188 sprintf(oname, "%s_%s.dat", sps_name, str);
189 if ((sip->sps_fp = fopen(oname, "w")) == NULL)
192 add_file(&seek_files, sip->sps_fp, oname);
200 void seeki_exit(void *param)
202 struct seeki *sip = param;
204 if (sip->sps_fp && sip->sps.nseeks != 0)
208 * Associated files are cleaned up by seek_clean
210 __destroy(sip->root.rb_node);
214 void seeki_add(void *handle, struct io *iop)
216 struct seeki *sip = handle;
217 char rw = IOP_READ(iop) ? 'r' : 'w';
218 long long dist = seek_dist(sip, iop);
219 double tstamp = BIT_TIME(iop->t.time);
220 FILE *fp = IOP_READ(iop) ? sip->rfp : sip->wfp;
223 fprintf(fp, "%15.9lf %13lld %c\n", tstamp, dist, rw);
225 fprintf(sip->cfp, "%15.9lf %13lld %c\n", tstamp, dist, rw);
229 sip->total_sectors += dist;
230 __insert(&sip->root, dist);
232 sps_add(sip, tstamp);
235 long long seeki_nseeks(void *handle)
237 return ((struct seeki *)handle)->tot_seeks;
240 double seeki_mean(void *handle)
242 struct seeki *sip = handle;
243 return sip->total_sectors / sip->tot_seeks;
246 int __median(struct rb_node *n, long long sofar, long long target, long
249 struct seek_bkt *sbp;
251 sbp = rb_entry(n, struct seek_bkt, rb_node);
252 if ((sofar + sbp->nseeks) >= target) {
257 if (n->rb_left && __median(n->rb_left, sofar, target, rvp))
260 if (n->rb_right && __median(n->rb_right, sofar, target, rvp))
267 long long seeki_median(void *handle)
269 long long rval = 0LL;
270 struct seeki *sip = handle;
272 if (sip->root.rb_node)
273 (void)__median(sip->root.rb_node, 0LL, sip->tot_seeks / 2,
279 void __mode(struct rb_node *n, struct mode *mp)
281 struct seek_bkt *sbp;
283 if (n->rb_left) __mode(n->rb_left, mp);
284 if (n->rb_right) __mode(n->rb_right, mp);
286 sbp = rb_entry(n, struct seek_bkt, rb_node);
287 if (mp->modes == NULL) {
288 mp->modes = malloc(sizeof(long long));
291 else if (sbp->nseeks > mp->most_seeks)
293 else if (sbp->nseeks == mp->most_seeks)
294 mp->modes = realloc(mp->modes, (mp->nmds + 1) *
299 mp->most_seeks = sbp->nseeks;
300 mp->modes[mp->nmds++] = sbp->sectors;
303 int seeki_mode(void *handle, struct mode *mp)
305 struct seeki *sip = handle;
306 struct rb_root *root = &sip->root;
308 memset(mp, 0, sizeof(struct mode));
310 __mode(root->rb_node, mp);