alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
Isabella Gottardi | c64f506 | 2022-01-21 15:27:13 +0000 | [diff] [blame] | 2 | * Copyright (c) 2022 Arm Limited. All rights reserved. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 3 | * SPDX-License-Identifier: Apache-2.0 |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | #ifndef APP_PROFILER_HPP |
| 18 | #define APP_PROFILER_HPP |
| 19 | |
| 20 | #include "hal.h" |
| 21 | |
| 22 | #include <string> |
| 23 | #include <map> |
| 24 | #include <vector> |
| 25 | |
| 26 | namespace arm { |
| 27 | namespace app { |
| 28 | |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 29 | /** Statistics for a profiling metric. */ |
| 30 | struct Statistics { |
| 31 | std::string name; |
| 32 | std::string unit; |
| 33 | std::uint64_t total; |
| 34 | double avrg; |
| 35 | std::uint64_t min; |
| 36 | std::uint64_t max; |
| 37 | }; |
| 38 | |
| 39 | /** Profiling results with calculated statistics. */ |
| 40 | struct ProfileResult { |
| 41 | std::string name; |
| 42 | std::uint32_t samplesNum; |
| 43 | std::vector<Statistics> data; |
| 44 | }; |
| 45 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 46 | /** A single profiling unit definition. */ |
| 47 | struct ProfilingUnit { |
Kshitij Sisodia | da2ec06 | 2022-04-01 14:43:53 +0100 | [diff] [blame] | 48 | pmu_counters counters; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | /* A collection of profiling units. */ |
| 52 | using ProfilingSeries = std::vector<arm::app::ProfilingUnit>; |
| 53 | |
| 54 | /* A map for string identifiable profiling series. */ |
| 55 | using ProfilingMap = std::map<std::string, ProfilingSeries>; |
| 56 | |
| 57 | /** |
| 58 | * @brief A very simple profiler example using the platform timer |
| 59 | * implementation. |
| 60 | */ |
| 61 | class Profiler { |
| 62 | public: |
| 63 | /** |
| 64 | * @brief Constructor for profiler. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 65 | * @param[in] name A friendly name for this profiler. |
| 66 | **/ |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame^] | 67 | Profiler(const char* name); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 68 | |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame^] | 69 | /** Default constructor. */ |
| 70 | Profiler(); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 71 | |
| 72 | /** Default destructor. */ |
| 73 | ~Profiler() = default; |
| 74 | |
| 75 | /** @brief Start profiling => get starting time-stamp. */ |
| 76 | bool StartProfiling(const char* name = nullptr); |
| 77 | |
| 78 | /** @brief Stop profiling => get the ending time-stamp. */ |
| 79 | bool StopProfiling(); |
| 80 | |
| 81 | /** @brief Stops the profiling and internally resets the |
| 82 | * platform timers. */ |
| 83 | bool StopProfilingAndReset(); |
| 84 | |
| 85 | /** @brief Reset the platform timers. */ |
| 86 | void Reset(); |
| 87 | |
| 88 | /** |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 89 | * @brief Collects profiling results statistics and resets the profiler. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 90 | **/ |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 91 | void GetAllResultsAndReset(std::vector<ProfileResult>& results); |
| 92 | |
| 93 | /** |
| 94 | * @brief Prints collected profiling results and resets the profiler. |
| 95 | **/ |
| 96 | void PrintProfilingResult(bool printFullStat = false); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 97 | |
| 98 | /** @brief Set the profiler name. */ |
| 99 | void SetName(const char* str); |
| 100 | |
| 101 | private: |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 102 | ProfilingMap m_series; /* Profiling series map. */ |
Kshitij Sisodia | da2ec06 | 2022-04-01 14:43:53 +0100 | [diff] [blame] | 103 | pmu_counters m_tstampSt{}; /* Container for a current starting timestamp. */ |
| 104 | pmu_counters m_tstampEnd{}; /* Container for a current ending timestamp. */ |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 105 | bool m_started = false; /* Indicates profiler has been started. */ |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 106 | std::string m_name; /* Name given to this profiler. */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 107 | |
| 108 | /** |
| 109 | * @brief Appends the profiling unit computed by the "start" and |
| 110 | * "end" timestamps to the profiling series identified by |
| 111 | * the name provided. |
| 112 | * @param[in] start Starting time-stamp. |
| 113 | * @param[in] end Ending time-stamp. |
| 114 | * @param[in] name Name for the profiling unit series to be |
| 115 | * appended to. |
| 116 | **/ |
Kshitij Sisodia | da2ec06 | 2022-04-01 14:43:53 +0100 | [diff] [blame] | 117 | void AddProfilingUnit(pmu_counters start, pmu_counters end, |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 118 | const std::string& name); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | } /* namespace app */ |
| 122 | } /* namespace arm */ |
| 123 | |
| 124 | #endif /* APP_PROFILER_HPP */ |