blob: f0b5be0bf912ac1e2a5d7a8f341a1bb22bbfdac3 [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
Jianliang Shen0474dbe2023-09-14 10:53:44 +080044def send_file_size(git_commit, config_name, bl2_sizes, tfms_sizes):
45 url = SQUAD_BASE_PROJECT_URL + git_commit + '/' + config_name
Hugo L'Hostise55a2752021-01-27 11:09:08 +000046
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
Jianliang Shen0474dbe2023-09-14 10:53:44 +080084def get_git_commit_hash(repo='trusted-firmware-m'):
Hugo L'Hostise55a2752021-01-27 11:09:08 +000085 cur_dir = os.path.abspath(os.getcwd())
Hugo L'Hostise55a2752021-01-27 11:09:08 +000086
Jianliang Shen0474dbe2023-09-14 10:53:44 +080087 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'Hostise55a2752021-01-27 11:09:08 +000090
Jianliang Shen0474dbe2023-09-14 10:53:44 +080091 return git_commit
Hugo L'Hostise55a2752021-01-27 11:09:08 +000092
Xinyu Zhang470b3c42022-09-19 14:41:45 +080093def print_image_sizes(image_sizes):
94 for sec, size in image_sizes.items():
95 print("{:4}: {}".format(sec, size))
96
Hugo L'Hostise55a2752021-01-27 11:09:08 +000097if __name__ == "__main__":
Xinyu Zhang97a47cb2022-10-21 11:13:01 +080098 # 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 Zhang470b3c42022-09-19 14:41:45 +0800101 print("Configuration " + os.getenv('CONFIG_NAME') + " is a reference")
Hugo L'Hostise55a2752021-01-27 11:09:08 +0000102 try :
Jianliang Shen0474dbe2023-09-14 10:53:44 +0800103 git_commit = get_git_commit_hash("trusted-firmware-m")
Hugo L'Hostise55a2752021-01-27 11:09:08 +0000104 except :
Jianliang Shen0474dbe2023-09-14 10:53:44 +0800105 git_commit = -1
Xinyu Zhang470b3c42022-09-19 14:41:45 +0800106
Xinyu Zhang470b3c42022-09-19 14:41:45 +0800107 print("------ BL2 Memory Footprint ------")
Xinyu Zhang97a47cb2022-10-21 11:13:01 +0800108 bl2_sizes = get_file_size("bl2.axf")
109 print("\n\n------ TFM Secure Memory Footprint ------")
Hugo L'Hostise55a2752021-01-27 11:09:08 +0000110 tfms_sizes = get_file_size("tfm_s.axf")
Xinyu Zhang470b3c42022-09-19 14:41:45 +0800111
Jianliang Shen0474dbe2023-09-14 10:53:44 +0800112 if (bl2_sizes != -1 and git_commit != -1) :
Xinyu Zhang97a47cb2022-10-21 11:13:01 +0800113 squad_config_name = os.getenv('CONFIG_NAME')
Jianliang Shen0474dbe2023-09-14 10:53:44 +0800114 send_file_size(git_commit, squad_config_name, bl2_sizes, tfms_sizes)
Hugo L'Hostise55a2752021-01-27 11:09:08 +0000115 else :
116 #Directory or file weren't found
Jianliang Shen0474dbe2023-09-14 10:53:44 +0800117 if git_commit == -1 :
Hugo L'Hostise55a2752021-01-27 11:09:08 +0000118 print("Error : trusted-firmware-m repo not found")
119 else :
120 print("Error : file not found")