blob: 4d7241e7db9d46411172c6798de83ed5d723583b [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;
Keith Davis02356de2019-08-26 18:28:17 +010038
Matteo Martincigh54fb9572019-10-02 12:50:57 +010039 // Check if the profiling service needs to be reset
Matteo Martincigha84edee2019-10-02 12:50:57 +010040 if (resetProfilingService)
Keith Davis02356de2019-08-26 18:28:17 +010041 {
Matteo Martincigha84edee2019-10-02 12:50:57 +010042 // Reset the profiling service
Matteo Martincigh54fb9572019-10-02 12:50:57 +010043 Reset();
Keith Davis02356de2019-08-26 18:28:17 +010044 }
45}
46
Jim Flynn64063552020-02-14 10:18:08 +000047bool ProfilingService::IsProfilingEnabled() const
Keith Davise394bd92019-12-02 15:12:19 +000048{
49 return m_Options.m_EnableProfiling;
50}
51
Jim Flynn672d06e2019-10-15 10:18:11 +010052ProfilingState ProfilingService::ConfigureProfilingService(
53 const ExternalProfilingOptions& options,
54 bool resetProfilingService)
55{
56 ResetExternalProfilingOptions(options, resetProfilingService);
57 ProfilingState currentState = m_StateMachine.GetCurrentState();
58 if (options.m_EnableProfiling)
59 {
60 switch (currentState)
61 {
62 case ProfilingState::Uninitialised:
63 Update(); // should transition to NotConnected
64 Update(); // will either stay in NotConnected because there is no server
65 // or will enter WaitingForAck.
66 currentState = m_StateMachine.GetCurrentState();
67 if (currentState == ProfilingState::WaitingForAck)
68 {
69 Update(); // poke it again to send out the metadata packet
70 }
71 currentState = m_StateMachine.GetCurrentState();
72 return currentState;
73 case ProfilingState::NotConnected:
74 Update(); // will either stay in NotConnected because there is no server
75 // or will enter WaitingForAck
76 currentState = m_StateMachine.GetCurrentState();
77 if (currentState == ProfilingState::WaitingForAck)
78 {
79 Update(); // poke it again to send out the metadata packet
80 }
81 currentState = m_StateMachine.GetCurrentState();
82 return currentState;
83 default:
84 return currentState;
85 }
86 }
87 else
88 {
89 // Make sure profiling is shutdown
90 switch (currentState)
91 {
92 case ProfilingState::Uninitialised:
93 case ProfilingState::NotConnected:
94 return currentState;
95 default:
96 Stop();
97 return m_StateMachine.GetCurrentState();
98 }
99 }
100}
101
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100102void ProfilingService::Update()
Keith Davis02356de2019-08-26 18:28:17 +0100103{
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100104 if (!m_Options.m_EnableProfiling)
Matteo Martincigha84edee2019-10-02 12:50:57 +0100105 {
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100106 // Don't run if profiling is disabled
107 return;
Matteo Martincigha84edee2019-10-02 12:50:57 +0100108 }
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100109
110 ProfilingState currentState = m_StateMachine.GetCurrentState();
111 switch (currentState)
Keith Davis02356de2019-08-26 18:28:17 +0100112 {
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100113 case ProfilingState::Uninitialised:
janeil01811ca552019-12-03 17:01:32 +0000114
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100115 // Initialize the profiling service
116 Initialize();
117
118 // Move to the next state
119 m_StateMachine.TransitionToState(ProfilingState::NotConnected);
120 break;
121 case ProfilingState::NotConnected:
Matteo Martincighd0613b52019-10-09 16:47:04 +0100122 // Stop the command thread (if running)
123 m_CommandHandler.Stop();
124
125 // Stop the send thread (if running)
Sadik Armagan3896b472020-02-10 12:24:15 +0000126 m_SendThread.Stop(false);
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100127
Matteo Martincighe8485382019-10-10 14:08:21 +0100128 // Stop the periodic counter capture thread (if running)
129 m_PeriodicCounterCapture.Stop();
130
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100131 // Reset any existing profiling connection
132 m_ProfilingConnection.reset();
133
Sadik Armaganbd9e2c52019-09-26 23:13:31 +0100134 try
Keith Davis02356de2019-08-26 18:28:17 +0100135 {
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100136 // Setup the profiling connection
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100137 ARMNN_ASSERT(m_ProfilingConnectionFactory);
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100138 m_ProfilingConnection = m_ProfilingConnectionFactory->GetProfilingConnection(m_Options);
Keith Davis02356de2019-08-26 18:28:17 +0100139 }
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100140 catch (const Exception& e)
Sadik Armaganbd9e2c52019-09-26 23:13:31 +0100141 {
Derek Lamberti08446972019-11-26 16:38:31 +0000142 ARMNN_LOG(warning) << "An error has occurred when creating the profiling connection: "
143 << e.what();
Sadik Armaganbd9e2c52019-09-26 23:13:31 +0100144 }
Sadik Armagana97a0be2020-03-03 10:44:56 +0000145 catch (const armnnProfiling::SocketConnectionException& e)
146 {
147 ARMNN_LOG(warning) << "An error has occurred when creating the profiling connection ["
148 << e.what() << "] on socket [" << e.GetSocketFd() << "].";
149 }
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100150
151 // Move to the next state
152 m_StateMachine.TransitionToState(m_ProfilingConnection
153 ? ProfilingState::WaitingForAck // Profiling connection obtained, wait for ack
154 : ProfilingState::NotConnected); // Profiling connection failed, stay in the
155 // "NotConnected" state
156 break;
157 case ProfilingState::WaitingForAck:
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100158 ARMNN_ASSERT(m_ProfilingConnection);
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100159
160 // Start the command thread
161 m_CommandHandler.Start(*m_ProfilingConnection);
162
163 // Start the send thread, while in "WaitingForAck" state it'll send out a "Stream MetaData" packet waiting for
164 // a valid "Connection Acknowledged" packet confirming the connection
Sadik Armagan3896b472020-02-10 12:24:15 +0000165 m_SendThread.Start(*m_ProfilingConnection);
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100166
167 // The connection acknowledged command handler will automatically transition the state to "Active" once a
168 // valid "Connection Acknowledged" packet has been received
169
170 break;
171 case ProfilingState::Active:
172
Matteo Martincighe8485382019-10-10 14:08:21 +0100173 // The period counter capture thread is started by the Periodic Counter Selection command handler upon
174 // request by an external profiling service
175
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100176 break;
177 default:
178 throw RuntimeException(boost::str(boost::format("Unknown profiling service state: %1")
179 % static_cast<int>(currentState)));
Sadik Armaganbd9e2c52019-09-26 23:13:31 +0100180 }
Keith Davis02356de2019-08-26 18:28:17 +0100181}
182
Jim Flynn53e46992019-10-14 12:31:10 +0100183void ProfilingService::Disconnect()
184{
185 ProfilingState currentState = m_StateMachine.GetCurrentState();
186 switch (currentState)
187 {
188 case ProfilingState::Uninitialised:
189 case ProfilingState::NotConnected:
190 case ProfilingState::WaitingForAck:
191 return; // NOP
192 case ProfilingState::Active:
193 // Stop the command thread (if running)
194 Stop();
195
196 break;
197 default:
198 throw RuntimeException(boost::str(boost::format("Unknown profiling service state: %1")
199 % static_cast<int>(currentState)));
200 }
201}
202
David Monahanc1536d62020-02-12 15:52:35 +0000203// Store a profiling context returned from a backend that support profiling, and register its counters
204void ProfilingService::AddBackendProfilingContext(const BackendId backendId,
205 std::shared_ptr<armnn::profiling::IBackendProfilingContext> profilingContext)
206{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100207 ARMNN_ASSERT(profilingContext != nullptr);
David Monahanc1536d62020-02-12 15:52:35 +0000208 // Register the backend counters
209 m_MaxGlobalCounterId = profilingContext->RegisterCounters(m_MaxGlobalCounterId);
210 m_BackendProfilingContexts.emplace(backendId, std::move(profilingContext));
211}
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100212const ICounterDirectory& ProfilingService::GetCounterDirectory() const
213{
214 return m_CounterDirectory;
215}
216
Jim Flynn97897022020-02-02 12:52:59 +0000217ICounterRegistry& ProfilingService::GetCounterRegistry()
218{
219 return m_CounterDirectory;
220}
221
Matteo Martincigha84edee2019-10-02 12:50:57 +0100222ProfilingState ProfilingService::GetCurrentState() const
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100223{
Matteo Martincigha84edee2019-10-02 12:50:57 +0100224 return m_StateMachine.GetCurrentState();
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100225}
226
227uint16_t ProfilingService::GetCounterCount() const
228{
229 return m_CounterDirectory.GetCounterCount();
230}
231
Matteo Martincighe8485382019-10-10 14:08:21 +0100232bool ProfilingService::IsCounterRegistered(uint16_t counterUid) const
233{
234 return counterUid < m_CounterIndex.size();
235}
236
Matteo Martincigha84edee2019-10-02 12:50:57 +0100237uint32_t ProfilingService::GetCounterValue(uint16_t counterUid) const
Keith Davis02356de2019-08-26 18:28:17 +0100238{
Matteo Martincighe8485382019-10-10 14:08:21 +0100239 CheckCounterUid(counterUid);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100240 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100241 ARMNN_ASSERT(counterValuePtr);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100242 return counterValuePtr->load(std::memory_order::memory_order_relaxed);
Keith Davis02356de2019-08-26 18:28:17 +0100243}
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100244
Jim Flynn8e0c7a62020-01-30 14:10:55 +0000245const ICounterMappings& ProfilingService::GetCounterMappings() const
246{
247 return m_CounterIdMap;
248}
249
Jim Flynn97897022020-02-02 12:52:59 +0000250IRegisterCounterMapping& ProfilingService::GetCounterMappingRegistry()
Jim Flynn8e0c7a62020-01-30 14:10:55 +0000251{
252 return m_CounterIdMap;
253}
254
James Conroy2dcd3fe2020-02-06 18:34:52 +0000255CaptureData ProfilingService::GetCaptureData()
256{
257 return m_Holder.GetCaptureData();
258}
259
Finn Williams032bc742020-02-12 11:02:34 +0000260void ProfilingService::SetCaptureData(uint32_t capturePeriod,
261 const std::vector<uint16_t>& counterIds,
262 const std::set<BackendId>& activeBackends)
James Conroy2dcd3fe2020-02-06 18:34:52 +0000263{
Finn Williams032bc742020-02-12 11:02:34 +0000264 m_Holder.SetCaptureData(capturePeriod, counterIds, activeBackends);
James Conroy2dcd3fe2020-02-06 18:34:52 +0000265}
266
Matteo Martincigha84edee2019-10-02 12:50:57 +0100267void ProfilingService::SetCounterValue(uint16_t counterUid, uint32_t value)
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100268{
Matteo Martincighe8485382019-10-10 14:08:21 +0100269 CheckCounterUid(counterUid);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100270 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100271 ARMNN_ASSERT(counterValuePtr);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100272 counterValuePtr->store(value, std::memory_order::memory_order_relaxed);
273}
274
275uint32_t ProfilingService::AddCounterValue(uint16_t counterUid, uint32_t value)
276{
Matteo Martincighe8485382019-10-10 14:08:21 +0100277 CheckCounterUid(counterUid);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100278 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100279 ARMNN_ASSERT(counterValuePtr);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100280 return counterValuePtr->fetch_add(value, std::memory_order::memory_order_relaxed);
281}
282
283uint32_t ProfilingService::SubtractCounterValue(uint16_t counterUid, uint32_t value)
284{
Matteo Martincighe8485382019-10-10 14:08:21 +0100285 CheckCounterUid(counterUid);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100286 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100287 ARMNN_ASSERT(counterValuePtr);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100288 return counterValuePtr->fetch_sub(value, std::memory_order::memory_order_relaxed);
289}
290
291uint32_t ProfilingService::IncrementCounterValue(uint16_t counterUid)
292{
Matteo Martincighe8485382019-10-10 14:08:21 +0100293 CheckCounterUid(counterUid);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100294 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100295 ARMNN_ASSERT(counterValuePtr);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100296 return counterValuePtr->operator++(std::memory_order::memory_order_relaxed);
297}
298
Jim Flynn00f3aaf2019-10-24 11:58:06 +0100299ProfilingDynamicGuid ProfilingService::NextGuid()
300{
Sadik Armagan3184c902020-03-18 10:57:30 +0000301 return ProfilingService::GetNextGuid();
Jim Flynn00f3aaf2019-10-24 11:58:06 +0100302}
303
304ProfilingStaticGuid ProfilingService::GenerateStaticId(const std::string& str)
305{
Sadik Armagan3184c902020-03-18 10:57:30 +0000306 return ProfilingService::GetStaticId(str);
Jim Flynn00f3aaf2019-10-24 11:58:06 +0100307}
308
Jim Flynn8b200652019-10-24 18:07:44 +0100309std::unique_ptr<ISendTimelinePacket> ProfilingService::GetSendTimelinePacket() const
310{
311 return m_TimelinePacketWriterFactory.GetSendTimelinePacket();
312}
313
Matteo Martincigha84edee2019-10-02 12:50:57 +0100314void ProfilingService::Initialize()
315{
Matteo Martincigha84edee2019-10-02 12:50:57 +0100316 // Register a category for the basic runtime counters
317 if (!m_CounterDirectory.IsCategoryRegistered("ArmNN_Runtime"))
318 {
319 m_CounterDirectory.RegisterCategory("ArmNN_Runtime");
320 }
321
Keith Davise394bd92019-12-02 15:12:19 +0000322 // Register a counter for the number of Network loads
323 if (!m_CounterDirectory.IsCounterRegistered("Network loads"))
Matteo Martincigha84edee2019-10-02 12:50:57 +0100324 {
325 const Counter* loadedNetworksCounter =
Keith Davise394bd92019-12-02 15:12:19 +0000326 m_CounterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
327 armnn::profiling::NETWORK_LOADS,
328 "ArmNN_Runtime",
Matteo Martincigha84edee2019-10-02 12:50:57 +0100329 0,
330 0,
331 1.f,
Keith Davise394bd92019-12-02 15:12:19 +0000332 "Network loads",
Matteo Martincigha84edee2019-10-02 12:50:57 +0100333 "The number of networks loaded at runtime",
334 std::string("networks"));
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100335 ARMNN_ASSERT(loadedNetworksCounter);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100336 InitializeCounterValue(loadedNetworksCounter->m_Uid);
337 }
Keith Davise394bd92019-12-02 15:12:19 +0000338 // Register a counter for the number of unloaded networks
339 if (!m_CounterDirectory.IsCounterRegistered("Network unloads"))
Matteo Martincigha84edee2019-10-02 12:50:57 +0100340 {
Keith Davise394bd92019-12-02 15:12:19 +0000341 const Counter* unloadedNetworksCounter =
342 m_CounterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
343 armnn::profiling::NETWORK_UNLOADS,
344 "ArmNN_Runtime",
Matteo Martincigha84edee2019-10-02 12:50:57 +0100345 0,
346 0,
347 1.f,
Keith Davise394bd92019-12-02 15:12:19 +0000348 "Network unloads",
349 "The number of networks unloaded at runtime",
350 std::string("networks"));
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100351 ARMNN_ASSERT(unloadedNetworksCounter);
Keith Davise394bd92019-12-02 15:12:19 +0000352 InitializeCounterValue(unloadedNetworksCounter->m_Uid);
353 }
354 // Register a counter for the number of registered backends
355 if (!m_CounterDirectory.IsCounterRegistered("Backends registered"))
356 {
357 const Counter* registeredBackendsCounter =
358 m_CounterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
359 armnn::profiling::REGISTERED_BACKENDS,
360 "ArmNN_Runtime",
361 0,
362 0,
363 1.f,
364 "Backends registered",
Matteo Martincigha84edee2019-10-02 12:50:57 +0100365 "The number of registered backends",
366 std::string("backends"));
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100367 ARMNN_ASSERT(registeredBackendsCounter);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100368 InitializeCounterValue(registeredBackendsCounter->m_Uid);
369 }
Keith Davise394bd92019-12-02 15:12:19 +0000370 // Register a counter for the number of registered backends
371 if (!m_CounterDirectory.IsCounterRegistered("Backends unregistered"))
372 {
373 const Counter* unregisteredBackendsCounter =
374 m_CounterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
375 armnn::profiling::UNREGISTERED_BACKENDS,
376 "ArmNN_Runtime",
377 0,
378 0,
379 1.f,
380 "Backends unregistered",
381 "The number of unregistered backends",
382 std::string("backends"));
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100383 ARMNN_ASSERT(unregisteredBackendsCounter);
Keith Davise394bd92019-12-02 15:12:19 +0000384 InitializeCounterValue(unregisteredBackendsCounter->m_Uid);
385 }
Matteo Martincigha84edee2019-10-02 12:50:57 +0100386 // Register a counter for the number of inferences run
387 if (!m_CounterDirectory.IsCounterRegistered("Inferences run"))
388 {
389 const Counter* inferencesRunCounter =
Keith Davise394bd92019-12-02 15:12:19 +0000390 m_CounterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
391 armnn::profiling::INFERENCES_RUN,
392 "ArmNN_Runtime",
Matteo Martincigha84edee2019-10-02 12:50:57 +0100393 0,
394 0,
395 1.f,
396 "Inferences run",
397 "The number of inferences run",
398 std::string("inferences"));
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100399 ARMNN_ASSERT(inferencesRunCounter);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100400 InitializeCounterValue(inferencesRunCounter->m_Uid);
401 }
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100402}
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100403
Matteo Martincigha84edee2019-10-02 12:50:57 +0100404void ProfilingService::InitializeCounterValue(uint16_t counterUid)
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100405{
Matteo Martincigha84edee2019-10-02 12:50:57 +0100406 // Increase the size of the counter index if necessary
407 if (counterUid >= m_CounterIndex.size())
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100408 {
Matteo Martincigha84edee2019-10-02 12:50:57 +0100409 m_CounterIndex.resize(boost::numeric_cast<size_t>(counterUid) + 1);
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100410 }
Matteo Martincigha84edee2019-10-02 12:50:57 +0100411
412 // Create a new atomic counter and add it to the list
413 m_CounterValues.emplace_back(0);
414
415 // Register the new counter to the counter index for quick access
416 std::atomic<uint32_t>* counterValuePtr = &(m_CounterValues.back());
417 m_CounterIndex.at(counterUid) = counterValuePtr;
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100418}
419
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100420void ProfilingService::Reset()
421{
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100422 // Stop the profiling service...
Jim Flynn53e46992019-10-14 12:31:10 +0100423 Stop();
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100424
Jim Flynn53e46992019-10-14 12:31:10 +0100425 // ...then delete all the counter data and configuration...
426 m_CounterIndex.clear();
427 m_CounterValues.clear();
428 m_CounterDirectory.Clear();
Jim Flynn97897022020-02-02 12:52:59 +0000429 m_CounterIdMap.Reset();
Finn Williams09ad6f92019-12-19 17:05:18 +0000430 m_BufferManager.Reset();
Matteo Martincighd0613b52019-10-09 16:47:04 +0100431
Jim Flynn53e46992019-10-14 12:31:10 +0100432 // ...finally reset the profiling state machine
433 m_StateMachine.Reset();
Colm Donelan1aff3932020-02-05 17:48:59 +0000434 m_BackendProfilingContexts.clear();
Keith Davis33ed2212020-03-30 10:43:41 +0100435 m_MaxGlobalCounterId = armnn::profiling::MAX_ARMNN_COUNTER;
Jim Flynn53e46992019-10-14 12:31:10 +0100436}
437
438void ProfilingService::Stop()
439{
Matteo Martincighd0613b52019-10-09 16:47:04 +0100440 // The order in which we reset/stop the components is not trivial!
Finn Williams09ad6f92019-12-19 17:05:18 +0000441 // First stop the producing threads
442 // Command Handler first as it is responsible for launching then Periodic Counter capture thread
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100443 m_CommandHandler.Stop();
Matteo Martincighe8485382019-10-10 14:08:21 +0100444 m_PeriodicCounterCapture.Stop();
Finn Williams09ad6f92019-12-19 17:05:18 +0000445 // The the consuming thread
Sadik Armagan3896b472020-02-10 12:24:15 +0000446 m_SendThread.Stop(false);
Matteo Martincighd0613b52019-10-09 16:47:04 +0100447
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100448 // ...then close and destroy the profiling connection...
Jim Flynn53e46992019-10-14 12:31:10 +0100449 if (m_ProfilingConnection != nullptr && m_ProfilingConnection->IsOpen())
450 {
451 m_ProfilingConnection->Close();
452 }
Matteo Martincighd0613b52019-10-09 16:47:04 +0100453 m_ProfilingConnection.reset();
454
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100455 // ...then move to the "NotConnected" state
Jim Flynn53e46992019-10-14 12:31:10 +0100456 m_StateMachine.TransitionToState(ProfilingState::NotConnected);
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100457}
458
Matteo Martincighe8485382019-10-10 14:08:21 +0100459inline void ProfilingService::CheckCounterUid(uint16_t counterUid) const
460{
461 if (!IsCounterRegistered(counterUid))
462 {
463 throw InvalidArgumentException(boost::str(boost::format("Counter UID %1% is not registered") % counterUid));
464 }
465}
466
Keith Davis33ed2212020-03-30 10:43:41 +0100467void ProfilingService::NotifyBackendsForTimelineReporting()
468{
469 BackendProfilingContext::iterator it = m_BackendProfilingContexts.begin();
470 while (it != m_BackendProfilingContexts.end())
471 {
472 auto& backendProfilingContext = it->second;
473 backendProfilingContext->EnableTimelineReporting(m_TimelineReporting);
474 // Increment the Iterator to point to next entry
475 it++;
476 }
477}
478
janeil01811ca552019-12-03 17:01:32 +0000479ProfilingService::~ProfilingService()
480{
481 Stop();
482}
Keith Davis02356de2019-08-26 18:28:17 +0100483} // namespace profiling
484
Matteo Martincigha84edee2019-10-02 12:50:57 +0100485} // namespace armnn