blob: 0b2e46d46ddac998f5848995d9bb1c2db16bd63c [file] [log] [blame]
telsoa014fcda012018-03-09 14:13:49 +00001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa014fcda012018-03-09 14:13:49 +00004//
5#pragma once
6
telsoa01c577f2c2018-08-31 09:22:23 +01007#include "ProfilingEvent.hpp"
telsoa014fcda012018-03-09 14:13:49 +00008
9#include "armnn/ArmNN.hpp"
telsoa01c577f2c2018-08-31 09:22:23 +010010#include "armnn/IProfiler.hpp"
11
12#include "WallClockTimer.hpp"
telsoa014fcda012018-03-09 14:13:49 +000013
14#include <chrono>
15#include <iosfwd>
16#include <ctime>
17#include <vector>
18#include <stack>
19#include <map>
20
telsoa01c577f2c2018-08-31 09:22:23 +010021#include <boost/core/ignore_unused.hpp>
22
telsoa014fcda012018-03-09 14:13:49 +000023namespace armnn
24{
25
telsoa014fcda012018-03-09 14:13:49 +000026// Simple single-threaded profiler.
27// Tracks events reported by BeginEvent()/EndEvent() and outputs detailed information and stats when
28// Profiler::AnalyzeEventsAndWriteResults() is called.
telsoa01c577f2c2018-08-31 09:22:23 +010029class Profiler final : public IProfiler
telsoa014fcda012018-03-09 14:13:49 +000030{
31public:
telsoa01c577f2c2018-08-31 09:22:23 +010032 Profiler();
33 ~Profiler();
34 using InstrumentPtr = std::unique_ptr<Instrument>;
35
telsoa014fcda012018-03-09 14:13:49 +000036 // Marks the beginning of a user-defined event.
telsoa01c577f2c2018-08-31 09:22:23 +010037 // No attempt will be made to copy the name string: it must be known at compile time.
Matteo Martincigh992d6dc2019-01-10 17:34:20 +000038 Event* BeginEvent(const BackendId& backendId, const std::string& name, std::vector<InstrumentPtr>&& instruments);
telsoa014fcda012018-03-09 14:13:49 +000039
40 // Marks the end of a user-defined event.
telsoa01c577f2c2018-08-31 09:22:23 +010041 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;
telsoa014fcda012018-03-09 14:13:49 +000048
49 // Increments the event tag, allowing grouping of events in a user-defined manner (e.g. per inference).
telsoa01c577f2c2018-08-31 09:22:23 +010050 void UpdateEventTag();
telsoa014fcda012018-03-09 14:13:49 +000051
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.
telsoa01c577f2c2018-08-31 09:22:23 +010054 void AnalyzeEventsAndWriteResults(std::ostream& outStream) const override;
telsoa014fcda012018-03-09 14:13:49 +000055
telsoa01c577f2c2018-08-31 09:22:23 +010056 // Print stats for events in JSON Format to the given output stream.
57 void Print(std::ostream& outStream) const override;
telsoa014fcda012018-03-09 14:13:49 +000058
telsoa01c577f2c2018-08-31 09:22:23 +010059 // Gets the color to render an event with, based on which device it denotes.
Matthew Bentham1b63d6c2019-04-05 09:35:15 +010060 uint32_t GetEventColor(const BackendId& backendId) const;
telsoa014fcda012018-03-09 14:13:49 +000061
62private:
telsoa01c577f2c2018-08-31 09:22:23 +010063 using EventPtr = std::unique_ptr<Event>;
telsoa014fcda012018-03-09 14:13:49 +000064 struct Marker
65 {
66 std::size_t m_Id;
telsoa014fcda012018-03-09 14:13:49 +000067 };
68
69 struct ProfilingEventStats
70 {
71 double m_TotalMs;
72 double m_MinMs;
73 double m_MaxMs;
telsoa01c577f2c2018-08-31 09:22:23 +010074 uint32_t m_Count;
telsoa014fcda012018-03-09 14:13:49 +000075 };
76
telsoa01c577f2c2018-08-31 09:22:23 +010077 template<typename EventIterType>
78 void AnalyzeEventSequenceAndWriteResults(EventIterType first, EventIterType last, std::ostream& outStream) const;
telsoa014fcda012018-03-09 14:13:49 +000079
80 std::map<std::string, ProfilingEventStats> CalculateProfilingEventStats() const;
telsoa01c577f2c2018-08-31 09:22:23 +010081 void PopulateInferences(std::vector<const Event*>& outInferences, int& outBaseLevel) const;
82 void PopulateDescendants(std::map<const Event*, std::vector<const Event*>>& outDescendantsMap) const;
telsoa014fcda012018-03-09 14:13:49 +000083
telsoa01c577f2c2018-08-31 09:22:23 +010084 std::stack<Event*> m_Parents;
85 std::vector<EventPtr> m_EventSequence;
86 bool m_ProfilingEnabled;
telsoa014fcda012018-03-09 14:13:49 +000087
telsoa01c577f2c2018-08-31 09:22:23 +010088private:
89 // Friend functions for unit testing, see ProfilerTests.cpp.
90 friend size_t GetProfilerEventSequenceSize(armnn::Profiler* profiler);
telsoa014fcda012018-03-09 14:13:49 +000091};
92
telsoa01c577f2c2018-08-31 09:22:23 +010093// Singleton profiler manager.
94// Keeps track of all the running profiler instances.
95class ProfilerManager
96{
97public:
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
107private:
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.
telsoa014fcda012018-03-09 14:13:49 +0000114class ScopedProfilingEvent
115{
116public:
telsoa01c577f2c2018-08-31 09:22:23 +0100117 using InstrumentPtr = std::unique_ptr<Instrument>;
118
119 template<typename... Args>
Matteo Martincigh992d6dc2019-01-10 17:34:20 +0000120 ScopedProfilingEvent(const BackendId& backendId, const std::string& name, Args... args)
telsoa01c577f2c2018-08-31 09:22:23 +0100121 : m_Event(nullptr)
122 , m_Profiler(ProfilerManager::GetInstance().GetProfiler())
telsoa014fcda012018-03-09 14:13:49 +0000123 {
telsoa01c577f2c2018-08-31 09:22:23 +0100124 if (m_Profiler && m_Profiler->IsProfilingEnabled())
125 {
126 std::vector<InstrumentPtr> instruments(0);
127 instruments.reserve(sizeof...(args)); //One allocation
128 ConstructNextInVector(instruments, args...);
Matteo Martincigh992d6dc2019-01-10 17:34:20 +0000129 m_Event = m_Profiler->BeginEvent(backendId, name, std::move(instruments));
telsoa01c577f2c2018-08-31 09:22:23 +0100130 }
telsoa014fcda012018-03-09 14:13:49 +0000131 }
132
133 ~ScopedProfilingEvent()
134 {
telsoa01c577f2c2018-08-31 09:22:23 +0100135 if (m_Profiler && m_Event)
136 {
137 m_Profiler->EndEvent(m_Event);
138 }
telsoa014fcda012018-03-09 14:13:49 +0000139 }
140
141private:
telsoa01c577f2c2018-08-31 09:22:23 +0100142
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
Matteo Martincigh992d6dc2019-01-10 17:34:20 +0000155 Event* m_Event; ///< Event to track
156 Profiler* m_Profiler; ///< Profiler used
telsoa014fcda012018-03-09 14:13:49 +0000157};
158
159} // namespace armnn
160
telsoa014fcda012018-03-09 14:13:49 +0000161// The event name must be known at compile time
Matteo Martincigh992d6dc2019-01-10 17:34:20 +0000162#define ARMNN_SCOPED_PROFILING_EVENT_WITH_INSTRUMENTS(backendId, /*name,*/ ...) \
163 armnn::ScopedProfilingEvent e_##__FILE__##__LINE__(backendId, /*name,*/ __VA_ARGS__);
telsoa014fcda012018-03-09 14:13:49 +0000164
Matteo Martincigh992d6dc2019-01-10 17:34:20 +0000165#define ARMNN_SCOPED_PROFILING_EVENT(backendId, name) \
166 ARMNN_SCOPED_PROFILING_EVENT_WITH_INSTRUMENTS(backendId, name, armnn::WallClockTimer())