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