blob: 21972b4c7fa1e3c41bfb4b52638b0af58fc3186b [file] [log] [blame]
Keith Davis02356de2019-08-26 18:28:17 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include "ProfilingService.hpp"
7
Keith Davise394bd92019-12-02 15:12:19 +00008#include <armnn/BackendId.hpp>
Derek Lamberti08446972019-11-26 16:38:31 +00009#include <armnn/Logging.hpp>
Sadik Armagana97a0be2020-03-03 10:44:56 +000010#include <common/include/SocketConnectionException.hpp>
Derek Lamberti08446972019-11-26 16:38:31 +000011
Matteo Martincigha84edee2019-10-02 12:50:57 +010012#include <boost/format.hpp>
13
Keith Davis02356de2019-08-26 18:28:17 +010014namespace armnn
15{
16
17namespace profiling
18{
19
Sadik Armagan3184c902020-03-18 10:57:30 +000020ProfilingGuidGenerator ProfilingService::m_GuidGenerator;
21
22ProfilingDynamicGuid ProfilingService::GetNextGuid()
23{
24 return m_GuidGenerator.NextGuid();
25}
26
27ProfilingStaticGuid ProfilingService::GetStaticId(const std::string& str)
28{
29 return m_GuidGenerator.GenerateStaticId(str);
30}
31
Matteo Martincigha84edee2019-10-02 12:50:57 +010032void ProfilingService::ResetExternalProfilingOptions(const ExternalProfilingOptions& options,
33 bool resetProfilingService)
Keith Davis02356de2019-08-26 18:28:17 +010034{
Matteo Martincigha84edee2019-10-02 12:50:57 +010035 // Update the profiling options
36 m_Options = options;
Keith Davis33ed2212020-03-30 10:43:41 +010037 m_TimelineReporting = options.m_TimelineEnabled;
Finn Williamsd7fcafa2020-04-23 17:55:18 +010038 m_ConnectionAcknowledgedCommandHandler.setTimelineEnabled(options.m_TimelineEnabled);
Keith Davis02356de2019-08-26 18:28:17 +010039
Matteo Martincigh54fb9572019-10-02 12:50:57 +010040 // Check if the profiling service needs to be reset
Matteo Martincigha84edee2019-10-02 12:50:57 +010041 if (resetProfilingService)
Keith Davis02356de2019-08-26 18:28:17 +010042 {
Matteo Martincigha84edee2019-10-02 12:50:57 +010043 // Reset the profiling service
Matteo Martincigh54fb9572019-10-02 12:50:57 +010044 Reset();
Keith Davis02356de2019-08-26 18:28:17 +010045 }
46}
47
Jim Flynn64063552020-02-14 10:18:08 +000048bool ProfilingService::IsProfilingEnabled() const
Keith Davise394bd92019-12-02 15:12:19 +000049{
50 return m_Options.m_EnableProfiling;
51}
52
Jim Flynn672d06e2019-10-15 10:18:11 +010053ProfilingState ProfilingService::ConfigureProfilingService(
54 const ExternalProfilingOptions& options,
55 bool resetProfilingService)
56{
57 ResetExternalProfilingOptions(options, resetProfilingService);
58 ProfilingState currentState = m_StateMachine.GetCurrentState();
59 if (options.m_EnableProfiling)
60 {
61 switch (currentState)
62 {
63 case ProfilingState::Uninitialised:
64 Update(); // should transition to NotConnected
65 Update(); // will either stay in NotConnected because there is no server
66 // or will enter WaitingForAck.
67 currentState = m_StateMachine.GetCurrentState();
68 if (currentState == ProfilingState::WaitingForAck)
69 {
70 Update(); // poke it again to send out the metadata packet
71 }
72 currentState = m_StateMachine.GetCurrentState();
73 return currentState;
74 case ProfilingState::NotConnected:
75 Update(); // will either stay in NotConnected because there is no server
76 // or will enter WaitingForAck
77 currentState = m_StateMachine.GetCurrentState();
78 if (currentState == ProfilingState::WaitingForAck)
79 {
80 Update(); // poke it again to send out the metadata packet
81 }
82 currentState = m_StateMachine.GetCurrentState();
83 return currentState;
84 default:
85 return currentState;
86 }
87 }
88 else
89 {
90 // Make sure profiling is shutdown
91 switch (currentState)
92 {
93 case ProfilingState::Uninitialised:
94 case ProfilingState::NotConnected:
95 return currentState;
96 default:
97 Stop();
98 return m_StateMachine.GetCurrentState();
99 }
100 }
101}
102
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100103void ProfilingService::Update()
Keith Davis02356de2019-08-26 18:28:17 +0100104{
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100105 if (!m_Options.m_EnableProfiling)
Matteo Martincigha84edee2019-10-02 12:50:57 +0100106 {
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100107 // Don't run if profiling is disabled
108 return;
Matteo Martincigha84edee2019-10-02 12:50:57 +0100109 }
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100110
111 ProfilingState currentState = m_StateMachine.GetCurrentState();
112 switch (currentState)
Keith Davis02356de2019-08-26 18:28:17 +0100113 {
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100114 case ProfilingState::Uninitialised:
janeil01811ca552019-12-03 17:01:32 +0000115
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100116 // Initialize the profiling service
117 Initialize();
118
119 // Move to the next state
120 m_StateMachine.TransitionToState(ProfilingState::NotConnected);
121 break;
122 case ProfilingState::NotConnected:
Matteo Martincighd0613b52019-10-09 16:47:04 +0100123 // Stop the command thread (if running)
124 m_CommandHandler.Stop();
125
126 // Stop the send thread (if running)
Sadik Armagan3896b472020-02-10 12:24:15 +0000127 m_SendThread.Stop(false);
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100128
Matteo Martincighe8485382019-10-10 14:08:21 +0100129 // Stop the periodic counter capture thread (if running)
130 m_PeriodicCounterCapture.Stop();
131
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100132 // Reset any existing profiling connection
133 m_ProfilingConnection.reset();
134
Sadik Armaganbd9e2c52019-09-26 23:13:31 +0100135 try
Keith Davis02356de2019-08-26 18:28:17 +0100136 {
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100137 // Setup the profiling connection
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100138 ARMNN_ASSERT(m_ProfilingConnectionFactory);
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100139 m_ProfilingConnection = m_ProfilingConnectionFactory->GetProfilingConnection(m_Options);
Keith Davis02356de2019-08-26 18:28:17 +0100140 }
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100141 catch (const Exception& e)
Sadik Armaganbd9e2c52019-09-26 23:13:31 +0100142 {
Derek Lamberti08446972019-11-26 16:38:31 +0000143 ARMNN_LOG(warning) << "An error has occurred when creating the profiling connection: "
144 << e.what();
Sadik Armaganbd9e2c52019-09-26 23:13:31 +0100145 }
Sadik Armagana97a0be2020-03-03 10:44:56 +0000146 catch (const armnnProfiling::SocketConnectionException& e)
147 {
148 ARMNN_LOG(warning) << "An error has occurred when creating the profiling connection ["
149 << e.what() << "] on socket [" << e.GetSocketFd() << "].";
150 }
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100151
152 // Move to the next state
153 m_StateMachine.TransitionToState(m_ProfilingConnection
154 ? ProfilingState::WaitingForAck // Profiling connection obtained, wait for ack
155 : ProfilingState::NotConnected); // Profiling connection failed, stay in the
156 // "NotConnected" state
157 break;
158 case ProfilingState::WaitingForAck:
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100159 ARMNN_ASSERT(m_ProfilingConnection);
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100160
161 // Start the command thread
162 m_CommandHandler.Start(*m_ProfilingConnection);
163
164 // Start the send thread, while in "WaitingForAck" state it'll send out a "Stream MetaData" packet waiting for
165 // a valid "Connection Acknowledged" packet confirming the connection
Sadik Armagan3896b472020-02-10 12:24:15 +0000166 m_SendThread.Start(*m_ProfilingConnection);
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100167
168 // The connection acknowledged command handler will automatically transition the state to "Active" once a
169 // valid "Connection Acknowledged" packet has been received
170
171 break;
172 case ProfilingState::Active:
173
Matteo Martincighe8485382019-10-10 14:08:21 +0100174 // The period counter capture thread is started by the Periodic Counter Selection command handler upon
175 // request by an external profiling service
176
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100177 break;
178 default:
179 throw RuntimeException(boost::str(boost::format("Unknown profiling service state: %1")
180 % static_cast<int>(currentState)));
Sadik Armaganbd9e2c52019-09-26 23:13:31 +0100181 }
Keith Davis02356de2019-08-26 18:28:17 +0100182}
183
Jim Flynn53e46992019-10-14 12:31:10 +0100184void ProfilingService::Disconnect()
185{
186 ProfilingState currentState = m_StateMachine.GetCurrentState();
187 switch (currentState)
188 {
189 case ProfilingState::Uninitialised:
190 case ProfilingState::NotConnected:
191 case ProfilingState::WaitingForAck:
192 return; // NOP
193 case ProfilingState::Active:
194 // Stop the command thread (if running)
195 Stop();
196
197 break;
198 default:
199 throw RuntimeException(boost::str(boost::format("Unknown profiling service state: %1")
200 % static_cast<int>(currentState)));
201 }
202}
203
David Monahanc1536d62020-02-12 15:52:35 +0000204// Store a profiling context returned from a backend that support profiling, and register its counters
205void ProfilingService::AddBackendProfilingContext(const BackendId backendId,
206 std::shared_ptr<armnn::profiling::IBackendProfilingContext> profilingContext)
207{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100208 ARMNN_ASSERT(profilingContext != nullptr);
David Monahanc1536d62020-02-12 15:52:35 +0000209 // Register the backend counters
210 m_MaxGlobalCounterId = profilingContext->RegisterCounters(m_MaxGlobalCounterId);
211 m_BackendProfilingContexts.emplace(backendId, std::move(profilingContext));
212}
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100213const ICounterDirectory& ProfilingService::GetCounterDirectory() const
214{
215 return m_CounterDirectory;
216}
217
Jim Flynn97897022020-02-02 12:52:59 +0000218ICounterRegistry& ProfilingService::GetCounterRegistry()
219{
220 return m_CounterDirectory;
221}
222
Matteo Martincigha84edee2019-10-02 12:50:57 +0100223ProfilingState ProfilingService::GetCurrentState() const
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100224{
Matteo Martincigha84edee2019-10-02 12:50:57 +0100225 return m_StateMachine.GetCurrentState();
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100226}
227
228uint16_t ProfilingService::GetCounterCount() const
229{
230 return m_CounterDirectory.GetCounterCount();
231}
232
Matteo Martincighe8485382019-10-10 14:08:21 +0100233bool ProfilingService::IsCounterRegistered(uint16_t counterUid) const
234{
Finn Williamsb205a332020-05-13 15:04:25 +0100235 return m_CounterDirectory.IsCounterRegistered(counterUid);
Matteo Martincighe8485382019-10-10 14:08:21 +0100236}
237
Finn Williamsf3fcf322020-05-11 14:38:02 +0100238uint32_t ProfilingService::GetAbsoluteCounterValue(uint16_t counterUid) const
Keith Davis02356de2019-08-26 18:28:17 +0100239{
Matteo Martincighe8485382019-10-10 14:08:21 +0100240 CheckCounterUid(counterUid);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100241 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100242 ARMNN_ASSERT(counterValuePtr);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100243 return counterValuePtr->load(std::memory_order::memory_order_relaxed);
Keith Davis02356de2019-08-26 18:28:17 +0100244}
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100245
Finn Williamsf3fcf322020-05-11 14:38:02 +0100246uint32_t ProfilingService::GetDeltaCounterValue(uint16_t counterUid)
247{
248 CheckCounterUid(counterUid);
249 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
250 ARMNN_ASSERT(counterValuePtr);
251 const uint32_t counterValue = counterValuePtr->load(std::memory_order::memory_order_relaxed);
252 SubtractCounterValue(counterUid, counterValue);
253 return counterValue;
254}
255
Jim Flynn8e0c7a62020-01-30 14:10:55 +0000256const ICounterMappings& ProfilingService::GetCounterMappings() const
257{
258 return m_CounterIdMap;
259}
260
Jim Flynn97897022020-02-02 12:52:59 +0000261IRegisterCounterMapping& ProfilingService::GetCounterMappingRegistry()
Jim Flynn8e0c7a62020-01-30 14:10:55 +0000262{
263 return m_CounterIdMap;
264}
265
James Conroy2dcd3fe2020-02-06 18:34:52 +0000266CaptureData ProfilingService::GetCaptureData()
267{
268 return m_Holder.GetCaptureData();
269}
270
Finn Williams032bc742020-02-12 11:02:34 +0000271void ProfilingService::SetCaptureData(uint32_t capturePeriod,
272 const std::vector<uint16_t>& counterIds,
273 const std::set<BackendId>& activeBackends)
James Conroy2dcd3fe2020-02-06 18:34:52 +0000274{
Finn Williams032bc742020-02-12 11:02:34 +0000275 m_Holder.SetCaptureData(capturePeriod, counterIds, activeBackends);
James Conroy2dcd3fe2020-02-06 18:34:52 +0000276}
277
Matteo Martincigha84edee2019-10-02 12:50:57 +0100278void ProfilingService::SetCounterValue(uint16_t counterUid, uint32_t value)
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100279{
Matteo Martincighe8485382019-10-10 14:08:21 +0100280 CheckCounterUid(counterUid);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100281 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100282 ARMNN_ASSERT(counterValuePtr);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100283 counterValuePtr->store(value, std::memory_order::memory_order_relaxed);
284}
285
286uint32_t ProfilingService::AddCounterValue(uint16_t counterUid, uint32_t value)
287{
Matteo Martincighe8485382019-10-10 14:08:21 +0100288 CheckCounterUid(counterUid);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100289 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100290 ARMNN_ASSERT(counterValuePtr);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100291 return counterValuePtr->fetch_add(value, std::memory_order::memory_order_relaxed);
292}
293
294uint32_t ProfilingService::SubtractCounterValue(uint16_t counterUid, uint32_t value)
295{
Matteo Martincighe8485382019-10-10 14:08:21 +0100296 CheckCounterUid(counterUid);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100297 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100298 ARMNN_ASSERT(counterValuePtr);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100299 return counterValuePtr->fetch_sub(value, std::memory_order::memory_order_relaxed);
300}
301
302uint32_t ProfilingService::IncrementCounterValue(uint16_t counterUid)
303{
Matteo Martincighe8485382019-10-10 14:08:21 +0100304 CheckCounterUid(counterUid);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100305 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100306 ARMNN_ASSERT(counterValuePtr);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100307 return counterValuePtr->operator++(std::memory_order::memory_order_relaxed);
308}
309
Jim Flynn00f3aaf2019-10-24 11:58:06 +0100310ProfilingDynamicGuid ProfilingService::NextGuid()
311{
Sadik Armagan3184c902020-03-18 10:57:30 +0000312 return ProfilingService::GetNextGuid();
Jim Flynn00f3aaf2019-10-24 11:58:06 +0100313}
314
315ProfilingStaticGuid ProfilingService::GenerateStaticId(const std::string& str)
316{
Sadik Armagan3184c902020-03-18 10:57:30 +0000317 return ProfilingService::GetStaticId(str);
Jim Flynn00f3aaf2019-10-24 11:58:06 +0100318}
319
Jim Flynn8b200652019-10-24 18:07:44 +0100320std::unique_ptr<ISendTimelinePacket> ProfilingService::GetSendTimelinePacket() const
321{
322 return m_TimelinePacketWriterFactory.GetSendTimelinePacket();
323}
324
Matteo Martincigha84edee2019-10-02 12:50:57 +0100325void ProfilingService::Initialize()
326{
Matteo Martincigha84edee2019-10-02 12:50:57 +0100327 // Register a category for the basic runtime counters
328 if (!m_CounterDirectory.IsCategoryRegistered("ArmNN_Runtime"))
329 {
330 m_CounterDirectory.RegisterCategory("ArmNN_Runtime");
331 }
332
Keith Davise394bd92019-12-02 15:12:19 +0000333 // Register a counter for the number of Network loads
334 if (!m_CounterDirectory.IsCounterRegistered("Network loads"))
Matteo Martincigha84edee2019-10-02 12:50:57 +0100335 {
336 const Counter* loadedNetworksCounter =
Keith Davise394bd92019-12-02 15:12:19 +0000337 m_CounterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
338 armnn::profiling::NETWORK_LOADS,
339 "ArmNN_Runtime",
Finn Williamsf3fcf322020-05-11 14:38:02 +0100340 0,
Matteo Martincigha84edee2019-10-02 12:50:57 +0100341 0,
342 1.f,
Keith Davise394bd92019-12-02 15:12:19 +0000343 "Network loads",
Matteo Martincigha84edee2019-10-02 12:50:57 +0100344 "The number of networks loaded at runtime",
345 std::string("networks"));
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100346 ARMNN_ASSERT(loadedNetworksCounter);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100347 InitializeCounterValue(loadedNetworksCounter->m_Uid);
348 }
Keith Davise394bd92019-12-02 15:12:19 +0000349 // Register a counter for the number of unloaded networks
350 if (!m_CounterDirectory.IsCounterRegistered("Network unloads"))
Matteo Martincigha84edee2019-10-02 12:50:57 +0100351 {
Keith Davise394bd92019-12-02 15:12:19 +0000352 const Counter* unloadedNetworksCounter =
353 m_CounterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
354 armnn::profiling::NETWORK_UNLOADS,
355 "ArmNN_Runtime",
Finn Williamsf3fcf322020-05-11 14:38:02 +0100356 0,
Matteo Martincigha84edee2019-10-02 12:50:57 +0100357 0,
358 1.f,
Keith Davise394bd92019-12-02 15:12:19 +0000359 "Network unloads",
360 "The number of networks unloaded at runtime",
361 std::string("networks"));
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100362 ARMNN_ASSERT(unloadedNetworksCounter);
Keith Davise394bd92019-12-02 15:12:19 +0000363 InitializeCounterValue(unloadedNetworksCounter->m_Uid);
364 }
365 // Register a counter for the number of registered backends
366 if (!m_CounterDirectory.IsCounterRegistered("Backends registered"))
367 {
368 const Counter* registeredBackendsCounter =
369 m_CounterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
370 armnn::profiling::REGISTERED_BACKENDS,
371 "ArmNN_Runtime",
Finn Williamsf3fcf322020-05-11 14:38:02 +0100372 0,
Keith Davise394bd92019-12-02 15:12:19 +0000373 0,
374 1.f,
375 "Backends registered",
Matteo Martincigha84edee2019-10-02 12:50:57 +0100376 "The number of registered backends",
377 std::string("backends"));
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100378 ARMNN_ASSERT(registeredBackendsCounter);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100379 InitializeCounterValue(registeredBackendsCounter->m_Uid);
Finn Williams45a73622020-05-15 18:41:05 +0100380
381 // Due to backends being registered before the profiling service becomes active,
382 // we need to set the counter to the correct value here
383 SetCounterValue(armnn::profiling::REGISTERED_BACKENDS, static_cast<uint32_t>(BackendRegistryInstance().Size()));
Matteo Martincigha84edee2019-10-02 12:50:57 +0100384 }
Keith Davise394bd92019-12-02 15:12:19 +0000385 // Register a counter for the number of registered backends
386 if (!m_CounterDirectory.IsCounterRegistered("Backends unregistered"))
387 {
388 const Counter* unregisteredBackendsCounter =
389 m_CounterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
390 armnn::profiling::UNREGISTERED_BACKENDS,
391 "ArmNN_Runtime",
Finn Williamsf3fcf322020-05-11 14:38:02 +0100392 0,
Keith Davise394bd92019-12-02 15:12:19 +0000393 0,
394 1.f,
395 "Backends unregistered",
396 "The number of unregistered backends",
397 std::string("backends"));
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100398 ARMNN_ASSERT(unregisteredBackendsCounter);
Keith Davise394bd92019-12-02 15:12:19 +0000399 InitializeCounterValue(unregisteredBackendsCounter->m_Uid);
400 }
Matteo Martincigha84edee2019-10-02 12:50:57 +0100401 // Register a counter for the number of inferences run
402 if (!m_CounterDirectory.IsCounterRegistered("Inferences run"))
403 {
404 const Counter* inferencesRunCounter =
Keith Davise394bd92019-12-02 15:12:19 +0000405 m_CounterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
406 armnn::profiling::INFERENCES_RUN,
407 "ArmNN_Runtime",
Finn Williamsf3fcf322020-05-11 14:38:02 +0100408 0,
Matteo Martincigha84edee2019-10-02 12:50:57 +0100409 0,
410 1.f,
411 "Inferences run",
412 "The number of inferences run",
413 std::string("inferences"));
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100414 ARMNN_ASSERT(inferencesRunCounter);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100415 InitializeCounterValue(inferencesRunCounter->m_Uid);
416 }
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100417}
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100418
Matteo Martincigha84edee2019-10-02 12:50:57 +0100419void ProfilingService::InitializeCounterValue(uint16_t counterUid)
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100420{
Matteo Martincigha84edee2019-10-02 12:50:57 +0100421 // Increase the size of the counter index if necessary
422 if (counterUid >= m_CounterIndex.size())
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100423 {
Matteo Martincigha84edee2019-10-02 12:50:57 +0100424 m_CounterIndex.resize(boost::numeric_cast<size_t>(counterUid) + 1);
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100425 }
Matteo Martincigha84edee2019-10-02 12:50:57 +0100426
427 // Create a new atomic counter and add it to the list
428 m_CounterValues.emplace_back(0);
429
430 // Register the new counter to the counter index for quick access
431 std::atomic<uint32_t>* counterValuePtr = &(m_CounterValues.back());
432 m_CounterIndex.at(counterUid) = counterValuePtr;
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100433}
434
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100435void ProfilingService::Reset()
436{
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100437 // Stop the profiling service...
Jim Flynn53e46992019-10-14 12:31:10 +0100438 Stop();
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100439
Jim Flynn53e46992019-10-14 12:31:10 +0100440 // ...then delete all the counter data and configuration...
441 m_CounterIndex.clear();
442 m_CounterValues.clear();
443 m_CounterDirectory.Clear();
Jim Flynn97897022020-02-02 12:52:59 +0000444 m_CounterIdMap.Reset();
Finn Williams09ad6f92019-12-19 17:05:18 +0000445 m_BufferManager.Reset();
Matteo Martincighd0613b52019-10-09 16:47:04 +0100446
Jim Flynn53e46992019-10-14 12:31:10 +0100447 // ...finally reset the profiling state machine
448 m_StateMachine.Reset();
Colm Donelan1aff3932020-02-05 17:48:59 +0000449 m_BackendProfilingContexts.clear();
Keith Davis33ed2212020-03-30 10:43:41 +0100450 m_MaxGlobalCounterId = armnn::profiling::MAX_ARMNN_COUNTER;
Jim Flynn53e46992019-10-14 12:31:10 +0100451}
452
453void ProfilingService::Stop()
454{
Matteo Martincighd0613b52019-10-09 16:47:04 +0100455 // The order in which we reset/stop the components is not trivial!
Finn Williams09ad6f92019-12-19 17:05:18 +0000456 // First stop the producing threads
457 // Command Handler first as it is responsible for launching then Periodic Counter capture thread
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100458 m_CommandHandler.Stop();
Matteo Martincighe8485382019-10-10 14:08:21 +0100459 m_PeriodicCounterCapture.Stop();
Finn Williams09ad6f92019-12-19 17:05:18 +0000460 // The the consuming thread
Sadik Armagan3896b472020-02-10 12:24:15 +0000461 m_SendThread.Stop(false);
Matteo Martincighd0613b52019-10-09 16:47:04 +0100462
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100463 // ...then close and destroy the profiling connection...
Jim Flynn53e46992019-10-14 12:31:10 +0100464 if (m_ProfilingConnection != nullptr && m_ProfilingConnection->IsOpen())
465 {
466 m_ProfilingConnection->Close();
467 }
Matteo Martincighd0613b52019-10-09 16:47:04 +0100468 m_ProfilingConnection.reset();
469
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100470 // ...then move to the "NotConnected" state
Jim Flynn53e46992019-10-14 12:31:10 +0100471 m_StateMachine.TransitionToState(ProfilingState::NotConnected);
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100472}
473
Matteo Martincighe8485382019-10-10 14:08:21 +0100474inline void ProfilingService::CheckCounterUid(uint16_t counterUid) const
475{
476 if (!IsCounterRegistered(counterUid))
477 {
478 throw InvalidArgumentException(boost::str(boost::format("Counter UID %1% is not registered") % counterUid));
479 }
480}
481
Keith Davis33ed2212020-03-30 10:43:41 +0100482void ProfilingService::NotifyBackendsForTimelineReporting()
483{
484 BackendProfilingContext::iterator it = m_BackendProfilingContexts.begin();
485 while (it != m_BackendProfilingContexts.end())
486 {
487 auto& backendProfilingContext = it->second;
488 backendProfilingContext->EnableTimelineReporting(m_TimelineReporting);
489 // Increment the Iterator to point to next entry
490 it++;
491 }
492}
493
janeil01811ca552019-12-03 17:01:32 +0000494ProfilingService::~ProfilingService()
495{
496 Stop();
497}
Keith Davis02356de2019-08-26 18:28:17 +0100498} // namespace profiling
499
Matteo Martincigha84edee2019-10-02 12:50:57 +0100500} // namespace armnn