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 | |
| 29 | /** A single profiling unit definition. */ |
| 30 | struct ProfilingUnit { |
| 31 | uint64_t npuCycles = 0; |
| 32 | uint64_t activeNpuCycles = 0; |
| 33 | uint64_t cpuCycles = 0; |
| 34 | time_t time = 0; |
| 35 | }; |
| 36 | |
| 37 | /* A collection of profiling units. */ |
| 38 | using ProfilingSeries = std::vector<arm::app::ProfilingUnit>; |
| 39 | |
| 40 | /* A map for string identifiable profiling series. */ |
| 41 | using ProfilingMap = std::map<std::string, ProfilingSeries>; |
| 42 | |
| 43 | /** |
| 44 | * @brief A very simple profiler example using the platform timer |
| 45 | * implementation. |
| 46 | */ |
| 47 | class Profiler { |
| 48 | public: |
| 49 | /** |
| 50 | * @brief Constructor for profiler. |
| 51 | * @param[in] platform Pointer to a valid, initialised hal platform. |
| 52 | * @param[in] name A friendly name for this profiler. |
| 53 | **/ |
| 54 | Profiler(hal_platform* platform, const char* name); |
| 55 | |
| 56 | /** Block the default constructor. */ |
| 57 | Profiler() = delete; |
| 58 | |
| 59 | /** Default destructor. */ |
| 60 | ~Profiler() = default; |
| 61 | |
| 62 | /** @brief Start profiling => get starting time-stamp. */ |
| 63 | bool StartProfiling(const char* name = nullptr); |
| 64 | |
| 65 | /** @brief Stop profiling => get the ending time-stamp. */ |
| 66 | bool StopProfiling(); |
| 67 | |
| 68 | /** @brief Stops the profiling and internally resets the |
| 69 | * platform timers. */ |
| 70 | bool StopProfilingAndReset(); |
| 71 | |
| 72 | /** @brief Reset the platform timers. */ |
| 73 | void Reset(); |
| 74 | |
| 75 | /** |
| 76 | * @brief Gets the results as string and resets the profiler. |
| 77 | * @returns Result string. |
| 78 | **/ |
| 79 | std::string GetResultsAndReset(); |
| 80 | |
| 81 | /** @brief Set the profiler name. */ |
| 82 | void SetName(const char* str); |
| 83 | |
| 84 | private: |
| 85 | ProfilingMap _m_series; /* Profiling series map. */ |
| 86 | time_counter _m_tstampSt; /* Container for a current starting timestamp. */ |
| 87 | time_counter _m_tstampEnd; /* Container for a current ending timestamp. */ |
| 88 | hal_platform * _m_pPlatform = nullptr; /* Platform pointer - to get the timer. */ |
| 89 | |
| 90 | bool _m_started = false; /* Indicates profiler has been started. */ |
| 91 | |
| 92 | std::string _m_name; /* Name given to this profiler. */ |
| 93 | |
| 94 | /** |
| 95 | * @brief Appends the profiling unit computed by the "start" and |
| 96 | * "end" timestamps to the profiling series identified by |
| 97 | * the name provided. |
| 98 | * @param[in] start Starting time-stamp. |
| 99 | * @param[in] end Ending time-stamp. |
| 100 | * @param[in] name Name for the profiling unit series to be |
| 101 | * appended to. |
| 102 | **/ |
| 103 | void _AddProfilingUnit(time_counter start, time_counter end, |
| 104 | const std::string& name); |
| 105 | }; |
| 106 | |
| 107 | } /* namespace app */ |
| 108 | } /* namespace arm */ |
| 109 | |
| 110 | #endif /* APP_PROFILER_HPP */ |