1#!/usr/bin/env python3
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, argparse
11from signal import signal, SIGPIPE, SIG_DFL
12
13signal(SIGPIPE, SIG_DFL)
14
15parser = 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')
20parser.add_argument('-p', dest='prefix', help='Arch prefix for the tool being used. Useful in cross build scenarios')
21parser.add_argument('file1', help='First file to compare')
22parser.add_argument('file2', help='Second file to compare')
23
24args = parser.parse_args()
25
26re_NUMBER = re.compile(r'\.[0-9]+')
27
28def getsizes(file, format):
29    sym = {}
30    nm = "nm"
31    if args.prefix:
32        nm = "{}nm".format(args.prefix)
33
34    with os.popen("{} --size-sort {}".format(nm, file)) as f:
35        for line in f:
36            if line.startswith("\n") or ":" in line:
37                continue
38            size, type, name = line.split()
39            if type in format:
40                # strip generated symbols
41                if name.startswith("__mod_"): continue
42                if name.startswith("__se_sys"): continue
43                if name.startswith("__se_compat_sys"): continue
44                if name.startswith("__addressable_"): continue
45                if name == "linux_banner": continue
46                if name == "vermagic": continue
47                # statics and some other optimizations adds random .NUMBER
48                name = re_NUMBER.sub('', name)
49                sym[name] = sym.get(name, 0) + int(size, 16)
50    return sym
51
52def 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
58
59    for a in old:
60        if a in new:
61            common[a] = 1
62
63    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))
69
70    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))
76
77    for name in common:
78        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
83    delta.sort(reverse=True)
84    return grow, shrink, add, remove, up, down, delta, old, new, otot, ntot
85
86def print_result(symboltype, symbolformat):
87    grow, shrink, add, remove, up, down, delta, old, new, otot, ntot = \
88    calc(args.file1, args.file2, symbolformat)
89
90    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
96    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))
101
102if args.c:
103    print_result("Function", "tTwW")
104    print_result("Data", "dDbBvV")
105    print_result("RO Data", "rR")
106elif args.d:
107    print_result("Data", "dDbBrRvV")
108elif args.t:
109    print_result("Function", "tTwW")
110else:
111    print_result("Function", "tTdDbBrRvVwW")
112