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 | |
Bernhard Reutner-Fischer | cf575ca | 2008-05-23 12:53:18 +0000 | [diff] [blame] | 12 | def usage(): |
Rob Landley | f8a8084 | 2006-05-07 19:26:53 +0000 | [diff] [blame] | 13 | sys.stderr.write("usage: %s file1 file2\n" % sys.argv[0]) |
| 14 | sys.exit(-1) |
| 15 | |
Bernhard Reutner-Fischer | cf575ca | 2008-05-23 12:53:18 +0000 | [diff] [blame] | 16 | if len(sys.argv) < 3: |
| 17 | usage() |
| 18 | |
Mike Frysinger | 98f7403 | 2009-06-01 16:37:27 -0400 | [diff] [blame] | 19 | for f in sys.argv[1:3]: |
Bernhard Reutner-Fischer | 45aebfd | 2007-04-05 12:27:12 +0000 | [diff] [blame] | 20 | if not os.path.exists(f): |
| 21 | sys.stderr.write("Error: file '%s' does not exist\n" % f) |
Bernhard Reutner-Fischer | cf575ca | 2008-05-23 12:53:18 +0000 | [diff] [blame] | 22 | usage() |
Bernhard Reutner-Fischer | 45aebfd | 2007-04-05 12:27:12 +0000 | [diff] [blame] | 23 | |
Bernhard Reutner-Fischer | 659507f | 2010-01-30 18:01:17 +0100 | [diff] [blame^] | 24 | sym_args = " ".join(sys.argv[3:]) |
Rob Landley | f8a8084 | 2006-05-07 19:26:53 +0000 | [diff] [blame] | 25 | def getsizes(file): |
Bernhard Reutner-Fischer | 659507f | 2010-01-30 18:01:17 +0100 | [diff] [blame^] | 26 | sym, alias = {}, {} |
| 27 | dynsym_filter = re.compile("^\s+\d+:\s+[\dA-Fa-f]+\s+\d+\s+\w+\s+\w+\s+\w+\s+\w+\s+\w+\n$") |
| 28 | for l in os.popen("readelf -W -s %s %s" % (sym_args, file)).readlines(): |
| 29 | if not dynsym_filter.match(l): continue |
| 30 | num, value, size, typ, bind, vis, ndx, name = l.strip().split() |
| 31 | if ndx == "UND": continue # skip undefined |
| 32 | if typ in ["SECTION", "FILES"]: continue # skip sections and files |
| 33 | if "." in name: name = "static." + name.split(".")[0] |
| 34 | value = int(value, 16) |
| 35 | size = int(size) |
| 36 | if bind != "GLOBAL": # see if it is an alias |
| 37 | alias[name] = {"addr" : value, "size": size} |
| 38 | else: |
| 39 | sym[name] = {"addr" : value, "size": size} |
| 40 | for a_nam, a_dat in alias.iteritems(): |
| 41 | impl = [k for k, v in sym.iteritems() if v.get("addr") == a_dat["addr"]] |
| 42 | # If the non-GLOBAL sym has an implementation elsewhere then |
| 43 | # it's an alias, disregard it. |
| 44 | if not impl: |
| 45 | # If this non-GLOBAL sym does not have an implementation at |
| 46 | # another address, then treat it as a normal symbol. |
| 47 | sym[a_nam] = a_dat |
| 48 | for l in os.popen("readelf -W -S " + file).readlines(): |
Rob Landley | f14f7fc | 2006-05-29 20:56:27 +0000 | [diff] [blame] | 49 | x = l.split() |
Bernhard Reutner-Fischer | 659507f | 2010-01-30 18:01:17 +0100 | [diff] [blame^] | 50 | if len(x)<6: continue |
| 51 | # Should take these into account too! |
| 52 | #if x[1] not in [".text", ".rodata", ".symtab", ".strtab"]: continue |
| 53 | if x[1] not in [".rodata"]: continue |
| 54 | sym[x[1]] = {"addr" : int(x[3], 16), "size" : int(x[5], 16)} |
Rob Landley | f8a8084 | 2006-05-07 19:26:53 +0000 | [diff] [blame] | 55 | return sym |
| 56 | |
| 57 | old = getsizes(sys.argv[1]) |
| 58 | new = getsizes(sys.argv[2]) |
| 59 | grow, shrink, add, remove, up, down = 0, 0, 0, 0, 0, 0 |
Bernhard Reutner-Fischer | 659507f | 2010-01-30 18:01:17 +0100 | [diff] [blame^] | 60 | delta, common = [], [] |
Rob Landley | f8a8084 | 2006-05-07 19:26:53 +0000 | [diff] [blame] | 61 | |
Bernhard Reutner-Fischer | 659507f | 2010-01-30 18:01:17 +0100 | [diff] [blame^] | 62 | for a in old.iterkeys(): |
Rob Landley | f8a8084 | 2006-05-07 19:26:53 +0000 | [diff] [blame] | 63 | if a in new: |
Bernhard Reutner-Fischer | 659507f | 2010-01-30 18:01:17 +0100 | [diff] [blame^] | 64 | common.append(a) |
Rob Landley | f8a8084 | 2006-05-07 19:26:53 +0000 | [diff] [blame] | 65 | |
| 66 | for name in old: |
| 67 | if name not in common: |
| 68 | remove += 1 |
Bernhard Reutner-Fischer | 659507f | 2010-01-30 18:01:17 +0100 | [diff] [blame^] | 69 | sz = old[name].get("size", 0) |
| 70 | down += sz |
| 71 | delta.append((-sz, name)) |
Rob Landley | f8a8084 | 2006-05-07 19:26:53 +0000 | [diff] [blame] | 72 | |
| 73 | for name in new: |
| 74 | if name not in common: |
| 75 | add += 1 |
Bernhard Reutner-Fischer | 659507f | 2010-01-30 18:01:17 +0100 | [diff] [blame^] | 76 | sz = new[name].get("size", 0) |
| 77 | up += sz |
| 78 | delta.append((sz, name)) |
Rob Landley | f8a8084 | 2006-05-07 19:26:53 +0000 | [diff] [blame] | 79 | |
| 80 | for name in common: |
Bernhard Reutner-Fischer | 659507f | 2010-01-30 18:01:17 +0100 | [diff] [blame^] | 81 | d = new[name].get("size", 0) - old[name].get("size", 0) |
Rob Landley | f8a8084 | 2006-05-07 19:26:53 +0000 | [diff] [blame] | 82 | if d>0: grow, up = grow+1, up+d |
Bernhard Reutner-Fischer | 659507f | 2010-01-30 18:01:17 +0100 | [diff] [blame^] | 83 | elif d<0: shrink, down = shrink+1, down-d |
| 84 | else: |
| 85 | continue |
Rob Landley | f8a8084 | 2006-05-07 19:26:53 +0000 | [diff] [blame] | 86 | delta.append((d, name)) |
| 87 | |
| 88 | delta.sort() |
| 89 | delta.reverse() |
| 90 | |
Rob Landley | f14f7fc | 2006-05-29 20:56:27 +0000 | [diff] [blame] | 91 | print "%-48s %7s %7s %+7s" % ("function", "old", "new", "delta") |
Rob Landley | f8a8084 | 2006-05-07 19:26:53 +0000 | [diff] [blame] | 92 | for d, n in delta: |
Bernhard Reutner-Fischer | 659507f | 2010-01-30 18:01:17 +0100 | [diff] [blame^] | 93 | if d: |
| 94 | old_sz = old.get(n, {}).get("size", "-") |
| 95 | new_sz = new.get(n, {}).get("size", "-") |
| 96 | print "%-48s %7s %7s %+7d" % (n, old_sz, new_sz, d) |
Rob Landley | f14f7fc | 2006-05-29 20:56:27 +0000 | [diff] [blame] | 97 | print "-"*78 |
| 98 | total="(add/remove: %s/%s grow/shrink: %s/%s up/down: %s/%s)%%sTotal: %s bytes"\ |
| 99 | % (add, remove, grow, shrink, up, -down, up-down) |
| 100 | print total % (" "*(80-len(total))) |