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