graph-build-time 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 == 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 (double the width)
  112. sz = plt.gcf().get_size_inches()
  113. plt.gcf().set_size_inches(sz[0] * 2, sz[1])
  114. # Add more space for the package names at the bottom
  115. plt.gcf().subplots_adjust(bottom=0.2)
  116. # Remove ticks in the graph for each package
  117. axes = plt.gcf().gca()
  118. for line in axes.get_xticklines():
  119. line.set_markersize(0)
  120. axes.set_ylabel('Time (seconds)')
  121. # Reduce size of legend text
  122. leg_prop = fm.FontProperties(size=6)
  123. # Draw legend
  124. plt.legend(legenditems, steps, prop=leg_prop)
  125. if order == "name":
  126. plt.title('Build time of packages\n')
  127. elif order == "build":
  128. plt.title('Build time of packages, by build order\n')
  129. elif order == "duration":
  130. plt.title('Build time of packages, by duration order\n')
  131. # Save graph
  132. plt.savefig(output)
  133. # Generate a pie chart with the time spent building each package.
  134. def pkg_pie_time_per_package(data, output):
  135. # Compute total build duration
  136. total = 0
  137. for p in data:
  138. total += p.get_duration()
  139. # Build the list of labels and values, and filter the packages
  140. # that account for less than 1% of the build time.
  141. labels = []
  142. values = []
  143. other_value = 0
  144. for p in data:
  145. if p.get_duration() < (total * 0.01):
  146. other_value += p.get_duration()
  147. else:
  148. labels.append(p.name)
  149. values.append(p.get_duration())
  150. labels.append('Other')
  151. values.append(other_value)
  152. plt.figure()
  153. # Draw pie graph
  154. patches, texts, autotexts = plt.pie(values, labels=labels,
  155. autopct='%1.1f%%', shadow=True,
  156. colors=colors)
  157. # Reduce text size
  158. proptease = fm.FontProperties()
  159. proptease.set_size('xx-small')
  160. plt.setp(autotexts, fontproperties=proptease)
  161. plt.setp(texts, fontproperties=proptease)
  162. plt.title('Build time per package')
  163. plt.savefig(output)
  164. # Generate a pie chart with a portion for the overall time spent in
  165. # each step for all packages.
  166. def pkg_pie_time_per_step(data, output):
  167. steps_values = []
  168. for step in steps:
  169. val = 0
  170. for p in data:
  171. val += p.get_duration(step)
  172. steps_values.append(val)
  173. plt.figure()
  174. # Draw pie graph
  175. patches, texts, autotexts = plt.pie(steps_values, labels=steps,
  176. autopct='%1.1f%%', shadow=True,
  177. colors=colors)
  178. # Reduce text size
  179. proptease = fm.FontProperties()
  180. proptease.set_size('xx-small')
  181. plt.setp(autotexts, fontproperties=proptease)
  182. plt.setp(texts, fontproperties=proptease)
  183. plt.title('Build time per step')
  184. plt.savefig(output)
  185. # Parses the csv file passed on standard input and returns a list of
  186. # Package objects, filed with the duration of each step and the total
  187. # duration of the package.
  188. def read_data(input_file):
  189. if input_file is None:
  190. input_file = sys.stdin
  191. else:
  192. input_file = open(input_file)
  193. reader = csv.reader(input_file, delimiter=':')
  194. pkgs = []
  195. # Auxilliary function to find a package by name in the list.
  196. def getpkg(name):
  197. for p in pkgs:
  198. if p.name == name:
  199. return p
  200. return None
  201. for row in reader:
  202. time = int(row[0].strip())
  203. state = row[1].strip()
  204. step = row[2].strip()
  205. pkg = row[3].strip()
  206. p = getpkg(pkg)
  207. if p is None:
  208. p = Package(pkg)
  209. pkgs.append(p)
  210. p.add_step(step, state, time)
  211. return pkgs
  212. parser = argparse.ArgumentParser(description='Draw build time graphs')
  213. parser.add_argument("--type", '-t', metavar="GRAPH_TYPE",
  214. help="Type of graph (histogram, pie-packages, pie-steps)")
  215. parser.add_argument("--order", '-O', metavar="GRAPH_ORDER",
  216. help="Ordering of packages: build or duration (for histogram only)")
  217. parser.add_argument("--alternate-colors", '-c', action="store_true",
  218. help="Use alternate colour-scheme")
  219. parser.add_argument("--input", '-i', metavar="OUTPUT",
  220. help="Input file (usually $(O)/build/build-time.log)")
  221. parser.add_argument("--output", '-o', metavar="OUTPUT", required=True,
  222. help="Output file (.pdf or .png extension)")
  223. args = parser.parse_args()
  224. d = read_data(args.input)
  225. if args.alternate_colors:
  226. colors = alternate_colors
  227. else:
  228. colors = default_colors
  229. if args.type == "histogram" or args.type == None:
  230. if args.order == "build" or args.order == "duration" or args.order == "name":
  231. pkg_histogram(d, args.output, args.order)
  232. elif args.order == None:
  233. pkg_histogram(d, args.output, "name")
  234. else:
  235. sys.stderr.write("Unknown ordering: %s\n" % args.order)
  236. exit(1)
  237. elif args.type == "pie-packages":
  238. pkg_pie_time_per_package(d, args.output)
  239. elif args.type == "pie-steps":
  240. pkg_pie_time_per_step(d, args.output)
  241. else:
  242. sys.stderr.write("Unknown type: %s\n" % args.type)
  243. exit(1)