bloat-o-meter 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. if len(sys.argv) != 3:
  11. sys.stderr.write("usage: %s file1 file2\n" % sys.argv[0])
  12. sys.exit(-1)
  13. def getsizes(file):
  14. sym = {}
  15. for l in os.popen("nm --size-sort " + file).readlines():
  16. size, type, name = l[:-1].split()
  17. if type in "tTdDbBrR":
  18. # strip generated symbols
  19. if name.startswith("__mod_"): continue
  20. if name == "linux_banner": continue
  21. # statics and some other optimizations adds random .NUMBER
  22. name = re.sub(r'\.[0-9]+', '', name)
  23. sym[name] = sym.get(name, 0) + int(size, 16)
  24. return sym
  25. old = getsizes(sys.argv[1])
  26. new = getsizes(sys.argv[2])
  27. grow, shrink, add, remove, up, down = 0, 0, 0, 0, 0, 0
  28. delta, common = [], {}
  29. for a in old:
  30. if a in new:
  31. common[a] = 1
  32. for name in old:
  33. if name not in common:
  34. remove += 1
  35. down += old[name]
  36. delta.append((-old[name], name))
  37. for name in new:
  38. if name not in common:
  39. add += 1
  40. up += new[name]
  41. delta.append((new[name], name))
  42. for name in common:
  43. d = new.get(name, 0) - old.get(name, 0)
  44. if d>0: grow, up = grow+1, up+d
  45. if d<0: shrink, down = shrink+1, down-d
  46. delta.append((d, name))
  47. delta.sort()
  48. delta.reverse()
  49. print "add/remove: %s/%s grow/shrink: %s/%s up/down: %s/%s (%s)" % \
  50. (add, remove, grow, shrink, up, -down, up-down)
  51. print "%-40s %7s %7s %+7s" % ("function", "old", "new", "delta")
  52. for d, n in delta:
  53. if d: print "%-40s %7s %7s %+7d" % (n, old.get(n,"-"), new.get(n,"-"), d)