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 |
| 44 | def send_file_size(change_id, config_name, bl2_sizes, tfms_sizes): |
| 45 | url = SQUAD_BASE_PROJECT_URL + change_id + '/' + config_name |
| 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 | |
Hugo L'Hostis | e55a275 | 2021-01-27 11:09:08 +0000 | [diff] [blame] | 84 | # Function based on get_local_git_info() from utils, getting change id for the tfm repo |
Xinyu Zhang | 470b3c4 | 2022-09-19 14:41:45 +0800 | [diff] [blame] | 85 | def get_change_id(repo='trusted-firmware-m'): |
| 86 | directory = os.path.join(os.getenv('WORKSPACE'), repo) |
Hugo L'Hostis | e55a275 | 2021-01-27 11:09:08 +0000 | [diff] [blame] | 87 | cur_dir = os.path.abspath(os.getcwd()) |
| 88 | cmd = "git log HEAD -n 1 --pretty=format:'%b'" |
| 89 | |
| 90 | os.chdir(directory) # Going to the repo's directory |
| 91 | |
| 92 | git_info_rex = re.compile(r'(?P<body>^[\s\S]*?)((?:Change-Id:\s)' |
| 93 | r'(?P<change_id>.*)\n?)', re.MULTILINE) |
| 94 | |
| 95 | r, e = subprocess.Popen(cmd, |
| 96 | shell=True, |
| 97 | stdout=subprocess.PIPE, |
| 98 | stderr=subprocess.PIPE).communicate() |
| 99 | |
| 100 | if e: |
| 101 | print("Error", e) |
| 102 | return -1 |
| 103 | else: |
| 104 | try: |
| 105 | txt_body = r.decode('ascii') |
| 106 | except UnicodeDecodeError as E: |
| 107 | txt_body = r.decode('utf-8') |
| 108 | result = txt_body.rstrip() |
| 109 | |
| 110 | try: |
| 111 | change_id = git_info_rex.search(result).groupdict()["change_id"].strip() |
| 112 | except: |
| 113 | return -1 |
| 114 | |
| 115 | os.chdir(cur_dir) #Going back to the initial directory |
| 116 | return change_id |
| 117 | |
Xinyu Zhang | 470b3c4 | 2022-09-19 14:41:45 +0800 | [diff] [blame] | 118 | def print_image_sizes(image_sizes): |
| 119 | for sec, size in image_sizes.items(): |
| 120 | print("{:4}: {}".format(sec, size)) |
| 121 | |
Hugo L'Hostis | e55a275 | 2021-01-27 11:09:08 +0000 | [diff] [blame] | 122 | if __name__ == "__main__": |
Xinyu Zhang | 97a47cb | 2022-10-21 11:13:01 +0800 | [diff] [blame] | 123 | # Export ARMClang v6.13 to ENV PATH |
Xinyu Zhang | 11b792a | 2023-10-27 17:36:40 +0800 | [diff] [blame] | 124 | os.environ["PATH"] += os.pathsep + os.getenv('ARMCLANG_6_18_PATH') |
Xinyu Zhang | 97a47cb | 2022-10-21 11:13:01 +0800 | [diff] [blame] | 125 | if os.getenv('CONFIG_NAME') in reference_configs: |
Xinyu Zhang | 470b3c4 | 2022-09-19 14:41:45 +0800 | [diff] [blame] | 126 | print("Configuration " + os.getenv('CONFIG_NAME') + " is a reference") |
Hugo L'Hostis | e55a275 | 2021-01-27 11:09:08 +0000 | [diff] [blame] | 127 | try : |
Xinyu Zhang | 470b3c4 | 2022-09-19 14:41:45 +0800 | [diff] [blame] | 128 | change_id = get_change_id("trusted-firmware-m") |
Hugo L'Hostis | e55a275 | 2021-01-27 11:09:08 +0000 | [diff] [blame] | 129 | except : |
| 130 | change_id = -1 |
Xinyu Zhang | 470b3c4 | 2022-09-19 14:41:45 +0800 | [diff] [blame] | 131 | |
Xinyu Zhang | 470b3c4 | 2022-09-19 14:41:45 +0800 | [diff] [blame] | 132 | print("------ BL2 Memory Footprint ------") |
Xinyu Zhang | 97a47cb | 2022-10-21 11:13:01 +0800 | [diff] [blame] | 133 | bl2_sizes = get_file_size("bl2.axf") |
| 134 | print("\n\n------ TFM Secure Memory Footprint ------") |
Hugo L'Hostis | e55a275 | 2021-01-27 11:09:08 +0000 | [diff] [blame] | 135 | tfms_sizes = get_file_size("tfm_s.axf") |
Xinyu Zhang | 470b3c4 | 2022-09-19 14:41:45 +0800 | [diff] [blame] | 136 | |
Hugo L'Hostis | e55a275 | 2021-01-27 11:09:08 +0000 | [diff] [blame] | 137 | if (bl2_sizes != -1 and change_id != -1) : |
Xinyu Zhang | 97a47cb | 2022-10-21 11:13:01 +0800 | [diff] [blame] | 138 | squad_config_name = os.getenv('CONFIG_NAME') |
Xinyu Zhang | 470b3c4 | 2022-09-19 14:41:45 +0800 | [diff] [blame] | 139 | send_file_size(change_id, squad_config_name, bl2_sizes, tfms_sizes) |
Hugo L'Hostis | e55a275 | 2021-01-27 11:09:08 +0000 | [diff] [blame] | 140 | else : |
| 141 | #Directory or file weren't found |
| 142 | if change_id == -1 : |
| 143 | print("Error : trusted-firmware-m repo not found") |
| 144 | else : |
| 145 | print("Error : file not found") |