3
0

bloat-o-meter 4.6 KB

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