2
1

graph-build-time 9.5 KB

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