draw.py 33 KB

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