2006-05-08 00:56:53 +05:30
|
|
|
#!/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 18:23:18 +05:30
|
|
|
def usage():
|
2006-05-08 00:56:53 +05:30
|
|
|
sys.stderr.write("usage: %s file1 file2\n" % sys.argv[0])
|
|
|
|
sys.exit(-1)
|
|
|
|
|
2008-05-23 18:23:18 +05:30
|
|
|
if len(sys.argv) < 3:
|
|
|
|
usage()
|
|
|
|
|
2009-06-02 02:07:27 +05:30
|
|
|
for f in sys.argv[1:3]:
|
2007-04-05 17:57:12 +05:30
|
|
|
if not os.path.exists(f):
|
|
|
|
sys.stderr.write("Error: file '%s' does not exist\n" % f)
|
2008-05-23 18:23:18 +05:30
|
|
|
usage()
|
2007-04-05 17:57:12 +05:30
|
|
|
|
2010-01-30 22:31:17 +05:30
|
|
|
sym_args = " ".join(sys.argv[3:])
|
2006-05-08 00:56:53 +05:30
|
|
|
def getsizes(file):
|
2010-01-30 22:31:17 +05:30
|
|
|
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-30 02:26:27 +05:30
|
|
|
x = l.split()
|
2010-01-30 22:31:17 +05:30
|
|
|
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-08 00:56:53 +05:30
|
|
|
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 22:31:17 +05:30
|
|
|
delta, common = [], []
|
2006-05-08 00:56:53 +05:30
|
|
|
|
2010-01-30 22:31:17 +05:30
|
|
|
for a in old.iterkeys():
|
2006-05-08 00:56:53 +05:30
|
|
|
if a in new:
|
2010-01-30 22:31:17 +05:30
|
|
|
common.append(a)
|
2006-05-08 00:56:53 +05:30
|
|
|
|
|
|
|
for name in old:
|
|
|
|
if name not in common:
|
|
|
|
remove += 1
|
2010-01-30 22:31:17 +05:30
|
|
|
sz = old[name].get("size", 0)
|
|
|
|
down += sz
|
|
|
|
delta.append((-sz, name))
|
2006-05-08 00:56:53 +05:30
|
|
|
|
|
|
|
for name in new:
|
|
|
|
if name not in common:
|
|
|
|
add += 1
|
2010-01-30 22:31:17 +05:30
|
|
|
sz = new[name].get("size", 0)
|
|
|
|
up += sz
|
|
|
|
delta.append((sz, name))
|
2006-05-08 00:56:53 +05:30
|
|
|
|
|
|
|
for name in common:
|
2010-01-30 22:31:17 +05:30
|
|
|
d = new[name].get("size", 0) - old[name].get("size", 0)
|
2006-05-08 00:56:53 +05:30
|
|
|
if d>0: grow, up = grow+1, up+d
|
2010-01-30 22:31:17 +05:30
|
|
|
elif d<0: shrink, down = shrink+1, down-d
|
|
|
|
else:
|
|
|
|
continue
|
2006-05-08 00:56:53 +05:30
|
|
|
delta.append((d, name))
|
|
|
|
|
|
|
|
delta.sort()
|
|
|
|
delta.reverse()
|
|
|
|
|
2006-05-30 02:26:27 +05:30
|
|
|
print "%-48s %7s %7s %+7s" % ("function", "old", "new", "delta")
|
2006-05-08 00:56:53 +05:30
|
|
|
for d, n in delta:
|
2010-01-30 22:31:17 +05:30
|
|
|
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-30 02:26:27 +05:30
|
|
|
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)))
|