diff options
author | Robert Yang <liezhi.yang@windriver.com> | 2012-06-06 13:52:43 +0800 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2012-06-15 15:12:42 +0100 |
commit | 3d78bc19c5b63541fd45dad7580ade55ba512764 (patch) | |
tree | f2d95055e6adfeb1a6b432aa83c28a5fc11f766f /scripts/pybootchartgui/pybootchartgui/draw.py | |
parent | bc19f8bc9c7c52365d3e7cc26f43a08a62a8492b (diff) | |
download | poky-3d78bc19c5b63541fd45dad7580ade55ba512764.tar.gz |
pybootchartgui: add the original code
This is from:
http://pybootchartgui.googlecode.com/files/pybootchartgui-r124.tar.gz
Will modify it to make the build profiling in pictures.
Remove the examples since they would not work any more, and they cost
much disk space.
[YOCTO #2403]
(From OE-Core rev: 1f0791109e1aed715f02945834d6d7fdb9a411b4)
Signed-off-by: Robert Yang <liezhi.yang@windriver.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'scripts/pybootchartgui/pybootchartgui/draw.py')
-rw-r--r-- | scripts/pybootchartgui/pybootchartgui/draw.py | 355 |
1 files changed, 355 insertions, 0 deletions
diff --git a/scripts/pybootchartgui/pybootchartgui/draw.py b/scripts/pybootchartgui/pybootchartgui/draw.py new file mode 100644 index 0000000000..249cd2ef81 --- /dev/null +++ b/scripts/pybootchartgui/pybootchartgui/draw.py | |||
@@ -0,0 +1,355 @@ | |||
1 | import cairo | ||
2 | import math | ||
3 | import re | ||
4 | |||
5 | # Process tree background color. | ||
6 | BACK_COLOR = (1.0, 1.0, 1.0, 1.0) | ||
7 | |||
8 | WHITE = (1.0, 1.0, 1.0, 1.0) | ||
9 | # Process tree border color. | ||
10 | BORDER_COLOR = (0.63, 0.63, 0.63, 1.0) | ||
11 | # Second tick line color. | ||
12 | TICK_COLOR = (0.92, 0.92, 0.92, 1.0) | ||
13 | # 5-second tick line color. | ||
14 | TICK_COLOR_BOLD = (0.86, 0.86, 0.86, 1.0) | ||
15 | # Text color. | ||
16 | TEXT_COLOR = (0.0, 0.0, 0.0, 1.0) | ||
17 | |||
18 | # Font family | ||
19 | FONT_NAME = "Bitstream Vera Sans" | ||
20 | # Title text font. | ||
21 | TITLE_FONT_SIZE = 18 | ||
22 | # Default text font. | ||
23 | TEXT_FONT_SIZE = 12 | ||
24 | # Axis label font. | ||
25 | AXIS_FONT_SIZE = 11 | ||
26 | # Legend font. | ||
27 | LEGEND_FONT_SIZE = 12 | ||
28 | |||
29 | # CPU load chart color. | ||
30 | CPU_COLOR = (0.40, 0.55, 0.70, 1.0) | ||
31 | # IO wait chart color. | ||
32 | IO_COLOR = (0.76, 0.48, 0.48, 0.5) | ||
33 | # Disk throughput color. | ||
34 | DISK_TPUT_COLOR = (0.20, 0.71, 0.20, 1.0) | ||
35 | # CPU load chart color. | ||
36 | FILE_OPEN_COLOR = (0.20, 0.71, 0.71, 1.0) | ||
37 | |||
38 | # Process border color. | ||
39 | PROC_BORDER_COLOR = (0.71, 0.71, 0.71, 1.0) | ||
40 | # Waiting process color. | ||
41 | PROC_COLOR_D = (0.76, 0.48, 0.48, 0.125) | ||
42 | # Running process color. | ||
43 | PROC_COLOR_R = CPU_COLOR | ||
44 | # Sleeping process color. | ||
45 | PROC_COLOR_S = (0.94, 0.94, 0.94, 1.0) | ||
46 | # Stopped process color. | ||
47 | PROC_COLOR_T = (0.94, 0.50, 0.50, 1.0) | ||
48 | # Zombie process color. | ||
49 | PROC_COLOR_Z = (0.71, 0.71, 0.71, 1.0) | ||
50 | # Dead process color. | ||
51 | PROC_COLOR_X = (0.71, 0.71, 0.71, 0.125) | ||
52 | # Paging process color. | ||
53 | PROC_COLOR_W = (0.71, 0.71, 0.71, 0.125) | ||
54 | |||
55 | # Process label color. | ||
56 | PROC_TEXT_COLOR = (0.19, 0.19, 0.19, 1.0) | ||
57 | # Process label font. | ||
58 | PROC_TEXT_FONT_SIZE = 12 | ||
59 | |||
60 | # Signature color. | ||
61 | SIG_COLOR = (0.0, 0.0, 0.0, 0.3125) | ||
62 | # Signature font. | ||
63 | SIG_FONT_SIZE = 14 | ||
64 | # Signature text. | ||
65 | SIGNATURE = "http://code.google.com/p/pybootchartgui" | ||
66 | |||
67 | # Process dependency line color. | ||
68 | DEP_COLOR = (0.75, 0.75, 0.75, 1.0) | ||
69 | # Process dependency line stroke. | ||
70 | DEP_STROKE = 1.0 | ||
71 | |||
72 | # Process description date format. | ||
73 | DESC_TIME_FORMAT = "mm:ss.SSS" | ||
74 | |||
75 | # Process states | ||
76 | STATE_UNDEFINED = 0 | ||
77 | STATE_RUNNING = 1 | ||
78 | STATE_SLEEPING = 2 | ||
79 | STATE_WAITING = 3 | ||
80 | STATE_STOPPED = 4 | ||
81 | STATE_ZOMBIE = 5 | ||
82 | |||
83 | STATE_COLORS = [(0,0,0,0), PROC_COLOR_R, PROC_COLOR_S, PROC_COLOR_D, PROC_COLOR_T, PROC_COLOR_Z, PROC_COLOR_X, PROC_COLOR_W] | ||
84 | |||
85 | # Convert ps process state to an int | ||
86 | def get_proc_state(flag): | ||
87 | return "RSDTZXW".index(flag) + 1 | ||
88 | |||
89 | |||
90 | def draw_text(ctx, text, color, x, y): | ||
91 | ctx.set_source_rgba(*color) | ||
92 | ctx.move_to(x, y) | ||
93 | ctx.show_text(text) | ||
94 | |||
95 | |||
96 | def draw_fill_rect(ctx, color, rect): | ||
97 | ctx.set_source_rgba(*color) | ||
98 | ctx.rectangle(*rect) | ||
99 | ctx.fill() | ||
100 | |||
101 | |||
102 | def draw_rect(ctx, color, rect): | ||
103 | ctx.set_source_rgba(*color) | ||
104 | ctx.rectangle(*rect) | ||
105 | ctx.stroke() | ||
106 | |||
107 | |||
108 | def draw_legend_box(ctx, label, fill_color, x, y, s): | ||
109 | draw_fill_rect(ctx, fill_color, (x, y - s, s, s)) | ||
110 | draw_rect(ctx, PROC_BORDER_COLOR, (x, y - s, s, s)) | ||
111 | draw_text(ctx, label, TEXT_COLOR, x + s + 5, y) | ||
112 | |||
113 | |||
114 | def draw_legend_line(ctx, label, fill_color, x, y, s): | ||
115 | draw_fill_rect(ctx, fill_color, (x, y - s/2, s + 1, 3)) | ||
116 | ctx.arc(x + (s + 1)/2.0, y - (s - 3)/2.0, 2.5, 0, 2.0 * math.pi) | ||
117 | ctx.fill() | ||
118 | draw_text(ctx, label, TEXT_COLOR, x + s + 5, y) | ||
119 | |||
120 | |||
121 | def draw_label_in_box(ctx, color, label, x, y, w, maxx): | ||
122 | label_w = ctx.text_extents(label)[2] | ||
123 | label_x = x + w / 2 - label_w / 2 | ||
124 | if label_w + 10 > w: | ||
125 | label_x = x + w + 5 | ||
126 | if label_x + label_w > maxx: | ||
127 | label_x = x - label_w - 5 | ||
128 | draw_text(ctx, label, color, label_x, y) | ||
129 | |||
130 | |||
131 | def draw_5sec_labels(ctx, rect, sec_w): | ||
132 | ctx.set_font_size(AXIS_FONT_SIZE) | ||
133 | for i in range(0, rect[2] + 1, sec_w): | ||
134 | if ((i / sec_w) % 5 == 0) : | ||
135 | label = "%ds" % (i / sec_w) | ||
136 | label_w = ctx.text_extents(label)[2] | ||
137 | draw_text(ctx, label, TEXT_COLOR, rect[0] + i - label_w/2, rect[1] - 2) | ||
138 | |||
139 | |||
140 | def draw_box_ticks(ctx, rect, sec_w): | ||
141 | draw_rect(ctx, BORDER_COLOR, tuple(rect)) | ||
142 | |||
143 | ctx.set_line_cap(cairo.LINE_CAP_SQUARE) | ||
144 | |||
145 | for i in range(sec_w, rect[2] + 1, sec_w): | ||
146 | if ((i / sec_w) % 5 == 0) : | ||
147 | ctx.set_source_rgba(*TICK_COLOR_BOLD) | ||
148 | else : | ||
149 | ctx.set_source_rgba(*TICK_COLOR) | ||
150 | ctx.move_to(rect[0] + i, rect[1] + 1) | ||
151 | ctx.line_to(rect[0] + i, rect[1] + rect[3] - 1) | ||
152 | ctx.stroke() | ||
153 | |||
154 | ctx.set_line_cap(cairo.LINE_CAP_BUTT) | ||
155 | |||
156 | def draw_chart(ctx, color, fill, chart_bounds, data, proc_tree): | ||
157 | ctx.set_line_width(0.5) | ||
158 | x_shift = proc_tree.start_time | ||
159 | x_scale = proc_tree.duration | ||
160 | |||
161 | def transform_point_coords(point, x_base, y_base, xscale, yscale, x_trans, y_trans): | ||
162 | x = (point[0] - x_base) * xscale + x_trans | ||
163 | y = (point[1] - y_base) * -yscale + y_trans + bar_h | ||
164 | return x, y | ||
165 | |||
166 | xscale = float(chart_bounds[2]) / max(x for (x,y) in data) | ||
167 | yscale = float(chart_bounds[3]) / max(y for (x,y) in data) | ||
168 | |||
169 | first = transform_point_coords(data[0], x_shift, 0, xscale, yscale, chart_bounds[0], chart_bounds[1]) | ||
170 | last = transform_point_coords(data[-1], x_shift, 0, xscale, yscale, chart_bounds[0], chart_bounds[1]) | ||
171 | |||
172 | ctx.set_source_rgba(*color) | ||
173 | ctx.move_to(*first) | ||
174 | for point in data: | ||
175 | x, y = transform_point_coords(point, x_shift, 0, xscale, yscale, chart_bounds[0], chart_bounds[1]) | ||
176 | ctx.line_to(x, y) | ||
177 | if fill: | ||
178 | ctx.stroke_preserve() | ||
179 | ctx.line_to(last[0], chart_bounds[1]+bar_h) | ||
180 | ctx.line_to(first[0], chart_bounds[1]+bar_h) | ||
181 | ctx.line_to(first[0], first[1]) | ||
182 | ctx.fill() | ||
183 | else: | ||
184 | ctx.stroke() | ||
185 | ctx.set_line_width(1.0) | ||
186 | |||
187 | header_h = 280 | ||
188 | bar_h = 55 | ||
189 | # offsets | ||
190 | off_x, off_y = 10, 10 | ||
191 | sec_w = 25 # the width of a second | ||
192 | proc_h = 16 # the height of a process | ||
193 | leg_s = 10 | ||
194 | MIN_IMG_W = 800 | ||
195 | |||
196 | |||
197 | def extents(headers, cpu_stats, disk_stats, proc_tree): | ||
198 | w = (proc_tree.duration * sec_w / 100) + 2*off_x | ||
199 | h = proc_h * proc_tree.num_proc + header_h + 2*off_y | ||
200 | return (w,h) | ||
201 | |||
202 | # | ||
203 | # Render the chart. | ||
204 | # | ||
205 | def render(ctx, headers, cpu_stats, disk_stats, proc_tree): | ||
206 | (w, h) = extents(headers, cpu_stats, disk_stats, proc_tree) | ||
207 | |||
208 | ctx.set_line_width(1.0) | ||
209 | ctx.select_font_face(FONT_NAME) | ||
210 | draw_fill_rect(ctx, WHITE, (0, 0, max(w, MIN_IMG_W), h)) | ||
211 | w -= 2*off_x | ||
212 | # draw the title and headers | ||
213 | curr_y = draw_header(ctx, headers, off_x, proc_tree.duration) | ||
214 | |||
215 | # render bar legend | ||
216 | ctx.set_font_size(LEGEND_FONT_SIZE) | ||
217 | |||
218 | draw_legend_box(ctx, "CPU (user+sys)", CPU_COLOR, off_x, curr_y+20, leg_s) | ||
219 | draw_legend_box(ctx, "I/O (wait)", IO_COLOR, off_x + 120, curr_y+20, leg_s) | ||
220 | |||
221 | # render I/O wait | ||
222 | chart_rect = (off_x, curr_y+30, w, bar_h) | ||
223 | draw_box_ticks(ctx, chart_rect, sec_w) | ||
224 | draw_chart(ctx, IO_COLOR, True, chart_rect, [(sample.time, sample.user + sample.sys + sample.io) for sample in cpu_stats], proc_tree) | ||
225 | # render CPU load | ||
226 | draw_chart(ctx, CPU_COLOR, True, chart_rect, [(sample.time, sample.user + sample.sys) for sample in cpu_stats], proc_tree) | ||
227 | |||
228 | curr_y = curr_y + 30 + bar_h | ||
229 | |||
230 | # render second chart | ||
231 | draw_legend_line(ctx, "Disk throughput", DISK_TPUT_COLOR, off_x, curr_y+20, leg_s) | ||
232 | draw_legend_box(ctx, "Disk utilization", IO_COLOR, off_x + 120, curr_y+20, leg_s) | ||
233 | |||
234 | # render I/O utilization | ||
235 | chart_rect = (off_x, curr_y+30, w, bar_h) | ||
236 | draw_box_ticks(ctx, chart_rect, sec_w) | ||
237 | draw_chart(ctx, IO_COLOR, True, chart_rect, [(sample.time, sample.util) for sample in disk_stats], proc_tree) | ||
238 | |||
239 | # render disk throughput | ||
240 | max_sample = max(disk_stats, key=lambda s: s.tput) | ||
241 | draw_chart(ctx, DISK_TPUT_COLOR, False, chart_rect, [(sample.time, sample.tput) for sample in disk_stats], proc_tree) | ||
242 | |||
243 | pos_x = off_x + ((max_sample.time - proc_tree.start_time) * w / proc_tree.duration) | ||
244 | |||
245 | shift_x, shift_y = -20, 20 | ||
246 | if (pos_x < off_x + 245): | ||
247 | shift_x, shift_y = 5, 40 | ||
248 | |||
249 | label = "%dMB/s" % round((max_sample.tput) / 1024.0) | ||
250 | draw_text(ctx, label, DISK_TPUT_COLOR, pos_x + shift_x, curr_y + shift_y) | ||
251 | |||
252 | |||
253 | # draw process boxes | ||
254 | draw_process_bar_chart(ctx, proc_tree, curr_y + bar_h, w, h) | ||
255 | |||
256 | ctx.set_font_size(SIG_FONT_SIZE) | ||
257 | draw_text(ctx, SIGNATURE, SIG_COLOR, off_x + 5, h - off_y - 5) | ||
258 | |||
259 | def draw_process_bar_chart(ctx, proc_tree, curr_y, w, h): | ||
260 | draw_legend_box(ctx, "Running (%cpu)", PROC_COLOR_R, off_x , curr_y + 45, leg_s) | ||
261 | draw_legend_box(ctx, "Unint.sleep (I/O)", PROC_COLOR_D, off_x+120, curr_y + 45, leg_s) | ||
262 | draw_legend_box(ctx, "Sleeping", PROC_COLOR_S, off_x+240, curr_y + 45, leg_s) | ||
263 | draw_legend_box(ctx, "Zombie", PROC_COLOR_Z, off_x+360, curr_y + 45, leg_s) | ||
264 | |||
265 | chart_rect = [off_x, curr_y+60, w, h - 2 * off_y - (curr_y+60) + proc_h] | ||
266 | ctx.set_font_size(PROC_TEXT_FONT_SIZE) | ||
267 | |||
268 | draw_box_ticks(ctx, chart_rect, sec_w) | ||
269 | draw_5sec_labels(ctx, chart_rect, sec_w) | ||
270 | |||
271 | y = curr_y+60 | ||
272 | for root in proc_tree.process_tree: | ||
273 | draw_processes_recursively(ctx, root, proc_tree, y, proc_h, chart_rect) | ||
274 | y = y + proc_h * proc_tree.num_nodes([root]) | ||
275 | |||
276 | |||
277 | def draw_header(ctx, headers, off_x, duration): | ||
278 | dur = duration / 100.0 | ||
279 | toshow = [ | ||
280 | ('system.uname', 'uname', lambda s: s), | ||
281 | ('system.release', 'release', lambda s: s), | ||
282 | ('system.cpu', 'CPU', lambda s: re.sub('model name\s*:\s*', '', s, 1)), | ||
283 | ('system.kernel.options', 'kernel options', lambda s: s), | ||
284 | ('pseudo.header', 'time', lambda s: '%02d:%05.2f' % (math.floor(dur/60), dur - 60 * math.floor(dur/60))) | ||
285 | ] | ||
286 | |||
287 | header_y = ctx.font_extents()[2] + 10 | ||
288 | ctx.set_font_size(TITLE_FONT_SIZE) | ||
289 | draw_text(ctx, headers['title'], TEXT_COLOR, off_x, header_y) | ||
290 | ctx.set_font_size(TEXT_FONT_SIZE) | ||
291 | |||
292 | for (headerkey, headertitle, mangle) in toshow: | ||
293 | header_y += ctx.font_extents()[2] | ||
294 | txt = headertitle + ': ' + mangle(headers.get(headerkey)) | ||
295 | draw_text(ctx, txt, TEXT_COLOR, off_x, header_y) | ||
296 | |||
297 | return header_y | ||
298 | |||
299 | def draw_processes_recursively(ctx, proc, proc_tree, y, proc_h, rect) : | ||
300 | x = rect[0] + ((proc.start_time - proc_tree.start_time) * rect[2] / proc_tree.duration) | ||
301 | w = ((proc.duration) * rect[2] / proc_tree.duration) | ||
302 | |||
303 | draw_process_activity_colors(ctx, proc, proc_tree, x, y, w, proc_h, rect) | ||
304 | draw_rect(ctx, PROC_BORDER_COLOR, (x, y, w, proc_h)) | ||
305 | draw_label_in_box(ctx, PROC_TEXT_COLOR, proc.cmd, x, y + proc_h - 4, w, rect[0] + rect[2]) | ||
306 | |||
307 | next_y = y + proc_h | ||
308 | for child in proc.child_list: | ||
309 | child_x, child_y = draw_processes_recursively(ctx, child, proc_tree, next_y, proc_h, rect) | ||
310 | draw_process_connecting_lines(ctx, x, y, child_x, child_y, proc_h) | ||
311 | next_y = next_y + proc_h * proc_tree.num_nodes([child]) | ||
312 | |||
313 | return x, y | ||
314 | |||
315 | |||
316 | def draw_process_activity_colors(ctx, proc, proc_tree, x, y, w, proc_h, rect): | ||
317 | draw_fill_rect(ctx, PROC_COLOR_S, (x, y, w, proc_h)) | ||
318 | |||
319 | last_tx = -1 | ||
320 | for sample in proc.samples : | ||
321 | tx = rect[0] + round(((sample.time - proc_tree.start_time) * rect[2] / proc_tree.duration)) | ||
322 | tw = round(proc_tree.sample_period * rect[2] / float(proc_tree.duration)) | ||
323 | if last_tx != -1 and abs(last_tx - tx) <= tw: | ||
324 | tw -= last_tx - tx | ||
325 | tx = last_tx | ||
326 | |||
327 | last_tx = tx + tw | ||
328 | state = get_proc_state( sample.state ) | ||
329 | |||
330 | color = STATE_COLORS[state] | ||
331 | if state == STATE_RUNNING: | ||
332 | alpha = sample.cpu_sample.user + sample.cpu_sample.sys | ||
333 | color = tuple(list(PROC_COLOR_R[0:3]) + [alpha]) | ||
334 | elif state == STATE_SLEEPING: | ||
335 | continue | ||
336 | |||
337 | draw_fill_rect(ctx, color, (tx, y, tw, proc_h)) | ||
338 | |||
339 | |||
340 | def draw_process_connecting_lines(ctx, px, py, x, y, proc_h): | ||
341 | ctx.set_source_rgba(*DEP_COLOR) | ||
342 | ctx.set_dash([2,2]) | ||
343 | if abs(px - x) < 3: | ||
344 | dep_off_x = 3 | ||
345 | dep_off_y = proc_h / 4 | ||
346 | ctx.move_to(x, y + proc_h / 2) | ||
347 | ctx.line_to(px - dep_off_x, y + proc_h / 2) | ||
348 | ctx.line_to(px - dep_off_x, py - dep_off_y) | ||
349 | ctx.line_to(px, py - dep_off_y) | ||
350 | else: | ||
351 | ctx.move_to(x, y + proc_h / 2) | ||
352 | ctx.line_to(px, y + proc_h / 2) | ||
353 | ctx.line_to(px, py) | ||
354 | ctx.stroke() | ||
355 | ctx.set_dash([]) | ||