blob: cb861b8e9250cc4aeca26823cc51826b1b4aca75 [file] [log] [blame]
Lauri Kasanen98f213e2011-07-08 10:56:58 +02001#!/usr/bin/env python
Rob Landleyf8a80842006-05-07 19:26:53 +00002#
3# Copyright 2004 Matt Mackall <mpm@selenic.com>
4#
Lauri Kasanen98f213e2011-07-08 10:56:58 +02005# Inspired by perl Bloat-O-Meter (c) 1997 by Andi Kleen
Rob Landleyf8a80842006-05-07 19:26:53 +00006#
7# This software may be used and distributed according to the terms
8# of the GNU General Public License, incorporated herein by reference.
9
maxwen27116ba2015-08-14 21:41:28 +020010import sys, os
Rob Landleyf8a80842006-05-07 19:26:53 +000011
Bernhard Reutner-Fischercf575ca2008-05-23 12:53:18 +000012def usage():
maxwen27116ba2015-08-14 21:41:28 +020013 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 Landleyf8a80842006-05-07 19:26:53 +000018
Bernhard Reutner-Fischerf16d7c42010-02-07 19:26:18 +010019f1, f2 = (None, None)
20flag_timing, dashes = (False, False)
21
22for 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
maxwen27116ba2015-08-14 21:41:28 +020037 else:
38 usage()
Bernhard Reutner-Fischerf16d7c42010-02-07 19:26:18 +010039if flag_timing:
40 import time
41if f1 is None or f2 is None:
Bernhard Reutner-Fischercf575ca2008-05-23 12:53:18 +000042 usage()
43
Bernhard Reutner-Fischerf16d7c42010-02-07 19:26:18 +010044sym_args = " ".join(sys.argv[3 + flag_timing + dashes:])
Rob Landleyf8a80842006-05-07 19:26:53 +000045def getsizes(file):
Bernhard Reutner-Fischerf16d7c42010-02-07 19:26:18 +010046 sym, alias, lut = {}, {}, {}
Bernhard Reutner-Fischer659507f2010-01-30 18:01:17 +010047 for l in os.popen("readelf -W -s %s %s" % (sym_args, file)).readlines():
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +020048 l = l.strip()
49 if not (len(l) and l[0].isdigit() and len(l.split()) == 8):
50 continue
51 num, value, size, typ, bind, vis, ndx, name = l.split()
52 if ndx == "UND": continue # skip undefined
53 if typ in ["SECTION", "FILES"]: continue # skip sections and files
Bernhard Reutner-Fischer659507f2010-01-30 18:01:17 +010054 if "." in name: name = "static." + name.split(".")[0]
55 value = int(value, 16)
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +020056 size = int(size, 16) if size.startswith('0x') else int(size)
Bernhard Reutner-Fischerf16d7c42010-02-07 19:26:18 +010057 if vis != "DEFAULT" and bind != "GLOBAL": # see if it is an alias
58 alias[(value, size)] = {"name" : name}
Bernhard Reutner-Fischer659507f2010-01-30 18:01:17 +010059 else:
60 sym[name] = {"addr" : value, "size": size}
Bernhard Reutner-Fischerf16d7c42010-02-07 19:26:18 +010061 lut[(value, size)] = 0
Marek Polacek8410ac12010-10-26 02:34:36 +020062 for addr, sz in iter(alias.keys()):
Bernhard Reutner-Fischer659507f2010-01-30 18:01:17 +010063 # If the non-GLOBAL sym has an implementation elsewhere then
64 # it's an alias, disregard it.
Bernhard Reutner-Fischerf16d7c42010-02-07 19:26:18 +010065 if not (addr, sz) in lut:
Bernhard Reutner-Fischer659507f2010-01-30 18:01:17 +010066 # If this non-GLOBAL sym does not have an implementation at
67 # another address, then treat it as a normal symbol.
Bernhard Reutner-Fischerf16d7c42010-02-07 19:26:18 +010068 sym[alias[(addr, sz)]["name"]] = {"addr" : addr, "size": sz}
Bernhard Reutner-Fischer659507f2010-01-30 18:01:17 +010069 for l in os.popen("readelf -W -S " + file).readlines():
Rob Landleyf14f7fc2006-05-29 20:56:27 +000070 x = l.split()
Bernhard Reutner-Fischer659507f2010-01-30 18:01:17 +010071 if len(x)<6: continue
72 # Should take these into account too!
73 #if x[1] not in [".text", ".rodata", ".symtab", ".strtab"]: continue
74 if x[1] not in [".rodata"]: continue
75 sym[x[1]] = {"addr" : int(x[3], 16), "size" : int(x[5], 16)}
Rob Landleyf8a80842006-05-07 19:26:53 +000076 return sym
77
Bernhard Reutner-Fischerf16d7c42010-02-07 19:26:18 +010078if flag_timing:
79 start_t1 = int(time.time() * 1e9)
80old = getsizes(f1)
81if flag_timing:
82 end_t1 = int(time.time() * 1e9)
83 start_t2 = int(time.time() * 1e9)
84new = getsizes(f2)
85if flag_timing:
86 end_t2 = int(time.time() * 1e9)
87 start_t3 = int(time.time() * 1e9)
Rob Landleyf8a80842006-05-07 19:26:53 +000088grow, shrink, add, remove, up, down = 0, 0, 0, 0, 0, 0
Bernhard Reutner-Fischerf16d7c42010-02-07 19:26:18 +010089delta, common = [], {}
Rob Landleyf8a80842006-05-07 19:26:53 +000090
Marek Polacek8410ac12010-10-26 02:34:36 +020091for name in iter(old.keys()):
Bernhard Reutner-Fischerf16d7c42010-02-07 19:26:18 +010092 if name in new:
93 common[name] = 1
Rob Landleyf8a80842006-05-07 19:26:53 +000094
95for name in old:
96 if name not in common:
97 remove += 1
Bernhard Reutner-Fischerf16d7c42010-02-07 19:26:18 +010098 sz = old[name]["size"]
Bernhard Reutner-Fischer659507f2010-01-30 18:01:17 +010099 down += sz
100 delta.append((-sz, name))
Rob Landleyf8a80842006-05-07 19:26:53 +0000101
102for name in new:
103 if name not in common:
104 add += 1
Bernhard Reutner-Fischerf16d7c42010-02-07 19:26:18 +0100105 sz = new[name]["size"]
Bernhard Reutner-Fischer659507f2010-01-30 18:01:17 +0100106 up += sz
107 delta.append((sz, name))
Rob Landleyf8a80842006-05-07 19:26:53 +0000108
109for name in common:
Bernhard Reutner-Fischer659507f2010-01-30 18:01:17 +0100110 d = new[name].get("size", 0) - old[name].get("size", 0)
Rob Landleyf8a80842006-05-07 19:26:53 +0000111 if d>0: grow, up = grow+1, up+d
Bernhard Reutner-Fischer659507f2010-01-30 18:01:17 +0100112 elif d<0: shrink, down = shrink+1, down-d
113 else:
114 continue
Rob Landleyf8a80842006-05-07 19:26:53 +0000115 delta.append((d, name))
116
117delta.sort()
118delta.reverse()
Bernhard Reutner-Fischerf16d7c42010-02-07 19:26:18 +0100119if flag_timing:
120 end_t3 = int(time.time() * 1e9)
Rob Landleyf8a80842006-05-07 19:26:53 +0000121
Bernhard Reutner-Fischereb255752010-02-07 19:42:48 +0100122print("%-48s %7s %7s %+7s" % ("function", "old", "new", "delta"))
Rob Landleyf8a80842006-05-07 19:26:53 +0000123for d, n in delta:
Bernhard Reutner-Fischer659507f2010-01-30 18:01:17 +0100124 if d:
125 old_sz = old.get(n, {}).get("size", "-")
126 new_sz = new.get(n, {}).get("size", "-")
Bernhard Reutner-Fischereb255752010-02-07 19:42:48 +0100127 print("%-48s %7s %7s %+7d" % (n, old_sz, new_sz, d))
128print("-"*78)
Rob Landleyf14f7fc2006-05-29 20:56:27 +0000129total="(add/remove: %s/%s grow/shrink: %s/%s up/down: %s/%s)%%sTotal: %s bytes"\
130 % (add, remove, grow, shrink, up, -down, up-down)
Bernhard Reutner-Fischereb255752010-02-07 19:42:48 +0100131print(total % (" "*(80-len(total))))
Bernhard Reutner-Fischerf16d7c42010-02-07 19:26:18 +0100132if flag_timing:
133 print("\n%d/%d; %d Parse origin/new; processing nsecs" %
134 (end_t1-start_t1, end_t2-start_t2, end_t3-start_t3))
135 print("total nsecs: %d" % (end_t3-start_t1))