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