blob: ce752e493161c8dc5e6952069f02d84a2075fc09 [file] [log] [blame]
Xiaofei Baibca03e52021-09-09 09:42:37 +00001#!/usr/bin/env python3
2
3"""
Xiaofei Baibca03e52021-09-09 09:42:37 +00004This script is for comparing the size of the library files from two
5different Git revisions within an Mbed TLS repository.
6The results of the comparison is formatted as csv and stored at a
7configurable location.
8Note: must be run from Mbed TLS root.
9"""
10
11# Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +000012# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Xiaofei Baibca03e52021-09-09 09:42:37 +000013
14import argparse
Yanray Wang21127f72023-07-19 12:09:45 +080015import logging
Xiaofei Baibca03e52021-09-09 09:42:37 +000016import os
Yanray Wang16ebc572023-05-30 18:10:20 +080017import re
Yanray Wang5605c6f2023-07-21 16:09:00 +080018import shutil
Xiaofei Baibca03e52021-09-09 09:42:37 +000019import subprocess
20import sys
Yanray Wang16ebc572023-05-30 18:10:20 +080021import typing
Yanray Wang23bd5322023-05-24 11:03:59 +080022from enum import Enum
Xiaofei Baibca03e52021-09-09 09:42:37 +000023
David Horstmannecd6d012024-05-10 16:58:31 +010024import framework_scripts_path # pylint: disable=unused-import
David Horstmanncd84bb22024-05-03 14:36:12 +010025from mbedtls_framework import build_tree
26from mbedtls_framework import logging_util
27from mbedtls_framework import typing_util
Gilles Peskined9071e72022-09-18 21:17:09 +020028
Yanray Wang23bd5322023-05-24 11:03:59 +080029class SupportedArch(Enum):
30 """Supported architecture for code size measurement."""
31 AARCH64 = 'aarch64'
32 AARCH32 = 'aarch32'
Yanray Wangaba71582023-05-29 16:45:56 +080033 ARMV8_M = 'armv8-m'
Yanray Wang23bd5322023-05-24 11:03:59 +080034 X86_64 = 'x86_64'
35 X86 = 'x86'
36
Yanray Wang955671b2023-07-21 12:08:27 +080037
Yanray Wang6a862582023-05-24 12:24:38 +080038class SupportedConfig(Enum):
39 """Supported configuration for code size measurement."""
40 DEFAULT = 'default'
41 TFM_MEDIUM = 'tfm-medium'
42
Yanray Wang955671b2023-07-21 12:08:27 +080043
Yanray Wang16ebc572023-05-30 18:10:20 +080044# Static library
45MBEDTLS_STATIC_LIB = {
46 'CRYPTO': 'library/libmbedcrypto.a',
47 'X509': 'library/libmbedx509.a',
48 'TLS': 'library/libmbedtls.a',
49}
50
Yanray Wang955671b2023-07-21 12:08:27 +080051class CodeSizeDistinctInfo: # pylint: disable=too-few-public-methods
52 """Data structure to store possibly distinct information for code size
53 comparison."""
54 def __init__( #pylint: disable=too-many-arguments
55 self,
56 version: str,
57 git_rev: str,
58 arch: str,
59 config: str,
Yanray Wang5605c6f2023-07-21 16:09:00 +080060 compiler: str,
61 opt_level: str,
Yanray Wang955671b2023-07-21 12:08:27 +080062 ) -> None:
63 """
64 :param: version: which version to compare with for code size.
65 :param: git_rev: Git revision to calculate code size.
66 :param: arch: architecture to measure code size on.
67 :param: config: Configuration type to calculate code size.
68 (See SupportedConfig)
Yanray Wang5605c6f2023-07-21 16:09:00 +080069 :param: compiler: compiler used to build library/*.o.
70 :param: opt_level: Options that control optimization. (E.g. -Os)
Yanray Wang955671b2023-07-21 12:08:27 +080071 """
72 self.version = version
73 self.git_rev = git_rev
74 self.arch = arch
75 self.config = config
Yanray Wang5605c6f2023-07-21 16:09:00 +080076 self.compiler = compiler
77 self.opt_level = opt_level
78 # Note: Variables below are not initialized by class instantiation.
79 self.pre_make_cmd = [] #type: typing.List[str]
80 self.make_cmd = ''
Yanray Wang955671b2023-07-21 12:08:27 +080081
Yanray Wanga6cf6922023-07-24 15:20:42 +080082 def get_info_indication(self):
83 """Return a unique string to indicate Code Size Distinct Information."""
Yanray Wang6ef50492023-07-26 14:59:37 +080084 return '{git_rev}-{arch}-{config}-{compiler}'.format(**self.__dict__)
Yanray Wanga6cf6922023-07-24 15:20:42 +080085
Yanray Wang955671b2023-07-21 12:08:27 +080086
87class CodeSizeCommonInfo: # pylint: disable=too-few-public-methods
88 """Data structure to store common information for code size comparison."""
89 def __init__(
90 self,
91 host_arch: str,
92 measure_cmd: str,
93 ) -> None:
94 """
95 :param host_arch: host architecture.
96 :param measure_cmd: command to measure code size for library/*.o.
97 """
98 self.host_arch = host_arch
99 self.measure_cmd = measure_cmd
100
Yanray Wanga6cf6922023-07-24 15:20:42 +0800101 def get_info_indication(self):
102 """Return a unique string to indicate Code Size Common Information."""
Yanray Wange4a36362023-07-25 10:37:11 +0800103 return '{measure_tool}'\
104 .format(measure_tool=self.measure_cmd.strip().split(' ')[0])
Yanray Wang955671b2023-07-21 12:08:27 +0800105
106class CodeSizeResultInfo: # pylint: disable=too-few-public-methods
107 """Data structure to store result options for code size comparison."""
Yanray Wangee07afa2023-07-28 16:34:05 +0800108 def __init__( #pylint: disable=too-many-arguments
Yanray Wang955671b2023-07-21 12:08:27 +0800109 self,
110 record_dir: str,
111 comp_dir: str,
112 with_markdown=False,
113 stdout=False,
Yanray Wangee07afa2023-07-28 16:34:05 +0800114 show_all=False,
Yanray Wang955671b2023-07-21 12:08:27 +0800115 ) -> None:
116 """
117 :param record_dir: directory to store code size record.
118 :param comp_dir: directory to store results of code size comparision.
119 :param with_markdown: write comparision result into a markdown table.
120 (Default: False)
121 :param stdout: direct comparison result into sys.stdout.
122 (Default False)
Yanray Wangee07afa2023-07-28 16:34:05 +0800123 :param show_all: show all objects in comparison result. (Default False)
Yanray Wang955671b2023-07-21 12:08:27 +0800124 """
125 self.record_dir = record_dir
126 self.comp_dir = comp_dir
127 self.with_markdown = with_markdown
128 self.stdout = stdout
Yanray Wangee07afa2023-07-28 16:34:05 +0800129 self.show_all = show_all
Yanray Wang955671b2023-07-21 12:08:27 +0800130
131
Yanray Wang23bd5322023-05-24 11:03:59 +0800132DETECT_ARCH_CMD = "cc -dM -E - < /dev/null"
133def detect_arch() -> str:
134 """Auto-detect host architecture."""
135 cc_output = subprocess.check_output(DETECT_ARCH_CMD, shell=True).decode()
Yanray Wang386c2f92023-07-20 15:32:15 +0800136 if '__aarch64__' in cc_output:
Yanray Wang23bd5322023-05-24 11:03:59 +0800137 return SupportedArch.AARCH64.value
Yanray Wang386c2f92023-07-20 15:32:15 +0800138 if '__arm__' in cc_output:
Yanray Wang23bd5322023-05-24 11:03:59 +0800139 return SupportedArch.AARCH32.value
Yanray Wang386c2f92023-07-20 15:32:15 +0800140 if '__x86_64__' in cc_output:
Yanray Wang23bd5322023-05-24 11:03:59 +0800141 return SupportedArch.X86_64.value
Yanray Wangca9a3cb2023-07-26 17:16:29 +0800142 if '__i386__' in cc_output:
Yanray Wang23bd5322023-05-24 11:03:59 +0800143 return SupportedArch.X86.value
144 else:
145 print("Unknown host architecture, cannot auto-detect arch.")
146 sys.exit(1)
Gilles Peskined9071e72022-09-18 21:17:09 +0200147
Yanray Wang28648232023-09-06 11:50:45 +0800148TFM_MEDIUM_CONFIG_H = 'configs/ext/tfm_mbedcrypto_config_profile_medium.h'
149TFM_MEDIUM_CRYPTO_CONFIG_H = 'configs/ext/crypto_config_profile_medium.h'
Yanray Wang5605c6f2023-07-21 16:09:00 +0800150
151CONFIG_H = 'include/mbedtls/mbedtls_config.h'
Ronald Cron7e5d61c2024-06-10 14:25:46 +0200152CRYPTO_CONFIG_H = 'tf-psa-crypto/include/psa/crypto_config.h'
Yanray Wang5605c6f2023-07-21 16:09:00 +0800153BACKUP_SUFFIX = '.code_size.bak'
154
Yanray Wang923f9432023-07-17 12:43:00 +0800155class CodeSizeBuildInfo: # pylint: disable=too-few-public-methods
Yanray Wang6a862582023-05-24 12:24:38 +0800156 """Gather information used to measure code size.
157
158 It collects information about architecture, configuration in order to
159 infer build command for code size measurement.
160 """
161
Yanray Wangc18cd892023-05-31 11:08:04 +0800162 SupportedArchConfig = [
Yanray Wang386c2f92023-07-20 15:32:15 +0800163 '-a ' + SupportedArch.AARCH64.value + ' -c ' + SupportedConfig.DEFAULT.value,
164 '-a ' + SupportedArch.AARCH32.value + ' -c ' + SupportedConfig.DEFAULT.value,
165 '-a ' + SupportedArch.X86_64.value + ' -c ' + SupportedConfig.DEFAULT.value,
166 '-a ' + SupportedArch.X86.value + ' -c ' + SupportedConfig.DEFAULT.value,
167 '-a ' + SupportedArch.ARMV8_M.value + ' -c ' + SupportedConfig.TFM_MEDIUM.value,
Yanray Wangc18cd892023-05-31 11:08:04 +0800168 ]
169
Yanray Wang802af162023-07-17 14:04:30 +0800170 def __init__(
171 self,
Yanray Wang955671b2023-07-21 12:08:27 +0800172 size_dist_info: CodeSizeDistinctInfo,
Yanray Wang21127f72023-07-19 12:09:45 +0800173 host_arch: str,
174 logger: logging.Logger,
Yanray Wang802af162023-07-17 14:04:30 +0800175 ) -> None:
Yanray Wang6a862582023-05-24 12:24:38 +0800176 """
Yanray Wang955671b2023-07-21 12:08:27 +0800177 :param size_dist_info:
178 CodeSizeDistinctInfo containing info for code size measurement.
179 - size_dist_info.arch: architecture to measure code size on.
180 - size_dist_info.config: configuration type to measure
181 code size with.
Yanray Wang5605c6f2023-07-21 16:09:00 +0800182 - size_dist_info.compiler: compiler used to build library/*.o.
183 - size_dist_info.opt_level: Options that control optimization.
184 (E.g. -Os)
Yanray Wang5b64e4c2023-07-20 15:09:51 +0800185 :param host_arch: host architecture.
186 :param logger: logging module
Yanray Wang6a862582023-05-24 12:24:38 +0800187 """
Yanray Wang5605c6f2023-07-21 16:09:00 +0800188 self.arch = size_dist_info.arch
189 self.config = size_dist_info.config
190 self.compiler = size_dist_info.compiler
191 self.opt_level = size_dist_info.opt_level
192
193 self.make_cmd = ['make', '-j', 'lib']
194
Yanray Wang802af162023-07-17 14:04:30 +0800195 self.host_arch = host_arch
Yanray Wang21127f72023-07-19 12:09:45 +0800196 self.logger = logger
Yanray Wang6a862582023-05-24 12:24:38 +0800197
Yanray Wang5605c6f2023-07-21 16:09:00 +0800198 def check_correctness(self) -> bool:
199 """Check whether we are using proper / supported combination
200 of information to build library/*.o."""
Yanray Wang6a862582023-05-24 12:24:38 +0800201
Yanray Wang5605c6f2023-07-21 16:09:00 +0800202 # default config
203 if self.config == SupportedConfig.DEFAULT.value and \
204 self.arch == self.host_arch:
205 return True
206 # TF-M
207 elif self.arch == SupportedArch.ARMV8_M.value and \
208 self.config == SupportedConfig.TFM_MEDIUM.value:
209 return True
210
211 return False
212
213 def infer_pre_make_command(self) -> typing.List[str]:
214 """Infer command to set up proper configuration before running make."""
215 pre_make_cmd = [] #type: typing.List[str]
216 if self.config == SupportedConfig.TFM_MEDIUM.value:
Yanray Wanga279ca92023-07-26 15:01:10 +0800217 pre_make_cmd.append('cp {src} {dest}'
Yanray Wange4a36362023-07-25 10:37:11 +0800218 .format(src=TFM_MEDIUM_CONFIG_H, dest=CONFIG_H))
Yanray Wanga279ca92023-07-26 15:01:10 +0800219 pre_make_cmd.append('cp {src} {dest}'
Yanray Wange4a36362023-07-25 10:37:11 +0800220 .format(src=TFM_MEDIUM_CRYPTO_CONFIG_H,
221 dest=CRYPTO_CONFIG_H))
Yanray Wang5605c6f2023-07-21 16:09:00 +0800222
223 return pre_make_cmd
224
225 def infer_make_cflags(self) -> str:
226 """Infer CFLAGS by instance attributes in CodeSizeDistinctInfo."""
227 cflags = [] #type: typing.List[str]
228
229 # set optimization level
230 cflags.append(self.opt_level)
231 # set compiler by config
232 if self.config == SupportedConfig.TFM_MEDIUM.value:
233 self.compiler = 'armclang'
234 cflags.append('-mcpu=cortex-m33')
235 # set target
236 if self.compiler == 'armclang':
237 cflags.append('--target=arm-arm-none-eabi')
238
239 return ' '.join(cflags)
240
241 def infer_make_command(self) -> str:
242 """Infer make command by CFLAGS and CC."""
243
244 if self.check_correctness():
245 # set CFLAGS=
246 self.make_cmd.append('CFLAGS=\'{}\''.format(self.infer_make_cflags()))
247 # set CC=
248 self.make_cmd.append('CC={}'.format(self.compiler))
249 return ' '.join(self.make_cmd)
Yanray Wang6a862582023-05-24 12:24:38 +0800250 else:
Yanray Wang21127f72023-07-19 12:09:45 +0800251 self.logger.error("Unsupported combination of architecture: {} " \
252 "and configuration: {}.\n"
Yanray Wang5605c6f2023-07-21 16:09:00 +0800253 .format(self.arch,
254 self.config))
Yanray Wang2ba9df22023-07-26 10:11:31 +0800255 self.logger.error("Please use supported combination of " \
Yanray Wang21127f72023-07-19 12:09:45 +0800256 "architecture and configuration:")
Yanray Wang923f9432023-07-17 12:43:00 +0800257 for comb in CodeSizeBuildInfo.SupportedArchConfig:
Yanray Wang2ba9df22023-07-26 10:11:31 +0800258 self.logger.error(comb)
259 self.logger.error("")
260 self.logger.error("For your system, please use:")
Yanray Wang923f9432023-07-17 12:43:00 +0800261 for comb in CodeSizeBuildInfo.SupportedArchConfig:
Yanray Wang802af162023-07-17 14:04:30 +0800262 if "default" in comb and self.host_arch not in comb:
Yanray Wang21f17442023-06-01 11:29:06 +0800263 continue
Yanray Wang2ba9df22023-07-26 10:11:31 +0800264 self.logger.error(comb)
Yanray Wang6a862582023-05-24 12:24:38 +0800265 sys.exit(1)
266
267
Yanray Wange0e27602023-07-14 17:37:45 +0800268class CodeSizeCalculator:
Yanray Wang5b64e4c2023-07-20 15:09:51 +0800269 """ A calculator to calculate code size of library/*.o based on
Yanray Wange0e27602023-07-14 17:37:45 +0800270 Git revision and code size measurement tool.
271 """
272
Yanray Wang5605c6f2023-07-21 16:09:00 +0800273 def __init__( #pylint: disable=too-many-arguments
Yanray Wange0e27602023-07-14 17:37:45 +0800274 self,
Yanray Wang955671b2023-07-21 12:08:27 +0800275 git_rev: str,
Yanray Wang5605c6f2023-07-21 16:09:00 +0800276 pre_make_cmd: typing.List[str],
Yanray Wange0e27602023-07-14 17:37:45 +0800277 make_cmd: str,
Yanray Wang21127f72023-07-19 12:09:45 +0800278 measure_cmd: str,
279 logger: logging.Logger,
Yanray Wange0e27602023-07-14 17:37:45 +0800280 ) -> None:
281 """
Yanray Wang955671b2023-07-21 12:08:27 +0800282 :param git_rev: Git revision. (E.g: commit)
Yanray Wang5605c6f2023-07-21 16:09:00 +0800283 :param pre_make_cmd: command to set up proper config before running make.
Yanray Wang5b64e4c2023-07-20 15:09:51 +0800284 :param make_cmd: command to build library/*.o.
285 :param measure_cmd: command to measure code size for library/*.o.
286 :param logger: logging module
Yanray Wange0e27602023-07-14 17:37:45 +0800287 """
288 self.repo_path = "."
289 self.git_command = "git"
290 self.make_clean = 'make clean'
291
Yanray Wang955671b2023-07-21 12:08:27 +0800292 self.git_rev = git_rev
Yanray Wang5605c6f2023-07-21 16:09:00 +0800293 self.pre_make_cmd = pre_make_cmd
Yanray Wange0e27602023-07-14 17:37:45 +0800294 self.make_cmd = make_cmd
Yanray Wang802af162023-07-17 14:04:30 +0800295 self.measure_cmd = measure_cmd
Yanray Wang21127f72023-07-19 12:09:45 +0800296 self.logger = logger
Yanray Wange0e27602023-07-14 17:37:45 +0800297
298 @staticmethod
Yanray Wang955671b2023-07-21 12:08:27 +0800299 def validate_git_revision(git_rev: str) -> str:
Yanray Wange0e27602023-07-14 17:37:45 +0800300 result = subprocess.check_output(["git", "rev-parse", "--verify",
Yanray Wang955671b2023-07-21 12:08:27 +0800301 git_rev + "^{commit}"],
302 shell=False, universal_newlines=True)
Yanray Wang386c2f92023-07-20 15:32:15 +0800303 return result[:7]
Yanray Wange0e27602023-07-14 17:37:45 +0800304
Yanray Wang21127f72023-07-19 12:09:45 +0800305 def _create_git_worktree(self) -> str:
Yanray Wang955671b2023-07-21 12:08:27 +0800306 """Create a separate worktree for Git revision.
307 If Git revision is current, use current worktree instead."""
Yanray Wange0e27602023-07-14 17:37:45 +0800308
Yanray Wang5605c6f2023-07-21 16:09:00 +0800309 if self.git_rev == 'current':
Yanray Wang21127f72023-07-19 12:09:45 +0800310 self.logger.debug("Using current work directory.")
Yanray Wange0e27602023-07-14 17:37:45 +0800311 git_worktree_path = self.repo_path
312 else:
Yanray Wang21127f72023-07-19 12:09:45 +0800313 self.logger.debug("Creating git worktree for {}."
Yanray Wang955671b2023-07-21 12:08:27 +0800314 .format(self.git_rev))
Yanray Wang21127f72023-07-19 12:09:45 +0800315 git_worktree_path = os.path.join(self.repo_path,
Yanray Wang955671b2023-07-21 12:08:27 +0800316 "temp-" + self.git_rev)
Yanray Wange0e27602023-07-14 17:37:45 +0800317 subprocess.check_output(
318 [self.git_command, "worktree", "add", "--detach",
Yanray Wang955671b2023-07-21 12:08:27 +0800319 git_worktree_path, self.git_rev], cwd=self.repo_path,
Yanray Wange0e27602023-07-14 17:37:45 +0800320 stderr=subprocess.STDOUT
321 )
322
323 return git_worktree_path
324
Yanray Wang5605c6f2023-07-21 16:09:00 +0800325 @staticmethod
326 def backup_config_files(restore: bool) -> None:
327 """Backup / Restore config files."""
328 if restore:
329 shutil.move(CONFIG_H + BACKUP_SUFFIX, CONFIG_H)
330 shutil.move(CRYPTO_CONFIG_H + BACKUP_SUFFIX, CRYPTO_CONFIG_H)
331 else:
332 shutil.copy(CONFIG_H, CONFIG_H + BACKUP_SUFFIX)
333 shutil.copy(CRYPTO_CONFIG_H, CRYPTO_CONFIG_H + BACKUP_SUFFIX)
334
Yanray Wange0e27602023-07-14 17:37:45 +0800335 def _build_libraries(self, git_worktree_path: str) -> None:
Yanray Wang5b64e4c2023-07-20 15:09:51 +0800336 """Build library/*.o in the specified worktree."""
Yanray Wange0e27602023-07-14 17:37:45 +0800337
Yanray Wang5b64e4c2023-07-20 15:09:51 +0800338 self.logger.debug("Building library/*.o for {}."
Yanray Wang955671b2023-07-21 12:08:27 +0800339 .format(self.git_rev))
Yanray Wange0e27602023-07-14 17:37:45 +0800340 my_environment = os.environ.copy()
341 try:
Yanray Wang5605c6f2023-07-21 16:09:00 +0800342 if self.git_rev == 'current':
343 self.backup_config_files(restore=False)
344 for pre_cmd in self.pre_make_cmd:
345 subprocess.check_output(
346 pre_cmd, env=my_environment, shell=True,
347 cwd=git_worktree_path, stderr=subprocess.STDOUT,
348 universal_newlines=True
349 )
Yanray Wange0e27602023-07-14 17:37:45 +0800350 subprocess.check_output(
351 self.make_clean, env=my_environment, shell=True,
352 cwd=git_worktree_path, stderr=subprocess.STDOUT,
Yanray Wang386c2f92023-07-20 15:32:15 +0800353 universal_newlines=True
Yanray Wange0e27602023-07-14 17:37:45 +0800354 )
355 subprocess.check_output(
356 self.make_cmd, env=my_environment, shell=True,
357 cwd=git_worktree_path, stderr=subprocess.STDOUT,
Yanray Wang386c2f92023-07-20 15:32:15 +0800358 universal_newlines=True
Yanray Wange0e27602023-07-14 17:37:45 +0800359 )
Yanray Wang5605c6f2023-07-21 16:09:00 +0800360 if self.git_rev == 'current':
361 self.backup_config_files(restore=True)
Yanray Wange0e27602023-07-14 17:37:45 +0800362 except subprocess.CalledProcessError as e:
363 self._handle_called_process_error(e, git_worktree_path)
364
Yanray Wang386c2f92023-07-20 15:32:15 +0800365 def _gen_raw_code_size(self, git_worktree_path: str) -> typing.Dict[str, str]:
Yanray Wang5b64e4c2023-07-20 15:09:51 +0800366 """Measure code size by a tool and return in UTF-8 encoding."""
Yanray Wang21127f72023-07-19 12:09:45 +0800367
368 self.logger.debug("Measuring code size for {} by `{}`."
Yanray Wang955671b2023-07-21 12:08:27 +0800369 .format(self.git_rev,
Yanray Wang21127f72023-07-19 12:09:45 +0800370 self.measure_cmd.strip().split(' ')[0]))
Yanray Wange0e27602023-07-14 17:37:45 +0800371
372 res = {}
373 for mod, st_lib in MBEDTLS_STATIC_LIB.items():
374 try:
375 result = subprocess.check_output(
Yanray Wang802af162023-07-17 14:04:30 +0800376 [self.measure_cmd + ' ' + st_lib], cwd=git_worktree_path,
377 shell=True, universal_newlines=True
Yanray Wange0e27602023-07-14 17:37:45 +0800378 )
379 res[mod] = result
380 except subprocess.CalledProcessError as e:
381 self._handle_called_process_error(e, git_worktree_path)
382
383 return res
384
385 def _remove_worktree(self, git_worktree_path: str) -> None:
386 """Remove temporary worktree."""
387 if git_worktree_path != self.repo_path:
Yanray Wang21127f72023-07-19 12:09:45 +0800388 self.logger.debug("Removing temporary worktree {}."
389 .format(git_worktree_path))
Yanray Wange0e27602023-07-14 17:37:45 +0800390 subprocess.check_output(
391 [self.git_command, "worktree", "remove", "--force",
392 git_worktree_path], cwd=self.repo_path,
393 stderr=subprocess.STDOUT
394 )
395
396 def _handle_called_process_error(self, e: subprocess.CalledProcessError,
397 git_worktree_path: str) -> None:
398 """Handle a CalledProcessError and quit the program gracefully.
399 Remove any extra worktrees so that the script may be called again."""
400
401 # Tell the user what went wrong
Yanray Wang21127f72023-07-19 12:09:45 +0800402 self.logger.error(e, exc_info=True)
Yanray Wang386c2f92023-07-20 15:32:15 +0800403 self.logger.error("Process output:\n {}".format(e.output))
Yanray Wange0e27602023-07-14 17:37:45 +0800404
405 # Quit gracefully by removing the existing worktree
406 self._remove_worktree(git_worktree_path)
407 sys.exit(-1)
408
Yanray Wang386c2f92023-07-20 15:32:15 +0800409 def cal_libraries_code_size(self) -> typing.Dict[str, str]:
Yanray Wang5b64e4c2023-07-20 15:09:51 +0800410 """Do a complete round to calculate code size of library/*.o
411 by measurement tool.
412
413 :return A dictionary of measured code size
414 - typing.Dict[mod: str]
415 """
Yanray Wange0e27602023-07-14 17:37:45 +0800416
Yanray Wang21127f72023-07-19 12:09:45 +0800417 git_worktree_path = self._create_git_worktree()
Yanray Wang6ae94a02023-07-26 17:12:57 +0800418 try:
419 self._build_libraries(git_worktree_path)
420 res = self._gen_raw_code_size(git_worktree_path)
421 finally:
422 self._remove_worktree(git_worktree_path)
Yanray Wange0e27602023-07-14 17:37:45 +0800423
424 return res
425
426
Yanray Wang15c43f32023-07-17 11:17:12 +0800427class CodeSizeGenerator:
Yanray Wang5b64e4c2023-07-20 15:09:51 +0800428 """ A generator based on size measurement tool for library/*.o.
Yanray Wang15c43f32023-07-17 11:17:12 +0800429
430 This is an abstract class. To use it, derive a class that implements
Yanray Wang95059002023-07-24 12:29:22 +0800431 write_record and write_comparison methods, then call both of them with
432 proper arguments.
Yanray Wang15c43f32023-07-17 11:17:12 +0800433 """
Yanray Wang21127f72023-07-19 12:09:45 +0800434 def __init__(self, logger: logging.Logger) -> None:
Yanray Wang5b64e4c2023-07-20 15:09:51 +0800435 """
436 :param logger: logging module
437 """
Yanray Wang21127f72023-07-19 12:09:45 +0800438 self.logger = logger
439
Yanray Wang95059002023-07-24 12:29:22 +0800440 def write_record(
Yanray Wang15c43f32023-07-17 11:17:12 +0800441 self,
Yanray Wang955671b2023-07-21 12:08:27 +0800442 git_rev: str,
Yanray Wang95059002023-07-24 12:29:22 +0800443 code_size_text: typing.Dict[str, str],
444 output: typing_util.Writable
Yanray Wang15c43f32023-07-17 11:17:12 +0800445 ) -> None:
446 """Write size record into a file.
447
Yanray Wang955671b2023-07-21 12:08:27 +0800448 :param git_rev: Git revision. (E.g: commit)
Yanray Wang5b64e4c2023-07-20 15:09:51 +0800449 :param code_size_text:
450 string output (utf-8) from measurement tool of code size.
451 - typing.Dict[mod: str]
Yanray Wang95059002023-07-24 12:29:22 +0800452 :param output: output stream which the code size record is written to.
453 (Note: Normally write code size record into File)
Yanray Wang15c43f32023-07-17 11:17:12 +0800454 """
455 raise NotImplementedError
456
Yanray Wangee07afa2023-07-28 16:34:05 +0800457 def write_comparison( #pylint: disable=too-many-arguments
Yanray Wang15c43f32023-07-17 11:17:12 +0800458 self,
459 old_rev: str,
460 new_rev: str,
Yanray Wang95059002023-07-24 12:29:22 +0800461 output: typing_util.Writable,
Yanray Wangee07afa2023-07-28 16:34:05 +0800462 with_markdown=False,
463 show_all=False
Yanray Wang15c43f32023-07-17 11:17:12 +0800464 ) -> None:
Yanray Wang955671b2023-07-21 12:08:27 +0800465 """Write a comparision result into a stream between two Git revisions.
Yanray Wang15c43f32023-07-17 11:17:12 +0800466
Yanray Wang5b64e4c2023-07-20 15:09:51 +0800467 :param old_rev: old Git revision to compared with.
468 :param new_rev: new Git revision to compared with.
Yanray Wang95059002023-07-24 12:29:22 +0800469 :param output: output stream which the code size record is written to.
470 (File / sys.stdout)
471 :param with_markdown: write comparision result in a markdown table.
472 (Default: False)
Yanray Wangee07afa2023-07-28 16:34:05 +0800473 :param show_all: show all objects in comparison result. (Default False)
Yanray Wang15c43f32023-07-17 11:17:12 +0800474 """
475 raise NotImplementedError
476
477
478class CodeSizeGeneratorWithSize(CodeSizeGenerator):
Yanray Wang16ebc572023-05-30 18:10:20 +0800479 """Code Size Base Class for size record saving and writing."""
480
Yanray Wangfc6ed4d2023-07-14 17:33:09 +0800481 class SizeEntry: # pylint: disable=too-few-public-methods
482 """Data Structure to only store information of code size."""
Yanray Wangdcf360d2023-07-27 15:28:20 +0800483 def __init__(self, text: int, data: int, bss: int, dec: int):
Yanray Wangfc6ed4d2023-07-14 17:33:09 +0800484 self.text = text
485 self.data = data
486 self.bss = bss
487 self.total = dec # total <=> dec
488
Yanray Wang21127f72023-07-19 12:09:45 +0800489 def __init__(self, logger: logging.Logger) -> None:
Yanray Wang955671b2023-07-21 12:08:27 +0800490 """ Variable code_size is used to store size info for any Git revisions.
Yanray Wang5b64e4c2023-07-20 15:09:51 +0800491 :param code_size:
492 Data Format as following:
Yanray Wangdcf360d2023-07-27 15:28:20 +0800493 code_size = {
494 git_rev: {
495 module: {
496 file_name: SizeEntry,
497 ...
498 },
499 ...
500 },
501 ...
502 }
Yanray Wang16ebc572023-05-30 18:10:20 +0800503 """
Yanray Wang21127f72023-07-19 12:09:45 +0800504 super().__init__(logger)
Yanray Wang16ebc572023-05-30 18:10:20 +0800505 self.code_size = {} #type: typing.Dict[str, typing.Dict]
Yanray Wangdcf360d2023-07-27 15:28:20 +0800506 self.mod_total_suffix = '-' + 'TOTALS'
Yanray Wang16ebc572023-05-30 18:10:20 +0800507
Yanray Wang955671b2023-07-21 12:08:27 +0800508 def _set_size_record(self, git_rev: str, mod: str, size_text: str) -> None:
509 """Store size information for target Git revision and high-level module.
Yanray Wang16ebc572023-05-30 18:10:20 +0800510
511 size_text Format: text data bss dec hex filename
512 """
513 size_record = {}
514 for line in size_text.splitlines()[1:]:
515 data = line.split()
Yanray Wangdcf360d2023-07-27 15:28:20 +0800516 if re.match(r'\s*\(TOTALS\)', data[5]):
517 data[5] = mod + self.mod_total_suffix
Yanray Wang9b174e92023-07-17 17:59:53 +0800518 # file_name: SizeEntry(text, data, bss, dec)
519 size_record[data[5]] = CodeSizeGeneratorWithSize.SizeEntry(
Yanray Wangdcf360d2023-07-27 15:28:20 +0800520 int(data[0]), int(data[1]), int(data[2]), int(data[3]))
Yanray Wang6ef50492023-07-26 14:59:37 +0800521 self.code_size.setdefault(git_rev, {}).update({mod: size_record})
Yanray Wang16ebc572023-05-30 18:10:20 +0800522
Yanray Wang955671b2023-07-21 12:08:27 +0800523 def read_size_record(self, git_rev: str, fname: str) -> None:
Yanray Wang16ebc572023-05-30 18:10:20 +0800524 """Read size information from csv file and write it into code_size.
525
526 fname Format: filename text data bss dec
527 """
528 mod = ""
529 size_record = {}
530 with open(fname, 'r') as csv_file:
531 for line in csv_file:
532 data = line.strip().split()
533 # check if we find the beginning of a module
534 if data and data[0] in MBEDTLS_STATIC_LIB:
535 mod = data[0]
536 continue
537
538 if mod:
Yanray Wang9b174e92023-07-17 17:59:53 +0800539 # file_name: SizeEntry(text, data, bss, dec)
540 size_record[data[0]] = CodeSizeGeneratorWithSize.SizeEntry(
Yanray Wangdcf360d2023-07-27 15:28:20 +0800541 int(data[1]), int(data[2]), int(data[3]), int(data[4]))
Yanray Wang16ebc572023-05-30 18:10:20 +0800542
543 # check if we hit record for the end of a module
Yanray Wangdcf360d2023-07-27 15:28:20 +0800544 m = re.match(r'\w+' + self.mod_total_suffix, line)
Yanray Wang16ebc572023-05-30 18:10:20 +0800545 if m:
Yanray Wang955671b2023-07-21 12:08:27 +0800546 if git_rev in self.code_size:
547 self.code_size[git_rev].update({mod: size_record})
Yanray Wang16ebc572023-05-30 18:10:20 +0800548 else:
Yanray Wang955671b2023-07-21 12:08:27 +0800549 self.code_size[git_rev] = {mod: size_record}
Yanray Wang16ebc572023-05-30 18:10:20 +0800550 mod = ""
551 size_record = {}
552
Yanray Wang95059002023-07-24 12:29:22 +0800553 def write_record(
Yanray Wang16ebc572023-05-30 18:10:20 +0800554 self,
Yanray Wang955671b2023-07-21 12:08:27 +0800555 git_rev: str,
Yanray Wang95059002023-07-24 12:29:22 +0800556 code_size_text: typing.Dict[str, str],
Yanray Wang16ebc572023-05-30 18:10:20 +0800557 output: typing_util.Writable
558 ) -> None:
559 """Write size information to a file.
560
Yanray Wangdcf360d2023-07-27 15:28:20 +0800561 Writing Format: filename text data bss total(dec)
Yanray Wang16ebc572023-05-30 18:10:20 +0800562 """
Yanray Wang95059002023-07-24 12:29:22 +0800563 for mod, size_text in code_size_text.items():
564 self._set_size_record(git_rev, mod, size_text)
565
Yanray Wangb664cb72023-07-18 12:28:35 +0800566 format_string = "{:<30} {:>7} {:>7} {:>7} {:>7}\n"
567 output.write(format_string.format("filename",
568 "text", "data", "bss", "total"))
Yanray Wang16ebc572023-05-30 18:10:20 +0800569
Yanray Wangdcf360d2023-07-27 15:28:20 +0800570 for mod, f_size in self.code_size[git_rev].items():
571 output.write("\n" + mod + "\n")
572 for fname, size_entry in f_size.items():
573 output.write(format_string
574 .format(fname,
575 size_entry.text, size_entry.data,
576 size_entry.bss, size_entry.total))
577
Yanray Wangee07afa2023-07-28 16:34:05 +0800578 def write_comparison( #pylint: disable=too-many-arguments
Yanray Wang16ebc572023-05-30 18:10:20 +0800579 self,
580 old_rev: str,
581 new_rev: str,
Yanray Wangb664cb72023-07-18 12:28:35 +0800582 output: typing_util.Writable,
Yanray Wangee07afa2023-07-28 16:34:05 +0800583 with_markdown=False,
584 show_all=False
Yanray Wang16ebc572023-05-30 18:10:20 +0800585 ) -> None:
Yanray Wangee07afa2023-07-28 16:34:05 +0800586 # pylint: disable=too-many-locals
Yanray Wang16ebc572023-05-30 18:10:20 +0800587 """Write comparison result into a file.
588
Yanray Wang8a25e6f2023-08-14 14:38:36 +0800589 Writing Format:
590 Markdown Output:
591 filename new(text) new(data) change(text) change(data)
592 CSV Output:
593 filename new(text) new(data) old(text) old(data) change(text) change(data)
Yanray Wang16ebc572023-05-30 18:10:20 +0800594 """
Yanray Wang8a25e6f2023-08-14 14:38:36 +0800595 header_line = ["filename", "new(text)", "old(text)", "change(text)",
596 "new(data)", "old(data)", "change(data)"]
Yanray Wangb664cb72023-07-18 12:28:35 +0800597 if with_markdown:
Yanray Wang8a25e6f2023-08-14 14:38:36 +0800598 dash_line = [":----", "----:", "----:", "----:",
599 "----:", "----:", "----:"]
600 # | filename | new(text) | new(data) | change(text) | change(data) |
601 line_format = "| {0:<30} | {1:>9} | {4:>9} | {3:>12} | {6:>12} |\n"
Yanray Wangdcf360d2023-07-27 15:28:20 +0800602 bold_text = lambda x: '**' + str(x) + '**'
Yanray Wangb664cb72023-07-18 12:28:35 +0800603 else:
Yanray Wang8a25e6f2023-08-14 14:38:36 +0800604 # filename new(text) new(data) old(text) old(data) change(text) change(data)
605 line_format = "{0:<30} {1:>9} {4:>9} {2:>10} {5:>10} {3:>12} {6:>12}\n"
Yanray Wangb664cb72023-07-18 12:28:35 +0800606
Yanray Wangdcf360d2023-07-27 15:28:20 +0800607 def cal_sect_change(
608 old_size: typing.Optional[CodeSizeGeneratorWithSize.SizeEntry],
609 new_size: typing.Optional[CodeSizeGeneratorWithSize.SizeEntry],
610 sect: str
611 ) -> typing.List:
612 """Inner helper function to calculate size change for a section.
Yanray Wangb664cb72023-07-18 12:28:35 +0800613
Yanray Wangdcf360d2023-07-27 15:28:20 +0800614 Convention for special cases:
615 - If the object has been removed in new Git revision,
616 the size is minus code size of old Git revision;
617 the size change is marked as `Removed`,
618 - If the object only exists in new Git revision,
619 the size is code size of new Git revision;
620 the size change is marked as `None`,
Yanray Wang9b174e92023-07-17 17:59:53 +0800621
Yanray Wangdcf360d2023-07-27 15:28:20 +0800622 :param: old_size: code size for objects in old Git revision.
623 :param: new_size: code size for objects in new Git revision.
624 :param: sect: section to calculate from `size` tool. This could be
625 any instance variable in SizeEntry.
626 :return: List of [section size of objects for new Git revision,
Yanray Wang8a25e6f2023-08-14 14:38:36 +0800627 section size of objects for old Git revision,
Yanray Wangdcf360d2023-07-27 15:28:20 +0800628 section size change of objects between two Git revisions]
629 """
630 if old_size and new_size:
631 new_attr = new_size.__dict__[sect]
Yanray Wang8a25e6f2023-08-14 14:38:36 +0800632 old_attr = old_size.__dict__[sect]
633 delta = new_attr - old_attr
Yanray Wang0de11832023-08-14 11:54:47 +0800634 change_attr = '{0:{1}}'.format(delta, '+' if delta else '')
Yanray Wangdcf360d2023-07-27 15:28:20 +0800635 elif old_size:
Yanray Wangbc775c42023-08-16 15:59:55 +0800636 new_attr = 'Removed'
Yanray Wang8a25e6f2023-08-14 14:38:36 +0800637 old_attr = old_size.__dict__[sect]
Yanray Wangbc775c42023-08-16 15:59:55 +0800638 delta = - old_attr
639 change_attr = '{0:{1}}'.format(delta, '+' if delta else '')
Yanray Wangdcf360d2023-07-27 15:28:20 +0800640 elif new_size:
641 new_attr = new_size.__dict__[sect]
Yanray Wang8a25e6f2023-08-14 14:38:36 +0800642 old_attr = 'NotCreated'
Yanray Wangbc775c42023-08-16 15:59:55 +0800643 delta = new_attr
644 change_attr = '{0:{1}}'.format(delta, '+' if delta else '')
Yanray Wang9b174e92023-07-17 17:59:53 +0800645 else:
Yanray Wangdcf360d2023-07-27 15:28:20 +0800646 # Should never happen
647 new_attr = 'Error'
Yanray Wang8a25e6f2023-08-14 14:38:36 +0800648 old_attr = 'Error'
Yanray Wangdcf360d2023-07-27 15:28:20 +0800649 change_attr = 'Error'
Yanray Wang8a25e6f2023-08-14 14:38:36 +0800650 return [new_attr, old_attr, change_attr]
Yanray Wangdcf360d2023-07-27 15:28:20 +0800651
652 # sort dictionary by key
653 sort_by_k = lambda item: item[0].lower()
654 def get_results(
655 f_rev_size:
656 typing.Dict[str,
657 typing.Dict[str,
658 CodeSizeGeneratorWithSize.SizeEntry]]
659 ) -> typing.List:
660 """Return List of results in the format of:
Yanray Wang8a25e6f2023-08-14 14:38:36 +0800661 [filename, new(text), old(text), change(text),
662 new(data), old(data), change(data)]
Yanray Wangdcf360d2023-07-27 15:28:20 +0800663 """
664 res = []
665 for fname, revs_size in sorted(f_rev_size.items(), key=sort_by_k):
666 old_size = revs_size.get(old_rev)
667 new_size = revs_size.get(new_rev)
668
669 text_sect = cal_sect_change(old_size, new_size, 'text')
670 data_sect = cal_sect_change(old_size, new_size, 'data')
671 # skip the files that haven't changed in code size
Yanray Wang8a25e6f2023-08-14 14:38:36 +0800672 if not show_all and text_sect[-1] == '0' and data_sect[-1] == '0':
Yanray Wangdcf360d2023-07-27 15:28:20 +0800673 continue
674
675 res.append([fname, *text_sect, *data_sect])
676 return res
677
678 # write header
679 output.write(line_format.format(*header_line))
680 if with_markdown:
681 output.write(line_format.format(*dash_line))
682 for mod in MBEDTLS_STATIC_LIB:
683 # convert self.code_size to:
684 # {
685 # file_name: {
686 # old_rev: SizeEntry,
687 # new_rev: SizeEntry
688 # },
689 # ...
690 # }
691 f_rev_size = {} #type: typing.Dict[str, typing.Dict]
692 for fname, size_entry in self.code_size[old_rev][mod].items():
693 f_rev_size.setdefault(fname, {}).update({old_rev: size_entry})
694 for fname, size_entry in self.code_size[new_rev][mod].items():
695 f_rev_size.setdefault(fname, {}).update({new_rev: size_entry})
696
697 mod_total_sz = f_rev_size.pop(mod + self.mod_total_suffix)
698 res = get_results(f_rev_size)
699 total_clm = get_results({mod + self.mod_total_suffix: mod_total_sz})
700 if with_markdown:
701 # bold row of mod-TOTALS in markdown table
702 total_clm = [[bold_text(j) for j in i] for i in total_clm]
703 res += total_clm
704
705 # write comparison result
706 for line in res:
707 output.write(line_format.format(*line))
Yanray Wang16ebc572023-05-30 18:10:20 +0800708
709
Yanray Wangfc6ed4d2023-07-14 17:33:09 +0800710class CodeSizeComparison:
Xiaofei Bai2400b502021-10-21 12:22:58 +0000711 """Compare code size between two Git revisions."""
Xiaofei Baibca03e52021-09-09 09:42:37 +0000712
Yanray Wang955671b2023-07-21 12:08:27 +0800713 def __init__( #pylint: disable=too-many-arguments
Yanray Wang72b105f2023-05-31 15:20:39 +0800714 self,
Yanray Wang955671b2023-07-21 12:08:27 +0800715 old_size_dist_info: CodeSizeDistinctInfo,
716 new_size_dist_info: CodeSizeDistinctInfo,
717 size_common_info: CodeSizeCommonInfo,
718 result_options: CodeSizeResultInfo,
Yanray Wang21127f72023-07-19 12:09:45 +0800719 logger: logging.Logger,
Yanray Wang72b105f2023-05-31 15:20:39 +0800720 ) -> None:
Xiaofei Baibca03e52021-09-09 09:42:37 +0000721 """
Yanray Wang955671b2023-07-21 12:08:27 +0800722 :param old_size_dist_info: CodeSizeDistinctInfo containing old distinct
723 info to compare code size with.
724 :param new_size_dist_info: CodeSizeDistinctInfo containing new distinct
725 info to take as comparision base.
726 :param size_common_info: CodeSizeCommonInfo containing common info for
727 both old and new size distinct info and
728 measurement tool.
729 :param result_options: CodeSizeResultInfo containing results options for
730 code size record and comparision.
Yanray Wang5b64e4c2023-07-20 15:09:51 +0800731 :param logger: logging module
Xiaofei Baibca03e52021-09-09 09:42:37 +0000732 """
Xiaofei Baibca03e52021-09-09 09:42:37 +0000733
Yanray Wang21127f72023-07-19 12:09:45 +0800734 self.logger = logger
735
Yanray Wang955671b2023-07-21 12:08:27 +0800736 self.old_size_dist_info = old_size_dist_info
737 self.new_size_dist_info = new_size_dist_info
738 self.size_common_info = size_common_info
Yanray Wang5605c6f2023-07-21 16:09:00 +0800739 # infer pre make command
740 self.old_size_dist_info.pre_make_cmd = CodeSizeBuildInfo(
741 self.old_size_dist_info, self.size_common_info.host_arch,
742 self.logger).infer_pre_make_command()
743 self.new_size_dist_info.pre_make_cmd = CodeSizeBuildInfo(
744 self.new_size_dist_info, self.size_common_info.host_arch,
745 self.logger).infer_pre_make_command()
Yanray Wang386c2f92023-07-20 15:32:15 +0800746 # infer make command
Yanray Wang955671b2023-07-21 12:08:27 +0800747 self.old_size_dist_info.make_cmd = CodeSizeBuildInfo(
748 self.old_size_dist_info, self.size_common_info.host_arch,
Yanray Wang21127f72023-07-19 12:09:45 +0800749 self.logger).infer_make_command()
Yanray Wang955671b2023-07-21 12:08:27 +0800750 self.new_size_dist_info.make_cmd = CodeSizeBuildInfo(
751 self.new_size_dist_info, self.size_common_info.host_arch,
Yanray Wang21127f72023-07-19 12:09:45 +0800752 self.logger).infer_make_command()
Yanray Wang386c2f92023-07-20 15:32:15 +0800753 # initialize size parser with corresponding measurement tool
Yanray Wang21127f72023-07-19 12:09:45 +0800754 self.code_size_generator = self.__generate_size_parser()
Xiaofei Baibca03e52021-09-09 09:42:37 +0000755
Yanray Wang955671b2023-07-21 12:08:27 +0800756 self.result_options = result_options
757 self.csv_dir = os.path.abspath(self.result_options.record_dir)
758 os.makedirs(self.csv_dir, exist_ok=True)
759 self.comp_dir = os.path.abspath(self.result_options.comp_dir)
760 os.makedirs(self.comp_dir, exist_ok=True)
761
Yanray Wang21127f72023-07-19 12:09:45 +0800762 def __generate_size_parser(self):
Yanray Wang5b64e4c2023-07-20 15:09:51 +0800763 """Generate a parser for the corresponding measurement tool."""
Yanray Wang955671b2023-07-21 12:08:27 +0800764 if re.match(r'size', self.size_common_info.measure_cmd.strip()):
Yanray Wang21127f72023-07-19 12:09:45 +0800765 return CodeSizeGeneratorWithSize(self.logger)
Yanray Wang802af162023-07-17 14:04:30 +0800766 else:
Yanray Wang21127f72023-07-19 12:09:45 +0800767 self.logger.error("Unsupported measurement tool: `{}`."
Yanray Wang955671b2023-07-21 12:08:27 +0800768 .format(self.size_common_info.measure_cmd
Yanray Wang21127f72023-07-19 12:09:45 +0800769 .strip().split(' ')[0]))
Yanray Wang802af162023-07-17 14:04:30 +0800770 sys.exit(1)
771
Yanray Wang386c2f92023-07-20 15:32:15 +0800772 def cal_code_size(
773 self,
Yanray Wang955671b2023-07-21 12:08:27 +0800774 size_dist_info: CodeSizeDistinctInfo
Yanray Wang386c2f92023-07-20 15:32:15 +0800775 ) -> typing.Dict[str, str]:
Yanray Wang5b64e4c2023-07-20 15:09:51 +0800776 """Calculate code size of library/*.o in a UTF-8 encoding"""
Xiaofei Baibca03e52021-09-09 09:42:37 +0000777
Yanray Wang955671b2023-07-21 12:08:27 +0800778 return CodeSizeCalculator(size_dist_info.git_rev,
Yanray Wang5605c6f2023-07-21 16:09:00 +0800779 size_dist_info.pre_make_cmd,
Yanray Wang955671b2023-07-21 12:08:27 +0800780 size_dist_info.make_cmd,
781 self.size_common_info.measure_cmd,
Yanray Wang21127f72023-07-19 12:09:45 +0800782 self.logger).cal_libraries_code_size()
Yanray Wang8804db92023-05-30 18:18:18 +0800783
Yanray Wang955671b2023-07-21 12:08:27 +0800784 def gen_code_size_report(self, size_dist_info: CodeSizeDistinctInfo) -> None:
Yanray Wang5e9130a2023-07-17 11:55:54 +0800785 """Generate code size record and write it into a file."""
Xiaofei Baibca03e52021-09-09 09:42:37 +0000786
Yanray Wang21127f72023-07-19 12:09:45 +0800787 self.logger.info("Start to generate code size record for {}."
Yanray Wang955671b2023-07-21 12:08:27 +0800788 .format(size_dist_info.git_rev))
Yanray Wanga6cf6922023-07-24 15:20:42 +0800789 output_file = os.path.join(
790 self.csv_dir,
791 '{}-{}.csv'
792 .format(size_dist_info.get_info_indication(),
793 self.size_common_info.get_info_indication()))
Xiaofei Baibca03e52021-09-09 09:42:37 +0000794 # Check if the corresponding record exists
Yanray Wang955671b2023-07-21 12:08:27 +0800795 if size_dist_info.git_rev != "current" and \
Yanray Wang21127f72023-07-19 12:09:45 +0800796 os.path.exists(output_file):
797 self.logger.debug("Code size csv file for {} already exists."
Yanray Wang955671b2023-07-21 12:08:27 +0800798 .format(size_dist_info.git_rev))
Yanray Wang21127f72023-07-19 12:09:45 +0800799 self.code_size_generator.read_size_record(
Yanray Wang955671b2023-07-21 12:08:27 +0800800 size_dist_info.git_rev, output_file)
Xiaofei Baibca03e52021-09-09 09:42:37 +0000801 else:
Yanray Wang95059002023-07-24 12:29:22 +0800802 # measure code size
803 code_size_text = self.cal_code_size(size_dist_info)
804
805 self.logger.debug("Generating code size csv for {}."
806 .format(size_dist_info.git_rev))
807 output = open(output_file, "w")
808 self.code_size_generator.write_record(
809 size_dist_info.git_rev, code_size_text, output)
Xiaofei Baibca03e52021-09-09 09:42:37 +0000810
Yanray Wang386c2f92023-07-20 15:32:15 +0800811 def gen_code_size_comparison(self) -> None:
Yanray Wang955671b2023-07-21 12:08:27 +0800812 """Generate results of code size changes between two Git revisions,
Yanray Wang5b64e4c2023-07-20 15:09:51 +0800813 old and new.
814
Yanray Wang955671b2023-07-21 12:08:27 +0800815 - Measured code size result of these two Git revisions must be available.
Yanray Wang5b64e4c2023-07-20 15:09:51 +0800816 - The result is directed into either file / stdout depending on
Yanray Wang955671b2023-07-21 12:08:27 +0800817 the option, size_common_info.result_options.stdout. (Default: file)
Yanray Wang5b64e4c2023-07-20 15:09:51 +0800818 """
Xiaofei Baibca03e52021-09-09 09:42:37 +0000819
Yanray Wang21127f72023-07-19 12:09:45 +0800820 self.logger.info("Start to generate comparision result between "\
821 "{} and {}."
Yanray Wang955671b2023-07-21 12:08:27 +0800822 .format(self.old_size_dist_info.git_rev,
823 self.new_size_dist_info.git_rev))
Yanray Wanga6cf6922023-07-24 15:20:42 +0800824 if self.result_options.stdout:
825 output = sys.stdout
826 else:
827 output_file = os.path.join(
828 self.comp_dir,
Yanray Wangb1673202023-07-28 13:47:19 +0800829 '{}-{}-{}.{}'
Yanray Wanga6cf6922023-07-24 15:20:42 +0800830 .format(self.old_size_dist_info.get_info_indication(),
831 self.new_size_dist_info.get_info_indication(),
Yanray Wangb1673202023-07-28 13:47:19 +0800832 self.size_common_info.get_info_indication(),
833 'md' if self.result_options.with_markdown else 'csv'))
Yanray Wanga6cf6922023-07-24 15:20:42 +0800834 output = open(output_file, "w")
Xiaofei Bai184e8b62021-10-26 09:23:42 +0000835
Yanray Wang95059002023-07-24 12:29:22 +0800836 self.logger.debug("Generating comparison results between {} and {}."
837 .format(self.old_size_dist_info.git_rev,
838 self.new_size_dist_info.git_rev))
Yanray Wangea842e72023-07-26 10:34:39 +0800839 if self.result_options.with_markdown or self.result_options.stdout:
840 print("Measure code size between {} and {} by `{}`."
841 .format(self.old_size_dist_info.get_info_indication(),
842 self.new_size_dist_info.get_info_indication(),
843 self.size_common_info.get_info_indication()),
844 file=output)
Yanray Wang95059002023-07-24 12:29:22 +0800845 self.code_size_generator.write_comparison(
Yanray Wang955671b2023-07-21 12:08:27 +0800846 self.old_size_dist_info.git_rev,
847 self.new_size_dist_info.git_rev,
Yanray Wangee07afa2023-07-28 16:34:05 +0800848 output, self.result_options.with_markdown,
849 self.result_options.show_all)
Yanray Wang21127f72023-07-19 12:09:45 +0800850
Yanray Wang386c2f92023-07-20 15:32:15 +0800851 def get_comparision_results(self) -> None:
Yanray Wang955671b2023-07-21 12:08:27 +0800852 """Compare size of library/*.o between self.old_size_dist_info and
853 self.old_size_dist_info and generate the result file."""
Gilles Peskined9071e72022-09-18 21:17:09 +0200854 build_tree.check_repo_path()
Yanray Wang955671b2023-07-21 12:08:27 +0800855 self.gen_code_size_report(self.old_size_dist_info)
856 self.gen_code_size_report(self.new_size_dist_info)
Yanray Wang386c2f92023-07-20 15:32:15 +0800857 self.gen_code_size_comparison()
Xiaofei Baibca03e52021-09-09 09:42:37 +0000858
Xiaofei Bai2400b502021-10-21 12:22:58 +0000859def main():
Yanray Wang502c54f2023-05-31 11:41:36 +0800860 parser = argparse.ArgumentParser(description=(__doc__))
861 group_required = parser.add_argument_group(
862 'required arguments',
863 'required arguments to parse for running ' + os.path.basename(__file__))
864 group_required.add_argument(
Yanray Wang5b64e4c2023-07-20 15:09:51 +0800865 '-o', '--old-rev', type=str, required=True,
Yanray Wang955671b2023-07-21 12:08:27 +0800866 help='old Git revision for comparison.')
Yanray Wang502c54f2023-05-31 11:41:36 +0800867
868 group_optional = parser.add_argument_group(
869 'optional arguments',
870 'optional arguments to parse for running ' + os.path.basename(__file__))
871 group_optional.add_argument(
Yanray Wang9e8b6712023-07-26 15:37:26 +0800872 '--record-dir', type=str, default='code_size_records',
Yanray Wang955671b2023-07-21 12:08:27 +0800873 help='directory where code size record is stored. '
874 '(Default: code_size_records)')
875 group_optional.add_argument(
Yanray Wang9e8b6712023-07-26 15:37:26 +0800876 '--comp-dir', type=str, default='comparison',
Yanray Wang5b64e4c2023-07-20 15:09:51 +0800877 help='directory where comparison result is stored. '
878 '(Default: comparison)')
Yanray Wang502c54f2023-05-31 11:41:36 +0800879 group_optional.add_argument(
Yanray Wang68265f42023-07-26 14:44:52 +0800880 '-n', '--new-rev', type=str, default='current',
Yanray Wang955671b2023-07-21 12:08:27 +0800881 help='new Git revision as comparison base. '
Yanray Wang5b64e4c2023-07-20 15:09:51 +0800882 '(Default is the current work directory, including uncommitted '
883 'changes.)')
Yanray Wang502c54f2023-05-31 11:41:36 +0800884 group_optional.add_argument(
Yanray Wang5b64e4c2023-07-20 15:09:51 +0800885 '-a', '--arch', type=str, default=detect_arch(),
Yanray Wang23bd5322023-05-24 11:03:59 +0800886 choices=list(map(lambda s: s.value, SupportedArch)),
Yanray Wang5b64e4c2023-07-20 15:09:51 +0800887 help='Specify architecture for code size comparison. '
888 '(Default is the host architecture.)')
Yanray Wang502c54f2023-05-31 11:41:36 +0800889 group_optional.add_argument(
Yanray Wang5b64e4c2023-07-20 15:09:51 +0800890 '-c', '--config', type=str, default=SupportedConfig.DEFAULT.value,
Yanray Wang6a862582023-05-24 12:24:38 +0800891 choices=list(map(lambda s: s.value, SupportedConfig)),
Yanray Wang5b64e4c2023-07-20 15:09:51 +0800892 help='Specify configuration type for code size comparison. '
Thomas Daubney540324c2023-10-06 17:07:24 +0100893 '(Default is the current Mbed TLS configuration.)')
Yanray Wangb664cb72023-07-18 12:28:35 +0800894 group_optional.add_argument(
895 '--markdown', action='store_true', dest='markdown',
Yanray Wang5b64e4c2023-07-20 15:09:51 +0800896 help='Show comparision of code size in a markdown table. '
897 '(Only show the files that have changed).')
Yanray Wang227576a2023-07-18 14:35:05 +0800898 group_optional.add_argument(
899 '--stdout', action='store_true', dest='stdout',
Yanray Wang5b64e4c2023-07-20 15:09:51 +0800900 help='Set this option to direct comparison result into sys.stdout. '
901 '(Default: file)')
Yanray Wang21127f72023-07-19 12:09:45 +0800902 group_optional.add_argument(
Yanray Wangee07afa2023-07-28 16:34:05 +0800903 '--show-all', action='store_true', dest='show_all',
904 help='Show all the objects in comparison result, including the ones '
905 'that haven\'t changed in code size. (Default: False)')
906 group_optional.add_argument(
Yanray Wang21127f72023-07-19 12:09:45 +0800907 '--verbose', action='store_true', dest='verbose',
Yanray Wang5b64e4c2023-07-20 15:09:51 +0800908 help='Show logs in detail for code size measurement. '
909 '(Default: False)')
Xiaofei Baibca03e52021-09-09 09:42:37 +0000910 comp_args = parser.parse_args()
911
Yanray Wang21127f72023-07-19 12:09:45 +0800912 logger = logging.getLogger()
Yanray Wang1998aac2023-08-14 10:33:37 +0800913 logging_util.configure_logger(logger, split_level=logging.NOTSET)
914 logger.setLevel(logging.DEBUG if comp_args.verbose else logging.INFO)
Yanray Wang21127f72023-07-19 12:09:45 +0800915
Yanray Wang9e8b6712023-07-26 15:37:26 +0800916 if os.path.isfile(comp_args.record_dir):
917 logger.error("record directory: {} is not a directory"
918 .format(comp_args.record_dir))
919 sys.exit(1)
Yanray Wang955671b2023-07-21 12:08:27 +0800920 if os.path.isfile(comp_args.comp_dir):
Yanray Wang9e8b6712023-07-26 15:37:26 +0800921 logger.error("comparison directory: {} is not a directory"
922 .format(comp_args.comp_dir))
923 sys.exit(1)
Xiaofei Baibca03e52021-09-09 09:42:37 +0000924
Yanray Wang68265f42023-07-26 14:44:52 +0800925 comp_args.old_rev = CodeSizeCalculator.validate_git_revision(
926 comp_args.old_rev)
927 if comp_args.new_rev != 'current':
928 comp_args.new_rev = CodeSizeCalculator.validate_git_revision(
Yanray Wang955671b2023-07-21 12:08:27 +0800929 comp_args.new_rev)
Xiaofei Bai2400b502021-10-21 12:22:58 +0000930
Yanray Wang5605c6f2023-07-21 16:09:00 +0800931 # version, git_rev, arch, config, compiler, opt_level
Yanray Wang955671b2023-07-21 12:08:27 +0800932 old_size_dist_info = CodeSizeDistinctInfo(
Yanray Wang68265f42023-07-26 14:44:52 +0800933 'old', comp_args.old_rev, comp_args.arch, comp_args.config, 'cc', '-Os')
Yanray Wang955671b2023-07-21 12:08:27 +0800934 new_size_dist_info = CodeSizeDistinctInfo(
Yanray Wang68265f42023-07-26 14:44:52 +0800935 'new', comp_args.new_rev, comp_args.arch, comp_args.config, 'cc', '-Os')
Yanray Wang5605c6f2023-07-21 16:09:00 +0800936 # host_arch, measure_cmd
Yanray Wang955671b2023-07-21 12:08:27 +0800937 size_common_info = CodeSizeCommonInfo(
938 detect_arch(), 'size -t')
Yanray Wangee07afa2023-07-28 16:34:05 +0800939 # record_dir, comp_dir, with_markdown, stdout, show_all
Yanray Wang955671b2023-07-21 12:08:27 +0800940 result_options = CodeSizeResultInfo(
941 comp_args.record_dir, comp_args.comp_dir,
Yanray Wangee07afa2023-07-28 16:34:05 +0800942 comp_args.markdown, comp_args.stdout, comp_args.show_all)
Yanray Wang923f9432023-07-17 12:43:00 +0800943
Yanray Wanga6cf6922023-07-24 15:20:42 +0800944 logger.info("Measure code size between {} and {} by `{}`."
945 .format(old_size_dist_info.get_info_indication(),
946 new_size_dist_info.get_info_indication(),
947 size_common_info.get_info_indication()))
Yanray Wang955671b2023-07-21 12:08:27 +0800948 CodeSizeComparison(old_size_dist_info, new_size_dist_info,
949 size_common_info, result_options,
950 logger).get_comparision_results()
Xiaofei Baibca03e52021-09-09 09:42:37 +0000951
Xiaofei Baibca03e52021-09-09 09:42:37 +0000952if __name__ == "__main__":
Xiaofei Bai2400b502021-10-21 12:22:58 +0000953 main()