bloat-o-meter 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/usr/bin/env python3
  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. if line.startswith("\n") or ":" in line:
  25. continue
  26. size, type, name = line.split()
  27. if type in format:
  28. # strip generated symbols
  29. if name.startswith("__mod_"): continue
  30. if name.startswith("__se_sys"): continue
  31. if name.startswith("__se_compat_sys"): continue
  32. if name.startswith("__addressable_"): continue
  33. if name == "linux_banner": continue
  34. # statics and some other optimizations adds random .NUMBER
  35. name = re_NUMBER.sub('', name)
  36. sym[name] = sym.get(name, 0) + int(size, 16)
  37. return sym
  38. def calc(oldfile, newfile, format):
  39. old = getsizes(oldfile, format)
  40. new = getsizes(newfile, format)
  41. grow, shrink, add, remove, up, down = 0, 0, 0, 0, 0, 0
  42. delta, common = [], {}
  43. otot, ntot = 0, 0
  44. for a in old:
  45. if a in new:
  46. common[a] = 1
  47. for name in old:
  48. otot += old[name]
  49. if name not in common:
  50. remove += 1
  51. down += old[name]
  52. delta.append((-old[name], name))
  53. for name in new:
  54. ntot += new[name]
  55. if name not in common:
  56. add += 1
  57. up += new[name]
  58. delta.append((new[name], name))
  59. for name in common:
  60. d = new.get(name, 0) - old.get(name, 0)
  61. if d>0: grow, up = grow+1, up+d
  62. if d<0: shrink, down = shrink+1, down-d
  63. delta.append((d, name))
  64. delta.sort()
  65. delta.reverse()
  66. return grow, shrink, add, remove, up, down, delta, old, new, otot, ntot
  67. def print_result(symboltype, symbolformat, argc):
  68. grow, shrink, add, remove, up, down, delta, old, new, otot, ntot = \
  69. calc(sys.argv[argc - 1], sys.argv[argc], symbolformat)
  70. print("add/remove: %s/%s grow/shrink: %s/%s up/down: %s/%s (%s)" % \
  71. (add, remove, grow, shrink, up, -down, up-down))
  72. print("%-40s %7s %7s %+7s" % (symboltype, "old", "new", "delta"))
  73. for d, n in delta:
  74. if d: print("%-40s %7s %7s %+7d" % (n, old.get(n,"-"), new.get(n,"-"), d))
  75. if otot:
  76. percent = (ntot - otot) * 100.0 / otot
  77. else:
  78. percent = 0
  79. print("Total: Before=%d, After=%d, chg %+.2f%%" % (otot, ntot, percent))
  80. if sys.argv[1] == "-c":
  81. print_result("Function", "tT", 3)
  82. print_result("Data", "dDbB", 3)
  83. print_result("RO Data", "rR", 3)
  84. elif sys.argv[1] == "-d":
  85. print_result("Data", "dDbBrR", 3)
  86. elif sys.argv[1] == "-t":
  87. print_result("Function", "tT", 3)
  88. else:
  89. print_result("Function", "tTdDbBrR", 2)