blob: 98ca6c2ae5097394d7818065fc24ebe362c15fe2 [file] [log] [blame]
Maurus Cuelenaerea85f3692009-07-01 10:41:34 +00001#!/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
12def usage():
13 sys.stderr.write("Usage: %s file1 file2\n" % sys.argv[0])
14 sys.exit(-1)
15
16if len(sys.argv) < 3:
17 usage()
18
19for f in sys.argv[1], sys.argv[2]:
20 if not os.path.exists(f):
21 sys.stderr.write("Error: file '%s' does not exist\n" % f)
22 usage()
23
24nm_args = " ".join([x for x in sys.argv[3:]])
25def getsizes(file):
26 sym = {}
27 for l in os.popen("nm --size-sort %s %s" % (nm_args, file)).readlines():
Thomas Martitz98d21212014-01-07 10:35:59 +010028 l = l.strip()
29 # Skip empty lines
30 if not len(l):
31 continue
32 # Skip archive members
Maurus Cuelenaerea85f3692009-07-01 10:41:34 +000033 if len(l.split()) == 1 and l.endswith(':'):
34 continue
35 size, type, name = l.split()
36 if type in "tTdDbBrR":
37 if "." in name: name = "static." + name.split(".")[0]
38 sym[name] = sym.get(name, 0) + int(size, 16)
39 for l in os.popen("readelf -S " + file).readlines():
40 x = l.split()
41 if len(x)<6 or x[1] != ".rodata": continue
42 sym[".rodata"] = int(x[5], 16)
43 return sym
44
45old = getsizes(sys.argv[1])
46new = getsizes(sys.argv[2])
47grow, shrink, add, remove, up, down = 0, 0, 0, 0, 0, 0
48delta, common = [], {}
49
50for a in old:
51 if a in new:
52 common[a] = 1
53
54for name in old:
55 if name not in common:
56 remove += 1
57 down += old[name]
58 delta.append((-old[name], name))
59
60for name in new:
61 if name not in common:
62 add += 1
63 up += new[name]
64 delta.append((new[name], name))
65
66for name in common:
67 d = new.get(name, 0) - old.get(name, 0)
68 if d>0: grow, up = grow+1, up+d
69 if d<0: shrink, down = shrink+1, down-d
70 delta.append((d, name))
71
72delta.sort()
73delta.reverse()
74
Thomas Martitz98d21212014-01-07 10:35:59 +010075print("%-48s %7s %7s %+7s" % ("function", "old", "new", "delta"))
Maurus Cuelenaerea85f3692009-07-01 10:41:34 +000076for d, n in delta:
Thomas Martitz98d21212014-01-07 10:35:59 +010077 if d: print("%-48s %7s %7s %+7d" % (n, old.get(n,"-"), new.get(n,"-"), d))
78print("-" * 78)
Maurus Cuelenaerea85f3692009-07-01 10:41:34 +000079total="(add/remove: %s/%s grow/shrink: %s/%s up/down: %s/%s)%%s\nTotal: %s bytes"\
80 % (add, remove, grow, shrink, up, -down, up-down)
Thomas Martitz98d21212014-01-07 10:35:59 +010081print(total % (" "*(80-len(total))))