graph-build-time 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. # Use the Agg backend (which produces a PNG output, see
  52. # http://matplotlib.org/faq/usage_faq.html#what-is-a-backend),
  53. # otherwise an incorrect backend is used on some host machines).
  54. # Note: matplotlib.use() must be called *before* matplotlib.pyplot.
  55. mpl.use('Agg')
  56. import matplotlib.pyplot as plt
  57. import matplotlib.font_manager as fm
  58. import csv
  59. import argparse
  60. import sys
  61. steps = [ 'extract', 'patch', 'configure', 'build',
  62. 'install-target', 'install-staging', 'install-images',
  63. 'install-host']
  64. default_colors = ['#e60004', '#009836', '#2e1d86', '#ffed00',
  65. '#0068b5', '#f28e00', '#940084', '#97c000']
  66. alternate_colors = ['#00e0e0', '#3f7f7f', '#ff0000', '#00c000',
  67. '#0080ff', '#c000ff', '#00eeee', '#e0e000']
  68. class Package:
  69. def __init__(self, name):
  70. self.name = name
  71. self.steps_duration = {}
  72. self.steps_start = {}
  73. self.steps_end = {}
  74. def add_step(self, step, state, time):
  75. if state == "start":
  76. self.steps_start[step] = time
  77. else:
  78. self.steps_end[step] = time
  79. if step in self.steps_start and step in self.steps_end:
  80. self.steps_duration[step] = self.steps_end[step] - self.steps_start[step]
  81. def get_duration(self, step=None):
  82. if step is None:
  83. duration = 0
  84. for step in list(self.steps_duration.keys()):
  85. duration += self.steps_duration[step]
  86. return duration
  87. if step in self.steps_duration:
  88. return self.steps_duration[step]
  89. return 0
  90. # Generate an histogram of the time spent in each step of each
  91. # package.
  92. def pkg_histogram(data, output, order="build"):
  93. n_pkgs = len(data)
  94. ind = numpy.arange(n_pkgs)
  95. if order == "duration":
  96. data = sorted(data, key=lambda p: p.get_duration(), reverse=True)
  97. elif order == "name":
  98. data = sorted(data, key=lambda p: p.name, reverse=False)
  99. # Prepare the vals array, containing one entry for each step
  100. vals = []
  101. for step in steps:
  102. val = []
  103. for p in data:
  104. val.append(p.get_duration(step))
  105. vals.append(val)
  106. bottom = [0] * n_pkgs
  107. legenditems = []
  108. plt.figure()
  109. # Draw the bars, step by step
  110. for i in range(0, len(vals)):
  111. b = plt.bar(ind+0.1, vals[i], width=0.8, color=colors[i], bottom=bottom, linewidth=0.25)
  112. legenditems.append(b[0])
  113. bottom = [ bottom[j] + vals[i][j] for j in range(0, len(vals[i])) ]
  114. # Draw the package names
  115. plt.xticks(ind + .6, [ p.name for p in data ], rotation=-60, rotation_mode="anchor", fontsize=8, ha='left')
  116. # Adjust size of graph depending on the number of packages
  117. # Ensure a minimal size twice as the default
  118. # Magic Numbers do Magic Layout!
  119. ratio = max(((n_pkgs + 10) / 48, 2))
  120. borders = 0.1 / ratio
  121. sz = plt.gcf().get_figwidth()
  122. plt.gcf().set_figwidth(sz * ratio)
  123. # Adjust space at borders, add more space for the
  124. # package names at the bottom
  125. plt.gcf().subplots_adjust(bottom=0.2, left=borders, right=1-borders)
  126. # Remove ticks in the graph for each package
  127. axes = plt.gcf().gca()
  128. for line in axes.get_xticklines():
  129. line.set_markersize(0)
  130. axes.set_ylabel('Time (seconds)')
  131. # Reduce size of legend text
  132. leg_prop = fm.FontProperties(size=6)
  133. # Draw legend
  134. plt.legend(legenditems, steps, prop=leg_prop)
  135. if order == "name":
  136. plt.title('Build time of packages\n')
  137. elif order == "build":
  138. plt.title('Build time of packages, by build order\n')
  139. elif order == "duration":
  140. plt.title('Build time of packages, by duration order\n')
  141. # Save graph
  142. plt.savefig(output)
  143. # Generate a pie chart with the time spent building each package.
  144. def pkg_pie_time_per_package(data, output):
  145. # Compute total build duration
  146. total = 0
  147. for p in data:
  148. total += p.get_duration()
  149. # Build the list of labels and values, and filter the packages
  150. # that account for less than 1% of the build time.
  151. labels = []
  152. values = []
  153. other_value = 0
  154. for p in data:
  155. if p.get_duration() < (total * 0.01):
  156. other_value += p.get_duration()
  157. else:
  158. labels.append(p.name)
  159. values.append(p.get_duration())
  160. labels.append('Other')
  161. values.append(other_value)
  162. plt.figure()
  163. # Draw pie graph
  164. patches, texts, autotexts = plt.pie(values, labels=labels,
  165. autopct='%1.1f%%', shadow=True,
  166. colors=colors)
  167. # Reduce text size
  168. proptease = fm.FontProperties()
  169. proptease.set_size('xx-small')
  170. plt.setp(autotexts, fontproperties=proptease)
  171. plt.setp(texts, fontproperties=proptease)
  172. plt.title('Build time per package')
  173. plt.savefig(output)
  174. # Generate a pie chart with a portion for the overall time spent in
  175. # each step for all packages.
  176. def pkg_pie_time_per_step(data, output):
  177. steps_values = []
  178. for step in steps:
  179. val = 0
  180. for p in data:
  181. val += p.get_duration(step)
  182. steps_values.append(val)
  183. plt.figure()
  184. # Draw pie graph
  185. patches, texts, autotexts = plt.pie(steps_values, labels=steps,
  186. autopct='%1.1f%%', shadow=True,
  187. colors=colors)
  188. # Reduce text size
  189. proptease = fm.FontProperties()
  190. proptease.set_size('xx-small')
  191. plt.setp(autotexts, fontproperties=proptease)
  192. plt.setp(texts, fontproperties=proptease)
  193. plt.title('Build time per step')
  194. plt.savefig(output)
  195. # Parses the csv file passed on standard input and returns a list of
  196. # Package objects, filed with the duration of each step and the total
  197. # duration of the package.
  198. def read_data(input_file):
  199. if input_file is None:
  200. input_file = sys.stdin
  201. else:
  202. input_file = open(input_file)
  203. reader = csv.reader(input_file, delimiter=':')
  204. pkgs = []
  205. # Auxilliary function to find a package by name in the list.
  206. def getpkg(name):
  207. for p in pkgs:
  208. if p.name == name:
  209. return p
  210. return None
  211. for row in reader:
  212. time = int(row[0].strip())
  213. state = row[1].strip()
  214. step = row[2].strip()
  215. pkg = row[3].strip()
  216. p = getpkg(pkg)
  217. if p is None:
  218. p = Package(pkg)
  219. pkgs.append(p)
  220. p.add_step(step, state, time)
  221. return pkgs
  222. parser = argparse.ArgumentParser(description='Draw build time graphs')
  223. parser.add_argument("--type", '-t', metavar="GRAPH_TYPE",
  224. help="Type of graph (histogram, pie-packages, pie-steps)")
  225. parser.add_argument("--order", '-O', metavar="GRAPH_ORDER",
  226. help="Ordering of packages: build or duration (for histogram only)")
  227. parser.add_argument("--alternate-colors", '-c', action="store_true",
  228. help="Use alternate colour-scheme")
  229. parser.add_argument("--input", '-i', metavar="OUTPUT",
  230. help="Input file (usually $(O)/build/build-time.log)")
  231. parser.add_argument("--output", '-o', metavar="OUTPUT", required=True,
  232. help="Output file (.pdf or .png extension)")
  233. args = parser.parse_args()
  234. d = read_data(args.input)
  235. if args.alternate_colors:
  236. colors = alternate_colors
  237. else:
  238. colors = default_colors
  239. if args.type == "histogram" or args.type is None:
  240. if args.order == "build" or args.order == "duration" or args.order == "name":
  241. pkg_histogram(d, args.output, args.order)
  242. elif args.order is None:
  243. pkg_histogram(d, args.output, "name")
  244. else:
  245. sys.stderr.write("Unknown ordering: %s\n" % args.order)
  246. exit(1)
  247. elif args.type == "pie-packages":
  248. pkg_pie_time_per_package(d, args.output)
  249. elif args.type == "pie-steps":
  250. pkg_pie_time_per_step(d, args.output)
  251. else:
  252. sys.stderr.write("Unknown type: %s\n" % args.type)
  253. exit(1)