blob: 294a294de9fecae7c1ac9fdefb34c72a7dbdca73 [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{
235 return counterUid < m_CounterIndex.size();
236}
237
Matteo Martincigha84edee2019-10-02 12:50:57 +0100238uint32_t ProfilingService::GetCounterValue(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
Jim Flynn8e0c7a62020-01-30 14:10:55 +0000246const ICounterMappings& ProfilingService::GetCounterMappings() const
247{
248 return m_CounterIdMap;
249}
250
Jim Flynn97897022020-02-02 12:52:59 +0000251IRegisterCounterMapping& ProfilingService::GetCounterMappingRegistry()
Jim Flynn8e0c7a62020-01-30 14:10:55 +0000252{
253 return m_CounterIdMap;
254}
255
James Conroy2dcd3fe2020-02-06 18:34:52 +0000256CaptureData ProfilingService::GetCaptureData()
257{
258 return m_Holder.GetCaptureData();
259}
260
Finn Williams032bc742020-02-12 11:02:34 +0000261void ProfilingService::SetCaptureData(uint32_t capturePeriod,
262 const std::vector<uint16_t>& counterIds,
263 const std::set<BackendId>& activeBackends)
James Conroy2dcd3fe2020-02-06 18:34:52 +0000264{
Finn Williams032bc742020-02-12 11:02:34 +0000265 m_Holder.SetCaptureData(capturePeriod, counterIds, activeBackends);
James Conroy2dcd3fe2020-02-06 18:34:52 +0000266}
267
Matteo Martincigha84edee2019-10-02 12:50:57 +0100268void ProfilingService::SetCounterValue(uint16_t counterUid, uint32_t value)
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100269{
Matteo Martincighe8485382019-10-10 14:08:21 +0100270 CheckCounterUid(counterUid);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100271 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100272 ARMNN_ASSERT(counterValuePtr);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100273 counterValuePtr->store(value, std::memory_order::memory_order_relaxed);
274}
275
276uint32_t ProfilingService::AddCounterValue(uint16_t counterUid, uint32_t value)
277{
Matteo Martincighe8485382019-10-10 14:08:21 +0100278 CheckCounterUid(counterUid);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100279 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100280 ARMNN_ASSERT(counterValuePtr);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100281 return counterValuePtr->fetch_add(value, std::memory_order::memory_order_relaxed);
282}
283
284uint32_t ProfilingService::SubtractCounterValue(uint16_t counterUid, uint32_t value)
285{
Matteo Martincighe8485382019-10-10 14:08:21 +0100286 CheckCounterUid(counterUid);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100287 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100288 ARMNN_ASSERT(counterValuePtr);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100289 return counterValuePtr->fetch_sub(value, std::memory_order::memory_order_relaxed);
290}
291
292uint32_t ProfilingService::IncrementCounterValue(uint16_t counterUid)
293{
Matteo Martincighe8485382019-10-10 14:08:21 +0100294 CheckCounterUid(counterUid);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100295 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100296 ARMNN_ASSERT(counterValuePtr);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100297 return counterValuePtr->operator++(std::memory_order::memory_order_relaxed);
298}
299
Jim Flynn00f3aaf2019-10-24 11:58:06 +0100300ProfilingDynamicGuid ProfilingService::NextGuid()
301{
Sadik Armagan3184c902020-03-18 10:57:30 +0000302 return ProfilingService::GetNextGuid();
Jim Flynn00f3aaf2019-10-24 11:58:06 +0100303}
304
305ProfilingStaticGuid ProfilingService::GenerateStaticId(const std::string& str)
306{
Sadik Armagan3184c902020-03-18 10:57:30 +0000307 return ProfilingService::GetStaticId(str);
Jim Flynn00f3aaf2019-10-24 11:58:06 +0100308}
309
Jim Flynn8b200652019-10-24 18:07:44 +0100310std::unique_ptr<ISendTimelinePacket> ProfilingService::GetSendTimelinePacket() const
311{
312 return m_TimelinePacketWriterFactory.GetSendTimelinePacket();
313}
314
Matteo Martincigha84edee2019-10-02 12:50:57 +0100315void ProfilingService::Initialize()
316{
Matteo Martincigha84edee2019-10-02 12:50:57 +0100317 // Register a category for the basic runtime counters
318 if (!m_CounterDirectory.IsCategoryRegistered("ArmNN_Runtime"))
319 {
320 m_CounterDirectory.RegisterCategory("ArmNN_Runtime");
321 }
322
Keith Davise394bd92019-12-02 15:12:19 +0000323 // Register a counter for the number of Network loads
324 if (!m_CounterDirectory.IsCounterRegistered("Network loads"))
Matteo Martincigha84edee2019-10-02 12:50:57 +0100325 {
326 const Counter* loadedNetworksCounter =
Keith Davise394bd92019-12-02 15:12:19 +0000327 m_CounterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
328 armnn::profiling::NETWORK_LOADS,
329 "ArmNN_Runtime",
Finn Williams78229c42020-05-08 12:24:31 +0100330 1,
Matteo Martincigha84edee2019-10-02 12:50:57 +0100331 0,
332 1.f,
Keith Davise394bd92019-12-02 15:12:19 +0000333 "Network loads",
Matteo Martincigha84edee2019-10-02 12:50:57 +0100334 "The number of networks loaded at runtime",
335 std::string("networks"));
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100336 ARMNN_ASSERT(loadedNetworksCounter);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100337 InitializeCounterValue(loadedNetworksCounter->m_Uid);
338 }
Keith Davise394bd92019-12-02 15:12:19 +0000339 // Register a counter for the number of unloaded networks
340 if (!m_CounterDirectory.IsCounterRegistered("Network unloads"))
Matteo Martincigha84edee2019-10-02 12:50:57 +0100341 {
Keith Davise394bd92019-12-02 15:12:19 +0000342 const Counter* unloadedNetworksCounter =
343 m_CounterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
344 armnn::profiling::NETWORK_UNLOADS,
345 "ArmNN_Runtime",
Finn Williams78229c42020-05-08 12:24:31 +0100346 1,
Matteo Martincigha84edee2019-10-02 12:50:57 +0100347 0,
348 1.f,
Keith Davise394bd92019-12-02 15:12:19 +0000349 "Network unloads",
350 "The number of networks unloaded at runtime",
351 std::string("networks"));
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100352 ARMNN_ASSERT(unloadedNetworksCounter);
Keith Davise394bd92019-12-02 15:12:19 +0000353 InitializeCounterValue(unloadedNetworksCounter->m_Uid);
354 }
355 // Register a counter for the number of registered backends
356 if (!m_CounterDirectory.IsCounterRegistered("Backends registered"))
357 {
358 const Counter* registeredBackendsCounter =
359 m_CounterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
360 armnn::profiling::REGISTERED_BACKENDS,
361 "ArmNN_Runtime",
Finn Williams78229c42020-05-08 12:24:31 +0100362 1,
Keith Davise394bd92019-12-02 15:12:19 +0000363 0,
364 1.f,
365 "Backends registered",
Matteo Martincigha84edee2019-10-02 12:50:57 +0100366 "The number of registered backends",
367 std::string("backends"));
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100368 ARMNN_ASSERT(registeredBackendsCounter);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100369 InitializeCounterValue(registeredBackendsCounter->m_Uid);
370 }
Keith Davise394bd92019-12-02 15:12:19 +0000371 // Register a counter for the number of registered backends
372 if (!m_CounterDirectory.IsCounterRegistered("Backends unregistered"))
373 {
374 const Counter* unregisteredBackendsCounter =
375 m_CounterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
376 armnn::profiling::UNREGISTERED_BACKENDS,
377 "ArmNN_Runtime",
Finn Williams78229c42020-05-08 12:24:31 +0100378 1,
Keith Davise394bd92019-12-02 15:12:19 +0000379 0,
380 1.f,
381 "Backends unregistered",
382 "The number of unregistered backends",
383 std::string("backends"));
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100384 ARMNN_ASSERT(unregisteredBackendsCounter);
Keith Davise394bd92019-12-02 15:12:19 +0000385 InitializeCounterValue(unregisteredBackendsCounter->m_Uid);
386 }
Matteo Martincigha84edee2019-10-02 12:50:57 +0100387 // Register a counter for the number of inferences run
388 if (!m_CounterDirectory.IsCounterRegistered("Inferences run"))
389 {
390 const Counter* inferencesRunCounter =
Keith Davise394bd92019-12-02 15:12:19 +0000391 m_CounterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
392 armnn::profiling::INFERENCES_RUN,
393 "ArmNN_Runtime",
Finn Williams78229c42020-05-08 12:24:31 +0100394 1,
Matteo Martincigha84edee2019-10-02 12:50:57 +0100395 0,
396 1.f,
397 "Inferences run",
398 "The number of inferences run",
399 std::string("inferences"));
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100400 ARMNN_ASSERT(inferencesRunCounter);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100401 InitializeCounterValue(inferencesRunCounter->m_Uid);
402 }
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100403}
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100404
Matteo Martincigha84edee2019-10-02 12:50:57 +0100405void ProfilingService::InitializeCounterValue(uint16_t counterUid)
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100406{
Matteo Martincigha84edee2019-10-02 12:50:57 +0100407 // Increase the size of the counter index if necessary
408 if (counterUid >= m_CounterIndex.size())
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100409 {
Matteo Martincigha84edee2019-10-02 12:50:57 +0100410 m_CounterIndex.resize(boost::numeric_cast<size_t>(counterUid) + 1);
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100411 }
Matteo Martincigha84edee2019-10-02 12:50:57 +0100412
413 // Create a new atomic counter and add it to the list
414 m_CounterValues.emplace_back(0);
415
416 // Register the new counter to the counter index for quick access
417 std::atomic<uint32_t>* counterValuePtr = &(m_CounterValues.back());
418 m_CounterIndex.at(counterUid) = counterValuePtr;
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100419}
420
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100421void ProfilingService::Reset()
422{
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100423 // Stop the profiling service...
Jim Flynn53e46992019-10-14 12:31:10 +0100424 Stop();
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100425
Jim Flynn53e46992019-10-14 12:31:10 +0100426 // ...then delete all the counter data and configuration...
427 m_CounterIndex.clear();
428 m_CounterValues.clear();
429 m_CounterDirectory.Clear();
Jim Flynn97897022020-02-02 12:52:59 +0000430 m_CounterIdMap.Reset();
Finn Williams09ad6f92019-12-19 17:05:18 +0000431 m_BufferManager.Reset();
Matteo Martincighd0613b52019-10-09 16:47:04 +0100432
Jim Flynn53e46992019-10-14 12:31:10 +0100433 // ...finally reset the profiling state machine
434 m_StateMachine.Reset();
Colm Donelan1aff3932020-02-05 17:48:59 +0000435 m_BackendProfilingContexts.clear();
Keith Davis33ed2212020-03-30 10:43:41 +0100436 m_MaxGlobalCounterId = armnn::profiling::MAX_ARMNN_COUNTER;
Jim Flynn53e46992019-10-14 12:31:10 +0100437}
438
439void ProfilingService::Stop()
440{
Matteo Martincighd0613b52019-10-09 16:47:04 +0100441 // The order in which we reset/stop the components is not trivial!
Finn Williams09ad6f92019-12-19 17:05:18 +0000442 // First stop the producing threads
443 // Command Handler first as it is responsible for launching then Periodic Counter capture thread
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100444 m_CommandHandler.Stop();
Matteo Martincighe8485382019-10-10 14:08:21 +0100445 m_PeriodicCounterCapture.Stop();
Finn Williams09ad6f92019-12-19 17:05:18 +0000446 // The the consuming thread
Sadik Armagan3896b472020-02-10 12:24:15 +0000447 m_SendThread.Stop(false);
Matteo Martincighd0613b52019-10-09 16:47:04 +0100448
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100449 // ...then close and destroy the profiling connection...
Jim Flynn53e46992019-10-14 12:31:10 +0100450 if (m_ProfilingConnection != nullptr && m_ProfilingConnection->IsOpen())
451 {
452 m_ProfilingConnection->Close();
453 }
Matteo Martincighd0613b52019-10-09 16:47:04 +0100454 m_ProfilingConnection.reset();
455
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100456 // ...then move to the "NotConnected" state
Jim Flynn53e46992019-10-14 12:31:10 +0100457 m_StateMachine.TransitionToState(ProfilingState::NotConnected);
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100458}
459
Matteo Martincighe8485382019-10-10 14:08:21 +0100460inline void ProfilingService::CheckCounterUid(uint16_t counterUid) const
461{
462 if (!IsCounterRegistered(counterUid))
463 {
464 throw InvalidArgumentException(boost::str(boost::format("Counter UID %1% is not registered") % counterUid));
465 }
466}
467
Keith Davis33ed2212020-03-30 10:43:41 +0100468void ProfilingService::NotifyBackendsForTimelineReporting()
469{
470 BackendProfilingContext::iterator it = m_BackendProfilingContexts.begin();
471 while (it != m_BackendProfilingContexts.end())
472 {
473 auto& backendProfilingContext = it->second;
474 backendProfilingContext->EnableTimelineReporting(m_TimelineReporting);
475 // Increment the Iterator to point to next entry
476 it++;
477 }
478}
479
janeil01811ca552019-12-03 17:01:32 +0000480ProfilingService::~ProfilingService()
481{
482 Stop();
483}
Keith Davis02356de2019-08-26 18:28:17 +0100484} // namespace profiling
485
Matteo Martincigha84edee2019-10-02 12:50:57 +0100486} // namespace armnn