Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 1 | // |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 2 | // Copyright © 2019 Arm Ltd and Contributors. All rights reserved. |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "PeriodicCounterSelectionCommandHandler.hpp" |
| 7 | #include "ProfilingUtils.hpp" |
| 8 | |
Jim Flynn | 2776183 | 2022-03-20 21:52:17 +0000 | [diff] [blame] | 9 | #include <client/include/ProfilingOptions.hpp> |
| 10 | |
Jim Flynn | 75c14f4 | 2022-03-10 22:05:42 +0000 | [diff] [blame] | 11 | #include <common/include/NumericCast.hpp> |
Matthew Sloyan | 371b70e | 2020-09-11 10:14:57 +0100 | [diff] [blame] | 12 | |
Jan Eilers | 156113c | 2020-09-09 19:11:16 +0100 | [diff] [blame] | 13 | #include <fmt/format.h> |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 14 | |
| 15 | #include <vector> |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 16 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 17 | namespace arm |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 18 | { |
| 19 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 20 | namespace pipe |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 21 | { |
| 22 | |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 23 | void PeriodicCounterSelectionCommandHandler::ParseData(const arm::pipe::Packet& packet, CaptureData& captureData) |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 24 | { |
| 25 | std::vector<uint16_t> counterIds; |
Jim Flynn | 75c14f4 | 2022-03-10 22:05:42 +0000 | [diff] [blame] | 26 | uint32_t sizeOfUint32 = arm::pipe::numeric_cast<uint32_t>(sizeof(uint32_t)); |
| 27 | uint32_t sizeOfUint16 = arm::pipe::numeric_cast<uint32_t>(sizeof(uint16_t)); |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 28 | uint32_t offset = 0; |
| 29 | |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 30 | if (packet.GetLength() < 4) |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 31 | { |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 32 | // Insufficient packet size |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | // Parse the capture period |
| 37 | uint32_t capturePeriod = ReadUint32(packet.GetData(), offset); |
| 38 | |
| 39 | // Set the capture period |
| 40 | captureData.SetCapturePeriod(capturePeriod); |
| 41 | |
| 42 | // Parse the counter ids |
| 43 | unsigned int counters = (packet.GetLength() - 4) / 2; |
| 44 | if (counters > 0) |
| 45 | { |
| 46 | counterIds.reserve(counters); |
| 47 | offset += sizeOfUint32; |
| 48 | for (unsigned int i = 0; i < counters; ++i) |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 49 | { |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 50 | // Parse the counter id |
| 51 | uint16_t counterId = ReadUint16(packet.GetData(), offset); |
| 52 | counterIds.emplace_back(counterId); |
| 53 | offset += sizeOfUint16; |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 54 | } |
| 55 | } |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 56 | |
| 57 | // Set the counter ids |
| 58 | captureData.SetCounterIds(counterIds); |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 59 | } |
| 60 | |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 61 | void PeriodicCounterSelectionCommandHandler::operator()(const arm::pipe::Packet& packet) |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 62 | { |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 63 | ProfilingState currentState = m_StateMachine.GetCurrentState(); |
| 64 | switch (currentState) |
| 65 | { |
| 66 | case ProfilingState::Uninitialised: |
| 67 | case ProfilingState::NotConnected: |
| 68 | case ProfilingState::WaitingForAck: |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 69 | throw arm::pipe::ProfilingException(fmt::format("Periodic Counter Selection Command Handler invoked while in " |
| 70 | "an wrong state: {}", |
| 71 | GetProfilingStateName(currentState))); |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 72 | case ProfilingState::Active: |
| 73 | { |
| 74 | // Process the packet |
| 75 | if (!(packet.GetPacketFamily() == 0u && packet.GetPacketId() == 4u)) |
| 76 | { |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 77 | throw arm::pipe::InvalidArgumentException(fmt::format("Expected Packet family = 0, id = 4 but " |
| 78 | "received family = {}, id = {}", |
| 79 | packet.GetPacketFamily(), |
| 80 | packet.GetPacketId())); |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 81 | } |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 82 | |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 83 | // Parse the packet to get the capture period and counter UIDs |
| 84 | CaptureData captureData; |
| 85 | ParseData(packet, captureData); |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 86 | |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 87 | // Get the capture data |
Colm Donelan | 0270524 | 2019-11-14 14:19:07 +0000 | [diff] [blame] | 88 | uint32_t capturePeriod = captureData.GetCapturePeriod(); |
| 89 | // Validate that the capture period is within the acceptable range. |
Jim Flynn | 4c9ed1d | 2022-01-23 23:57:20 +0000 | [diff] [blame] | 90 | if (capturePeriod > 0 && capturePeriod < arm::pipe::LOWEST_CAPTURE_PERIOD) |
Colm Donelan | 0270524 | 2019-11-14 14:19:07 +0000 | [diff] [blame] | 91 | { |
Jim Flynn | 4c9ed1d | 2022-01-23 23:57:20 +0000 | [diff] [blame] | 92 | capturePeriod = arm::pipe::LOWEST_CAPTURE_PERIOD; |
Colm Donelan | 0270524 | 2019-11-14 14:19:07 +0000 | [diff] [blame] | 93 | } |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 94 | const std::vector<uint16_t>& counterIds = captureData.GetCounterIds(); |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 95 | |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 96 | // Check whether the selected counter UIDs are valid |
| 97 | std::vector<uint16_t> validCounterIds; |
| 98 | for (uint16_t counterId : counterIds) |
| 99 | { |
| 100 | // Check whether the counter is registered |
| 101 | if (!m_ReadCounterValues.IsCounterRegistered(counterId)) |
| 102 | { |
| 103 | // Invalid counter UID, ignore it and continue |
| 104 | continue; |
| 105 | } |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 106 | // The counter is valid |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 107 | validCounterIds.emplace_back(counterId); |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 108 | } |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 109 | |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 110 | std::sort(validCounterIds.begin(), validCounterIds.end()); |
| 111 | |
| 112 | auto backendIdStart = std::find_if(validCounterIds.begin(), validCounterIds.end(), [&](uint16_t& counterId) |
| 113 | { |
| 114 | return counterId > m_MaxArmCounterId; |
| 115 | }); |
| 116 | |
Cathal Corbett | 6f07372 | 2022-03-04 12:11:09 +0000 | [diff] [blame] | 117 | std::set<std::string> activeBackends; |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 118 | std::set<uint16_t> backendCounterIds = std::set<uint16_t>(backendIdStart, validCounterIds.end()); |
| 119 | |
| 120 | if (m_BackendCounterMap.size() != 0) |
| 121 | { |
| 122 | std::set<uint16_t> newCounterIds; |
| 123 | std::set<uint16_t> unusedCounterIds; |
| 124 | |
| 125 | // Get any backend counter ids that is in backendCounterIds but not in m_PrevBackendCounterIds |
| 126 | std::set_difference(backendCounterIds.begin(), backendCounterIds.end(), |
| 127 | m_PrevBackendCounterIds.begin(), m_PrevBackendCounterIds.end(), |
| 128 | std::inserter(newCounterIds, newCounterIds.begin())); |
| 129 | |
| 130 | // Get any backend counter ids that is in m_PrevBackendCounterIds but not in backendCounterIds |
| 131 | std::set_difference(m_PrevBackendCounterIds.begin(), m_PrevBackendCounterIds.end(), |
| 132 | backendCounterIds.begin(), backendCounterIds.end(), |
| 133 | std::inserter(unusedCounterIds, unusedCounterIds.begin())); |
| 134 | |
| 135 | activeBackends = ProcessBackendCounterIds(capturePeriod, newCounterIds, unusedCounterIds); |
| 136 | } |
| 137 | else |
| 138 | { |
| 139 | activeBackends = ProcessBackendCounterIds(capturePeriod, backendCounterIds, {}); |
| 140 | } |
| 141 | |
| 142 | // save the new backend counter ids for next time |
| 143 | m_PrevBackendCounterIds = backendCounterIds; |
| 144 | |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 145 | // Set the capture data with only the valid armnn counter UIDs |
| 146 | m_CaptureDataHolder.SetCaptureData(capturePeriod, {validCounterIds.begin(), backendIdStart}, activeBackends); |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 147 | |
| 148 | // Echo back the Periodic Counter Selection packet to the Counter Stream Buffer |
| 149 | m_SendCounterPacket.SendPeriodicCounterSelectionPacket(capturePeriod, validCounterIds); |
| 150 | |
Finn Williams | f4d59a6 | 2019-10-14 15:55:18 +0100 | [diff] [blame] | 151 | if (capturePeriod == 0 || validCounterIds.empty()) |
| 152 | { |
| 153 | // No data capture stop the thread |
| 154 | m_PeriodicCounterCapture.Stop(); |
| 155 | } |
| 156 | else |
| 157 | { |
| 158 | // Start the Period Counter Capture thread (if not running already) |
| 159 | m_PeriodicCounterCapture.Start(); |
| 160 | } |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 161 | |
| 162 | break; |
| 163 | } |
| 164 | default: |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 165 | throw arm::pipe::ProfilingException(fmt::format("Unknown profiling service state: {}", |
| 166 | static_cast<int>(currentState))); |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 167 | } |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 168 | } |
| 169 | |
Cathal Corbett | 6f07372 | 2022-03-04 12:11:09 +0000 | [diff] [blame] | 170 | std::set<std::string> PeriodicCounterSelectionCommandHandler::ProcessBackendCounterIds( |
Rob Hughes | b98032f | 2020-04-24 11:41:34 +0100 | [diff] [blame] | 171 | const uint32_t capturePeriod, |
Finn Williams | fe5a24b | 2020-04-09 16:05:28 +0100 | [diff] [blame] | 172 | const std::set<uint16_t> newCounterIds, |
| 173 | const std::set<uint16_t> unusedCounterIds) |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 174 | { |
Cathal Corbett | 6f07372 | 2022-03-04 12:11:09 +0000 | [diff] [blame] | 175 | std::set<std::string> changedBackends; |
| 176 | std::set<std::string> activeBackends = m_CaptureDataHolder.GetCaptureData().GetActiveBackends(); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 177 | |
| 178 | for (uint16_t counterId : newCounterIds) |
| 179 | { |
| 180 | auto backendId = m_CounterIdMap.GetBackendId(counterId); |
| 181 | m_BackendCounterMap[backendId.second].emplace_back(backendId.first); |
| 182 | changedBackends.insert(backendId.second); |
| 183 | } |
| 184 | // Add any new backends to active backends |
| 185 | activeBackends.insert(changedBackends.begin(), changedBackends.end()); |
| 186 | |
| 187 | for (uint16_t counterId : unusedCounterIds) |
| 188 | { |
| 189 | auto backendId = m_CounterIdMap.GetBackendId(counterId); |
| 190 | std::vector<uint16_t>& backendCounters = m_BackendCounterMap[backendId.second]; |
| 191 | |
| 192 | backendCounters.erase(std::remove(backendCounters.begin(), backendCounters.end(), backendId.first)); |
| 193 | |
| 194 | if(backendCounters.size() == 0) |
| 195 | { |
| 196 | // If a backend has no counters associated with it we remove it from active backends and |
| 197 | // send a capture period of zero with an empty vector, this will deactivate all the backends counters |
| 198 | activeBackends.erase(backendId.second); |
Cathal Corbett | 6f07372 | 2022-03-04 12:11:09 +0000 | [diff] [blame] | 199 | ActivateBackendCounters(backendId.second, 0, {}); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 200 | } |
| 201 | else |
| 202 | { |
| 203 | changedBackends.insert(backendId.second); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | // If the capture period remains the same we only need to update the backends who's counters have changed |
| 208 | if(capturePeriod == m_PrevCapturePeriod) |
| 209 | { |
| 210 | for (auto backend : changedBackends) |
| 211 | { |
Cathal Corbett | 6f07372 | 2022-03-04 12:11:09 +0000 | [diff] [blame] | 212 | ActivateBackendCounters(backend, capturePeriod, m_BackendCounterMap[backend]); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | // Otherwise update all the backends with the new capture period and any new/unused counters |
| 216 | else |
| 217 | { |
| 218 | for (auto backend : m_BackendCounterMap) |
| 219 | { |
Cathal Corbett | 6f07372 | 2022-03-04 12:11:09 +0000 | [diff] [blame] | 220 | ActivateBackendCounters(backend.first, capturePeriod, backend.second); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 221 | } |
| 222 | if(capturePeriod == 0) |
| 223 | { |
| 224 | activeBackends = {}; |
| 225 | } |
| 226 | m_PrevCapturePeriod = capturePeriod; |
| 227 | } |
| 228 | |
| 229 | return activeBackends; |
| 230 | } |
| 231 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 232 | } // namespace pipe |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 233 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 234 | } // namespace arm |