David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 1 | // |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 2 | // Copyright © 2020 Arm Ltd and Contributors. All rights reserved. |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
Jim Flynn | 3443025 | 2022-03-04 15:03:58 +0000 | [diff] [blame] | 6 | #include "ArmNNProfilingServiceInitialiser.hpp" |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 7 | #include "CounterDirectory.hpp" |
| 8 | #include "CounterIdMap.hpp" |
| 9 | #include "Holder.hpp" |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 10 | #include "MockBackend.hpp" |
| 11 | #include "MockBackendId.hpp" |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 12 | #include "PeriodicCounterCapture.hpp" |
| 13 | #include "PeriodicCounterSelectionCommandHandler.hpp" |
Jim Flynn | 4c9ed1d | 2022-01-23 23:57:20 +0000 | [diff] [blame] | 14 | #include "ProfilingOptionsConverter.hpp" |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 15 | #include "ProfilingStateMachine.hpp" |
| 16 | #include "ProfilingUtils.hpp" |
| 17 | #include "RequestCounterDirectoryCommandHandler.hpp" |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 18 | |
Sadik Armagan | a097d2a | 2021-11-24 15:47:28 +0000 | [diff] [blame] | 19 | #include <TestUtils.hpp> |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 20 | |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 21 | #include <armnn/utility/IgnoreUnused.hpp> |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 22 | #include <armnn/BackendId.hpp> |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 23 | #include <armnn/Logging.hpp> |
Colm Donelan | fcb802b | 2020-02-13 20:47:08 +0000 | [diff] [blame] | 24 | #include <armnn/profiling/ISendTimelinePacket.hpp> |
Jim Flynn | 4c9ed1d | 2022-01-23 23:57:20 +0000 | [diff] [blame] | 25 | #include <armnn/profiling/ProfilingOptions.hpp> |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 26 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 27 | #include <doctest/doctest.h> |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 28 | #include <vector> |
| 29 | |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 30 | #include <cstdint> |
| 31 | #include <limits> |
| 32 | #include <backends/BackendProfiling.hpp> |
| 33 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 34 | using namespace arm::pipe; |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 35 | |
| 36 | class ReadCounterVals : public IReadCounterValues |
| 37 | { |
| 38 | virtual bool IsCounterRegistered(uint16_t counterUid) const override |
| 39 | { |
| 40 | return (counterUid > 4 && counterUid < 11); |
| 41 | } |
Jim Flynn | 3443025 | 2022-03-04 15:03:58 +0000 | [diff] [blame] | 42 | virtual bool IsCounterRegistered(const std::string& counterName) const override |
| 43 | { |
| 44 | armnn::IgnoreUnused(counterName); |
| 45 | return false; |
| 46 | } |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 47 | virtual uint16_t GetCounterCount() const override |
| 48 | { |
| 49 | return 1; |
| 50 | } |
Finn Williams | f3fcf32 | 2020-05-11 14:38:02 +0100 | [diff] [blame] | 51 | virtual uint32_t GetAbsoluteCounterValue(uint16_t counterUid) const override |
| 52 | { |
| 53 | return counterUid; |
| 54 | } |
| 55 | virtual uint32_t GetDeltaCounterValue(uint16_t counterUid) override |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 56 | { |
| 57 | return counterUid; |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | class MockBackendSendCounterPacket : public ISendCounterPacket |
| 62 | { |
| 63 | public: |
| 64 | using IndexValuePairsVector = std::vector<CounterValue>; |
| 65 | |
| 66 | /// Create and write a StreamMetaDataPacket in the buffer |
| 67 | virtual void SendStreamMetaDataPacket() {} |
| 68 | |
| 69 | /// Create and write a CounterDirectoryPacket from the parameters to the buffer. |
| 70 | virtual void SendCounterDirectoryPacket(const ICounterDirectory& counterDirectory) |
| 71 | { |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 72 | armnn::IgnoreUnused(counterDirectory); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /// Create and write a PeriodicCounterCapturePacket from the parameters to the buffer. |
| 76 | virtual void SendPeriodicCounterCapturePacket(uint64_t timestamp, const IndexValuePairsVector& values) |
| 77 | { |
| 78 | m_timestamps.emplace_back(Timestamp{timestamp, values}); |
| 79 | } |
| 80 | |
| 81 | /// Create and write a PeriodicCounterSelectionPacket from the parameters to the buffer. |
| 82 | virtual void SendPeriodicCounterSelectionPacket(uint32_t capturePeriod, |
| 83 | const std::vector<uint16_t>& selectedCounterIds) |
| 84 | { |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 85 | armnn::IgnoreUnused(capturePeriod); |
| 86 | armnn::IgnoreUnused(selectedCounterIds); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | std::vector<Timestamp> GetTimestamps() |
| 90 | { |
| 91 | return m_timestamps; |
| 92 | } |
| 93 | |
| 94 | void ClearTimestamps() |
| 95 | { |
| 96 | m_timestamps.clear(); |
| 97 | } |
| 98 | |
| 99 | private: |
| 100 | std::vector<Timestamp> m_timestamps; |
| 101 | }; |
| 102 | |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 103 | arm::pipe::Packet PacketWriter(uint32_t period, std::vector<uint16_t> countervalues) |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 104 | { |
| 105 | const uint32_t packetId = 0x40000; |
| 106 | uint32_t offset = 0; |
| 107 | uint32_t dataLength = static_cast<uint32_t>(4 + countervalues.size() * 2); |
| 108 | std::unique_ptr<unsigned char[]> uniqueData = std::make_unique<unsigned char[]>(dataLength); |
| 109 | unsigned char* data1 = reinterpret_cast<unsigned char*>(uniqueData.get()); |
| 110 | |
| 111 | WriteUint32(data1, offset, period); |
| 112 | offset += 4; |
| 113 | for (auto countervalue : countervalues) |
| 114 | { |
| 115 | WriteUint16(data1, offset, countervalue); |
| 116 | offset += 2; |
| 117 | } |
| 118 | |
| 119 | return {packetId, dataLength, uniqueData}; |
| 120 | } |
| 121 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 122 | TEST_SUITE("BackendProfilingTestSuite") |
| 123 | { |
| 124 | TEST_CASE("BackendProfilingCounterRegisterMockBackendTest") |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 125 | { |
| 126 | // Reset the profiling service to the uninitialized state |
| 127 | armnn::IRuntime::CreationOptions options; |
Finn Williams | 45a7362 | 2020-05-15 18:41:05 +0100 | [diff] [blame] | 128 | options.m_ProfilingOptions.m_EnableProfiling = true; |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 129 | |
| 130 | armnn::MockBackendInitialiser initialiser; |
| 131 | // Create a runtime |
Kevin May | d92a6e4 | 2021-02-04 10:27:41 +0000 | [diff] [blame] | 132 | armnn::RuntimeImpl runtime(options); |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 133 | |
Narumol Prangnawarat | 060bad5 | 2020-11-20 16:17:48 +0000 | [diff] [blame] | 134 | unsigned int shiftedId = 0; |
| 135 | |
Nikhil Raj | 01bc02d | 2021-05-07 17:07:09 +0100 | [diff] [blame] | 136 | if (armnn::BackendRegistry().IsBackendRegistered("EthosNAcc")) |
| 137 | { |
| 138 | shiftedId = 4; |
| 139 | } |
Narumol Prangnawarat | 060bad5 | 2020-11-20 16:17:48 +0000 | [diff] [blame] | 140 | |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 141 | // Check if the MockBackends 3 dummy counters {0, 1, 2-5 (four cores)} are registered |
| 142 | armnn::BackendId mockId = armnn::MockBackendId(); |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 143 | const ICounterMappings& counterMap = GetProfilingService(&runtime).GetCounterMappings(); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 144 | CHECK(counterMap.GetGlobalId(0, mockId) == 5 + shiftedId); |
| 145 | CHECK(counterMap.GetGlobalId(1, mockId) == 6 + shiftedId); |
| 146 | CHECK(counterMap.GetGlobalId(2, mockId) == 7 + shiftedId); |
| 147 | CHECK(counterMap.GetGlobalId(3, mockId) == 8 + shiftedId); |
| 148 | CHECK(counterMap.GetGlobalId(4, mockId) == 9 + shiftedId); |
| 149 | CHECK(counterMap.GetGlobalId(5, mockId) == 10 + shiftedId); |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 150 | options.m_ProfilingOptions.m_EnableProfiling = false; |
Jim Flynn | 4c9ed1d | 2022-01-23 23:57:20 +0000 | [diff] [blame] | 151 | GetProfilingService(&runtime).ResetExternalProfilingOptions( |
| 152 | ConvertExternalProfilingOptions(options.m_ProfilingOptions), true); |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 155 | TEST_CASE("TestBackendCounters") |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 156 | { |
| 157 | Holder holder; |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 158 | arm::pipe::PacketVersionResolver packetVersionResolver; |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 159 | ProfilingStateMachine stateMachine; |
| 160 | ReadCounterVals readCounterVals; |
| 161 | CounterIdMap counterIdMap; |
| 162 | MockBackendSendCounterPacket sendCounterPacket; |
| 163 | |
Cathal Corbett | 6f07372 | 2022-03-04 12:11:09 +0000 | [diff] [blame] | 164 | const std::string cpuAccId(GetComputeDeviceAsCString(armnn::Compute::CpuAcc)); |
| 165 | const std::string gpuAccId(GetComputeDeviceAsCString(armnn::Compute::GpuAcc)); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 166 | |
Jim Flynn | 4c9ed1d | 2022-01-23 23:57:20 +0000 | [diff] [blame] | 167 | ProfilingOptions options; |
| 168 | options.m_EnableProfiling = true; |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 169 | |
Jim Flynn | 3443025 | 2022-03-04 15:03:58 +0000 | [diff] [blame] | 170 | armnn::ArmNNProfilingServiceInitialiser initialiser; |
| 171 | std::unique_ptr<IProfilingService> profilingService = arm::pipe::IProfilingService::CreateProfilingService( |
| 172 | arm::pipe::MAX_ARMNN_COUNTER, initialiser); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 173 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 174 | std::unique_ptr<IBackendProfiling> cpuBackendProfilingPtr = |
Jim Flynn | 3443025 | 2022-03-04 15:03:58 +0000 | [diff] [blame] | 175 | std::make_unique<BackendProfiling>(options, *profilingService.get(), cpuAccId); |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 176 | std::unique_ptr<IBackendProfiling> gpuBackendProfilingPtr = |
Jim Flynn | 3443025 | 2022-03-04 15:03:58 +0000 | [diff] [blame] | 177 | std::make_unique<BackendProfiling>(options, *profilingService.get(), gpuAccId); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 178 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 179 | std::shared_ptr<IBackendProfilingContext> cpuProfilingContextPtr = |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 180 | std::make_shared<armnn::MockBackendProfilingContext>(cpuBackendProfilingPtr); |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 181 | std::shared_ptr<IBackendProfilingContext> gpuProfilingContextPtr = |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 182 | std::make_shared<armnn::MockBackendProfilingContext>(gpuBackendProfilingPtr); |
| 183 | |
Cathal Corbett | 6f07372 | 2022-03-04 12:11:09 +0000 | [diff] [blame] | 184 | std::unordered_map<std::string, |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 185 | std::shared_ptr<IBackendProfilingContext>> backendProfilingContexts; |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 186 | |
| 187 | backendProfilingContexts[cpuAccId] = cpuProfilingContextPtr; |
| 188 | backendProfilingContexts[gpuAccId] = gpuProfilingContextPtr; |
| 189 | |
| 190 | uint16_t globalId = 5; |
| 191 | |
| 192 | counterIdMap.RegisterMapping(globalId++, 0, cpuAccId); |
| 193 | counterIdMap.RegisterMapping(globalId++, 1, cpuAccId); |
| 194 | counterIdMap.RegisterMapping(globalId++, 2, cpuAccId); |
| 195 | |
| 196 | counterIdMap.RegisterMapping(globalId++, 0, gpuAccId); |
| 197 | counterIdMap.RegisterMapping(globalId++, 1, gpuAccId); |
| 198 | counterIdMap.RegisterMapping(globalId++, 2, gpuAccId); |
| 199 | |
| 200 | backendProfilingContexts[cpuAccId] = cpuProfilingContextPtr; |
| 201 | backendProfilingContexts[gpuAccId] = gpuProfilingContextPtr; |
| 202 | |
| 203 | PeriodicCounterCapture periodicCounterCapture(holder, sendCounterPacket, readCounterVals, |
| 204 | counterIdMap, backendProfilingContexts); |
| 205 | |
| 206 | uint16_t maxArmnnCounterId = 4; |
| 207 | |
| 208 | PeriodicCounterSelectionCommandHandler periodicCounterSelectionCommandHandler(0, |
| 209 | 4, |
| 210 | packetVersionResolver.ResolvePacketVersion(0, 4).GetEncodedValue(), |
| 211 | backendProfilingContexts, |
| 212 | counterIdMap, |
| 213 | holder, |
| 214 | maxArmnnCounterId, |
| 215 | periodicCounterCapture, |
| 216 | readCounterVals, |
| 217 | sendCounterPacket, |
| 218 | stateMachine); |
| 219 | |
| 220 | stateMachine.TransitionToState(ProfilingState::NotConnected); |
| 221 | stateMachine.TransitionToState(ProfilingState::WaitingForAck); |
| 222 | stateMachine.TransitionToState(ProfilingState::Active); |
| 223 | |
| 224 | uint32_t period = 12345u; |
| 225 | |
| 226 | std::vector<uint16_t> cpuCounters{5, 6, 7}; |
| 227 | std::vector<uint16_t> gpuCounters{8, 9, 10}; |
| 228 | |
| 229 | // Request only gpu counters |
| 230 | periodicCounterSelectionCommandHandler(PacketWriter(period, gpuCounters)); |
| 231 | periodicCounterCapture.Stop(); |
| 232 | |
Cathal Corbett | 6f07372 | 2022-03-04 12:11:09 +0000 | [diff] [blame] | 233 | std::set<std::string> activeIds = holder.GetCaptureData().GetActiveBackends(); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 234 | CHECK(activeIds.size() == 1); |
| 235 | CHECK((activeIds.find(gpuAccId) != activeIds.end())); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 236 | |
| 237 | std::vector<Timestamp> recievedTimestamp = sendCounterPacket.GetTimestamps(); |
| 238 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 239 | CHECK(recievedTimestamp[0].timestamp == period); |
| 240 | CHECK(recievedTimestamp.size() == 1); |
| 241 | CHECK(recievedTimestamp[0].counterValues.size() == gpuCounters.size()); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 242 | for (unsigned long i=0; i< gpuCounters.size(); ++i) |
| 243 | { |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 244 | CHECK(recievedTimestamp[0].counterValues[i].counterId == gpuCounters[i]); |
| 245 | CHECK(recievedTimestamp[0].counterValues[i].counterValue == i + 1u); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 246 | } |
| 247 | sendCounterPacket.ClearTimestamps(); |
| 248 | |
| 249 | // Request only cpu counters |
| 250 | periodicCounterSelectionCommandHandler(PacketWriter(period, cpuCounters)); |
| 251 | periodicCounterCapture.Stop(); |
| 252 | |
| 253 | activeIds = holder.GetCaptureData().GetActiveBackends(); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 254 | CHECK(activeIds.size() == 1); |
| 255 | CHECK((activeIds.find(cpuAccId) != activeIds.end())); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 256 | |
| 257 | recievedTimestamp = sendCounterPacket.GetTimestamps(); |
| 258 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 259 | CHECK(recievedTimestamp[0].timestamp == period); |
| 260 | CHECK(recievedTimestamp.size() == 1); |
| 261 | CHECK(recievedTimestamp[0].counterValues.size() == cpuCounters.size()); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 262 | for (unsigned long i=0; i< cpuCounters.size(); ++i) |
| 263 | { |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 264 | CHECK(recievedTimestamp[0].counterValues[i].counterId == cpuCounters[i]); |
| 265 | CHECK(recievedTimestamp[0].counterValues[i].counterValue == i + 1u); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 266 | } |
| 267 | sendCounterPacket.ClearTimestamps(); |
| 268 | |
| 269 | // Request combination of cpu & gpu counters with new period |
| 270 | period = 12222u; |
| 271 | periodicCounterSelectionCommandHandler(PacketWriter(period, {cpuCounters[0], gpuCounters[2], |
| 272 | gpuCounters[1], cpuCounters[1], gpuCounters[0]})); |
| 273 | periodicCounterCapture.Stop(); |
| 274 | |
| 275 | activeIds = holder.GetCaptureData().GetActiveBackends(); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 276 | CHECK(activeIds.size() == 2); |
| 277 | CHECK((activeIds.find(cpuAccId) != activeIds.end())); |
| 278 | CHECK((activeIds.find(gpuAccId) != activeIds.end())); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 279 | |
| 280 | recievedTimestamp = sendCounterPacket.GetTimestamps(); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 281 | // |
| 282 | CHECK(recievedTimestamp[0].timestamp == period); |
| 283 | CHECK(recievedTimestamp[1].timestamp == period); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 284 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 285 | CHECK(recievedTimestamp.size() == 2); |
| 286 | CHECK(recievedTimestamp[0].counterValues.size() == 2); |
| 287 | CHECK(recievedTimestamp[1].counterValues.size() == gpuCounters.size()); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 288 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 289 | CHECK(recievedTimestamp[0].counterValues[0].counterId == cpuCounters[0]); |
| 290 | CHECK(recievedTimestamp[0].counterValues[0].counterValue == 1u); |
| 291 | CHECK(recievedTimestamp[0].counterValues[1].counterId == cpuCounters[1]); |
| 292 | CHECK(recievedTimestamp[0].counterValues[1].counterValue == 2u); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 293 | |
| 294 | for (unsigned long i=0; i< gpuCounters.size(); ++i) |
| 295 | { |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 296 | CHECK(recievedTimestamp[1].counterValues[i].counterId == gpuCounters[i]); |
| 297 | CHECK(recievedTimestamp[1].counterValues[i].counterValue == i + 1u); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | sendCounterPacket.ClearTimestamps(); |
| 301 | |
| 302 | // Request all counters |
| 303 | std::vector<uint16_t> counterValues; |
| 304 | counterValues.insert(counterValues.begin(), cpuCounters.begin(), cpuCounters.end()); |
| 305 | counterValues.insert(counterValues.begin(), gpuCounters.begin(), gpuCounters.end()); |
| 306 | |
| 307 | periodicCounterSelectionCommandHandler(PacketWriter(period, counterValues)); |
| 308 | periodicCounterCapture.Stop(); |
| 309 | |
| 310 | activeIds = holder.GetCaptureData().GetActiveBackends(); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 311 | CHECK(activeIds.size() == 2); |
| 312 | CHECK((activeIds.find(cpuAccId) != activeIds.end())); |
| 313 | CHECK((activeIds.find(gpuAccId) != activeIds.end())); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 314 | |
| 315 | recievedTimestamp = sendCounterPacket.GetTimestamps(); |
| 316 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 317 | CHECK(recievedTimestamp[0].counterValues.size() == cpuCounters.size()); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 318 | for (unsigned long i=0; i< cpuCounters.size(); ++i) |
| 319 | { |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 320 | CHECK(recievedTimestamp[0].counterValues[i].counterId == cpuCounters[i]); |
| 321 | CHECK(recievedTimestamp[0].counterValues[i].counterValue == i + 1u); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 324 | CHECK(recievedTimestamp[1].counterValues.size() == gpuCounters.size()); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 325 | for (unsigned long i=0; i< gpuCounters.size(); ++i) |
| 326 | { |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 327 | CHECK(recievedTimestamp[1].counterValues[i].counterId == gpuCounters[i]); |
| 328 | CHECK(recievedTimestamp[1].counterValues[i].counterValue == i + 1u); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 329 | } |
| 330 | sendCounterPacket.ClearTimestamps(); |
| 331 | |
| 332 | // Request random counters with duplicates and invalid counters |
| 333 | counterValues = {0, 0, 200, cpuCounters[2], gpuCounters[0],3 ,30, cpuCounters[0],cpuCounters[2], gpuCounters[1], 3, |
| 334 | 90, 0, 30, gpuCounters[0], gpuCounters[0]}; |
| 335 | |
| 336 | periodicCounterSelectionCommandHandler(PacketWriter(period, counterValues)); |
| 337 | periodicCounterCapture.Stop(); |
| 338 | |
| 339 | activeIds = holder.GetCaptureData().GetActiveBackends(); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 340 | CHECK(activeIds.size() == 2); |
| 341 | CHECK((activeIds.find(cpuAccId) != activeIds.end())); |
| 342 | CHECK((activeIds.find(gpuAccId) != activeIds.end())); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 343 | |
| 344 | recievedTimestamp = sendCounterPacket.GetTimestamps(); |
| 345 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 346 | CHECK(recievedTimestamp.size() == 2); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 347 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 348 | CHECK(recievedTimestamp[0].counterValues.size() == 2); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 349 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 350 | CHECK(recievedTimestamp[0].counterValues[0].counterId == cpuCounters[0]); |
| 351 | CHECK(recievedTimestamp[0].counterValues[0].counterValue == 1u); |
| 352 | CHECK(recievedTimestamp[0].counterValues[1].counterId == cpuCounters[2]); |
| 353 | CHECK(recievedTimestamp[0].counterValues[1].counterValue == 3u); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 354 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 355 | CHECK(recievedTimestamp[1].counterValues.size() == 2); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 356 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 357 | CHECK(recievedTimestamp[1].counterValues[0].counterId == gpuCounters[0]); |
| 358 | CHECK(recievedTimestamp[1].counterValues[0].counterValue == 1u); |
| 359 | CHECK(recievedTimestamp[1].counterValues[1].counterId == gpuCounters[1]); |
| 360 | CHECK(recievedTimestamp[1].counterValues[1].counterValue == 2u); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 361 | |
| 362 | sendCounterPacket.ClearTimestamps(); |
| 363 | |
| 364 | // Request no counters |
| 365 | periodicCounterSelectionCommandHandler(PacketWriter(period, {})); |
| 366 | periodicCounterCapture.Stop(); |
| 367 | |
| 368 | activeIds = holder.GetCaptureData().GetActiveBackends(); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 369 | CHECK(activeIds.size() == 0); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 370 | |
| 371 | recievedTimestamp = sendCounterPacket.GetTimestamps(); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 372 | CHECK(recievedTimestamp.size() == 0); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 373 | |
| 374 | sendCounterPacket.ClearTimestamps(); |
| 375 | |
| 376 | // Request period of zero |
| 377 | periodicCounterSelectionCommandHandler(PacketWriter(0, counterValues)); |
| 378 | periodicCounterCapture.Stop(); |
| 379 | |
| 380 | activeIds = holder.GetCaptureData().GetActiveBackends(); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 381 | CHECK(activeIds.size() == 0); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 382 | |
| 383 | recievedTimestamp = sendCounterPacket.GetTimestamps(); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 384 | CHECK(recievedTimestamp.size() == 0); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 387 | TEST_CASE("TestBackendCounterLogging") |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 388 | { |
| 389 | std::stringstream ss; |
| 390 | |
| 391 | struct StreamRedirector |
| 392 | { |
| 393 | public: |
| 394 | StreamRedirector(std::ostream &stream, std::streambuf *newStreamBuffer) |
| 395 | : m_Stream(stream), m_BackupBuffer(m_Stream.rdbuf(newStreamBuffer)) |
| 396 | {} |
| 397 | |
| 398 | ~StreamRedirector() |
| 399 | { m_Stream.rdbuf(m_BackupBuffer); } |
| 400 | |
| 401 | private: |
| 402 | std::ostream &m_Stream; |
| 403 | std::streambuf *m_BackupBuffer; |
| 404 | }; |
| 405 | |
| 406 | Holder holder; |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 407 | arm::pipe::PacketVersionResolver packetVersionResolver; |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 408 | ProfilingStateMachine stateMachine; |
| 409 | ReadCounterVals readCounterVals; |
| 410 | StreamRedirector redirect(std::cout, ss.rdbuf()); |
| 411 | CounterIdMap counterIdMap; |
| 412 | MockBackendSendCounterPacket sendCounterPacket; |
| 413 | |
Cathal Corbett | 6f07372 | 2022-03-04 12:11:09 +0000 | [diff] [blame] | 414 | const std::string cpuAccId(GetComputeDeviceAsCString(armnn::Compute::CpuAcc)); |
| 415 | const std::string gpuAccId(GetComputeDeviceAsCString(armnn::Compute::GpuAcc)); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 416 | |
Jim Flynn | 4c9ed1d | 2022-01-23 23:57:20 +0000 | [diff] [blame] | 417 | ProfilingOptions options; |
| 418 | options.m_EnableProfiling = true; |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 419 | |
Jim Flynn | 3443025 | 2022-03-04 15:03:58 +0000 | [diff] [blame] | 420 | armnn::ArmNNProfilingServiceInitialiser initialiser; |
| 421 | std::unique_ptr<IProfilingService> profilingService = arm::pipe::IProfilingService::CreateProfilingService( |
| 422 | arm::pipe::MAX_ARMNN_COUNTER, initialiser); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 423 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 424 | std::unique_ptr<IBackendProfiling> cpuBackendProfilingPtr = |
Jim Flynn | 3443025 | 2022-03-04 15:03:58 +0000 | [diff] [blame] | 425 | std::make_unique<BackendProfiling>(options, *profilingService.get(), cpuAccId); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 426 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 427 | std::shared_ptr<IBackendProfilingContext> cpuProfilingContextPtr = |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 428 | std::make_shared<armnn::MockBackendProfilingContext>(cpuBackendProfilingPtr); |
| 429 | |
Cathal Corbett | 6f07372 | 2022-03-04 12:11:09 +0000 | [diff] [blame] | 430 | std::unordered_map<std::string, |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 431 | std::shared_ptr<IBackendProfilingContext>> backendProfilingContexts; |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 432 | |
| 433 | uint16_t globalId = 5; |
| 434 | counterIdMap.RegisterMapping(globalId, 0, cpuAccId); |
| 435 | backendProfilingContexts[cpuAccId] = cpuProfilingContextPtr; |
| 436 | |
| 437 | PeriodicCounterCapture periodicCounterCapture(holder, sendCounterPacket, readCounterVals, |
| 438 | counterIdMap, backendProfilingContexts); |
| 439 | |
| 440 | uint16_t maxArmnnCounterId = 4; |
| 441 | |
| 442 | PeriodicCounterSelectionCommandHandler periodicCounterSelectionCommandHandler(0, |
| 443 | 4, |
| 444 | packetVersionResolver.ResolvePacketVersion(0, 4).GetEncodedValue(), |
| 445 | backendProfilingContexts, |
| 446 | counterIdMap, |
| 447 | holder, |
| 448 | maxArmnnCounterId, |
| 449 | periodicCounterCapture, |
| 450 | readCounterVals, |
| 451 | sendCounterPacket, |
| 452 | stateMachine); |
| 453 | |
| 454 | stateMachine.TransitionToState(ProfilingState::NotConnected); |
| 455 | stateMachine.TransitionToState(ProfilingState::WaitingForAck); |
| 456 | stateMachine.TransitionToState(ProfilingState::Active); |
| 457 | |
| 458 | uint32_t period = 15939u; |
| 459 | |
| 460 | armnn::SetAllLoggingSinks(true, false, false); |
| 461 | SetLogFilter(armnn::LogSeverity::Warning); |
| 462 | periodicCounterSelectionCommandHandler(PacketWriter(period, {5})); |
| 463 | periodicCounterCapture.Stop(); |
| 464 | SetLogFilter(armnn::LogSeverity::Fatal); |
| 465 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 466 | CHECK(ss.str().find("ActivateCounters example test error") != std::string::npos); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 469 | TEST_CASE("BackendProfilingContextGetSendTimelinePacket") |
Colm Donelan | fcb802b | 2020-02-13 20:47:08 +0000 | [diff] [blame] | 470 | { |
| 471 | // Reset the profiling service to the uninitialized state |
| 472 | armnn::IRuntime::CreationOptions options; |
| 473 | options.m_ProfilingOptions.m_EnableProfiling = true; |
Jim Flynn | 3443025 | 2022-03-04 15:03:58 +0000 | [diff] [blame] | 474 | |
| 475 | armnn::ArmNNProfilingServiceInitialiser psInitialiser; |
| 476 | std::unique_ptr<IProfilingService> profilingService = arm::pipe::IProfilingService::CreateProfilingService( |
| 477 | arm::pipe::MAX_ARMNN_COUNTER, psInitialiser); |
| 478 | |
| 479 | profilingService->ConfigureProfilingService( |
Jim Flynn | 4c9ed1d | 2022-01-23 23:57:20 +0000 | [diff] [blame] | 480 | ConvertExternalProfilingOptions(options.m_ProfilingOptions), true); |
Colm Donelan | fcb802b | 2020-02-13 20:47:08 +0000 | [diff] [blame] | 481 | |
| 482 | armnn::MockBackendInitialiser initialiser; |
| 483 | // Create a runtime. During this the mock backend will be registered and context returned. |
| 484 | armnn::IRuntimePtr runtime(armnn::IRuntime::Create(options)); |
| 485 | armnn::MockBackendProfilingService mockProfilingService = armnn::MockBackendProfilingService::Instance(); |
Jim Flynn | 3443025 | 2022-03-04 15:03:58 +0000 | [diff] [blame] | 486 | armnn::MockBackendProfilingContext* mockBackEndProfilingContext = mockProfilingService.GetContext(); |
Colm Donelan | fcb802b | 2020-02-13 20:47:08 +0000 | [diff] [blame] | 487 | // Check that there is a valid context set. |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 488 | CHECK(mockBackEndProfilingContext); |
Colm Donelan | fcb802b | 2020-02-13 20:47:08 +0000 | [diff] [blame] | 489 | armnn::IBackendInternal::IBackendProfilingPtr& backendProfilingIface = |
| 490 | mockBackEndProfilingContext->GetBackendProfiling(); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 491 | CHECK(backendProfilingIface); |
Colm Donelan | fcb802b | 2020-02-13 20:47:08 +0000 | [diff] [blame] | 492 | |
| 493 | // Now for the meat of the test. We're just going to send a random packet and make sure there |
| 494 | // are no exceptions or errors. The sending of packets is already tested in SendTimelinePacketTests. |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 495 | std::unique_ptr<ISendTimelinePacket> timelinePacket = |
Colm Donelan | fcb802b | 2020-02-13 20:47:08 +0000 | [diff] [blame] | 496 | backendProfilingIface->GetSendTimelinePacket(); |
| 497 | // Send TimelineEntityClassBinaryPacket |
| 498 | const uint64_t entityBinaryPacketProfilingGuid = 123456u; |
| 499 | timelinePacket->SendTimelineEntityBinaryPacket(entityBinaryPacketProfilingGuid); |
| 500 | timelinePacket->Commit(); |
| 501 | |
| 502 | // Reset the profiling servie after the test. |
| 503 | options.m_ProfilingOptions.m_EnableProfiling = false; |
Jim Flynn | 3443025 | 2022-03-04 15:03:58 +0000 | [diff] [blame] | 504 | profilingService->ResetExternalProfilingOptions( |
Jim Flynn | 4c9ed1d | 2022-01-23 23:57:20 +0000 | [diff] [blame] | 505 | ConvertExternalProfilingOptions(options.m_ProfilingOptions), true); |
Colm Donelan | fcb802b | 2020-02-13 20:47:08 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 508 | TEST_CASE("GetProfilingGuidGenerator") |
Colm Donelan | fcb802b | 2020-02-13 20:47:08 +0000 | [diff] [blame] | 509 | { |
| 510 | // Reset the profiling service to the uninitialized state |
| 511 | armnn::IRuntime::CreationOptions options; |
| 512 | options.m_ProfilingOptions.m_EnableProfiling = true; |
Colm Donelan | fcb802b | 2020-02-13 20:47:08 +0000 | [diff] [blame] | 513 | |
| 514 | armnn::MockBackendInitialiser initialiser; |
| 515 | // Create a runtime. During this the mock backend will be registered and context returned. |
| 516 | armnn::IRuntimePtr runtime(armnn::IRuntime::Create(options)); |
| 517 | armnn::MockBackendProfilingService mockProfilingService = armnn::MockBackendProfilingService::Instance(); |
| 518 | armnn::MockBackendProfilingContext *mockBackEndProfilingContext = mockProfilingService.GetContext(); |
| 519 | // Check that there is a valid context set. |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 520 | CHECK(mockBackEndProfilingContext); |
Colm Donelan | fcb802b | 2020-02-13 20:47:08 +0000 | [diff] [blame] | 521 | armnn::IBackendInternal::IBackendProfilingPtr& backendProfilingIface = |
| 522 | mockBackEndProfilingContext->GetBackendProfiling(); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 523 | CHECK(backendProfilingIface); |
Colm Donelan | fcb802b | 2020-02-13 20:47:08 +0000 | [diff] [blame] | 524 | |
| 525 | // Get the Guid generator and check the getting two Guid's results in the second being greater than the first. |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 526 | IProfilingGuidGenerator& guidGenerator = backendProfilingIface->GetProfilingGuidGenerator(); |
| 527 | const ProfilingDynamicGuid& firstGuid = guidGenerator.NextGuid(); |
| 528 | const ProfilingDynamicGuid& secondGuid = guidGenerator.NextGuid(); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 529 | CHECK(secondGuid > firstGuid); |
Colm Donelan | fcb802b | 2020-02-13 20:47:08 +0000 | [diff] [blame] | 530 | |
| 531 | // Reset the profiling servie after the test. |
| 532 | options.m_ProfilingOptions.m_EnableProfiling = false; |
Colm Donelan | fcb802b | 2020-02-13 20:47:08 +0000 | [diff] [blame] | 533 | } |
| 534 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 535 | } |