]> rtime.felk.cvut.cz Git - coffee/buildroot.git/blob - support/scripts/size-stats
85e7c1596b2df67e9648b29b4633c2ce571decc6
[coffee/buildroot.git] / support / scripts / size-stats
1 #!/usr/bin/env python
2
3 # Copyright (C) 2014 by Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
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
19 import sys
20 import os
21 import os.path
22 import argparse
23 import csv
24 import collections
25
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)
34
35 colors = ['#e60004', '#009836', '#2e1d86', '#ffed00',
36           '#0068b5', '#f28e00', '#940084', '#97c000']
37
38 #
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.
43 #
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
48 #
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)
56
57 #
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.
62 #
63 # builddir: path to the Buildroot output directory
64 #
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
75
76 #
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.
80 #
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.
85 #
86 # builddir: path to the Buildroot output directory
87 #
88 def build_package_size(filesdict, builddir):
89     pkgsize = collections.defaultdict(int)
90
91     seeninodes = set()
92     for root, _, files in os.walk(os.path.join(builddir, "target")):
93         for f in files:
94             fpath = os.path.join(root, f)
95             if os.path.islink(fpath):
96                 continue
97
98             st = os.stat(fpath)
99             if st.st_ino in seeninodes:
100                 # hard link
101                 continue
102             else:
103                 seeninodes.add(st.st_ino)
104
105             frelpath = os.path.relpath(fpath, os.path.join(builddir, "target"))
106             if not frelpath in filesdict:
107                 print("WARNING: %s is not part of any package" % frelpath)
108                 pkg = "unknown"
109             else:
110                 pkg = filesdict[frelpath][0]
111
112             pkgsize[pkg] += st.st_size
113
114     return pkgsize
115
116 #
117 # Given a dict returned by build_package_size(), this function
118 # generates a pie chart of the size installed by each package.
119 #
120 # pkgsize: dictionary with the name of the package as a key, and the
121 # size as the value, as returned by build_package_size.
122 #
123 # outputf: output file for the graph
124 #
125 def draw_graph(pkgsize, outputf):
126     total = sum(pkgsize.values())
127     labels = []
128     values = []
129     other_value = 0
130     for (p, sz) in sorted(pkgsize.items(), key=lambda x: x[1]):
131         if sz < (total * 0.01):
132             other_value += sz
133         else:
134             labels.append("%s (%d kB)" % (p, sz / 1000.))
135             values.append(sz)
136     labels.append("Other (%d kB)" % (other_value / 1000.))
137     values.append(other_value)
138
139     plt.figure()
140     patches, texts, autotexts = plt.pie(values, labels=labels,
141                                         autopct='%1.1f%%', shadow=True,
142                                         colors=colors)
143     # Reduce text size
144     proptease = fm.FontProperties()
145     proptease.set_size('xx-small')
146     plt.setp(autotexts, fontproperties=proptease)
147     plt.setp(texts, fontproperties=proptease)
148
149     plt.suptitle("Filesystem size per package", fontsize=18, y=.97)
150     plt.title("Total filesystem size: %d kB" % (total / 1000.), fontsize=10, y=.96)
151     plt.savefig(outputf)
152
153 #
154 # Generate a CSV file with statistics about the size of each file, its
155 # size contribution to the package and to the overall system.
156 #
157 # filesdict: dictionary with the name of the files as key, and as
158 # value a tuple containing the name of the package to which the files
159 # belongs, and the size of the file. As returned by
160 # build_package_dict.
161 #
162 # pkgsize: dictionary with the name of the package as a key, and the
163 # size as the value, as returned by build_package_size.
164 #
165 # outputf: output CSV file
166 #
167 def gen_files_csv(filesdict, pkgsizes, outputf):
168     total = 0
169     for (p, sz) in pkgsizes.items():
170         total += sz
171     with open(outputf, 'w') as csvfile:
172         wr = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_MINIMAL)
173         wr.writerow(["File name",
174                      "Package name",
175                      "File size",
176                      "Package size",
177                      "File size in package (%)",
178                      "File size in system (%)"])
179         for f, (pkgname, filesize) in filesdict.items():
180             pkgsize = pkgsizes[pkgname]
181
182             if pkgsize == 0:
183                 percent_pkg = 0
184             else:
185                 percent_pkg = float(filesize) / pkgsize * 100
186
187             percent_total = float(filesize) / total * 100
188
189             wr.writerow([f, pkgname, filesize, pkgsize,
190                          "%.1f" % percent_pkg,
191                          "%.1f" % percent_total])
192
193
194 #
195 # Generate a CSV file with statistics about the size of each package,
196 # and their size contribution to the overall system.
197 #
198 # pkgsize: dictionary with the name of the package as a key, and the
199 # size as the value, as returned by build_package_size.
200 #
201 # outputf: output CSV file
202 #
203 def gen_packages_csv(pkgsizes, outputf):
204     total = sum(pkgsizes.values())
205     with open(outputf, 'w') as csvfile:
206         wr = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_MINIMAL)
207         wr.writerow(["Package name", "Package size", "Package size in system (%)"])
208         for (pkg, size) in pkgsizes.items():
209             wr.writerow([pkg, size, "%.1f" % (float(size) / total * 100)])
210
211 parser = argparse.ArgumentParser(description='Draw size statistics graphs')
212
213 parser.add_argument("--builddir", '-i', metavar="BUILDDIR", required=True,
214                     help="Buildroot output directory")
215 parser.add_argument("--graph", '-g', metavar="GRAPH",
216                     help="Graph output file (.pdf or .png extension)")
217 parser.add_argument("--file-size-csv", '-f', metavar="FILE_SIZE_CSV",
218                     help="CSV output file with file size statistics")
219 parser.add_argument("--package-size-csv", '-p', metavar="PKG_SIZE_CSV",
220                     help="CSV output file with package size statistics")
221 args = parser.parse_args()
222
223 # Find out which package installed what files
224 pkgdict = build_package_dict(args.builddir)
225
226 # Collect the size installed by each package
227 pkgsize = build_package_size(pkgdict, args.builddir)
228
229 if args.graph:
230     draw_graph(pkgsize, args.graph)
231 if args.file_size_csv:
232     gen_files_csv(pkgdict, pkgsize, args.file_size_csv)
233 if args.package_size_csv:
234     gen_packages_csv(pkgsize, args.package_size_csv)