Laurent Carlier | 749294b | 2020-06-01 09:03:17 +0100 | [diff] [blame] | 1 | // |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
| 5 | #pragma once |
| 6 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 7 | #include "ProfilingEvent.hpp" |
Keith Davis | 554fa09 | 2021-07-20 11:25:22 +0100 | [diff] [blame^] | 8 | #include "ProfilingDetails.hpp" |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 9 | |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 10 | #include <armnn/utility/IgnoreUnused.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 11 | #include "armnn/IProfiler.hpp" |
| 12 | |
| 13 | #include "WallClockTimer.hpp" |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 14 | |
| 15 | #include <chrono> |
| 16 | #include <iosfwd> |
| 17 | #include <ctime> |
| 18 | #include <vector> |
| 19 | #include <stack> |
| 20 | #include <map> |
| 21 | |
| 22 | namespace armnn |
| 23 | { |
| 24 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 25 | // Simple single-threaded profiler. |
| 26 | // Tracks events reported by BeginEvent()/EndEvent() and outputs detailed information and stats when |
| 27 | // Profiler::AnalyzeEventsAndWriteResults() is called. |
Francis Murtagh | 33199c2 | 2021-02-15 10:11:28 +0000 | [diff] [blame] | 28 | class ProfilerImpl |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 29 | { |
| 30 | public: |
Francis Murtagh | 33199c2 | 2021-02-15 10:11:28 +0000 | [diff] [blame] | 31 | ProfilerImpl(); |
| 32 | ~ProfilerImpl(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 33 | using InstrumentPtr = std::unique_ptr<Instrument>; |
| 34 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 35 | // Marks the beginning of a user-defined event. |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 36 | // No attempt will be made to copy the name string: it must be known at compile time. |
Francis Murtagh | 33199c2 | 2021-02-15 10:11:28 +0000 | [diff] [blame] | 37 | Event* BeginEvent(armnn::IProfiler* profiler, |
| 38 | const BackendId& backendId, |
| 39 | const std::string& name, |
| 40 | std::vector<InstrumentPtr>&& instruments); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 41 | |
Keith Davis | 554fa09 | 2021-07-20 11:25:22 +0100 | [diff] [blame^] | 42 | template<typename DescriptorType> |
| 43 | void AddLayerDetails(const std::string& label, |
| 44 | const DescriptorType& desc, |
| 45 | const WorkloadInfo& infos) |
| 46 | { |
| 47 | m_ProfilingDetails->AddDetailsToString(label, desc, infos); |
| 48 | } |
| 49 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 50 | // Marks the end of a user-defined event. |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 51 | void EndEvent(Event* event); |
| 52 | |
| 53 | // Enables/disables profiling. |
Francis Murtagh | 33199c2 | 2021-02-15 10:11:28 +0000 | [diff] [blame] | 54 | void EnableProfiling(bool enableProfiling); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 55 | |
| 56 | // Checks if profiling is enabled. |
Francis Murtagh | 33199c2 | 2021-02-15 10:11:28 +0000 | [diff] [blame] | 57 | bool IsProfilingEnabled(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 58 | |
| 59 | // Increments the event tag, allowing grouping of events in a user-defined manner (e.g. per inference). |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 60 | void UpdateEventTag(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 61 | |
| 62 | // Analyzes the tracked events and writes the results to the given output stream. |
| 63 | // Please refer to the configuration variables in Profiling.cpp to customize the information written. |
Francis Murtagh | 33199c2 | 2021-02-15 10:11:28 +0000 | [diff] [blame] | 64 | void AnalyzeEventsAndWriteResults(std::ostream& outStream) const; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 65 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 66 | // Print stats for events in JSON Format to the given output stream. |
Francis Murtagh | 33199c2 | 2021-02-15 10:11:28 +0000 | [diff] [blame] | 67 | void Print(std::ostream& outStream) const; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 68 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 69 | // Gets the color to render an event with, based on which device it denotes. |
Matthew Bentham | 1b63d6c | 2019-04-05 09:35:15 +0100 | [diff] [blame] | 70 | uint32_t GetEventColor(const BackendId& backendId) const; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 71 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 72 | using EventPtr = std::unique_ptr<Event>; |
Keith Davis | 554fa09 | 2021-07-20 11:25:22 +0100 | [diff] [blame^] | 73 | using DescPtr = std::unique_ptr<ProfilingDetails>; |
| 74 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 75 | struct Marker |
| 76 | { |
| 77 | std::size_t m_Id; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | struct ProfilingEventStats |
| 81 | { |
| 82 | double m_TotalMs; |
| 83 | double m_MinMs; |
| 84 | double m_MaxMs; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 85 | uint32_t m_Count; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 88 | template<typename EventIterType> |
| 89 | void AnalyzeEventSequenceAndWriteResults(EventIterType first, EventIterType last, std::ostream& outStream) const; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 90 | |
| 91 | std::map<std::string, ProfilingEventStats> CalculateProfilingEventStats() const; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 92 | void PopulateInferences(std::vector<const Event*>& outInferences, int& outBaseLevel) const; |
| 93 | void PopulateDescendants(std::map<const Event*, std::vector<const Event*>>& outDescendantsMap) const; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 94 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 95 | std::stack<Event*> m_Parents; |
| 96 | std::vector<EventPtr> m_EventSequence; |
Keith Davis | 554fa09 | 2021-07-20 11:25:22 +0100 | [diff] [blame^] | 97 | DescPtr m_ProfilingDetails = std::make_unique<ProfilingDetails>(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 98 | bool m_ProfilingEnabled; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 101 | // Singleton profiler manager. |
| 102 | // Keeps track of all the running profiler instances. |
| 103 | class ProfilerManager |
| 104 | { |
| 105 | public: |
| 106 | // Register the given profiler as a thread local pointer. |
Francis Murtagh | 33199c2 | 2021-02-15 10:11:28 +0000 | [diff] [blame] | 107 | void RegisterProfiler(IProfiler* profiler); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 108 | |
| 109 | // Gets the thread local pointer to the profiler. |
Francis Murtagh | 33199c2 | 2021-02-15 10:11:28 +0000 | [diff] [blame] | 110 | IProfiler* GetProfiler(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 111 | |
| 112 | // Accesses the singleton. |
| 113 | static ProfilerManager& GetInstance(); |
| 114 | |
| 115 | private: |
| 116 | // The constructor is kept private so that other instances of this class (other that the singleton's) |
| 117 | // can't be allocated. |
| 118 | ProfilerManager() {} |
| 119 | }; |
| 120 | |
| 121 | // Helper to easily add event markers to the codebase. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 122 | class ScopedProfilingEvent |
| 123 | { |
| 124 | public: |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 125 | using InstrumentPtr = std::unique_ptr<Instrument>; |
| 126 | |
| 127 | template<typename... Args> |
Derek Lamberti | a08d29b | 2020-06-19 14:33:05 +0100 | [diff] [blame] | 128 | ScopedProfilingEvent(const BackendId& backendId, const std::string& name, Args&&... args) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 129 | : m_Event(nullptr) |
| 130 | , m_Profiler(ProfilerManager::GetInstance().GetProfiler()) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 131 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 132 | if (m_Profiler && m_Profiler->IsProfilingEnabled()) |
| 133 | { |
| 134 | std::vector<InstrumentPtr> instruments(0); |
| 135 | instruments.reserve(sizeof...(args)); //One allocation |
Derek Lamberti | a08d29b | 2020-06-19 14:33:05 +0100 | [diff] [blame] | 136 | ConstructNextInVector(instruments, std::forward<Args>(args)...); |
Matteo Martincigh | 992d6dc | 2019-01-10 17:34:20 +0000 | [diff] [blame] | 137 | m_Event = m_Profiler->BeginEvent(backendId, name, std::move(instruments)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 138 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | ~ScopedProfilingEvent() |
| 142 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 143 | if (m_Profiler && m_Event) |
| 144 | { |
Francis Murtagh | 33199c2 | 2021-02-15 10:11:28 +0000 | [diff] [blame] | 145 | m_Profiler->pProfilerImpl->EndEvent(m_Event); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 146 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | private: |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 150 | |
| 151 | void ConstructNextInVector(std::vector<InstrumentPtr>& instruments) |
| 152 | { |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 153 | IgnoreUnused(instruments); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | template<typename Arg, typename... Args> |
Derek Lamberti | a08d29b | 2020-06-19 14:33:05 +0100 | [diff] [blame] | 157 | void ConstructNextInVector(std::vector<InstrumentPtr>& instruments, Arg&& arg, Args&&... args) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 158 | { |
Derek Lamberti | a08d29b | 2020-06-19 14:33:05 +0100 | [diff] [blame] | 159 | instruments.emplace_back(std::make_unique<Arg>(std::forward<Arg>(arg))); |
| 160 | ConstructNextInVector(instruments, std::forward<Args>(args)...); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 161 | } |
| 162 | |
Matteo Martincigh | 992d6dc | 2019-01-10 17:34:20 +0000 | [diff] [blame] | 163 | Event* m_Event; ///< Event to track |
Francis Murtagh | 33199c2 | 2021-02-15 10:11:28 +0000 | [diff] [blame] | 164 | IProfiler* m_Profiler; ///< Profiler used |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 165 | }; |
| 166 | |
Keith Davis | 554fa09 | 2021-07-20 11:25:22 +0100 | [diff] [blame^] | 167 | // Helper to easily add operator details during profiling. |
| 168 | class ScopedProfilingUpdateDescriptions |
| 169 | { |
| 170 | public: |
| 171 | template<typename DescriptorType> |
| 172 | ScopedProfilingUpdateDescriptions(const std::string& name, const DescriptorType& desc, const WorkloadInfo& infos) |
| 173 | : m_Profiler(ProfilerManager::GetInstance().GetProfiler()) |
| 174 | { |
| 175 | if (m_Profiler && m_Profiler->IsProfilingEnabled()) |
| 176 | { |
| 177 | m_Profiler->AddLayerDetails(name, desc, infos); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | ~ScopedProfilingUpdateDescriptions() |
| 182 | {} |
| 183 | |
| 184 | private: |
| 185 | |
| 186 | IProfiler* m_Profiler; ///< Profiler used |
| 187 | }; |
| 188 | |
| 189 | template<typename DescriptorType> |
| 190 | void IProfiler::AddLayerDetails(const std::string& name, |
| 191 | const DescriptorType& desc, |
| 192 | const WorkloadInfo& infos) |
| 193 | { |
| 194 | return pProfilerImpl->AddLayerDetails(name, desc, infos); |
| 195 | } |
| 196 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 197 | } // namespace armnn |
| 198 | |
Keith Davis | 554fa09 | 2021-07-20 11:25:22 +0100 | [diff] [blame^] | 199 | // Event Definitions for profiling |
Jim Flynn | c456c31 | 2020-10-13 14:40:29 +0100 | [diff] [blame] | 200 | #define ARMNN_SCOPED_PROFILING_EVENT_WITH_INSTRUMENTS_UNIQUE_LOC_INNER(lineNumber, backendId, /*name,*/ ...) \ |
| 201 | armnn::ScopedProfilingEvent e_ ## lineNumber(backendId, /*name,*/ __VA_ARGS__); |
Derek Lamberti | 6b4dfc2 | 2019-08-07 17:01:57 +0100 | [diff] [blame] | 202 | |
Jim Flynn | c456c31 | 2020-10-13 14:40:29 +0100 | [diff] [blame] | 203 | #define ARMNN_SCOPED_PROFILING_EVENT_WITH_INSTRUMENTS_UNIQUE_LOC(lineNumber, backendId, /*name,*/ ...) \ |
| 204 | ARMNN_SCOPED_PROFILING_EVENT_WITH_INSTRUMENTS_UNIQUE_LOC_INNER(lineNumber, backendId, /*name,*/ __VA_ARGS__) |
Derek Lamberti | 6b4dfc2 | 2019-08-07 17:01:57 +0100 | [diff] [blame] | 205 | |
Jim Flynn | c456c31 | 2020-10-13 14:40:29 +0100 | [diff] [blame] | 206 | // The event name must be known at compile time i.e. if you are going to use this version of the macro |
| 207 | // in code the first argument you supply after the backendId must be the name. |
| 208 | // NOTE: need to pass the line number as an argument from here so by the time it gets to the UNIQUE_LOC_INNER |
| 209 | // above it has expanded to a string and will concat (##) correctly with the 'e_' prefix to yield a |
| 210 | // legal and unique variable name (so long as you don't use the macro twice on the same line). |
| 211 | // The concat preprocessing operator (##) very unhelpfully will not expand macros see |
| 212 | // https://gcc.gnu.org/onlinedocs/cpp/Concatenation.html for the gory details. |
Matteo Martincigh | 992d6dc | 2019-01-10 17:34:20 +0000 | [diff] [blame] | 213 | #define ARMNN_SCOPED_PROFILING_EVENT_WITH_INSTRUMENTS(backendId, /*name,*/ ...) \ |
Jim Flynn | c456c31 | 2020-10-13 14:40:29 +0100 | [diff] [blame] | 214 | ARMNN_SCOPED_PROFILING_EVENT_WITH_INSTRUMENTS_UNIQUE_LOC(__LINE__,backendId, /*name,*/ __VA_ARGS__) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 215 | |
Matteo Martincigh | 992d6dc | 2019-01-10 17:34:20 +0000 | [diff] [blame] | 216 | #define ARMNN_SCOPED_PROFILING_EVENT(backendId, name) \ |
| 217 | ARMNN_SCOPED_PROFILING_EVENT_WITH_INSTRUMENTS(backendId, name, armnn::WallClockTimer()) |
Keith Davis | 554fa09 | 2021-07-20 11:25:22 +0100 | [diff] [blame^] | 218 | |
| 219 | // Workload Description definitons for profiling |
| 220 | #define ARMNN_REPORT_PROFILING_WORKLOAD_DESC_UNIQUE_LOC_INNER(lineNumber, name, desc, infos) \ |
| 221 | armnn::ScopedProfilingUpdateDescriptions e_ ## lineNumber(name, desc, infos); |
| 222 | |
| 223 | #define ARMNN_REPORT_PROFILING_WORKLOAD_DESC_UNIQUE_LOC(lineNumber, name, desc, infos) \ |
| 224 | ARMNN_REPORT_PROFILING_WORKLOAD_DESC_UNIQUE_LOC_INNER(lineNumber, name, desc, infos) |
| 225 | |
| 226 | #define ARMNN_REPORT_PROFILING_WORKLOAD_DESC(name, desc, infos) \ |
| 227 | ARMNN_REPORT_PROFILING_WORKLOAD_DESC_UNIQUE_LOC(__LINE__, name, desc, infos) |