blob: fc708b55c355ecc2fb6ef2aa4d0f6bfa89f0d8de [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
Andrew Geissler5199d832021-09-24 16:47:35 -0500270 if (max_x - x_shift):
271 xscale = float (chart_bounds[2]) / (max_x - x_shift)
272 else:
273 xscale = float (chart_bounds[2])
Brad Bishopc342db32019-05-15 21:57:59 -0400274 # If data_range is given, scale the chart so that the value range in
275 # data_range matches the chart bounds exactly.
276 # Otherwise, scale so that the actual data matches the chart bounds.
Andrew Geisslerc926e172021-05-07 16:11:35 -0500277 if data_range and (data_range[1] - data_range[0]):
Brad Bishopc342db32019-05-15 21:57:59 -0400278 yscale = float(chart_bounds[3]) / (data_range[1] - data_range[0])
279 ybase = data_range[0]
280 else:
281 yscale = float(chart_bounds[3]) / max_y
282 ybase = 0
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500283
Brad Bishopc342db32019-05-15 21:57:59 -0400284 first = transform_point_coords (data[0], x_shift, ybase, xscale, yscale, \
285 chart_bounds[0], chart_bounds[1])
286 last = transform_point_coords (data[-1], x_shift, ybase, xscale, yscale, \
287 chart_bounds[0], chart_bounds[1])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500288
Brad Bishopc342db32019-05-15 21:57:59 -0400289 ctx.set_source_rgba(*color)
290 ctx.move_to(*first)
291 for point in data:
292 x, y = transform_point_coords (point, x_shift, ybase, xscale, yscale, \
293 chart_bounds[0], chart_bounds[1])
294 ctx.line_to(x, y)
295 if fill:
296 ctx.stroke_preserve()
297 ctx.line_to(last[0], chart_bounds[1]+chart_bounds[3])
298 ctx.line_to(first[0], chart_bounds[1]+chart_bounds[3])
299 ctx.line_to(first[0], first[1])
300 ctx.fill()
301 else:
302 ctx.stroke()
303 ctx.set_line_width(1.0)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500304
305bar_h = 55
306meminfo_bar_h = 2 * bar_h
307header_h = 60
308# offsets
309off_x, off_y = 220, 10
310sec_w_base = 1 # the width of a second
311proc_h = 16 # the height of a process
312leg_s = 10
313MIN_IMG_W = 800
Andrew Geissler82c905d2020-04-13 13:39:40 -0500314CUML_HEIGHT = 2000 # Increased value to accommodate CPU and I/O Graphs
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500315OPTIONS = None
316
317def extents(options, xscale, trace):
Brad Bishopc342db32019-05-15 21:57:59 -0400318 start = min(trace.start.keys())
319 end = start
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500320
Brad Bishopc342db32019-05-15 21:57:59 -0400321 processes = 0
322 for proc in trace.processes:
323 if not options.app_options.show_all and \
324 trace.processes[proc][1] - trace.processes[proc][0] < options.app_options.mintime:
325 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500326
Brad Bishopc342db32019-05-15 21:57:59 -0400327 if trace.processes[proc][1] > end:
328 end = trace.processes[proc][1]
329 processes += 1
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500330
Brad Bishopc342db32019-05-15 21:57:59 -0400331 if trace.min is not None and trace.max is not None:
332 start = trace.min
333 end = trace.max
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500334
Brad Bishopc342db32019-05-15 21:57:59 -0400335 w = int ((end - start) * sec_w_base * xscale) + 2 * off_x
336 h = proc_h * processes + header_h + 2 * off_y
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500337
Brad Bishopc342db32019-05-15 21:57:59 -0400338 if options.charts:
339 if trace.cpu_stats:
340 h += 30 + bar_h
341 if trace.disk_stats:
342 h += 30 + bar_h
343 if trace.monitor_disk:
344 h += 30 + bar_h
345 if trace.mem_stats:
346 h += meminfo_bar_h
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500347
Brad Bishopc342db32019-05-15 21:57:59 -0400348 # Allow for width of process legend and offset
349 if w < (720 + off_x):
350 w = 720 + off_x
351
352 return (w, h)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500353
354def clip_visible(clip, rect):
Brad Bishopc342db32019-05-15 21:57:59 -0400355 xmax = max (clip[0], rect[0])
356 ymax = max (clip[1], rect[1])
357 xmin = min (clip[0] + clip[2], rect[0] + rect[2])
358 ymin = min (clip[1] + clip[3], rect[1] + rect[3])
359 return (xmin > xmax and ymin > ymax)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500360
361def render_charts(ctx, options, clip, trace, curr_y, w, h, sec_w):
Brad Bishopc342db32019-05-15 21:57:59 -0400362 proc_tree = options.proc_tree(trace)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500363
Brad Bishopc342db32019-05-15 21:57:59 -0400364 # render bar legend
365 if trace.cpu_stats:
366 ctx.set_font_size(LEGEND_FONT_SIZE)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500367
Brad Bishopc342db32019-05-15 21:57:59 -0400368 draw_legend_box(ctx, "CPU (user+sys)", CPU_COLOR, off_x, curr_y+20, leg_s)
369 draw_legend_box(ctx, "I/O (wait)", IO_COLOR, off_x + 120, curr_y+20, leg_s)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500370
Brad Bishopc342db32019-05-15 21:57:59 -0400371 # render I/O wait
372 chart_rect = (off_x, curr_y+30, w, bar_h)
373 if clip_visible (clip, chart_rect):
374 draw_box_ticks (ctx, chart_rect, sec_w)
375 draw_annotations (ctx, proc_tree, trace.times, chart_rect)
376 draw_chart (ctx, IO_COLOR, True, chart_rect, \
377 [(sample.time, sample.user + sample.sys + sample.io) for sample in trace.cpu_stats], \
378 proc_tree, None)
379 # render CPU load
380 draw_chart (ctx, CPU_COLOR, True, chart_rect, \
381 [(sample.time, sample.user + sample.sys) for sample in trace.cpu_stats], \
382 proc_tree, None)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500383
Brad Bishopc342db32019-05-15 21:57:59 -0400384 curr_y = curr_y + 30 + bar_h
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500385
Brad Bishopc342db32019-05-15 21:57:59 -0400386 # render second chart
387 if trace.disk_stats:
388 draw_legend_line(ctx, "Disk throughput", DISK_TPUT_COLOR, off_x, curr_y+20, leg_s)
389 draw_legend_box(ctx, "Disk utilization", IO_COLOR, off_x + 120, curr_y+20, leg_s)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500390
Brad Bishopc342db32019-05-15 21:57:59 -0400391 # render I/O utilization
392 chart_rect = (off_x, curr_y+30, w, bar_h)
393 if clip_visible (clip, chart_rect):
394 draw_box_ticks (ctx, chart_rect, sec_w)
395 draw_annotations (ctx, proc_tree, trace.times, chart_rect)
396 draw_chart (ctx, IO_COLOR, True, chart_rect, \
397 [(sample.time, sample.util) for sample in trace.disk_stats], \
398 proc_tree, None)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500399
Brad Bishopc342db32019-05-15 21:57:59 -0400400 # render disk throughput
401 max_sample = max (trace.disk_stats, key = lambda s: s.tput)
402 if clip_visible (clip, chart_rect):
403 draw_chart (ctx, DISK_TPUT_COLOR, False, chart_rect, \
404 [(sample.time, sample.tput) for sample in trace.disk_stats], \
405 proc_tree, None)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500406
Brad Bishopc342db32019-05-15 21:57:59 -0400407 pos_x = off_x + ((max_sample.time - proc_tree.start_time) * w / proc_tree.duration)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500408
Brad Bishopc342db32019-05-15 21:57:59 -0400409 shift_x, shift_y = -20, 20
410 if (pos_x < off_x + 245):
411 shift_x, shift_y = 5, 40
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500412
Brad Bishopc342db32019-05-15 21:57:59 -0400413 label = "%dMB/s" % round ((max_sample.tput) / 1024.0)
414 draw_text (ctx, label, DISK_TPUT_COLOR, pos_x + shift_x, curr_y + shift_y)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500415
Brad Bishopc342db32019-05-15 21:57:59 -0400416 curr_y = curr_y + 30 + bar_h
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500417
Brad Bishopc342db32019-05-15 21:57:59 -0400418 # render disk space usage
419 #
420 # Draws the amount of disk space used on each volume relative to the
421 # lowest recorded amount. The graphs for each volume are stacked above
422 # each other so that total disk usage is visible.
423 if trace.monitor_disk:
424 ctx.set_font_size(LEGEND_FONT_SIZE)
425 # Determine set of volumes for which we have
426 # information and the minimal amount of used disk
427 # space for each. Currently samples are allowed to
428 # not have a values for all volumes; drawing could be
429 # made more efficient if that wasn't the case.
430 volumes = set()
431 min_used = {}
432 for sample in trace.monitor_disk:
433 for volume, used in sample.records.items():
434 volumes.add(volume)
435 if volume not in min_used or min_used[volume] > used:
436 min_used[volume] = used
437 volumes = sorted(list(volumes))
438 disk_scale = 0
439 for i, volume in enumerate(volumes):
440 volume_scale = max([sample.records[volume] - min_used[volume]
441 for sample in trace.monitor_disk
442 if volume in sample.records])
443 # Does not take length of volume name into account, but fixed offset
444 # works okay in practice.
445 draw_legend_box(ctx, '%s (max: %u MiB)' % (volume, volume_scale / 1024 / 1024),
446 VOLUME_COLORS[i % len(VOLUME_COLORS)],
447 off_x + i * 250, curr_y+20, leg_s)
448 disk_scale += volume_scale
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500449
Brad Bishopc342db32019-05-15 21:57:59 -0400450 # render used amount of disk space
451 chart_rect = (off_x, curr_y+30, w, bar_h)
452 if clip_visible (clip, chart_rect):
453 draw_box_ticks (ctx, chart_rect, sec_w)
454 draw_annotations (ctx, proc_tree, trace.times, chart_rect)
455 for i in range(len(volumes), 0, -1):
456 draw_chart (ctx, VOLUME_COLORS[(i - 1) % len(VOLUME_COLORS)], True, chart_rect, \
457 [(sample.time,
458 # Sum up used space of all volumes including the current one
459 # so that the graphs appear as stacked on top of each other.
460 functools.reduce(lambda x,y: x+y,
461 [sample.records[volume] - min_used[volume]
462 for volume in volumes[0:i]
463 if volume in sample.records],
464 0))
465 for sample in trace.monitor_disk], \
466 proc_tree, [0, disk_scale])
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500467
Brad Bishopc342db32019-05-15 21:57:59 -0400468 curr_y = curr_y + 30 + bar_h
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500469
Brad Bishopc342db32019-05-15 21:57:59 -0400470 # render mem usage
471 chart_rect = (off_x, curr_y+30, w, meminfo_bar_h)
472 mem_stats = trace.mem_stats
473 if mem_stats and clip_visible (clip, chart_rect):
474 mem_scale = max(sample.buffers for sample in mem_stats)
475 draw_legend_box(ctx, "Mem cached (scale: %u MiB)" % (float(mem_scale) / 1024), MEM_CACHED_COLOR, off_x, curr_y+20, leg_s)
476 draw_legend_box(ctx, "Used", MEM_USED_COLOR, off_x + 240, curr_y+20, leg_s)
477 draw_legend_box(ctx, "Buffers", MEM_BUFFERS_COLOR, off_x + 360, curr_y+20, leg_s)
478 draw_legend_line(ctx, "Swap (scale: %u MiB)" % max([(sample.swap)/1024 for sample in mem_stats]), \
479 MEM_SWAP_COLOR, off_x + 480, curr_y+20, leg_s)
480 draw_box_ticks(ctx, chart_rect, sec_w)
481 draw_annotations(ctx, proc_tree, trace.times, chart_rect)
482 draw_chart(ctx, MEM_BUFFERS_COLOR, True, chart_rect, \
483 [(sample.time, sample.buffers) for sample in trace.mem_stats], \
484 proc_tree, [0, mem_scale])
485 draw_chart(ctx, MEM_USED_COLOR, True, chart_rect, \
486 [(sample.time, sample.used) for sample in mem_stats], \
487 proc_tree, [0, mem_scale])
488 draw_chart(ctx, MEM_CACHED_COLOR, True, chart_rect, \
489 [(sample.time, sample.cached) for sample in mem_stats], \
490 proc_tree, [0, mem_scale])
491 draw_chart(ctx, MEM_SWAP_COLOR, False, chart_rect, \
492 [(sample.time, float(sample.swap)) for sample in mem_stats], \
493 proc_tree, None)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500494
Brad Bishopc342db32019-05-15 21:57:59 -0400495 curr_y = curr_y + meminfo_bar_h
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500496
Brad Bishopc342db32019-05-15 21:57:59 -0400497 return curr_y
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500498
499def render_processes_chart(ctx, options, trace, curr_y, w, h, sec_w):
Brad Bishopc342db32019-05-15 21:57:59 -0400500 chart_rect = [off_x, curr_y+header_h, w, h - curr_y - 1 * off_y - header_h ]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500501
Brad Bishopc342db32019-05-15 21:57:59 -0400502 draw_legend_box (ctx, "Configure", \
503 TASK_COLOR_CONFIGURE, off_x , curr_y + 45, leg_s)
504 draw_legend_box (ctx, "Compile", \
505 TASK_COLOR_COMPILE, off_x+120, curr_y + 45, leg_s)
506 draw_legend_box (ctx, "Install", \
507 TASK_COLOR_INSTALL, off_x+240, curr_y + 45, leg_s)
508 draw_legend_box (ctx, "Populate Sysroot", \
509 TASK_COLOR_SYSROOT, off_x+360, curr_y + 45, leg_s)
510 draw_legend_box (ctx, "Package", \
511 TASK_COLOR_PACKAGE, off_x+480, curr_y + 45, leg_s)
512 draw_legend_box (ctx, "Package Write", \
513 TASK_COLOR_PACKAGE_WRITE, off_x+600, curr_y + 45, leg_s)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500514
Brad Bishopc342db32019-05-15 21:57:59 -0400515 ctx.set_font_size(PROC_TEXT_FONT_SIZE)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500516
Brad Bishopc342db32019-05-15 21:57:59 -0400517 draw_box_ticks(ctx, chart_rect, sec_w)
518 draw_sec_labels(ctx, options, chart_rect, sec_w, 30)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500519
Brad Bishopc342db32019-05-15 21:57:59 -0400520 y = curr_y+header_h
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500521
Brad Bishopc342db32019-05-15 21:57:59 -0400522 offset = trace.min or min(trace.start.keys())
523 for start in sorted(trace.start.keys()):
524 for process in sorted(trace.start[start]):
525 if not options.app_options.show_all and \
526 trace.processes[process][1] - start < options.app_options.mintime:
527 continue
528 task = process.split(":")[1]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500529
Brad Bishopc342db32019-05-15 21:57:59 -0400530 #print(process)
531 #print(trace.processes[process][1])
532 #print(s)
533
534 x = chart_rect[0] + (start - offset) * sec_w
535 w = ((trace.processes[process][1] - start) * sec_w)
536
537 #print("proc at %s %s %s %s" % (x, y, w, proc_h))
538 col = None
539 if task == "do_compile":
540 col = TASK_COLOR_COMPILE
541 elif task == "do_configure":
542 col = TASK_COLOR_CONFIGURE
543 elif task == "do_install":
544 col = TASK_COLOR_INSTALL
545 elif task == "do_populate_sysroot":
546 col = TASK_COLOR_SYSROOT
547 elif task == "do_package":
548 col = TASK_COLOR_PACKAGE
549 elif task == "do_package_write_rpm" or \
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500550 task == "do_package_write_deb" or \
551 task == "do_package_write_ipk":
Brad Bishopc342db32019-05-15 21:57:59 -0400552 col = TASK_COLOR_PACKAGE_WRITE
553 else:
554 col = WHITE
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500555
Brad Bishopc342db32019-05-15 21:57:59 -0400556 if col:
557 draw_fill_rect(ctx, col, (x, y, w, proc_h))
558 draw_rect(ctx, PROC_BORDER_COLOR, (x, y, w, proc_h))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500559
Brad Bishopc342db32019-05-15 21:57:59 -0400560 draw_label_in_box(ctx, PROC_TEXT_COLOR, process, x, y + proc_h - 4, w, proc_h)
561 y = y + proc_h
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500562
Brad Bishopc342db32019-05-15 21:57:59 -0400563 return curr_y
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500564
565#
566# Render the chart.
567#
568def render(ctx, options, xscale, trace):
Brad Bishopc342db32019-05-15 21:57:59 -0400569 (w, h) = extents (options, xscale, trace)
570 global OPTIONS
571 OPTIONS = options.app_options
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500572
Brad Bishopc342db32019-05-15 21:57:59 -0400573 # x, y, w, h
574 clip = ctx.clip_extents()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500575
Brad Bishopc342db32019-05-15 21:57:59 -0400576 sec_w = int (xscale * sec_w_base)
577 ctx.set_line_width(1.0)
578 ctx.select_font_face(FONT_NAME)
579 draw_fill_rect(ctx, WHITE, (0, 0, max(w, MIN_IMG_W), h))
580 w -= 2*off_x
581 curr_y = off_y;
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500582
Brad Bishopc342db32019-05-15 21:57:59 -0400583 if options.charts:
584 curr_y = render_charts (ctx, options, clip, trace, curr_y, w, h, sec_w)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500585
Brad Bishopc342db32019-05-15 21:57:59 -0400586 curr_y = render_processes_chart (ctx, options, trace, curr_y, w, h, sec_w)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500587
Brad Bishopc342db32019-05-15 21:57:59 -0400588 return
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500589
Brad Bishopc342db32019-05-15 21:57:59 -0400590 proc_tree = options.proc_tree (trace)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500591
Brad Bishopc342db32019-05-15 21:57:59 -0400592 # draw the title and headers
593 if proc_tree.idle:
594 duration = proc_tree.idle
595 else:
596 duration = proc_tree.duration
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500597
Brad Bishopc342db32019-05-15 21:57:59 -0400598 if not options.kernel_only:
599 curr_y = draw_header (ctx, trace.headers, duration)
600 else:
601 curr_y = off_y;
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500602
Brad Bishopc342db32019-05-15 21:57:59 -0400603 # draw process boxes
604 proc_height = h
605 if proc_tree.taskstats and options.cumulative:
606 proc_height -= CUML_HEIGHT
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500607
Brad Bishopc342db32019-05-15 21:57:59 -0400608 draw_process_bar_chart(ctx, clip, options, proc_tree, trace.times,
609 curr_y, w, proc_height, sec_w)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500610
Brad Bishopc342db32019-05-15 21:57:59 -0400611 curr_y = proc_height
612 ctx.set_font_size(SIG_FONT_SIZE)
613 draw_text(ctx, SIGNATURE, SIG_COLOR, off_x + 5, proc_height - 8)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500614
Brad Bishopc342db32019-05-15 21:57:59 -0400615 # draw a cumulative CPU-time-per-process graph
616 if proc_tree.taskstats and options.cumulative:
617 cuml_rect = (off_x, curr_y + off_y, w, CUML_HEIGHT/2 - off_y * 2)
618 if clip_visible (clip, cuml_rect):
619 draw_cuml_graph(ctx, proc_tree, cuml_rect, duration, sec_w, STAT_TYPE_CPU)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500620
Brad Bishopc342db32019-05-15 21:57:59 -0400621 # draw a cumulative I/O-time-per-process graph
622 if proc_tree.taskstats and options.cumulative:
623 cuml_rect = (off_x, curr_y + off_y * 100, w, CUML_HEIGHT/2 - off_y * 2)
624 if clip_visible (clip, cuml_rect):
625 draw_cuml_graph(ctx, proc_tree, cuml_rect, duration, sec_w, STAT_TYPE_IO)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500626
627def draw_process_bar_chart(ctx, clip, options, proc_tree, times, curr_y, w, h, sec_w):
Brad Bishopc342db32019-05-15 21:57:59 -0400628 header_size = 0
629 if not options.kernel_only:
630 draw_legend_box (ctx, "Running (%cpu)",
631 PROC_COLOR_R, off_x , curr_y + 45, leg_s)
632 draw_legend_box (ctx, "Unint.sleep (I/O)",
633 PROC_COLOR_D, off_x+120, curr_y + 45, leg_s)
634 draw_legend_box (ctx, "Sleeping",
635 PROC_COLOR_S, off_x+240, curr_y + 45, leg_s)
636 draw_legend_box (ctx, "Zombie",
637 PROC_COLOR_Z, off_x+360, curr_y + 45, leg_s)
638 header_size = 45
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500639
Brad Bishopc342db32019-05-15 21:57:59 -0400640 chart_rect = [off_x, curr_y + header_size + 15,
641 w, h - 2 * off_y - (curr_y + header_size + 15) + proc_h]
642 ctx.set_font_size (PROC_TEXT_FONT_SIZE)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500643
Brad Bishopc342db32019-05-15 21:57:59 -0400644 draw_box_ticks (ctx, chart_rect, sec_w)
645 if sec_w > 100:
646 nsec = 1
647 else:
648 nsec = 5
649 draw_sec_labels (ctx, options, chart_rect, sec_w, nsec)
650 draw_annotations (ctx, proc_tree, times, chart_rect)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500651
Brad Bishopc342db32019-05-15 21:57:59 -0400652 y = curr_y + 60
653 for root in proc_tree.process_tree:
654 draw_processes_recursively(ctx, root, proc_tree, y, proc_h, chart_rect, clip)
655 y = y + proc_h * proc_tree.num_nodes([root])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500656
657
658def draw_header (ctx, headers, duration):
659 toshow = [
660 ('system.uname', 'uname', lambda s: s),
661 ('system.release', 'release', lambda s: s),
662 ('system.cpu', 'CPU', lambda s: re.sub('model name\s*:\s*', '', s, 1)),
663 ('system.kernel.options', 'kernel options', lambda s: s),
664 ]
665
666 header_y = ctx.font_extents()[2] + 10
667 ctx.set_font_size(TITLE_FONT_SIZE)
668 draw_text(ctx, headers['title'], TEXT_COLOR, off_x, header_y)
669 ctx.set_font_size(TEXT_FONT_SIZE)
670
671 for (headerkey, headertitle, mangle) in toshow:
672 header_y += ctx.font_extents()[2]
673 if headerkey in headers:
674 value = headers.get(headerkey)
675 else:
676 value = ""
677 txt = headertitle + ': ' + mangle(value)
678 draw_text(ctx, txt, TEXT_COLOR, off_x, header_y)
679
680 dur = duration / 100.0
681 txt = 'time : %02d:%05.2f' % (math.floor(dur/60), dur - 60 * math.floor(dur/60))
682 if headers.get('system.maxpid') is not None:
683 txt = txt + ' max pid: %s' % (headers.get('system.maxpid'))
684
685 header_y += ctx.font_extents()[2]
686 draw_text (ctx, txt, TEXT_COLOR, off_x, header_y)
687
688 return header_y
689
690def draw_processes_recursively(ctx, proc, proc_tree, y, proc_h, rect, clip) :
Brad Bishopc342db32019-05-15 21:57:59 -0400691 x = rect[0] + ((proc.start_time - proc_tree.start_time) * rect[2] / proc_tree.duration)
692 w = ((proc.duration) * rect[2] / proc_tree.duration)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500693
Brad Bishopc342db32019-05-15 21:57:59 -0400694 draw_process_activity_colors(ctx, proc, proc_tree, x, y, w, proc_h, rect, clip)
695 draw_rect(ctx, PROC_BORDER_COLOR, (x, y, w, proc_h))
696 ipid = int(proc.pid)
697 if not OPTIONS.show_all:
698 cmdString = proc.cmd
699 else:
700 cmdString = ''
701 if (OPTIONS.show_pid or OPTIONS.show_all) and ipid is not 0:
702 cmdString = cmdString + " [" + str(ipid // 1000) + "]"
703 if OPTIONS.show_all:
704 if proc.args:
705 cmdString = cmdString + " '" + "' '".join(proc.args) + "'"
706 else:
707 cmdString = cmdString + " " + proc.exe
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500708
Brad Bishopc342db32019-05-15 21:57:59 -0400709 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 -0500710
Brad Bishopc342db32019-05-15 21:57:59 -0400711 next_y = y + proc_h
712 for child in proc.child_list:
713 if next_y > clip[1] + clip[3]:
714 break
715 child_x, child_y = draw_processes_recursively(ctx, child, proc_tree, next_y, proc_h, rect, clip)
716 draw_process_connecting_lines(ctx, x, y, child_x, child_y, proc_h)
717 next_y = next_y + proc_h * proc_tree.num_nodes([child])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500718
Brad Bishopc342db32019-05-15 21:57:59 -0400719 return x, y
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500720
721
722def draw_process_activity_colors(ctx, proc, proc_tree, x, y, w, proc_h, rect, clip):
723
Brad Bishopc342db32019-05-15 21:57:59 -0400724 if y > clip[1] + clip[3] or y + proc_h + 2 < clip[1]:
725 return
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500726
Brad Bishopc342db32019-05-15 21:57:59 -0400727 draw_fill_rect(ctx, PROC_COLOR_S, (x, y, w, proc_h))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500728
Brad Bishopc342db32019-05-15 21:57:59 -0400729 last_tx = -1
730 for sample in proc.samples :
731 tx = rect[0] + round(((sample.time - proc_tree.start_time) * rect[2] / proc_tree.duration))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500732
Brad Bishopc342db32019-05-15 21:57:59 -0400733 # samples are sorted chronologically
734 if tx < clip[0]:
735 continue
736 if tx > clip[0] + clip[2]:
737 break
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500738
Brad Bishopc342db32019-05-15 21:57:59 -0400739 tw = round(proc_tree.sample_period * rect[2] / float(proc_tree.duration))
740 if last_tx != -1 and abs(last_tx - tx) <= tw:
741 tw -= last_tx - tx
742 tx = last_tx
743 tw = max (tw, 1) # nice to see at least something
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500744
Brad Bishopc342db32019-05-15 21:57:59 -0400745 last_tx = tx + tw
746 state = get_proc_state( sample.state )
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500747
Brad Bishopc342db32019-05-15 21:57:59 -0400748 color = STATE_COLORS[state]
749 if state == STATE_RUNNING:
750 alpha = min (sample.cpu_sample.user + sample.cpu_sample.sys, 1.0)
751 color = tuple(list(PROC_COLOR_R[0:3]) + [alpha])
752# print "render time %d [ tx %d tw %d ], sample state %s color %s alpha %g" % (sample.time, tx, tw, state, color, alpha)
753 elif state == STATE_SLEEPING:
754 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500755
Brad Bishopc342db32019-05-15 21:57:59 -0400756 draw_fill_rect(ctx, color, (tx, y, tw, proc_h))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500757
758def draw_process_connecting_lines(ctx, px, py, x, y, proc_h):
Brad Bishopc342db32019-05-15 21:57:59 -0400759 ctx.set_source_rgba(*DEP_COLOR)
760 ctx.set_dash([2, 2])
761 if abs(px - x) < 3:
762 dep_off_x = 3
763 dep_off_y = proc_h / 4
764 ctx.move_to(x, y + proc_h / 2)
765 ctx.line_to(px - dep_off_x, y + proc_h / 2)
766 ctx.line_to(px - dep_off_x, py - dep_off_y)
767 ctx.line_to(px, py - dep_off_y)
768 else:
769 ctx.move_to(x, y + proc_h / 2)
770 ctx.line_to(px, y + proc_h / 2)
771 ctx.line_to(px, py)
772 ctx.stroke()
773 ctx.set_dash([])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500774
775# elide the bootchart collector - it is quite distorting
776def elide_bootchart(proc):
Brad Bishopc342db32019-05-15 21:57:59 -0400777 return proc.cmd == 'bootchartd' or proc.cmd == 'bootchart-colle'
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500778
779class CumlSample:
Brad Bishopc342db32019-05-15 21:57:59 -0400780 def __init__(self, proc):
781 self.cmd = proc.cmd
782 self.samples = []
783 self.merge_samples (proc)
784 self.color = None
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500785
Brad Bishopc342db32019-05-15 21:57:59 -0400786 def merge_samples(self, proc):
787 self.samples.extend (proc.samples)
788 self.samples.sort (key = lambda p: p.time)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500789
Brad Bishopc342db32019-05-15 21:57:59 -0400790 def next(self):
791 global palette_idx
792 palette_idx += HSV_STEP
793 return palette_idx
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500794
Brad Bishopc342db32019-05-15 21:57:59 -0400795 def get_color(self):
796 if self.color is None:
797 i = self.next() % HSV_MAX_MOD
798 h = 0.0
799 if i is not 0:
800 h = (1.0 * i) / HSV_MAX_MOD
801 s = 0.5
802 v = 1.0
803 c = colorsys.hsv_to_rgb (h, s, v)
804 self.color = (c[0], c[1], c[2], 1.0)
805 return self.color
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500806
807
808def draw_cuml_graph(ctx, proc_tree, chart_bounds, duration, sec_w, stat_type):
Brad Bishopc342db32019-05-15 21:57:59 -0400809 global palette_idx
810 palette_idx = 0
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500811
Brad Bishopc342db32019-05-15 21:57:59 -0400812 time_hash = {}
813 total_time = 0.0
814 m_proc_list = {}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500815
Brad Bishopc342db32019-05-15 21:57:59 -0400816 if stat_type is STAT_TYPE_CPU:
817 sample_value = 'cpu'
818 else:
819 sample_value = 'io'
820 for proc in proc_tree.process_list:
821 if elide_bootchart(proc):
822 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500823
Brad Bishopc342db32019-05-15 21:57:59 -0400824 for sample in proc.samples:
825 total_time += getattr(sample.cpu_sample, sample_value)
826 if not sample.time in time_hash:
827 time_hash[sample.time] = 1
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500828
Brad Bishopc342db32019-05-15 21:57:59 -0400829 # merge pids with the same cmd
830 if not proc.cmd in m_proc_list:
831 m_proc_list[proc.cmd] = CumlSample (proc)
832 continue
833 s = m_proc_list[proc.cmd]
834 s.merge_samples (proc)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500835
Brad Bishopc342db32019-05-15 21:57:59 -0400836 # all the sample times
837 times = sorted(time_hash)
838 if len (times) < 2:
839 print("degenerate boot chart")
840 return
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500841
Brad Bishopc342db32019-05-15 21:57:59 -0400842 pix_per_ns = chart_bounds[3] / total_time
843# print "total time: %g pix-per-ns %g" % (total_time, pix_per_ns)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500844
Brad Bishopc342db32019-05-15 21:57:59 -0400845 # FIXME: we have duplicates in the process list too [!] - why !?
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500846
Brad Bishopc342db32019-05-15 21:57:59 -0400847 # Render bottom up, left to right
848 below = {}
849 for time in times:
850 below[time] = chart_bounds[1] + chart_bounds[3]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500851
Brad Bishopc342db32019-05-15 21:57:59 -0400852 # same colors each time we render
853 random.seed (0)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500854
Brad Bishopc342db32019-05-15 21:57:59 -0400855 ctx.set_line_width(1)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500856
Brad Bishopc342db32019-05-15 21:57:59 -0400857 legends = []
858 labels = []
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500859
Brad Bishopc342db32019-05-15 21:57:59 -0400860 # render each pid in order
861 for cs in m_proc_list.values():
862 row = {}
863 cuml = 0.0
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500864
Brad Bishopc342db32019-05-15 21:57:59 -0400865 # print "pid : %s -> %g samples %d" % (proc.cmd, cuml, len (cs.samples))
866 for sample in cs.samples:
867 cuml += getattr(sample.cpu_sample, sample_value)
868 row[sample.time] = cuml
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500869
Brad Bishopc342db32019-05-15 21:57:59 -0400870 process_total_time = cuml
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500871
Brad Bishopc342db32019-05-15 21:57:59 -0400872 # hide really tiny processes
873 if cuml * pix_per_ns <= 2:
874 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500875
Brad Bishopc342db32019-05-15 21:57:59 -0400876 last_time = times[0]
877 y = last_below = below[last_time]
878 last_cuml = cuml = 0.0
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500879
Brad Bishopc342db32019-05-15 21:57:59 -0400880 ctx.set_source_rgba(*cs.get_color())
881 for time in times:
882 render_seg = False
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500883
Brad Bishopc342db32019-05-15 21:57:59 -0400884 # did the underlying trend increase ?
885 if below[time] != last_below:
886 last_below = below[last_time]
887 last_cuml = cuml
888 render_seg = True
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500889
Brad Bishopc342db32019-05-15 21:57:59 -0400890 # did we move up a pixel increase ?
891 if time in row:
892 nc = round (row[time] * pix_per_ns)
893 if nc != cuml:
894 last_cuml = cuml
895 cuml = nc
896 render_seg = True
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500897
Brad Bishopc342db32019-05-15 21:57:59 -0400898# if last_cuml > cuml:
899# assert fail ... - un-sorted process samples
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500900
Brad Bishopc342db32019-05-15 21:57:59 -0400901 # draw the trailing rectangle from the last time to
902 # before now, at the height of the last segment.
903 if render_seg:
904 w = math.ceil ((time - last_time) * chart_bounds[2] / proc_tree.duration) + 1
905 x = chart_bounds[0] + round((last_time - proc_tree.start_time) * chart_bounds[2] / proc_tree.duration)
906 ctx.rectangle (x, below[last_time] - last_cuml, w, last_cuml)
907 ctx.fill()
908# ctx.stroke()
909 last_time = time
910 y = below [time] - cuml
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500911
Brad Bishopc342db32019-05-15 21:57:59 -0400912 row[time] = y
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500913
Brad Bishopc342db32019-05-15 21:57:59 -0400914 # render the last segment
915 x = chart_bounds[0] + round((last_time - proc_tree.start_time) * chart_bounds[2] / proc_tree.duration)
916 y = below[last_time] - cuml
917 ctx.rectangle (x, y, chart_bounds[2] - x, cuml)
918 ctx.fill()
919# ctx.stroke()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500920
Brad Bishopc342db32019-05-15 21:57:59 -0400921 # render legend if it will fit
922 if cuml > 8:
923 label = cs.cmd
924 extnts = ctx.text_extents(label)
925 label_w = extnts[2]
926 label_h = extnts[3]
927# print "Text extents %g by %g" % (label_w, label_h)
928 labels.append((label,
929 chart_bounds[0] + chart_bounds[2] - label_w - off_x * 2,
930 y + (cuml + label_h) / 2))
931 if cs in legends:
932 print("ARGH - duplicate process in list !")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500933
Brad Bishopc342db32019-05-15 21:57:59 -0400934 legends.append ((cs, process_total_time))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500935
Brad Bishopc342db32019-05-15 21:57:59 -0400936 below = row
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500937
Brad Bishopc342db32019-05-15 21:57:59 -0400938 # render grid-lines over the top
939 draw_box_ticks(ctx, chart_bounds, sec_w)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500940
Brad Bishopc342db32019-05-15 21:57:59 -0400941 # render labels
942 for l in labels:
943 draw_text(ctx, l[0], TEXT_COLOR, l[1], l[2])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500944
Brad Bishopc342db32019-05-15 21:57:59 -0400945 # Render legends
946 font_height = 20
947 label_width = 300
948 LEGENDS_PER_COL = 15
949 LEGENDS_TOTAL = 45
950 ctx.set_font_size (TITLE_FONT_SIZE)
951 dur_secs = duration / 100
952 cpu_secs = total_time / 1000000000
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500953
Brad Bishopc342db32019-05-15 21:57:59 -0400954 # misleading - with multiple CPUs ...
955# idle = ((dur_secs - cpu_secs) / dur_secs) * 100.0
956 if stat_type is STAT_TYPE_CPU:
957 label = "Cumulative CPU usage, by process; total CPU: " \
958 " %.5g(s) time: %.3g(s)" % (cpu_secs, dur_secs)
959 else:
960 label = "Cumulative I/O usage, by process; total I/O: " \
961 " %.5g(s) time: %.3g(s)" % (cpu_secs, dur_secs)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500962
Brad Bishopc342db32019-05-15 21:57:59 -0400963 draw_text(ctx, label, TEXT_COLOR, chart_bounds[0] + off_x,
964 chart_bounds[1] + font_height)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500965
Brad Bishopc342db32019-05-15 21:57:59 -0400966 i = 0
967 legends = sorted(legends, key=itemgetter(1), reverse=True)
968 ctx.set_font_size(TEXT_FONT_SIZE)
969 for t in legends:
970 cs = t[0]
971 time = t[1]
972 x = chart_bounds[0] + off_x + int (i/LEGENDS_PER_COL) * label_width
973 y = chart_bounds[1] + font_height * ((i % LEGENDS_PER_COL) + 2)
974 str = "%s - %.0f(ms) (%2.2f%%)" % (cs.cmd, time/1000000, (time/total_time) * 100.0)
975 draw_legend_box(ctx, str, cs.color, x, y, leg_s)
976 i = i + 1
977 if i >= LEGENDS_TOTAL:
978 break