Francis Murtagh | fcb8ef6 | 2019-09-20 15:40:09 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2019 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "PeriodicCounterCapture.hpp" |
| 7 | |
| 8 | namespace armnn |
| 9 | { |
| 10 | |
| 11 | namespace profiling |
| 12 | { |
| 13 | |
| 14 | PeriodicCounterCapture::PeriodicCounterCapture(const Holder& data, ISendCounterPacket& packet, |
| 15 | const IReadCounterValue& readCounterValue) |
| 16 | : m_CaptureDataHolder(data) |
| 17 | , m_IsRunning(false) |
| 18 | , m_ReadCounterValue(readCounterValue) |
| 19 | , m_SendCounterPacket(packet) |
| 20 | {} |
| 21 | |
| 22 | CaptureData PeriodicCounterCapture::ReadCaptureData() |
| 23 | { |
| 24 | return m_CaptureDataHolder.GetCaptureData(); |
| 25 | } |
| 26 | |
| 27 | void PeriodicCounterCapture::Functionality(const IReadCounterValue& readCounterValue) |
| 28 | { |
| 29 | bool threadRunning = true; |
| 30 | |
| 31 | while(threadRunning) |
| 32 | { |
| 33 | auto currentCaptureData = ReadCaptureData(); |
| 34 | std::vector<uint16_t> counterIds = currentCaptureData.GetCounterIds(); |
| 35 | if (currentCaptureData.GetCapturePeriod() == 0 || counterIds.empty()) |
| 36 | { |
| 37 | threadRunning = false; |
| 38 | m_IsRunning.store(false, std::memory_order_relaxed); |
| 39 | } |
| 40 | else |
| 41 | { |
| 42 | std::vector<std::pair<uint16_t, uint32_t>> values; |
| 43 | auto numCounters = counterIds.size(); |
| 44 | values.reserve(numCounters); |
| 45 | |
| 46 | // Create vector of pairs of CounterIndexes and Values |
| 47 | uint32_t counterValue; |
| 48 | for (uint16_t index = 0; index < numCounters; ++index) |
| 49 | { |
| 50 | auto requestedId = counterIds[index]; |
| 51 | readCounterValue.GetCounterValue(requestedId, counterValue); |
| 52 | values.emplace_back(std::make_pair(requestedId, counterValue)); |
| 53 | } |
| 54 | |
| 55 | #if USE_CLOCK_MONOTONIC_RAW |
| 56 | using clock = MonotonicClockRaw; |
| 57 | #else |
| 58 | using clock = std::chrono::steady_clock; |
| 59 | #endif |
| 60 | // Take a timestamp |
| 61 | auto timestamp = clock::now(); |
| 62 | |
| 63 | m_SendCounterPacket.SendPeriodicCounterCapturePacket( |
| 64 | static_cast<uint64_t>(timestamp.time_since_epoch().count()), values); |
| 65 | std::this_thread::sleep_for(std::chrono::milliseconds(currentCaptureData.GetCapturePeriod())); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | void PeriodicCounterCapture::Start() |
| 71 | { |
| 72 | bool tstVal = false; |
| 73 | |
| 74 | if (m_IsRunning.compare_exchange_strong(tstVal, true, std::memory_order_relaxed)) |
| 75 | { |
| 76 | // Check that the thread execution is finished. |
| 77 | if (m_PeriodCaptureThread.joinable()) |
| 78 | { |
| 79 | m_PeriodCaptureThread.join(); |
| 80 | } |
| 81 | // Starts the new thread. |
| 82 | m_PeriodCaptureThread = std::thread(&PeriodicCounterCapture::Functionality, this, |
| 83 | std::ref(m_ReadCounterValue)); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | void PeriodicCounterCapture::Join() |
| 88 | { |
| 89 | m_PeriodCaptureThread.join(); |
| 90 | } |
| 91 | |
| 92 | } // namespace profiling |
| 93 | |
| 94 | } // namespace armnn |