From 54fb957c9640d61ab575d7acfc4c430a15123315 Mon Sep 17 00:00:00 2001 From: Matteo Martincigh Date: Wed, 2 Oct 2019 12:50:57 +0100 Subject: IVGCVSW-3937 Add the necessary components to the ProfilingService class to process a connection to an external profiling service (e.g. gatord) * Added the required components (CommandHandlerRegistry, CommandHandler, SendCounterPacket, ...) to the ProfilingService class * Reworked the ProfilingService::Run procedure and renamed it to Update * Handling all states but Active in the Run method (future work) * Updated the unit and tests accordingly * Added component tests to check that the Connection Acknowledged packet is handled correctly * Added test util classes, made the default constructor/destructor protected to superclass a ProfilingService object * Added IProfilingConnectionFactory interface Signed-off-by: Matteo Martincigh Change-Id: I010d94b18980c9e6394253f4b2bbe4fe5bb3fe4f --- src/profiling/ProfilingService.cpp | 92 +++++++++++++++++++++++++++----------- 1 file changed, 67 insertions(+), 25 deletions(-) (limited to 'src/profiling/ProfilingService.cpp') diff --git a/src/profiling/ProfilingService.cpp b/src/profiling/ProfilingService.cpp index 2da0f79da2..19cf9cb58e 100644 --- a/src/profiling/ProfilingService.cpp +++ b/src/profiling/ProfilingService.cpp @@ -20,37 +20,76 @@ void ProfilingService::ResetExternalProfilingOptions(const ExternalProfilingOpti // Update the profiling options m_Options = options; + // Check if the profiling service needs to be reset if (resetProfilingService) { // Reset the profiling service - m_CounterDirectory.Clear(); - m_ProfilingConnection.reset(); - m_StateMachine.Reset(); - m_CounterIndex.clear(); - m_CounterValues.clear(); + Reset(); } - - // Re-initialize the profiling service - Initialize(); } -void ProfilingService::Run() +void ProfilingService::Update() { - if (m_StateMachine.GetCurrentState() == ProfilingState::Uninitialised) + if (!m_Options.m_EnableProfiling) { - Initialize(); + // Don't run if profiling is disabled + return; } - else if (m_StateMachine.GetCurrentState() == ProfilingState::NotConnected) + + ProfilingState currentState = m_StateMachine.GetCurrentState(); + switch (currentState) { + case ProfilingState::Uninitialised: + // Initialize the profiling service + Initialize(); + + // Move to the next state + m_StateMachine.TransitionToState(ProfilingState::NotConnected); + break; + case ProfilingState::NotConnected: + BOOST_ASSERT(m_ProfilingConnectionFactory); + + // Reset any existing profiling connection + m_ProfilingConnection.reset(); + try { - m_ProfilingConnectionFactory.GetProfilingConnection(m_Options); - m_StateMachine.TransitionToState(ProfilingState::WaitingForAck); + // Setup the profiling connection + //m_ProfilingConnection = m_ProfilingConnectionFactory.GetProfilingConnection(m_Options); + m_ProfilingConnection = m_ProfilingConnectionFactory->GetProfilingConnection(m_Options); } - catch (const armnn::Exception& e) + catch (const Exception& e) { - std::cerr << e.what() << std::endl; + BOOST_LOG_TRIVIAL(warning) << "An error has occurred when creating the profiling connection: " + << e.what(); } + + // Move to the next state + m_StateMachine.TransitionToState(m_ProfilingConnection + ? ProfilingState::WaitingForAck // Profiling connection obtained, wait for ack + : ProfilingState::NotConnected); // Profiling connection failed, stay in the + // "NotConnected" state + break; + case ProfilingState::WaitingForAck: + BOOST_ASSERT(m_ProfilingConnection); + + // Start the command thread + m_CommandHandler.Start(*m_ProfilingConnection); + + // Start the send thread, while in "WaitingForAck" state it'll send out a "Stream MetaData" packet waiting for + // a valid "Connection Acknowledged" packet confirming the connection + m_SendCounterPacket.Start(*m_ProfilingConnection); + + // The connection acknowledged command handler will automatically transition the state to "Active" once a + // valid "Connection Acknowledged" packet has been received + + break; + case ProfilingState::Active: + + break; + default: + throw RuntimeException(boost::str(boost::format("Unknown profiling service state: %1") + % static_cast(currentState))); } } @@ -119,12 +158,6 @@ uint32_t ProfilingService::DecrementCounterValue(uint16_t counterUid) void ProfilingService::Initialize() { - if (!m_Options.m_EnableProfiling) - { - // Skip the initialization if profiling is disabled - return; - } - // Register a category for the basic runtime counters if (!m_CounterDirectory.IsCategoryRegistered("ArmNN_Runtime")) { @@ -175,9 +208,6 @@ void ProfilingService::Initialize() BOOST_ASSERT(inferencesRunCounter); InitializeCounterValue(inferencesRunCounter->m_Uid); } - - // Initialization is done, update the profiling service state - m_StateMachine.TransitionToState(ProfilingState::NotConnected); } void ProfilingService::InitializeCounterValue(uint16_t counterUid) @@ -196,6 +226,18 @@ void ProfilingService::InitializeCounterValue(uint16_t counterUid) m_CounterIndex.at(counterUid) = counterValuePtr; } +void ProfilingService::Reset() +{ + // Reset the profiling service + m_CounterDirectory.Clear(); + m_ProfilingConnection.reset(); + m_StateMachine.Reset(); + m_CounterIndex.clear(); + m_CounterValues.clear(); + m_CommandHandler.Stop(); + m_SendCounterPacket.Stop(false); +} + } // namespace profiling } // namespace armnn -- cgit v1.2.1