blob: 4326361426391a4bd1a5553381df9a4e903f1fac [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
359 if trace.monitor_disk:
360 h += 30 + bar_h
361 if trace.mem_stats:
362 h += meminfo_bar_h
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500363
Brad Bishopc342db32019-05-15 21:57:59 -0400364 # Allow for width of process legend and offset
365 if w < (720 + off_x):
366 w = 720 + off_x
367
368 return (w, h)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500369
370def clip_visible(clip, rect):
Brad Bishopc342db32019-05-15 21:57:59 -0400371 xmax = max (clip[0], rect[0])
372 ymax = max (clip[1], rect[1])
373 xmin = min (clip[0] + clip[2], rect[0] + rect[2])
374 ymin = min (clip[1] + clip[3], rect[1] + rect[3])
375 return (xmin > xmax and ymin > ymax)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500376
377def render_charts(ctx, options, clip, trace, curr_y, w, h, sec_w):
Brad Bishopc342db32019-05-15 21:57:59 -0400378 proc_tree = options.proc_tree(trace)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500379
Brad Bishopc342db32019-05-15 21:57:59 -0400380 # render bar legend
381 if trace.cpu_stats:
382 ctx.set_font_size(LEGEND_FONT_SIZE)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500383
Brad Bishopc342db32019-05-15 21:57:59 -0400384 draw_legend_box(ctx, "CPU (user+sys)", CPU_COLOR, off_x, curr_y+20, leg_s)
385 draw_legend_box(ctx, "I/O (wait)", IO_COLOR, off_x + 120, curr_y+20, leg_s)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500386
Brad Bishopc342db32019-05-15 21:57:59 -0400387 # render I/O wait
388 chart_rect = (off_x, curr_y+30, w, bar_h)
389 if clip_visible (clip, chart_rect):
390 draw_box_ticks (ctx, chart_rect, sec_w)
391 draw_annotations (ctx, proc_tree, trace.times, chart_rect)
392 draw_chart (ctx, IO_COLOR, True, chart_rect, \
393 [(sample.time, sample.user + sample.sys + sample.io) for sample in trace.cpu_stats], \
394 proc_tree, None)
395 # render CPU load
396 draw_chart (ctx, CPU_COLOR, True, chart_rect, \
397 [(sample.time, sample.user + sample.sys) for sample in trace.cpu_stats], \
398 proc_tree, None)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500399
Brad Bishopc342db32019-05-15 21:57:59 -0400400 curr_y = curr_y + 30 + bar_h
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500401
Brad Bishopc342db32019-05-15 21:57:59 -0400402 # render second chart
403 if trace.disk_stats:
404 draw_legend_line(ctx, "Disk throughput", DISK_TPUT_COLOR, off_x, curr_y+20, leg_s)
405 draw_legend_box(ctx, "Disk utilization", IO_COLOR, off_x + 120, curr_y+20, leg_s)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500406
Brad Bishopc342db32019-05-15 21:57:59 -0400407 # render I/O utilization
408 chart_rect = (off_x, curr_y+30, w, bar_h)
409 if clip_visible (clip, chart_rect):
410 draw_box_ticks (ctx, chart_rect, sec_w)
411 draw_annotations (ctx, proc_tree, trace.times, chart_rect)
412 draw_chart (ctx, IO_COLOR, True, chart_rect, \
413 [(sample.time, sample.util) for sample in trace.disk_stats], \
414 proc_tree, None)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500415
Brad Bishopc342db32019-05-15 21:57:59 -0400416 # render disk throughput
417 max_sample = max (trace.disk_stats, key = lambda s: s.tput)
418 if clip_visible (clip, chart_rect):
419 draw_chart (ctx, DISK_TPUT_COLOR, False, chart_rect, \
420 [(sample.time, sample.tput) for sample in trace.disk_stats], \
421 proc_tree, None)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500422
Brad Bishopc342db32019-05-15 21:57:59 -0400423 pos_x = off_x + ((max_sample.time - proc_tree.start_time) * w / proc_tree.duration)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500424
Brad Bishopc342db32019-05-15 21:57:59 -0400425 shift_x, shift_y = -20, 20
426 if (pos_x < off_x + 245):
427 shift_x, shift_y = 5, 40
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500428
Brad Bishopc342db32019-05-15 21:57:59 -0400429 label = "%dMB/s" % round ((max_sample.tput) / 1024.0)
430 draw_text (ctx, label, DISK_TPUT_COLOR, pos_x + shift_x, curr_y + shift_y)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500431
Brad Bishopc342db32019-05-15 21:57:59 -0400432 curr_y = curr_y + 30 + bar_h
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500433
Andrew Geissler615f2f12022-07-15 14:00:58 -0500434 # render CPU pressure chart
435 if trace.cpu_pressure:
Patrick Williamsdb4c27e2022-08-05 08:10:29 -0500436 max_sample_avg = max (trace.cpu_pressure, key = lambda s: s.avg10)
437 max_sample_total = max (trace.cpu_pressure, key = lambda s: s.deltaTotal)
438 draw_legend_line(ctx, "avg10 CPU Pressure (max %d%%)" % (max_sample_avg.avg10), CPU_PRESSURE_AVG10_COLOR, off_x, curr_y+20, leg_s)
439 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 -0500440
441 # render delta total cpu
442 chart_rect = (off_x, curr_y+30, w, bar_h)
443 if clip_visible (clip, chart_rect):
444 draw_box_ticks (ctx, chart_rect, sec_w)
445 draw_annotations (ctx, proc_tree, trace.times, chart_rect)
446 draw_chart (ctx, CPU_PRESSURE_TOTAL_COLOR, True, chart_rect, \
447 [(sample.time, sample.deltaTotal) for sample in trace.cpu_pressure], \
448 proc_tree, None)
449
450 # render avg10 cpu
Andrew Geissler615f2f12022-07-15 14:00:58 -0500451 if clip_visible (clip, chart_rect):
452 draw_chart (ctx, CPU_PRESSURE_AVG10_COLOR, False, chart_rect, \
453 [(sample.time, sample.avg10) for sample in trace.cpu_pressure], \
454 proc_tree, None)
455
Patrick Williamsdb4c27e2022-08-05 08:10:29 -0500456 pos_x = off_x + ((max_sample_avg.time - proc_tree.start_time) * w / proc_tree.duration)
Andrew Geissler615f2f12022-07-15 14:00:58 -0500457
458 shift_x, shift_y = -20, 20
459 if (pos_x < off_x + 245):
460 shift_x, shift_y = 5, 40
461
462
Patrick Williamsdb4c27e2022-08-05 08:10:29 -0500463 label = "%d%%" % (max_sample_avg.avg10)
Andrew Geissler615f2f12022-07-15 14:00:58 -0500464 draw_text (ctx, label, CPU_PRESSURE_AVG10_COLOR, pos_x + shift_x, curr_y + shift_y)
465
466 curr_y = curr_y + 30 + bar_h
467
Patrick Williamsdb4c27e2022-08-05 08:10:29 -0500468 # render I/O pressure chart
Andrew Geissler615f2f12022-07-15 14:00:58 -0500469 if trace.io_pressure:
Patrick Williamsdb4c27e2022-08-05 08:10:29 -0500470 max_sample_avg = max (trace.io_pressure, key = lambda s: s.avg10)
471 max_sample_total = max (trace.io_pressure, key = lambda s: s.deltaTotal)
472 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)
473 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 -0500474
Patrick Williamsdb4c27e2022-08-05 08:10:29 -0500475 # render delta total io
Andrew Geissler615f2f12022-07-15 14:00:58 -0500476 chart_rect = (off_x, curr_y+30, w, bar_h)
477 if clip_visible (clip, chart_rect):
478 draw_box_ticks (ctx, chart_rect, sec_w)
479 draw_annotations (ctx, proc_tree, trace.times, chart_rect)
480 draw_chart (ctx, IO_PRESSURE_TOTAL_COLOR, True, chart_rect, \
481 [(sample.time, sample.deltaTotal) for sample in trace.io_pressure], \
482 proc_tree, None)
483
Patrick Williamsdb4c27e2022-08-05 08:10:29 -0500484 # render avg10 io
Andrew Geissler615f2f12022-07-15 14:00:58 -0500485 if clip_visible (clip, chart_rect):
486 draw_chart (ctx, IO_PRESSURE_AVG10_COLOR, False, chart_rect, \
487 [(sample.time, sample.avg10) for sample in trace.io_pressure], \
488 proc_tree, None)
489
Patrick Williamsdb4c27e2022-08-05 08:10:29 -0500490 pos_x = off_x + ((max_sample_avg.time - proc_tree.start_time) * w / proc_tree.duration)
Andrew Geissler615f2f12022-07-15 14:00:58 -0500491
492 shift_x, shift_y = -20, 20
493 if (pos_x < off_x + 245):
494 shift_x, shift_y = 5, 40
495
Patrick Williamsdb4c27e2022-08-05 08:10:29 -0500496
497 label = "%d%%" % (max_sample_avg.avg10)
Andrew Geissler615f2f12022-07-15 14:00:58 -0500498 draw_text (ctx, label, IO_PRESSURE_AVG10_COLOR, pos_x + shift_x, curr_y + shift_y)
499
500 curr_y = curr_y + 30 + bar_h
501
Patrick Williamsdb4c27e2022-08-05 08:10:29 -0500502 # render MEM pressure chart
503 if trace.mem_pressure:
504 max_sample_avg = max (trace.mem_pressure, key = lambda s: s.avg10)
505 max_sample_total = max (trace.mem_pressure, key = lambda s: s.deltaTotal)
506 draw_legend_line(ctx, "avg10 MEM Pressure (max %d%%)" % (max_sample_avg.avg10), MEM_PRESSURE_AVG10_COLOR, off_x, curr_y+20, leg_s)
507 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)
508
509 # render delta total mem
510 chart_rect = (off_x, curr_y+30, w, bar_h)
511 if clip_visible (clip, chart_rect):
512 draw_box_ticks (ctx, chart_rect, sec_w)
513 draw_annotations (ctx, proc_tree, trace.times, chart_rect)
514 draw_chart (ctx, MEM_PRESSURE_TOTAL_COLOR, True, chart_rect, \
515 [(sample.time, sample.deltaTotal) for sample in trace.mem_pressure], \
516 proc_tree, None)
517
518 # render avg10 mem
519 if clip_visible (clip, chart_rect):
520 draw_chart (ctx, MEM_PRESSURE_AVG10_COLOR, False, chart_rect, \
521 [(sample.time, sample.avg10) for sample in trace.mem_pressure], \
522 proc_tree, None)
523
524 pos_x = off_x + ((max_sample_avg.time - proc_tree.start_time) * w / proc_tree.duration)
525
526 shift_x, shift_y = -20, 20
527 if (pos_x < off_x + 245):
528 shift_x, shift_y = 5, 40
529
530
531 label = "%d%%" % (max_sample_avg.avg10)
532 draw_text (ctx, label, MEM_PRESSURE_AVG10_COLOR, pos_x + shift_x, curr_y + shift_y)
533
534 curr_y = curr_y + 30 + bar_h
535
Brad Bishopc342db32019-05-15 21:57:59 -0400536 # render disk space usage
537 #
538 # Draws the amount of disk space used on each volume relative to the
539 # lowest recorded amount. The graphs for each volume are stacked above
540 # each other so that total disk usage is visible.
541 if trace.monitor_disk:
542 ctx.set_font_size(LEGEND_FONT_SIZE)
543 # Determine set of volumes for which we have
544 # information and the minimal amount of used disk
545 # space for each. Currently samples are allowed to
546 # not have a values for all volumes; drawing could be
547 # made more efficient if that wasn't the case.
548 volumes = set()
549 min_used = {}
550 for sample in trace.monitor_disk:
551 for volume, used in sample.records.items():
552 volumes.add(volume)
553 if volume not in min_used or min_used[volume] > used:
554 min_used[volume] = used
555 volumes = sorted(list(volumes))
556 disk_scale = 0
557 for i, volume in enumerate(volumes):
558 volume_scale = max([sample.records[volume] - min_used[volume]
559 for sample in trace.monitor_disk
560 if volume in sample.records])
561 # Does not take length of volume name into account, but fixed offset
562 # works okay in practice.
563 draw_legend_box(ctx, '%s (max: %u MiB)' % (volume, volume_scale / 1024 / 1024),
564 VOLUME_COLORS[i % len(VOLUME_COLORS)],
565 off_x + i * 250, curr_y+20, leg_s)
566 disk_scale += volume_scale
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500567
Brad Bishopc342db32019-05-15 21:57:59 -0400568 # render used amount of disk space
569 chart_rect = (off_x, curr_y+30, w, bar_h)
570 if clip_visible (clip, chart_rect):
571 draw_box_ticks (ctx, chart_rect, sec_w)
572 draw_annotations (ctx, proc_tree, trace.times, chart_rect)
573 for i in range(len(volumes), 0, -1):
574 draw_chart (ctx, VOLUME_COLORS[(i - 1) % len(VOLUME_COLORS)], True, chart_rect, \
575 [(sample.time,
576 # Sum up used space of all volumes including the current one
577 # so that the graphs appear as stacked on top of each other.
578 functools.reduce(lambda x,y: x+y,
579 [sample.records[volume] - min_used[volume]
580 for volume in volumes[0:i]
581 if volume in sample.records],
582 0))
583 for sample in trace.monitor_disk], \
584 proc_tree, [0, disk_scale])
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500585
Brad Bishopc342db32019-05-15 21:57:59 -0400586 curr_y = curr_y + 30 + bar_h
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500587
Brad Bishopc342db32019-05-15 21:57:59 -0400588 # render mem usage
589 chart_rect = (off_x, curr_y+30, w, meminfo_bar_h)
590 mem_stats = trace.mem_stats
591 if mem_stats and clip_visible (clip, chart_rect):
592 mem_scale = max(sample.buffers for sample in mem_stats)
593 draw_legend_box(ctx, "Mem cached (scale: %u MiB)" % (float(mem_scale) / 1024), MEM_CACHED_COLOR, off_x, curr_y+20, leg_s)
594 draw_legend_box(ctx, "Used", MEM_USED_COLOR, off_x + 240, curr_y+20, leg_s)
595 draw_legend_box(ctx, "Buffers", MEM_BUFFERS_COLOR, off_x + 360, curr_y+20, leg_s)
596 draw_legend_line(ctx, "Swap (scale: %u MiB)" % max([(sample.swap)/1024 for sample in mem_stats]), \
597 MEM_SWAP_COLOR, off_x + 480, curr_y+20, leg_s)
598 draw_box_ticks(ctx, chart_rect, sec_w)
599 draw_annotations(ctx, proc_tree, trace.times, chart_rect)
600 draw_chart(ctx, MEM_BUFFERS_COLOR, True, chart_rect, \
601 [(sample.time, sample.buffers) for sample in trace.mem_stats], \
602 proc_tree, [0, mem_scale])
603 draw_chart(ctx, MEM_USED_COLOR, True, chart_rect, \
604 [(sample.time, sample.used) for sample in mem_stats], \
605 proc_tree, [0, mem_scale])
606 draw_chart(ctx, MEM_CACHED_COLOR, True, chart_rect, \
607 [(sample.time, sample.cached) for sample in mem_stats], \
608 proc_tree, [0, mem_scale])
609 draw_chart(ctx, MEM_SWAP_COLOR, False, chart_rect, \
610 [(sample.time, float(sample.swap)) for sample in mem_stats], \
611 proc_tree, None)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500612
Brad Bishopc342db32019-05-15 21:57:59 -0400613 curr_y = curr_y + meminfo_bar_h
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500614
Brad Bishopc342db32019-05-15 21:57:59 -0400615 return curr_y
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500616
617def render_processes_chart(ctx, options, trace, curr_y, w, h, sec_w):
Brad Bishopc342db32019-05-15 21:57:59 -0400618 chart_rect = [off_x, curr_y+header_h, w, h - curr_y - 1 * off_y - header_h ]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500619
Brad Bishopc342db32019-05-15 21:57:59 -0400620 draw_legend_box (ctx, "Configure", \
621 TASK_COLOR_CONFIGURE, off_x , curr_y + 45, leg_s)
622 draw_legend_box (ctx, "Compile", \
623 TASK_COLOR_COMPILE, off_x+120, curr_y + 45, leg_s)
624 draw_legend_box (ctx, "Install", \
625 TASK_COLOR_INSTALL, off_x+240, curr_y + 45, leg_s)
626 draw_legend_box (ctx, "Populate Sysroot", \
627 TASK_COLOR_SYSROOT, off_x+360, curr_y + 45, leg_s)
628 draw_legend_box (ctx, "Package", \
629 TASK_COLOR_PACKAGE, off_x+480, curr_y + 45, leg_s)
630 draw_legend_box (ctx, "Package Write", \
631 TASK_COLOR_PACKAGE_WRITE, off_x+600, curr_y + 45, leg_s)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500632
Brad Bishopc342db32019-05-15 21:57:59 -0400633 ctx.set_font_size(PROC_TEXT_FONT_SIZE)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500634
Brad Bishopc342db32019-05-15 21:57:59 -0400635 draw_box_ticks(ctx, chart_rect, sec_w)
636 draw_sec_labels(ctx, options, chart_rect, sec_w, 30)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500637
Brad Bishopc342db32019-05-15 21:57:59 -0400638 y = curr_y+header_h
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500639
Brad Bishopc342db32019-05-15 21:57:59 -0400640 offset = trace.min or min(trace.start.keys())
641 for start in sorted(trace.start.keys()):
642 for process in sorted(trace.start[start]):
643 if not options.app_options.show_all and \
644 trace.processes[process][1] - start < options.app_options.mintime:
645 continue
646 task = process.split(":")[1]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500647
Brad Bishopc342db32019-05-15 21:57:59 -0400648 #print(process)
649 #print(trace.processes[process][1])
650 #print(s)
651
652 x = chart_rect[0] + (start - offset) * sec_w
653 w = ((trace.processes[process][1] - start) * sec_w)
654
655 #print("proc at %s %s %s %s" % (x, y, w, proc_h))
656 col = None
657 if task == "do_compile":
658 col = TASK_COLOR_COMPILE
659 elif task == "do_configure":
660 col = TASK_COLOR_CONFIGURE
661 elif task == "do_install":
662 col = TASK_COLOR_INSTALL
663 elif task == "do_populate_sysroot":
664 col = TASK_COLOR_SYSROOT
665 elif task == "do_package":
666 col = TASK_COLOR_PACKAGE
667 elif task == "do_package_write_rpm" or \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500668 task == "do_package_write_deb" or \
669 task == "do_package_write_ipk":
Brad Bishopc342db32019-05-15 21:57:59 -0400670 col = TASK_COLOR_PACKAGE_WRITE
671 else:
672 col = WHITE
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500673
Brad Bishopc342db32019-05-15 21:57:59 -0400674 if col:
675 draw_fill_rect(ctx, col, (x, y, w, proc_h))
676 draw_rect(ctx, PROC_BORDER_COLOR, (x, y, w, proc_h))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500677
Brad Bishopc342db32019-05-15 21:57:59 -0400678 draw_label_in_box(ctx, PROC_TEXT_COLOR, process, x, y + proc_h - 4, w, proc_h)
679 y = y + proc_h
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500680
Brad Bishopc342db32019-05-15 21:57:59 -0400681 return curr_y
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500682
683#
684# Render the chart.
685#
686def render(ctx, options, xscale, trace):
Brad Bishopc342db32019-05-15 21:57:59 -0400687 (w, h) = extents (options, xscale, trace)
688 global OPTIONS
689 OPTIONS = options.app_options
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500690
Brad Bishopc342db32019-05-15 21:57:59 -0400691 # x, y, w, h
692 clip = ctx.clip_extents()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500693
Brad Bishopc342db32019-05-15 21:57:59 -0400694 sec_w = int (xscale * sec_w_base)
695 ctx.set_line_width(1.0)
696 ctx.select_font_face(FONT_NAME)
697 draw_fill_rect(ctx, WHITE, (0, 0, max(w, MIN_IMG_W), h))
698 w -= 2*off_x
699 curr_y = off_y;
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500700
Brad Bishopc342db32019-05-15 21:57:59 -0400701 if options.charts:
702 curr_y = render_charts (ctx, options, clip, trace, curr_y, w, h, sec_w)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500703
Brad Bishopc342db32019-05-15 21:57:59 -0400704 curr_y = render_processes_chart (ctx, options, trace, curr_y, w, h, sec_w)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500705
Brad Bishopc342db32019-05-15 21:57:59 -0400706 return
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500707
Brad Bishopc342db32019-05-15 21:57:59 -0400708 proc_tree = options.proc_tree (trace)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500709
Brad Bishopc342db32019-05-15 21:57:59 -0400710 # draw the title and headers
711 if proc_tree.idle:
712 duration = proc_tree.idle
713 else:
714 duration = proc_tree.duration
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500715
Brad Bishopc342db32019-05-15 21:57:59 -0400716 if not options.kernel_only:
717 curr_y = draw_header (ctx, trace.headers, duration)
718 else:
719 curr_y = off_y;
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500720
Brad Bishopc342db32019-05-15 21:57:59 -0400721 # draw process boxes
722 proc_height = h
723 if proc_tree.taskstats and options.cumulative:
724 proc_height -= CUML_HEIGHT
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500725
Brad Bishopc342db32019-05-15 21:57:59 -0400726 draw_process_bar_chart(ctx, clip, options, proc_tree, trace.times,
727 curr_y, w, proc_height, sec_w)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500728
Brad Bishopc342db32019-05-15 21:57:59 -0400729 curr_y = proc_height
730 ctx.set_font_size(SIG_FONT_SIZE)
731 draw_text(ctx, SIGNATURE, SIG_COLOR, off_x + 5, proc_height - 8)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500732
Brad Bishopc342db32019-05-15 21:57:59 -0400733 # draw a cumulative CPU-time-per-process graph
734 if proc_tree.taskstats and options.cumulative:
735 cuml_rect = (off_x, curr_y + off_y, w, CUML_HEIGHT/2 - off_y * 2)
736 if clip_visible (clip, cuml_rect):
737 draw_cuml_graph(ctx, proc_tree, cuml_rect, duration, sec_w, STAT_TYPE_CPU)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500738
Brad Bishopc342db32019-05-15 21:57:59 -0400739 # draw a cumulative I/O-time-per-process graph
740 if proc_tree.taskstats and options.cumulative:
741 cuml_rect = (off_x, curr_y + off_y * 100, w, CUML_HEIGHT/2 - off_y * 2)
742 if clip_visible (clip, cuml_rect):
743 draw_cuml_graph(ctx, proc_tree, cuml_rect, duration, sec_w, STAT_TYPE_IO)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500744
745def draw_process_bar_chart(ctx, clip, options, proc_tree, times, curr_y, w, h, sec_w):
Brad Bishopc342db32019-05-15 21:57:59 -0400746 header_size = 0
747 if not options.kernel_only:
748 draw_legend_box (ctx, "Running (%cpu)",
749 PROC_COLOR_R, off_x , curr_y + 45, leg_s)
750 draw_legend_box (ctx, "Unint.sleep (I/O)",
751 PROC_COLOR_D, off_x+120, curr_y + 45, leg_s)
752 draw_legend_box (ctx, "Sleeping",
753 PROC_COLOR_S, off_x+240, curr_y + 45, leg_s)
754 draw_legend_box (ctx, "Zombie",
755 PROC_COLOR_Z, off_x+360, curr_y + 45, leg_s)
756 header_size = 45
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500757
Brad Bishopc342db32019-05-15 21:57:59 -0400758 chart_rect = [off_x, curr_y + header_size + 15,
759 w, h - 2 * off_y - (curr_y + header_size + 15) + proc_h]
760 ctx.set_font_size (PROC_TEXT_FONT_SIZE)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500761
Brad Bishopc342db32019-05-15 21:57:59 -0400762 draw_box_ticks (ctx, chart_rect, sec_w)
763 if sec_w > 100:
764 nsec = 1
765 else:
766 nsec = 5
767 draw_sec_labels (ctx, options, chart_rect, sec_w, nsec)
768 draw_annotations (ctx, proc_tree, times, chart_rect)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500769
Brad Bishopc342db32019-05-15 21:57:59 -0400770 y = curr_y + 60
771 for root in proc_tree.process_tree:
772 draw_processes_recursively(ctx, root, proc_tree, y, proc_h, chart_rect, clip)
773 y = y + proc_h * proc_tree.num_nodes([root])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500774
775
776def draw_header (ctx, headers, duration):
777 toshow = [
778 ('system.uname', 'uname', lambda s: s),
779 ('system.release', 'release', lambda s: s),
780 ('system.cpu', 'CPU', lambda s: re.sub('model name\s*:\s*', '', s, 1)),
781 ('system.kernel.options', 'kernel options', lambda s: s),
782 ]
783
784 header_y = ctx.font_extents()[2] + 10
785 ctx.set_font_size(TITLE_FONT_SIZE)
786 draw_text(ctx, headers['title'], TEXT_COLOR, off_x, header_y)
787 ctx.set_font_size(TEXT_FONT_SIZE)
788
789 for (headerkey, headertitle, mangle) in toshow:
790 header_y += ctx.font_extents()[2]
791 if headerkey in headers:
792 value = headers.get(headerkey)
793 else:
794 value = ""
795 txt = headertitle + ': ' + mangle(value)
796 draw_text(ctx, txt, TEXT_COLOR, off_x, header_y)
797
798 dur = duration / 100.0
799 txt = 'time : %02d:%05.2f' % (math.floor(dur/60), dur - 60 * math.floor(dur/60))
800 if headers.get('system.maxpid') is not None:
801 txt = txt + ' max pid: %s' % (headers.get('system.maxpid'))
802
803 header_y += ctx.font_extents()[2]
804 draw_text (ctx, txt, TEXT_COLOR, off_x, header_y)
805
806 return header_y
807
808def draw_processes_recursively(ctx, proc, proc_tree, y, proc_h, rect, clip) :
Brad Bishopc342db32019-05-15 21:57:59 -0400809 x = rect[0] + ((proc.start_time - proc_tree.start_time) * rect[2] / proc_tree.duration)
810 w = ((proc.duration) * rect[2] / proc_tree.duration)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500811
Brad Bishopc342db32019-05-15 21:57:59 -0400812 draw_process_activity_colors(ctx, proc, proc_tree, x, y, w, proc_h, rect, clip)
813 draw_rect(ctx, PROC_BORDER_COLOR, (x, y, w, proc_h))
814 ipid = int(proc.pid)
815 if not OPTIONS.show_all:
816 cmdString = proc.cmd
817 else:
818 cmdString = ''
Patrick Williamsdb4c27e2022-08-05 08:10:29 -0500819 if (OPTIONS.show_pid or OPTIONS.show_all) and ipid != 0:
Brad Bishopc342db32019-05-15 21:57:59 -0400820 cmdString = cmdString + " [" + str(ipid // 1000) + "]"
821 if OPTIONS.show_all:
822 if proc.args:
823 cmdString = cmdString + " '" + "' '".join(proc.args) + "'"
824 else:
825 cmdString = cmdString + " " + proc.exe
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500826
Brad Bishopc342db32019-05-15 21:57:59 -0400827 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 -0500828
Brad Bishopc342db32019-05-15 21:57:59 -0400829 next_y = y + proc_h
830 for child in proc.child_list:
831 if next_y > clip[1] + clip[3]:
832 break
833 child_x, child_y = draw_processes_recursively(ctx, child, proc_tree, next_y, proc_h, rect, clip)
834 draw_process_connecting_lines(ctx, x, y, child_x, child_y, proc_h)
835 next_y = next_y + proc_h * proc_tree.num_nodes([child])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500836
Brad Bishopc342db32019-05-15 21:57:59 -0400837 return x, y
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500838
839
840def draw_process_activity_colors(ctx, proc, proc_tree, x, y, w, proc_h, rect, clip):
841
Brad Bishopc342db32019-05-15 21:57:59 -0400842 if y > clip[1] + clip[3] or y + proc_h + 2 < clip[1]:
843 return
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500844
Brad Bishopc342db32019-05-15 21:57:59 -0400845 draw_fill_rect(ctx, PROC_COLOR_S, (x, y, w, proc_h))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500846
Brad Bishopc342db32019-05-15 21:57:59 -0400847 last_tx = -1
848 for sample in proc.samples :
849 tx = rect[0] + round(((sample.time - proc_tree.start_time) * rect[2] / proc_tree.duration))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500850
Brad Bishopc342db32019-05-15 21:57:59 -0400851 # samples are sorted chronologically
852 if tx < clip[0]:
853 continue
854 if tx > clip[0] + clip[2]:
855 break
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500856
Brad Bishopc342db32019-05-15 21:57:59 -0400857 tw = round(proc_tree.sample_period * rect[2] / float(proc_tree.duration))
858 if last_tx != -1 and abs(last_tx - tx) <= tw:
859 tw -= last_tx - tx
860 tx = last_tx
861 tw = max (tw, 1) # nice to see at least something
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500862
Brad Bishopc342db32019-05-15 21:57:59 -0400863 last_tx = tx + tw
864 state = get_proc_state( sample.state )
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500865
Brad Bishopc342db32019-05-15 21:57:59 -0400866 color = STATE_COLORS[state]
867 if state == STATE_RUNNING:
868 alpha = min (sample.cpu_sample.user + sample.cpu_sample.sys, 1.0)
869 color = tuple(list(PROC_COLOR_R[0:3]) + [alpha])
870# print "render time %d [ tx %d tw %d ], sample state %s color %s alpha %g" % (sample.time, tx, tw, state, color, alpha)
871 elif state == STATE_SLEEPING:
872 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500873
Brad Bishopc342db32019-05-15 21:57:59 -0400874 draw_fill_rect(ctx, color, (tx, y, tw, proc_h))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500875
876def draw_process_connecting_lines(ctx, px, py, x, y, proc_h):
Brad Bishopc342db32019-05-15 21:57:59 -0400877 ctx.set_source_rgba(*DEP_COLOR)
878 ctx.set_dash([2, 2])
879 if abs(px - x) < 3:
880 dep_off_x = 3
881 dep_off_y = proc_h / 4
882 ctx.move_to(x, y + proc_h / 2)
883 ctx.line_to(px - dep_off_x, y + proc_h / 2)
884 ctx.line_to(px - dep_off_x, py - dep_off_y)
885 ctx.line_to(px, py - dep_off_y)
886 else:
887 ctx.move_to(x, y + proc_h / 2)
888 ctx.line_to(px, y + proc_h / 2)
889 ctx.line_to(px, py)
890 ctx.stroke()
891 ctx.set_dash([])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500892
893# elide the bootchart collector - it is quite distorting
894def elide_bootchart(proc):
Brad Bishopc342db32019-05-15 21:57:59 -0400895 return proc.cmd == 'bootchartd' or proc.cmd == 'bootchart-colle'
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500896
897class CumlSample:
Brad Bishopc342db32019-05-15 21:57:59 -0400898 def __init__(self, proc):
899 self.cmd = proc.cmd
900 self.samples = []
901 self.merge_samples (proc)
902 self.color = None
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500903
Brad Bishopc342db32019-05-15 21:57:59 -0400904 def merge_samples(self, proc):
905 self.samples.extend (proc.samples)
906 self.samples.sort (key = lambda p: p.time)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500907
Brad Bishopc342db32019-05-15 21:57:59 -0400908 def next(self):
909 global palette_idx
910 palette_idx += HSV_STEP
911 return palette_idx
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500912
Brad Bishopc342db32019-05-15 21:57:59 -0400913 def get_color(self):
914 if self.color is None:
915 i = self.next() % HSV_MAX_MOD
916 h = 0.0
Patrick Williamsdb4c27e2022-08-05 08:10:29 -0500917 if i != 0:
Brad Bishopc342db32019-05-15 21:57:59 -0400918 h = (1.0 * i) / HSV_MAX_MOD
919 s = 0.5
920 v = 1.0
921 c = colorsys.hsv_to_rgb (h, s, v)
922 self.color = (c[0], c[1], c[2], 1.0)
923 return self.color
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500924
925
926def draw_cuml_graph(ctx, proc_tree, chart_bounds, duration, sec_w, stat_type):
Brad Bishopc342db32019-05-15 21:57:59 -0400927 global palette_idx
928 palette_idx = 0
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500929
Brad Bishopc342db32019-05-15 21:57:59 -0400930 time_hash = {}
931 total_time = 0.0
932 m_proc_list = {}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500933
Brad Bishopc342db32019-05-15 21:57:59 -0400934 if stat_type is STAT_TYPE_CPU:
935 sample_value = 'cpu'
936 else:
937 sample_value = 'io'
938 for proc in proc_tree.process_list:
939 if elide_bootchart(proc):
940 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500941
Brad Bishopc342db32019-05-15 21:57:59 -0400942 for sample in proc.samples:
943 total_time += getattr(sample.cpu_sample, sample_value)
944 if not sample.time in time_hash:
945 time_hash[sample.time] = 1
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500946
Brad Bishopc342db32019-05-15 21:57:59 -0400947 # merge pids with the same cmd
948 if not proc.cmd in m_proc_list:
949 m_proc_list[proc.cmd] = CumlSample (proc)
950 continue
951 s = m_proc_list[proc.cmd]
952 s.merge_samples (proc)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500953
Brad Bishopc342db32019-05-15 21:57:59 -0400954 # all the sample times
955 times = sorted(time_hash)
956 if len (times) < 2:
957 print("degenerate boot chart")
958 return
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500959
Brad Bishopc342db32019-05-15 21:57:59 -0400960 pix_per_ns = chart_bounds[3] / total_time
961# print "total time: %g pix-per-ns %g" % (total_time, pix_per_ns)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500962
Brad Bishopc342db32019-05-15 21:57:59 -0400963 # FIXME: we have duplicates in the process list too [!] - why !?
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500964
Brad Bishopc342db32019-05-15 21:57:59 -0400965 # Render bottom up, left to right
966 below = {}
967 for time in times:
968 below[time] = chart_bounds[1] + chart_bounds[3]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500969
Brad Bishopc342db32019-05-15 21:57:59 -0400970 # same colors each time we render
971 random.seed (0)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500972
Brad Bishopc342db32019-05-15 21:57:59 -0400973 ctx.set_line_width(1)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500974
Brad Bishopc342db32019-05-15 21:57:59 -0400975 legends = []
976 labels = []
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500977
Brad Bishopc342db32019-05-15 21:57:59 -0400978 # render each pid in order
979 for cs in m_proc_list.values():
980 row = {}
981 cuml = 0.0
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500982
Brad Bishopc342db32019-05-15 21:57:59 -0400983 # print "pid : %s -> %g samples %d" % (proc.cmd, cuml, len (cs.samples))
984 for sample in cs.samples:
985 cuml += getattr(sample.cpu_sample, sample_value)
986 row[sample.time] = cuml
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500987
Brad Bishopc342db32019-05-15 21:57:59 -0400988 process_total_time = cuml
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500989
Brad Bishopc342db32019-05-15 21:57:59 -0400990 # hide really tiny processes
991 if cuml * pix_per_ns <= 2:
992 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500993
Brad Bishopc342db32019-05-15 21:57:59 -0400994 last_time = times[0]
995 y = last_below = below[last_time]
996 last_cuml = cuml = 0.0
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500997
Brad Bishopc342db32019-05-15 21:57:59 -0400998 ctx.set_source_rgba(*cs.get_color())
999 for time in times:
1000 render_seg = False
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001001
Brad Bishopc342db32019-05-15 21:57:59 -04001002 # did the underlying trend increase ?
1003 if below[time] != last_below:
1004 last_below = below[last_time]
1005 last_cuml = cuml
1006 render_seg = True
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001007
Brad Bishopc342db32019-05-15 21:57:59 -04001008 # did we move up a pixel increase ?
1009 if time in row:
1010 nc = round (row[time] * pix_per_ns)
1011 if nc != cuml:
1012 last_cuml = cuml
1013 cuml = nc
1014 render_seg = True
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001015
Brad Bishopc342db32019-05-15 21:57:59 -04001016# if last_cuml > cuml:
1017# assert fail ... - un-sorted process samples
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001018
Brad Bishopc342db32019-05-15 21:57:59 -04001019 # draw the trailing rectangle from the last time to
1020 # before now, at the height of the last segment.
1021 if render_seg:
1022 w = math.ceil ((time - last_time) * chart_bounds[2] / proc_tree.duration) + 1
1023 x = chart_bounds[0] + round((last_time - proc_tree.start_time) * chart_bounds[2] / proc_tree.duration)
1024 ctx.rectangle (x, below[last_time] - last_cuml, w, last_cuml)
1025 ctx.fill()
1026# ctx.stroke()
1027 last_time = time
1028 y = below [time] - cuml
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001029
Brad Bishopc342db32019-05-15 21:57:59 -04001030 row[time] = y
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001031
Brad Bishopc342db32019-05-15 21:57:59 -04001032 # render the last segment
1033 x = chart_bounds[0] + round((last_time - proc_tree.start_time) * chart_bounds[2] / proc_tree.duration)
1034 y = below[last_time] - cuml
1035 ctx.rectangle (x, y, chart_bounds[2] - x, cuml)
1036 ctx.fill()
1037# ctx.stroke()
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001038
Brad Bishopc342db32019-05-15 21:57:59 -04001039 # render legend if it will fit
1040 if cuml > 8:
1041 label = cs.cmd
1042 extnts = ctx.text_extents(label)
1043 label_w = extnts[2]
1044 label_h = extnts[3]
1045# print "Text extents %g by %g" % (label_w, label_h)
1046 labels.append((label,
1047 chart_bounds[0] + chart_bounds[2] - label_w - off_x * 2,
1048 y + (cuml + label_h) / 2))
1049 if cs in legends:
1050 print("ARGH - duplicate process in list !")
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001051
Brad Bishopc342db32019-05-15 21:57:59 -04001052 legends.append ((cs, process_total_time))
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001053
Brad Bishopc342db32019-05-15 21:57:59 -04001054 below = row
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001055
Brad Bishopc342db32019-05-15 21:57:59 -04001056 # render grid-lines over the top
1057 draw_box_ticks(ctx, chart_bounds, sec_w)
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001058
Brad Bishopc342db32019-05-15 21:57:59 -04001059 # render labels
1060 for l in labels:
1061 draw_text(ctx, l[0], TEXT_COLOR, l[1], l[2])
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001062
Brad Bishopc342db32019-05-15 21:57:59 -04001063 # Render legends
1064 font_height = 20
1065 label_width = 300
1066 LEGENDS_PER_COL = 15
1067 LEGENDS_TOTAL = 45
1068 ctx.set_font_size (TITLE_FONT_SIZE)
1069 dur_secs = duration / 100
1070 cpu_secs = total_time / 1000000000
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001071
Brad Bishopc342db32019-05-15 21:57:59 -04001072 # misleading - with multiple CPUs ...
1073# idle = ((dur_secs - cpu_secs) / dur_secs) * 100.0
1074 if stat_type is STAT_TYPE_CPU:
1075 label = "Cumulative CPU usage, by process; total CPU: " \
1076 " %.5g(s) time: %.3g(s)" % (cpu_secs, dur_secs)
1077 else:
1078 label = "Cumulative I/O usage, by process; total I/O: " \
1079 " %.5g(s) time: %.3g(s)" % (cpu_secs, dur_secs)
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001080
Brad Bishopc342db32019-05-15 21:57:59 -04001081 draw_text(ctx, label, TEXT_COLOR, chart_bounds[0] + off_x,
1082 chart_bounds[1] + font_height)
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001083
Brad Bishopc342db32019-05-15 21:57:59 -04001084 i = 0
1085 legends = sorted(legends, key=itemgetter(1), reverse=True)
1086 ctx.set_font_size(TEXT_FONT_SIZE)
1087 for t in legends:
1088 cs = t[0]
1089 time = t[1]
1090 x = chart_bounds[0] + off_x + int (i/LEGENDS_PER_COL) * label_width
1091 y = chart_bounds[1] + font_height * ((i % LEGENDS_PER_COL) + 2)
1092 str = "%s - %.0f(ms) (%2.2f%%)" % (cs.cmd, time/1000000, (time/total_time) * 100.0)
1093 draw_legend_box(ctx, str, cs.color, x, y, leg_s)
1094 i = i + 1
1095 if i >= LEGENDS_TOTAL:
1096 break