blob: 9b80a8fa7294e0d418927977d7747f954d3c7ea3 [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 "CounterIdMap.hpp"
8#include "Holder.hpp"
David Monahanc1536d62020-02-12 15:52:35 +00009#include "MockBackendId.hpp"
Finn Williams032bc742020-02-12 11:02:34 +000010#include "PeriodicCounterCapture.hpp"
11#include "PeriodicCounterSelectionCommandHandler.hpp"
Jim Flynn4c9ed1d2022-01-23 23:57:20 +000012#include "ProfilingOptionsConverter.hpp"
Finn Williams032bc742020-02-12 11:02:34 +000013#include "ProfilingStateMachine.hpp"
14#include "ProfilingUtils.hpp"
15#include "RequestCounterDirectoryCommandHandler.hpp"
David Monahanc1536d62020-02-12 15:52:35 +000016
Sadik Armagana097d2a2021-11-24 15:47:28 +000017#include <TestUtils.hpp>
Sadik Armagan3184c902020-03-18 10:57:30 +000018
David Monahanc1536d62020-02-12 15:52:35 +000019#include <armnn/BackendId.hpp>
Finn Williams032bc742020-02-12 11:02:34 +000020#include <armnn/Logging.hpp>
Jim Flynnc454ac92022-03-16 18:43:18 +000021
22#include <armnn/profiling/ArmNNProfiling.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>
Jim Flynnc454ac92022-03-16 18:43:18 +000025
26#include <armnn/utility/IgnoreUnused.hpp>
27
Cathal Corbett3464ba12022-03-04 11:36:39 +000028#include <armnnTestUtils/MockBackend.hpp>
Finn Williams032bc742020-02-12 11:02:34 +000029
Finn Williams032bc742020-02-12 11:02:34 +000030#include <backends/BackendProfiling.hpp>
31
Jim Flynnc454ac92022-03-16 18:43:18 +000032#include <common/include/CounterDirectory.hpp>
33
34#include <doctest/doctest.h>
35
36#include <vector>
37#include <cstdint>
38#include <limits>
Jim Flynn6c9f17d2022-03-10 23:13:01 +000039
40namespace arm
41{
42
43namespace pipe
44{
45
46struct LogLevelSwapper
47{
48public:
49 LogLevelSwapper(arm::pipe::LogSeverity severity)
50 {
51 // Set the new log level
52 arm::pipe::ConfigureLogging(true, true, severity);
53 }
54 ~LogLevelSwapper()
55 {
56 // The default log level for unit tests is "Fatal"
57 arm::pipe::ConfigureLogging(true, true, arm::pipe::LogSeverity::Fatal);
58 }
59};
60
61} // namespace pipe
62
63} // namespace arm
64
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000065using namespace arm::pipe;
Finn Williams032bc742020-02-12 11:02:34 +000066
67class ReadCounterVals : public IReadCounterValues
68{
69 virtual bool IsCounterRegistered(uint16_t counterUid) const override
70 {
71 return (counterUid > 4 && counterUid < 11);
72 }
Jim Flynn34430252022-03-04 15:03:58 +000073 virtual bool IsCounterRegistered(const std::string& counterName) const override
74 {
75 armnn::IgnoreUnused(counterName);
76 return false;
77 }
Finn Williams032bc742020-02-12 11:02:34 +000078 virtual uint16_t GetCounterCount() const override
79 {
80 return 1;
81 }
Finn Williamsf3fcf322020-05-11 14:38:02 +010082 virtual uint32_t GetAbsoluteCounterValue(uint16_t counterUid) const override
83 {
84 return counterUid;
85 }
86 virtual uint32_t GetDeltaCounterValue(uint16_t counterUid) override
Finn Williams032bc742020-02-12 11:02:34 +000087 {
88 return counterUid;
89 }
90};
91
92class MockBackendSendCounterPacket : public ISendCounterPacket
93{
94public:
95 using IndexValuePairsVector = std::vector<CounterValue>;
96
97 /// Create and write a StreamMetaDataPacket in the buffer
98 virtual void SendStreamMetaDataPacket() {}
99
100 /// Create and write a CounterDirectoryPacket from the parameters to the buffer.
101 virtual void SendCounterDirectoryPacket(const ICounterDirectory& counterDirectory)
102 {
Jan Eilers8eb25602020-03-09 12:13:48 +0000103 armnn::IgnoreUnused(counterDirectory);
Finn Williams032bc742020-02-12 11:02:34 +0000104 }
105
106 /// Create and write a PeriodicCounterCapturePacket from the parameters to the buffer.
107 virtual void SendPeriodicCounterCapturePacket(uint64_t timestamp, const IndexValuePairsVector& values)
108 {
109 m_timestamps.emplace_back(Timestamp{timestamp, values});
110 }
111
112 /// Create and write a PeriodicCounterSelectionPacket from the parameters to the buffer.
113 virtual void SendPeriodicCounterSelectionPacket(uint32_t capturePeriod,
114 const std::vector<uint16_t>& selectedCounterIds)
115 {
Jan Eilers8eb25602020-03-09 12:13:48 +0000116 armnn::IgnoreUnused(capturePeriod);
117 armnn::IgnoreUnused(selectedCounterIds);
Finn Williams032bc742020-02-12 11:02:34 +0000118 }
119
120 std::vector<Timestamp> GetTimestamps()
121 {
122 return m_timestamps;
123 }
124
125 void ClearTimestamps()
126 {
127 m_timestamps.clear();
128 }
129
130private:
131 std::vector<Timestamp> m_timestamps;
132};
133
Jim Flynnbbfe6032020-07-20 16:57:44 +0100134arm::pipe::Packet PacketWriter(uint32_t period, std::vector<uint16_t> countervalues)
Finn Williams032bc742020-02-12 11:02:34 +0000135{
136 const uint32_t packetId = 0x40000;
137 uint32_t offset = 0;
138 uint32_t dataLength = static_cast<uint32_t>(4 + countervalues.size() * 2);
139 std::unique_ptr<unsigned char[]> uniqueData = std::make_unique<unsigned char[]>(dataLength);
140 unsigned char* data1 = reinterpret_cast<unsigned char*>(uniqueData.get());
141
142 WriteUint32(data1, offset, period);
143 offset += 4;
144 for (auto countervalue : countervalues)
145 {
146 WriteUint16(data1, offset, countervalue);
147 offset += 2;
148 }
149
150 return {packetId, dataLength, uniqueData};
151}
152
Sadik Armagan1625efc2021-06-10 18:24:34 +0100153TEST_SUITE("BackendProfilingTestSuite")
154{
155TEST_CASE("BackendProfilingCounterRegisterMockBackendTest")
David Monahanc1536d62020-02-12 15:52:35 +0000156{
Jim Flynn6c9f17d2022-03-10 23:13:01 +0000157 arm::pipe::LogLevelSwapper logLevelSwapper(arm::pipe::LogSeverity::Fatal);
158
David Monahanc1536d62020-02-12 15:52:35 +0000159 // Reset the profiling service to the uninitialized state
160 armnn::IRuntime::CreationOptions options;
Finn Williams45a73622020-05-15 18:41:05 +0100161 options.m_ProfilingOptions.m_EnableProfiling = true;
David Monahanc1536d62020-02-12 15:52:35 +0000162
163 armnn::MockBackendInitialiser initialiser;
164 // Create a runtime
Kevin Mayd92a6e42021-02-04 10:27:41 +0000165 armnn::RuntimeImpl runtime(options);
David Monahanc1536d62020-02-12 15:52:35 +0000166
Narumol Prangnawarat060bad52020-11-20 16:17:48 +0000167 unsigned int shiftedId = 0;
168
Nikhil Raj01bc02d2021-05-07 17:07:09 +0100169 if (armnn::BackendRegistry().IsBackendRegistered("EthosNAcc"))
170 {
171 shiftedId = 4;
172 }
Narumol Prangnawarat060bad52020-11-20 16:17:48 +0000173
David Monahanc1536d62020-02-12 15:52:35 +0000174 // Check if the MockBackends 3 dummy counters {0, 1, 2-5 (four cores)} are registered
175 armnn::BackendId mockId = armnn::MockBackendId();
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000176 const ICounterMappings& counterMap = GetProfilingService(&runtime).GetCounterMappings();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100177 CHECK(counterMap.GetGlobalId(0, mockId) == 5 + shiftedId);
178 CHECK(counterMap.GetGlobalId(1, mockId) == 6 + shiftedId);
179 CHECK(counterMap.GetGlobalId(2, mockId) == 7 + shiftedId);
180 CHECK(counterMap.GetGlobalId(3, mockId) == 8 + shiftedId);
181 CHECK(counterMap.GetGlobalId(4, mockId) == 9 + shiftedId);
182 CHECK(counterMap.GetGlobalId(5, mockId) == 10 + shiftedId);
David Monahanc1536d62020-02-12 15:52:35 +0000183 options.m_ProfilingOptions.m_EnableProfiling = false;
Jim Flynn4c9ed1d2022-01-23 23:57:20 +0000184 GetProfilingService(&runtime).ResetExternalProfilingOptions(
185 ConvertExternalProfilingOptions(options.m_ProfilingOptions), true);
David Monahanc1536d62020-02-12 15:52:35 +0000186}
187
Sadik Armagan1625efc2021-06-10 18:24:34 +0100188TEST_CASE("TestBackendCounters")
Finn Williams032bc742020-02-12 11:02:34 +0000189{
Jim Flynn6c9f17d2022-03-10 23:13:01 +0000190 arm::pipe::LogLevelSwapper logLevelSwapper(arm::pipe::LogSeverity::Fatal);
191
Finn Williams032bc742020-02-12 11:02:34 +0000192 Holder holder;
Jim Flynnbbfe6032020-07-20 16:57:44 +0100193 arm::pipe::PacketVersionResolver packetVersionResolver;
Finn Williams032bc742020-02-12 11:02:34 +0000194 ProfilingStateMachine stateMachine;
195 ReadCounterVals readCounterVals;
196 CounterIdMap counterIdMap;
197 MockBackendSendCounterPacket sendCounterPacket;
198
Cathal Corbett6f073722022-03-04 12:11:09 +0000199 const std::string cpuAccId(GetComputeDeviceAsCString(armnn::Compute::CpuAcc));
200 const std::string gpuAccId(GetComputeDeviceAsCString(armnn::Compute::GpuAcc));
Finn Williams032bc742020-02-12 11:02:34 +0000201
Jim Flynn4c9ed1d2022-01-23 23:57:20 +0000202 ProfilingOptions options;
203 options.m_EnableProfiling = true;
Finn Williams032bc742020-02-12 11:02:34 +0000204
Jim Flynn34430252022-03-04 15:03:58 +0000205 armnn::ArmNNProfilingServiceInitialiser initialiser;
206 std::unique_ptr<IProfilingService> profilingService = arm::pipe::IProfilingService::CreateProfilingService(
Jim Flynn9c85b412022-03-16 00:27:43 +0000207 arm::pipe::MAX_ARMNN_COUNTER,
208 initialiser,
209 arm::pipe::ARMNN_SOFTWARE_INFO,
210 arm::pipe::ARMNN_SOFTWARE_VERSION,
211 arm::pipe::ARMNN_HARDWARE_VERSION);
Finn Williams032bc742020-02-12 11:02:34 +0000212
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000213 std::unique_ptr<IBackendProfiling> cpuBackendProfilingPtr =
Jim Flynn34430252022-03-04 15:03:58 +0000214 std::make_unique<BackendProfiling>(options, *profilingService.get(), cpuAccId);
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000215 std::unique_ptr<IBackendProfiling> gpuBackendProfilingPtr =
Jim Flynn34430252022-03-04 15:03:58 +0000216 std::make_unique<BackendProfiling>(options, *profilingService.get(), gpuAccId);
Finn Williams032bc742020-02-12 11:02:34 +0000217
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000218 std::shared_ptr<IBackendProfilingContext> cpuProfilingContextPtr =
Finn Williams032bc742020-02-12 11:02:34 +0000219 std::make_shared<armnn::MockBackendProfilingContext>(cpuBackendProfilingPtr);
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000220 std::shared_ptr<IBackendProfilingContext> gpuProfilingContextPtr =
Finn Williams032bc742020-02-12 11:02:34 +0000221 std::make_shared<armnn::MockBackendProfilingContext>(gpuBackendProfilingPtr);
222
Cathal Corbett6f073722022-03-04 12:11:09 +0000223 std::unordered_map<std::string,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000224 std::shared_ptr<IBackendProfilingContext>> backendProfilingContexts;
Finn Williams032bc742020-02-12 11:02:34 +0000225
226 backendProfilingContexts[cpuAccId] = cpuProfilingContextPtr;
227 backendProfilingContexts[gpuAccId] = gpuProfilingContextPtr;
228
229 uint16_t globalId = 5;
230
231 counterIdMap.RegisterMapping(globalId++, 0, cpuAccId);
232 counterIdMap.RegisterMapping(globalId++, 1, cpuAccId);
233 counterIdMap.RegisterMapping(globalId++, 2, cpuAccId);
234
235 counterIdMap.RegisterMapping(globalId++, 0, gpuAccId);
236 counterIdMap.RegisterMapping(globalId++, 1, gpuAccId);
237 counterIdMap.RegisterMapping(globalId++, 2, gpuAccId);
238
239 backendProfilingContexts[cpuAccId] = cpuProfilingContextPtr;
240 backendProfilingContexts[gpuAccId] = gpuProfilingContextPtr;
241
242 PeriodicCounterCapture periodicCounterCapture(holder, sendCounterPacket, readCounterVals,
243 counterIdMap, backendProfilingContexts);
244
245 uint16_t maxArmnnCounterId = 4;
246
247 PeriodicCounterSelectionCommandHandler periodicCounterSelectionCommandHandler(0,
248 4,
249 packetVersionResolver.ResolvePacketVersion(0, 4).GetEncodedValue(),
250 backendProfilingContexts,
251 counterIdMap,
252 holder,
253 maxArmnnCounterId,
254 periodicCounterCapture,
255 readCounterVals,
256 sendCounterPacket,
257 stateMachine);
258
259 stateMachine.TransitionToState(ProfilingState::NotConnected);
260 stateMachine.TransitionToState(ProfilingState::WaitingForAck);
261 stateMachine.TransitionToState(ProfilingState::Active);
262
263 uint32_t period = 12345u;
264
265 std::vector<uint16_t> cpuCounters{5, 6, 7};
266 std::vector<uint16_t> gpuCounters{8, 9, 10};
267
268 // Request only gpu counters
269 periodicCounterSelectionCommandHandler(PacketWriter(period, gpuCounters));
270 periodicCounterCapture.Stop();
271
Cathal Corbett6f073722022-03-04 12:11:09 +0000272 std::set<std::string> activeIds = holder.GetCaptureData().GetActiveBackends();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100273 CHECK(activeIds.size() == 1);
274 CHECK((activeIds.find(gpuAccId) != activeIds.end()));
Finn Williams032bc742020-02-12 11:02:34 +0000275
276 std::vector<Timestamp> recievedTimestamp = sendCounterPacket.GetTimestamps();
277
Sadik Armagan1625efc2021-06-10 18:24:34 +0100278 CHECK(recievedTimestamp[0].timestamp == period);
279 CHECK(recievedTimestamp.size() == 1);
280 CHECK(recievedTimestamp[0].counterValues.size() == gpuCounters.size());
Finn Williams032bc742020-02-12 11:02:34 +0000281 for (unsigned long i=0; i< gpuCounters.size(); ++i)
282 {
Sadik Armagan1625efc2021-06-10 18:24:34 +0100283 CHECK(recievedTimestamp[0].counterValues[i].counterId == gpuCounters[i]);
284 CHECK(recievedTimestamp[0].counterValues[i].counterValue == i + 1u);
Finn Williams032bc742020-02-12 11:02:34 +0000285 }
286 sendCounterPacket.ClearTimestamps();
287
288 // Request only cpu counters
289 periodicCounterSelectionCommandHandler(PacketWriter(period, cpuCounters));
290 periodicCounterCapture.Stop();
291
292 activeIds = holder.GetCaptureData().GetActiveBackends();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100293 CHECK(activeIds.size() == 1);
294 CHECK((activeIds.find(cpuAccId) != activeIds.end()));
Finn Williams032bc742020-02-12 11:02:34 +0000295
296 recievedTimestamp = sendCounterPacket.GetTimestamps();
297
Sadik Armagan1625efc2021-06-10 18:24:34 +0100298 CHECK(recievedTimestamp[0].timestamp == period);
299 CHECK(recievedTimestamp.size() == 1);
300 CHECK(recievedTimestamp[0].counterValues.size() == cpuCounters.size());
Finn Williams032bc742020-02-12 11:02:34 +0000301 for (unsigned long i=0; i< cpuCounters.size(); ++i)
302 {
Sadik Armagan1625efc2021-06-10 18:24:34 +0100303 CHECK(recievedTimestamp[0].counterValues[i].counterId == cpuCounters[i]);
304 CHECK(recievedTimestamp[0].counterValues[i].counterValue == i + 1u);
Finn Williams032bc742020-02-12 11:02:34 +0000305 }
306 sendCounterPacket.ClearTimestamps();
307
308 // Request combination of cpu & gpu counters with new period
309 period = 12222u;
310 periodicCounterSelectionCommandHandler(PacketWriter(period, {cpuCounters[0], gpuCounters[2],
311 gpuCounters[1], cpuCounters[1], gpuCounters[0]}));
312 periodicCounterCapture.Stop();
313
314 activeIds = holder.GetCaptureData().GetActiveBackends();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100315 CHECK(activeIds.size() == 2);
316 CHECK((activeIds.find(cpuAccId) != activeIds.end()));
317 CHECK((activeIds.find(gpuAccId) != activeIds.end()));
Finn Williams032bc742020-02-12 11:02:34 +0000318
319 recievedTimestamp = sendCounterPacket.GetTimestamps();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100320//
321 CHECK(recievedTimestamp[0].timestamp == period);
322 CHECK(recievedTimestamp[1].timestamp == period);
Finn Williams032bc742020-02-12 11:02:34 +0000323
Sadik Armagan1625efc2021-06-10 18:24:34 +0100324 CHECK(recievedTimestamp.size() == 2);
325 CHECK(recievedTimestamp[0].counterValues.size() == 2);
326 CHECK(recievedTimestamp[1].counterValues.size() == gpuCounters.size());
Finn Williams032bc742020-02-12 11:02:34 +0000327
Sadik Armagan1625efc2021-06-10 18:24:34 +0100328 CHECK(recievedTimestamp[0].counterValues[0].counterId == cpuCounters[0]);
329 CHECK(recievedTimestamp[0].counterValues[0].counterValue == 1u);
330 CHECK(recievedTimestamp[0].counterValues[1].counterId == cpuCounters[1]);
331 CHECK(recievedTimestamp[0].counterValues[1].counterValue == 2u);
Finn Williams032bc742020-02-12 11:02:34 +0000332
333 for (unsigned long i=0; i< gpuCounters.size(); ++i)
334 {
Sadik Armagan1625efc2021-06-10 18:24:34 +0100335 CHECK(recievedTimestamp[1].counterValues[i].counterId == gpuCounters[i]);
336 CHECK(recievedTimestamp[1].counterValues[i].counterValue == i + 1u);
Finn Williams032bc742020-02-12 11:02:34 +0000337 }
338
339 sendCounterPacket.ClearTimestamps();
340
341 // Request all counters
342 std::vector<uint16_t> counterValues;
343 counterValues.insert(counterValues.begin(), cpuCounters.begin(), cpuCounters.end());
344 counterValues.insert(counterValues.begin(), gpuCounters.begin(), gpuCounters.end());
345
346 periodicCounterSelectionCommandHandler(PacketWriter(period, counterValues));
347 periodicCounterCapture.Stop();
348
349 activeIds = holder.GetCaptureData().GetActiveBackends();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100350 CHECK(activeIds.size() == 2);
351 CHECK((activeIds.find(cpuAccId) != activeIds.end()));
352 CHECK((activeIds.find(gpuAccId) != activeIds.end()));
Finn Williams032bc742020-02-12 11:02:34 +0000353
354 recievedTimestamp = sendCounterPacket.GetTimestamps();
355
Sadik Armagan1625efc2021-06-10 18:24:34 +0100356 CHECK(recievedTimestamp[0].counterValues.size() == cpuCounters.size());
Finn Williams032bc742020-02-12 11:02:34 +0000357 for (unsigned long i=0; i< cpuCounters.size(); ++i)
358 {
Sadik Armagan1625efc2021-06-10 18:24:34 +0100359 CHECK(recievedTimestamp[0].counterValues[i].counterId == cpuCounters[i]);
360 CHECK(recievedTimestamp[0].counterValues[i].counterValue == i + 1u);
Finn Williams032bc742020-02-12 11:02:34 +0000361 }
362
Sadik Armagan1625efc2021-06-10 18:24:34 +0100363 CHECK(recievedTimestamp[1].counterValues.size() == gpuCounters.size());
Finn Williams032bc742020-02-12 11:02:34 +0000364 for (unsigned long i=0; i< gpuCounters.size(); ++i)
365 {
Sadik Armagan1625efc2021-06-10 18:24:34 +0100366 CHECK(recievedTimestamp[1].counterValues[i].counterId == gpuCounters[i]);
367 CHECK(recievedTimestamp[1].counterValues[i].counterValue == i + 1u);
Finn Williams032bc742020-02-12 11:02:34 +0000368 }
369 sendCounterPacket.ClearTimestamps();
370
371 // Request random counters with duplicates and invalid counters
372 counterValues = {0, 0, 200, cpuCounters[2], gpuCounters[0],3 ,30, cpuCounters[0],cpuCounters[2], gpuCounters[1], 3,
373 90, 0, 30, gpuCounters[0], gpuCounters[0]};
374
375 periodicCounterSelectionCommandHandler(PacketWriter(period, counterValues));
376 periodicCounterCapture.Stop();
377
378 activeIds = holder.GetCaptureData().GetActiveBackends();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100379 CHECK(activeIds.size() == 2);
380 CHECK((activeIds.find(cpuAccId) != activeIds.end()));
381 CHECK((activeIds.find(gpuAccId) != activeIds.end()));
Finn Williams032bc742020-02-12 11:02:34 +0000382
383 recievedTimestamp = sendCounterPacket.GetTimestamps();
384
Sadik Armagan1625efc2021-06-10 18:24:34 +0100385 CHECK(recievedTimestamp.size() == 2);
Finn Williams032bc742020-02-12 11:02:34 +0000386
Sadik Armagan1625efc2021-06-10 18:24:34 +0100387 CHECK(recievedTimestamp[0].counterValues.size() == 2);
Finn Williams032bc742020-02-12 11:02:34 +0000388
Sadik Armagan1625efc2021-06-10 18:24:34 +0100389 CHECK(recievedTimestamp[0].counterValues[0].counterId == cpuCounters[0]);
390 CHECK(recievedTimestamp[0].counterValues[0].counterValue == 1u);
391 CHECK(recievedTimestamp[0].counterValues[1].counterId == cpuCounters[2]);
392 CHECK(recievedTimestamp[0].counterValues[1].counterValue == 3u);
Finn Williams032bc742020-02-12 11:02:34 +0000393
Sadik Armagan1625efc2021-06-10 18:24:34 +0100394 CHECK(recievedTimestamp[1].counterValues.size() == 2);
Finn Williams032bc742020-02-12 11:02:34 +0000395
Sadik Armagan1625efc2021-06-10 18:24:34 +0100396 CHECK(recievedTimestamp[1].counterValues[0].counterId == gpuCounters[0]);
397 CHECK(recievedTimestamp[1].counterValues[0].counterValue == 1u);
398 CHECK(recievedTimestamp[1].counterValues[1].counterId == gpuCounters[1]);
399 CHECK(recievedTimestamp[1].counterValues[1].counterValue == 2u);
Finn Williams032bc742020-02-12 11:02:34 +0000400
401 sendCounterPacket.ClearTimestamps();
402
403 // Request no counters
404 periodicCounterSelectionCommandHandler(PacketWriter(period, {}));
405 periodicCounterCapture.Stop();
406
407 activeIds = holder.GetCaptureData().GetActiveBackends();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100408 CHECK(activeIds.size() == 0);
Finn Williams032bc742020-02-12 11:02:34 +0000409
410 recievedTimestamp = sendCounterPacket.GetTimestamps();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100411 CHECK(recievedTimestamp.size() == 0);
Finn Williams032bc742020-02-12 11:02:34 +0000412
413 sendCounterPacket.ClearTimestamps();
414
415 // Request period of zero
416 periodicCounterSelectionCommandHandler(PacketWriter(0, counterValues));
417 periodicCounterCapture.Stop();
418
419 activeIds = holder.GetCaptureData().GetActiveBackends();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100420 CHECK(activeIds.size() == 0);
Finn Williams032bc742020-02-12 11:02:34 +0000421
422 recievedTimestamp = sendCounterPacket.GetTimestamps();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100423 CHECK(recievedTimestamp.size() == 0);
Finn Williams032bc742020-02-12 11:02:34 +0000424}
425
Sadik Armagan1625efc2021-06-10 18:24:34 +0100426TEST_CASE("TestBackendCounterLogging")
Finn Williams032bc742020-02-12 11:02:34 +0000427{
428 std::stringstream ss;
429
430 struct StreamRedirector
431 {
432 public:
433 StreamRedirector(std::ostream &stream, std::streambuf *newStreamBuffer)
434 : m_Stream(stream), m_BackupBuffer(m_Stream.rdbuf(newStreamBuffer))
435 {}
436
437 ~StreamRedirector()
438 { m_Stream.rdbuf(m_BackupBuffer); }
439
440 private:
441 std::ostream &m_Stream;
442 std::streambuf *m_BackupBuffer;
443 };
444
445 Holder holder;
Jim Flynnbbfe6032020-07-20 16:57:44 +0100446 arm::pipe::PacketVersionResolver packetVersionResolver;
Finn Williams032bc742020-02-12 11:02:34 +0000447 ProfilingStateMachine stateMachine;
448 ReadCounterVals readCounterVals;
449 StreamRedirector redirect(std::cout, ss.rdbuf());
450 CounterIdMap counterIdMap;
451 MockBackendSendCounterPacket sendCounterPacket;
452
Cathal Corbett6f073722022-03-04 12:11:09 +0000453 const std::string cpuAccId(GetComputeDeviceAsCString(armnn::Compute::CpuAcc));
454 const std::string gpuAccId(GetComputeDeviceAsCString(armnn::Compute::GpuAcc));
Finn Williams032bc742020-02-12 11:02:34 +0000455
Jim Flynn4c9ed1d2022-01-23 23:57:20 +0000456 ProfilingOptions options;
457 options.m_EnableProfiling = true;
Finn Williams032bc742020-02-12 11:02:34 +0000458
Jim Flynn34430252022-03-04 15:03:58 +0000459 armnn::ArmNNProfilingServiceInitialiser initialiser;
460 std::unique_ptr<IProfilingService> profilingService = arm::pipe::IProfilingService::CreateProfilingService(
Jim Flynn9c85b412022-03-16 00:27:43 +0000461 arm::pipe::MAX_ARMNN_COUNTER,
462 initialiser,
463 arm::pipe::ARMNN_SOFTWARE_INFO,
464 arm::pipe::ARMNN_SOFTWARE_VERSION,
465 arm::pipe::ARMNN_HARDWARE_VERSION);
Finn Williams032bc742020-02-12 11:02:34 +0000466
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000467 std::unique_ptr<IBackendProfiling> cpuBackendProfilingPtr =
Jim Flynn34430252022-03-04 15:03:58 +0000468 std::make_unique<BackendProfiling>(options, *profilingService.get(), cpuAccId);
Finn Williams032bc742020-02-12 11:02:34 +0000469
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000470 std::shared_ptr<IBackendProfilingContext> cpuProfilingContextPtr =
Finn Williams032bc742020-02-12 11:02:34 +0000471 std::make_shared<armnn::MockBackendProfilingContext>(cpuBackendProfilingPtr);
472
Cathal Corbett6f073722022-03-04 12:11:09 +0000473 std::unordered_map<std::string,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000474 std::shared_ptr<IBackendProfilingContext>> backendProfilingContexts;
Finn Williams032bc742020-02-12 11:02:34 +0000475
476 uint16_t globalId = 5;
477 counterIdMap.RegisterMapping(globalId, 0, cpuAccId);
478 backendProfilingContexts[cpuAccId] = cpuProfilingContextPtr;
479
480 PeriodicCounterCapture periodicCounterCapture(holder, sendCounterPacket, readCounterVals,
481 counterIdMap, backendProfilingContexts);
482
483 uint16_t maxArmnnCounterId = 4;
484
485 PeriodicCounterSelectionCommandHandler periodicCounterSelectionCommandHandler(0,
486 4,
487 packetVersionResolver.ResolvePacketVersion(0, 4).GetEncodedValue(),
488 backendProfilingContexts,
489 counterIdMap,
490 holder,
491 maxArmnnCounterId,
492 periodicCounterCapture,
493 readCounterVals,
494 sendCounterPacket,
495 stateMachine);
496
497 stateMachine.TransitionToState(ProfilingState::NotConnected);
498 stateMachine.TransitionToState(ProfilingState::WaitingForAck);
499 stateMachine.TransitionToState(ProfilingState::Active);
500
501 uint32_t period = 15939u;
502
Jim Flynn6c9f17d2022-03-10 23:13:01 +0000503 arm::pipe::SetAllLoggingSinks(true, false, false);
504 arm::pipe::SetLogFilter(arm::pipe::LogSeverity::Warning);
Finn Williams032bc742020-02-12 11:02:34 +0000505 periodicCounterSelectionCommandHandler(PacketWriter(period, {5}));
506 periodicCounterCapture.Stop();
Jim Flynn6c9f17d2022-03-10 23:13:01 +0000507 arm::pipe::SetLogFilter(arm::pipe::LogSeverity::Fatal);
Finn Williams032bc742020-02-12 11:02:34 +0000508
Sadik Armagan1625efc2021-06-10 18:24:34 +0100509 CHECK(ss.str().find("ActivateCounters example test error") != std::string::npos);
Finn Williams032bc742020-02-12 11:02:34 +0000510}
511
Sadik Armagan1625efc2021-06-10 18:24:34 +0100512TEST_CASE("BackendProfilingContextGetSendTimelinePacket")
Colm Donelanfcb802b2020-02-13 20:47:08 +0000513{
Jim Flynn6c9f17d2022-03-10 23:13:01 +0000514 arm::pipe::LogLevelSwapper logLevelSwapper(arm::pipe::LogSeverity::Fatal);
515
Colm Donelanfcb802b2020-02-13 20:47:08 +0000516 // Reset the profiling service to the uninitialized state
517 armnn::IRuntime::CreationOptions options;
518 options.m_ProfilingOptions.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +0000519
520 armnn::ArmNNProfilingServiceInitialiser psInitialiser;
521 std::unique_ptr<IProfilingService> profilingService = arm::pipe::IProfilingService::CreateProfilingService(
Jim Flynn9c85b412022-03-16 00:27:43 +0000522 arm::pipe::MAX_ARMNN_COUNTER,
523 psInitialiser,
524 arm::pipe::ARMNN_SOFTWARE_INFO,
525 arm::pipe::ARMNN_SOFTWARE_VERSION,
526 arm::pipe::ARMNN_HARDWARE_VERSION);
Jim Flynn34430252022-03-04 15:03:58 +0000527
528 profilingService->ConfigureProfilingService(
Jim Flynn4c9ed1d2022-01-23 23:57:20 +0000529 ConvertExternalProfilingOptions(options.m_ProfilingOptions), true);
Colm Donelanfcb802b2020-02-13 20:47:08 +0000530
531 armnn::MockBackendInitialiser initialiser;
532 // Create a runtime. During this the mock backend will be registered and context returned.
533 armnn::IRuntimePtr runtime(armnn::IRuntime::Create(options));
534 armnn::MockBackendProfilingService mockProfilingService = armnn::MockBackendProfilingService::Instance();
Jim Flynn34430252022-03-04 15:03:58 +0000535 armnn::MockBackendProfilingContext* mockBackEndProfilingContext = mockProfilingService.GetContext();
Colm Donelanfcb802b2020-02-13 20:47:08 +0000536 // Check that there is a valid context set.
Sadik Armagan1625efc2021-06-10 18:24:34 +0100537 CHECK(mockBackEndProfilingContext);
Colm Donelanfcb802b2020-02-13 20:47:08 +0000538 armnn::IBackendInternal::IBackendProfilingPtr& backendProfilingIface =
539 mockBackEndProfilingContext->GetBackendProfiling();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100540 CHECK(backendProfilingIface);
Colm Donelanfcb802b2020-02-13 20:47:08 +0000541
542 // Now for the meat of the test. We're just going to send a random packet and make sure there
543 // are no exceptions or errors. The sending of packets is already tested in SendTimelinePacketTests.
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000544 std::unique_ptr<ISendTimelinePacket> timelinePacket =
Colm Donelanfcb802b2020-02-13 20:47:08 +0000545 backendProfilingIface->GetSendTimelinePacket();
546 // Send TimelineEntityClassBinaryPacket
547 const uint64_t entityBinaryPacketProfilingGuid = 123456u;
548 timelinePacket->SendTimelineEntityBinaryPacket(entityBinaryPacketProfilingGuid);
549 timelinePacket->Commit();
550
551 // Reset the profiling servie after the test.
552 options.m_ProfilingOptions.m_EnableProfiling = false;
Jim Flynn34430252022-03-04 15:03:58 +0000553 profilingService->ResetExternalProfilingOptions(
Jim Flynn4c9ed1d2022-01-23 23:57:20 +0000554 ConvertExternalProfilingOptions(options.m_ProfilingOptions), true);
Colm Donelanfcb802b2020-02-13 20:47:08 +0000555}
556
Sadik Armagan1625efc2021-06-10 18:24:34 +0100557TEST_CASE("GetProfilingGuidGenerator")
Colm Donelanfcb802b2020-02-13 20:47:08 +0000558{
Jim Flynn6c9f17d2022-03-10 23:13:01 +0000559 arm::pipe::LogLevelSwapper logLevelSwapper(arm::pipe::LogSeverity::Fatal);
560
Colm Donelanfcb802b2020-02-13 20:47:08 +0000561 // Reset the profiling service to the uninitialized state
562 armnn::IRuntime::CreationOptions options;
563 options.m_ProfilingOptions.m_EnableProfiling = true;
Colm Donelanfcb802b2020-02-13 20:47:08 +0000564
565 armnn::MockBackendInitialiser initialiser;
566 // Create a runtime. During this the mock backend will be registered and context returned.
567 armnn::IRuntimePtr runtime(armnn::IRuntime::Create(options));
568 armnn::MockBackendProfilingService mockProfilingService = armnn::MockBackendProfilingService::Instance();
569 armnn::MockBackendProfilingContext *mockBackEndProfilingContext = mockProfilingService.GetContext();
570 // Check that there is a valid context set.
Sadik Armagan1625efc2021-06-10 18:24:34 +0100571 CHECK(mockBackEndProfilingContext);
Colm Donelanfcb802b2020-02-13 20:47:08 +0000572 armnn::IBackendInternal::IBackendProfilingPtr& backendProfilingIface =
573 mockBackEndProfilingContext->GetBackendProfiling();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100574 CHECK(backendProfilingIface);
Colm Donelanfcb802b2020-02-13 20:47:08 +0000575
576 // 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 +0000577 IProfilingGuidGenerator& guidGenerator = backendProfilingIface->GetProfilingGuidGenerator();
578 const ProfilingDynamicGuid& firstGuid = guidGenerator.NextGuid();
579 const ProfilingDynamicGuid& secondGuid = guidGenerator.NextGuid();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100580 CHECK(secondGuid > firstGuid);
Colm Donelanfcb802b2020-02-13 20:47:08 +0000581
582 // Reset the profiling servie after the test.
583 options.m_ProfilingOptions.m_EnableProfiling = false;
Colm Donelanfcb802b2020-02-13 20:47:08 +0000584}
585
Sadik Armagan1625efc2021-06-10 18:24:34 +0100586}