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 | |
Bernhard Reutner-Fischer | f16d7c4 | 2010-02-07 19:26:18 +0100 | [diff] [blame^] | 10 | import sys, os#, re |
Rob Landley | f8a8084 | 2006-05-07 19:26:53 +0000 | [diff] [blame] | 11 | |
Bernhard Reutner-Fischer | cf575ca | 2008-05-23 12:53:18 +0000 | [diff] [blame] | 12 | def usage(): |
Bernhard Reutner-Fischer | f16d7c4 | 2010-02-07 19:26:18 +0100 | [diff] [blame^] | 13 | sys.stderr.write("usage: %s [-t] file1 file2\n" % sys.argv[0]) |
Rob Landley | f8a8084 | 2006-05-07 19:26:53 +0000 | [diff] [blame] | 14 | sys.exit(-1) |
| 15 | |
Bernhard Reutner-Fischer | f16d7c4 | 2010-02-07 19:26:18 +0100 | [diff] [blame^] | 16 | f1, f2 = (None, None) |
| 17 | flag_timing, dashes = (False, False) |
| 18 | |
| 19 | for f in sys.argv[1:]: |
| 20 | if f.startswith("-"): |
| 21 | if f == "--": # sym_args |
| 22 | dashes = True |
| 23 | break |
| 24 | if f == "-t": # timings |
| 25 | flag_timing = True |
| 26 | else: |
| 27 | if not os.path.exists(f): |
| 28 | sys.stderr.write("Error: file '%s' does not exist\n" % f) |
| 29 | usage() |
| 30 | if f1 is None: |
| 31 | f1 = f |
| 32 | elif f2 is None: |
| 33 | f2 = f |
| 34 | if flag_timing: |
| 35 | import time |
| 36 | if f1 is None or f2 is None: |
Bernhard Reutner-Fischer | cf575ca | 2008-05-23 12:53:18 +0000 | [diff] [blame] | 37 | usage() |
| 38 | |
Bernhard Reutner-Fischer | f16d7c4 | 2010-02-07 19:26:18 +0100 | [diff] [blame^] | 39 | sym_args = " ".join(sys.argv[3 + flag_timing + dashes:]) |
Rob Landley | f8a8084 | 2006-05-07 19:26:53 +0000 | [diff] [blame] | 40 | def getsizes(file): |
Bernhard Reutner-Fischer | f16d7c4 | 2010-02-07 19:26:18 +0100 | [diff] [blame^] | 41 | sym, alias, lut = {}, {}, {} |
| 42 | #dynsym_filter = re.compile("^\d+:\s+[\dA-Fa-f]+\s+\d+\s+\w+\s+\w+\s+\w+\s+\w+\s+\w+$") |
Bernhard Reutner-Fischer | 659507f | 2010-01-30 18:01:17 +0100 | [diff] [blame] | 43 | for l in os.popen("readelf -W -s %s %s" % (sym_args, file)).readlines(): |
Bernhard Reutner-Fischer | f16d7c4 | 2010-02-07 19:26:18 +0100 | [diff] [blame^] | 44 | if True: |
| 45 | l = l.strip() |
| 46 | if not (len(l) and l[0].isdigit() and len(l.split()) == 8): |
| 47 | continue |
| 48 | num, value, size, typ, bind, vis, ndx, name = l.split() |
| 49 | if ndx == "UND": continue # skip undefined |
| 50 | if typ in ["SECTION", "FILES"]: continue # skip sections and files |
| 51 | #else: |
| 52 | # l = l.strip() |
| 53 | # match = dynsym_filter.match(l) |
| 54 | # if not match: continue |
| 55 | # x, value, size, typ, bind, x, ndx, name = l.split() |
| 56 | # if ndx == "UND": continue # skip undefined |
| 57 | # if typ in ["SECTION", "FILES"]: continue # skip sections and files |
Bernhard Reutner-Fischer | 659507f | 2010-01-30 18:01:17 +0100 | [diff] [blame] | 58 | if "." in name: name = "static." + name.split(".")[0] |
| 59 | value = int(value, 16) |
| 60 | size = int(size) |
Bernhard Reutner-Fischer | f16d7c4 | 2010-02-07 19:26:18 +0100 | [diff] [blame^] | 61 | if vis != "DEFAULT" and bind != "GLOBAL": # see if it is an alias |
| 62 | alias[(value, size)] = {"name" : name} |
Bernhard Reutner-Fischer | 659507f | 2010-01-30 18:01:17 +0100 | [diff] [blame] | 63 | else: |
| 64 | sym[name] = {"addr" : value, "size": size} |
Bernhard Reutner-Fischer | f16d7c4 | 2010-02-07 19:26:18 +0100 | [diff] [blame^] | 65 | lut[(value, size)] = 0 |
| 66 | for addr, sz in alias.iterkeys(): |
Bernhard Reutner-Fischer | 659507f | 2010-01-30 18:01:17 +0100 | [diff] [blame] | 67 | # If the non-GLOBAL sym has an implementation elsewhere then |
| 68 | # it's an alias, disregard it. |
Bernhard Reutner-Fischer | f16d7c4 | 2010-02-07 19:26:18 +0100 | [diff] [blame^] | 69 | if not (addr, sz) in lut: |
Bernhard Reutner-Fischer | 659507f | 2010-01-30 18:01:17 +0100 | [diff] [blame] | 70 | # If this non-GLOBAL sym does not have an implementation at |
| 71 | # another address, then treat it as a normal symbol. |
Bernhard Reutner-Fischer | f16d7c4 | 2010-02-07 19:26:18 +0100 | [diff] [blame^] | 72 | sym[alias[(addr, sz)]["name"]] = {"addr" : addr, "size": sz} |
Bernhard Reutner-Fischer | 659507f | 2010-01-30 18:01:17 +0100 | [diff] [blame] | 73 | for l in os.popen("readelf -W -S " + file).readlines(): |
Rob Landley | f14f7fc | 2006-05-29 20:56:27 +0000 | [diff] [blame] | 74 | x = l.split() |
Bernhard Reutner-Fischer | 659507f | 2010-01-30 18:01:17 +0100 | [diff] [blame] | 75 | if len(x)<6: continue |
| 76 | # Should take these into account too! |
| 77 | #if x[1] not in [".text", ".rodata", ".symtab", ".strtab"]: continue |
| 78 | if x[1] not in [".rodata"]: continue |
| 79 | 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] | 80 | return sym |
| 81 | |
Bernhard Reutner-Fischer | f16d7c4 | 2010-02-07 19:26:18 +0100 | [diff] [blame^] | 82 | if flag_timing: |
| 83 | start_t1 = int(time.time() * 1e9) |
| 84 | old = getsizes(f1) |
| 85 | if flag_timing: |
| 86 | end_t1 = int(time.time() * 1e9) |
| 87 | start_t2 = int(time.time() * 1e9) |
| 88 | new = getsizes(f2) |
| 89 | if flag_timing: |
| 90 | end_t2 = int(time.time() * 1e9) |
| 91 | start_t3 = int(time.time() * 1e9) |
Rob Landley | f8a8084 | 2006-05-07 19:26:53 +0000 | [diff] [blame] | 92 | grow, shrink, add, remove, up, down = 0, 0, 0, 0, 0, 0 |
Bernhard Reutner-Fischer | f16d7c4 | 2010-02-07 19:26:18 +0100 | [diff] [blame^] | 93 | delta, common = [], {} |
Rob Landley | f8a8084 | 2006-05-07 19:26:53 +0000 | [diff] [blame] | 94 | |
Bernhard Reutner-Fischer | f16d7c4 | 2010-02-07 19:26:18 +0100 | [diff] [blame^] | 95 | for name in old.iterkeys(): |
| 96 | if name in new: |
| 97 | common[name] = 1 |
Rob Landley | f8a8084 | 2006-05-07 19:26:53 +0000 | [diff] [blame] | 98 | |
| 99 | for name in old: |
| 100 | if name not in common: |
| 101 | remove += 1 |
Bernhard Reutner-Fischer | f16d7c4 | 2010-02-07 19:26:18 +0100 | [diff] [blame^] | 102 | sz = old[name]["size"] |
Bernhard Reutner-Fischer | 659507f | 2010-01-30 18:01:17 +0100 | [diff] [blame] | 103 | down += sz |
| 104 | delta.append((-sz, name)) |
Rob Landley | f8a8084 | 2006-05-07 19:26:53 +0000 | [diff] [blame] | 105 | |
| 106 | for name in new: |
| 107 | if name not in common: |
| 108 | add += 1 |
Bernhard Reutner-Fischer | f16d7c4 | 2010-02-07 19:26:18 +0100 | [diff] [blame^] | 109 | sz = new[name]["size"] |
Bernhard Reutner-Fischer | 659507f | 2010-01-30 18:01:17 +0100 | [diff] [blame] | 110 | up += sz |
| 111 | delta.append((sz, name)) |
Rob Landley | f8a8084 | 2006-05-07 19:26:53 +0000 | [diff] [blame] | 112 | |
| 113 | for name in common: |
Bernhard Reutner-Fischer | 659507f | 2010-01-30 18:01:17 +0100 | [diff] [blame] | 114 | d = new[name].get("size", 0) - old[name].get("size", 0) |
Rob Landley | f8a8084 | 2006-05-07 19:26:53 +0000 | [diff] [blame] | 115 | if d>0: grow, up = grow+1, up+d |
Bernhard Reutner-Fischer | 659507f | 2010-01-30 18:01:17 +0100 | [diff] [blame] | 116 | elif d<0: shrink, down = shrink+1, down-d |
| 117 | else: |
| 118 | continue |
Rob Landley | f8a8084 | 2006-05-07 19:26:53 +0000 | [diff] [blame] | 119 | delta.append((d, name)) |
| 120 | |
| 121 | delta.sort() |
| 122 | delta.reverse() |
Bernhard Reutner-Fischer | f16d7c4 | 2010-02-07 19:26:18 +0100 | [diff] [blame^] | 123 | if flag_timing: |
| 124 | end_t3 = int(time.time() * 1e9) |
Rob Landley | f8a8084 | 2006-05-07 19:26:53 +0000 | [diff] [blame] | 125 | |
Rob Landley | f14f7fc | 2006-05-29 20:56:27 +0000 | [diff] [blame] | 126 | print "%-48s %7s %7s %+7s" % ("function", "old", "new", "delta") |
Rob Landley | f8a8084 | 2006-05-07 19:26:53 +0000 | [diff] [blame] | 127 | for d, n in delta: |
Bernhard Reutner-Fischer | 659507f | 2010-01-30 18:01:17 +0100 | [diff] [blame] | 128 | if d: |
| 129 | old_sz = old.get(n, {}).get("size", "-") |
| 130 | new_sz = new.get(n, {}).get("size", "-") |
| 131 | print "%-48s %7s %7s %+7d" % (n, old_sz, new_sz, d) |
Rob Landley | f14f7fc | 2006-05-29 20:56:27 +0000 | [diff] [blame] | 132 | print "-"*78 |
| 133 | total="(add/remove: %s/%s grow/shrink: %s/%s up/down: %s/%s)%%sTotal: %s bytes"\ |
| 134 | % (add, remove, grow, shrink, up, -down, up-down) |
| 135 | print total % (" "*(80-len(total))) |
Bernhard Reutner-Fischer | f16d7c4 | 2010-02-07 19:26:18 +0100 | [diff] [blame^] | 136 | if flag_timing: |
| 137 | print("\n%d/%d; %d Parse origin/new; processing nsecs" % |
| 138 | (end_t1-start_t1, end_t2-start_t2, end_t3-start_t3)) |
| 139 | print("total nsecs: %d" % (end_t3-start_t1)) |