2021-09-09 11:42:37 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
"""
|
|
|
|
This script is for comparing the size of the library files from two
|
|
|
|
different Git revisions within an Mbed TLS repository.
|
|
|
|
The results of the comparison is formatted as csv and stored at a
|
|
|
|
configurable location.
|
|
|
|
Note: must be run from Mbed TLS root.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Copyright The Mbed TLS Contributors
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
# not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import os
|
2023-05-30 12:10:20 +02:00
|
|
|
import re
|
2021-09-09 11:42:37 +02:00
|
|
|
import subprocess
|
|
|
|
import sys
|
2023-05-30 12:10:20 +02:00
|
|
|
import typing
|
2023-05-24 05:03:59 +02:00
|
|
|
from enum import Enum
|
2021-09-09 11:42:37 +02:00
|
|
|
|
2023-05-30 12:10:20 +02:00
|
|
|
from mbedtls_dev import typing_util
|
2022-09-18 21:17:09 +02:00
|
|
|
from mbedtls_dev import build_tree
|
|
|
|
|
2023-05-24 05:03:59 +02:00
|
|
|
class SupportedArch(Enum):
|
|
|
|
"""Supported architecture for code size measurement."""
|
|
|
|
AARCH64 = 'aarch64'
|
|
|
|
AARCH32 = 'aarch32'
|
2023-05-29 10:45:56 +02:00
|
|
|
ARMV8_M = 'armv8-m'
|
2023-05-24 05:03:59 +02:00
|
|
|
X86_64 = 'x86_64'
|
|
|
|
X86 = 'x86'
|
|
|
|
|
2023-05-24 06:24:38 +02:00
|
|
|
CONFIG_TFM_MEDIUM_MBEDCRYPTO_H = "../configs/tfm_mbedcrypto_config_profile_medium.h"
|
|
|
|
CONFIG_TFM_MEDIUM_PSA_CRYPTO_H = "../configs/crypto_config_profile_medium.h"
|
|
|
|
class SupportedConfig(Enum):
|
|
|
|
"""Supported configuration for code size measurement."""
|
|
|
|
DEFAULT = 'default'
|
|
|
|
TFM_MEDIUM = 'tfm-medium'
|
|
|
|
|
2023-05-30 12:10:20 +02:00
|
|
|
# Static library
|
|
|
|
MBEDTLS_STATIC_LIB = {
|
|
|
|
'CRYPTO': 'library/libmbedcrypto.a',
|
|
|
|
'X509': 'library/libmbedx509.a',
|
|
|
|
'TLS': 'library/libmbedtls.a',
|
|
|
|
}
|
|
|
|
|
2023-05-24 05:03:59 +02:00
|
|
|
DETECT_ARCH_CMD = "cc -dM -E - < /dev/null"
|
|
|
|
def detect_arch() -> str:
|
|
|
|
"""Auto-detect host architecture."""
|
|
|
|
cc_output = subprocess.check_output(DETECT_ARCH_CMD, shell=True).decode()
|
|
|
|
if "__aarch64__" in cc_output:
|
|
|
|
return SupportedArch.AARCH64.value
|
|
|
|
if "__arm__" in cc_output:
|
|
|
|
return SupportedArch.AARCH32.value
|
|
|
|
if "__x86_64__" in cc_output:
|
|
|
|
return SupportedArch.X86_64.value
|
|
|
|
if "__x86__" in cc_output:
|
|
|
|
return SupportedArch.X86.value
|
|
|
|
else:
|
|
|
|
print("Unknown host architecture, cannot auto-detect arch.")
|
|
|
|
sys.exit(1)
|
2022-09-18 21:17:09 +02:00
|
|
|
|
2023-05-24 06:24:38 +02:00
|
|
|
class CodeSizeInfo: # pylint: disable=too-few-public-methods
|
|
|
|
"""Gather information used to measure code size.
|
|
|
|
|
|
|
|
It collects information about architecture, configuration in order to
|
|
|
|
infer build command for code size measurement.
|
|
|
|
"""
|
|
|
|
|
2023-05-31 05:08:04 +02:00
|
|
|
SupportedArchConfig = [
|
|
|
|
"-a " + SupportedArch.AARCH64.value + " -c " + SupportedConfig.DEFAULT.value,
|
|
|
|
"-a " + SupportedArch.AARCH32.value + " -c " + SupportedConfig.DEFAULT.value,
|
|
|
|
"-a " + SupportedArch.X86_64.value + " -c " + SupportedConfig.DEFAULT.value,
|
|
|
|
"-a " + SupportedArch.X86.value + " -c " + SupportedConfig.DEFAULT.value,
|
|
|
|
"-a " + SupportedArch.ARMV8_M.value + " -c " + SupportedConfig.TFM_MEDIUM.value,
|
|
|
|
]
|
|
|
|
|
2023-06-01 05:29:06 +02:00
|
|
|
def __init__(self, arch: str, config: str, sys_arch: str) -> None:
|
2023-05-24 06:24:38 +02:00
|
|
|
"""
|
|
|
|
arch: architecture to measure code size on.
|
|
|
|
config: configuration type to measure code size with.
|
2023-07-04 11:21:28 +02:00
|
|
|
sys_arch: host architecture.
|
2023-05-24 06:24:38 +02:00
|
|
|
make_command: command to build library (Inferred from arch and config).
|
|
|
|
"""
|
|
|
|
self.arch = arch
|
|
|
|
self.config = config
|
2023-06-01 05:29:06 +02:00
|
|
|
self.sys_arch = sys_arch
|
2023-05-24 06:24:38 +02:00
|
|
|
self.make_command = self.set_make_command()
|
|
|
|
|
|
|
|
def set_make_command(self) -> str:
|
|
|
|
"""Infer build command based on architecture and configuration."""
|
|
|
|
|
2023-06-01 05:29:06 +02:00
|
|
|
if self.config == SupportedConfig.DEFAULT.value and \
|
|
|
|
self.arch == self.sys_arch:
|
2023-05-24 06:24:38 +02:00
|
|
|
return 'make -j lib CFLAGS=\'-Os \' '
|
2023-05-29 10:45:56 +02:00
|
|
|
elif self.arch == SupportedArch.ARMV8_M.value and \
|
2023-05-24 06:24:38 +02:00
|
|
|
self.config == SupportedConfig.TFM_MEDIUM.value:
|
|
|
|
return \
|
2023-05-29 08:48:18 +02:00
|
|
|
'make -j lib CC=armclang \
|
2023-05-24 06:24:38 +02:00
|
|
|
CFLAGS=\'--target=arm-arm-none-eabi -mcpu=cortex-m33 -Os \
|
|
|
|
-DMBEDTLS_CONFIG_FILE=\\\"' + CONFIG_TFM_MEDIUM_MBEDCRYPTO_H + '\\\" \
|
|
|
|
-DMBEDTLS_PSA_CRYPTO_CONFIG_FILE=\\\"' + CONFIG_TFM_MEDIUM_PSA_CRYPTO_H + '\\\" \''
|
|
|
|
else:
|
2023-06-01 05:29:06 +02:00
|
|
|
print("Unsupported combination of architecture: {} and configuration: {}"
|
2023-05-24 06:24:38 +02:00
|
|
|
.format(self.arch, self.config))
|
2023-05-31 05:08:04 +02:00
|
|
|
print("\nPlease use supported combination of architecture and configuration:")
|
|
|
|
for comb in CodeSizeInfo.SupportedArchConfig:
|
|
|
|
print(comb)
|
2023-06-01 05:29:06 +02:00
|
|
|
print("\nFor your system, please use:")
|
|
|
|
for comb in CodeSizeInfo.SupportedArchConfig:
|
|
|
|
if "default" in comb and self.sys_arch not in comb:
|
|
|
|
continue
|
|
|
|
print(comb)
|
2023-05-24 06:24:38 +02:00
|
|
|
sys.exit(1)
|
|
|
|
|
2023-05-30 12:10:20 +02:00
|
|
|
class SizeEntry: # pylint: disable=too-few-public-methods
|
|
|
|
"""Data Structure to only store information of code size."""
|
|
|
|
def __init__(self, text, data, bss, dec):
|
|
|
|
self.text = text
|
|
|
|
self.data = data
|
|
|
|
self.bss = bss
|
|
|
|
self.total = dec # total <=> dec
|
|
|
|
|
|
|
|
class CodeSizeBase:
|
|
|
|
"""Code Size Base Class for size record saving and writing."""
|
|
|
|
|
|
|
|
def __init__(self) -> None:
|
|
|
|
""" Variable code_size is used to store size info for any revisions.
|
|
|
|
code_size: (data format)
|
|
|
|
{revision: {module: {file_name: SizeEntry,
|
|
|
|
etc ...
|
|
|
|
},
|
|
|
|
etc ...
|
|
|
|
},
|
|
|
|
etc ...
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
self.code_size = {} #type: typing.Dict[str, typing.Dict]
|
|
|
|
|
|
|
|
def set_size_record(self, revision: str, mod: str, size_text: str) -> None:
|
|
|
|
"""Store size information for target revision and high-level module.
|
|
|
|
|
|
|
|
size_text Format: text data bss dec hex filename
|
|
|
|
"""
|
|
|
|
size_record = {}
|
|
|
|
for line in size_text.splitlines()[1:]:
|
|
|
|
data = line.split()
|
|
|
|
size_record[data[5]] = SizeEntry(data[0], data[1], data[2], data[3])
|
|
|
|
if revision in self.code_size:
|
|
|
|
self.code_size[revision].update({mod: size_record})
|
|
|
|
else:
|
|
|
|
self.code_size[revision] = {mod: size_record}
|
|
|
|
|
|
|
|
def read_size_record(self, revision: str, fname: str) -> None:
|
|
|
|
"""Read size information from csv file and write it into code_size.
|
|
|
|
|
|
|
|
fname Format: filename text data bss dec
|
|
|
|
"""
|
|
|
|
mod = ""
|
|
|
|
size_record = {}
|
|
|
|
with open(fname, 'r') as csv_file:
|
|
|
|
for line in csv_file:
|
|
|
|
data = line.strip().split()
|
|
|
|
# check if we find the beginning of a module
|
|
|
|
if data and data[0] in MBEDTLS_STATIC_LIB:
|
|
|
|
mod = data[0]
|
|
|
|
continue
|
2023-05-24 06:24:38 +02:00
|
|
|
|
2023-05-30 12:10:20 +02:00
|
|
|
if mod:
|
|
|
|
size_record[data[0]] = \
|
|
|
|
SizeEntry(data[1], data[2], data[3], data[4])
|
|
|
|
|
|
|
|
# check if we hit record for the end of a module
|
|
|
|
m = re.match(r'.?TOTALS', line)
|
|
|
|
if m:
|
|
|
|
if revision in self.code_size:
|
|
|
|
self.code_size[revision].update({mod: size_record})
|
|
|
|
else:
|
|
|
|
self.code_size[revision] = {mod: size_record}
|
|
|
|
mod = ""
|
|
|
|
size_record = {}
|
|
|
|
|
|
|
|
def _size_reader_helper(
|
|
|
|
self,
|
|
|
|
revision: str,
|
|
|
|
output: typing_util.Writable
|
|
|
|
) -> typing.Iterator[tuple]:
|
|
|
|
"""A helper function to peel code_size based on revision."""
|
|
|
|
for mod, file_size in self.code_size[revision].items():
|
|
|
|
output.write("\n" + mod + "\n")
|
|
|
|
for fname, size_entry in file_size.items():
|
|
|
|
yield mod, fname, size_entry
|
|
|
|
|
|
|
|
def write_size_record(
|
|
|
|
self,
|
|
|
|
revision: str,
|
|
|
|
output: typing_util.Writable
|
|
|
|
) -> None:
|
|
|
|
"""Write size information to a file.
|
|
|
|
|
|
|
|
Writing Format: file_name text data bss total(dec)
|
|
|
|
"""
|
|
|
|
output.write("{:<30} {:>7} {:>7} {:>7} {:>7}\n"
|
|
|
|
.format("filename", "text", "data", "bss", "total"))
|
|
|
|
for _, fname, size_entry in self._size_reader_helper(revision, output):
|
|
|
|
output.write("{:<30} {:>7} {:>7} {:>7} {:>7}\n"
|
|
|
|
.format(fname, size_entry.text, size_entry.data,\
|
|
|
|
size_entry.bss, size_entry.total))
|
|
|
|
|
|
|
|
def write_comparison(
|
|
|
|
self,
|
|
|
|
old_rev: str,
|
|
|
|
new_rev: str,
|
|
|
|
output: typing_util.Writable
|
|
|
|
) -> None:
|
|
|
|
"""Write comparison result into a file.
|
|
|
|
|
|
|
|
Writing Format: file_name current(total) old(total) change(Byte) change_pct(%)
|
|
|
|
"""
|
|
|
|
output.write("{:<30} {:>7} {:>7} {:>7} {:>7}\n"
|
|
|
|
.format("filename", "current", "old", "change", "change%"))
|
|
|
|
for mod, fname, size_entry in self._size_reader_helper(new_rev, output):
|
|
|
|
new_size = int(size_entry.total)
|
|
|
|
# check if we have the file in old revision
|
|
|
|
if fname in self.code_size[old_rev][mod]:
|
|
|
|
old_size = int(self.code_size[old_rev][mod][fname].total)
|
|
|
|
change = new_size - old_size
|
|
|
|
if old_size != 0:
|
|
|
|
change_pct = change / old_size
|
|
|
|
else:
|
|
|
|
change_pct = 0
|
|
|
|
output.write("{:<30} {:>7} {:>7} {:>7} {:>7.2%}\n"
|
|
|
|
.format(fname, new_size, old_size, change, change_pct))
|
|
|
|
else:
|
|
|
|
output.write("{} {}\n".format(fname, new_size))
|
|
|
|
|
|
|
|
|
|
|
|
class CodeSizeComparison(CodeSizeBase):
|
2021-10-21 14:22:58 +02:00
|
|
|
"""Compare code size between two Git revisions."""
|
2021-09-09 11:42:37 +02:00
|
|
|
|
2023-05-31 09:20:39 +02:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
old_revision: str,
|
|
|
|
new_revision: str,
|
|
|
|
result_dir: str,
|
|
|
|
code_size_info: CodeSizeInfo
|
|
|
|
) -> None:
|
2021-09-09 11:42:37 +02:00
|
|
|
"""
|
2023-05-24 06:24:38 +02:00
|
|
|
old_revision: revision to compare against.
|
2021-09-09 11:42:37 +02:00
|
|
|
new_revision:
|
2023-05-24 06:24:38 +02:00
|
|
|
result_dir: directory for comparison result.
|
|
|
|
code_size_info: an object containing information to build library.
|
2021-09-09 11:42:37 +02:00
|
|
|
"""
|
2023-05-30 12:18:18 +02:00
|
|
|
super().__init__()
|
2021-09-09 11:42:37 +02:00
|
|
|
self.repo_path = "."
|
|
|
|
self.result_dir = os.path.abspath(result_dir)
|
2021-10-26 11:23:42 +02:00
|
|
|
os.makedirs(self.result_dir, exist_ok=True)
|
2021-09-09 11:42:37 +02:00
|
|
|
|
|
|
|
self.csv_dir = os.path.abspath("code_size_records/")
|
2021-10-26 11:23:42 +02:00
|
|
|
os.makedirs(self.csv_dir, exist_ok=True)
|
2021-09-09 11:42:37 +02:00
|
|
|
|
|
|
|
self.old_rev = old_revision
|
|
|
|
self.new_rev = new_revision
|
|
|
|
self.git_command = "git"
|
2023-07-04 10:49:04 +02:00
|
|
|
self.make_clean = 'make clean'
|
2023-05-24 06:24:38 +02:00
|
|
|
self.make_command = code_size_info.make_command
|
2023-05-24 11:13:29 +02:00
|
|
|
self.fname_suffix = "-" + code_size_info.arch + "-" +\
|
|
|
|
code_size_info.config
|
2021-09-09 11:42:37 +02:00
|
|
|
|
2021-10-21 14:22:58 +02:00
|
|
|
@staticmethod
|
2023-05-31 09:20:39 +02:00
|
|
|
def validate_revision(revision: str) -> bytes:
|
2021-11-03 08:12:31 +01:00
|
|
|
result = subprocess.check_output(["git", "rev-parse", "--verify",
|
|
|
|
revision + "^{commit}"], shell=False)
|
2021-10-26 11:23:42 +02:00
|
|
|
return result
|
2021-10-21 14:22:58 +02:00
|
|
|
|
2023-05-31 09:20:39 +02:00
|
|
|
def _create_git_worktree(self, revision: str) -> str:
|
2021-09-09 11:42:37 +02:00
|
|
|
"""Make a separate worktree for revision.
|
|
|
|
Do not modify the current worktree."""
|
|
|
|
|
2021-10-26 11:23:42 +02:00
|
|
|
if revision == "current":
|
2023-05-31 09:47:25 +02:00
|
|
|
print("Using current work directory")
|
2021-09-09 11:42:37 +02:00
|
|
|
git_worktree_path = self.repo_path
|
|
|
|
else:
|
|
|
|
print("Creating git worktree for", revision)
|
2021-10-26 11:23:42 +02:00
|
|
|
git_worktree_path = os.path.join(self.repo_path, "temp-" + revision)
|
2021-09-09 11:42:37 +02:00
|
|
|
subprocess.check_output(
|
|
|
|
[self.git_command, "worktree", "add", "--detach",
|
|
|
|
git_worktree_path, revision], cwd=self.repo_path,
|
|
|
|
stderr=subprocess.STDOUT
|
|
|
|
)
|
2023-04-13 17:32:21 +02:00
|
|
|
|
2021-09-09 11:42:37 +02:00
|
|
|
return git_worktree_path
|
|
|
|
|
2023-05-31 09:20:39 +02:00
|
|
|
def _build_libraries(self, git_worktree_path: str) -> None:
|
2021-09-09 11:42:37 +02:00
|
|
|
"""Build libraries in the specified worktree."""
|
|
|
|
|
|
|
|
my_environment = os.environ.copy()
|
2023-04-13 17:32:21 +02:00
|
|
|
try:
|
2023-07-04 10:49:04 +02:00
|
|
|
subprocess.check_output(
|
|
|
|
self.make_clean, env=my_environment, shell=True,
|
|
|
|
cwd=git_worktree_path, stderr=subprocess.STDOUT,
|
|
|
|
)
|
2023-04-13 17:32:21 +02:00
|
|
|
subprocess.check_output(
|
|
|
|
self.make_command, env=my_environment, shell=True,
|
|
|
|
cwd=git_worktree_path, stderr=subprocess.STDOUT,
|
|
|
|
)
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
self._handle_called_process_error(e, git_worktree_path)
|
2021-09-09 11:42:37 +02:00
|
|
|
|
2023-05-31 09:20:39 +02:00
|
|
|
def _gen_code_size_csv(self, revision: str, git_worktree_path: str) -> None:
|
2021-09-09 11:42:37 +02:00
|
|
|
"""Generate code size csv file."""
|
|
|
|
|
2021-10-26 11:23:42 +02:00
|
|
|
if revision == "current":
|
2023-05-31 09:47:25 +02:00
|
|
|
print("Measuring code size in current work directory")
|
2021-10-26 11:23:42 +02:00
|
|
|
else:
|
|
|
|
print("Measuring code size for", revision)
|
2023-05-30 12:18:18 +02:00
|
|
|
|
|
|
|
for mod, st_lib in MBEDTLS_STATIC_LIB.items():
|
|
|
|
try:
|
|
|
|
result = subprocess.check_output(
|
|
|
|
["size", st_lib, "-t"], cwd=git_worktree_path
|
|
|
|
)
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
self._handle_called_process_error(e, git_worktree_path)
|
|
|
|
size_text = result.decode("utf-8")
|
|
|
|
|
|
|
|
self.set_size_record(revision, mod, size_text)
|
|
|
|
|
|
|
|
print("Generating code size csv for", revision)
|
|
|
|
csv_file = open(os.path.join(self.csv_dir, revision +
|
|
|
|
self.fname_suffix + ".csv"), "w")
|
|
|
|
self.write_size_record(revision, csv_file)
|
2021-09-09 11:42:37 +02:00
|
|
|
|
2023-05-31 09:20:39 +02:00
|
|
|
def _remove_worktree(self, git_worktree_path: str) -> None:
|
2021-09-09 11:42:37 +02:00
|
|
|
"""Remove temporary worktree."""
|
|
|
|
if git_worktree_path != self.repo_path:
|
|
|
|
print("Removing temporary worktree", git_worktree_path)
|
|
|
|
subprocess.check_output(
|
|
|
|
[self.git_command, "worktree", "remove", "--force",
|
|
|
|
git_worktree_path], cwd=self.repo_path,
|
|
|
|
stderr=subprocess.STDOUT
|
|
|
|
)
|
|
|
|
|
2023-05-31 09:20:39 +02:00
|
|
|
def _get_code_size_for_rev(self, revision: str) -> None:
|
2021-09-09 11:42:37 +02:00
|
|
|
"""Generate code size csv file for the specified git revision."""
|
|
|
|
|
|
|
|
# Check if the corresponding record exists
|
2023-05-24 11:13:29 +02:00
|
|
|
csv_fname = revision + self.fname_suffix + ".csv"
|
2021-10-26 11:23:42 +02:00
|
|
|
if (revision != "current") and \
|
2021-09-09 11:42:37 +02:00
|
|
|
os.path.exists(os.path.join(self.csv_dir, csv_fname)):
|
|
|
|
print("Code size csv file for", revision, "already exists.")
|
2023-05-30 12:18:18 +02:00
|
|
|
self.read_size_record(revision, os.path.join(self.csv_dir, csv_fname))
|
2021-09-09 11:42:37 +02:00
|
|
|
else:
|
|
|
|
git_worktree_path = self._create_git_worktree(revision)
|
|
|
|
self._build_libraries(git_worktree_path)
|
|
|
|
self._gen_code_size_csv(revision, git_worktree_path)
|
|
|
|
self._remove_worktree(git_worktree_path)
|
|
|
|
|
2023-05-31 09:20:39 +02:00
|
|
|
def _gen_code_size_comparison(self) -> int:
|
2021-09-09 11:42:37 +02:00
|
|
|
"""Generate results of the size changes between two revisions,
|
|
|
|
old and new. Measured code size results of these two revisions
|
2021-10-21 14:22:58 +02:00
|
|
|
must be available."""
|
|
|
|
|
2023-05-24 11:13:29 +02:00
|
|
|
res_file = open(os.path.join(self.result_dir, "compare-" +
|
|
|
|
self.old_rev + "-" + self.new_rev +
|
|
|
|
self.fname_suffix +
|
|
|
|
".csv"), "w")
|
2021-10-26 11:23:42 +02:00
|
|
|
|
2023-05-31 09:47:25 +02:00
|
|
|
print("\nGenerating comparison results between",\
|
|
|
|
self.old_rev, "and", self.new_rev)
|
2023-05-30 12:18:18 +02:00
|
|
|
self.write_comparison(self.old_rev, self.new_rev, res_file)
|
2021-09-09 11:42:37 +02:00
|
|
|
|
2021-10-21 14:22:58 +02:00
|
|
|
return 0
|
2021-09-09 11:42:37 +02:00
|
|
|
|
2023-05-31 09:20:39 +02:00
|
|
|
def get_comparision_results(self) -> int:
|
2021-09-09 11:42:37 +02:00
|
|
|
"""Compare size of library/*.o between self.old_rev and self.new_rev,
|
|
|
|
and generate the result file."""
|
2022-09-18 21:17:09 +02:00
|
|
|
build_tree.check_repo_path()
|
2021-09-09 11:42:37 +02:00
|
|
|
self._get_code_size_for_rev(self.old_rev)
|
|
|
|
self._get_code_size_for_rev(self.new_rev)
|
2023-05-30 12:18:18 +02:00
|
|
|
return self._gen_code_size_comparison()
|
2021-09-09 11:42:37 +02:00
|
|
|
|
2023-04-13 17:32:21 +02:00
|
|
|
def _handle_called_process_error(self, e: subprocess.CalledProcessError,
|
2023-05-31 09:20:39 +02:00
|
|
|
git_worktree_path: str) -> None:
|
2023-04-13 17:32:21 +02:00
|
|
|
"""Handle a CalledProcessError and quit the program gracefully.
|
|
|
|
Remove any extra worktrees so that the script may be called again."""
|
|
|
|
|
|
|
|
# Tell the user what went wrong
|
|
|
|
print("The following command: {} failed and exited with code {}"
|
|
|
|
.format(e.cmd, e.returncode))
|
|
|
|
print("Process output:\n {}".format(str(e.output, "utf-8")))
|
|
|
|
|
|
|
|
# Quit gracefully by removing the existing worktree
|
|
|
|
self._remove_worktree(git_worktree_path)
|
|
|
|
sys.exit(-1)
|
|
|
|
|
2021-10-21 14:22:58 +02:00
|
|
|
def main():
|
2023-05-31 05:41:36 +02:00
|
|
|
parser = argparse.ArgumentParser(description=(__doc__))
|
|
|
|
group_required = parser.add_argument_group(
|
|
|
|
'required arguments',
|
|
|
|
'required arguments to parse for running ' + os.path.basename(__file__))
|
|
|
|
group_required.add_argument(
|
|
|
|
"-o", "--old-rev", type=str, required=True,
|
|
|
|
help="old revision for comparison.")
|
|
|
|
|
|
|
|
group_optional = parser.add_argument_group(
|
|
|
|
'optional arguments',
|
|
|
|
'optional arguments to parse for running ' + os.path.basename(__file__))
|
|
|
|
group_optional.add_argument(
|
2021-09-09 11:42:37 +02:00
|
|
|
"-r", "--result-dir", type=str, default="comparison",
|
|
|
|
help="directory where comparison result is stored, \
|
2023-05-31 05:41:36 +02:00
|
|
|
default is comparison")
|
|
|
|
group_optional.add_argument(
|
2021-10-26 11:23:42 +02:00
|
|
|
"-n", "--new-rev", type=str, default=None,
|
|
|
|
help="new revision for comparison, default is the current work \
|
2023-05-31 05:41:36 +02:00
|
|
|
directory, including uncommitted changes.")
|
|
|
|
group_optional.add_argument(
|
2023-05-24 05:03:59 +02:00
|
|
|
"-a", "--arch", type=str, default=detect_arch(),
|
|
|
|
choices=list(map(lambda s: s.value, SupportedArch)),
|
|
|
|
help="specify architecture for code size comparison, default is the\
|
2023-05-31 05:41:36 +02:00
|
|
|
host architecture.")
|
|
|
|
group_optional.add_argument(
|
2023-05-24 06:24:38 +02:00
|
|
|
"-c", "--config", type=str, default=SupportedConfig.DEFAULT.value,
|
|
|
|
choices=list(map(lambda s: s.value, SupportedConfig)),
|
|
|
|
help="specify configuration type for code size comparison,\
|
2023-05-31 05:41:36 +02:00
|
|
|
default is the current MbedTLS configuration.")
|
2021-09-09 11:42:37 +02:00
|
|
|
comp_args = parser.parse_args()
|
|
|
|
|
|
|
|
if os.path.isfile(comp_args.result_dir):
|
|
|
|
print("Error: {} is not a directory".format(comp_args.result_dir))
|
|
|
|
parser.exit()
|
|
|
|
|
2021-10-26 11:23:42 +02:00
|
|
|
validate_res = CodeSizeComparison.validate_revision(comp_args.old_rev)
|
2021-11-03 08:12:31 +01:00
|
|
|
old_revision = validate_res.decode().replace("\n", "")
|
2021-10-26 11:23:42 +02:00
|
|
|
|
|
|
|
if comp_args.new_rev is not None:
|
|
|
|
validate_res = CodeSizeComparison.validate_revision(comp_args.new_rev)
|
2021-11-03 08:12:31 +01:00
|
|
|
new_revision = validate_res.decode().replace("\n", "")
|
2021-10-26 11:23:42 +02:00
|
|
|
else:
|
|
|
|
new_revision = "current"
|
2021-10-21 14:22:58 +02:00
|
|
|
|
2023-06-01 05:29:06 +02:00
|
|
|
code_size_info = CodeSizeInfo(comp_args.arch, comp_args.config,
|
|
|
|
detect_arch())
|
2023-05-31 09:47:25 +02:00
|
|
|
print("Measure code size for architecture: {}, configuration: {}\n"
|
2023-05-29 10:45:56 +02:00
|
|
|
.format(code_size_info.arch, code_size_info.config))
|
2021-09-09 11:42:37 +02:00
|
|
|
result_dir = comp_args.result_dir
|
2023-05-24 06:24:38 +02:00
|
|
|
size_compare = CodeSizeComparison(old_revision, new_revision, result_dir,
|
|
|
|
code_size_info)
|
2021-09-09 11:42:37 +02:00
|
|
|
return_code = size_compare.get_comparision_results()
|
|
|
|
sys.exit(return_code)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-10-21 14:22:58 +02:00
|
|
|
main()
|