blob: b5f398db97f3961014994cf7107db8105d86d6a4 [file] [log] [blame]
Keith Davis02356de2019-08-26 18:28:17 +01001//
Jim Flynn6398a982020-05-27 17:05:21 +01002// Copyright © 2019 Arm Ltd and Contributors. All rights reserved.
Keith Davis02356de2019-08-26 18:28:17 +01003// SPDX-License-Identifier: MIT
4//
5
6#include "ProfilingService.hpp"
7
Jim Flynn6c9f17d2022-03-10 23:13:01 +00008#include <common/include/Logging.hpp>
Jim Flynn75c14f42022-03-10 22:05:42 +00009#include <common/include/NumericCast.hpp>
Nikhil Raj7dcc6972021-04-30 15:44:24 +010010#include <common/include/ProfilingGuid.hpp>
Sadik Armagana97a0be2020-03-03 10:44:56 +000011#include <common/include/SocketConnectionException.hpp>
Derek Lamberti08446972019-11-26 16:38:31 +000012
Jan Eilers156113c2020-09-09 19:11:16 +010013#include <fmt/format.h>
Matteo Martincigha84edee2019-10-02 12:50:57 +010014
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000015namespace arm
Keith Davis02356de2019-08-26 18:28:17 +010016{
17
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000018namespace pipe
Keith Davis02356de2019-08-26 18:28:17 +010019{
20
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000021void ProfilingService::ResetExternalProfilingOptions(const arm::pipe::ProfilingOptions& options,
Matteo Martincigha84edee2019-10-02 12:50:57 +010022 bool resetProfilingService)
Keith Davis02356de2019-08-26 18:28:17 +010023{
Matteo Martincigha84edee2019-10-02 12:50:57 +010024 // Update the profiling options
25 m_Options = options;
Keith Davis33ed2212020-03-30 10:43:41 +010026 m_TimelineReporting = options.m_TimelineEnabled;
Finn Williamsd7fcafa2020-04-23 17:55:18 +010027 m_ConnectionAcknowledgedCommandHandler.setTimelineEnabled(options.m_TimelineEnabled);
Keith Davis02356de2019-08-26 18:28:17 +010028
Matteo Martincigh54fb9572019-10-02 12:50:57 +010029 // Check if the profiling service needs to be reset
Matteo Martincigha84edee2019-10-02 12:50:57 +010030 if (resetProfilingService)
Keith Davis02356de2019-08-26 18:28:17 +010031 {
Matteo Martincigha84edee2019-10-02 12:50:57 +010032 // Reset the profiling service
Matteo Martincigh54fb9572019-10-02 12:50:57 +010033 Reset();
Keith Davis02356de2019-08-26 18:28:17 +010034 }
35}
36
Jim Flynn64063552020-02-14 10:18:08 +000037bool ProfilingService::IsProfilingEnabled() const
Keith Davise394bd92019-12-02 15:12:19 +000038{
39 return m_Options.m_EnableProfiling;
40}
41
Jim Flynn672d06e2019-10-15 10:18:11 +010042ProfilingState ProfilingService::ConfigureProfilingService(
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000043 const ProfilingOptions& options,
Jim Flynn672d06e2019-10-15 10:18:11 +010044 bool resetProfilingService)
45{
46 ResetExternalProfilingOptions(options, resetProfilingService);
47 ProfilingState currentState = m_StateMachine.GetCurrentState();
48 if (options.m_EnableProfiling)
49 {
50 switch (currentState)
51 {
52 case ProfilingState::Uninitialised:
53 Update(); // should transition to NotConnected
54 Update(); // will either stay in NotConnected because there is no server
55 // or will enter WaitingForAck.
56 currentState = m_StateMachine.GetCurrentState();
57 if (currentState == ProfilingState::WaitingForAck)
58 {
59 Update(); // poke it again to send out the metadata packet
60 }
61 currentState = m_StateMachine.GetCurrentState();
62 return currentState;
63 case ProfilingState::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 default:
74 return currentState;
75 }
76 }
77 else
78 {
79 // Make sure profiling is shutdown
80 switch (currentState)
81 {
82 case ProfilingState::Uninitialised:
83 case ProfilingState::NotConnected:
84 return currentState;
85 default:
86 Stop();
87 return m_StateMachine.GetCurrentState();
88 }
89 }
90}
91
Matteo Martincigh54fb9572019-10-02 12:50:57 +010092void ProfilingService::Update()
Keith Davis02356de2019-08-26 18:28:17 +010093{
Matteo Martincigh54fb9572019-10-02 12:50:57 +010094 if (!m_Options.m_EnableProfiling)
Matteo Martincigha84edee2019-10-02 12:50:57 +010095 {
Matteo Martincigh54fb9572019-10-02 12:50:57 +010096 // Don't run if profiling is disabled
97 return;
Matteo Martincigha84edee2019-10-02 12:50:57 +010098 }
Matteo Martincigh54fb9572019-10-02 12:50:57 +010099
100 ProfilingState currentState = m_StateMachine.GetCurrentState();
101 switch (currentState)
Keith Davis02356de2019-08-26 18:28:17 +0100102 {
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100103 case ProfilingState::Uninitialised:
janeil01811ca552019-12-03 17:01:32 +0000104
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100105 // Initialize the profiling service
106 Initialize();
107
108 // Move to the next state
109 m_StateMachine.TransitionToState(ProfilingState::NotConnected);
110 break;
111 case ProfilingState::NotConnected:
Matteo Martincighd0613b52019-10-09 16:47:04 +0100112 // Stop the command thread (if running)
113 m_CommandHandler.Stop();
114
115 // Stop the send thread (if running)
Sadik Armagan3896b472020-02-10 12:24:15 +0000116 m_SendThread.Stop(false);
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100117
Matteo Martincighe8485382019-10-10 14:08:21 +0100118 // Stop the periodic counter capture thread (if running)
119 m_PeriodicCounterCapture.Stop();
120
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100121 // Reset any existing profiling connection
122 m_ProfilingConnection.reset();
123
Sadik Armaganbd9e2c52019-09-26 23:13:31 +0100124 try
Keith Davis02356de2019-08-26 18:28:17 +0100125 {
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100126 // Setup the profiling connection
Jim Flynn6730fe92022-03-10 22:57:47 +0000127 ARM_PIPE_ASSERT(m_ProfilingConnectionFactory);
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100128 m_ProfilingConnection = m_ProfilingConnectionFactory->GetProfilingConnection(m_Options);
Keith Davis02356de2019-08-26 18:28:17 +0100129 }
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000130 catch (const arm::pipe::ProfilingException& e)
Sadik Armaganbd9e2c52019-09-26 23:13:31 +0100131 {
Jim Flynn6c9f17d2022-03-10 23:13:01 +0000132 ARM_PIPE_LOG(warning) << "An error has occurred when creating the profiling connection: "
Derek Lamberti08446972019-11-26 16:38:31 +0000133 << e.what();
Sadik Armaganbd9e2c52019-09-26 23:13:31 +0100134 }
Jim Flynnbbfe6032020-07-20 16:57:44 +0100135 catch (const arm::pipe::SocketConnectionException& e)
Sadik Armagana97a0be2020-03-03 10:44:56 +0000136 {
Jim Flynn6c9f17d2022-03-10 23:13:01 +0000137 ARM_PIPE_LOG(warning) << "An error has occurred when creating the profiling connection ["
Sadik Armagana97a0be2020-03-03 10:44:56 +0000138 << e.what() << "] on socket [" << e.GetSocketFd() << "].";
139 }
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100140
141 // Move to the next state
142 m_StateMachine.TransitionToState(m_ProfilingConnection
143 ? ProfilingState::WaitingForAck // Profiling connection obtained, wait for ack
144 : ProfilingState::NotConnected); // Profiling connection failed, stay in the
145 // "NotConnected" state
146 break;
147 case ProfilingState::WaitingForAck:
Jim Flynn6730fe92022-03-10 22:57:47 +0000148 ARM_PIPE_ASSERT(m_ProfilingConnection);
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100149
150 // Start the command thread
151 m_CommandHandler.Start(*m_ProfilingConnection);
152
153 // Start the send thread, while in "WaitingForAck" state it'll send out a "Stream MetaData" packet waiting for
154 // a valid "Connection Acknowledged" packet confirming the connection
Sadik Armagan3896b472020-02-10 12:24:15 +0000155 m_SendThread.Start(*m_ProfilingConnection);
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100156
157 // The connection acknowledged command handler will automatically transition the state to "Active" once a
158 // valid "Connection Acknowledged" packet has been received
159
160 break;
161 case ProfilingState::Active:
162
Matteo Martincighe8485382019-10-10 14:08:21 +0100163 // The period counter capture thread is started by the Periodic Counter Selection command handler upon
164 // request by an external profiling service
165
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100166 break;
167 default:
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000168 throw arm::pipe::ProfilingException(fmt::format("Unknown profiling service state: {}",
169 static_cast<int>(currentState)));
Sadik Armaganbd9e2c52019-09-26 23:13:31 +0100170 }
Keith Davis02356de2019-08-26 18:28:17 +0100171}
172
Jim Flynn53e46992019-10-14 12:31:10 +0100173void ProfilingService::Disconnect()
174{
175 ProfilingState currentState = m_StateMachine.GetCurrentState();
176 switch (currentState)
177 {
178 case ProfilingState::Uninitialised:
179 case ProfilingState::NotConnected:
180 case ProfilingState::WaitingForAck:
181 return; // NOP
182 case ProfilingState::Active:
183 // Stop the command thread (if running)
184 Stop();
185
186 break;
187 default:
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000188 throw arm::pipe::ProfilingException(fmt::format("Unknown profiling service state: {}",
189 static_cast<int>(currentState)));
Jim Flynn53e46992019-10-14 12:31:10 +0100190 }
191}
192
David Monahanc1536d62020-02-12 15:52:35 +0000193// Store a profiling context returned from a backend that support profiling, and register its counters
Cathal Corbett6f073722022-03-04 12:11:09 +0000194void ProfilingService::AddBackendProfilingContext(const std::string& backendId,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000195 std::shared_ptr<IBackendProfilingContext> profilingContext)
David Monahanc1536d62020-02-12 15:52:35 +0000196{
Jim Flynn6730fe92022-03-10 22:57:47 +0000197 ARM_PIPE_ASSERT(profilingContext != nullptr);
David Monahanc1536d62020-02-12 15:52:35 +0000198 // Register the backend counters
199 m_MaxGlobalCounterId = profilingContext->RegisterCounters(m_MaxGlobalCounterId);
200 m_BackendProfilingContexts.emplace(backendId, std::move(profilingContext));
201}
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100202const ICounterDirectory& ProfilingService::GetCounterDirectory() const
203{
204 return m_CounterDirectory;
205}
206
Jim Flynn97897022020-02-02 12:52:59 +0000207ICounterRegistry& ProfilingService::GetCounterRegistry()
208{
209 return m_CounterDirectory;
210}
211
Matteo Martincigha84edee2019-10-02 12:50:57 +0100212ProfilingState ProfilingService::GetCurrentState() const
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100213{
Matteo Martincigha84edee2019-10-02 12:50:57 +0100214 return m_StateMachine.GetCurrentState();
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100215}
216
217uint16_t ProfilingService::GetCounterCount() const
218{
219 return m_CounterDirectory.GetCounterCount();
220}
221
Matteo Martincighe8485382019-10-10 14:08:21 +0100222bool ProfilingService::IsCounterRegistered(uint16_t counterUid) const
223{
Finn Williamsb205a332020-05-13 15:04:25 +0100224 return m_CounterDirectory.IsCounterRegistered(counterUid);
Matteo Martincighe8485382019-10-10 14:08:21 +0100225}
226
Finn Williamsf3fcf322020-05-11 14:38:02 +0100227uint32_t ProfilingService::GetAbsoluteCounterValue(uint16_t counterUid) const
Keith Davis02356de2019-08-26 18:28:17 +0100228{
Matteo Martincighe8485382019-10-10 14:08:21 +0100229 CheckCounterUid(counterUid);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100230 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
Jim Flynn6730fe92022-03-10 22:57:47 +0000231 ARM_PIPE_ASSERT(counterValuePtr);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100232 return counterValuePtr->load(std::memory_order::memory_order_relaxed);
Keith Davis02356de2019-08-26 18:28:17 +0100233}
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100234
Finn Williamsf3fcf322020-05-11 14:38:02 +0100235uint32_t ProfilingService::GetDeltaCounterValue(uint16_t counterUid)
236{
237 CheckCounterUid(counterUid);
238 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
Jim Flynn6730fe92022-03-10 22:57:47 +0000239 ARM_PIPE_ASSERT(counterValuePtr);
Finn Williamsf3fcf322020-05-11 14:38:02 +0100240 const uint32_t counterValue = counterValuePtr->load(std::memory_order::memory_order_relaxed);
241 SubtractCounterValue(counterUid, counterValue);
242 return counterValue;
243}
244
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
Jim Flynn34430252022-03-04 15:03:58 +0000255bool ProfilingService::IsCategoryRegistered(const std::string& categoryName) const
256{
257 return m_CounterDirectory.IsCategoryRegistered(categoryName);
258}
259
260bool ProfilingService::IsCounterRegistered(const std::string& counterName) const
261{
262 return m_CounterDirectory.IsCounterRegistered(counterName);
263}
264
James Conroy2dcd3fe2020-02-06 18:34:52 +0000265CaptureData ProfilingService::GetCaptureData()
266{
267 return m_Holder.GetCaptureData();
268}
269
Finn Williams032bc742020-02-12 11:02:34 +0000270void ProfilingService::SetCaptureData(uint32_t capturePeriod,
271 const std::vector<uint16_t>& counterIds,
Cathal Corbett6f073722022-03-04 12:11:09 +0000272 const std::set<std::string>& activeBackends)
James Conroy2dcd3fe2020-02-06 18:34:52 +0000273{
Finn Williams032bc742020-02-12 11:02:34 +0000274 m_Holder.SetCaptureData(capturePeriod, counterIds, activeBackends);
James Conroy2dcd3fe2020-02-06 18:34:52 +0000275}
276
Matteo Martincigha84edee2019-10-02 12:50:57 +0100277void ProfilingService::SetCounterValue(uint16_t counterUid, uint32_t value)
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100278{
Matteo Martincighe8485382019-10-10 14:08:21 +0100279 CheckCounterUid(counterUid);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100280 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
Jim Flynn6730fe92022-03-10 22:57:47 +0000281 ARM_PIPE_ASSERT(counterValuePtr);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100282 counterValuePtr->store(value, std::memory_order::memory_order_relaxed);
283}
284
285uint32_t ProfilingService::AddCounterValue(uint16_t counterUid, uint32_t value)
286{
Matteo Martincighe8485382019-10-10 14:08:21 +0100287 CheckCounterUid(counterUid);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100288 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
Jim Flynn6730fe92022-03-10 22:57:47 +0000289 ARM_PIPE_ASSERT(counterValuePtr);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100290 return counterValuePtr->fetch_add(value, std::memory_order::memory_order_relaxed);
291}
292
293uint32_t ProfilingService::SubtractCounterValue(uint16_t counterUid, uint32_t value)
294{
Matteo Martincighe8485382019-10-10 14:08:21 +0100295 CheckCounterUid(counterUid);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100296 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
Jim Flynn6730fe92022-03-10 22:57:47 +0000297 ARM_PIPE_ASSERT(counterValuePtr);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100298 return counterValuePtr->fetch_sub(value, std::memory_order::memory_order_relaxed);
299}
300
301uint32_t ProfilingService::IncrementCounterValue(uint16_t counterUid)
302{
Matteo Martincighe8485382019-10-10 14:08:21 +0100303 CheckCounterUid(counterUid);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100304 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
Jim Flynn6730fe92022-03-10 22:57:47 +0000305 ARM_PIPE_ASSERT(counterValuePtr);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100306 return counterValuePtr->operator++(std::memory_order::memory_order_relaxed);
307}
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{
Jim Flynn34430252022-03-04 15:03:58 +0000316 m_Initialiser.InitialiseProfilingService(*this);
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100317}
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100318
Matteo Martincigha84edee2019-10-02 12:50:57 +0100319void ProfilingService::InitializeCounterValue(uint16_t counterUid)
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100320{
Matteo Martincigha84edee2019-10-02 12:50:57 +0100321 // Increase the size of the counter index if necessary
322 if (counterUid >= m_CounterIndex.size())
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100323 {
Jim Flynn75c14f42022-03-10 22:05:42 +0000324 m_CounterIndex.resize(arm::pipe::numeric_cast<size_t>(counterUid) + 1);
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100325 }
Matteo Martincigha84edee2019-10-02 12:50:57 +0100326
327 // Create a new atomic counter and add it to the list
328 m_CounterValues.emplace_back(0);
329
330 // Register the new counter to the counter index for quick access
331 std::atomic<uint32_t>* counterValuePtr = &(m_CounterValues.back());
332 m_CounterIndex.at(counterUid) = counterValuePtr;
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100333}
334
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100335void ProfilingService::Reset()
336{
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100337 // Stop the profiling service...
Jim Flynn53e46992019-10-14 12:31:10 +0100338 Stop();
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100339
Jim Flynn53e46992019-10-14 12:31:10 +0100340 // ...then delete all the counter data and configuration...
341 m_CounterIndex.clear();
342 m_CounterValues.clear();
343 m_CounterDirectory.Clear();
Jim Flynn97897022020-02-02 12:52:59 +0000344 m_CounterIdMap.Reset();
Finn Williams09ad6f92019-12-19 17:05:18 +0000345 m_BufferManager.Reset();
Matteo Martincighd0613b52019-10-09 16:47:04 +0100346
Jim Flynn53e46992019-10-14 12:31:10 +0100347 // ...finally reset the profiling state machine
348 m_StateMachine.Reset();
Colm Donelan1aff3932020-02-05 17:48:59 +0000349 m_BackendProfilingContexts.clear();
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000350 m_MaxGlobalCounterId = MAX_ARMNN_COUNTER;
Jim Flynn53e46992019-10-14 12:31:10 +0100351}
352
353void ProfilingService::Stop()
354{
Jim Flynn6398a982020-05-27 17:05:21 +0100355 { // only lock when we are updating the inference completed variable
356 std::unique_lock<std::mutex> lck(m_ServiceActiveMutex);
357 m_ServiceActive = false;
358 }
Matteo Martincighd0613b52019-10-09 16:47:04 +0100359 // The order in which we reset/stop the components is not trivial!
Finn Williams09ad6f92019-12-19 17:05:18 +0000360 // First stop the producing threads
361 // Command Handler first as it is responsible for launching then Periodic Counter capture thread
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100362 m_CommandHandler.Stop();
Matteo Martincighe8485382019-10-10 14:08:21 +0100363 m_PeriodicCounterCapture.Stop();
Finn Williams09ad6f92019-12-19 17:05:18 +0000364 // The the consuming thread
Sadik Armagan3896b472020-02-10 12:24:15 +0000365 m_SendThread.Stop(false);
Matteo Martincighd0613b52019-10-09 16:47:04 +0100366
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100367 // ...then close and destroy the profiling connection...
Jim Flynn53e46992019-10-14 12:31:10 +0100368 if (m_ProfilingConnection != nullptr && m_ProfilingConnection->IsOpen())
369 {
370 m_ProfilingConnection->Close();
371 }
Matteo Martincighd0613b52019-10-09 16:47:04 +0100372 m_ProfilingConnection.reset();
373
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100374 // ...then move to the "NotConnected" state
Jim Flynn53e46992019-10-14 12:31:10 +0100375 m_StateMachine.TransitionToState(ProfilingState::NotConnected);
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100376}
377
Matteo Martincighe8485382019-10-10 14:08:21 +0100378inline void ProfilingService::CheckCounterUid(uint16_t counterUid) const
379{
380 if (!IsCounterRegistered(counterUid))
381 {
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000382 throw arm::pipe::InvalidArgumentException(fmt::format("Counter UID {} is not registered", counterUid));
Matteo Martincighe8485382019-10-10 14:08:21 +0100383 }
384}
385
Keith Davis33ed2212020-03-30 10:43:41 +0100386void ProfilingService::NotifyBackendsForTimelineReporting()
387{
388 BackendProfilingContext::iterator it = m_BackendProfilingContexts.begin();
389 while (it != m_BackendProfilingContexts.end())
390 {
391 auto& backendProfilingContext = it->second;
392 backendProfilingContext->EnableTimelineReporting(m_TimelineReporting);
393 // Increment the Iterator to point to next entry
394 it++;
395 }
396}
397
Jim Flynn6398a982020-05-27 17:05:21 +0100398void ProfilingService::NotifyProfilingServiceActive()
399{
400 { // only lock when we are updating the inference completed variable
401 std::unique_lock<std::mutex> lck(m_ServiceActiveMutex);
402 m_ServiceActive = true;
403 }
404 m_ServiceActiveConditionVariable.notify_one();
405}
406
407void ProfilingService::WaitForProfilingServiceActivation(unsigned int timeout)
408{
409 std::unique_lock<std::mutex> lck(m_ServiceActiveMutex);
410
411 auto start = std::chrono::high_resolution_clock::now();
412 // Here we we will go back to sleep after a spurious wake up if
413 // m_InferenceCompleted is not yet true.
414 if (!m_ServiceActiveConditionVariable.wait_for(lck,
415 std::chrono::milliseconds(timeout),
416 [&]{return m_ServiceActive == true;}))
417 {
418 if (m_ServiceActive == true)
419 {
420 return;
421 }
422 auto finish = std::chrono::high_resolution_clock::now();
423 std::chrono::duration<double, std::milli> elapsed = finish - start;
424 std::stringstream ss;
425 ss << "Timed out waiting on profiling service activation for " << elapsed.count() << " ms";
Jim Flynn6c9f17d2022-03-10 23:13:01 +0000426 ARM_PIPE_LOG(warning) << ss.str();
Jim Flynn6398a982020-05-27 17:05:21 +0100427 }
428 return;
429}
430
janeil01811ca552019-12-03 17:01:32 +0000431ProfilingService::~ProfilingService()
432{
433 Stop();
434}
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000435} // namespace pipe
Keith Davis02356de2019-08-26 18:28:17 +0100436
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000437} // namespace arm