bloat-o-meter 2.1 KB

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