draw.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  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:
  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 accomodate 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. return (w, h)
  294. def clip_visible(clip, rect):
  295. xmax = max (clip[0], rect[0])
  296. ymax = max (clip[1], rect[1])
  297. xmin = min (clip[0] + clip[2], rect[0] + rect[2])
  298. ymin = min (clip[1] + clip[3], rect[1] + rect[3])
  299. return (xmin > xmax and ymin > ymax)
  300. def render_charts(ctx, options, clip, trace, curr_y, w, h, sec_w):
  301. proc_tree = options.proc_tree(trace)
  302. # render bar legend
  303. if trace.cpu_stats:
  304. ctx.set_font_size(LEGEND_FONT_SIZE)
  305. draw_legend_box(ctx, "CPU (user+sys)", CPU_COLOR, off_x, curr_y+20, leg_s)
  306. draw_legend_box(ctx, "I/O (wait)", IO_COLOR, off_x + 120, curr_y+20, leg_s)
  307. # render I/O wait
  308. chart_rect = (off_x, curr_y+30, w, bar_h)
  309. if clip_visible (clip, chart_rect):
  310. draw_box_ticks (ctx, chart_rect, sec_w)
  311. draw_annotations (ctx, proc_tree, trace.times, chart_rect)
  312. draw_chart (ctx, IO_COLOR, True, chart_rect, \
  313. [(sample.time, sample.user + sample.sys + sample.io) for sample in trace.cpu_stats], \
  314. proc_tree, None)
  315. # render CPU load
  316. draw_chart (ctx, CPU_COLOR, True, chart_rect, \
  317. [(sample.time, sample.user + sample.sys) for sample in trace.cpu_stats], \
  318. proc_tree, None)
  319. curr_y = curr_y + 30 + bar_h
  320. # render second chart
  321. if trace.disk_stats:
  322. draw_legend_line(ctx, "Disk throughput", DISK_TPUT_COLOR, off_x, curr_y+20, leg_s)
  323. draw_legend_box(ctx, "Disk utilization", IO_COLOR, off_x + 120, curr_y+20, leg_s)
  324. # render I/O utilization
  325. chart_rect = (off_x, curr_y+30, w, bar_h)
  326. if clip_visible (clip, chart_rect):
  327. draw_box_ticks (ctx, chart_rect, sec_w)
  328. draw_annotations (ctx, proc_tree, trace.times, chart_rect)
  329. draw_chart (ctx, IO_COLOR, True, chart_rect, \
  330. [(sample.time, sample.util) for sample in trace.disk_stats], \
  331. proc_tree, None)
  332. # render disk throughput
  333. max_sample = max (trace.disk_stats, key = lambda s: s.tput)
  334. if clip_visible (clip, chart_rect):
  335. draw_chart (ctx, DISK_TPUT_COLOR, False, chart_rect, \
  336. [(sample.time, sample.tput) for sample in trace.disk_stats], \
  337. proc_tree, None)
  338. pos_x = off_x + ((max_sample.time - proc_tree.start_time) * w / proc_tree.duration)
  339. shift_x, shift_y = -20, 20
  340. if (pos_x < off_x + 245):
  341. shift_x, shift_y = 5, 40
  342. label = "%dMB/s" % round ((max_sample.tput) / 1024.0)
  343. draw_text (ctx, label, DISK_TPUT_COLOR, pos_x + shift_x, curr_y + shift_y)
  344. curr_y = curr_y + 30 + bar_h
  345. # render disk space usage
  346. #
  347. # Draws the amount of disk space used on each volume relative to the
  348. # lowest recorded amount. The graphs for each volume are stacked above
  349. # each other so that total disk usage is visible.
  350. if trace.monitor_disk:
  351. ctx.set_font_size(LEGEND_FONT_SIZE)
  352. # Determine set of volumes for which we have
  353. # information and the minimal amount of used disk
  354. # space for each. Currently samples are allowed to
  355. # not have a values for all volumes; drawing could be
  356. # made more efficient if that wasn't the case.
  357. volumes = set()
  358. min_used = {}
  359. for sample in trace.monitor_disk:
  360. for volume, used in sample.records.items():
  361. volumes.add(volume)
  362. if volume not in min_used or min_used[volume] > used:
  363. min_used[volume] = used
  364. volumes = sorted(list(volumes))
  365. disk_scale = 0
  366. for i, volume in enumerate(volumes):
  367. volume_scale = max([sample.records[volume] - min_used[volume]
  368. for sample in trace.monitor_disk
  369. if volume in sample.records])
  370. # Does not take length of volume name into account, but fixed offset
  371. # works okay in practice.
  372. draw_legend_box(ctx, '%s (max: %u MiB)' % (volume, volume_scale / 1024 / 1024),
  373. VOLUME_COLORS[i % len(VOLUME_COLORS)],
  374. off_x + i * 250, curr_y+20, leg_s)
  375. disk_scale += volume_scale
  376. # render used amount of disk space
  377. chart_rect = (off_x, curr_y+30, w, bar_h)
  378. if clip_visible (clip, chart_rect):
  379. draw_box_ticks (ctx, chart_rect, sec_w)
  380. draw_annotations (ctx, proc_tree, trace.times, chart_rect)
  381. for i in range(len(volumes), 0, -1):
  382. draw_chart (ctx, VOLUME_COLORS[(i - 1) % len(VOLUME_COLORS)], True, chart_rect, \
  383. [(sample.time,
  384. # Sum up used space of all volumes including the current one
  385. # so that the graphs appear as stacked on top of each other.
  386. functools.reduce(lambda x,y: x+y,
  387. [sample.records[volume] - min_used[volume]
  388. for volume in volumes[0:i]
  389. if volume in sample.records],
  390. 0))
  391. for sample in trace.monitor_disk], \
  392. proc_tree, [0, disk_scale])
  393. curr_y = curr_y + 30 + bar_h
  394. # render mem usage
  395. chart_rect = (off_x, curr_y+30, w, meminfo_bar_h)
  396. mem_stats = trace.mem_stats
  397. if mem_stats and clip_visible (clip, chart_rect):
  398. mem_scale = max(sample.buffers for sample in mem_stats)
  399. draw_legend_box(ctx, "Mem cached (scale: %u MiB)" % (float(mem_scale) / 1024), MEM_CACHED_COLOR, off_x, curr_y+20, leg_s)
  400. draw_legend_box(ctx, "Used", MEM_USED_COLOR, off_x + 240, curr_y+20, leg_s)
  401. draw_legend_box(ctx, "Buffers", MEM_BUFFERS_COLOR, off_x + 360, curr_y+20, leg_s)
  402. draw_legend_line(ctx, "Swap (scale: %u MiB)" % max([(sample.swap)/1024 for sample in mem_stats]), \
  403. MEM_SWAP_COLOR, off_x + 480, curr_y+20, leg_s)
  404. draw_box_ticks(ctx, chart_rect, sec_w)
  405. draw_annotations(ctx, proc_tree, trace.times, chart_rect)
  406. draw_chart(ctx, MEM_BUFFERS_COLOR, True, chart_rect, \
  407. [(sample.time, sample.buffers) for sample in trace.mem_stats], \
  408. proc_tree, [0, mem_scale])
  409. draw_chart(ctx, MEM_USED_COLOR, True, chart_rect, \
  410. [(sample.time, sample.used) for sample in mem_stats], \
  411. proc_tree, [0, mem_scale])
  412. draw_chart(ctx, MEM_CACHED_COLOR, True, chart_rect, \
  413. [(sample.time, sample.cached) for sample in mem_stats], \
  414. proc_tree, [0, mem_scale])
  415. draw_chart(ctx, MEM_SWAP_COLOR, False, chart_rect, \
  416. [(sample.time, float(sample.swap)) for sample in mem_stats], \
  417. proc_tree, None)
  418. curr_y = curr_y + meminfo_bar_h
  419. return curr_y
  420. def render_processes_chart(ctx, options, trace, curr_y, w, h, sec_w):
  421. chart_rect = [off_x, curr_y+header_h, w, h - 2 * off_y - header_h - leg_s + proc_h]
  422. draw_legend_box (ctx, "Configure", \
  423. TASK_COLOR_CONFIGURE, off_x , curr_y + 45, leg_s)
  424. draw_legend_box (ctx, "Compile", \
  425. TASK_COLOR_COMPILE, off_x+120, curr_y + 45, leg_s)
  426. draw_legend_box (ctx, "Install", \
  427. TASK_COLOR_INSTALL, off_x+240, curr_y + 45, leg_s)
  428. draw_legend_box (ctx, "Populate Sysroot", \
  429. TASK_COLOR_SYSROOT, off_x+360, curr_y + 45, leg_s)
  430. draw_legend_box (ctx, "Package", \
  431. TASK_COLOR_PACKAGE, off_x+480, curr_y + 45, leg_s)
  432. draw_legend_box (ctx, "Package Write", \
  433. TASK_COLOR_PACKAGE_WRITE, off_x+600, curr_y + 45, leg_s)
  434. ctx.set_font_size(PROC_TEXT_FONT_SIZE)
  435. draw_box_ticks(ctx, chart_rect, sec_w)
  436. draw_sec_labels(ctx, options, chart_rect, sec_w, 30)
  437. y = curr_y+header_h
  438. offset = trace.min or min(trace.start.keys())
  439. for s in sorted(trace.start.keys()):
  440. for val in sorted(trace.start[s]):
  441. if not options.app_options.show_all and \
  442. trace.processes[val][1] - s < options.app_options.mintime:
  443. continue
  444. task = val.split(":")[1]
  445. #print(val)
  446. #print(trace.processes[val][1])
  447. #print(s)
  448. x = chart_rect[0] + (s - offset) * sec_w
  449. w = ((trace.processes[val][1] - s) * sec_w)
  450. #print("proc at %s %s %s %s" % (x, y, w, proc_h))
  451. col = None
  452. if task == "do_compile":
  453. col = TASK_COLOR_COMPILE
  454. elif task == "do_configure":
  455. col = TASK_COLOR_CONFIGURE
  456. elif task == "do_install":
  457. col = TASK_COLOR_INSTALL
  458. elif task == "do_populate_sysroot":
  459. col = TASK_COLOR_SYSROOT
  460. elif task == "do_package":
  461. col = TASK_COLOR_PACKAGE
  462. elif task == "do_package_write_rpm" or \
  463. task == "do_package_write_deb" or \
  464. task == "do_package_write_ipk":
  465. col = TASK_COLOR_PACKAGE_WRITE
  466. else:
  467. col = WHITE
  468. if col:
  469. draw_fill_rect(ctx, col, (x, y, w, proc_h))
  470. draw_rect(ctx, PROC_BORDER_COLOR, (x, y, w, proc_h))
  471. draw_label_in_box(ctx, PROC_TEXT_COLOR, val, x, y + proc_h - 4, w, proc_h)
  472. y = y + proc_h
  473. return curr_y
  474. #
  475. # Render the chart.
  476. #
  477. def render(ctx, options, xscale, trace):
  478. (w, h) = extents (options, xscale, trace)
  479. global OPTIONS
  480. OPTIONS = options.app_options
  481. # x, y, w, h
  482. clip = ctx.clip_extents()
  483. sec_w = int (xscale * sec_w_base)
  484. ctx.set_line_width(1.0)
  485. ctx.select_font_face(FONT_NAME)
  486. draw_fill_rect(ctx, WHITE, (0, 0, max(w, MIN_IMG_W), h))
  487. w -= 2*off_x
  488. curr_y = off_y;
  489. if options.charts:
  490. curr_y = render_charts (ctx, options, clip, trace, curr_y, w, h, sec_w)
  491. curr_y = render_processes_chart (ctx, options, trace, curr_y, w, h, sec_w)
  492. return
  493. proc_tree = options.proc_tree (trace)
  494. # draw the title and headers
  495. if proc_tree.idle:
  496. duration = proc_tree.idle
  497. else:
  498. duration = proc_tree.duration
  499. if not options.kernel_only:
  500. curr_y = draw_header (ctx, trace.headers, duration)
  501. else:
  502. curr_y = off_y;
  503. # draw process boxes
  504. proc_height = h
  505. if proc_tree.taskstats and options.cumulative:
  506. proc_height -= CUML_HEIGHT
  507. draw_process_bar_chart(ctx, clip, options, proc_tree, trace.times,
  508. curr_y, w, proc_height, sec_w)
  509. curr_y = proc_height
  510. ctx.set_font_size(SIG_FONT_SIZE)
  511. draw_text(ctx, SIGNATURE, SIG_COLOR, off_x + 5, proc_height - 8)
  512. # draw a cumulative CPU-time-per-process graph
  513. if proc_tree.taskstats and options.cumulative:
  514. cuml_rect = (off_x, curr_y + off_y, w, CUML_HEIGHT/2 - off_y * 2)
  515. if clip_visible (clip, cuml_rect):
  516. draw_cuml_graph(ctx, proc_tree, cuml_rect, duration, sec_w, STAT_TYPE_CPU)
  517. # draw a cumulative I/O-time-per-process graph
  518. if proc_tree.taskstats and options.cumulative:
  519. cuml_rect = (off_x, curr_y + off_y * 100, w, CUML_HEIGHT/2 - off_y * 2)
  520. if clip_visible (clip, cuml_rect):
  521. draw_cuml_graph(ctx, proc_tree, cuml_rect, duration, sec_w, STAT_TYPE_IO)
  522. def draw_process_bar_chart(ctx, clip, options, proc_tree, times, curr_y, w, h, sec_w):
  523. header_size = 0
  524. if not options.kernel_only:
  525. draw_legend_box (ctx, "Running (%cpu)",
  526. PROC_COLOR_R, off_x , curr_y + 45, leg_s)
  527. draw_legend_box (ctx, "Unint.sleep (I/O)",
  528. PROC_COLOR_D, off_x+120, curr_y + 45, leg_s)
  529. draw_legend_box (ctx, "Sleeping",
  530. PROC_COLOR_S, off_x+240, curr_y + 45, leg_s)
  531. draw_legend_box (ctx, "Zombie",
  532. PROC_COLOR_Z, off_x+360, curr_y + 45, leg_s)
  533. header_size = 45
  534. chart_rect = [off_x, curr_y + header_size + 15,
  535. w, h - 2 * off_y - (curr_y + header_size + 15) + proc_h]
  536. ctx.set_font_size (PROC_TEXT_FONT_SIZE)
  537. draw_box_ticks (ctx, chart_rect, sec_w)
  538. if sec_w > 100:
  539. nsec = 1
  540. else:
  541. nsec = 5
  542. draw_sec_labels (ctx, options, chart_rect, sec_w, nsec)
  543. draw_annotations (ctx, proc_tree, times, chart_rect)
  544. y = curr_y + 60
  545. for root in proc_tree.process_tree:
  546. draw_processes_recursively(ctx, root, proc_tree, y, proc_h, chart_rect, clip)
  547. y = y + proc_h * proc_tree.num_nodes([root])
  548. def draw_header (ctx, headers, duration):
  549. toshow = [
  550. ('system.uname', 'uname', lambda s: s),
  551. ('system.release', 'release', lambda s: s),
  552. ('system.cpu', 'CPU', lambda s: re.sub('model name\s*:\s*', '', s, 1)),
  553. ('system.kernel.options', 'kernel options', lambda s: s),
  554. ]
  555. header_y = ctx.font_extents()[2] + 10
  556. ctx.set_font_size(TITLE_FONT_SIZE)
  557. draw_text(ctx, headers['title'], TEXT_COLOR, off_x, header_y)
  558. ctx.set_font_size(TEXT_FONT_SIZE)
  559. for (headerkey, headertitle, mangle) in toshow:
  560. header_y += ctx.font_extents()[2]
  561. if headerkey in headers:
  562. value = headers.get(headerkey)
  563. else:
  564. value = ""
  565. txt = headertitle + ': ' + mangle(value)
  566. draw_text(ctx, txt, TEXT_COLOR, off_x, header_y)
  567. dur = duration / 100.0
  568. txt = 'time : %02d:%05.2f' % (math.floor(dur/60), dur - 60 * math.floor(dur/60))
  569. if headers.get('system.maxpid') is not None:
  570. txt = txt + ' max pid: %s' % (headers.get('system.maxpid'))
  571. header_y += ctx.font_extents()[2]
  572. draw_text (ctx, txt, TEXT_COLOR, off_x, header_y)
  573. return header_y
  574. def draw_processes_recursively(ctx, proc, proc_tree, y, proc_h, rect, clip) :
  575. x = rect[0] + ((proc.start_time - proc_tree.start_time) * rect[2] / proc_tree.duration)
  576. w = ((proc.duration) * rect[2] / proc_tree.duration)
  577. draw_process_activity_colors(ctx, proc, proc_tree, x, y, w, proc_h, rect, clip)
  578. draw_rect(ctx, PROC_BORDER_COLOR, (x, y, w, proc_h))
  579. ipid = int(proc.pid)
  580. if not OPTIONS.show_all:
  581. cmdString = proc.cmd
  582. else:
  583. cmdString = ''
  584. if (OPTIONS.show_pid or OPTIONS.show_all) and ipid is not 0:
  585. cmdString = cmdString + " [" + str(ipid // 1000) + "]"
  586. if OPTIONS.show_all:
  587. if proc.args:
  588. cmdString = cmdString + " '" + "' '".join(proc.args) + "'"
  589. else:
  590. cmdString = cmdString + " " + proc.exe
  591. draw_label_in_box(ctx, PROC_TEXT_COLOR, cmdString, x, y + proc_h - 4, w, rect[0] + rect[2])
  592. next_y = y + proc_h
  593. for child in proc.child_list:
  594. if next_y > clip[1] + clip[3]:
  595. break
  596. child_x, child_y = draw_processes_recursively(ctx, child, proc_tree, next_y, proc_h, rect, clip)
  597. draw_process_connecting_lines(ctx, x, y, child_x, child_y, proc_h)
  598. next_y = next_y + proc_h * proc_tree.num_nodes([child])
  599. return x, y
  600. def draw_process_activity_colors(ctx, proc, proc_tree, x, y, w, proc_h, rect, clip):
  601. if y > clip[1] + clip[3] or y + proc_h + 2 < clip[1]:
  602. return
  603. draw_fill_rect(ctx, PROC_COLOR_S, (x, y, w, proc_h))
  604. last_tx = -1
  605. for sample in proc.samples :
  606. tx = rect[0] + round(((sample.time - proc_tree.start_time) * rect[2] / proc_tree.duration))
  607. # samples are sorted chronologically
  608. if tx < clip[0]:
  609. continue
  610. if tx > clip[0] + clip[2]:
  611. break
  612. tw = round(proc_tree.sample_period * rect[2] / float(proc_tree.duration))
  613. if last_tx != -1 and abs(last_tx - tx) <= tw:
  614. tw -= last_tx - tx
  615. tx = last_tx
  616. tw = max (tw, 1) # nice to see at least something
  617. last_tx = tx + tw
  618. state = get_proc_state( sample.state )
  619. color = STATE_COLORS[state]
  620. if state == STATE_RUNNING:
  621. alpha = min (sample.cpu_sample.user + sample.cpu_sample.sys, 1.0)
  622. color = tuple(list(PROC_COLOR_R[0:3]) + [alpha])
  623. # print "render time %d [ tx %d tw %d ], sample state %s color %s alpha %g" % (sample.time, tx, tw, state, color, alpha)
  624. elif state == STATE_SLEEPING:
  625. continue
  626. draw_fill_rect(ctx, color, (tx, y, tw, proc_h))
  627. def draw_process_connecting_lines(ctx, px, py, x, y, proc_h):
  628. ctx.set_source_rgba(*DEP_COLOR)
  629. ctx.set_dash([2, 2])
  630. if abs(px - x) < 3:
  631. dep_off_x = 3
  632. dep_off_y = proc_h / 4
  633. ctx.move_to(x, y + proc_h / 2)
  634. ctx.line_to(px - dep_off_x, y + proc_h / 2)
  635. ctx.line_to(px - dep_off_x, py - dep_off_y)
  636. ctx.line_to(px, py - dep_off_y)
  637. else:
  638. ctx.move_to(x, y + proc_h / 2)
  639. ctx.line_to(px, y + proc_h / 2)
  640. ctx.line_to(px, py)
  641. ctx.stroke()
  642. ctx.set_dash([])
  643. # elide the bootchart collector - it is quite distorting
  644. def elide_bootchart(proc):
  645. return proc.cmd == 'bootchartd' or proc.cmd == 'bootchart-colle'
  646. class CumlSample:
  647. def __init__(self, proc):
  648. self.cmd = proc.cmd
  649. self.samples = []
  650. self.merge_samples (proc)
  651. self.color = None
  652. def merge_samples(self, proc):
  653. self.samples.extend (proc.samples)
  654. self.samples.sort (key = lambda p: p.time)
  655. def next(self):
  656. global palette_idx
  657. palette_idx += HSV_STEP
  658. return palette_idx
  659. def get_color(self):
  660. if self.color is None:
  661. i = self.next() % HSV_MAX_MOD
  662. h = 0.0
  663. if i is not 0:
  664. h = (1.0 * i) / HSV_MAX_MOD
  665. s = 0.5
  666. v = 1.0
  667. c = colorsys.hsv_to_rgb (h, s, v)
  668. self.color = (c[0], c[1], c[2], 1.0)
  669. return self.color
  670. def draw_cuml_graph(ctx, proc_tree, chart_bounds, duration, sec_w, stat_type):
  671. global palette_idx
  672. palette_idx = 0
  673. time_hash = {}
  674. total_time = 0.0
  675. m_proc_list = {}
  676. if stat_type is STAT_TYPE_CPU:
  677. sample_value = 'cpu'
  678. else:
  679. sample_value = 'io'
  680. for proc in proc_tree.process_list:
  681. if elide_bootchart(proc):
  682. continue
  683. for sample in proc.samples:
  684. total_time += getattr(sample.cpu_sample, sample_value)
  685. if not sample.time in time_hash:
  686. time_hash[sample.time] = 1
  687. # merge pids with the same cmd
  688. if not proc.cmd in m_proc_list:
  689. m_proc_list[proc.cmd] = CumlSample (proc)
  690. continue
  691. s = m_proc_list[proc.cmd]
  692. s.merge_samples (proc)
  693. # all the sample times
  694. times = sorted(time_hash)
  695. if len (times) < 2:
  696. print("degenerate boot chart")
  697. return
  698. pix_per_ns = chart_bounds[3] / total_time
  699. # print "total time: %g pix-per-ns %g" % (total_time, pix_per_ns)
  700. # FIXME: we have duplicates in the process list too [!] - why !?
  701. # Render bottom up, left to right
  702. below = {}
  703. for time in times:
  704. below[time] = chart_bounds[1] + chart_bounds[3]
  705. # same colors each time we render
  706. random.seed (0)
  707. ctx.set_line_width(1)
  708. legends = []
  709. labels = []
  710. # render each pid in order
  711. for cs in m_proc_list.values():
  712. row = {}
  713. cuml = 0.0
  714. # print "pid : %s -> %g samples %d" % (proc.cmd, cuml, len (cs.samples))
  715. for sample in cs.samples:
  716. cuml += getattr(sample.cpu_sample, sample_value)
  717. row[sample.time] = cuml
  718. process_total_time = cuml
  719. # hide really tiny processes
  720. if cuml * pix_per_ns <= 2:
  721. continue
  722. last_time = times[0]
  723. y = last_below = below[last_time]
  724. last_cuml = cuml = 0.0
  725. ctx.set_source_rgba(*cs.get_color())
  726. for time in times:
  727. render_seg = False
  728. # did the underlying trend increase ?
  729. if below[time] != last_below:
  730. last_below = below[last_time]
  731. last_cuml = cuml
  732. render_seg = True
  733. # did we move up a pixel increase ?
  734. if time in row:
  735. nc = round (row[time] * pix_per_ns)
  736. if nc != cuml:
  737. last_cuml = cuml
  738. cuml = nc
  739. render_seg = True
  740. # if last_cuml > cuml:
  741. # assert fail ... - un-sorted process samples
  742. # draw the trailing rectangle from the last time to
  743. # before now, at the height of the last segment.
  744. if render_seg:
  745. w = math.ceil ((time - last_time) * chart_bounds[2] / proc_tree.duration) + 1
  746. x = chart_bounds[0] + round((last_time - proc_tree.start_time) * chart_bounds[2] / proc_tree.duration)
  747. ctx.rectangle (x, below[last_time] - last_cuml, w, last_cuml)
  748. ctx.fill()
  749. # ctx.stroke()
  750. last_time = time
  751. y = below [time] - cuml
  752. row[time] = y
  753. # render the last segment
  754. x = chart_bounds[0] + round((last_time - proc_tree.start_time) * chart_bounds[2] / proc_tree.duration)
  755. y = below[last_time] - cuml
  756. ctx.rectangle (x, y, chart_bounds[2] - x, cuml)
  757. ctx.fill()
  758. # ctx.stroke()
  759. # render legend if it will fit
  760. if cuml > 8:
  761. label = cs.cmd
  762. extnts = ctx.text_extents(label)
  763. label_w = extnts[2]
  764. label_h = extnts[3]
  765. # print "Text extents %g by %g" % (label_w, label_h)
  766. labels.append((label,
  767. chart_bounds[0] + chart_bounds[2] - label_w - off_x * 2,
  768. y + (cuml + label_h) / 2))
  769. if cs in legends:
  770. print("ARGH - duplicate process in list !")
  771. legends.append ((cs, process_total_time))
  772. below = row
  773. # render grid-lines over the top
  774. draw_box_ticks(ctx, chart_bounds, sec_w)
  775. # render labels
  776. for l in labels:
  777. draw_text(ctx, l[0], TEXT_COLOR, l[1], l[2])
  778. # Render legends
  779. font_height = 20
  780. label_width = 300
  781. LEGENDS_PER_COL = 15
  782. LEGENDS_TOTAL = 45
  783. ctx.set_font_size (TITLE_FONT_SIZE)
  784. dur_secs = duration / 100
  785. cpu_secs = total_time / 1000000000
  786. # misleading - with multiple CPUs ...
  787. # idle = ((dur_secs - cpu_secs) / dur_secs) * 100.0
  788. if stat_type is STAT_TYPE_CPU:
  789. label = "Cumulative CPU usage, by process; total CPU: " \
  790. " %.5g(s) time: %.3g(s)" % (cpu_secs, dur_secs)
  791. else:
  792. label = "Cumulative I/O usage, by process; total I/O: " \
  793. " %.5g(s) time: %.3g(s)" % (cpu_secs, dur_secs)
  794. draw_text(ctx, label, TEXT_COLOR, chart_bounds[0] + off_x,
  795. chart_bounds[1] + font_height)
  796. i = 0
  797. legends = sorted(legends, key=itemgetter(1), reverse=True)
  798. ctx.set_font_size(TEXT_FONT_SIZE)
  799. for t in legends:
  800. cs = t[0]
  801. time = t[1]
  802. x = chart_bounds[0] + off_x + int (i/LEGENDS_PER_COL) * label_width
  803. y = chart_bounds[1] + font_height * ((i % LEGENDS_PER_COL) + 2)
  804. str = "%s - %.0f(ms) (%2.2f%%)" % (cs.cmd, time/1000000, (time/total_time) * 100.0)
  805. draw_legend_box(ctx, str, cs.color, x, y, leg_s)
  806. i = i + 1
  807. if i >= LEGENDS_TOTAL:
  808. break