David Wang | bcb8b14 | 2022-02-17 17:31:40 +0800 | [diff] [blame] | 1 | ############# |
| 2 | TF-M profiler |
| 3 | ############# |
| 4 | |
| 5 | TF-M profiler is a tool for profiling and benchmarking TF-M. The developer can |
| 6 | leverage it to get the interested data of runtime. |
| 7 | |
| 8 | Initially, TF-M profiler supports only count logging. You can add "checkpoint" |
| 9 | in the program. The timer count or CPU cycle count of this checkpoint can be |
| 10 | saved at runtime and be analysed in the future. |
| 11 | |
| 12 | ************************ |
| 13 | How to use TF-M profiler |
| 14 | ************************ |
| 15 | |
| 16 | 1. Integrate profiler tool with TF-M |
| 17 | |
| 18 | The profiler should be compiled together with TF-M, thus running in SPE. |
| 19 | |
| 20 | The source file need to be compiled with TF-M is mainly `profiler.c`. The header |
| 21 | files 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 | |
| 29 | The size of the database is determined by `PROF_DB_MAX` defined in |
| 30 | `export/prof_common.h`. |
| 31 | |
| 32 | The developer can override the size by redefining `PROF_DB_MAX`. |
| 33 | |
| 34 | 2. Add checkpoints for secure side |
| 35 | |
| 36 | The developer should identify the places in source code for adding the |
| 37 | checkpoints. The count value of the timer or CPU cycle will be saved into the |
| 38 | database for the checkpoints. |
| 39 | |
| 40 | The interface APIs are defined in `export/prof_if_s.h` for secure side. |
| 41 | |
| 42 | 3. Add checkpoints for non-secure side |
| 43 | |
| 44 | It'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 | |
| 47 | The interface APIs for non-secure side are defined in `export/ns/prof_if_ns.h`. |
| 48 | |
| 49 | 4. Run the program and collect data |
| 50 | |
| 51 | After successfully run the program, the data should be saved into the database. |
| 52 | |
| 53 | The developer can dump the data throught the interface defined in the header |
| 54 | files mentioned above. |
| 55 | |
| 56 | A customized software or tool can be used to generate the analysis report based |
| 57 | on the data. |
| 58 | |
| 59 | ******************** |
| 60 | Interface user guide |
| 61 | ******************** |
| 62 | |
| 63 | Initialization |
| 64 | ============== |
| 65 | |
| 66 | `prof_data_init()` should be called in secure side before calling any API of |
| 67 | profiler. |
| 68 | |
| 69 | Count logging |
| 70 | ============= |
| 71 | |
| 72 | The counter logging related APIs are defined in macros to keep the interface |
| 73 | consistent between secure and non-secure side. |
| 74 | |
| 75 | `PROF_TIMING_LOG`: This API is used to log the counter value when calling this |
| 76 | macro. `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 |
| 79 | you have many checkpoints for different purposes. Topic can help to organize |
| 80 | them 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 |
| 83 | unsigned value. |
| 84 | |
| 85 | Data fetching |
| 86 | ============= |
| 87 | |
| 88 | For the same consistent reason as counter logging, same macros are defined as |
| 89 | the interface for both secure and non-secure side. |
| 90 | |
| 91 | The 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 |
| 93 | from 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 |
| 95 | previous result. |
| 96 | |
| 97 | The 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 |
| 106 | both `topic` and `cp`. |
| 107 | |
| 108 | The APIs return `false` if no matching data found until the end of the database. |
| 109 | |
| 110 | Calibration |
| 111 | =========== |
| 112 | |
| 113 | The profiler itself has the tick or cycle cost. To get a more accurate data, a |
| 114 | calibration system is introduced. It's optional. |
| 115 | |
| 116 | The counter logging APIs can be called from secure or non-secure side. And the |
| 117 | cost of calling functions from these two worlds are different. So, secure and |
| 118 | non-secure have different calibration data. |
| 119 | |
| 120 | The system performance might float during the initialization, for example change |
| 121 | CPU frequency, enable cache, etc. So, it's recommendated that the calibration is |
| 122 | done just before the first checkpoint. |
| 123 | |
| 124 | `PROF_DO_CALIBRATE`: Call this macro to get calibration value. The more `rounds` |
| 125 | the more accurate. |
| 126 | |
| 127 | `PROF_GET_CALI_VALUE_FROM_TAG`: Get the calibration value from the tag. The |
| 128 | calibrated counter is "current_counter" - "previous_counter" - |
| 129 | "current_cali_value". |
| 130 | "current_cali_value" = PROF_GET_CALI_VALUE_FROM_TAG(current_tag). |
| 131 | |
Summer Qin | 07e8f21 | 2023-07-05 17:05:07 +0800 | [diff] [blame^] | 132 | Data analysis |
| 133 | ============= |
| 134 | |
| 135 | Data analysis interfaces can be used to do some basic analysis and the data |
| 136 | returned is calibrated already. |
| 137 | |
| 138 | `PROF_DATA_DIFF`: Get the counter value difference for the two tags. Returning |
| 139 | `0` indicates errors. |
| 140 | |
| 141 | If the checkpoints are logged by multi-times, you can get the following counter |
| 142 | value differences between two tags: |
| 143 | |
| 144 | `PROF_DATA_DIFF_MIN`: Get the minimum counter value difference for the two tags. |
| 145 | Returning `UINT32_MAX` indicates errors. |
| 146 | |
| 147 | `PROF_DATA_DIFF_MAX`: Get the maximum counter value difference for the two tags. |
| 148 | Returning `0` indicates errors. |
| 149 | |
| 150 | `PROF_DATA_DIFF_AVG`: Get the average counter value difference for the two tags. |
| 151 | Returning `0` indicates errors. |
| 152 | |
David Wang | bcb8b14 | 2022-02-17 17:31:40 +0800 | [diff] [blame] | 153 | -------------- |
| 154 | |
Summer Qin | 07e8f21 | 2023-07-05 17:05:07 +0800 | [diff] [blame^] | 155 | *Copyright (c) 2022-2023, Arm Limited. All rights reserved.* |