bloat_diff.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright 2017 gRPC authors.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. import argparse
  17. import csv
  18. import glob
  19. import math
  20. import multiprocessing
  21. import os
  22. import pathlib
  23. import shutil
  24. import subprocess
  25. import sys
  26. sys.path.append(
  27. os.path.join(os.path.dirname(sys.argv[0]), '..', '..', 'run_tests',
  28. 'python_utils'))
  29. import check_on_pr
  30. argp = argparse.ArgumentParser(description='Perform diff on microbenchmarks')
  31. argp.add_argument('-d',
  32. '--diff_base',
  33. type=str,
  34. help='Commit or branch to compare the current one to')
  35. argp.add_argument('-j', '--jobs', type=int, default=multiprocessing.cpu_count())
  36. args = argp.parse_args()
  37. # the libraries for which check bloat difference is calculated
  38. LIBS = [
  39. 'libgrpc.so',
  40. 'libgrpc++.so',
  41. ]
  42. def _build(output_dir):
  43. """Perform the cmake build under the output_dir."""
  44. shutil.rmtree(output_dir, ignore_errors=True)
  45. subprocess.check_call('mkdir -p %s' % output_dir, shell=True, cwd='.')
  46. subprocess.check_call([
  47. 'cmake', '-DgRPC_BUILD_TESTS=OFF', '-DBUILD_SHARED_LIBS=ON',
  48. '-DCMAKE_BUILD_TYPE=RelWithDebInfo', '-DCMAKE_C_FLAGS="-gsplit-dwarf"',
  49. '-DCMAKE_CXX_FLAGS="-gsplit-dwarf"', '..'
  50. ],
  51. cwd=output_dir)
  52. subprocess.check_call('make -j%d' % args.jobs, shell=True, cwd=output_dir)
  53. def _rank_diff_bytes(diff_bytes):
  54. """Determine how significant diff_bytes is, and return a simple integer representing that"""
  55. mul = 1
  56. if diff_bytes < 0:
  57. mul = -1
  58. diff_bytes = -diff_bytes
  59. if diff_bytes < 2 * 1024:
  60. return 0
  61. if diff_bytes < 16 * 1024:
  62. return 1 * mul
  63. if diff_bytes < 128 * 1024:
  64. return 2 * mul
  65. return 3 * mul
  66. _build('bloat_diff_new')
  67. if args.diff_base:
  68. where_am_i = subprocess.check_output(
  69. ['git', 'rev-parse', '--abbrev-ref', 'HEAD']).decode().strip()
  70. # checkout the diff base (="old")
  71. subprocess.check_call(['git', 'checkout', args.diff_base])
  72. subprocess.check_call(['git', 'submodule', 'update'])
  73. try:
  74. _build('bloat_diff_old')
  75. finally:
  76. # restore the original revision (="new")
  77. subprocess.check_call(['git', 'checkout', where_am_i])
  78. subprocess.check_call(['git', 'submodule', 'update'])
  79. pathlib.Path('bloaty-build').mkdir(exist_ok=True)
  80. subprocess.check_call(
  81. ['cmake', '-G', 'Unix Makefiles', '../third_party/bloaty'],
  82. cwd='bloaty-build')
  83. subprocess.check_call('make -j%d' % args.jobs, shell=True, cwd='bloaty-build')
  84. text = ''
  85. diff_size = 0
  86. for lib in LIBS:
  87. text += '****************************************************************\n\n'
  88. text += lib + '\n\n'
  89. old_version = glob.glob('bloat_diff_old/%s' % lib)
  90. new_version = glob.glob('bloat_diff_new/%s' % lib)
  91. for filename in [old_version, new_version]:
  92. if filename:
  93. subprocess.check_call('strip %s -o %s.stripped' %
  94. (filename[0], filename[0]),
  95. shell=True)
  96. assert len(new_version) == 1
  97. cmd = 'bloaty-build/bloaty -d compileunits,symbols'
  98. if old_version:
  99. assert len(old_version) == 1
  100. text += subprocess.check_output(
  101. '%s -n 0 --debug-file=%s --debug-file=%s %s.stripped -- %s.stripped'
  102. % (cmd, new_version[0], old_version[0], new_version[0],
  103. old_version[0]),
  104. shell=True).decode()
  105. sections = [
  106. x for x in csv.reader(
  107. subprocess.check_output(
  108. 'bloaty-build/bloaty -n 0 --csv %s -- %s' %
  109. (new_version[0], old_version[0]),
  110. shell=True).decode().splitlines())
  111. ]
  112. print(sections)
  113. for section in sections[1:]:
  114. # skip debug sections for bloat severity calculation
  115. if section[0].startswith(".debug"):
  116. continue
  117. # skip dynamic loader sections too
  118. if section[0].startswith(".dyn"):
  119. continue
  120. diff_size += int(section[2])
  121. else:
  122. text += subprocess.check_output('%s %s.stripped -n 0 --debug-file=%s' %
  123. (cmd, new_version[0], new_version[0]),
  124. shell=True).decode()
  125. text += '\n\n'
  126. severity = _rank_diff_bytes(diff_size)
  127. print("SEVERITY: %d" % severity)
  128. print(text)
  129. check_on_pr.check_on_pr('Bloat Difference', '```\n%s\n```' % text)
  130. check_on_pr.label_significance_on_pr('bloat', severity)