12 #include <common/include/SocketConnectionException.hpp> 14 #include <fmt/format.h> 22 ProfilingGuidGenerator ProfilingService::m_GuidGenerator;
36 m_GuidGenerator.
Reset();
40 bool resetProfilingService)
48 if (resetProfilingService)
62 bool resetProfilingService)
119 switch (currentState)
131 m_CommandHandler.
Stop();
134 m_SendThread.
Stop(
false);
137 m_PeriodicCounterCapture.
Stop();
140 m_ProfilingConnection.reset();
146 m_ProfilingConnection = m_ProfilingConnectionFactory->GetProfilingConnection(m_Options);
150 ARMNN_LOG(
warning) <<
"An error has occurred when creating the profiling connection: " 153 catch (
const arm::pipe::SocketConnectionException& e)
155 ARMNN_LOG(
warning) <<
"An error has occurred when creating the profiling connection [" 156 << e.what() <<
"] on socket [" << e.GetSocketFd() <<
"].";
169 m_CommandHandler.
Start(*m_ProfilingConnection);
173 m_SendThread.
Start(*m_ProfilingConnection);
187 static_cast<int>(currentState)));
194 switch (currentState)
207 static_cast<int>(currentState)));
213 std::shared_ptr<armnn::profiling::IBackendProfilingContext> profilingContext)
217 m_MaxGlobalCounterId = profilingContext->RegisterCounters(m_MaxGlobalCounterId);
218 m_BackendProfilingContexts.emplace(backendId, std::move(profilingContext));
222 return m_CounterDirectory;
227 return m_CounterDirectory;
247 CheckCounterUid(counterUid);
248 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
250 return counterValuePtr->load(std::memory_order::memory_order_relaxed);
255 CheckCounterUid(counterUid);
256 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
258 const uint32_t counterValue = counterValuePtr->load(std::memory_order::memory_order_relaxed);
265 return m_CounterIdMap;
270 return m_CounterIdMap;
279 const std::vector<uint16_t>& counterIds,
280 const std::set<BackendId>& activeBackends)
282 m_Holder.
SetCaptureData(capturePeriod, counterIds, activeBackends);
287 CheckCounterUid(counterUid);
288 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
290 counterValuePtr->store(value, std::memory_order::memory_order_relaxed);
295 CheckCounterUid(counterUid);
296 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
298 return counterValuePtr->fetch_add(value, std::memory_order::memory_order_relaxed);
303 CheckCounterUid(counterUid);
304 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
306 return counterValuePtr->fetch_sub(value, std::memory_order::memory_order_relaxed);
311 CheckCounterUid(counterUid);
312 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
314 return counterValuePtr->operator++(std::memory_order::memory_order_relaxed);
332 void ProfilingService::Initialize()
343 const Counter* loadedNetworksCounter =
345 armnn::profiling::NETWORK_LOADS,
351 "The number of networks loaded at runtime",
352 std::string(
"networks"));
354 InitializeCounterValue(loadedNetworksCounter->
m_Uid);
359 const Counter* unloadedNetworksCounter =
361 armnn::profiling::NETWORK_UNLOADS,
367 "The number of networks unloaded at runtime",
368 std::string(
"networks"));
370 InitializeCounterValue(unloadedNetworksCounter->
m_Uid);
375 const Counter* registeredBackendsCounter =
377 armnn::profiling::REGISTERED_BACKENDS,
382 "Backends registered",
383 "The number of registered backends",
384 std::string(
"backends"));
386 InitializeCounterValue(registeredBackendsCounter->
m_Uid);
395 const Counter* unregisteredBackendsCounter =
397 armnn::profiling::UNREGISTERED_BACKENDS,
402 "Backends unregistered",
403 "The number of unregistered backends",
404 std::string(
"backends"));
406 InitializeCounterValue(unregisteredBackendsCounter->
m_Uid);
411 const Counter* inferencesRunCounter =
413 armnn::profiling::INFERENCES_RUN,
419 "The number of inferences run",
420 std::string(
"inferences"));
422 InitializeCounterValue(inferencesRunCounter->
m_Uid);
426 void ProfilingService::InitializeCounterValue(uint16_t counterUid)
429 if (counterUid >= m_CounterIndex.size())
431 m_CounterIndex.resize(armnn::numeric_cast<size_t>(counterUid) + 1);
435 m_CounterValues.emplace_back(0);
438 std::atomic<uint32_t>* counterValuePtr = &(m_CounterValues.back());
439 m_CounterIndex.at(counterUid) = counterValuePtr;
442 void ProfilingService::Reset()
448 m_CounterIndex.clear();
449 m_CounterValues.clear();
450 m_CounterDirectory.
Clear();
451 m_CounterIdMap.
Reset();
452 m_BufferManager.
Reset();
455 m_StateMachine.
Reset();
456 m_BackendProfilingContexts.clear();
457 m_MaxGlobalCounterId = armnn::profiling::MAX_ARMNN_COUNTER;
460 void ProfilingService::Stop()
463 std::unique_lock<std::mutex> lck(m_ServiceActiveMutex);
464 m_ServiceActive =
false;
469 m_CommandHandler.
Stop();
470 m_PeriodicCounterCapture.
Stop();
472 m_SendThread.
Stop(
false);
475 if (m_ProfilingConnection !=
nullptr && m_ProfilingConnection->IsOpen())
477 m_ProfilingConnection->Close();
479 m_ProfilingConnection.reset();
485 inline void ProfilingService::CheckCounterUid(uint16_t counterUid)
const 495 BackendProfilingContext::iterator it = m_BackendProfilingContexts.begin();
496 while (it != m_BackendProfilingContexts.end())
498 auto& backendProfilingContext = it->second;
499 backendProfilingContext->EnableTimelineReporting(m_TimelineReporting);
508 std::unique_lock<std::mutex> lck(m_ServiceActiveMutex);
509 m_ServiceActive =
true;
511 m_ServiceActiveConditionVariable.notify_one();
516 std::unique_lock<std::mutex> lck(m_ServiceActiveMutex);
518 auto start = std::chrono::high_resolution_clock::now();
521 if (!m_ServiceActiveConditionVariable.wait_for(lck,
522 std::chrono::milliseconds(timeout),
523 [&]{return m_ServiceActive == true;}))
525 if (m_ServiceActive ==
true)
529 auto finish = std::chrono::high_resolution_clock::now();
530 std::chrono::duration<double, std::milli> elapsed = finish - start;
531 std::stringstream ss;
532 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()
void Reset()
Reset the generator back to zero. Used mainly for test.
Strongly typed guids to distinguish between those generated at runtime, and those that are statically...
virtual const char * what() const noexcept override
#define ARMNN_LOG(severity)
void NotifyBackendsForTimelineReporting() override
void NotifyProfilingServiceActive() override
BackendRegistry & BackendRegistryInstance()
Copyright (c) 2020 ARM Limited.
ProfilingStaticGuid GenerateStaticId(const std::string &str) override
Create a ProfilingStaticGuid based on a hash of the string.
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
ProfilingDynamicGuid NextGuid() override
Return the next random Guid in the sequence.
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()
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.