bloat-o-meter 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. def usage():
  11. sys.stderr.write("usage: %s file1 file2\n" % sys.argv[0])
  12. sys.exit(-1)
  13. if len(sys.argv) < 3:
  14. usage()
  15. for f in sys.argv[1], sys.argv[2]:
  16. if not os.path.exists(f):
  17. sys.stderr.write("Error: file '%s' does not exist\n" % f)
  18. usage()
  19. nm_args = " ".join([x for x in sys.argv[3:]])
  20. def getsizes(file):
  21. sym = {}
  22. for l in os.popen("nm --size-sort %s %s" % (nm_args, file)).readlines():
  23. l = l.strip()
  24. # Skip empty lines
  25. if not len(l): continue
  26. # Skip archive members
  27. if len(l.split()) == 1 and l.endswith(':'):
  28. continue
  29. size, type, name = l.split()
  30. if type in "tTdDbBrR":
  31. if "." in name: name = "static." + name.split(".")[0]
  32. sym[name] = sym.get(name, 0) + int(size, 16)
  33. for l in os.popen("readelf -S " + file).readlines():
  34. x = l.split()
  35. if len(x)<6 or x[1] != ".rodata": continue
  36. sym[".rodata"] = int(x[5], 16)
  37. return sym
  38. old = getsizes(sys.argv[1])
  39. new = getsizes(sys.argv[2])
  40. grow, shrink, add, remove, up, down = 0, 0, 0, 0, 0, 0
  41. delta, common = [], {}
  42. for a in old:
  43. if a in new:
  44. common[a] = 1
  45. for name in old:
  46. if name not in common:
  47. remove += 1
  48. down += old[name]
  49. delta.append((-old[name], name))
  50. for name in new:
  51. if name not in common:
  52. add += 1
  53. up += new[name]
  54. delta.append((new[name], name))
  55. for name in common:
  56. d = new.get(name, 0) - old.get(name, 0)
  57. if d>0: grow, up = grow+1, up+d
  58. if d<0: shrink, down = shrink+1, down-d
  59. delta.append((d, name))
  60. delta.sort()
  61. delta.reverse()
  62. print "%-48s %7s %7s %+7s" % ("function", "old", "new", "delta")
  63. for d, n in delta:
  64. if d: print "%-48s %7s %7s %+7d" % (n, old.get(n,"-"), new.get(n,"-"), d)
  65. print "-"*78
  66. total="(add/remove: %s/%s grow/shrink: %s/%s up/down: %s/%s)%%sTotal: %s bytes"\
  67. % (add, remove, grow, shrink, up, -down, up-down)
  68. print total % (" "*(80-len(total)))