blob: 29eb7505bc20beb07e57de63a7314391c57367c6 [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
83# Process border color.
84PROC_BORDER_COLOR = (0.71, 0.71, 0.71, 1.0)
85# Waiting process color.
86PROC_COLOR_D = (0.76, 0.48, 0.48, 0.5)
87# Running process color.
88PROC_COLOR_R = CPU_COLOR
89# Sleeping process color.
90PROC_COLOR_S = (0.94, 0.94, 0.94, 1.0)
91# Stopped process color.
92PROC_COLOR_T = (0.94, 0.50, 0.50, 1.0)
93# Zombie process color.
94PROC_COLOR_Z = (0.71, 0.71, 0.71, 1.0)
95# Dead process color.
96PROC_COLOR_X = (0.71, 0.71, 0.71, 0.125)
97# Paging process color.
98PROC_COLOR_W = (0.71, 0.71, 0.71, 0.125)
99
100# Process label color.
101PROC_TEXT_COLOR = (0.19, 0.19, 0.19, 1.0)
102# Process label font.
103PROC_TEXT_FONT_SIZE = 12
104
105# Signature color.
106SIG_COLOR = (0.0, 0.0, 0.0, 0.3125)
107# Signature font.
108SIG_FONT_SIZE = 14
109# Signature text.
110SIGNATURE = "http://github.com/mmeeks/bootchart"
111
112# Process dependency line color.
113DEP_COLOR = (0.75, 0.75, 0.75, 1.0)
114# Process dependency line stroke.
115DEP_STROKE = 1.0
116
117# Process description date format.
118DESC_TIME_FORMAT = "mm:ss.SSS"
119
120# Cumulative coloring bits
121HSV_MAX_MOD = 31
122HSV_STEP = 7
123
124# Configure task color
125TASK_COLOR_CONFIGURE = (1.0, 1.0, 0.00, 1.0)
126# Compile task color.
127TASK_COLOR_COMPILE = (0.0, 1.00, 0.00, 1.0)
128# Install task color
129TASK_COLOR_INSTALL = (1.0, 0.00, 1.00, 1.0)
130# Sysroot task color
131TASK_COLOR_SYSROOT = (0.0, 0.00, 1.00, 1.0)
132# Package task color
133TASK_COLOR_PACKAGE = (0.0, 1.00, 1.00, 1.0)
134# Package Write RPM/DEB/IPK task color
135TASK_COLOR_PACKAGE_WRITE = (0.0, 0.50, 0.50, 1.0)
136
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500137# Distinct colors used for different disk volumnes.
138# If we have more volumns, colors get re-used.
139VOLUME_COLORS = [
Brad Bishopc342db32019-05-15 21:57:59 -0400140 (1.0, 1.0, 0.00, 1.0),
141 (0.0, 1.00, 0.00, 1.0),
142 (1.0, 0.00, 1.00, 1.0),
143 (0.0, 0.00, 1.00, 1.0),
144 (0.0, 1.00, 1.00, 1.0),
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500145]
146
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500147# Process states
148STATE_UNDEFINED = 0
149STATE_RUNNING = 1
150STATE_SLEEPING = 2
151STATE_WAITING = 3
152STATE_STOPPED = 4
153STATE_ZOMBIE = 5
154
155STATE_COLORS = [(0, 0, 0, 0), PROC_COLOR_R, PROC_COLOR_S, PROC_COLOR_D, \
Brad Bishopc342db32019-05-15 21:57:59 -0400156 PROC_COLOR_T, PROC_COLOR_Z, PROC_COLOR_X, PROC_COLOR_W]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500157
158# CumulativeStats Types
159STAT_TYPE_CPU = 0
160STAT_TYPE_IO = 1
161
162# Convert ps process state to an int
163def get_proc_state(flag):
Brad Bishopc342db32019-05-15 21:57:59 -0400164 return "RSDTZXW".find(flag) + 1
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500165
166def draw_text(ctx, text, color, x, y):
Brad Bishopc342db32019-05-15 21:57:59 -0400167 ctx.set_source_rgba(*color)
168 ctx.move_to(x, y)
169 ctx.show_text(text)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500170
171def draw_fill_rect(ctx, color, rect):
Brad Bishopc342db32019-05-15 21:57:59 -0400172 ctx.set_source_rgba(*color)
173 ctx.rectangle(*rect)
174 ctx.fill()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500175
176def draw_rect(ctx, color, rect):
Brad Bishopc342db32019-05-15 21:57:59 -0400177 ctx.set_source_rgba(*color)
178 ctx.rectangle(*rect)
179 ctx.stroke()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500180
181def draw_legend_box(ctx, label, fill_color, x, y, s):
Brad Bishopc342db32019-05-15 21:57:59 -0400182 draw_fill_rect(ctx, fill_color, (x, y - s, s, s))
183 draw_rect(ctx, PROC_BORDER_COLOR, (x, y - s, s, s))
184 draw_text(ctx, label, TEXT_COLOR, x + s + 5, y)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500185
186def draw_legend_line(ctx, label, fill_color, x, y, s):
Brad Bishopc342db32019-05-15 21:57:59 -0400187 draw_fill_rect(ctx, fill_color, (x, y - s/2, s + 1, 3))
188 ctx.arc(x + (s + 1)/2.0, y - (s - 3)/2.0, 2.5, 0, 2.0 * math.pi)
189 ctx.fill()
190 draw_text(ctx, label, TEXT_COLOR, x + s + 5, y)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500191
192def draw_label_in_box(ctx, color, label, x, y, w, maxx):
Brad Bishopc342db32019-05-15 21:57:59 -0400193 label_w = ctx.text_extents(label)[2]
194 label_x = x + w / 2 - label_w / 2
195 if label_w + 10 > w:
196 label_x = x + w + 5
197 if label_x + label_w > maxx:
198 label_x = x - label_w - 5
199 draw_text(ctx, label, color, label_x, y)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500200
201def draw_sec_labels(ctx, options, rect, sec_w, nsecs):
Brad Bishopc342db32019-05-15 21:57:59 -0400202 ctx.set_font_size(AXIS_FONT_SIZE)
203 prev_x = 0
204 for i in range(0, rect[2] + 1, sec_w):
205 if ((i / sec_w) % nsecs == 0) :
206 if options.app_options.as_minutes :
207 label = "%.1f" % (i / sec_w / 60.0)
208 else :
209 label = "%d" % (i / sec_w)
210 label_w = ctx.text_extents(label)[2]
211 x = rect[0] + i - label_w/2
212 if x >= prev_x:
213 draw_text(ctx, label, TEXT_COLOR, x, rect[1] - 2)
214 prev_x = x + label_w
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500215
216def draw_box_ticks(ctx, rect, sec_w):
Brad Bishopc342db32019-05-15 21:57:59 -0400217 draw_rect(ctx, BORDER_COLOR, tuple(rect))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500218
Brad Bishopc342db32019-05-15 21:57:59 -0400219 ctx.set_line_cap(cairo.LINE_CAP_SQUARE)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500220
Brad Bishopc342db32019-05-15 21:57:59 -0400221 for i in range(sec_w, rect[2] + 1, sec_w):
222 if ((i / sec_w) % 10 == 0) :
223 ctx.set_line_width(1.5)
224 elif sec_w < 5 :
225 continue
226 else :
227 ctx.set_line_width(1.0)
228 if ((i / sec_w) % 30 == 0) :
229 ctx.set_source_rgba(*TICK_COLOR_BOLD)
230 else :
231 ctx.set_source_rgba(*TICK_COLOR)
232 ctx.move_to(rect[0] + i, rect[1] + 1)
233 ctx.line_to(rect[0] + i, rect[1] + rect[3] - 1)
234 ctx.stroke()
235 ctx.set_line_width(1.0)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500236
Brad Bishopc342db32019-05-15 21:57:59 -0400237 ctx.set_line_cap(cairo.LINE_CAP_BUTT)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500238
239def draw_annotations(ctx, proc_tree, times, rect):
240 ctx.set_line_cap(cairo.LINE_CAP_SQUARE)
241 ctx.set_source_rgba(*ANNOTATION_COLOR)
242 ctx.set_dash([4, 4])
243
244 for time in times:
245 if time is not None:
246 x = ((time - proc_tree.start_time) * rect[2] / proc_tree.duration)
247
248 ctx.move_to(rect[0] + x, rect[1] + 1)
249 ctx.line_to(rect[0] + x, rect[1] + rect[3] - 1)
250 ctx.stroke()
251
252 ctx.set_line_cap(cairo.LINE_CAP_BUTT)
253 ctx.set_dash([])
254
255def draw_chart(ctx, color, fill, chart_bounds, data, proc_tree, data_range):
Brad Bishopc342db32019-05-15 21:57:59 -0400256 ctx.set_line_width(0.5)
257 x_shift = proc_tree.start_time
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500258
Brad Bishopc342db32019-05-15 21:57:59 -0400259 def transform_point_coords(point, x_base, y_base, \
260 xscale, yscale, x_trans, y_trans):
261 x = (point[0] - x_base) * xscale + x_trans
262 y = (point[1] - y_base) * -yscale + y_trans + chart_bounds[3]
263 return x, y
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500264
Brad Bishopc342db32019-05-15 21:57:59 -0400265 max_x = max (x for (x, y) in data)
266 max_y = max (y for (x, y) in data)
267 # avoid divide by zero
268 if max_y == 0:
269 max_y = 1.0
270 xscale = float (chart_bounds[2]) / (max_x - x_shift)
271 # If data_range is given, scale the chart so that the value range in
272 # data_range matches the chart bounds exactly.
273 # Otherwise, scale so that the actual data matches the chart bounds.
Andrew Geisslerc926e172021-05-07 16:11:35 -0500274 if data_range and (data_range[1] - data_range[0]):
Brad Bishopc342db32019-05-15 21:57:59 -0400275 yscale = float(chart_bounds[3]) / (data_range[1] - data_range[0])
276 ybase = data_range[0]
277 else:
278 yscale = float(chart_bounds[3]) / max_y
279 ybase = 0
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500280
Brad Bishopc342db32019-05-15 21:57:59 -0400281 first = transform_point_coords (data[0], x_shift, ybase, xscale, yscale, \
282 chart_bounds[0], chart_bounds[1])
283 last = transform_point_coords (data[-1], x_shift, ybase, xscale, yscale, \
284 chart_bounds[0], chart_bounds[1])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500285
Brad Bishopc342db32019-05-15 21:57:59 -0400286 ctx.set_source_rgba(*color)
287 ctx.move_to(*first)
288 for point in data:
289 x, y = transform_point_coords (point, x_shift, ybase, xscale, yscale, \
290 chart_bounds[0], chart_bounds[1])
291 ctx.line_to(x, y)
292 if fill:
293 ctx.stroke_preserve()
294 ctx.line_to(last[0], chart_bounds[1]+chart_bounds[3])
295 ctx.line_to(first[0], chart_bounds[1]+chart_bounds[3])
296 ctx.line_to(first[0], first[1])
297 ctx.fill()
298 else:
299 ctx.stroke()
300 ctx.set_line_width(1.0)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500301
302bar_h = 55
303meminfo_bar_h = 2 * bar_h
304header_h = 60
305# offsets
306off_x, off_y = 220, 10
307sec_w_base = 1 # the width of a second
308proc_h = 16 # the height of a process
309leg_s = 10
310MIN_IMG_W = 800
Andrew Geissler82c905d2020-04-13 13:39:40 -0500311CUML_HEIGHT = 2000 # Increased value to accommodate CPU and I/O Graphs
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500312OPTIONS = None
313
314def extents(options, xscale, trace):
Brad Bishopc342db32019-05-15 21:57:59 -0400315 start = min(trace.start.keys())
316 end = start
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500317
Brad Bishopc342db32019-05-15 21:57:59 -0400318 processes = 0
319 for proc in trace.processes:
320 if not options.app_options.show_all and \
321 trace.processes[proc][1] - trace.processes[proc][0] < options.app_options.mintime:
322 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500323
Brad Bishopc342db32019-05-15 21:57:59 -0400324 if trace.processes[proc][1] > end:
325 end = trace.processes[proc][1]
326 processes += 1
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500327
Brad Bishopc342db32019-05-15 21:57:59 -0400328 if trace.min is not None and trace.max is not None:
329 start = trace.min
330 end = trace.max
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500331
Brad Bishopc342db32019-05-15 21:57:59 -0400332 w = int ((end - start) * sec_w_base * xscale) + 2 * off_x
333 h = proc_h * processes + header_h + 2 * off_y
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500334
Brad Bishopc342db32019-05-15 21:57:59 -0400335 if options.charts:
336 if trace.cpu_stats:
337 h += 30 + bar_h
338 if trace.disk_stats:
339 h += 30 + bar_h
340 if trace.monitor_disk:
341 h += 30 + bar_h
342 if trace.mem_stats:
343 h += meminfo_bar_h
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500344
Brad Bishopc342db32019-05-15 21:57:59 -0400345 # Allow for width of process legend and offset
346 if w < (720 + off_x):
347 w = 720 + off_x
348
349 return (w, h)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500350
351def clip_visible(clip, rect):
Brad Bishopc342db32019-05-15 21:57:59 -0400352 xmax = max (clip[0], rect[0])
353 ymax = max (clip[1], rect[1])
354 xmin = min (clip[0] + clip[2], rect[0] + rect[2])
355 ymin = min (clip[1] + clip[3], rect[1] + rect[3])
356 return (xmin > xmax and ymin > ymax)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500357
358def render_charts(ctx, options, clip, trace, curr_y, w, h, sec_w):
Brad Bishopc342db32019-05-15 21:57:59 -0400359 proc_tree = options.proc_tree(trace)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500360
Brad Bishopc342db32019-05-15 21:57:59 -0400361 # render bar legend
362 if trace.cpu_stats:
363 ctx.set_font_size(LEGEND_FONT_SIZE)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500364
Brad Bishopc342db32019-05-15 21:57:59 -0400365 draw_legend_box(ctx, "CPU (user+sys)", CPU_COLOR, off_x, curr_y+20, leg_s)
366 draw_legend_box(ctx, "I/O (wait)", IO_COLOR, off_x + 120, curr_y+20, leg_s)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500367
Brad Bishopc342db32019-05-15 21:57:59 -0400368 # render I/O wait
369 chart_rect = (off_x, curr_y+30, w, bar_h)
370 if clip_visible (clip, chart_rect):
371 draw_box_ticks (ctx, chart_rect, sec_w)
372 draw_annotations (ctx, proc_tree, trace.times, chart_rect)
373 draw_chart (ctx, IO_COLOR, True, chart_rect, \
374 [(sample.time, sample.user + sample.sys + sample.io) for sample in trace.cpu_stats], \
375 proc_tree, None)
376 # render CPU load
377 draw_chart (ctx, CPU_COLOR, True, chart_rect, \
378 [(sample.time, sample.user + sample.sys) for sample in trace.cpu_stats], \
379 proc_tree, None)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500380
Brad Bishopc342db32019-05-15 21:57:59 -0400381 curr_y = curr_y + 30 + bar_h
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500382
Brad Bishopc342db32019-05-15 21:57:59 -0400383 # render second chart
384 if trace.disk_stats:
385 draw_legend_line(ctx, "Disk throughput", DISK_TPUT_COLOR, off_x, curr_y+20, leg_s)
386 draw_legend_box(ctx, "Disk utilization", IO_COLOR, off_x + 120, curr_y+20, leg_s)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500387
Brad Bishopc342db32019-05-15 21:57:59 -0400388 # render I/O utilization
389 chart_rect = (off_x, curr_y+30, w, bar_h)
390 if clip_visible (clip, chart_rect):
391 draw_box_ticks (ctx, chart_rect, sec_w)
392 draw_annotations (ctx, proc_tree, trace.times, chart_rect)
393 draw_chart (ctx, IO_COLOR, True, chart_rect, \
394 [(sample.time, sample.util) for sample in trace.disk_stats], \
395 proc_tree, None)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500396
Brad Bishopc342db32019-05-15 21:57:59 -0400397 # render disk throughput
398 max_sample = max (trace.disk_stats, key = lambda s: s.tput)
399 if clip_visible (clip, chart_rect):
400 draw_chart (ctx, DISK_TPUT_COLOR, False, chart_rect, \
401 [(sample.time, sample.tput) for sample in trace.disk_stats], \
402 proc_tree, None)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500403
Brad Bishopc342db32019-05-15 21:57:59 -0400404 pos_x = off_x + ((max_sample.time - proc_tree.start_time) * w / proc_tree.duration)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500405
Brad Bishopc342db32019-05-15 21:57:59 -0400406 shift_x, shift_y = -20, 20
407 if (pos_x < off_x + 245):
408 shift_x, shift_y = 5, 40
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500409
Brad Bishopc342db32019-05-15 21:57:59 -0400410 label = "%dMB/s" % round ((max_sample.tput) / 1024.0)
411 draw_text (ctx, label, DISK_TPUT_COLOR, pos_x + shift_x, curr_y + shift_y)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500412
Brad Bishopc342db32019-05-15 21:57:59 -0400413 curr_y = curr_y + 30 + bar_h
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500414
Brad Bishopc342db32019-05-15 21:57:59 -0400415 # render disk space usage
416 #
417 # Draws the amount of disk space used on each volume relative to the
418 # lowest recorded amount. The graphs for each volume are stacked above
419 # each other so that total disk usage is visible.
420 if trace.monitor_disk:
421 ctx.set_font_size(LEGEND_FONT_SIZE)
422 # Determine set of volumes for which we have
423 # information and the minimal amount of used disk
424 # space for each. Currently samples are allowed to
425 # not have a values for all volumes; drawing could be
426 # made more efficient if that wasn't the case.
427 volumes = set()
428 min_used = {}
429 for sample in trace.monitor_disk:
430 for volume, used in sample.records.items():
431 volumes.add(volume)
432 if volume not in min_used or min_used[volume] > used:
433 min_used[volume] = used
434 volumes = sorted(list(volumes))
435 disk_scale = 0
436 for i, volume in enumerate(volumes):
437 volume_scale = max([sample.records[volume] - min_used[volume]
438 for sample in trace.monitor_disk
439 if volume in sample.records])
440 # Does not take length of volume name into account, but fixed offset
441 # works okay in practice.
442 draw_legend_box(ctx, '%s (max: %u MiB)' % (volume, volume_scale / 1024 / 1024),
443 VOLUME_COLORS[i % len(VOLUME_COLORS)],
444 off_x + i * 250, curr_y+20, leg_s)
445 disk_scale += volume_scale
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500446
Brad Bishopc342db32019-05-15 21:57:59 -0400447 # render used amount of disk space
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 for i in range(len(volumes), 0, -1):
453 draw_chart (ctx, VOLUME_COLORS[(i - 1) % len(VOLUME_COLORS)], True, chart_rect, \
454 [(sample.time,
455 # Sum up used space of all volumes including the current one
456 # so that the graphs appear as stacked on top of each other.
457 functools.reduce(lambda x,y: x+y,
458 [sample.records[volume] - min_used[volume]
459 for volume in volumes[0:i]
460 if volume in sample.records],
461 0))
462 for sample in trace.monitor_disk], \
463 proc_tree, [0, disk_scale])
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500464
Brad Bishopc342db32019-05-15 21:57:59 -0400465 curr_y = curr_y + 30 + bar_h
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500466
Brad Bishopc342db32019-05-15 21:57:59 -0400467 # render mem usage
468 chart_rect = (off_x, curr_y+30, w, meminfo_bar_h)
469 mem_stats = trace.mem_stats
470 if mem_stats and clip_visible (clip, chart_rect):
471 mem_scale = max(sample.buffers for sample in mem_stats)
472 draw_legend_box(ctx, "Mem cached (scale: %u MiB)" % (float(mem_scale) / 1024), MEM_CACHED_COLOR, off_x, curr_y+20, leg_s)
473 draw_legend_box(ctx, "Used", MEM_USED_COLOR, off_x + 240, curr_y+20, leg_s)
474 draw_legend_box(ctx, "Buffers", MEM_BUFFERS_COLOR, off_x + 360, curr_y+20, leg_s)
475 draw_legend_line(ctx, "Swap (scale: %u MiB)" % max([(sample.swap)/1024 for sample in mem_stats]), \
476 MEM_SWAP_COLOR, off_x + 480, curr_y+20, leg_s)
477 draw_box_ticks(ctx, chart_rect, sec_w)
478 draw_annotations(ctx, proc_tree, trace.times, chart_rect)
479 draw_chart(ctx, MEM_BUFFERS_COLOR, True, chart_rect, \
480 [(sample.time, sample.buffers) for sample in trace.mem_stats], \
481 proc_tree, [0, mem_scale])
482 draw_chart(ctx, MEM_USED_COLOR, True, chart_rect, \
483 [(sample.time, sample.used) for sample in mem_stats], \
484 proc_tree, [0, mem_scale])
485 draw_chart(ctx, MEM_CACHED_COLOR, True, chart_rect, \
486 [(sample.time, sample.cached) for sample in mem_stats], \
487 proc_tree, [0, mem_scale])
488 draw_chart(ctx, MEM_SWAP_COLOR, False, chart_rect, \
489 [(sample.time, float(sample.swap)) for sample in mem_stats], \
490 proc_tree, None)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500491
Brad Bishopc342db32019-05-15 21:57:59 -0400492 curr_y = curr_y + meminfo_bar_h
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500493
Brad Bishopc342db32019-05-15 21:57:59 -0400494 return curr_y
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500495
496def render_processes_chart(ctx, options, trace, curr_y, w, h, sec_w):
Brad Bishopc342db32019-05-15 21:57:59 -0400497 chart_rect = [off_x, curr_y+header_h, w, h - curr_y - 1 * off_y - header_h ]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500498
Brad Bishopc342db32019-05-15 21:57:59 -0400499 draw_legend_box (ctx, "Configure", \
500 TASK_COLOR_CONFIGURE, off_x , curr_y + 45, leg_s)
501 draw_legend_box (ctx, "Compile", \
502 TASK_COLOR_COMPILE, off_x+120, curr_y + 45, leg_s)
503 draw_legend_box (ctx, "Install", \
504 TASK_COLOR_INSTALL, off_x+240, curr_y + 45, leg_s)
505 draw_legend_box (ctx, "Populate Sysroot", \
506 TASK_COLOR_SYSROOT, off_x+360, curr_y + 45, leg_s)
507 draw_legend_box (ctx, "Package", \
508 TASK_COLOR_PACKAGE, off_x+480, curr_y + 45, leg_s)
509 draw_legend_box (ctx, "Package Write", \
510 TASK_COLOR_PACKAGE_WRITE, off_x+600, curr_y + 45, leg_s)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500511
Brad Bishopc342db32019-05-15 21:57:59 -0400512 ctx.set_font_size(PROC_TEXT_FONT_SIZE)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500513
Brad Bishopc342db32019-05-15 21:57:59 -0400514 draw_box_ticks(ctx, chart_rect, sec_w)
515 draw_sec_labels(ctx, options, chart_rect, sec_w, 30)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500516
Brad Bishopc342db32019-05-15 21:57:59 -0400517 y = curr_y+header_h
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500518
Brad Bishopc342db32019-05-15 21:57:59 -0400519 offset = trace.min or min(trace.start.keys())
520 for start in sorted(trace.start.keys()):
521 for process in sorted(trace.start[start]):
522 if not options.app_options.show_all and \
523 trace.processes[process][1] - start < options.app_options.mintime:
524 continue
525 task = process.split(":")[1]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500526
Brad Bishopc342db32019-05-15 21:57:59 -0400527 #print(process)
528 #print(trace.processes[process][1])
529 #print(s)
530
531 x = chart_rect[0] + (start - offset) * sec_w
532 w = ((trace.processes[process][1] - start) * sec_w)
533
534 #print("proc at %s %s %s %s" % (x, y, w, proc_h))
535 col = None
536 if task == "do_compile":
537 col = TASK_COLOR_COMPILE
538 elif task == "do_configure":
539 col = TASK_COLOR_CONFIGURE
540 elif task == "do_install":
541 col = TASK_COLOR_INSTALL
542 elif task == "do_populate_sysroot":
543 col = TASK_COLOR_SYSROOT
544 elif task == "do_package":
545 col = TASK_COLOR_PACKAGE
546 elif task == "do_package_write_rpm" or \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500547 task == "do_package_write_deb" or \
548 task == "do_package_write_ipk":
Brad Bishopc342db32019-05-15 21:57:59 -0400549 col = TASK_COLOR_PACKAGE_WRITE
550 else:
551 col = WHITE
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500552
Brad Bishopc342db32019-05-15 21:57:59 -0400553 if col:
554 draw_fill_rect(ctx, col, (x, y, w, proc_h))
555 draw_rect(ctx, PROC_BORDER_COLOR, (x, y, w, proc_h))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500556
Brad Bishopc342db32019-05-15 21:57:59 -0400557 draw_label_in_box(ctx, PROC_TEXT_COLOR, process, x, y + proc_h - 4, w, proc_h)
558 y = y + proc_h
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500559
Brad Bishopc342db32019-05-15 21:57:59 -0400560 return curr_y
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500561
562#
563# Render the chart.
564#
565def render(ctx, options, xscale, trace):
Brad Bishopc342db32019-05-15 21:57:59 -0400566 (w, h) = extents (options, xscale, trace)
567 global OPTIONS
568 OPTIONS = options.app_options
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500569
Brad Bishopc342db32019-05-15 21:57:59 -0400570 # x, y, w, h
571 clip = ctx.clip_extents()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500572
Brad Bishopc342db32019-05-15 21:57:59 -0400573 sec_w = int (xscale * sec_w_base)
574 ctx.set_line_width(1.0)
575 ctx.select_font_face(FONT_NAME)
576 draw_fill_rect(ctx, WHITE, (0, 0, max(w, MIN_IMG_W), h))
577 w -= 2*off_x
578 curr_y = off_y;
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500579
Brad Bishopc342db32019-05-15 21:57:59 -0400580 if options.charts:
581 curr_y = render_charts (ctx, options, clip, trace, curr_y, w, h, sec_w)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500582
Brad Bishopc342db32019-05-15 21:57:59 -0400583 curr_y = render_processes_chart (ctx, options, trace, curr_y, w, h, sec_w)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500584
Brad Bishopc342db32019-05-15 21:57:59 -0400585 return
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500586
Brad Bishopc342db32019-05-15 21:57:59 -0400587 proc_tree = options.proc_tree (trace)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500588
Brad Bishopc342db32019-05-15 21:57:59 -0400589 # draw the title and headers
590 if proc_tree.idle:
591 duration = proc_tree.idle
592 else:
593 duration = proc_tree.duration
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500594
Brad Bishopc342db32019-05-15 21:57:59 -0400595 if not options.kernel_only:
596 curr_y = draw_header (ctx, trace.headers, duration)
597 else:
598 curr_y = off_y;
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500599
Brad Bishopc342db32019-05-15 21:57:59 -0400600 # draw process boxes
601 proc_height = h
602 if proc_tree.taskstats and options.cumulative:
603 proc_height -= CUML_HEIGHT
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500604
Brad Bishopc342db32019-05-15 21:57:59 -0400605 draw_process_bar_chart(ctx, clip, options, proc_tree, trace.times,
606 curr_y, w, proc_height, sec_w)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500607
Brad Bishopc342db32019-05-15 21:57:59 -0400608 curr_y = proc_height
609 ctx.set_font_size(SIG_FONT_SIZE)
610 draw_text(ctx, SIGNATURE, SIG_COLOR, off_x + 5, proc_height - 8)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500611
Brad Bishopc342db32019-05-15 21:57:59 -0400612 # draw a cumulative CPU-time-per-process graph
613 if proc_tree.taskstats and options.cumulative:
614 cuml_rect = (off_x, curr_y + off_y, w, CUML_HEIGHT/2 - off_y * 2)
615 if clip_visible (clip, cuml_rect):
616 draw_cuml_graph(ctx, proc_tree, cuml_rect, duration, sec_w, STAT_TYPE_CPU)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500617
Brad Bishopc342db32019-05-15 21:57:59 -0400618 # draw a cumulative I/O-time-per-process graph
619 if proc_tree.taskstats and options.cumulative:
620 cuml_rect = (off_x, curr_y + off_y * 100, w, CUML_HEIGHT/2 - off_y * 2)
621 if clip_visible (clip, cuml_rect):
622 draw_cuml_graph(ctx, proc_tree, cuml_rect, duration, sec_w, STAT_TYPE_IO)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500623
624def draw_process_bar_chart(ctx, clip, options, proc_tree, times, curr_y, w, h, sec_w):
Brad Bishopc342db32019-05-15 21:57:59 -0400625 header_size = 0
626 if not options.kernel_only:
627 draw_legend_box (ctx, "Running (%cpu)",
628 PROC_COLOR_R, off_x , curr_y + 45, leg_s)
629 draw_legend_box (ctx, "Unint.sleep (I/O)",
630 PROC_COLOR_D, off_x+120, curr_y + 45, leg_s)
631 draw_legend_box (ctx, "Sleeping",
632 PROC_COLOR_S, off_x+240, curr_y + 45, leg_s)
633 draw_legend_box (ctx, "Zombie",
634 PROC_COLOR_Z, off_x+360, curr_y + 45, leg_s)
635 header_size = 45
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500636
Brad Bishopc342db32019-05-15 21:57:59 -0400637 chart_rect = [off_x, curr_y + header_size + 15,
638 w, h - 2 * off_y - (curr_y + header_size + 15) + proc_h]
639 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 if sec_w > 100:
643 nsec = 1
644 else:
645 nsec = 5
646 draw_sec_labels (ctx, options, chart_rect, sec_w, nsec)
647 draw_annotations (ctx, proc_tree, times, chart_rect)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500648
Brad Bishopc342db32019-05-15 21:57:59 -0400649 y = curr_y + 60
650 for root in proc_tree.process_tree:
651 draw_processes_recursively(ctx, root, proc_tree, y, proc_h, chart_rect, clip)
652 y = y + proc_h * proc_tree.num_nodes([root])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500653
654
655def draw_header (ctx, headers, duration):
656 toshow = [
657 ('system.uname', 'uname', lambda s: s),
658 ('system.release', 'release', lambda s: s),
659 ('system.cpu', 'CPU', lambda s: re.sub('model name\s*:\s*', '', s, 1)),
660 ('system.kernel.options', 'kernel options', lambda s: s),
661 ]
662
663 header_y = ctx.font_extents()[2] + 10
664 ctx.set_font_size(TITLE_FONT_SIZE)
665 draw_text(ctx, headers['title'], TEXT_COLOR, off_x, header_y)
666 ctx.set_font_size(TEXT_FONT_SIZE)
667
668 for (headerkey, headertitle, mangle) in toshow:
669 header_y += ctx.font_extents()[2]
670 if headerkey in headers:
671 value = headers.get(headerkey)
672 else:
673 value = ""
674 txt = headertitle + ': ' + mangle(value)
675 draw_text(ctx, txt, TEXT_COLOR, off_x, header_y)
676
677 dur = duration / 100.0
678 txt = 'time : %02d:%05.2f' % (math.floor(dur/60), dur - 60 * math.floor(dur/60))
679 if headers.get('system.maxpid') is not None:
680 txt = txt + ' max pid: %s' % (headers.get('system.maxpid'))
681
682 header_y += ctx.font_extents()[2]
683 draw_text (ctx, txt, TEXT_COLOR, off_x, header_y)
684
685 return header_y
686
687def draw_processes_recursively(ctx, proc, proc_tree, y, proc_h, rect, clip) :
Brad Bishopc342db32019-05-15 21:57:59 -0400688 x = rect[0] + ((proc.start_time - proc_tree.start_time) * rect[2] / proc_tree.duration)
689 w = ((proc.duration) * rect[2] / proc_tree.duration)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500690
Brad Bishopc342db32019-05-15 21:57:59 -0400691 draw_process_activity_colors(ctx, proc, proc_tree, x, y, w, proc_h, rect, clip)
692 draw_rect(ctx, PROC_BORDER_COLOR, (x, y, w, proc_h))
693 ipid = int(proc.pid)
694 if not OPTIONS.show_all:
695 cmdString = proc.cmd
696 else:
697 cmdString = ''
698 if (OPTIONS.show_pid or OPTIONS.show_all) and ipid is not 0:
699 cmdString = cmdString + " [" + str(ipid // 1000) + "]"
700 if OPTIONS.show_all:
701 if proc.args:
702 cmdString = cmdString + " '" + "' '".join(proc.args) + "'"
703 else:
704 cmdString = cmdString + " " + proc.exe
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500705
Brad Bishopc342db32019-05-15 21:57:59 -0400706 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 -0500707
Brad Bishopc342db32019-05-15 21:57:59 -0400708 next_y = y + proc_h
709 for child in proc.child_list:
710 if next_y > clip[1] + clip[3]:
711 break
712 child_x, child_y = draw_processes_recursively(ctx, child, proc_tree, next_y, proc_h, rect, clip)
713 draw_process_connecting_lines(ctx, x, y, child_x, child_y, proc_h)
714 next_y = next_y + proc_h * proc_tree.num_nodes([child])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500715
Brad Bishopc342db32019-05-15 21:57:59 -0400716 return x, y
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500717
718
719def draw_process_activity_colors(ctx, proc, proc_tree, x, y, w, proc_h, rect, clip):
720
Brad Bishopc342db32019-05-15 21:57:59 -0400721 if y > clip[1] + clip[3] or y + proc_h + 2 < clip[1]:
722 return
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500723
Brad Bishopc342db32019-05-15 21:57:59 -0400724 draw_fill_rect(ctx, PROC_COLOR_S, (x, y, w, proc_h))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500725
Brad Bishopc342db32019-05-15 21:57:59 -0400726 last_tx = -1
727 for sample in proc.samples :
728 tx = rect[0] + round(((sample.time - proc_tree.start_time) * rect[2] / proc_tree.duration))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500729
Brad Bishopc342db32019-05-15 21:57:59 -0400730 # samples are sorted chronologically
731 if tx < clip[0]:
732 continue
733 if tx > clip[0] + clip[2]:
734 break
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500735
Brad Bishopc342db32019-05-15 21:57:59 -0400736 tw = round(proc_tree.sample_period * rect[2] / float(proc_tree.duration))
737 if last_tx != -1 and abs(last_tx - tx) <= tw:
738 tw -= last_tx - tx
739 tx = last_tx
740 tw = max (tw, 1) # nice to see at least something
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500741
Brad Bishopc342db32019-05-15 21:57:59 -0400742 last_tx = tx + tw
743 state = get_proc_state( sample.state )
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500744
Brad Bishopc342db32019-05-15 21:57:59 -0400745 color = STATE_COLORS[state]
746 if state == STATE_RUNNING:
747 alpha = min (sample.cpu_sample.user + sample.cpu_sample.sys, 1.0)
748 color = tuple(list(PROC_COLOR_R[0:3]) + [alpha])
749# print "render time %d [ tx %d tw %d ], sample state %s color %s alpha %g" % (sample.time, tx, tw, state, color, alpha)
750 elif state == STATE_SLEEPING:
751 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500752
Brad Bishopc342db32019-05-15 21:57:59 -0400753 draw_fill_rect(ctx, color, (tx, y, tw, proc_h))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500754
755def draw_process_connecting_lines(ctx, px, py, x, y, proc_h):
Brad Bishopc342db32019-05-15 21:57:59 -0400756 ctx.set_source_rgba(*DEP_COLOR)
757 ctx.set_dash([2, 2])
758 if abs(px - x) < 3:
759 dep_off_x = 3
760 dep_off_y = proc_h / 4
761 ctx.move_to(x, y + proc_h / 2)
762 ctx.line_to(px - dep_off_x, y + proc_h / 2)
763 ctx.line_to(px - dep_off_x, py - dep_off_y)
764 ctx.line_to(px, py - dep_off_y)
765 else:
766 ctx.move_to(x, y + proc_h / 2)
767 ctx.line_to(px, y + proc_h / 2)
768 ctx.line_to(px, py)
769 ctx.stroke()
770 ctx.set_dash([])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500771
772# elide the bootchart collector - it is quite distorting
773def elide_bootchart(proc):
Brad Bishopc342db32019-05-15 21:57:59 -0400774 return proc.cmd == 'bootchartd' or proc.cmd == 'bootchart-colle'
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500775
776class CumlSample:
Brad Bishopc342db32019-05-15 21:57:59 -0400777 def __init__(self, proc):
778 self.cmd = proc.cmd
779 self.samples = []
780 self.merge_samples (proc)
781 self.color = None
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500782
Brad Bishopc342db32019-05-15 21:57:59 -0400783 def merge_samples(self, proc):
784 self.samples.extend (proc.samples)
785 self.samples.sort (key = lambda p: p.time)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500786
Brad Bishopc342db32019-05-15 21:57:59 -0400787 def next(self):
788 global palette_idx
789 palette_idx += HSV_STEP
790 return palette_idx
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500791
Brad Bishopc342db32019-05-15 21:57:59 -0400792 def get_color(self):
793 if self.color is None:
794 i = self.next() % HSV_MAX_MOD
795 h = 0.0
796 if i is not 0:
797 h = (1.0 * i) / HSV_MAX_MOD
798 s = 0.5
799 v = 1.0
800 c = colorsys.hsv_to_rgb (h, s, v)
801 self.color = (c[0], c[1], c[2], 1.0)
802 return self.color
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500803
804
805def draw_cuml_graph(ctx, proc_tree, chart_bounds, duration, sec_w, stat_type):
Brad Bishopc342db32019-05-15 21:57:59 -0400806 global palette_idx
807 palette_idx = 0
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500808
Brad Bishopc342db32019-05-15 21:57:59 -0400809 time_hash = {}
810 total_time = 0.0
811 m_proc_list = {}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500812
Brad Bishopc342db32019-05-15 21:57:59 -0400813 if stat_type is STAT_TYPE_CPU:
814 sample_value = 'cpu'
815 else:
816 sample_value = 'io'
817 for proc in proc_tree.process_list:
818 if elide_bootchart(proc):
819 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500820
Brad Bishopc342db32019-05-15 21:57:59 -0400821 for sample in proc.samples:
822 total_time += getattr(sample.cpu_sample, sample_value)
823 if not sample.time in time_hash:
824 time_hash[sample.time] = 1
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500825
Brad Bishopc342db32019-05-15 21:57:59 -0400826 # merge pids with the same cmd
827 if not proc.cmd in m_proc_list:
828 m_proc_list[proc.cmd] = CumlSample (proc)
829 continue
830 s = m_proc_list[proc.cmd]
831 s.merge_samples (proc)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500832
Brad Bishopc342db32019-05-15 21:57:59 -0400833 # all the sample times
834 times = sorted(time_hash)
835 if len (times) < 2:
836 print("degenerate boot chart")
837 return
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500838
Brad Bishopc342db32019-05-15 21:57:59 -0400839 pix_per_ns = chart_bounds[3] / total_time
840# print "total time: %g pix-per-ns %g" % (total_time, pix_per_ns)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500841
Brad Bishopc342db32019-05-15 21:57:59 -0400842 # FIXME: we have duplicates in the process list too [!] - why !?
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500843
Brad Bishopc342db32019-05-15 21:57:59 -0400844 # Render bottom up, left to right
845 below = {}
846 for time in times:
847 below[time] = chart_bounds[1] + chart_bounds[3]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500848
Brad Bishopc342db32019-05-15 21:57:59 -0400849 # same colors each time we render
850 random.seed (0)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500851
Brad Bishopc342db32019-05-15 21:57:59 -0400852 ctx.set_line_width(1)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500853
Brad Bishopc342db32019-05-15 21:57:59 -0400854 legends = []
855 labels = []
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500856
Brad Bishopc342db32019-05-15 21:57:59 -0400857 # render each pid in order
858 for cs in m_proc_list.values():
859 row = {}
860 cuml = 0.0
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500861
Brad Bishopc342db32019-05-15 21:57:59 -0400862 # print "pid : %s -> %g samples %d" % (proc.cmd, cuml, len (cs.samples))
863 for sample in cs.samples:
864 cuml += getattr(sample.cpu_sample, sample_value)
865 row[sample.time] = cuml
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500866
Brad Bishopc342db32019-05-15 21:57:59 -0400867 process_total_time = cuml
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500868
Brad Bishopc342db32019-05-15 21:57:59 -0400869 # hide really tiny processes
870 if cuml * pix_per_ns <= 2:
871 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500872
Brad Bishopc342db32019-05-15 21:57:59 -0400873 last_time = times[0]
874 y = last_below = below[last_time]
875 last_cuml = cuml = 0.0
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500876
Brad Bishopc342db32019-05-15 21:57:59 -0400877 ctx.set_source_rgba(*cs.get_color())
878 for time in times:
879 render_seg = False
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500880
Brad Bishopc342db32019-05-15 21:57:59 -0400881 # did the underlying trend increase ?
882 if below[time] != last_below:
883 last_below = below[last_time]
884 last_cuml = cuml
885 render_seg = True
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500886
Brad Bishopc342db32019-05-15 21:57:59 -0400887 # did we move up a pixel increase ?
888 if time in row:
889 nc = round (row[time] * pix_per_ns)
890 if nc != cuml:
891 last_cuml = cuml
892 cuml = nc
893 render_seg = True
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500894
Brad Bishopc342db32019-05-15 21:57:59 -0400895# if last_cuml > cuml:
896# assert fail ... - un-sorted process samples
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500897
Brad Bishopc342db32019-05-15 21:57:59 -0400898 # draw the trailing rectangle from the last time to
899 # before now, at the height of the last segment.
900 if render_seg:
901 w = math.ceil ((time - last_time) * chart_bounds[2] / proc_tree.duration) + 1
902 x = chart_bounds[0] + round((last_time - proc_tree.start_time) * chart_bounds[2] / proc_tree.duration)
903 ctx.rectangle (x, below[last_time] - last_cuml, w, last_cuml)
904 ctx.fill()
905# ctx.stroke()
906 last_time = time
907 y = below [time] - cuml
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500908
Brad Bishopc342db32019-05-15 21:57:59 -0400909 row[time] = y
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500910
Brad Bishopc342db32019-05-15 21:57:59 -0400911 # render the last segment
912 x = chart_bounds[0] + round((last_time - proc_tree.start_time) * chart_bounds[2] / proc_tree.duration)
913 y = below[last_time] - cuml
914 ctx.rectangle (x, y, chart_bounds[2] - x, cuml)
915 ctx.fill()
916# ctx.stroke()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500917
Brad Bishopc342db32019-05-15 21:57:59 -0400918 # render legend if it will fit
919 if cuml > 8:
920 label = cs.cmd
921 extnts = ctx.text_extents(label)
922 label_w = extnts[2]
923 label_h = extnts[3]
924# print "Text extents %g by %g" % (label_w, label_h)
925 labels.append((label,
926 chart_bounds[0] + chart_bounds[2] - label_w - off_x * 2,
927 y + (cuml + label_h) / 2))
928 if cs in legends:
929 print("ARGH - duplicate process in list !")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500930
Brad Bishopc342db32019-05-15 21:57:59 -0400931 legends.append ((cs, process_total_time))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500932
Brad Bishopc342db32019-05-15 21:57:59 -0400933 below = row
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500934
Brad Bishopc342db32019-05-15 21:57:59 -0400935 # render grid-lines over the top
936 draw_box_ticks(ctx, chart_bounds, sec_w)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500937
Brad Bishopc342db32019-05-15 21:57:59 -0400938 # render labels
939 for l in labels:
940 draw_text(ctx, l[0], TEXT_COLOR, l[1], l[2])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500941
Brad Bishopc342db32019-05-15 21:57:59 -0400942 # Render legends
943 font_height = 20
944 label_width = 300
945 LEGENDS_PER_COL = 15
946 LEGENDS_TOTAL = 45
947 ctx.set_font_size (TITLE_FONT_SIZE)
948 dur_secs = duration / 100
949 cpu_secs = total_time / 1000000000
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500950
Brad Bishopc342db32019-05-15 21:57:59 -0400951 # misleading - with multiple CPUs ...
952# idle = ((dur_secs - cpu_secs) / dur_secs) * 100.0
953 if stat_type is STAT_TYPE_CPU:
954 label = "Cumulative CPU usage, by process; total CPU: " \
955 " %.5g(s) time: %.3g(s)" % (cpu_secs, dur_secs)
956 else:
957 label = "Cumulative I/O usage, by process; total I/O: " \
958 " %.5g(s) time: %.3g(s)" % (cpu_secs, dur_secs)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500959
Brad Bishopc342db32019-05-15 21:57:59 -0400960 draw_text(ctx, label, TEXT_COLOR, chart_bounds[0] + off_x,
961 chart_bounds[1] + font_height)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500962
Brad Bishopc342db32019-05-15 21:57:59 -0400963 i = 0
964 legends = sorted(legends, key=itemgetter(1), reverse=True)
965 ctx.set_font_size(TEXT_FONT_SIZE)
966 for t in legends:
967 cs = t[0]
968 time = t[1]
969 x = chart_bounds[0] + off_x + int (i/LEGENDS_PER_COL) * label_width
970 y = chart_bounds[1] + font_height * ((i % LEGENDS_PER_COL) + 2)
971 str = "%s - %.0f(ms) (%2.2f%%)" % (cs.cmd, time/1000000, (time/total_time) * 100.0)
972 draw_legend_box(ctx, str, cs.color, x, y, leg_s)
973 i = i + 1
974 if i >= LEGENDS_TOTAL:
975 break