Rob Landley | f8a8084 | 2006-05-07 19:26:53 +0000 | [diff] [blame] | 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 | def getsizes(file): |
| 17 | sym = {} |
| 18 | for l in os.popen("nm --size-sort " + file).readlines(): |
| 19 | size, type, name = l[:-1].split() |
Denis Vlasenko | 2425bdc | 2006-11-29 14:32:01 +0000 | [diff] [blame] | 20 | if type in "tTdDbBrR": |
Rob Landley | 712ba85 | 2006-05-08 18:30:33 +0000 | [diff] [blame] | 21 | if "." in name: name = "static." + name.split(".")[0] |
| 22 | sym[name] = sym.get(name, 0) + int(size, 16) |
Rob Landley | f14f7fc | 2006-05-29 20:56:27 +0000 | [diff] [blame] | 23 | for l in os.popen("readelf -S " + file).readlines(): |
| 24 | x = l.split() |
| 25 | if len(x)<6 or x[1] != ".rodata": continue |
| 26 | sym[".rodata"] = int(x[5], 16) |
Rob Landley | f8a8084 | 2006-05-07 19:26:53 +0000 | [diff] [blame] | 27 | return sym |
| 28 | |
| 29 | old = getsizes(sys.argv[1]) |
| 30 | new = getsizes(sys.argv[2]) |
| 31 | grow, shrink, add, remove, up, down = 0, 0, 0, 0, 0, 0 |
| 32 | delta, common = [], {} |
| 33 | |
| 34 | for a in old: |
| 35 | if a in new: |
| 36 | common[a] = 1 |
| 37 | |
| 38 | for name in old: |
| 39 | if name not in common: |
| 40 | remove += 1 |
| 41 | down += old[name] |
| 42 | delta.append((-old[name], name)) |
| 43 | |
| 44 | for name in new: |
| 45 | if name not in common: |
| 46 | add += 1 |
| 47 | up += new[name] |
| 48 | delta.append((new[name], name)) |
| 49 | |
| 50 | for name in common: |
| 51 | d = new.get(name, 0) - old.get(name, 0) |
| 52 | if d>0: grow, up = grow+1, up+d |
| 53 | if d<0: shrink, down = shrink+1, down-d |
| 54 | delta.append((d, name)) |
| 55 | |
| 56 | delta.sort() |
| 57 | delta.reverse() |
| 58 | |
Rob Landley | f14f7fc | 2006-05-29 20:56:27 +0000 | [diff] [blame] | 59 | print "%-48s %7s %7s %+7s" % ("function", "old", "new", "delta") |
Rob Landley | f8a8084 | 2006-05-07 19:26:53 +0000 | [diff] [blame] | 60 | for d, n in delta: |
Rob Landley | f14f7fc | 2006-05-29 20:56:27 +0000 | [diff] [blame] | 61 | if d: print "%-48s %7s %7s %+7d" % (n, old.get(n,"-"), new.get(n,"-"), d) |
| 62 | print "-"*78 |
| 63 | total="(add/remove: %s/%s grow/shrink: %s/%s up/down: %s/%s)%%sTotal: %s bytes"\ |
| 64 | % (add, remove, grow, shrink, up, -down, up-down) |
| 65 | print total % (" "*(80-len(total))) |