graph-build-time 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #!/usr/bin/env python
  2. # Copyright (C) 2011 by Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
  3. # Copyright (C) 2013 by Yann E. MORIN <yann.morin.1998@free.fr>
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. # This script generates graphs of packages build time, from the timing
  19. # data generated by Buildroot in the $(O)/build-time.log file.
  20. #
  21. # Example usage:
  22. #
  23. # cat $(O)/build-time.log | ./support/scripts/graph-build-time --type=histogram --output=foobar.pdf
  24. #
  25. # Three graph types are available :
  26. #
  27. # * histogram, which creates an histogram of the build time for each
  28. # package, decomposed by each step (extract, patch, configure,
  29. # etc.). The order in which the packages are shown is
  30. # configurable: by package name, by build order, or by duration
  31. # order. See the --order option.
  32. #
  33. # * pie-packages, which creates a pie chart of the build time of
  34. # each package (without decomposition in steps). Packages that
  35. # contributed to less than 1% of the overall build time are all
  36. # grouped together in an "Other" entry.
  37. #
  38. # * pie-steps, which creates a pie chart of the time spent globally
  39. # on each step (extract, patch, configure, etc...)
  40. #
  41. # The default is to generate an histogram ordered by package name.
  42. #
  43. # Requirements:
  44. #
  45. # * matplotlib (python-matplotlib on Debian/Ubuntu systems)
  46. # * numpy (python-numpy on Debian/Ubuntu systems)
  47. # * argparse (by default in Python 2.7, requires python-argparse if
  48. # Python 2.6 is used)
  49. import matplotlib as mpl
  50. import numpy
  51. mpl.use('Agg')
  52. import matplotlib.pyplot as plt
  53. import matplotlib.font_manager as fm
  54. import csv
  55. import argparse
  56. import sys
  57. steps = [ 'extract', 'patch', 'configure', 'build',
  58. 'install-target', 'install-staging', 'install-images',
  59. 'install-host']
  60. default_colors = ['#e60004', '#009836', '#2e1d86', '#ffed00',
  61. '#0068b5', '#f28e00', '#940084', '#97c000']
  62. alternate_colors = ['#00e0e0', '#3f7f7f', '#ff0000', '#00c000',
  63. '#0080ff', '#c000ff', '#00eeee', '#e0e000']
  64. class Package:
  65. def __init__(self, name):
  66. self.name = name
  67. self.steps_duration = {}
  68. self.steps_start = {}
  69. self.steps_end = {}
  70. def add_step(self, step, state, time):
  71. if state == "start":
  72. self.steps_start[step] = time
  73. else:
  74. self.steps_end[step] = time
  75. if step in self.steps_start and step in self.steps_end:
  76. self.steps_duration[step] = self.steps_end[step] - self.steps_start[step]
  77. def get_duration(self, step=None):
  78. if step is None:
  79. duration = 0
  80. for step in list(self.steps_duration.keys()):
  81. duration += self.steps_duration[step]
  82. return duration
  83. if step in self.steps_duration:
  84. return self.steps_duration[step]
  85. return 0
  86. # Generate an histogram of the time spent in each step of each
  87. # package.
  88. def pkg_histogram(data, output, order="build"):
  89. n_pkgs = len(data)
  90. ind = numpy.arange(n_pkgs)
  91. if order == "duration":
  92. data = sorted(data, key=lambda p: p.get_duration(), reverse=True)
  93. elif order == "name":
  94. data = sorted(data, key=lambda p: p.name, reverse=False)
  95. # Prepare the vals array, containing one entry for each step
  96. vals = []
  97. for step in steps:
  98. val = []
  99. for p in data:
  100. val.append(p.get_duration(step))
  101. vals.append(val)
  102. bottom = [0] * n_pkgs
  103. legenditems = []
  104. plt.figure()
  105. # Draw the bars, step by step
  106. for i in range(0, len(vals)):
  107. b = plt.bar(ind+0.1, vals[i], width=0.8, color=colors[i], bottom=bottom, linewidth=0.25)
  108. legenditems.append(b[0])
  109. bottom = [ bottom[j] + vals[i][j] for j in range(0, len(vals[i])) ]
  110. # Draw the package names
  111. plt.xticks(ind + .6, [ p.name for p in data ], rotation=-60, rotation_mode="anchor", fontsize=8, ha='left')
  112. # Adjust size of graph depending on the number of packages
  113. # Ensure a minimal size twice as the default
  114. # Magic Numbers do Magic Layout!
  115. ratio = max(((n_pkgs + 10) / 48, 2))
  116. borders = 0.1 / ratio
  117. sz = plt.gcf().get_figwidth()
  118. plt.gcf().set_figwidth(sz * ratio)
  119. # Adjust space at borders, add more space for the
  120. # package names at the bottom
  121. plt.gcf().subplots_adjust(bottom=0.2, left=borders, right=1-borders)
  122. # Remove ticks in the graph for each package
  123. axes = plt.gcf().gca()
  124. for line in axes.get_xticklines():
  125. line.set_markersize(0)
  126. axes.set_ylabel('Time (seconds)')
  127. # Reduce size of legend text
  128. leg_prop = fm.FontProperties(size=6)
  129. # Draw legend
  130. plt.legend(legenditems, steps, prop=leg_prop)
  131. if order == "name":
  132. plt.title('Build time of packages\n')
  133. elif order == "build":
  134. plt.title('Build time of packages, by build order\n')
  135. elif order == "duration":
  136. plt.title('Build time of packages, by duration order\n')
  137. # Save graph
  138. plt.savefig(output)
  139. # Generate a pie chart with the time spent building each package.
  140. def pkg_pie_time_per_package(data, output):
  141. # Compute total build duration
  142. total = 0
  143. for p in data:
  144. total += p.get_duration()
  145. # Build the list of labels and values, and filter the packages
  146. # that account for less than 1% of the build time.
  147. labels = []
  148. values = []
  149. other_value = 0
  150. for p in data:
  151. if p.get_duration() < (total * 0.01):
  152. other_value += p.get_duration()
  153. else:
  154. labels.append(p.name)
  155. values.append(p.get_duration())
  156. labels.append('Other')
  157. values.append(other_value)
  158. plt.figure()
  159. # Draw pie graph
  160. patches, texts, autotexts = plt.pie(values, labels=labels,
  161. autopct='%1.1f%%', shadow=True,
  162. colors=colors)
  163. # Reduce text size
  164. proptease = fm.FontProperties()
  165. proptease.set_size('xx-small')
  166. plt.setp(autotexts, fontproperties=proptease)
  167. plt.setp(texts, fontproperties=proptease)
  168. plt.title('Build time per package')
  169. plt.savefig(output)
  170. # Generate a pie chart with a portion for the overall time spent in
  171. # each step for all packages.
  172. def pkg_pie_time_per_step(data, output):
  173. steps_values = []
  174. for step in steps:
  175. val = 0
  176. for p in data:
  177. val += p.get_duration(step)
  178. steps_values.append(val)
  179. plt.figure()
  180. # Draw pie graph
  181. patches, texts, autotexts = plt.pie(steps_values, labels=steps,
  182. autopct='%1.1f%%', shadow=True,
  183. colors=colors)
  184. # Reduce text size
  185. proptease = fm.FontProperties()
  186. proptease.set_size('xx-small')
  187. plt.setp(autotexts, fontproperties=proptease)
  188. plt.setp(texts, fontproperties=proptease)
  189. plt.title('Build time per step')
  190. plt.savefig(output)
  191. # Parses the csv file passed on standard input and returns a list of
  192. # Package objects, filed with the duration of each step and the total
  193. # duration of the package.
  194. def read_data(input_file):
  195. if input_file is None:
  196. input_file = sys.stdin
  197. else:
  198. input_file = open(input_file)
  199. reader = csv.reader(input_file, delimiter=':')
  200. pkgs = []
  201. # Auxilliary function to find a package by name in the list.
  202. def getpkg(name):
  203. for p in pkgs:
  204. if p.name == name:
  205. return p
  206. return None
  207. for row in reader:
  208. time = int(row[0].strip())
  209. state = row[1].strip()
  210. step = row[2].strip()
  211. pkg = row[3].strip()
  212. p = getpkg(pkg)
  213. if p is None:
  214. p = Package(pkg)
  215. pkgs.append(p)
  216. p.add_step(step, state, time)
  217. return pkgs
  218. parser = argparse.ArgumentParser(description='Draw build time graphs')
  219. parser.add_argument("--type", '-t', metavar="GRAPH_TYPE",
  220. help="Type of graph (histogram, pie-packages, pie-steps)")
  221. parser.add_argument("--order", '-O', metavar="GRAPH_ORDER",
  222. help="Ordering of packages: build or duration (for histogram only)")
  223. parser.add_argument("--alternate-colors", '-c', action="store_true",
  224. help="Use alternate colour-scheme")
  225. parser.add_argument("--input", '-i', metavar="OUTPUT",
  226. help="Input file (usually $(O)/build/build-time.log)")
  227. parser.add_argument("--output", '-o', metavar="OUTPUT", required=True,
  228. help="Output file (.pdf or .png extension)")
  229. args = parser.parse_args()
  230. d = read_data(args.input)
  231. if args.alternate_colors:
  232. colors = alternate_colors
  233. else:
  234. colors = default_colors
  235. if args.type == "histogram" or args.type is None:
  236. if args.order == "build" or args.order == "duration" or args.order == "name":
  237. pkg_histogram(d, args.output, args.order)
  238. elif args.order is None:
  239. pkg_histogram(d, args.output, "name")
  240. else:
  241. sys.stderr.write("Unknown ordering: %s\n" % args.order)
  242. exit(1)
  243. elif args.type == "pie-packages":
  244. pkg_pie_time_per_package(d, args.output)
  245. elif args.type == "pie-steps":
  246. pkg_pie_time_per_step(d, args.output)
  247. else:
  248. sys.stderr.write("Unknown type: %s\n" % args.type)
  249. exit(1)