From e0e6efc1072358b843f47d2ffffc3d873a4889c6 Mon Sep 17 00:00:00 2001 From: Matteo Martincigh Date: Fri, 4 Oct 2019 17:17:42 +0100 Subject: IVGCVSW-3937 Refactor and improve the PeriodicCounterCapture class * Conformed the PeriodicCounterCapture class to the other thread-based classes * Code refactoring * Renamed CounterValues file to ICounterValues * Removed no longer used file * Updated unit tests accordingly Signed-off-by: Matteo Martincigh Change-Id: I8c42aa17e17a90cda5cf86eb8ac2d13501ecdadc --- src/profiling/PeriodicCounterCapture.cpp | 114 +++++++++++++++---------------- 1 file changed, 57 insertions(+), 57 deletions(-) (limited to 'src/profiling/PeriodicCounterCapture.cpp') diff --git a/src/profiling/PeriodicCounterCapture.cpp b/src/profiling/PeriodicCounterCapture.cpp index 5fbaa5d035..9002bfc065 100644 --- a/src/profiling/PeriodicCounterCapture.cpp +++ b/src/profiling/PeriodicCounterCapture.cpp @@ -11,82 +11,82 @@ namespace armnn namespace profiling { -PeriodicCounterCapture::PeriodicCounterCapture(const Holder& data, ISendCounterPacket& packet, - const IReadCounterValue& readCounterValue) - : m_CaptureDataHolder(data) - , m_IsRunning(false) - , m_ReadCounterValue(readCounterValue) - , m_SendCounterPacket(packet) -{} +void PeriodicCounterCapture::Start() +{ + // Check if the capture thread is already running + if (m_IsRunning.load()) + { + // The capture thread is already running + return; + } + + // Mark the capture thread as running + m_IsRunning.store(true); + + // Keep the capture procedure going until the capture thread is signalled to stop + m_KeepRunning.store(true); + + // Start the new capture thread. + m_PeriodCaptureThread = std::thread(&PeriodicCounterCapture::Capture, + this, + std::ref(m_ReadCounterValues)); +} + +void PeriodicCounterCapture::Stop() +{ + m_KeepRunning.store(false); + + if (m_PeriodCaptureThread.joinable()) + { + m_PeriodCaptureThread.join(); + } +} CaptureData PeriodicCounterCapture::ReadCaptureData() { return m_CaptureDataHolder.GetCaptureData(); } -void PeriodicCounterCapture::Functionality(const IReadCounterValue& readCounterValue) +void PeriodicCounterCapture::Capture(const IReadCounterValues& readCounterValues) { - bool threadRunning = true; - - while(threadRunning) + while (m_KeepRunning.load()) { auto currentCaptureData = ReadCaptureData(); std::vector counterIds = currentCaptureData.GetCounterIds(); if (currentCaptureData.GetCapturePeriod() == 0 || counterIds.empty()) { - threadRunning = false; - m_IsRunning.store(false, std::memory_order_relaxed); + m_KeepRunning.store(false); + break; } - else - { - std::vector> values; - auto numCounters = counterIds.size(); - values.reserve(numCounters); - - // Create vector of pairs of CounterIndexes and Values - uint32_t counterValue; - for (uint16_t index = 0; index < numCounters; ++index) - { - auto requestedId = counterIds[index]; - readCounterValue.GetCounterValue(requestedId, counterValue); - values.emplace_back(std::make_pair(requestedId, counterValue)); - } - - #if USE_CLOCK_MONOTONIC_RAW - using clock = MonotonicClockRaw; - #else - using clock = std::chrono::steady_clock; - #endif - // Take a timestamp - auto timestamp = clock::now(); - - m_SendCounterPacket.SendPeriodicCounterCapturePacket( - static_cast(timestamp.time_since_epoch().count()), values); - std::this_thread::sleep_for(std::chrono::milliseconds(currentCaptureData.GetCapturePeriod())); - } - } -} -void PeriodicCounterCapture::Start() -{ - bool tstVal = false; + std::vector> values; + auto numCounters = counterIds.size(); + values.reserve(numCounters); - if (m_IsRunning.compare_exchange_strong(tstVal, true, std::memory_order_relaxed)) - { - // Check that the thread execution is finished. - if (m_PeriodCaptureThread.joinable()) + // Create vector of pairs of CounterIndexes and Values + uint32_t counterValue = 0; + for (uint16_t index = 0; index < numCounters; ++index) { - m_PeriodCaptureThread.join(); + auto requestedId = counterIds[index]; + counterValue = readCounterValues.GetCounterValue(requestedId); + values.emplace_back(std::make_pair(requestedId, counterValue)); } - // Starts the new thread. - m_PeriodCaptureThread = std::thread(&PeriodicCounterCapture::Functionality, this, - std::ref(m_ReadCounterValue)); + + #if USE_CLOCK_MONOTONIC_RAW + using clock = MonotonicClockRaw; + #else + using clock = std::chrono::steady_clock; + #endif + + // Take a timestamp + auto timestamp = clock::now(); + + m_SendCounterPacket.SendPeriodicCounterCapturePacket( + static_cast(timestamp.time_since_epoch().count()), values); + std::this_thread::sleep_for(std::chrono::milliseconds(currentCaptureData.GetCapturePeriod())); } -} -void PeriodicCounterCapture::Join() -{ - m_PeriodCaptureThread.join(); + m_IsRunning.store(false); } } // namespace profiling -- cgit v1.2.1