bloat-o-meter 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. import sys, os, re
  10. from signal import signal, SIGPIPE, SIG_DFL
  11. signal(SIGPIPE, SIG_DFL)
  12. if len(sys.argv) != 3:
  13. sys.stderr.write("usage: %s file1 file2\n" % sys.argv[0])
  14. sys.exit(-1)
  15. re_NUMBER = re.compile(r'\.[0-9]+')
  16. def getsizes(file):
  17. sym = {}
  18. with os.popen("nm --size-sort " + file) as f:
  19. for line in f:
  20. size, type, name = line.split()
  21. if type in "tTdDbBrR":
  22. # strip generated symbols
  23. if name.startswith("__mod_"): continue
  24. if name.startswith("SyS_"): continue
  25. if name.startswith("compat_SyS_"): continue
  26. if name == "linux_banner": continue
  27. # statics and some other optimizations adds random .NUMBER
  28. name = re_NUMBER.sub('', name)
  29. sym[name] = sym.get(name, 0) + int(size, 16)
  30. return sym
  31. old = getsizes(sys.argv[1])
  32. new = getsizes(sys.argv[2])
  33. grow, shrink, add, remove, up, down = 0, 0, 0, 0, 0, 0
  34. delta, common = [], {}
  35. otot, ntot = 0, 0
  36. for a in old:
  37. if a in new:
  38. common[a] = 1
  39. for name in old:
  40. otot += old[name]
  41. if name not in common:
  42. remove += 1
  43. down += old[name]
  44. delta.append((-old[name], name))
  45. for name in new:
  46. ntot += new[name]
  47. if name not in common:
  48. add += 1
  49. up += new[name]
  50. delta.append((new[name], name))
  51. for name in common:
  52. d = new.get(name, 0) - old.get(name, 0)
  53. if d>0: grow, up = grow+1, up+d
  54. if d<0: shrink, down = shrink+1, down-d
  55. delta.append((d, name))
  56. delta.sort()
  57. delta.reverse()
  58. print("add/remove: %s/%s grow/shrink: %s/%s up/down: %s/%s (%s)" % \
  59. (add, remove, grow, shrink, up, -down, up-down))
  60. print("%-40s %7s %7s %+7s" % ("function", "old", "new", "delta"))
  61. for d, n in delta:
  62. if d: print("%-40s %7s %7s %+7d" % (n, old.get(n,"-"), new.get(n,"-"), d))
  63. print("Total: Before=%d, After=%d, chg %+.2f%%" % \
  64. (otot, ntot, (ntot - otot)*100.0/otot))