3
0

bloat-o-meter 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 [-t] file1 file2\n" % sys.argv[0])
  12. sys.exit(-1)
  13. f1, f2 = (None, None)
  14. flag_timing, dashes = (False, False)
  15. for f in sys.argv[1:]:
  16. if f.startswith("-"):
  17. if f == "--": # sym_args
  18. dashes = True
  19. break
  20. if f == "-t": # timings
  21. flag_timing = True
  22. else:
  23. if not os.path.exists(f):
  24. sys.stderr.write("Error: file '%s' does not exist\n" % f)
  25. usage()
  26. if f1 is None:
  27. f1 = f
  28. elif f2 is None:
  29. f2 = f
  30. if flag_timing:
  31. import time
  32. if f1 is None or f2 is None:
  33. usage()
  34. sym_args = " ".join(sys.argv[3 + flag_timing + dashes:])
  35. def getsizes(file):
  36. sym, alias, lut = {}, {}, {}
  37. #dynsym_filter = re.compile("^\d+:\s+[\dA-Fa-f]+\s+\d+\s+\w+\s+\w+\s+\w+\s+\w+\s+\w+$")
  38. for l in os.popen("readelf -W -s %s %s" % (sym_args, file)).readlines():
  39. if True:
  40. l = l.strip()
  41. if not (len(l) and l[0].isdigit() and len(l.split()) == 8):
  42. continue
  43. num, value, size, typ, bind, vis, ndx, name = l.split()
  44. if ndx == "UND": continue # skip undefined
  45. if typ in ["SECTION", "FILES"]: continue # skip sections and files
  46. #else:
  47. # l = l.strip()
  48. # match = dynsym_filter.match(l)
  49. # if not match: continue
  50. # x, value, size, typ, bind, x, ndx, name = l.split()
  51. # if ndx == "UND": continue # skip undefined
  52. # if typ in ["SECTION", "FILES"]: continue # skip sections and files
  53. if "." in name: name = "static." + name.split(".")[0]
  54. value = int(value, 16)
  55. size = int(size)
  56. if vis != "DEFAULT" and bind != "GLOBAL": # see if it is an alias
  57. alias[(value, size)] = {"name" : name}
  58. else:
  59. sym[name] = {"addr" : value, "size": size}
  60. lut[(value, size)] = 0
  61. for addr, sz in iter(alias.keys()):
  62. # If the non-GLOBAL sym has an implementation elsewhere then
  63. # it's an alias, disregard it.
  64. if not (addr, sz) in lut:
  65. # If this non-GLOBAL sym does not have an implementation at
  66. # another address, then treat it as a normal symbol.
  67. sym[alias[(addr, sz)]["name"]] = {"addr" : addr, "size": sz}
  68. for l in os.popen("readelf -W -S " + file).readlines():
  69. x = l.split()
  70. if len(x)<6: continue
  71. # Should take these into account too!
  72. #if x[1] not in [".text", ".rodata", ".symtab", ".strtab"]: continue
  73. if x[1] not in [".rodata"]: continue
  74. sym[x[1]] = {"addr" : int(x[3], 16), "size" : int(x[5], 16)}
  75. return sym
  76. if flag_timing:
  77. start_t1 = int(time.time() * 1e9)
  78. old = getsizes(f1)
  79. if flag_timing:
  80. end_t1 = int(time.time() * 1e9)
  81. start_t2 = int(time.time() * 1e9)
  82. new = getsizes(f2)
  83. if flag_timing:
  84. end_t2 = int(time.time() * 1e9)
  85. start_t3 = int(time.time() * 1e9)
  86. grow, shrink, add, remove, up, down = 0, 0, 0, 0, 0, 0
  87. delta, common = [], {}
  88. for name in iter(old.keys()):
  89. if name in new:
  90. common[name] = 1
  91. for name in old:
  92. if name not in common:
  93. remove += 1
  94. sz = old[name]["size"]
  95. down += sz
  96. delta.append((-sz, name))
  97. for name in new:
  98. if name not in common:
  99. add += 1
  100. sz = new[name]["size"]
  101. up += sz
  102. delta.append((sz, name))
  103. for name in common:
  104. d = new[name].get("size", 0) - old[name].get("size", 0)
  105. if d>0: grow, up = grow+1, up+d
  106. elif d<0: shrink, down = shrink+1, down-d
  107. else:
  108. continue
  109. delta.append((d, name))
  110. delta.sort()
  111. delta.reverse()
  112. if flag_timing:
  113. end_t3 = int(time.time() * 1e9)
  114. print("%-48s %7s %7s %+7s" % ("function", "old", "new", "delta"))
  115. for d, n in delta:
  116. if d:
  117. old_sz = old.get(n, {}).get("size", "-")
  118. new_sz = new.get(n, {}).get("size", "-")
  119. print("%-48s %7s %7s %+7d" % (n, old_sz, new_sz, d))
  120. print("-"*78)
  121. total="(add/remove: %s/%s grow/shrink: %s/%s up/down: %s/%s)%%sTotal: %s bytes"\
  122. % (add, remove, grow, shrink, up, -down, up-down)
  123. print(total % (" "*(80-len(total))))
  124. if flag_timing:
  125. print("\n%d/%d; %d Parse origin/new; processing nsecs" %
  126. (end_t1-start_t1, end_t2-start_t2, end_t3-start_t3))
  127. print("total nsecs: %d" % (end_t3-start_t1))