summaryrefslogtreecommitdiffstats
path: root/scripts/pybootchartgui/pybootchartgui
diff options
context:
space:
mode:
authorPeter Kjellerstedt <peter.kjellerstedt@axis.com>2013-11-15 18:08:50 +0100
committerRichard Purdie <richard.purdie@linuxfoundation.org>2013-11-18 12:44:06 +0000
commitd0e513d4371789d59bcb902497ca7c537a94c1f0 (patch)
tree1542d41042ac935aaaa9f6735170ffd8f9044c87 /scripts/pybootchartgui/pybootchartgui
parenta52c9b9a76371ac3f29b0355fea08b788eb2dd10 (diff)
downloadpoky-d0e513d4371789d59bcb902497ca7c537a94c1f0.tar.gz
pybootchartgui: Import pybootchartgui 0.14.5
This update the pybootchartgui code to the latest release from its new location at "https://github.com/mmeeks/bootchart". This only imports the relevant parts, and not all of bootchart2. (From OE-Core rev: 6f1568e54a7808b2ab568618fc5bb244249579f1) Signed-off-by: Peter Kjellerstedt <peter.kjellerstedt@axis.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'scripts/pybootchartgui/pybootchartgui')
-rw-r--r--scripts/pybootchartgui/pybootchartgui/batch.py53
-rw-r--r--scripts/pybootchartgui/pybootchartgui/draw.py710
-rw-r--r--scripts/pybootchartgui/pybootchartgui/gui.py594
l---------[-rw-r--r--]scripts/pybootchartgui/pybootchartgui/main.py79
-rw-r--r--scripts/pybootchartgui/pybootchartgui/main.py.in171
-rw-r--r--scripts/pybootchartgui/pybootchartgui/parsing.py791
-rw-r--r--scripts/pybootchartgui/pybootchartgui/process_tree.py126
-rw-r--r--scripts/pybootchartgui/pybootchartgui/samples.py226
-rw-r--r--scripts/pybootchartgui/pybootchartgui/tests/parser_test.py82
-rw-r--r--scripts/pybootchartgui/pybootchartgui/tests/process_tree_test.py30
10 files changed, 2036 insertions, 826 deletions
diff --git a/scripts/pybootchartgui/pybootchartgui/batch.py b/scripts/pybootchartgui/pybootchartgui/batch.py
index 3c1dbf8416..05c714e95e 100644
--- a/scripts/pybootchartgui/pybootchartgui/batch.py
+++ b/scripts/pybootchartgui/pybootchartgui/batch.py
@@ -1,23 +1,46 @@
1import cairo 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.
2 12
3import draw 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/>.
4 15
5def render(res, format, filename): 16import cairo
17from . import draw
18from .draw import RenderOptions
19
20def render(writer, trace, app_options, filename):
6 handlers = { 21 handlers = {
7 "png": (lambda w,h: cairo.ImageSurface(cairo.FORMAT_ARGB32,w,h), lambda sfc: sfc.write_to_png(filename)), 22 "png": (lambda w, h: cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h), \
8 "pdf": (lambda w,h: cairo.PDFSurface(filename, w, h), lambda sfc: 0), 23 lambda sfc: sfc.write_to_png(filename)),
9 "svg": (lambda w,h: cairo.SVGSurface(filename, w, h), lambda sfc: 0) 24 "pdf": (lambda w, h: cairo.PDFSurface(filename, w, h), lambda sfc: 0),
25 "svg": (lambda w, h: cairo.SVGSurface(filename, w, h), lambda sfc: 0)
10 } 26 }
11 27
12 if not(handlers.has_key(format)): 28 if app_options.format is None:
13 print "Unknown format '%s'." % format 29 fmt = filename.rsplit('.', 1)[1]
30 else:
31 fmt = app_options.format
32
33 if not (fmt in handlers):
34 writer.error ("Unknown format '%s'." % fmt)
14 return 10 35 return 10
15 36
16 make_surface, write_surface = handlers[format] 37 make_surface, write_surface = handlers[fmt]
17 w,h = draw.extents(res) 38 options = RenderOptions (app_options)
18 w = max(w, draw.MIN_IMG_W) 39 (w, h) = draw.extents (options, 1.0, trace)
19 surface = make_surface(w,h) 40 w = max (w, draw.MIN_IMG_W)
20 ctx = cairo.Context(surface) 41 surface = make_surface (w, h)
21 draw.render(ctx, res) 42 ctx = cairo.Context (surface)
22 write_surface(surface) 43 draw.render (ctx, options, 1.0, trace)
44 write_surface (surface)
45 writer.status ("bootchart written to '%s'" % filename)
23 46
diff --git a/scripts/pybootchartgui/pybootchartgui/draw.py b/scripts/pybootchartgui/pybootchartgui/draw.py
index 1b872de75e..c3492c7cb9 100644
--- a/scripts/pybootchartgui/pybootchartgui/draw.py
+++ b/scripts/pybootchartgui/pybootchartgui/draw.py
@@ -1,6 +1,40 @@
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
1import cairo 17import cairo
2import math 18import math
3import re 19import re
20import random
21import colorsys
22from operator import itemgetter
23
24class RenderOptions:
25
26 def __init__(self, app_options):
27 # should we render a cumulative CPU time chart
28 self.cumulative = True
29 self.charts = True
30 self.kernel_only = False
31 self.app_options = app_options
32
33 def proc_tree (self, trace):
34 if self.kernel_only:
35 return trace.kernel_tree
36 else:
37 return trace.proc_tree
4 38
5# Process tree background color. 39# Process tree background color.
6BACK_COLOR = (1.0, 1.0, 1.0, 1.0) 40BACK_COLOR = (1.0, 1.0, 1.0, 1.0)
@@ -12,11 +46,13 @@ BORDER_COLOR = (0.63, 0.63, 0.63, 1.0)
12TICK_COLOR = (0.92, 0.92, 0.92, 1.0) 46TICK_COLOR = (0.92, 0.92, 0.92, 1.0)
13# 5-second tick line color. 47# 5-second tick line color.
14TICK_COLOR_BOLD = (0.86, 0.86, 0.86, 1.0) 48TICK_COLOR_BOLD = (0.86, 0.86, 0.86, 1.0)
49# Annotation colour
50ANNOTATION_COLOR = (0.63, 0.0, 0.0, 0.5)
15# Text color. 51# Text color.
16TEXT_COLOR = (0.0, 0.0, 0.0, 1.0) 52TEXT_COLOR = (0.0, 0.0, 0.0, 1.0)
17 53
18# Font family 54# Font family
19FONT_NAME = "Bitstream Vera Sans" 55FONT_NAME = "Bitstream Vera Sans"
20# Title text font. 56# Title text font.
21TITLE_FONT_SIZE = 18 57TITLE_FONT_SIZE = 18
22# Default text font. 58# Default text font.
@@ -25,7 +61,7 @@ TEXT_FONT_SIZE = 12
25AXIS_FONT_SIZE = 11 61AXIS_FONT_SIZE = 11
26# Legend font. 62# Legend font.
27LEGEND_FONT_SIZE = 12 63LEGEND_FONT_SIZE = 12
28 64
29# CPU load chart color. 65# CPU load chart color.
30CPU_COLOR = (0.40, 0.55, 0.70, 1.0) 66CPU_COLOR = (0.40, 0.55, 0.70, 1.0)
31# IO wait chart color. 67# IO wait chart color.
@@ -34,11 +70,19 @@ IO_COLOR = (0.76, 0.48, 0.48, 0.5)
34DISK_TPUT_COLOR = (0.20, 0.71, 0.20, 1.0) 70DISK_TPUT_COLOR = (0.20, 0.71, 0.20, 1.0)
35# CPU load chart color. 71# CPU load chart color.
36FILE_OPEN_COLOR = (0.20, 0.71, 0.71, 1.0) 72FILE_OPEN_COLOR = (0.20, 0.71, 0.71, 1.0)
37 73# Mem cached color
74MEM_CACHED_COLOR = CPU_COLOR
75# Mem used color
76MEM_USED_COLOR = IO_COLOR
77# Buffers color
78MEM_BUFFERS_COLOR = (0.4, 0.4, 0.4, 0.3)
79# Swap color
80MEM_SWAP_COLOR = DISK_TPUT_COLOR
81
38# Process border color. 82# Process border color.
39PROC_BORDER_COLOR = (0.71, 0.71, 0.71, 1.0) 83PROC_BORDER_COLOR = (0.71, 0.71, 0.71, 1.0)
40 84# Waiting process color.
41PROC_COLOR_D = (0.76, 0.48, 0.48, 0.125) 85PROC_COLOR_D = (0.76, 0.48, 0.48, 0.5)
42# Running process color. 86# Running process color.
43PROC_COLOR_R = CPU_COLOR 87PROC_COLOR_R = CPU_COLOR
44# Sleeping process color. 88# Sleeping process color.
@@ -62,8 +106,8 @@ SIG_COLOR = (0.0, 0.0, 0.0, 0.3125)
62# Signature font. 106# Signature font.
63SIG_FONT_SIZE = 14 107SIG_FONT_SIZE = 14
64# Signature text. 108# Signature text.
65SIGNATURE = "http://code.google.com/p/pybootchartgui" 109SIGNATURE = "http://github.com/mmeeks/bootchart"
66 110
67# Process dependency line color. 111# Process dependency line color.
68DEP_COLOR = (0.75, 0.75, 0.75, 1.0) 112DEP_COLOR = (0.75, 0.75, 0.75, 1.0)
69# Process dependency line stroke. 113# Process dependency line stroke.
@@ -72,6 +116,10 @@ DEP_STROKE = 1.0
72# Process description date format. 116# Process description date format.
73DESC_TIME_FORMAT = "mm:ss.SSS" 117DESC_TIME_FORMAT = "mm:ss.SSS"
74 118
119# Cumulative coloring bits
120HSV_MAX_MOD = 31
121HSV_STEP = 7
122
75# Configure task color 123# Configure task color
76TASK_COLOR_CONFIGURE = (1.0, 1.0, 0.00, 1.0) 124TASK_COLOR_CONFIGURE = (1.0, 1.0, 0.00, 1.0)
77# Compile task color. 125# Compile task color.
@@ -91,66 +139,67 @@ STATE_WAITING = 3
91STATE_STOPPED = 4 139STATE_STOPPED = 4
92STATE_ZOMBIE = 5 140STATE_ZOMBIE = 5
93 141
94STATE_COLORS = [(0,0,0,0), PROC_COLOR_R, PROC_COLOR_S, PROC_COLOR_D, PROC_COLOR_T, PROC_COLOR_Z, PROC_COLOR_X, PROC_COLOR_W] 142STATE_COLORS = [(0, 0, 0, 0), PROC_COLOR_R, PROC_COLOR_S, PROC_COLOR_D, \
143 PROC_COLOR_T, PROC_COLOR_Z, PROC_COLOR_X, PROC_COLOR_W]
144
145# CumulativeStats Types
146STAT_TYPE_CPU = 0
147STAT_TYPE_IO = 1
95 148
96# Convert ps process state to an int 149# Convert ps process state to an int
97def get_proc_state(flag): 150def get_proc_state(flag):
98 return "RSDTZXW".index(flag) + 1 151 return "RSDTZXW".find(flag) + 1
99
100 152
101def draw_text(ctx, text, color, x, y): 153def draw_text(ctx, text, color, x, y):
102 ctx.set_source_rgba(*color) 154 ctx.set_source_rgba(*color)
103 ctx.move_to(x, y) 155 ctx.move_to(x, y)
104 ctx.show_text(text) 156 ctx.show_text(text)
105 157
106
107def draw_fill_rect(ctx, color, rect): 158def draw_fill_rect(ctx, color, rect):
108 ctx.set_source_rgba(*color) 159 ctx.set_source_rgba(*color)
109 ctx.rectangle(*rect) 160 ctx.rectangle(*rect)
110 ctx.fill() 161 ctx.fill()
111
112 162
113def draw_rect(ctx, color, rect): 163def draw_rect(ctx, color, rect):
114 ctx.set_source_rgba(*color) 164 ctx.set_source_rgba(*color)
115 ctx.rectangle(*rect) 165 ctx.rectangle(*rect)
116 ctx.stroke() 166 ctx.stroke()
117 167
118
119def draw_legend_box(ctx, label, fill_color, x, y, s): 168def draw_legend_box(ctx, label, fill_color, x, y, s):
120 draw_fill_rect(ctx, fill_color, (x, y - s, s, s)) 169 draw_fill_rect(ctx, fill_color, (x, y - s, s, s))
121 draw_rect(ctx, PROC_BORDER_COLOR, (x, y - s, s, s)) 170 draw_rect(ctx, PROC_BORDER_COLOR, (x, y - s, s, s))
122 draw_text(ctx, label, TEXT_COLOR, x + s + 5, y) 171 draw_text(ctx, label, TEXT_COLOR, x + s + 5, y)
123 172
124
125def draw_legend_line(ctx, label, fill_color, x, y, s): 173def draw_legend_line(ctx, label, fill_color, x, y, s):
126 draw_fill_rect(ctx, fill_color, (x, y - s/2, s + 1, 3)) 174 draw_fill_rect(ctx, fill_color, (x, y - s/2, s + 1, 3))
127 ctx.arc(x + (s + 1)/2.0, y - (s - 3)/2.0, 2.5, 0, 2.0 * math.pi) 175 ctx.arc(x + (s + 1)/2.0, y - (s - 3)/2.0, 2.5, 0, 2.0 * math.pi)
128 ctx.fill() 176 ctx.fill()
129 draw_text(ctx, label, TEXT_COLOR, x + s + 5, y) 177 draw_text(ctx, label, TEXT_COLOR, x + s + 5, y)
130
131 178
132def draw_label_in_box(ctx, color, label, x, y, w, maxx): 179def draw_label_in_box(ctx, color, label, x, y, w, maxx):
133 label_w = ctx.text_extents(label)[2] 180 label_w = ctx.text_extents(label)[2]
134 label_x = x + w / 2 - label_w / 2 181 label_x = x + w / 2 - label_w / 2
135 if label_w + 10 > w: 182 if label_w + 10 > w:
136 label_x = x + w + 5 183 label_x = x + w + 5
137 if label_x + label_w > maxx: 184 if label_x + label_w > maxx:
138 label_x = x - label_w - 5 185 label_x = x - label_w - 5
139 draw_text(ctx, label, color, label_x, y) 186 draw_text(ctx, label, color, label_x, y)
140 187
141 188def draw_sec_labels(ctx, rect, sec_w, nsecs):
142def draw_5sec_labels(ctx, rect, sec_w): 189 ctx.set_font_size(AXIS_FONT_SIZE)
143 ctx.set_font_size(AXIS_FONT_SIZE) 190 prev_x = 0
144 for i in range(0, rect[2] + 1, sec_w): 191 for i in range(0, rect[2] + 1, sec_w):
145 if ((i / sec_w) % 30 == 0) : 192 if ((i / sec_w) % nsecs == 0) :
146 label = "%ds" % (i / sec_w) 193 label = "%ds" % (i / sec_w)
147 label_w = ctx.text_extents(label)[2] 194 label_w = ctx.text_extents(label)[2]
148 draw_text(ctx, label, TEXT_COLOR, rect[0] + i - label_w/2, rect[1] - 2) 195 x = rect[0] + i - label_w/2
149 196 if x >= prev_x:
197 draw_text(ctx, label, TEXT_COLOR, x, rect[1] - 2)
198 prev_x = x + label_w
150 199
151def draw_box_ticks(ctx, rect, sec_w): 200def draw_box_ticks(ctx, rect, sec_w):
152 draw_rect(ctx, BORDER_COLOR, tuple(rect)) 201 draw_rect(ctx, BORDER_COLOR, tuple(rect))
153 202
154 ctx.set_line_cap(cairo.LINE_CAP_SQUARE) 203 ctx.set_line_cap(cairo.LINE_CAP_SQUARE)
155 204
156 for i in range(sec_w, rect[2] + 1, sec_w): 205 for i in range(sec_w, rect[2] + 1, sec_w):
@@ -164,136 +213,211 @@ def draw_box_ticks(ctx, rect, sec_w):
164 213
165 ctx.set_line_cap(cairo.LINE_CAP_BUTT) 214 ctx.set_line_cap(cairo.LINE_CAP_BUTT)
166 215
167def draw_chart(ctx, color, fill, chart_bounds, data, proc_tree): 216def draw_annotations(ctx, proc_tree, times, rect):
217 ctx.set_line_cap(cairo.LINE_CAP_SQUARE)
218 ctx.set_source_rgba(*ANNOTATION_COLOR)
219 ctx.set_dash([4, 4])
220
221 for time in times:
222 if time is not None:
223 x = ((time - proc_tree.start_time) * rect[2] / proc_tree.duration)
224
225 ctx.move_to(rect[0] + x, rect[1] + 1)
226 ctx.line_to(rect[0] + x, rect[1] + rect[3] - 1)
227 ctx.stroke()
228
229 ctx.set_line_cap(cairo.LINE_CAP_BUTT)
230 ctx.set_dash([])
231
232def draw_chart(ctx, color, fill, chart_bounds, data, proc_tree, data_range):
168 ctx.set_line_width(0.5) 233 ctx.set_line_width(0.5)
169 x_shift = proc_tree.start_time 234 x_shift = proc_tree.start_time
170 x_scale = proc_tree.duration 235
171 236 def transform_point_coords(point, x_base, y_base, \
172 def transform_point_coords(point, x_base, y_base, xscale, yscale, x_trans, y_trans): 237 xscale, yscale, x_trans, y_trans):
173 x = (point[0] - x_base) * xscale + x_trans 238 x = (point[0] - x_base) * xscale + x_trans
174 y = (point[1] - y_base) * -yscale + y_trans + bar_h 239 y = (point[1] - y_base) * -yscale + y_trans + chart_bounds[3]
175 return x, y 240 return x, y
176 241
177 xscale = float(chart_bounds[2]) / max(x for (x,y) in data) 242 max_x = max (x for (x, y) in data)
178 yscale = float(chart_bounds[3]) / max(y for (x,y) in data) 243 max_y = max (y for (x, y) in data)
179 244 # avoid divide by zero
180 first = transform_point_coords(data[0], x_shift, 0, xscale, yscale, chart_bounds[0], chart_bounds[1]) 245 if max_y == 0:
181 last = transform_point_coords(data[-1], x_shift, 0, xscale, yscale, chart_bounds[0], chart_bounds[1]) 246 max_y = 1.0
182 247 xscale = float (chart_bounds[2]) / max_x
248 # If data_range is given, scale the chart so that the value range in
249 # data_range matches the chart bounds exactly.
250 # Otherwise, scale so that the actual data matches the chart bounds.
251 if data_range:
252 yscale = float(chart_bounds[3]) / (data_range[1] - data_range[0])
253 ybase = data_range[0]
254 else:
255 yscale = float(chart_bounds[3]) / max_y
256 ybase = 0
257
258 first = transform_point_coords (data[0], x_shift, ybase, xscale, yscale, \
259 chart_bounds[0], chart_bounds[1])
260 last = transform_point_coords (data[-1], x_shift, ybase, xscale, yscale, \
261 chart_bounds[0], chart_bounds[1])
262
183 ctx.set_source_rgba(*color) 263 ctx.set_source_rgba(*color)
184 ctx.move_to(*first) 264 ctx.move_to(*first)
185 for point in data: 265 for point in data:
186 x, y = transform_point_coords(point, x_shift, 0, xscale, yscale, chart_bounds[0], chart_bounds[1]) 266 x, y = transform_point_coords (point, x_shift, ybase, xscale, yscale, \
267 chart_bounds[0], chart_bounds[1])
187 ctx.line_to(x, y) 268 ctx.line_to(x, y)
188 if fill: 269 if fill:
189 ctx.stroke_preserve() 270 ctx.stroke_preserve()
190 ctx.line_to(last[0], chart_bounds[1]+bar_h) 271 ctx.line_to(last[0], chart_bounds[1]+chart_bounds[3])
191 ctx.line_to(first[0], chart_bounds[1]+bar_h) 272 ctx.line_to(first[0], chart_bounds[1]+chart_bounds[3])
192 ctx.line_to(first[0], first[1]) 273 ctx.line_to(first[0], first[1])
193 ctx.fill() 274 ctx.fill()
194 else: 275 else:
195 ctx.stroke() 276 ctx.stroke()
196 ctx.set_line_width(1.0) 277 ctx.set_line_width(1.0)
197 278
198header_h = 280
199bar_h = 55 279bar_h = 55
280meminfo_bar_h = 2 * bar_h
281header_h = 110 + 2 * (30 + bar_h) + 1 * (30 + meminfo_bar_h)
200# offsets 282# offsets
201off_x, off_y = 10, 10 283off_x, off_y = 10, 10
202sec_w = 1 # the width of a second 284sec_w_base = 1 # the width of a second
203proc_h = 16 # the height of a process 285proc_h = 16 # the height of a process
204leg_s = 10 286leg_s = 10
205MIN_IMG_W = 800 287MIN_IMG_W = 800
288CUML_HEIGHT = 2000 # Increased value to accomodate CPU and I/O Graphs
289OPTIONS = None
206 290
291def extents(options, xscale, trace):
292 start = min(trace.start.keys())
293 end = max(trace.end.keys())
207 294
208def extents(res): 295 w = int ((end - start) * sec_w_base * xscale) + 2*off_x
209 start = min(res.start.keys()) 296 h = proc_h * len(trace.processes) + header_h + 2 * off_y
210 end = max(res.end.keys())
211 297
212 w = ((end - start) * sec_w) + 2*off_x 298 return (w, h)
213 h = proc_h * len(res.processes) + header_h + 2*off_y
214 299
215 return (w,h) 300def clip_visible(clip, rect):
301 xmax = max (clip[0], rect[0])
302 ymax = max (clip[1], rect[1])
303 xmin = min (clip[0] + clip[2], rect[0] + rect[2])
304 ymin = min (clip[1] + clip[3], rect[1] + rect[3])
305 return (xmin > xmax and ymin > ymax)
216 306
217# 307def render_charts(ctx, options, clip, trace, curr_y, w, h, sec_w):
218# Render the chart. 308 proc_tree = options.proc_tree(trace)
219#
220def render(ctx, res):
221 (w, h) = extents(res)
222 309
223 ctx.set_line_width(1.0)
224 ctx.select_font_face(FONT_NAME)
225 draw_fill_rect(ctx, WHITE, (0, 0, max(w, MIN_IMG_W), h))
226 w -= 2*off_x
227 # draw the title and headers
228 #curr_y = draw_header(ctx, headers, off_x, proc_tree.duration)
229 curr_y = 0
230
231 # render bar legend 310 # render bar legend
232 ctx.set_font_size(LEGEND_FONT_SIZE) 311 ctx.set_font_size(LEGEND_FONT_SIZE)
233 312
234 #print "w, h %s %s" % (w, h) 313 draw_legend_box(ctx, "CPU (user+sys)", CPU_COLOR, off_x, curr_y+20, leg_s)
235 314 draw_legend_box(ctx, "I/O (wait)", IO_COLOR, off_x + 120, curr_y+20, leg_s)
236 #draw_legend_box(ctx, "CPU (user+sys)", CPU_COLOR, off_x, curr_y+20, leg_s)
237 #draw_legend_box(ctx, "I/O (wait)", IO_COLOR, off_x + 120, curr_y+20, leg_s)
238 315
239 # render I/O wait 316 # render I/O wait
240 #chart_rect = (off_x, curr_y+30, w, bar_h) 317 chart_rect = (off_x, curr_y+30, w, bar_h)
241 #draw_box_ticks(ctx, chart_rect, sec_w) 318 if clip_visible (clip, chart_rect):
242 #draw_chart(ctx, IO_COLOR, True, chart_rect, [(sample.time, sample.user + sample.sys + sample.io) for sample in cpu_stats], proc_tree) 319 draw_box_ticks (ctx, chart_rect, sec_w)
243 # render CPU load 320 draw_annotations (ctx, proc_tree, trace.times, chart_rect)
244 #draw_chart(ctx, CPU_COLOR, True, chart_rect, [(sample.time, sample.user + sample.sys) for sample in cpu_stats], proc_tree) 321 draw_chart (ctx, IO_COLOR, True, chart_rect, \
245 322 [(sample.time, sample.user + sample.sys + sample.io) for sample in trace.cpu_stats], \
246 #curr_y = curr_y + 30 + bar_h 323 proc_tree, None)
324 # render CPU load
325 draw_chart (ctx, CPU_COLOR, True, chart_rect, \
326 [(sample.time, sample.user + sample.sys) for sample in trace.cpu_stats], \
327 proc_tree, None)
328
329 curr_y = curr_y + 30 + bar_h
247 330
248 # render second chart 331 # render second chart
249 #draw_legend_line(ctx, "Disk throughput", DISK_TPUT_COLOR, off_x, curr_y+20, leg_s) 332 draw_legend_line(ctx, "Disk throughput", DISK_TPUT_COLOR, off_x, curr_y+20, leg_s)
250 #draw_legend_box(ctx, "Disk utilization", IO_COLOR, off_x + 120, curr_y+20, leg_s) 333 draw_legend_box(ctx, "Disk utilization", IO_COLOR, off_x + 120, curr_y+20, leg_s)
251 334
252 # render I/O utilization 335 # render I/O utilization
253 #chart_rect = (off_x, curr_y+30, w, bar_h) 336 chart_rect = (off_x, curr_y+30, w, bar_h)
254 #draw_box_ticks(ctx, chart_rect, sec_w) 337 if clip_visible (clip, chart_rect):
255 #draw_chart(ctx, IO_COLOR, True, chart_rect, [(sample.time, sample.util) for sample in disk_stats], proc_tree) 338 draw_box_ticks (ctx, chart_rect, sec_w)
256 339 draw_annotations (ctx, proc_tree, trace.times, chart_rect)
340 draw_chart (ctx, IO_COLOR, True, chart_rect, \
341 [(sample.time, sample.util) for sample in trace.disk_stats], \
342 proc_tree, None)
343
257 # render disk throughput 344 # render disk throughput
258 #max_sample = max(disk_stats, key=lambda s: s.tput) 345 max_sample = max (trace.disk_stats, key = lambda s: s.tput)
259 #draw_chart(ctx, DISK_TPUT_COLOR, False, chart_rect, [(sample.time, sample.tput) for sample in disk_stats], proc_tree) 346 if clip_visible (clip, chart_rect):
260 347 draw_chart (ctx, DISK_TPUT_COLOR, False, chart_rect, \
261 #pos_x = off_x + ((max_sample.time - proc_tree.start_time) * w / proc_tree.duration) 348 [(sample.time, sample.tput) for sample in trace.disk_stats], \
262 pos_x = off_x 349 proc_tree, None)
350
351 pos_x = off_x + ((max_sample.time - proc_tree.start_time) * w / proc_tree.duration)
263 352
264 shift_x, shift_y = -20, 20 353 shift_x, shift_y = -20, 20
265 if (pos_x < off_x + 245): 354 if (pos_x < off_x + 245):
266 shift_x, shift_y = 5, 40 355 shift_x, shift_y = 5, 40
267
268 #label = "%dMB/s" % round((max_sample.tput) / 1024.0)
269 #draw_text(ctx, label, DISK_TPUT_COLOR, pos_x + shift_x, curr_y + shift_y)
270
271
272 356
357 label = "%dMB/s" % round ((max_sample.tput) / 1024.0)
358 draw_text (ctx, label, DISK_TPUT_COLOR, pos_x + shift_x, curr_y + shift_y)
359
360 curr_y = curr_y + 30 + bar_h
361
362 # render mem usage
363 chart_rect = (off_x, curr_y+30, w, meminfo_bar_h)
364 mem_stats = trace.mem_stats
365 if mem_stats and clip_visible (clip, chart_rect):
366 mem_scale = max(sample.records['MemTotal'] - sample.records['MemFree'] for sample in mem_stats)
367 draw_legend_box(ctx, "Mem cached (scale: %u MiB)" % (float(mem_scale) / 1024), MEM_CACHED_COLOR, off_x, curr_y+20, leg_s)
368 draw_legend_box(ctx, "Used", MEM_USED_COLOR, off_x + 240, curr_y+20, leg_s)
369 draw_legend_box(ctx, "Buffers", MEM_BUFFERS_COLOR, off_x + 360, curr_y+20, leg_s)
370 draw_legend_line(ctx, "Swap (scale: %u MiB)" % max([(sample.records['SwapTotal'] - sample.records['SwapFree'])/1024 for sample in mem_stats]), \
371 MEM_SWAP_COLOR, off_x + 480, curr_y+20, leg_s)
372 draw_box_ticks(ctx, chart_rect, sec_w)
373 draw_annotations(ctx, proc_tree, trace.times, chart_rect)
374 draw_chart(ctx, MEM_BUFFERS_COLOR, True, chart_rect, \
375 [(sample.time, sample.records['MemTotal'] - sample.records['MemFree']) for sample in trace.mem_stats], \
376 proc_tree, [0, mem_scale])
377 draw_chart(ctx, MEM_USED_COLOR, True, chart_rect, \
378 [(sample.time, sample.records['MemTotal'] - sample.records['MemFree'] - sample.records['Buffers']) for sample in mem_stats], \
379 proc_tree, [0, mem_scale])
380 draw_chart(ctx, MEM_CACHED_COLOR, True, chart_rect, \
381 [(sample.time, sample.records['Cached']) for sample in mem_stats], \
382 proc_tree, [0, mem_scale])
383 draw_chart(ctx, MEM_SWAP_COLOR, False, chart_rect, \
384 [(sample.time, float(sample.records['SwapTotal'] - sample.records['SwapFree'])) for sample in mem_stats], \
385 proc_tree, None)
386
387 curr_y = curr_y + meminfo_bar_h
388
389 return curr_y
390
391def render_processes_chart(ctx, options, trace, curr_y, w, h, sec_w):
273 chart_rect = [off_x, curr_y+60, w, h - 2 * off_y - (curr_y+60) + proc_h] 392 chart_rect = [off_x, curr_y+60, w, h - 2 * off_y - (curr_y+60) + proc_h]
274 393
275 draw_legend_box(ctx, "Configure", TASK_COLOR_CONFIGURE, off_x , curr_y + 45, leg_s) 394 draw_legend_box (ctx, "Configure", \
276 draw_legend_box(ctx, "Compile", TASK_COLOR_COMPILE, off_x+120, curr_y + 45, leg_s) 395 TASK_COLOR_CONFIGURE, off_x , curr_y + 45, leg_s)
277 draw_legend_box(ctx, "Install", TASK_COLOR_INSTALL, off_x+240, curr_y + 45, leg_s) 396 draw_legend_box (ctx, "Compile", \
278 draw_legend_box(ctx, "Package", TASK_COLOR_PACKAGE, off_x+360, curr_y + 45, leg_s) 397 TASK_COLOR_COMPILE, off_x+120, curr_y + 45, leg_s)
279 draw_legend_box(ctx, "Populate Sysroot", TASK_COLOR_SYSROOT, off_x+480, curr_y + 45, leg_s) 398 draw_legend_box (ctx, "Install", \
280 399 TASK_COLOR_INSTALL, off_x+240, curr_y + 45, leg_s)
400 draw_legend_box (ctx, "Package", \
401 TASK_COLOR_PACKAGE, off_x+360, curr_y + 45, leg_s)
402 draw_legend_box (ctx, "Populate Sysroot", \
403 TASK_COLOR_SYSROOT, off_x+480, curr_y + 45, leg_s)
404
281 ctx.set_font_size(PROC_TEXT_FONT_SIZE) 405 ctx.set_font_size(PROC_TEXT_FONT_SIZE)
282 406
283 draw_box_ticks(ctx, chart_rect, sec_w) 407 draw_box_ticks(ctx, chart_rect, sec_w)
284 draw_5sec_labels(ctx, chart_rect, sec_w) 408 draw_sec_labels(ctx, chart_rect, sec_w, 30)
285 409
286 y = curr_y+60 410 y = curr_y+60
287 411
288 offset = min(res.start.keys()) 412 offset = min(trace.start.keys())
289 for s in sorted(res.start.keys()): 413 for s in sorted(trace.start.keys()):
290 for val in sorted(res.start[s]): 414 for val in sorted(trace.start[s]):
291 task = val.split(":")[1] 415 task = val.split(":")[1]
292 #print val 416 #print val
293 #print res.processes[val][1] 417 #print trace.processes[val][1]
294 #print s 418 #print s
295 x = (s - offset) * sec_w 419 x = (s - offset) * sec_w
296 w = ((res.processes[val][1] - s) * sec_w) 420 w = ((trace.processes[val][1] - s) * sec_w)
297 421
298 #print "proc at %s %s %s %s" % (x, y, w, proc_h) 422 #print "proc at %s %s %s %s" % (x, y, w, proc_h)
299 col = None 423 col = None
@@ -315,95 +439,419 @@ def render(ctx, res):
315 draw_label_in_box(ctx, PROC_TEXT_COLOR, val, x, y + proc_h - 4, w, proc_h) 439 draw_label_in_box(ctx, PROC_TEXT_COLOR, val, x, y + proc_h - 4, w, proc_h)
316 y = y + proc_h 440 y = y + proc_h
317 441
442 return curr_y
443
444#
445# Render the chart.
446#
447def render(ctx, options, xscale, trace):
448 (w, h) = extents (options, xscale, trace)
449 global OPTIONS
450 OPTIONS = options.app_options
451
452 # x, y, w, h
453 clip = ctx.clip_extents()
454
455 sec_w = int (xscale * sec_w_base)
456 ctx.set_line_width(1.0)
457 ctx.select_font_face(FONT_NAME)
458 draw_fill_rect(ctx, WHITE, (0, 0, max(w, MIN_IMG_W), h))
459 w -= 2*off_x
460 curr_y = off_y;
461
462 curr_y = render_processes_chart (ctx, options, trace, curr_y, w, h, sec_w)
463
464 return
465
466 proc_tree = options.proc_tree (trace)
467
468 # draw the title and headers
469 if proc_tree.idle:
470 duration = proc_tree.idle
471 else:
472 duration = proc_tree.duration
473
474 if not options.kernel_only:
475 curr_y = draw_header (ctx, trace.headers, duration)
476 else:
477 curr_y = off_y;
478
479 if options.charts:
480 curr_y = render_charts (ctx, options, clip, trace, curr_y, w, h, sec_w)
481
318 # draw process boxes 482 # draw process boxes
319 #draw_process_bar_chart(ctx, proc_tree, curr_y + bar_h, w, h) 483 proc_height = h
484 if proc_tree.taskstats and options.cumulative:
485 proc_height -= CUML_HEIGHT
320 486
321 ctx.set_font_size(SIG_FONT_SIZE) 487 draw_process_bar_chart(ctx, clip, options, proc_tree, trace.times,
322 draw_text(ctx, SIGNATURE, SIG_COLOR, off_x + 5, h - off_y - 5) 488 curr_y, w, proc_height, sec_w)
323 489
324def draw_process_bar_chart(ctx, proc_tree, curr_y, w, h): 490 curr_y = proc_height
491 ctx.set_font_size(SIG_FONT_SIZE)
492 draw_text(ctx, SIGNATURE, SIG_COLOR, off_x + 5, proc_height - 8)
493
494 # draw a cumulative CPU-time-per-process graph
495 if proc_tree.taskstats and options.cumulative:
496 cuml_rect = (off_x, curr_y + off_y, w, CUML_HEIGHT/2 - off_y * 2)
497 if clip_visible (clip, cuml_rect):
498 draw_cuml_graph(ctx, proc_tree, cuml_rect, duration, sec_w, STAT_TYPE_CPU)
499
500 # draw a cumulative I/O-time-per-process graph
501 if proc_tree.taskstats and options.cumulative:
502 cuml_rect = (off_x, curr_y + off_y * 100, w, CUML_HEIGHT/2 - off_y * 2)
503 if clip_visible (clip, cuml_rect):
504 draw_cuml_graph(ctx, proc_tree, cuml_rect, duration, sec_w, STAT_TYPE_IO)
505
506def draw_process_bar_chart(ctx, clip, options, proc_tree, times, curr_y, w, h, sec_w):
507 header_size = 0
508 if not options.kernel_only:
509 draw_legend_box (ctx, "Running (%cpu)",
510 PROC_COLOR_R, off_x , curr_y + 45, leg_s)
511 draw_legend_box (ctx, "Unint.sleep (I/O)",
512 PROC_COLOR_D, off_x+120, curr_y + 45, leg_s)
513 draw_legend_box (ctx, "Sleeping",
514 PROC_COLOR_S, off_x+240, curr_y + 45, leg_s)
515 draw_legend_box (ctx, "Zombie",
516 PROC_COLOR_Z, off_x+360, curr_y + 45, leg_s)
517 header_size = 45
518
519 chart_rect = [off_x, curr_y + header_size + 15,
520 w, h - 2 * off_y - (curr_y + header_size + 15) + proc_h]
521 ctx.set_font_size (PROC_TEXT_FONT_SIZE)
522
523 draw_box_ticks (ctx, chart_rect, sec_w)
524 if sec_w > 100:
525 nsec = 1
526 else:
527 nsec = 5
528 draw_sec_labels (ctx, chart_rect, sec_w, nsec)
529 draw_annotations (ctx, proc_tree, times, chart_rect)
325 530
326 for root in proc_tree.process_tree: 531 y = curr_y + 60
327 draw_processes_recursively(ctx, root, proc_tree, y, proc_h, chart_rect) 532 for root in proc_tree.process_tree:
328 y = y + proc_h * proc_tree.num_nodes([root]) 533 draw_processes_recursively(ctx, root, proc_tree, y, proc_h, chart_rect, clip)
534 y = y + proc_h * proc_tree.num_nodes([root])
329 535
330 536
331def draw_header(ctx, headers, off_x, duration): 537def draw_header (ctx, headers, duration):
332 dur = duration / 100.0
333 toshow = [ 538 toshow = [
334 ('system.uname', 'uname', lambda s: s), 539 ('system.uname', 'uname', lambda s: s),
335 ('system.release', 'release', lambda s: s), 540 ('system.release', 'release', lambda s: s),
336 ('system.cpu', 'CPU', lambda s: re.sub('model name\s*:\s*', '', s, 1)), 541 ('system.cpu', 'CPU', lambda s: re.sub('model name\s*:\s*', '', s, 1)),
337 ('system.kernel.options', 'kernel options', lambda s: s), 542 ('system.kernel.options', 'kernel options', lambda s: s),
338 ('pseudo.header', 'time', lambda s: '%02d:%05.2f' % (math.floor(dur/60), dur - 60 * math.floor(dur/60)))
339 ] 543 ]
340 544
341 header_y = ctx.font_extents()[2] + 10 545 header_y = ctx.font_extents()[2] + 10
342 ctx.set_font_size(TITLE_FONT_SIZE) 546 ctx.set_font_size(TITLE_FONT_SIZE)
343 draw_text(ctx, headers['title'], TEXT_COLOR, off_x, header_y) 547 draw_text(ctx, headers['title'], TEXT_COLOR, off_x, header_y)
344 ctx.set_font_size(TEXT_FONT_SIZE) 548 ctx.set_font_size(TEXT_FONT_SIZE)
345 549
346 for (headerkey, headertitle, mangle) in toshow: 550 for (headerkey, headertitle, mangle) in toshow:
347 header_y += ctx.font_extents()[2] 551 header_y += ctx.font_extents()[2]
348 txt = headertitle + ': ' + mangle(headers.get(headerkey)) 552 if headerkey in headers:
553 value = headers.get(headerkey)
554 else:
555 value = ""
556 txt = headertitle + ': ' + mangle(value)
349 draw_text(ctx, txt, TEXT_COLOR, off_x, header_y) 557 draw_text(ctx, txt, TEXT_COLOR, off_x, header_y)
350 558
559 dur = duration / 100.0
560 txt = 'time : %02d:%05.2f' % (math.floor(dur/60), dur - 60 * math.floor(dur/60))
561 if headers.get('system.maxpid') is not None:
562 txt = txt + ' max pid: %s' % (headers.get('system.maxpid'))
563
564 header_y += ctx.font_extents()[2]
565 draw_text (ctx, txt, TEXT_COLOR, off_x, header_y)
566
351 return header_y 567 return header_y
352 568
353def draw_processes_recursively(ctx, proc, proc_tree, y, proc_h, rect) : 569def draw_processes_recursively(ctx, proc, proc_tree, y, proc_h, rect, clip) :
354 x = rect[0] + ((proc.start_time - proc_tree.start_time) * rect[2] / proc_tree.duration) 570 x = rect[0] + ((proc.start_time - proc_tree.start_time) * rect[2] / proc_tree.duration)
355 w = ((proc.duration) * rect[2] / proc_tree.duration) 571 w = ((proc.duration) * rect[2] / proc_tree.duration)
356 572
357 draw_process_activity_colors(ctx, proc, proc_tree, x, y, w, proc_h, rect) 573 draw_process_activity_colors(ctx, proc, proc_tree, x, y, w, proc_h, rect, clip)
358 draw_rect(ctx, PROC_BORDER_COLOR, (x, y, w, proc_h)) 574 draw_rect(ctx, PROC_BORDER_COLOR, (x, y, w, proc_h))
359 draw_label_in_box(ctx, PROC_TEXT_COLOR, proc.cmd, x, y + proc_h - 4, w, rect[0] + rect[2]) 575 ipid = int(proc.pid)
576 if not OPTIONS.show_all:
577 cmdString = proc.cmd
578 else:
579 cmdString = ''
580 if (OPTIONS.show_pid or OPTIONS.show_all) and ipid is not 0:
581 cmdString = cmdString + " [" + str(ipid // 1000) + "]"
582 if OPTIONS.show_all:
583 if proc.args:
584 cmdString = cmdString + " '" + "' '".join(proc.args) + "'"
585 else:
586 cmdString = cmdString + " " + proc.exe
587
588 draw_label_in_box(ctx, PROC_TEXT_COLOR, cmdString, x, y + proc_h - 4, w, rect[0] + rect[2])
360 589
361 next_y = y + proc_h 590 next_y = y + proc_h
362 for child in proc.child_list: 591 for child in proc.child_list:
363 child_x, child_y = draw_processes_recursively(ctx, child, proc_tree, next_y, proc_h, rect) 592 if next_y > clip[1] + clip[3]:
593 break
594 child_x, child_y = draw_processes_recursively(ctx, child, proc_tree, next_y, proc_h, rect, clip)
364 draw_process_connecting_lines(ctx, x, y, child_x, child_y, proc_h) 595 draw_process_connecting_lines(ctx, x, y, child_x, child_y, proc_h)
365 next_y = next_y + proc_h * proc_tree.num_nodes([child]) 596 next_y = next_y + proc_h * proc_tree.num_nodes([child])
366 597
367 return x, y 598 return x, y
368 599
369 600
370def draw_process_activity_colors(ctx, proc, proc_tree, x, y, w, proc_h, rect): 601def draw_process_activity_colors(ctx, proc, proc_tree, x, y, w, proc_h, rect, clip):
602
603 if y > clip[1] + clip[3] or y + proc_h + 2 < clip[1]:
604 return
605
371 draw_fill_rect(ctx, PROC_COLOR_S, (x, y, w, proc_h)) 606 draw_fill_rect(ctx, PROC_COLOR_S, (x, y, w, proc_h))
372 607
373 last_tx = -1 608 last_tx = -1
374 for sample in proc.samples : 609 for sample in proc.samples :
375 tx = rect[0] + round(((sample.time - proc_tree.start_time) * rect[2] / proc_tree.duration)) 610 tx = rect[0] + round(((sample.time - proc_tree.start_time) * rect[2] / proc_tree.duration))
611
612 # samples are sorted chronologically
613 if tx < clip[0]:
614 continue
615 if tx > clip[0] + clip[2]:
616 break
617
376 tw = round(proc_tree.sample_period * rect[2] / float(proc_tree.duration)) 618 tw = round(proc_tree.sample_period * rect[2] / float(proc_tree.duration))
377 if last_tx != -1 and abs(last_tx - tx) <= tw: 619 if last_tx != -1 and abs(last_tx - tx) <= tw:
378 tw -= last_tx - tx 620 tw -= last_tx - tx
379 tx = last_tx 621 tx = last_tx
380 622 tw = max (tw, 1) # nice to see at least something
623
381 last_tx = tx + tw 624 last_tx = tx + tw
382 state = get_proc_state( sample.state ) 625 state = get_proc_state( sample.state )
383 626
384 color = STATE_COLORS[state] 627 color = STATE_COLORS[state]
385 if state == STATE_RUNNING: 628 if state == STATE_RUNNING:
386 alpha = sample.cpu_sample.user + sample.cpu_sample.sys 629 alpha = min (sample.cpu_sample.user + sample.cpu_sample.sys, 1.0)
387 color = tuple(list(PROC_COLOR_R[0:3]) + [alpha]) 630 color = tuple(list(PROC_COLOR_R[0:3]) + [alpha])
631# print "render time %d [ tx %d tw %d ], sample state %s color %s alpha %g" % (sample.time, tx, tw, state, color, alpha)
388 elif state == STATE_SLEEPING: 632 elif state == STATE_SLEEPING:
389 continue 633 continue
390 634
391 draw_fill_rect(ctx, color, (tx, y, tw, proc_h)) 635 draw_fill_rect(ctx, color, (tx, y, tw, proc_h))
392 636
393
394def draw_process_connecting_lines(ctx, px, py, x, y, proc_h): 637def draw_process_connecting_lines(ctx, px, py, x, y, proc_h):
395 ctx.set_source_rgba(*DEP_COLOR) 638 ctx.set_source_rgba(*DEP_COLOR)
396 ctx.set_dash([2,2]) 639 ctx.set_dash([2, 2])
397 if abs(px - x) < 3: 640 if abs(px - x) < 3:
398 dep_off_x = 3 641 dep_off_x = 3
399 dep_off_y = proc_h / 4 642 dep_off_y = proc_h / 4
400 ctx.move_to(x, y + proc_h / 2) 643 ctx.move_to(x, y + proc_h / 2)
401 ctx.line_to(px - dep_off_x, y + proc_h / 2) 644 ctx.line_to(px - dep_off_x, y + proc_h / 2)
402 ctx.line_to(px - dep_off_x, py - dep_off_y) 645 ctx.line_to(px - dep_off_x, py - dep_off_y)
403 ctx.line_to(px, py - dep_off_y) 646 ctx.line_to(px, py - dep_off_y)
404 else: 647 else:
405 ctx.move_to(x, y + proc_h / 2) 648 ctx.move_to(x, y + proc_h / 2)
406 ctx.line_to(px, y + proc_h / 2) 649 ctx.line_to(px, y + proc_h / 2)
407 ctx.line_to(px, py) 650 ctx.line_to(px, py)
408 ctx.stroke() 651 ctx.stroke()
409 ctx.set_dash([]) 652 ctx.set_dash([])
653
654# elide the bootchart collector - it is quite distorting
655def elide_bootchart(proc):
656 return proc.cmd == 'bootchartd' or proc.cmd == 'bootchart-colle'
657
658class CumlSample:
659 def __init__(self, proc):
660 self.cmd = proc.cmd
661 self.samples = []
662 self.merge_samples (proc)
663 self.color = None
664
665 def merge_samples(self, proc):
666 self.samples.extend (proc.samples)
667 self.samples.sort (key = lambda p: p.time)
668
669 def next(self):
670 global palette_idx
671 palette_idx += HSV_STEP
672 return palette_idx
673
674 def get_color(self):
675 if self.color is None:
676 i = self.next() % HSV_MAX_MOD
677 h = 0.0
678 if i is not 0:
679 h = (1.0 * i) / HSV_MAX_MOD
680 s = 0.5
681 v = 1.0
682 c = colorsys.hsv_to_rgb (h, s, v)
683 self.color = (c[0], c[1], c[2], 1.0)
684 return self.color
685
686
687def draw_cuml_graph(ctx, proc_tree, chart_bounds, duration, sec_w, stat_type):
688 global palette_idx
689 palette_idx = 0
690
691 time_hash = {}
692 total_time = 0.0
693 m_proc_list = {}
694
695 if stat_type is STAT_TYPE_CPU:
696 sample_value = 'cpu'
697 else:
698 sample_value = 'io'
699 for proc in proc_tree.process_list:
700 if elide_bootchart(proc):
701 continue
702
703 for sample in proc.samples:
704 total_time += getattr(sample.cpu_sample, sample_value)
705 if not sample.time in time_hash:
706 time_hash[sample.time] = 1
707
708 # merge pids with the same cmd
709 if not proc.cmd in m_proc_list:
710 m_proc_list[proc.cmd] = CumlSample (proc)
711 continue
712 s = m_proc_list[proc.cmd]
713 s.merge_samples (proc)
714
715 # all the sample times
716 times = sorted(time_hash)
717 if len (times) < 2:
718 print("degenerate boot chart")
719 return
720
721 pix_per_ns = chart_bounds[3] / total_time
722# print "total time: %g pix-per-ns %g" % (total_time, pix_per_ns)
723
724 # FIXME: we have duplicates in the process list too [!] - why !?
725
726 # Render bottom up, left to right
727 below = {}
728 for time in times:
729 below[time] = chart_bounds[1] + chart_bounds[3]
730
731 # same colors each time we render
732 random.seed (0)
733
734 ctx.set_line_width(1)
735
736 legends = []
737 labels = []
738
739 # render each pid in order
740 for cs in m_proc_list.values():
741 row = {}
742 cuml = 0.0
743
744 # print "pid : %s -> %g samples %d" % (proc.cmd, cuml, len (cs.samples))
745 for sample in cs.samples:
746 cuml += getattr(sample.cpu_sample, sample_value)
747 row[sample.time] = cuml
748
749 process_total_time = cuml
750
751 # hide really tiny processes
752 if cuml * pix_per_ns <= 2:
753 continue
754
755 last_time = times[0]
756 y = last_below = below[last_time]
757 last_cuml = cuml = 0.0
758
759 ctx.set_source_rgba(*cs.get_color())
760 for time in times:
761 render_seg = False
762
763 # did the underlying trend increase ?
764 if below[time] != last_below:
765 last_below = below[last_time]
766 last_cuml = cuml
767 render_seg = True
768
769 # did we move up a pixel increase ?
770 if time in row:
771 nc = round (row[time] * pix_per_ns)
772 if nc != cuml:
773 last_cuml = cuml
774 cuml = nc
775 render_seg = True
776
777# if last_cuml > cuml:
778# assert fail ... - un-sorted process samples
779
780 # draw the trailing rectangle from the last time to
781 # before now, at the height of the last segment.
782 if render_seg:
783 w = math.ceil ((time - last_time) * chart_bounds[2] / proc_tree.duration) + 1
784 x = chart_bounds[0] + round((last_time - proc_tree.start_time) * chart_bounds[2] / proc_tree.duration)
785 ctx.rectangle (x, below[last_time] - last_cuml, w, last_cuml)
786 ctx.fill()
787# ctx.stroke()
788 last_time = time
789 y = below [time] - cuml
790
791 row[time] = y
792
793 # render the last segment
794 x = chart_bounds[0] + round((last_time - proc_tree.start_time) * chart_bounds[2] / proc_tree.duration)
795 y = below[last_time] - cuml
796 ctx.rectangle (x, y, chart_bounds[2] - x, cuml)
797 ctx.fill()
798# ctx.stroke()
799
800 # render legend if it will fit
801 if cuml > 8:
802 label = cs.cmd
803 extnts = ctx.text_extents(label)
804 label_w = extnts[2]
805 label_h = extnts[3]
806# print "Text extents %g by %g" % (label_w, label_h)
807 labels.append((label,
808 chart_bounds[0] + chart_bounds[2] - label_w - off_x * 2,
809 y + (cuml + label_h) / 2))
810 if cs in legends:
811 print("ARGH - duplicate process in list !")
812
813 legends.append ((cs, process_total_time))
814
815 below = row
816
817 # render grid-lines over the top
818 draw_box_ticks(ctx, chart_bounds, sec_w)
819
820 # render labels
821 for l in labels:
822 draw_text(ctx, l[0], TEXT_COLOR, l[1], l[2])
823
824 # Render legends
825 font_height = 20
826 label_width = 300
827 LEGENDS_PER_COL = 15
828 LEGENDS_TOTAL = 45
829 ctx.set_font_size (TITLE_FONT_SIZE)
830 dur_secs = duration / 100
831 cpu_secs = total_time / 1000000000
832
833 # misleading - with multiple CPUs ...
834# idle = ((dur_secs - cpu_secs) / dur_secs) * 100.0
835 if stat_type is STAT_TYPE_CPU:
836 label = "Cumulative CPU usage, by process; total CPU: " \
837 " %.5g(s) time: %.3g(s)" % (cpu_secs, dur_secs)
838 else:
839 label = "Cumulative I/O usage, by process; total I/O: " \
840 " %.5g(s) time: %.3g(s)" % (cpu_secs, dur_secs)
841
842 draw_text(ctx, label, TEXT_COLOR, chart_bounds[0] + off_x,
843 chart_bounds[1] + font_height)
844
845 i = 0
846 legends = sorted(legends, key=itemgetter(1), reverse=True)
847 ctx.set_font_size(TEXT_FONT_SIZE)
848 for t in legends:
849 cs = t[0]
850 time = t[1]
851 x = chart_bounds[0] + off_x + int (i/LEGENDS_PER_COL) * label_width
852 y = chart_bounds[1] + font_height * ((i % LEGENDS_PER_COL) + 2)
853 str = "%s - %.0f(ms) (%2.2f%%)" % (cs.cmd, time/1000000, (time/total_time) * 100.0)
854 draw_legend_box(ctx, str, cs.color, x, y, leg_s)
855 i = i + 1
856 if i >= LEGENDS_TOTAL:
857 break
diff --git a/scripts/pybootchartgui/pybootchartgui/gui.py b/scripts/pybootchartgui/pybootchartgui/gui.py
index 310c3d1bcc..ddeb88cd9b 100644
--- a/scripts/pybootchartgui/pybootchartgui/gui.py
+++ b/scripts/pybootchartgui/pybootchartgui/gui.py
@@ -1,273 +1,347 @@
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
1import gobject 16import gobject
2import gtk 17import gtk
3import gtk.gdk 18import gtk.gdk
4import gtk.keysyms 19import gtk.keysyms
5 20from . import draw
6import draw 21from .draw import RenderOptions
7 22
8class PyBootchartWidget(gtk.DrawingArea): 23class PyBootchartWidget(gtk.DrawingArea):
9 __gsignals__ = { 24 __gsignals__ = {
10 'expose-event': 'override', 25 'expose-event': 'override',
11 'clicked' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_STRING, gtk.gdk.Event)), 26 'clicked' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_STRING, gtk.gdk.Event)),
12 'position-changed' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_INT, gobject.TYPE_INT)), 27 'position-changed' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_INT, gobject.TYPE_INT)),
13 'set-scroll-adjustments' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gtk.Adjustment, gtk.Adjustment)) 28 'set-scroll-adjustments' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gtk.Adjustment, gtk.Adjustment))
14 } 29 }
15 30
16 def __init__(self, res): 31 def __init__(self, trace, options, xscale):
17 gtk.DrawingArea.__init__(self) 32 gtk.DrawingArea.__init__(self)
18 33
19 self.res = res 34 self.trace = trace
20 35 self.options = options
21 self.set_flags(gtk.CAN_FOCUS) 36
22 37 self.set_flags(gtk.CAN_FOCUS)
23 self.add_events(gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.BUTTON_RELEASE_MASK) 38
24 self.connect("button-press-event", self.on_area_button_press) 39 self.add_events(gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.BUTTON_RELEASE_MASK)
25 self.connect("button-release-event", self.on_area_button_release) 40 self.connect("button-press-event", self.on_area_button_press)
26 self.add_events(gtk.gdk.POINTER_MOTION_MASK | gtk.gdk.POINTER_MOTION_HINT_MASK | gtk.gdk.BUTTON_RELEASE_MASK) 41 self.connect("button-release-event", self.on_area_button_release)
27 self.connect("motion-notify-event", self.on_area_motion_notify) 42 self.add_events(gtk.gdk.POINTER_MOTION_MASK | gtk.gdk.POINTER_MOTION_HINT_MASK | gtk.gdk.BUTTON_RELEASE_MASK)
28 self.connect("scroll-event", self.on_area_scroll_event) 43 self.connect("motion-notify-event", self.on_area_motion_notify)
29 self.connect('key-press-event', self.on_key_press_event) 44 self.connect("scroll-event", self.on_area_scroll_event)
30 45 self.connect('key-press-event', self.on_key_press_event)
31 self.connect('set-scroll-adjustments', self.on_set_scroll_adjustments) 46
32 self.connect("size-allocate", self.on_allocation_size_changed) 47 self.connect('set-scroll-adjustments', self.on_set_scroll_adjustments)
33 self.connect("position-changed", self.on_position_changed) 48 self.connect("size-allocate", self.on_allocation_size_changed)
34 49 self.connect("position-changed", self.on_position_changed)
35 self.zoom_ratio = 1.0 50
36 self.x, self.y = 0.0, 0.0 51 self.zoom_ratio = 1.0
37 52 self.xscale = xscale
38 self.chart_width, self.chart_height = draw.extents(res) 53 self.x, self.y = 0.0, 0.0
39 self.hadj = None 54
40 self.vadj = None 55 self.chart_width, self.chart_height = draw.extents(self.options, self.xscale, self.trace)
41 56 self.hadj = None
42 def do_expose_event(self, event): 57 self.vadj = None
43 cr = self.window.cairo_create() 58 self.hadj_changed_signal_id = None
44 59 self.vadj_changed_signal_id = None
45 # set a clip region for the expose event 60
46 cr.rectangle( 61 def do_expose_event(self, event):
47 event.area.x, event.area.y, 62 cr = self.window.cairo_create()
48 event.area.width, event.area.height 63
49 ) 64 # set a clip region for the expose event
50 cr.clip() 65 cr.rectangle(
51 self.draw(cr, self.get_allocation()) 66 event.area.x, event.area.y,
52 return False 67 event.area.width, event.area.height
53 68 )
54 def draw(self, cr, rect): 69 cr.clip()
55 cr.set_source_rgba(1.0, 1.0, 1.0, 1.0) 70 self.draw(cr, self.get_allocation())
56 cr.paint() 71 return False
57 cr.scale(self.zoom_ratio, self.zoom_ratio) 72
58 cr.translate(-self.x, -self.y) 73 def draw(self, cr, rect):
59 draw.render(cr, self.res) 74 cr.set_source_rgba(1.0, 1.0, 1.0, 1.0)
60 75 cr.paint()
61 def position_changed(self): 76 cr.scale(self.zoom_ratio, self.zoom_ratio)
62 self.emit("position-changed", self.x, self.y) 77 cr.translate(-self.x, -self.y)
63 78 draw.render(cr, self.options, self.xscale, self.trace)
64 ZOOM_INCREMENT = 1.25 79
65 80 def position_changed(self):
66 def zoom_image(self, zoom_ratio): 81 self.emit("position-changed", self.x, self.y)
67 self.zoom_ratio = zoom_ratio 82
68 self._set_scroll_adjustments(self.hadj, self.vadj) 83 ZOOM_INCREMENT = 1.25
69 self.queue_draw() 84
70 85 def zoom_image (self, zoom_ratio):
71 def zoom_to_rect(self, rect): 86 self.zoom_ratio = zoom_ratio
72 zoom_ratio = float(rect.width)/float(self.chart_width) 87 self._set_scroll_adjustments (self.hadj, self.vadj)
73 self.zoom_image(zoom_ratio) 88 self.queue_draw()
74 self.x = 0 89
75 self.position_changed() 90 def zoom_to_rect (self, rect):
76 91 zoom_ratio = float(rect.width)/float(self.chart_width)
77 def on_zoom_in(self, action): 92 self.zoom_image(zoom_ratio)
78 self.zoom_image(self.zoom_ratio * self.ZOOM_INCREMENT) 93 self.x = 0
79 94 self.position_changed()
80 def on_zoom_out(self, action): 95
81 self.zoom_image(self.zoom_ratio / self.ZOOM_INCREMENT) 96 def set_xscale(self, xscale):
82 97 old_mid_x = self.x + self.hadj.page_size / 2
83 def on_zoom_fit(self, action): 98 self.xscale = xscale
84 self.zoom_to_rect(self.get_allocation()) 99 self.chart_width, self.chart_height = draw.extents(self.options, self.xscale, self.trace)
85 100 new_x = old_mid_x
86 def on_zoom_100(self, action): 101 self.zoom_image (self.zoom_ratio)
87 self.zoom_image(1.0) 102
88 103 def on_expand(self, action):
89 POS_INCREMENT = 100 104 self.set_xscale (self.xscale * 1.5)
90 105
91 def on_key_press_event(self, widget, event): 106 def on_contract(self, action):
92 if event.keyval == gtk.keysyms.Left: 107 self.set_xscale (self.xscale / 1.5)
93 self.x -= self.POS_INCREMENT/self.zoom_ratio 108
94 elif event.keyval == gtk.keysyms.Right: 109 def on_zoom_in(self, action):
95 self.x += self.POS_INCREMENT/self.zoom_ratio 110 self.zoom_image(self.zoom_ratio * self.ZOOM_INCREMENT)
96 elif event.keyval == gtk.keysyms.Up: 111
97 self.y -= self.POS_INCREMENT/self.zoom_ratio 112 def on_zoom_out(self, action):
98 elif event.keyval == gtk.keysyms.Down: 113 self.zoom_image(self.zoom_ratio / self.ZOOM_INCREMENT)
99 self.y += self.POS_INCREMENT/self.zoom_ratio 114
100 elif event.keyval == gtk.keysyms.Page_Up: 115 def on_zoom_fit(self, action):
101 self.zoom_image(self.zoom_ratio * self.ZOOM_INCREMENT) 116 self.zoom_to_rect(self.get_allocation())
102 elif event.keyval == gtk.keysyms.Page_Down: 117
103 self.zoom_image(self.zoom_ratio / self.ZOOM_INCREMENT) 118 def on_zoom_100(self, action):
104 else: 119 self.zoom_image(1.0)
105 return False 120 self.set_xscale(1.0)
106 self.queue_draw() 121
107 self.position_changed() 122 def show_toggled(self, button):
123 self.options.app_options.show_all = button.get_property ('active')
124 self.queue_draw()
125
126 POS_INCREMENT = 100
127
128 def on_key_press_event(self, widget, event):
129 if event.keyval == gtk.keysyms.Left:
130 self.x -= self.POS_INCREMENT/self.zoom_ratio
131 elif event.keyval == gtk.keysyms.Right:
132 self.x += self.POS_INCREMENT/self.zoom_ratio
133 elif event.keyval == gtk.keysyms.Up:
134 self.y -= self.POS_INCREMENT/self.zoom_ratio
135 elif event.keyval == gtk.keysyms.Down:
136 self.y += self.POS_INCREMENT/self.zoom_ratio
137 else:
138 return False
139 self.queue_draw()
140 self.position_changed()
141 return True
142
143 def on_area_button_press(self, area, event):
144 if event.button == 2 or event.button == 1:
145 area.window.set_cursor(gtk.gdk.Cursor(gtk.gdk.FLEUR))
146 self.prevmousex = event.x
147 self.prevmousey = event.y
148 if event.type not in (gtk.gdk.BUTTON_PRESS, gtk.gdk.BUTTON_RELEASE):
149 return False
150 return False
151
152 def on_area_button_release(self, area, event):
153 if event.button == 2 or event.button == 1:
154 area.window.set_cursor(gtk.gdk.Cursor(gtk.gdk.ARROW))
155 self.prevmousex = None
156 self.prevmousey = None
157 return True
158 return False
159
160 def on_area_scroll_event(self, area, event):
161 if event.state & gtk.gdk.CONTROL_MASK:
162 if event.direction == gtk.gdk.SCROLL_UP:
163 self.zoom_image(self.zoom_ratio * self.ZOOM_INCREMENT)
108 return True 164 return True
109 165 if event.direction == gtk.gdk.SCROLL_DOWN:
110 def on_area_button_press(self, area, event): 166 self.zoom_image(self.zoom_ratio / self.ZOOM_INCREMENT)
111 if event.button == 2 or event.button == 1:
112 area.window.set_cursor(gtk.gdk.Cursor(gtk.gdk.FLEUR))
113 self.prevmousex = event.x
114 self.prevmousey = event.y
115 if event.type not in (gtk.gdk.BUTTON_PRESS, gtk.gdk.BUTTON_RELEASE):
116 return False
117 return False
118
119 def on_area_button_release(self, area, event):
120 if event.button == 2 or event.button == 1:
121 area.window.set_cursor(gtk.gdk.Cursor(gtk.gdk.ARROW))
122 self.prevmousex = None
123 self.prevmousey = None
124 return True
125 return False
126
127 def on_area_scroll_event(self, area, event):
128 if event.direction == gtk.gdk.SCROLL_UP:
129 self.zoom_image(self.zoom_ratio * self.ZOOM_INCREMENT)
130 return True
131 if event.direction == gtk.gdk.SCROLL_DOWN:
132 self.zoom_image(self.zoom_ratio / self.ZOOM_INCREMENT)
133 return True
134 return False
135
136 def on_area_motion_notify(self, area, event):
137 state = event.state
138 if state & gtk.gdk.BUTTON2_MASK or state & gtk.gdk.BUTTON1_MASK:
139 x, y = int(event.x), int(event.y)
140 # pan the image
141 self.x += (self.prevmousex - x)/self.zoom_ratio
142 self.y += (self.prevmousey - y)/self.zoom_ratio
143 self.queue_draw()
144 self.prevmousex = x
145 self.prevmousey = y
146 self.position_changed()
147 return True 167 return True
148 168 return False
149 def on_set_scroll_adjustments(self, area, hadj, vadj): 169
150 self._set_scroll_adjustments(hadj, vadj) 170 def on_area_motion_notify(self, area, event):
151 171 state = event.state
152 def on_allocation_size_changed(self, widget, allocation): 172 if state & gtk.gdk.BUTTON2_MASK or state & gtk.gdk.BUTTON1_MASK:
153 self.hadj.page_size = allocation.width 173 x, y = int(event.x), int(event.y)
154 self.hadj.page_increment = allocation.width * 0.9 174 # pan the image
155 self.vadj.page_size = allocation.height 175 self.x += (self.prevmousex - x)/self.zoom_ratio
156 self.vadj.page_increment = allocation.height * 0.9 176 self.y += (self.prevmousey - y)/self.zoom_ratio
157 177 self.queue_draw()
158 def _set_scroll_adjustments(self, hadj, vadj): 178 self.prevmousex = x
159 if hadj == None: 179 self.prevmousey = y
160 hadj = gtk.Adjustment(0.0, 0.0, 0.0, 0.0, 0.0, 0.0) 180 self.position_changed()
161 if vadj == None: 181 return True
162 vadj = gtk.Adjustment(0.0, 0.0, 0.0, 0.0, 0.0, 0.0) 182
163 183 def on_set_scroll_adjustments(self, area, hadj, vadj):
164 if self.hadj != None and hadj != self.hadj: 184 self._set_scroll_adjustments (hadj, vadj)
165 self.hadj.disconnect(self.hadj_changed_signal_id) 185
166 if self.vadj != None and vadj != self.vadj: 186 def on_allocation_size_changed(self, widget, allocation):
167 self.vadj.disconnect(self.vadj_changed_signal_id) 187 self.hadj.page_size = allocation.width
168 188 self.hadj.page_increment = allocation.width * 0.9
169 if hadj != None: 189 self.vadj.page_size = allocation.height
170 self.hadj = hadj 190 self.vadj.page_increment = allocation.height * 0.9
171 self._set_adj_upper(self.hadj, self.zoom_ratio * self.chart_width) 191
172 self.hadj_changed_signal_id = self.hadj.connect('value-changed', self.on_adjustments_changed) 192 def _set_adj_upper(self, adj, upper):
173 193 changed = False
174 if vadj != None: 194 value_changed = False
175 self.vadj = vadj 195
176 self._set_adj_upper(self.vadj, self.zoom_ratio * self.chart_height) 196 if adj.upper != upper:
177 self.vadj_changed_signal_id = self.vadj.connect('value-changed', self.on_adjustments_changed) 197 adj.upper = upper
178 198 changed = True
179 def _set_adj_upper(self, adj, upper): 199
180 changed = False 200 max_value = max(0.0, upper - adj.page_size)
181 value_changed = False 201 if adj.value > max_value:
182 202 adj.value = max_value
183 if adj.upper != upper: 203 value_changed = True
184 adj.upper = upper 204
185 changed = True 205 if changed:
186 206 adj.changed()
187 max_value = max(0.0, upper - adj.page_size) 207 if value_changed:
188 if adj.value > max_value: 208 adj.value_changed()
189 adj.value = max_value 209
190 value_changed = True 210 def _set_scroll_adjustments(self, hadj, vadj):
191 211 if hadj == None:
192 if changed: 212 hadj = gtk.Adjustment(0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
193 adj.changed() 213 if vadj == None:
194 if value_changed: 214 vadj = gtk.Adjustment(0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
195 adj.value_changed() 215
196 216 if self.hadj_changed_signal_id != None and \
197 def on_adjustments_changed(self, adj): 217 self.hadj != None and hadj != self.hadj:
198 self.x = self.hadj.value / self.zoom_ratio 218 self.hadj.disconnect (self.hadj_changed_signal_id)
199 self.y = self.vadj.value / self.zoom_ratio 219 if self.vadj_changed_signal_id != None and \
200 self.queue_draw() 220 self.vadj != None and vadj != self.vadj:
201 221 self.vadj.disconnect (self.vadj_changed_signal_id)
202 def on_position_changed(self, widget, x, y): 222
203 self.hadj.value = x * self.zoom_ratio 223 if hadj != None:
204 self.vadj.value = y * self.zoom_ratio 224 self.hadj = hadj
225 self._set_adj_upper (self.hadj, self.zoom_ratio * self.chart_width)
226 self.hadj_changed_signal_id = self.hadj.connect('value-changed', self.on_adjustments_changed)
227
228 if vadj != None:
229 self.vadj = vadj
230 self._set_adj_upper (self.vadj, self.zoom_ratio * self.chart_height)
231 self.vadj_changed_signal_id = self.vadj.connect('value-changed', self.on_adjustments_changed)
232
233 def on_adjustments_changed(self, adj):
234 self.x = self.hadj.value / self.zoom_ratio
235 self.y = self.vadj.value / self.zoom_ratio
236 self.queue_draw()
237
238 def on_position_changed(self, widget, x, y):
239 self.hadj.value = x * self.zoom_ratio
240 self.vadj.value = y * self.zoom_ratio
205 241
206PyBootchartWidget.set_set_scroll_adjustments_signal('set-scroll-adjustments') 242PyBootchartWidget.set_set_scroll_adjustments_signal('set-scroll-adjustments')
207 243
244class PyBootchartShell(gtk.VBox):
245 ui = '''
246 <ui>
247 <toolbar name="ToolBar">
248 <toolitem action="Expand"/>
249 <toolitem action="Contract"/>
250 <separator/>
251 <toolitem action="ZoomIn"/>
252 <toolitem action="ZoomOut"/>
253 <toolitem action="ZoomFit"/>
254 <toolitem action="Zoom100"/>
255 </toolbar>
256 </ui>
257 '''
258 def __init__(self, window, trace, options, xscale):
259 gtk.VBox.__init__(self)
260
261 self.widget = PyBootchartWidget(trace, options, xscale)
262
263 # Create a UIManager instance
264 uimanager = self.uimanager = gtk.UIManager()
265
266 # Add the accelerator group to the toplevel window
267 accelgroup = uimanager.get_accel_group()
268 window.add_accel_group(accelgroup)
269
270 # Create an ActionGroup
271 actiongroup = gtk.ActionGroup('Actions')
272 self.actiongroup = actiongroup
273
274 # Create actions
275 actiongroup.add_actions((
276 ('Expand', gtk.STOCK_ADD, None, None, None, self.widget.on_expand),
277 ('Contract', gtk.STOCK_REMOVE, None, None, None, self.widget.on_contract),
278 ('ZoomIn', gtk.STOCK_ZOOM_IN, None, None, None, self.widget.on_zoom_in),
279 ('ZoomOut', gtk.STOCK_ZOOM_OUT, None, None, None, self.widget.on_zoom_out),
280 ('ZoomFit', gtk.STOCK_ZOOM_FIT, 'Fit Width', None, None, self.widget.on_zoom_fit),
281 ('Zoom100', gtk.STOCK_ZOOM_100, None, None, None, self.widget.on_zoom_100),
282 ))
283
284 # Add the actiongroup to the uimanager
285 uimanager.insert_action_group(actiongroup, 0)
286
287 # Add a UI description
288 uimanager.add_ui_from_string(self.ui)
289
290 # Scrolled window
291 scrolled = gtk.ScrolledWindow()
292 scrolled.add(self.widget)
293
294 # toolbar / h-box
295 hbox = gtk.HBox(False, 8)
296
297 # Create a Toolbar
298 toolbar = uimanager.get_widget('/ToolBar')
299 hbox.pack_start(toolbar, True, True)
300
301 if not options.kernel_only:
302 # Misc. options
303 button = gtk.CheckButton("Show more")
304 button.connect ('toggled', self.widget.show_toggled)
305 hbox.pack_start (button, False, True)
306
307 self.pack_start(hbox, False)
308 self.pack_start(scrolled)
309 self.show_all()
310
311 def grab_focus(self, window):
312 window.set_focus(self.widget)
313
314
208class PyBootchartWindow(gtk.Window): 315class PyBootchartWindow(gtk.Window):
209 316
210 ui = ''' 317 def __init__(self, trace, app_options):
211 <ui> 318 gtk.Window.__init__(self)
212 <toolbar name="ToolBar"> 319
213 <toolitem action="ZoomIn"/> 320 window = self
214 <toolitem action="ZoomOut"/> 321 window.set_title("Bootchart %s" % trace.filename)
215 <toolitem action="ZoomFit"/> 322 window.set_default_size(750, 550)
216 <toolitem action="Zoom100"/> 323
217 </toolbar> 324 tab_page = gtk.Notebook()
218 </ui> 325 tab_page.show()
219 ''' 326 window.add(tab_page)
220 327
221 def __init__(self, res): 328 full_opts = RenderOptions(app_options)
222 gtk.Window.__init__(self) 329 full_tree = PyBootchartShell(window, trace, full_opts, 1.0)
223 330 tab_page.append_page (full_tree, gtk.Label("Full tree"))
224 window = self 331
225 window.set_title('Bootchart') 332 if trace.kernel is not None and len (trace.kernel) > 2:
226 window.set_default_size(512, 512) 333 kernel_opts = RenderOptions(app_options)
227 vbox = gtk.VBox() 334 kernel_opts.cumulative = False
228 window.add(vbox) 335 kernel_opts.charts = False
229 336 kernel_opts.kernel_only = True
230 self.widget = PyBootchartWidget(res) 337 kernel_tree = PyBootchartShell(window, trace, kernel_opts, 5.0)
231 338 tab_page.append_page (kernel_tree, gtk.Label("Kernel boot"))
232 # Create a UIManager instance 339
233 uimanager = self.uimanager = gtk.UIManager() 340 full_tree.grab_focus(self)
234 341 self.show()
235 # Add the accelerator group to the toplevel window 342
236 accelgroup = uimanager.get_accel_group() 343
237 window.add_accel_group(accelgroup) 344def show(trace, options):
238 345 win = PyBootchartWindow(trace, options)
239 # Create an ActionGroup 346 win.connect('destroy', gtk.main_quit)
240 actiongroup = gtk.ActionGroup('Actions') 347 gtk.main()
241 self.actiongroup = actiongroup
242
243 # Create actions
244 actiongroup.add_actions((
245 ('ZoomIn', gtk.STOCK_ZOOM_IN, None, None, None, self.widget.on_zoom_in),
246 ('ZoomOut', gtk.STOCK_ZOOM_OUT, None, None, None, self.widget.on_zoom_out),
247 ('ZoomFit', gtk.STOCK_ZOOM_FIT, 'Fit Width', None, None, self.widget.on_zoom_fit),
248 ('Zoom100', gtk.STOCK_ZOOM_100, None, None, None, self.widget.on_zoom_100),
249 ))
250
251 # Add the actiongroup to the uimanager
252 uimanager.insert_action_group(actiongroup, 0)
253
254 # Add a UI description
255 uimanager.add_ui_from_string(self.ui)
256
257 # Scrolled window
258 scrolled = gtk.ScrolledWindow()
259 scrolled.add(self.widget)
260
261 # Create a Toolbar
262 toolbar = uimanager.get_widget('/ToolBar')
263 vbox.pack_start(toolbar, False)
264 vbox.pack_start(scrolled)
265
266 self.set_focus(self.widget)
267
268 self.show_all()
269
270def show(res):
271 win = PyBootchartWindow(res)
272 win.connect('destroy', gtk.main_quit)
273 gtk.main()
diff --git a/scripts/pybootchartgui/pybootchartgui/main.py b/scripts/pybootchartgui/pybootchartgui/main.py
index e22636c23e..b45ae0a3d2 100644..120000
--- a/scripts/pybootchartgui/pybootchartgui/main.py
+++ b/scripts/pybootchartgui/pybootchartgui/main.py
@@ -1,78 +1 @@
1import sys main.py.in \ No newline at end of file
2import os
3import optparse
4
5import parsing
6import gui
7import batch
8
9def _mk_options_parser():
10 """Make an options parser."""
11 usage = "%prog [options] /path/to/tmp/buildstats/<recipe-machine>/<BUILDNAME>/"
12 version = "%prog v1.0.0"
13 parser = optparse.OptionParser(usage, version=version)
14 parser.add_option("-i", "--interactive", action="store_true", dest="interactive", default=False,
15 help="start in active mode")
16 parser.add_option("-f", "--format", dest="format", default="svg", choices=["svg", "pdf", "png"],
17 help="image format: svg, pdf, png, [default: %default]")
18 parser.add_option("-o", "--output", dest="output", metavar="PATH", default=None,
19 help="output path (file or directory) where charts are stored")
20 parser.add_option("-s", "--split", dest="num", type=int, default=1,
21 help="split the output chart into <NUM> charts, only works with \"-o PATH\"")
22 parser.add_option("-m", "--mintime", dest="mintime", type=int, default=8,
23 help="only tasks longer than this time will be displayed")
24 parser.add_option("-n", "--no-prune", action="store_false", dest="prune", default=True,
25 help="do not prune the process tree")
26 parser.add_option("-q", "--quiet", action="store_true", dest="quiet", default=False,
27 help="suppress informational messages")
28 parser.add_option("--very-quiet", action="store_true", dest="veryquiet", default=False,
29 help="suppress all messages except errors")
30 parser.add_option("--verbose", action="store_true", dest="verbose", default=False,
31 help="print all messages")
32 return parser
33
34def _get_filename(path):
35 """Construct a usable filename for outputs"""
36 dir = "."
37 file = "bootchart"
38 if os.path.isdir(path):
39 dir = path
40 elif path != None:
41 file = path
42 return os.path.join(dir, file)
43
44def main(argv=None):
45 try:
46 if argv is None:
47 argv = sys.argv[1:]
48
49 parser = _mk_options_parser()
50 options, args = parser.parse_args(argv)
51
52 if len(args) == 0:
53 parser.error("insufficient arguments, expected at least one path.")
54 return 2
55
56 res = parsing.parse(args, options.prune, options.mintime)
57 if options.interactive or options.output == None:
58 gui.show(res)
59 else:
60 filename = _get_filename(options.output)
61 res_list = parsing.split_res(res, options.num)
62 n = 1
63 for r in res_list:
64 if len(res_list) == 1:
65 f = filename + "." + options.format
66 else:
67 f = filename + "_" + str(n) + "." + options.format
68 n = n + 1
69 batch.render(r, options.format, f)
70 print "bootchart written to", f
71 return 0
72 except parsing.ParseError, ex:
73 print("Parse error: %s" % ex)
74 return 2
75
76
77if __name__ == '__main__':
78 sys.exit(main())
diff --git a/scripts/pybootchartgui/pybootchartgui/main.py.in b/scripts/pybootchartgui/pybootchartgui/main.py.in
new file mode 100644
index 0000000000..265cb18383
--- /dev/null
+++ b/scripts/pybootchartgui/pybootchartgui/main.py.in
@@ -0,0 +1,171 @@
1#
2# ***********************************************************************
3# Warning: This file is auto-generated from main.py.in - edit it there.
4# ***********************************************************************
5#
6# pybootchartgui is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10
11# pybootchartgui is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15
16# You should have received a copy of the GNU General Public License
17# along with pybootchartgui. If not, see <http://www.gnu.org/licenses/>.
18
19from __future__ import print_function
20
21import sys
22import os
23import optparse
24
25from . import parsing
26from . import batch
27
28def _mk_options_parser():
29 """Make an options parser."""
30 usage = "%prog [options] /path/to/tmp/buildstats/<recipe-machine>/<BUILDNAME>/"
31 version = "%prog v1.0.0"
32 parser = optparse.OptionParser(usage, version=version)
33 parser.add_option("-i", "--interactive", action="store_true", dest="interactive", default=False,
34 help="start in active mode")
35 parser.add_option("-f", "--format", dest="format", default="png", choices=["png", "svg", "pdf"],
36 help="image format (png, svg, pdf); default format png")
37 parser.add_option("-o", "--output", dest="output", metavar="PATH", default=None,
38 help="output path (file or directory) where charts are stored")
39 parser.add_option("-s", "--split", dest="num", type=int, default=1,
40 help="split the output chart into <NUM> charts, only works with \"-o PATH\"")
41 parser.add_option("-m", "--mintime", dest="mintime", type=int, default=8,
42 help="only tasks longer than this time will be displayed")
43 parser.add_option("-n", "--no-prune", action="store_false", dest="prune", default=True,
44 help="do not prune the process tree")
45 parser.add_option("-q", "--quiet", action="store_true", dest="quiet", default=False,
46 help="suppress informational messages")
47 parser.add_option("-t", "--boot-time", action="store_true", dest="boottime", default=False,
48 help="only display the boot time of the boot in text format (stdout)")
49 parser.add_option("--very-quiet", action="store_true", dest="veryquiet", default=False,
50 help="suppress all messages except errors")
51 parser.add_option("--verbose", action="store_true", dest="verbose", default=False,
52 help="print all messages")
53 parser.add_option("--profile", action="store_true", dest="profile", default=False,
54 help="profile rendering of chart (only useful when in batch mode indicated by -f)")
55 parser.add_option("--show-pid", action="store_true", dest="show_pid", default=False,
56 help="show process ids in the bootchart as 'processname [pid]'")
57 parser.add_option("--show-all", action="store_true", dest="show_all", default=False,
58 help="show all process information in the bootchart as '/process/path/exe [pid] [args]'")
59 parser.add_option("--crop-after", dest="crop_after", metavar="PROCESS", default=None,
60 help="crop chart when idle after PROCESS is started")
61 parser.add_option("--annotate", action="append", dest="annotate", metavar="PROCESS", default=None,
62 help="annotate position where PROCESS is started; can be specified multiple times. " +
63 "To create a single annotation when any one of a set of processes is started, use commas to separate the names")
64 parser.add_option("--annotate-file", dest="annotate_file", metavar="FILENAME", default=None,
65 help="filename to write annotation points to")
66 return parser
67
68class Writer:
69 def __init__(self, write, options):
70 self.write = write
71 self.options = options
72
73 def error(self, msg):
74 self.write(msg)
75
76 def warn(self, msg):
77 if not self.options.quiet:
78 self.write(msg)
79
80 def info(self, msg):
81 if self.options.verbose:
82 self.write(msg)
83
84 def status(self, msg):
85 if not self.options.quiet:
86 self.write(msg)
87
88def _mk_writer(options):
89 def write(s):
90 print(s)
91 return Writer(write, options)
92
93def _get_filename(path):
94 """Construct a usable filename for outputs"""
95 dname = "."
96 fname = "bootchart"
97 if path != None:
98 if os.path.isdir(path):
99 dname = path
100 else:
101 fname = path
102 return os.path.join(dname, fname)
103
104def main(argv=None):
105 try:
106 if argv is None:
107 argv = sys.argv[1:]
108
109 parser = _mk_options_parser()
110 options, args = parser.parse_args(argv)
111 writer = _mk_writer(options)
112
113 if len(args) == 0:
114 print("No path given, trying /var/log/bootchart.tgz")
115 args = [ "/var/log/bootchart.tgz" ]
116
117 res = parsing.Trace(writer, args, options)
118
119 if options.interactive or options.output == None:
120 from . import gui
121 gui.show(res, options)
122 elif options.boottime:
123 import math
124 proc_tree = res.proc_tree
125 if proc_tree.idle:
126 duration = proc_tree.idle
127 else:
128 duration = proc_tree.duration
129 dur = duration / 100.0
130 print('%02d:%05.2f' % (math.floor(dur/60), dur - 60 * math.floor(dur/60)))
131 else:
132 if options.annotate_file:
133 f = open (options.annotate_file, "w")
134 try:
135 for time in res[4]:
136 if time is not None:
137 # output as ms
138 print(time * 10, file=f)
139 else:
140 print(file=f)
141 finally:
142 f.close()
143 filename = _get_filename(options.output)
144 res_list = parsing.split_res(res, options.num)
145 n = 1
146 for r in res_list:
147 if len(res_list) == 1:
148 f = filename + "." + options.format
149 else:
150 f = filename + "_" + str(n) + "." + options.format
151 n = n + 1
152 def render():
153 batch.render(writer, r, options, f)
154 if options.profile:
155 import cProfile
156 import pstats
157 profile = '%s.prof' % os.path.splitext(filename)[0]
158 cProfile.runctx('render()', globals(), locals(), profile)
159 p = pstats.Stats(profile)
160 p.strip_dirs().sort_stats('time').print_stats(20)
161 else:
162 render()
163
164 return 0
165 except parsing.ParseError as ex:
166 print(("Parse error: %s" % ex))
167 return 2
168
169
170if __name__ == '__main__':
171 sys.exit(main())
diff --git a/scripts/pybootchartgui/pybootchartgui/parsing.py b/scripts/pybootchartgui/pybootchartgui/parsing.py
index 6343fd5a7b..c8a6a5721a 100644
--- a/scripts/pybootchartgui/pybootchartgui/parsing.py
+++ b/scripts/pybootchartgui/pybootchartgui/parsing.py
@@ -1,178 +1,638 @@
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
1from __future__ import with_statement 17from __future__ import with_statement
2 18
3import os 19import os
4import string 20import string
5import re 21import re
22import sys
6import tarfile 23import tarfile
24from time import clock
7from collections import defaultdict 25from collections import defaultdict
26from functools import reduce
27
28from .samples import *
29from .process_tree import ProcessTree
30
31if sys.version_info >= (3, 0):
32 long = int
33
34# Parsing produces as its end result a 'Trace'
35
36class Trace:
37 def __init__(self, writer, paths, options):
38 self.processes = {}
39 self.start = {}
40 self.end = {}
41 self.headers = None
42 self.disk_stats = None
43 self.ps_stats = None
44 self.taskstats = None
45 self.cpu_stats = None
46 self.cmdline = None
47 self.kernel = None
48 self.kernel_tree = None
49 self.filename = None
50 self.parent_map = None
51 self.mem_stats = None
52
53 parse_paths (writer, self, paths, options.mintime)
54 if not self.valid():
55 raise ParseError("empty state: '%s' does not contain a valid bootchart" % ", ".join(paths))
56
57 return
58
59 # Turn that parsed information into something more useful
60 # link processes into a tree of pointers, calculate statistics
61 self.compile(writer)
62
63 # Crop the chart to the end of the first idle period after the given
64 # process
65 if options.crop_after:
66 idle = self.crop (writer, options.crop_after)
67 else:
68 idle = None
69
70 # Annotate other times as the first start point of given process lists
71 self.times = [ idle ]
72 if options.annotate:
73 for procnames in options.annotate:
74 names = [x[:15] for x in procnames.split(",")]
75 for proc in self.ps_stats.process_map.values():
76 if proc.cmd in names:
77 self.times.append(proc.start_time)
78 break
79 else:
80 self.times.append(None)
81
82 self.proc_tree = ProcessTree(writer, self.kernel, self.ps_stats,
83 self.ps_stats.sample_period,
84 self.headers.get("profile.process"),
85 options.prune, idle, self.taskstats,
86 self.parent_map is not None)
87
88 if self.kernel is not None:
89 self.kernel_tree = ProcessTree(writer, self.kernel, None, 0,
90 self.headers.get("profile.process"),
91 False, None, None, True)
92
93 def valid(self):
94 return len(self.processes) != 0
95 return self.headers != None and self.disk_stats != None and \
96 self.ps_stats != None and self.cpu_stats != None
97
98
99 def compile(self, writer):
100
101 def find_parent_id_for(pid):
102 if pid is 0:
103 return 0
104 ppid = self.parent_map.get(pid)
105 if ppid:
106 # many of these double forks are so short lived
107 # that we have no samples, or process info for them
108 # so climb the parent hierarcy to find one
109 if int (ppid * 1000) not in self.ps_stats.process_map:
110# print "Pid '%d' short lived with no process" % ppid
111 ppid = find_parent_id_for (ppid)
112# else:
113# print "Pid '%d' has an entry" % ppid
114 else:
115# print "Pid '%d' missing from pid map" % pid
116 return 0
117 return ppid
118
119 # merge in the cmdline data
120 if self.cmdline is not None:
121 for proc in self.ps_stats.process_map.values():
122 rpid = int (proc.pid // 1000)
123 if rpid in self.cmdline:
124 cmd = self.cmdline[rpid]
125 proc.exe = cmd['exe']
126 proc.args = cmd['args']
127# else:
128# print "proc %d '%s' not in cmdline" % (rpid, proc.exe)
129
130 # re-parent any stray orphans if we can
131 if self.parent_map is not None:
132 for process in self.ps_stats.process_map.values():
133 ppid = find_parent_id_for (int(process.pid // 1000))
134 if ppid:
135 process.ppid = ppid * 1000
136
137 # stitch the tree together with pointers
138 for process in self.ps_stats.process_map.values():
139 process.set_parent (self.ps_stats.process_map)
140
141 # count on fingers variously
142 for process in self.ps_stats.process_map.values():
143 process.calc_stats (self.ps_stats.sample_period)
144
145 def crop(self, writer, crop_after):
146
147 def is_idle_at(util, start, j):
148 k = j + 1
149 while k < len(util) and util[k][0] < start + 300:
150 k += 1
151 k = min(k, len(util)-1)
152
153 if util[j][1] >= 0.25:
154 return False
155
156 avgload = sum(u[1] for u in util[j:k+1]) / (k-j+1)
157 if avgload < 0.25:
158 return True
159 else:
160 return False
161 def is_idle(util, start):
162 for j in range(0, len(util)):
163 if util[j][0] < start:
164 continue
165 return is_idle_at(util, start, j)
166 else:
167 return False
168
169 names = [x[:15] for x in crop_after.split(",")]
170 for proc in self.ps_stats.process_map.values():
171 if proc.cmd in names or proc.exe in names:
172 writer.info("selected proc '%s' from list (start %d)"
173 % (proc.cmd, proc.start_time))
174 break
175 if proc is None:
176 writer.warn("no selected crop proc '%s' in list" % crop_after)
177
178
179 cpu_util = [(sample.time, sample.user + sample.sys + sample.io) for sample in self.cpu_stats]
180 disk_util = [(sample.time, sample.util) for sample in self.disk_stats]
181
182 idle = None
183 for i in range(0, len(cpu_util)):
184 if cpu_util[i][0] < proc.start_time:
185 continue
186 if is_idle_at(cpu_util, cpu_util[i][0], i) \
187 and is_idle(disk_util, cpu_util[i][0]):
188 idle = cpu_util[i][0]
189 break
190
191 if idle is None:
192 writer.warn ("not idle after proc '%s'" % crop_after)
193 return None
194
195 crop_at = idle + 300
196 writer.info ("cropping at time %d" % crop_at)
197 while len (self.cpu_stats) \
198 and self.cpu_stats[-1].time > crop_at:
199 self.cpu_stats.pop()
200 while len (self.disk_stats) \
201 and self.disk_stats[-1].time > crop_at:
202 self.disk_stats.pop()
203
204 self.ps_stats.end_time = crop_at
205
206 cropped_map = {}
207 for key, value in self.ps_stats.process_map.items():
208 if (value.start_time <= crop_at):
209 cropped_map[key] = value
210
211 for proc in cropped_map.values():
212 proc.duration = min (proc.duration, crop_at - proc.start_time)
213 while len (proc.samples) \
214 and proc.samples[-1].time > crop_at:
215 proc.samples.pop()
216
217 self.ps_stats.process_map = cropped_map
218
219 return idle
220
8 221
9from samples import *
10from process_tree import ProcessTree
11 222
12class ParseError(Exception): 223class ParseError(Exception):
13 """Represents errors during parse of the bootchart.""" 224 """Represents errors during parse of the bootchart."""
14 def __init__(self, value): 225 def __init__(self, value):
15 self.value = value 226 self.value = value
16 227
17 def __str__(self): 228 def __str__(self):
18 return self.value 229 return self.value
19 230
20def _parse_headers(file): 231def _parse_headers(file):
21 """Parses the headers of the bootchart.""" 232 """Parses the headers of the bootchart."""
22 def parse((headers,last), line): 233 def parse(acc, line):
23 if '=' in line: last,value = map(string.strip, line.split('=', 1)) 234 (headers, last) = acc
24 else: value = line.strip() 235 if '=' in line:
25 headers[last] += value 236 last, value = map (lambda x: x.strip(), line.split('=', 1))
26 return headers,last 237 else:
27 return reduce(parse, file.read().split('\n'), (defaultdict(str),''))[0] 238 value = line.strip()
239 headers[last] += value
240 return headers, last
241 return reduce(parse, file.read().decode('utf-8').split('\n'), (defaultdict(str),''))[0]
28 242
29def _parse_timed_blocks(file): 243def _parse_timed_blocks(file):
30 """Parses (ie., splits) a file into so-called timed-blocks. A 244 """Parses (ie., splits) a file into so-called timed-blocks. A
31 timed-block consists of a timestamp on a line by itself followed 245 timed-block consists of a timestamp on a line by itself followed
32 by zero or more lines of data for that point in time.""" 246 by zero or more lines of data for that point in time."""
33 def parse(block): 247 def parse(block):
34 lines = block.split('\n') 248 lines = block.split('\n')
35 if not lines: 249 if not lines:
36 raise ParseError('expected a timed-block consisting a timestamp followed by data lines') 250 raise ParseError('expected a timed-block consisting a timestamp followed by data lines')
37 try: 251 try:
38 return (int(lines[0]), lines[1:]) 252 return (int(lines[0]), lines[1:])
39 except ValueError: 253 except ValueError:
40 raise ParseError("expected a timed-block, but timestamp '%s' is not an integer" % lines[0]) 254 raise ParseError("expected a timed-block, but timestamp '%s' is not an integer" % lines[0])
41 blocks = file.read().split('\n\n') 255 blocks = file.read().decode('utf-8').split('\n\n')
42 return [parse(block) for block in blocks if block.strip()] 256 return [parse(block) for block in blocks if block.strip() and not block.endswith(' not running\n')]
43 257
44def _parse_proc_ps_log(file): 258def _parse_proc_ps_log(writer, file):
45 """ 259 """
46 * See proc(5) for details. 260 * See proc(5) for details.
47 * 261 *
48 * {pid, comm, state, ppid, pgrp, session, tty_nr, tpgid, flags, minflt, cminflt, majflt, cmajflt, utime, stime, 262 * {pid, comm, state, ppid, pgrp, session, tty_nr, tpgid, flags, minflt, cminflt, majflt, cmajflt, utime, stime,
49 * cutime, cstime, priority, nice, 0, itrealvalue, starttime, vsize, rss, rlim, startcode, endcode, startstack, 263 * cutime, cstime, priority, nice, 0, itrealvalue, starttime, vsize, rss, rlim, startcode, endcode, startstack,
50 * kstkesp, kstkeip} 264 * kstkesp, kstkeip}
51 """ 265 """
52 processMap = {} 266 processMap = {}
53 ltime = 0 267 ltime = 0
54 timed_blocks = _parse_timed_blocks(file) 268 timed_blocks = _parse_timed_blocks(file)
55 for time, lines in timed_blocks: 269 for time, lines in timed_blocks:
56 for line in lines: 270 for line in lines:
57 tokens = line.split(' ') 271 if not line: continue
58 272 tokens = line.split(' ')
59 offset = [index for index, token in enumerate(tokens[1:]) if token.endswith(')')][0] 273 if len(tokens) < 21:
60 pid, cmd, state, ppid = int(tokens[0]), ' '.join(tokens[1:2+offset]), tokens[2+offset], int(tokens[3+offset]) 274 continue
61 userCpu, sysCpu, stime= int(tokens[13+offset]), int(tokens[14+offset]), int(tokens[21+offset]) 275
62 276 offset = [index for index, token in enumerate(tokens[1:]) if token[-1] == ')'][0]
63 if processMap.has_key(pid): 277 pid, cmd, state, ppid = int(tokens[0]), ' '.join(tokens[1:2+offset]), tokens[2+offset], int(tokens[3+offset])
64 process = processMap[pid] 278 userCpu, sysCpu, stime = int(tokens[13+offset]), int(tokens[14+offset]), int(tokens[21+offset])
65 process.cmd = cmd.replace('(', '').replace(')', '') # why rename after latest name?? 279
66 else: 280 # magic fixed point-ness ...
67 process = Process(pid, cmd, ppid, min(time, stime)) 281 pid *= 1000
68 processMap[pid] = process 282 ppid *= 1000
69 283 if pid in processMap:
70 if process.last_user_cpu_time is not None and process.last_sys_cpu_time is not None and ltime is not None: 284 process = processMap[pid]
71 userCpuLoad, sysCpuLoad = process.calc_load(userCpu, sysCpu, time - ltime) 285 process.cmd = cmd.strip('()') # why rename after latest name??
72 cpuSample = CPUSample('null', userCpuLoad, sysCpuLoad, 0.0) 286 else:
73 process.samples.append(ProcessSample(time, state, cpuSample)) 287 process = Process(writer, pid, cmd.strip('()'), ppid, min(time, stime))
74 288 processMap[pid] = process
75 process.last_user_cpu_time = userCpu 289
76 process.last_sys_cpu_time = sysCpu 290 if process.last_user_cpu_time is not None and process.last_sys_cpu_time is not None and ltime is not None:
77 ltime = time 291 userCpuLoad, sysCpuLoad = process.calc_load(userCpu, sysCpu, max(1, time - ltime))
78 292 cpuSample = CPUSample('null', userCpuLoad, sysCpuLoad, 0.0)
79 startTime = timed_blocks[0][0] 293 process.samples.append(ProcessSample(time, state, cpuSample))
80 avgSampleLength = (ltime - startTime)/(len(timed_blocks)-1) 294
81 295 process.last_user_cpu_time = userCpu
82 for process in processMap.values(): 296 process.last_sys_cpu_time = sysCpu
83 process.set_parent(processMap) 297 ltime = time
84 298
85 for process in processMap.values(): 299 if len (timed_blocks) < 2:
86 process.calc_stats(avgSampleLength) 300 return None
87 301
88 return ProcessStats(processMap.values(), avgSampleLength, startTime, ltime) 302 startTime = timed_blocks[0][0]
89 303 avgSampleLength = (ltime - startTime)/(len (timed_blocks) - 1)
304
305 return ProcessStats (writer, processMap, len (timed_blocks), avgSampleLength, startTime, ltime)
306
307def _parse_taskstats_log(writer, file):
308 """
309 * See bootchart-collector.c for details.
310 *
311 * { pid, ppid, comm, cpu_run_real_total, blkio_delay_total, swapin_delay_total }
312 *
313 """
314 processMap = {}
315 pidRewrites = {}
316 ltime = None
317 timed_blocks = _parse_timed_blocks(file)
318 for time, lines in timed_blocks:
319 # we have no 'stime' from taskstats, so prep 'init'
320 if ltime is None:
321 process = Process(writer, 1, '[init]', 0, 0)
322 processMap[1000] = process
323 ltime = time
324# continue
325 for line in lines:
326 if not line: continue
327 tokens = line.split(' ')
328 if len(tokens) != 6:
329 continue
330
331 opid, ppid, cmd = int(tokens[0]), int(tokens[1]), tokens[2]
332 cpu_ns, blkio_delay_ns, swapin_delay_ns = long(tokens[-3]), long(tokens[-2]), long(tokens[-1]),
333
334 # make space for trees of pids
335 opid *= 1000
336 ppid *= 1000
337
338 # when the process name changes, we re-write the pid.
339 if opid in pidRewrites:
340 pid = pidRewrites[opid]
341 else:
342 pid = opid
343
344 cmd = cmd.strip('(').strip(')')
345 if pid in processMap:
346 process = processMap[pid]
347 if process.cmd != cmd:
348 pid += 1
349 pidRewrites[opid] = pid
350# print "process mutation ! '%s' vs '%s' pid %s -> pid %s\n" % (process.cmd, cmd, opid, pid)
351 process = process.split (writer, pid, cmd, ppid, time)
352 processMap[pid] = process
353 else:
354 process.cmd = cmd;
355 else:
356 process = Process(writer, pid, cmd, ppid, time)
357 processMap[pid] = process
358
359 delta_cpu_ns = (float) (cpu_ns - process.last_cpu_ns)
360 delta_blkio_delay_ns = (float) (blkio_delay_ns - process.last_blkio_delay_ns)
361 delta_swapin_delay_ns = (float) (swapin_delay_ns - process.last_swapin_delay_ns)
362
363 # make up some state data ...
364 if delta_cpu_ns > 0:
365 state = "R"
366 elif delta_blkio_delay_ns + delta_swapin_delay_ns > 0:
367 state = "D"
368 else:
369 state = "S"
370
371 # retain the ns timing information into a CPUSample - that tries
372 # with the old-style to be a %age of CPU used in this time-slice.
373 if delta_cpu_ns + delta_blkio_delay_ns + delta_swapin_delay_ns > 0:
374# print "proc %s cpu_ns %g delta_cpu %g" % (cmd, cpu_ns, delta_cpu_ns)
375 cpuSample = CPUSample('null', delta_cpu_ns, 0.0,
376 delta_blkio_delay_ns,
377 delta_swapin_delay_ns)
378 process.samples.append(ProcessSample(time, state, cpuSample))
379
380 process.last_cpu_ns = cpu_ns
381 process.last_blkio_delay_ns = blkio_delay_ns
382 process.last_swapin_delay_ns = swapin_delay_ns
383 ltime = time
384
385 if len (timed_blocks) < 2:
386 return None
387
388 startTime = timed_blocks[0][0]
389 avgSampleLength = (ltime - startTime)/(len(timed_blocks)-1)
390
391 return ProcessStats (writer, processMap, len (timed_blocks), avgSampleLength, startTime, ltime)
392
90def _parse_proc_stat_log(file): 393def _parse_proc_stat_log(file):
91 samples = [] 394 samples = []
92 ltimes = None 395 ltimes = None
93 for time, lines in _parse_timed_blocks(file): 396 for time, lines in _parse_timed_blocks(file):
94 # CPU times {user, nice, system, idle, io_wait, irq, softirq} 397 # skip emtpy lines
95 tokens = lines[0].split(); 398 if not lines:
96 times = [ int(token) for token in tokens[1:] ] 399 continue
97 if ltimes: 400 # CPU times {user, nice, system, idle, io_wait, irq, softirq}
98 user = float((times[0] + times[1]) - (ltimes[0] + ltimes[1])) 401 tokens = lines[0].split()
99 system = float((times[2] + times[5] + times[6]) - (ltimes[2] + ltimes[5] + ltimes[6])) 402 times = [ int(token) for token in tokens[1:] ]
100 idle = float(times[3] - ltimes[3]) 403 if ltimes:
101 iowait = float(times[4] - ltimes[4]) 404 user = float((times[0] + times[1]) - (ltimes[0] + ltimes[1]))
102 405 system = float((times[2] + times[5] + times[6]) - (ltimes[2] + ltimes[5] + ltimes[6]))
103 aSum = max(user + system + idle + iowait, 1) 406 idle = float(times[3] - ltimes[3])
104 samples.append( CPUSample(time, user/aSum, system/aSum, iowait/aSum) ) 407 iowait = float(times[4] - ltimes[4])
105 408
106 ltimes = times 409 aSum = max(user + system + idle + iowait, 1)
107 # skip the rest of statistics lines 410 samples.append( CPUSample(time, user/aSum, system/aSum, iowait/aSum) )
108 return samples 411
109 412 ltimes = times
110 413 # skip the rest of statistics lines
414 return samples
415
111def _parse_proc_disk_stat_log(file, numCpu): 416def _parse_proc_disk_stat_log(file, numCpu):
112 """ 417 """
113 Parse file for disk stats, but only look at the whole disks, eg. sda, 418 Parse file for disk stats, but only look at the whole device, eg. sda,
114 not sda1, sda2 etc. The format of relevant lines should be: 419 not sda1, sda2 etc. The format of relevant lines should be:
115 {major minor name rio rmerge rsect ruse wio wmerge wsect wuse running use aveq} 420 {major minor name rio rmerge rsect ruse wio wmerge wsect wuse running use aveq}
116 """ 421 """
117 DISK_REGEX = 'hd.$|sd.$' 422 disk_regex_re = re.compile ('^([hsv]d.|mtdblock\d|mmcblk\d|cciss/c\d+d\d+.*)$')
118 423
119 def is_relevant_line(line): 424 # this gets called an awful lot.
120 return len(line.split()) == 14 and re.match(DISK_REGEX, line.split()[2]) 425 def is_relevant_line(linetokens):
121 426 if len(linetokens) != 14:
122 disk_stat_samples = [] 427 return False
123 428 disk = linetokens[2]
124 for time, lines in _parse_timed_blocks(file): 429 return disk_regex_re.match(disk)
125 sample = DiskStatSample(time) 430
126 relevant_tokens = [line.split() for line in lines if is_relevant_line(line)] 431 disk_stat_samples = []
127 432
128 for tokens in relevant_tokens: 433 for time, lines in _parse_timed_blocks(file):
129 disk, rsect, wsect, use = tokens[2], int(tokens[5]), int(tokens[9]), int(tokens[12]) 434 sample = DiskStatSample(time)
130 sample.add_diskdata([rsect, wsect, use]) 435 relevant_tokens = [linetokens for linetokens in map (lambda x: x.split(),lines) if is_relevant_line(linetokens)]
131 436
132 disk_stat_samples.append(sample) 437 for tokens in relevant_tokens:
133 438 disk, rsect, wsect, use = tokens[2], int(tokens[5]), int(tokens[9]), int(tokens[12])
134 disk_stats = [] 439 sample.add_diskdata([rsect, wsect, use])
135 for sample1, sample2 in zip(disk_stat_samples[:-1], disk_stat_samples[1:]): 440
136 interval = sample1.time - sample2.time 441 disk_stat_samples.append(sample)
137 sums = [ a - b for a, b in zip(sample1.diskdata, sample2.diskdata) ] 442
138 readTput = sums[0] / 2.0 * 100.0 / interval 443 disk_stats = []
139 writeTput = sums[1] / 2.0 * 100.0 / interval 444 for sample1, sample2 in zip(disk_stat_samples[:-1], disk_stat_samples[1:]):
140 util = float( sums[2] ) / 10 / interval / numCpu 445 interval = sample1.time - sample2.time
141 util = max(0.0, min(1.0, util)) 446 if interval == 0:
142 disk_stats.append(DiskSample(sample2.time, readTput, writeTput, util)) 447 interval = 1
143 448 sums = [ a - b for a, b in zip(sample1.diskdata, sample2.diskdata) ]
144 return disk_stats 449 readTput = sums[0] / 2.0 * 100.0 / interval
145 450 writeTput = sums[1] / 2.0 * 100.0 / interval
146 451 util = float( sums[2] ) / 10 / interval / numCpu
452 util = max(0.0, min(1.0, util))
453 disk_stats.append(DiskSample(sample2.time, readTput, writeTput, util))
454
455 return disk_stats
456
457def _parse_proc_meminfo_log(file):
458 """
459 Parse file for global memory statistics.
460 The format of relevant lines should be: ^key: value( unit)?
461 """
462 used_values = ('MemTotal', 'MemFree', 'Buffers', 'Cached', 'SwapTotal', 'SwapFree',)
463
464 mem_stats = []
465 meminfo_re = re.compile(r'([^ \t:]+):\s*(\d+).*')
466
467 for time, lines in _parse_timed_blocks(file):
468 sample = MemSample(time)
469
470 for line in lines:
471 match = meminfo_re.match(line)
472 if not match:
473 raise ParseError("Invalid meminfo line \"%s\"" % match.groups(0))
474 sample.add_value(match.group(1), int(match.group(2)))
475
476 if sample.valid():
477 mem_stats.append(sample)
478
479 return mem_stats
480
481# if we boot the kernel with: initcall_debug printk.time=1 we can
482# get all manner of interesting data from the dmesg output
483# We turn this into a pseudo-process tree: each event is
484# characterised by a
485# we don't try to detect a "kernel finished" state - since the kernel
486# continues to do interesting things after init is called.
487#
488# sample input:
489# [ 0.000000] ACPI: FACP 3f4fc000 000F4 (v04 INTEL Napa 00000001 MSFT 01000013)
490# ...
491# [ 0.039993] calling migration_init+0x0/0x6b @ 1
492# [ 0.039993] initcall migration_init+0x0/0x6b returned 1 after 0 usecs
493def _parse_dmesg(writer, file):
494 timestamp_re = re.compile ("^\[\s*(\d+\.\d+)\s*]\s+(.*)$")
495 split_re = re.compile ("^(\S+)\s+([\S\+_-]+) (.*)$")
496 processMap = {}
497 idx = 0
498 inc = 1.0 / 1000000
499 kernel = Process(writer, idx, "k-boot", 0, 0.1)
500 processMap['k-boot'] = kernel
501 base_ts = False
502 max_ts = 0
503 for line in file.read().decode('utf-8').split('\n'):
504 t = timestamp_re.match (line)
505 if t is None:
506# print "duff timestamp " + line
507 continue
508
509 time_ms = float (t.group(1)) * 1000
510 # looks like we may have a huge diff after the clock
511 # has been set up. This could lead to huge graph:
512 # so huge we will be killed by the OOM.
513 # So instead of using the plain timestamp we will
514 # use a delta to first one and skip the first one
515 # for convenience
516 if max_ts == 0 and not base_ts and time_ms > 1000:
517 base_ts = time_ms
518 continue
519 max_ts = max(time_ms, max_ts)
520 if base_ts:
521# print "fscked clock: used %f instead of %f" % (time_ms - base_ts, time_ms)
522 time_ms -= base_ts
523 m = split_re.match (t.group(2))
524
525 if m is None:
526 continue
527# print "match: '%s'" % (m.group(1))
528 type = m.group(1)
529 func = m.group(2)
530 rest = m.group(3)
531
532 if t.group(2).startswith ('Write protecting the') or \
533 t.group(2).startswith ('Freeing unused kernel memory'):
534 kernel.duration = time_ms / 10
535 continue
536
537# print "foo: '%s' '%s' '%s'" % (type, func, rest)
538 if type == "calling":
539 ppid = kernel.pid
540 p = re.match ("\@ (\d+)", rest)
541 if p is not None:
542 ppid = float (p.group(1)) // 1000
543# print "match: '%s' ('%g') at '%s'" % (func, ppid, time_ms)
544 name = func.split ('+', 1) [0]
545 idx += inc
546 processMap[func] = Process(writer, ppid + idx, name, ppid, time_ms / 10)
547 elif type == "initcall":
548# print "finished: '%s' at '%s'" % (func, time_ms)
549 if func in processMap:
550 process = processMap[func]
551 process.duration = (time_ms / 10) - process.start_time
552 else:
553 print("corrupted init call for %s" % (func))
554
555 elif type == "async_waiting" or type == "async_continuing":
556 continue # ignore
557
558 return processMap.values()
559
560#
561# Parse binary pacct accounting file output if we have one
562# cf. /usr/include/linux/acct.h
563#
564def _parse_pacct(writer, file):
565 # read LE int32
566 def _read_le_int32(file):
567 byts = file.read(4)
568 return (ord(byts[0])) | (ord(byts[1]) << 8) | \
569 (ord(byts[2]) << 16) | (ord(byts[3]) << 24)
570
571 parent_map = {}
572 parent_map[0] = 0
573 while file.read(1) != "": # ignore flags
574 ver = file.read(1)
575 if ord(ver) < 3:
576 print("Invalid version 0x%x" % (ord(ver)))
577 return None
578
579 file.seek (14, 1) # user, group etc.
580 pid = _read_le_int32 (file)
581 ppid = _read_le_int32 (file)
582# print "Parent of %d is %d" % (pid, ppid)
583 parent_map[pid] = ppid
584 file.seek (4 + 4 + 16, 1) # timings
585 file.seek (16, 1) # acct_comm
586 return parent_map
587
588def _parse_paternity_log(writer, file):
589 parent_map = {}
590 parent_map[0] = 0
591 for line in file.read().decode('utf-8').split('\n'):
592 if not line:
593 continue
594 elems = line.split(' ') # <Child> <Parent>
595 if len (elems) >= 2:
596# print "paternity of %d is %d" % (int(elems[0]), int(elems[1]))
597 parent_map[int(elems[0])] = int(elems[1])
598 else:
599 print("Odd paternity line '%s'" % (line))
600 return parent_map
601
602def _parse_cmdline_log(writer, file):
603 cmdLines = {}
604 for block in file.read().decode('utf-8').split('\n\n'):
605 lines = block.split('\n')
606 if len (lines) >= 3:
607# print "Lines '%s'" % (lines[0])
608 pid = int (lines[0])
609 values = {}
610 values['exe'] = lines[1].lstrip(':')
611 args = lines[2].lstrip(':').split('\0')
612 args.pop()
613 values['args'] = args
614 cmdLines[pid] = values
615 return cmdLines
616
147def get_num_cpus(headers): 617def get_num_cpus(headers):
148 """Get the number of CPUs from the system.cpu header property. As the 618 """Get the number of CPUs from the system.cpu header property. As the
149 CPU utilization graphs are relative, the number of CPUs currently makes 619 CPU utilization graphs are relative, the number of CPUs currently makes
150 no difference.""" 620 no difference."""
151 if headers is None: 621 if headers is None:
152 return 1 622 return 1
623 if headers.get("system.cpu.num"):
624 return max (int (headers.get("system.cpu.num")), 1)
153 cpu_model = headers.get("system.cpu") 625 cpu_model = headers.get("system.cpu")
154 if cpu_model is None: 626 if cpu_model is None:
155 return 1 627 return 1
156 mat = re.match(".*\\((\\d+)\\)", cpu_model) 628 mat = re.match(".*\\((\\d+)\\)", cpu_model)
157 if mat is None: 629 if mat is None:
158 return 1 630 return 1
159 return int(mat.group(1)) 631 return max (int(mat.group(1)), 1)
160
161class ParserState:
162 def __init__(self):
163 self.processes = {}
164 self.start = {}
165 self.end = {}
166 632
167 def valid(self): 633def _do_parse(writer, state, filename, file, mintime):
168 return len(self.processes) != 0 634 writer.info("parsing '%s'" % filename)
169 635 t1 = clock()
170
171_relevant_files = set(["header", "proc_diskstats.log", "proc_ps.log", "proc_stat.log"])
172
173def _do_parse(state, filename, file, mintime):
174 #print filename
175 #writer.status("parsing '%s'" % filename)
176 paths = filename.split("/") 636 paths = filename.split("/")
177 task = paths[-1] 637 task = paths[-1]
178 pn = paths[-2] 638 pn = paths[-2]
@@ -194,44 +654,49 @@ def _do_parse(state, filename, file, mintime):
194 state.end[end] = [] 654 state.end[end] = []
195 if k not in state.end[end]: 655 if k not in state.end[end]:
196 state.end[end].append(pn + ":" + task) 656 state.end[end].append(pn + ":" + task)
657 t2 = clock()
658 writer.info(" %s seconds" % str(t2-t1))
197 return state 659 return state
198 660
199def parse_file(state, filename, mintime): 661def parse_file(writer, state, filename, mintime):
662 if state.filename is None:
663 state.filename = filename
200 basename = os.path.basename(filename) 664 basename = os.path.basename(filename)
201 with open(filename, "rb") as file: 665 with open(filename, "rb") as file:
202 return _do_parse(state, filename, file, mintime) 666 return _do_parse(writer, state, filename, file, mintime)
203 667
204def parse_paths(state, paths, mintime): 668def parse_paths(writer, state, paths, mintime):
205 for path in paths: 669 for path in paths:
206 root,extension = os.path.splitext(path) 670 if state.filename is None:
671 state.filename = path
672 root, extension = os.path.splitext(path)
207 if not(os.path.exists(path)): 673 if not(os.path.exists(path)):
208 print "warning: path '%s' does not exist, ignoring." % path 674 writer.warn("warning: path '%s' does not exist, ignoring." % path)
209 continue 675 continue
676 #state.filename = path
210 if os.path.isdir(path): 677 if os.path.isdir(path):
211 files = [ f for f in [os.path.join(path, f) for f in os.listdir(path)] ] 678 files = [ f for f in [os.path.join(path, f) for f in os.listdir(path)] ]
212 files.sort() 679 files.sort()
213 state = parse_paths(state, files, mintime) 680 state = parse_paths(writer, state, files, mintime)
214 elif extension in [".tar", ".tgz", ".tar.gz"]: 681 elif extension in [".tar", ".tgz", ".gz"]:
682 if extension == ".gz":
683 root, extension = os.path.splitext(root)
684 if extension != ".tar":
685 writer.warn("warning: can only handle zipped tar files, not zipped '%s'-files; ignoring" % extension)
686 continue
215 tf = None 687 tf = None
216 try: 688 try:
689 writer.status("parsing '%s'" % path)
217 tf = tarfile.open(path, 'r:*') 690 tf = tarfile.open(path, 'r:*')
218 for name in tf.getnames(): 691 for name in tf.getnames():
219 state = _do_parse(state, name, tf.extractfile(name)) 692 state = _do_parse(writer, state, name, tf.extractfile(name))
220 except tarfile.ReadError, error: 693 except tarfile.ReadError as error:
221 raise ParseError("error: could not read tarfile '%s': %s." % (path, error)) 694 raise ParseError("error: could not read tarfile '%s': %s." % (path, error))
222 finally: 695 finally:
223 if tf != None: 696 if tf != None:
224 tf.close() 697 tf.close()
225 else: 698 else:
226 state = parse_file(state, path, mintime) 699 state = parse_file(writer, state, path, mintime)
227 return state
228
229def parse(paths, prune, mintime):
230 state = parse_paths(ParserState(), paths, mintime)
231 if not state.valid():
232 raise ParseError("empty state: '%s' does not contain a valid bootchart" % ", ".join(paths))
233 #monitored_app = state.headers.get("profile.process")
234 #proc_tree = ProcessTree(state.ps_stats, monitored_app, prune)
235 return state 700 return state
236 701
237def split_res(res, n): 702def split_res(res, n):
diff --git a/scripts/pybootchartgui/pybootchartgui/process_tree.py b/scripts/pybootchartgui/pybootchartgui/process_tree.py
index bde29ebda8..cf88110b1c 100644
--- a/scripts/pybootchartgui/pybootchartgui/process_tree.py
+++ b/scripts/pybootchartgui/pybootchartgui/process_tree.py
@@ -1,3 +1,18 @@
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
1class ProcessTree: 16class ProcessTree:
2 """ProcessTree encapsulates a process tree. The tree is built from log files 17 """ProcessTree encapsulates a process tree. The tree is built from log files
3 retrieved during the boot process. When building the process tree, it is 18 retrieved during the boot process. When building the process tree, it is
@@ -19,55 +34,61 @@ class ProcessTree:
19 are merged together. 34 are merged together.
20 35
21 """ 36 """
22 LOGGER_PROC = 'bootchartd' 37 LOGGER_PROC = 'bootchart-colle'
23 EXPLODER_PROCESSES = set(['hwup']) 38 EXPLODER_PROCESSES = set(['hwup'])
24 39
25 def __init__(self, psstats, monitoredApp, prune, for_testing = False): 40 def __init__(self, writer, kernel, psstats, sample_period,
41 monitoredApp, prune, idle, taskstats,
42 accurate_parentage, for_testing = False):
43 self.writer = writer
26 self.process_tree = [] 44 self.process_tree = []
27 self.psstats = psstats 45 self.taskstats = taskstats
28 self.process_list = sorted(psstats.process_list, key = lambda p: p.pid) 46 if psstats is None:
29 self.sample_period = psstats.sample_period 47 process_list = kernel
30 48 elif kernel is None:
31 self.build() 49 process_list = psstats.process_map.values()
32 self.update_ppids_for_daemons(self.process_list) 50 else:
51 process_list = list(kernel) + list(psstats.process_map.values())
52 self.process_list = sorted(process_list, key = lambda p: p.pid)
53 self.sample_period = sample_period
54
55 self.build()
56 if not accurate_parentage:
57 self.update_ppids_for_daemons(self.process_list)
33 58
34 self.start_time = self.get_start_time(self.process_tree) 59 self.start_time = self.get_start_time(self.process_tree)
35 self.end_time = self.get_end_time(self.process_tree) 60 self.end_time = self.get_end_time(self.process_tree)
36 self.duration = self.end_time - self.start_time 61 self.duration = self.end_time - self.start_time
62 self.idle = idle
37 63
38 if for_testing: 64 if for_testing:
39 return 65 return
40 66
41 # print 'proc_tree before prune: num_proc=%i, duration=%i' % (self.num_nodes(self.process_list), self.duration) 67 removed = self.merge_logger(self.process_tree, self.LOGGER_PROC, monitoredApp, False)
42 68 writer.status("merged %i logger processes" % removed)
43 removed = self.merge_logger(self.process_tree, self.LOGGER_PROC, monitoredApp, False)
44 print "Merged %i logger processes" % removed
45 69
46 if prune: 70 if prune:
47 removed = self.prune(self.process_tree, None) 71 p_processes = self.prune(self.process_tree, None)
48 print "Pruned %i processes" % removed 72 p_exploders = self.merge_exploders(self.process_tree, self.EXPLODER_PROCESSES)
49 removed = self.merge_exploders(self.process_tree, self.EXPLODER_PROCESSES) 73 p_threads = self.merge_siblings(self.process_tree)
50 print "Pruned %i exploders" % removed 74 p_runs = self.merge_runs(self.process_tree)
51 removed = self.merge_siblings(self.process_tree) 75 writer.status("pruned %i process, %i exploders, %i threads, and %i runs" % (p_processes, p_exploders, p_threads, p_runs))
52 print "Pruned %i threads" % removed
53 removed = self.merge_runs(self.process_tree)
54 print "Pruned %i runs" % removed
55 76
56 self.sort(self.process_tree) 77 self.sort(self.process_tree)
57 78
58 self.start_time = self.get_start_time(self.process_tree) 79 self.start_time = self.get_start_time(self.process_tree)
59 self.end_time = self.get_end_time(self.process_tree) 80 self.end_time = self.get_end_time(self.process_tree)
60 self.duration = self.end_time - self.start_time 81 self.duration = self.end_time - self.start_time
61 82
62 self.num_proc = self.num_nodes(self.process_tree) 83 self.num_proc = self.num_nodes(self.process_tree)
63 84
64 def build(self): 85 def build(self):
65 """Build the process tree from the list of top samples.""" 86 """Build the process tree from the list of top samples."""
66 self.process_tree = [] 87 self.process_tree = []
67 for proc in self.process_list: 88 for proc in self.process_list:
68 if not proc.parent: 89 if not proc.parent:
69 self.process_tree.append(proc) 90 self.process_tree.append(proc)
70 else: 91 else:
71 proc.parent.child_list.append(proc) 92 proc.parent.child_list.append(proc)
72 93
73 def sort(self, process_subtree): 94 def sort(self, process_subtree):
@@ -85,11 +106,11 @@ class ProcessTree:
85 106
86 def get_start_time(self, process_subtree): 107 def get_start_time(self, process_subtree):
87 """Returns the start time of the process subtree. This is the start 108 """Returns the start time of the process subtree. This is the start
88 time of the earliest process. 109 time of the earliest process.
89 110
90 """ 111 """
91 if not process_subtree: 112 if not process_subtree:
92 return 100000000; 113 return 100000000
93 return min( [min(proc.start_time, self.get_start_time(proc.child_list)) for proc in process_subtree] ) 114 return min( [min(proc.start_time, self.get_start_time(proc.child_list)) for proc in process_subtree] )
94 115
95 def get_end_time(self, process_subtree): 116 def get_end_time(self, process_subtree):
@@ -98,13 +119,13 @@ class ProcessTree:
98 119
99 """ 120 """
100 if not process_subtree: 121 if not process_subtree:
101 return -100000000; 122 return -100000000
102 return max( [max(proc.start_time + proc.duration, self.get_end_time(proc.child_list)) for proc in process_subtree] ) 123 return max( [max(proc.start_time + proc.duration, self.get_end_time(proc.child_list)) for proc in process_subtree] )
103 124
104 def get_max_pid(self, process_subtree): 125 def get_max_pid(self, process_subtree):
105 """Returns the max PID found in the process tree.""" 126 """Returns the max PID found in the process tree."""
106 if not process_subtree: 127 if not process_subtree:
107 return -100000000; 128 return -100000000
108 return max( [max(proc.pid, self.get_max_pid(proc.child_list)) for proc in process_subtree] ) 129 return max( [max(proc.pid, self.get_max_pid(proc.child_list)) for proc in process_subtree] )
109 130
110 def update_ppids_for_daemons(self, process_list): 131 def update_ppids_for_daemons(self, process_list):
@@ -118,29 +139,29 @@ class ProcessTree:
118 rcendpid = -1 139 rcendpid = -1
119 rcproc = None 140 rcproc = None
120 for p in process_list: 141 for p in process_list:
121 if p.cmd == "rc" and p.ppid == 1: 142 if p.cmd == "rc" and p.ppid // 1000 == 1:
122 rcproc = p 143 rcproc = p
123 rcstartpid = p.pid 144 rcstartpid = p.pid
124 rcendpid = self.get_max_pid(p.child_list) 145 rcendpid = self.get_max_pid(p.child_list)
125 if rcstartpid != -1 and rcendpid != -1: 146 if rcstartpid != -1 and rcendpid != -1:
126 for p in process_list: 147 for p in process_list:
127 if p.pid > rcstartpid and p.pid < rcendpid and p.ppid == 1: 148 if p.pid > rcstartpid and p.pid < rcendpid and p.ppid // 1000 == 1:
128 p.ppid = rcstartpid 149 p.ppid = rcstartpid
129 p.parent = rcproc 150 p.parent = rcproc
130 for p in process_list: 151 for p in process_list:
131 p.child_list = [] 152 p.child_list = []
132 self.build() 153 self.build()
133 154
134 def prune(self, process_subtree, parent): 155 def prune(self, process_subtree, parent):
135 """Prunes the process tree by removing idle processes and processes 156 """Prunes the process tree by removing idle processes and processes
136 that only live for the duration of a single top sample. Sibling 157 that only live for the duration of a single top sample. Sibling
137 processes with the same command line (i.e. threads) are merged 158 processes with the same command line (i.e. threads) are merged
138 together. This filters out sleepy background processes, short-lived 159 together. This filters out sleepy background processes, short-lived
139 processes and bootcharts' analysis tools. 160 processes and bootcharts' analysis tools.
140 """ 161 """
141 def is_idle_background_process_without_children(p): 162 def is_idle_background_process_without_children(p):
142 process_end = p.start_time + p.duration 163 process_end = p.start_time + p.duration
143 return not p.active and \ 164 return not p.active and \
144 process_end >= self.start_time + self.duration and \ 165 process_end >= self.start_time + self.duration and \
145 p.start_time > self.start_time and \ 166 p.start_time > self.start_time and \
146 p.duration > 0.9 * self.duration and \ 167 p.duration > 0.9 * self.duration and \
@@ -175,8 +196,8 @@ class ProcessTree:
175 196
176 def merge_logger(self, process_subtree, logger_proc, monitored_app, app_tree): 197 def merge_logger(self, process_subtree, logger_proc, monitored_app, app_tree):
177 """Merges the logger's process subtree. The logger will typically 198 """Merges the logger's process subtree. The logger will typically
178 spawn lots of sleep and cat processes, thus polluting the 199 spawn lots of sleep and cat processes, thus polluting the
179 process tree. 200 process tree.
180 201
181 """ 202 """
182 num_removed = 0 203 num_removed = 0
@@ -185,7 +206,7 @@ class ProcessTree:
185 if logger_proc == p.cmd and not app_tree: 206 if logger_proc == p.cmd and not app_tree:
186 is_app_tree = True 207 is_app_tree = True
187 num_removed += self.merge_logger(p.child_list, logger_proc, monitored_app, is_app_tree) 208 num_removed += self.merge_logger(p.child_list, logger_proc, monitored_app, is_app_tree)
188 # don't remove the logger itself 209 # don't remove the logger itself
189 continue 210 continue
190 211
191 if app_tree and monitored_app != None and monitored_app == p.cmd: 212 if app_tree and monitored_app != None and monitored_app == p.cmd:
@@ -193,7 +214,7 @@ class ProcessTree:
193 214
194 if is_app_tree: 215 if is_app_tree:
195 for child in p.child_list: 216 for child in p.child_list:
196 self.__merge_processes(p, child) 217 self.merge_processes(p, child)
197 num_removed += 1 218 num_removed += 1
198 p.child_list = [] 219 p.child_list = []
199 else: 220 else:
@@ -202,7 +223,7 @@ class ProcessTree:
202 223
203 def merge_exploders(self, process_subtree, processes): 224 def merge_exploders(self, process_subtree, processes):
204 """Merges specific process subtrees (used for processes which usually 225 """Merges specific process subtrees (used for processes which usually
205 spawn huge meaningless process trees). 226 spawn huge meaningless process trees).
206 227
207 """ 228 """
208 num_removed = 0 229 num_removed = 0
@@ -210,7 +231,7 @@ class ProcessTree:
210 if processes in processes and len(p.child_list) > 0: 231 if processes in processes and len(p.child_list) > 0:
211 subtreemap = self.getProcessMap(p.child_list) 232 subtreemap = self.getProcessMap(p.child_list)
212 for child in subtreemap.values(): 233 for child in subtreemap.values():
213 self.__merge_processes(p, child) 234 self.merge_processes(p, child)
214 num_removed += len(subtreemap) 235 num_removed += len(subtreemap)
215 p.child_list = [] 236 p.child_list = []
216 p.cmd += " (+)" 237 p.cmd += " (+)"
@@ -218,10 +239,10 @@ class ProcessTree:
218 num_removed += self.merge_exploders(p.child_list, processes) 239 num_removed += self.merge_exploders(p.child_list, processes)
219 return num_removed 240 return num_removed
220 241
221 def merge_siblings(self,process_subtree): 242 def merge_siblings(self, process_subtree):
222 """Merges thread processes. Sibling processes with the same command 243 """Merges thread processes. Sibling processes with the same command
223 line are merged together. 244 line are merged together.
224 245
225 """ 246 """
226 num_removed = 0 247 num_removed = 0
227 idx = 0 248 idx = 0
@@ -233,7 +254,7 @@ class ProcessTree:
233 idx -= 1 254 idx -= 1
234 num_removed += 1 255 num_removed += 1
235 p.child_list.extend(nextp.child_list) 256 p.child_list.extend(nextp.child_list)
236 self.__merge_processes(p, nextp) 257 self.merge_processes(p, nextp)
237 num_removed += self.merge_siblings(p.child_list) 258 num_removed += self.merge_siblings(p.child_list)
238 idx += 1 259 idx += 1
239 if len(process_subtree) > 0: 260 if len(process_subtree) > 0:
@@ -243,7 +264,7 @@ class ProcessTree:
243 264
244 def merge_runs(self, process_subtree): 265 def merge_runs(self, process_subtree):
245 """Merges process runs. Single child processes which share the same 266 """Merges process runs. Single child processes which share the same
246 command line with the parent are merged. 267 command line with the parent are merged.
247 268
248 """ 269 """
249 num_removed = 0 270 num_removed = 0
@@ -253,16 +274,17 @@ class ProcessTree:
253 if len(p.child_list) == 1 and p.child_list[0].cmd == p.cmd: 274 if len(p.child_list) == 1 and p.child_list[0].cmd == p.cmd:
254 child = p.child_list[0] 275 child = p.child_list[0]
255 p.child_list = list(child.child_list) 276 p.child_list = list(child.child_list)
256 self.__merge_processes(p, child) 277 self.merge_processes(p, child)
257 num_removed += 1 278 num_removed += 1
258 continue 279 continue
259 num_removed += self.merge_runs(p.child_list) 280 num_removed += self.merge_runs(p.child_list)
260 idx += 1 281 idx += 1
261 return num_removed 282 return num_removed
262 283
263 def __merge_processes(self, p1, p2): 284 def merge_processes(self, p1, p2):
264 """Merges two process samples.""" 285 """Merges two process' samples."""
265 p1.samples.extend(p2.samples) 286 p1.samples.extend(p2.samples)
287 p1.samples.sort( key = lambda p: p.time )
266 p1time = p1.start_time 288 p1time = p1.start_time
267 p2time = p2.start_time 289 p2time = p2.start_time
268 p1.start_time = min(p1time, p2time) 290 p1.start_time = min(p1time, p2time)
diff --git a/scripts/pybootchartgui/pybootchartgui/samples.py b/scripts/pybootchartgui/pybootchartgui/samples.py
index c94b30d032..015d743aa0 100644
--- a/scripts/pybootchartgui/pybootchartgui/samples.py
+++ b/scripts/pybootchartgui/pybootchartgui/samples.py
@@ -1,93 +1,151 @@
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
1class DiskStatSample: 17class DiskStatSample:
2 def __init__(self, time): 18 def __init__(self, time):
3 self.time = time 19 self.time = time
4 self.diskdata = [0, 0, 0] 20 self.diskdata = [0, 0, 0]
5 def add_diskdata(self, new_diskdata): 21 def add_diskdata(self, new_diskdata):
6 self.diskdata = [ a + b for a, b in zip(self.diskdata, new_diskdata) ] 22 self.diskdata = [ a + b for a, b in zip(self.diskdata, new_diskdata) ]
7 23
8class CPUSample: 24class CPUSample:
9 def __init__(self, time, user, sys, io): 25 def __init__(self, time, user, sys, io = 0.0, swap = 0.0):
10 self.time = time 26 self.time = time
11 self.user = user 27 self.user = user
12 self.sys = sys 28 self.sys = sys
13 self.io = io 29 self.io = io
14 30 self.swap = swap
15 def __str__(self): 31
16 return str(self.time) + "\t" + str(self.user) + "\t" + str(self.sys) + "\t" + str(self.io); 32 @property
17 33 def cpu(self):
34 return self.user + self.sys
35
36 def __str__(self):
37 return str(self.time) + "\t" + str(self.user) + "\t" + \
38 str(self.sys) + "\t" + str(self.io) + "\t" + str (self.swap)
39
40class MemSample:
41 used_values = ('MemTotal', 'MemFree', 'Buffers', 'Cached', 'SwapTotal', 'SwapFree',)
42
43 def __init__(self, time):
44 self.time = time
45 self.records = {}
46
47 def add_value(self, name, value):
48 if name in MemSample.used_values:
49 self.records[name] = value
50
51 def valid(self):
52 keys = self.records.keys()
53 # discard incomplete samples
54 return [v for v in MemSample.used_values if v not in keys] == []
55
18class ProcessSample: 56class ProcessSample:
19 def __init__(self, time, state, cpu_sample): 57 def __init__(self, time, state, cpu_sample):
20 self.time = time 58 self.time = time
21 self.state = state 59 self.state = state
22 self.cpu_sample = cpu_sample 60 self.cpu_sample = cpu_sample
23 61
24 def __str__(self): 62 def __str__(self):
25 return str(self.time) + "\t" + str(self.state) + "\t" + str(self.cpu_sample); 63 return str(self.time) + "\t" + str(self.state) + "\t" + str(self.cpu_sample)
26 64
27class ProcessStats: 65class ProcessStats:
28 def __init__(self, process_list, sample_period, start_time, end_time): 66 def __init__(self, writer, process_map, sample_count, sample_period, start_time, end_time):
29 self.process_list = process_list 67 self.process_map = process_map
30 self.sample_period = sample_period 68 self.sample_count = sample_count
31 self.start_time = start_time 69 self.sample_period = sample_period
32 self.end_time = end_time 70 self.start_time = start_time
33 71 self.end_time = end_time
34class Process: 72 writer.info ("%d samples, avg. sample length %f" % (self.sample_count, self.sample_period))
35 def __init__(self, pid, cmd, ppid, start_time): 73 writer.info ("process list size: %d" % len (self.process_map.values()))
36 self.pid = pid
37 self.cmd = cmd.strip('(').strip(')')
38 self.ppid = ppid
39 self.start_time = start_time
40 self.samples = []
41 self.parent = None
42 self.child_list = []
43
44 self.duration = 0
45 self.active = None
46
47 self.last_user_cpu_time = None
48 self.last_sys_cpu_time = None
49
50 def __str__(self):
51 return " ".join([str(self.pid), self.cmd, str(self.ppid), '[ ' + str(len(self.samples)) + ' samples ]' ])
52
53 def calc_stats(self, samplePeriod):
54 if self.samples:
55 firstSample = self.samples[0]
56 lastSample = self.samples[-1]
57 self.start_time = min(firstSample.time, self.start_time)
58 self.duration = lastSample.time - self.start_time + samplePeriod
59
60 activeCount = sum( [1 for sample in self.samples if sample.cpu_sample and sample.cpu_sample.sys + sample.cpu_sample.user + sample.cpu_sample.io > 0.0] )
61 activeCount = activeCount + sum( [1 for sample in self.samples if sample.state == 'D'] )
62 self.active = (activeCount>2)
63
64 def calc_load(self, userCpu, sysCpu, interval):
65
66 userCpuLoad = float(userCpu - self.last_user_cpu_time) / interval
67 sysCpuLoad = float(sysCpu - self.last_sys_cpu_time) / interval
68 cpuLoad = userCpuLoad + sysCpuLoad
69 # normalize
70 if cpuLoad > 1.0:
71 userCpuLoad = userCpuLoad / cpuLoad;
72 sysCpuLoad = sysCpuLoad / cpuLoad;
73 return (userCpuLoad, sysCpuLoad)
74
75 def set_parent(self, processMap):
76 if self.ppid != None:
77 self.parent = processMap.get(self.ppid)
78 if self.parent == None and self.pid > 1:
79 print "warning: no parent for pid '%i' with ppid '%i'" % (self.pid,self.ppid)
80 def get_end_time(self):
81 return self.start_time + self.duration
82 74
83class DiskSample: 75class Process:
84 def __init__(self, time, read, write, util): 76 def __init__(self, writer, pid, cmd, ppid, start_time):
85 self.time = time 77 self.writer = writer
86 self.read = read 78 self.pid = pid
87 self.write = write 79 self.cmd = cmd
88 self.util = util 80 self.exe = cmd
89 self.tput = read + write 81 self.args = []
82 self.ppid = ppid
83 self.start_time = start_time
84 self.duration = 0
85 self.samples = []
86 self.parent = None
87 self.child_list = []
88
89 self.active = None
90 self.last_user_cpu_time = None
91 self.last_sys_cpu_time = None
92
93 self.last_cpu_ns = 0
94 self.last_blkio_delay_ns = 0
95 self.last_swapin_delay_ns = 0
96
97 # split this process' run - triggered by a name change
98 def split(self, writer, pid, cmd, ppid, start_time):
99 split = Process (writer, pid, cmd, ppid, start_time)
100
101 split.last_cpu_ns = self.last_cpu_ns
102 split.last_blkio_delay_ns = self.last_blkio_delay_ns
103 split.last_swapin_delay_ns = self.last_swapin_delay_ns
104
105 return split
106
107 def __str__(self):
108 return " ".join([str(self.pid), self.cmd, str(self.ppid), '[ ' + str(len(self.samples)) + ' samples ]' ])
90 109
91 def __str__(self): 110 def calc_stats(self, samplePeriod):
92 return "\t".join([str(self.time), str(self.read), str(self.write), str(self.util)]) 111 if self.samples:
112 firstSample = self.samples[0]
113 lastSample = self.samples[-1]
114 self.start_time = min(firstSample.time, self.start_time)
115 self.duration = lastSample.time - self.start_time + samplePeriod
116
117 activeCount = sum( [1 for sample in self.samples if sample.cpu_sample and sample.cpu_sample.sys + sample.cpu_sample.user + sample.cpu_sample.io > 0.0] )
118 activeCount = activeCount + sum( [1 for sample in self.samples if sample.state == 'D'] )
119 self.active = (activeCount>2)
120
121 def calc_load(self, userCpu, sysCpu, interval):
122 userCpuLoad = float(userCpu - self.last_user_cpu_time) / interval
123 sysCpuLoad = float(sysCpu - self.last_sys_cpu_time) / interval
124 cpuLoad = userCpuLoad + sysCpuLoad
125 # normalize
126 if cpuLoad > 1.0:
127 userCpuLoad = userCpuLoad / cpuLoad
128 sysCpuLoad = sysCpuLoad / cpuLoad
129 return (userCpuLoad, sysCpuLoad)
130
131 def set_parent(self, processMap):
132 if self.ppid != None:
133 self.parent = processMap.get (self.ppid)
134 if self.parent == None and self.pid // 1000 > 1 and \
135 not (self.ppid == 2000 or self.pid == 2000): # kernel threads: ppid=2
136 self.writer.warn("Missing CONFIG_PROC_EVENTS: no parent for pid '%i' ('%s') with ppid '%i'" \
137 % (self.pid,self.cmd,self.ppid))
138
139 def get_end_time(self):
140 return self.start_time + self.duration
141
142class DiskSample:
143 def __init__(self, time, read, write, util):
144 self.time = time
145 self.read = read
146 self.write = write
147 self.util = util
148 self.tput = read + write
93 149
150 def __str__(self):
151 return "\t".join([str(self.time), str(self.read), str(self.write), str(self.util)])
diff --git a/scripts/pybootchartgui/pybootchartgui/tests/parser_test.py b/scripts/pybootchartgui/pybootchartgui/tests/parser_test.py
index 574c2c7a2b..00fb3bf797 100644
--- a/scripts/pybootchartgui/pybootchartgui/tests/parser_test.py
+++ b/scripts/pybootchartgui/pybootchartgui/tests/parser_test.py
@@ -4,90 +4,102 @@ import unittest
4 4
5sys.path.insert(0, os.getcwd()) 5sys.path.insert(0, os.getcwd())
6 6
7import parsing 7import pybootchartgui.parsing as parsing
8import pybootchartgui.main as main
8 9
9debug = False 10debug = False
10 11
11def floatEq(f1, f2): 12def floatEq(f1, f2):
12 return math.fabs(f1-f2) < 0.00001 13 return math.fabs(f1-f2) < 0.00001
13 14
15bootchart_dir = os.path.join(os.path.dirname(sys.argv[0]), '../../examples/1/')
16parser = main._mk_options_parser()
17options, args = parser.parse_args(['--q', bootchart_dir])
18writer = main._mk_writer(options)
19
14class TestBCParser(unittest.TestCase): 20class TestBCParser(unittest.TestCase):
15 21
16 def setUp(self): 22 def setUp(self):
17 self.name = "My first unittest" 23 self.name = "My first unittest"
18 self.rootdir = '../examples/1' 24 self.rootdir = bootchart_dir
19 25
20 def mk_fname(self,f): 26 def mk_fname(self,f):
21 return os.path.join(self.rootdir, f) 27 return os.path.join(self.rootdir, f)
22 28
23 def testParseHeader(self): 29 def testParseHeader(self):
24 state = parsing.parse_file(parsing.ParserState(), self.mk_fname('header')) 30 trace = parsing.Trace(writer, args, options)
31 state = parsing.parse_file(writer, trace, self.mk_fname('header'))
25 self.assertEqual(6, len(state.headers)) 32 self.assertEqual(6, len(state.headers))
26 self.assertEqual(2, parsing.get_num_cpus(state.headers)) 33 self.assertEqual(2, parsing.get_num_cpus(state.headers))
27 34
28 def test_parseTimedBlocks(self): 35 def test_parseTimedBlocks(self):
29 state = parsing.parse_file(parsing.ParserState(), self.mk_fname('proc_diskstats.log')) 36 trace = parsing.Trace(writer, args, options)
37 state = parsing.parse_file(writer, trace, self.mk_fname('proc_diskstats.log'))
30 self.assertEqual(141, len(state.disk_stats)) 38 self.assertEqual(141, len(state.disk_stats))
31 39
32 def testParseProcPsLog(self): 40 def testParseProcPsLog(self):
33 state = parsing.parse_file(parsing.ParserState(), self.mk_fname('proc_ps.log')) 41 trace = parsing.Trace(writer, args, options)
42 state = parsing.parse_file(writer, trace, self.mk_fname('proc_ps.log'))
34 samples = state.ps_stats 43 samples = state.ps_stats
35 processes = samples.process_list 44 processes = samples.process_map
36 sorted_processes = sorted(processes, key=lambda p: p.pid ) 45 sorted_processes = [processes[k] for k in sorted(processes.keys())]
37 46
38 for index, line in enumerate(open(self.mk_fname('extract2.proc_ps.log'))): 47 ps_data = open(self.mk_fname('extract2.proc_ps.log'))
48 for index, line in enumerate(ps_data):
39 tokens = line.split(); 49 tokens = line.split();
40 process = sorted_processes[index] 50 process = sorted_processes[index]
41 if debug: 51 if debug:
42 print tokens[0:4] 52 print(tokens[0:4])
43 print process.pid, process.cmd, process.ppid, len(process.samples) 53 print(process.pid / 1000, process.cmd, process.ppid, len(process.samples))
44 print '-------------------' 54 print('-------------------')
45 55
46 self.assertEqual(tokens[0], str(process.pid)) 56 self.assertEqual(tokens[0], str(process.pid // 1000))
47 self.assertEqual(tokens[1], str(process.cmd)) 57 self.assertEqual(tokens[1], str(process.cmd))
48 self.assertEqual(tokens[2], str(process.ppid)) 58 self.assertEqual(tokens[2], str(process.ppid // 1000))
49 self.assertEqual(tokens[3], str(len(process.samples))) 59 self.assertEqual(tokens[3], str(len(process.samples)))
50 60 ps_data.close()
51 61
52 def testparseProcDiskStatLog(self): 62 def testparseProcDiskStatLog(self):
53 state_with_headers = parsing.parse_file(parsing.ParserState(), self.mk_fname('header')) 63 trace = parsing.Trace(writer, args, options)
64 state_with_headers = parsing.parse_file(writer, trace, self.mk_fname('header'))
54 state_with_headers.headers['system.cpu'] = 'xxx (2)' 65 state_with_headers.headers['system.cpu'] = 'xxx (2)'
55 samples = parsing.parse_file(state_with_headers, self.mk_fname('proc_diskstats.log')).disk_stats 66 samples = parsing.parse_file(writer, state_with_headers, self.mk_fname('proc_diskstats.log')).disk_stats
56 self.assertEqual(141, len(samples)) 67 self.assertEqual(141, len(samples))
57 68
58 for index, line in enumerate(open(self.mk_fname('extract.proc_diskstats.log'))): 69 diskstats_data = open(self.mk_fname('extract.proc_diskstats.log'))
70 for index, line in enumerate(diskstats_data):
59 tokens = line.split('\t') 71 tokens = line.split('\t')
60 sample = samples[index] 72 sample = samples[index]
61 if debug: 73 if debug:
62 print line.rstrip(), 74 print(line.rstrip())
63 print sample 75 print(sample)
64 print '-------------------' 76 print('-------------------')
65 77
66 self.assertEqual(tokens[0], str(sample.time)) 78 self.assertEqual(tokens[0], str(sample.time))
67 self.assert_(floatEq(float(tokens[1]), sample.read)) 79 self.assert_(floatEq(float(tokens[1]), sample.read))
68 self.assert_(floatEq(float(tokens[2]), sample.write)) 80 self.assert_(floatEq(float(tokens[2]), sample.write))
69 self.assert_(floatEq(float(tokens[3]), sample.util)) 81 self.assert_(floatEq(float(tokens[3]), sample.util))
82 diskstats_data.close()
70 83
71 def testparseProcStatLog(self): 84 def testparseProcStatLog(self):
72 samples = parsing.parse_file(parsing.ParserState(), self.mk_fname('proc_stat.log')).cpu_stats 85 trace = parsing.Trace(writer, args, options)
86 samples = parsing.parse_file(writer, trace, self.mk_fname('proc_stat.log')).cpu_stats
73 self.assertEqual(141, len(samples)) 87 self.assertEqual(141, len(samples))
74 88
75 for index, line in enumerate(open(self.mk_fname('extract.proc_stat.log'))): 89 stat_data = open(self.mk_fname('extract.proc_stat.log'))
90 for index, line in enumerate(stat_data):
76 tokens = line.split('\t') 91 tokens = line.split('\t')
77 sample = samples[index] 92 sample = samples[index]
78 if debug: 93 if debug:
79 print line.rstrip() 94 print(line.rstrip())
80 print sample 95 print(sample)
81 print '-------------------' 96 print('-------------------')
82 self.assert_(floatEq(float(tokens[0]), sample.time)) 97 self.assert_(floatEq(float(tokens[0]), sample.time))
83 self.assert_(floatEq(float(tokens[1]), sample.user)) 98 self.assert_(floatEq(float(tokens[1]), sample.user))
84 self.assert_(floatEq(float(tokens[2]), sample.sys)) 99 self.assert_(floatEq(float(tokens[2]), sample.sys))
85 self.assert_(floatEq(float(tokens[3]), sample.io)) 100 self.assert_(floatEq(float(tokens[3]), sample.io))
86 101 stat_data.close()
87 def testParseLogDir(self): 102
88 res = parsing.parse([self.rootdir], False)
89 self.assertEqual(4, len(res))
90
91if __name__ == '__main__': 103if __name__ == '__main__':
92 unittest.main() 104 unittest.main()
93 105
diff --git a/scripts/pybootchartgui/pybootchartgui/tests/process_tree_test.py b/scripts/pybootchartgui/pybootchartgui/tests/process_tree_test.py
index 971e125eab..6f46a1c03d 100644
--- a/scripts/pybootchartgui/pybootchartgui/tests/process_tree_test.py
+++ b/scripts/pybootchartgui/pybootchartgui/tests/process_tree_test.py
@@ -4,16 +4,28 @@ import unittest
4 4
5sys.path.insert(0, os.getcwd()) 5sys.path.insert(0, os.getcwd())
6 6
7import parsing 7import pybootchartgui.parsing as parsing
8import process_tree 8import pybootchartgui.process_tree as process_tree
9import pybootchartgui.main as main
10
11if sys.version_info >= (3, 0):
12 long = int
9 13
10class TestProcessTree(unittest.TestCase): 14class TestProcessTree(unittest.TestCase):
11 15
12 def setUp(self): 16 def setUp(self):
13 self.name = "Process tree unittest" 17 self.name = "Process tree unittest"
14 self.rootdir = '../examples/1' 18 self.rootdir = os.path.join(os.path.dirname(sys.argv[0]), '../../examples/1/')
15 self.ps_stats = parsing.parse_file(parsing.ParserState(), self.mk_fname('proc_ps.log')).ps_stats 19
16 self.processtree = process_tree.ProcessTree(self.ps_stats, None, False, for_testing = True) 20 parser = main._mk_options_parser()
21 options, args = parser.parse_args(['--q', self.rootdir])
22 writer = main._mk_writer(options)
23 trace = parsing.Trace(writer, args, options)
24
25 parsing.parse_file(writer, trace, self.mk_fname('proc_ps.log'))
26 trace.compile(writer)
27 self.processtree = process_tree.ProcessTree(writer, None, trace.ps_stats, \
28 trace.ps_stats.sample_period, None, options.prune, None, None, False, for_testing = True)
17 29
18 def mk_fname(self,f): 30 def mk_fname(self,f):
19 return os.path.join(self.rootdir, f) 31 return os.path.join(self.rootdir, f)
@@ -26,14 +38,16 @@ class TestProcessTree(unittest.TestCase):
26 return flattened 38 return flattened
27 39
28 def checkAgainstJavaExtract(self, filename, process_tree): 40 def checkAgainstJavaExtract(self, filename, process_tree):
29 for expected, actual in zip(open(filename), self.flatten(process_tree)): 41 test_data = open(filename)
42 for expected, actual in zip(test_data, self.flatten(process_tree)):
30 tokens = expected.split('\t') 43 tokens = expected.split('\t')
31 self.assertEqual(int(tokens[0]), actual.pid) 44 self.assertEqual(int(tokens[0]), actual.pid // 1000)
32 self.assertEqual(tokens[1], actual.cmd) 45 self.assertEqual(tokens[1], actual.cmd)
33 self.assertEqual(long(tokens[2]), 10 * actual.start_time) 46 self.assertEqual(long(tokens[2]), 10 * actual.start_time)
34 self.assert_(long(tokens[3]) - 10 * actual.duration < 5, "duration") 47 self.assert_(long(tokens[3]) - 10 * actual.duration < 5, "duration")
35 self.assertEqual(int(tokens[4]), len(actual.child_list)) 48 self.assertEqual(int(tokens[4]), len(actual.child_list))
36 self.assertEqual(int(tokens[5]), len(actual.samples)) 49 self.assertEqual(int(tokens[5]), len(actual.samples))
50 test_data.close()
37 51
38 def testBuild(self): 52 def testBuild(self):
39 process_tree = self.processtree.process_tree 53 process_tree = self.processtree.process_tree