blob: 44b193d3b13752c807eb2130334420865aee3e9b [file] [log] [blame]
Hugo L'Hostise55a2752021-01-27 11:09:08 +00001#!/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 Zhang9a29f032022-01-19 15:13:24 +08006#Copyright (c) 2020-2022, Arm Limited. All rights reserved.
Hugo L'Hostise55a2752021-01-27 11:09:08 +00007#
8#SPDX-License-Identifier: BSD-3-Clause
9
10
11import argparse
12import os
13import re
14import sys
15import json
16import requests
17import subprocess
18import configs
19from tfm_ci_pylib import utils
20
21# Arguments/parameters given by CI
22PATH_TO_TFM = sys.argv[1]
23CI_CONFIG = sys.argv[2]
24REFERENCE_CONFIGS = sys.argv[3].split(",")
25SQUAD_TOKEN = sys.argv[4]
26
27# local constant
28SQUAD_BASE_PROJECT_URL = ("https://qa-reports.linaro.org/api/submit/tf/tf-m/")
29
30# This function uses arm_non_eabi_size to get the sizes of a file
31# in the build directory of tfm
32def get_file_size(filename):
33 f_path = os.path.join(PATH_TO_TFM, "build", "bin", filename)
34 if os.path.exists(f_path) :
Hugo L'Hostis26dc2962021-04-22 15:44:48 +010035 file_sizes = utils.arm_non_eabi_size(f_path)[0]
36 return file_sizes
Hugo L'Hostise55a2752021-01-27 11:09:08 +000037 else :
38 print(f_path + "Not found")
39 return -1
40
41# This function creates a json file containing all the data about
42# memory footprint and sends this data to SQUAD
43def send_file_size(change_id, config_name, bl2_sizes, tfms_sizes):
44 url = SQUAD_BASE_PROJECT_URL + change_id + '/' + config_name
45
46 try:
47 metrics = json.dumps({ "bl2_size" : bl2_sizes["dec"],
48 "bl2_data" : bl2_sizes["data"],
49 "bl2_bss" : bl2_sizes["bss"],
Hugo L'Hostis26dc2962021-04-22 15:44:48 +010050 "bl2_text" : bl2_sizes["text"],
Hugo L'Hostise55a2752021-01-27 11:09:08 +000051 "tfms_size" : tfms_sizes["dec"],
52 "tfms_data" : tfms_sizes["data"],
Hugo L'Hostis26dc2962021-04-22 15:44:48 +010053 "tfms_bss" : tfms_sizes["bss"],
54 "tfms_text" : tfms_sizes["text"]})
Hugo L'Hostise55a2752021-01-27 11:09:08 +000055 except:
56 return -1
57
58 headers = {"Auth-Token": SQUAD_TOKEN}
59 data= {"metrics": metrics}
60
61 try:
62 #Sending the data to SQUAD, 40s timeout
63 result = requests.post(url, headers=headers, data=data, timeout=40)
64 except:
65 return -1
66
67 with open(os.path.join(PATH_TO_TFM,
68 "..",
69 "tf-m-ci-scripts",
70 "Memory_footprint",
71 "filesize.json"), "w") as F:
72 #Storing the json file
73 F.write(metrics)
74
75 if not result.ok:
76 print(f"Error submitting to qa-reports: {result.reason}: {result.text}")
77 return -1
78 else :
79 print ("POST request sent to project " + config_name )
80 return 0
81
82#Function used to launch the configs.py script and get the printed output
83def get_configs_by_name(config_names):
84 clist = list(configs._builtin_configs.keys())
85 out_cfg = {}
86 for group in clist:
87 build_manager = configs.get_build_manager(group)
88 for _cfg_name in config_names:
89 if _cfg_name in build_manager._tbm_build_cfg.keys():
90 out_cfg[_cfg_name] = build_manager._tbm_build_cfg[_cfg_name]
91 return out_cfg
92
93# This funcion manipulates the format of the config given
94# as entry to extract the name of the configuration used
95def identify_config():
96 name_config = "Unknown"
97
98 try :
99 cfg = get_configs_by_name([CI_CONFIG])[CI_CONFIG]
Xinyu Zhang73ed2992021-09-15 11:38:23 +0800100 if (not cfg.lib_model and cfg.isolation_level == "1" and
Hugo L'Hostise55a2752021-01-27 11:09:08 +0000101 not cfg.test_regression and cfg.test_psa_api == "OFF" and
Xinyu Zhang589fd052022-04-19 17:54:16 +0800102 cfg.cmake_build_type == "Release" and
Hugo L'Hostise55a2752021-01-27 11:09:08 +0000103 cfg.with_bl2 and cfg.with_ns and
104 cfg.profile == "" and cfg.partition_ps == "ON"):
105 name_config = "CoreIPC"
Xinyu Zhang73ed2992021-09-15 11:38:23 +0800106 elif (cfg.lib_model and cfg.isolation_level == "1" and
Hugo L'Hostise55a2752021-01-27 11:09:08 +0000107 not cfg.test_regression and cfg.test_psa_api == "OFF" and
Xinyu Zhang589fd052022-04-19 17:54:16 +0800108 cfg.cmake_build_type == "Release" and
Hugo L'Hostise55a2752021-01-27 11:09:08 +0000109 cfg.with_bl2 and cfg.with_ns and
110 cfg.profile == "" and cfg.partition_ps == "ON"):
111 name_config = "Default"
Xinyu Zhang73ed2992021-09-15 11:38:23 +0800112 elif (not cfg.lib_model and cfg.isolation_level == "2" and
Hugo L'Hostise55a2752021-01-27 11:09:08 +0000113 not cfg.test_regression and cfg.test_psa_api == "OFF" and
Xinyu Zhang589fd052022-04-19 17:54:16 +0800114 cfg.cmake_build_type == "Release" and
Hugo L'Hostise55a2752021-01-27 11:09:08 +0000115 cfg.with_bl2 and cfg.with_ns and
116 cfg.profile == "" and cfg.partition_ps == "ON"):
117 name_config = "CoreIPCTfmLevel2"
Xinyu Zhang73ed2992021-09-15 11:38:23 +0800118 elif (cfg.lib_model and cfg.isolation_level == "1" and
Hugo L'Hostise55a2752021-01-27 11:09:08 +0000119 not cfg.test_regression and cfg.test_psa_api == "OFF" and
Xinyu Zhang589fd052022-04-19 17:54:16 +0800120 cfg.cmake_build_type == "Release" and
Hugo L'Hostise55a2752021-01-27 11:09:08 +0000121 cfg.with_bl2 and cfg.with_ns and
122 cfg.profile == "profile_small" and cfg.partition_ps == "OFF"):
123 name_config = "DefaultProfileS"
Xinyu Zhang73ed2992021-09-15 11:38:23 +0800124 elif (cfg.lib_model and cfg.isolation_level == "1" and
Hugo L'Hostisc99cd2b2021-04-12 11:48:49 +0100125 not cfg.test_regression and cfg.test_psa_api == "OFF" and
Xinyu Zhang589fd052022-04-19 17:54:16 +0800126 cfg.cmake_build_type == "Minsizerel" and
Hugo L'Hostisc99cd2b2021-04-12 11:48:49 +0100127 cfg.with_bl2 and cfg.with_ns and
128 cfg.profile == "profile_small" and cfg.partition_ps == "OFF"):
129 name_config = "MinSizeProfileS"
Xinyu Zhang73ed2992021-09-15 11:38:23 +0800130 elif (not cfg.lib_model and cfg.isolation_level == "2" and
Hugo L'Hostise55a2752021-01-27 11:09:08 +0000131 not cfg.test_regression and cfg.test_psa_api == "OFF" and
Xinyu Zhang589fd052022-04-19 17:54:16 +0800132 cfg.cmake_build_type == "Release" and
Hugo L'Hostise55a2752021-01-27 11:09:08 +0000133 cfg.with_bl2 and cfg.with_ns and
134 cfg.profile == "profile_medium" and cfg.partition_ps == "ON"):
135 name_config = "DefaultProfileM"
Xinyu Zhang73ed2992021-09-15 11:38:23 +0800136 elif (not cfg.lib_model and cfg.isolation_level == "3" and
Hugo L'Hostis26dc2962021-04-22 15:44:48 +0100137 not cfg.test_regression and cfg.test_psa_api == "OFF" and
Xinyu Zhang589fd052022-04-19 17:54:16 +0800138 cfg.cmake_build_type == "Release" and
Hugo L'Hostis26dc2962021-04-22 15:44:48 +0100139 cfg.with_bl2 and cfg.with_ns and
140 cfg.profile == "profile_large" and cfg.partition_ps == "ON"):
141 name_config = "DefaultProfileL"
Xinyu Zhang433771e2022-04-01 16:49:17 +0800142 ret = [cfg.tfm_platform,cfg.compiler, name_config]
Hugo L'Hostise55a2752021-01-27 11:09:08 +0000143 except:
144 ret = ["Unknown", "Unknown", "Unknown"]
145 return ret
146
147# Function based on get_local_git_info() from utils, getting change id for the tfm repo
148def get_change_id(directory):
149 directory = os.path.abspath(directory)
150 cur_dir = os.path.abspath(os.getcwd())
151 cmd = "git log HEAD -n 1 --pretty=format:'%b'"
152
153 os.chdir(directory) # Going to the repo's directory
154
155 git_info_rex = re.compile(r'(?P<body>^[\s\S]*?)((?:Change-Id:\s)'
156 r'(?P<change_id>.*)\n?)', re.MULTILINE)
157
158 r, e = subprocess.Popen(cmd,
159 shell=True,
160 stdout=subprocess.PIPE,
161 stderr=subprocess.PIPE).communicate()
162
163 if e:
164 print("Error", e)
165 return -1
166 else:
167 try:
168 txt_body = r.decode('ascii')
169 except UnicodeDecodeError as E:
170 txt_body = r.decode('utf-8')
171 result = txt_body.rstrip()
172
173 try:
174 change_id = git_info_rex.search(result).groupdict()["change_id"].strip()
175 except:
176 return -1
177
178 os.chdir(cur_dir) #Going back to the initial directory
179 return change_id
180
181if __name__ == "__main__":
Xinyu Zhang9a29f032022-01-19 15:13:24 +0800182 # Export GCC v7.3.1 to ENV PATH
183 os.environ["PATH"] += os.pathsep + os.getenv('GCC_7_3_1_PATH')
Hugo L'Hostise55a2752021-01-27 11:09:08 +0000184 for i in range(len(REFERENCE_CONFIGS)):
185 REFERENCE_CONFIGS[i] = REFERENCE_CONFIGS[i].strip().lower()
186 config = identify_config()
187 if (config[2].lower() in REFERENCE_CONFIGS
Summer Qin3c2b5722021-05-26 10:43:45 +0800188 and config[0] == "arm/mps2/an521"
Arthur Shed6fd4a02022-06-20 18:55:44 +0800189 and config[1] == "GCC_7_3_1"):
Hugo L'Hostise55a2752021-01-27 11:09:08 +0000190 # Pushing data for AN521 and GNUARM
191 print("Configuration " + config[2] + " is a reference")
192 try :
193 change_id = get_change_id(PATH_TO_TFM)
194 except :
195 change_id = -1
196 bl2_sizes = get_file_size("bl2.axf")
197 tfms_sizes = get_file_size("tfm_s.axf")
198 if (bl2_sizes != -1 and change_id != -1) :
199 send_file_size(change_id, config[2], bl2_sizes, tfms_sizes)
200 else :
201 #Directory or file weren't found
202 if change_id == -1 :
203 print("Error : trusted-firmware-m repo not found")
204 else :
205 print("Error : file not found")