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