alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021 Arm Limited. All rights reserved. |
| 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 { |
| 48 | uint64_t npuCycles = 0; |
| 49 | uint64_t activeNpuCycles = 0; |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 50 | uint64_t idleNpuCycles = 0; |
| 51 | uint64_t axi0writes = 0; |
| 52 | uint64_t axi0reads = 0; |
| 53 | uint64_t axi1reads = 0; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 54 | uint64_t cpuCycles = 0; |
| 55 | time_t time = 0; |
| 56 | }; |
| 57 | |
| 58 | /* A collection of profiling units. */ |
| 59 | using ProfilingSeries = std::vector<arm::app::ProfilingUnit>; |
| 60 | |
| 61 | /* A map for string identifiable profiling series. */ |
| 62 | using ProfilingMap = std::map<std::string, ProfilingSeries>; |
| 63 | |
| 64 | /** |
| 65 | * @brief A very simple profiler example using the platform timer |
| 66 | * implementation. |
| 67 | */ |
| 68 | class Profiler { |
| 69 | public: |
| 70 | /** |
| 71 | * @brief Constructor for profiler. |
| 72 | * @param[in] platform Pointer to a valid, initialised hal platform. |
| 73 | * @param[in] name A friendly name for this profiler. |
| 74 | **/ |
| 75 | Profiler(hal_platform* platform, const char* name); |
| 76 | |
| 77 | /** Block the default constructor. */ |
| 78 | Profiler() = delete; |
| 79 | |
| 80 | /** Default destructor. */ |
| 81 | ~Profiler() = default; |
| 82 | |
| 83 | /** @brief Start profiling => get starting time-stamp. */ |
| 84 | bool StartProfiling(const char* name = nullptr); |
| 85 | |
| 86 | /** @brief Stop profiling => get the ending time-stamp. */ |
| 87 | bool StopProfiling(); |
| 88 | |
| 89 | /** @brief Stops the profiling and internally resets the |
| 90 | * platform timers. */ |
| 91 | bool StopProfilingAndReset(); |
| 92 | |
| 93 | /** @brief Reset the platform timers. */ |
| 94 | void Reset(); |
| 95 | |
| 96 | /** |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 97 | * @brief Collects profiling results statistics and resets the profiler. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 98 | **/ |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 99 | void GetAllResultsAndReset(std::vector<ProfileResult>& results); |
| 100 | |
| 101 | /** |
| 102 | * @brief Prints collected profiling results and resets the profiler. |
| 103 | **/ |
| 104 | void PrintProfilingResult(bool printFullStat = false); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 105 | |
| 106 | /** @brief Set the profiler name. */ |
| 107 | void SetName(const char* str); |
| 108 | |
| 109 | private: |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 110 | ProfilingMap m_series; /* Profiling series map. */ |
| 111 | time_counter m_tstampSt{}; /* Container for a current starting timestamp. */ |
| 112 | time_counter m_tstampEnd{}; /* Container for a current ending timestamp. */ |
| 113 | hal_platform * m_pPlatform = nullptr; /* Platform pointer - to get the timer. */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 114 | |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 115 | bool m_started = false; /* Indicates profiler has been started. */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 116 | |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 117 | std::string m_name; /* Name given to this profiler. */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 118 | |
| 119 | /** |
| 120 | * @brief Appends the profiling unit computed by the "start" and |
| 121 | * "end" timestamps to the profiling series identified by |
| 122 | * the name provided. |
| 123 | * @param[in] start Starting time-stamp. |
| 124 | * @param[in] end Ending time-stamp. |
| 125 | * @param[in] name Name for the profiling unit series to be |
| 126 | * appended to. |
| 127 | **/ |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 128 | void AddProfilingUnit(time_counter start, time_counter end, |
| 129 | const std::string& name); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | } /* namespace app */ |
| 133 | } /* namespace arm */ |
| 134 | |
| 135 | #endif /* APP_PROFILER_HPP */ |