From ddb8c8ddfdcbafc1f353d9258949a5af1363daee Mon Sep 17 00:00:00 2001 From: "simon.chupin" Date: Tue, 28 Dec 2021 18:29:01 +0100 Subject: [PATCH] add test for checking memory segments with esptool.py --- tools/idf_size.py | 15 ++------- tools/test_idf_size/expected_output | 14 ++++++++ tools/test_idf_size/mem_test.py | 52 +++++++++++++++++++++++++++++ tools/test_idf_size/test.sh | 19 +++++++++-- 4 files changed, 85 insertions(+), 15 deletions(-) create mode 100644 tools/test_idf_size/mem_test.py diff --git a/tools/idf_size.py b/tools/idf_size.py index 2f34eae588..cde256c9b0 100755 --- a/tools/idf_size.py +++ b/tools/idf_size.py @@ -6,19 +6,8 @@ # Includes information which is not shown in "xtensa-esp32-elf-size", # or easy to parse from "xtensa-esp32-elf-objdump" or raw map files. # -# Copyright 2017-2021 Espressif Systems (Shanghai) CO LTD -# -# 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. +# SPDX-FileCopyrightText: 2017-2022 Espressif Systems (Shanghai) CO LTD +# SPDX-License-Identifier: Apache-2.0 # from __future__ import division, print_function, unicode_literals diff --git a/tools/test_idf_size/expected_output b/tools/test_idf_size/expected_output index e7c5f4ef6a..a04e5e0a1a 100644 --- a/tools/test_idf_size/expected_output +++ b/tools/test_idf_size/expected_output @@ -1,4 +1,18 @@ +*** +Building project for esp32... + +*** +Running mem_test.py for esp32... +Test complete without errors + +*** +Building project for esp32s2... + +*** +Running mem_test.py for esp32s2... +Test complete without errors + *** Running idf_size.py... Total sizes: diff --git a/tools/test_idf_size/mem_test.py b/tools/test_idf_size/mem_test.py new file mode 100644 index 0000000000..4bfaf0d5cd --- /dev/null +++ b/tools/test_idf_size/mem_test.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python +# +# SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD +# SPDX-License-Identifier: Apache-2.0 +# +import argparse +import json +import os +import re +from typing import Dict + +IDF_PATH = os.environ['IDF_PATH'] +MAX_SIZE_DIFF = 50 + + +def mem_test(size_json, esptool_output): # type: (dict, list) -> None + seg_len = {} # type: Dict[str, int] + for i in esptool_output: + tmp = i.split(' ') + if tmp[0] == 'Segment': + # tmp look like ['Segment', '2:', 'len', '0x02780', 'load', '0x3fc90610', 'file_offs', '0x00007ab0', '[BYTE_ACCESSIBLE,MEM_INTERNAL,DRAM]'] + # tmp[3] contains the size of the segment and tmp[8] contains the name of the memory segment + esptool_mem = {'mem_type':tmp[8], 'size':tmp[3]} + seg = re.sub(r'MEM_INTERNAL|,|BYTE_ACCESSIBLE|\n|\[|\]', '', esptool_mem['mem_type']) + # If there are two IRAMs in esptool output it will compute these two IRAM lengths in a seg_len['IRAM'] + seg_len[seg] = int(esptool_mem['size'], 16) if seg not in seg_len else seg_len[seg] + int(esptool_mem['size'], 16) + # including flash_other to DROM because flash_other contain .flash.appdesc that includes in DROM that produced by esptool + size_from_map = [('IROM', size_json['flash_code']), ('IRAM', size_json['iram_text'] + size_json['iram_vectors'] + size_json['diram_text'] + + size_json['diram_vectors']), ('DROM', size_json['flash_rodata'] + size_json['flash_other']), ('DRAM', size_json + ['dram_data'] + size_json['diram_data'])] + for mem_type, size in size_from_map: + if abs(size - seg_len[mem_type]) > MAX_SIZE_DIFF: + raise RuntimeError(mem_type + " segment in idf_size isn't correct regarding esptool") + print('Test complete without errors') + + +def main(): # type: () -> None + parser = argparse.ArgumentParser(description='mem_test.py - a tool to test accuracy of the sizes of the memory segments regarding idf.py size by esptool') + + parser.add_argument( + 'size_json', help='JSON file with the output of the idf.py size', + type=argparse.FileType('r')) + parser.add_argument( + 'esptool_output', help='File with the output of the esptool', + type=argparse.FileType('r')) + + args = parser.parse_args() + mem_test(json.loads(args.size_json.read()), args.esptool_output.read().split('\n')) + + +if __name__ == '__main__': + main() diff --git a/tools/test_idf_size/test.sh b/tools/test_idf_size/test.sh index 0f174d7868..4038b0e3bb 100755 --- a/tools/test_idf_size/test.sh +++ b/tools/test_idf_size/test.sh @@ -1,7 +1,22 @@ #!/usr/bin/env bash -{ coverage debug sys \ - && coverage erase &> output \ + +memory_test () { + pushd $IDF_PATH/examples/get-started/hello_world \ + && echo -e "\n***\nBuilding project for $1..." &>> $IDF_PATH/tools/test_idf_size/output \ + && idf.py set-target $1 \ + && idf.py build \ + && echo -e "\n***\nRunning mem_test.py for $1..." &>> $IDF_PATH/tools/test_idf_size/output \ + && python -m coverage run -a $IDF_PATH/tools/idf_size.py --json build/hello-world.map > size_output.json \ + && python $IDF_PATH/components/esptool_py/esptool/esptool.py --chip $1 image_info build/hello-world.bin > esptool_output \ + && python -m coverage run -a $IDF_PATH/tools/test_idf_size/mem_test.py size_output.json esptool_output &>> $IDF_PATH/tools/test_idf_size/output \ + && popd +} + +{ python -m coverage debug sys \ + && python -m coverage erase &> output \ + && memory_test esp32 \ + && memory_test esp32s2 \ && echo -e "\n***\nRunning idf_size.py..." &>> output \ && coverage run -a $IDF_PATH/tools/idf_size.py app.map &>> output \ && echo -e "\n***\nRunning idf_size.py on bootloader..." &>> output \