Skip to content

Commit

Permalink
Add memory to results
Browse files Browse the repository at this point in the history
  • Loading branch information
pkoscik committed Jan 3, 2024
1 parent c206a11 commit 6a4e19e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
46 changes: 46 additions & 0 deletions scripts/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
get_versions,
get_yaml_data,
calculate_md5,
conv_zephyr_mem_usage,
)

templateLoader = jinja2.FileSystemLoader(searchpath="./")
Expand Down Expand Up @@ -73,6 +74,7 @@ def __init__(self, platform: str, sample_path: str, sample_name: str) -> None:
}

_MEMORY_EXTENSION_REGEX = r"region `(\S+)' overflowed by (\d+) bytes"
_MEMORY_USAGE_REGEX = r"(?P<region>\w+){1}:\s*(?P<used>\d+\s+\w{1,2})\s*(?P<size>\d+\s+\w{1,2})\s*(?P<percentage>\d+.\d+%)"
_ARCH_ERROR_REGEX = r"Arch .*? not supported"

def __del__(self):
Expand Down Expand Up @@ -151,6 +153,49 @@ def get_artifacts(self) -> dict:

return artifacts

def get_memory_usage(self) -> dict | None:
"""
Get memory usage (in bytes) from the Zephyr log file.
The results are returned as a dict:
{'MEM': {'used': 100, 'size': 200 }, ...}
SIZE is the original node size.
If memory node was extended, USED is going to exceed SIZE.
"""
if not self.success:
return None

memory = {}
with open(self.log_file) as f:
match = re.findall(self._MEMORY_USAGE_REGEX, f.read())

# Get memory usage statistics
for m in match:
region, used, size, _ = m
memory[region] = {
'used': conv_zephyr_mem_usage(used),
'size': conv_zephyr_mem_usage(size),
}

# Check if flash size was increased
if "memory" in self.overlays:
if 'FLASH' in memory:
_, flash_size = find_node_size('flash', self.dts_original)
flash_size = int(flash_size[-1], 16)
memory['FLASH'].update({
'size': flash_size,
})

if 'RAM' in memory:
_, ram_size = find_node_size('sram', self.dts_original)
ram_size = int(ram_size[-1], 16)
memory['RAM'].update({
'size': ram_size,
})

return memory

def _copy_original_dts_file(self, dts_original_path: str) -> None:
"""
Checks if the DTS file was modified. If not, preserves the original file.
Expand Down Expand Up @@ -459,6 +504,7 @@ def main(board_dir: str, board_name: str, sample_name: str) -> None:
"arch": arch,
"platform_full_name": platform_full_name,
"board_dir": board_dir,
"memory": run.get_memory_usage(),
}

info = "Success!" if run.success else "Fail!"
Expand Down
22 changes: 22 additions & 0 deletions scripts/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,25 @@ def get_yaml_data(yaml_filename: str):
"""
with open(yaml_filename) as f:
return yaml.load(f, Loader=yaml.SafeLoader)


def conv_zephyr_mem_usage(val: str) -> int:
"""
Convert Zephyr memory usage value to bytes.
Args:
val (str): Memory usage value with unit (B, KB, MB, GB).
Returns:
int: Memory usage in bytes.
"""
if val.endswith(' B'):
val = int(val[:-2])
elif val.endswith(' KB'):
val = int(val[:-2]) * 1024
elif val.endswith(' MB'):
val = int(val[:-2]) * 1024 ** 2
elif val.endswith(' GB'):
val = int(val[:-2]) * 1024 ** 3

return val

0 comments on commit 6a4e19e

Please sign in to comment.