bloat-o-meter 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. for f in sys.argv[1], sys.argv[2]:
  14. if not os.path.exists(f):
  15. sys.stderr.write("Error: file '%s' does not exist\n" % f)
  16. sys.exit(-1)
  17. def getsizes(file):
  18. sym = {}
  19. for l in os.popen("nm --size-sort " + file).readlines():
  20. size, type, name = l[:-1].split()
  21. if type in "tTdDbBrR":
  22. if "." in name: name = "static." + name.split(".")[0]
  23. sym[name] = sym.get(name, 0) + int(size, 16)
  24. for l in os.popen("readelf -S " + file).readlines():
  25. x = l.split()
  26. if len(x)<6 or x[1] != ".rodata": continue
  27. sym[".rodata"] = int(x[5], 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. for a in old:
  34. if a in new:
  35. common[a] = 1
  36. for name in old:
  37. if name not in common:
  38. remove += 1
  39. down += old[name]
  40. delta.append((-old[name], name))
  41. for name in new:
  42. if name not in common:
  43. add += 1
  44. up += new[name]
  45. delta.append((new[name], name))
  46. for name in common:
  47. d = new.get(name, 0) - old.get(name, 0)
  48. if d>0: grow, up = grow+1, up+d
  49. if d<0: shrink, down = shrink+1, down-d
  50. delta.append((d, name))
  51. delta.sort()
  52. delta.reverse()
  53. print "%-48s %7s %7s %+7s" % ("function", "old", "new", "delta")
  54. for d, n in delta:
  55. if d: print "%-48s %7s %7s %+7d" % (n, old.get(n,"-"), new.get(n,"-"), d)
  56. print "-"*78
  57. total="(add/remove: %s/%s grow/shrink: %s/%s up/down: %s/%s)%%sTotal: %s bytes"\
  58. % (add, remove, grow, shrink, up, -down, up-down)
  59. print total % (" "*(80-len(total)))