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