10 #include <common/include/SocketConnectionException.hpp> 12 #include <boost/format.hpp> 20 ProfilingGuidGenerator ProfilingService::m_GuidGenerator;
33 bool resetProfilingService)
41 if (resetProfilingService)
55 bool resetProfilingService)
112 switch (currentState)
124 m_CommandHandler.
Stop();
127 m_SendThread.
Stop(
false);
130 m_PeriodicCounterCapture.
Stop();
133 m_ProfilingConnection.reset();
139 m_ProfilingConnection = m_ProfilingConnectionFactory->GetProfilingConnection(m_Options);
143 ARMNN_LOG(
warning) <<
"An error has occurred when creating the profiling connection: " 146 catch (
const armnnProfiling::SocketConnectionException& e)
148 ARMNN_LOG(
warning) <<
"An error has occurred when creating the profiling connection [" 149 << e.what() <<
"] on socket [" << e.GetSocketFd() <<
"].";
162 m_CommandHandler.
Start(*m_ProfilingConnection);
166 m_SendThread.
Start(*m_ProfilingConnection);
179 throw RuntimeException(boost::str(boost::format(
"Unknown profiling service state: %1")
180 % static_cast<int>(currentState)));
187 switch (currentState)
199 throw RuntimeException(boost::str(boost::format(
"Unknown profiling service state: %1")
200 % static_cast<int>(currentState)));
206 std::shared_ptr<armnn::profiling::IBackendProfilingContext> profilingContext)
210 m_MaxGlobalCounterId = profilingContext->RegisterCounters(m_MaxGlobalCounterId);
211 m_BackendProfilingContexts.emplace(backendId, std::move(profilingContext));
215 return m_CounterDirectory;
220 return m_CounterDirectory;
240 CheckCounterUid(counterUid);
241 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
243 return counterValuePtr->load(std::memory_order::memory_order_relaxed);
248 CheckCounterUid(counterUid);
249 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
251 const uint32_t counterValue = counterValuePtr->load(std::memory_order::memory_order_relaxed);
258 return m_CounterIdMap;
263 return m_CounterIdMap;
272 const std::vector<uint16_t>& counterIds,
273 const std::set<BackendId>& activeBackends)
275 m_Holder.
SetCaptureData(capturePeriod, counterIds, activeBackends);
280 CheckCounterUid(counterUid);
281 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
283 counterValuePtr->store(value, std::memory_order::memory_order_relaxed);
288 CheckCounterUid(counterUid);
289 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
291 return counterValuePtr->fetch_add(value, std::memory_order::memory_order_relaxed);
296 CheckCounterUid(counterUid);
297 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
299 return counterValuePtr->fetch_sub(value, std::memory_order::memory_order_relaxed);
304 CheckCounterUid(counterUid);
305 std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid);
307 return counterValuePtr->operator++(std::memory_order::memory_order_relaxed);
325 void ProfilingService::Initialize()
336 const Counter* loadedNetworksCounter =
338 armnn::profiling::NETWORK_LOADS,
344 "The number of networks loaded at runtime",
345 std::string(
"networks"));
347 InitializeCounterValue(loadedNetworksCounter->
m_Uid);
352 const Counter* unloadedNetworksCounter =
354 armnn::profiling::NETWORK_UNLOADS,
360 "The number of networks unloaded at runtime",
361 std::string(
"networks"));
363 InitializeCounterValue(unloadedNetworksCounter->
m_Uid);
368 const Counter* registeredBackendsCounter =
370 armnn::profiling::REGISTERED_BACKENDS,
375 "Backends registered",
376 "The number of registered backends",
377 std::string(
"backends"));
379 InitializeCounterValue(registeredBackendsCounter->
m_Uid);
388 const Counter* unregisteredBackendsCounter =
390 armnn::profiling::UNREGISTERED_BACKENDS,
395 "Backends unregistered",
396 "The number of unregistered backends",
397 std::string(
"backends"));
399 InitializeCounterValue(unregisteredBackendsCounter->
m_Uid);
404 const Counter* inferencesRunCounter =
406 armnn::profiling::INFERENCES_RUN,
412 "The number of inferences run",
413 std::string(
"inferences"));
415 InitializeCounterValue(inferencesRunCounter->
m_Uid);
419 void ProfilingService::InitializeCounterValue(uint16_t counterUid)
422 if (counterUid >= m_CounterIndex.size())
424 m_CounterIndex.resize(boost::numeric_cast<size_t>(counterUid) + 1);
428 m_CounterValues.emplace_back(0);
431 std::atomic<uint32_t>* counterValuePtr = &(m_CounterValues.back());
432 m_CounterIndex.at(counterUid) = counterValuePtr;
435 void ProfilingService::Reset()
441 m_CounterIndex.clear();
442 m_CounterValues.clear();
443 m_CounterDirectory.
Clear();
444 m_CounterIdMap.
Reset();
445 m_BufferManager.
Reset();
448 m_StateMachine.
Reset();
449 m_BackendProfilingContexts.clear();
450 m_MaxGlobalCounterId = armnn::profiling::MAX_ARMNN_COUNTER;
453 void ProfilingService::Stop()
458 m_CommandHandler.
Stop();
459 m_PeriodicCounterCapture.
Stop();
461 m_SendThread.
Stop(
false);
464 if (m_ProfilingConnection !=
nullptr && m_ProfilingConnection->IsOpen())
466 m_ProfilingConnection->Close();
468 m_ProfilingConnection.reset();
474 inline void ProfilingService::CheckCounterUid(uint16_t counterUid)
const 484 BackendProfilingContext::iterator it = m_BackendProfilingContexts.begin();
485 while (it != m_BackendProfilingContexts.end())
487 auto& backendProfilingContext = it->second;
488 backendProfilingContext->EnableTimelineReporting(m_TimelineReporting);
bool IsCounterRegistered(uint16_t counterUid) const override
bool IsCategoryRegistered(const std::string &categoryName) const
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()
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
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
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
armnn::Runtime::CreationOptions::ExternalProfilingOptions options
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.