blob: 2e081c9007dfbba4a3685af8ec52c6506e650aae [file] [log] [blame]
Jianliang Sheneba97722023-08-16 13:34:50 +08001################################
2Profiler tool and TF-M Profiling
3################################
David Wangbcb8b142022-02-17 17:31:40 +08004
Jianliang Sheneba97722023-08-16 13:34:50 +08005The profiler is a tool for profiling and benchmarking programs. The developer can
David Wangbcb8b142022-02-17 17:31:40 +08006leverage it to get the interested data of runtime.
7
Jianliang Sheneba97722023-08-16 13:34:50 +08008Initially, the profiler supports only count logging. You can add "checkpoint"
David Wangbcb8b142022-02-17 17:31:40 +08009in the program. The timer count or CPU cycle count of this checkpoint can be
10saved at runtime and be analysed in the future.
11
Jianliang Sheneba97722023-08-16 13:34:50 +080012*********************************
13TF-M Profiling Build Instructions
14*********************************
David Wangbcb8b142022-02-17 17:31:40 +080015
Jianliang Sheneba97722023-08-16 13:34:50 +080016TF-M has integrated some built-in profiling cases. There are two configurations
17for profiling:
David Wangbcb8b142022-02-17 17:31:40 +080018
Jianliang Sheneba97722023-08-16 13:34:50 +080019* ``CONFIG_TFM_ENABLE_PROFILING``: Enable profiling building in TF-M SPE and NSPE.
20 It cannot be enabled together with any regression test configs, for example ``TEST_NS``.
21* ``TFM_TOOLS_PATH``: Path of tf-m-tools repo. The default value is ``DOWNLOAD``
22 to fetch the remote source.
David Wangbcb8b142022-02-17 17:31:40 +080023
Jianliang Sheneba97722023-08-16 13:34:50 +080024The section `TF-M Profiling Cases`_ introduces the profiling cases in TF-M.
25To enable the built-in profiling cases in TF-M, run:
David Wangbcb8b142022-02-17 17:31:40 +080026
Jianliang Sheneba97722023-08-16 13:34:50 +080027.. code-block:: console
David Wangbcb8b142022-02-17 17:31:40 +080028
Jianliang Shen25f6d7b2023-11-07 14:30:48 +080029 cd <path to tf-m-tools>/profiling/profiling_cases/tfm_profiling
30 mkdir build
31
32 # Build SPE
33 cmake -S <path to tf-m> -B build/spe -DTFM_PLATFORM=arm/mps2/an521 \
34 -DCONFIG_TFM_ENABLE_PROFILING=ON -DCMAKE_BUILD_TYPE=Release \
35 -DTFM_EXTRA_PARTITION_PATHS=${PWD}/../prof_psa_client_api/partitions/prof_server_partition;${PWD}/../prof_psa_client_api/partitions/prof_client_partition \
36 -DTFM_EXTRA_MANIFEST_LIST_FILES=${PWD}/../prof_psa_client_api/partitions/prof_psa_client_api_manifest_list.yaml \
37 -DTFM_PARTITION_LOG_LEVEL=TFM_PARTITION_LOG_LEVEL_INFO
38
39 # Another simple way to configure SPE:
40 cmake -S <path to tf-m> -B build/spe -DTFM_PLATFORM=arm/mps2/an521 \
41 -DTFM_EXTRA_CONFIG_PATH=${PWD}/../prof_psa_client_api/partitions/config_spe.cmake
42 cmake --build build/spe -- install -j
43
44 # Build NSPE
45 cmake -S . -B build/nspe -DCONFIG_SPE_PATH=build/spe/api_ns \
46 -DTFM_TOOLCHAIN_FILE=build/spe/api_ns/cmake/toolchain_ns_GNUARM.cmake
47 cmake --build build/nspe -- -j
David Wangbcb8b142022-02-17 17:31:40 +080048
Jianliang Sheneba97722023-08-16 13:34:50 +080049******************************
50Profiler Integration Reference
51******************************
David Wangbcb8b142022-02-17 17:31:40 +080052
Jianliang Sheneba97722023-08-16 13:34:50 +080053`profiler/profiler.c` is the main source file to be complied with the tagert program.
David Wangbcb8b142022-02-17 17:31:40 +080054
55Initialization
56==============
57
Jianliang Sheneba97722023-08-16 13:34:50 +080058``PROFILING_INIT()`` defined in `profiling/export/prof_intf_s.h` shall be called
59on the secure side before calling any other API of the profiler. It initializes the
60HAL and the backend database which can be customized by users.
David Wangbcb8b142022-02-17 17:31:40 +080061
Jianliang Sheneba97722023-08-16 13:34:50 +080062Implement the HAL
63-----------------
64
65`export/prof_hal.h` defines the HAL that should be implemented by the platform.
66
67* ``prof_hal_init()``: Initialize the counter hardware.
68
69* ``prof_hal_get_count()``: Get current counter value.
70
71Users shall implement platform-specific hardware support in ``prof_hal_init()``
72and ``prof_hal_get_count()`` under `export/platform`.
73
74Take `export/platform/tfm_hal_dwt_prof.c` as an example, it uses Data Watchpoint
75and Trace unit (DWT) to count the CPU cycles which can be a reference for
76performance.
77
78Setup Database
79--------------
80
81The size of the database is determined by ``PROF_DB_MAX`` defined in
82`export/prof_common.h`.
83
84The developer can override the size by redefining ``PROF_DB_MAX``.
85
86Add Checkpoints
87===============
88
89The developer should identify the places in the source code for adding the
90checkpoints. The count value of the timer or CPU cycle will be saved into the
91database for the checkpoints. The interface APIs are defined in `export/prof_intf_s.h` for the secure side.
92
93It's also supported to add checkpoints on the non-secure side.
94Add `export/ns/prof_intf_ns.c` to the source file list of the non-secure side.
95The interface APIs for the non-secure side are defined in `export/ns/prof_intf_ns.h`.
David Wangbcb8b142022-02-17 17:31:40 +080096
97The counter logging related APIs are defined in macros to keep the interface
Jianliang Sheneba97722023-08-16 13:34:50 +080098consistent between the secure and non-secure sides.
David Wangbcb8b142022-02-17 17:31:40 +080099
Jianliang Sheneba97722023-08-16 13:34:50 +0800100Users can call macro ``PROF_TIMING_LOG()`` logs the counter value.
David Wangbcb8b142022-02-17 17:31:40 +0800101
Jianliang Sheneba97722023-08-16 13:34:50 +0800102.. code-block:: c
103 PROF_TIMING_LOG(topic_id, cp_id);
David Wangbcb8b142022-02-17 17:31:40 +0800104
Jianliang Sheneba97722023-08-16 13:34:50 +0800105+------------+--------------------------------------------------------------+
106| Parameters | Description |
107+============+==============================================================+
108| topic_id | Topic is used to gather a group of checkpoints. |
109| | It's useful when you have many checkpoints for different |
110| | purposes. Topic can help to organize them and filter the |
111| | related information out. It's an 8-bit unsigned value. |
112+------------+--------------------------------------------------------------+
113| cp_id | Checkpoint ID. Different topics can have same cp_id. |
114| | It's a 16-bit unsigned value. |
115+------------+--------------------------------------------------------------+
David Wangbcb8b142022-02-17 17:31:40 +0800116
Jianliang Sheneba97722023-08-16 13:34:50 +0800117Collect Data
118============
David Wangbcb8b142022-02-17 17:31:40 +0800119
Jianliang Sheneba97722023-08-16 13:34:50 +0800120After successfully running the program, the data should be saved into the database.
121The developer can dump the data through the interface defined in the header
122files mentioned above.
David Wangbcb8b142022-02-17 17:31:40 +0800123
Jianliang Sheneba97722023-08-16 13:34:50 +0800124For the same consistent reason as counter logging, the same macros are defined as
125the interfaces for both secure and non-secure sides.
126
127The data fetching interfaces work in a stream way. ``PROF_FETCH_DATA_START`` and
128``PROF_FETCH_DATA_BY_TOPIC_START`` search the data that matches the given pattern
129from the beginning of the database. ``PROF_FETCH_DATA_CONTINUE`` and
130``PROF_FETCH_DATA_BY_TOPIC_CONTINUE`` search from the next data set of the
David Wangbcb8b142022-02-17 17:31:40 +0800131previous result.
132
Jianliang Sheneba97722023-08-16 13:34:50 +0800133.. Note::
Kevin Pengdc06d4b2023-07-13 15:31:15 +0800134
Jianliang Sheneba97722023-08-16 13:34:50 +0800135 All the APIs increase the internal search index, be careful about mixing using them
Kevin Pengdc06d4b2023-07-13 15:31:15 +0800136 for different checkpoints and topics at the same time.
137
Jianliang Sheneba97722023-08-16 13:34:50 +0800138The match condition of a search is controlled by the tag mask. It's ``tag value``
139& ``tag_mask`` == ``tag_pattern``. To enumerate the whole database, set
140``tag_mask`` and ``tag_pattern`` both to ``0``.
David Wangbcb8b142022-02-17 17:31:40 +0800141
Jianliang Sheneba97722023-08-16 13:34:50 +0800142* ``PROF_FETCH_DATA_XXX``: The generic interface for getting data.
143* ``PROF_FETCH_DATA_BY_TOPIC_XXX``: Get data for a specific ``topic``.
David Wangbcb8b142022-02-17 17:31:40 +0800144
Jianliang Sheneba97722023-08-16 13:34:50 +0800145The APIs return ``false`` if no matching data is found until the end of the database.
David Wangbcb8b142022-02-17 17:31:40 +0800146
147Calibration
148===========
149
Jianliang Sheneba97722023-08-16 13:34:50 +0800150The profiler itself has the tick or cycle cost. To get more accurate data, a
David Wangbcb8b142022-02-17 17:31:40 +0800151calibration system is introduced. It's optional.
152
Jianliang Sheneba97722023-08-16 13:34:50 +0800153The counter logging APIs can be called from the secure or non-secure side. And the
154cost of calling functions from these two worlds is different. So, secure and
David Wangbcb8b142022-02-17 17:31:40 +0800155non-secure have different calibration data.
156
Jianliang Sheneba97722023-08-16 13:34:50 +0800157The system performance might float during the initialization, for example, change
158CPU frequency, enable cache, etc. So, it's recommended that the calibration is
David Wangbcb8b142022-02-17 17:31:40 +0800159done just before the first checkpoint.
160
Jianliang Sheneba97722023-08-16 13:34:50 +0800161* ``PROF_DO_CALIBRATE``: Call this macro to get the calibration value. The more ``rounds``
162 the more accurate.
163* ``PROF_GET_CALI_VALUE_FROM_TAG``: Get the calibration value from the tag.
164 The calibrated counter is ``current_counter - previous_counter - current_cali_value``.
165 Here ``current_cali_value`` equals ``PROF_GET_CALI_VALUE_FROM_TAG`` (current_tag).
David Wangbcb8b142022-02-17 17:31:40 +0800166
Jianliang Sheneba97722023-08-16 13:34:50 +0800167Data Analysis
Summer Qin07e8f212023-07-05 17:05:07 +0800168=============
169
170Data analysis interfaces can be used to do some basic analysis and the data
171returned is calibrated already.
172
Jianliang Sheneba97722023-08-16 13:34:50 +0800173``PROF_DATA_DIFF``: Get the counter value difference for the two tags. Returning
174``0`` indicates errors.
Summer Qin07e8f212023-07-05 17:05:07 +0800175
176If the checkpoints are logged by multi-times, you can get the following counter
177value differences between two tags:
178
Jianliang Sheneba97722023-08-16 13:34:50 +0800179* ``PROF_DATA_DIFF_MIN``: Get the minimum counter value difference for the two tags.
180 Returning ``UINT32_MAX`` indicates errors.
181* ``PROF_DATA_DIFF_MAX``: Get the maximum counter value difference for the two tags.
182 Returning ``0`` indicates errors.
183* ``PROF_DATA_DIFF_AVG``: Get the average counter value difference for the two tags.
184 Returning ``0`` indicates errors.
Summer Qin07e8f212023-07-05 17:05:07 +0800185
Jianliang Sheneba97722023-08-16 13:34:50 +0800186A customized software or tool can be used to generate the analysis report based
187on the data.
Summer Qin07e8f212023-07-05 17:05:07 +0800188
Jianliang Sheneba97722023-08-16 13:34:50 +0800189Profiler Self-test
190==================
191
192`profiler_self_test` is a quick test for all interfaces above. To build and run
193in the Linux:
194
195.. code-block:: console
196
197 cd profiler_self_test
198 mkdir build && cd build
199 cmake .. && make
200 ./prof_self_test
201
202********************
203TF-M Profiling Cases
204********************
205
206The profiler tool has already been integrated into TF-M to analyze the program
207performance with the built-in profiling cases. Users can also add a new
208profiling case to get a specific profiling report. TF-M profiling provides
209example profiling cases in `profiling_cases`.
210
211PSA Client API Profiling
212========================
213
214This profiling case analyzes the performance of PSA Client APIs called from SPE
215and NSPE, including ``psa_connect()``, ``psa_call()``, ``psa_close()`` and ``stateless psa_call()``.
216The main structure is:
217
218::
219
220 prof_psa_client_api/
221 ├── cases
222 │ ├── non_secure
223 │ └── secure
224 └── partitions
225 ├── prof_server_partition
226 └── prof_client_partition
227
228* The `cases` folder is the basic SPE and NSPE profiling log and analysis code.
Jianliang Shen25f6d7b2023-11-07 14:30:48 +0800229* NSPE can use `prof_log` library to print the analysis result.
Jianliang Sheneba97722023-08-16 13:34:50 +0800230* `prof_server_partition` is a dummy secure partition. It immediately returns
231 once it receives a PSA client call from a client.
232* `prof_client_partition` is the SPE profiling entry to trigger the secure profiling.
233
234To make this profiling report more accurate, It is recommended to disable other
235partitions and all irrelevant tests.
236
237Adding New TF-M Profiling Case
238==============================
239
240Users can add source folder `<prof_example>` under path `profiling_cases` to
241customize performance analysis of target processes, such as the APIs of secure
242partitions, the functions in the SPM, or the user's interfaces. The
243integration requires these steps:
244
2451. Confirm the target process block to create profiling cases.
2462. Enable or create the server partition if necessary. Note that the other
247 irrelevant partitions shall be disabled.
2483. Find ways to output profiling data.
2494. Trigger profiling cases in SPE or NSPE.
250
251 a. For SPE, a secure client partition can be created to trigger the secure profiling.
Jianliang Shen25f6d7b2023-11-07 14:30:48 +0800252 b. For NSPE, the profiling case entry can be added to the 'tfm_ns' target under the `tfm_profiling` folder.
Jianliang Sheneba97722023-08-16 13:34:50 +0800253
254.. Note::
255
256 If the profiling case requires extra out-of-tree secure partition build, the
257 paths of extra partitions and manifest list file shall be appended in
258 ``TFM_EXTRA_PARTITION_PATHS`` and ``TFM_EXTRA_MANIFEST_LIST_FILES``. Refer to
259 `Adding Secure Partition`_.
260
261.. _Adding Secure Partition: https://git.trustedfirmware.org/TF-M/trusted-firmware-m.git/tree/docs/integration_guide/services/tfm_secure_partition_addition.rst
Summer Qin07e8f212023-07-05 17:05:07 +0800262
David Wangbcb8b142022-02-17 17:31:40 +0800263--------------
264
Summer Qin07e8f212023-07-05 17:05:07 +0800265*Copyright (c) 2022-2023, Arm Limited. All rights reserved.*