blob: 36303afa9dfc33b10770384d44398efbfe2f890c [file] [log] [blame]
Andy Shevchenko51839e292020-12-09 13:50:17 +02001#!/usr/bin/env python3
Matt Mackalld9606002006-01-08 01:05:19 -08002#
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
Nikolay Borisovb62eb272022-07-01 14:35:12 +030010import sys, os, re, argparse
Alexey Dobriyaneef06b822016-11-10 10:46:13 -080011from signal import signal, SIGPIPE, SIG_DFL
12
13signal(SIGPIPE, SIG_DFL)
Matt Mackalld9606002006-01-08 01:05:19 -080014
Nikolay Borisovb62eb272022-07-01 14:35:12 +030015parser = argparse.ArgumentParser(description="Simple script used to compare the symbol sizes of 2 object files")
16group = parser.add_mutually_exclusive_group()
17group.add_argument('-c', help='categorize output based on symbol type', action='store_true')
18group.add_argument('-d', help='Show delta of Data Section', action='store_true')
19group.add_argument('-t', help='Show delta of text Section', action='store_true')
Nikolay Borisov8b5db662022-07-01 14:35:13 +030020parser.add_argument('-p', dest='prefix', help='Arch prefix for the tool being used. Useful in cross build scenarios')
Nikolay Borisovb62eb272022-07-01 14:35:12 +030021parser.add_argument('file1', help='First file to compare')
22parser.add_argument('file2', help='Second file to compare')
23
24args = parser.parse_args()
Matt Mackalld9606002006-01-08 01:05:19 -080025
Alexey Dobriyan0d7bbb42016-12-12 16:40:48 -080026re_NUMBER = re.compile(r'\.[0-9]+')
27
Maninder Singh192efb72017-11-15 17:31:14 -080028def getsizes(file, format):
Matt Mackalld9606002006-01-08 01:05:19 -080029 sym = {}
Nikolay Borisov8b5db662022-07-01 14:35:13 +030030 nm = "nm"
31 if args.prefix:
32 nm = "{}nm".format(args.prefix)
33
34 with os.popen("{} --size-sort {}".format(nm, file)) as f:
Alexey Dobriyan3af06fd2016-12-12 16:40:45 -080035 for line in f:
Nikolay Borisov1d35b602020-08-06 23:17:32 -070036 if line.startswith("\n") or ":" in line:
37 continue
Alexey Dobriyan3af06fd2016-12-12 16:40:45 -080038 size, type, name = line.split()
Maninder Singh192efb72017-11-15 17:31:14 -080039 if type in format:
Alexey Dobriyan3af06fd2016-12-12 16:40:45 -080040 # strip generated symbols
41 if name.startswith("__mod_"): continue
Dominik Brodowskie145242e2018-04-09 12:51:42 +020042 if name.startswith("__se_sys"): continue
Dominik Brodowski5ac9efa2018-04-09 12:51:43 +020043 if name.startswith("__se_compat_sys"): continue
Rasmus Villemoese0b24752018-12-28 00:31:18 -080044 if name.startswith("__addressable_"): continue
Alexey Dobriyan3af06fd2016-12-12 16:40:45 -080045 if name == "linux_banner": continue
Paul Gortmakerdec81a52022-04-29 14:37:57 -070046 if name == "vermagic": continue
Alexey Dobriyan3af06fd2016-12-12 16:40:45 -080047 # statics and some other optimizations adds random .NUMBER
Alexey Dobriyan0d7bbb42016-12-12 16:40:48 -080048 name = re_NUMBER.sub('', name)
Alexey Dobriyan3af06fd2016-12-12 16:40:45 -080049 sym[name] = sym.get(name, 0) + int(size, 16)
Matt Mackalld9606002006-01-08 01:05:19 -080050 return sym
51
Maninder Singh192efb72017-11-15 17:31:14 -080052def calc(oldfile, newfile, format):
53 old = getsizes(oldfile, format)
54 new = getsizes(newfile, format)
55 grow, shrink, add, remove, up, down = 0, 0, 0, 0, 0, 0
56 delta, common = [], {}
57 otot, ntot = 0, 0
Matt Mackalld9606002006-01-08 01:05:19 -080058
Maninder Singh192efb72017-11-15 17:31:14 -080059 for a in old:
60 if a in new:
61 common[a] = 1
Matt Mackalld9606002006-01-08 01:05:19 -080062
Maninder Singh192efb72017-11-15 17:31:14 -080063 for name in old:
64 otot += old[name]
65 if name not in common:
66 remove += 1
67 down += old[name]
68 delta.append((-old[name], name))
Matt Mackalld9606002006-01-08 01:05:19 -080069
Maninder Singh192efb72017-11-15 17:31:14 -080070 for name in new:
71 ntot += new[name]
72 if name not in common:
73 add += 1
74 up += new[name]
75 delta.append((new[name], name))
Matt Mackalld9606002006-01-08 01:05:19 -080076
Maninder Singh192efb72017-11-15 17:31:14 -080077 for name in common:
Matt Mackalld9606002006-01-08 01:05:19 -080078 d = new.get(name, 0) - old.get(name, 0)
79 if d>0: grow, up = grow+1, up+d
80 if d<0: shrink, down = shrink+1, down-d
81 delta.append((d, name))
82
Alexander Pantyukhin0371ba62023-01-06 14:13:19 +050083 delta.sort(reverse=True)
Maninder Singh192efb72017-11-15 17:31:14 -080084 return grow, shrink, add, remove, up, down, delta, old, new, otot, ntot
Matt Mackalld9606002006-01-08 01:05:19 -080085
Nikolay Borisovb62eb272022-07-01 14:35:12 +030086def print_result(symboltype, symbolformat):
Maninder Singh192efb72017-11-15 17:31:14 -080087 grow, shrink, add, remove, up, down, delta, old, new, otot, ntot = \
Nikolay Borisovb62eb272022-07-01 14:35:12 +030088 calc(args.file1, args.file2, symbolformat)
Vineet Guptab21e91c2016-05-19 17:09:17 -070089
Maninder Singh192efb72017-11-15 17:31:14 -080090 print("add/remove: %s/%s grow/shrink: %s/%s up/down: %s/%s (%s)" % \
91 (add, remove, grow, shrink, up, -down, up-down))
92 print("%-40s %7s %7s %+7s" % (symboltype, "old", "new", "delta"))
93 for d, n in delta:
94 if d: print("%-40s %7s %7s %+7d" % (n, old.get(n,"-"), new.get(n,"-"), d))
95
Andy Shevchenkoedbddb82017-11-29 16:11:05 -080096 if otot:
97 percent = (ntot - otot) * 100.0 / otot
98 else:
99 percent = 0
100 print("Total: Before=%d, After=%d, chg %+.2f%%" % (otot, ntot, percent))
Maninder Singh192efb72017-11-15 17:31:14 -0800101
Nikolay Borisovb62eb272022-07-01 14:35:12 +0300102if args.c:
103 print_result("Function", "tT")
104 print_result("Data", "dDbB")
105 print_result("RO Data", "rR")
106elif args.d:
107 print_result("Data", "dDbBrR")
108elif args.t:
109 print_result("Function", "tT")
Maninder Singh192efb72017-11-15 17:31:14 -0800110else:
Nikolay Borisovb62eb272022-07-01 14:35:12 +0300111 print_result("Function", "tTdDbBrR")