xref: /linux/scripts/bloat-o-meter (revision b62eb2731e17e83c32e1a6089b4463da1a75e66e)
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('file1', help='First file to compare')
21parser.add_argument('file2', help='Second file to compare')
22
23args = parser.parse_args()
24
25re_NUMBER = re.compile(r'\.[0-9]+')
26
27def getsizes(file, format):
28    sym = {}
29    with os.popen("nm --size-sort " + file) as f:
30        for line in f:
31            if line.startswith("\n") or ":" in line:
32                continue
33            size, type, name = line.split()
34            if type in format:
35                # strip generated symbols
36                if name.startswith("__mod_"): continue
37                if name.startswith("__se_sys"): continue
38                if name.startswith("__se_compat_sys"): continue
39                if name.startswith("__addressable_"): continue
40                if name == "linux_banner": continue
41                if name == "vermagic": continue
42                # statics and some other optimizations adds random .NUMBER
43                name = re_NUMBER.sub('', name)
44                sym[name] = sym.get(name, 0) + int(size, 16)
45    return sym
46
47def calc(oldfile, newfile, format):
48    old = getsizes(oldfile, format)
49    new = getsizes(newfile, format)
50    grow, shrink, add, remove, up, down = 0, 0, 0, 0, 0, 0
51    delta, common = [], {}
52    otot, ntot = 0, 0
53
54    for a in old:
55        if a in new:
56            common[a] = 1
57
58    for name in old:
59        otot += old[name]
60        if name not in common:
61            remove += 1
62            down += old[name]
63            delta.append((-old[name], name))
64
65    for name in new:
66        ntot += new[name]
67        if name not in common:
68            add += 1
69            up += new[name]
70            delta.append((new[name], name))
71
72    for name in common:
73        d = new.get(name, 0) - old.get(name, 0)
74        if d>0: grow, up = grow+1, up+d
75        if d<0: shrink, down = shrink+1, down-d
76        delta.append((d, name))
77
78    delta.sort()
79    delta.reverse()
80    return grow, shrink, add, remove, up, down, delta, old, new, otot, ntot
81
82def print_result(symboltype, symbolformat):
83    grow, shrink, add, remove, up, down, delta, old, new, otot, ntot = \
84    calc(args.file1, args.file2, symbolformat)
85
86    print("add/remove: %s/%s grow/shrink: %s/%s up/down: %s/%s (%s)" % \
87          (add, remove, grow, shrink, up, -down, up-down))
88    print("%-40s %7s %7s %+7s" % (symboltype, "old", "new", "delta"))
89    for d, n in delta:
90        if d: print("%-40s %7s %7s %+7d" % (n, old.get(n,"-"), new.get(n,"-"), d))
91
92    if otot:
93        percent = (ntot - otot) * 100.0 / otot
94    else:
95        percent = 0
96    print("Total: Before=%d, After=%d, chg %+.2f%%" % (otot, ntot, percent))
97
98if args.c:
99    print_result("Function", "tT")
100    print_result("Data", "dDbB")
101    print_result("RO Data", "rR")
102elif args.d:
103    print_result("Data", "dDbBrR")
104elif args.t:
105    print_result("Function", "tT")
106else:
107    print_result("Function", "tTdDbBrR")
108