telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1 | // |
| 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. |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 27 | class Profiler final : public IProfiler |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 28 | { |
| 29 | public: |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 30 | Profiler(); |
| 31 | ~Profiler(); |
| 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. |
Matteo Martincigh | 992d6dc | 2019-01-10 17:34:20 +0000 | [diff] [blame] | 36 | Event* BeginEvent(const BackendId& backendId, const std::string& name, std::vector<InstrumentPtr>&& instruments); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 37 | |
| 38 | // Marks the end of a user-defined event. |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 39 | void EndEvent(Event* event); |
| 40 | |
| 41 | // Enables/disables profiling. |
| 42 | void EnableProfiling(bool enableProfiling) override; |
| 43 | |
| 44 | // Checks if profiling is enabled. |
| 45 | bool IsProfilingEnabled() override; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 46 | |
| 47 | // 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] | 48 | void UpdateEventTag(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 49 | |
| 50 | // Analyzes the tracked events and writes the results to the given output stream. |
| 51 | // Please refer to the configuration variables in Profiling.cpp to customize the information written. |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 52 | void AnalyzeEventsAndWriteResults(std::ostream& outStream) const override; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 53 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 54 | // Print stats for events in JSON Format to the given output stream. |
| 55 | void Print(std::ostream& outStream) const override; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 56 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 57 | // 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] | 58 | uint32_t GetEventColor(const BackendId& backendId) const; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 59 | |
| 60 | private: |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 61 | using EventPtr = std::unique_ptr<Event>; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 62 | struct Marker |
| 63 | { |
| 64 | std::size_t m_Id; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | struct ProfilingEventStats |
| 68 | { |
| 69 | double m_TotalMs; |
| 70 | double m_MinMs; |
| 71 | double m_MaxMs; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 72 | uint32_t m_Count; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 75 | template<typename EventIterType> |
| 76 | void AnalyzeEventSequenceAndWriteResults(EventIterType first, EventIterType last, std::ostream& outStream) const; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 77 | |
| 78 | std::map<std::string, ProfilingEventStats> CalculateProfilingEventStats() const; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 79 | void PopulateInferences(std::vector<const Event*>& outInferences, int& outBaseLevel) const; |
| 80 | void PopulateDescendants(std::map<const Event*, std::vector<const Event*>>& outDescendantsMap) const; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 81 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 82 | std::stack<Event*> m_Parents; |
| 83 | std::vector<EventPtr> m_EventSequence; |
| 84 | bool m_ProfilingEnabled; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 85 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 86 | private: |
| 87 | // Friend functions for unit testing, see ProfilerTests.cpp. |
| 88 | friend size_t GetProfilerEventSequenceSize(armnn::Profiler* profiler); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 91 | // Singleton profiler manager. |
| 92 | // Keeps track of all the running profiler instances. |
| 93 | class ProfilerManager |
| 94 | { |
| 95 | public: |
| 96 | // Register the given profiler as a thread local pointer. |
| 97 | void RegisterProfiler(Profiler* profiler); |
| 98 | |
| 99 | // Gets the thread local pointer to the profiler. |
| 100 | Profiler* GetProfiler(); |
| 101 | |
| 102 | // Accesses the singleton. |
| 103 | static ProfilerManager& GetInstance(); |
| 104 | |
| 105 | private: |
| 106 | // The constructor is kept private so that other instances of this class (other that the singleton's) |
| 107 | // can't be allocated. |
| 108 | ProfilerManager() {} |
| 109 | }; |
| 110 | |
| 111 | // Helper to easily add event markers to the codebase. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 112 | class ScopedProfilingEvent |
| 113 | { |
| 114 | public: |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 115 | using InstrumentPtr = std::unique_ptr<Instrument>; |
| 116 | |
| 117 | template<typename... Args> |
Matteo Martincigh | 992d6dc | 2019-01-10 17:34:20 +0000 | [diff] [blame] | 118 | ScopedProfilingEvent(const BackendId& backendId, const std::string& name, Args... args) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 119 | : m_Event(nullptr) |
| 120 | , m_Profiler(ProfilerManager::GetInstance().GetProfiler()) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 121 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 122 | if (m_Profiler && m_Profiler->IsProfilingEnabled()) |
| 123 | { |
| 124 | std::vector<InstrumentPtr> instruments(0); |
| 125 | instruments.reserve(sizeof...(args)); //One allocation |
| 126 | ConstructNextInVector(instruments, args...); |
Matteo Martincigh | 992d6dc | 2019-01-10 17:34:20 +0000 | [diff] [blame] | 127 | m_Event = m_Profiler->BeginEvent(backendId, name, std::move(instruments)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 128 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | ~ScopedProfilingEvent() |
| 132 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 133 | if (m_Profiler && m_Event) |
| 134 | { |
| 135 | m_Profiler->EndEvent(m_Event); |
| 136 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | private: |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 140 | |
| 141 | void ConstructNextInVector(std::vector<InstrumentPtr>& instruments) |
| 142 | { |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame^] | 143 | IgnoreUnused(instruments); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | template<typename Arg, typename... Args> |
| 147 | void ConstructNextInVector(std::vector<InstrumentPtr>& instruments, Arg arg, Args... args) |
| 148 | { |
| 149 | instruments.emplace_back(std::make_unique<Arg>(arg)); |
| 150 | ConstructNextInVector(instruments, args...); |
| 151 | } |
| 152 | |
Matteo Martincigh | 992d6dc | 2019-01-10 17:34:20 +0000 | [diff] [blame] | 153 | Event* m_Event; ///< Event to track |
| 154 | Profiler* m_Profiler; ///< Profiler used |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | } // namespace armnn |
| 158 | |
Derek Lamberti | 6b4dfc2 | 2019-08-07 17:01:57 +0100 | [diff] [blame] | 159 | |
| 160 | #include <boost/preprocessor.hpp> |
| 161 | |
| 162 | #define ARMNN_SCOPED_PROFILING_EVENT_WITH_INSTRUMENTS_UNIQUE_LOC(backendId, /*name,*/ ...) \ |
| 163 | armnn::ScopedProfilingEvent BOOST_PP_CAT(e_,__LINE__)(backendId, /*name,*/ __VA_ARGS__); |
| 164 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 165 | // The event name must be known at compile time |
Matteo Martincigh | 992d6dc | 2019-01-10 17:34:20 +0000 | [diff] [blame] | 166 | #define ARMNN_SCOPED_PROFILING_EVENT_WITH_INSTRUMENTS(backendId, /*name,*/ ...) \ |
Derek Lamberti | 6b4dfc2 | 2019-08-07 17:01:57 +0100 | [diff] [blame] | 167 | ARMNN_SCOPED_PROFILING_EVENT_WITH_INSTRUMENTS_UNIQUE_LOC(backendId, /*name,*/ __VA_ARGS__); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 168 | |
Matteo Martincigh | 992d6dc | 2019-01-10 17:34:20 +0000 | [diff] [blame] | 169 | #define ARMNN_SCOPED_PROFILING_EVENT(backendId, name) \ |
| 170 | ARMNN_SCOPED_PROFILING_EVENT_WITH_INSTRUMENTS(backendId, name, armnn::WallClockTimer()) |