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