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 | |
| 9 | #include "armnn/ArmNN.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 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 21 | #include <boost/core/ignore_unused.hpp> |
| 22 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 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. |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 29 | class Profiler final : public IProfiler |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 30 | { |
| 31 | public: |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 32 | Profiler(); |
| 33 | ~Profiler(); |
| 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. |
| 38 | Event* BeginEvent(Compute compute, const std::string& name, std::vector<InstrumentPtr>&& instruments); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 39 | |
| 40 | // Marks the end of a user-defined event. |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 41 | void EndEvent(Event* event); |
| 42 | |
| 43 | // Enables/disables profiling. |
| 44 | void EnableProfiling(bool enableProfiling) override; |
| 45 | |
| 46 | // Checks if profiling is enabled. |
| 47 | bool IsProfilingEnabled() override; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 48 | |
| 49 | // 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] | 50 | void UpdateEventTag(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 51 | |
| 52 | // Analyzes the tracked events and writes the results to the given output stream. |
| 53 | // 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] | 54 | void AnalyzeEventsAndWriteResults(std::ostream& outStream) const override; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 55 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 56 | // Print stats for events in JSON Format to the given output stream. |
| 57 | void Print(std::ostream& outStream) const override; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 58 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 59 | // Gets the color to render an event with, based on which device it denotes. |
| 60 | uint32_t GetEventColor(Compute compute) const; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 61 | |
| 62 | private: |
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 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 88 | private: |
| 89 | // Friend functions for unit testing, see ProfilerTests.cpp. |
| 90 | friend size_t GetProfilerEventSequenceSize(armnn::Profiler* profiler); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 93 | // Singleton profiler manager. |
| 94 | // Keeps track of all the running profiler instances. |
| 95 | class ProfilerManager |
| 96 | { |
| 97 | public: |
| 98 | // Register the given profiler as a thread local pointer. |
| 99 | void RegisterProfiler(Profiler* profiler); |
| 100 | |
| 101 | // Gets the thread local pointer to the profiler. |
| 102 | Profiler* GetProfiler(); |
| 103 | |
| 104 | // Accesses the singleton. |
| 105 | static ProfilerManager& GetInstance(); |
| 106 | |
| 107 | private: |
| 108 | // The constructor is kept private so that other instances of this class (other that the singleton's) |
| 109 | // can't be allocated. |
| 110 | ProfilerManager() {} |
| 111 | }; |
| 112 | |
| 113 | // Helper to easily add event markers to the codebase. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 114 | class ScopedProfilingEvent |
| 115 | { |
| 116 | public: |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 117 | using InstrumentPtr = std::unique_ptr<Instrument>; |
| 118 | |
| 119 | template<typename... Args> |
| 120 | ScopedProfilingEvent(Compute compute, const std::string& name, Args... args) |
| 121 | : m_Event(nullptr) |
| 122 | , m_Profiler(ProfilerManager::GetInstance().GetProfiler()) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 123 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 124 | if (m_Profiler && m_Profiler->IsProfilingEnabled()) |
| 125 | { |
| 126 | std::vector<InstrumentPtr> instruments(0); |
| 127 | instruments.reserve(sizeof...(args)); //One allocation |
| 128 | ConstructNextInVector(instruments, args...); |
| 129 | m_Event = m_Profiler->BeginEvent(compute, name, std::move(instruments)); |
| 130 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | ~ScopedProfilingEvent() |
| 134 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 135 | if (m_Profiler && m_Event) |
| 136 | { |
| 137 | m_Profiler->EndEvent(m_Event); |
| 138 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | private: |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 142 | |
| 143 | void ConstructNextInVector(std::vector<InstrumentPtr>& instruments) |
| 144 | { |
| 145 | boost::ignore_unused(instruments); |
| 146 | } |
| 147 | |
| 148 | template<typename Arg, typename... Args> |
| 149 | void ConstructNextInVector(std::vector<InstrumentPtr>& instruments, Arg arg, Args... args) |
| 150 | { |
| 151 | instruments.emplace_back(std::make_unique<Arg>(arg)); |
| 152 | ConstructNextInVector(instruments, args...); |
| 153 | } |
| 154 | |
| 155 | Event* m_Event; ///< Event to track |
| 156 | Profiler* m_Profiler; ///< Profiler used |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | } // namespace armnn |
| 160 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 161 | // The event name must be known at compile time |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 162 | #define ARMNN_SCOPED_PROFILING_EVENT_WITH_INSTRUMENTS(compute, /*name,*/ ...) \ |
| 163 | armnn::ScopedProfilingEvent e_##__FILE__##__LINE__(compute, /*name,*/ __VA_ARGS__); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 164 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 165 | #define ARMNN_SCOPED_PROFILING_EVENT(compute, name) \ |
| 166 | ARMNN_SCOPED_PROFILING_EVENT_WITH_INSTRUMENTS(compute, name, armnn::WallClockTimer()) |