blob: a13df3a3fa91c94f9a61966be06e78de0c3169a3 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001# 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
17import cairo
18import math
19import re
20import random
21import colorsys
Brad Bishopc342db32019-05-15 21:57:59 -040022import functools
Patrick Williamsc124f4f2015-09-15 14:41:29 -050023from operator import itemgetter
24
25class RenderOptions:
26
Brad Bishopc342db32019-05-15 21:57:59 -040027 def __init__(self, app_options):
28 # should we render a cumulative CPU time chart
29 self.cumulative = True
30 self.charts = True
31 self.kernel_only = False
32 self.app_options = app_options
Patrick Williamsc124f4f2015-09-15 14:41:29 -050033
Brad Bishopc342db32019-05-15 21:57:59 -040034 def proc_tree (self, trace):
35 if self.kernel_only:
36 return trace.kernel_tree
37 else:
38 return trace.proc_tree
Patrick Williamsc124f4f2015-09-15 14:41:29 -050039
40# Process tree background color.
41BACK_COLOR = (1.0, 1.0, 1.0, 1.0)
42
43WHITE = (1.0, 1.0, 1.0, 1.0)
44# Process tree border color.
45BORDER_COLOR = (0.63, 0.63, 0.63, 1.0)
46# Second tick line color.
47TICK_COLOR = (0.92, 0.92, 0.92, 1.0)
48# 5-second tick line color.
49TICK_COLOR_BOLD = (0.86, 0.86, 0.86, 1.0)
50# Annotation colour
51ANNOTATION_COLOR = (0.63, 0.0, 0.0, 0.5)
52# Text color.
53TEXT_COLOR = (0.0, 0.0, 0.0, 1.0)
54
55# Font family
56FONT_NAME = "Bitstream Vera Sans"
57# Title text font.
58TITLE_FONT_SIZE = 18
59# Default text font.
60TEXT_FONT_SIZE = 12
61# Axis label font.
62AXIS_FONT_SIZE = 11
63# Legend font.
64LEGEND_FONT_SIZE = 12
65
66# CPU load chart color.
67CPU_COLOR = (0.40, 0.55, 0.70, 1.0)
68# IO wait chart color.
69IO_COLOR = (0.76, 0.48, 0.48, 0.5)
70# Disk throughput color.
71DISK_TPUT_COLOR = (0.20, 0.71, 0.20, 1.0)
72# CPU load chart color.
73FILE_OPEN_COLOR = (0.20, 0.71, 0.71, 1.0)
74# Mem cached color
75MEM_CACHED_COLOR = CPU_COLOR
76# Mem used color
77MEM_USED_COLOR = IO_COLOR
78# Buffers color
79MEM_BUFFERS_COLOR = (0.4, 0.4, 0.4, 0.3)
80# Swap color
81MEM_SWAP_COLOR = DISK_TPUT_COLOR
82
Andrew Geissler615f2f12022-07-15 14:00:58 -050083# avg10 CPU pressure color
84CPU_PRESSURE_AVG10_COLOR = (0.0, 0.0, 0.0, 1.0)
85# delta total CPU pressure color
86CPU_PRESSURE_TOTAL_COLOR = CPU_COLOR
87# avg10 IO pressure color
88IO_PRESSURE_AVG10_COLOR = (0.0, 0.0, 0.0, 1.0)
89# delta total IO pressure color
90IO_PRESSURE_TOTAL_COLOR = IO_COLOR
91
92
93
94
Patrick Williamsc124f4f2015-09-15 14:41:29 -050095# Process border color.
96PROC_BORDER_COLOR = (0.71, 0.71, 0.71, 1.0)
97# Waiting process color.
98PROC_COLOR_D = (0.76, 0.48, 0.48, 0.5)
99# Running process color.
100PROC_COLOR_R = CPU_COLOR
101# Sleeping process color.
102PROC_COLOR_S = (0.94, 0.94, 0.94, 1.0)
103# Stopped process color.
104PROC_COLOR_T = (0.94, 0.50, 0.50, 1.0)
105# Zombie process color.
106PROC_COLOR_Z = (0.71, 0.71, 0.71, 1.0)
107# Dead process color.
108PROC_COLOR_X = (0.71, 0.71, 0.71, 0.125)
109# Paging process color.
110PROC_COLOR_W = (0.71, 0.71, 0.71, 0.125)
111
112# Process label color.
113PROC_TEXT_COLOR = (0.19, 0.19, 0.19, 1.0)
114# Process label font.
115PROC_TEXT_FONT_SIZE = 12
116
117# Signature color.
118SIG_COLOR = (0.0, 0.0, 0.0, 0.3125)
119# Signature font.
120SIG_FONT_SIZE = 14
121# Signature text.
122SIGNATURE = "http://github.com/mmeeks/bootchart"
123
124# Process dependency line color.
125DEP_COLOR = (0.75, 0.75, 0.75, 1.0)
126# Process dependency line stroke.
127DEP_STROKE = 1.0
128
129# Process description date format.
130DESC_TIME_FORMAT = "mm:ss.SSS"
131
132# Cumulative coloring bits
133HSV_MAX_MOD = 31
134HSV_STEP = 7
135
136# Configure task color
137TASK_COLOR_CONFIGURE = (1.0, 1.0, 0.00, 1.0)
138# Compile task color.
139TASK_COLOR_COMPILE = (0.0, 1.00, 0.00, 1.0)
140# Install task color
141TASK_COLOR_INSTALL = (1.0, 0.00, 1.00, 1.0)
142# Sysroot task color
143TASK_COLOR_SYSROOT = (0.0, 0.00, 1.00, 1.0)
144# Package task color
145TASK_COLOR_PACKAGE = (0.0, 1.00, 1.00, 1.0)
146# Package Write RPM/DEB/IPK task color
147TASK_COLOR_PACKAGE_WRITE = (0.0, 0.50, 0.50, 1.0)
148
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500149# Distinct colors used for different disk volumnes.
150# If we have more volumns, colors get re-used.
151VOLUME_COLORS = [
Brad Bishopc342db32019-05-15 21:57:59 -0400152 (1.0, 1.0, 0.00, 1.0),
153 (0.0, 1.00, 0.00, 1.0),
154 (1.0, 0.00, 1.00, 1.0),
155 (0.0, 0.00, 1.00, 1.0),
156 (0.0, 1.00, 1.00, 1.0),
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500157]
158
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500159# Process states
160STATE_UNDEFINED = 0
161STATE_RUNNING = 1
162STATE_SLEEPING = 2
163STATE_WAITING = 3
164STATE_STOPPED = 4
165STATE_ZOMBIE = 5
166
167STATE_COLORS = [(0, 0, 0, 0), PROC_COLOR_R, PROC_COLOR_S, PROC_COLOR_D, \
Brad Bishopc342db32019-05-15 21:57:59 -0400168 PROC_COLOR_T, PROC_COLOR_Z, PROC_COLOR_X, PROC_COLOR_W]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500169
170# CumulativeStats Types
171STAT_TYPE_CPU = 0
172STAT_TYPE_IO = 1
173
174# Convert ps process state to an int
175def get_proc_state(flag):
Brad Bishopc342db32019-05-15 21:57:59 -0400176 return "RSDTZXW".find(flag) + 1
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500177
178def draw_text(ctx, text, color, x, y):
Brad Bishopc342db32019-05-15 21:57:59 -0400179 ctx.set_source_rgba(*color)
180 ctx.move_to(x, y)
181 ctx.show_text(text)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500182
183def draw_fill_rect(ctx, color, rect):
Brad Bishopc342db32019-05-15 21:57:59 -0400184 ctx.set_source_rgba(*color)
185 ctx.rectangle(*rect)
186 ctx.fill()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500187
188def draw_rect(ctx, color, rect):
Brad Bishopc342db32019-05-15 21:57:59 -0400189 ctx.set_source_rgba(*color)
190 ctx.rectangle(*rect)
191 ctx.stroke()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500192
193def draw_legend_box(ctx, label, fill_color, x, y, s):
Brad Bishopc342db32019-05-15 21:57:59 -0400194 draw_fill_rect(ctx, fill_color, (x, y - s, s, s))
195 draw_rect(ctx, PROC_BORDER_COLOR, (x, y - s, s, s))
196 draw_text(ctx, label, TEXT_COLOR, x + s + 5, y)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500197
198def draw_legend_line(ctx, label, fill_color, x, y, s):
Brad Bishopc342db32019-05-15 21:57:59 -0400199 draw_fill_rect(ctx, fill_color, (x, y - s/2, s + 1, 3))
200 ctx.arc(x + (s + 1)/2.0, y - (s - 3)/2.0, 2.5, 0, 2.0 * math.pi)
201 ctx.fill()
202 draw_text(ctx, label, TEXT_COLOR, x + s + 5, y)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500203
204def draw_label_in_box(ctx, color, label, x, y, w, maxx):
Brad Bishopc342db32019-05-15 21:57:59 -0400205 label_w = ctx.text_extents(label)[2]
206 label_x = x + w / 2 - label_w / 2
207 if label_w + 10 > w:
208 label_x = x + w + 5
209 if label_x + label_w > maxx:
210 label_x = x - label_w - 5
211 draw_text(ctx, label, color, label_x, y)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500212
213def draw_sec_labels(ctx, options, rect, sec_w, nsecs):
Brad Bishopc342db32019-05-15 21:57:59 -0400214 ctx.set_font_size(AXIS_FONT_SIZE)
215 prev_x = 0
216 for i in range(0, rect[2] + 1, sec_w):
217 if ((i / sec_w) % nsecs == 0) :
218 if options.app_options.as_minutes :
219 label = "%.1f" % (i / sec_w / 60.0)
220 else :
221 label = "%d" % (i / sec_w)
222 label_w = ctx.text_extents(label)[2]
223 x = rect[0] + i - label_w/2
224 if x >= prev_x:
225 draw_text(ctx, label, TEXT_COLOR, x, rect[1] - 2)
226 prev_x = x + label_w
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500227
228def draw_box_ticks(ctx, rect, sec_w):
Brad Bishopc342db32019-05-15 21:57:59 -0400229 draw_rect(ctx, BORDER_COLOR, tuple(rect))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500230
Brad Bishopc342db32019-05-15 21:57:59 -0400231 ctx.set_line_cap(cairo.LINE_CAP_SQUARE)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500232
Brad Bishopc342db32019-05-15 21:57:59 -0400233 for i in range(sec_w, rect[2] + 1, sec_w):
234 if ((i / sec_w) % 10 == 0) :
235 ctx.set_line_width(1.5)
236 elif sec_w < 5 :
237 continue
238 else :
239 ctx.set_line_width(1.0)
240 if ((i / sec_w) % 30 == 0) :
241 ctx.set_source_rgba(*TICK_COLOR_BOLD)
242 else :
243 ctx.set_source_rgba(*TICK_COLOR)
244 ctx.move_to(rect[0] + i, rect[1] + 1)
245 ctx.line_to(rect[0] + i, rect[1] + rect[3] - 1)
246 ctx.stroke()
247 ctx.set_line_width(1.0)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500248
Brad Bishopc342db32019-05-15 21:57:59 -0400249 ctx.set_line_cap(cairo.LINE_CAP_BUTT)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500250
251def draw_annotations(ctx, proc_tree, times, rect):
252 ctx.set_line_cap(cairo.LINE_CAP_SQUARE)
253 ctx.set_source_rgba(*ANNOTATION_COLOR)
254 ctx.set_dash([4, 4])
255
256 for time in times:
257 if time is not None:
258 x = ((time - proc_tree.start_time) * rect[2] / proc_tree.duration)
259
260 ctx.move_to(rect[0] + x, rect[1] + 1)
261 ctx.line_to(rect[0] + x, rect[1] + rect[3] - 1)
262 ctx.stroke()
263
264 ctx.set_line_cap(cairo.LINE_CAP_BUTT)
265 ctx.set_dash([])
266
267def draw_chart(ctx, color, fill, chart_bounds, data, proc_tree, data_range):
Brad Bishopc342db32019-05-15 21:57:59 -0400268 ctx.set_line_width(0.5)
269 x_shift = proc_tree.start_time
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500270
Brad Bishopc342db32019-05-15 21:57:59 -0400271 def transform_point_coords(point, x_base, y_base, \
272 xscale, yscale, x_trans, y_trans):
273 x = (point[0] - x_base) * xscale + x_trans
274 y = (point[1] - y_base) * -yscale + y_trans + chart_bounds[3]
275 return x, y
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500276
Brad Bishopc342db32019-05-15 21:57:59 -0400277 max_x = max (x for (x, y) in data)
278 max_y = max (y for (x, y) in data)
279 # avoid divide by zero
280 if max_y == 0:
281 max_y = 1.0
Andrew Geissler5199d832021-09-24 16:47:35 -0500282 if (max_x - x_shift):
283 xscale = float (chart_bounds[2]) / (max_x - x_shift)
284 else:
285 xscale = float (chart_bounds[2])
Brad Bishopc342db32019-05-15 21:57:59 -0400286 # If data_range is given, scale the chart so that the value range in
287 # data_range matches the chart bounds exactly.
288 # Otherwise, scale so that the actual data matches the chart bounds.
Andrew Geisslerc926e172021-05-07 16:11:35 -0500289 if data_range and (data_range[1] - data_range[0]):
Brad Bishopc342db32019-05-15 21:57:59 -0400290 yscale = float(chart_bounds[3]) / (data_range[1] - data_range[0])
291 ybase = data_range[0]
292 else:
293 yscale = float(chart_bounds[3]) / max_y
294 ybase = 0
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500295
Brad Bishopc342db32019-05-15 21:57:59 -0400296 first = transform_point_coords (data[0], x_shift, ybase, xscale, yscale, \
297 chart_bounds[0], chart_bounds[1])
298 last = transform_point_coords (data[-1], x_shift, ybase, xscale, yscale, \
299 chart_bounds[0], chart_bounds[1])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500300
Brad Bishopc342db32019-05-15 21:57:59 -0400301 ctx.set_source_rgba(*color)
302 ctx.move_to(*first)
303 for point in data:
304 x, y = transform_point_coords (point, x_shift, ybase, xscale, yscale, \
305 chart_bounds[0], chart_bounds[1])
306 ctx.line_to(x, y)
307 if fill:
308 ctx.stroke_preserve()
309 ctx.line_to(last[0], chart_bounds[1]+chart_bounds[3])
310 ctx.line_to(first[0], chart_bounds[1]+chart_bounds[3])
311 ctx.line_to(first[0], first[1])
312 ctx.fill()
313 else:
314 ctx.stroke()
315 ctx.set_line_width(1.0)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500316
317bar_h = 55
318meminfo_bar_h = 2 * bar_h
319header_h = 60
320# offsets
321off_x, off_y = 220, 10
322sec_w_base = 1 # the width of a second
323proc_h = 16 # the height of a process
324leg_s = 10
325MIN_IMG_W = 800
Andrew Geissler82c905d2020-04-13 13:39:40 -0500326CUML_HEIGHT = 2000 # Increased value to accommodate CPU and I/O Graphs
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500327OPTIONS = None
328
329def extents(options, xscale, trace):
Brad Bishopc342db32019-05-15 21:57:59 -0400330 start = min(trace.start.keys())
331 end = start
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500332
Brad Bishopc342db32019-05-15 21:57:59 -0400333 processes = 0
334 for proc in trace.processes:
335 if not options.app_options.show_all and \
336 trace.processes[proc][1] - trace.processes[proc][0] < options.app_options.mintime:
337 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500338
Brad Bishopc342db32019-05-15 21:57:59 -0400339 if trace.processes[proc][1] > end:
340 end = trace.processes[proc][1]
341 processes += 1
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500342
Brad Bishopc342db32019-05-15 21:57:59 -0400343 if trace.min is not None and trace.max is not None:
344 start = trace.min
345 end = trace.max
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500346
Brad Bishopc342db32019-05-15 21:57:59 -0400347 w = int ((end - start) * sec_w_base * xscale) + 2 * off_x
348 h = proc_h * processes + header_h + 2 * off_y
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500349
Brad Bishopc342db32019-05-15 21:57:59 -0400350 if options.charts:
351 if trace.cpu_stats:
352 h += 30 + bar_h
353 if trace.disk_stats:
354 h += 30 + bar_h
355 if trace.monitor_disk:
356 h += 30 + bar_h
357 if trace.mem_stats:
358 h += meminfo_bar_h
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500359
Brad Bishopc342db32019-05-15 21:57:59 -0400360 # Allow for width of process legend and offset
361 if w < (720 + off_x):
362 w = 720 + off_x
363
364 return (w, h)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500365
366def clip_visible(clip, rect):
Brad Bishopc342db32019-05-15 21:57:59 -0400367 xmax = max (clip[0], rect[0])
368 ymax = max (clip[1], rect[1])
369 xmin = min (clip[0] + clip[2], rect[0] + rect[2])
370 ymin = min (clip[1] + clip[3], rect[1] + rect[3])
371 return (xmin > xmax and ymin > ymax)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500372
373def render_charts(ctx, options, clip, trace, curr_y, w, h, sec_w):
Brad Bishopc342db32019-05-15 21:57:59 -0400374 proc_tree = options.proc_tree(trace)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500375
Brad Bishopc342db32019-05-15 21:57:59 -0400376 # render bar legend
377 if trace.cpu_stats:
378 ctx.set_font_size(LEGEND_FONT_SIZE)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500379
Brad Bishopc342db32019-05-15 21:57:59 -0400380 draw_legend_box(ctx, "CPU (user+sys)", CPU_COLOR, off_x, curr_y+20, leg_s)
381 draw_legend_box(ctx, "I/O (wait)", IO_COLOR, off_x + 120, curr_y+20, leg_s)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500382
Brad Bishopc342db32019-05-15 21:57:59 -0400383 # render I/O wait
384 chart_rect = (off_x, curr_y+30, w, bar_h)
385 if clip_visible (clip, chart_rect):
386 draw_box_ticks (ctx, chart_rect, sec_w)
387 draw_annotations (ctx, proc_tree, trace.times, chart_rect)
388 draw_chart (ctx, IO_COLOR, True, chart_rect, \
389 [(sample.time, sample.user + sample.sys + sample.io) for sample in trace.cpu_stats], \
390 proc_tree, None)
391 # render CPU load
392 draw_chart (ctx, CPU_COLOR, True, chart_rect, \
393 [(sample.time, sample.user + sample.sys) for sample in trace.cpu_stats], \
394 proc_tree, None)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500395
Brad Bishopc342db32019-05-15 21:57:59 -0400396 curr_y = curr_y + 30 + bar_h
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500397
Brad Bishopc342db32019-05-15 21:57:59 -0400398 # render second chart
399 if trace.disk_stats:
400 draw_legend_line(ctx, "Disk throughput", DISK_TPUT_COLOR, off_x, curr_y+20, leg_s)
401 draw_legend_box(ctx, "Disk utilization", IO_COLOR, off_x + 120, curr_y+20, leg_s)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500402
Brad Bishopc342db32019-05-15 21:57:59 -0400403 # render I/O utilization
404 chart_rect = (off_x, curr_y+30, w, bar_h)
405 if clip_visible (clip, chart_rect):
406 draw_box_ticks (ctx, chart_rect, sec_w)
407 draw_annotations (ctx, proc_tree, trace.times, chart_rect)
408 draw_chart (ctx, IO_COLOR, True, chart_rect, \
409 [(sample.time, sample.util) for sample in trace.disk_stats], \
410 proc_tree, None)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500411
Brad Bishopc342db32019-05-15 21:57:59 -0400412 # render disk throughput
413 max_sample = max (trace.disk_stats, key = lambda s: s.tput)
414 if clip_visible (clip, chart_rect):
415 draw_chart (ctx, DISK_TPUT_COLOR, False, chart_rect, \
416 [(sample.time, sample.tput) for sample in trace.disk_stats], \
417 proc_tree, None)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500418
Brad Bishopc342db32019-05-15 21:57:59 -0400419 pos_x = off_x + ((max_sample.time - proc_tree.start_time) * w / proc_tree.duration)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500420
Brad Bishopc342db32019-05-15 21:57:59 -0400421 shift_x, shift_y = -20, 20
422 if (pos_x < off_x + 245):
423 shift_x, shift_y = 5, 40
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500424
Brad Bishopc342db32019-05-15 21:57:59 -0400425 label = "%dMB/s" % round ((max_sample.tput) / 1024.0)
426 draw_text (ctx, label, DISK_TPUT_COLOR, pos_x + shift_x, curr_y + shift_y)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500427
Brad Bishopc342db32019-05-15 21:57:59 -0400428 curr_y = curr_y + 30 + bar_h
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500429
Andrew Geissler615f2f12022-07-15 14:00:58 -0500430 # render CPU pressure chart
431 if trace.cpu_pressure:
432 draw_legend_line(ctx, "avg10 CPU Pressure", CPU_PRESSURE_AVG10_COLOR, off_x, curr_y+20, leg_s)
433 draw_legend_box(ctx, "delta total CPU Pressure", CPU_PRESSURE_TOTAL_COLOR, off_x + 140, curr_y+20, leg_s)
434
435 # render delta total cpu
436 chart_rect = (off_x, curr_y+30, w, bar_h)
437 if clip_visible (clip, chart_rect):
438 draw_box_ticks (ctx, chart_rect, sec_w)
439 draw_annotations (ctx, proc_tree, trace.times, chart_rect)
440 draw_chart (ctx, CPU_PRESSURE_TOTAL_COLOR, True, chart_rect, \
441 [(sample.time, sample.deltaTotal) for sample in trace.cpu_pressure], \
442 proc_tree, None)
443
444 # render avg10 cpu
445 max_sample = max (trace.cpu_pressure, key = lambda s: s.avg10)
446 if clip_visible (clip, chart_rect):
447 draw_chart (ctx, CPU_PRESSURE_AVG10_COLOR, False, chart_rect, \
448 [(sample.time, sample.avg10) for sample in trace.cpu_pressure], \
449 proc_tree, None)
450
451 pos_x = off_x + ((max_sample.time - proc_tree.start_time) * w / proc_tree.duration)
452
453 shift_x, shift_y = -20, 20
454 if (pos_x < off_x + 245):
455 shift_x, shift_y = 5, 40
456
457
458 label = "%d%%" % (max_sample.avg10)
459 draw_text (ctx, label, CPU_PRESSURE_AVG10_COLOR, pos_x + shift_x, curr_y + shift_y)
460
461 curr_y = curr_y + 30 + bar_h
462
463 # render delta total io
464 if trace.io_pressure:
465 draw_legend_line(ctx, "avg10 I/O Pressure", IO_PRESSURE_AVG10_COLOR, off_x, curr_y+20, leg_s)
466 draw_legend_box(ctx, "delta total I/O Pressure", IO_PRESSURE_TOTAL_COLOR, off_x + 140, curr_y+20, leg_s)
467
468 # render avg10 io
469 chart_rect = (off_x, curr_y+30, w, bar_h)
470 if clip_visible (clip, chart_rect):
471 draw_box_ticks (ctx, chart_rect, sec_w)
472 draw_annotations (ctx, proc_tree, trace.times, chart_rect)
473 draw_chart (ctx, IO_PRESSURE_TOTAL_COLOR, True, chart_rect, \
474 [(sample.time, sample.deltaTotal) for sample in trace.io_pressure], \
475 proc_tree, None)
476
477 # render io pressure
478 max_sample = max (trace.io_pressure, key = lambda s: s.avg10)
479 if clip_visible (clip, chart_rect):
480 draw_chart (ctx, IO_PRESSURE_AVG10_COLOR, False, chart_rect, \
481 [(sample.time, sample.avg10) for sample in trace.io_pressure], \
482 proc_tree, None)
483
484 pos_x = off_x + ((max_sample.time - proc_tree.start_time) * w / proc_tree.duration)
485
486 shift_x, shift_y = -20, 20
487 if (pos_x < off_x + 245):
488 shift_x, shift_y = 5, 40
489
490 label = "%d%%" % (max_sample.avg10)
491 draw_text (ctx, label, IO_PRESSURE_AVG10_COLOR, pos_x + shift_x, curr_y + shift_y)
492
493 curr_y = curr_y + 30 + bar_h
494
Brad Bishopc342db32019-05-15 21:57:59 -0400495 # render disk space usage
496 #
497 # Draws the amount of disk space used on each volume relative to the
498 # lowest recorded amount. The graphs for each volume are stacked above
499 # each other so that total disk usage is visible.
500 if trace.monitor_disk:
501 ctx.set_font_size(LEGEND_FONT_SIZE)
502 # Determine set of volumes for which we have
503 # information and the minimal amount of used disk
504 # space for each. Currently samples are allowed to
505 # not have a values for all volumes; drawing could be
506 # made more efficient if that wasn't the case.
507 volumes = set()
508 min_used = {}
509 for sample in trace.monitor_disk:
510 for volume, used in sample.records.items():
511 volumes.add(volume)
512 if volume not in min_used or min_used[volume] > used:
513 min_used[volume] = used
514 volumes = sorted(list(volumes))
515 disk_scale = 0
516 for i, volume in enumerate(volumes):
517 volume_scale = max([sample.records[volume] - min_used[volume]
518 for sample in trace.monitor_disk
519 if volume in sample.records])
520 # Does not take length of volume name into account, but fixed offset
521 # works okay in practice.
522 draw_legend_box(ctx, '%s (max: %u MiB)' % (volume, volume_scale / 1024 / 1024),
523 VOLUME_COLORS[i % len(VOLUME_COLORS)],
524 off_x + i * 250, curr_y+20, leg_s)
525 disk_scale += volume_scale
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500526
Brad Bishopc342db32019-05-15 21:57:59 -0400527 # render used amount of disk space
528 chart_rect = (off_x, curr_y+30, w, bar_h)
529 if clip_visible (clip, chart_rect):
530 draw_box_ticks (ctx, chart_rect, sec_w)
531 draw_annotations (ctx, proc_tree, trace.times, chart_rect)
532 for i in range(len(volumes), 0, -1):
533 draw_chart (ctx, VOLUME_COLORS[(i - 1) % len(VOLUME_COLORS)], True, chart_rect, \
534 [(sample.time,
535 # Sum up used space of all volumes including the current one
536 # so that the graphs appear as stacked on top of each other.
537 functools.reduce(lambda x,y: x+y,
538 [sample.records[volume] - min_used[volume]
539 for volume in volumes[0:i]
540 if volume in sample.records],
541 0))
542 for sample in trace.monitor_disk], \
543 proc_tree, [0, disk_scale])
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500544
Brad Bishopc342db32019-05-15 21:57:59 -0400545 curr_y = curr_y + 30 + bar_h
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500546
Brad Bishopc342db32019-05-15 21:57:59 -0400547 # render mem usage
548 chart_rect = (off_x, curr_y+30, w, meminfo_bar_h)
549 mem_stats = trace.mem_stats
550 if mem_stats and clip_visible (clip, chart_rect):
551 mem_scale = max(sample.buffers for sample in mem_stats)
552 draw_legend_box(ctx, "Mem cached (scale: %u MiB)" % (float(mem_scale) / 1024), MEM_CACHED_COLOR, off_x, curr_y+20, leg_s)
553 draw_legend_box(ctx, "Used", MEM_USED_COLOR, off_x + 240, curr_y+20, leg_s)
554 draw_legend_box(ctx, "Buffers", MEM_BUFFERS_COLOR, off_x + 360, curr_y+20, leg_s)
555 draw_legend_line(ctx, "Swap (scale: %u MiB)" % max([(sample.swap)/1024 for sample in mem_stats]), \
556 MEM_SWAP_COLOR, off_x + 480, curr_y+20, leg_s)
557 draw_box_ticks(ctx, chart_rect, sec_w)
558 draw_annotations(ctx, proc_tree, trace.times, chart_rect)
559 draw_chart(ctx, MEM_BUFFERS_COLOR, True, chart_rect, \
560 [(sample.time, sample.buffers) for sample in trace.mem_stats], \
561 proc_tree, [0, mem_scale])
562 draw_chart(ctx, MEM_USED_COLOR, True, chart_rect, \
563 [(sample.time, sample.used) for sample in mem_stats], \
564 proc_tree, [0, mem_scale])
565 draw_chart(ctx, MEM_CACHED_COLOR, True, chart_rect, \
566 [(sample.time, sample.cached) for sample in mem_stats], \
567 proc_tree, [0, mem_scale])
568 draw_chart(ctx, MEM_SWAP_COLOR, False, chart_rect, \
569 [(sample.time, float(sample.swap)) for sample in mem_stats], \
570 proc_tree, None)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500571
Brad Bishopc342db32019-05-15 21:57:59 -0400572 curr_y = curr_y + meminfo_bar_h
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500573
Brad Bishopc342db32019-05-15 21:57:59 -0400574 return curr_y
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500575
576def render_processes_chart(ctx, options, trace, curr_y, w, h, sec_w):
Brad Bishopc342db32019-05-15 21:57:59 -0400577 chart_rect = [off_x, curr_y+header_h, w, h - curr_y - 1 * off_y - header_h ]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500578
Brad Bishopc342db32019-05-15 21:57:59 -0400579 draw_legend_box (ctx, "Configure", \
580 TASK_COLOR_CONFIGURE, off_x , curr_y + 45, leg_s)
581 draw_legend_box (ctx, "Compile", \
582 TASK_COLOR_COMPILE, off_x+120, curr_y + 45, leg_s)
583 draw_legend_box (ctx, "Install", \
584 TASK_COLOR_INSTALL, off_x+240, curr_y + 45, leg_s)
585 draw_legend_box (ctx, "Populate Sysroot", \
586 TASK_COLOR_SYSROOT, off_x+360, curr_y + 45, leg_s)
587 draw_legend_box (ctx, "Package", \
588 TASK_COLOR_PACKAGE, off_x+480, curr_y + 45, leg_s)
589 draw_legend_box (ctx, "Package Write", \
590 TASK_COLOR_PACKAGE_WRITE, off_x+600, curr_y + 45, leg_s)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500591
Brad Bishopc342db32019-05-15 21:57:59 -0400592 ctx.set_font_size(PROC_TEXT_FONT_SIZE)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500593
Brad Bishopc342db32019-05-15 21:57:59 -0400594 draw_box_ticks(ctx, chart_rect, sec_w)
595 draw_sec_labels(ctx, options, chart_rect, sec_w, 30)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500596
Brad Bishopc342db32019-05-15 21:57:59 -0400597 y = curr_y+header_h
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500598
Brad Bishopc342db32019-05-15 21:57:59 -0400599 offset = trace.min or min(trace.start.keys())
600 for start in sorted(trace.start.keys()):
601 for process in sorted(trace.start[start]):
602 if not options.app_options.show_all and \
603 trace.processes[process][1] - start < options.app_options.mintime:
604 continue
605 task = process.split(":")[1]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500606
Brad Bishopc342db32019-05-15 21:57:59 -0400607 #print(process)
608 #print(trace.processes[process][1])
609 #print(s)
610
611 x = chart_rect[0] + (start - offset) * sec_w
612 w = ((trace.processes[process][1] - start) * sec_w)
613
614 #print("proc at %s %s %s %s" % (x, y, w, proc_h))
615 col = None
616 if task == "do_compile":
617 col = TASK_COLOR_COMPILE
618 elif task == "do_configure":
619 col = TASK_COLOR_CONFIGURE
620 elif task == "do_install":
621 col = TASK_COLOR_INSTALL
622 elif task == "do_populate_sysroot":
623 col = TASK_COLOR_SYSROOT
624 elif task == "do_package":
625 col = TASK_COLOR_PACKAGE
626 elif task == "do_package_write_rpm" or \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500627 task == "do_package_write_deb" or \
628 task == "do_package_write_ipk":
Brad Bishopc342db32019-05-15 21:57:59 -0400629 col = TASK_COLOR_PACKAGE_WRITE
630 else:
631 col = WHITE
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500632
Brad Bishopc342db32019-05-15 21:57:59 -0400633 if col:
634 draw_fill_rect(ctx, col, (x, y, w, proc_h))
635 draw_rect(ctx, PROC_BORDER_COLOR, (x, y, w, proc_h))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500636
Brad Bishopc342db32019-05-15 21:57:59 -0400637 draw_label_in_box(ctx, PROC_TEXT_COLOR, process, x, y + proc_h - 4, w, proc_h)
638 y = y + proc_h
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500639
Brad Bishopc342db32019-05-15 21:57:59 -0400640 return curr_y
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500641
642#
643# Render the chart.
644#
645def render(ctx, options, xscale, trace):
Brad Bishopc342db32019-05-15 21:57:59 -0400646 (w, h) = extents (options, xscale, trace)
647 global OPTIONS
648 OPTIONS = options.app_options
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500649
Brad Bishopc342db32019-05-15 21:57:59 -0400650 # x, y, w, h
651 clip = ctx.clip_extents()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500652
Brad Bishopc342db32019-05-15 21:57:59 -0400653 sec_w = int (xscale * sec_w_base)
654 ctx.set_line_width(1.0)
655 ctx.select_font_face(FONT_NAME)
656 draw_fill_rect(ctx, WHITE, (0, 0, max(w, MIN_IMG_W), h))
657 w -= 2*off_x
658 curr_y = off_y;
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500659
Brad Bishopc342db32019-05-15 21:57:59 -0400660 if options.charts:
661 curr_y = render_charts (ctx, options, clip, trace, curr_y, w, h, sec_w)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500662
Brad Bishopc342db32019-05-15 21:57:59 -0400663 curr_y = render_processes_chart (ctx, options, trace, curr_y, w, h, sec_w)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500664
Brad Bishopc342db32019-05-15 21:57:59 -0400665 return
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500666
Brad Bishopc342db32019-05-15 21:57:59 -0400667 proc_tree = options.proc_tree (trace)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500668
Brad Bishopc342db32019-05-15 21:57:59 -0400669 # draw the title and headers
670 if proc_tree.idle:
671 duration = proc_tree.idle
672 else:
673 duration = proc_tree.duration
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500674
Brad Bishopc342db32019-05-15 21:57:59 -0400675 if not options.kernel_only:
676 curr_y = draw_header (ctx, trace.headers, duration)
677 else:
678 curr_y = off_y;
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500679
Brad Bishopc342db32019-05-15 21:57:59 -0400680 # draw process boxes
681 proc_height = h
682 if proc_tree.taskstats and options.cumulative:
683 proc_height -= CUML_HEIGHT
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500684
Brad Bishopc342db32019-05-15 21:57:59 -0400685 draw_process_bar_chart(ctx, clip, options, proc_tree, trace.times,
686 curr_y, w, proc_height, sec_w)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500687
Brad Bishopc342db32019-05-15 21:57:59 -0400688 curr_y = proc_height
689 ctx.set_font_size(SIG_FONT_SIZE)
690 draw_text(ctx, SIGNATURE, SIG_COLOR, off_x + 5, proc_height - 8)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500691
Brad Bishopc342db32019-05-15 21:57:59 -0400692 # draw a cumulative CPU-time-per-process graph
693 if proc_tree.taskstats and options.cumulative:
694 cuml_rect = (off_x, curr_y + off_y, w, CUML_HEIGHT/2 - off_y * 2)
695 if clip_visible (clip, cuml_rect):
696 draw_cuml_graph(ctx, proc_tree, cuml_rect, duration, sec_w, STAT_TYPE_CPU)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500697
Brad Bishopc342db32019-05-15 21:57:59 -0400698 # draw a cumulative I/O-time-per-process graph
699 if proc_tree.taskstats and options.cumulative:
700 cuml_rect = (off_x, curr_y + off_y * 100, w, CUML_HEIGHT/2 - off_y * 2)
701 if clip_visible (clip, cuml_rect):
702 draw_cuml_graph(ctx, proc_tree, cuml_rect, duration, sec_w, STAT_TYPE_IO)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500703
704def draw_process_bar_chart(ctx, clip, options, proc_tree, times, curr_y, w, h, sec_w):
Brad Bishopc342db32019-05-15 21:57:59 -0400705 header_size = 0
706 if not options.kernel_only:
707 draw_legend_box (ctx, "Running (%cpu)",
708 PROC_COLOR_R, off_x , curr_y + 45, leg_s)
709 draw_legend_box (ctx, "Unint.sleep (I/O)",
710 PROC_COLOR_D, off_x+120, curr_y + 45, leg_s)
711 draw_legend_box (ctx, "Sleeping",
712 PROC_COLOR_S, off_x+240, curr_y + 45, leg_s)
713 draw_legend_box (ctx, "Zombie",
714 PROC_COLOR_Z, off_x+360, curr_y + 45, leg_s)
715 header_size = 45
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500716
Brad Bishopc342db32019-05-15 21:57:59 -0400717 chart_rect = [off_x, curr_y + header_size + 15,
718 w, h - 2 * off_y - (curr_y + header_size + 15) + proc_h]
719 ctx.set_font_size (PROC_TEXT_FONT_SIZE)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500720
Brad Bishopc342db32019-05-15 21:57:59 -0400721 draw_box_ticks (ctx, chart_rect, sec_w)
722 if sec_w > 100:
723 nsec = 1
724 else:
725 nsec = 5
726 draw_sec_labels (ctx, options, chart_rect, sec_w, nsec)
727 draw_annotations (ctx, proc_tree, times, chart_rect)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500728
Brad Bishopc342db32019-05-15 21:57:59 -0400729 y = curr_y + 60
730 for root in proc_tree.process_tree:
731 draw_processes_recursively(ctx, root, proc_tree, y, proc_h, chart_rect, clip)
732 y = y + proc_h * proc_tree.num_nodes([root])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500733
734
735def draw_header (ctx, headers, duration):
736 toshow = [
737 ('system.uname', 'uname', lambda s: s),
738 ('system.release', 'release', lambda s: s),
739 ('system.cpu', 'CPU', lambda s: re.sub('model name\s*:\s*', '', s, 1)),
740 ('system.kernel.options', 'kernel options', lambda s: s),
741 ]
742
743 header_y = ctx.font_extents()[2] + 10
744 ctx.set_font_size(TITLE_FONT_SIZE)
745 draw_text(ctx, headers['title'], TEXT_COLOR, off_x, header_y)
746 ctx.set_font_size(TEXT_FONT_SIZE)
747
748 for (headerkey, headertitle, mangle) in toshow:
749 header_y += ctx.font_extents()[2]
750 if headerkey in headers:
751 value = headers.get(headerkey)
752 else:
753 value = ""
754 txt = headertitle + ': ' + mangle(value)
755 draw_text(ctx, txt, TEXT_COLOR, off_x, header_y)
756
757 dur = duration / 100.0
758 txt = 'time : %02d:%05.2f' % (math.floor(dur/60), dur - 60 * math.floor(dur/60))
759 if headers.get('system.maxpid') is not None:
760 txt = txt + ' max pid: %s' % (headers.get('system.maxpid'))
761
762 header_y += ctx.font_extents()[2]
763 draw_text (ctx, txt, TEXT_COLOR, off_x, header_y)
764
765 return header_y
766
767def draw_processes_recursively(ctx, proc, proc_tree, y, proc_h, rect, clip) :
Brad Bishopc342db32019-05-15 21:57:59 -0400768 x = rect[0] + ((proc.start_time - proc_tree.start_time) * rect[2] / proc_tree.duration)
769 w = ((proc.duration) * rect[2] / proc_tree.duration)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500770
Brad Bishopc342db32019-05-15 21:57:59 -0400771 draw_process_activity_colors(ctx, proc, proc_tree, x, y, w, proc_h, rect, clip)
772 draw_rect(ctx, PROC_BORDER_COLOR, (x, y, w, proc_h))
773 ipid = int(proc.pid)
774 if not OPTIONS.show_all:
775 cmdString = proc.cmd
776 else:
777 cmdString = ''
778 if (OPTIONS.show_pid or OPTIONS.show_all) and ipid is not 0:
779 cmdString = cmdString + " [" + str(ipid // 1000) + "]"
780 if OPTIONS.show_all:
781 if proc.args:
782 cmdString = cmdString + " '" + "' '".join(proc.args) + "'"
783 else:
784 cmdString = cmdString + " " + proc.exe
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500785
Brad Bishopc342db32019-05-15 21:57:59 -0400786 draw_label_in_box(ctx, PROC_TEXT_COLOR, cmdString, x, y + proc_h - 4, w, rect[0] + rect[2])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500787
Brad Bishopc342db32019-05-15 21:57:59 -0400788 next_y = y + proc_h
789 for child in proc.child_list:
790 if next_y > clip[1] + clip[3]:
791 break
792 child_x, child_y = draw_processes_recursively(ctx, child, proc_tree, next_y, proc_h, rect, clip)
793 draw_process_connecting_lines(ctx, x, y, child_x, child_y, proc_h)
794 next_y = next_y + proc_h * proc_tree.num_nodes([child])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500795
Brad Bishopc342db32019-05-15 21:57:59 -0400796 return x, y
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500797
798
799def draw_process_activity_colors(ctx, proc, proc_tree, x, y, w, proc_h, rect, clip):
800
Brad Bishopc342db32019-05-15 21:57:59 -0400801 if y > clip[1] + clip[3] or y + proc_h + 2 < clip[1]:
802 return
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500803
Brad Bishopc342db32019-05-15 21:57:59 -0400804 draw_fill_rect(ctx, PROC_COLOR_S, (x, y, w, proc_h))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500805
Brad Bishopc342db32019-05-15 21:57:59 -0400806 last_tx = -1
807 for sample in proc.samples :
808 tx = rect[0] + round(((sample.time - proc_tree.start_time) * rect[2] / proc_tree.duration))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500809
Brad Bishopc342db32019-05-15 21:57:59 -0400810 # samples are sorted chronologically
811 if tx < clip[0]:
812 continue
813 if tx > clip[0] + clip[2]:
814 break
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500815
Brad Bishopc342db32019-05-15 21:57:59 -0400816 tw = round(proc_tree.sample_period * rect[2] / float(proc_tree.duration))
817 if last_tx != -1 and abs(last_tx - tx) <= tw:
818 tw -= last_tx - tx
819 tx = last_tx
820 tw = max (tw, 1) # nice to see at least something
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500821
Brad Bishopc342db32019-05-15 21:57:59 -0400822 last_tx = tx + tw
823 state = get_proc_state( sample.state )
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500824
Brad Bishopc342db32019-05-15 21:57:59 -0400825 color = STATE_COLORS[state]
826 if state == STATE_RUNNING:
827 alpha = min (sample.cpu_sample.user + sample.cpu_sample.sys, 1.0)
828 color = tuple(list(PROC_COLOR_R[0:3]) + [alpha])
829# print "render time %d [ tx %d tw %d ], sample state %s color %s alpha %g" % (sample.time, tx, tw, state, color, alpha)
830 elif state == STATE_SLEEPING:
831 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500832
Brad Bishopc342db32019-05-15 21:57:59 -0400833 draw_fill_rect(ctx, color, (tx, y, tw, proc_h))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500834
835def draw_process_connecting_lines(ctx, px, py, x, y, proc_h):
Brad Bishopc342db32019-05-15 21:57:59 -0400836 ctx.set_source_rgba(*DEP_COLOR)
837 ctx.set_dash([2, 2])
838 if abs(px - x) < 3:
839 dep_off_x = 3
840 dep_off_y = proc_h / 4
841 ctx.move_to(x, y + proc_h / 2)
842 ctx.line_to(px - dep_off_x, y + proc_h / 2)
843 ctx.line_to(px - dep_off_x, py - dep_off_y)
844 ctx.line_to(px, py - dep_off_y)
845 else:
846 ctx.move_to(x, y + proc_h / 2)
847 ctx.line_to(px, y + proc_h / 2)
848 ctx.line_to(px, py)
849 ctx.stroke()
850 ctx.set_dash([])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500851
852# elide the bootchart collector - it is quite distorting
853def elide_bootchart(proc):
Brad Bishopc342db32019-05-15 21:57:59 -0400854 return proc.cmd == 'bootchartd' or proc.cmd == 'bootchart-colle'
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500855
856class CumlSample:
Brad Bishopc342db32019-05-15 21:57:59 -0400857 def __init__(self, proc):
858 self.cmd = proc.cmd
859 self.samples = []
860 self.merge_samples (proc)
861 self.color = None
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500862
Brad Bishopc342db32019-05-15 21:57:59 -0400863 def merge_samples(self, proc):
864 self.samples.extend (proc.samples)
865 self.samples.sort (key = lambda p: p.time)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500866
Brad Bishopc342db32019-05-15 21:57:59 -0400867 def next(self):
868 global palette_idx
869 palette_idx += HSV_STEP
870 return palette_idx
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500871
Brad Bishopc342db32019-05-15 21:57:59 -0400872 def get_color(self):
873 if self.color is None:
874 i = self.next() % HSV_MAX_MOD
875 h = 0.0
876 if i is not 0:
877 h = (1.0 * i) / HSV_MAX_MOD
878 s = 0.5
879 v = 1.0
880 c = colorsys.hsv_to_rgb (h, s, v)
881 self.color = (c[0], c[1], c[2], 1.0)
882 return self.color
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500883
884
885def draw_cuml_graph(ctx, proc_tree, chart_bounds, duration, sec_w, stat_type):
Brad Bishopc342db32019-05-15 21:57:59 -0400886 global palette_idx
887 palette_idx = 0
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500888
Brad Bishopc342db32019-05-15 21:57:59 -0400889 time_hash = {}
890 total_time = 0.0
891 m_proc_list = {}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500892
Brad Bishopc342db32019-05-15 21:57:59 -0400893 if stat_type is STAT_TYPE_CPU:
894 sample_value = 'cpu'
895 else:
896 sample_value = 'io'
897 for proc in proc_tree.process_list:
898 if elide_bootchart(proc):
899 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500900
Brad Bishopc342db32019-05-15 21:57:59 -0400901 for sample in proc.samples:
902 total_time += getattr(sample.cpu_sample, sample_value)
903 if not sample.time in time_hash:
904 time_hash[sample.time] = 1
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500905
Brad Bishopc342db32019-05-15 21:57:59 -0400906 # merge pids with the same cmd
907 if not proc.cmd in m_proc_list:
908 m_proc_list[proc.cmd] = CumlSample (proc)
909 continue
910 s = m_proc_list[proc.cmd]
911 s.merge_samples (proc)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500912
Brad Bishopc342db32019-05-15 21:57:59 -0400913 # all the sample times
914 times = sorted(time_hash)
915 if len (times) < 2:
916 print("degenerate boot chart")
917 return
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500918
Brad Bishopc342db32019-05-15 21:57:59 -0400919 pix_per_ns = chart_bounds[3] / total_time
920# print "total time: %g pix-per-ns %g" % (total_time, pix_per_ns)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500921
Brad Bishopc342db32019-05-15 21:57:59 -0400922 # FIXME: we have duplicates in the process list too [!] - why !?
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500923
Brad Bishopc342db32019-05-15 21:57:59 -0400924 # Render bottom up, left to right
925 below = {}
926 for time in times:
927 below[time] = chart_bounds[1] + chart_bounds[3]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500928
Brad Bishopc342db32019-05-15 21:57:59 -0400929 # same colors each time we render
930 random.seed (0)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500931
Brad Bishopc342db32019-05-15 21:57:59 -0400932 ctx.set_line_width(1)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500933
Brad Bishopc342db32019-05-15 21:57:59 -0400934 legends = []
935 labels = []
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500936
Brad Bishopc342db32019-05-15 21:57:59 -0400937 # render each pid in order
938 for cs in m_proc_list.values():
939 row = {}
940 cuml = 0.0
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500941
Brad Bishopc342db32019-05-15 21:57:59 -0400942 # print "pid : %s -> %g samples %d" % (proc.cmd, cuml, len (cs.samples))
943 for sample in cs.samples:
944 cuml += getattr(sample.cpu_sample, sample_value)
945 row[sample.time] = cuml
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500946
Brad Bishopc342db32019-05-15 21:57:59 -0400947 process_total_time = cuml
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500948
Brad Bishopc342db32019-05-15 21:57:59 -0400949 # hide really tiny processes
950 if cuml * pix_per_ns <= 2:
951 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500952
Brad Bishopc342db32019-05-15 21:57:59 -0400953 last_time = times[0]
954 y = last_below = below[last_time]
955 last_cuml = cuml = 0.0
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500956
Brad Bishopc342db32019-05-15 21:57:59 -0400957 ctx.set_source_rgba(*cs.get_color())
958 for time in times:
959 render_seg = False
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500960
Brad Bishopc342db32019-05-15 21:57:59 -0400961 # did the underlying trend increase ?
962 if below[time] != last_below:
963 last_below = below[last_time]
964 last_cuml = cuml
965 render_seg = True
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500966
Brad Bishopc342db32019-05-15 21:57:59 -0400967 # did we move up a pixel increase ?
968 if time in row:
969 nc = round (row[time] * pix_per_ns)
970 if nc != cuml:
971 last_cuml = cuml
972 cuml = nc
973 render_seg = True
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500974
Brad Bishopc342db32019-05-15 21:57:59 -0400975# if last_cuml > cuml:
976# assert fail ... - un-sorted process samples
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500977
Brad Bishopc342db32019-05-15 21:57:59 -0400978 # draw the trailing rectangle from the last time to
979 # before now, at the height of the last segment.
980 if render_seg:
981 w = math.ceil ((time - last_time) * chart_bounds[2] / proc_tree.duration) + 1
982 x = chart_bounds[0] + round((last_time - proc_tree.start_time) * chart_bounds[2] / proc_tree.duration)
983 ctx.rectangle (x, below[last_time] - last_cuml, w, last_cuml)
984 ctx.fill()
985# ctx.stroke()
986 last_time = time
987 y = below [time] - cuml
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500988
Brad Bishopc342db32019-05-15 21:57:59 -0400989 row[time] = y
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500990
Brad Bishopc342db32019-05-15 21:57:59 -0400991 # render the last segment
992 x = chart_bounds[0] + round((last_time - proc_tree.start_time) * chart_bounds[2] / proc_tree.duration)
993 y = below[last_time] - cuml
994 ctx.rectangle (x, y, chart_bounds[2] - x, cuml)
995 ctx.fill()
996# ctx.stroke()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500997
Brad Bishopc342db32019-05-15 21:57:59 -0400998 # render legend if it will fit
999 if cuml > 8:
1000 label = cs.cmd
1001 extnts = ctx.text_extents(label)
1002 label_w = extnts[2]
1003 label_h = extnts[3]
1004# print "Text extents %g by %g" % (label_w, label_h)
1005 labels.append((label,
1006 chart_bounds[0] + chart_bounds[2] - label_w - off_x * 2,
1007 y + (cuml + label_h) / 2))
1008 if cs in legends:
1009 print("ARGH - duplicate process in list !")
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001010
Brad Bishopc342db32019-05-15 21:57:59 -04001011 legends.append ((cs, process_total_time))
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001012
Brad Bishopc342db32019-05-15 21:57:59 -04001013 below = row
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001014
Brad Bishopc342db32019-05-15 21:57:59 -04001015 # render grid-lines over the top
1016 draw_box_ticks(ctx, chart_bounds, sec_w)
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001017
Brad Bishopc342db32019-05-15 21:57:59 -04001018 # render labels
1019 for l in labels:
1020 draw_text(ctx, l[0], TEXT_COLOR, l[1], l[2])
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001021
Brad Bishopc342db32019-05-15 21:57:59 -04001022 # Render legends
1023 font_height = 20
1024 label_width = 300
1025 LEGENDS_PER_COL = 15
1026 LEGENDS_TOTAL = 45
1027 ctx.set_font_size (TITLE_FONT_SIZE)
1028 dur_secs = duration / 100
1029 cpu_secs = total_time / 1000000000
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001030
Brad Bishopc342db32019-05-15 21:57:59 -04001031 # misleading - with multiple CPUs ...
1032# idle = ((dur_secs - cpu_secs) / dur_secs) * 100.0
1033 if stat_type is STAT_TYPE_CPU:
1034 label = "Cumulative CPU usage, by process; total CPU: " \
1035 " %.5g(s) time: %.3g(s)" % (cpu_secs, dur_secs)
1036 else:
1037 label = "Cumulative I/O usage, by process; total I/O: " \
1038 " %.5g(s) time: %.3g(s)" % (cpu_secs, dur_secs)
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001039
Brad Bishopc342db32019-05-15 21:57:59 -04001040 draw_text(ctx, label, TEXT_COLOR, chart_bounds[0] + off_x,
1041 chart_bounds[1] + font_height)
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001042
Brad Bishopc342db32019-05-15 21:57:59 -04001043 i = 0
1044 legends = sorted(legends, key=itemgetter(1), reverse=True)
1045 ctx.set_font_size(TEXT_FONT_SIZE)
1046 for t in legends:
1047 cs = t[0]
1048 time = t[1]
1049 x = chart_bounds[0] + off_x + int (i/LEGENDS_PER_COL) * label_width
1050 y = chart_bounds[1] + font_height * ((i % LEGENDS_PER_COL) + 2)
1051 str = "%s - %.0f(ms) (%2.2f%%)" % (cs.cmd, time/1000000, (time/total_time) * 100.0)
1052 draw_legend_box(ctx, str, cs.color, x, y, leg_s)
1053 i = i + 1
1054 if i >= LEGENDS_TOTAL:
1055 break