graph-build-time 9.7 KB

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