Maurus Cuelenaere | a85f369 | 2009-07-01 10:41:34 +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 | def usage(): |
| 13 | sys.stderr.write("Usage: %s file1 file2\n" % sys.argv[0]) |
| 14 | sys.exit(-1) |
| 15 | |
| 16 | if len(sys.argv) < 3: |
| 17 | usage() |
| 18 | |
| 19 | for f in sys.argv[1], sys.argv[2]: |
| 20 | if not os.path.exists(f): |
| 21 | sys.stderr.write("Error: file '%s' does not exist\n" % f) |
| 22 | usage() |
| 23 | |
| 24 | nm_args = " ".join([x for x in sys.argv[3:]]) |
| 25 | def getsizes(file): |
| 26 | sym = {} |
| 27 | for l in os.popen("nm --size-sort %s %s" % (nm_args, file)).readlines(): |
Thomas Martitz | 98d2121 | 2014-01-07 10:35:59 +0100 | [diff] [blame] | 28 | l = l.strip() |
| 29 | # Skip empty lines |
| 30 | if not len(l): |
| 31 | continue |
| 32 | # Skip archive members |
Maurus Cuelenaere | a85f369 | 2009-07-01 10:41:34 +0000 | [diff] [blame] | 33 | if len(l.split()) == 1 and l.endswith(':'): |
| 34 | continue |
| 35 | size, type, name = l.split() |
| 36 | if type in "tTdDbBrR": |
| 37 | if "." in name: name = "static." + name.split(".")[0] |
| 38 | sym[name] = sym.get(name, 0) + int(size, 16) |
| 39 | for l in os.popen("readelf -S " + file).readlines(): |
| 40 | x = l.split() |
| 41 | if len(x)<6 or x[1] != ".rodata": continue |
| 42 | sym[".rodata"] = int(x[5], 16) |
| 43 | return sym |
| 44 | |
| 45 | old = getsizes(sys.argv[1]) |
| 46 | new = getsizes(sys.argv[2]) |
| 47 | grow, shrink, add, remove, up, down = 0, 0, 0, 0, 0, 0 |
| 48 | delta, common = [], {} |
| 49 | |
| 50 | for a in old: |
| 51 | if a in new: |
| 52 | common[a] = 1 |
| 53 | |
| 54 | for name in old: |
| 55 | if name not in common: |
| 56 | remove += 1 |
| 57 | down += old[name] |
| 58 | delta.append((-old[name], name)) |
| 59 | |
| 60 | for name in new: |
| 61 | if name not in common: |
| 62 | add += 1 |
| 63 | up += new[name] |
| 64 | delta.append((new[name], name)) |
| 65 | |
| 66 | for name in common: |
| 67 | d = new.get(name, 0) - old.get(name, 0) |
| 68 | if d>0: grow, up = grow+1, up+d |
| 69 | if d<0: shrink, down = shrink+1, down-d |
| 70 | delta.append((d, name)) |
| 71 | |
| 72 | delta.sort() |
| 73 | delta.reverse() |
| 74 | |
Thomas Martitz | 98d2121 | 2014-01-07 10:35:59 +0100 | [diff] [blame] | 75 | print("%-48s %7s %7s %+7s" % ("function", "old", "new", "delta")) |
Maurus Cuelenaere | a85f369 | 2009-07-01 10:41:34 +0000 | [diff] [blame] | 76 | for d, n in delta: |
Thomas Martitz | 98d2121 | 2014-01-07 10:35:59 +0100 | [diff] [blame] | 77 | if d: print("%-48s %7s %7s %+7d" % (n, old.get(n,"-"), new.get(n,"-"), d)) |
| 78 | print("-" * 78) |
Maurus Cuelenaere | a85f369 | 2009-07-01 10:41:34 +0000 | [diff] [blame] | 79 | total="(add/remove: %s/%s grow/shrink: %s/%s up/down: %s/%s)%%s\nTotal: %s bytes"\ |
| 80 | % (add, remove, grow, shrink, up, -down, up-down) |
Thomas Martitz | 98d2121 | 2014-01-07 10:35:59 +0100 | [diff] [blame] | 81 | print(total % (" "*(80-len(total)))) |