2006-05-07 19:26:53 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
#
|
|
|
|
# Copyright 2004 Matt Mackall <mpm@selenic.com>
|
|
|
|
#
|
|
|
|
# inspired by perl Bloat-O-Meter (c) 1997 by Andi Kleen
|
|
|
|
#
|
|
|
|
# This software may be used and distributed according to the terms
|
|
|
|
# of the GNU General Public License, incorporated herein by reference.
|
|
|
|
|
|
|
|
import sys, os, re
|
|
|
|
|
2008-05-23 12:53:18 +00:00
|
|
|
def usage():
|
2006-05-07 19:26:53 +00:00
|
|
|
sys.stderr.write("usage: %s file1 file2\n" % sys.argv[0])
|
|
|
|
sys.exit(-1)
|
|
|
|
|
2008-05-23 12:53:18 +00:00
|
|
|
if len(sys.argv) < 3:
|
|
|
|
usage()
|
|
|
|
|
2009-06-01 20:37:27 +00:00
|
|
|
for f in sys.argv[1:3]:
|
2007-04-05 12:27:12 +00:00
|
|
|
if not os.path.exists(f):
|
|
|
|
sys.stderr.write("Error: file '%s' does not exist\n" % f)
|
2008-05-23 12:53:18 +00:00
|
|
|
usage()
|
2007-04-05 12:27:12 +00:00
|
|
|
|
2010-01-30 17:01:17 +00:00
|
|
|
sym_args = " ".join(sys.argv[3:])
|
2006-05-07 19:26:53 +00:00
|
|
|
def getsizes(file):
|
2010-01-30 17:01:17 +00:00
|
|
|
sym, alias = {}, {}
|
|
|
|
dynsym_filter = re.compile("^\s+\d+:\s+[\dA-Fa-f]+\s+\d+\s+\w+\s+\w+\s+\w+\s+\w+\s+\w+\n$")
|
|
|
|
for l in os.popen("readelf -W -s %s %s" % (sym_args, file)).readlines():
|
|
|
|
if not dynsym_filter.match(l): continue
|
|
|
|
num, value, size, typ, bind, vis, ndx, name = l.strip().split()
|
|
|
|
if ndx == "UND": continue # skip undefined
|
|
|
|
if typ in ["SECTION", "FILES"]: continue # skip sections and files
|
|
|
|
if "." in name: name = "static." + name.split(".")[0]
|
|
|
|
value = int(value, 16)
|
|
|
|
size = int(size)
|
|
|
|
if bind != "GLOBAL": # see if it is an alias
|
|
|
|
alias[name] = {"addr" : value, "size": size}
|
|
|
|
else:
|
|
|
|
sym[name] = {"addr" : value, "size": size}
|
|
|
|
for a_nam, a_dat in alias.iteritems():
|
|
|
|
impl = [k for k, v in sym.iteritems() if v.get("addr") == a_dat["addr"]]
|
|
|
|
# If the non-GLOBAL sym has an implementation elsewhere then
|
|
|
|
# it's an alias, disregard it.
|
|
|
|
if not impl:
|
|
|
|
# If this non-GLOBAL sym does not have an implementation at
|
|
|
|
# another address, then treat it as a normal symbol.
|
|
|
|
sym[a_nam] = a_dat
|
|
|
|
for l in os.popen("readelf -W -S " + file).readlines():
|
2006-05-29 20:56:27 +00:00
|
|
|
x = l.split()
|
2010-01-30 17:01:17 +00:00
|
|
|
if len(x)<6: continue
|
|
|
|
# Should take these into account too!
|
|
|
|
#if x[1] not in [".text", ".rodata", ".symtab", ".strtab"]: continue
|
|
|
|
if x[1] not in [".rodata"]: continue
|
|
|
|
sym[x[1]] = {"addr" : int(x[3], 16), "size" : int(x[5], 16)}
|
2006-05-07 19:26:53 +00:00
|
|
|
return sym
|
|
|
|
|
|
|
|
old = getsizes(sys.argv[1])
|
|
|
|
new = getsizes(sys.argv[2])
|
|
|
|
grow, shrink, add, remove, up, down = 0, 0, 0, 0, 0, 0
|
2010-01-30 17:01:17 +00:00
|
|
|
delta, common = [], []
|
2006-05-07 19:26:53 +00:00
|
|
|
|
2010-01-30 17:01:17 +00:00
|
|
|
for a in old.iterkeys():
|
2006-05-07 19:26:53 +00:00
|
|
|
if a in new:
|
2010-01-30 17:01:17 +00:00
|
|
|
common.append(a)
|
2006-05-07 19:26:53 +00:00
|
|
|
|
|
|
|
for name in old:
|
|
|
|
if name not in common:
|
|
|
|
remove += 1
|
2010-01-30 17:01:17 +00:00
|
|
|
sz = old[name].get("size", 0)
|
|
|
|
down += sz
|
|
|
|
delta.append((-sz, name))
|
2006-05-07 19:26:53 +00:00
|
|
|
|
|
|
|
for name in new:
|
|
|
|
if name not in common:
|
|
|
|
add += 1
|
2010-01-30 17:01:17 +00:00
|
|
|
sz = new[name].get("size", 0)
|
|
|
|
up += sz
|
|
|
|
delta.append((sz, name))
|
2006-05-07 19:26:53 +00:00
|
|
|
|
|
|
|
for name in common:
|
2010-01-30 17:01:17 +00:00
|
|
|
d = new[name].get("size", 0) - old[name].get("size", 0)
|
2006-05-07 19:26:53 +00:00
|
|
|
if d>0: grow, up = grow+1, up+d
|
2010-01-30 17:01:17 +00:00
|
|
|
elif d<0: shrink, down = shrink+1, down-d
|
|
|
|
else:
|
|
|
|
continue
|
2006-05-07 19:26:53 +00:00
|
|
|
delta.append((d, name))
|
|
|
|
|
|
|
|
delta.sort()
|
|
|
|
delta.reverse()
|
|
|
|
|
2006-05-29 20:56:27 +00:00
|
|
|
print "%-48s %7s %7s %+7s" % ("function", "old", "new", "delta")
|
2006-05-07 19:26:53 +00:00
|
|
|
for d, n in delta:
|
2010-01-30 17:01:17 +00:00
|
|
|
if d:
|
|
|
|
old_sz = old.get(n, {}).get("size", "-")
|
|
|
|
new_sz = new.get(n, {}).get("size", "-")
|
|
|
|
print "%-48s %7s %7s %+7d" % (n, old_sz, new_sz, d)
|
2006-05-29 20:56:27 +00:00
|
|
|
print "-"*78
|
|
|
|
total="(add/remove: %s/%s grow/shrink: %s/%s up/down: %s/%s)%%sTotal: %s bytes"\
|
|
|
|
% (add, remove, grow, shrink, up, -down, up-down)
|
|
|
|
print total % (" "*(80-len(total)))
|