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