binary_sizes.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. #!/usr/bin/env vpython3
  2. #
  3. # Copyright 2020 The Chromium Authors. All rights reserved.
  4. # Use of this source code is governed by a BSD-style license that can be
  5. # found in the LICENSE file.
  6. '''Implements Chrome-Fuchsia package binary size checks.'''
  7. import argparse
  8. import collections
  9. import copy
  10. import json
  11. import logging
  12. import math
  13. import os
  14. import re
  15. import shutil
  16. import subprocess
  17. import sys
  18. import tempfile
  19. import time
  20. import traceback
  21. import uuid
  22. from common import GetHostToolPathFromPlatform, GetHostArchFromPlatform
  23. from common import SDK_ROOT, DIR_SOURCE_ROOT
  24. PACKAGES_BLOBS_FILE = 'package_blobs.json'
  25. PACKAGES_SIZES_FILE = 'package_sizes.json'
  26. # Structure representing the compressed and uncompressed sizes for a Fuchsia
  27. # package.
  28. PackageSizes = collections.namedtuple('PackageSizes',
  29. ['compressed', 'uncompressed'])
  30. # Structure representing a Fuchsia package blob and its compressed and
  31. # uncompressed sizes.
  32. Blob = collections.namedtuple(
  33. 'Blob', ['name', 'hash', 'compressed', 'uncompressed', 'is_counted'])
  34. def CreateSizesExternalDiagnostic(sizes_guid):
  35. """Creates a histogram external sizes diagnostic."""
  36. benchmark_diagnostic = {
  37. 'type': 'GenericSet',
  38. 'guid': str(sizes_guid),
  39. 'values': ['sizes'],
  40. }
  41. return benchmark_diagnostic
  42. def CreateSizesHistogramItem(name, size, sizes_guid):
  43. """Create a performance dashboard histogram from the histogram template and
  44. binary size data."""
  45. # Chromium performance dashboard histogram containing binary size data.
  46. histogram = {
  47. 'name': name,
  48. 'unit': 'sizeInBytes_smallerIsBetter',
  49. 'diagnostics': {
  50. 'benchmarks': str(sizes_guid),
  51. },
  52. 'sampleValues': [size],
  53. 'running': [1, size, math.log(size), size, size, size, 0],
  54. 'description': 'chrome-fuchsia package binary sizes',
  55. 'summaryOptions': {
  56. 'avg': True,
  57. 'count': False,
  58. 'max': False,
  59. 'min': False,
  60. 'std': False,
  61. 'sum': False,
  62. },
  63. }
  64. return histogram
  65. def CreateSizesHistogram(package_sizes):
  66. """Create a performance dashboard histogram from binary size data."""
  67. sizes_guid = uuid.uuid1()
  68. histogram = [CreateSizesExternalDiagnostic(sizes_guid)]
  69. for name, size in package_sizes.items():
  70. histogram.append(
  71. CreateSizesHistogramItem('%s_%s' % (name, 'compressed'),
  72. size.compressed, sizes_guid))
  73. histogram.append(
  74. CreateSizesHistogramItem('%s_%s' % (name, 'uncompressed'),
  75. size.uncompressed, sizes_guid))
  76. return histogram
  77. def CreateTestResults(test_status, timestamp):
  78. """Create test results data to write to JSON test results file.
  79. The JSON data format is defined in
  80. https://chromium.googlesource.com/chromium/src/+/main/docs/testing/json_test_results_format.md
  81. """
  82. results = {
  83. 'tests': {},
  84. 'interrupted': False,
  85. 'path_delimiter': '.',
  86. 'version': 3,
  87. 'seconds_since_epoch': timestamp,
  88. }
  89. num_failures_by_type = {result: 0 for result in ['FAIL', 'PASS', 'CRASH']}
  90. for metric in test_status:
  91. actual_status = test_status[metric]
  92. num_failures_by_type[actual_status] += 1
  93. results['tests'][metric] = {
  94. 'expected': 'PASS',
  95. 'actual': actual_status,
  96. }
  97. results['num_failures_by_type'] = num_failures_by_type
  98. return results
  99. def GetTestStatus(package_sizes, sizes_config, test_completed):
  100. """Checks package sizes against size limits.
  101. Returns a tuple of overall test pass/fail status and a dictionary mapping size
  102. limit checks to PASS/FAIL/CRASH status."""
  103. if not test_completed:
  104. test_status = {'binary_sizes': 'CRASH'}
  105. else:
  106. test_status = {}
  107. for metric, limit in sizes_config['size_limits'].items():
  108. # Strip the "_compressed" suffix from |metric| if it exists.
  109. match = re.match(r'(?P<name>\w+)_compressed', metric)
  110. package_name = match.group('name') if match else metric
  111. if package_name not in package_sizes:
  112. raise Exception('package "%s" not in sizes "%s"' %
  113. (package_name, str(package_sizes)))
  114. if package_sizes[package_name].compressed <= limit:
  115. test_status[metric] = 'PASS'
  116. else:
  117. test_status[metric] = 'FAIL'
  118. all_tests_passed = all(status == 'PASS' for status in test_status.values())
  119. return all_tests_passed, test_status
  120. def WriteSimpleTestResults(results_path, test_completed):
  121. """Writes simplified test results file.
  122. Used when test status is not available.
  123. """
  124. simple_isolated_script_output = {
  125. 'valid': test_completed,
  126. 'failures': [],
  127. 'version': 'simplified',
  128. }
  129. with open(results_path, 'w') as output_file:
  130. json.dump(simple_isolated_script_output, output_file)
  131. def WriteTestResults(results_path, test_completed, test_status, timestamp):
  132. """Writes test results file containing test PASS/FAIL/CRASH statuses."""
  133. if test_status:
  134. test_results = CreateTestResults(test_status, timestamp)
  135. with open(results_path, 'w') as results_file:
  136. json.dump(test_results, results_file)
  137. else:
  138. WriteSimpleTestResults(results_path, test_completed)
  139. def WriteGerritPluginSizeData(output_path, package_sizes):
  140. """Writes a package size dictionary in json format for the Gerrit binary
  141. sizes plugin."""
  142. with open(output_path, 'w') as sizes_file:
  143. sizes_data = {name: size.compressed for name, size in package_sizes.items()}
  144. json.dump(sizes_data, sizes_file)
  145. def ReadPackageBlobsJson(json_path):
  146. """Reads package blob info from json file.
  147. Opens json file of blob info written by WritePackageBlobsJson,
  148. and converts back into package blobs used in this script.
  149. """
  150. with open(json_path, 'rt') as json_file:
  151. formatted_blob_info = json.load(json_file)
  152. package_blobs = {}
  153. for package in formatted_blob_info:
  154. package_blobs[package] = {}
  155. for blob_info in formatted_blob_info[package]:
  156. blob = Blob(name=blob_info['path'],
  157. hash=blob_info['merkle'],
  158. uncompressed=blob_info['bytes'],
  159. compressed=blob_info['size'],
  160. is_counted=blob_info['is_counted'])
  161. package_blobs[package][blob.name] = blob
  162. return package_blobs
  163. def WritePackageBlobsJson(json_path, package_blobs):
  164. """Writes package blob information in human-readable JSON format.
  165. The json data is an array of objects containing these keys:
  166. 'path': string giving blob location in the local file system
  167. 'merkle': the blob's Merkle hash
  168. 'bytes': the number of uncompressed bytes in the blod
  169. 'size': the size of the compressed blob in bytes. A multiple of the blobfs
  170. block size (8192)
  171. 'is_counted: true if the blob counts towards the package budget, or false
  172. if not (for ICU blobs or blobs distributed in the SDK)"""
  173. formatted_blob_stats_per_package = {}
  174. for package in package_blobs:
  175. blob_data = []
  176. for blob_name in package_blobs[package]:
  177. blob = package_blobs[package][blob_name]
  178. blob_data.append({
  179. 'path': str(blob.name),
  180. 'merkle': str(blob.hash),
  181. 'bytes': blob.uncompressed,
  182. 'size': blob.compressed,
  183. 'is_counted': blob.is_counted
  184. })
  185. formatted_blob_stats_per_package[package] = blob_data
  186. with (open(json_path, 'w')) as json_file:
  187. json.dump(formatted_blob_stats_per_package, json_file, indent=2)
  188. def WritePackageSizesJson(json_path, package_sizes):
  189. """Writes package sizes into a human-readable JSON format.
  190. JSON data is a dictionary of each package name being a key, with
  191. the following keys within the sub-object:
  192. 'compressed': compressed size of the package in bytes.
  193. 'uncompressed': uncompressed size of the package in bytes.
  194. """
  195. formatted_package_sizes = {}
  196. for package, size_info in package_sizes.items():
  197. formatted_package_sizes[package] = {
  198. 'uncompressed': size_info.uncompressed,
  199. 'compressed': size_info.compressed
  200. }
  201. with (open(json_path, 'w')) as json_file:
  202. json.dump(formatted_package_sizes, json_file, indent=2)
  203. def ReadPackageSizesJson(json_path):
  204. """Reads package_sizes from a given JSON file.
  205. Opens json file of blob info written by WritePackageSizesJson,
  206. and converts back into package sizes used in this script.
  207. """
  208. with open(json_path, 'rt') as json_file:
  209. formatted_package_info = json.load(json_file)
  210. package_sizes = {}
  211. for package, size_info in formatted_package_info.items():
  212. package_sizes[package] = PackageSizes(
  213. compressed=size_info['compressed'],
  214. uncompressed=size_info['uncompressed'])
  215. return package_sizes
  216. def GetCompressedSize(file_path):
  217. """Measures file size after blobfs compression."""
  218. compressor_path = GetHostToolPathFromPlatform('blobfs-compression')
  219. try:
  220. temp_dir = tempfile.mkdtemp()
  221. compressed_file_path = os.path.join(temp_dir, os.path.basename(file_path))
  222. compressor_cmd = [
  223. compressor_path,
  224. '--source_file=%s' % file_path,
  225. '--compressed_file=%s' % compressed_file_path
  226. ]
  227. proc = subprocess.Popen(compressor_cmd,
  228. stdout=subprocess.PIPE,
  229. stderr=subprocess.STDOUT)
  230. proc.wait()
  231. compressor_output = proc.stdout.read().decode('utf-8')
  232. if proc.returncode != 0:
  233. print(compressor_output, file=sys.stderr)
  234. raise Exception('Error while running %s' % compressor_path)
  235. finally:
  236. shutil.rmtree(temp_dir)
  237. # Match a compressed bytes total from blobfs-compression output like
  238. # Wrote 360830 bytes (40% compression)
  239. blobfs_compressed_bytes_re = r'Wrote\s+(?P<bytes>\d+)\s+bytes'
  240. match = re.search(blobfs_compressed_bytes_re, compressor_output)
  241. if not match:
  242. print(compressor_output, file=sys.stderr)
  243. raise Exception('Could not get compressed bytes for %s' % file_path)
  244. # Round the compressed file size up to an integer number of blobfs blocks.
  245. BLOBFS_BLOCK_SIZE = 8192 # Fuchsia's blobfs file system uses 8KiB blocks.
  246. blob_bytes = int(match.group('bytes'))
  247. return int(math.ceil(blob_bytes / BLOBFS_BLOCK_SIZE)) * BLOBFS_BLOCK_SIZE
  248. def ExtractFarFile(file_path, extract_dir):
  249. """Extracts contents of a Fuchsia archive file to the specified directory."""
  250. far_tool = GetHostToolPathFromPlatform('far')
  251. if not os.path.isfile(far_tool):
  252. raise Exception('Could not find FAR host tool "%s".' % far_tool)
  253. if not os.path.isfile(file_path):
  254. raise Exception('Could not find FAR file "%s".' % file_path)
  255. subprocess.check_call([
  256. far_tool, 'extract',
  257. '--archive=%s' % file_path,
  258. '--output=%s' % extract_dir
  259. ])
  260. def GetBlobNameHashes(meta_dir):
  261. """Returns mapping from Fuchsia pkgfs paths to blob hashes. The mapping is
  262. read from the extracted meta.far archive contained in an extracted package
  263. archive."""
  264. blob_name_hashes = {}
  265. contents_path = os.path.join(meta_dir, 'meta', 'contents')
  266. with open(contents_path) as lines:
  267. for line in lines:
  268. (pkgfs_path, blob_hash) = line.strip().split('=')
  269. blob_name_hashes[pkgfs_path] = blob_hash
  270. return blob_name_hashes
  271. # Compiled regular expression matching strings like *.so, *.so.1, *.so.2, ...
  272. SO_FILENAME_REGEXP = re.compile(r'\.so(\.\d+)?$')
  273. def GetSdkModules():
  274. """Finds shared objects (.so) under the Fuchsia SDK arch directory in dist or
  275. lib subdirectories.
  276. Returns a set of shared objects' filenames.
  277. """
  278. # Fuchsia SDK arch directory path (contains all shared object files).
  279. sdk_arch_dir = os.path.join(SDK_ROOT, 'arch')
  280. # Leaf subdirectories containing shared object files.
  281. sdk_so_leaf_dirs = ['dist', 'lib']
  282. # Match a shared object file name.
  283. sdk_so_filename_re = r'\.so(\.\d+)?$'
  284. lib_names = set()
  285. for dirpath, _, file_names in os.walk(sdk_arch_dir):
  286. if os.path.basename(dirpath) in sdk_so_leaf_dirs:
  287. for name in file_names:
  288. if SO_FILENAME_REGEXP.search(name):
  289. lib_names.add(name)
  290. return lib_names
  291. def FarBaseName(name):
  292. _, name = os.path.split(name)
  293. name = re.sub(r'\.far$', '', name)
  294. return name
  295. def GetPackageMerkleRoot(far_file_path):
  296. """Returns a package's Merkle digest."""
  297. # The digest is the first word on the first line of the merkle tool's output.
  298. merkle_tool = GetHostToolPathFromPlatform('merkleroot')
  299. output = subprocess.check_output([merkle_tool, far_file_path])
  300. return output.splitlines()[0].split()[0]
  301. def GetBlobs(far_file, build_out_dir):
  302. """Calculates compressed and uncompressed blob sizes for specified FAR file.
  303. Marks ICU blobs and blobs from SDK libraries as not counted."""
  304. base_name = FarBaseName(far_file)
  305. extract_dir = tempfile.mkdtemp()
  306. # Extract files and blobs from the specified Fuchsia archive.
  307. far_file_path = os.path.join(build_out_dir, far_file)
  308. far_extract_dir = os.path.join(extract_dir, base_name)
  309. ExtractFarFile(far_file_path, far_extract_dir)
  310. # Extract the meta.far archive contained in the specified Fuchsia archive.
  311. meta_far_file_path = os.path.join(far_extract_dir, 'meta.far')
  312. meta_far_extract_dir = os.path.join(extract_dir, '%s_meta' % base_name)
  313. ExtractFarFile(meta_far_file_path, meta_far_extract_dir)
  314. # Map Linux filesystem blob names to blob hashes.
  315. blob_name_hashes = GetBlobNameHashes(meta_far_extract_dir)
  316. # "System" files whose sizes are not charged against component size budgets.
  317. # Fuchsia SDK modules and the ICU icudtl.dat file sizes are not counted.
  318. system_files = GetSdkModules() | set(['icudtl.dat'])
  319. # Add the meta.far file blob.
  320. blobs = {}
  321. meta_name = 'meta.far'
  322. meta_hash = GetPackageMerkleRoot(meta_far_file_path)
  323. compressed = GetCompressedSize(meta_far_file_path)
  324. uncompressed = os.path.getsize(meta_far_file_path)
  325. blobs[meta_name] = Blob(meta_name, meta_hash, compressed, uncompressed, True)
  326. # Add package blobs.
  327. for blob_name, blob_hash in blob_name_hashes.items():
  328. extracted_blob_path = os.path.join(far_extract_dir, blob_hash)
  329. compressed = GetCompressedSize(extracted_blob_path)
  330. uncompressed = os.path.getsize(extracted_blob_path)
  331. is_counted = os.path.basename(blob_name) not in system_files
  332. blobs[blob_name] = Blob(blob_name, blob_hash, compressed, uncompressed,
  333. is_counted)
  334. shutil.rmtree(extract_dir)
  335. return blobs
  336. def GetPackageBlobs(far_files, build_out_dir):
  337. """Returns dictionary mapping package names to blobs contained in the package.
  338. Prints package blob size statistics."""
  339. package_blobs = {}
  340. for far_file in far_files:
  341. package_name = FarBaseName(far_file)
  342. if package_name in package_blobs:
  343. raise Exception('Duplicate FAR file base name "%s".' % package_name)
  344. package_blobs[package_name] = GetBlobs(far_file, build_out_dir)
  345. # Print package blob sizes (does not count sharing).
  346. for package_name in sorted(package_blobs.keys()):
  347. print('Package blob sizes: %s' % package_name)
  348. print('%-64s %12s %12s %s' %
  349. ('blob hash', 'compressed', 'uncompressed', 'path'))
  350. print('%s %s %s %s' % (64 * '-', 12 * '-', 12 * '-', 20 * '-'))
  351. for blob_name in sorted(package_blobs[package_name].keys()):
  352. blob = package_blobs[package_name][blob_name]
  353. if blob.is_counted:
  354. print('%64s %12d %12d %s' %
  355. (blob.hash, blob.compressed, blob.uncompressed, blob.name))
  356. return package_blobs
  357. def GetPackageSizes(package_blobs):
  358. """Calculates compressed and uncompressed package sizes from blob sizes."""
  359. # TODO(crbug.com/1126177): Use partial sizes for blobs shared by
  360. # non Chrome-Fuchsia packages.
  361. # Count number of packages sharing blobs (a count of 1 is not shared).
  362. blob_counts = collections.defaultdict(int)
  363. for package_name in package_blobs:
  364. for blob_name in package_blobs[package_name]:
  365. blob = package_blobs[package_name][blob_name]
  366. blob_counts[blob.hash] += 1
  367. # Package sizes are the sum of blob sizes divided by their share counts.
  368. package_sizes = {}
  369. for package_name in package_blobs:
  370. compressed_total = 0
  371. uncompressed_total = 0
  372. for blob_name in package_blobs[package_name]:
  373. blob = package_blobs[package_name][blob_name]
  374. if blob.is_counted:
  375. count = blob_counts[blob.hash]
  376. compressed_total += blob.compressed // count
  377. uncompressed_total += blob.uncompressed // count
  378. package_sizes[package_name] = PackageSizes(compressed_total,
  379. uncompressed_total)
  380. return package_sizes
  381. def GetBinarySizesAndBlobs(args, sizes_config):
  382. """Get binary size data and contained blobs for packages specified in args.
  383. If "total_size_name" is set, then computes a synthetic package size which is
  384. the aggregated sizes across all packages."""
  385. # Calculate compressed and uncompressed package sizes.
  386. package_blobs = GetPackageBlobs(sizes_config['far_files'], args.build_out_dir)
  387. package_sizes = GetPackageSizes(package_blobs)
  388. # Optionally calculate total compressed and uncompressed package sizes.
  389. if 'far_total_name' in sizes_config:
  390. compressed = sum([a.compressed for a in package_sizes.values()])
  391. uncompressed = sum([a.uncompressed for a in package_sizes.values()])
  392. package_sizes[sizes_config['far_total_name']] = PackageSizes(
  393. compressed, uncompressed)
  394. for name, size in package_sizes.items():
  395. print('%s: compressed size %d, uncompressed size %d' %
  396. (name, size.compressed, size.uncompressed))
  397. return package_sizes, package_blobs
  398. def main():
  399. parser = argparse.ArgumentParser()
  400. parser.add_argument(
  401. '--build-out-dir',
  402. '--output-directory',
  403. type=os.path.realpath,
  404. required=True,
  405. help='Location of the build artifacts.',
  406. )
  407. parser.add_argument(
  408. '--isolated-script-test-output',
  409. type=os.path.realpath,
  410. help='File to which simplified JSON results will be written.')
  411. parser.add_argument(
  412. '--size-plugin-json-path',
  413. help='Optional path for json size data for the Gerrit binary size plugin',
  414. )
  415. parser.add_argument(
  416. '--sizes-path',
  417. default=os.path.join('tools', 'fuchsia', 'size_tests', 'fyi_sizes.json'),
  418. help='path to package size limits json file. The path is relative to '
  419. 'the workspace src directory')
  420. parser.add_argument('--verbose',
  421. '-v',
  422. action='store_true',
  423. help='Enable verbose output')
  424. # Accepted to conform to the isolated script interface, but ignored.
  425. parser.add_argument('--isolated-script-test-filter', help=argparse.SUPPRESS)
  426. parser.add_argument('--isolated-script-test-perf-output',
  427. help=argparse.SUPPRESS)
  428. args = parser.parse_args()
  429. if args.verbose:
  430. print('Fuchsia binary sizes')
  431. print('Working directory', os.getcwd())
  432. print('Args:')
  433. for var in vars(args):
  434. print(' {}: {}'.format(var, getattr(args, var) or ''))
  435. if not os.path.isdir(args.build_out_dir):
  436. raise Exception('Could not find build output directory "%s".' %
  437. args.build_out_dir)
  438. with open(os.path.join(DIR_SOURCE_ROOT, args.sizes_path)) as sizes_file:
  439. sizes_config = json.load(sizes_file)
  440. if args.verbose:
  441. print('Sizes Config:')
  442. print(json.dumps(sizes_config))
  443. for far_rel_path in sizes_config['far_files']:
  444. far_abs_path = os.path.join(args.build_out_dir, far_rel_path)
  445. if not os.path.isfile(far_abs_path):
  446. raise Exception('Could not find FAR file "%s".' % far_abs_path)
  447. test_name = 'sizes'
  448. timestamp = time.time()
  449. test_completed = False
  450. all_tests_passed = False
  451. test_status = {}
  452. package_sizes = {}
  453. package_blobs = {}
  454. sizes_histogram = []
  455. results_directory = None
  456. if args.isolated_script_test_output:
  457. results_directory = os.path.join(
  458. os.path.dirname(args.isolated_script_test_output), test_name)
  459. if not os.path.exists(results_directory):
  460. os.makedirs(results_directory)
  461. try:
  462. package_sizes, package_blobs = GetBinarySizesAndBlobs(args, sizes_config)
  463. sizes_histogram = CreateSizesHistogram(package_sizes)
  464. test_completed = True
  465. except:
  466. _, value, trace = sys.exc_info()
  467. traceback.print_tb(trace)
  468. print(str(value))
  469. finally:
  470. all_tests_passed, test_status = GetTestStatus(package_sizes, sizes_config,
  471. test_completed)
  472. if results_directory:
  473. WriteTestResults(os.path.join(results_directory, 'test_results.json'),
  474. test_completed, test_status, timestamp)
  475. with open(os.path.join(results_directory, 'perf_results.json'), 'w') as f:
  476. json.dump(sizes_histogram, f)
  477. WritePackageBlobsJson(
  478. os.path.join(results_directory, PACKAGES_BLOBS_FILE), package_blobs)
  479. WritePackageSizesJson(
  480. os.path.join(results_directory, PACKAGES_SIZES_FILE), package_sizes)
  481. if args.isolated_script_test_output:
  482. WriteTestResults(args.isolated_script_test_output, test_completed,
  483. test_status, timestamp)
  484. if args.size_plugin_json_path:
  485. WriteGerritPluginSizeData(args.size_plugin_json_path, package_sizes)
  486. return 0 if all_tests_passed else 1
  487. if __name__ == '__main__':
  488. sys.exit(main())