3
0

bloat-o-meter 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. if "." in name: name = "static." + name.split(".")[0]
  19. sym[name] = sym.get(name, 0) + int(size, 16)
  20. for l in os.popen("readelf -S " + file).readlines():
  21. x = l.split()
  22. if len(x)<6 or x[1] != ".rodata": continue
  23. sym[".rodata"] = int(x[5], 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 "%-48s %7s %7s %+7s" % ("function", "old", "new", "delta")
  50. for d, n in delta:
  51. if d: print "%-48s %7s %7s %+7d" % (n, old.get(n,"-"), new.get(n,"-"), d)
  52. print "-"*78
  53. total="(add/remove: %s/%s grow/shrink: %s/%s up/down: %s/%s)%%sTotal: %s bytes"\
  54. % (add, remove, grow, shrink, up, -down, up-down)
  55. print total % (" "*(80-len(total)))