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 | |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 8 | #include <boost/log/trivial.hpp> |
Finn Williams | f4d59a6 | 2019-10-14 15:55:18 +0100 | [diff] [blame] | 9 | #include <iostream> |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 10 | |
Francis Murtagh | fcb8ef6 | 2019-09-20 15:40:09 +0100 | [diff] [blame] | 11 | namespace armnn |
| 12 | { |
| 13 | |
| 14 | namespace profiling |
| 15 | { |
| 16 | |
Matteo Martincigh | e0e6efc | 2019-10-04 17:17:42 +0100 | [diff] [blame] | 17 | void PeriodicCounterCapture::Start() |
| 18 | { |
| 19 | // Check if the capture thread is already running |
Finn Williams | f4d59a6 | 2019-10-14 15:55:18 +0100 | [diff] [blame] | 20 | if (m_IsRunning) |
Matteo Martincigh | e0e6efc | 2019-10-04 17:17:42 +0100 | [diff] [blame] | 21 | { |
| 22 | // The capture thread is already running |
| 23 | return; |
| 24 | } |
| 25 | |
| 26 | // Mark the capture thread as running |
Finn Williams | f4d59a6 | 2019-10-14 15:55:18 +0100 | [diff] [blame] | 27 | m_IsRunning = true; |
Matteo Martincigh | e0e6efc | 2019-10-04 17:17:42 +0100 | [diff] [blame] | 28 | |
| 29 | // Keep the capture procedure going until the capture thread is signalled to stop |
| 30 | m_KeepRunning.store(true); |
| 31 | |
| 32 | // Start the new capture thread. |
| 33 | m_PeriodCaptureThread = std::thread(&PeriodicCounterCapture::Capture, |
| 34 | this, |
| 35 | std::ref(m_ReadCounterValues)); |
| 36 | } |
| 37 | |
| 38 | void PeriodicCounterCapture::Stop() |
| 39 | { |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 40 | // Signal the capture thread to stop |
Matteo Martincigh | e0e6efc | 2019-10-04 17:17:42 +0100 | [diff] [blame] | 41 | m_KeepRunning.store(false); |
| 42 | |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 43 | // Check that the capture thread is running |
Matteo Martincigh | e0e6efc | 2019-10-04 17:17:42 +0100 | [diff] [blame] | 44 | if (m_PeriodCaptureThread.joinable()) |
| 45 | { |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 46 | // Wait for the capture thread to complete operations |
Matteo Martincigh | e0e6efc | 2019-10-04 17:17:42 +0100 | [diff] [blame] | 47 | m_PeriodCaptureThread.join(); |
| 48 | } |
Finn Williams | f4d59a6 | 2019-10-14 15:55:18 +0100 | [diff] [blame] | 49 | |
| 50 | m_IsRunning = false; |
Matteo Martincigh | e0e6efc | 2019-10-04 17:17:42 +0100 | [diff] [blame] | 51 | } |
Francis Murtagh | fcb8ef6 | 2019-09-20 15:40:09 +0100 | [diff] [blame] | 52 | |
| 53 | CaptureData PeriodicCounterCapture::ReadCaptureData() |
| 54 | { |
| 55 | return m_CaptureDataHolder.GetCaptureData(); |
| 56 | } |
| 57 | |
Matteo Martincigh | e0e6efc | 2019-10-04 17:17:42 +0100 | [diff] [blame] | 58 | void PeriodicCounterCapture::Capture(const IReadCounterValues& readCounterValues) |
Francis Murtagh | fcb8ef6 | 2019-09-20 15:40:09 +0100 | [diff] [blame] | 59 | { |
Finn Williams | f4d59a6 | 2019-10-14 15:55:18 +0100 | [diff] [blame] | 60 | do |
Francis Murtagh | fcb8ef6 | 2019-09-20 15:40:09 +0100 | [diff] [blame] | 61 | { |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 62 | // Check if the current capture data indicates that there's data capture |
Francis Murtagh | fcb8ef6 | 2019-09-20 15:40:09 +0100 | [diff] [blame] | 63 | auto currentCaptureData = ReadCaptureData(); |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 64 | const std::vector<uint16_t>& counterIds = currentCaptureData.GetCounterIds(); |
Finn Williams | f4d59a6 | 2019-10-14 15:55:18 +0100 | [diff] [blame] | 65 | |
Francis Murtagh | fcb8ef6 | 2019-09-20 15:40:09 +0100 | [diff] [blame] | 66 | if (currentCaptureData.GetCapturePeriod() == 0 || counterIds.empty()) |
| 67 | { |
Finn Williams | f4d59a6 | 2019-10-14 15:55:18 +0100 | [diff] [blame] | 68 | // No data capture, wait the indicated capture period (milliseconds) |
| 69 | std::this_thread::sleep_for(std::chrono::milliseconds(5)); |
| 70 | continue; |
Francis Murtagh | fcb8ef6 | 2019-09-20 15:40:09 +0100 | [diff] [blame] | 71 | } |
Matteo Martincigh | e0e6efc | 2019-10-04 17:17:42 +0100 | [diff] [blame] | 72 | |
| 73 | std::vector<std::pair<uint16_t, uint32_t>> values; |
| 74 | auto numCounters = counterIds.size(); |
| 75 | values.reserve(numCounters); |
| 76 | |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 77 | // Create a vector of pairs of CounterIndexes and Values |
Matteo Martincigh | e0e6efc | 2019-10-04 17:17:42 +0100 | [diff] [blame] | 78 | for (uint16_t index = 0; index < numCounters; ++index) |
Francis Murtagh | fcb8ef6 | 2019-09-20 15:40:09 +0100 | [diff] [blame] | 79 | { |
Matteo Martincigh | e0e6efc | 2019-10-04 17:17:42 +0100 | [diff] [blame] | 80 | auto requestedId = counterIds[index]; |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 81 | uint32_t counterValue = 0; |
| 82 | try |
| 83 | { |
| 84 | counterValue = readCounterValues.GetCounterValue(requestedId); |
| 85 | } |
| 86 | catch (const Exception& e) |
| 87 | { |
| 88 | // Report the error and continue |
| 89 | BOOST_LOG_TRIVIAL(warning) << "An error has occurred when getting a counter value: " |
| 90 | << e.what() << std::endl; |
| 91 | continue; |
| 92 | } |
Matteo Martincigh | e0e6efc | 2019-10-04 17:17:42 +0100 | [diff] [blame] | 93 | values.emplace_back(std::make_pair(requestedId, counterValue)); |
| 94 | } |
Francis Murtagh | fcb8ef6 | 2019-09-20 15:40:09 +0100 | [diff] [blame] | 95 | |
Matteo Martincigh | e0e6efc | 2019-10-04 17:17:42 +0100 | [diff] [blame] | 96 | #if USE_CLOCK_MONOTONIC_RAW |
| 97 | using clock = MonotonicClockRaw; |
| 98 | #else |
| 99 | using clock = std::chrono::steady_clock; |
| 100 | #endif |
Francis Murtagh | fcb8ef6 | 2019-09-20 15:40:09 +0100 | [diff] [blame] | 101 | |
Matteo Martincigh | e0e6efc | 2019-10-04 17:17:42 +0100 | [diff] [blame] | 102 | // Take a timestamp |
| 103 | auto timestamp = clock::now(); |
Francis Murtagh | fcb8ef6 | 2019-09-20 15:40:09 +0100 | [diff] [blame] | 104 | |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 105 | // Write a Periodic Counter Capture packet to the Counter Stream Buffer |
Matteo Martincigh | e0e6efc | 2019-10-04 17:17:42 +0100 | [diff] [blame] | 106 | m_SendCounterPacket.SendPeriodicCounterCapturePacket( |
Francis Murtagh | fcb8ef6 | 2019-09-20 15:40:09 +0100 | [diff] [blame] | 107 | static_cast<uint64_t>(timestamp.time_since_epoch().count()), values); |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 108 | |
| 109 | // Notify the Send Thread that new data is available in the Counter Stream Buffer |
| 110 | m_SendCounterPacket.SetReadyToRead(); |
| 111 | |
| 112 | // Wait the indicated capture period (microseconds) |
| 113 | std::this_thread::sleep_for(std::chrono::microseconds(currentCaptureData.GetCapturePeriod())); |
Francis Murtagh | fcb8ef6 | 2019-09-20 15:40:09 +0100 | [diff] [blame] | 114 | |
Finn Williams | f4d59a6 | 2019-10-14 15:55:18 +0100 | [diff] [blame] | 115 | } |
| 116 | while (m_KeepRunning.load()); |
| 117 | |
Francis Murtagh | fcb8ef6 | 2019-09-20 15:40:09 +0100 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | } // namespace profiling |
| 121 | |
| 122 | } // namespace armnn |