ArmNN
 22.02
ProfilingService.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2019 Arm Ltd and Contributors. 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"
26 #include "SendCounterPacket.hpp"
27 #include "SendThread.hpp"
28 #include "SendTimelinePacket.hpp"
30 #include "INotifyBackends.hpp"
32 
33 #include <common/include/ProfilingGuidGenerator.hpp>
34 
35 #include <list>
36 
37 namespace armnn
38 {
39 
40 namespace profiling
41 {
42 // Static constants describing ArmNN's counter UID's
43 static const uint16_t NETWORK_LOADS = 0;
44 static const uint16_t NETWORK_UNLOADS = 1;
45 static const uint16_t REGISTERED_BACKENDS = 2;
46 static const uint16_t UNREGISTERED_BACKENDS = 3;
47 static const uint16_t INFERENCES_RUN = 4;
48 static const uint16_t MAX_ARMNN_COUNTER = INFERENCES_RUN;
49 
51 {
52 public:
54  using IProfilingConnectionFactoryPtr = std::unique_ptr<IProfilingConnectionFactory>;
55  using IProfilingConnectionPtr = std::unique_ptr<IProfilingConnection>;
56  using CounterIndices = std::vector<std::atomic<uint32_t>*>;
57  using CounterValues = std::list<std::atomic<uint32_t>>;
58  using BackendProfilingContext = std::unordered_map<BackendId,
59  std::shared_ptr<armnn::profiling::IBackendProfilingContext>>;
60 
62  : m_Options()
63  , m_TimelineReporting(false)
64  , m_CounterDirectory()
65  , m_ProfilingConnectionFactory(new ProfilingConnectionFactory())
66  , m_ProfilingConnection()
67  , m_StateMachine()
68  , m_CounterIndex()
69  , m_CounterValues()
70  , m_CommandHandlerRegistry()
71  , m_PacketVersionResolver()
72  , m_CommandHandler(1000,
73  false,
74  m_CommandHandlerRegistry,
75  m_PacketVersionResolver)
76  , m_BufferManager()
77  , m_SendCounterPacket(m_BufferManager)
78  , m_SendThread(m_StateMachine, m_BufferManager, m_SendCounterPacket)
79  , m_SendTimelinePacket(m_BufferManager)
80  , m_PeriodicCounterCapture(m_Holder, m_SendCounterPacket, *this, m_CounterIdMap, m_BackendProfilingContexts)
81  , m_ConnectionAcknowledgedCommandHandler(0,
82  1,
83  m_PacketVersionResolver.ResolvePacketVersion(0, 1).GetEncodedValue(),
84  m_CounterDirectory,
85  m_SendCounterPacket,
86  m_SendTimelinePacket,
87  m_StateMachine,
88  *this,
89  m_BackendProfilingContexts)
90  , m_RequestCounterDirectoryCommandHandler(0,
91  3,
92  m_PacketVersionResolver.ResolvePacketVersion(0, 3).GetEncodedValue(),
93  m_CounterDirectory,
94  m_SendCounterPacket,
95  m_SendTimelinePacket,
96  m_StateMachine)
97  , m_PeriodicCounterSelectionCommandHandler(0,
98  4,
99  m_PacketVersionResolver.ResolvePacketVersion(0, 4).GetEncodedValue(),
100  m_BackendProfilingContexts,
101  m_CounterIdMap,
102  m_Holder,
103  MAX_ARMNN_COUNTER,
104  m_PeriodicCounterCapture,
105  *this,
106  m_SendCounterPacket,
107  m_StateMachine)
108  , m_PerJobCounterSelectionCommandHandler(0,
109  5,
110  m_PacketVersionResolver.ResolvePacketVersion(0, 5).GetEncodedValue(),
111  m_StateMachine)
112  , m_ActivateTimelineReportingCommandHandler(0,
113  6,
114  m_PacketVersionResolver.ResolvePacketVersion(0, 6)
115  .GetEncodedValue(),
116  m_SendTimelinePacket,
117  m_StateMachine,
118  reportStructure,
119  m_TimelineReporting,
120  *this)
121  , m_DeactivateTimelineReportingCommandHandler(0,
122  7,
123  m_PacketVersionResolver.ResolvePacketVersion(0, 7)
124  .GetEncodedValue(),
125  m_TimelineReporting,
126  m_StateMachine,
127  *this)
128  , m_TimelinePacketWriterFactory(m_BufferManager)
129  , m_MaxGlobalCounterId(armnn::profiling::INFERENCES_RUN)
130  , m_ServiceActive(false)
131  {
132  // Register the "Connection Acknowledged" command handler
133  m_CommandHandlerRegistry.RegisterFunctor(&m_ConnectionAcknowledgedCommandHandler);
134 
135  // Register the "Request Counter Directory" command handler
136  m_CommandHandlerRegistry.RegisterFunctor(&m_RequestCounterDirectoryCommandHandler);
137 
138  // Register the "Periodic Counter Selection" command handler
139  m_CommandHandlerRegistry.RegisterFunctor(&m_PeriodicCounterSelectionCommandHandler);
140 
141  // Register the "Per-Job Counter Selection" command handler
142  m_CommandHandlerRegistry.RegisterFunctor(&m_PerJobCounterSelectionCommandHandler);
143 
144  m_CommandHandlerRegistry.RegisterFunctor(&m_ActivateTimelineReportingCommandHandler);
145 
146  m_CommandHandlerRegistry.RegisterFunctor(&m_DeactivateTimelineReportingCommandHandler);
147  }
148 
150 
151  // Resets the profiling options, optionally clears the profiling service entirely
152  void ResetExternalProfilingOptions(const ExternalProfilingOptions& options, bool resetProfilingService = false);
154  bool resetProfilingService = false);
155 
156 
157  // Updates the profiling service, making it transition to a new state if necessary
158  void Update();
159 
160  // Disconnects the profiling service from the external server
161  void Disconnect();
162 
163  // Store a profiling context returned from a backend that support profiling.
164  void AddBackendProfilingContext(const BackendId backendId,
165  std::shared_ptr<armnn::profiling::IBackendProfilingContext> profilingContext);
166 
167  // Enable the recording of timeline events and entities
168  void NotifyBackendsForTimelineReporting() override;
169 
170  const ICounterDirectory& GetCounterDirectory() const;
173  bool IsCounterRegistered(uint16_t counterUid) const override;
174  uint32_t GetAbsoluteCounterValue(uint16_t counterUid) const override;
175  uint32_t GetDeltaCounterValue(uint16_t counterUid) override;
176  uint16_t GetCounterCount() const override;
177  // counter global/backend mapping functions
178  const ICounterMappings& GetCounterMappings() const override;
180 
181  // Getters for the profiling service state
182  bool IsProfilingEnabled() const override;
183 
184  CaptureData GetCaptureData() override;
185  void SetCaptureData(uint32_t capturePeriod,
186  const std::vector<uint16_t>& counterIds,
187  const std::set<BackendId>& activeBackends);
188 
189  // Setters for the profiling service state
190  void SetCounterValue(uint16_t counterUid, uint32_t value) override;
191  uint32_t AddCounterValue(uint16_t counterUid, uint32_t value) override;
192  uint32_t SubtractCounterValue(uint16_t counterUid, uint32_t value) override;
193  uint32_t IncrementCounterValue(uint16_t counterUid) override;
194 
195  // IProfilingGuidGenerator functions
196  /// Return the next random Guid in the sequence
197  ProfilingDynamicGuid NextGuid() override;
198  /// Create a ProfilingStaticGuid based on a hash of the string
199  ProfilingStaticGuid GenerateStaticId(const std::string& str) override;
200 
201 
202  std::unique_ptr<ISendTimelinePacket> GetSendTimelinePacket() const override;
203 
205  {
206  return m_SendCounterPacket;
207  }
208 
209  static ProfilingDynamicGuid GetNextGuid();
210 
211  static ProfilingStaticGuid GetStaticId(const std::string& str);
212 
213  void ResetGuidGenerator();
214 
216  {
217  return m_TimelineReporting;
218  }
219 
220  void AddLocalPacketHandler(ILocalPacketHandlerSharedPtr localPacketHandler);
221 
222  void NotifyProfilingServiceActive() override; // IProfilingServiceStatus
223  void WaitForProfilingServiceActivation(unsigned int timeout) override; // IProfilingServiceStatus
224 
225 private:
226  //Copy/move constructors/destructors and copy/move assignment operators are deleted
227  ProfilingService(const ProfilingService&) = delete;
229  ProfilingService& operator=(const ProfilingService&) = delete;
230  ProfilingService& operator=(ProfilingService&&) = delete;
231 
232  // Initialization/reset functions
233  void Initialize();
234  void InitializeCounterValue(uint16_t counterUid);
235  void Reset();
236  void Stop();
237 
238  // Helper function
239  void CheckCounterUid(uint16_t counterUid) const;
240 
241  // Profiling service components
242  ExternalProfilingOptions m_Options;
243  std::atomic<bool> m_TimelineReporting;
244  CounterDirectory m_CounterDirectory;
245  CounterIdMap m_CounterIdMap;
246  IProfilingConnectionFactoryPtr m_ProfilingConnectionFactory;
247  IProfilingConnectionPtr m_ProfilingConnection;
248  ProfilingStateMachine m_StateMachine;
249  CounterIndices m_CounterIndex;
250  CounterValues m_CounterValues;
251  arm::pipe::CommandHandlerRegistry m_CommandHandlerRegistry;
252  arm::pipe::PacketVersionResolver m_PacketVersionResolver;
253  CommandHandler m_CommandHandler;
254  BufferManager m_BufferManager;
255  SendCounterPacket m_SendCounterPacket;
256  SendThread m_SendThread;
257  SendTimelinePacket m_SendTimelinePacket;
258 
259  Holder m_Holder;
260 
261  PeriodicCounterCapture m_PeriodicCounterCapture;
262 
263  ConnectionAcknowledgedCommandHandler m_ConnectionAcknowledgedCommandHandler;
264  RequestCounterDirectoryCommandHandler m_RequestCounterDirectoryCommandHandler;
265  PeriodicCounterSelectionCommandHandler m_PeriodicCounterSelectionCommandHandler;
266  PerJobCounterSelectionCommandHandler m_PerJobCounterSelectionCommandHandler;
267  ActivateTimelineReportingCommandHandler m_ActivateTimelineReportingCommandHandler;
268  DeactivateTimelineReportingCommandHandler m_DeactivateTimelineReportingCommandHandler;
269 
270  TimelinePacketWriterFactory m_TimelinePacketWriterFactory;
271  BackendProfilingContext m_BackendProfilingContexts;
272  uint16_t m_MaxGlobalCounterId;
273 
274  static ProfilingGuidGenerator m_GuidGenerator;
275 
276  // Signalling to let external actors know when service is active or not
277  std::mutex m_ServiceActiveMutex;
278  std::condition_variable m_ServiceActiveConditionVariable;
279  bool m_ServiceActive;
280 
281 protected:
282 
283  // Protected methods for testing
287  {
288  ARMNN_ASSERT(instance.m_ProfilingConnectionFactory);
289  ARMNN_ASSERT(other);
290 
291  backup = instance.m_ProfilingConnectionFactory.release();
292  instance.m_ProfilingConnectionFactory.reset(other);
293  }
295  {
296  return instance.m_ProfilingConnection.get();
297  }
299  {
300  instance.m_StateMachine.TransitionToState(newState);
301  }
302  bool WaitForPacketSent(ProfilingService& instance, uint32_t timeout = 1000)
303  {
304  return instance.m_SendThread.WaitForPacketSent(timeout);
305  }
306 
308  {
309  return instance.m_BufferManager;
310  }
311 };
312 
313 } // namespace profiling
314 
315 } // namespace armnn
bool IsCounterRegistered(uint16_t counterUid) const override
void WaitForProfilingServiceActivation(unsigned int timeout) 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
Copyright (c) 2021 ARM Limited and Contributors.
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:260
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 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)
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.