[20] | 1 | #!/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 | |
---|
| 10 | import sys, os, re |
---|
| 11 | |
---|
| 12 | if len(sys.argv) != 3: |
---|
| 13 | sys.stderr.write("usage: %s file1 file2\n" % sys.argv[0]) |
---|
| 14 | sys.exit(-1) |
---|
| 15 | |
---|
| 16 | for f in sys.argv[1], sys.argv[2]: |
---|
| 17 | if not os.path.exists(f): |
---|
| 18 | sys.stderr.write("Error: file '%s' does not exist\n" % f) |
---|
| 19 | sys.exit(-1) |
---|
| 20 | |
---|
| 21 | def getsizes(file): |
---|
| 22 | sym = {} |
---|
| 23 | for l in os.popen("nm --size-sort " + file).readlines(): |
---|
| 24 | size, type, name = l[:-1].split() |
---|
| 25 | if type in "tTdDbBrR": |
---|
| 26 | if "." in name: name = "static." + name.split(".")[0] |
---|
| 27 | sym[name] = sym.get(name, 0) + int(size, 16) |
---|
| 28 | for l in os.popen("readelf -S " + file).readlines(): |
---|
| 29 | x = l.split() |
---|
| 30 | if len(x)<6 or x[1] != ".rodata": continue |
---|
| 31 | sym[".rodata"] = int(x[5], 16) |
---|
| 32 | return sym |
---|
| 33 | |
---|
| 34 | old = getsizes(sys.argv[1]) |
---|
| 35 | new = getsizes(sys.argv[2]) |
---|
| 36 | grow, shrink, add, remove, up, down = 0, 0, 0, 0, 0, 0 |
---|
| 37 | delta, common = [], {} |
---|
| 38 | |
---|
| 39 | for a in old: |
---|
| 40 | if a in new: |
---|
| 41 | common[a] = 1 |
---|
| 42 | |
---|
| 43 | for name in old: |
---|
| 44 | if name not in common: |
---|
| 45 | remove += 1 |
---|
| 46 | down += old[name] |
---|
| 47 | delta.append((-old[name], name)) |
---|
| 48 | |
---|
| 49 | for name in new: |
---|
| 50 | if name not in common: |
---|
| 51 | add += 1 |
---|
| 52 | up += new[name] |
---|
| 53 | delta.append((new[name], name)) |
---|
| 54 | |
---|
| 55 | for name in common: |
---|
| 56 | d = new.get(name, 0) - old.get(name, 0) |
---|
| 57 | if d>0: grow, up = grow+1, up+d |
---|
| 58 | if d<0: shrink, down = shrink+1, down-d |
---|
| 59 | delta.append((d, name)) |
---|
| 60 | |
---|
| 61 | delta.sort() |
---|
| 62 | delta.reverse() |
---|
| 63 | |
---|
| 64 | print "%-48s %7s %7s %+7s" % ("function", "old", "new", "delta") |
---|
| 65 | for d, n in delta: |
---|
| 66 | if d: print "%-48s %7s %7s %+7d" % (n, old.get(n,"-"), new.get(n,"-"), d) |
---|
| 67 | print "-"*78 |
---|
| 68 | total="(add/remove: %s/%s grow/shrink: %s/%s up/down: %s/%s)%%sTotal: %s bytes"\ |
---|
| 69 | % (add, remove, grow, shrink, up, -down, up-down) |
---|
| 70 | print total % (" "*(80-len(total))) |
---|