Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # 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 | |
| 17 | import cairo |
| 18 | import math |
| 19 | import re |
| 20 | import random |
| 21 | import colorsys |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 22 | import functools |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 23 | from operator import itemgetter |
| 24 | |
| 25 | class RenderOptions: |
| 26 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 27 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 33 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 34 | def proc_tree (self, trace): |
| 35 | if self.kernel_only: |
| 36 | return trace.kernel_tree |
| 37 | else: |
| 38 | return trace.proc_tree |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 39 | |
| 40 | # Process tree background color. |
| 41 | BACK_COLOR = (1.0, 1.0, 1.0, 1.0) |
| 42 | |
| 43 | WHITE = (1.0, 1.0, 1.0, 1.0) |
| 44 | # Process tree border color. |
| 45 | BORDER_COLOR = (0.63, 0.63, 0.63, 1.0) |
| 46 | # Second tick line color. |
| 47 | TICK_COLOR = (0.92, 0.92, 0.92, 1.0) |
| 48 | # 5-second tick line color. |
| 49 | TICK_COLOR_BOLD = (0.86, 0.86, 0.86, 1.0) |
| 50 | # Annotation colour |
| 51 | ANNOTATION_COLOR = (0.63, 0.0, 0.0, 0.5) |
| 52 | # Text color. |
| 53 | TEXT_COLOR = (0.0, 0.0, 0.0, 1.0) |
| 54 | |
| 55 | # Font family |
| 56 | FONT_NAME = "Bitstream Vera Sans" |
| 57 | # Title text font. |
| 58 | TITLE_FONT_SIZE = 18 |
| 59 | # Default text font. |
| 60 | TEXT_FONT_SIZE = 12 |
| 61 | # Axis label font. |
| 62 | AXIS_FONT_SIZE = 11 |
| 63 | # Legend font. |
| 64 | LEGEND_FONT_SIZE = 12 |
| 65 | |
| 66 | # CPU load chart color. |
| 67 | CPU_COLOR = (0.40, 0.55, 0.70, 1.0) |
| 68 | # IO wait chart color. |
| 69 | IO_COLOR = (0.76, 0.48, 0.48, 0.5) |
| 70 | # Disk throughput color. |
| 71 | DISK_TPUT_COLOR = (0.20, 0.71, 0.20, 1.0) |
| 72 | # CPU load chart color. |
| 73 | FILE_OPEN_COLOR = (0.20, 0.71, 0.71, 1.0) |
| 74 | # Mem cached color |
| 75 | MEM_CACHED_COLOR = CPU_COLOR |
| 76 | # Mem used color |
| 77 | MEM_USED_COLOR = IO_COLOR |
| 78 | # Buffers color |
| 79 | MEM_BUFFERS_COLOR = (0.4, 0.4, 0.4, 0.3) |
| 80 | # Swap color |
| 81 | MEM_SWAP_COLOR = DISK_TPUT_COLOR |
| 82 | |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame^] | 83 | # avg10 CPU pressure color |
| 84 | CPU_PRESSURE_AVG10_COLOR = (0.0, 0.0, 0.0, 1.0) |
| 85 | # delta total CPU pressure color |
| 86 | CPU_PRESSURE_TOTAL_COLOR = CPU_COLOR |
| 87 | # avg10 IO pressure color |
| 88 | IO_PRESSURE_AVG10_COLOR = (0.0, 0.0, 0.0, 1.0) |
| 89 | # delta total IO pressure color |
| 90 | IO_PRESSURE_TOTAL_COLOR = IO_COLOR |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 95 | # Process border color. |
| 96 | PROC_BORDER_COLOR = (0.71, 0.71, 0.71, 1.0) |
| 97 | # Waiting process color. |
| 98 | PROC_COLOR_D = (0.76, 0.48, 0.48, 0.5) |
| 99 | # Running process color. |
| 100 | PROC_COLOR_R = CPU_COLOR |
| 101 | # Sleeping process color. |
| 102 | PROC_COLOR_S = (0.94, 0.94, 0.94, 1.0) |
| 103 | # Stopped process color. |
| 104 | PROC_COLOR_T = (0.94, 0.50, 0.50, 1.0) |
| 105 | # Zombie process color. |
| 106 | PROC_COLOR_Z = (0.71, 0.71, 0.71, 1.0) |
| 107 | # Dead process color. |
| 108 | PROC_COLOR_X = (0.71, 0.71, 0.71, 0.125) |
| 109 | # Paging process color. |
| 110 | PROC_COLOR_W = (0.71, 0.71, 0.71, 0.125) |
| 111 | |
| 112 | # Process label color. |
| 113 | PROC_TEXT_COLOR = (0.19, 0.19, 0.19, 1.0) |
| 114 | # Process label font. |
| 115 | PROC_TEXT_FONT_SIZE = 12 |
| 116 | |
| 117 | # Signature color. |
| 118 | SIG_COLOR = (0.0, 0.0, 0.0, 0.3125) |
| 119 | # Signature font. |
| 120 | SIG_FONT_SIZE = 14 |
| 121 | # Signature text. |
| 122 | SIGNATURE = "http://github.com/mmeeks/bootchart" |
| 123 | |
| 124 | # Process dependency line color. |
| 125 | DEP_COLOR = (0.75, 0.75, 0.75, 1.0) |
| 126 | # Process dependency line stroke. |
| 127 | DEP_STROKE = 1.0 |
| 128 | |
| 129 | # Process description date format. |
| 130 | DESC_TIME_FORMAT = "mm:ss.SSS" |
| 131 | |
| 132 | # Cumulative coloring bits |
| 133 | HSV_MAX_MOD = 31 |
| 134 | HSV_STEP = 7 |
| 135 | |
| 136 | # Configure task color |
| 137 | TASK_COLOR_CONFIGURE = (1.0, 1.0, 0.00, 1.0) |
| 138 | # Compile task color. |
| 139 | TASK_COLOR_COMPILE = (0.0, 1.00, 0.00, 1.0) |
| 140 | # Install task color |
| 141 | TASK_COLOR_INSTALL = (1.0, 0.00, 1.00, 1.0) |
| 142 | # Sysroot task color |
| 143 | TASK_COLOR_SYSROOT = (0.0, 0.00, 1.00, 1.0) |
| 144 | # Package task color |
| 145 | TASK_COLOR_PACKAGE = (0.0, 1.00, 1.00, 1.0) |
| 146 | # Package Write RPM/DEB/IPK task color |
| 147 | TASK_COLOR_PACKAGE_WRITE = (0.0, 0.50, 0.50, 1.0) |
| 148 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 149 | # Distinct colors used for different disk volumnes. |
| 150 | # If we have more volumns, colors get re-used. |
| 151 | VOLUME_COLORS = [ |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 152 | (1.0, 1.0, 0.00, 1.0), |
| 153 | (0.0, 1.00, 0.00, 1.0), |
| 154 | (1.0, 0.00, 1.00, 1.0), |
| 155 | (0.0, 0.00, 1.00, 1.0), |
| 156 | (0.0, 1.00, 1.00, 1.0), |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 157 | ] |
| 158 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 159 | # Process states |
| 160 | STATE_UNDEFINED = 0 |
| 161 | STATE_RUNNING = 1 |
| 162 | STATE_SLEEPING = 2 |
| 163 | STATE_WAITING = 3 |
| 164 | STATE_STOPPED = 4 |
| 165 | STATE_ZOMBIE = 5 |
| 166 | |
| 167 | STATE_COLORS = [(0, 0, 0, 0), PROC_COLOR_R, PROC_COLOR_S, PROC_COLOR_D, \ |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 168 | PROC_COLOR_T, PROC_COLOR_Z, PROC_COLOR_X, PROC_COLOR_W] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 169 | |
| 170 | # CumulativeStats Types |
| 171 | STAT_TYPE_CPU = 0 |
| 172 | STAT_TYPE_IO = 1 |
| 173 | |
| 174 | # Convert ps process state to an int |
| 175 | def get_proc_state(flag): |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 176 | return "RSDTZXW".find(flag) + 1 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 177 | |
| 178 | def draw_text(ctx, text, color, x, y): |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 179 | ctx.set_source_rgba(*color) |
| 180 | ctx.move_to(x, y) |
| 181 | ctx.show_text(text) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 182 | |
| 183 | def draw_fill_rect(ctx, color, rect): |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 184 | ctx.set_source_rgba(*color) |
| 185 | ctx.rectangle(*rect) |
| 186 | ctx.fill() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 187 | |
| 188 | def draw_rect(ctx, color, rect): |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 189 | ctx.set_source_rgba(*color) |
| 190 | ctx.rectangle(*rect) |
| 191 | ctx.stroke() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 192 | |
| 193 | def draw_legend_box(ctx, label, fill_color, x, y, s): |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 194 | draw_fill_rect(ctx, fill_color, (x, y - s, s, s)) |
| 195 | draw_rect(ctx, PROC_BORDER_COLOR, (x, y - s, s, s)) |
| 196 | draw_text(ctx, label, TEXT_COLOR, x + s + 5, y) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 197 | |
| 198 | def draw_legend_line(ctx, label, fill_color, x, y, s): |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 199 | draw_fill_rect(ctx, fill_color, (x, y - s/2, s + 1, 3)) |
| 200 | ctx.arc(x + (s + 1)/2.0, y - (s - 3)/2.0, 2.5, 0, 2.0 * math.pi) |
| 201 | ctx.fill() |
| 202 | draw_text(ctx, label, TEXT_COLOR, x + s + 5, y) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 203 | |
| 204 | def draw_label_in_box(ctx, color, label, x, y, w, maxx): |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 205 | label_w = ctx.text_extents(label)[2] |
| 206 | label_x = x + w / 2 - label_w / 2 |
| 207 | if label_w + 10 > w: |
| 208 | label_x = x + w + 5 |
| 209 | if label_x + label_w > maxx: |
| 210 | label_x = x - label_w - 5 |
| 211 | draw_text(ctx, label, color, label_x, y) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 212 | |
| 213 | def draw_sec_labels(ctx, options, rect, sec_w, nsecs): |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 214 | ctx.set_font_size(AXIS_FONT_SIZE) |
| 215 | prev_x = 0 |
| 216 | for i in range(0, rect[2] + 1, sec_w): |
| 217 | if ((i / sec_w) % nsecs == 0) : |
| 218 | if options.app_options.as_minutes : |
| 219 | label = "%.1f" % (i / sec_w / 60.0) |
| 220 | else : |
| 221 | label = "%d" % (i / sec_w) |
| 222 | label_w = ctx.text_extents(label)[2] |
| 223 | x = rect[0] + i - label_w/2 |
| 224 | if x >= prev_x: |
| 225 | draw_text(ctx, label, TEXT_COLOR, x, rect[1] - 2) |
| 226 | prev_x = x + label_w |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 227 | |
| 228 | def draw_box_ticks(ctx, rect, sec_w): |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 229 | draw_rect(ctx, BORDER_COLOR, tuple(rect)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 230 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 231 | ctx.set_line_cap(cairo.LINE_CAP_SQUARE) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 232 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 233 | for i in range(sec_w, rect[2] + 1, sec_w): |
| 234 | if ((i / sec_w) % 10 == 0) : |
| 235 | ctx.set_line_width(1.5) |
| 236 | elif sec_w < 5 : |
| 237 | continue |
| 238 | else : |
| 239 | ctx.set_line_width(1.0) |
| 240 | if ((i / sec_w) % 30 == 0) : |
| 241 | ctx.set_source_rgba(*TICK_COLOR_BOLD) |
| 242 | else : |
| 243 | ctx.set_source_rgba(*TICK_COLOR) |
| 244 | ctx.move_to(rect[0] + i, rect[1] + 1) |
| 245 | ctx.line_to(rect[0] + i, rect[1] + rect[3] - 1) |
| 246 | ctx.stroke() |
| 247 | ctx.set_line_width(1.0) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 248 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 249 | ctx.set_line_cap(cairo.LINE_CAP_BUTT) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 250 | |
| 251 | def draw_annotations(ctx, proc_tree, times, rect): |
| 252 | ctx.set_line_cap(cairo.LINE_CAP_SQUARE) |
| 253 | ctx.set_source_rgba(*ANNOTATION_COLOR) |
| 254 | ctx.set_dash([4, 4]) |
| 255 | |
| 256 | for time in times: |
| 257 | if time is not None: |
| 258 | x = ((time - proc_tree.start_time) * rect[2] / proc_tree.duration) |
| 259 | |
| 260 | ctx.move_to(rect[0] + x, rect[1] + 1) |
| 261 | ctx.line_to(rect[0] + x, rect[1] + rect[3] - 1) |
| 262 | ctx.stroke() |
| 263 | |
| 264 | ctx.set_line_cap(cairo.LINE_CAP_BUTT) |
| 265 | ctx.set_dash([]) |
| 266 | |
| 267 | def draw_chart(ctx, color, fill, chart_bounds, data, proc_tree, data_range): |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 268 | ctx.set_line_width(0.5) |
| 269 | x_shift = proc_tree.start_time |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 270 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 271 | def transform_point_coords(point, x_base, y_base, \ |
| 272 | xscale, yscale, x_trans, y_trans): |
| 273 | x = (point[0] - x_base) * xscale + x_trans |
| 274 | y = (point[1] - y_base) * -yscale + y_trans + chart_bounds[3] |
| 275 | return x, y |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 276 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 277 | max_x = max (x for (x, y) in data) |
| 278 | max_y = max (y for (x, y) in data) |
| 279 | # avoid divide by zero |
| 280 | if max_y == 0: |
| 281 | max_y = 1.0 |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 282 | if (max_x - x_shift): |
| 283 | xscale = float (chart_bounds[2]) / (max_x - x_shift) |
| 284 | else: |
| 285 | xscale = float (chart_bounds[2]) |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 286 | # If data_range is given, scale the chart so that the value range in |
| 287 | # data_range matches the chart bounds exactly. |
| 288 | # Otherwise, scale so that the actual data matches the chart bounds. |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 289 | if data_range and (data_range[1] - data_range[0]): |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 290 | yscale = float(chart_bounds[3]) / (data_range[1] - data_range[0]) |
| 291 | ybase = data_range[0] |
| 292 | else: |
| 293 | yscale = float(chart_bounds[3]) / max_y |
| 294 | ybase = 0 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 295 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 296 | first = transform_point_coords (data[0], x_shift, ybase, xscale, yscale, \ |
| 297 | chart_bounds[0], chart_bounds[1]) |
| 298 | last = transform_point_coords (data[-1], x_shift, ybase, xscale, yscale, \ |
| 299 | chart_bounds[0], chart_bounds[1]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 300 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 301 | ctx.set_source_rgba(*color) |
| 302 | ctx.move_to(*first) |
| 303 | for point in data: |
| 304 | x, y = transform_point_coords (point, x_shift, ybase, xscale, yscale, \ |
| 305 | chart_bounds[0], chart_bounds[1]) |
| 306 | ctx.line_to(x, y) |
| 307 | if fill: |
| 308 | ctx.stroke_preserve() |
| 309 | ctx.line_to(last[0], chart_bounds[1]+chart_bounds[3]) |
| 310 | ctx.line_to(first[0], chart_bounds[1]+chart_bounds[3]) |
| 311 | ctx.line_to(first[0], first[1]) |
| 312 | ctx.fill() |
| 313 | else: |
| 314 | ctx.stroke() |
| 315 | ctx.set_line_width(1.0) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 316 | |
| 317 | bar_h = 55 |
| 318 | meminfo_bar_h = 2 * bar_h |
| 319 | header_h = 60 |
| 320 | # offsets |
| 321 | off_x, off_y = 220, 10 |
| 322 | sec_w_base = 1 # the width of a second |
| 323 | proc_h = 16 # the height of a process |
| 324 | leg_s = 10 |
| 325 | MIN_IMG_W = 800 |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 326 | CUML_HEIGHT = 2000 # Increased value to accommodate CPU and I/O Graphs |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 327 | OPTIONS = None |
| 328 | |
| 329 | def extents(options, xscale, trace): |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 330 | start = min(trace.start.keys()) |
| 331 | end = start |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 332 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 333 | processes = 0 |
| 334 | for proc in trace.processes: |
| 335 | if not options.app_options.show_all and \ |
| 336 | trace.processes[proc][1] - trace.processes[proc][0] < options.app_options.mintime: |
| 337 | continue |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 338 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 339 | if trace.processes[proc][1] > end: |
| 340 | end = trace.processes[proc][1] |
| 341 | processes += 1 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 342 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 343 | if trace.min is not None and trace.max is not None: |
| 344 | start = trace.min |
| 345 | end = trace.max |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 346 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 347 | w = int ((end - start) * sec_w_base * xscale) + 2 * off_x |
| 348 | h = proc_h * processes + header_h + 2 * off_y |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 349 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 350 | if options.charts: |
| 351 | if trace.cpu_stats: |
| 352 | h += 30 + bar_h |
| 353 | if trace.disk_stats: |
| 354 | h += 30 + bar_h |
| 355 | if trace.monitor_disk: |
| 356 | h += 30 + bar_h |
| 357 | if trace.mem_stats: |
| 358 | h += meminfo_bar_h |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 359 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 360 | # Allow for width of process legend and offset |
| 361 | if w < (720 + off_x): |
| 362 | w = 720 + off_x |
| 363 | |
| 364 | return (w, h) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 365 | |
| 366 | def clip_visible(clip, rect): |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 367 | xmax = max (clip[0], rect[0]) |
| 368 | ymax = max (clip[1], rect[1]) |
| 369 | xmin = min (clip[0] + clip[2], rect[0] + rect[2]) |
| 370 | ymin = min (clip[1] + clip[3], rect[1] + rect[3]) |
| 371 | return (xmin > xmax and ymin > ymax) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 372 | |
| 373 | def render_charts(ctx, options, clip, trace, curr_y, w, h, sec_w): |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 374 | proc_tree = options.proc_tree(trace) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 375 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 376 | # render bar legend |
| 377 | if trace.cpu_stats: |
| 378 | ctx.set_font_size(LEGEND_FONT_SIZE) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 379 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 380 | draw_legend_box(ctx, "CPU (user+sys)", CPU_COLOR, off_x, curr_y+20, leg_s) |
| 381 | draw_legend_box(ctx, "I/O (wait)", IO_COLOR, off_x + 120, curr_y+20, leg_s) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 382 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 383 | # render I/O wait |
| 384 | chart_rect = (off_x, curr_y+30, w, bar_h) |
| 385 | if clip_visible (clip, chart_rect): |
| 386 | draw_box_ticks (ctx, chart_rect, sec_w) |
| 387 | draw_annotations (ctx, proc_tree, trace.times, chart_rect) |
| 388 | draw_chart (ctx, IO_COLOR, True, chart_rect, \ |
| 389 | [(sample.time, sample.user + sample.sys + sample.io) for sample in trace.cpu_stats], \ |
| 390 | proc_tree, None) |
| 391 | # render CPU load |
| 392 | draw_chart (ctx, CPU_COLOR, True, chart_rect, \ |
| 393 | [(sample.time, sample.user + sample.sys) for sample in trace.cpu_stats], \ |
| 394 | proc_tree, None) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 395 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 396 | curr_y = curr_y + 30 + bar_h |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 397 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 398 | # render second chart |
| 399 | if trace.disk_stats: |
| 400 | draw_legend_line(ctx, "Disk throughput", DISK_TPUT_COLOR, off_x, curr_y+20, leg_s) |
| 401 | draw_legend_box(ctx, "Disk utilization", IO_COLOR, off_x + 120, curr_y+20, leg_s) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 402 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 403 | # render I/O utilization |
| 404 | chart_rect = (off_x, curr_y+30, w, bar_h) |
| 405 | if clip_visible (clip, chart_rect): |
| 406 | draw_box_ticks (ctx, chart_rect, sec_w) |
| 407 | draw_annotations (ctx, proc_tree, trace.times, chart_rect) |
| 408 | draw_chart (ctx, IO_COLOR, True, chart_rect, \ |
| 409 | [(sample.time, sample.util) for sample in trace.disk_stats], \ |
| 410 | proc_tree, None) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 411 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 412 | # render disk throughput |
| 413 | max_sample = max (trace.disk_stats, key = lambda s: s.tput) |
| 414 | if clip_visible (clip, chart_rect): |
| 415 | draw_chart (ctx, DISK_TPUT_COLOR, False, chart_rect, \ |
| 416 | [(sample.time, sample.tput) for sample in trace.disk_stats], \ |
| 417 | proc_tree, None) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 418 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 419 | pos_x = off_x + ((max_sample.time - proc_tree.start_time) * w / proc_tree.duration) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 420 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 421 | shift_x, shift_y = -20, 20 |
| 422 | if (pos_x < off_x + 245): |
| 423 | shift_x, shift_y = 5, 40 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 424 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 425 | label = "%dMB/s" % round ((max_sample.tput) / 1024.0) |
| 426 | draw_text (ctx, label, DISK_TPUT_COLOR, pos_x + shift_x, curr_y + shift_y) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 427 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 428 | curr_y = curr_y + 30 + bar_h |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 429 | |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame^] | 430 | # render CPU pressure chart |
| 431 | if trace.cpu_pressure: |
| 432 | draw_legend_line(ctx, "avg10 CPU Pressure", CPU_PRESSURE_AVG10_COLOR, off_x, curr_y+20, leg_s) |
| 433 | draw_legend_box(ctx, "delta total CPU Pressure", CPU_PRESSURE_TOTAL_COLOR, off_x + 140, curr_y+20, leg_s) |
| 434 | |
| 435 | # render delta total cpu |
| 436 | chart_rect = (off_x, curr_y+30, w, bar_h) |
| 437 | if clip_visible (clip, chart_rect): |
| 438 | draw_box_ticks (ctx, chart_rect, sec_w) |
| 439 | draw_annotations (ctx, proc_tree, trace.times, chart_rect) |
| 440 | draw_chart (ctx, CPU_PRESSURE_TOTAL_COLOR, True, chart_rect, \ |
| 441 | [(sample.time, sample.deltaTotal) for sample in trace.cpu_pressure], \ |
| 442 | proc_tree, None) |
| 443 | |
| 444 | # render avg10 cpu |
| 445 | max_sample = max (trace.cpu_pressure, key = lambda s: s.avg10) |
| 446 | if clip_visible (clip, chart_rect): |
| 447 | draw_chart (ctx, CPU_PRESSURE_AVG10_COLOR, False, chart_rect, \ |
| 448 | [(sample.time, sample.avg10) for sample in trace.cpu_pressure], \ |
| 449 | proc_tree, None) |
| 450 | |
| 451 | pos_x = off_x + ((max_sample.time - proc_tree.start_time) * w / proc_tree.duration) |
| 452 | |
| 453 | shift_x, shift_y = -20, 20 |
| 454 | if (pos_x < off_x + 245): |
| 455 | shift_x, shift_y = 5, 40 |
| 456 | |
| 457 | |
| 458 | label = "%d%%" % (max_sample.avg10) |
| 459 | draw_text (ctx, label, CPU_PRESSURE_AVG10_COLOR, pos_x + shift_x, curr_y + shift_y) |
| 460 | |
| 461 | curr_y = curr_y + 30 + bar_h |
| 462 | |
| 463 | # render delta total io |
| 464 | if trace.io_pressure: |
| 465 | draw_legend_line(ctx, "avg10 I/O Pressure", IO_PRESSURE_AVG10_COLOR, off_x, curr_y+20, leg_s) |
| 466 | draw_legend_box(ctx, "delta total I/O Pressure", IO_PRESSURE_TOTAL_COLOR, off_x + 140, curr_y+20, leg_s) |
| 467 | |
| 468 | # render avg10 io |
| 469 | chart_rect = (off_x, curr_y+30, w, bar_h) |
| 470 | if clip_visible (clip, chart_rect): |
| 471 | draw_box_ticks (ctx, chart_rect, sec_w) |
| 472 | draw_annotations (ctx, proc_tree, trace.times, chart_rect) |
| 473 | draw_chart (ctx, IO_PRESSURE_TOTAL_COLOR, True, chart_rect, \ |
| 474 | [(sample.time, sample.deltaTotal) for sample in trace.io_pressure], \ |
| 475 | proc_tree, None) |
| 476 | |
| 477 | # render io pressure |
| 478 | max_sample = max (trace.io_pressure, key = lambda s: s.avg10) |
| 479 | if clip_visible (clip, chart_rect): |
| 480 | draw_chart (ctx, IO_PRESSURE_AVG10_COLOR, False, chart_rect, \ |
| 481 | [(sample.time, sample.avg10) for sample in trace.io_pressure], \ |
| 482 | proc_tree, None) |
| 483 | |
| 484 | pos_x = off_x + ((max_sample.time - proc_tree.start_time) * w / proc_tree.duration) |
| 485 | |
| 486 | shift_x, shift_y = -20, 20 |
| 487 | if (pos_x < off_x + 245): |
| 488 | shift_x, shift_y = 5, 40 |
| 489 | |
| 490 | label = "%d%%" % (max_sample.avg10) |
| 491 | draw_text (ctx, label, IO_PRESSURE_AVG10_COLOR, pos_x + shift_x, curr_y + shift_y) |
| 492 | |
| 493 | curr_y = curr_y + 30 + bar_h |
| 494 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 495 | # render disk space usage |
| 496 | # |
| 497 | # Draws the amount of disk space used on each volume relative to the |
| 498 | # lowest recorded amount. The graphs for each volume are stacked above |
| 499 | # each other so that total disk usage is visible. |
| 500 | if trace.monitor_disk: |
| 501 | ctx.set_font_size(LEGEND_FONT_SIZE) |
| 502 | # Determine set of volumes for which we have |
| 503 | # information and the minimal amount of used disk |
| 504 | # space for each. Currently samples are allowed to |
| 505 | # not have a values for all volumes; drawing could be |
| 506 | # made more efficient if that wasn't the case. |
| 507 | volumes = set() |
| 508 | min_used = {} |
| 509 | for sample in trace.monitor_disk: |
| 510 | for volume, used in sample.records.items(): |
| 511 | volumes.add(volume) |
| 512 | if volume not in min_used or min_used[volume] > used: |
| 513 | min_used[volume] = used |
| 514 | volumes = sorted(list(volumes)) |
| 515 | disk_scale = 0 |
| 516 | for i, volume in enumerate(volumes): |
| 517 | volume_scale = max([sample.records[volume] - min_used[volume] |
| 518 | for sample in trace.monitor_disk |
| 519 | if volume in sample.records]) |
| 520 | # Does not take length of volume name into account, but fixed offset |
| 521 | # works okay in practice. |
| 522 | draw_legend_box(ctx, '%s (max: %u MiB)' % (volume, volume_scale / 1024 / 1024), |
| 523 | VOLUME_COLORS[i % len(VOLUME_COLORS)], |
| 524 | off_x + i * 250, curr_y+20, leg_s) |
| 525 | disk_scale += volume_scale |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 526 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 527 | # render used amount of disk space |
| 528 | chart_rect = (off_x, curr_y+30, w, bar_h) |
| 529 | if clip_visible (clip, chart_rect): |
| 530 | draw_box_ticks (ctx, chart_rect, sec_w) |
| 531 | draw_annotations (ctx, proc_tree, trace.times, chart_rect) |
| 532 | for i in range(len(volumes), 0, -1): |
| 533 | draw_chart (ctx, VOLUME_COLORS[(i - 1) % len(VOLUME_COLORS)], True, chart_rect, \ |
| 534 | [(sample.time, |
| 535 | # Sum up used space of all volumes including the current one |
| 536 | # so that the graphs appear as stacked on top of each other. |
| 537 | functools.reduce(lambda x,y: x+y, |
| 538 | [sample.records[volume] - min_used[volume] |
| 539 | for volume in volumes[0:i] |
| 540 | if volume in sample.records], |
| 541 | 0)) |
| 542 | for sample in trace.monitor_disk], \ |
| 543 | proc_tree, [0, disk_scale]) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 544 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 545 | curr_y = curr_y + 30 + bar_h |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 546 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 547 | # render mem usage |
| 548 | chart_rect = (off_x, curr_y+30, w, meminfo_bar_h) |
| 549 | mem_stats = trace.mem_stats |
| 550 | if mem_stats and clip_visible (clip, chart_rect): |
| 551 | mem_scale = max(sample.buffers for sample in mem_stats) |
| 552 | draw_legend_box(ctx, "Mem cached (scale: %u MiB)" % (float(mem_scale) / 1024), MEM_CACHED_COLOR, off_x, curr_y+20, leg_s) |
| 553 | draw_legend_box(ctx, "Used", MEM_USED_COLOR, off_x + 240, curr_y+20, leg_s) |
| 554 | draw_legend_box(ctx, "Buffers", MEM_BUFFERS_COLOR, off_x + 360, curr_y+20, leg_s) |
| 555 | draw_legend_line(ctx, "Swap (scale: %u MiB)" % max([(sample.swap)/1024 for sample in mem_stats]), \ |
| 556 | MEM_SWAP_COLOR, off_x + 480, curr_y+20, leg_s) |
| 557 | draw_box_ticks(ctx, chart_rect, sec_w) |
| 558 | draw_annotations(ctx, proc_tree, trace.times, chart_rect) |
| 559 | draw_chart(ctx, MEM_BUFFERS_COLOR, True, chart_rect, \ |
| 560 | [(sample.time, sample.buffers) for sample in trace.mem_stats], \ |
| 561 | proc_tree, [0, mem_scale]) |
| 562 | draw_chart(ctx, MEM_USED_COLOR, True, chart_rect, \ |
| 563 | [(sample.time, sample.used) for sample in mem_stats], \ |
| 564 | proc_tree, [0, mem_scale]) |
| 565 | draw_chart(ctx, MEM_CACHED_COLOR, True, chart_rect, \ |
| 566 | [(sample.time, sample.cached) for sample in mem_stats], \ |
| 567 | proc_tree, [0, mem_scale]) |
| 568 | draw_chart(ctx, MEM_SWAP_COLOR, False, chart_rect, \ |
| 569 | [(sample.time, float(sample.swap)) for sample in mem_stats], \ |
| 570 | proc_tree, None) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 571 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 572 | curr_y = curr_y + meminfo_bar_h |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 573 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 574 | return curr_y |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 575 | |
| 576 | def render_processes_chart(ctx, options, trace, curr_y, w, h, sec_w): |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 577 | chart_rect = [off_x, curr_y+header_h, w, h - curr_y - 1 * off_y - header_h ] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 578 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 579 | draw_legend_box (ctx, "Configure", \ |
| 580 | TASK_COLOR_CONFIGURE, off_x , curr_y + 45, leg_s) |
| 581 | draw_legend_box (ctx, "Compile", \ |
| 582 | TASK_COLOR_COMPILE, off_x+120, curr_y + 45, leg_s) |
| 583 | draw_legend_box (ctx, "Install", \ |
| 584 | TASK_COLOR_INSTALL, off_x+240, curr_y + 45, leg_s) |
| 585 | draw_legend_box (ctx, "Populate Sysroot", \ |
| 586 | TASK_COLOR_SYSROOT, off_x+360, curr_y + 45, leg_s) |
| 587 | draw_legend_box (ctx, "Package", \ |
| 588 | TASK_COLOR_PACKAGE, off_x+480, curr_y + 45, leg_s) |
| 589 | draw_legend_box (ctx, "Package Write", \ |
| 590 | TASK_COLOR_PACKAGE_WRITE, off_x+600, curr_y + 45, leg_s) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 591 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 592 | ctx.set_font_size(PROC_TEXT_FONT_SIZE) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 593 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 594 | draw_box_ticks(ctx, chart_rect, sec_w) |
| 595 | draw_sec_labels(ctx, options, chart_rect, sec_w, 30) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 596 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 597 | y = curr_y+header_h |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 598 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 599 | offset = trace.min or min(trace.start.keys()) |
| 600 | for start in sorted(trace.start.keys()): |
| 601 | for process in sorted(trace.start[start]): |
| 602 | if not options.app_options.show_all and \ |
| 603 | trace.processes[process][1] - start < options.app_options.mintime: |
| 604 | continue |
| 605 | task = process.split(":")[1] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 606 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 607 | #print(process) |
| 608 | #print(trace.processes[process][1]) |
| 609 | #print(s) |
| 610 | |
| 611 | x = chart_rect[0] + (start - offset) * sec_w |
| 612 | w = ((trace.processes[process][1] - start) * sec_w) |
| 613 | |
| 614 | #print("proc at %s %s %s %s" % (x, y, w, proc_h)) |
| 615 | col = None |
| 616 | if task == "do_compile": |
| 617 | col = TASK_COLOR_COMPILE |
| 618 | elif task == "do_configure": |
| 619 | col = TASK_COLOR_CONFIGURE |
| 620 | elif task == "do_install": |
| 621 | col = TASK_COLOR_INSTALL |
| 622 | elif task == "do_populate_sysroot": |
| 623 | col = TASK_COLOR_SYSROOT |
| 624 | elif task == "do_package": |
| 625 | col = TASK_COLOR_PACKAGE |
| 626 | elif task == "do_package_write_rpm" or \ |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 627 | task == "do_package_write_deb" or \ |
| 628 | task == "do_package_write_ipk": |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 629 | col = TASK_COLOR_PACKAGE_WRITE |
| 630 | else: |
| 631 | col = WHITE |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 632 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 633 | if col: |
| 634 | draw_fill_rect(ctx, col, (x, y, w, proc_h)) |
| 635 | draw_rect(ctx, PROC_BORDER_COLOR, (x, y, w, proc_h)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 636 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 637 | draw_label_in_box(ctx, PROC_TEXT_COLOR, process, x, y + proc_h - 4, w, proc_h) |
| 638 | y = y + proc_h |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 639 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 640 | return curr_y |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 641 | |
| 642 | # |
| 643 | # Render the chart. |
| 644 | # |
| 645 | def render(ctx, options, xscale, trace): |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 646 | (w, h) = extents (options, xscale, trace) |
| 647 | global OPTIONS |
| 648 | OPTIONS = options.app_options |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 649 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 650 | # x, y, w, h |
| 651 | clip = ctx.clip_extents() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 652 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 653 | sec_w = int (xscale * sec_w_base) |
| 654 | ctx.set_line_width(1.0) |
| 655 | ctx.select_font_face(FONT_NAME) |
| 656 | draw_fill_rect(ctx, WHITE, (0, 0, max(w, MIN_IMG_W), h)) |
| 657 | w -= 2*off_x |
| 658 | curr_y = off_y; |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 659 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 660 | if options.charts: |
| 661 | curr_y = render_charts (ctx, options, clip, trace, curr_y, w, h, sec_w) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 662 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 663 | curr_y = render_processes_chart (ctx, options, trace, curr_y, w, h, sec_w) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 664 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 665 | return |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 666 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 667 | proc_tree = options.proc_tree (trace) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 668 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 669 | # draw the title and headers |
| 670 | if proc_tree.idle: |
| 671 | duration = proc_tree.idle |
| 672 | else: |
| 673 | duration = proc_tree.duration |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 674 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 675 | if not options.kernel_only: |
| 676 | curr_y = draw_header (ctx, trace.headers, duration) |
| 677 | else: |
| 678 | curr_y = off_y; |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 679 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 680 | # draw process boxes |
| 681 | proc_height = h |
| 682 | if proc_tree.taskstats and options.cumulative: |
| 683 | proc_height -= CUML_HEIGHT |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 684 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 685 | draw_process_bar_chart(ctx, clip, options, proc_tree, trace.times, |
| 686 | curr_y, w, proc_height, sec_w) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 687 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 688 | curr_y = proc_height |
| 689 | ctx.set_font_size(SIG_FONT_SIZE) |
| 690 | draw_text(ctx, SIGNATURE, SIG_COLOR, off_x + 5, proc_height - 8) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 691 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 692 | # draw a cumulative CPU-time-per-process graph |
| 693 | if proc_tree.taskstats and options.cumulative: |
| 694 | cuml_rect = (off_x, curr_y + off_y, w, CUML_HEIGHT/2 - off_y * 2) |
| 695 | if clip_visible (clip, cuml_rect): |
| 696 | draw_cuml_graph(ctx, proc_tree, cuml_rect, duration, sec_w, STAT_TYPE_CPU) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 697 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 698 | # draw a cumulative I/O-time-per-process graph |
| 699 | if proc_tree.taskstats and options.cumulative: |
| 700 | cuml_rect = (off_x, curr_y + off_y * 100, w, CUML_HEIGHT/2 - off_y * 2) |
| 701 | if clip_visible (clip, cuml_rect): |
| 702 | draw_cuml_graph(ctx, proc_tree, cuml_rect, duration, sec_w, STAT_TYPE_IO) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 703 | |
| 704 | def draw_process_bar_chart(ctx, clip, options, proc_tree, times, curr_y, w, h, sec_w): |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 705 | header_size = 0 |
| 706 | if not options.kernel_only: |
| 707 | draw_legend_box (ctx, "Running (%cpu)", |
| 708 | PROC_COLOR_R, off_x , curr_y + 45, leg_s) |
| 709 | draw_legend_box (ctx, "Unint.sleep (I/O)", |
| 710 | PROC_COLOR_D, off_x+120, curr_y + 45, leg_s) |
| 711 | draw_legend_box (ctx, "Sleeping", |
| 712 | PROC_COLOR_S, off_x+240, curr_y + 45, leg_s) |
| 713 | draw_legend_box (ctx, "Zombie", |
| 714 | PROC_COLOR_Z, off_x+360, curr_y + 45, leg_s) |
| 715 | header_size = 45 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 716 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 717 | chart_rect = [off_x, curr_y + header_size + 15, |
| 718 | w, h - 2 * off_y - (curr_y + header_size + 15) + proc_h] |
| 719 | ctx.set_font_size (PROC_TEXT_FONT_SIZE) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 720 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 721 | draw_box_ticks (ctx, chart_rect, sec_w) |
| 722 | if sec_w > 100: |
| 723 | nsec = 1 |
| 724 | else: |
| 725 | nsec = 5 |
| 726 | draw_sec_labels (ctx, options, chart_rect, sec_w, nsec) |
| 727 | draw_annotations (ctx, proc_tree, times, chart_rect) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 728 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 729 | y = curr_y + 60 |
| 730 | for root in proc_tree.process_tree: |
| 731 | draw_processes_recursively(ctx, root, proc_tree, y, proc_h, chart_rect, clip) |
| 732 | y = y + proc_h * proc_tree.num_nodes([root]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 733 | |
| 734 | |
| 735 | def draw_header (ctx, headers, duration): |
| 736 | toshow = [ |
| 737 | ('system.uname', 'uname', lambda s: s), |
| 738 | ('system.release', 'release', lambda s: s), |
| 739 | ('system.cpu', 'CPU', lambda s: re.sub('model name\s*:\s*', '', s, 1)), |
| 740 | ('system.kernel.options', 'kernel options', lambda s: s), |
| 741 | ] |
| 742 | |
| 743 | header_y = ctx.font_extents()[2] + 10 |
| 744 | ctx.set_font_size(TITLE_FONT_SIZE) |
| 745 | draw_text(ctx, headers['title'], TEXT_COLOR, off_x, header_y) |
| 746 | ctx.set_font_size(TEXT_FONT_SIZE) |
| 747 | |
| 748 | for (headerkey, headertitle, mangle) in toshow: |
| 749 | header_y += ctx.font_extents()[2] |
| 750 | if headerkey in headers: |
| 751 | value = headers.get(headerkey) |
| 752 | else: |
| 753 | value = "" |
| 754 | txt = headertitle + ': ' + mangle(value) |
| 755 | draw_text(ctx, txt, TEXT_COLOR, off_x, header_y) |
| 756 | |
| 757 | dur = duration / 100.0 |
| 758 | txt = 'time : %02d:%05.2f' % (math.floor(dur/60), dur - 60 * math.floor(dur/60)) |
| 759 | if headers.get('system.maxpid') is not None: |
| 760 | txt = txt + ' max pid: %s' % (headers.get('system.maxpid')) |
| 761 | |
| 762 | header_y += ctx.font_extents()[2] |
| 763 | draw_text (ctx, txt, TEXT_COLOR, off_x, header_y) |
| 764 | |
| 765 | return header_y |
| 766 | |
| 767 | def draw_processes_recursively(ctx, proc, proc_tree, y, proc_h, rect, clip) : |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 768 | x = rect[0] + ((proc.start_time - proc_tree.start_time) * rect[2] / proc_tree.duration) |
| 769 | w = ((proc.duration) * rect[2] / proc_tree.duration) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 770 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 771 | draw_process_activity_colors(ctx, proc, proc_tree, x, y, w, proc_h, rect, clip) |
| 772 | draw_rect(ctx, PROC_BORDER_COLOR, (x, y, w, proc_h)) |
| 773 | ipid = int(proc.pid) |
| 774 | if not OPTIONS.show_all: |
| 775 | cmdString = proc.cmd |
| 776 | else: |
| 777 | cmdString = '' |
| 778 | if (OPTIONS.show_pid or OPTIONS.show_all) and ipid is not 0: |
| 779 | cmdString = cmdString + " [" + str(ipid // 1000) + "]" |
| 780 | if OPTIONS.show_all: |
| 781 | if proc.args: |
| 782 | cmdString = cmdString + " '" + "' '".join(proc.args) + "'" |
| 783 | else: |
| 784 | cmdString = cmdString + " " + proc.exe |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 785 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 786 | draw_label_in_box(ctx, PROC_TEXT_COLOR, cmdString, x, y + proc_h - 4, w, rect[0] + rect[2]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 787 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 788 | next_y = y + proc_h |
| 789 | for child in proc.child_list: |
| 790 | if next_y > clip[1] + clip[3]: |
| 791 | break |
| 792 | child_x, child_y = draw_processes_recursively(ctx, child, proc_tree, next_y, proc_h, rect, clip) |
| 793 | draw_process_connecting_lines(ctx, x, y, child_x, child_y, proc_h) |
| 794 | next_y = next_y + proc_h * proc_tree.num_nodes([child]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 795 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 796 | return x, y |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 797 | |
| 798 | |
| 799 | def draw_process_activity_colors(ctx, proc, proc_tree, x, y, w, proc_h, rect, clip): |
| 800 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 801 | if y > clip[1] + clip[3] or y + proc_h + 2 < clip[1]: |
| 802 | return |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 803 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 804 | draw_fill_rect(ctx, PROC_COLOR_S, (x, y, w, proc_h)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 805 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 806 | last_tx = -1 |
| 807 | for sample in proc.samples : |
| 808 | tx = rect[0] + round(((sample.time - proc_tree.start_time) * rect[2] / proc_tree.duration)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 809 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 810 | # samples are sorted chronologically |
| 811 | if tx < clip[0]: |
| 812 | continue |
| 813 | if tx > clip[0] + clip[2]: |
| 814 | break |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 815 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 816 | tw = round(proc_tree.sample_period * rect[2] / float(proc_tree.duration)) |
| 817 | if last_tx != -1 and abs(last_tx - tx) <= tw: |
| 818 | tw -= last_tx - tx |
| 819 | tx = last_tx |
| 820 | tw = max (tw, 1) # nice to see at least something |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 821 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 822 | last_tx = tx + tw |
| 823 | state = get_proc_state( sample.state ) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 824 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 825 | color = STATE_COLORS[state] |
| 826 | if state == STATE_RUNNING: |
| 827 | alpha = min (sample.cpu_sample.user + sample.cpu_sample.sys, 1.0) |
| 828 | color = tuple(list(PROC_COLOR_R[0:3]) + [alpha]) |
| 829 | # print "render time %d [ tx %d tw %d ], sample state %s color %s alpha %g" % (sample.time, tx, tw, state, color, alpha) |
| 830 | elif state == STATE_SLEEPING: |
| 831 | continue |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 832 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 833 | draw_fill_rect(ctx, color, (tx, y, tw, proc_h)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 834 | |
| 835 | def draw_process_connecting_lines(ctx, px, py, x, y, proc_h): |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 836 | ctx.set_source_rgba(*DEP_COLOR) |
| 837 | ctx.set_dash([2, 2]) |
| 838 | if abs(px - x) < 3: |
| 839 | dep_off_x = 3 |
| 840 | dep_off_y = proc_h / 4 |
| 841 | ctx.move_to(x, y + proc_h / 2) |
| 842 | ctx.line_to(px - dep_off_x, y + proc_h / 2) |
| 843 | ctx.line_to(px - dep_off_x, py - dep_off_y) |
| 844 | ctx.line_to(px, py - dep_off_y) |
| 845 | else: |
| 846 | ctx.move_to(x, y + proc_h / 2) |
| 847 | ctx.line_to(px, y + proc_h / 2) |
| 848 | ctx.line_to(px, py) |
| 849 | ctx.stroke() |
| 850 | ctx.set_dash([]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 851 | |
| 852 | # elide the bootchart collector - it is quite distorting |
| 853 | def elide_bootchart(proc): |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 854 | return proc.cmd == 'bootchartd' or proc.cmd == 'bootchart-colle' |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 855 | |
| 856 | class CumlSample: |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 857 | def __init__(self, proc): |
| 858 | self.cmd = proc.cmd |
| 859 | self.samples = [] |
| 860 | self.merge_samples (proc) |
| 861 | self.color = None |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 862 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 863 | def merge_samples(self, proc): |
| 864 | self.samples.extend (proc.samples) |
| 865 | self.samples.sort (key = lambda p: p.time) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 866 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 867 | def next(self): |
| 868 | global palette_idx |
| 869 | palette_idx += HSV_STEP |
| 870 | return palette_idx |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 871 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 872 | def get_color(self): |
| 873 | if self.color is None: |
| 874 | i = self.next() % HSV_MAX_MOD |
| 875 | h = 0.0 |
| 876 | if i is not 0: |
| 877 | h = (1.0 * i) / HSV_MAX_MOD |
| 878 | s = 0.5 |
| 879 | v = 1.0 |
| 880 | c = colorsys.hsv_to_rgb (h, s, v) |
| 881 | self.color = (c[0], c[1], c[2], 1.0) |
| 882 | return self.color |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 883 | |
| 884 | |
| 885 | def draw_cuml_graph(ctx, proc_tree, chart_bounds, duration, sec_w, stat_type): |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 886 | global palette_idx |
| 887 | palette_idx = 0 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 888 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 889 | time_hash = {} |
| 890 | total_time = 0.0 |
| 891 | m_proc_list = {} |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 892 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 893 | if stat_type is STAT_TYPE_CPU: |
| 894 | sample_value = 'cpu' |
| 895 | else: |
| 896 | sample_value = 'io' |
| 897 | for proc in proc_tree.process_list: |
| 898 | if elide_bootchart(proc): |
| 899 | continue |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 900 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 901 | for sample in proc.samples: |
| 902 | total_time += getattr(sample.cpu_sample, sample_value) |
| 903 | if not sample.time in time_hash: |
| 904 | time_hash[sample.time] = 1 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 905 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 906 | # merge pids with the same cmd |
| 907 | if not proc.cmd in m_proc_list: |
| 908 | m_proc_list[proc.cmd] = CumlSample (proc) |
| 909 | continue |
| 910 | s = m_proc_list[proc.cmd] |
| 911 | s.merge_samples (proc) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 912 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 913 | # all the sample times |
| 914 | times = sorted(time_hash) |
| 915 | if len (times) < 2: |
| 916 | print("degenerate boot chart") |
| 917 | return |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 918 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 919 | pix_per_ns = chart_bounds[3] / total_time |
| 920 | # print "total time: %g pix-per-ns %g" % (total_time, pix_per_ns) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 921 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 922 | # FIXME: we have duplicates in the process list too [!] - why !? |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 923 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 924 | # Render bottom up, left to right |
| 925 | below = {} |
| 926 | for time in times: |
| 927 | below[time] = chart_bounds[1] + chart_bounds[3] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 928 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 929 | # same colors each time we render |
| 930 | random.seed (0) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 931 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 932 | ctx.set_line_width(1) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 933 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 934 | legends = [] |
| 935 | labels = [] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 936 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 937 | # render each pid in order |
| 938 | for cs in m_proc_list.values(): |
| 939 | row = {} |
| 940 | cuml = 0.0 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 941 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 942 | # print "pid : %s -> %g samples %d" % (proc.cmd, cuml, len (cs.samples)) |
| 943 | for sample in cs.samples: |
| 944 | cuml += getattr(sample.cpu_sample, sample_value) |
| 945 | row[sample.time] = cuml |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 946 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 947 | process_total_time = cuml |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 948 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 949 | # hide really tiny processes |
| 950 | if cuml * pix_per_ns <= 2: |
| 951 | continue |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 952 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 953 | last_time = times[0] |
| 954 | y = last_below = below[last_time] |
| 955 | last_cuml = cuml = 0.0 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 956 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 957 | ctx.set_source_rgba(*cs.get_color()) |
| 958 | for time in times: |
| 959 | render_seg = False |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 960 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 961 | # did the underlying trend increase ? |
| 962 | if below[time] != last_below: |
| 963 | last_below = below[last_time] |
| 964 | last_cuml = cuml |
| 965 | render_seg = True |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 966 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 967 | # did we move up a pixel increase ? |
| 968 | if time in row: |
| 969 | nc = round (row[time] * pix_per_ns) |
| 970 | if nc != cuml: |
| 971 | last_cuml = cuml |
| 972 | cuml = nc |
| 973 | render_seg = True |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 974 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 975 | # if last_cuml > cuml: |
| 976 | # assert fail ... - un-sorted process samples |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 977 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 978 | # draw the trailing rectangle from the last time to |
| 979 | # before now, at the height of the last segment. |
| 980 | if render_seg: |
| 981 | w = math.ceil ((time - last_time) * chart_bounds[2] / proc_tree.duration) + 1 |
| 982 | x = chart_bounds[0] + round((last_time - proc_tree.start_time) * chart_bounds[2] / proc_tree.duration) |
| 983 | ctx.rectangle (x, below[last_time] - last_cuml, w, last_cuml) |
| 984 | ctx.fill() |
| 985 | # ctx.stroke() |
| 986 | last_time = time |
| 987 | y = below [time] - cuml |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 988 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 989 | row[time] = y |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 990 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 991 | # render the last segment |
| 992 | x = chart_bounds[0] + round((last_time - proc_tree.start_time) * chart_bounds[2] / proc_tree.duration) |
| 993 | y = below[last_time] - cuml |
| 994 | ctx.rectangle (x, y, chart_bounds[2] - x, cuml) |
| 995 | ctx.fill() |
| 996 | # ctx.stroke() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 997 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 998 | # render legend if it will fit |
| 999 | if cuml > 8: |
| 1000 | label = cs.cmd |
| 1001 | extnts = ctx.text_extents(label) |
| 1002 | label_w = extnts[2] |
| 1003 | label_h = extnts[3] |
| 1004 | # print "Text extents %g by %g" % (label_w, label_h) |
| 1005 | labels.append((label, |
| 1006 | chart_bounds[0] + chart_bounds[2] - label_w - off_x * 2, |
| 1007 | y + (cuml + label_h) / 2)) |
| 1008 | if cs in legends: |
| 1009 | print("ARGH - duplicate process in list !") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1010 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1011 | legends.append ((cs, process_total_time)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1012 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1013 | below = row |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1014 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1015 | # render grid-lines over the top |
| 1016 | draw_box_ticks(ctx, chart_bounds, sec_w) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1017 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1018 | # render labels |
| 1019 | for l in labels: |
| 1020 | draw_text(ctx, l[0], TEXT_COLOR, l[1], l[2]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1021 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1022 | # Render legends |
| 1023 | font_height = 20 |
| 1024 | label_width = 300 |
| 1025 | LEGENDS_PER_COL = 15 |
| 1026 | LEGENDS_TOTAL = 45 |
| 1027 | ctx.set_font_size (TITLE_FONT_SIZE) |
| 1028 | dur_secs = duration / 100 |
| 1029 | cpu_secs = total_time / 1000000000 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1030 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1031 | # misleading - with multiple CPUs ... |
| 1032 | # idle = ((dur_secs - cpu_secs) / dur_secs) * 100.0 |
| 1033 | if stat_type is STAT_TYPE_CPU: |
| 1034 | label = "Cumulative CPU usage, by process; total CPU: " \ |
| 1035 | " %.5g(s) time: %.3g(s)" % (cpu_secs, dur_secs) |
| 1036 | else: |
| 1037 | label = "Cumulative I/O usage, by process; total I/O: " \ |
| 1038 | " %.5g(s) time: %.3g(s)" % (cpu_secs, dur_secs) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1039 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1040 | draw_text(ctx, label, TEXT_COLOR, chart_bounds[0] + off_x, |
| 1041 | chart_bounds[1] + font_height) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1042 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1043 | i = 0 |
| 1044 | legends = sorted(legends, key=itemgetter(1), reverse=True) |
| 1045 | ctx.set_font_size(TEXT_FONT_SIZE) |
| 1046 | for t in legends: |
| 1047 | cs = t[0] |
| 1048 | time = t[1] |
| 1049 | x = chart_bounds[0] + off_x + int (i/LEGENDS_PER_COL) * label_width |
| 1050 | y = chart_bounds[1] + font_height * ((i % LEGENDS_PER_COL) + 2) |
| 1051 | str = "%s - %.0f(ms) (%2.2f%%)" % (cs.cmd, time/1000000, (time/total_time) * 100.0) |
| 1052 | draw_legend_box(ctx, str, cs.color, x, y, leg_s) |
| 1053 | i = i + 1 |
| 1054 | if i >= LEGENDS_TOTAL: |
| 1055 | break |