blob: c6e67833abf35656d03e858226fe39816deead19 [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
Patrick Williamsdb4c27e2022-08-05 08:10:29 -050091# avg10 memory pressure color
92MEM_PRESSURE_AVG10_COLOR = (0.0, 0.0, 0.0, 1.0)
93# delta total memory pressure color
94MEM_PRESSURE_TOTAL_COLOR = DISK_TPUT_COLOR
Andrew Geissler615f2f12022-07-15 14:00:58 -050095
96
97
98
Patrick Williamsc124f4f2015-09-15 14:41:29 -050099# Process border color.
100PROC_BORDER_COLOR = (0.71, 0.71, 0.71, 1.0)
101# Waiting process color.
102PROC_COLOR_D = (0.76, 0.48, 0.48, 0.5)
103# Running process color.
104PROC_COLOR_R = CPU_COLOR
105# Sleeping process color.
106PROC_COLOR_S = (0.94, 0.94, 0.94, 1.0)
107# Stopped process color.
108PROC_COLOR_T = (0.94, 0.50, 0.50, 1.0)
109# Zombie process color.
110PROC_COLOR_Z = (0.71, 0.71, 0.71, 1.0)
111# Dead process color.
112PROC_COLOR_X = (0.71, 0.71, 0.71, 0.125)
113# Paging process color.
114PROC_COLOR_W = (0.71, 0.71, 0.71, 0.125)
115
116# Process label color.
117PROC_TEXT_COLOR = (0.19, 0.19, 0.19, 1.0)
118# Process label font.
119PROC_TEXT_FONT_SIZE = 12
120
121# Signature color.
122SIG_COLOR = (0.0, 0.0, 0.0, 0.3125)
123# Signature font.
124SIG_FONT_SIZE = 14
125# Signature text.
126SIGNATURE = "http://github.com/mmeeks/bootchart"
127
128# Process dependency line color.
129DEP_COLOR = (0.75, 0.75, 0.75, 1.0)
130# Process dependency line stroke.
131DEP_STROKE = 1.0
132
133# Process description date format.
134DESC_TIME_FORMAT = "mm:ss.SSS"
135
136# Cumulative coloring bits
137HSV_MAX_MOD = 31
138HSV_STEP = 7
139
140# Configure task color
141TASK_COLOR_CONFIGURE = (1.0, 1.0, 0.00, 1.0)
142# Compile task color.
143TASK_COLOR_COMPILE = (0.0, 1.00, 0.00, 1.0)
144# Install task color
145TASK_COLOR_INSTALL = (1.0, 0.00, 1.00, 1.0)
146# Sysroot task color
147TASK_COLOR_SYSROOT = (0.0, 0.00, 1.00, 1.0)
148# Package task color
149TASK_COLOR_PACKAGE = (0.0, 1.00, 1.00, 1.0)
150# Package Write RPM/DEB/IPK task color
151TASK_COLOR_PACKAGE_WRITE = (0.0, 0.50, 0.50, 1.0)
152
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500153# Distinct colors used for different disk volumnes.
154# If we have more volumns, colors get re-used.
155VOLUME_COLORS = [
Brad Bishopc342db32019-05-15 21:57:59 -0400156 (1.0, 1.0, 0.00, 1.0),
157 (0.0, 1.00, 0.00, 1.0),
158 (1.0, 0.00, 1.00, 1.0),
159 (0.0, 0.00, 1.00, 1.0),
160 (0.0, 1.00, 1.00, 1.0),
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500161]
162
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500163# Process states
164STATE_UNDEFINED = 0
165STATE_RUNNING = 1
166STATE_SLEEPING = 2
167STATE_WAITING = 3
168STATE_STOPPED = 4
169STATE_ZOMBIE = 5
170
171STATE_COLORS = [(0, 0, 0, 0), PROC_COLOR_R, PROC_COLOR_S, PROC_COLOR_D, \
Brad Bishopc342db32019-05-15 21:57:59 -0400172 PROC_COLOR_T, PROC_COLOR_Z, PROC_COLOR_X, PROC_COLOR_W]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500173
174# CumulativeStats Types
175STAT_TYPE_CPU = 0
176STAT_TYPE_IO = 1
177
178# Convert ps process state to an int
179def get_proc_state(flag):
Brad Bishopc342db32019-05-15 21:57:59 -0400180 return "RSDTZXW".find(flag) + 1
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500181
182def draw_text(ctx, text, color, x, y):
Brad Bishopc342db32019-05-15 21:57:59 -0400183 ctx.set_source_rgba(*color)
184 ctx.move_to(x, y)
185 ctx.show_text(text)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500186
187def draw_fill_rect(ctx, color, rect):
Brad Bishopc342db32019-05-15 21:57:59 -0400188 ctx.set_source_rgba(*color)
189 ctx.rectangle(*rect)
190 ctx.fill()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500191
192def draw_rect(ctx, color, rect):
Brad Bishopc342db32019-05-15 21:57:59 -0400193 ctx.set_source_rgba(*color)
194 ctx.rectangle(*rect)
195 ctx.stroke()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500196
197def draw_legend_box(ctx, label, fill_color, x, y, s):
Brad Bishopc342db32019-05-15 21:57:59 -0400198 draw_fill_rect(ctx, fill_color, (x, y - s, s, s))
199 draw_rect(ctx, PROC_BORDER_COLOR, (x, y - s, s, s))
200 draw_text(ctx, label, TEXT_COLOR, x + s + 5, y)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500201
202def draw_legend_line(ctx, label, fill_color, x, y, s):
Brad Bishopc342db32019-05-15 21:57:59 -0400203 draw_fill_rect(ctx, fill_color, (x, y - s/2, s + 1, 3))
204 ctx.arc(x + (s + 1)/2.0, y - (s - 3)/2.0, 2.5, 0, 2.0 * math.pi)
205 ctx.fill()
206 draw_text(ctx, label, TEXT_COLOR, x + s + 5, y)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500207
208def draw_label_in_box(ctx, color, label, x, y, w, maxx):
Brad Bishopc342db32019-05-15 21:57:59 -0400209 label_w = ctx.text_extents(label)[2]
210 label_x = x + w / 2 - label_w / 2
211 if label_w + 10 > w:
212 label_x = x + w + 5
213 if label_x + label_w > maxx:
214 label_x = x - label_w - 5
215 draw_text(ctx, label, color, label_x, y)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500216
217def draw_sec_labels(ctx, options, rect, sec_w, nsecs):
Brad Bishopc342db32019-05-15 21:57:59 -0400218 ctx.set_font_size(AXIS_FONT_SIZE)
219 prev_x = 0
220 for i in range(0, rect[2] + 1, sec_w):
221 if ((i / sec_w) % nsecs == 0) :
222 if options.app_options.as_minutes :
223 label = "%.1f" % (i / sec_w / 60.0)
224 else :
225 label = "%d" % (i / sec_w)
226 label_w = ctx.text_extents(label)[2]
227 x = rect[0] + i - label_w/2
228 if x >= prev_x:
229 draw_text(ctx, label, TEXT_COLOR, x, rect[1] - 2)
230 prev_x = x + label_w
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500231
232def draw_box_ticks(ctx, rect, sec_w):
Brad Bishopc342db32019-05-15 21:57:59 -0400233 draw_rect(ctx, BORDER_COLOR, tuple(rect))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500234
Brad Bishopc342db32019-05-15 21:57:59 -0400235 ctx.set_line_cap(cairo.LINE_CAP_SQUARE)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500236
Brad Bishopc342db32019-05-15 21:57:59 -0400237 for i in range(sec_w, rect[2] + 1, sec_w):
238 if ((i / sec_w) % 10 == 0) :
239 ctx.set_line_width(1.5)
240 elif sec_w < 5 :
241 continue
242 else :
243 ctx.set_line_width(1.0)
244 if ((i / sec_w) % 30 == 0) :
245 ctx.set_source_rgba(*TICK_COLOR_BOLD)
246 else :
247 ctx.set_source_rgba(*TICK_COLOR)
248 ctx.move_to(rect[0] + i, rect[1] + 1)
249 ctx.line_to(rect[0] + i, rect[1] + rect[3] - 1)
250 ctx.stroke()
251 ctx.set_line_width(1.0)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500252
Brad Bishopc342db32019-05-15 21:57:59 -0400253 ctx.set_line_cap(cairo.LINE_CAP_BUTT)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500254
255def draw_annotations(ctx, proc_tree, times, rect):
256 ctx.set_line_cap(cairo.LINE_CAP_SQUARE)
257 ctx.set_source_rgba(*ANNOTATION_COLOR)
258 ctx.set_dash([4, 4])
259
260 for time in times:
261 if time is not None:
262 x = ((time - proc_tree.start_time) * rect[2] / proc_tree.duration)
263
264 ctx.move_to(rect[0] + x, rect[1] + 1)
265 ctx.line_to(rect[0] + x, rect[1] + rect[3] - 1)
266 ctx.stroke()
267
268 ctx.set_line_cap(cairo.LINE_CAP_BUTT)
269 ctx.set_dash([])
270
271def draw_chart(ctx, color, fill, chart_bounds, data, proc_tree, data_range):
Brad Bishopc342db32019-05-15 21:57:59 -0400272 ctx.set_line_width(0.5)
273 x_shift = proc_tree.start_time
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500274
Brad Bishopc342db32019-05-15 21:57:59 -0400275 def transform_point_coords(point, x_base, y_base, \
276 xscale, yscale, x_trans, y_trans):
277 x = (point[0] - x_base) * xscale + x_trans
278 y = (point[1] - y_base) * -yscale + y_trans + chart_bounds[3]
279 return x, y
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500280
Brad Bishopc342db32019-05-15 21:57:59 -0400281 max_x = max (x for (x, y) in data)
282 max_y = max (y for (x, y) in data)
283 # avoid divide by zero
284 if max_y == 0:
285 max_y = 1.0
Andrew Geissler5199d832021-09-24 16:47:35 -0500286 if (max_x - x_shift):
287 xscale = float (chart_bounds[2]) / (max_x - x_shift)
288 else:
289 xscale = float (chart_bounds[2])
Brad Bishopc342db32019-05-15 21:57:59 -0400290 # If data_range is given, scale the chart so that the value range in
291 # data_range matches the chart bounds exactly.
292 # Otherwise, scale so that the actual data matches the chart bounds.
Andrew Geisslerc926e172021-05-07 16:11:35 -0500293 if data_range and (data_range[1] - data_range[0]):
Brad Bishopc342db32019-05-15 21:57:59 -0400294 yscale = float(chart_bounds[3]) / (data_range[1] - data_range[0])
295 ybase = data_range[0]
296 else:
297 yscale = float(chart_bounds[3]) / max_y
298 ybase = 0
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500299
Brad Bishopc342db32019-05-15 21:57:59 -0400300 first = transform_point_coords (data[0], x_shift, ybase, xscale, yscale, \
301 chart_bounds[0], chart_bounds[1])
302 last = transform_point_coords (data[-1], x_shift, ybase, xscale, yscale, \
303 chart_bounds[0], chart_bounds[1])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500304
Brad Bishopc342db32019-05-15 21:57:59 -0400305 ctx.set_source_rgba(*color)
306 ctx.move_to(*first)
307 for point in data:
308 x, y = transform_point_coords (point, x_shift, ybase, xscale, yscale, \
309 chart_bounds[0], chart_bounds[1])
310 ctx.line_to(x, y)
311 if fill:
312 ctx.stroke_preserve()
313 ctx.line_to(last[0], chart_bounds[1]+chart_bounds[3])
314 ctx.line_to(first[0], chart_bounds[1]+chart_bounds[3])
315 ctx.line_to(first[0], first[1])
316 ctx.fill()
317 else:
318 ctx.stroke()
319 ctx.set_line_width(1.0)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500320
321bar_h = 55
322meminfo_bar_h = 2 * bar_h
323header_h = 60
324# offsets
325off_x, off_y = 220, 10
326sec_w_base = 1 # the width of a second
327proc_h = 16 # the height of a process
328leg_s = 10
329MIN_IMG_W = 800
Andrew Geissler82c905d2020-04-13 13:39:40 -0500330CUML_HEIGHT = 2000 # Increased value to accommodate CPU and I/O Graphs
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500331OPTIONS = None
332
333def extents(options, xscale, trace):
Brad Bishopc342db32019-05-15 21:57:59 -0400334 start = min(trace.start.keys())
335 end = start
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500336
Brad Bishopc342db32019-05-15 21:57:59 -0400337 processes = 0
338 for proc in trace.processes:
339 if not options.app_options.show_all and \
340 trace.processes[proc][1] - trace.processes[proc][0] < options.app_options.mintime:
341 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500342
Brad Bishopc342db32019-05-15 21:57:59 -0400343 if trace.processes[proc][1] > end:
344 end = trace.processes[proc][1]
345 processes += 1
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500346
Brad Bishopc342db32019-05-15 21:57:59 -0400347 if trace.min is not None and trace.max is not None:
348 start = trace.min
349 end = trace.max
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500350
Brad Bishopc342db32019-05-15 21:57:59 -0400351 w = int ((end - start) * sec_w_base * xscale) + 2 * off_x
352 h = proc_h * processes + header_h + 2 * off_y
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500353
Brad Bishopc342db32019-05-15 21:57:59 -0400354 if options.charts:
355 if trace.cpu_stats:
356 h += 30 + bar_h
357 if trace.disk_stats:
358 h += 30 + bar_h
Andrew Geisslerfc113ea2023-03-31 09:59:46 -0500359 if trace.cpu_pressure:
360 h += 30 + bar_h
361 if trace.io_pressure:
362 h += 30 + bar_h
363 if trace.mem_pressure:
364 h += 30 + bar_h
Brad Bishopc342db32019-05-15 21:57:59 -0400365 if trace.monitor_disk:
366 h += 30 + bar_h
367 if trace.mem_stats:
368 h += meminfo_bar_h
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500369
Brad Bishopc342db32019-05-15 21:57:59 -0400370 # Allow for width of process legend and offset
371 if w < (720 + off_x):
372 w = 720 + off_x
373
374 return (w, h)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500375
376def clip_visible(clip, rect):
Brad Bishopc342db32019-05-15 21:57:59 -0400377 xmax = max (clip[0], rect[0])
378 ymax = max (clip[1], rect[1])
379 xmin = min (clip[0] + clip[2], rect[0] + rect[2])
380 ymin = min (clip[1] + clip[3], rect[1] + rect[3])
381 return (xmin > xmax and ymin > ymax)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500382
383def render_charts(ctx, options, clip, trace, curr_y, w, h, sec_w):
Brad Bishopc342db32019-05-15 21:57:59 -0400384 proc_tree = options.proc_tree(trace)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500385
Brad Bishopc342db32019-05-15 21:57:59 -0400386 # render bar legend
387 if trace.cpu_stats:
388 ctx.set_font_size(LEGEND_FONT_SIZE)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500389
Brad Bishopc342db32019-05-15 21:57:59 -0400390 draw_legend_box(ctx, "CPU (user+sys)", CPU_COLOR, off_x, curr_y+20, leg_s)
391 draw_legend_box(ctx, "I/O (wait)", IO_COLOR, off_x + 120, curr_y+20, leg_s)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500392
Brad Bishopc342db32019-05-15 21:57:59 -0400393 # render I/O wait
394 chart_rect = (off_x, curr_y+30, w, bar_h)
395 if clip_visible (clip, chart_rect):
396 draw_box_ticks (ctx, chart_rect, sec_w)
397 draw_annotations (ctx, proc_tree, trace.times, chart_rect)
398 draw_chart (ctx, IO_COLOR, True, chart_rect, \
399 [(sample.time, sample.user + sample.sys + sample.io) for sample in trace.cpu_stats], \
400 proc_tree, None)
401 # render CPU load
402 draw_chart (ctx, CPU_COLOR, True, chart_rect, \
403 [(sample.time, sample.user + sample.sys) for sample in trace.cpu_stats], \
404 proc_tree, None)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500405
Brad Bishopc342db32019-05-15 21:57:59 -0400406 curr_y = curr_y + 30 + bar_h
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500407
Brad Bishopc342db32019-05-15 21:57:59 -0400408 # render second chart
409 if trace.disk_stats:
410 draw_legend_line(ctx, "Disk throughput", DISK_TPUT_COLOR, off_x, curr_y+20, leg_s)
411 draw_legend_box(ctx, "Disk utilization", IO_COLOR, off_x + 120, curr_y+20, leg_s)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500412
Brad Bishopc342db32019-05-15 21:57:59 -0400413 # render I/O utilization
414 chart_rect = (off_x, curr_y+30, w, bar_h)
415 if clip_visible (clip, chart_rect):
416 draw_box_ticks (ctx, chart_rect, sec_w)
417 draw_annotations (ctx, proc_tree, trace.times, chart_rect)
418 draw_chart (ctx, IO_COLOR, True, chart_rect, \
419 [(sample.time, sample.util) for sample in trace.disk_stats], \
420 proc_tree, None)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500421
Brad Bishopc342db32019-05-15 21:57:59 -0400422 # render disk throughput
423 max_sample = max (trace.disk_stats, key = lambda s: s.tput)
424 if clip_visible (clip, chart_rect):
425 draw_chart (ctx, DISK_TPUT_COLOR, False, chart_rect, \
426 [(sample.time, sample.tput) for sample in trace.disk_stats], \
427 proc_tree, None)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500428
Brad Bishopc342db32019-05-15 21:57:59 -0400429 pos_x = off_x + ((max_sample.time - proc_tree.start_time) * w / proc_tree.duration)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500430
Brad Bishopc342db32019-05-15 21:57:59 -0400431 shift_x, shift_y = -20, 20
432 if (pos_x < off_x + 245):
433 shift_x, shift_y = 5, 40
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500434
Brad Bishopc342db32019-05-15 21:57:59 -0400435 label = "%dMB/s" % round ((max_sample.tput) / 1024.0)
436 draw_text (ctx, label, DISK_TPUT_COLOR, pos_x + shift_x, curr_y + shift_y)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500437
Brad Bishopc342db32019-05-15 21:57:59 -0400438 curr_y = curr_y + 30 + bar_h
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500439
Andrew Geissler615f2f12022-07-15 14:00:58 -0500440 # render CPU pressure chart
441 if trace.cpu_pressure:
Patrick Williamsdb4c27e2022-08-05 08:10:29 -0500442 max_sample_avg = max (trace.cpu_pressure, key = lambda s: s.avg10)
443 max_sample_total = max (trace.cpu_pressure, key = lambda s: s.deltaTotal)
444 draw_legend_line(ctx, "avg10 CPU Pressure (max %d%%)" % (max_sample_avg.avg10), CPU_PRESSURE_AVG10_COLOR, off_x, curr_y+20, leg_s)
445 draw_legend_box(ctx, "delta total CPU Pressure (max %d)" % (max_sample_total.deltaTotal), CPU_PRESSURE_TOTAL_COLOR, off_x + 240, curr_y+20, leg_s)
Andrew Geissler615f2f12022-07-15 14:00:58 -0500446
447 # render delta total cpu
448 chart_rect = (off_x, curr_y+30, w, bar_h)
449 if clip_visible (clip, chart_rect):
450 draw_box_ticks (ctx, chart_rect, sec_w)
451 draw_annotations (ctx, proc_tree, trace.times, chart_rect)
452 draw_chart (ctx, CPU_PRESSURE_TOTAL_COLOR, True, chart_rect, \
453 [(sample.time, sample.deltaTotal) for sample in trace.cpu_pressure], \
454 proc_tree, None)
455
456 # render avg10 cpu
Andrew Geissler615f2f12022-07-15 14:00:58 -0500457 if clip_visible (clip, chart_rect):
458 draw_chart (ctx, CPU_PRESSURE_AVG10_COLOR, False, chart_rect, \
459 [(sample.time, sample.avg10) for sample in trace.cpu_pressure], \
460 proc_tree, None)
461
Patrick Williamsdb4c27e2022-08-05 08:10:29 -0500462 pos_x = off_x + ((max_sample_avg.time - proc_tree.start_time) * w / proc_tree.duration)
Andrew Geissler615f2f12022-07-15 14:00:58 -0500463
464 shift_x, shift_y = -20, 20
465 if (pos_x < off_x + 245):
466 shift_x, shift_y = 5, 40
467
468
Patrick Williamsdb4c27e2022-08-05 08:10:29 -0500469 label = "%d%%" % (max_sample_avg.avg10)
Andrew Geissler615f2f12022-07-15 14:00:58 -0500470 draw_text (ctx, label, CPU_PRESSURE_AVG10_COLOR, pos_x + shift_x, curr_y + shift_y)
471
472 curr_y = curr_y + 30 + bar_h
473
Patrick Williamsdb4c27e2022-08-05 08:10:29 -0500474 # render I/O pressure chart
Andrew Geissler615f2f12022-07-15 14:00:58 -0500475 if trace.io_pressure:
Patrick Williamsdb4c27e2022-08-05 08:10:29 -0500476 max_sample_avg = max (trace.io_pressure, key = lambda s: s.avg10)
477 max_sample_total = max (trace.io_pressure, key = lambda s: s.deltaTotal)
478 draw_legend_line(ctx, "avg10 I/O Pressure (max %d%%)" % (max_sample_avg.avg10), IO_PRESSURE_AVG10_COLOR, off_x, curr_y+20, leg_s)
479 draw_legend_box(ctx, "delta total I/O Pressure (max %d)" % (max_sample_total.deltaTotal), IO_PRESSURE_TOTAL_COLOR, off_x + 240, curr_y+20, leg_s)
Andrew Geissler615f2f12022-07-15 14:00:58 -0500480
Patrick Williamsdb4c27e2022-08-05 08:10:29 -0500481 # render delta total io
Andrew Geissler615f2f12022-07-15 14:00:58 -0500482 chart_rect = (off_x, curr_y+30, w, bar_h)
483 if clip_visible (clip, chart_rect):
484 draw_box_ticks (ctx, chart_rect, sec_w)
485 draw_annotations (ctx, proc_tree, trace.times, chart_rect)
486 draw_chart (ctx, IO_PRESSURE_TOTAL_COLOR, True, chart_rect, \
487 [(sample.time, sample.deltaTotal) for sample in trace.io_pressure], \
488 proc_tree, None)
489
Patrick Williamsdb4c27e2022-08-05 08:10:29 -0500490 # render avg10 io
Andrew Geissler615f2f12022-07-15 14:00:58 -0500491 if clip_visible (clip, chart_rect):
492 draw_chart (ctx, IO_PRESSURE_AVG10_COLOR, False, chart_rect, \
493 [(sample.time, sample.avg10) for sample in trace.io_pressure], \
494 proc_tree, None)
495
Patrick Williamsdb4c27e2022-08-05 08:10:29 -0500496 pos_x = off_x + ((max_sample_avg.time - proc_tree.start_time) * w / proc_tree.duration)
Andrew Geissler615f2f12022-07-15 14:00:58 -0500497
498 shift_x, shift_y = -20, 20
499 if (pos_x < off_x + 245):
500 shift_x, shift_y = 5, 40
501
Patrick Williamsdb4c27e2022-08-05 08:10:29 -0500502
503 label = "%d%%" % (max_sample_avg.avg10)
Andrew Geissler615f2f12022-07-15 14:00:58 -0500504 draw_text (ctx, label, IO_PRESSURE_AVG10_COLOR, pos_x + shift_x, curr_y + shift_y)
505
506 curr_y = curr_y + 30 + bar_h
507
Patrick Williamsdb4c27e2022-08-05 08:10:29 -0500508 # render MEM pressure chart
509 if trace.mem_pressure:
510 max_sample_avg = max (trace.mem_pressure, key = lambda s: s.avg10)
511 max_sample_total = max (trace.mem_pressure, key = lambda s: s.deltaTotal)
512 draw_legend_line(ctx, "avg10 MEM Pressure (max %d%%)" % (max_sample_avg.avg10), MEM_PRESSURE_AVG10_COLOR, off_x, curr_y+20, leg_s)
513 draw_legend_box(ctx, "delta total MEM Pressure (max %d)" % (max_sample_total.deltaTotal), MEM_PRESSURE_TOTAL_COLOR, off_x + 240, curr_y+20, leg_s)
514
515 # render delta total mem
516 chart_rect = (off_x, curr_y+30, w, bar_h)
517 if clip_visible (clip, chart_rect):
518 draw_box_ticks (ctx, chart_rect, sec_w)
519 draw_annotations (ctx, proc_tree, trace.times, chart_rect)
520 draw_chart (ctx, MEM_PRESSURE_TOTAL_COLOR, True, chart_rect, \
521 [(sample.time, sample.deltaTotal) for sample in trace.mem_pressure], \
522 proc_tree, None)
523
524 # render avg10 mem
525 if clip_visible (clip, chart_rect):
526 draw_chart (ctx, MEM_PRESSURE_AVG10_COLOR, False, chart_rect, \
527 [(sample.time, sample.avg10) for sample in trace.mem_pressure], \
528 proc_tree, None)
529
530 pos_x = off_x + ((max_sample_avg.time - proc_tree.start_time) * w / proc_tree.duration)
531
532 shift_x, shift_y = -20, 20
533 if (pos_x < off_x + 245):
534 shift_x, shift_y = 5, 40
535
536
537 label = "%d%%" % (max_sample_avg.avg10)
538 draw_text (ctx, label, MEM_PRESSURE_AVG10_COLOR, pos_x + shift_x, curr_y + shift_y)
539
540 curr_y = curr_y + 30 + bar_h
541
Brad Bishopc342db32019-05-15 21:57:59 -0400542 # render disk space usage
543 #
544 # Draws the amount of disk space used on each volume relative to the
545 # lowest recorded amount. The graphs for each volume are stacked above
546 # each other so that total disk usage is visible.
547 if trace.monitor_disk:
548 ctx.set_font_size(LEGEND_FONT_SIZE)
549 # Determine set of volumes for which we have
550 # information and the minimal amount of used disk
551 # space for each. Currently samples are allowed to
552 # not have a values for all volumes; drawing could be
553 # made more efficient if that wasn't the case.
554 volumes = set()
555 min_used = {}
556 for sample in trace.monitor_disk:
557 for volume, used in sample.records.items():
558 volumes.add(volume)
559 if volume not in min_used or min_used[volume] > used:
560 min_used[volume] = used
561 volumes = sorted(list(volumes))
562 disk_scale = 0
563 for i, volume in enumerate(volumes):
564 volume_scale = max([sample.records[volume] - min_used[volume]
565 for sample in trace.monitor_disk
566 if volume in sample.records])
567 # Does not take length of volume name into account, but fixed offset
568 # works okay in practice.
569 draw_legend_box(ctx, '%s (max: %u MiB)' % (volume, volume_scale / 1024 / 1024),
570 VOLUME_COLORS[i % len(VOLUME_COLORS)],
571 off_x + i * 250, curr_y+20, leg_s)
572 disk_scale += volume_scale
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500573
Brad Bishopc342db32019-05-15 21:57:59 -0400574 # render used amount of disk space
575 chart_rect = (off_x, curr_y+30, w, bar_h)
576 if clip_visible (clip, chart_rect):
577 draw_box_ticks (ctx, chart_rect, sec_w)
578 draw_annotations (ctx, proc_tree, trace.times, chart_rect)
579 for i in range(len(volumes), 0, -1):
580 draw_chart (ctx, VOLUME_COLORS[(i - 1) % len(VOLUME_COLORS)], True, chart_rect, \
581 [(sample.time,
582 # Sum up used space of all volumes including the current one
583 # so that the graphs appear as stacked on top of each other.
584 functools.reduce(lambda x,y: x+y,
585 [sample.records[volume] - min_used[volume]
586 for volume in volumes[0:i]
587 if volume in sample.records],
588 0))
589 for sample in trace.monitor_disk], \
590 proc_tree, [0, disk_scale])
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500591
Brad Bishopc342db32019-05-15 21:57:59 -0400592 curr_y = curr_y + 30 + bar_h
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500593
Brad Bishopc342db32019-05-15 21:57:59 -0400594 # render mem usage
595 chart_rect = (off_x, curr_y+30, w, meminfo_bar_h)
596 mem_stats = trace.mem_stats
597 if mem_stats and clip_visible (clip, chart_rect):
598 mem_scale = max(sample.buffers for sample in mem_stats)
599 draw_legend_box(ctx, "Mem cached (scale: %u MiB)" % (float(mem_scale) / 1024), MEM_CACHED_COLOR, off_x, curr_y+20, leg_s)
600 draw_legend_box(ctx, "Used", MEM_USED_COLOR, off_x + 240, curr_y+20, leg_s)
601 draw_legend_box(ctx, "Buffers", MEM_BUFFERS_COLOR, off_x + 360, curr_y+20, leg_s)
602 draw_legend_line(ctx, "Swap (scale: %u MiB)" % max([(sample.swap)/1024 for sample in mem_stats]), \
603 MEM_SWAP_COLOR, off_x + 480, curr_y+20, leg_s)
604 draw_box_ticks(ctx, chart_rect, sec_w)
605 draw_annotations(ctx, proc_tree, trace.times, chart_rect)
606 draw_chart(ctx, MEM_BUFFERS_COLOR, True, chart_rect, \
607 [(sample.time, sample.buffers) for sample in trace.mem_stats], \
608 proc_tree, [0, mem_scale])
609 draw_chart(ctx, MEM_USED_COLOR, True, chart_rect, \
610 [(sample.time, sample.used) for sample in mem_stats], \
611 proc_tree, [0, mem_scale])
612 draw_chart(ctx, MEM_CACHED_COLOR, True, chart_rect, \
613 [(sample.time, sample.cached) for sample in mem_stats], \
614 proc_tree, [0, mem_scale])
615 draw_chart(ctx, MEM_SWAP_COLOR, False, chart_rect, \
616 [(sample.time, float(sample.swap)) for sample in mem_stats], \
617 proc_tree, None)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500618
Brad Bishopc342db32019-05-15 21:57:59 -0400619 curr_y = curr_y + meminfo_bar_h
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500620
Brad Bishopc342db32019-05-15 21:57:59 -0400621 return curr_y
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500622
Andrew Geissler8f840682023-07-21 09:09:43 -0500623def render_processes_chart(ctx, options, trace, curr_y, width, h, sec_w):
624 chart_rect = [off_x, curr_y+header_h, width, h - curr_y - 1 * off_y - header_h ]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500625
Brad Bishopc342db32019-05-15 21:57:59 -0400626 draw_legend_box (ctx, "Configure", \
627 TASK_COLOR_CONFIGURE, off_x , curr_y + 45, leg_s)
628 draw_legend_box (ctx, "Compile", \
629 TASK_COLOR_COMPILE, off_x+120, curr_y + 45, leg_s)
630 draw_legend_box (ctx, "Install", \
631 TASK_COLOR_INSTALL, off_x+240, curr_y + 45, leg_s)
632 draw_legend_box (ctx, "Populate Sysroot", \
633 TASK_COLOR_SYSROOT, off_x+360, curr_y + 45, leg_s)
634 draw_legend_box (ctx, "Package", \
635 TASK_COLOR_PACKAGE, off_x+480, curr_y + 45, leg_s)
636 draw_legend_box (ctx, "Package Write", \
637 TASK_COLOR_PACKAGE_WRITE, off_x+600, curr_y + 45, leg_s)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500638
Brad Bishopc342db32019-05-15 21:57:59 -0400639 ctx.set_font_size(PROC_TEXT_FONT_SIZE)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500640
Brad Bishopc342db32019-05-15 21:57:59 -0400641 draw_box_ticks(ctx, chart_rect, sec_w)
642 draw_sec_labels(ctx, options, chart_rect, sec_w, 30)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500643
Brad Bishopc342db32019-05-15 21:57:59 -0400644 y = curr_y+header_h
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500645
Brad Bishopc342db32019-05-15 21:57:59 -0400646 offset = trace.min or min(trace.start.keys())
647 for start in sorted(trace.start.keys()):
648 for process in sorted(trace.start[start]):
Andrew Geissler8f840682023-07-21 09:09:43 -0500649 elapsed_time = trace.processes[process][1] - start
Brad Bishopc342db32019-05-15 21:57:59 -0400650 if not options.app_options.show_all and \
Andrew Geissler8f840682023-07-21 09:09:43 -0500651 elapsed_time < options.app_options.mintime:
Brad Bishopc342db32019-05-15 21:57:59 -0400652 continue
653 task = process.split(":")[1]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500654
Brad Bishopc342db32019-05-15 21:57:59 -0400655 #print(process)
656 #print(trace.processes[process][1])
657 #print(s)
658
659 x = chart_rect[0] + (start - offset) * sec_w
Andrew Geissler8f840682023-07-21 09:09:43 -0500660 w = elapsed_time * sec_w
Brad Bishopc342db32019-05-15 21:57:59 -0400661
Andrew Geissler5082cc72023-09-11 08:41:39 -0400662 def set_alfa(color, alfa):
663 clist = list(color)
664 clist[-1] = alfa
665 return tuple(clist)
666
Brad Bishopc342db32019-05-15 21:57:59 -0400667 #print("proc at %s %s %s %s" % (x, y, w, proc_h))
668 col = None
669 if task == "do_compile":
670 col = TASK_COLOR_COMPILE
Andrew Geissler5082cc72023-09-11 08:41:39 -0400671 elif "do_compile" in task:
672 col = set_alfa(TASK_COLOR_COMPILE, 0.25)
Brad Bishopc342db32019-05-15 21:57:59 -0400673 elif task == "do_configure":
674 col = TASK_COLOR_CONFIGURE
Andrew Geissler5082cc72023-09-11 08:41:39 -0400675 elif "do_configure" in task:
676 col = set_alfa(TASK_COLOR_CONFIGURE, 0.25)
Brad Bishopc342db32019-05-15 21:57:59 -0400677 elif task == "do_install":
678 col = TASK_COLOR_INSTALL
679 elif task == "do_populate_sysroot":
680 col = TASK_COLOR_SYSROOT
681 elif task == "do_package":
682 col = TASK_COLOR_PACKAGE
683 elif task == "do_package_write_rpm" or \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500684 task == "do_package_write_deb" or \
685 task == "do_package_write_ipk":
Brad Bishopc342db32019-05-15 21:57:59 -0400686 col = TASK_COLOR_PACKAGE_WRITE
687 else:
688 col = WHITE
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500689
Brad Bishopc342db32019-05-15 21:57:59 -0400690 if col:
691 draw_fill_rect(ctx, col, (x, y, w, proc_h))
692 draw_rect(ctx, PROC_BORDER_COLOR, (x, y, w, proc_h))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500693
Patrick Williamsb542dec2023-06-09 01:26:37 -0500694 # Show elapsed time for each task
Andrew Geissler8f840682023-07-21 09:09:43 -0500695 process = "%ds %s" % (elapsed_time, process)
696 draw_label_in_box(ctx, PROC_TEXT_COLOR, process, x, y + proc_h - 4, w, width)
Patrick Williamsb542dec2023-06-09 01:26:37 -0500697
Brad Bishopc342db32019-05-15 21:57:59 -0400698 y = y + proc_h
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500699
Brad Bishopc342db32019-05-15 21:57:59 -0400700 return curr_y
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500701
702#
703# Render the chart.
704#
705def render(ctx, options, xscale, trace):
Brad Bishopc342db32019-05-15 21:57:59 -0400706 (w, h) = extents (options, xscale, trace)
707 global OPTIONS
708 OPTIONS = options.app_options
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500709
Brad Bishopc342db32019-05-15 21:57:59 -0400710 # x, y, w, h
711 clip = ctx.clip_extents()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500712
Brad Bishopc342db32019-05-15 21:57:59 -0400713 sec_w = int (xscale * sec_w_base)
714 ctx.set_line_width(1.0)
715 ctx.select_font_face(FONT_NAME)
716 draw_fill_rect(ctx, WHITE, (0, 0, max(w, MIN_IMG_W), h))
717 w -= 2*off_x
718 curr_y = off_y;
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500719
Brad Bishopc342db32019-05-15 21:57:59 -0400720 if options.charts:
721 curr_y = render_charts (ctx, options, clip, trace, curr_y, w, h, sec_w)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500722
Brad Bishopc342db32019-05-15 21:57:59 -0400723 curr_y = render_processes_chart (ctx, options, trace, curr_y, w, h, sec_w)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500724
Brad Bishopc342db32019-05-15 21:57:59 -0400725 return
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500726
Brad Bishopc342db32019-05-15 21:57:59 -0400727 proc_tree = options.proc_tree (trace)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500728
Brad Bishopc342db32019-05-15 21:57:59 -0400729 # draw the title and headers
730 if proc_tree.idle:
731 duration = proc_tree.idle
732 else:
733 duration = proc_tree.duration
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500734
Brad Bishopc342db32019-05-15 21:57:59 -0400735 if not options.kernel_only:
736 curr_y = draw_header (ctx, trace.headers, duration)
737 else:
738 curr_y = off_y;
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500739
Brad Bishopc342db32019-05-15 21:57:59 -0400740 # draw process boxes
741 proc_height = h
742 if proc_tree.taskstats and options.cumulative:
743 proc_height -= CUML_HEIGHT
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500744
Brad Bishopc342db32019-05-15 21:57:59 -0400745 draw_process_bar_chart(ctx, clip, options, proc_tree, trace.times,
746 curr_y, w, proc_height, sec_w)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500747
Brad Bishopc342db32019-05-15 21:57:59 -0400748 curr_y = proc_height
749 ctx.set_font_size(SIG_FONT_SIZE)
750 draw_text(ctx, SIGNATURE, SIG_COLOR, off_x + 5, proc_height - 8)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500751
Brad Bishopc342db32019-05-15 21:57:59 -0400752 # draw a cumulative CPU-time-per-process graph
753 if proc_tree.taskstats and options.cumulative:
754 cuml_rect = (off_x, curr_y + off_y, w, CUML_HEIGHT/2 - off_y * 2)
755 if clip_visible (clip, cuml_rect):
756 draw_cuml_graph(ctx, proc_tree, cuml_rect, duration, sec_w, STAT_TYPE_CPU)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500757
Brad Bishopc342db32019-05-15 21:57:59 -0400758 # draw a cumulative I/O-time-per-process graph
759 if proc_tree.taskstats and options.cumulative:
760 cuml_rect = (off_x, curr_y + off_y * 100, w, CUML_HEIGHT/2 - off_y * 2)
761 if clip_visible (clip, cuml_rect):
762 draw_cuml_graph(ctx, proc_tree, cuml_rect, duration, sec_w, STAT_TYPE_IO)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500763
764def draw_process_bar_chart(ctx, clip, options, proc_tree, times, curr_y, w, h, sec_w):
Brad Bishopc342db32019-05-15 21:57:59 -0400765 header_size = 0
766 if not options.kernel_only:
767 draw_legend_box (ctx, "Running (%cpu)",
768 PROC_COLOR_R, off_x , curr_y + 45, leg_s)
769 draw_legend_box (ctx, "Unint.sleep (I/O)",
770 PROC_COLOR_D, off_x+120, curr_y + 45, leg_s)
771 draw_legend_box (ctx, "Sleeping",
772 PROC_COLOR_S, off_x+240, curr_y + 45, leg_s)
773 draw_legend_box (ctx, "Zombie",
774 PROC_COLOR_Z, off_x+360, curr_y + 45, leg_s)
775 header_size = 45
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500776
Brad Bishopc342db32019-05-15 21:57:59 -0400777 chart_rect = [off_x, curr_y + header_size + 15,
778 w, h - 2 * off_y - (curr_y + header_size + 15) + proc_h]
779 ctx.set_font_size (PROC_TEXT_FONT_SIZE)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500780
Brad Bishopc342db32019-05-15 21:57:59 -0400781 draw_box_ticks (ctx, chart_rect, sec_w)
782 if sec_w > 100:
783 nsec = 1
784 else:
785 nsec = 5
786 draw_sec_labels (ctx, options, chart_rect, sec_w, nsec)
787 draw_annotations (ctx, proc_tree, times, chart_rect)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500788
Brad Bishopc342db32019-05-15 21:57:59 -0400789 y = curr_y + 60
790 for root in proc_tree.process_tree:
791 draw_processes_recursively(ctx, root, proc_tree, y, proc_h, chart_rect, clip)
792 y = y + proc_h * proc_tree.num_nodes([root])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500793
794
795def draw_header (ctx, headers, duration):
796 toshow = [
797 ('system.uname', 'uname', lambda s: s),
798 ('system.release', 'release', lambda s: s),
799 ('system.cpu', 'CPU', lambda s: re.sub('model name\s*:\s*', '', s, 1)),
800 ('system.kernel.options', 'kernel options', lambda s: s),
801 ]
802
803 header_y = ctx.font_extents()[2] + 10
804 ctx.set_font_size(TITLE_FONT_SIZE)
805 draw_text(ctx, headers['title'], TEXT_COLOR, off_x, header_y)
806 ctx.set_font_size(TEXT_FONT_SIZE)
807
808 for (headerkey, headertitle, mangle) in toshow:
809 header_y += ctx.font_extents()[2]
810 if headerkey in headers:
811 value = headers.get(headerkey)
812 else:
813 value = ""
814 txt = headertitle + ': ' + mangle(value)
815 draw_text(ctx, txt, TEXT_COLOR, off_x, header_y)
816
817 dur = duration / 100.0
818 txt = 'time : %02d:%05.2f' % (math.floor(dur/60), dur - 60 * math.floor(dur/60))
819 if headers.get('system.maxpid') is not None:
820 txt = txt + ' max pid: %s' % (headers.get('system.maxpid'))
821
822 header_y += ctx.font_extents()[2]
823 draw_text (ctx, txt, TEXT_COLOR, off_x, header_y)
824
825 return header_y
826
827def draw_processes_recursively(ctx, proc, proc_tree, y, proc_h, rect, clip) :
Brad Bishopc342db32019-05-15 21:57:59 -0400828 x = rect[0] + ((proc.start_time - proc_tree.start_time) * rect[2] / proc_tree.duration)
829 w = ((proc.duration) * rect[2] / proc_tree.duration)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500830
Brad Bishopc342db32019-05-15 21:57:59 -0400831 draw_process_activity_colors(ctx, proc, proc_tree, x, y, w, proc_h, rect, clip)
832 draw_rect(ctx, PROC_BORDER_COLOR, (x, y, w, proc_h))
833 ipid = int(proc.pid)
834 if not OPTIONS.show_all:
835 cmdString = proc.cmd
836 else:
837 cmdString = ''
Patrick Williamsdb4c27e2022-08-05 08:10:29 -0500838 if (OPTIONS.show_pid or OPTIONS.show_all) and ipid != 0:
Brad Bishopc342db32019-05-15 21:57:59 -0400839 cmdString = cmdString + " [" + str(ipid // 1000) + "]"
840 if OPTIONS.show_all:
841 if proc.args:
842 cmdString = cmdString + " '" + "' '".join(proc.args) + "'"
843 else:
844 cmdString = cmdString + " " + proc.exe
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500845
Brad Bishopc342db32019-05-15 21:57:59 -0400846 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 -0500847
Brad Bishopc342db32019-05-15 21:57:59 -0400848 next_y = y + proc_h
849 for child in proc.child_list:
850 if next_y > clip[1] + clip[3]:
851 break
852 child_x, child_y = draw_processes_recursively(ctx, child, proc_tree, next_y, proc_h, rect, clip)
853 draw_process_connecting_lines(ctx, x, y, child_x, child_y, proc_h)
854 next_y = next_y + proc_h * proc_tree.num_nodes([child])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500855
Brad Bishopc342db32019-05-15 21:57:59 -0400856 return x, y
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500857
858
859def draw_process_activity_colors(ctx, proc, proc_tree, x, y, w, proc_h, rect, clip):
860
Brad Bishopc342db32019-05-15 21:57:59 -0400861 if y > clip[1] + clip[3] or y + proc_h + 2 < clip[1]:
862 return
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500863
Brad Bishopc342db32019-05-15 21:57:59 -0400864 draw_fill_rect(ctx, PROC_COLOR_S, (x, y, w, proc_h))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500865
Brad Bishopc342db32019-05-15 21:57:59 -0400866 last_tx = -1
867 for sample in proc.samples :
868 tx = rect[0] + round(((sample.time - proc_tree.start_time) * rect[2] / proc_tree.duration))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500869
Brad Bishopc342db32019-05-15 21:57:59 -0400870 # samples are sorted chronologically
871 if tx < clip[0]:
872 continue
873 if tx > clip[0] + clip[2]:
874 break
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500875
Brad Bishopc342db32019-05-15 21:57:59 -0400876 tw = round(proc_tree.sample_period * rect[2] / float(proc_tree.duration))
877 if last_tx != -1 and abs(last_tx - tx) <= tw:
878 tw -= last_tx - tx
879 tx = last_tx
880 tw = max (tw, 1) # nice to see at least something
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500881
Brad Bishopc342db32019-05-15 21:57:59 -0400882 last_tx = tx + tw
883 state = get_proc_state( sample.state )
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500884
Brad Bishopc342db32019-05-15 21:57:59 -0400885 color = STATE_COLORS[state]
886 if state == STATE_RUNNING:
887 alpha = min (sample.cpu_sample.user + sample.cpu_sample.sys, 1.0)
888 color = tuple(list(PROC_COLOR_R[0:3]) + [alpha])
889# print "render time %d [ tx %d tw %d ], sample state %s color %s alpha %g" % (sample.time, tx, tw, state, color, alpha)
890 elif state == STATE_SLEEPING:
891 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500892
Brad Bishopc342db32019-05-15 21:57:59 -0400893 draw_fill_rect(ctx, color, (tx, y, tw, proc_h))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500894
895def draw_process_connecting_lines(ctx, px, py, x, y, proc_h):
Brad Bishopc342db32019-05-15 21:57:59 -0400896 ctx.set_source_rgba(*DEP_COLOR)
897 ctx.set_dash([2, 2])
898 if abs(px - x) < 3:
899 dep_off_x = 3
900 dep_off_y = proc_h / 4
901 ctx.move_to(x, y + proc_h / 2)
902 ctx.line_to(px - dep_off_x, y + proc_h / 2)
903 ctx.line_to(px - dep_off_x, py - dep_off_y)
904 ctx.line_to(px, py - dep_off_y)
905 else:
906 ctx.move_to(x, y + proc_h / 2)
907 ctx.line_to(px, y + proc_h / 2)
908 ctx.line_to(px, py)
909 ctx.stroke()
910 ctx.set_dash([])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500911
912# elide the bootchart collector - it is quite distorting
913def elide_bootchart(proc):
Brad Bishopc342db32019-05-15 21:57:59 -0400914 return proc.cmd == 'bootchartd' or proc.cmd == 'bootchart-colle'
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500915
916class CumlSample:
Brad Bishopc342db32019-05-15 21:57:59 -0400917 def __init__(self, proc):
918 self.cmd = proc.cmd
919 self.samples = []
920 self.merge_samples (proc)
921 self.color = None
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500922
Brad Bishopc342db32019-05-15 21:57:59 -0400923 def merge_samples(self, proc):
924 self.samples.extend (proc.samples)
925 self.samples.sort (key = lambda p: p.time)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500926
Brad Bishopc342db32019-05-15 21:57:59 -0400927 def next(self):
928 global palette_idx
929 palette_idx += HSV_STEP
930 return palette_idx
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500931
Brad Bishopc342db32019-05-15 21:57:59 -0400932 def get_color(self):
933 if self.color is None:
934 i = self.next() % HSV_MAX_MOD
935 h = 0.0
Patrick Williamsdb4c27e2022-08-05 08:10:29 -0500936 if i != 0:
Brad Bishopc342db32019-05-15 21:57:59 -0400937 h = (1.0 * i) / HSV_MAX_MOD
938 s = 0.5
939 v = 1.0
940 c = colorsys.hsv_to_rgb (h, s, v)
941 self.color = (c[0], c[1], c[2], 1.0)
942 return self.color
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500943
944
945def draw_cuml_graph(ctx, proc_tree, chart_bounds, duration, sec_w, stat_type):
Brad Bishopc342db32019-05-15 21:57:59 -0400946 global palette_idx
947 palette_idx = 0
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500948
Brad Bishopc342db32019-05-15 21:57:59 -0400949 time_hash = {}
950 total_time = 0.0
951 m_proc_list = {}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500952
Brad Bishopc342db32019-05-15 21:57:59 -0400953 if stat_type is STAT_TYPE_CPU:
954 sample_value = 'cpu'
955 else:
956 sample_value = 'io'
957 for proc in proc_tree.process_list:
958 if elide_bootchart(proc):
959 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500960
Brad Bishopc342db32019-05-15 21:57:59 -0400961 for sample in proc.samples:
962 total_time += getattr(sample.cpu_sample, sample_value)
963 if not sample.time in time_hash:
964 time_hash[sample.time] = 1
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500965
Brad Bishopc342db32019-05-15 21:57:59 -0400966 # merge pids with the same cmd
967 if not proc.cmd in m_proc_list:
968 m_proc_list[proc.cmd] = CumlSample (proc)
969 continue
970 s = m_proc_list[proc.cmd]
971 s.merge_samples (proc)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500972
Brad Bishopc342db32019-05-15 21:57:59 -0400973 # all the sample times
974 times = sorted(time_hash)
975 if len (times) < 2:
976 print("degenerate boot chart")
977 return
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500978
Brad Bishopc342db32019-05-15 21:57:59 -0400979 pix_per_ns = chart_bounds[3] / total_time
980# print "total time: %g pix-per-ns %g" % (total_time, pix_per_ns)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500981
Brad Bishopc342db32019-05-15 21:57:59 -0400982 # FIXME: we have duplicates in the process list too [!] - why !?
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500983
Brad Bishopc342db32019-05-15 21:57:59 -0400984 # Render bottom up, left to right
985 below = {}
986 for time in times:
987 below[time] = chart_bounds[1] + chart_bounds[3]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500988
Brad Bishopc342db32019-05-15 21:57:59 -0400989 # same colors each time we render
990 random.seed (0)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500991
Brad Bishopc342db32019-05-15 21:57:59 -0400992 ctx.set_line_width(1)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500993
Brad Bishopc342db32019-05-15 21:57:59 -0400994 legends = []
995 labels = []
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500996
Brad Bishopc342db32019-05-15 21:57:59 -0400997 # render each pid in order
998 for cs in m_proc_list.values():
999 row = {}
1000 cuml = 0.0
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001001
Brad Bishopc342db32019-05-15 21:57:59 -04001002 # print "pid : %s -> %g samples %d" % (proc.cmd, cuml, len (cs.samples))
1003 for sample in cs.samples:
1004 cuml += getattr(sample.cpu_sample, sample_value)
1005 row[sample.time] = cuml
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001006
Brad Bishopc342db32019-05-15 21:57:59 -04001007 process_total_time = cuml
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001008
Brad Bishopc342db32019-05-15 21:57:59 -04001009 # hide really tiny processes
1010 if cuml * pix_per_ns <= 2:
1011 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001012
Brad Bishopc342db32019-05-15 21:57:59 -04001013 last_time = times[0]
1014 y = last_below = below[last_time]
1015 last_cuml = cuml = 0.0
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001016
Brad Bishopc342db32019-05-15 21:57:59 -04001017 ctx.set_source_rgba(*cs.get_color())
1018 for time in times:
1019 render_seg = False
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001020
Brad Bishopc342db32019-05-15 21:57:59 -04001021 # did the underlying trend increase ?
1022 if below[time] != last_below:
1023 last_below = below[last_time]
1024 last_cuml = cuml
1025 render_seg = True
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001026
Brad Bishopc342db32019-05-15 21:57:59 -04001027 # did we move up a pixel increase ?
1028 if time in row:
1029 nc = round (row[time] * pix_per_ns)
1030 if nc != cuml:
1031 last_cuml = cuml
1032 cuml = nc
1033 render_seg = True
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001034
Brad Bishopc342db32019-05-15 21:57:59 -04001035# if last_cuml > cuml:
1036# assert fail ... - un-sorted process samples
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001037
Brad Bishopc342db32019-05-15 21:57:59 -04001038 # draw the trailing rectangle from the last time to
1039 # before now, at the height of the last segment.
1040 if render_seg:
1041 w = math.ceil ((time - last_time) * chart_bounds[2] / proc_tree.duration) + 1
1042 x = chart_bounds[0] + round((last_time - proc_tree.start_time) * chart_bounds[2] / proc_tree.duration)
1043 ctx.rectangle (x, below[last_time] - last_cuml, w, last_cuml)
1044 ctx.fill()
1045# ctx.stroke()
1046 last_time = time
1047 y = below [time] - cuml
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001048
Brad Bishopc342db32019-05-15 21:57:59 -04001049 row[time] = y
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001050
Brad Bishopc342db32019-05-15 21:57:59 -04001051 # render the last segment
1052 x = chart_bounds[0] + round((last_time - proc_tree.start_time) * chart_bounds[2] / proc_tree.duration)
1053 y = below[last_time] - cuml
1054 ctx.rectangle (x, y, chart_bounds[2] - x, cuml)
1055 ctx.fill()
1056# ctx.stroke()
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001057
Brad Bishopc342db32019-05-15 21:57:59 -04001058 # render legend if it will fit
1059 if cuml > 8:
1060 label = cs.cmd
1061 extnts = ctx.text_extents(label)
1062 label_w = extnts[2]
1063 label_h = extnts[3]
1064# print "Text extents %g by %g" % (label_w, label_h)
1065 labels.append((label,
1066 chart_bounds[0] + chart_bounds[2] - label_w - off_x * 2,
1067 y + (cuml + label_h) / 2))
1068 if cs in legends:
1069 print("ARGH - duplicate process in list !")
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001070
Brad Bishopc342db32019-05-15 21:57:59 -04001071 legends.append ((cs, process_total_time))
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001072
Brad Bishopc342db32019-05-15 21:57:59 -04001073 below = row
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001074
Brad Bishopc342db32019-05-15 21:57:59 -04001075 # render grid-lines over the top
1076 draw_box_ticks(ctx, chart_bounds, sec_w)
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001077
Brad Bishopc342db32019-05-15 21:57:59 -04001078 # render labels
1079 for l in labels:
1080 draw_text(ctx, l[0], TEXT_COLOR, l[1], l[2])
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001081
Brad Bishopc342db32019-05-15 21:57:59 -04001082 # Render legends
1083 font_height = 20
1084 label_width = 300
1085 LEGENDS_PER_COL = 15
1086 LEGENDS_TOTAL = 45
1087 ctx.set_font_size (TITLE_FONT_SIZE)
1088 dur_secs = duration / 100
1089 cpu_secs = total_time / 1000000000
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001090
Brad Bishopc342db32019-05-15 21:57:59 -04001091 # misleading - with multiple CPUs ...
1092# idle = ((dur_secs - cpu_secs) / dur_secs) * 100.0
1093 if stat_type is STAT_TYPE_CPU:
1094 label = "Cumulative CPU usage, by process; total CPU: " \
1095 " %.5g(s) time: %.3g(s)" % (cpu_secs, dur_secs)
1096 else:
1097 label = "Cumulative I/O usage, by process; total I/O: " \
1098 " %.5g(s) time: %.3g(s)" % (cpu_secs, dur_secs)
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001099
Brad Bishopc342db32019-05-15 21:57:59 -04001100 draw_text(ctx, label, TEXT_COLOR, chart_bounds[0] + off_x,
1101 chart_bounds[1] + font_height)
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001102
Brad Bishopc342db32019-05-15 21:57:59 -04001103 i = 0
1104 legends = sorted(legends, key=itemgetter(1), reverse=True)
1105 ctx.set_font_size(TEXT_FONT_SIZE)
1106 for t in legends:
1107 cs = t[0]
1108 time = t[1]
1109 x = chart_bounds[0] + off_x + int (i/LEGENDS_PER_COL) * label_width
1110 y = chart_bounds[1] + font_height * ((i % LEGENDS_PER_COL) + 2)
1111 str = "%s - %.0f(ms) (%2.2f%%)" % (cs.cmd, time/1000000, (time/total_time) * 100.0)
1112 draw_legend_box(ctx, str, cs.color, x, y, leg_s)
1113 i = i + 1
1114 if i >= LEGENDS_TOTAL:
1115 break