151839e29SAndy Shevchenko#!/usr/bin/env python3 2d960600dSMatt Mackall# 3d960600dSMatt Mackall# Copyright 2004 Matt Mackall <mpm@selenic.com> 4d960600dSMatt Mackall# 5d960600dSMatt Mackall# inspired by perl Bloat-O-Meter (c) 1997 by Andi Kleen 6d960600dSMatt Mackall# 7d960600dSMatt Mackall# This software may be used and distributed according to the terms 8d960600dSMatt Mackall# of the GNU General Public License, incorporated herein by reference. 9d960600dSMatt Mackall 10b62eb273SNikolay Borisovimport sys, os, re, argparse 11eef06b82SAlexey Dobriyanfrom signal import signal, SIGPIPE, SIG_DFL 12eef06b82SAlexey Dobriyan 13eef06b82SAlexey Dobriyansignal(SIGPIPE, SIG_DFL) 14d960600dSMatt Mackall 15b62eb273SNikolay Borisovparser = argparse.ArgumentParser(description="Simple script used to compare the symbol sizes of 2 object files") 16b62eb273SNikolay Borisovgroup = parser.add_mutually_exclusive_group() 17b62eb273SNikolay Borisovgroup.add_argument('-c', help='categorize output based on symbol type', action='store_true') 18b62eb273SNikolay Borisovgroup.add_argument('-d', help='Show delta of Data Section', action='store_true') 19b62eb273SNikolay Borisovgroup.add_argument('-t', help='Show delta of text Section', action='store_true') 208b5db667SNikolay Borisovparser.add_argument('-p', dest='prefix', help='Arch prefix for the tool being used. Useful in cross build scenarios') 213f80aa1aSValtteri Koskivuoriparser.add_argument('file_old', help='First file to compare') 223f80aa1aSValtteri Koskivuoriparser.add_argument('file_new', help='Second file to compare') 23b62eb273SNikolay Borisov 24b62eb273SNikolay Borisovargs = parser.parse_args() 25d960600dSMatt Mackall 260d7bbb43SAlexey Dobriyanre_NUMBER = re.compile(r'\.[0-9]+') 270d7bbb43SAlexey Dobriyan 28192efb7aSManinder Singhdef getsizes(file, format): 29d960600dSMatt Mackall sym = {} 308b5db667SNikolay Borisov nm = "nm" 318b5db667SNikolay Borisov if args.prefix: 328b5db667SNikolay Borisov nm = "{}nm".format(args.prefix) 338b5db667SNikolay Borisov 348b5db667SNikolay Borisov with os.popen("{} --size-sort {}".format(nm, file)) as f: 353af06fd9SAlexey Dobriyan for line in f: 361d35b605SNikolay Borisov if line.startswith("\n") or ":" in line: 371d35b605SNikolay Borisov continue 383af06fd9SAlexey Dobriyan size, type, name = line.split() 39192efb7aSManinder Singh if type in format: 40c50e3f51SJean Delvare # strip generated symbols 41c2e182faSJosh Triplett if name.startswith("__mod_"): continue 42e145242eSDominik Brodowski if name.startswith("__se_sys"): continue 435ac9efa3SDominik Brodowski if name.startswith("__se_compat_sys"): continue 44e0b2475aSRasmus Villemoes if name.startswith("__addressable_"): continue 45cc20650aSEric Dumazet if name.startswith("__noinstr_text_start"): continue 46*c210dfaaSYury Norov if name.startswith("_sdata"): continue 475a7b2d27SJosh Triplett if name == "linux_banner": continue 48dec81a53SPaul Gortmaker if name == "vermagic": continue 4921cf6e58SAndi Kleen # statics and some other optimizations adds random .NUMBER 500d7bbb43SAlexey Dobriyan name = re_NUMBER.sub('', name) 5151849738SRob Landley sym[name] = sym.get(name, 0) + int(size, 16) 52d960600dSMatt Mackall return sym 53d960600dSMatt Mackall 54192efb7aSManinder Singhdef calc(oldfile, newfile, format): 55192efb7aSManinder Singh old = getsizes(oldfile, format) 56192efb7aSManinder Singh new = getsizes(newfile, format) 57d960600dSMatt Mackall grow, shrink, add, remove, up, down = 0, 0, 0, 0, 0, 0 58d960600dSMatt Mackall delta, common = [], {} 59b21e91c3SVineet Gupta otot, ntot = 0, 0 60d960600dSMatt Mackall 61d960600dSMatt Mackall for a in old: 62d960600dSMatt Mackall if a in new: 63d960600dSMatt Mackall common[a] = 1 64d960600dSMatt Mackall 65d960600dSMatt Mackall for name in old: 66b21e91c3SVineet Gupta otot += old[name] 67d960600dSMatt Mackall if name not in common: 68d960600dSMatt Mackall remove += 1 69d960600dSMatt Mackall down += old[name] 70d960600dSMatt Mackall delta.append((-old[name], name)) 71d960600dSMatt Mackall 72d960600dSMatt Mackall for name in new: 73b21e91c3SVineet Gupta ntot += new[name] 74d960600dSMatt Mackall if name not in common: 75d960600dSMatt Mackall add += 1 76d960600dSMatt Mackall up += new[name] 77d960600dSMatt Mackall delta.append((new[name], name)) 78d960600dSMatt Mackall 79d960600dSMatt Mackall for name in common: 80d960600dSMatt Mackall d = new.get(name, 0) - old.get(name, 0) 81d960600dSMatt Mackall if d>0: grow, up = grow+1, up+d 82d960600dSMatt Mackall if d<0: shrink, down = shrink+1, down-d 83d960600dSMatt Mackall delta.append((d, name)) 84d960600dSMatt Mackall 850371ba6fSAlexander Pantyukhin delta.sort(reverse=True) 86192efb7aSManinder Singh return grow, shrink, add, remove, up, down, delta, old, new, otot, ntot 87192efb7aSManinder Singh 88b62eb273SNikolay Borisovdef print_result(symboltype, symbolformat): 89192efb7aSManinder Singh grow, shrink, add, remove, up, down, delta, old, new, otot, ntot = \ 903f80aa1aSValtteri Koskivuori calc(args.file_old, args.file_new, symbolformat) 91d960600dSMatt Mackall 9272214a24SSergey Senozhatsky print("add/remove: %s/%s grow/shrink: %s/%s up/down: %s/%s (%s)" % \ 9372214a24SSergey Senozhatsky (add, remove, grow, shrink, up, -down, up-down)) 94192efb7aSManinder Singh print("%-40s %7s %7s %+7s" % (symboltype, "old", "new", "delta")) 95d960600dSMatt Mackall for d, n in delta: 9672214a24SSergey Senozhatsky if d: print("%-40s %7s %7s %+7d" % (n, old.get(n,"-"), new.get(n,"-"), d)) 97b21e91c3SVineet Gupta 98edbddb83SAndy Shevchenko if otot: 99edbddb83SAndy Shevchenko percent = (ntot - otot) * 100.0 / otot 100edbddb83SAndy Shevchenko else: 101edbddb83SAndy Shevchenko percent = 0 102edbddb83SAndy Shevchenko print("Total: Before=%d, After=%d, chg %+.2f%%" % (otot, ntot, percent)) 103192efb7aSManinder Singh 104b62eb273SNikolay Borisovif args.c: 105198430f7SGeert Uytterhoeven print_result("Function", "tTwW") 106198430f7SGeert Uytterhoeven print_result("Data", "dDbBvV") 107b62eb273SNikolay Borisov print_result("RO Data", "rR") 108b62eb273SNikolay Borisovelif args.d: 109198430f7SGeert Uytterhoeven print_result("Data", "dDbBrRvV") 110b62eb273SNikolay Borisovelif args.t: 111198430f7SGeert Uytterhoeven print_result("Function", "tTwW") 112192efb7aSManinder Singhelse: 113198430f7SGeert Uytterhoeven print_result("Function", "tTdDbBrRvVwW") 114