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 | |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 8 | #include <armnn/Logging.hpp> |
| 9 | |
Finn Williams | f4d59a6 | 2019-10-14 15:55:18 +0100 | [diff] [blame] | 10 | #include <iostream> |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 11 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 12 | namespace arm |
Francis Murtagh | fcb8ef6 | 2019-09-20 15:40:09 +0100 | [diff] [blame] | 13 | { |
| 14 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 15 | namespace pipe |
Francis Murtagh | fcb8ef6 | 2019-09-20 15:40:09 +0100 | [diff] [blame] | 16 | { |
| 17 | |
Matteo Martincigh | e0e6efc | 2019-10-04 17:17:42 +0100 | [diff] [blame] | 18 | void PeriodicCounterCapture::Start() |
| 19 | { |
| 20 | // Check if the capture thread is already running |
Finn Williams | f4d59a6 | 2019-10-14 15:55:18 +0100 | [diff] [blame] | 21 | if (m_IsRunning) |
Matteo Martincigh | e0e6efc | 2019-10-04 17:17:42 +0100 | [diff] [blame] | 22 | { |
| 23 | // The capture thread is already running |
| 24 | return; |
| 25 | } |
| 26 | |
| 27 | // Mark the capture thread as running |
Finn Williams | f4d59a6 | 2019-10-14 15:55:18 +0100 | [diff] [blame] | 28 | m_IsRunning = true; |
Matteo Martincigh | e0e6efc | 2019-10-04 17:17:42 +0100 | [diff] [blame] | 29 | |
| 30 | // Keep the capture procedure going until the capture thread is signalled to stop |
| 31 | m_KeepRunning.store(true); |
| 32 | |
| 33 | // Start the new capture thread. |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 34 | m_PeriodCaptureThread = std::thread(&PeriodicCounterCapture::Capture, this, std::ref(m_ReadCounterValues)); |
Matteo Martincigh | e0e6efc | 2019-10-04 17:17:42 +0100 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | void PeriodicCounterCapture::Stop() |
| 38 | { |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 39 | // Signal the capture thread to stop |
Matteo Martincigh | e0e6efc | 2019-10-04 17:17:42 +0100 | [diff] [blame] | 40 | m_KeepRunning.store(false); |
| 41 | |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 42 | // Check that the capture thread is running |
Matteo Martincigh | e0e6efc | 2019-10-04 17:17:42 +0100 | [diff] [blame] | 43 | if (m_PeriodCaptureThread.joinable()) |
| 44 | { |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 45 | // Wait for the capture thread to complete operations |
Matteo Martincigh | e0e6efc | 2019-10-04 17:17:42 +0100 | [diff] [blame] | 46 | m_PeriodCaptureThread.join(); |
| 47 | } |
Finn Williams | f4d59a6 | 2019-10-14 15:55:18 +0100 | [diff] [blame] | 48 | |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 49 | // Mark the capture thread as not running |
Finn Williams | f4d59a6 | 2019-10-14 15:55:18 +0100 | [diff] [blame] | 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 | |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 58 | void PeriodicCounterCapture::DispatchPeriodicCounterCapturePacket( |
Cathal Corbett | 6f07372 | 2022-03-04 12:11:09 +0000 | [diff] [blame] | 59 | const std::string& backendId, const std::vector<Timestamp>& timestampValues) |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 60 | { |
| 61 | // Report counter values |
Derek Lamberti | eac4adb | 2020-08-25 13:05:59 +0100 | [diff] [blame] | 62 | for (const auto& timestampInfo : timestampValues) |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 63 | { |
| 64 | std::vector<CounterValue> backendCounterValues = timestampInfo.counterValues; |
| 65 | for_each(backendCounterValues.begin(), backendCounterValues.end(), [&](CounterValue& backendCounterValue) |
| 66 | { |
| 67 | // translate the counterId to globalCounterId |
| 68 | backendCounterValue.counterId = m_CounterIdMap.GetGlobalId(backendCounterValue.counterId, backendId); |
| 69 | }); |
| 70 | |
| 71 | // Send Periodic Counter Capture Packet for the Timestamp |
| 72 | m_SendCounterPacket.SendPeriodicCounterCapturePacket(timestampInfo.timestamp, backendCounterValues); |
| 73 | } |
| 74 | } |
| 75 | |
Finn Williams | f3fcf32 | 2020-05-11 14:38:02 +0100 | [diff] [blame] | 76 | void PeriodicCounterCapture::Capture(IReadCounterValues& readCounterValues) |
Francis Murtagh | fcb8ef6 | 2019-09-20 15:40:09 +0100 | [diff] [blame] | 77 | { |
Finn Williams | f4d59a6 | 2019-10-14 15:55:18 +0100 | [diff] [blame] | 78 | do |
Francis Murtagh | fcb8ef6 | 2019-09-20 15:40:09 +0100 | [diff] [blame] | 79 | { |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 80 | // Check if the current capture data indicates that there's data capture |
Francis Murtagh | fcb8ef6 | 2019-09-20 15:40:09 +0100 | [diff] [blame] | 81 | auto currentCaptureData = ReadCaptureData(); |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 82 | const std::vector<uint16_t>& counterIds = currentCaptureData.GetCounterIds(); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 83 | const uint32_t capturePeriod = currentCaptureData.GetCapturePeriod(); |
Finn Williams | f4d59a6 | 2019-10-14 15:55:18 +0100 | [diff] [blame] | 84 | |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 85 | if (capturePeriod == 0) |
Francis Murtagh | fcb8ef6 | 2019-09-20 15:40:09 +0100 | [diff] [blame] | 86 | { |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 87 | // No data capture, wait the indicated capture period (milliseconds), if it is not zero |
| 88 | std::this_thread::sleep_for(std::chrono::milliseconds(50u)); |
Finn Williams | f4d59a6 | 2019-10-14 15:55:18 +0100 | [diff] [blame] | 89 | continue; |
Francis Murtagh | fcb8ef6 | 2019-09-20 15:40:09 +0100 | [diff] [blame] | 90 | } |
Matteo Martincigh | e0e6efc | 2019-10-04 17:17:42 +0100 | [diff] [blame] | 91 | |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 92 | if(counterIds.size() != 0) |
Francis Murtagh | fcb8ef6 | 2019-09-20 15:40:09 +0100 | [diff] [blame] | 93 | { |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 94 | std::vector<CounterValue> counterValues; |
| 95 | |
| 96 | auto numCounters = counterIds.size(); |
| 97 | counterValues.reserve(numCounters); |
| 98 | |
| 99 | // Create a vector of pairs of CounterIndexes and Values |
| 100 | for (uint16_t index = 0; index < numCounters; ++index) |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 101 | { |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 102 | auto requestedId = counterIds[index]; |
| 103 | uint32_t counterValue = 0; |
| 104 | try |
| 105 | { |
Finn Williams | f3fcf32 | 2020-05-11 14:38:02 +0100 | [diff] [blame] | 106 | counterValue = readCounterValues.GetDeltaCounterValue(requestedId); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 107 | } |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 108 | catch (const arm::pipe::ProfilingException& e) |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 109 | { |
| 110 | // Report the error and continue |
| 111 | ARMNN_LOG(warning) << "An error has occurred when getting a counter value: " |
| 112 | << e.what(); |
| 113 | continue; |
| 114 | } |
| 115 | |
| 116 | counterValues.emplace_back(CounterValue {requestedId, counterValue }); |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 117 | } |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 118 | |
| 119 | // Send Periodic Counter Capture Packet for the Timestamp |
| 120 | m_SendCounterPacket.SendPeriodicCounterCapturePacket(GetTimestamp(), counterValues); |
Matteo Martincigh | e0e6efc | 2019-10-04 17:17:42 +0100 | [diff] [blame] | 121 | } |
Francis Murtagh | fcb8ef6 | 2019-09-20 15:40:09 +0100 | [diff] [blame] | 122 | |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 123 | // Report counter values for each active backend |
| 124 | auto activeBackends = currentCaptureData.GetActiveBackends(); |
Cathal Corbett | 6f07372 | 2022-03-04 12:11:09 +0000 | [diff] [blame] | 125 | for_each(activeBackends.begin(), activeBackends.end(), [&](const std::string& backendId) |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 126 | { |
| 127 | DispatchPeriodicCounterCapturePacket( |
Finn Williams | fe5a24b | 2020-04-09 16:05:28 +0100 | [diff] [blame] | 128 | backendId, m_BackendProfilingContexts.at(backendId)->ReportCounterValues()); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 129 | }); |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 130 | |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 131 | // Wait the indicated capture period (microseconds) |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 132 | std::this_thread::sleep_for(std::chrono::microseconds(capturePeriod)); |
Finn Williams | f4d59a6 | 2019-10-14 15:55:18 +0100 | [diff] [blame] | 133 | } |
| 134 | while (m_KeepRunning.load()); |
Francis Murtagh | fcb8ef6 | 2019-09-20 15:40:09 +0100 | [diff] [blame] | 135 | } |
| 136 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 137 | } // namespace pipe |
Francis Murtagh | fcb8ef6 | 2019-09-20 15:40:09 +0100 | [diff] [blame] | 138 | |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 139 | } // namespace arm |