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