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