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.
|
|
|
|
|
2010-02-07 18:26:18 +00:00
|
|
|
import sys, os#, re
|
2006-05-07 19:26:53 +00:00
|
|
|
|
2008-05-23 12:53:18 +00:00
|
|
|
def usage():
|
2010-02-07 18:26:18 +00:00
|
|
|
sys.stderr.write("usage: %s [-t] file1 file2\n" % sys.argv[0])
|
2006-05-07 19:26:53 +00:00
|
|
|
sys.exit(-1)
|
|
|
|
|
2010-02-07 18:26:18 +00:00
|
|
|
f1, f2 = (None, None)
|
|
|
|
flag_timing, dashes = (False, False)
|
2008-05-23 12:53:18 +00:00
|
|
|
|
2010-02-07 18:26:18 +00:00
|
|
|
for f in sys.argv[1:]:
|
|
|
|
if f.startswith("-"):
|
|
|
|
if f == "--": # sym_args
|
|
|
|
dashes = True
|
|
|
|
break
|
|
|
|
if f == "-t": # timings
|
|
|
|
flag_timing = True
|
|
|
|
else:
|
|
|
|
if not os.path.exists(f):
|
|
|
|
sys.stderr.write("Error: file '%s' does not exist\n" % f)
|
|
|
|
usage()
|
|
|
|
if f1 is None:
|
|
|
|
f1 = f
|
|
|
|
elif f2 is None:
|
|
|
|
f2 = f
|
|
|
|
if flag_timing:
|
|
|
|
import time
|
|
|
|
if f1 is None or f2 is None:
|
|
|
|
usage()
|
2007-04-05 12:27:12 +00:00
|
|
|
|
2010-02-07 18:26:18 +00:00
|
|
|
sym_args = " ".join(sys.argv[3 + flag_timing + dashes:])
|
2006-05-07 19:26:53 +00:00
|
|
|
def getsizes(file):
|
2010-02-07 18:26:18 +00:00
|
|
|
sym, alias, lut = {}, {}, {}
|
|
|
|
#dynsym_filter = re.compile("^\d+:\s+[\dA-Fa-f]+\s+\d+\s+\w+\s+\w+\s+\w+\s+\w+\s+\w+$")
|
2010-01-30 17:01:17 +00:00
|
|
|
for l in os.popen("readelf -W -s %s %s" % (sym_args, file)).readlines():
|
2010-02-07 18:26:18 +00:00
|
|
|
if True:
|
|
|
|
l = l.strip()
|
|
|
|
if not (len(l) and l[0].isdigit() and len(l.split()) == 8):
|
|
|
|
continue
|
|
|
|
num, value, size, typ, bind, vis, ndx, name = l.split()
|
|
|
|
if ndx == "UND": continue # skip undefined
|
|
|
|
if typ in ["SECTION", "FILES"]: continue # skip sections and files
|
|
|
|
#else:
|
|
|
|
# l = l.strip()
|
|
|
|
# match = dynsym_filter.match(l)
|
|
|
|
# if not match: continue
|
|
|
|
# x, value, size, typ, bind, x, ndx, name = l.split()
|
|
|
|
# if ndx == "UND": continue # skip undefined
|
|
|
|
# if typ in ["SECTION", "FILES"]: continue # skip sections and files
|
2010-01-30 17:01:17 +00:00
|
|
|
if "." in name: name = "static." + name.split(".")[0]
|
|
|
|
value = int(value, 16)
|
|
|
|
size = int(size)
|
2010-02-07 18:26:18 +00:00
|
|
|
if vis != "DEFAULT" and bind != "GLOBAL": # see if it is an alias
|
|
|
|
alias[(value, size)] = {"name" : name}
|
2010-01-30 17:01:17 +00:00
|
|
|
else:
|
|
|
|
sym[name] = {"addr" : value, "size": size}
|
2010-02-07 18:26:18 +00:00
|
|
|
lut[(value, size)] = 0
|
|
|
|
for addr, sz in alias.iterkeys():
|
2010-01-30 17:01:17 +00:00
|
|
|
# If the non-GLOBAL sym has an implementation elsewhere then
|
|
|
|
# it's an alias, disregard it.
|
2010-02-07 18:26:18 +00:00
|
|
|
if not (addr, sz) in lut:
|
2010-01-30 17:01:17 +00:00
|
|
|
# If this non-GLOBAL sym does not have an implementation at
|
|
|
|
# another address, then treat it as a normal symbol.
|
2010-02-07 18:26:18 +00:00
|
|
|
sym[alias[(addr, sz)]["name"]] = {"addr" : addr, "size": sz}
|
2010-01-30 17:01:17 +00:00
|
|
|
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
|
|
|
|
|
2010-02-07 18:26:18 +00:00
|
|
|
if flag_timing:
|
|
|
|
start_t1 = int(time.time() * 1e9)
|
|
|
|
old = getsizes(f1)
|
|
|
|
if flag_timing:
|
|
|
|
end_t1 = int(time.time() * 1e9)
|
|
|
|
start_t2 = int(time.time() * 1e9)
|
|
|
|
new = getsizes(f2)
|
|
|
|
if flag_timing:
|
|
|
|
end_t2 = int(time.time() * 1e9)
|
|
|
|
start_t3 = int(time.time() * 1e9)
|
2006-05-07 19:26:53 +00:00
|
|
|
grow, shrink, add, remove, up, down = 0, 0, 0, 0, 0, 0
|
2010-02-07 18:26:18 +00:00
|
|
|
delta, common = [], {}
|
2006-05-07 19:26:53 +00:00
|
|
|
|
2010-02-07 18:26:18 +00:00
|
|
|
for name in old.iterkeys():
|
|
|
|
if name in new:
|
|
|
|
common[name] = 1
|
2006-05-07 19:26:53 +00:00
|
|
|
|
|
|
|
for name in old:
|
|
|
|
if name not in common:
|
|
|
|
remove += 1
|
2010-02-07 18:26:18 +00:00
|
|
|
sz = old[name]["size"]
|
2010-01-30 17:01:17 +00:00
|
|
|
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-02-07 18:26:18 +00:00
|
|
|
sz = new[name]["size"]
|
2010-01-30 17:01:17 +00:00
|
|
|
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()
|
2010-02-07 18:26:18 +00:00
|
|
|
if flag_timing:
|
|
|
|
end_t3 = int(time.time() * 1e9)
|
2006-05-07 19:26:53 +00:00
|
|
|
|
2010-02-07 18:42:48 +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", "-")
|
2010-02-07 18:42:48 +00:00
|
|
|
print("%-48s %7s %7s %+7d" % (n, old_sz, new_sz, d))
|
|
|
|
print("-"*78)
|
2006-05-29 20:56:27 +00:00
|
|
|
total="(add/remove: %s/%s grow/shrink: %s/%s up/down: %s/%s)%%sTotal: %s bytes"\
|
|
|
|
% (add, remove, grow, shrink, up, -down, up-down)
|
2010-02-07 18:42:48 +00:00
|
|
|
print(total % (" "*(80-len(total))))
|
2010-02-07 18:26:18 +00:00
|
|
|
if flag_timing:
|
|
|
|
print("\n%d/%d; %d Parse origin/new; processing nsecs" %
|
|
|
|
(end_t1-start_t1, end_t2-start_t2, end_t3-start_t3))
|
|
|
|
print("total nsecs: %d" % (end_t3-start_t1))
|