xref: /linux/scripts/bloat-o-meter (revision ca55b2fef3a9373fcfc30f82fd26bc7fccbda732)
1#!/usr/bin/python
2#
3# Copyright 2004 Matt Mackall <mpm@selenic.com>
4#
5# inspired by perl Bloat-O-Meter (c) 1997 by Andi Kleen
6#
7# This software may be used and distributed according to the terms
8# of the GNU General Public License, incorporated herein by reference.
9
10import sys, os, re
11
12if len(sys.argv) != 3:
13    sys.stderr.write("usage: %s file1 file2\n" % sys.argv[0])
14    sys.exit(-1)
15
16def getsizes(file):
17    sym = {}
18    for l in os.popen("nm --size-sort " + file).readlines():
19        size, type, name = l[:-1].split()
20        if type in "tTdDbBrR":
21            # strip generated symbols
22            if name.startswith("__mod_"): continue
23            if name.startswith("SyS_"): continue
24            if name.startswith("compat_SyS_"): continue
25            if name == "linux_banner": continue
26            # statics and some other optimizations adds random .NUMBER
27            name = re.sub(r'\.[0-9]+', '', name)
28            sym[name] = sym.get(name, 0) + int(size, 16)
29    return sym
30
31old = getsizes(sys.argv[1])
32new = getsizes(sys.argv[2])
33grow, shrink, add, remove, up, down = 0, 0, 0, 0, 0, 0
34delta, common = [], {}
35
36for a in old:
37    if a in new:
38        common[a] = 1
39
40for name in old:
41    if name not in common:
42        remove += 1
43        down += old[name]
44        delta.append((-old[name], name))
45
46for name in new:
47    if name not in common:
48        add += 1
49        up += new[name]
50        delta.append((new[name], name))
51
52for name in common:
53        d = new.get(name, 0) - old.get(name, 0)
54        if d>0: grow, up = grow+1, up+d
55        if d<0: shrink, down = shrink+1, down-d
56        delta.append((d, name))
57
58delta.sort()
59delta.reverse()
60
61print "add/remove: %s/%s grow/shrink: %s/%s up/down: %s/%s (%s)" % \
62      (add, remove, grow, shrink, up, -down, up-down)
63print "%-40s %7s %7s %+7s" % ("function", "old", "new", "delta")
64for d, n in delta:
65    if d: print "%-40s %7s %7s %+7d" % (n, old.get(n,"-"), new.get(n,"-"), d)
66