ArmNN
 20.05
ProfilingService.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
9 #include "BufferManager.hpp"
10 #include "CommandHandler.hpp"
12 #include "CounterDirectory.hpp"
13 #include "CounterIdMap.hpp"
15 #include "ICounterRegistry.hpp"
16 #include "ICounterValues.hpp"
18 #include "IProfilingService.hpp"
19 #include "IReportStructure.hpp"
27 #include "SendCounterPacket.hpp"
28 #include "SendThread.hpp"
29 #include "SendTimelinePacket.hpp"
31 #include "INotifyBackends.hpp"
33 
34 namespace armnn
35 {
36 
37 namespace profiling
38 {
39 // Static constants describing ArmNN's counter UID's
40 static const uint16_t NETWORK_LOADS = 0;
41 static const uint16_t NETWORK_UNLOADS = 1;
42 static const uint16_t REGISTERED_BACKENDS = 2;
43 static const uint16_t UNREGISTERED_BACKENDS = 3;
44 static const uint16_t INFERENCES_RUN = 4;
45 static const uint16_t MAX_ARMNN_COUNTER = INFERENCES_RUN;
46 
48 {
49 public:
51  using IProfilingConnectionFactoryPtr = std::unique_ptr<IProfilingConnectionFactory>;
52  using IProfilingConnectionPtr = std::unique_ptr<IProfilingConnection>;
53  using CounterIndices = std::vector<std::atomic<uint32_t>*>;
54  using CounterValues = std::list<std::atomic<uint32_t>>;
55  using BackendProfilingContext = std::unordered_map<BackendId,
56  std::shared_ptr<armnn::profiling::IBackendProfilingContext>>;
57 
59  : m_Options()
60  , m_TimelineReporting(false)
61  , m_CounterDirectory()
62  , m_ProfilingConnectionFactory(new ProfilingConnectionFactory())
63  , m_ProfilingConnection()
64  , m_StateMachine()
65  , m_CounterIndex()
66  , m_CounterValues()
67  , m_CommandHandlerRegistry()
68  , m_PacketVersionResolver()
69  , m_CommandHandler(1000,
70  false,
71  m_CommandHandlerRegistry,
72  m_PacketVersionResolver)
73  , m_BufferManager()
74  , m_SendCounterPacket(m_BufferManager)
75  , m_SendThread(m_StateMachine, m_BufferManager, m_SendCounterPacket)
76  , m_SendTimelinePacket(m_BufferManager)
77  , m_PeriodicCounterCapture(m_Holder, m_SendCounterPacket, *this, m_CounterIdMap, m_BackendProfilingContexts)
78  , m_ConnectionAcknowledgedCommandHandler(0,
79  1,
80  m_PacketVersionResolver.ResolvePacketVersion(0, 1).GetEncodedValue(),
81  m_CounterDirectory,
82  m_SendCounterPacket,
83  m_SendTimelinePacket,
84  m_StateMachine,
85  m_BackendProfilingContexts)
86  , m_RequestCounterDirectoryCommandHandler(0,
87  3,
88  m_PacketVersionResolver.ResolvePacketVersion(0, 3).GetEncodedValue(),
89  m_CounterDirectory,
90  m_SendCounterPacket,
91  m_SendTimelinePacket,
92  m_StateMachine)
93  , m_PeriodicCounterSelectionCommandHandler(0,
94  4,
95  m_PacketVersionResolver.ResolvePacketVersion(0, 4).GetEncodedValue(),
96  m_BackendProfilingContexts,
97  m_CounterIdMap,
98  m_Holder,
99  MAX_ARMNN_COUNTER,
100  m_PeriodicCounterCapture,
101  *this,
102  m_SendCounterPacket,
103  m_StateMachine)
104  , m_PerJobCounterSelectionCommandHandler(0,
105  5,
106  m_PacketVersionResolver.ResolvePacketVersion(0, 5).GetEncodedValue(),
107  m_StateMachine)
108  , m_ActivateTimelineReportingCommandHandler(0,
109  6,
110  m_PacketVersionResolver.ResolvePacketVersion(0, 6)
111  .GetEncodedValue(),
112  m_SendTimelinePacket,
113  m_StateMachine,
114  reportStructure,
115  m_TimelineReporting,
116  *this)
117  , m_DeactivateTimelineReportingCommandHandler(0,
118  7,
119  m_PacketVersionResolver.ResolvePacketVersion(0, 7)
120  .GetEncodedValue(),
121  m_TimelineReporting,
122  m_StateMachine,
123  *this)
124  , m_TimelinePacketWriterFactory(m_BufferManager)
125  , m_MaxGlobalCounterId(armnn::profiling::INFERENCES_RUN)
126  {
127  // Register the "Connection Acknowledged" command handler
128  m_CommandHandlerRegistry.RegisterFunctor(&m_ConnectionAcknowledgedCommandHandler);
129 
130  // Register the "Request Counter Directory" command handler
131  m_CommandHandlerRegistry.RegisterFunctor(&m_RequestCounterDirectoryCommandHandler);
132 
133  // Register the "Periodic Counter Selection" command handler
134  m_CommandHandlerRegistry.RegisterFunctor(&m_PeriodicCounterSelectionCommandHandler);
135 
136  // Register the "Per-Job Counter Selection" command handler
137  m_CommandHandlerRegistry.RegisterFunctor(&m_PerJobCounterSelectionCommandHandler);
138 
139  m_CommandHandlerRegistry.RegisterFunctor(&m_ActivateTimelineReportingCommandHandler);
140 
141  m_CommandHandlerRegistry.RegisterFunctor(&m_DeactivateTimelineReportingCommandHandler);
142  }
143 
145 
146  // Resets the profiling options, optionally clears the profiling service entirely
147  void ResetExternalProfilingOptions(const ExternalProfilingOptions& options, bool resetProfilingService = false);
149  bool resetProfilingService = false);
150 
151 
152  // Updates the profiling service, making it transition to a new state if necessary
153  void Update();
154 
155  // Disconnects the profiling service from the external server
156  void Disconnect();
157 
158  // Store a profiling context returned from a backend that support profiling.
159  void AddBackendProfilingContext(const BackendId backendId,
160  std::shared_ptr<armnn::profiling::IBackendProfilingContext> profilingContext);
161 
162  // Enable the recording of timeline events and entities
163  void NotifyBackendsForTimelineReporting() override;
164 
165  const ICounterDirectory& GetCounterDirectory() const;
168  bool IsCounterRegistered(uint16_t counterUid) const override;
169  uint32_t GetAbsoluteCounterValue(uint16_t counterUid) const override;
170  uint32_t GetDeltaCounterValue(uint16_t counterUid) override;
171  uint16_t GetCounterCount() const override;
172  // counter global/backend mapping functions
173  const ICounterMappings& GetCounterMappings() const override;
175 
176  // Getters for the profiling service state
177  bool IsProfilingEnabled() const override;
178 
179  CaptureData GetCaptureData() override;
180  void SetCaptureData(uint32_t capturePeriod,
181  const std::vector<uint16_t>& counterIds,
182  const std::set<BackendId>& activeBackends);
183 
184  // Setters for the profiling service state
185  void SetCounterValue(uint16_t counterUid, uint32_t value) override;
186  uint32_t AddCounterValue(uint16_t counterUid, uint32_t value) override;
187  uint32_t SubtractCounterValue(uint16_t counterUid, uint32_t value) override;
188  uint32_t IncrementCounterValue(uint16_t counterUid) override;
189 
190  // IProfilingGuidGenerator functions
191  /// Return the next random Guid in the sequence
192  ProfilingDynamicGuid NextGuid() override;
193  /// Create a ProfilingStaticGuid based on a hash of the string
194  ProfilingStaticGuid GenerateStaticId(const std::string& str) override;
195 
196  std::unique_ptr<ISendTimelinePacket> GetSendTimelinePacket() const override;
197 
199  {
200  return m_SendCounterPacket;
201  }
202 
204 
205  static ProfilingStaticGuid GetStaticId(const std::string& str);
206 
208  {
209  return m_TimelineReporting;
210  }
211 
212  void AddLocalPacketHandler(ILocalPacketHandlerSharedPtr localPacketHandler);
213 private:
214  //Copy/move constructors/destructors and copy/move assignment operators are deleted
215  ProfilingService(const ProfilingService&) = delete;
217  ProfilingService& operator=(const ProfilingService&) = delete;
218  ProfilingService& operator=(ProfilingService&&) = delete;
219 
220  // Initialization/reset functions
221  void Initialize();
222  void InitializeCounterValue(uint16_t counterUid);
223  void Reset();
224  void Stop();
225 
226  // Helper function
227  void CheckCounterUid(uint16_t counterUid) const;
228 
229  // Profiling service components
230  ExternalProfilingOptions m_Options;
231  std::atomic<bool> m_TimelineReporting;
232  CounterDirectory m_CounterDirectory;
233  CounterIdMap m_CounterIdMap;
234  IProfilingConnectionFactoryPtr m_ProfilingConnectionFactory;
235  IProfilingConnectionPtr m_ProfilingConnection;
236  ProfilingStateMachine m_StateMachine;
237  CounterIndices m_CounterIndex;
238  CounterValues m_CounterValues;
239  CommandHandlerRegistry m_CommandHandlerRegistry;
240  PacketVersionResolver m_PacketVersionResolver;
241  CommandHandler m_CommandHandler;
242  BufferManager m_BufferManager;
243  SendCounterPacket m_SendCounterPacket;
244  SendThread m_SendThread;
245  SendTimelinePacket m_SendTimelinePacket;
246 
247  Holder m_Holder;
248 
249  PeriodicCounterCapture m_PeriodicCounterCapture;
250 
251  ConnectionAcknowledgedCommandHandler m_ConnectionAcknowledgedCommandHandler;
252  RequestCounterDirectoryCommandHandler m_RequestCounterDirectoryCommandHandler;
253  PeriodicCounterSelectionCommandHandler m_PeriodicCounterSelectionCommandHandler;
254  PerJobCounterSelectionCommandHandler m_PerJobCounterSelectionCommandHandler;
255  ActivateTimelineReportingCommandHandler m_ActivateTimelineReportingCommandHandler;
256  DeactivateTimelineReportingCommandHandler m_DeactivateTimelineReportingCommandHandler;
257 
258  TimelinePacketWriterFactory m_TimelinePacketWriterFactory;
259  BackendProfilingContext m_BackendProfilingContexts;
260  uint16_t m_MaxGlobalCounterId;
261 
262  static ProfilingGuidGenerator m_GuidGenerator;
263 
264 protected:
265 
266  // Protected methods for testing
270  {
271  ARMNN_ASSERT(instance.m_ProfilingConnectionFactory);
272  ARMNN_ASSERT(other);
273 
274  backup = instance.m_ProfilingConnectionFactory.release();
275  instance.m_ProfilingConnectionFactory.reset(other);
276  }
278  {
279  return instance.m_ProfilingConnection.get();
280  }
282  {
283  instance.m_StateMachine.TransitionToState(newState);
284  }
285  bool WaitForPacketSent(ProfilingService& instance, uint32_t timeout = 1000)
286  {
287  return instance.m_SendThread.WaitForPacketSent(timeout);
288  }
289 
291  {
292  return instance.m_BufferManager;
293  }
294 };
295 
296 } // namespace profiling
297 
298 } // namespace armnn
bool IsCounterRegistered(uint16_t counterUid) const override
std::shared_ptr< ILocalPacketHandler > ILocalPacketHandlerSharedPtr
uint32_t GetAbsoluteCounterValue(uint16_t counterUid) const override
std::list< std::atomic< uint32_t > > CounterValues
std::unordered_map< BackendId, std::shared_ptr< armnn::profiling::IBackendProfilingContext > > BackendProfilingContext
ProfilingState GetCurrentState() const
DataLayout::NHWC false
Strongly typed guids to distinguish between those generated at runtime, and those that are statically...
Definition: Types.hpp:296
Copyright (c) 2020 ARM Limited.
ProfilingDynamicGuid NextGuid() override
Return the next random Guid in the sequence.
uint32_t IncrementCounterValue(uint16_t counterUid) override
std::unique_ptr< IProfilingConnection > IProfilingConnectionPtr
void AddLocalPacketHandler(ILocalPacketHandlerSharedPtr localPacketHandler)
bool WaitForPacketSent(ProfilingService &instance, uint32_t timeout=1000)
bool WaitForPacketSent(uint32_t timeout)
Definition: SendThread.cpp:262
uint32_t GetDeltaCounterValue(uint16_t counterUid) override
uint32_t SubtractCounterValue(uint16_t counterUid, uint32_t value) override
std::vector< std::atomic< uint32_t > * > CounterIndices
void SwapProfilingConnectionFactory(ProfilingService &instance, IProfilingConnectionFactory *other, IProfilingConnectionFactory *&backup)
static ProfilingStaticGuid GetStaticId(const std::string &str)
void RegisterFunctor(CommandHandlerFunctor *functor, uint32_t familyId, uint32_t packetId, uint32_t version)
void SetCaptureData(uint32_t capturePeriod, const std::vector< uint16_t > &counterIds, const std::set< BackendId > &activeBackends)
std::unique_ptr< ISendTimelinePacket > GetSendTimelinePacket() const override
const ICounterMappings & GetCounterMappings() const override
void ResetExternalProfilingOptions(const ExternalProfilingOptions &options, bool resetProfilingService=false)
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
uint32_t AddCounterValue(uint16_t counterUid, uint32_t value) override
IRegisterCounterMapping & GetCounterMappingRegistry()
BufferManager & GetBufferManager(ProfilingService &instance)
ProfilingService(Optional< IReportStructure &> reportStructure=EmptyOptional())
void SetCounterValue(uint16_t counterUid, uint32_t value) override
EmptyOptional is used to initialize the Optional class in case we want to have default value for an O...
Definition: Optional.hpp:32
bool IsProfilingEnabled() const override
IProfilingConnection * GetProfilingConnection(ProfilingService &instance)
static ProfilingDynamicGuid GetNextGuid()
void TransitionToState(ProfilingState newState)
std::unique_ptr< IProfilingConnectionFactory > IProfilingConnectionFactoryPtr
void TransitionToState(ProfilingService &instance, ProfilingState newState)
ISendCounterPacket & GetSendCounterPacket() override
void AddBackendProfilingContext(const BackendId backendId, std::shared_ptr< armnn::profiling::IBackendProfilingContext > profilingContext)
armnn::Runtime::CreationOptions::ExternalProfilingOptions options
const ICounterDirectory & GetCounterDirectory() const
ProfilingState ConfigureProfilingService(const ExternalProfilingOptions &options, bool resetProfilingService=false)
uint16_t GetCounterCount() const override
ProfilingStaticGuid GenerateStaticId(const std::string &str) override
Create a ProfilingStaticGuid based on a hash of the string.