blob: 6893d61f077b2be8c608d1ebe33627aff83dee9d [file] [log] [blame]
David Wangbcb8b142022-02-17 17:31:40 +08001#############
2TF-M profiler
3#############
4
5TF-M profiler is a tool for profiling and benchmarking TF-M. The developer can
6leverage it to get the interested data of runtime.
7
8Initially, TF-M profiler supports only count logging. You can add "checkpoint"
9in the program. The timer count or CPU cycle count of this checkpoint can be
10saved at runtime and be analysed in the future.
11
12************************
13How to use TF-M profiler
14************************
15
161. Integrate profiler tool with TF-M
17
18The profiler should be compiled together with TF-M, thus running in SPE.
19
Jianliang Shene898f5c2023-08-10 15:20:33 +080020The source file need to be compiled with TF-M is mainly `profiler/profiler.c`.
21The header files are in `export` folder.
David Wangbcb8b142022-02-17 17:31:40 +080022
23`export/prof_hal.h` defines the HAL that should be implemented by the platform.
24
25* `prof_hal_init()`: Initialize the counter hardware.
26
27* `prof_hal_get_count()`: Get current counter value.
28
29The size of the database is determined by `PROF_DB_MAX` defined in
30`export/prof_common.h`.
31
32The developer can override the size by redefining `PROF_DB_MAX`.
33
342. Add checkpoints for secure side
35
36The developer should identify the places in source code for adding the
37checkpoints. The count value of the timer or CPU cycle will be saved into the
38database for the checkpoints.
39
Jianliang Shene898f5c2023-08-10 15:20:33 +080040The interface APIs are defined in `export/prof_intf_s.h` for secure side.
David Wangbcb8b142022-02-17 17:31:40 +080041
423. Add checkpoints for non-secure side
43
44It's supported to add checkpionts in non-secure side. Add
Jianliang Shene898f5c2023-08-10 15:20:33 +080045`export/ns/prof_intf_ns.c` to the source file list of non-secure side.
David Wangbcb8b142022-02-17 17:31:40 +080046
Jianliang Shene898f5c2023-08-10 15:20:33 +080047The interface APIs for non-secure side are defined in `export/ns/prof_intf_ns.h`.
David Wangbcb8b142022-02-17 17:31:40 +080048
494. Run the program and collect data
50
51After successfully run the program, the data should be saved into the database.
52
53The developer can dump the data throught the interface defined in the header
54files mentioned above.
55
56A customized software or tool can be used to generate the analysis report based
57on the data.
58
59********************
60Interface user guide
61********************
62
63Initialization
64==============
65
66`prof_data_init()` should be called in secure side before calling any API of
67profiler.
68
69Count logging
70=============
71
72The counter logging related APIs are defined in macros to keep the interface
73consistent between secure and non-secure side.
74
75`PROF_TIMING_LOG`: This API is used to log the counter value when calling this
76macro. `topic_id` and `cp_id` are the parameters.
77
78`topic_id`: Topic is used to gather a group of checkpoints. It's useful when
79you have many checkpoints for different purposes. Topic can help to organize
80them and filter the related information out. It's a 8-bit unsigned value.
81
82`cp_id`: Checkpoint ID. Different topics can have same `cp_id`. It's a 16-bit
83unsigned value.
84
85Data fetching
86=============
87
88For the same consistent reason as counter logging, same macros are defined as
Kevin Pengdc06d4b2023-07-13 15:31:15 +080089the interfaces for both secure and non-secure side.
David Wangbcb8b142022-02-17 17:31:40 +080090
Kevin Pengdc06d4b2023-07-13 15:31:15 +080091The data fetching interfaces work as stream way. `PROF_FETCH_DATA_START` and
92`PROF_FETCH_DATA_BY_TOPIC_START` search the data that matches the given pattern
David Wangbcb8b142022-02-17 17:31:40 +080093from the beginning of the database. `PROF_FETCH_DATA_CONTINUE` and
Kevin Pengdc06d4b2023-07-13 15:31:15 +080094`PROF_FETCH_DATA_BY_TOPIC_CONTINUE` search from the next data set of the
David Wangbcb8b142022-02-17 17:31:40 +080095previous result.
96
Kevin Pengdc06d4b2023-07-13 15:31:15 +080097 .. Note::
98
99 All the APIs increase the internal search index, be careful on mixing using them
100 for different checkpoints and topics at the same time.
101
David Wangbcb8b142022-02-17 17:31:40 +0800102The match condition of a search is controlled by the tag mask. It's `tag value`
103& `tag_mask` == `tag_pattern`. To enumerate the whole database, set
104`tag_mask` and `tag_pattern` both to `0`.
105
106`PROF_FETCH_DATA_XXX`: The generic interface for getting data.
107
108`PROF_FETCH_DATA_BY_TOPIC_XXX`: Get data for a specific `topic`.
109
David Wangbcb8b142022-02-17 17:31:40 +0800110The APIs return `false` if no matching data found until the end of the database.
111
112Calibration
113===========
114
115The profiler itself has the tick or cycle cost. To get a more accurate data, a
116calibration system is introduced. It's optional.
117
118The counter logging APIs can be called from secure or non-secure side. And the
119cost of calling functions from these two worlds are different. So, secure and
120non-secure have different calibration data.
121
122The system performance might float during the initialization, for example change
123CPU frequency, enable cache, etc. So, it's recommendated that the calibration is
124done just before the first checkpoint.
125
126`PROF_DO_CALIBRATE`: Call this macro to get calibration value. The more `rounds`
127the more accurate.
128
129`PROF_GET_CALI_VALUE_FROM_TAG`: Get the calibration value from the tag. The
130calibrated counter is "current_counter" - "previous_counter" -
131"current_cali_value".
132"current_cali_value" = PROF_GET_CALI_VALUE_FROM_TAG(current_tag).
133
Summer Qin07e8f212023-07-05 17:05:07 +0800134Data analysis
135=============
136
137Data analysis interfaces can be used to do some basic analysis and the data
138returned is calibrated already.
139
140`PROF_DATA_DIFF`: Get the counter value difference for the two tags. Returning
141`0` indicates errors.
142
143If the checkpoints are logged by multi-times, you can get the following counter
144value differences between two tags:
145
146`PROF_DATA_DIFF_MIN`: Get the minimum counter value difference for the two tags.
147Returning `UINT32_MAX` indicates errors.
148
149`PROF_DATA_DIFF_MAX`: Get the maximum counter value difference for the two tags.
150Returning `0` indicates errors.
151
152`PROF_DATA_DIFF_AVG`: Get the average counter value difference for the two tags.
153Returning `0` indicates errors.
154
David Wangbcb8b142022-02-17 17:31:40 +0800155--------------
156
Summer Qin07e8f212023-07-05 17:05:07 +0800157*Copyright (c) 2022-2023, Arm Limited. All rights reserved.*