0001 #!/usr/bin/env python3
0002 #
0003 # Copyright 2004 Matt Mackall <mpm@selenic.com>
0004 #
0005 # inspired by perl Bloat-O-Meter (c) 1997 by Andi Kleen
0006 #
0007 # This software may be used and distributed according to the terms
0008 # of the GNU General Public License, incorporated herein by reference.
0009
0010 import sys, os, re, argparse
0011 from signal import signal, SIGPIPE, SIG_DFL
0012
0013 signal(SIGPIPE, SIG_DFL)
0014
0015 parser = argparse.ArgumentParser(description="Simple script used to compare the symbol sizes of 2 object files")
0016 group = parser.add_mutually_exclusive_group()
0017 group.add_argument('-c', help='categorize output based on symbol type', action='store_true')
0018 group.add_argument('-d', help='Show delta of Data Section', action='store_true')
0019 group.add_argument('-t', help='Show delta of text Section', action='store_true')
0020 parser.add_argument('-p', dest='prefix', help='Arch prefix for the tool being used. Useful in cross build scenarios')
0021 parser.add_argument('file1', help='First file to compare')
0022 parser.add_argument('file2', help='Second file to compare')
0023
0024 args = parser.parse_args()
0025
0026 re_NUMBER = re.compile(r'\.[0-9]+')
0027
0028 def getsizes(file, format):
0029 sym = {}
0030 nm = "nm"
0031 if args.prefix:
0032 nm = "{}nm".format(args.prefix)
0033
0034 with os.popen("{} --size-sort {}".format(nm, file)) as f:
0035 for line in f:
0036 if line.startswith("\n") or ":" in line:
0037 continue
0038 size, type, name = line.split()
0039 if type in format:
0040 # strip generated symbols
0041 if name.startswith("__mod_"): continue
0042 if name.startswith("__se_sys"): continue
0043 if name.startswith("__se_compat_sys"): continue
0044 if name.startswith("__addressable_"): continue
0045 if name == "linux_banner": continue
0046 if name == "vermagic": continue
0047 # statics and some other optimizations adds random .NUMBER
0048 name = re_NUMBER.sub('', name)
0049 sym[name] = sym.get(name, 0) + int(size, 16)
0050 return sym
0051
0052 def calc(oldfile, newfile, format):
0053 old = getsizes(oldfile, format)
0054 new = getsizes(newfile, format)
0055 grow, shrink, add, remove, up, down = 0, 0, 0, 0, 0, 0
0056 delta, common = [], {}
0057 otot, ntot = 0, 0
0058
0059 for a in old:
0060 if a in new:
0061 common[a] = 1
0062
0063 for name in old:
0064 otot += old[name]
0065 if name not in common:
0066 remove += 1
0067 down += old[name]
0068 delta.append((-old[name], name))
0069
0070 for name in new:
0071 ntot += new[name]
0072 if name not in common:
0073 add += 1
0074 up += new[name]
0075 delta.append((new[name], name))
0076
0077 for name in common:
0078 d = new.get(name, 0) - old.get(name, 0)
0079 if d>0: grow, up = grow+1, up+d
0080 if d<0: shrink, down = shrink+1, down-d
0081 delta.append((d, name))
0082
0083 delta.sort()
0084 delta.reverse()
0085 return grow, shrink, add, remove, up, down, delta, old, new, otot, ntot
0086
0087 def print_result(symboltype, symbolformat):
0088 grow, shrink, add, remove, up, down, delta, old, new, otot, ntot = \
0089 calc(args.file1, args.file2, symbolformat)
0090
0091 print("add/remove: %s/%s grow/shrink: %s/%s up/down: %s/%s (%s)" % \
0092 (add, remove, grow, shrink, up, -down, up-down))
0093 print("%-40s %7s %7s %+7s" % (symboltype, "old", "new", "delta"))
0094 for d, n in delta:
0095 if d: print("%-40s %7s %7s %+7d" % (n, old.get(n,"-"), new.get(n,"-"), d))
0096
0097 if otot:
0098 percent = (ntot - otot) * 100.0 / otot
0099 else:
0100 percent = 0
0101 print("Total: Before=%d, After=%d, chg %+.2f%%" % (otot, ntot, percent))
0102
0103 if args.c:
0104 print_result("Function", "tT")
0105 print_result("Data", "dDbB")
0106 print_result("RO Data", "rR")
0107 elif args.d:
0108 print_result("Data", "dDbBrR")
0109 elif args.t:
0110 print_result("Function", "tT")
0111 else:
0112 print_result("Function", "tTdDbBrR")