blob: f1ec46baa7640bfa44fefbf2c663ae9124f25709 [file] [log] [blame]
David Monahanc1536d62020-02-12 15:52:35 +00001//
Jim Flynnbbfe6032020-07-20 16:57:44 +01002// Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
David Monahanc1536d62020-02-12 15:52:35 +00003// SPDX-License-Identifier: MIT
4//
5
Jim Flynn34430252022-03-04 15:03:58 +00006#include "ArmNNProfilingServiceInitialiser.hpp"
Finn Williams032bc742020-02-12 11:02:34 +00007#include "CounterDirectory.hpp"
8#include "CounterIdMap.hpp"
9#include "Holder.hpp"
David Monahanc1536d62020-02-12 15:52:35 +000010#include "MockBackendId.hpp"
Finn Williams032bc742020-02-12 11:02:34 +000011#include "PeriodicCounterCapture.hpp"
12#include "PeriodicCounterSelectionCommandHandler.hpp"
Jim Flynn4c9ed1d2022-01-23 23:57:20 +000013#include "ProfilingOptionsConverter.hpp"
Finn Williams032bc742020-02-12 11:02:34 +000014#include "ProfilingStateMachine.hpp"
15#include "ProfilingUtils.hpp"
16#include "RequestCounterDirectoryCommandHandler.hpp"
David Monahanc1536d62020-02-12 15:52:35 +000017
Sadik Armagana097d2a2021-11-24 15:47:28 +000018#include <TestUtils.hpp>
Sadik Armagan3184c902020-03-18 10:57:30 +000019
Jan Eilers8eb25602020-03-09 12:13:48 +000020#include <armnn/utility/IgnoreUnused.hpp>
David Monahanc1536d62020-02-12 15:52:35 +000021#include <armnn/BackendId.hpp>
Finn Williams032bc742020-02-12 11:02:34 +000022#include <armnn/Logging.hpp>
Colm Donelanfcb802b2020-02-13 20:47:08 +000023#include <armnn/profiling/ISendTimelinePacket.hpp>
Jim Flynn4c9ed1d2022-01-23 23:57:20 +000024#include <armnn/profiling/ProfilingOptions.hpp>
Cathal Corbett3464ba12022-03-04 11:36:39 +000025#include <armnnTestUtils/MockBackend.hpp>
Finn Williams032bc742020-02-12 11:02:34 +000026
Sadik Armagan1625efc2021-06-10 18:24:34 +010027#include <doctest/doctest.h>
David Monahanc1536d62020-02-12 15:52:35 +000028#include <vector>
29
Finn Williams032bc742020-02-12 11:02:34 +000030#include <cstdint>
31#include <limits>
32#include <backends/BackendProfiling.hpp>
33
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000034using namespace arm::pipe;
Finn Williams032bc742020-02-12 11:02:34 +000035
36class ReadCounterVals : public IReadCounterValues
37{
38 virtual bool IsCounterRegistered(uint16_t counterUid) const override
39 {
40 return (counterUid > 4 && counterUid < 11);
41 }
Jim Flynn34430252022-03-04 15:03:58 +000042 virtual bool IsCounterRegistered(const std::string& counterName) const override
43 {
44 armnn::IgnoreUnused(counterName);
45 return false;
46 }
Finn Williams032bc742020-02-12 11:02:34 +000047 virtual uint16_t GetCounterCount() const override
48 {
49 return 1;
50 }
Finn Williamsf3fcf322020-05-11 14:38:02 +010051 virtual uint32_t GetAbsoluteCounterValue(uint16_t counterUid) const override
52 {
53 return counterUid;
54 }
55 virtual uint32_t GetDeltaCounterValue(uint16_t counterUid) override
Finn Williams032bc742020-02-12 11:02:34 +000056 {
57 return counterUid;
58 }
59};
60
61class MockBackendSendCounterPacket : public ISendCounterPacket
62{
63public:
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 Eilers8eb25602020-03-09 12:13:48 +000072 armnn::IgnoreUnused(counterDirectory);
Finn Williams032bc742020-02-12 11:02:34 +000073 }
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 Eilers8eb25602020-03-09 12:13:48 +000085 armnn::IgnoreUnused(capturePeriod);
86 armnn::IgnoreUnused(selectedCounterIds);
Finn Williams032bc742020-02-12 11:02:34 +000087 }
88
89 std::vector<Timestamp> GetTimestamps()
90 {
91 return m_timestamps;
92 }
93
94 void ClearTimestamps()
95 {
96 m_timestamps.clear();
97 }
98
99private:
100 std::vector<Timestamp> m_timestamps;
101};
102
Jim Flynnbbfe6032020-07-20 16:57:44 +0100103arm::pipe::Packet PacketWriter(uint32_t period, std::vector<uint16_t> countervalues)
Finn Williams032bc742020-02-12 11:02:34 +0000104{
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 Armagan1625efc2021-06-10 18:24:34 +0100122TEST_SUITE("BackendProfilingTestSuite")
123{
124TEST_CASE("BackendProfilingCounterRegisterMockBackendTest")
David Monahanc1536d62020-02-12 15:52:35 +0000125{
126 // Reset the profiling service to the uninitialized state
127 armnn::IRuntime::CreationOptions options;
Finn Williams45a73622020-05-15 18:41:05 +0100128 options.m_ProfilingOptions.m_EnableProfiling = true;
David Monahanc1536d62020-02-12 15:52:35 +0000129
130 armnn::MockBackendInitialiser initialiser;
131 // Create a runtime
Kevin Mayd92a6e42021-02-04 10:27:41 +0000132 armnn::RuntimeImpl runtime(options);
David Monahanc1536d62020-02-12 15:52:35 +0000133
Narumol Prangnawarat060bad52020-11-20 16:17:48 +0000134 unsigned int shiftedId = 0;
135
Nikhil Raj01bc02d2021-05-07 17:07:09 +0100136 if (armnn::BackendRegistry().IsBackendRegistered("EthosNAcc"))
137 {
138 shiftedId = 4;
139 }
Narumol Prangnawarat060bad52020-11-20 16:17:48 +0000140
David Monahanc1536d62020-02-12 15:52:35 +0000141 // Check if the MockBackends 3 dummy counters {0, 1, 2-5 (four cores)} are registered
142 armnn::BackendId mockId = armnn::MockBackendId();
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000143 const ICounterMappings& counterMap = GetProfilingService(&runtime).GetCounterMappings();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100144 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 Monahanc1536d62020-02-12 15:52:35 +0000150 options.m_ProfilingOptions.m_EnableProfiling = false;
Jim Flynn4c9ed1d2022-01-23 23:57:20 +0000151 GetProfilingService(&runtime).ResetExternalProfilingOptions(
152 ConvertExternalProfilingOptions(options.m_ProfilingOptions), true);
David Monahanc1536d62020-02-12 15:52:35 +0000153}
154
Sadik Armagan1625efc2021-06-10 18:24:34 +0100155TEST_CASE("TestBackendCounters")
Finn Williams032bc742020-02-12 11:02:34 +0000156{
157 Holder holder;
Jim Flynnbbfe6032020-07-20 16:57:44 +0100158 arm::pipe::PacketVersionResolver packetVersionResolver;
Finn Williams032bc742020-02-12 11:02:34 +0000159 ProfilingStateMachine stateMachine;
160 ReadCounterVals readCounterVals;
161 CounterIdMap counterIdMap;
162 MockBackendSendCounterPacket sendCounterPacket;
163
Cathal Corbett6f073722022-03-04 12:11:09 +0000164 const std::string cpuAccId(GetComputeDeviceAsCString(armnn::Compute::CpuAcc));
165 const std::string gpuAccId(GetComputeDeviceAsCString(armnn::Compute::GpuAcc));
Finn Williams032bc742020-02-12 11:02:34 +0000166
Jim Flynn4c9ed1d2022-01-23 23:57:20 +0000167 ProfilingOptions options;
168 options.m_EnableProfiling = true;
Finn Williams032bc742020-02-12 11:02:34 +0000169
Jim Flynn34430252022-03-04 15:03:58 +0000170 armnn::ArmNNProfilingServiceInitialiser initialiser;
171 std::unique_ptr<IProfilingService> profilingService = arm::pipe::IProfilingService::CreateProfilingService(
172 arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Finn Williams032bc742020-02-12 11:02:34 +0000173
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000174 std::unique_ptr<IBackendProfiling> cpuBackendProfilingPtr =
Jim Flynn34430252022-03-04 15:03:58 +0000175 std::make_unique<BackendProfiling>(options, *profilingService.get(), cpuAccId);
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000176 std::unique_ptr<IBackendProfiling> gpuBackendProfilingPtr =
Jim Flynn34430252022-03-04 15:03:58 +0000177 std::make_unique<BackendProfiling>(options, *profilingService.get(), gpuAccId);
Finn Williams032bc742020-02-12 11:02:34 +0000178
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000179 std::shared_ptr<IBackendProfilingContext> cpuProfilingContextPtr =
Finn Williams032bc742020-02-12 11:02:34 +0000180 std::make_shared<armnn::MockBackendProfilingContext>(cpuBackendProfilingPtr);
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000181 std::shared_ptr<IBackendProfilingContext> gpuProfilingContextPtr =
Finn Williams032bc742020-02-12 11:02:34 +0000182 std::make_shared<armnn::MockBackendProfilingContext>(gpuBackendProfilingPtr);
183
Cathal Corbett6f073722022-03-04 12:11:09 +0000184 std::unordered_map<std::string,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000185 std::shared_ptr<IBackendProfilingContext>> backendProfilingContexts;
Finn Williams032bc742020-02-12 11:02:34 +0000186
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 Corbett6f073722022-03-04 12:11:09 +0000233 std::set<std::string> activeIds = holder.GetCaptureData().GetActiveBackends();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100234 CHECK(activeIds.size() == 1);
235 CHECK((activeIds.find(gpuAccId) != activeIds.end()));
Finn Williams032bc742020-02-12 11:02:34 +0000236
237 std::vector<Timestamp> recievedTimestamp = sendCounterPacket.GetTimestamps();
238
Sadik Armagan1625efc2021-06-10 18:24:34 +0100239 CHECK(recievedTimestamp[0].timestamp == period);
240 CHECK(recievedTimestamp.size() == 1);
241 CHECK(recievedTimestamp[0].counterValues.size() == gpuCounters.size());
Finn Williams032bc742020-02-12 11:02:34 +0000242 for (unsigned long i=0; i< gpuCounters.size(); ++i)
243 {
Sadik Armagan1625efc2021-06-10 18:24:34 +0100244 CHECK(recievedTimestamp[0].counterValues[i].counterId == gpuCounters[i]);
245 CHECK(recievedTimestamp[0].counterValues[i].counterValue == i + 1u);
Finn Williams032bc742020-02-12 11:02:34 +0000246 }
247 sendCounterPacket.ClearTimestamps();
248
249 // Request only cpu counters
250 periodicCounterSelectionCommandHandler(PacketWriter(period, cpuCounters));
251 periodicCounterCapture.Stop();
252
253 activeIds = holder.GetCaptureData().GetActiveBackends();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100254 CHECK(activeIds.size() == 1);
255 CHECK((activeIds.find(cpuAccId) != activeIds.end()));
Finn Williams032bc742020-02-12 11:02:34 +0000256
257 recievedTimestamp = sendCounterPacket.GetTimestamps();
258
Sadik Armagan1625efc2021-06-10 18:24:34 +0100259 CHECK(recievedTimestamp[0].timestamp == period);
260 CHECK(recievedTimestamp.size() == 1);
261 CHECK(recievedTimestamp[0].counterValues.size() == cpuCounters.size());
Finn Williams032bc742020-02-12 11:02:34 +0000262 for (unsigned long i=0; i< cpuCounters.size(); ++i)
263 {
Sadik Armagan1625efc2021-06-10 18:24:34 +0100264 CHECK(recievedTimestamp[0].counterValues[i].counterId == cpuCounters[i]);
265 CHECK(recievedTimestamp[0].counterValues[i].counterValue == i + 1u);
Finn Williams032bc742020-02-12 11:02:34 +0000266 }
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 Armagan1625efc2021-06-10 18:24:34 +0100276 CHECK(activeIds.size() == 2);
277 CHECK((activeIds.find(cpuAccId) != activeIds.end()));
278 CHECK((activeIds.find(gpuAccId) != activeIds.end()));
Finn Williams032bc742020-02-12 11:02:34 +0000279
280 recievedTimestamp = sendCounterPacket.GetTimestamps();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100281//
282 CHECK(recievedTimestamp[0].timestamp == period);
283 CHECK(recievedTimestamp[1].timestamp == period);
Finn Williams032bc742020-02-12 11:02:34 +0000284
Sadik Armagan1625efc2021-06-10 18:24:34 +0100285 CHECK(recievedTimestamp.size() == 2);
286 CHECK(recievedTimestamp[0].counterValues.size() == 2);
287 CHECK(recievedTimestamp[1].counterValues.size() == gpuCounters.size());
Finn Williams032bc742020-02-12 11:02:34 +0000288
Sadik Armagan1625efc2021-06-10 18:24:34 +0100289 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 Williams032bc742020-02-12 11:02:34 +0000293
294 for (unsigned long i=0; i< gpuCounters.size(); ++i)
295 {
Sadik Armagan1625efc2021-06-10 18:24:34 +0100296 CHECK(recievedTimestamp[1].counterValues[i].counterId == gpuCounters[i]);
297 CHECK(recievedTimestamp[1].counterValues[i].counterValue == i + 1u);
Finn Williams032bc742020-02-12 11:02:34 +0000298 }
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 Armagan1625efc2021-06-10 18:24:34 +0100311 CHECK(activeIds.size() == 2);
312 CHECK((activeIds.find(cpuAccId) != activeIds.end()));
313 CHECK((activeIds.find(gpuAccId) != activeIds.end()));
Finn Williams032bc742020-02-12 11:02:34 +0000314
315 recievedTimestamp = sendCounterPacket.GetTimestamps();
316
Sadik Armagan1625efc2021-06-10 18:24:34 +0100317 CHECK(recievedTimestamp[0].counterValues.size() == cpuCounters.size());
Finn Williams032bc742020-02-12 11:02:34 +0000318 for (unsigned long i=0; i< cpuCounters.size(); ++i)
319 {
Sadik Armagan1625efc2021-06-10 18:24:34 +0100320 CHECK(recievedTimestamp[0].counterValues[i].counterId == cpuCounters[i]);
321 CHECK(recievedTimestamp[0].counterValues[i].counterValue == i + 1u);
Finn Williams032bc742020-02-12 11:02:34 +0000322 }
323
Sadik Armagan1625efc2021-06-10 18:24:34 +0100324 CHECK(recievedTimestamp[1].counterValues.size() == gpuCounters.size());
Finn Williams032bc742020-02-12 11:02:34 +0000325 for (unsigned long i=0; i< gpuCounters.size(); ++i)
326 {
Sadik Armagan1625efc2021-06-10 18:24:34 +0100327 CHECK(recievedTimestamp[1].counterValues[i].counterId == gpuCounters[i]);
328 CHECK(recievedTimestamp[1].counterValues[i].counterValue == i + 1u);
Finn Williams032bc742020-02-12 11:02:34 +0000329 }
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 Armagan1625efc2021-06-10 18:24:34 +0100340 CHECK(activeIds.size() == 2);
341 CHECK((activeIds.find(cpuAccId) != activeIds.end()));
342 CHECK((activeIds.find(gpuAccId) != activeIds.end()));
Finn Williams032bc742020-02-12 11:02:34 +0000343
344 recievedTimestamp = sendCounterPacket.GetTimestamps();
345
Sadik Armagan1625efc2021-06-10 18:24:34 +0100346 CHECK(recievedTimestamp.size() == 2);
Finn Williams032bc742020-02-12 11:02:34 +0000347
Sadik Armagan1625efc2021-06-10 18:24:34 +0100348 CHECK(recievedTimestamp[0].counterValues.size() == 2);
Finn Williams032bc742020-02-12 11:02:34 +0000349
Sadik Armagan1625efc2021-06-10 18:24:34 +0100350 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 Williams032bc742020-02-12 11:02:34 +0000354
Sadik Armagan1625efc2021-06-10 18:24:34 +0100355 CHECK(recievedTimestamp[1].counterValues.size() == 2);
Finn Williams032bc742020-02-12 11:02:34 +0000356
Sadik Armagan1625efc2021-06-10 18:24:34 +0100357 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 Williams032bc742020-02-12 11:02:34 +0000361
362 sendCounterPacket.ClearTimestamps();
363
364 // Request no counters
365 periodicCounterSelectionCommandHandler(PacketWriter(period, {}));
366 periodicCounterCapture.Stop();
367
368 activeIds = holder.GetCaptureData().GetActiveBackends();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100369 CHECK(activeIds.size() == 0);
Finn Williams032bc742020-02-12 11:02:34 +0000370
371 recievedTimestamp = sendCounterPacket.GetTimestamps();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100372 CHECK(recievedTimestamp.size() == 0);
Finn Williams032bc742020-02-12 11:02:34 +0000373
374 sendCounterPacket.ClearTimestamps();
375
376 // Request period of zero
377 periodicCounterSelectionCommandHandler(PacketWriter(0, counterValues));
378 periodicCounterCapture.Stop();
379
380 activeIds = holder.GetCaptureData().GetActiveBackends();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100381 CHECK(activeIds.size() == 0);
Finn Williams032bc742020-02-12 11:02:34 +0000382
383 recievedTimestamp = sendCounterPacket.GetTimestamps();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100384 CHECK(recievedTimestamp.size() == 0);
Finn Williams032bc742020-02-12 11:02:34 +0000385}
386
Sadik Armagan1625efc2021-06-10 18:24:34 +0100387TEST_CASE("TestBackendCounterLogging")
Finn Williams032bc742020-02-12 11:02:34 +0000388{
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 Flynnbbfe6032020-07-20 16:57:44 +0100407 arm::pipe::PacketVersionResolver packetVersionResolver;
Finn Williams032bc742020-02-12 11:02:34 +0000408 ProfilingStateMachine stateMachine;
409 ReadCounterVals readCounterVals;
410 StreamRedirector redirect(std::cout, ss.rdbuf());
411 CounterIdMap counterIdMap;
412 MockBackendSendCounterPacket sendCounterPacket;
413
Cathal Corbett6f073722022-03-04 12:11:09 +0000414 const std::string cpuAccId(GetComputeDeviceAsCString(armnn::Compute::CpuAcc));
415 const std::string gpuAccId(GetComputeDeviceAsCString(armnn::Compute::GpuAcc));
Finn Williams032bc742020-02-12 11:02:34 +0000416
Jim Flynn4c9ed1d2022-01-23 23:57:20 +0000417 ProfilingOptions options;
418 options.m_EnableProfiling = true;
Finn Williams032bc742020-02-12 11:02:34 +0000419
Jim Flynn34430252022-03-04 15:03:58 +0000420 armnn::ArmNNProfilingServiceInitialiser initialiser;
421 std::unique_ptr<IProfilingService> profilingService = arm::pipe::IProfilingService::CreateProfilingService(
422 arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Finn Williams032bc742020-02-12 11:02:34 +0000423
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000424 std::unique_ptr<IBackendProfiling> cpuBackendProfilingPtr =
Jim Flynn34430252022-03-04 15:03:58 +0000425 std::make_unique<BackendProfiling>(options, *profilingService.get(), cpuAccId);
Finn Williams032bc742020-02-12 11:02:34 +0000426
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000427 std::shared_ptr<IBackendProfilingContext> cpuProfilingContextPtr =
Finn Williams032bc742020-02-12 11:02:34 +0000428 std::make_shared<armnn::MockBackendProfilingContext>(cpuBackendProfilingPtr);
429
Cathal Corbett6f073722022-03-04 12:11:09 +0000430 std::unordered_map<std::string,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000431 std::shared_ptr<IBackendProfilingContext>> backendProfilingContexts;
Finn Williams032bc742020-02-12 11:02:34 +0000432
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 Armagan1625efc2021-06-10 18:24:34 +0100466 CHECK(ss.str().find("ActivateCounters example test error") != std::string::npos);
Finn Williams032bc742020-02-12 11:02:34 +0000467}
468
Sadik Armagan1625efc2021-06-10 18:24:34 +0100469TEST_CASE("BackendProfilingContextGetSendTimelinePacket")
Colm Donelanfcb802b2020-02-13 20:47:08 +0000470{
471 // Reset the profiling service to the uninitialized state
472 armnn::IRuntime::CreationOptions options;
473 options.m_ProfilingOptions.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +0000474
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 Flynn4c9ed1d2022-01-23 23:57:20 +0000480 ConvertExternalProfilingOptions(options.m_ProfilingOptions), true);
Colm Donelanfcb802b2020-02-13 20:47:08 +0000481
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 Flynn34430252022-03-04 15:03:58 +0000486 armnn::MockBackendProfilingContext* mockBackEndProfilingContext = mockProfilingService.GetContext();
Colm Donelanfcb802b2020-02-13 20:47:08 +0000487 // Check that there is a valid context set.
Sadik Armagan1625efc2021-06-10 18:24:34 +0100488 CHECK(mockBackEndProfilingContext);
Colm Donelanfcb802b2020-02-13 20:47:08 +0000489 armnn::IBackendInternal::IBackendProfilingPtr& backendProfilingIface =
490 mockBackEndProfilingContext->GetBackendProfiling();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100491 CHECK(backendProfilingIface);
Colm Donelanfcb802b2020-02-13 20:47:08 +0000492
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 Corbett5aa9fd72022-02-25 15:33:28 +0000495 std::unique_ptr<ISendTimelinePacket> timelinePacket =
Colm Donelanfcb802b2020-02-13 20:47:08 +0000496 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 Flynn34430252022-03-04 15:03:58 +0000504 profilingService->ResetExternalProfilingOptions(
Jim Flynn4c9ed1d2022-01-23 23:57:20 +0000505 ConvertExternalProfilingOptions(options.m_ProfilingOptions), true);
Colm Donelanfcb802b2020-02-13 20:47:08 +0000506}
507
Sadik Armagan1625efc2021-06-10 18:24:34 +0100508TEST_CASE("GetProfilingGuidGenerator")
Colm Donelanfcb802b2020-02-13 20:47:08 +0000509{
510 // Reset the profiling service to the uninitialized state
511 armnn::IRuntime::CreationOptions options;
512 options.m_ProfilingOptions.m_EnableProfiling = true;
Colm Donelanfcb802b2020-02-13 20:47:08 +0000513
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 Armagan1625efc2021-06-10 18:24:34 +0100520 CHECK(mockBackEndProfilingContext);
Colm Donelanfcb802b2020-02-13 20:47:08 +0000521 armnn::IBackendInternal::IBackendProfilingPtr& backendProfilingIface =
522 mockBackEndProfilingContext->GetBackendProfiling();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100523 CHECK(backendProfilingIface);
Colm Donelanfcb802b2020-02-13 20:47:08 +0000524
525 // Get the Guid generator and check the getting two Guid's results in the second being greater than the first.
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000526 IProfilingGuidGenerator& guidGenerator = backendProfilingIface->GetProfilingGuidGenerator();
527 const ProfilingDynamicGuid& firstGuid = guidGenerator.NextGuid();
528 const ProfilingDynamicGuid& secondGuid = guidGenerator.NextGuid();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100529 CHECK(secondGuid > firstGuid);
Colm Donelanfcb802b2020-02-13 20:47:08 +0000530
531 // Reset the profiling servie after the test.
532 options.m_ProfilingOptions.m_EnableProfiling = false;
Colm Donelanfcb802b2020-02-13 20:47:08 +0000533}
534
Sadik Armagan1625efc2021-06-10 18:24:34 +0100535}