bloat-o-meter 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright 2004 Matt Mackall <[email protected]>
  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. import sys, os, re, argparse
  10. from signal import signal, SIGPIPE, SIG_DFL
  11. signal(SIGPIPE, SIG_DFL)
  12. parser = argparse.ArgumentParser(description="Simple script used to compare the symbol sizes of 2 object files")
  13. group = parser.add_mutually_exclusive_group()
  14. group.add_argument('-c', help='categorize output based on symbol type', action='store_true')
  15. group.add_argument('-d', help='Show delta of Data Section', action='store_true')
  16. group.add_argument('-t', help='Show delta of text Section', action='store_true')
  17. parser.add_argument('-p', dest='prefix', help='Arch prefix for the tool being used. Useful in cross build scenarios')
  18. parser.add_argument('file1', help='First file to compare')
  19. parser.add_argument('file2', help='Second file to compare')
  20. args = parser.parse_args()
  21. re_NUMBER = re.compile(r'\.[0-9]+')
  22. def getsizes(file, format):
  23. sym = {}
  24. nm = "nm"
  25. if args.prefix:
  26. nm = "{}nm".format(args.prefix)
  27. with os.popen("{} --size-sort {}".format(nm, file)) as f:
  28. for line in f:
  29. if line.startswith("\n") or ":" in line:
  30. continue
  31. size, type, name = line.split()
  32. if type in format:
  33. # strip generated symbols
  34. if name.startswith("__mod_"): continue
  35. if name.startswith("__se_sys"): continue
  36. if name.startswith("__se_compat_sys"): continue
  37. if name.startswith("__addressable_"): continue
  38. if name == "linux_banner": continue
  39. if name == "vermagic": continue
  40. # statics and some other optimizations adds random .NUMBER
  41. name = re_NUMBER.sub('', name)
  42. sym[name] = sym.get(name, 0) + int(size, 16)
  43. return sym
  44. def calc(oldfile, newfile, format):
  45. old = getsizes(oldfile, format)
  46. new = getsizes(newfile, format)
  47. grow, shrink, add, remove, up, down = 0, 0, 0, 0, 0, 0
  48. delta, common = [], {}
  49. otot, ntot = 0, 0
  50. for a in old:
  51. if a in new:
  52. common[a] = 1
  53. for name in old:
  54. otot += old[name]
  55. if name not in common:
  56. remove += 1
  57. down += old[name]
  58. delta.append((-old[name], name))
  59. for name in new:
  60. ntot += new[name]
  61. if name not in common:
  62. add += 1
  63. up += new[name]
  64. delta.append((new[name], name))
  65. for name in common:
  66. d = new.get(name, 0) - old.get(name, 0)
  67. if d>0: grow, up = grow+1, up+d
  68. if d<0: shrink, down = shrink+1, down-d
  69. delta.append((d, name))
  70. delta.sort()
  71. delta.reverse()
  72. return grow, shrink, add, remove, up, down, delta, old, new, otot, ntot
  73. def print_result(symboltype, symbolformat):
  74. grow, shrink, add, remove, up, down, delta, old, new, otot, ntot = \
  75. calc(args.file1, args.file2, symbolformat)
  76. print("add/remove: %s/%s grow/shrink: %s/%s up/down: %s/%s (%s)" % \
  77. (add, remove, grow, shrink, up, -down, up-down))
  78. print("%-40s %7s %7s %+7s" % (symboltype, "old", "new", "delta"))
  79. for d, n in delta:
  80. if d: print("%-40s %7s %7s %+7d" % (n, old.get(n,"-"), new.get(n,"-"), d))
  81. if otot:
  82. percent = (ntot - otot) * 100.0 / otot
  83. else:
  84. percent = 0
  85. print("Total: Before=%d, After=%d, chg %+.2f%%" % (otot, ntot, percent))
  86. if args.c:
  87. print_result("Function", "tT")
  88. print_result("Data", "dDbB")
  89. print_result("RO Data", "rR")
  90. elif args.d:
  91. print_result("Data", "dDbBrR")
  92. elif args.t:
  93. print_result("Function", "tT")
  94. else:
  95. print_result("Function", "tTdDbBrR")