blob: aa126969cfc530dbb7f118b20cd2ec8a580ef520 [file] [log] [blame]
Rob Landleyf8a80842006-05-07 19:26:53 +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
Bernhard Reutner-Fischercf575ca2008-05-23 12:53:18 +000012def usage():
Rob Landleyf8a80842006-05-07 19:26:53 +000013 sys.stderr.write("usage: %s file1 file2\n" % sys.argv[0])
14 sys.exit(-1)
15
Bernhard Reutner-Fischercf575ca2008-05-23 12:53:18 +000016if len(sys.argv) < 3:
17 usage()
18
Mike Frysinger98f74032009-06-01 16:37:27 -040019for f in sys.argv[1:3]:
Bernhard Reutner-Fischer45aebfd2007-04-05 12:27:12 +000020 if not os.path.exists(f):
21 sys.stderr.write("Error: file '%s' does not exist\n" % f)
Bernhard Reutner-Fischercf575ca2008-05-23 12:53:18 +000022 usage()
Bernhard Reutner-Fischer45aebfd2007-04-05 12:27:12 +000023
Bernhard Reutner-Fischer659507f2010-01-30 18:01:17 +010024sym_args = " ".join(sys.argv[3:])
Rob Landleyf8a80842006-05-07 19:26:53 +000025def getsizes(file):
Bernhard Reutner-Fischer659507f2010-01-30 18:01:17 +010026 sym, alias = {}, {}
27 dynsym_filter = re.compile("^\s+\d+:\s+[\dA-Fa-f]+\s+\d+\s+\w+\s+\w+\s+\w+\s+\w+\s+\w+\n$")
28 for l in os.popen("readelf -W -s %s %s" % (sym_args, file)).readlines():
29 if not dynsym_filter.match(l): continue
30 num, value, size, typ, bind, vis, ndx, name = l.strip().split()
31 if ndx == "UND": continue # skip undefined
32 if typ in ["SECTION", "FILES"]: continue # skip sections and files
33 if "." in name: name = "static." + name.split(".")[0]
34 value = int(value, 16)
35 size = int(size)
36 if bind != "GLOBAL": # see if it is an alias
37 alias[name] = {"addr" : value, "size": size}
38 else:
39 sym[name] = {"addr" : value, "size": size}
40 for a_nam, a_dat in alias.iteritems():
41 impl = [k for k, v in sym.iteritems() if v.get("addr") == a_dat["addr"]]
42 # If the non-GLOBAL sym has an implementation elsewhere then
43 # it's an alias, disregard it.
44 if not impl:
45 # If this non-GLOBAL sym does not have an implementation at
46 # another address, then treat it as a normal symbol.
47 sym[a_nam] = a_dat
48 for l in os.popen("readelf -W -S " + file).readlines():
Rob Landleyf14f7fc2006-05-29 20:56:27 +000049 x = l.split()
Bernhard Reutner-Fischer659507f2010-01-30 18:01:17 +010050 if len(x)<6: continue
51 # Should take these into account too!
52 #if x[1] not in [".text", ".rodata", ".symtab", ".strtab"]: continue
53 if x[1] not in [".rodata"]: continue
54 sym[x[1]] = {"addr" : int(x[3], 16), "size" : int(x[5], 16)}
Rob Landleyf8a80842006-05-07 19:26:53 +000055 return sym
56
57old = getsizes(sys.argv[1])
58new = getsizes(sys.argv[2])
59grow, shrink, add, remove, up, down = 0, 0, 0, 0, 0, 0
Bernhard Reutner-Fischer659507f2010-01-30 18:01:17 +010060delta, common = [], []
Rob Landleyf8a80842006-05-07 19:26:53 +000061
Bernhard Reutner-Fischer659507f2010-01-30 18:01:17 +010062for a in old.iterkeys():
Rob Landleyf8a80842006-05-07 19:26:53 +000063 if a in new:
Bernhard Reutner-Fischer659507f2010-01-30 18:01:17 +010064 common.append(a)
Rob Landleyf8a80842006-05-07 19:26:53 +000065
66for name in old:
67 if name not in common:
68 remove += 1
Bernhard Reutner-Fischer659507f2010-01-30 18:01:17 +010069 sz = old[name].get("size", 0)
70 down += sz
71 delta.append((-sz, name))
Rob Landleyf8a80842006-05-07 19:26:53 +000072
73for name in new:
74 if name not in common:
75 add += 1
Bernhard Reutner-Fischer659507f2010-01-30 18:01:17 +010076 sz = new[name].get("size", 0)
77 up += sz
78 delta.append((sz, name))
Rob Landleyf8a80842006-05-07 19:26:53 +000079
80for name in common:
Bernhard Reutner-Fischer659507f2010-01-30 18:01:17 +010081 d = new[name].get("size", 0) - old[name].get("size", 0)
Rob Landleyf8a80842006-05-07 19:26:53 +000082 if d>0: grow, up = grow+1, up+d
Bernhard Reutner-Fischer659507f2010-01-30 18:01:17 +010083 elif d<0: shrink, down = shrink+1, down-d
84 else:
85 continue
Rob Landleyf8a80842006-05-07 19:26:53 +000086 delta.append((d, name))
87
88delta.sort()
89delta.reverse()
90
Rob Landleyf14f7fc2006-05-29 20:56:27 +000091print "%-48s %7s %7s %+7s" % ("function", "old", "new", "delta")
Rob Landleyf8a80842006-05-07 19:26:53 +000092for d, n in delta:
Bernhard Reutner-Fischer659507f2010-01-30 18:01:17 +010093 if d:
94 old_sz = old.get(n, {}).get("size", "-")
95 new_sz = new.get(n, {}).get("size", "-")
96 print "%-48s %7s %7s %+7d" % (n, old_sz, new_sz, d)
Rob Landleyf14f7fc2006-05-29 20:56:27 +000097print "-"*78
98total="(add/remove: %s/%s grow/shrink: %s/%s up/down: %s/%s)%%sTotal: %s bytes"\
99 % (add, remove, grow, shrink, up, -down, up-down)
100print total % (" "*(80-len(total)))