janus-gateway: make echo test plugin optional
[buildroot-gz.git] / support / scripts / size-stats
blob0ddcc0790217a3e7623d8539fb55bb2a4f3e308e
1 #!/usr/bin/env python
3 # Copyright (C) 2014 by Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
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.
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.
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
19 import sys
20 import os
21 import os.path
22 import argparse
23 import csv
24 import collections
26 try:
27 import matplotlib
28 matplotlib.use('Agg')
29 import matplotlib.font_manager as fm
30 import matplotlib.pyplot as plt
31 except ImportError:
32 sys.stderr.write("You need python-matplotlib to generate the size graph\n")
33 exit(1)
35 colors = ['#e60004', '#009836', '#2e1d86', '#ffed00',
36 '#0068b5', '#f28e00', '#940084', '#97c000']
39 # This function adds a new file to 'filesdict', after checking its
40 # size. The 'filesdict' contain the relative path of the file as the
41 # key, and as the value a tuple containing the name of the package to
42 # which the file belongs and the size of the file.
44 # filesdict: the dict to which the file is added
45 # relpath: relative path of the file
46 # fullpath: absolute path to the file
47 # pkg: package to which the file belongs
49 def add_file(filesdict, relpath, abspath, pkg):
50 if not os.path.exists(abspath):
51 return
52 if os.path.islink(abspath):
53 return
54 sz = os.stat(abspath).st_size
55 filesdict[relpath] = (pkg, sz)
58 # This function returns a dict where each key is the path of a file in
59 # the root filesystem, and the value is a tuple containing two
60 # elements: the name of the package to which this file belongs and the
61 # size of the file.
63 # builddir: path to the Buildroot output directory
65 def build_package_dict(builddir):
66 filesdict = {}
67 with open(os.path.join(builddir, "build", "packages-file-list.txt")) as filelistf:
68 for l in filelistf.readlines():
69 pkg, fpath = l.split(",", 1)
70 # remove the initial './' in each file path
71 fpath = fpath.strip()[2:]
72 fullpath = os.path.join(builddir, "target", fpath)
73 add_file(filesdict, fpath, fullpath, pkg)
74 return filesdict
77 # This function builds a dictionary that contains the name of a
78 # package as key, and the size of the files installed by this package
79 # as the value.
81 # filesdict: dictionary with the name of the files as key, and as
82 # value a tuple containing the name of the package to which the files
83 # belongs, and the size of the file. As returned by
84 # build_package_dict.
86 # builddir: path to the Buildroot output directory
88 def build_package_size(filesdict, builddir):
89 pkgsize = collections.defaultdict(int)
91 for root, _, files in os.walk(os.path.join(builddir, "target")):
92 for f in files:
93 fpath = os.path.join(root, f)
94 if os.path.islink(fpath):
95 continue
96 frelpath = os.path.relpath(fpath, os.path.join(builddir, "target"))
97 if not frelpath in filesdict:
98 print("WARNING: %s is not part of any package" % frelpath)
99 pkg = "unknown"
100 else:
101 pkg = filesdict[frelpath][0]
103 pkgsize[pkg] += os.path.getsize(fpath)
105 return pkgsize
108 # Given a dict returned by build_package_size(), this function
109 # generates a pie chart of the size installed by each package.
111 # pkgsize: dictionary with the name of the package as a key, and the
112 # size as the value, as returned by build_package_size.
114 # outputf: output file for the graph
116 def draw_graph(pkgsize, outputf):
117 total = sum(pkgsize.values())
118 labels = []
119 values = []
120 other_value = 0
121 for (p, sz) in pkgsize.items():
122 if sz < (total * 0.01):
123 other_value += sz
124 else:
125 labels.append("%s (%d kB)" % (p, sz / 1000.))
126 values.append(sz)
127 labels.append("Other (%d kB)" % (other_value / 1000.))
128 values.append(other_value)
130 plt.figure()
131 patches, texts, autotexts = plt.pie(values, labels=labels,
132 autopct='%1.1f%%', shadow=True,
133 colors=colors)
134 # Reduce text size
135 proptease = fm.FontProperties()
136 proptease.set_size('xx-small')
137 plt.setp(autotexts, fontproperties=proptease)
138 plt.setp(texts, fontproperties=proptease)
140 plt.suptitle("Filesystem size per package", fontsize=18, y=.97)
141 plt.title("Total filesystem size: %d kB" % (total / 1000.), fontsize=10, y=.96)
142 plt.savefig(outputf)
145 # Generate a CSV file with statistics about the size of each file, its
146 # size contribution to the package and to the overall system.
148 # filesdict: dictionary with the name of the files as key, and as
149 # value a tuple containing the name of the package to which the files
150 # belongs, and the size of the file. As returned by
151 # build_package_dict.
153 # pkgsize: dictionary with the name of the package as a key, and the
154 # size as the value, as returned by build_package_size.
156 # outputf: output CSV file
158 def gen_files_csv(filesdict, pkgsizes, outputf):
159 total = 0
160 for (p, sz) in pkgsizes.items():
161 total += sz
162 with open(outputf, 'w') as csvfile:
163 wr = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_MINIMAL)
164 wr.writerow(["File name",
165 "Package name",
166 "File size",
167 "Package size",
168 "File size in package (%)",
169 "File size in system (%)"])
170 for f, (pkgname, filesize) in filesdict.items():
171 pkgsize = pkgsizes[pkgname]
172 wr.writerow([f, pkgname, filesize, pkgsize,
173 "%.1f" % (float(filesize) / pkgsize * 100),
174 "%.1f" % (float(filesize) / total * 100)])
178 # Generate a CSV file with statistics about the size of each package,
179 # and their size contribution to the overall system.
181 # pkgsize: dictionary with the name of the package as a key, and the
182 # size as the value, as returned by build_package_size.
184 # outputf: output CSV file
186 def gen_packages_csv(pkgsizes, outputf):
187 total = sum(pkgsizes.values())
188 with open(outputf, 'w') as csvfile:
189 wr = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_MINIMAL)
190 wr.writerow(["Package name", "Package size", "Package size in system (%)"])
191 for (pkg, size) in pkgsizes.items():
192 wr.writerow([pkg, size, "%.1f" % (float(size) / total * 100)])
194 parser = argparse.ArgumentParser(description='Draw size statistics graphs')
196 parser.add_argument("--builddir", '-i', metavar="BUILDDIR", required=True,
197 help="Buildroot output directory")
198 parser.add_argument("--graph", '-g', metavar="GRAPH",
199 help="Graph output file (.pdf or .png extension)")
200 parser.add_argument("--file-size-csv", '-f', metavar="FILE_SIZE_CSV",
201 help="CSV output file with file size statistics")
202 parser.add_argument("--package-size-csv", '-p', metavar="PKG_SIZE_CSV",
203 help="CSV output file with package size statistics")
204 args = parser.parse_args()
206 # Find out which package installed what files
207 pkgdict = build_package_dict(args.builddir)
209 # Collect the size installed by each package
210 pkgsize = build_package_size(pkgdict, args.builddir)
212 if args.graph:
213 draw_graph(pkgsize, args.graph)
214 if args.file_size_csv:
215 gen_files_csv(pkgdict, pkgsize, args.file_size_csv)
216 if args.package_size_csv:
217 gen_packages_csv(pkgsize, args.package_size_csv)