diff options
author | Tudor Florea <tudor.florea@enea.com> | 2014-10-16 03:05:19 +0200 |
---|---|---|
committer | Tudor Florea <tudor.florea@enea.com> | 2014-10-16 03:05:19 +0200 |
commit | c527fd1f14c27855a37f2e8ac5346ce8d940ced2 (patch) | |
tree | bb002c1fdf011c41dbd2f0927bed23ecb5f83c97 /scripts/pybootchartgui/pybootchartgui/draw.py | |
download | poky-c527fd1f14c27855a37f2e8ac5346ce8d940ced2.tar.gz |
initial commit for Enea Linux 4.0-140929daisy-140929
Migrated from the internal git server on the daisy-enea-point-release branch
Signed-off-by: Tudor Florea <tudor.florea@enea.com>
Diffstat (limited to 'scripts/pybootchartgui/pybootchartgui/draw.py')
-rw-r--r-- | scripts/pybootchartgui/pybootchartgui/draw.py | 894 |
1 files changed, 894 insertions, 0 deletions
diff --git a/scripts/pybootchartgui/pybootchartgui/draw.py b/scripts/pybootchartgui/pybootchartgui/draw.py new file mode 100644 index 0000000000..8c574be50c --- /dev/null +++ b/scripts/pybootchartgui/pybootchartgui/draw.py | |||
@@ -0,0 +1,894 @@ | |||
1 | # This file is part of pybootchartgui. | ||
2 | |||
3 | # pybootchartgui is free software: you can redistribute it and/or modify | ||
4 | # it under the terms of the GNU General Public License as published by | ||
5 | # the Free Software Foundation, either version 3 of the License, or | ||
6 | # (at your option) any later version. | ||
7 | |||
8 | # pybootchartgui is distributed in the hope that it will be useful, | ||
9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
11 | # GNU General Public License for more details. | ||
12 | |||
13 | # You should have received a copy of the GNU General Public License | ||
14 | # along with pybootchartgui. If not, see <http://www.gnu.org/licenses/>. | ||
15 | |||
16 | |||
17 | import cairo | ||
18 | import math | ||
19 | import re | ||
20 | import random | ||
21 | import colorsys | ||
22 | from operator import itemgetter | ||
23 | |||
24 | class RenderOptions: | ||
25 | |||
26 | def __init__(self, app_options): | ||
27 | # should we render a cumulative CPU time chart | ||
28 | self.cumulative = True | ||
29 | self.charts = True | ||
30 | self.kernel_only = False | ||
31 | self.app_options = app_options | ||
32 | |||
33 | def proc_tree (self, trace): | ||
34 | if self.kernel_only: | ||
35 | return trace.kernel_tree | ||
36 | else: | ||
37 | return trace.proc_tree | ||
38 | |||
39 | # Process tree background color. | ||
40 | BACK_COLOR = (1.0, 1.0, 1.0, 1.0) | ||
41 | |||
42 | WHITE = (1.0, 1.0, 1.0, 1.0) | ||
43 | # Process tree border color. | ||
44 | BORDER_COLOR = (0.63, 0.63, 0.63, 1.0) | ||
45 | # Second tick line color. | ||
46 | TICK_COLOR = (0.92, 0.92, 0.92, 1.0) | ||
47 | # 5-second tick line color. | ||
48 | TICK_COLOR_BOLD = (0.86, 0.86, 0.86, 1.0) | ||
49 | # Annotation colour | ||
50 | ANNOTATION_COLOR = (0.63, 0.0, 0.0, 0.5) | ||
51 | # Text color. | ||
52 | TEXT_COLOR = (0.0, 0.0, 0.0, 1.0) | ||
53 | |||
54 | # Font family | ||
55 | FONT_NAME = "Bitstream Vera Sans" | ||
56 | # Title text font. | ||
57 | TITLE_FONT_SIZE = 18 | ||
58 | # Default text font. | ||
59 | TEXT_FONT_SIZE = 12 | ||
60 | # Axis label font. | ||
61 | AXIS_FONT_SIZE = 11 | ||
62 | # Legend font. | ||
63 | LEGEND_FONT_SIZE = 12 | ||
64 | |||
65 | # CPU load chart color. | ||
66 | CPU_COLOR = (0.40, 0.55, 0.70, 1.0) | ||
67 | # IO wait chart color. | ||
68 | IO_COLOR = (0.76, 0.48, 0.48, 0.5) | ||
69 | # Disk throughput color. | ||
70 | DISK_TPUT_COLOR = (0.20, 0.71, 0.20, 1.0) | ||
71 | # CPU load chart color. | ||
72 | FILE_OPEN_COLOR = (0.20, 0.71, 0.71, 1.0) | ||
73 | # Mem cached color | ||
74 | MEM_CACHED_COLOR = CPU_COLOR | ||
75 | # Mem used color | ||
76 | MEM_USED_COLOR = IO_COLOR | ||
77 | # Buffers color | ||
78 | MEM_BUFFERS_COLOR = (0.4, 0.4, 0.4, 0.3) | ||
79 | # Swap color | ||
80 | MEM_SWAP_COLOR = DISK_TPUT_COLOR | ||
81 | |||
82 | # Process border color. | ||
83 | PROC_BORDER_COLOR = (0.71, 0.71, 0.71, 1.0) | ||
84 | # Waiting process color. | ||
85 | PROC_COLOR_D = (0.76, 0.48, 0.48, 0.5) | ||
86 | # Running process color. | ||
87 | PROC_COLOR_R = CPU_COLOR | ||
88 | # Sleeping process color. | ||
89 | PROC_COLOR_S = (0.94, 0.94, 0.94, 1.0) | ||
90 | # Stopped process color. | ||
91 | PROC_COLOR_T = (0.94, 0.50, 0.50, 1.0) | ||
92 | # Zombie process color. | ||
93 | PROC_COLOR_Z = (0.71, 0.71, 0.71, 1.0) | ||
94 | # Dead process color. | ||
95 | PROC_COLOR_X = (0.71, 0.71, 0.71, 0.125) | ||
96 | # Paging process color. | ||
97 | PROC_COLOR_W = (0.71, 0.71, 0.71, 0.125) | ||
98 | |||
99 | # Process label color. | ||
100 | PROC_TEXT_COLOR = (0.19, 0.19, 0.19, 1.0) | ||
101 | # Process label font. | ||
102 | PROC_TEXT_FONT_SIZE = 12 | ||
103 | |||
104 | # Signature color. | ||
105 | SIG_COLOR = (0.0, 0.0, 0.0, 0.3125) | ||
106 | # Signature font. | ||
107 | SIG_FONT_SIZE = 14 | ||
108 | # Signature text. | ||
109 | SIGNATURE = "http://github.com/mmeeks/bootchart" | ||
110 | |||
111 | # Process dependency line color. | ||
112 | DEP_COLOR = (0.75, 0.75, 0.75, 1.0) | ||
113 | # Process dependency line stroke. | ||
114 | DEP_STROKE = 1.0 | ||
115 | |||
116 | # Process description date format. | ||
117 | DESC_TIME_FORMAT = "mm:ss.SSS" | ||
118 | |||
119 | # Cumulative coloring bits | ||
120 | HSV_MAX_MOD = 31 | ||
121 | HSV_STEP = 7 | ||
122 | |||
123 | # Configure task color | ||
124 | TASK_COLOR_CONFIGURE = (1.0, 1.0, 0.00, 1.0) | ||
125 | # Compile task color. | ||
126 | TASK_COLOR_COMPILE = (0.0, 1.00, 0.00, 1.0) | ||
127 | # Install task color | ||
128 | TASK_COLOR_INSTALL = (1.0, 0.00, 1.00, 1.0) | ||
129 | # Sysroot task color | ||
130 | TASK_COLOR_SYSROOT = (0.0, 0.00, 1.00, 1.0) | ||
131 | # Package task color | ||
132 | TASK_COLOR_PACKAGE = (0.0, 1.00, 1.00, 1.0) | ||
133 | # Package Write RPM/DEB/IPK task color | ||
134 | TASK_COLOR_PACKAGE_WRITE = (0.0, 0.50, 0.50, 1.0) | ||
135 | |||
136 | # Process states | ||
137 | STATE_UNDEFINED = 0 | ||
138 | STATE_RUNNING = 1 | ||
139 | STATE_SLEEPING = 2 | ||
140 | STATE_WAITING = 3 | ||
141 | STATE_STOPPED = 4 | ||
142 | STATE_ZOMBIE = 5 | ||
143 | |||
144 | STATE_COLORS = [(0, 0, 0, 0), PROC_COLOR_R, PROC_COLOR_S, PROC_COLOR_D, \ | ||
145 | PROC_COLOR_T, PROC_COLOR_Z, PROC_COLOR_X, PROC_COLOR_W] | ||
146 | |||
147 | # CumulativeStats Types | ||
148 | STAT_TYPE_CPU = 0 | ||
149 | STAT_TYPE_IO = 1 | ||
150 | |||
151 | # Convert ps process state to an int | ||
152 | def get_proc_state(flag): | ||
153 | return "RSDTZXW".find(flag) + 1 | ||
154 | |||
155 | def draw_text(ctx, text, color, x, y): | ||
156 | ctx.set_source_rgba(*color) | ||
157 | ctx.move_to(x, y) | ||
158 | ctx.show_text(text) | ||
159 | |||
160 | def draw_fill_rect(ctx, color, rect): | ||
161 | ctx.set_source_rgba(*color) | ||
162 | ctx.rectangle(*rect) | ||
163 | ctx.fill() | ||
164 | |||
165 | def draw_rect(ctx, color, rect): | ||
166 | ctx.set_source_rgba(*color) | ||
167 | ctx.rectangle(*rect) | ||
168 | ctx.stroke() | ||
169 | |||
170 | def draw_legend_box(ctx, label, fill_color, x, y, s): | ||
171 | draw_fill_rect(ctx, fill_color, (x, y - s, s, s)) | ||
172 | draw_rect(ctx, PROC_BORDER_COLOR, (x, y - s, s, s)) | ||
173 | draw_text(ctx, label, TEXT_COLOR, x + s + 5, y) | ||
174 | |||
175 | def draw_legend_line(ctx, label, fill_color, x, y, s): | ||
176 | draw_fill_rect(ctx, fill_color, (x, y - s/2, s + 1, 3)) | ||
177 | ctx.arc(x + (s + 1)/2.0, y - (s - 3)/2.0, 2.5, 0, 2.0 * math.pi) | ||
178 | ctx.fill() | ||
179 | draw_text(ctx, label, TEXT_COLOR, x + s + 5, y) | ||
180 | |||
181 | def draw_label_in_box(ctx, color, label, x, y, w, maxx): | ||
182 | label_w = ctx.text_extents(label)[2] | ||
183 | label_x = x + w / 2 - label_w / 2 | ||
184 | if label_w + 10 > w: | ||
185 | label_x = x + w + 5 | ||
186 | if label_x + label_w > maxx: | ||
187 | label_x = x - label_w - 5 | ||
188 | draw_text(ctx, label, color, label_x, y) | ||
189 | |||
190 | def draw_sec_labels(ctx, options, rect, sec_w, nsecs): | ||
191 | ctx.set_font_size(AXIS_FONT_SIZE) | ||
192 | prev_x = 0 | ||
193 | for i in range(0, rect[2] + 1, sec_w): | ||
194 | if ((i / sec_w) % nsecs == 0) : | ||
195 | if options.app_options.as_minutes : | ||
196 | label = "%.1f" % (i / sec_w / 60.0) | ||
197 | else : | ||
198 | label = "%d" % (i / sec_w) | ||
199 | label_w = ctx.text_extents(label)[2] | ||
200 | x = rect[0] + i - label_w/2 | ||
201 | if x >= prev_x: | ||
202 | draw_text(ctx, label, TEXT_COLOR, x, rect[1] - 2) | ||
203 | prev_x = x + label_w | ||
204 | |||
205 | def draw_box_ticks(ctx, rect, sec_w): | ||
206 | draw_rect(ctx, BORDER_COLOR, tuple(rect)) | ||
207 | |||
208 | ctx.set_line_cap(cairo.LINE_CAP_SQUARE) | ||
209 | |||
210 | for i in range(sec_w, rect[2] + 1, sec_w): | ||
211 | if ((i / sec_w) % 10 == 0) : | ||
212 | ctx.set_line_width(1.5) | ||
213 | elif sec_w < 5 : | ||
214 | continue | ||
215 | else : | ||
216 | ctx.set_line_width(1.0) | ||
217 | if ((i / sec_w) % 30 == 0) : | ||
218 | ctx.set_source_rgba(*TICK_COLOR_BOLD) | ||
219 | else : | ||
220 | ctx.set_source_rgba(*TICK_COLOR) | ||
221 | ctx.move_to(rect[0] + i, rect[1] + 1) | ||
222 | ctx.line_to(rect[0] + i, rect[1] + rect[3] - 1) | ||
223 | ctx.stroke() | ||
224 | ctx.set_line_width(1.0) | ||
225 | |||
226 | ctx.set_line_cap(cairo.LINE_CAP_BUTT) | ||
227 | |||
228 | def draw_annotations(ctx, proc_tree, times, rect): | ||
229 | ctx.set_line_cap(cairo.LINE_CAP_SQUARE) | ||
230 | ctx.set_source_rgba(*ANNOTATION_COLOR) | ||
231 | ctx.set_dash([4, 4]) | ||
232 | |||
233 | for time in times: | ||
234 | if time is not None: | ||
235 | x = ((time - proc_tree.start_time) * rect[2] / proc_tree.duration) | ||
236 | |||
237 | ctx.move_to(rect[0] + x, rect[1] + 1) | ||
238 | ctx.line_to(rect[0] + x, rect[1] + rect[3] - 1) | ||
239 | ctx.stroke() | ||
240 | |||
241 | ctx.set_line_cap(cairo.LINE_CAP_BUTT) | ||
242 | ctx.set_dash([]) | ||
243 | |||
244 | def draw_chart(ctx, color, fill, chart_bounds, data, proc_tree, data_range): | ||
245 | ctx.set_line_width(0.5) | ||
246 | x_shift = proc_tree.start_time | ||
247 | |||
248 | def transform_point_coords(point, x_base, y_base, \ | ||
249 | xscale, yscale, x_trans, y_trans): | ||
250 | x = (point[0] - x_base) * xscale + x_trans | ||
251 | y = (point[1] - y_base) * -yscale + y_trans + chart_bounds[3] | ||
252 | return x, y | ||
253 | |||
254 | max_x = max (x for (x, y) in data) | ||
255 | max_y = max (y for (x, y) in data) | ||
256 | # avoid divide by zero | ||
257 | if max_y == 0: | ||
258 | max_y = 1.0 | ||
259 | xscale = float (chart_bounds[2]) / max_x | ||
260 | # If data_range is given, scale the chart so that the value range in | ||
261 | # data_range matches the chart bounds exactly. | ||
262 | # Otherwise, scale so that the actual data matches the chart bounds. | ||
263 | if data_range: | ||
264 | yscale = float(chart_bounds[3]) / (data_range[1] - data_range[0]) | ||
265 | ybase = data_range[0] | ||
266 | else: | ||
267 | yscale = float(chart_bounds[3]) / max_y | ||
268 | ybase = 0 | ||
269 | |||
270 | first = transform_point_coords (data[0], x_shift, ybase, xscale, yscale, \ | ||
271 | chart_bounds[0], chart_bounds[1]) | ||
272 | last = transform_point_coords (data[-1], x_shift, ybase, xscale, yscale, \ | ||
273 | chart_bounds[0], chart_bounds[1]) | ||
274 | |||
275 | ctx.set_source_rgba(*color) | ||
276 | ctx.move_to(*first) | ||
277 | for point in data: | ||
278 | x, y = transform_point_coords (point, x_shift, ybase, xscale, yscale, \ | ||
279 | chart_bounds[0], chart_bounds[1]) | ||
280 | ctx.line_to(x, y) | ||
281 | if fill: | ||
282 | ctx.stroke_preserve() | ||
283 | ctx.line_to(last[0], chart_bounds[1]+chart_bounds[3]) | ||
284 | ctx.line_to(first[0], chart_bounds[1]+chart_bounds[3]) | ||
285 | ctx.line_to(first[0], first[1]) | ||
286 | ctx.fill() | ||
287 | else: | ||
288 | ctx.stroke() | ||
289 | ctx.set_line_width(1.0) | ||
290 | |||
291 | bar_h = 55 | ||
292 | meminfo_bar_h = 2 * bar_h | ||
293 | header_h = 60 | ||
294 | # offsets | ||
295 | off_x, off_y = 220, 10 | ||
296 | sec_w_base = 1 # the width of a second | ||
297 | proc_h = 16 # the height of a process | ||
298 | leg_s = 10 | ||
299 | MIN_IMG_W = 800 | ||
300 | CUML_HEIGHT = 2000 # Increased value to accomodate CPU and I/O Graphs | ||
301 | OPTIONS = None | ||
302 | |||
303 | def extents(options, xscale, trace): | ||
304 | start = min(trace.start.keys()) | ||
305 | end = start | ||
306 | |||
307 | processes = 0 | ||
308 | for proc in trace.processes: | ||
309 | if not options.app_options.show_all and \ | ||
310 | trace.processes[proc][1] - trace.processes[proc][0] < options.app_options.mintime: | ||
311 | continue | ||
312 | |||
313 | if trace.processes[proc][1] > end: | ||
314 | end = trace.processes[proc][1] | ||
315 | processes += 1 | ||
316 | |||
317 | if trace.min is not None and trace.max is not None: | ||
318 | start = trace.min | ||
319 | end = trace.max | ||
320 | |||
321 | w = int ((end - start) * sec_w_base * xscale) + 2 * off_x | ||
322 | h = proc_h * processes + header_h + 2 * off_y | ||
323 | |||
324 | return (w, h) | ||
325 | |||
326 | def clip_visible(clip, rect): | ||
327 | xmax = max (clip[0], rect[0]) | ||
328 | ymax = max (clip[1], rect[1]) | ||
329 | xmin = min (clip[0] + clip[2], rect[0] + rect[2]) | ||
330 | ymin = min (clip[1] + clip[3], rect[1] + rect[3]) | ||
331 | return (xmin > xmax and ymin > ymax) | ||
332 | |||
333 | def render_charts(ctx, options, clip, trace, curr_y, w, h, sec_w): | ||
334 | proc_tree = options.proc_tree(trace) | ||
335 | |||
336 | # render bar legend | ||
337 | ctx.set_font_size(LEGEND_FONT_SIZE) | ||
338 | |||
339 | draw_legend_box(ctx, "CPU (user+sys)", CPU_COLOR, off_x, curr_y+20, leg_s) | ||
340 | draw_legend_box(ctx, "I/O (wait)", IO_COLOR, off_x + 120, curr_y+20, leg_s) | ||
341 | |||
342 | # render I/O wait | ||
343 | chart_rect = (off_x, curr_y+30, w, bar_h) | ||
344 | if clip_visible (clip, chart_rect): | ||
345 | draw_box_ticks (ctx, chart_rect, sec_w) | ||
346 | draw_annotations (ctx, proc_tree, trace.times, chart_rect) | ||
347 | draw_chart (ctx, IO_COLOR, True, chart_rect, \ | ||
348 | [(sample.time, sample.user + sample.sys + sample.io) for sample in trace.cpu_stats], \ | ||
349 | proc_tree, None) | ||
350 | # render CPU load | ||
351 | draw_chart (ctx, CPU_COLOR, True, chart_rect, \ | ||
352 | [(sample.time, sample.user + sample.sys) for sample in trace.cpu_stats], \ | ||
353 | proc_tree, None) | ||
354 | |||
355 | curr_y = curr_y + 30 + bar_h | ||
356 | |||
357 | # render second chart | ||
358 | draw_legend_line(ctx, "Disk throughput", DISK_TPUT_COLOR, off_x, curr_y+20, leg_s) | ||
359 | draw_legend_box(ctx, "Disk utilization", IO_COLOR, off_x + 120, curr_y+20, leg_s) | ||
360 | |||
361 | # render I/O utilization | ||
362 | chart_rect = (off_x, curr_y+30, w, bar_h) | ||
363 | if clip_visible (clip, chart_rect): | ||
364 | draw_box_ticks (ctx, chart_rect, sec_w) | ||
365 | draw_annotations (ctx, proc_tree, trace.times, chart_rect) | ||
366 | draw_chart (ctx, IO_COLOR, True, chart_rect, \ | ||
367 | [(sample.time, sample.util) for sample in trace.disk_stats], \ | ||
368 | proc_tree, None) | ||
369 | |||
370 | # render disk throughput | ||
371 | max_sample = max (trace.disk_stats, key = lambda s: s.tput) | ||
372 | if clip_visible (clip, chart_rect): | ||
373 | draw_chart (ctx, DISK_TPUT_COLOR, False, chart_rect, \ | ||
374 | [(sample.time, sample.tput) for sample in trace.disk_stats], \ | ||
375 | proc_tree, None) | ||
376 | |||
377 | pos_x = off_x + ((max_sample.time - proc_tree.start_time) * w / proc_tree.duration) | ||
378 | |||
379 | shift_x, shift_y = -20, 20 | ||
380 | if (pos_x < off_x + 245): | ||
381 | shift_x, shift_y = 5, 40 | ||
382 | |||
383 | label = "%dMB/s" % round ((max_sample.tput) / 1024.0) | ||
384 | draw_text (ctx, label, DISK_TPUT_COLOR, pos_x + shift_x, curr_y + shift_y) | ||
385 | |||
386 | curr_y = curr_y + 30 + bar_h | ||
387 | |||
388 | # render mem usage | ||
389 | chart_rect = (off_x, curr_y+30, w, meminfo_bar_h) | ||
390 | mem_stats = trace.mem_stats | ||
391 | if mem_stats and clip_visible (clip, chart_rect): | ||
392 | mem_scale = max(sample.records['MemTotal'] - sample.records['MemFree'] for sample in mem_stats) | ||
393 | draw_legend_box(ctx, "Mem cached (scale: %u MiB)" % (float(mem_scale) / 1024), MEM_CACHED_COLOR, off_x, curr_y+20, leg_s) | ||
394 | draw_legend_box(ctx, "Used", MEM_USED_COLOR, off_x + 240, curr_y+20, leg_s) | ||
395 | draw_legend_box(ctx, "Buffers", MEM_BUFFERS_COLOR, off_x + 360, curr_y+20, leg_s) | ||
396 | draw_legend_line(ctx, "Swap (scale: %u MiB)" % max([(sample.records['SwapTotal'] - sample.records['SwapFree'])/1024 for sample in mem_stats]), \ | ||
397 | MEM_SWAP_COLOR, off_x + 480, curr_y+20, leg_s) | ||
398 | draw_box_ticks(ctx, chart_rect, sec_w) | ||
399 | draw_annotations(ctx, proc_tree, trace.times, chart_rect) | ||
400 | draw_chart(ctx, MEM_BUFFERS_COLOR, True, chart_rect, \ | ||
401 | [(sample.time, sample.records['MemTotal'] - sample.records['MemFree']) for sample in trace.mem_stats], \ | ||
402 | proc_tree, [0, mem_scale]) | ||
403 | draw_chart(ctx, MEM_USED_COLOR, True, chart_rect, \ | ||
404 | [(sample.time, sample.records['MemTotal'] - sample.records['MemFree'] - sample.records['Buffers']) for sample in mem_stats], \ | ||
405 | proc_tree, [0, mem_scale]) | ||
406 | draw_chart(ctx, MEM_CACHED_COLOR, True, chart_rect, \ | ||
407 | [(sample.time, sample.records['Cached']) for sample in mem_stats], \ | ||
408 | proc_tree, [0, mem_scale]) | ||
409 | draw_chart(ctx, MEM_SWAP_COLOR, False, chart_rect, \ | ||
410 | [(sample.time, float(sample.records['SwapTotal'] - sample.records['SwapFree'])) for sample in mem_stats], \ | ||
411 | proc_tree, None) | ||
412 | |||
413 | curr_y = curr_y + meminfo_bar_h | ||
414 | |||
415 | return curr_y | ||
416 | |||
417 | def render_processes_chart(ctx, options, trace, curr_y, w, h, sec_w): | ||
418 | chart_rect = [off_x, curr_y+header_h, w, h - 2 * off_y - (curr_y+header_h) + proc_h] | ||
419 | |||
420 | draw_legend_box (ctx, "Configure", \ | ||
421 | TASK_COLOR_CONFIGURE, off_x , curr_y + 45, leg_s) | ||
422 | draw_legend_box (ctx, "Compile", \ | ||
423 | TASK_COLOR_COMPILE, off_x+120, curr_y + 45, leg_s) | ||
424 | draw_legend_box (ctx, "Install", \ | ||
425 | TASK_COLOR_INSTALL, off_x+240, curr_y + 45, leg_s) | ||
426 | draw_legend_box (ctx, "Populate Sysroot", \ | ||
427 | TASK_COLOR_SYSROOT, off_x+360, curr_y + 45, leg_s) | ||
428 | draw_legend_box (ctx, "Package", \ | ||
429 | TASK_COLOR_PACKAGE, off_x+480, curr_y + 45, leg_s) | ||
430 | draw_legend_box (ctx, "Package Write", | ||
431 | TASK_COLOR_PACKAGE_WRITE, off_x+600, curr_y + 45, leg_s) | ||
432 | |||
433 | ctx.set_font_size(PROC_TEXT_FONT_SIZE) | ||
434 | |||
435 | draw_box_ticks(ctx, chart_rect, sec_w) | ||
436 | draw_sec_labels(ctx, options, chart_rect, sec_w, 30) | ||
437 | |||
438 | y = curr_y+header_h | ||
439 | |||
440 | offset = trace.min or min(trace.start.keys()) | ||
441 | for s in sorted(trace.start.keys()): | ||
442 | for val in sorted(trace.start[s]): | ||
443 | if not options.app_options.show_all and \ | ||
444 | trace.processes[val][1] - s < options.app_options.mintime: | ||
445 | continue | ||
446 | task = val.split(":")[1] | ||
447 | #print val | ||
448 | #print trace.processes[val][1] | ||
449 | #print s | ||
450 | x = chart_rect[0] + (s - offset) * sec_w | ||
451 | w = ((trace.processes[val][1] - s) * sec_w) | ||
452 | |||
453 | #print "proc at %s %s %s %s" % (x, y, w, proc_h) | ||
454 | col = None | ||
455 | if task == "do_compile": | ||
456 | col = TASK_COLOR_COMPILE | ||
457 | elif task == "do_configure": | ||
458 | col = TASK_COLOR_CONFIGURE | ||
459 | elif task == "do_install": | ||
460 | col = TASK_COLOR_INSTALL | ||
461 | elif task == "do_populate_sysroot": | ||
462 | col = TASK_COLOR_SYSROOT | ||
463 | elif task == "do_package": | ||
464 | col = TASK_COLOR_PACKAGE | ||
465 | elif task == "do_package_write_rpm" or \ | ||
466 | task == "do_package_write_deb" or \ | ||
467 | task == "do_package_write_ipk": | ||
468 | col = TASK_COLOR_PACKAGE_WRITE | ||
469 | else: | ||
470 | col = WHITE | ||
471 | |||
472 | if col: | ||
473 | draw_fill_rect(ctx, col, (x, y, w, proc_h)) | ||
474 | draw_rect(ctx, PROC_BORDER_COLOR, (x, y, w, proc_h)) | ||
475 | |||
476 | draw_label_in_box(ctx, PROC_TEXT_COLOR, val, x, y + proc_h - 4, w, proc_h) | ||
477 | y = y + proc_h | ||
478 | |||
479 | return curr_y | ||
480 | |||
481 | # | ||
482 | # Render the chart. | ||
483 | # | ||
484 | def render(ctx, options, xscale, trace): | ||
485 | (w, h) = extents (options, xscale, trace) | ||
486 | global OPTIONS | ||
487 | OPTIONS = options.app_options | ||
488 | |||
489 | # x, y, w, h | ||
490 | clip = ctx.clip_extents() | ||
491 | |||
492 | sec_w = int (xscale * sec_w_base) | ||
493 | ctx.set_line_width(1.0) | ||
494 | ctx.select_font_face(FONT_NAME) | ||
495 | draw_fill_rect(ctx, WHITE, (0, 0, max(w, MIN_IMG_W), h)) | ||
496 | w -= 2*off_x | ||
497 | curr_y = off_y; | ||
498 | |||
499 | curr_y = render_processes_chart (ctx, options, trace, curr_y, w, h, sec_w) | ||
500 | |||
501 | return | ||
502 | |||
503 | proc_tree = options.proc_tree (trace) | ||
504 | |||
505 | # draw the title and headers | ||
506 | if proc_tree.idle: | ||
507 | duration = proc_tree.idle | ||
508 | else: | ||
509 | duration = proc_tree.duration | ||
510 | |||
511 | if not options.kernel_only: | ||
512 | curr_y = draw_header (ctx, trace.headers, duration) | ||
513 | else: | ||
514 | curr_y = off_y; | ||
515 | |||
516 | if options.charts: | ||
517 | curr_y = render_charts (ctx, options, clip, trace, curr_y, w, h, sec_w) | ||
518 | |||
519 | # draw process boxes | ||
520 | proc_height = h | ||
521 | if proc_tree.taskstats and options.cumulative: | ||
522 | proc_height -= CUML_HEIGHT | ||
523 | |||
524 | draw_process_bar_chart(ctx, clip, options, proc_tree, trace.times, | ||
525 | curr_y, w, proc_height, sec_w) | ||
526 | |||
527 | curr_y = proc_height | ||
528 | ctx.set_font_size(SIG_FONT_SIZE) | ||
529 | draw_text(ctx, SIGNATURE, SIG_COLOR, off_x + 5, proc_height - 8) | ||
530 | |||
531 | # draw a cumulative CPU-time-per-process graph | ||
532 | if proc_tree.taskstats and options.cumulative: | ||
533 | cuml_rect = (off_x, curr_y + off_y, w, CUML_HEIGHT/2 - off_y * 2) | ||
534 | if clip_visible (clip, cuml_rect): | ||
535 | draw_cuml_graph(ctx, proc_tree, cuml_rect, duration, sec_w, STAT_TYPE_CPU) | ||
536 | |||
537 | # draw a cumulative I/O-time-per-process graph | ||
538 | if proc_tree.taskstats and options.cumulative: | ||
539 | cuml_rect = (off_x, curr_y + off_y * 100, w, CUML_HEIGHT/2 - off_y * 2) | ||
540 | if clip_visible (clip, cuml_rect): | ||
541 | draw_cuml_graph(ctx, proc_tree, cuml_rect, duration, sec_w, STAT_TYPE_IO) | ||
542 | |||
543 | def draw_process_bar_chart(ctx, clip, options, proc_tree, times, curr_y, w, h, sec_w): | ||
544 | header_size = 0 | ||
545 | if not options.kernel_only: | ||
546 | draw_legend_box (ctx, "Running (%cpu)", | ||
547 | PROC_COLOR_R, off_x , curr_y + 45, leg_s) | ||
548 | draw_legend_box (ctx, "Unint.sleep (I/O)", | ||
549 | PROC_COLOR_D, off_x+120, curr_y + 45, leg_s) | ||
550 | draw_legend_box (ctx, "Sleeping", | ||
551 | PROC_COLOR_S, off_x+240, curr_y + 45, leg_s) | ||
552 | draw_legend_box (ctx, "Zombie", | ||
553 | PROC_COLOR_Z, off_x+360, curr_y + 45, leg_s) | ||
554 | header_size = 45 | ||
555 | |||
556 | chart_rect = [off_x, curr_y + header_size + 15, | ||
557 | w, h - 2 * off_y - (curr_y + header_size + 15) + proc_h] | ||
558 | ctx.set_font_size (PROC_TEXT_FONT_SIZE) | ||
559 | |||
560 | draw_box_ticks (ctx, chart_rect, sec_w) | ||
561 | if sec_w > 100: | ||
562 | nsec = 1 | ||
563 | else: | ||
564 | nsec = 5 | ||
565 | draw_sec_labels (ctx, options, chart_rect, sec_w, nsec) | ||
566 | draw_annotations (ctx, proc_tree, times, chart_rect) | ||
567 | |||
568 | y = curr_y + 60 | ||
569 | for root in proc_tree.process_tree: | ||
570 | draw_processes_recursively(ctx, root, proc_tree, y, proc_h, chart_rect, clip) | ||
571 | y = y + proc_h * proc_tree.num_nodes([root]) | ||
572 | |||
573 | |||
574 | def draw_header (ctx, headers, duration): | ||
575 | toshow = [ | ||
576 | ('system.uname', 'uname', lambda s: s), | ||
577 | ('system.release', 'release', lambda s: s), | ||
578 | ('system.cpu', 'CPU', lambda s: re.sub('model name\s*:\s*', '', s, 1)), | ||
579 | ('system.kernel.options', 'kernel options', lambda s: s), | ||
580 | ] | ||
581 | |||
582 | header_y = ctx.font_extents()[2] + 10 | ||
583 | ctx.set_font_size(TITLE_FONT_SIZE) | ||
584 | draw_text(ctx, headers['title'], TEXT_COLOR, off_x, header_y) | ||
585 | ctx.set_font_size(TEXT_FONT_SIZE) | ||
586 | |||
587 | for (headerkey, headertitle, mangle) in toshow: | ||
588 | header_y += ctx.font_extents()[2] | ||
589 | if headerkey in headers: | ||
590 | value = headers.get(headerkey) | ||
591 | else: | ||
592 | value = "" | ||
593 | txt = headertitle + ': ' + mangle(value) | ||
594 | draw_text(ctx, txt, TEXT_COLOR, off_x, header_y) | ||
595 | |||
596 | dur = duration / 100.0 | ||
597 | txt = 'time : %02d:%05.2f' % (math.floor(dur/60), dur - 60 * math.floor(dur/60)) | ||
598 | if headers.get('system.maxpid') is not None: | ||
599 | txt = txt + ' max pid: %s' % (headers.get('system.maxpid')) | ||
600 | |||
601 | header_y += ctx.font_extents()[2] | ||
602 | draw_text (ctx, txt, TEXT_COLOR, off_x, header_y) | ||
603 | |||
604 | return header_y | ||
605 | |||
606 | def draw_processes_recursively(ctx, proc, proc_tree, y, proc_h, rect, clip) : | ||
607 | x = rect[0] + ((proc.start_time - proc_tree.start_time) * rect[2] / proc_tree.duration) | ||
608 | w = ((proc.duration) * rect[2] / proc_tree.duration) | ||
609 | |||
610 | draw_process_activity_colors(ctx, proc, proc_tree, x, y, w, proc_h, rect, clip) | ||
611 | draw_rect(ctx, PROC_BORDER_COLOR, (x, y, w, proc_h)) | ||
612 | ipid = int(proc.pid) | ||
613 | if not OPTIONS.show_all: | ||
614 | cmdString = proc.cmd | ||
615 | else: | ||
616 | cmdString = '' | ||
617 | if (OPTIONS.show_pid or OPTIONS.show_all) and ipid is not 0: | ||
618 | cmdString = cmdString + " [" + str(ipid // 1000) + "]" | ||
619 | if OPTIONS.show_all: | ||
620 | if proc.args: | ||
621 | cmdString = cmdString + " '" + "' '".join(proc.args) + "'" | ||
622 | else: | ||
623 | cmdString = cmdString + " " + proc.exe | ||
624 | |||
625 | draw_label_in_box(ctx, PROC_TEXT_COLOR, cmdString, x, y + proc_h - 4, w, rect[0] + rect[2]) | ||
626 | |||
627 | next_y = y + proc_h | ||
628 | for child in proc.child_list: | ||
629 | if next_y > clip[1] + clip[3]: | ||
630 | break | ||
631 | child_x, child_y = draw_processes_recursively(ctx, child, proc_tree, next_y, proc_h, rect, clip) | ||
632 | draw_process_connecting_lines(ctx, x, y, child_x, child_y, proc_h) | ||
633 | next_y = next_y + proc_h * proc_tree.num_nodes([child]) | ||
634 | |||
635 | return x, y | ||
636 | |||
637 | |||
638 | def draw_process_activity_colors(ctx, proc, proc_tree, x, y, w, proc_h, rect, clip): | ||
639 | |||
640 | if y > clip[1] + clip[3] or y + proc_h + 2 < clip[1]: | ||
641 | return | ||
642 | |||
643 | draw_fill_rect(ctx, PROC_COLOR_S, (x, y, w, proc_h)) | ||
644 | |||
645 | last_tx = -1 | ||
646 | for sample in proc.samples : | ||
647 | tx = rect[0] + round(((sample.time - proc_tree.start_time) * rect[2] / proc_tree.duration)) | ||
648 | |||
649 | # samples are sorted chronologically | ||
650 | if tx < clip[0]: | ||
651 | continue | ||
652 | if tx > clip[0] + clip[2]: | ||
653 | break | ||
654 | |||
655 | tw = round(proc_tree.sample_period * rect[2] / float(proc_tree.duration)) | ||
656 | if last_tx != -1 and abs(last_tx - tx) <= tw: | ||
657 | tw -= last_tx - tx | ||
658 | tx = last_tx | ||
659 | tw = max (tw, 1) # nice to see at least something | ||
660 | |||
661 | last_tx = tx + tw | ||
662 | state = get_proc_state( sample.state ) | ||
663 | |||
664 | color = STATE_COLORS[state] | ||
665 | if state == STATE_RUNNING: | ||
666 | alpha = min (sample.cpu_sample.user + sample.cpu_sample.sys, 1.0) | ||
667 | color = tuple(list(PROC_COLOR_R[0:3]) + [alpha]) | ||
668 | # print "render time %d [ tx %d tw %d ], sample state %s color %s alpha %g" % (sample.time, tx, tw, state, color, alpha) | ||
669 | elif state == STATE_SLEEPING: | ||
670 | continue | ||
671 | |||
672 | draw_fill_rect(ctx, color, (tx, y, tw, proc_h)) | ||
673 | |||
674 | def draw_process_connecting_lines(ctx, px, py, x, y, proc_h): | ||
675 | ctx.set_source_rgba(*DEP_COLOR) | ||
676 | ctx.set_dash([2, 2]) | ||
677 | if abs(px - x) < 3: | ||
678 | dep_off_x = 3 | ||
679 | dep_off_y = proc_h / 4 | ||
680 | ctx.move_to(x, y + proc_h / 2) | ||
681 | ctx.line_to(px - dep_off_x, y + proc_h / 2) | ||
682 | ctx.line_to(px - dep_off_x, py - dep_off_y) | ||
683 | ctx.line_to(px, py - dep_off_y) | ||
684 | else: | ||
685 | ctx.move_to(x, y + proc_h / 2) | ||
686 | ctx.line_to(px, y + proc_h / 2) | ||
687 | ctx.line_to(px, py) | ||
688 | ctx.stroke() | ||
689 | ctx.set_dash([]) | ||
690 | |||
691 | # elide the bootchart collector - it is quite distorting | ||
692 | def elide_bootchart(proc): | ||
693 | return proc.cmd == 'bootchartd' or proc.cmd == 'bootchart-colle' | ||
694 | |||
695 | class CumlSample: | ||
696 | def __init__(self, proc): | ||
697 | self.cmd = proc.cmd | ||
698 | self.samples = [] | ||
699 | self.merge_samples (proc) | ||
700 | self.color = None | ||
701 | |||
702 | def merge_samples(self, proc): | ||
703 | self.samples.extend (proc.samples) | ||
704 | self.samples.sort (key = lambda p: p.time) | ||
705 | |||
706 | def next(self): | ||
707 | global palette_idx | ||
708 | palette_idx += HSV_STEP | ||
709 | return palette_idx | ||
710 | |||
711 | def get_color(self): | ||
712 | if self.color is None: | ||
713 | i = self.next() % HSV_MAX_MOD | ||
714 | h = 0.0 | ||
715 | if i is not 0: | ||
716 | h = (1.0 * i) / HSV_MAX_MOD | ||
717 | s = 0.5 | ||
718 | v = 1.0 | ||
719 | c = colorsys.hsv_to_rgb (h, s, v) | ||
720 | self.color = (c[0], c[1], c[2], 1.0) | ||
721 | return self.color | ||
722 | |||
723 | |||
724 | def draw_cuml_graph(ctx, proc_tree, chart_bounds, duration, sec_w, stat_type): | ||
725 | global palette_idx | ||
726 | palette_idx = 0 | ||
727 | |||
728 | time_hash = {} | ||
729 | total_time = 0.0 | ||
730 | m_proc_list = {} | ||
731 | |||
732 | if stat_type is STAT_TYPE_CPU: | ||
733 | sample_value = 'cpu' | ||
734 | else: | ||
735 | sample_value = 'io' | ||
736 | for proc in proc_tree.process_list: | ||
737 | if elide_bootchart(proc): | ||
738 | continue | ||
739 | |||
740 | for sample in proc.samples: | ||
741 | total_time += getattr(sample.cpu_sample, sample_value) | ||
742 | if not sample.time in time_hash: | ||
743 | time_hash[sample.time] = 1 | ||
744 | |||
745 | # merge pids with the same cmd | ||
746 | if not proc.cmd in m_proc_list: | ||
747 | m_proc_list[proc.cmd] = CumlSample (proc) | ||
748 | continue | ||
749 | s = m_proc_list[proc.cmd] | ||
750 | s.merge_samples (proc) | ||
751 | |||
752 | # all the sample times | ||
753 | times = sorted(time_hash) | ||
754 | if len (times) < 2: | ||
755 | print("degenerate boot chart") | ||
756 | return | ||
757 | |||
758 | pix_per_ns = chart_bounds[3] / total_time | ||
759 | # print "total time: %g pix-per-ns %g" % (total_time, pix_per_ns) | ||
760 | |||
761 | # FIXME: we have duplicates in the process list too [!] - why !? | ||
762 | |||
763 | # Render bottom up, left to right | ||
764 | below = {} | ||
765 | for time in times: | ||
766 | below[time] = chart_bounds[1] + chart_bounds[3] | ||
767 | |||
768 | # same colors each time we render | ||
769 | random.seed (0) | ||
770 | |||
771 | ctx.set_line_width(1) | ||
772 | |||
773 | legends = [] | ||
774 | labels = [] | ||
775 | |||
776 | # render each pid in order | ||
777 | for cs in m_proc_list.values(): | ||
778 | row = {} | ||
779 | cuml = 0.0 | ||
780 | |||
781 | # print "pid : %s -> %g samples %d" % (proc.cmd, cuml, len (cs.samples)) | ||
782 | for sample in cs.samples: | ||
783 | cuml += getattr(sample.cpu_sample, sample_value) | ||
784 | row[sample.time] = cuml | ||
785 | |||
786 | process_total_time = cuml | ||
787 | |||
788 | # hide really tiny processes | ||
789 | if cuml * pix_per_ns <= 2: | ||
790 | continue | ||
791 | |||
792 | last_time = times[0] | ||
793 | y = last_below = below[last_time] | ||
794 | last_cuml = cuml = 0.0 | ||
795 | |||
796 | ctx.set_source_rgba(*cs.get_color()) | ||
797 | for time in times: | ||
798 | render_seg = False | ||
799 | |||
800 | # did the underlying trend increase ? | ||
801 | if below[time] != last_below: | ||
802 | last_below = below[last_time] | ||
803 | last_cuml = cuml | ||
804 | render_seg = True | ||
805 | |||
806 | # did we move up a pixel increase ? | ||
807 | if time in row: | ||
808 | nc = round (row[time] * pix_per_ns) | ||
809 | if nc != cuml: | ||
810 | last_cuml = cuml | ||
811 | cuml = nc | ||
812 | render_seg = True | ||
813 | |||
814 | # if last_cuml > cuml: | ||
815 | # assert fail ... - un-sorted process samples | ||
816 | |||
817 | # draw the trailing rectangle from the last time to | ||
818 | # before now, at the height of the last segment. | ||
819 | if render_seg: | ||
820 | w = math.ceil ((time - last_time) * chart_bounds[2] / proc_tree.duration) + 1 | ||
821 | x = chart_bounds[0] + round((last_time - proc_tree.start_time) * chart_bounds[2] / proc_tree.duration) | ||
822 | ctx.rectangle (x, below[last_time] - last_cuml, w, last_cuml) | ||
823 | ctx.fill() | ||
824 | # ctx.stroke() | ||
825 | last_time = time | ||
826 | y = below [time] - cuml | ||
827 | |||
828 | row[time] = y | ||
829 | |||
830 | # render the last segment | ||
831 | x = chart_bounds[0] + round((last_time - proc_tree.start_time) * chart_bounds[2] / proc_tree.duration) | ||
832 | y = below[last_time] - cuml | ||
833 | ctx.rectangle (x, y, chart_bounds[2] - x, cuml) | ||
834 | ctx.fill() | ||
835 | # ctx.stroke() | ||
836 | |||
837 | # render legend if it will fit | ||
838 | if cuml > 8: | ||
839 | label = cs.cmd | ||
840 | extnts = ctx.text_extents(label) | ||
841 | label_w = extnts[2] | ||
842 | label_h = extnts[3] | ||
843 | # print "Text extents %g by %g" % (label_w, label_h) | ||
844 | labels.append((label, | ||
845 | chart_bounds[0] + chart_bounds[2] - label_w - off_x * 2, | ||
846 | y + (cuml + label_h) / 2)) | ||
847 | if cs in legends: | ||
848 | print("ARGH - duplicate process in list !") | ||
849 | |||
850 | legends.append ((cs, process_total_time)) | ||
851 | |||
852 | below = row | ||
853 | |||
854 | # render grid-lines over the top | ||
855 | draw_box_ticks(ctx, chart_bounds, sec_w) | ||
856 | |||
857 | # render labels | ||
858 | for l in labels: | ||
859 | draw_text(ctx, l[0], TEXT_COLOR, l[1], l[2]) | ||
860 | |||
861 | # Render legends | ||
862 | font_height = 20 | ||
863 | label_width = 300 | ||
864 | LEGENDS_PER_COL = 15 | ||
865 | LEGENDS_TOTAL = 45 | ||
866 | ctx.set_font_size (TITLE_FONT_SIZE) | ||
867 | dur_secs = duration / 100 | ||
868 | cpu_secs = total_time / 1000000000 | ||
869 | |||
870 | # misleading - with multiple CPUs ... | ||
871 | # idle = ((dur_secs - cpu_secs) / dur_secs) * 100.0 | ||
872 | if stat_type is STAT_TYPE_CPU: | ||
873 | label = "Cumulative CPU usage, by process; total CPU: " \ | ||
874 | " %.5g(s) time: %.3g(s)" % (cpu_secs, dur_secs) | ||
875 | else: | ||
876 | label = "Cumulative I/O usage, by process; total I/O: " \ | ||
877 | " %.5g(s) time: %.3g(s)" % (cpu_secs, dur_secs) | ||
878 | |||
879 | draw_text(ctx, label, TEXT_COLOR, chart_bounds[0] + off_x, | ||
880 | chart_bounds[1] + font_height) | ||
881 | |||
882 | i = 0 | ||
883 | legends = sorted(legends, key=itemgetter(1), reverse=True) | ||
884 | ctx.set_font_size(TEXT_FONT_SIZE) | ||
885 | for t in legends: | ||
886 | cs = t[0] | ||
887 | time = t[1] | ||
888 | x = chart_bounds[0] + off_x + int (i/LEGENDS_PER_COL) * label_width | ||
889 | y = chart_bounds[1] + font_height * ((i % LEGENDS_PER_COL) + 2) | ||
890 | str = "%s - %.0f(ms) (%2.2f%%)" % (cs.cmd, time/1000000, (time/total_time) * 100.0) | ||
891 | draw_legend_box(ctx, str, cs.color, x, y, leg_s) | ||
892 | i = i + 1 | ||
893 | if i >= LEGENDS_TOTAL: | ||
894 | break | ||