bloat-o-meter 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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#, 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. for l in os.popen("readelf -W -s %s %s" % (sym_args, file)).readlines():
  38. l = l.strip()
  39. if not (len(l) and l[0].isdigit() and len(l.split()) == 8):
  40. continue
  41. num, value, size, typ, bind, vis, ndx, name = l.split()
  42. if ndx == "UND": continue # skip undefined
  43. if typ in ["SECTION", "FILES"]: continue # skip sections and files
  44. if "." in name: name = "static." + name.split(".")[0]
  45. value = int(value, 16)
  46. size = int(size, 16) if size.startswith('0x') else int(size)
  47. if vis != "DEFAULT" and bind != "GLOBAL": # see if it is an alias
  48. alias[(value, size)] = {"name" : name}
  49. else:
  50. sym[name] = {"addr" : value, "size": size}
  51. lut[(value, size)] = 0
  52. for addr, sz in iter(alias.keys()):
  53. # If the non-GLOBAL sym has an implementation elsewhere then
  54. # it's an alias, disregard it.
  55. if not (addr, sz) in lut:
  56. # If this non-GLOBAL sym does not have an implementation at
  57. # another address, then treat it as a normal symbol.
  58. sym[alias[(addr, sz)]["name"]] = {"addr" : addr, "size": sz}
  59. for l in os.popen("readelf -W -S " + file).readlines():
  60. x = l.split()
  61. if len(x)<6: continue
  62. # Should take these into account too!
  63. #if x[1] not in [".text", ".rodata", ".symtab", ".strtab"]: continue
  64. if x[1] not in [".rodata"]: continue
  65. sym[x[1]] = {"addr" : int(x[3], 16), "size" : int(x[5], 16)}
  66. return sym
  67. if flag_timing:
  68. start_t1 = int(time.time() * 1e9)
  69. old = getsizes(f1)
  70. if flag_timing:
  71. end_t1 = int(time.time() * 1e9)
  72. start_t2 = int(time.time() * 1e9)
  73. new = getsizes(f2)
  74. if flag_timing:
  75. end_t2 = int(time.time() * 1e9)
  76. start_t3 = int(time.time() * 1e9)
  77. grow, shrink, add, remove, up, down = 0, 0, 0, 0, 0, 0
  78. delta, common = [], {}
  79. for name in iter(old.keys()):
  80. if name in new:
  81. common[name] = 1
  82. for name in old:
  83. if name not in common:
  84. remove += 1
  85. sz = old[name]["size"]
  86. down += sz
  87. delta.append((-sz, name))
  88. for name in new:
  89. if name not in common:
  90. add += 1
  91. sz = new[name]["size"]
  92. up += sz
  93. delta.append((sz, name))
  94. for name in common:
  95. d = new[name].get("size", 0) - old[name].get("size", 0)
  96. if d>0: grow, up = grow+1, up+d
  97. elif d<0: shrink, down = shrink+1, down-d
  98. else:
  99. continue
  100. delta.append((d, name))
  101. delta.sort()
  102. delta.reverse()
  103. if flag_timing:
  104. end_t3 = int(time.time() * 1e9)
  105. print("%-48s %7s %7s %+7s" % ("function", "old", "new", "delta"))
  106. for d, n in delta:
  107. if d:
  108. old_sz = old.get(n, {}).get("size", "-")
  109. new_sz = new.get(n, {}).get("size", "-")
  110. print("%-48s %7s %7s %+7d" % (n, old_sz, new_sz, d))
  111. print("-"*78)
  112. total="(add/remove: %s/%s grow/shrink: %s/%s up/down: %s/%s)%%sTotal: %s bytes"\
  113. % (add, remove, grow, shrink, up, -down, up-down)
  114. print(total % (" "*(80-len(total))))
  115. if flag_timing:
  116. print("\n%d/%d; %d Parse origin/new; processing nsecs" %
  117. (end_t1-start_t1, end_t2-start_t2, end_t3-start_t3))
  118. print("total nsecs: %d" % (end_t3-start_t1))