blob: 21514f26b5006e3d385a0b9a99e3ab8159d109b3 [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
20The source file need to be compiled with TF-M is mainly `profiler.c`. The header
21files are in `export` folder.
22
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
40The interface APIs are defined in `export/prof_if_s.h` for secure side.
41
423. Add checkpoints for non-secure side
43
44It's supported to add checkpionts in non-secure side. Add
45`export/ns/prof_if_ns.c` to the source file list of non-secure side.
46
47The interface APIs for non-secure side are defined in `export/ns/prof_if_ns.h`.
48
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
89the interface for both secure and non-secure side.
90
91The data fetching interface works as stream way. `PROF_FETCH_DATA_START` and
92`PROF_FETCH_DATA_BY_XXX_START` search the data that matches the given pattern
93from the beginning of the database. `PROF_FETCH_DATA_CONTINUE` and
94`PROF_FETCH_DATA_BY_XXX_CONTINUE` search from the next data set of the
95previous result.
96
97The match condition of a search is controlled by the tag mask. It's `tag value`
98& `tag_mask` == `tag_pattern`. To enumerate the whole database, set
99`tag_mask` and `tag_pattern` both to `0`.
100
101`PROF_FETCH_DATA_XXX`: The generic interface for getting data.
102
103`PROF_FETCH_DATA_BY_TOPIC_XXX`: Get data for a specific `topic`.
104
105`PROF_FETCH_DATA_BY_CP_XXX`: Get data for a specific checkpoint by specifying
106both `topic` and `cp`.
107
108The APIs return `false` if no matching data found until the end of the database.
109
110Calibration
111===========
112
113The profiler itself has the tick or cycle cost. To get a more accurate data, a
114calibration system is introduced. It's optional.
115
116The counter logging APIs can be called from secure or non-secure side. And the
117cost of calling functions from these two worlds are different. So, secure and
118non-secure have different calibration data.
119
120The system performance might float during the initialization, for example change
121CPU frequency, enable cache, etc. So, it's recommendated that the calibration is
122done just before the first checkpoint.
123
124`PROF_DO_CALIBRATE`: Call this macro to get calibration value. The more `rounds`
125the more accurate.
126
127`PROF_GET_CALI_VALUE_FROM_TAG`: Get the calibration value from the tag. The
128calibrated counter is "current_counter" - "previous_counter" -
129"current_cali_value".
130"current_cali_value" = PROF_GET_CALI_VALUE_FROM_TAG(current_tag).
131
Summer Qin07e8f212023-07-05 17:05:07 +0800132Data analysis
133=============
134
135Data analysis interfaces can be used to do some basic analysis and the data
136returned is calibrated already.
137
138`PROF_DATA_DIFF`: Get the counter value difference for the two tags. Returning
139`0` indicates errors.
140
141If the checkpoints are logged by multi-times, you can get the following counter
142value differences between two tags:
143
144`PROF_DATA_DIFF_MIN`: Get the minimum counter value difference for the two tags.
145Returning `UINT32_MAX` indicates errors.
146
147`PROF_DATA_DIFF_MAX`: Get the maximum counter value difference for the two tags.
148Returning `0` indicates errors.
149
150`PROF_DATA_DIFF_AVG`: Get the average counter value difference for the two tags.
151Returning `0` indicates errors.
152
David Wangbcb8b142022-02-17 17:31:40 +0800153--------------
154
Summer Qin07e8f212023-07-05 17:05:07 +0800155*Copyright (c) 2022-2023, Arm Limited. All rights reserved.*