blob: 38d2f5c1c0613cf21e9732be6dd2ae3ffa318487 [file] [log] [blame]
Rob Landleyf8a80842006-05-07 19:26:53 +00001#!/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
10import sys, os, re
11
Bernhard Reutner-Fischercf575ca2008-05-23 12:53:18 +000012def usage():
Rob Landleyf8a80842006-05-07 19:26:53 +000013 sys.stderr.write("usage: %s file1 file2\n" % sys.argv[0])
14 sys.exit(-1)
15
Bernhard Reutner-Fischercf575ca2008-05-23 12:53:18 +000016if len(sys.argv) < 3:
17 usage()
18
Bernhard Reutner-Fischer45aebfd2007-04-05 12:27:12 +000019for 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)
Bernhard Reutner-Fischercf575ca2008-05-23 12:53:18 +000022 usage()
Bernhard Reutner-Fischer45aebfd2007-04-05 12:27:12 +000023
Bernhard Reutner-Fischercf575ca2008-05-23 12:53:18 +000024nm_args = " ".join([x for x in sys.argv[3:]])
Rob Landleyf8a80842006-05-07 19:26:53 +000025def getsizes(file):
26 sym = {}
Bernhard Reutner-Fischercf575ca2008-05-23 12:53:18 +000027 for l in os.popen("nm --size-sort %s %s" % (nm_args, file)).readlines():
Mike Frysinger5177e282009-06-01 16:34:11 -040028 l = l.strip()
29 # Skip empty lines
Bernhard Reutner-Fischercf575ca2008-05-23 12:53:18 +000030 if not len(l): continue
Mike Frysinger5177e282009-06-01 16:34:11 -040031 # Skip archive members
Bernhard Reutner-Fischer49bdf282008-05-21 10:32:39 +000032 if len(l.split()) == 1 and l.endswith(':'):
33 continue
Bernhard Reutner-Fischercf575ca2008-05-23 12:53:18 +000034 size, type, name = l.split()
Denis Vlasenko2425bdc2006-11-29 14:32:01 +000035 if type in "tTdDbBrR":
Rob Landley712ba852006-05-08 18:30:33 +000036 if "." in name: name = "static." + name.split(".")[0]
37 sym[name] = sym.get(name, 0) + int(size, 16)
Rob Landleyf14f7fc2006-05-29 20:56:27 +000038 for l in os.popen("readelf -S " + file).readlines():
39 x = l.split()
40 if len(x)<6 or x[1] != ".rodata": continue
41 sym[".rodata"] = int(x[5], 16)
Rob Landleyf8a80842006-05-07 19:26:53 +000042 return sym
43
44old = getsizes(sys.argv[1])
45new = getsizes(sys.argv[2])
46grow, shrink, add, remove, up, down = 0, 0, 0, 0, 0, 0
47delta, common = [], {}
48
49for a in old:
50 if a in new:
51 common[a] = 1
52
53for name in old:
54 if name not in common:
55 remove += 1
56 down += old[name]
57 delta.append((-old[name], name))
58
59for name in new:
60 if name not in common:
61 add += 1
62 up += new[name]
63 delta.append((new[name], name))
64
65for name in common:
66 d = new.get(name, 0) - old.get(name, 0)
67 if d>0: grow, up = grow+1, up+d
68 if d<0: shrink, down = shrink+1, down-d
69 delta.append((d, name))
70
71delta.sort()
72delta.reverse()
73
Rob Landleyf14f7fc2006-05-29 20:56:27 +000074print "%-48s %7s %7s %+7s" % ("function", "old", "new", "delta")
Rob Landleyf8a80842006-05-07 19:26:53 +000075for d, n in delta:
Rob Landleyf14f7fc2006-05-29 20:56:27 +000076 if d: print "%-48s %7s %7s %+7d" % (n, old.get(n,"-"), new.get(n,"-"), d)
77print "-"*78
78total="(add/remove: %s/%s grow/shrink: %s/%s up/down: %s/%s)%%sTotal: %s bytes"\
79 % (add, remove, grow, shrink, up, -down, up-down)
80print total % (" "*(80-len(total)))