51839e29cb595 (Andy Shevchenko 2020-12-09 13:50:17 +0200 1) #!/usr/bin/env python3
d960600df3ce3 (Matt Mackall 2006-01-08 01:05:19 -0800 2) #
d960600df3ce3 (Matt Mackall 2006-01-08 01:05:19 -0800 3) # Copyright 2004 Matt Mackall <mpm@selenic.com>
d960600df3ce3 (Matt Mackall 2006-01-08 01:05:19 -0800 4) #
d960600df3ce3 (Matt Mackall 2006-01-08 01:05:19 -0800 5) # inspired by perl Bloat-O-Meter (c) 1997 by Andi Kleen
d960600df3ce3 (Matt Mackall 2006-01-08 01:05:19 -0800 6) #
d960600df3ce3 (Matt Mackall 2006-01-08 01:05:19 -0800 7) # This software may be used and distributed according to the terms
d960600df3ce3 (Matt Mackall 2006-01-08 01:05:19 -0800 8) # of the GNU General Public License, incorporated herein by reference.
d960600df3ce3 (Matt Mackall 2006-01-08 01:05:19 -0800 9)
d960600df3ce3 (Matt Mackall 2006-01-08 01:05:19 -0800 10) import sys, os, re
eef06b82f16c7 (Alexey Dobriyan 2016-11-10 10:46:13 -0800 11) from signal import signal, SIGPIPE, SIG_DFL
eef06b82f16c7 (Alexey Dobriyan 2016-11-10 10:46:13 -0800 12)
eef06b82f16c7 (Alexey Dobriyan 2016-11-10 10:46:13 -0800 13) signal(SIGPIPE, SIG_DFL)
d960600df3ce3 (Matt Mackall 2006-01-08 01:05:19 -0800 14)
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 15) if len(sys.argv) < 3:
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 16) sys.stderr.write("usage: %s [option] file1 file2\n" % sys.argv[0])
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 17) sys.stderr.write("The options are:\n")
61fc470814d8a (Matteo Croce 2018-02-14 18:47:18 +0100 18) sys.stderr.write("-c categorize output based on symbol type\n")
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 19) sys.stderr.write("-d Show delta of Data Section\n")
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 20) sys.stderr.write("-t Show delta of text Section\n")
d960600df3ce3 (Matt Mackall 2006-01-08 01:05:19 -0800 21) sys.exit(-1)
d960600df3ce3 (Matt Mackall 2006-01-08 01:05:19 -0800 22)
0d7bbb43641c3 (Alexey Dobriyan 2016-12-12 16:40:48 -0800 23) re_NUMBER = re.compile(r'\.[0-9]+')
0d7bbb43641c3 (Alexey Dobriyan 2016-12-12 16:40:48 -0800 24)
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 25) def getsizes(file, format):
d960600df3ce3 (Matt Mackall 2006-01-08 01:05:19 -0800 26) sym = {}
3af06fd96aae1 (Alexey Dobriyan 2016-12-12 16:40:45 -0800 27) with os.popen("nm --size-sort " + file) as f:
3af06fd96aae1 (Alexey Dobriyan 2016-12-12 16:40:45 -0800 28) for line in f:
1d35b6054a9b9 (Nikolay Borisov 2020-08-06 23:17:32 -0700 29) if line.startswith("\n") or ":" in line:
1d35b6054a9b9 (Nikolay Borisov 2020-08-06 23:17:32 -0700 30) continue
3af06fd96aae1 (Alexey Dobriyan 2016-12-12 16:40:45 -0800 31) size, type, name = line.split()
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 32) if type in format:
3af06fd96aae1 (Alexey Dobriyan 2016-12-12 16:40:45 -0800 33) # strip generated symbols
3af06fd96aae1 (Alexey Dobriyan 2016-12-12 16:40:45 -0800 34) if name.startswith("__mod_"): continue
e145242ea0df6 (Dominik Brodowski 2018-04-09 12:51:42 +0200 35) if name.startswith("__se_sys"): continue
5ac9efa3c50d7 (Dominik Brodowski 2018-04-09 12:51:43 +0200 36) if name.startswith("__se_compat_sys"): continue
e0b2475a3f36a (Rasmus Villemoes 2018-12-28 00:31:18 -0800 37) if name.startswith("__addressable_"): continue
3af06fd96aae1 (Alexey Dobriyan 2016-12-12 16:40:45 -0800 38) if name == "linux_banner": continue
3af06fd96aae1 (Alexey Dobriyan 2016-12-12 16:40:45 -0800 39) # statics and some other optimizations adds random .NUMBER
0d7bbb43641c3 (Alexey Dobriyan 2016-12-12 16:40:48 -0800 40) name = re_NUMBER.sub('', name)
3af06fd96aae1 (Alexey Dobriyan 2016-12-12 16:40:45 -0800 41) sym[name] = sym.get(name, 0) + int(size, 16)
d960600df3ce3 (Matt Mackall 2006-01-08 01:05:19 -0800 42) return sym
d960600df3ce3 (Matt Mackall 2006-01-08 01:05:19 -0800 43)
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 44) def calc(oldfile, newfile, format):
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 45) old = getsizes(oldfile, format)
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 46) new = getsizes(newfile, format)
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 47) grow, shrink, add, remove, up, down = 0, 0, 0, 0, 0, 0
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 48) delta, common = [], {}
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 49) otot, ntot = 0, 0
d960600df3ce3 (Matt Mackall 2006-01-08 01:05:19 -0800 50)
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 51) for a in old:
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 52) if a in new:
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 53) common[a] = 1
d960600df3ce3 (Matt Mackall 2006-01-08 01:05:19 -0800 54)
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 55) for name in old:
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 56) otot += old[name]
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 57) if name not in common:
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 58) remove += 1
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 59) down += old[name]
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 60) delta.append((-old[name], name))
d960600df3ce3 (Matt Mackall 2006-01-08 01:05:19 -0800 61)
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 62) for name in new:
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 63) ntot += new[name]
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 64) if name not in common:
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 65) add += 1
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 66) up += new[name]
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 67) delta.append((new[name], name))
d960600df3ce3 (Matt Mackall 2006-01-08 01:05:19 -0800 68)
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 69) for name in common:
d960600df3ce3 (Matt Mackall 2006-01-08 01:05:19 -0800 70) d = new.get(name, 0) - old.get(name, 0)
d960600df3ce3 (Matt Mackall 2006-01-08 01:05:19 -0800 71) if d>0: grow, up = grow+1, up+d
d960600df3ce3 (Matt Mackall 2006-01-08 01:05:19 -0800 72) if d<0: shrink, down = shrink+1, down-d
d960600df3ce3 (Matt Mackall 2006-01-08 01:05:19 -0800 73) delta.append((d, name))
d960600df3ce3 (Matt Mackall 2006-01-08 01:05:19 -0800 74)
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 75) delta.sort()
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 76) delta.reverse()
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 77) return grow, shrink, add, remove, up, down, delta, old, new, otot, ntot
d960600df3ce3 (Matt Mackall 2006-01-08 01:05:19 -0800 78)
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 79) def print_result(symboltype, symbolformat, argc):
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 80) grow, shrink, add, remove, up, down, delta, old, new, otot, ntot = \
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 81) calc(sys.argv[argc - 1], sys.argv[argc], symbolformat)
b21e91c305bce (Vineet Gupta 2016-05-19 17:09:17 -0700 82)
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 83) print("add/remove: %s/%s grow/shrink: %s/%s up/down: %s/%s (%s)" % \
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 84) (add, remove, grow, shrink, up, -down, up-down))
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 85) print("%-40s %7s %7s %+7s" % (symboltype, "old", "new", "delta"))
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 86) for d, n in delta:
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 87) if d: print("%-40s %7s %7s %+7d" % (n, old.get(n,"-"), new.get(n,"-"), d))
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 88)
edbddb83a15b4 (Andy Shevchenko 2017-11-29 16:11:05 -0800 89) if otot:
edbddb83a15b4 (Andy Shevchenko 2017-11-29 16:11:05 -0800 90) percent = (ntot - otot) * 100.0 / otot
edbddb83a15b4 (Andy Shevchenko 2017-11-29 16:11:05 -0800 91) else:
edbddb83a15b4 (Andy Shevchenko 2017-11-29 16:11:05 -0800 92) percent = 0
edbddb83a15b4 (Andy Shevchenko 2017-11-29 16:11:05 -0800 93) print("Total: Before=%d, After=%d, chg %+.2f%%" % (otot, ntot, percent))
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 94)
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 95) if sys.argv[1] == "-c":
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 96) print_result("Function", "tT", 3)
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 97) print_result("Data", "dDbB", 3)
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 98) print_result("RO Data", "rR", 3)
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 99) elif sys.argv[1] == "-d":
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 100) print_result("Data", "dDbBrR", 3)
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 101) elif sys.argv[1] == "-t":
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 102) print_result("Function", "tT", 3)
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 103) else:
192efb7a1f9b6 (Maninder Singh 2017-11-15 17:31:14 -0800 104) print_result("Function", "tTdDbBrR", 2)