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