1*4882a593Smuzhiyun#!/usr/bin/env python3 2*4882a593Smuzhiyun 3*4882a593Smuzhiyun# Copyright (C) 2014 by Thomas Petazzoni <thomas.petazzoni@free-electrons.com> 4*4882a593Smuzhiyun 5*4882a593Smuzhiyun# This program is free software; you can redistribute it and/or modify 6*4882a593Smuzhiyun# it under the terms of the GNU General Public License as published by 7*4882a593Smuzhiyun# the Free Software Foundation; either version 2 of the License, or 8*4882a593Smuzhiyun# (at your option) any later version. 9*4882a593Smuzhiyun# 10*4882a593Smuzhiyun# This program is distributed in the hope that it will be useful, 11*4882a593Smuzhiyun# but WITHOUT ANY WARRANTY; without even the implied warranty of 12*4882a593Smuzhiyun# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13*4882a593Smuzhiyun# General Public License for more details. 14*4882a593Smuzhiyun# 15*4882a593Smuzhiyun# You should have received a copy of the GNU General Public License 16*4882a593Smuzhiyun# along with this program; if not, write to the Free Software 17*4882a593Smuzhiyun# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18*4882a593Smuzhiyun 19*4882a593Smuzhiyunimport sys 20*4882a593Smuzhiyunimport os 21*4882a593Smuzhiyunimport os.path 22*4882a593Smuzhiyunimport argparse 23*4882a593Smuzhiyunimport csv 24*4882a593Smuzhiyunimport collections 25*4882a593Smuzhiyunimport math 26*4882a593Smuzhiyun 27*4882a593Smuzhiyuntry: 28*4882a593Smuzhiyun import matplotlib 29*4882a593Smuzhiyun matplotlib.use('Agg') 30*4882a593Smuzhiyun import matplotlib.font_manager as fm 31*4882a593Smuzhiyun import matplotlib.pyplot as plt 32*4882a593Smuzhiyunexcept ImportError: 33*4882a593Smuzhiyun sys.stderr.write("You need python-matplotlib to generate the size graph\n") 34*4882a593Smuzhiyun exit(1) 35*4882a593Smuzhiyun 36*4882a593Smuzhiyun 37*4882a593Smuzhiyunclass Config: 38*4882a593Smuzhiyun biggest_first = False 39*4882a593Smuzhiyun iec = False 40*4882a593Smuzhiyun size_limit = 0.01 41*4882a593Smuzhiyun colors = ['#e60004', '#f28e00', '#ffed00', '#940084', 42*4882a593Smuzhiyun '#2e1d86', '#0068b5', '#009836', '#97c000'] 43*4882a593Smuzhiyun 44*4882a593Smuzhiyun 45*4882a593Smuzhiyun# 46*4882a593Smuzhiyun# This function adds a new file to 'filesdict', after checking its 47*4882a593Smuzhiyun# size. The 'filesdict' contain the relative path of the file as the 48*4882a593Smuzhiyun# key, and as the value a tuple containing the name of the package to 49*4882a593Smuzhiyun# which the file belongs and the size of the file. 50*4882a593Smuzhiyun# 51*4882a593Smuzhiyun# filesdict: the dict to which the file is added 52*4882a593Smuzhiyun# relpath: relative path of the file 53*4882a593Smuzhiyun# fullpath: absolute path to the file 54*4882a593Smuzhiyun# pkg: package to which the file belongs 55*4882a593Smuzhiyun# 56*4882a593Smuzhiyundef add_file(filesdict, relpath, abspath, pkg): 57*4882a593Smuzhiyun if not os.path.exists(abspath): 58*4882a593Smuzhiyun return 59*4882a593Smuzhiyun if os.path.islink(abspath): 60*4882a593Smuzhiyun return 61*4882a593Smuzhiyun sz = os.stat(abspath).st_size 62*4882a593Smuzhiyun filesdict[relpath] = (pkg, sz) 63*4882a593Smuzhiyun 64*4882a593Smuzhiyun 65*4882a593Smuzhiyun# 66*4882a593Smuzhiyun# This function returns a dict where each key is the path of a file in 67*4882a593Smuzhiyun# the root filesystem, and the value is a tuple containing two 68*4882a593Smuzhiyun# elements: the name of the package to which this file belongs and the 69*4882a593Smuzhiyun# size of the file. 70*4882a593Smuzhiyun# 71*4882a593Smuzhiyun# builddir: path to the Buildroot output directory 72*4882a593Smuzhiyun# 73*4882a593Smuzhiyundef build_package_dict(builddir): 74*4882a593Smuzhiyun filesdict = {} 75*4882a593Smuzhiyun with open(os.path.join(builddir, "build", "packages-file-list.txt")) as f: 76*4882a593Smuzhiyun for line in f.readlines(): 77*4882a593Smuzhiyun pkg, fpath = line.split(",", 1) 78*4882a593Smuzhiyun # remove the initial './' in each file path 79*4882a593Smuzhiyun fpath = fpath.strip()[2:] 80*4882a593Smuzhiyun fullpath = os.path.join(builddir, "target", fpath) 81*4882a593Smuzhiyun add_file(filesdict, fpath, fullpath, pkg) 82*4882a593Smuzhiyun return filesdict 83*4882a593Smuzhiyun 84*4882a593Smuzhiyun 85*4882a593Smuzhiyun# 86*4882a593Smuzhiyun# This function builds a dictionary that contains the name of a 87*4882a593Smuzhiyun# package as key, and the size of the files installed by this package 88*4882a593Smuzhiyun# as the value. 89*4882a593Smuzhiyun# 90*4882a593Smuzhiyun# filesdict: dictionary with the name of the files as key, and as 91*4882a593Smuzhiyun# value a tuple containing the name of the package to which the files 92*4882a593Smuzhiyun# belongs, and the size of the file. As returned by 93*4882a593Smuzhiyun# build_package_dict. 94*4882a593Smuzhiyun# 95*4882a593Smuzhiyun# builddir: path to the Buildroot output directory 96*4882a593Smuzhiyun# 97*4882a593Smuzhiyundef build_package_size(filesdict, builddir): 98*4882a593Smuzhiyun pkgsize = collections.defaultdict(int) 99*4882a593Smuzhiyun 100*4882a593Smuzhiyun seeninodes = set() 101*4882a593Smuzhiyun for root, _, files in os.walk(os.path.join(builddir, "target")): 102*4882a593Smuzhiyun for f in files: 103*4882a593Smuzhiyun fpath = os.path.join(root, f) 104*4882a593Smuzhiyun if os.path.islink(fpath): 105*4882a593Smuzhiyun continue 106*4882a593Smuzhiyun 107*4882a593Smuzhiyun st = os.stat(fpath) 108*4882a593Smuzhiyun if st.st_ino in seeninodes: 109*4882a593Smuzhiyun # hard link 110*4882a593Smuzhiyun continue 111*4882a593Smuzhiyun else: 112*4882a593Smuzhiyun seeninodes.add(st.st_ino) 113*4882a593Smuzhiyun 114*4882a593Smuzhiyun frelpath = os.path.relpath(fpath, os.path.join(builddir, "target")) 115*4882a593Smuzhiyun if frelpath not in filesdict: 116*4882a593Smuzhiyun print("WARNING: %s is not part of any package" % frelpath) 117*4882a593Smuzhiyun pkg = "unknown" 118*4882a593Smuzhiyun else: 119*4882a593Smuzhiyun pkg = filesdict[frelpath][0] 120*4882a593Smuzhiyun 121*4882a593Smuzhiyun pkgsize[pkg] += st.st_size 122*4882a593Smuzhiyun 123*4882a593Smuzhiyun return pkgsize 124*4882a593Smuzhiyun 125*4882a593Smuzhiyun 126*4882a593Smuzhiyun# 127*4882a593Smuzhiyun# Given a dict returned by build_package_size(), this function 128*4882a593Smuzhiyun# generates a pie chart of the size installed by each package. 129*4882a593Smuzhiyun# 130*4882a593Smuzhiyun# pkgsize: dictionary with the name of the package as a key, and the 131*4882a593Smuzhiyun# size as the value, as returned by build_package_size. 132*4882a593Smuzhiyun# 133*4882a593Smuzhiyun# outputf: output file for the graph 134*4882a593Smuzhiyun# 135*4882a593Smuzhiyundef draw_graph(pkgsize, outputf): 136*4882a593Smuzhiyun def size2string(sz): 137*4882a593Smuzhiyun if Config.iec: 138*4882a593Smuzhiyun divider = 1024.0 139*4882a593Smuzhiyun prefixes = ['', 'Ki', 'Mi', 'Gi', 'Ti'] 140*4882a593Smuzhiyun else: 141*4882a593Smuzhiyun divider = 1000.0 142*4882a593Smuzhiyun prefixes = ['', 'k', 'M', 'G', 'T'] 143*4882a593Smuzhiyun while sz > divider and len(prefixes) > 1: 144*4882a593Smuzhiyun prefixes = prefixes[1:] 145*4882a593Smuzhiyun sz = sz/divider 146*4882a593Smuzhiyun # precision is made so that there are always at least three meaningful 147*4882a593Smuzhiyun # digits displayed (e.g. '3.14' and '10.4', not just '3' and '10') 148*4882a593Smuzhiyun precision = int(2-math.floor(math.log10(sz))) if sz < 1000 else 0 149*4882a593Smuzhiyun return '{:.{prec}f} {}B'.format(sz, prefixes[0], prec=precision) 150*4882a593Smuzhiyun 151*4882a593Smuzhiyun total = sum(pkgsize.values()) 152*4882a593Smuzhiyun labels = [] 153*4882a593Smuzhiyun values = [] 154*4882a593Smuzhiyun other_value = 0 155*4882a593Smuzhiyun unknown_value = 0 156*4882a593Smuzhiyun for (p, sz) in sorted(pkgsize.items(), key=lambda x: x[1], 157*4882a593Smuzhiyun reverse=Config.biggest_first): 158*4882a593Smuzhiyun if sz < (total * Config.size_limit): 159*4882a593Smuzhiyun other_value += sz 160*4882a593Smuzhiyun elif p == "unknown": 161*4882a593Smuzhiyun unknown_value = sz 162*4882a593Smuzhiyun else: 163*4882a593Smuzhiyun labels.append("%s (%s)" % (p, size2string(sz))) 164*4882a593Smuzhiyun values.append(sz) 165*4882a593Smuzhiyun if unknown_value != 0: 166*4882a593Smuzhiyun labels.append("Unknown (%s)" % (size2string(unknown_value))) 167*4882a593Smuzhiyun values.append(unknown_value) 168*4882a593Smuzhiyun if other_value != 0: 169*4882a593Smuzhiyun labels.append("Other (%s)" % (size2string(other_value))) 170*4882a593Smuzhiyun values.append(other_value) 171*4882a593Smuzhiyun 172*4882a593Smuzhiyun plt.figure() 173*4882a593Smuzhiyun patches, texts, autotexts = plt.pie(values, labels=labels, 174*4882a593Smuzhiyun autopct='%1.1f%%', shadow=True, 175*4882a593Smuzhiyun colors=Config.colors) 176*4882a593Smuzhiyun # Reduce text size 177*4882a593Smuzhiyun proptease = fm.FontProperties() 178*4882a593Smuzhiyun proptease.set_size('xx-small') 179*4882a593Smuzhiyun plt.setp(autotexts, fontproperties=proptease) 180*4882a593Smuzhiyun plt.setp(texts, fontproperties=proptease) 181*4882a593Smuzhiyun 182*4882a593Smuzhiyun plt.suptitle("Filesystem size per package", fontsize=18, y=.97) 183*4882a593Smuzhiyun plt.title("Total filesystem size: %s" % (size2string(total)), fontsize=10, 184*4882a593Smuzhiyun y=.96) 185*4882a593Smuzhiyun plt.savefig(outputf) 186*4882a593Smuzhiyun 187*4882a593Smuzhiyun 188*4882a593Smuzhiyun# 189*4882a593Smuzhiyun# Generate a CSV file with statistics about the size of each file, its 190*4882a593Smuzhiyun# size contribution to the package and to the overall system. 191*4882a593Smuzhiyun# 192*4882a593Smuzhiyun# filesdict: dictionary with the name of the files as key, and as 193*4882a593Smuzhiyun# value a tuple containing the name of the package to which the files 194*4882a593Smuzhiyun# belongs, and the size of the file. As returned by 195*4882a593Smuzhiyun# build_package_dict. 196*4882a593Smuzhiyun# 197*4882a593Smuzhiyun# pkgsize: dictionary with the name of the package as a key, and the 198*4882a593Smuzhiyun# size as the value, as returned by build_package_size. 199*4882a593Smuzhiyun# 200*4882a593Smuzhiyun# outputf: output CSV file 201*4882a593Smuzhiyun# 202*4882a593Smuzhiyundef gen_files_csv(filesdict, pkgsizes, outputf): 203*4882a593Smuzhiyun total = 0 204*4882a593Smuzhiyun for (p, sz) in pkgsizes.items(): 205*4882a593Smuzhiyun total += sz 206*4882a593Smuzhiyun with open(outputf, 'w') as csvfile: 207*4882a593Smuzhiyun wr = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_MINIMAL) 208*4882a593Smuzhiyun wr.writerow(["File name", 209*4882a593Smuzhiyun "Package name", 210*4882a593Smuzhiyun "File size", 211*4882a593Smuzhiyun "Package size", 212*4882a593Smuzhiyun "File size in package (%)", 213*4882a593Smuzhiyun "File size in system (%)"]) 214*4882a593Smuzhiyun for f, (pkgname, filesize) in filesdict.items(): 215*4882a593Smuzhiyun pkgsize = pkgsizes[pkgname] 216*4882a593Smuzhiyun 217*4882a593Smuzhiyun if pkgsize == 0: 218*4882a593Smuzhiyun percent_pkg = 0 219*4882a593Smuzhiyun else: 220*4882a593Smuzhiyun percent_pkg = float(filesize) / pkgsize * 100 221*4882a593Smuzhiyun 222*4882a593Smuzhiyun percent_total = float(filesize) / total * 100 223*4882a593Smuzhiyun 224*4882a593Smuzhiyun wr.writerow([f, pkgname, filesize, pkgsize, 225*4882a593Smuzhiyun "%.1f" % percent_pkg, 226*4882a593Smuzhiyun "%.1f" % percent_total]) 227*4882a593Smuzhiyun 228*4882a593Smuzhiyun 229*4882a593Smuzhiyun# 230*4882a593Smuzhiyun# Generate a CSV file with statistics about the size of each package, 231*4882a593Smuzhiyun# and their size contribution to the overall system. 232*4882a593Smuzhiyun# 233*4882a593Smuzhiyun# pkgsize: dictionary with the name of the package as a key, and the 234*4882a593Smuzhiyun# size as the value, as returned by build_package_size. 235*4882a593Smuzhiyun# 236*4882a593Smuzhiyun# outputf: output CSV file 237*4882a593Smuzhiyun# 238*4882a593Smuzhiyundef gen_packages_csv(pkgsizes, outputf): 239*4882a593Smuzhiyun total = sum(pkgsizes.values()) 240*4882a593Smuzhiyun with open(outputf, 'w') as csvfile: 241*4882a593Smuzhiyun wr = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_MINIMAL) 242*4882a593Smuzhiyun wr.writerow(["Package name", "Package size", 243*4882a593Smuzhiyun "Package size in system (%)"]) 244*4882a593Smuzhiyun for (pkg, size) in pkgsizes.items(): 245*4882a593Smuzhiyun wr.writerow([pkg, size, "%.1f" % (float(size) / total * 100)]) 246*4882a593Smuzhiyun 247*4882a593Smuzhiyun 248*4882a593Smuzhiyun# 249*4882a593Smuzhiyun# Our special action for --iec, --binary, --si, --decimal 250*4882a593Smuzhiyun# 251*4882a593Smuzhiyunclass PrefixAction(argparse.Action): 252*4882a593Smuzhiyun def __init__(self, option_strings, dest, **kwargs): 253*4882a593Smuzhiyun for key in ["type", "nargs"]: 254*4882a593Smuzhiyun if key in kwargs: 255*4882a593Smuzhiyun raise ValueError('"{}" not allowed'.format(key)) 256*4882a593Smuzhiyun super(PrefixAction, self).__init__(option_strings, dest, nargs=0, 257*4882a593Smuzhiyun type=bool, **kwargs) 258*4882a593Smuzhiyun 259*4882a593Smuzhiyun def __call__(self, parser, namespace, values, option_string=None): 260*4882a593Smuzhiyun setattr(namespace, self.dest, option_string in ["--iec", "--binary"]) 261*4882a593Smuzhiyun 262*4882a593Smuzhiyun 263*4882a593Smuzhiyundef main(): 264*4882a593Smuzhiyun parser = argparse.ArgumentParser(description='Draw size statistics graphs') 265*4882a593Smuzhiyun 266*4882a593Smuzhiyun parser.add_argument("--builddir", '-i', metavar="BUILDDIR", required=True, 267*4882a593Smuzhiyun help="Buildroot output directory") 268*4882a593Smuzhiyun parser.add_argument("--graph", '-g', metavar="GRAPH", 269*4882a593Smuzhiyun help="Graph output file (.pdf or .png extension)") 270*4882a593Smuzhiyun parser.add_argument("--file-size-csv", '-f', metavar="FILE_SIZE_CSV", 271*4882a593Smuzhiyun help="CSV output file with file size statistics") 272*4882a593Smuzhiyun parser.add_argument("--package-size-csv", '-p', metavar="PKG_SIZE_CSV", 273*4882a593Smuzhiyun help="CSV output file with package size statistics") 274*4882a593Smuzhiyun parser.add_argument("--biggest-first", action='store_true', 275*4882a593Smuzhiyun help="Sort packages in decreasing size order, " + 276*4882a593Smuzhiyun "rather than in increasing size order") 277*4882a593Smuzhiyun parser.add_argument("--iec", "--binary", "--si", "--decimal", 278*4882a593Smuzhiyun action=PrefixAction, 279*4882a593Smuzhiyun help="Use IEC (binary, powers of 1024) or SI (decimal, " 280*4882a593Smuzhiyun "powers of 1000, the default) prefixes") 281*4882a593Smuzhiyun parser.add_argument("--size-limit", "-l", type=float, 282*4882a593Smuzhiyun help='Under this size ratio, files are accounted to ' + 283*4882a593Smuzhiyun 'the generic "Other" package. Default: 0.01 (1%%)') 284*4882a593Smuzhiyun args = parser.parse_args() 285*4882a593Smuzhiyun 286*4882a593Smuzhiyun Config.biggest_first = args.biggest_first 287*4882a593Smuzhiyun Config.iec = args.iec 288*4882a593Smuzhiyun if args.size_limit is not None: 289*4882a593Smuzhiyun if args.size_limit < 0.0 or args.size_limit > 1.0: 290*4882a593Smuzhiyun parser.error("--size-limit must be in [0.0..1.0]") 291*4882a593Smuzhiyun Config.size_limit = args.size_limit 292*4882a593Smuzhiyun 293*4882a593Smuzhiyun # Find out which package installed what files 294*4882a593Smuzhiyun pkgdict = build_package_dict(args.builddir) 295*4882a593Smuzhiyun 296*4882a593Smuzhiyun # Collect the size installed by each package 297*4882a593Smuzhiyun pkgsize = build_package_size(pkgdict, args.builddir) 298*4882a593Smuzhiyun 299*4882a593Smuzhiyun if args.graph: 300*4882a593Smuzhiyun draw_graph(pkgsize, args.graph) 301*4882a593Smuzhiyun if args.file_size_csv: 302*4882a593Smuzhiyun gen_files_csv(pkgdict, pkgsize, args.file_size_csv) 303*4882a593Smuzhiyun if args.package_size_csv: 304*4882a593Smuzhiyun gen_packages_csv(pkgsize, args.package_size_csv) 305*4882a593Smuzhiyun 306*4882a593Smuzhiyun 307*4882a593Smuzhiyunif __name__ == "__main__": 308*4882a593Smuzhiyun main() 309