alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
Richard Burton | bcec675 | 2023-11-03 16:21:58 +0000 | [diff] [blame] | 2 | * SPDX-FileCopyrightText: Copyright 2022-2023 Arm Limited and/or its affiliates <open-source-office@arm.com> |
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 | |
Richard Burton | bcec675 | 2023-11-03 16:21:58 +0000 | [diff] [blame] | 22 | #include <cstdint> |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 23 | #include <string> |
| 24 | #include <map> |
| 25 | #include <vector> |
| 26 | |
| 27 | namespace arm { |
| 28 | namespace app { |
| 29 | |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 30 | /** Statistics for a profiling metric. */ |
| 31 | struct Statistics { |
| 32 | std::string name; |
| 33 | std::string unit; |
| 34 | std::uint64_t total; |
| 35 | double avrg; |
| 36 | std::uint64_t min; |
| 37 | std::uint64_t max; |
Richard Burton | 2c2cacd | 2022-05-13 09:46:57 +0100 | [diff] [blame] | 38 | std::uint32_t samplesNum = 0; |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | /** Profiling results with calculated statistics. */ |
| 42 | struct ProfileResult { |
| 43 | std::string name; |
| 44 | std::uint32_t samplesNum; |
| 45 | std::vector<Statistics> data; |
| 46 | }; |
| 47 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 48 | /** A single profiling unit definition. */ |
| 49 | struct ProfilingUnit { |
Kshitij Sisodia | da2ec06 | 2022-04-01 14:43:53 +0100 | [diff] [blame] | 50 | pmu_counters counters; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
Richard Burton | 2c2cacd | 2022-05-13 09:46:57 +0100 | [diff] [blame] | 53 | /* A map for string identifiable profiling statistics. */ |
| 54 | using ProfilingStats = std::map<std::string, std::vector<Statistics>>; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * @brief A very simple profiler example using the platform timer |
| 58 | * implementation. |
| 59 | */ |
| 60 | class Profiler { |
| 61 | public: |
| 62 | /** |
| 63 | * @brief Constructor for profiler. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 64 | * @param[in] name A friendly name for this profiler. |
| 65 | **/ |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 66 | Profiler(const char* name); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 67 | |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 68 | /** Default constructor. */ |
| 69 | Profiler(); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 70 | |
| 71 | /** Default destructor. */ |
| 72 | ~Profiler() = default; |
| 73 | |
| 74 | /** @brief Start profiling => get starting time-stamp. */ |
| 75 | bool StartProfiling(const char* name = nullptr); |
| 76 | |
| 77 | /** @brief Stop profiling => get the ending time-stamp. */ |
| 78 | bool StopProfiling(); |
| 79 | |
| 80 | /** @brief Stops the profiling and internally resets the |
| 81 | * platform timers. */ |
| 82 | bool StopProfilingAndReset(); |
| 83 | |
| 84 | /** @brief Reset the platform timers. */ |
| 85 | void Reset(); |
| 86 | |
| 87 | /** |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 88 | * @brief Collects profiling results statistics and resets the profiler. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 89 | **/ |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 90 | void GetAllResultsAndReset(std::vector<ProfileResult>& results); |
| 91 | |
| 92 | /** |
| 93 | * @brief Prints collected profiling results and resets the profiler. |
| 94 | **/ |
| 95 | void PrintProfilingResult(bool printFullStat = false); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 96 | |
| 97 | /** @brief Set the profiler name. */ |
| 98 | void SetName(const char* str); |
| 99 | |
| 100 | private: |
Richard Burton | 2c2cacd | 2022-05-13 09:46:57 +0100 | [diff] [blame] | 101 | ProfilingStats m_profStats; /* Profiling stats map. */ |
| 102 | pmu_counters m_tstampSt{}; /* Container for a current starting timestamp. */ |
| 103 | pmu_counters m_tstampEnd{}; /* Container for a current ending timestamp. */ |
| 104 | bool m_started = false; /* Indicates profiler has been started. */ |
| 105 | std::string m_name; /* Name given to this profiler. */ |
| 106 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 107 | |
| 108 | /** |
Richard Burton | 2c2cacd | 2022-05-13 09:46:57 +0100 | [diff] [blame] | 109 | * @brief Updates the running average stats with those computed |
| 110 | * by the "start" and "end" timestamps for the profiling |
| 111 | * stats identified by the name provided. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 112 | * @param[in] start Starting time-stamp. |
| 113 | * @param[in] end Ending time-stamp. |
Richard Burton | 2c2cacd | 2022-05-13 09:46:57 +0100 | [diff] [blame] | 114 | * @param[in] name Name for the profiling running stats series to be |
| 115 | * updated. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 116 | **/ |
Richard Burton | 2c2cacd | 2022-05-13 09:46:57 +0100 | [diff] [blame] | 117 | void UpdateRunningStats(pmu_counters start, pmu_counters end, |
| 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 */ |