• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * blktrace output analysis: generate a timeline & gather statistics
3  *
4  * (C) Copyright 2009 Hewlett-Packard Development Company, L.P.
5  *	Alan D. Brunelle (alan.brunelle@hp.com)
6  *
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.
11  *
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.
16  *
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
20  *
21  */
22 #include "globals.h"
23 
24 struct p_live {
25 	struct rb_node rb_node;
26 	struct list_head head;
27 	__u64 dt, ct;
28 };
29 
30 struct get_info {
31 	struct p_live_info *plip;
32 	__u64 t_start, t_end;
33 	__u64 tot_live;
34 
35 	FILE *ofp;
36 	double last_0;
37 	int base_y;
38 };
39 
40 static struct rb_root p_live_root;
41 static LIST_HEAD(all_p_lives);
42 
do_open(struct d_info * dip)43 static FILE *do_open(struct d_info *dip)
44 {
45 	FILE *ofp;
46 
47 	if (do_p_live) {
48 		char *bn = dip ? dip->dip_name : "sys";
49 		char *nm = malloc(strlen(bn) + 16);
50 
51 		sprintf(nm, "%s_live.dat", bn);
52 		ofp = my_fopen(nm, "w");
53 		if (ofp)
54 			add_file(ofp, nm);
55 		else
56 			free(nm);
57 	}
58 	else
59 		ofp = NULL;
60 
61 	return ofp;
62 }
63 
inside(struct p_live * plp,__u64 dt,__u64 ct)64 static inline int inside(struct p_live *plp, __u64 dt, __u64 ct)
65 {
66 	if (plp->dt <= dt && dt <= plp->ct)
67 		return 1;
68 	if (plp->dt <= ct && ct <= plp->ct)
69 		return 1;
70 	if (dt < plp->dt && plp->ct < ct)
71 		return 1;
72 	return 0;
73 }
74 
__p_live_add(struct rb_root * root,__u64 dt,__u64 ct)75 static void __p_live_add(struct rb_root *root, __u64 dt, __u64 ct)
76 {
77 	struct p_live *plp;
78 	struct rb_node *parent = NULL;
79 	struct rb_node **p = &root->rb_node;
80 
81 	while (*p) {
82 		parent = *p;
83 		plp = rb_entry(parent, struct p_live, rb_node);
84 
85 		if (inside(plp, dt, ct)) {
86 			list_del(&plp->head);
87 			rb_erase(&plp->rb_node, root);
88 			__p_live_add(root, min(plp->dt, dt), max(plp->ct, ct));
89 			free(plp);
90 			return;
91 		}
92 
93 		if (ct < plp->dt)
94 			p = &(*p)->rb_left;
95 		else
96 			p = &(*p)->rb_right;
97 	}
98 
99 	plp = malloc(sizeof(*plp));
100 	plp->dt = dt;
101 	plp->ct = ct;
102 
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);
106 }
107 
p_live_alloc(void)108 void *p_live_alloc(void)
109 {
110 	size_t sz = sizeof(struct rb_root);
111 	return memset(malloc(sz), 0, sz);
112 }
113 
p_live_free(void * p)114 void p_live_free(void *p)
115 {
116 	free(p);
117 }
118 
p_live_add(struct d_info * dip,__u64 dt,__u64 ct)119 void p_live_add(struct d_info *dip, __u64 dt, __u64 ct)
120 {
121 	__p_live_add(dip->p_live_handle, dt, ct);
122 	__p_live_add(&p_live_root, dt, ct);
123 }
124 
p_live_visit(struct rb_node * n,struct get_info * gip)125 static void p_live_visit(struct rb_node *n, struct get_info *gip)
126 {
127 	struct p_live *plp = rb_entry(n, struct p_live, rb_node);
128 
129 	if (n->rb_left)
130 		p_live_visit(n->rb_left, gip);
131 
132 	if (gip->ofp) {
133 		float y0 = gip->base_y;
134 		float y1 = gip->base_y + 0.9;
135 
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);
142 	}
143 
144 	gip->plip->nlives++;
145 	gip->tot_live += (plp->ct - plp->dt);
146 
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;
151 
152 	if (n->rb_right)
153 		p_live_visit(n->rb_right, gip);
154 }
155 
p_live_get(struct d_info * dip,int base_y)156 struct p_live_info *p_live_get(struct d_info *dip, int base_y)
157 {
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 = {
162 		.plip = plip,
163 		.t_start = 0.0,
164 		.t_end = 0.0,
165 		.ofp = ofp,
166 		.base_y = base_y,
167 	};
168 	struct rb_root *root = (dip) ? dip->p_live_handle : &p_live_root;
169 
170 	memset(plip, 0, sizeof(*plip));
171 	if (root->rb_node)
172 		p_live_visit(root->rb_node, &gi);
173 
174 	if (plip->nlives == 0) {
175 		plip->avg_live = plip->avg_lull = plip->p_live = 0.0;
176 	}
177 	else if (plip->nlives == 1) {
178 		plip->avg_lull = 0.0;
179 		plip->p_live = 100.0;
180 	}
181 	else {
182 		double t_time = BIT_TIME(gi.t_end - gi.t_start);
183 		double tot_live = BIT_TIME(gi.tot_live);
184 
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);
188 	}
189 
190 	return plip;
191 }
192 
p_live_exit(void)193 void p_live_exit(void)
194 {
195 	struct list_head *p, *q;
196 
197 	list_for_each_safe(p, q, &all_p_lives) {
198 		struct p_live *plp = list_entry(p, struct p_live, head);
199 
200 		list_del(&plp->head);
201 		free(plp);
202 	}
203 }
204