blob: 801c0bc634fdf7b3d5bf8e4e154e3b8e90a0dd1b [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
Hugo L'Hostise55a2752021-01-27 11:09:08 +000011import os
12import re
13import sys
14import json
15import requests
16import subprocess
Hugo L'Hostise55a2752021-01-27 11:09:08 +000017from tfm_ci_pylib import utils
18
Xinyu Zhang470b3c42022-09-19 14:41:45 +080019# SQAUD constant
20SQUAD_TOKEN = sys.argv[1]
21SQUAD_BASE_PROJECT_URL = ("https://qa-reports.linaro.org/api/submit/tf/tf-m/")
Hugo L'Hostise55a2752021-01-27 11:09:08 +000022
Xinyu Zhang470b3c42022-09-19 14:41:45 +080023reference_configs = [
Xinyu Zhang97a47cb2022-10-21 11:13:01 +080024 '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 Zhang470b3c42022-09-19 14:41:45 +080028]
Hugo L'Hostise55a2752021-01-27 11:09:08 +000029
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):
Xinyu Zhang470b3c42022-09-19 14:41:45 +080033 f_path = os.path.join(os.getenv('WORKSPACE'), "trusted-firmware-m", "build", "bin", filename)
Hugo L'Hostise55a2752021-01-27 11:09:08 +000034 if os.path.exists(f_path) :
Xinyu Zhang97a47cb2022-10-21 11:13:01 +080035 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'Hostise55a2752021-01-27 11:09:08 +000038 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
44def 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 Zhang97a47cb2022-10-21 11:13:01 +080048 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 Zhange1897462022-10-25 10:20:23 +080053 "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'Hostise55a2752021-01-27 11:09:08 +000058 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 Zhang470b3c42022-09-19 14:41:45 +080070 with open(os.path.join(os.getenv('WORKSPACE'),
Hugo L'Hostise55a2752021-01-27 11:09:08 +000071 "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'Hostise55a2752021-01-27 11:09:08 +000084# Function based on get_local_git_info() from utils, getting change id for the tfm repo
Xinyu Zhang470b3c42022-09-19 14:41:45 +080085def get_change_id(repo='trusted-firmware-m'):
86 directory = os.path.join(os.getenv('WORKSPACE'), repo)
Hugo L'Hostise55a2752021-01-27 11:09:08 +000087 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 Zhang470b3c42022-09-19 14:41:45 +0800118def print_image_sizes(image_sizes):
119 for sec, size in image_sizes.items():
120 print("{:4}: {}".format(sec, size))
121
Hugo L'Hostise55a2752021-01-27 11:09:08 +0000122if __name__ == "__main__":
Xinyu Zhang97a47cb2022-10-21 11:13:01 +0800123 # Export ARMClang v6.13 to ENV PATH
Xinyu Zhang11b792a2023-10-27 17:36:40 +0800124 os.environ["PATH"] += os.pathsep + os.getenv('ARMCLANG_6_18_PATH')
Xinyu Zhang97a47cb2022-10-21 11:13:01 +0800125 if os.getenv('CONFIG_NAME') in reference_configs:
Xinyu Zhang470b3c42022-09-19 14:41:45 +0800126 print("Configuration " + os.getenv('CONFIG_NAME') + " is a reference")
Hugo L'Hostise55a2752021-01-27 11:09:08 +0000127 try :
Xinyu Zhang470b3c42022-09-19 14:41:45 +0800128 change_id = get_change_id("trusted-firmware-m")
Hugo L'Hostise55a2752021-01-27 11:09:08 +0000129 except :
130 change_id = -1
Xinyu Zhang470b3c42022-09-19 14:41:45 +0800131
Xinyu Zhang470b3c42022-09-19 14:41:45 +0800132 print("------ BL2 Memory Footprint ------")
Xinyu Zhang97a47cb2022-10-21 11:13:01 +0800133 bl2_sizes = get_file_size("bl2.axf")
134 print("\n\n------ TFM Secure Memory Footprint ------")
Hugo L'Hostise55a2752021-01-27 11:09:08 +0000135 tfms_sizes = get_file_size("tfm_s.axf")
Xinyu Zhang470b3c42022-09-19 14:41:45 +0800136
Hugo L'Hostise55a2752021-01-27 11:09:08 +0000137 if (bl2_sizes != -1 and change_id != -1) :
Xinyu Zhang97a47cb2022-10-21 11:13:01 +0800138 squad_config_name = os.getenv('CONFIG_NAME')
Xinyu Zhang470b3c42022-09-19 14:41:45 +0800139 send_file_size(change_id, squad_config_name, bl2_sizes, tfms_sizes)
Hugo L'Hostise55a2752021-01-27 11:09:08 +0000140 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")