Hugo L'Hostis | e55a275 | 2021-01-27 11:09:08 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | #memory_footprint.py : Script for sending memory footprint data from the TFM CI |
| 4 | #to a SQUAD web interface |
| 5 | # |
Xinyu Zhang | 9a29f03 | 2022-01-19 15:13:24 +0800 | [diff] [blame] | 6 | #Copyright (c) 2020-2022, Arm Limited. All rights reserved. |
Hugo L'Hostis | e55a275 | 2021-01-27 11:09:08 +0000 | [diff] [blame] | 7 | # |
| 8 | #SPDX-License-Identifier: BSD-3-Clause |
| 9 | |
| 10 | |
Hugo L'Hostis | e55a275 | 2021-01-27 11:09:08 +0000 | [diff] [blame] | 11 | import os |
| 12 | import re |
| 13 | import sys |
| 14 | import json |
| 15 | import requests |
| 16 | import subprocess |
Hugo L'Hostis | e55a275 | 2021-01-27 11:09:08 +0000 | [diff] [blame] | 17 | from tfm_ci_pylib import utils |
| 18 | |
Xinyu Zhang | 470b3c4 | 2022-09-19 14:41:45 +0800 | [diff] [blame] | 19 | # SQAUD constant |
| 20 | SQUAD_TOKEN = sys.argv[1] |
| 21 | SQUAD_BASE_PROJECT_URL = ("https://qa-reports.linaro.org/api/submit/tf/tf-m/") |
Hugo L'Hostis | e55a275 | 2021-01-27 11:09:08 +0000 | [diff] [blame] | 22 | |
Xinyu Zhang | 470b3c4 | 2022-09-19 14:41:45 +0800 | [diff] [blame] | 23 | reference_configs = [ |
Xinyu Zhang | 97a47cb | 2022-10-21 11:13:01 +0800 | [diff] [blame] | 24 | 'AN521_ARMCLANG_1_Minsizerel_BL2', |
| 25 | 'AN521_ARMCLANG_1_Minsizerel_BL2_SMALL_PSOFF', |
| 26 | 'AN521_ARMCLANG_2_Minsizerel_BL2_MEDIUM_PSOFF', |
| 27 | 'AN521_ARMCLANG_3_Minsizerel_BL2_LARGE_PSOFF' |
Xinyu Zhang | 470b3c4 | 2022-09-19 14:41:45 +0800 | [diff] [blame] | 28 | ] |
Hugo L'Hostis | e55a275 | 2021-01-27 11:09:08 +0000 | [diff] [blame] | 29 | |
| 30 | # This function uses arm_non_eabi_size to get the sizes of a file |
| 31 | # in the build directory of tfm |
| 32 | def get_file_size(filename): |
Xinyu Zhang | 470b3c4 | 2022-09-19 14:41:45 +0800 | [diff] [blame] | 33 | f_path = os.path.join(os.getenv('WORKSPACE'), "trusted-firmware-m", "build", "bin", filename) |
Hugo L'Hostis | e55a275 | 2021-01-27 11:09:08 +0000 | [diff] [blame] | 34 | if os.path.exists(f_path) : |
Xinyu Zhang | 97a47cb | 2022-10-21 11:13:01 +0800 | [diff] [blame] | 35 | data_fromelf = utils.fromelf(f_path) |
| 36 | print(data_fromelf[1]) # Output of fromelf |
| 37 | return data_fromelf[0] # Data parsed from output of fromelf |
Hugo L'Hostis | e55a275 | 2021-01-27 11:09:08 +0000 | [diff] [blame] | 38 | else : |
| 39 | print(f_path + "Not found") |
| 40 | return -1 |
| 41 | |
| 42 | # This function creates a json file containing all the data about |
| 43 | # memory footprint and sends this data to SQUAD |
Jianliang Shen | 0474dbe | 2023-09-14 10:53:44 +0800 | [diff] [blame^] | 44 | def send_file_size(git_commit, config_name, bl2_sizes, tfms_sizes): |
| 45 | url = SQUAD_BASE_PROJECT_URL + git_commit + '/' + config_name |
Hugo L'Hostis | e55a275 | 2021-01-27 11:09:08 +0000 | [diff] [blame] | 46 | |
| 47 | try: |
Xinyu Zhang | 97a47cb | 2022-10-21 11:13:01 +0800 | [diff] [blame] | 48 | metrics = json.dumps({ "bl2_code_size" : bl2_sizes["Code"], |
| 49 | "bl2_inline_data" : bl2_sizes["Inline Data"], |
| 50 | "bl2_ro_data" : bl2_sizes["RO Data"], |
| 51 | "bl2_rw_data" : bl2_sizes["RW Data"], |
| 52 | "bl2_zi_data" : bl2_sizes["ZI Data"], |
Xinyu Zhang | e189746 | 2022-10-25 10:20:23 +0800 | [diff] [blame] | 53 | "spe_code_size" : tfms_sizes["Code"], |
| 54 | "spe_inline_data" : tfms_sizes["Inline Data"], |
| 55 | "spe_ro_data" : tfms_sizes["RO Data"], |
| 56 | "spe_rw_data" : tfms_sizes["RW Data"], |
| 57 | "spe_zi_data" : tfms_sizes["ZI Data"]}) |
Hugo L'Hostis | e55a275 | 2021-01-27 11:09:08 +0000 | [diff] [blame] | 58 | except: |
| 59 | return -1 |
| 60 | |
| 61 | headers = {"Auth-Token": SQUAD_TOKEN} |
| 62 | data= {"metrics": metrics} |
| 63 | |
| 64 | try: |
| 65 | #Sending the data to SQUAD, 40s timeout |
| 66 | result = requests.post(url, headers=headers, data=data, timeout=40) |
| 67 | except: |
| 68 | return -1 |
| 69 | |
Xinyu Zhang | 470b3c4 | 2022-09-19 14:41:45 +0800 | [diff] [blame] | 70 | with open(os.path.join(os.getenv('WORKSPACE'), |
Hugo L'Hostis | e55a275 | 2021-01-27 11:09:08 +0000 | [diff] [blame] | 71 | "tf-m-ci-scripts", |
| 72 | "Memory_footprint", |
| 73 | "filesize.json"), "w") as F: |
| 74 | #Storing the json file |
| 75 | F.write(metrics) |
| 76 | |
| 77 | if not result.ok: |
| 78 | print(f"Error submitting to qa-reports: {result.reason}: {result.text}") |
| 79 | return -1 |
| 80 | else : |
| 81 | print ("POST request sent to project " + config_name ) |
| 82 | return 0 |
| 83 | |
Jianliang Shen | 0474dbe | 2023-09-14 10:53:44 +0800 | [diff] [blame^] | 84 | def get_git_commit_hash(repo='trusted-firmware-m'): |
Hugo L'Hostis | e55a275 | 2021-01-27 11:09:08 +0000 | [diff] [blame] | 85 | cur_dir = os.path.abspath(os.getcwd()) |
Hugo L'Hostis | e55a275 | 2021-01-27 11:09:08 +0000 | [diff] [blame] | 86 | |
Jianliang Shen | 0474dbe | 2023-09-14 10:53:44 +0800 | [diff] [blame^] | 87 | os.chdir(os.path.join(os.getenv('WORKSPACE'), repo)) # Going to the repo's directory |
| 88 | git_commit = os.popen('git rev-parse --short HEAD').read()[:-1] |
| 89 | os.chdir(cur_dir) # Going back to the initial directory |
Hugo L'Hostis | e55a275 | 2021-01-27 11:09:08 +0000 | [diff] [blame] | 90 | |
Jianliang Shen | 0474dbe | 2023-09-14 10:53:44 +0800 | [diff] [blame^] | 91 | return git_commit |
Hugo L'Hostis | e55a275 | 2021-01-27 11:09:08 +0000 | [diff] [blame] | 92 | |
Xinyu Zhang | 470b3c4 | 2022-09-19 14:41:45 +0800 | [diff] [blame] | 93 | def print_image_sizes(image_sizes): |
| 94 | for sec, size in image_sizes.items(): |
| 95 | print("{:4}: {}".format(sec, size)) |
| 96 | |
Hugo L'Hostis | e55a275 | 2021-01-27 11:09:08 +0000 | [diff] [blame] | 97 | if __name__ == "__main__": |
Xinyu Zhang | 97a47cb | 2022-10-21 11:13:01 +0800 | [diff] [blame] | 98 | # Export ARMClang v6.13 to ENV PATH |
| 99 | os.environ["PATH"] += os.pathsep + os.getenv('ARMCLANG_6_13_PATH') |
| 100 | if os.getenv('CONFIG_NAME') in reference_configs: |
Xinyu Zhang | 470b3c4 | 2022-09-19 14:41:45 +0800 | [diff] [blame] | 101 | print("Configuration " + os.getenv('CONFIG_NAME') + " is a reference") |
Hugo L'Hostis | e55a275 | 2021-01-27 11:09:08 +0000 | [diff] [blame] | 102 | try : |
Jianliang Shen | 0474dbe | 2023-09-14 10:53:44 +0800 | [diff] [blame^] | 103 | git_commit = get_git_commit_hash("trusted-firmware-m") |
Hugo L'Hostis | e55a275 | 2021-01-27 11:09:08 +0000 | [diff] [blame] | 104 | except : |
Jianliang Shen | 0474dbe | 2023-09-14 10:53:44 +0800 | [diff] [blame^] | 105 | git_commit = -1 |
Xinyu Zhang | 470b3c4 | 2022-09-19 14:41:45 +0800 | [diff] [blame] | 106 | |
Xinyu Zhang | 470b3c4 | 2022-09-19 14:41:45 +0800 | [diff] [blame] | 107 | print("------ BL2 Memory Footprint ------") |
Xinyu Zhang | 97a47cb | 2022-10-21 11:13:01 +0800 | [diff] [blame] | 108 | bl2_sizes = get_file_size("bl2.axf") |
| 109 | print("\n\n------ TFM Secure Memory Footprint ------") |
Hugo L'Hostis | e55a275 | 2021-01-27 11:09:08 +0000 | [diff] [blame] | 110 | tfms_sizes = get_file_size("tfm_s.axf") |
Xinyu Zhang | 470b3c4 | 2022-09-19 14:41:45 +0800 | [diff] [blame] | 111 | |
Jianliang Shen | 0474dbe | 2023-09-14 10:53:44 +0800 | [diff] [blame^] | 112 | if (bl2_sizes != -1 and git_commit != -1) : |
Xinyu Zhang | 97a47cb | 2022-10-21 11:13:01 +0800 | [diff] [blame] | 113 | squad_config_name = os.getenv('CONFIG_NAME') |
Jianliang Shen | 0474dbe | 2023-09-14 10:53:44 +0800 | [diff] [blame^] | 114 | send_file_size(git_commit, squad_config_name, bl2_sizes, tfms_sizes) |
Hugo L'Hostis | e55a275 | 2021-01-27 11:09:08 +0000 | [diff] [blame] | 115 | else : |
| 116 | #Directory or file weren't found |
Jianliang Shen | 0474dbe | 2023-09-14 10:53:44 +0800 | [diff] [blame^] | 117 | if git_commit == -1 : |
Hugo L'Hostis | e55a275 | 2021-01-27 11:09:08 +0000 | [diff] [blame] | 118 | print("Error : trusted-firmware-m repo not found") |
| 119 | else : |
| 120 | print("Error : file not found") |