blob: 9769b4f7a1e0869977fec4857807a7ba8c32682a [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
Finn Williams032bc742020-02-12 11:02:34 +00006#include "CounterDirectory.hpp"
7#include "CounterIdMap.hpp"
8#include "Holder.hpp"
David Monahanc1536d62020-02-12 15:52:35 +00009#include "MockBackend.hpp"
10#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>
Finn Williams032bc742020-02-12 11:02:34 +000025
Sadik Armagan1625efc2021-06-10 18:24:34 +010026#include <doctest/doctest.h>
David Monahanc1536d62020-02-12 15:52:35 +000027#include <vector>
28
Finn Williams032bc742020-02-12 11:02:34 +000029#include <cstdint>
30#include <limits>
31#include <backends/BackendProfiling.hpp>
32
33using namespace armnn::profiling;
34
35class 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 Williamsf3fcf322020-05-11 14:38:02 +010045 virtual uint32_t GetAbsoluteCounterValue(uint16_t counterUid) const override
46 {
47 return counterUid;
48 }
49 virtual uint32_t GetDeltaCounterValue(uint16_t counterUid) override
Finn Williams032bc742020-02-12 11:02:34 +000050 {
51 return counterUid;
52 }
53};
54
55class MockBackendSendCounterPacket : public ISendCounterPacket
56{
57public:
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 Eilers8eb25602020-03-09 12:13:48 +000066 armnn::IgnoreUnused(counterDirectory);
Finn Williams032bc742020-02-12 11:02:34 +000067 }
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 Eilers8eb25602020-03-09 12:13:48 +000079 armnn::IgnoreUnused(capturePeriod);
80 armnn::IgnoreUnused(selectedCounterIds);
Finn Williams032bc742020-02-12 11:02:34 +000081 }
82
83 std::vector<Timestamp> GetTimestamps()
84 {
85 return m_timestamps;
86 }
87
88 void ClearTimestamps()
89 {
90 m_timestamps.clear();
91 }
92
93private:
94 std::vector<Timestamp> m_timestamps;
95};
96
Jim Flynnbbfe6032020-07-20 16:57:44 +010097arm::pipe::Packet PacketWriter(uint32_t period, std::vector<uint16_t> countervalues)
Finn Williams032bc742020-02-12 11:02:34 +000098{
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 Armagan1625efc2021-06-10 18:24:34 +0100116TEST_SUITE("BackendProfilingTestSuite")
117{
118TEST_CASE("BackendProfilingCounterRegisterMockBackendTest")
David Monahanc1536d62020-02-12 15:52:35 +0000119{
120 // Reset the profiling service to the uninitialized state
121 armnn::IRuntime::CreationOptions options;
Finn Williams45a73622020-05-15 18:41:05 +0100122 options.m_ProfilingOptions.m_EnableProfiling = true;
David Monahanc1536d62020-02-12 15:52:35 +0000123
124 armnn::MockBackendInitialiser initialiser;
125 // Create a runtime
Kevin Mayd92a6e42021-02-04 10:27:41 +0000126 armnn::RuntimeImpl runtime(options);
David Monahanc1536d62020-02-12 15:52:35 +0000127
Narumol Prangnawarat060bad52020-11-20 16:17:48 +0000128 unsigned int shiftedId = 0;
129
Nikhil Raj01bc02d2021-05-07 17:07:09 +0100130 if (armnn::BackendRegistry().IsBackendRegistered("EthosNAcc"))
131 {
132 shiftedId = 4;
133 }
Narumol Prangnawarat060bad52020-11-20 16:17:48 +0000134
David Monahanc1536d62020-02-12 15:52:35 +0000135 // Check if the MockBackends 3 dummy counters {0, 1, 2-5 (four cores)} are registered
136 armnn::BackendId mockId = armnn::MockBackendId();
Sadik Armagan3184c902020-03-18 10:57:30 +0000137 const armnn::profiling::ICounterMappings& counterMap = GetProfilingService(&runtime).GetCounterMappings();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100138 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 Monahanc1536d62020-02-12 15:52:35 +0000144 options.m_ProfilingOptions.m_EnableProfiling = false;
Jim Flynn4c9ed1d2022-01-23 23:57:20 +0000145 GetProfilingService(&runtime).ResetExternalProfilingOptions(
146 ConvertExternalProfilingOptions(options.m_ProfilingOptions), true);
David Monahanc1536d62020-02-12 15:52:35 +0000147}
148
Sadik Armagan1625efc2021-06-10 18:24:34 +0100149TEST_CASE("TestBackendCounters")
Finn Williams032bc742020-02-12 11:02:34 +0000150{
151 Holder holder;
Jim Flynnbbfe6032020-07-20 16:57:44 +0100152 arm::pipe::PacketVersionResolver packetVersionResolver;
Finn Williams032bc742020-02-12 11:02:34 +0000153 ProfilingStateMachine stateMachine;
154 ReadCounterVals readCounterVals;
155 CounterIdMap counterIdMap;
156 MockBackendSendCounterPacket sendCounterPacket;
157
158 const armnn::BackendId cpuAccId(armnn::Compute::CpuAcc);
159 const armnn::BackendId gpuAccId(armnn::Compute::GpuAcc);
160
Jim Flynn4c9ed1d2022-01-23 23:57:20 +0000161 ProfilingOptions options;
162 options.m_EnableProfiling = true;
Finn Williams032bc742020-02-12 11:02:34 +0000163
Sadik Armagan3184c902020-03-18 10:57:30 +0000164 armnn::profiling::ProfilingService profilingService;
Finn Williams032bc742020-02-12 11:02:34 +0000165
166 std::unique_ptr<armnn::profiling::IBackendProfiling> cpuBackendProfilingPtr =
167 std::make_unique<BackendProfiling>(options, profilingService, cpuAccId);
168 std::unique_ptr<armnn::profiling::IBackendProfiling> gpuBackendProfilingPtr =
169 std::make_unique<BackendProfiling>(options, profilingService, gpuAccId);
170
171 std::shared_ptr<armnn::profiling::IBackendProfilingContext> cpuProfilingContextPtr =
172 std::make_shared<armnn::MockBackendProfilingContext>(cpuBackendProfilingPtr);
173 std::shared_ptr<armnn::profiling::IBackendProfilingContext> gpuProfilingContextPtr =
174 std::make_shared<armnn::MockBackendProfilingContext>(gpuBackendProfilingPtr);
175
176 std::unordered_map<armnn::BackendId,
177 std::shared_ptr<armnn::profiling::IBackendProfilingContext>> backendProfilingContexts;
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
225 std::set<armnn::BackendId> activeIds = holder.GetCaptureData().GetActiveBackends();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100226 CHECK(activeIds.size() == 1);
227 CHECK((activeIds.find(gpuAccId) != activeIds.end()));
Finn Williams032bc742020-02-12 11:02:34 +0000228
229 std::vector<Timestamp> recievedTimestamp = sendCounterPacket.GetTimestamps();
230
Sadik Armagan1625efc2021-06-10 18:24:34 +0100231 CHECK(recievedTimestamp[0].timestamp == period);
232 CHECK(recievedTimestamp.size() == 1);
233 CHECK(recievedTimestamp[0].counterValues.size() == gpuCounters.size());
Finn Williams032bc742020-02-12 11:02:34 +0000234 for (unsigned long i=0; i< gpuCounters.size(); ++i)
235 {
Sadik Armagan1625efc2021-06-10 18:24:34 +0100236 CHECK(recievedTimestamp[0].counterValues[i].counterId == gpuCounters[i]);
237 CHECK(recievedTimestamp[0].counterValues[i].counterValue == i + 1u);
Finn Williams032bc742020-02-12 11:02:34 +0000238 }
239 sendCounterPacket.ClearTimestamps();
240
241 // Request only cpu counters
242 periodicCounterSelectionCommandHandler(PacketWriter(period, cpuCounters));
243 periodicCounterCapture.Stop();
244
245 activeIds = holder.GetCaptureData().GetActiveBackends();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100246 CHECK(activeIds.size() == 1);
247 CHECK((activeIds.find(cpuAccId) != activeIds.end()));
Finn Williams032bc742020-02-12 11:02:34 +0000248
249 recievedTimestamp = sendCounterPacket.GetTimestamps();
250
Sadik Armagan1625efc2021-06-10 18:24:34 +0100251 CHECK(recievedTimestamp[0].timestamp == period);
252 CHECK(recievedTimestamp.size() == 1);
253 CHECK(recievedTimestamp[0].counterValues.size() == cpuCounters.size());
Finn Williams032bc742020-02-12 11:02:34 +0000254 for (unsigned long i=0; i< cpuCounters.size(); ++i)
255 {
Sadik Armagan1625efc2021-06-10 18:24:34 +0100256 CHECK(recievedTimestamp[0].counterValues[i].counterId == cpuCounters[i]);
257 CHECK(recievedTimestamp[0].counterValues[i].counterValue == i + 1u);
Finn Williams032bc742020-02-12 11:02:34 +0000258 }
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 Armagan1625efc2021-06-10 18:24:34 +0100268 CHECK(activeIds.size() == 2);
269 CHECK((activeIds.find(cpuAccId) != activeIds.end()));
270 CHECK((activeIds.find(gpuAccId) != activeIds.end()));
Finn Williams032bc742020-02-12 11:02:34 +0000271
272 recievedTimestamp = sendCounterPacket.GetTimestamps();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100273//
274 CHECK(recievedTimestamp[0].timestamp == period);
275 CHECK(recievedTimestamp[1].timestamp == period);
Finn Williams032bc742020-02-12 11:02:34 +0000276
Sadik Armagan1625efc2021-06-10 18:24:34 +0100277 CHECK(recievedTimestamp.size() == 2);
278 CHECK(recievedTimestamp[0].counterValues.size() == 2);
279 CHECK(recievedTimestamp[1].counterValues.size() == gpuCounters.size());
Finn Williams032bc742020-02-12 11:02:34 +0000280
Sadik Armagan1625efc2021-06-10 18:24:34 +0100281 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 Williams032bc742020-02-12 11:02:34 +0000285
286 for (unsigned long i=0; i< gpuCounters.size(); ++i)
287 {
Sadik Armagan1625efc2021-06-10 18:24:34 +0100288 CHECK(recievedTimestamp[1].counterValues[i].counterId == gpuCounters[i]);
289 CHECK(recievedTimestamp[1].counterValues[i].counterValue == i + 1u);
Finn Williams032bc742020-02-12 11:02:34 +0000290 }
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 Armagan1625efc2021-06-10 18:24:34 +0100303 CHECK(activeIds.size() == 2);
304 CHECK((activeIds.find(cpuAccId) != activeIds.end()));
305 CHECK((activeIds.find(gpuAccId) != activeIds.end()));
Finn Williams032bc742020-02-12 11:02:34 +0000306
307 recievedTimestamp = sendCounterPacket.GetTimestamps();
308
Sadik Armagan1625efc2021-06-10 18:24:34 +0100309 CHECK(recievedTimestamp[0].counterValues.size() == cpuCounters.size());
Finn Williams032bc742020-02-12 11:02:34 +0000310 for (unsigned long i=0; i< cpuCounters.size(); ++i)
311 {
Sadik Armagan1625efc2021-06-10 18:24:34 +0100312 CHECK(recievedTimestamp[0].counterValues[i].counterId == cpuCounters[i]);
313 CHECK(recievedTimestamp[0].counterValues[i].counterValue == i + 1u);
Finn Williams032bc742020-02-12 11:02:34 +0000314 }
315
Sadik Armagan1625efc2021-06-10 18:24:34 +0100316 CHECK(recievedTimestamp[1].counterValues.size() == gpuCounters.size());
Finn Williams032bc742020-02-12 11:02:34 +0000317 for (unsigned long i=0; i< gpuCounters.size(); ++i)
318 {
Sadik Armagan1625efc2021-06-10 18:24:34 +0100319 CHECK(recievedTimestamp[1].counterValues[i].counterId == gpuCounters[i]);
320 CHECK(recievedTimestamp[1].counterValues[i].counterValue == i + 1u);
Finn Williams032bc742020-02-12 11:02:34 +0000321 }
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 Armagan1625efc2021-06-10 18:24:34 +0100332 CHECK(activeIds.size() == 2);
333 CHECK((activeIds.find(cpuAccId) != activeIds.end()));
334 CHECK((activeIds.find(gpuAccId) != activeIds.end()));
Finn Williams032bc742020-02-12 11:02:34 +0000335
336 recievedTimestamp = sendCounterPacket.GetTimestamps();
337
Sadik Armagan1625efc2021-06-10 18:24:34 +0100338 CHECK(recievedTimestamp.size() == 2);
Finn Williams032bc742020-02-12 11:02:34 +0000339
Sadik Armagan1625efc2021-06-10 18:24:34 +0100340 CHECK(recievedTimestamp[0].counterValues.size() == 2);
Finn Williams032bc742020-02-12 11:02:34 +0000341
Sadik Armagan1625efc2021-06-10 18:24:34 +0100342 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 Williams032bc742020-02-12 11:02:34 +0000346
Sadik Armagan1625efc2021-06-10 18:24:34 +0100347 CHECK(recievedTimestamp[1].counterValues.size() == 2);
Finn Williams032bc742020-02-12 11:02:34 +0000348
Sadik Armagan1625efc2021-06-10 18:24:34 +0100349 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 Williams032bc742020-02-12 11:02:34 +0000353
354 sendCounterPacket.ClearTimestamps();
355
356 // Request no counters
357 periodicCounterSelectionCommandHandler(PacketWriter(period, {}));
358 periodicCounterCapture.Stop();
359
360 activeIds = holder.GetCaptureData().GetActiveBackends();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100361 CHECK(activeIds.size() == 0);
Finn Williams032bc742020-02-12 11:02:34 +0000362
363 recievedTimestamp = sendCounterPacket.GetTimestamps();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100364 CHECK(recievedTimestamp.size() == 0);
Finn Williams032bc742020-02-12 11:02:34 +0000365
366 sendCounterPacket.ClearTimestamps();
367
368 // Request period of zero
369 periodicCounterSelectionCommandHandler(PacketWriter(0, counterValues));
370 periodicCounterCapture.Stop();
371
372 activeIds = holder.GetCaptureData().GetActiveBackends();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100373 CHECK(activeIds.size() == 0);
Finn Williams032bc742020-02-12 11:02:34 +0000374
375 recievedTimestamp = sendCounterPacket.GetTimestamps();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100376 CHECK(recievedTimestamp.size() == 0);
Finn Williams032bc742020-02-12 11:02:34 +0000377}
378
Sadik Armagan1625efc2021-06-10 18:24:34 +0100379TEST_CASE("TestBackendCounterLogging")
Finn Williams032bc742020-02-12 11:02:34 +0000380{
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 Flynnbbfe6032020-07-20 16:57:44 +0100399 arm::pipe::PacketVersionResolver packetVersionResolver;
Finn Williams032bc742020-02-12 11:02:34 +0000400 ProfilingStateMachine stateMachine;
401 ReadCounterVals readCounterVals;
402 StreamRedirector redirect(std::cout, ss.rdbuf());
403 CounterIdMap counterIdMap;
404 MockBackendSendCounterPacket sendCounterPacket;
405
406 const armnn::BackendId cpuAccId(armnn::Compute::CpuAcc);
407 const armnn::BackendId gpuAccId(armnn::Compute::GpuAcc);
408
Jim Flynn4c9ed1d2022-01-23 23:57:20 +0000409 ProfilingOptions options;
410 options.m_EnableProfiling = true;
Finn Williams032bc742020-02-12 11:02:34 +0000411
Sadik Armagan3184c902020-03-18 10:57:30 +0000412 armnn::profiling::ProfilingService profilingService;
Finn Williams032bc742020-02-12 11:02:34 +0000413
414 std::unique_ptr<armnn::profiling::IBackendProfiling> cpuBackendProfilingPtr =
415 std::make_unique<BackendProfiling>(options, profilingService, cpuAccId);
416
417 std::shared_ptr<armnn::profiling::IBackendProfilingContext> cpuProfilingContextPtr =
418 std::make_shared<armnn::MockBackendProfilingContext>(cpuBackendProfilingPtr);
419
420 std::unordered_map<armnn::BackendId,
421 std::shared_ptr<armnn::profiling::IBackendProfilingContext>> backendProfilingContexts;
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 Armagan1625efc2021-06-10 18:24:34 +0100456 CHECK(ss.str().find("ActivateCounters example test error") != std::string::npos);
Finn Williams032bc742020-02-12 11:02:34 +0000457}
458
Sadik Armagan1625efc2021-06-10 18:24:34 +0100459TEST_CASE("BackendProfilingContextGetSendTimelinePacket")
Colm Donelanfcb802b2020-02-13 20:47:08 +0000460{
461 // Reset the profiling service to the uninitialized state
462 armnn::IRuntime::CreationOptions options;
463 options.m_ProfilingOptions.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +0000464 armnn::profiling::ProfilingService profilingService;
Jim Flynn4c9ed1d2022-01-23 23:57:20 +0000465 profilingService.ConfigureProfilingService(
466 ConvertExternalProfilingOptions(options.m_ProfilingOptions), true);
Colm Donelanfcb802b2020-02-13 20:47:08 +0000467
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 Armagan1625efc2021-06-10 18:24:34 +0100474 CHECK(mockBackEndProfilingContext);
Colm Donelanfcb802b2020-02-13 20:47:08 +0000475 armnn::IBackendInternal::IBackendProfilingPtr& backendProfilingIface =
476 mockBackEndProfilingContext->GetBackendProfiling();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100477 CHECK(backendProfilingIface);
Colm Donelanfcb802b2020-02-13 20:47:08 +0000478
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.
481 std::unique_ptr<armnn::profiling::ISendTimelinePacket> timelinePacket =
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 Flynn4c9ed1d2022-01-23 23:57:20 +0000490 profilingService.ResetExternalProfilingOptions(
491 ConvertExternalProfilingOptions(options.m_ProfilingOptions), true);
Colm Donelanfcb802b2020-02-13 20:47:08 +0000492}
493
Sadik Armagan1625efc2021-06-10 18:24:34 +0100494TEST_CASE("GetProfilingGuidGenerator")
Colm Donelanfcb802b2020-02-13 20:47:08 +0000495{
496 // Reset the profiling service to the uninitialized state
497 armnn::IRuntime::CreationOptions options;
498 options.m_ProfilingOptions.m_EnableProfiling = true;
Colm Donelanfcb802b2020-02-13 20:47:08 +0000499
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 Armagan1625efc2021-06-10 18:24:34 +0100506 CHECK(mockBackEndProfilingContext);
Colm Donelanfcb802b2020-02-13 20:47:08 +0000507 armnn::IBackendInternal::IBackendProfilingPtr& backendProfilingIface =
508 mockBackEndProfilingContext->GetBackendProfiling();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100509 CHECK(backendProfilingIface);
Colm Donelanfcb802b2020-02-13 20:47:08 +0000510
511 // Get the Guid generator and check the getting two Guid's results in the second being greater than the first.
512 armnn::profiling::IProfilingGuidGenerator& guidGenerator = backendProfilingIface->GetProfilingGuidGenerator();
Colm Donelanfcb802b2020-02-13 20:47:08 +0000513 const armnn::profiling::ProfilingDynamicGuid& firstGuid = guidGenerator.NextGuid();
Colm Donelanfcb802b2020-02-13 20:47:08 +0000514 const armnn::profiling::ProfilingDynamicGuid& secondGuid = guidGenerator.NextGuid();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100515 CHECK(secondGuid > firstGuid);
Colm Donelanfcb802b2020-02-13 20:47:08 +0000516
517 // Reset the profiling servie after the test.
518 options.m_ProfilingOptions.m_EnableProfiling = false;
Colm Donelanfcb802b2020-02-13 20:47:08 +0000519}
520
Sadik Armagan1625efc2021-06-10 18:24:34 +0100521}