10 #include <common/include/SocketConnectionException.hpp> 12 #include <boost/format.hpp> 20 ProfilingGuidGenerator ProfilingService::m_GuidGenerator;
34 m_GuidGenerator.
Reset();
38 bool resetProfilingService)
46 if (resetProfilingService)
60 bool resetProfilingService)
117 switch (currentState)
129 m_CommandHandler.
Stop();
132 m_SendThread.
Stop(
false);
135 m_PeriodicCounterCapture.
Stop();
138 m_ProfilingConnection.reset();
144 m_ProfilingConnection = m_ProfilingConnectionFactory->GetProfilingConnection(m_Options);
148 ARMNN_LOG(
warning) <<
"An error has occurred when creating the profiling connection: " 151 catch (
const arm::pipe::SocketConnectionException& e)
153 ARMNN_LOG(
warning) <<
"An error has occurred when creating the profiling connection [" 154 << e.what() <<
"] on socket [" << e.GetSocketFd() <<
"].";
167 m_CommandHandler.
Start(*m_ProfilingConnection);
171 m_SendThread.
Start(*m_ProfilingConnection);
184 throw RuntimeException(boost::str(boost::format(
"Unknown profiling service state: %1")
185 % static_cast<int>(currentState)));
192 switch (currentState)
204 throw RuntimeException(boost::str(boost::format(
"Unknown profiling service state: %1")
205 % static_cast<int>(currentState)));
211 std::shared_ptr<armnn::profiling::IBackendProfilingContext> profilingContext)
215 m_MaxGlobalCounterId = profilingContext->RegisterCounters(m_MaxGlobalCounterId);
216 m_BackendProfilingContexts.emplace(backendId, std::move(profilingContext));
220 return m_CounterDirectory;
225 return m_CounterDirectory;
245 CheckCounterUid(counterUid);
246 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
248 return counterValuePtr->load(std::memory_order::memory_order_relaxed);
253 CheckCounterUid(counterUid);
254 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
256 const uint32_t counterValue = counterValuePtr->load(std::memory_order::memory_order_relaxed);
263 return m_CounterIdMap;
268 return m_CounterIdMap;
277 const std::vector<uint16_t>& counterIds,
278 const std::set<BackendId>& activeBackends)
280 m_Holder.
SetCaptureData(capturePeriod, counterIds, activeBackends);
285 CheckCounterUid(counterUid);
286 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
288 counterValuePtr->store(value, std::memory_order::memory_order_relaxed);
293 CheckCounterUid(counterUid);
294 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
296 return counterValuePtr->fetch_add(value, std::memory_order::memory_order_relaxed);
301 CheckCounterUid(counterUid);
302 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
304 return counterValuePtr->fetch_sub(value, std::memory_order::memory_order_relaxed);
309 CheckCounterUid(counterUid);
310 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
312 return counterValuePtr->operator++(std::memory_order::memory_order_relaxed);
330 void ProfilingService::Initialize()
341 const Counter* loadedNetworksCounter =
343 armnn::profiling::NETWORK_LOADS,
349 "The number of networks loaded at runtime",
350 std::string(
"networks"));
352 InitializeCounterValue(loadedNetworksCounter->
m_Uid);
357 const Counter* unloadedNetworksCounter =
359 armnn::profiling::NETWORK_UNLOADS,
365 "The number of networks unloaded at runtime",
366 std::string(
"networks"));
368 InitializeCounterValue(unloadedNetworksCounter->
m_Uid);
373 const Counter* registeredBackendsCounter =
375 armnn::profiling::REGISTERED_BACKENDS,
380 "Backends registered",
381 "The number of registered backends",
382 std::string(
"backends"));
384 InitializeCounterValue(registeredBackendsCounter->
m_Uid);
393 const Counter* unregisteredBackendsCounter =
395 armnn::profiling::UNREGISTERED_BACKENDS,
400 "Backends unregistered",
401 "The number of unregistered backends",
402 std::string(
"backends"));
404 InitializeCounterValue(unregisteredBackendsCounter->
m_Uid);
409 const Counter* inferencesRunCounter =
411 armnn::profiling::INFERENCES_RUN,
417 "The number of inferences run",
418 std::string(
"inferences"));
420 InitializeCounterValue(inferencesRunCounter->
m_Uid);
424 void ProfilingService::InitializeCounterValue(uint16_t counterUid)
427 if (counterUid >= m_CounterIndex.size())
429 m_CounterIndex.resize(boost::numeric_cast<size_t>(counterUid) + 1);
433 m_CounterValues.emplace_back(0);
436 std::atomic<uint32_t>* counterValuePtr = &(m_CounterValues.back());
437 m_CounterIndex.at(counterUid) = counterValuePtr;
440 void ProfilingService::Reset()
446 m_CounterIndex.clear();
447 m_CounterValues.clear();
448 m_CounterDirectory.
Clear();
449 m_CounterIdMap.
Reset();
450 m_BufferManager.
Reset();
453 m_StateMachine.
Reset();
454 m_BackendProfilingContexts.clear();
455 m_MaxGlobalCounterId = armnn::profiling::MAX_ARMNN_COUNTER;
458 void ProfilingService::Stop()
461 std::unique_lock<std::mutex> lck(m_ServiceActiveMutex);
462 m_ServiceActive =
false;
467 m_CommandHandler.
Stop();
468 m_PeriodicCounterCapture.
Stop();
470 m_SendThread.
Stop(
false);
473 if (m_ProfilingConnection !=
nullptr && m_ProfilingConnection->IsOpen())
475 m_ProfilingConnection->Close();
477 m_ProfilingConnection.reset();
483 inline void ProfilingService::CheckCounterUid(uint16_t counterUid)
const 493 BackendProfilingContext::iterator it = m_BackendProfilingContexts.begin();
494 while (it != m_BackendProfilingContexts.end())
496 auto& backendProfilingContext = it->second;
497 backendProfilingContext->EnableTimelineReporting(m_TimelineReporting);
506 std::unique_lock<std::mutex> lck(m_ServiceActiveMutex);
507 m_ServiceActive =
true;
509 m_ServiceActiveConditionVariable.notify_one();
514 std::unique_lock<std::mutex> lck(m_ServiceActiveMutex);
516 auto start = std::chrono::high_resolution_clock::now();
519 if (!m_ServiceActiveConditionVariable.wait_for(lck,
520 std::chrono::milliseconds(timeout),
521 [&]{return m_ServiceActive == true;}))
523 if (m_ServiceActive ==
true)
527 auto finish = std::chrono::high_resolution_clock::now();
528 std::chrono::duration<double, std::milli> elapsed = finish - start;
529 std::stringstream ss;
530 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.