bloat-o-meter 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. from signal import signal, SIGPIPE, SIG_DFL
  11. signal(SIGPIPE, SIG_DFL)
  12. if len(sys.argv) < 3:
  13. sys.stderr.write("usage: %s [option] file1 file2\n" % sys.argv[0])
  14. sys.stderr.write("The options are:\n")
  15. sys.stderr.write("-c categorize output based on symbol type\n")
  16. sys.stderr.write("-d Show delta of Data Section\n")
  17. sys.stderr.write("-t Show delta of text Section\n")
  18. sys.exit(-1)
  19. re_NUMBER = re.compile(r'\.[0-9]+')
  20. def getsizes(file, format):
  21. sym = {}
  22. with os.popen("nm --size-sort " + file) as f:
  23. for line in f:
  24. size, type, name = line.split()
  25. if type in format:
  26. # strip generated symbols
  27. if name.startswith("__mod_"): continue
  28. if name.startswith("__se_sys"): continue
  29. if name.startswith("__se_compat_sys"): continue
  30. if name == "linux_banner": continue
  31. # statics and some other optimizations adds random .NUMBER
  32. name = re_NUMBER.sub('', name)
  33. sym[name] = sym.get(name, 0) + int(size, 16)
  34. return sym
  35. def calc(oldfile, newfile, format):
  36. old = getsizes(oldfile, format)
  37. new = getsizes(newfile, format)
  38. grow, shrink, add, remove, up, down = 0, 0, 0, 0, 0, 0
  39. delta, common = [], {}
  40. otot, ntot = 0, 0
  41. for a in old:
  42. if a in new:
  43. common[a] = 1
  44. for name in old:
  45. otot += old[name]
  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. ntot += new[name]
  52. if name not in common:
  53. add += 1
  54. up += new[name]
  55. delta.append((new[name], name))
  56. for name in common:
  57. d = new.get(name, 0) - old.get(name, 0)
  58. if d>0: grow, up = grow+1, up+d
  59. if d<0: shrink, down = shrink+1, down-d
  60. delta.append((d, name))
  61. delta.sort()
  62. delta.reverse()
  63. return grow, shrink, add, remove, up, down, delta, old, new, otot, ntot
  64. def print_result(symboltype, symbolformat, argc):
  65. grow, shrink, add, remove, up, down, delta, old, new, otot, ntot = \
  66. calc(sys.argv[argc - 1], sys.argv[argc], symbolformat)
  67. print("add/remove: %s/%s grow/shrink: %s/%s up/down: %s/%s (%s)" % \
  68. (add, remove, grow, shrink, up, -down, up-down))
  69. print("%-40s %7s %7s %+7s" % (symboltype, "old", "new", "delta"))
  70. for d, n in delta:
  71. if d: print("%-40s %7s %7s %+7d" % (n, old.get(n,"-"), new.get(n,"-"), d))
  72. if otot:
  73. percent = (ntot - otot) * 100.0 / otot
  74. else:
  75. percent = 0
  76. print("Total: Before=%d, After=%d, chg %+.2f%%" % (otot, ntot, percent))
  77. if sys.argv[1] == "-c":
  78. print_result("Function", "tT", 3)
  79. print_result("Data", "dDbB", 3)
  80. print_result("RO Data", "rR", 3)
  81. elif sys.argv[1] == "-d":
  82. print_result("Data", "dDbBrR", 3)
  83. elif sys.argv[1] == "-t":
  84. print_result("Function", "tT", 3)
  85. else:
  86. print_result("Function", "tTdDbBrR", 2)