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 | |
Matteo Martincigh | e0e6efc | 2019-10-04 17:17:42 +0100 | [diff] [blame^] | 14 | void PeriodicCounterCapture::Start() |
| 15 | { |
| 16 | // Check if the capture thread is already running |
| 17 | if (m_IsRunning.load()) |
| 18 | { |
| 19 | // The capture thread is already running |
| 20 | return; |
| 21 | } |
| 22 | |
| 23 | // Mark the capture thread as running |
| 24 | m_IsRunning.store(true); |
| 25 | |
| 26 | // Keep the capture procedure going until the capture thread is signalled to stop |
| 27 | m_KeepRunning.store(true); |
| 28 | |
| 29 | // Start the new capture thread. |
| 30 | m_PeriodCaptureThread = std::thread(&PeriodicCounterCapture::Capture, |
| 31 | this, |
| 32 | std::ref(m_ReadCounterValues)); |
| 33 | } |
| 34 | |
| 35 | void PeriodicCounterCapture::Stop() |
| 36 | { |
| 37 | m_KeepRunning.store(false); |
| 38 | |
| 39 | if (m_PeriodCaptureThread.joinable()) |
| 40 | { |
| 41 | m_PeriodCaptureThread.join(); |
| 42 | } |
| 43 | } |
Francis Murtagh | fcb8ef6 | 2019-09-20 15:40:09 +0100 | [diff] [blame] | 44 | |
| 45 | CaptureData PeriodicCounterCapture::ReadCaptureData() |
| 46 | { |
| 47 | return m_CaptureDataHolder.GetCaptureData(); |
| 48 | } |
| 49 | |
Matteo Martincigh | e0e6efc | 2019-10-04 17:17:42 +0100 | [diff] [blame^] | 50 | void PeriodicCounterCapture::Capture(const IReadCounterValues& readCounterValues) |
Francis Murtagh | fcb8ef6 | 2019-09-20 15:40:09 +0100 | [diff] [blame] | 51 | { |
Matteo Martincigh | e0e6efc | 2019-10-04 17:17:42 +0100 | [diff] [blame^] | 52 | while (m_KeepRunning.load()) |
Francis Murtagh | fcb8ef6 | 2019-09-20 15:40:09 +0100 | [diff] [blame] | 53 | { |
| 54 | auto currentCaptureData = ReadCaptureData(); |
| 55 | std::vector<uint16_t> counterIds = currentCaptureData.GetCounterIds(); |
| 56 | if (currentCaptureData.GetCapturePeriod() == 0 || counterIds.empty()) |
| 57 | { |
Matteo Martincigh | e0e6efc | 2019-10-04 17:17:42 +0100 | [diff] [blame^] | 58 | m_KeepRunning.store(false); |
| 59 | break; |
Francis Murtagh | fcb8ef6 | 2019-09-20 15:40:09 +0100 | [diff] [blame] | 60 | } |
Matteo Martincigh | e0e6efc | 2019-10-04 17:17:42 +0100 | [diff] [blame^] | 61 | |
| 62 | std::vector<std::pair<uint16_t, uint32_t>> values; |
| 63 | auto numCounters = counterIds.size(); |
| 64 | values.reserve(numCounters); |
| 65 | |
| 66 | // Create vector of pairs of CounterIndexes and Values |
| 67 | uint32_t counterValue = 0; |
| 68 | for (uint16_t index = 0; index < numCounters; ++index) |
Francis Murtagh | fcb8ef6 | 2019-09-20 15:40:09 +0100 | [diff] [blame] | 69 | { |
Matteo Martincigh | e0e6efc | 2019-10-04 17:17:42 +0100 | [diff] [blame^] | 70 | auto requestedId = counterIds[index]; |
| 71 | counterValue = readCounterValues.GetCounterValue(requestedId); |
| 72 | values.emplace_back(std::make_pair(requestedId, counterValue)); |
| 73 | } |
Francis Murtagh | fcb8ef6 | 2019-09-20 15:40:09 +0100 | [diff] [blame] | 74 | |
Matteo Martincigh | e0e6efc | 2019-10-04 17:17:42 +0100 | [diff] [blame^] | 75 | #if USE_CLOCK_MONOTONIC_RAW |
| 76 | using clock = MonotonicClockRaw; |
| 77 | #else |
| 78 | using clock = std::chrono::steady_clock; |
| 79 | #endif |
Francis Murtagh | fcb8ef6 | 2019-09-20 15:40:09 +0100 | [diff] [blame] | 80 | |
Matteo Martincigh | e0e6efc | 2019-10-04 17:17:42 +0100 | [diff] [blame^] | 81 | // Take a timestamp |
| 82 | auto timestamp = clock::now(); |
Francis Murtagh | fcb8ef6 | 2019-09-20 15:40:09 +0100 | [diff] [blame] | 83 | |
Matteo Martincigh | e0e6efc | 2019-10-04 17:17:42 +0100 | [diff] [blame^] | 84 | m_SendCounterPacket.SendPeriodicCounterCapturePacket( |
Francis Murtagh | fcb8ef6 | 2019-09-20 15:40:09 +0100 | [diff] [blame] | 85 | static_cast<uint64_t>(timestamp.time_since_epoch().count()), values); |
Matteo Martincigh | e0e6efc | 2019-10-04 17:17:42 +0100 | [diff] [blame^] | 86 | std::this_thread::sleep_for(std::chrono::milliseconds(currentCaptureData.GetCapturePeriod())); |
Francis Murtagh | fcb8ef6 | 2019-09-20 15:40:09 +0100 | [diff] [blame] | 87 | } |
Francis Murtagh | fcb8ef6 | 2019-09-20 15:40:09 +0100 | [diff] [blame] | 88 | |
Matteo Martincigh | e0e6efc | 2019-10-04 17:17:42 +0100 | [diff] [blame^] | 89 | m_IsRunning.store(false); |
Francis Murtagh | fcb8ef6 | 2019-09-20 15:40:09 +0100 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | } // namespace profiling |
| 93 | |
| 94 | } // namespace armnn |