2 * blktrace output analysis: generate a timeline & gather statistics
4 * (C) Copyright 2009 Hewlett-Packard Development Company, L.P.
5 * Alan D. Brunelle (alan.brunelle@hp.com)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 struct rb_node rb_node;
26 struct list_head head;
31 struct p_live_info *plip;
40 static struct rb_root p_live_root;
41 static LIST_HEAD(all_p_lives);
43 static FILE *do_open(struct d_info *dip)
48 char *bn = dip ? dip->dip_name : "sys";
49 char *nm = malloc(strlen(bn) + 16);
51 sprintf(nm, "%s_live.dat", bn);
52 ofp = my_fopen(nm, "w");
64 static inline int inside(struct p_live *plp, __u64 dt, __u64 ct)
66 if (plp->dt <= dt && dt <= plp->ct)
68 if (plp->dt <= ct && ct <= plp->ct)
70 if (dt < plp->dt && plp->ct < ct)
75 static void __p_live_add(struct rb_root *root, __u64 dt, __u64 ct)
78 struct rb_node *parent = NULL;
79 struct rb_node **p = &root->rb_node;
83 plp = rb_entry(parent, struct p_live, rb_node);
85 if (inside(plp, dt, ct)) {
87 rb_erase(&plp->rb_node, root);
88 __p_live_add(root, min(plp->dt, dt), max(plp->ct, ct));
99 plp = malloc(sizeof(*plp));
103 rb_link_node(&plp->rb_node, parent, p);
104 rb_insert_color(&plp->rb_node, root);
105 list_add_tail(&plp->head, &all_p_lives);
108 void *p_live_alloc(void)
110 size_t sz = sizeof(struct rb_root);
111 return memset(malloc(sz), 0, sz);
114 void p_live_free(void *p)
119 void p_live_add(struct d_info *dip, __u64 dt, __u64 ct)
121 __p_live_add(dip->p_live_handle, dt, ct);
122 __p_live_add(&p_live_root, dt, ct);
125 static void p_live_visit(struct rb_node *n, struct get_info *gip)
127 struct p_live *plp = rb_entry(n, struct p_live, rb_node);
130 p_live_visit(n->rb_left, gip);
133 float y0 = gip->base_y;
134 float y1 = gip->base_y + 0.9;
136 fprintf(gip->ofp, "%.9lf %.1f\n", gip->last_0, y0);
137 fprintf(gip->ofp, "%.9lf %.1f\n", BIT_TIME(plp->dt), y0);
138 fprintf(gip->ofp, "%.9lf %.1f\n", BIT_TIME(plp->dt), y1);
139 fprintf(gip->ofp, "%.9lf %.1f\n", BIT_TIME(plp->ct), y1);
140 fprintf(gip->ofp, "%.9lf %.1f\n", BIT_TIME(plp->ct), y0);
141 gip->last_0 = BIT_TIME(plp->ct);
145 gip->tot_live += (plp->ct - plp->dt);
147 if (gip->t_start < 0.0 || plp->dt < gip->t_start)
148 gip->t_start = plp->dt;
149 if (gip->t_end < 0.0 || plp->ct > gip->t_end)
150 gip->t_end = plp->ct;
153 p_live_visit(n->rb_right, gip);
156 struct p_live_info *p_live_get(struct d_info *dip, int base_y)
158 FILE *ofp = do_open(dip);
159 static struct p_live_info pli;
160 struct p_live_info *plip = &pli;
161 struct get_info gi = {
168 struct rb_root *root = (dip) ? dip->p_live_handle : &p_live_root;
170 memset(plip, 0, sizeof(*plip));
172 p_live_visit(root->rb_node, &gi);
174 if (plip->nlives == 0) {
175 plip->avg_live = plip->avg_lull = plip->p_live = 0.0;
177 else if (plip->nlives == 1) {
178 plip->avg_lull = 0.0;
179 plip->p_live = 100.0;
182 double t_time = BIT_TIME(gi.t_end - gi.t_start);
183 double tot_live = BIT_TIME(gi.tot_live);
185 plip->p_live = 100.0 * (tot_live / t_time);
186 plip->avg_live = tot_live / plip->nlives;
187 plip->avg_lull = (t_time - tot_live) / (plip->nlives - 1);
193 void p_live_exit(void)
195 struct list_head *p, *q;
197 list_for_each_safe(p, q, &all_p_lives) {
198 struct p_live *plp = list_entry(p, struct p_live, head);
200 list_del(&plp->head);