blob: e913b757b4bca9ebb5586b5ff06de22f1dcc1c6f [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
Elena Uziunaiteb90a3402023-11-13 16:24:28 +0000103
Jianliang Sheneba97722023-08-16 13:34:50 +0800104 PROF_TIMING_LOG(topic_id, cp_id);
David Wangbcb8b142022-02-17 17:31:40 +0800105
Jianliang Sheneba97722023-08-16 13:34:50 +0800106+------------+--------------------------------------------------------------+
107| Parameters | Description |
108+============+==============================================================+
109| topic_id | Topic is used to gather a group of checkpoints. |
110| | It's useful when you have many checkpoints for different |
111| | purposes. Topic can help to organize them and filter the |
112| | related information out. It's an 8-bit unsigned value. |
113+------------+--------------------------------------------------------------+
114| cp_id | Checkpoint ID. Different topics can have same cp_id. |
115| | It's a 16-bit unsigned value. |
116+------------+--------------------------------------------------------------+
David Wangbcb8b142022-02-17 17:31:40 +0800117
Jianliang Sheneba97722023-08-16 13:34:50 +0800118Collect Data
119============
David Wangbcb8b142022-02-17 17:31:40 +0800120
Jianliang Sheneba97722023-08-16 13:34:50 +0800121After successfully running the program, the data should be saved into the database.
122The developer can dump the data through the interface defined in the header
123files mentioned above.
David Wangbcb8b142022-02-17 17:31:40 +0800124
Jianliang Sheneba97722023-08-16 13:34:50 +0800125For the same consistent reason as counter logging, the same macros are defined as
126the interfaces for both secure and non-secure sides.
127
128The data fetching interfaces work in a stream way. ``PROF_FETCH_DATA_START`` and
129``PROF_FETCH_DATA_BY_TOPIC_START`` search the data that matches the given pattern
130from the beginning of the database. ``PROF_FETCH_DATA_CONTINUE`` and
131``PROF_FETCH_DATA_BY_TOPIC_CONTINUE`` search from the next data set of the
David Wangbcb8b142022-02-17 17:31:40 +0800132previous result.
133
Jianliang Sheneba97722023-08-16 13:34:50 +0800134.. Note::
Kevin Pengdc06d4b2023-07-13 15:31:15 +0800135
Jianliang Sheneba97722023-08-16 13:34:50 +0800136 All the APIs increase the internal search index, be careful about mixing using them
Kevin Pengdc06d4b2023-07-13 15:31:15 +0800137 for different checkpoints and topics at the same time.
138
Jianliang Sheneba97722023-08-16 13:34:50 +0800139The match condition of a search is controlled by the tag mask. It's ``tag value``
140& ``tag_mask`` == ``tag_pattern``. To enumerate the whole database, set
141``tag_mask`` and ``tag_pattern`` both to ``0``.
David Wangbcb8b142022-02-17 17:31:40 +0800142
Jianliang Sheneba97722023-08-16 13:34:50 +0800143* ``PROF_FETCH_DATA_XXX``: The generic interface for getting data.
144* ``PROF_FETCH_DATA_BY_TOPIC_XXX``: Get data for a specific ``topic``.
David Wangbcb8b142022-02-17 17:31:40 +0800145
Jianliang Sheneba97722023-08-16 13:34:50 +0800146The APIs return ``false`` if no matching data is found until the end of the database.
David Wangbcb8b142022-02-17 17:31:40 +0800147
148Calibration
149===========
150
Jianliang Sheneba97722023-08-16 13:34:50 +0800151The profiler itself has the tick or cycle cost. To get more accurate data, a
David Wangbcb8b142022-02-17 17:31:40 +0800152calibration system is introduced. It's optional.
153
Jianliang Sheneba97722023-08-16 13:34:50 +0800154The counter logging APIs can be called from the secure or non-secure side. And the
155cost of calling functions from these two worlds is different. So, secure and
David Wangbcb8b142022-02-17 17:31:40 +0800156non-secure have different calibration data.
157
Jianliang Sheneba97722023-08-16 13:34:50 +0800158The system performance might float during the initialization, for example, change
159CPU frequency, enable cache, etc. So, it's recommended that the calibration is
David Wangbcb8b142022-02-17 17:31:40 +0800160done just before the first checkpoint.
161
Jianliang Sheneba97722023-08-16 13:34:50 +0800162* ``PROF_DO_CALIBRATE``: Call this macro to get the calibration value. The more ``rounds``
163 the more accurate.
164* ``PROF_GET_CALI_VALUE_FROM_TAG``: Get the calibration value from the tag.
165 The calibrated counter is ``current_counter - previous_counter - current_cali_value``.
166 Here ``current_cali_value`` equals ``PROF_GET_CALI_VALUE_FROM_TAG`` (current_tag).
David Wangbcb8b142022-02-17 17:31:40 +0800167
Jianliang Sheneba97722023-08-16 13:34:50 +0800168Data Analysis
Summer Qin07e8f212023-07-05 17:05:07 +0800169=============
170
171Data analysis interfaces can be used to do some basic analysis and the data
172returned is calibrated already.
173
Jianliang Sheneba97722023-08-16 13:34:50 +0800174``PROF_DATA_DIFF``: Get the counter value difference for the two tags. Returning
175``0`` indicates errors.
Summer Qin07e8f212023-07-05 17:05:07 +0800176
177If the checkpoints are logged by multi-times, you can get the following counter
178value differences between two tags:
179
Jianliang Sheneba97722023-08-16 13:34:50 +0800180* ``PROF_DATA_DIFF_MIN``: Get the minimum counter value difference for the two tags.
181 Returning ``UINT32_MAX`` indicates errors.
182* ``PROF_DATA_DIFF_MAX``: Get the maximum counter value difference for the two tags.
183 Returning ``0`` indicates errors.
184* ``PROF_DATA_DIFF_AVG``: Get the average counter value difference for the two tags.
185 Returning ``0`` indicates errors.
Summer Qin07e8f212023-07-05 17:05:07 +0800186
Jianliang Sheneba97722023-08-16 13:34:50 +0800187A customized software or tool can be used to generate the analysis report based
188on the data.
Summer Qin07e8f212023-07-05 17:05:07 +0800189
Jianliang Sheneba97722023-08-16 13:34:50 +0800190Profiler Self-test
191==================
192
193`profiler_self_test` is a quick test for all interfaces above. To build and run
194in the Linux:
195
196.. code-block:: console
197
198 cd profiler_self_test
199 mkdir build && cd build
200 cmake .. && make
201 ./prof_self_test
202
203********************
204TF-M Profiling Cases
205********************
206
207The profiler tool has already been integrated into TF-M to analyze the program
208performance with the built-in profiling cases. Users can also add a new
209profiling case to get a specific profiling report. TF-M profiling provides
210example profiling cases in `profiling_cases`.
211
212PSA Client API Profiling
213========================
214
215This profiling case analyzes the performance of PSA Client APIs called from SPE
216and NSPE, including ``psa_connect()``, ``psa_call()``, ``psa_close()`` and ``stateless psa_call()``.
217The main structure is:
218
219::
220
221 prof_psa_client_api/
222 ├── cases
223 │ ├── non_secure
224 │ └── secure
225 └── partitions
226 ├── prof_server_partition
227 └── prof_client_partition
228
229* The `cases` folder is the basic SPE and NSPE profiling log and analysis code.
Jianliang Shen25f6d7b2023-11-07 14:30:48 +0800230* NSPE can use `prof_log` library to print the analysis result.
Jianliang Sheneba97722023-08-16 13:34:50 +0800231* `prof_server_partition` is a dummy secure partition. It immediately returns
232 once it receives a PSA client call from a client.
233* `prof_client_partition` is the SPE profiling entry to trigger the secure profiling.
234
235To make this profiling report more accurate, It is recommended to disable other
236partitions and all irrelevant tests.
237
238Adding New TF-M Profiling Case
239==============================
240
241Users can add source folder `<prof_example>` under path `profiling_cases` to
242customize performance analysis of target processes, such as the APIs of secure
243partitions, the functions in the SPM, or the user's interfaces. The
244integration requires these steps:
245
2461. Confirm the target process block to create profiling cases.
2472. Enable or create the server partition if necessary. Note that the other
248 irrelevant partitions shall be disabled.
2493. Find ways to output profiling data.
2504. Trigger profiling cases in SPE or NSPE.
251
252 a. For SPE, a secure client partition can be created to trigger the secure profiling.
Jianliang Shen25f6d7b2023-11-07 14:30:48 +0800253 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 +0800254
255.. Note::
256
257 If the profiling case requires extra out-of-tree secure partition build, the
258 paths of extra partitions and manifest list file shall be appended in
259 ``TFM_EXTRA_PARTITION_PATHS`` and ``TFM_EXTRA_MANIFEST_LIST_FILES``. Refer to
Elena Uziunaiteb90a3402023-11-13 16:24:28 +0000260 :doc:`Adding Secure Partition<TF-M:integration_guide/services/tfm_secure_partition_addition>`.
Summer Qin07e8f212023-07-05 17:05:07 +0800261
David Wangbcb8b142022-02-17 17:31:40 +0800262--------------
263
Summer Qin07e8f212023-07-05 17:05:07 +0800264*Copyright (c) 2022-2023, Arm Limited. All rights reserved.*