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