12 #include <common/include/ProfilingGuid.hpp> 14 #include <common/include/SocketConnectionException.hpp> 16 #include <fmt/format.h> 24 ProfilingGuidGenerator ProfilingService::m_GuidGenerator;
28 return m_GuidGenerator.NextGuid();
33 return m_GuidGenerator.GenerateStaticId(str);
38 m_GuidGenerator.Reset();
42 bool resetProfilingService)
50 if (resetProfilingService)
64 bool resetProfilingService)
100 switch (currentState)
121 switch (currentState)
133 m_CommandHandler.
Stop();
136 m_SendThread.
Stop(
false);
139 m_PeriodicCounterCapture.
Stop();
142 m_ProfilingConnection.reset();
148 m_ProfilingConnection = m_ProfilingConnectionFactory->GetProfilingConnection(m_Options);
152 ARMNN_LOG(
warning) <<
"An error has occurred when creating the profiling connection: " 155 catch (
const arm::pipe::SocketConnectionException& e)
157 ARMNN_LOG(
warning) <<
"An error has occurred when creating the profiling connection [" 158 << e.what() <<
"] on socket [" << e.GetSocketFd() <<
"].";
171 m_CommandHandler.
Start(*m_ProfilingConnection);
175 m_SendThread.
Start(*m_ProfilingConnection);
189 static_cast<int>(currentState)));
196 switch (currentState)
209 static_cast<int>(currentState)));
215 std::shared_ptr<armnn::profiling::IBackendProfilingContext> profilingContext)
219 m_MaxGlobalCounterId = profilingContext->RegisterCounters(m_MaxGlobalCounterId);
220 m_BackendProfilingContexts.emplace(backendId, std::move(profilingContext));
224 return m_CounterDirectory;
229 return m_CounterDirectory;
249 CheckCounterUid(counterUid);
250 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
252 return counterValuePtr->load(std::memory_order::memory_order_relaxed);
257 CheckCounterUid(counterUid);
258 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
260 const uint32_t counterValue = counterValuePtr->load(std::memory_order::memory_order_relaxed);
267 return m_CounterIdMap;
272 return m_CounterIdMap;
281 const std::vector<uint16_t>& counterIds,
282 const std::set<BackendId>& activeBackends)
284 m_Holder.
SetCaptureData(capturePeriod, counterIds, activeBackends);
289 CheckCounterUid(counterUid);
290 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
292 counterValuePtr->store(value, std::memory_order::memory_order_relaxed);
297 CheckCounterUid(counterUid);
298 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
300 return counterValuePtr->fetch_add(value, std::memory_order::memory_order_relaxed);
305 CheckCounterUid(counterUid);
306 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
308 return counterValuePtr->fetch_sub(value, std::memory_order::memory_order_relaxed);
313 CheckCounterUid(counterUid);
314 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
316 return counterValuePtr->operator++(std::memory_order::memory_order_relaxed);
334 void ProfilingService::Initialize()
345 const Counter* loadedNetworksCounter =
347 armnn::profiling::NETWORK_LOADS,
353 "The number of networks loaded at runtime",
354 std::string(
"networks"));
356 InitializeCounterValue(loadedNetworksCounter->
m_Uid);
361 const Counter* unloadedNetworksCounter =
363 armnn::profiling::NETWORK_UNLOADS,
369 "The number of networks unloaded at runtime",
370 std::string(
"networks"));
372 InitializeCounterValue(unloadedNetworksCounter->
m_Uid);
377 const Counter* registeredBackendsCounter =
379 armnn::profiling::REGISTERED_BACKENDS,
384 "Backends registered",
385 "The number of registered backends",
386 std::string(
"backends"));
388 InitializeCounterValue(registeredBackendsCounter->
m_Uid);
397 const Counter* unregisteredBackendsCounter =
399 armnn::profiling::UNREGISTERED_BACKENDS,
404 "Backends unregistered",
405 "The number of unregistered backends",
406 std::string(
"backends"));
408 InitializeCounterValue(unregisteredBackendsCounter->
m_Uid);
413 const Counter* inferencesRunCounter =
415 armnn::profiling::INFERENCES_RUN,
421 "The number of inferences run",
422 std::string(
"inferences"));
424 InitializeCounterValue(inferencesRunCounter->
m_Uid);
428 void ProfilingService::InitializeCounterValue(uint16_t counterUid)
431 if (counterUid >= m_CounterIndex.size())
433 m_CounterIndex.resize(armnn::numeric_cast<size_t>(counterUid) + 1);
437 m_CounterValues.emplace_back(0);
440 std::atomic<uint32_t>* counterValuePtr = &(m_CounterValues.back());
441 m_CounterIndex.at(counterUid) = counterValuePtr;
444 void ProfilingService::Reset()
450 m_CounterIndex.clear();
451 m_CounterValues.clear();
452 m_CounterDirectory.
Clear();
453 m_CounterIdMap.
Reset();
454 m_BufferManager.
Reset();
457 m_StateMachine.
Reset();
458 m_BackendProfilingContexts.clear();
459 m_MaxGlobalCounterId = armnn::profiling::MAX_ARMNN_COUNTER;
462 void ProfilingService::Stop()
465 std::unique_lock<std::mutex> lck(m_ServiceActiveMutex);
466 m_ServiceActive =
false;
471 m_CommandHandler.
Stop();
472 m_PeriodicCounterCapture.
Stop();
474 m_SendThread.
Stop(
false);
477 if (m_ProfilingConnection !=
nullptr && m_ProfilingConnection->IsOpen())
479 m_ProfilingConnection->Close();
481 m_ProfilingConnection.reset();
487 inline void ProfilingService::CheckCounterUid(uint16_t counterUid)
const 497 BackendProfilingContext::iterator it = m_BackendProfilingContexts.begin();
498 while (it != m_BackendProfilingContexts.end())
500 auto& backendProfilingContext = it->second;
501 backendProfilingContext->EnableTimelineReporting(m_TimelineReporting);
510 std::unique_lock<std::mutex> lck(m_ServiceActiveMutex);
511 m_ServiceActive =
true;
513 m_ServiceActiveConditionVariable.notify_one();
518 std::unique_lock<std::mutex> lck(m_ServiceActiveMutex);
520 auto start = std::chrono::high_resolution_clock::now();
523 if (!m_ServiceActiveConditionVariable.wait_for(lck,
524 std::chrono::milliseconds(timeout),
525 [&]{return m_ServiceActive == true;}))
527 if (m_ServiceActive ==
true)
531 auto finish = std::chrono::high_resolution_clock::now();
532 std::chrono::duration<double, std::milli> elapsed = finish - start;
533 std::stringstream ss;
534 ss <<
"Timed out waiting on profiling service activation for " << elapsed.count() <<
" ms";
bool IsCounterRegistered(uint16_t counterUid) const override
bool IsCategoryRegistered(const std::string &categoryName) const
void WaitForProfilingServiceActivation(unsigned int timeout) override
ProfilingState GetCurrentState() const
const Category * RegisterCategory(const std::string &categoryName) override
const Counter * RegisterCounter(const BackendId &backendId, const uint16_t uid, const std::string &parentCategoryName, uint16_t counterClass, uint16_t interpolation, double multiplier, const std::string &name, const std::string &description, const Optional< std::string > &units=EmptyOptional(), const Optional< uint16_t > &numberOfCores=EmptyOptional(), const Optional< uint16_t > &deviceUid=EmptyOptional(), const Optional< uint16_t > &counterSetUid=EmptyOptional()) override
uint32_t GetAbsoluteCounterValue(uint16_t counterUid) const override
ProfilingState GetCurrentState() const
void Start(IProfilingConnection &profilingConnection)
ICounterRegistry & GetCounterRegistry()
virtual const char * what() const noexcept override
#define ARMNN_LOG(severity)
void NotifyBackendsForTimelineReporting() override
void NotifyProfilingServiceActive() override
BackendRegistry & BackendRegistryInstance()
Copyright (c) 2021 ARM Limited and Contributors.
bool m_EnableProfiling
Indicates whether external profiling is enabled or not.
ProfilingDynamicGuid NextGuid() override
Return the next random Guid in the sequence.
uint32_t IncrementCounterValue(uint16_t counterUid) override
void ResetGuidGenerator()
uint32_t GetDeltaCounterValue(uint16_t counterUid) override
uint32_t SubtractCounterValue(uint16_t counterUid, uint32_t value) override
static ProfilingStaticGuid GetStaticId(const std::string &str)
CaptureData GetCaptureData() const
void SetCaptureData(uint32_t capturePeriod, const std::vector< uint16_t > &counterIds, const std::set< BackendId > &activeBackends)
void SetCaptureData(uint32_t capturePeriod, const std::vector< uint16_t > &counterIds, const std::set< armnn::BackendId > &activeBackends)
std::unique_ptr< ISendTimelinePacket > GetSendTimelinePacket() const override
const ICounterMappings & GetCounterMappings() const override
void ResetExternalProfilingOptions(const ExternalProfilingOptions &options, bool resetProfilingService=false)
#define ARMNN_ASSERT(COND)
uint32_t AddCounterValue(uint16_t counterUid, uint32_t value) override
IRegisterCounterMapping & GetCounterMappingRegistry()
void SetCounterValue(uint16_t counterUid, uint32_t value) override
CaptureData GetCaptureData() override
bool IsProfilingEnabled() const override
Base class for all ArmNN exceptions so that users can filter to just those.
bool IsCounterRegistered(uint16_t counterUid) const
static ProfilingDynamicGuid GetNextGuid()
bool m_TimelineEnabled
Indicates whether external timeline profiling is enabled or not.
void setTimelineEnabled(bool timelineEnabled)
uint16_t GetCounterCount() const override
void TransitionToState(ProfilingState newState)
void AddBackendProfilingContext(const BackendId backendId, std::shared_ptr< armnn::profiling::IBackendProfilingContext > profilingContext)
std::unique_ptr< ISendTimelinePacket > GetSendTimelinePacket() const
const ICounterDirectory & GetCounterDirectory() const
ProfilingState ConfigureProfilingService(const ExternalProfilingOptions &options, bool resetProfilingService=false)
void Stop(bool rethrowSendThreadExceptions=true) override
Stop the thread.
uint16_t GetCounterCount() const override
void Start(IProfilingConnection &profilingConnection) override
Start the thread.
ProfilingStaticGuid GenerateStaticId(const std::string &str) override
Create a ProfilingStaticGuid based on a hash of the string.