blob: 1cf0e1e4b9997bd95f97bad5aee10a652219ac74 [file] [log] [blame]
Francis Murtagh1f7db452019-08-14 09:49:34 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
Ferran Balaguer1b941722019-08-28 16:57:18 +01006#include "SendCounterPacketTests.hpp"
Teresa Charlin9bab4962019-09-06 12:28:35 +01007
Matteo Martincigh6db5f202019-09-05 12:02:04 +01008#include <CommandHandlerKey.hpp>
9#include <CommandHandlerFunctor.hpp>
10#include <CommandHandlerRegistry.hpp>
Sadik Armaganb5f01b22019-09-18 17:29:00 +010011#include <ConnectionAcknowledgedCommandHandler.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010012#include <CounterDirectory.hpp>
13#include <EncodeVersion.hpp>
14#include <Holder.hpp>
15#include <Packet.hpp>
16#include <PacketVersionResolver.hpp>
17#include <PeriodicCounterSelectionCommandHandler.hpp>
18#include <ProfilingStateMachine.hpp>
19#include <ProfilingService.hpp>
20#include <ProfilingUtils.hpp>
Teresa Charlin9bab4962019-09-06 12:28:35 +010021#include <Runtime.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010022#include <SocketProfilingConnection.hpp>
Keith Davis02356de2019-08-26 18:28:17 +010023
Matteo Martincigh6db5f202019-09-05 12:02:04 +010024#include <armnn/Conversion.hpp>
Ferran Balaguer1b941722019-08-28 16:57:18 +010025
Aron Virginas-Tare898db92019-08-22 12:56:34 +010026#include <boost/test/unit_test.hpp>
Ferran Balaguer1b941722019-08-28 16:57:18 +010027#include <boost/numeric/conversion/cast.hpp>
Francis Murtagh1f7db452019-08-14 09:49:34 +010028
Nikhil Rajbc626052019-08-15 15:49:45 +010029#include <cstdint>
30#include <cstring>
Aron Virginas-Tare898db92019-08-22 12:56:34 +010031#include <limits>
Francis Murtagh11f99b42019-08-16 11:28:52 +010032#include <map>
Aron Virginas-Tare898db92019-08-22 12:56:34 +010033#include <random>
Nikhil Raj3ecc5102019-09-03 15:55:33 +010034#include <thread>
Francis Murtagh1f7db452019-08-14 09:49:34 +010035
36BOOST_AUTO_TEST_SUITE(ExternalProfiling)
37
Aron Virginas-Tare898db92019-08-22 12:56:34 +010038using namespace armnn::profiling;
39
Francis Murtagh1f7db452019-08-14 09:49:34 +010040BOOST_AUTO_TEST_CASE(CheckCommandHandlerKeyComparisons)
41{
42 CommandHandlerKey testKey0(1, 1);
43 CommandHandlerKey testKey1(1, 1);
44 CommandHandlerKey testKey2(1, 1);
45 CommandHandlerKey testKey3(0, 0);
46 CommandHandlerKey testKey4(2, 2);
47 CommandHandlerKey testKey5(0, 2);
48
49 BOOST_CHECK(testKey1<testKey4);
50 BOOST_CHECK(testKey1>testKey3);
51 BOOST_CHECK(testKey1<=testKey4);
52 BOOST_CHECK(testKey1>=testKey3);
53 BOOST_CHECK(testKey1<=testKey2);
54 BOOST_CHECK(testKey1>=testKey2);
55 BOOST_CHECK(testKey1==testKey2);
56 BOOST_CHECK(testKey1==testKey1);
57
58 BOOST_CHECK(!(testKey1==testKey5));
59 BOOST_CHECK(!(testKey1!=testKey1));
60 BOOST_CHECK(testKey1!=testKey5);
61
62 BOOST_CHECK(testKey1==testKey2 && testKey2==testKey1);
63 BOOST_CHECK(testKey0==testKey1 && testKey1==testKey2 && testKey0==testKey2);
64
65 BOOST_CHECK(testKey1.GetPacketId()==1);
66 BOOST_CHECK(testKey1.GetVersion()==1);
67
68 std::vector<CommandHandlerKey> vect =
Aron Virginas-Tare898db92019-08-22 12:56:34 +010069 {
70 CommandHandlerKey(0,1), CommandHandlerKey(2,0), CommandHandlerKey(1,0),
71 CommandHandlerKey(2,1), CommandHandlerKey(1,1), CommandHandlerKey(0,1),
72 CommandHandlerKey(2,0), CommandHandlerKey(0,0)
73 };
Francis Murtagh1f7db452019-08-14 09:49:34 +010074
75 std::sort(vect.begin(), vect.end());
76
77 std::vector<CommandHandlerKey> expectedVect =
Aron Virginas-Tare898db92019-08-22 12:56:34 +010078 {
79 CommandHandlerKey(0,0), CommandHandlerKey(0,1), CommandHandlerKey(0,1),
80 CommandHandlerKey(1,0), CommandHandlerKey(1,1), CommandHandlerKey(2,0),
81 CommandHandlerKey(2,0), CommandHandlerKey(2,1)
82 };
Francis Murtagh1f7db452019-08-14 09:49:34 +010083
84 BOOST_CHECK(vect == expectedVect);
85}
86
Nikhil Rajd88e47c2019-08-19 10:04:23 +010087BOOST_AUTO_TEST_CASE(CheckEncodeVersion)
88{
Aron Virginas-Tare898db92019-08-22 12:56:34 +010089 Version version1(12);
Nikhil Rajd88e47c2019-08-19 10:04:23 +010090
91 BOOST_CHECK(version1.GetMajor() == 0);
92 BOOST_CHECK(version1.GetMinor() == 0);
93 BOOST_CHECK(version1.GetPatch() == 12);
94
Aron Virginas-Tare898db92019-08-22 12:56:34 +010095 Version version2(4108);
Nikhil Rajd88e47c2019-08-19 10:04:23 +010096
97 BOOST_CHECK(version2.GetMajor() == 0);
98 BOOST_CHECK(version2.GetMinor() == 1);
99 BOOST_CHECK(version2.GetPatch() == 12);
100
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100101 Version version3(4198412);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100102
103 BOOST_CHECK(version3.GetMajor() == 1);
104 BOOST_CHECK(version3.GetMinor() == 1);
105 BOOST_CHECK(version3.GetPatch() == 12);
106
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100107 Version version4(0);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100108
109 BOOST_CHECK(version4.GetMajor() == 0);
110 BOOST_CHECK(version4.GetMinor() == 0);
111 BOOST_CHECK(version4.GetPatch() == 0);
112
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100113 Version version5(1, 0, 0);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100114 BOOST_CHECK(version5.GetEncodedValue() == 4194304);
115}
116
Nikhil Rajbc626052019-08-15 15:49:45 +0100117BOOST_AUTO_TEST_CASE(CheckPacketClass)
118{
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100119 uint32_t length = 4;
120 std::unique_ptr<char[]> packetData0 = std::make_unique<char[]>(length);
121 std::unique_ptr<char[]> packetData1 = std::make_unique<char[]>(0);
122 std::unique_ptr<char[]> nullPacketData;
Nikhil Rajbc626052019-08-15 15:49:45 +0100123
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100124 Packet packetTest0(472580096, length, packetData0);
Nikhil Rajbc626052019-08-15 15:49:45 +0100125
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100126 BOOST_CHECK(packetTest0.GetHeader() == 472580096);
127 BOOST_CHECK(packetTest0.GetPacketFamily() == 7);
128 BOOST_CHECK(packetTest0.GetPacketId() == 43);
129 BOOST_CHECK(packetTest0.GetLength() == length);
130 BOOST_CHECK(packetTest0.GetPacketType() == 3);
131 BOOST_CHECK(packetTest0.GetPacketClass() == 5);
Nikhil Rajbc626052019-08-15 15:49:45 +0100132
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100133 BOOST_CHECK_THROW(Packet packetTest1(472580096, 0, packetData1), armnn::Exception);
134 BOOST_CHECK_NO_THROW(Packet packetTest2(472580096, 0, nullPacketData));
Nikhil Rajbc626052019-08-15 15:49:45 +0100135
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100136 Packet packetTest3(472580096, 0, nullPacketData);
137 BOOST_CHECK(packetTest3.GetLength() == 0);
138 BOOST_CHECK(packetTest3.GetData() == nullptr);
139
140 const char* packetTest0Data = packetTest0.GetData();
141 Packet packetTest4(std::move(packetTest0));
142
143 BOOST_CHECK(packetTest0.GetData() == nullptr);
144 BOOST_CHECK(packetTest4.GetData() == packetTest0Data);
145
146 BOOST_CHECK(packetTest4.GetHeader() == 472580096);
147 BOOST_CHECK(packetTest4.GetPacketFamily() == 7);
148 BOOST_CHECK(packetTest4.GetPacketId() == 43);
149 BOOST_CHECK(packetTest4.GetLength() == length);
150 BOOST_CHECK(packetTest4.GetPacketType() == 3);
151 BOOST_CHECK(packetTest4.GetPacketClass() == 5);
Nikhil Rajbc626052019-08-15 15:49:45 +0100152}
153
Francis Murtagh94d79152019-08-16 17:45:07 +0100154// Create Derived Classes
155class TestFunctorA : public CommandHandlerFunctor
156{
157public:
158 using CommandHandlerFunctor::CommandHandlerFunctor;
159
160 int GetCount() { return m_Count; }
161
162 void operator()(const Packet& packet) override
163 {
164 m_Count++;
165 }
166
167private:
168 int m_Count = 0;
169};
170
171class TestFunctorB : public TestFunctorA
172{
173 using TestFunctorA::TestFunctorA;
174};
175
176class TestFunctorC : public TestFunctorA
177{
178 using TestFunctorA::TestFunctorA;
179};
180
Francis Murtagh11f99b42019-08-16 11:28:52 +0100181BOOST_AUTO_TEST_CASE(CheckCommandHandlerFunctor)
182{
Francis Murtagh11f99b42019-08-16 11:28:52 +0100183 // Hard code the version as it will be the same during a single profiling session
184 uint32_t version = 1;
185
186 TestFunctorA testFunctorA(461, version);
187 TestFunctorB testFunctorB(963, version);
188 TestFunctorC testFunctorC(983, version);
189
190 CommandHandlerKey keyA(testFunctorA.GetPacketId(), testFunctorA.GetVersion());
191 CommandHandlerKey keyB(testFunctorB.GetPacketId(), testFunctorB.GetVersion());
192 CommandHandlerKey keyC(testFunctorC.GetPacketId(), testFunctorC.GetVersion());
193
194 // Create the unwrapped map to simulate the Command Handler Registry
195 std::map<CommandHandlerKey, CommandHandlerFunctor*> registry;
196
197 registry.insert(std::make_pair(keyB, &testFunctorB));
198 registry.insert(std::make_pair(keyA, &testFunctorA));
199 registry.insert(std::make_pair(keyC, &testFunctorC));
200
201 // Check the order of the map is correct
202 auto it = registry.begin();
203 BOOST_CHECK(it->first==keyA);
204 it++;
205 BOOST_CHECK(it->first==keyB);
206 it++;
207 BOOST_CHECK(it->first==keyC);
208
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100209 std::unique_ptr<char[]> packetDataA;
210 std::unique_ptr<char[]> packetDataB;
211 std::unique_ptr<char[]> packetDataC;
212
213 Packet packetA(500000000, 0, packetDataA);
214 Packet packetB(600000000, 0, packetDataB);
215 Packet packetC(400000000, 0, packetDataC);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100216
217 // Check the correct operator of derived class is called
218 registry.at(CommandHandlerKey(packetA.GetPacketId(), version))->operator()(packetA);
219 BOOST_CHECK(testFunctorA.GetCount() == 1);
220 BOOST_CHECK(testFunctorB.GetCount() == 0);
221 BOOST_CHECK(testFunctorC.GetCount() == 0);
222
223 registry.at(CommandHandlerKey(packetB.GetPacketId(), version))->operator()(packetB);
224 BOOST_CHECK(testFunctorA.GetCount() == 1);
225 BOOST_CHECK(testFunctorB.GetCount() == 1);
226 BOOST_CHECK(testFunctorC.GetCount() == 0);
227
228 registry.at(CommandHandlerKey(packetC.GetPacketId(), version))->operator()(packetC);
229 BOOST_CHECK(testFunctorA.GetCount() == 1);
230 BOOST_CHECK(testFunctorB.GetCount() == 1);
231 BOOST_CHECK(testFunctorC.GetCount() == 1);
232}
233
Francis Murtagh94d79152019-08-16 17:45:07 +0100234BOOST_AUTO_TEST_CASE(CheckCommandHandlerRegistry)
235{
236 // Hard code the version as it will be the same during a single profiling session
237 uint32_t version = 1;
238
239 TestFunctorA testFunctorA(461, version);
240 TestFunctorB testFunctorB(963, version);
241 TestFunctorC testFunctorC(983, version);
242
243 // Create the Command Handler Registry
244 CommandHandlerRegistry registry;
245
246 // Register multiple different derived classes
247 registry.RegisterFunctor(&testFunctorA, testFunctorA.GetPacketId(), testFunctorA.GetVersion());
248 registry.RegisterFunctor(&testFunctorB, testFunctorB.GetPacketId(), testFunctorB.GetVersion());
249 registry.RegisterFunctor(&testFunctorC, testFunctorC.GetPacketId(), testFunctorC.GetVersion());
250
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100251 std::unique_ptr<char[]> packetDataA;
252 std::unique_ptr<char[]> packetDataB;
253 std::unique_ptr<char[]> packetDataC;
254
255 Packet packetA(500000000, 0, packetDataA);
256 Packet packetB(600000000, 0, packetDataB);
257 Packet packetC(400000000, 0, packetDataC);
Francis Murtagh94d79152019-08-16 17:45:07 +0100258
259 // Check the correct operator of derived class is called
260 registry.GetFunctor(packetA.GetPacketId(), version)->operator()(packetA);
261 BOOST_CHECK(testFunctorA.GetCount() == 1);
262 BOOST_CHECK(testFunctorB.GetCount() == 0);
263 BOOST_CHECK(testFunctorC.GetCount() == 0);
264
265 registry.GetFunctor(packetB.GetPacketId(), version)->operator()(packetB);
266 BOOST_CHECK(testFunctorA.GetCount() == 1);
267 BOOST_CHECK(testFunctorB.GetCount() == 1);
268 BOOST_CHECK(testFunctorC.GetCount() == 0);
269
270 registry.GetFunctor(packetC.GetPacketId(), version)->operator()(packetC);
271 BOOST_CHECK(testFunctorA.GetCount() == 1);
272 BOOST_CHECK(testFunctorB.GetCount() == 1);
273 BOOST_CHECK(testFunctorC.GetCount() == 1);
274
275 // Re-register an existing key with a new function
276 registry.RegisterFunctor(&testFunctorC, testFunctorA.GetPacketId(), version);
277 registry.GetFunctor(packetA.GetPacketId(), version)->operator()(packetC);
278 BOOST_CHECK(testFunctorA.GetCount() == 1);
279 BOOST_CHECK(testFunctorB.GetCount() == 1);
280 BOOST_CHECK(testFunctorC.GetCount() == 2);
281
282 // Check that non-existent key returns nullptr for its functor
283 BOOST_CHECK_THROW(registry.GetFunctor(0, 0), armnn::Exception);
284}
285
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100286BOOST_AUTO_TEST_CASE(CheckPacketVersionResolver)
287{
288 // Set up random number generator for generating packetId values
289 std::random_device device;
290 std::mt19937 generator(device());
291 std::uniform_int_distribution<uint32_t> distribution(std::numeric_limits<uint32_t>::min(),
292 std::numeric_limits<uint32_t>::max());
293
294 // NOTE: Expected version is always 1.0.0, regardless of packetId
295 const Version expectedVersion(1, 0, 0);
296
297 PacketVersionResolver packetVersionResolver;
298
299 constexpr unsigned int numTests = 10u;
300
301 for (unsigned int i = 0u; i < numTests; ++i)
302 {
303 const uint32_t packetId = distribution(generator);
304 Version resolvedVersion = packetVersionResolver.ResolvePacketVersion(packetId);
305
306 BOOST_TEST(resolvedVersion == expectedVersion);
307 }
308}
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100309void ProfilingCurrentStateThreadImpl(ProfilingStateMachine& states)
310{
311 ProfilingState newState = ProfilingState::NotConnected;
312 states.GetCurrentState();
313 states.TransitionToState(newState);
314}
315
316BOOST_AUTO_TEST_CASE(CheckProfilingStateMachine)
317{
318 ProfilingStateMachine profilingState1(ProfilingState::Uninitialised);
319 profilingState1.TransitionToState(ProfilingState::Uninitialised);
320 BOOST_CHECK(profilingState1.GetCurrentState() == ProfilingState::Uninitialised);
321
322 ProfilingStateMachine profilingState2(ProfilingState::Uninitialised);
323 profilingState2.TransitionToState(ProfilingState::NotConnected);
324 BOOST_CHECK(profilingState2.GetCurrentState() == ProfilingState::NotConnected);
325
326 ProfilingStateMachine profilingState3(ProfilingState::NotConnected);
327 profilingState3.TransitionToState(ProfilingState::NotConnected);
328 BOOST_CHECK(profilingState3.GetCurrentState() == ProfilingState::NotConnected);
329
330 ProfilingStateMachine profilingState4(ProfilingState::NotConnected);
331 profilingState4.TransitionToState(ProfilingState::WaitingForAck);
332 BOOST_CHECK(profilingState4.GetCurrentState() == ProfilingState::WaitingForAck);
333
334 ProfilingStateMachine profilingState5(ProfilingState::WaitingForAck);
335 profilingState5.TransitionToState(ProfilingState::WaitingForAck);
336 BOOST_CHECK(profilingState5.GetCurrentState() == ProfilingState::WaitingForAck);
337
338 ProfilingStateMachine profilingState6(ProfilingState::WaitingForAck);
339 profilingState6.TransitionToState(ProfilingState::Active);
340 BOOST_CHECK(profilingState6.GetCurrentState() == ProfilingState::Active);
341
342 ProfilingStateMachine profilingState7(ProfilingState::Active);
343 profilingState7.TransitionToState(ProfilingState::NotConnected);
344 BOOST_CHECK(profilingState7.GetCurrentState() == ProfilingState::NotConnected);
345
346 ProfilingStateMachine profilingState8(ProfilingState::Active);
347 profilingState8.TransitionToState(ProfilingState::Active);
348 BOOST_CHECK(profilingState8.GetCurrentState() == ProfilingState::Active);
349
350 ProfilingStateMachine profilingState9(ProfilingState::Uninitialised);
351 BOOST_CHECK_THROW(profilingState9.TransitionToState(ProfilingState::WaitingForAck),
352 armnn::Exception);
353
354 ProfilingStateMachine profilingState10(ProfilingState::Uninitialised);
355 BOOST_CHECK_THROW(profilingState10.TransitionToState(ProfilingState::Active),
356 armnn::Exception);
357
358 ProfilingStateMachine profilingState11(ProfilingState::NotConnected);
359 BOOST_CHECK_THROW(profilingState11.TransitionToState(ProfilingState::Uninitialised),
360 armnn::Exception);
361
362 ProfilingStateMachine profilingState12(ProfilingState::NotConnected);
363 BOOST_CHECK_THROW(profilingState12.TransitionToState(ProfilingState::Active),
364 armnn::Exception);
365
366 ProfilingStateMachine profilingState13(ProfilingState::WaitingForAck);
367 BOOST_CHECK_THROW(profilingState13.TransitionToState(ProfilingState::Uninitialised),
368 armnn::Exception);
369
370 ProfilingStateMachine profilingState14(ProfilingState::WaitingForAck);
371 BOOST_CHECK_THROW(profilingState14.TransitionToState(ProfilingState::NotConnected),
372 armnn::Exception);
373
374 ProfilingStateMachine profilingState15(ProfilingState::Active);
375 BOOST_CHECK_THROW(profilingState15.TransitionToState(ProfilingState::Uninitialised),
376 armnn::Exception);
377
378 ProfilingStateMachine profilingState16(armnn::profiling::ProfilingState::Active);
379 BOOST_CHECK_THROW(profilingState16.TransitionToState(ProfilingState::WaitingForAck),
380 armnn::Exception);
381
382 ProfilingStateMachine profilingState17(ProfilingState::Uninitialised);
383
384 std::thread thread1 (ProfilingCurrentStateThreadImpl,std::ref(profilingState17));
385 std::thread thread2 (ProfilingCurrentStateThreadImpl,std::ref(profilingState17));
386 std::thread thread3 (ProfilingCurrentStateThreadImpl,std::ref(profilingState17));
387 std::thread thread4 (ProfilingCurrentStateThreadImpl,std::ref(profilingState17));
388 std::thread thread5 (ProfilingCurrentStateThreadImpl,std::ref(profilingState17));
389
390 thread1.join();
391 thread2.join();
392 thread3.join();
393 thread4.join();
394 thread5.join();
395
396 BOOST_TEST((profilingState17.GetCurrentState() == ProfilingState::NotConnected));
397}
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100398
Jim Flynn8355ec92019-09-17 12:29:50 +0100399void CaptureDataWriteThreadImpl(Holder& holder, uint32_t capturePeriod, const std::vector<uint16_t>& counterIds)
Francis Murtagh68f78d82019-09-04 16:42:29 +0100400{
401 holder.SetCaptureData(capturePeriod, counterIds);
402}
403
Francis Murtaghbd707162019-09-09 11:26:44 +0100404void CaptureDataReadThreadImpl(const Holder& holder, CaptureData& captureData)
Francis Murtagh68f78d82019-09-04 16:42:29 +0100405{
406 captureData = holder.GetCaptureData();
407}
408
409BOOST_AUTO_TEST_CASE(CheckCaptureDataHolder)
410{
Francis Murtaghbd707162019-09-09 11:26:44 +0100411 std::map<uint32_t, std::vector<uint16_t>> periodIdMap;
412 std::vector<uint16_t> counterIds;
Jim Flynn8355ec92019-09-17 12:29:50 +0100413 uint32_t numThreads = 10;
414 for (uint32_t i = 0; i < numThreads; ++i)
Francis Murtaghbd707162019-09-09 11:26:44 +0100415 {
416 counterIds.emplace_back(i);
417 periodIdMap.insert(std::make_pair(i, counterIds));
418 }
Francis Murtagh68f78d82019-09-04 16:42:29 +0100419
Jim Flynn8355ec92019-09-17 12:29:50 +0100420 // Verify the read and write threads set the holder correctly
421 // and retrieve the expected values
Francis Murtagh68f78d82019-09-04 16:42:29 +0100422 Holder holder;
423 BOOST_CHECK((holder.GetCaptureData()).GetCapturePeriod() == 0);
424 BOOST_CHECK(((holder.GetCaptureData()).GetCounterIds()).empty());
425
426 // Check Holder functions
Francis Murtaghbd707162019-09-09 11:26:44 +0100427 std::thread thread1(CaptureDataWriteThreadImpl, std::ref(holder), 2, std::ref(periodIdMap[2]));
Francis Murtagh68f78d82019-09-04 16:42:29 +0100428 thread1.join();
Francis Murtaghbd707162019-09-09 11:26:44 +0100429 BOOST_CHECK((holder.GetCaptureData()).GetCapturePeriod() == 2);
430 BOOST_CHECK((holder.GetCaptureData()).GetCounterIds() == periodIdMap[2]);
Jim Flynn8355ec92019-09-17 12:29:50 +0100431 // NOTE: now that we have some initial values in the holder we don't have to worry
432 // in the multi-threaded section below about a read thread accessing the holder
433 // before any write thread has gotten to it so we read period = 0, counterIds empty
434 // instead of period = 0, counterIds = {0} as will the case when write thread 0
435 // has executed.
Francis Murtagh68f78d82019-09-04 16:42:29 +0100436
437 CaptureData captureData;
438 std::thread thread2(CaptureDataReadThreadImpl, std::ref(holder), std::ref(captureData));
439 thread2.join();
Jim Flynn8355ec92019-09-17 12:29:50 +0100440 BOOST_CHECK(captureData.GetCapturePeriod() == 2);
Francis Murtaghbd707162019-09-09 11:26:44 +0100441 BOOST_CHECK(captureData.GetCounterIds() == periodIdMap[2]);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100442
Jim Flynn8355ec92019-09-17 12:29:50 +0100443 std::map<uint32_t, CaptureData> captureDataIdMap;
444 for (uint32_t i = 0; i < numThreads; ++i)
445 {
446 CaptureData perThreadCaptureData;
447 captureDataIdMap.insert(std::make_pair(i, perThreadCaptureData));
448 }
449
Francis Murtaghbd707162019-09-09 11:26:44 +0100450 std::vector<std::thread> threadsVect;
Jim Flynn8355ec92019-09-17 12:29:50 +0100451 std::vector<std::thread> readThreadsVect;
452 for (uint32_t i = 0; i < numThreads; ++i)
Francis Murtaghbd707162019-09-09 11:26:44 +0100453 {
454 threadsVect.emplace_back(std::thread(CaptureDataWriteThreadImpl,
455 std::ref(holder),
456 i,
Jim Flynn8355ec92019-09-17 12:29:50 +0100457 std::ref(periodIdMap[i])));
Francis Murtagh06965692019-09-05 16:29:01 +0100458
Jim Flynn8355ec92019-09-17 12:29:50 +0100459 // Verify that the CaptureData goes into the thread in a virgin state
460 BOOST_CHECK(captureDataIdMap.at(i).GetCapturePeriod() == 0);
461 BOOST_CHECK(captureDataIdMap.at(i).GetCounterIds().empty());
462 readThreadsVect.emplace_back(std::thread(CaptureDataReadThreadImpl,
463 std::ref(holder),
464 std::ref(captureDataIdMap.at(i))));
Francis Murtaghbd707162019-09-09 11:26:44 +0100465 }
466
Jim Flynn8355ec92019-09-17 12:29:50 +0100467 for (uint32_t i = 0; i < numThreads; ++i)
Francis Murtaghbd707162019-09-09 11:26:44 +0100468 {
469 threadsVect[i].join();
Francis Murtaghbd707162019-09-09 11:26:44 +0100470 readThreadsVect[i].join();
471 }
Francis Murtagh68f78d82019-09-04 16:42:29 +0100472
Jim Flynn8355ec92019-09-17 12:29:50 +0100473 // Look at the CaptureData that each read thread has filled
474 // the capture period it read should match the counter ids entry
475 for (uint32_t i = 0; i < numThreads; ++i)
476 {
477 CaptureData perThreadCaptureData = captureDataIdMap.at(i);
478 BOOST_CHECK(perThreadCaptureData.GetCounterIds() == periodIdMap.at(perThreadCaptureData.GetCapturePeriod()));
479 }
Matthew Bentham46d1c622019-09-13 12:45:04 +0100480}
Francis Murtagh68f78d82019-09-04 16:42:29 +0100481
Matthew Bentham46d1c622019-09-13 12:45:04 +0100482BOOST_AUTO_TEST_CASE(CaptureDataMethods)
483{
Jim Flynn8355ec92019-09-17 12:29:50 +0100484 // Check CaptureData setter and getter functions
Matthew Bentham46d1c622019-09-13 12:45:04 +0100485 std::vector<uint16_t> counterIds = {42, 29, 13};
Jim Flynn8355ec92019-09-17 12:29:50 +0100486 CaptureData captureData;
487 BOOST_CHECK(captureData.GetCapturePeriod() == 0);
488 BOOST_CHECK((captureData.GetCounterIds()).empty());
489 captureData.SetCapturePeriod(150);
490 captureData.SetCounterIds(counterIds);
491 BOOST_CHECK(captureData.GetCapturePeriod() == 150);
492 BOOST_CHECK(captureData.GetCounterIds() == counterIds);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100493
Jim Flynn8355ec92019-09-17 12:29:50 +0100494 // Check assignment operator
Francis Murtagh68f78d82019-09-04 16:42:29 +0100495 CaptureData secondCaptureData;
Francis Murtagh68f78d82019-09-04 16:42:29 +0100496
Jim Flynn8355ec92019-09-17 12:29:50 +0100497 secondCaptureData = captureData;
498 BOOST_CHECK(secondCaptureData.GetCapturePeriod() == 150);
Matthew Bentham46d1c622019-09-13 12:45:04 +0100499 BOOST_CHECK(secondCaptureData.GetCounterIds() == counterIds);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100500
501 // Check copy constructor
Jim Flynn8355ec92019-09-17 12:29:50 +0100502 CaptureData copyConstructedCaptureData(captureData);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100503
Jim Flynn8355ec92019-09-17 12:29:50 +0100504 BOOST_CHECK(copyConstructedCaptureData.GetCapturePeriod() == 150);
Matthew Bentham46d1c622019-09-13 12:45:04 +0100505 BOOST_CHECK(copyConstructedCaptureData.GetCounterIds() == counterIds);
Keith Davis02356de2019-08-26 18:28:17 +0100506}
Francis Murtagh68f78d82019-09-04 16:42:29 +0100507
Keith Davis02356de2019-08-26 18:28:17 +0100508BOOST_AUTO_TEST_CASE(CheckProfilingServiceDisabled)
509{
510 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
511 ProfilingService service(options);
512 BOOST_CHECK(service.GetCurrentState() == ProfilingState::Uninitialised);
513 service.Run();
514 BOOST_CHECK(service.GetCurrentState() == ProfilingState::Uninitialised);
515}
516
Keith Davis02356de2019-08-26 18:28:17 +0100517BOOST_AUTO_TEST_CASE(CheckProfilingServiceEnabled)
518{
519 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
520 options.m_EnableProfiling = true;
521 ProfilingService service(options);
522 BOOST_CHECK(service.GetCurrentState() == ProfilingState::NotConnected);
523 service.Run();
524 BOOST_CHECK(service.GetCurrentState() == ProfilingState::WaitingForAck);
525}
526
527
528BOOST_AUTO_TEST_CASE(CheckProfilingServiceEnabledRuntime)
529{
530 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
531 ProfilingService service(options);
532 BOOST_CHECK(service.GetCurrentState() == ProfilingState::Uninitialised);
533 service.Run();
534 BOOST_CHECK(service.GetCurrentState() == ProfilingState::Uninitialised);
535 service.m_Options.m_EnableProfiling = true;
536 service.Run();
537 BOOST_CHECK(service.GetCurrentState() == ProfilingState::NotConnected);
538 service.Run();
539 BOOST_CHECK(service.GetCurrentState() == ProfilingState::WaitingForAck);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100540}
541
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100542BOOST_AUTO_TEST_CASE(CheckProfilingObjectUids)
Matteo Martincighab173e92019-09-05 12:02:04 +0100543{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100544 uint16_t uid = 0;
545 BOOST_CHECK_NO_THROW(uid = GetNextUid());
546 BOOST_CHECK(uid >= 1);
547
548 uint16_t nextUid = 0;
549 BOOST_CHECK_NO_THROW(nextUid = GetNextUid());
550 BOOST_CHECK(nextUid > uid);
551
552 std::vector<uint16_t> counterUids;
553 BOOST_CHECK_NO_THROW(counterUids = GetNextCounterUids(0));
554 BOOST_CHECK(counterUids.size() == 1);
555 BOOST_CHECK(counterUids[0] >= 0);
556
557 std::vector<uint16_t> nextCounterUids;
558 BOOST_CHECK_NO_THROW(nextCounterUids = GetNextCounterUids(1));
559 BOOST_CHECK(nextCounterUids.size() == 1);
560 BOOST_CHECK(nextCounterUids[0] > counterUids[0]);
561
562 std::vector<uint16_t> counterUidsMultiCore;
563 uint16_t numberOfCores = 13;
564 BOOST_CHECK_NO_THROW(counterUidsMultiCore = GetNextCounterUids(numberOfCores));
565 BOOST_CHECK(counterUidsMultiCore.size() == numberOfCores);
566 BOOST_CHECK(counterUidsMultiCore.front() >= nextCounterUids[0]);
567 for (size_t i = 1; i < numberOfCores; i ++)
568 {
569 BOOST_CHECK(counterUidsMultiCore[i] == counterUidsMultiCore[i - 1] + 1);
570 }
571 BOOST_CHECK(counterUidsMultiCore.back() == counterUidsMultiCore.front() + numberOfCores - 1);
Matteo Martincighab173e92019-09-05 12:02:04 +0100572}
573
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100574BOOST_AUTO_TEST_CASE(CheckCounterDirectoryRegisterCategory)
Matteo Martincighab173e92019-09-05 12:02:04 +0100575{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100576 CounterDirectory counterDirectory;
577 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
578 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
579 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
580 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
Matteo Martincighab173e92019-09-05 12:02:04 +0100581
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100582 // Register a category with an invalid name
583 const Category* noCategory = nullptr;
584 BOOST_CHECK_THROW(noCategory = counterDirectory.RegisterCategory(""), armnn::InvalidArgumentException);
585 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
586 BOOST_CHECK(!noCategory);
Matteo Martincighab173e92019-09-05 12:02:04 +0100587
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100588 // Register a category with an invalid name
589 BOOST_CHECK_THROW(noCategory = counterDirectory.RegisterCategory("invalid category"),
590 armnn::InvalidArgumentException);
591 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
592 BOOST_CHECK(!noCategory);
593
594 // Register a new category
595 const std::string categoryName = "some_category";
596 const Category* category = nullptr;
597 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
598 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
599 BOOST_CHECK(category);
600 BOOST_CHECK(category->m_Name == categoryName);
601 BOOST_CHECK(category->m_Counters.empty());
602 BOOST_CHECK(category->m_DeviceUid == 0);
603 BOOST_CHECK(category->m_CounterSetUid == 0);
604
605 // Get the registered category
606 const Category* registeredCategory = counterDirectory.GetCategory(categoryName);
607 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
608 BOOST_CHECK(registeredCategory);
609 BOOST_CHECK(registeredCategory == category);
610
611 // Try to get a category not registered
612 const Category* notRegisteredCategory = counterDirectory.GetCategory("not_registered_category");
613 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
614 BOOST_CHECK(!notRegisteredCategory);
615
616 // Register a category already registered
617 const Category* anotherCategory = nullptr;
618 BOOST_CHECK_THROW(anotherCategory = counterDirectory.RegisterCategory(categoryName),
619 armnn::InvalidArgumentException);
620 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
621 BOOST_CHECK(!anotherCategory);
622
623 // Register a device for testing
624 const std::string deviceName = "some_device";
625 const Device* device = nullptr;
626 BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName));
627 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
628 BOOST_CHECK(device);
629 BOOST_CHECK(device->m_Uid >= 1);
630 BOOST_CHECK(device->m_Name == deviceName);
631 BOOST_CHECK(device->m_Cores == 0);
632
633 // Register a new category not associated to any device
634 const std::string categoryWoDeviceName = "some_category_without_device";
635 const Category* categoryWoDevice = nullptr;
636 BOOST_CHECK_NO_THROW(categoryWoDevice = counterDirectory.RegisterCategory(categoryWoDeviceName, 0));
637 BOOST_CHECK(counterDirectory.GetCategoryCount() == 2);
638 BOOST_CHECK(categoryWoDevice);
639 BOOST_CHECK(categoryWoDevice->m_Name == categoryWoDeviceName);
640 BOOST_CHECK(categoryWoDevice->m_Counters.empty());
641 BOOST_CHECK(categoryWoDevice->m_DeviceUid == 0);
642 BOOST_CHECK(categoryWoDevice->m_CounterSetUid == 0);
643
644 // Register a new category associated to an invalid device
645 const std::string categoryWInvalidDeviceName = "some_category_with_invalid_device";
646
647 ARMNN_NO_CONVERSION_WARN_BEGIN
648 uint16_t invalidDeviceUid = device->m_Uid + 10;
649 ARMNN_NO_CONVERSION_WARN_END
650
651 const Category* categoryWInvalidDevice = nullptr;
652 BOOST_CHECK_THROW(categoryWInvalidDevice
653 = counterDirectory.RegisterCategory(categoryWInvalidDeviceName,
654 invalidDeviceUid),
655 armnn::InvalidArgumentException);
656 BOOST_CHECK(counterDirectory.GetCategoryCount() == 2);
657 BOOST_CHECK(!categoryWInvalidDevice);
658
659 // Register a new category associated to a valid device
660 const std::string categoryWValidDeviceName = "some_category_with_valid_device";
661 const Category* categoryWValidDevice = nullptr;
662 BOOST_CHECK_NO_THROW(categoryWValidDevice
663 = counterDirectory.RegisterCategory(categoryWValidDeviceName,
664 device->m_Uid));
665 BOOST_CHECK(counterDirectory.GetCategoryCount() == 3);
666 BOOST_CHECK(categoryWValidDevice);
667 BOOST_CHECK(categoryWValidDevice != category);
668 BOOST_CHECK(categoryWValidDevice->m_Name == categoryWValidDeviceName);
669 BOOST_CHECK(categoryWValidDevice->m_DeviceUid == device->m_Uid);
670 BOOST_CHECK(categoryWValidDevice->m_CounterSetUid == 0);
671
672 // Register a counter set for testing
673 const std::string counterSetName = "some_counter_set";
674 const CounterSet* counterSet = nullptr;
675 BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
676 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
677 BOOST_CHECK(counterSet);
678 BOOST_CHECK(counterSet->m_Uid >= 1);
679 BOOST_CHECK(counterSet->m_Name == counterSetName);
680 BOOST_CHECK(counterSet->m_Count == 0);
681
682 // Register a new category not associated to any counter set
683 const std::string categoryWoCounterSetName = "some_category_without_counter_set";
684 const Category* categoryWoCounterSet = nullptr;
685 BOOST_CHECK_NO_THROW(categoryWoCounterSet
686 = counterDirectory.RegisterCategory(categoryWoCounterSetName,
687 armnn::EmptyOptional(),
688 0));
689 BOOST_CHECK(counterDirectory.GetCategoryCount() == 4);
690 BOOST_CHECK(categoryWoCounterSet);
691 BOOST_CHECK(categoryWoCounterSet->m_Name == categoryWoCounterSetName);
692 BOOST_CHECK(categoryWoCounterSet->m_DeviceUid == 0);
693 BOOST_CHECK(categoryWoCounterSet->m_CounterSetUid == 0);
694
695 // Register a new category associated to an invalid counter set
696 const std::string categoryWInvalidCounterSetName = "some_category_with_invalid_counter_set";
697
698 ARMNN_NO_CONVERSION_WARN_BEGIN
699 uint16_t invalidCunterSetUid = counterSet->m_Uid + 10;
700 ARMNN_NO_CONVERSION_WARN_END
701
702 const Category* categoryWInvalidCounterSet = nullptr;
703 BOOST_CHECK_THROW(categoryWInvalidCounterSet
704 = counterDirectory.RegisterCategory(categoryWInvalidCounterSetName,
705 armnn::EmptyOptional(),
706 invalidCunterSetUid),
707 armnn::InvalidArgumentException);
708 BOOST_CHECK(counterDirectory.GetCategoryCount() == 4);
709 BOOST_CHECK(!categoryWInvalidCounterSet);
710
711 // Register a new category associated to a valid counter set
712 const std::string categoryWValidCounterSetName = "some_category_with_valid_counter_set";
713 const Category* categoryWValidCounterSet = nullptr;
714 BOOST_CHECK_NO_THROW(categoryWValidCounterSet
715 = counterDirectory.RegisterCategory(categoryWValidCounterSetName,
716 armnn::EmptyOptional(),
717 counterSet->m_Uid));
718 BOOST_CHECK(counterDirectory.GetCategoryCount() == 5);
719 BOOST_CHECK(categoryWValidCounterSet);
720 BOOST_CHECK(categoryWValidCounterSet != category);
721 BOOST_CHECK(categoryWValidCounterSet->m_Name == categoryWValidCounterSetName);
722 BOOST_CHECK(categoryWValidCounterSet->m_DeviceUid == 0);
723 BOOST_CHECK(categoryWValidCounterSet->m_CounterSetUid == counterSet->m_Uid);
724
725 // Register a new category associated to a valid device and counter set
726 const std::string categoryWValidDeviceAndValidCounterSetName = "some_category_with_valid_device_and_counter_set";
727 const Category* categoryWValidDeviceAndValidCounterSet = nullptr;
728 BOOST_CHECK_NO_THROW(categoryWValidDeviceAndValidCounterSet
729 = counterDirectory.RegisterCategory(categoryWValidDeviceAndValidCounterSetName,
730 device->m_Uid,
731 counterSet->m_Uid));
732 BOOST_CHECK(counterDirectory.GetCategoryCount() == 6);
733 BOOST_CHECK(categoryWValidDeviceAndValidCounterSet);
734 BOOST_CHECK(categoryWValidDeviceAndValidCounterSet != category);
735 BOOST_CHECK(categoryWValidDeviceAndValidCounterSet->m_Name == categoryWValidDeviceAndValidCounterSetName);
736 BOOST_CHECK(categoryWValidDeviceAndValidCounterSet->m_DeviceUid == device->m_Uid);
737 BOOST_CHECK(categoryWValidDeviceAndValidCounterSet->m_CounterSetUid == counterSet->m_Uid);
738}
739
740BOOST_AUTO_TEST_CASE(CheckCounterDirectoryRegisterDevice)
741{
742 CounterDirectory counterDirectory;
743 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
744 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
745 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
746 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
747
748 // Register a device with an invalid name
749 const Device* noDevice = nullptr;
750 BOOST_CHECK_THROW(noDevice = counterDirectory.RegisterDevice(""), armnn::InvalidArgumentException);
751 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
752 BOOST_CHECK(!noDevice);
753
754 // Register a device with an invalid name
755 BOOST_CHECK_THROW(noDevice = counterDirectory.RegisterDevice("inv@lid nam€"), armnn::InvalidArgumentException);
756 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
757 BOOST_CHECK(!noDevice);
758
759 // Register a new device with no cores or parent category
760 const std::string deviceName = "some_device";
761 const Device* device = nullptr;
762 BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName));
763 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
764 BOOST_CHECK(device);
765 BOOST_CHECK(device->m_Name == deviceName);
766 BOOST_CHECK(device->m_Uid >= 1);
767 BOOST_CHECK(device->m_Cores == 0);
768
Matteo Martincigh657ab2d2019-09-18 10:53:24 +0100769 // Try getting an unregistered device
770 const Device* unregisteredDevice = counterDirectory.GetDevice(9999);
771 BOOST_CHECK(!unregisteredDevice);
772
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100773 // Get the registered device
774 const Device* registeredDevice = counterDirectory.GetDevice(device->m_Uid);
775 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
776 BOOST_CHECK(registeredDevice);
777 BOOST_CHECK(registeredDevice == device);
778
Matteo Martincigh657ab2d2019-09-18 10:53:24 +0100779 // Register a device with the name of a device already registered
780 const Device* deviceSameName = nullptr;
781 BOOST_CHECK_THROW(deviceSameName = counterDirectory.RegisterDevice(deviceName), armnn::InvalidArgumentException);
782 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
783 BOOST_CHECK(!deviceSameName);
784
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100785 // Register a new device with cores and no parent category
786 const std::string deviceWCoresName = "some_device_with_cores";
787 const Device* deviceWCores = nullptr;
788 BOOST_CHECK_NO_THROW(deviceWCores = counterDirectory.RegisterDevice(deviceWCoresName, 2));
789 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
790 BOOST_CHECK(deviceWCores);
791 BOOST_CHECK(deviceWCores->m_Name == deviceWCoresName);
792 BOOST_CHECK(deviceWCores->m_Uid >= 1);
793 BOOST_CHECK(deviceWCores->m_Uid > device->m_Uid);
794 BOOST_CHECK(deviceWCores->m_Cores == 2);
795
796 // Get the registered device
797 const Device* registeredDeviceWCores = counterDirectory.GetDevice(deviceWCores->m_Uid);
798 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
799 BOOST_CHECK(registeredDeviceWCores);
800 BOOST_CHECK(registeredDeviceWCores == deviceWCores);
801 BOOST_CHECK(registeredDeviceWCores != device);
802
803 // Register a new device with cores and invalid parent category
804 const std::string deviceWCoresWInvalidParentCategoryName = "some_device_with_cores_with_invalid_parent_category";
805 const Device* deviceWCoresWInvalidParentCategory = nullptr;
806 BOOST_CHECK_THROW(deviceWCoresWInvalidParentCategory
807 = counterDirectory.RegisterDevice(deviceWCoresWInvalidParentCategoryName,
808 3,
809 std::string("")),
810 armnn::InvalidArgumentException);
811 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
812 BOOST_CHECK(!deviceWCoresWInvalidParentCategory);
813
814 // Register a new device with cores and invalid parent category
815 const std::string deviceWCoresWInvalidParentCategoryName2 = "some_device_with_cores_with_invalid_parent_category2";
816 const Device* deviceWCoresWInvalidParentCategory2 = nullptr;
817 BOOST_CHECK_THROW(deviceWCoresWInvalidParentCategory2
818 = counterDirectory.RegisterDevice(deviceWCoresWInvalidParentCategoryName2,
819 3,
820 std::string("invalid_parent_category")),
821 armnn::InvalidArgumentException);
822 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
823 BOOST_CHECK(!deviceWCoresWInvalidParentCategory2);
824
825 // Register a category for testing
826 const std::string categoryName = "some_category";
827 const Category* category = nullptr;
828 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
829 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
830 BOOST_CHECK(category);
831 BOOST_CHECK(category->m_Name == categoryName);
832 BOOST_CHECK(category->m_Counters.empty());
833 BOOST_CHECK(category->m_DeviceUid == 0);
834 BOOST_CHECK(category->m_CounterSetUid == 0);
835
836 // Register a new device with cores and valid parent category
837 const std::string deviceWCoresWValidParentCategoryName = "some_device_with_cores_with_valid_parent_category";
838 const Device* deviceWCoresWValidParentCategory = nullptr;
839 BOOST_CHECK_NO_THROW(deviceWCoresWValidParentCategory
840 = counterDirectory.RegisterDevice(deviceWCoresWValidParentCategoryName,
841 4,
842 categoryName));
843 BOOST_CHECK(counterDirectory.GetDeviceCount() == 3);
844 BOOST_CHECK(deviceWCoresWValidParentCategory);
845 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Name == deviceWCoresWValidParentCategoryName);
846 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Uid >= 1);
847 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Uid > device->m_Uid);
848 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Uid > deviceWCores->m_Uid);
849 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Cores == 4);
850 BOOST_CHECK(category->m_DeviceUid == deviceWCoresWValidParentCategory->m_Uid);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +0100851
852 // Register a device associated to a category already associated to a different device
853 const std::string deviceSameCategoryName = "some_device_with_invalid_parent_category";
854 const Device* deviceSameCategory = nullptr;
855 BOOST_CHECK_THROW(deviceSameCategory = counterDirectory.RegisterDevice(deviceSameCategoryName, 0, categoryName),
856 armnn::InvalidArgumentException);
857 BOOST_CHECK(counterDirectory.GetDeviceCount() == 3);
858 BOOST_CHECK(!deviceSameCategory);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100859}
860
861BOOST_AUTO_TEST_CASE(CheckCounterDirectoryRegisterCounterSet)
862{
863 CounterDirectory counterDirectory;
864 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
865 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
866 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
867 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
868
869 // Register a counter set with an invalid name
870 const CounterSet* noCounterSet = nullptr;
871 BOOST_CHECK_THROW(noCounterSet = counterDirectory.RegisterCounterSet(""), armnn::InvalidArgumentException);
872 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
873 BOOST_CHECK(!noCounterSet);
874
875 // Register a counter set with an invalid name
876 BOOST_CHECK_THROW(noCounterSet = counterDirectory.RegisterCounterSet("invalid name"),
877 armnn::InvalidArgumentException);
878 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
879 BOOST_CHECK(!noCounterSet);
880
881 // Register a new counter set with no count or parent category
882 const std::string counterSetName = "some_counter_set";
883 const CounterSet* counterSet = nullptr;
884 BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
885 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
886 BOOST_CHECK(counterSet);
887 BOOST_CHECK(counterSet->m_Name == counterSetName);
888 BOOST_CHECK(counterSet->m_Uid >= 1);
889 BOOST_CHECK(counterSet->m_Count == 0);
890
Matteo Martincigh657ab2d2019-09-18 10:53:24 +0100891 // Try getting an unregistered counter set
892 const CounterSet* unregisteredCounterSet = counterDirectory.GetCounterSet(9999);
893 BOOST_CHECK(!unregisteredCounterSet);
894
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100895 // Get the registered counter set
896 const CounterSet* registeredCounterSet = counterDirectory.GetCounterSet(counterSet->m_Uid);
897 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
898 BOOST_CHECK(registeredCounterSet);
899 BOOST_CHECK(registeredCounterSet == counterSet);
900
Matteo Martincigh657ab2d2019-09-18 10:53:24 +0100901 // Register a counter set with the name of a counter set already registered
902 const CounterSet* counterSetSameName = nullptr;
903 BOOST_CHECK_THROW(counterSetSameName = counterDirectory.RegisterCounterSet(counterSetName),
904 armnn::InvalidArgumentException);
905 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
906 BOOST_CHECK(!counterSetSameName);
907
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100908 // Register a new counter set with count and no parent category
909 const std::string counterSetWCountName = "some_counter_set_with_count";
910 const CounterSet* counterSetWCount = nullptr;
911 BOOST_CHECK_NO_THROW(counterSetWCount = counterDirectory.RegisterCounterSet(counterSetWCountName, 37));
912 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 2);
913 BOOST_CHECK(counterSetWCount);
914 BOOST_CHECK(counterSetWCount->m_Name == counterSetWCountName);
915 BOOST_CHECK(counterSetWCount->m_Uid >= 1);
916 BOOST_CHECK(counterSetWCount->m_Uid > counterSet->m_Uid);
917 BOOST_CHECK(counterSetWCount->m_Count == 37);
918
919 // Get the registered counter set
920 const CounterSet* registeredCounterSetWCount = counterDirectory.GetCounterSet(counterSetWCount->m_Uid);
921 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 2);
922 BOOST_CHECK(registeredCounterSetWCount);
923 BOOST_CHECK(registeredCounterSetWCount == counterSetWCount);
924 BOOST_CHECK(registeredCounterSetWCount != counterSet);
925
926 // Register a new counter set with count and invalid parent category
927 const std::string counterSetWCountWInvalidParentCategoryName = "some_counter_set_with_count_"
928 "with_invalid_parent_category";
929 const CounterSet* counterSetWCountWInvalidParentCategory = nullptr;
930 BOOST_CHECK_THROW(counterSetWCountWInvalidParentCategory
931 = counterDirectory.RegisterCounterSet(counterSetWCountWInvalidParentCategoryName,
932 42,
933 std::string("")),
934 armnn::InvalidArgumentException);
935 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 2);
936 BOOST_CHECK(!counterSetWCountWInvalidParentCategory);
937
938 // Register a new counter set with count and invalid parent category
939 const std::string counterSetWCountWInvalidParentCategoryName2 = "some_counter_set_with_count_"
940 "with_invalid_parent_category2";
941 const CounterSet* counterSetWCountWInvalidParentCategory2 = nullptr;
942 BOOST_CHECK_THROW(counterSetWCountWInvalidParentCategory2
943 = counterDirectory.RegisterCounterSet(counterSetWCountWInvalidParentCategoryName2,
944 42,
945 std::string("invalid_parent_category")),
946 armnn::InvalidArgumentException);
947 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 2);
948 BOOST_CHECK(!counterSetWCountWInvalidParentCategory2);
949
950 // Register a category for testing
951 const std::string categoryName = "some_category";
952 const Category* category = nullptr;
953 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
954 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
955 BOOST_CHECK(category);
956 BOOST_CHECK(category->m_Name == categoryName);
957 BOOST_CHECK(category->m_Counters.empty());
958 BOOST_CHECK(category->m_DeviceUid == 0);
959 BOOST_CHECK(category->m_CounterSetUid == 0);
960
961 // Register a new counter set with count and valid parent category
962 const std::string counterSetWCountWValidParentCategoryName = "some_counter_set_with_count_"
963 "with_valid_parent_category";
964 const CounterSet* counterSetWCountWValidParentCategory = nullptr;
965 BOOST_CHECK_NO_THROW(counterSetWCountWValidParentCategory
966 = counterDirectory.RegisterCounterSet(counterSetWCountWValidParentCategoryName,
967 42,
Matteo Martincigh657ab2d2019-09-18 10:53:24 +0100968 categoryName));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100969 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 3);
970 BOOST_CHECK(counterSetWCountWValidParentCategory);
971 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Name == counterSetWCountWValidParentCategoryName);
972 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Uid >= 1);
973 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Uid > counterSet->m_Uid);
974 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Uid > counterSetWCount->m_Uid);
975 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Count == 42);
976 BOOST_CHECK(category->m_CounterSetUid == counterSetWCountWValidParentCategory->m_Uid);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +0100977
978 // Register a counter set associated to a category already associated to a different counter set
979 const std::string counterSetSameCategoryName = "some_counter_set_with_invalid_parent_category";
980 const CounterSet* counterSetSameCategory = nullptr;
981 BOOST_CHECK_THROW(counterSetSameCategory = counterDirectory.RegisterCounterSet(counterSetSameCategoryName,
982 0,
983 categoryName),
984 armnn::InvalidArgumentException);
985 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 3);
986 BOOST_CHECK(!counterSetSameCategory);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100987}
988
989BOOST_AUTO_TEST_CASE(CheckCounterDirectoryRegisterCounter)
990{
991 CounterDirectory counterDirectory;
992 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
993 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
994 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
995 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
996
997 // Register a counter with an invalid parent category name
998 const Counter* noCounter = nullptr;
999 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("",
1000 0,
1001 1,
1002 123.45f,
1003 "valid name",
1004 "valid description"),
1005 armnn::InvalidArgumentException);
1006 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1007 BOOST_CHECK(!noCounter);
1008
1009 // Register a counter with an invalid parent category name
1010 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("invalid parent category",
1011 0,
1012 1,
1013 123.45f,
1014 "valid name",
1015 "valid description"),
1016 armnn::InvalidArgumentException);
1017 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1018 BOOST_CHECK(!noCounter);
1019
1020 // Register a counter with an invalid class
1021 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("valid_parent_category",
1022 2,
1023 1,
1024 123.45f,
1025 "valid name",
1026 "valid description"),
1027 armnn::InvalidArgumentException);
1028 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1029 BOOST_CHECK(!noCounter);
1030
1031 // Register a counter with an invalid interpolation
1032 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("valid_parent_category",
1033 0,
1034 3,
1035 123.45f,
1036 "valid name",
1037 "valid description"),
1038 armnn::InvalidArgumentException);
1039 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1040 BOOST_CHECK(!noCounter);
1041
1042 // Register a counter with an invalid multiplier
1043 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("valid_parent_category",
1044 0,
1045 1,
1046 .0f,
1047 "valid name",
1048 "valid description"),
1049 armnn::InvalidArgumentException);
1050 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1051 BOOST_CHECK(!noCounter);
1052
1053 // Register a counter with an invalid name
1054 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("valid_parent_category",
1055 0,
1056 1,
1057 123.45f,
1058 "",
1059 "valid description"),
1060 armnn::InvalidArgumentException);
1061 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1062 BOOST_CHECK(!noCounter);
1063
1064 // Register a counter with an invalid name
1065 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("valid_parent_category",
1066 0,
1067 1,
1068 123.45f,
1069 "invalid nam€",
1070 "valid description"),
1071 armnn::InvalidArgumentException);
1072 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1073 BOOST_CHECK(!noCounter);
1074
1075 // Register a counter with an invalid description
1076 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("valid_parent_category",
1077 0,
1078 1,
1079 123.45f,
1080 "valid name",
1081 ""),
1082 armnn::InvalidArgumentException);
1083 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1084 BOOST_CHECK(!noCounter);
1085
1086 // Register a counter with an invalid description
1087 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("valid_parent_category",
1088 0,
1089 1,
1090 123.45f,
1091 "valid name",
1092 "inv@lid description"),
1093 armnn::InvalidArgumentException);
1094 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1095 BOOST_CHECK(!noCounter);
1096
1097 // Register a counter with an invalid unit2
1098 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("valid_parent_category",
1099 0,
1100 1,
1101 123.45f,
1102 "valid name",
1103 "valid description",
1104 std::string("Mb/s2")),
1105 armnn::InvalidArgumentException);
1106 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1107 BOOST_CHECK(!noCounter);
1108
1109 // Register a counter with a non-existing parent category name
1110 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("invalid_parent_category",
1111 0,
1112 1,
1113 123.45f,
1114 "valid name",
1115 "valid description"),
1116 armnn::InvalidArgumentException);
1117 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1118 BOOST_CHECK(!noCounter);
1119
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001120 // Try getting an unregistered counter
1121 const Counter* unregisteredCounter = counterDirectory.GetCounter(9999);
1122 BOOST_CHECK(!unregisteredCounter);
1123
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001124 // Register a category for testing
1125 const std::string categoryName = "some_category";
1126 const Category* category = nullptr;
1127 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
1128 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
1129 BOOST_CHECK(category);
1130 BOOST_CHECK(category->m_Name == categoryName);
1131 BOOST_CHECK(category->m_Counters.empty());
1132 BOOST_CHECK(category->m_DeviceUid == 0);
1133 BOOST_CHECK(category->m_CounterSetUid == 0);
1134
1135 // Register a counter with a valid parent category name
1136 const Counter* counter = nullptr;
1137 BOOST_CHECK_NO_THROW(counter = counterDirectory.RegisterCounter(categoryName,
1138 0,
1139 1,
1140 123.45f,
1141 "valid name",
1142 "valid description"));
1143 BOOST_CHECK(counterDirectory.GetCounterCount() == 1);
1144 BOOST_CHECK(counter);
1145 BOOST_CHECK(counter->m_Uid >= 0);
1146 BOOST_CHECK(counter->m_MaxCounterUid == counter->m_Uid);
1147 BOOST_CHECK(counter->m_Class == 0);
1148 BOOST_CHECK(counter->m_Interpolation == 1);
1149 BOOST_CHECK(counter->m_Multiplier == 123.45f);
1150 BOOST_CHECK(counter->m_Name == "valid name");
1151 BOOST_CHECK(counter->m_Description == "valid description");
1152 BOOST_CHECK(counter->m_Units == "");
1153 BOOST_CHECK(counter->m_DeviceUid == 0);
1154 BOOST_CHECK(counter->m_CounterSetUid == 0);
1155 BOOST_CHECK(category->m_Counters.size() == 1);
1156 BOOST_CHECK(category->m_Counters.back() == counter->m_Uid);
1157
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001158 // Register a counter with a name of a counter already registered for the given parent category name
1159 const Counter* counterSameName = nullptr;
1160 BOOST_CHECK_THROW(counterSameName = counterDirectory.RegisterCounter(categoryName,
1161 0,
1162 0,
1163 1.0f,
1164 "valid name",
1165 "valid description"),
1166 armnn::InvalidArgumentException);
1167 BOOST_CHECK(counterDirectory.GetCounterCount() == 1);
1168 BOOST_CHECK(!counterSameName);
1169
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001170 // Register a counter with a valid parent category name and units
1171 const Counter* counterWUnits = nullptr;
1172 BOOST_CHECK_NO_THROW(counterWUnits = counterDirectory.RegisterCounter(categoryName,
1173 0,
1174 1,
1175 123.45f,
1176 "valid name 2",
1177 "valid description",
1178 std::string("Mnnsq2"))); // Units
1179 BOOST_CHECK(counterDirectory.GetCounterCount() == 2);
1180 BOOST_CHECK(counterWUnits);
1181 BOOST_CHECK(counterWUnits->m_Uid >= 0);
1182 BOOST_CHECK(counterWUnits->m_Uid > counter->m_Uid);
1183 BOOST_CHECK(counterWUnits->m_MaxCounterUid == counterWUnits->m_Uid);
1184 BOOST_CHECK(counterWUnits->m_Class == 0);
1185 BOOST_CHECK(counterWUnits->m_Interpolation == 1);
1186 BOOST_CHECK(counterWUnits->m_Multiplier == 123.45f);
1187 BOOST_CHECK(counterWUnits->m_Name == "valid name 2");
1188 BOOST_CHECK(counterWUnits->m_Description == "valid description");
1189 BOOST_CHECK(counterWUnits->m_Units == "Mnnsq2");
1190 BOOST_CHECK(counterWUnits->m_DeviceUid == 0);
1191 BOOST_CHECK(counterWUnits->m_CounterSetUid == 0);
1192 BOOST_CHECK(category->m_Counters.size() == 2);
1193 BOOST_CHECK(category->m_Counters.back() == counterWUnits->m_Uid);
1194
1195 // Register a counter with a valid parent category name and not associated with a device
1196 const Counter* counterWoDevice = nullptr;
1197 BOOST_CHECK_NO_THROW(counterWoDevice = counterDirectory.RegisterCounter(categoryName,
1198 0,
1199 1,
1200 123.45f,
1201 "valid name 3",
1202 "valid description",
1203 armnn::EmptyOptional(), // Units
1204 armnn::EmptyOptional(), // Number of cores
1205 0)); // Device UID
1206 BOOST_CHECK(counterDirectory.GetCounterCount() == 3);
1207 BOOST_CHECK(counterWoDevice);
1208 BOOST_CHECK(counterWoDevice->m_Uid >= 0);
1209 BOOST_CHECK(counterWoDevice->m_Uid > counter->m_Uid);
1210 BOOST_CHECK(counterWoDevice->m_MaxCounterUid == counterWoDevice->m_Uid);
1211 BOOST_CHECK(counterWoDevice->m_Class == 0);
1212 BOOST_CHECK(counterWoDevice->m_Interpolation == 1);
1213 BOOST_CHECK(counterWoDevice->m_Multiplier == 123.45f);
1214 BOOST_CHECK(counterWoDevice->m_Name == "valid name 3");
1215 BOOST_CHECK(counterWoDevice->m_Description == "valid description");
1216 BOOST_CHECK(counterWoDevice->m_Units == "");
1217 BOOST_CHECK(counterWoDevice->m_DeviceUid == 0);
1218 BOOST_CHECK(counterWoDevice->m_CounterSetUid == 0);
1219 BOOST_CHECK(category->m_Counters.size() == 3);
1220 BOOST_CHECK(category->m_Counters.back() == counterWoDevice->m_Uid);
1221
1222 // Register a counter with a valid parent category name and associated to an invalid device
1223 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(categoryName,
1224 0,
1225 1,
1226 123.45f,
1227 "valid name 4",
1228 "valid description",
1229 armnn::EmptyOptional(), // Units
1230 armnn::EmptyOptional(), // Number of cores
1231 100), // Device UID
1232 armnn::InvalidArgumentException);
1233 BOOST_CHECK(counterDirectory.GetCounterCount() == 3);
1234 BOOST_CHECK(!noCounter);
1235
1236 // Register a device for testing
1237 const std::string deviceName = "some_device";
1238 const Device* device = nullptr;
1239 BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName));
1240 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
1241 BOOST_CHECK(device);
1242 BOOST_CHECK(device->m_Name == deviceName);
1243 BOOST_CHECK(device->m_Uid >= 1);
1244 BOOST_CHECK(device->m_Cores == 0);
1245
1246 // Register a counter with a valid parent category name and associated to a device
1247 const Counter* counterWDevice = nullptr;
1248 BOOST_CHECK_NO_THROW(counterWDevice = counterDirectory.RegisterCounter(categoryName,
1249 0,
1250 1,
1251 123.45f,
1252 "valid name 5",
1253 "valid description",
1254 armnn::EmptyOptional(), // Units
1255 armnn::EmptyOptional(), // Number of cores
1256 device->m_Uid)); // Device UID
1257 BOOST_CHECK(counterDirectory.GetCounterCount() == 4);
1258 BOOST_CHECK(counterWDevice);
1259 BOOST_CHECK(counterWDevice->m_Uid >= 0);
1260 BOOST_CHECK(counterWDevice->m_Uid > counter->m_Uid);
1261 BOOST_CHECK(counterWDevice->m_MaxCounterUid == counterWDevice->m_Uid);
1262 BOOST_CHECK(counterWDevice->m_Class == 0);
1263 BOOST_CHECK(counterWDevice->m_Interpolation == 1);
1264 BOOST_CHECK(counterWDevice->m_Multiplier == 123.45f);
1265 BOOST_CHECK(counterWDevice->m_Name == "valid name 5");
1266 BOOST_CHECK(counterWDevice->m_Description == "valid description");
1267 BOOST_CHECK(counterWDevice->m_Units == "");
1268 BOOST_CHECK(counterWDevice->m_DeviceUid == device->m_Uid);
1269 BOOST_CHECK(counterWDevice->m_CounterSetUid == 0);
1270 BOOST_CHECK(category->m_Counters.size() == 4);
1271 BOOST_CHECK(category->m_Counters.back() == counterWDevice->m_Uid);
1272
1273 // Register a counter with a valid parent category name and not associated with a counter set
1274 const Counter* counterWoCounterSet = nullptr;
1275 BOOST_CHECK_NO_THROW(counterWoCounterSet
1276 = counterDirectory.RegisterCounter(categoryName,
1277 0,
1278 1,
1279 123.45f,
1280 "valid name 6",
1281 "valid description",
1282 armnn::EmptyOptional(), // Units
1283 armnn::EmptyOptional(), // Number of cores
1284 armnn::EmptyOptional(), // Device UID
1285 0)); // Counter set UID
1286 BOOST_CHECK(counterDirectory.GetCounterCount() == 5);
1287 BOOST_CHECK(counterWoCounterSet);
1288 BOOST_CHECK(counterWoCounterSet->m_Uid >= 0);
1289 BOOST_CHECK(counterWoCounterSet->m_Uid > counter->m_Uid);
1290 BOOST_CHECK(counterWoCounterSet->m_MaxCounterUid == counterWoCounterSet->m_Uid);
1291 BOOST_CHECK(counterWoCounterSet->m_Class == 0);
1292 BOOST_CHECK(counterWoCounterSet->m_Interpolation == 1);
1293 BOOST_CHECK(counterWoCounterSet->m_Multiplier == 123.45f);
1294 BOOST_CHECK(counterWoCounterSet->m_Name == "valid name 6");
1295 BOOST_CHECK(counterWoCounterSet->m_Description == "valid description");
1296 BOOST_CHECK(counterWoCounterSet->m_Units == "");
1297 BOOST_CHECK(counterWoCounterSet->m_DeviceUid == 0);
1298 BOOST_CHECK(counterWoCounterSet->m_CounterSetUid == 0);
1299 BOOST_CHECK(category->m_Counters.size() == 5);
1300 BOOST_CHECK(category->m_Counters.back() == counterWoCounterSet->m_Uid);
1301
1302 // Register a counter with a valid parent category name and associated to an invalid counter set
1303 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(categoryName,
1304 0,
1305 1,
1306 123.45f,
1307 "valid name 7",
1308 "valid description",
1309 armnn::EmptyOptional(), // Units
1310 armnn::EmptyOptional(), // Number of cores
1311 armnn::EmptyOptional(), // Device UID
1312 100), // Counter set UID
1313 armnn::InvalidArgumentException);
1314 BOOST_CHECK(counterDirectory.GetCounterCount() == 5);
1315 BOOST_CHECK(!noCounter);
1316
1317 // Register a counter with a valid parent category name and with a given number of cores
1318 const Counter* counterWNumberOfCores = nullptr;
1319 uint16_t numberOfCores = 15;
1320 BOOST_CHECK_NO_THROW(counterWNumberOfCores
1321 = counterDirectory.RegisterCounter(categoryName,
1322 0,
1323 1,
1324 123.45f,
1325 "valid name 8",
1326 "valid description",
1327 armnn::EmptyOptional(), // Units
1328 numberOfCores, // Number of cores
1329 armnn::EmptyOptional(), // Device UID
1330 armnn::EmptyOptional())); // Counter set UID
1331 BOOST_CHECK(counterDirectory.GetCounterCount() == 20);
1332 BOOST_CHECK(counterWNumberOfCores);
1333 BOOST_CHECK(counterWNumberOfCores->m_Uid >= 0);
1334 BOOST_CHECK(counterWNumberOfCores->m_Uid > counter->m_Uid);
1335 BOOST_CHECK(counterWNumberOfCores->m_MaxCounterUid == counterWNumberOfCores->m_Uid + numberOfCores - 1);
1336 BOOST_CHECK(counterWNumberOfCores->m_Class == 0);
1337 BOOST_CHECK(counterWNumberOfCores->m_Interpolation == 1);
1338 BOOST_CHECK(counterWNumberOfCores->m_Multiplier == 123.45f);
1339 BOOST_CHECK(counterWNumberOfCores->m_Name == "valid name 8");
1340 BOOST_CHECK(counterWNumberOfCores->m_Description == "valid description");
1341 BOOST_CHECK(counterWNumberOfCores->m_Units == "");
1342 BOOST_CHECK(counterWNumberOfCores->m_DeviceUid == 0);
1343 BOOST_CHECK(counterWNumberOfCores->m_CounterSetUid == 0);
1344 BOOST_CHECK(category->m_Counters.size() == 20);
1345 for (size_t i = 0; i < numberOfCores; i ++)
1346 {
1347 BOOST_CHECK(category->m_Counters[category->m_Counters.size() - numberOfCores + i] ==
1348 counterWNumberOfCores->m_Uid + i);
1349 }
1350
1351 // Register a multi-core device for testing
1352 const std::string multiCoreDeviceName = "some_multi_core_device";
1353 const Device* multiCoreDevice = nullptr;
1354 BOOST_CHECK_NO_THROW(multiCoreDevice = counterDirectory.RegisterDevice(multiCoreDeviceName, 4));
1355 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
1356 BOOST_CHECK(multiCoreDevice);
1357 BOOST_CHECK(multiCoreDevice->m_Name == multiCoreDeviceName);
1358 BOOST_CHECK(multiCoreDevice->m_Uid >= 1);
1359 BOOST_CHECK(multiCoreDevice->m_Cores == 4);
1360
1361 // Register a counter with a valid parent category name and associated to the multi-core device
1362 const Counter* counterWMultiCoreDevice = nullptr;
1363 BOOST_CHECK_NO_THROW(counterWMultiCoreDevice
1364 = counterDirectory.RegisterCounter(categoryName,
1365 0,
1366 1,
1367 123.45f,
1368 "valid name 9",
1369 "valid description",
1370 armnn::EmptyOptional(), // Units
1371 armnn::EmptyOptional(), // Number of cores
1372 multiCoreDevice->m_Uid, // Device UID
1373 armnn::EmptyOptional())); // Counter set UID
1374 BOOST_CHECK(counterDirectory.GetCounterCount() == 24);
1375 BOOST_CHECK(counterWMultiCoreDevice);
1376 BOOST_CHECK(counterWMultiCoreDevice->m_Uid >= 0);
1377 BOOST_CHECK(counterWMultiCoreDevice->m_Uid > counter->m_Uid);
1378 BOOST_CHECK(counterWMultiCoreDevice->m_MaxCounterUid ==
1379 counterWMultiCoreDevice->m_Uid + multiCoreDevice->m_Cores - 1);
1380 BOOST_CHECK(counterWMultiCoreDevice->m_Class == 0);
1381 BOOST_CHECK(counterWMultiCoreDevice->m_Interpolation == 1);
1382 BOOST_CHECK(counterWMultiCoreDevice->m_Multiplier == 123.45f);
1383 BOOST_CHECK(counterWMultiCoreDevice->m_Name == "valid name 9");
1384 BOOST_CHECK(counterWMultiCoreDevice->m_Description == "valid description");
1385 BOOST_CHECK(counterWMultiCoreDevice->m_Units == "");
1386 BOOST_CHECK(counterWMultiCoreDevice->m_DeviceUid == multiCoreDevice->m_Uid);
1387 BOOST_CHECK(counterWMultiCoreDevice->m_CounterSetUid == 0);
1388 BOOST_CHECK(category->m_Counters.size() == 24);
1389 for (size_t i = 0; i < 4; i ++)
1390 {
1391 BOOST_CHECK(category->m_Counters[category->m_Counters.size() - 4 + i] == counterWMultiCoreDevice->m_Uid + i);
1392 }
1393
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001394 // Register a multi-core device associate to a parent category for testing
1395 const std::string multiCoreDeviceNameWParentCategory = "some_multi_core_device_with_parent_category";
1396 const Device* multiCoreDeviceWParentCategory = nullptr;
1397 BOOST_CHECK_NO_THROW(multiCoreDeviceWParentCategory =
1398 counterDirectory.RegisterDevice(multiCoreDeviceNameWParentCategory, 2, categoryName));
1399 BOOST_CHECK(counterDirectory.GetDeviceCount() == 3);
1400 BOOST_CHECK(multiCoreDeviceWParentCategory);
1401 BOOST_CHECK(multiCoreDeviceWParentCategory->m_Name == multiCoreDeviceNameWParentCategory);
1402 BOOST_CHECK(multiCoreDeviceWParentCategory->m_Uid >= 1);
1403 BOOST_CHECK(multiCoreDeviceWParentCategory->m_Cores == 2);
1404
1405 // Register a counter with a valid parent category name and getting the number of cores of the multi-core device
1406 // associated to that category
1407 const Counter* counterWMultiCoreDeviceWParentCategory = nullptr;
1408 BOOST_CHECK_NO_THROW(counterWMultiCoreDeviceWParentCategory
1409 = counterDirectory.RegisterCounter(categoryName,
1410 0,
1411 1,
1412 123.45f,
1413 "valid name 10",
1414 "valid description",
1415 armnn::EmptyOptional(), // Units
1416 armnn::EmptyOptional(), // Number of cores
1417 armnn::EmptyOptional(), // Device UID
1418 armnn::EmptyOptional())); // Counter set UID
1419 BOOST_CHECK(counterDirectory.GetCounterCount() == 26);
1420 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory);
1421 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Uid >= 0);
1422 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Uid > counter->m_Uid);
1423 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_MaxCounterUid ==
1424 counterWMultiCoreDeviceWParentCategory->m_Uid + multiCoreDeviceWParentCategory->m_Cores - 1);
1425 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Class == 0);
1426 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Interpolation == 1);
1427 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Multiplier == 123.45f);
1428 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Name == "valid name 10");
1429 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Description == "valid description");
1430 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Units == "");
1431 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_DeviceUid == 0);
1432 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_CounterSetUid == 0);
1433 BOOST_CHECK(category->m_Counters.size() == 26);
1434 for (size_t i = 0; i < 2; i ++)
1435 {
1436 BOOST_CHECK(category->m_Counters[category->m_Counters.size() - 2 + i] ==
1437 counterWMultiCoreDeviceWParentCategory->m_Uid + i);
1438 }
1439
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001440 // Register a counter set for testing
1441 const std::string counterSetName = "some_counter_set";
1442 const CounterSet* counterSet = nullptr;
1443 BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
1444 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1445 BOOST_CHECK(counterSet);
1446 BOOST_CHECK(counterSet->m_Name == counterSetName);
1447 BOOST_CHECK(counterSet->m_Uid >= 1);
1448 BOOST_CHECK(counterSet->m_Count == 0);
1449
1450 // Register a counter with a valid parent category name and associated to a counter set
1451 const Counter* counterWCounterSet = nullptr;
1452 BOOST_CHECK_NO_THROW(counterWCounterSet
1453 = counterDirectory.RegisterCounter(categoryName,
1454 0,
1455 1,
1456 123.45f,
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001457 "valid name 11",
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001458 "valid description",
1459 armnn::EmptyOptional(), // Units
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001460 0, // Number of cores
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001461 armnn::EmptyOptional(), // Device UID
1462 counterSet->m_Uid)); // Counter set UID
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001463 BOOST_CHECK(counterDirectory.GetCounterCount() == 27);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001464 BOOST_CHECK(counterWCounterSet);
1465 BOOST_CHECK(counterWCounterSet->m_Uid >= 0);
1466 BOOST_CHECK(counterWCounterSet->m_Uid > counter->m_Uid);
1467 BOOST_CHECK(counterWCounterSet->m_MaxCounterUid == counterWCounterSet->m_Uid);
1468 BOOST_CHECK(counterWCounterSet->m_Class == 0);
1469 BOOST_CHECK(counterWCounterSet->m_Interpolation == 1);
1470 BOOST_CHECK(counterWCounterSet->m_Multiplier == 123.45f);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001471 BOOST_CHECK(counterWCounterSet->m_Name == "valid name 11");
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001472 BOOST_CHECK(counterWCounterSet->m_Description == "valid description");
1473 BOOST_CHECK(counterWCounterSet->m_Units == "");
1474 BOOST_CHECK(counterWCounterSet->m_DeviceUid == 0);
1475 BOOST_CHECK(counterWCounterSet->m_CounterSetUid == counterSet->m_Uid);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001476 BOOST_CHECK(category->m_Counters.size() == 27);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001477 BOOST_CHECK(category->m_Counters.back() == counterWCounterSet->m_Uid);
1478
1479 // Register a counter with a valid parent category name and associated to a device and a counter set
1480 const Counter* counterWDeviceWCounterSet = nullptr;
1481 BOOST_CHECK_NO_THROW(counterWDeviceWCounterSet
1482 = counterDirectory.RegisterCounter(categoryName,
1483 0,
1484 1,
1485 123.45f,
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001486 "valid name 12",
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001487 "valid description",
1488 armnn::EmptyOptional(), // Units
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001489 1, // Number of cores
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001490 device->m_Uid, // Device UID
1491 counterSet->m_Uid)); // Counter set UID
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001492 BOOST_CHECK(counterDirectory.GetCounterCount() == 28);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001493 BOOST_CHECK(counterWDeviceWCounterSet);
1494 BOOST_CHECK(counterWDeviceWCounterSet->m_Uid >= 0);
1495 BOOST_CHECK(counterWDeviceWCounterSet->m_Uid > counter->m_Uid);
1496 BOOST_CHECK(counterWDeviceWCounterSet->m_MaxCounterUid == counterWDeviceWCounterSet->m_Uid);
1497 BOOST_CHECK(counterWDeviceWCounterSet->m_Class == 0);
1498 BOOST_CHECK(counterWDeviceWCounterSet->m_Interpolation == 1);
1499 BOOST_CHECK(counterWDeviceWCounterSet->m_Multiplier == 123.45f);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001500 BOOST_CHECK(counterWDeviceWCounterSet->m_Name == "valid name 12");
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001501 BOOST_CHECK(counterWDeviceWCounterSet->m_Description == "valid description");
1502 BOOST_CHECK(counterWDeviceWCounterSet->m_Units == "");
1503 BOOST_CHECK(counterWDeviceWCounterSet->m_DeviceUid == device->m_Uid);
1504 BOOST_CHECK(counterWDeviceWCounterSet->m_CounterSetUid == counterSet->m_Uid);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001505 BOOST_CHECK(category->m_Counters.size() == 28);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001506 BOOST_CHECK(category->m_Counters.back() == counterWDeviceWCounterSet->m_Uid);
1507
1508 // Register another category for testing
1509 const std::string anotherCategoryName = "some_other_category";
1510 const Category* anotherCategory = nullptr;
1511 BOOST_CHECK_NO_THROW(anotherCategory = counterDirectory.RegisterCategory(anotherCategoryName));
1512 BOOST_CHECK(counterDirectory.GetCategoryCount() == 2);
1513 BOOST_CHECK(anotherCategory);
1514 BOOST_CHECK(anotherCategory != category);
1515 BOOST_CHECK(anotherCategory->m_Name == anotherCategoryName);
1516 BOOST_CHECK(anotherCategory->m_Counters.empty());
1517 BOOST_CHECK(anotherCategory->m_DeviceUid == 0);
1518 BOOST_CHECK(anotherCategory->m_CounterSetUid == 0);
1519
1520 // Register a counter to the other category
1521 const Counter* anotherCounter = nullptr;
1522 BOOST_CHECK_NO_THROW(anotherCounter = counterDirectory.RegisterCounter(anotherCategoryName,
1523 1,
1524 0,
1525 .00043f,
1526 "valid name",
1527 "valid description",
1528 armnn::EmptyOptional(), // Units
1529 armnn::EmptyOptional(), // Number of cores
1530 device->m_Uid, // Device UID
1531 counterSet->m_Uid)); // Counter set UID
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001532 BOOST_CHECK(counterDirectory.GetCounterCount() == 29);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001533 BOOST_CHECK(anotherCounter);
1534 BOOST_CHECK(anotherCounter->m_Uid >= 0);
1535 BOOST_CHECK(anotherCounter->m_MaxCounterUid == anotherCounter->m_Uid);
1536 BOOST_CHECK(anotherCounter->m_Class == 1);
1537 BOOST_CHECK(anotherCounter->m_Interpolation == 0);
1538 BOOST_CHECK(anotherCounter->m_Multiplier == .00043f);
1539 BOOST_CHECK(anotherCounter->m_Name == "valid name");
1540 BOOST_CHECK(anotherCounter->m_Description == "valid description");
1541 BOOST_CHECK(anotherCounter->m_Units == "");
1542 BOOST_CHECK(anotherCounter->m_DeviceUid == device->m_Uid);
1543 BOOST_CHECK(anotherCounter->m_CounterSetUid == counterSet->m_Uid);
1544 BOOST_CHECK(anotherCategory->m_Counters.size() == 1);
1545 BOOST_CHECK(anotherCategory->m_Counters.back() == anotherCounter->m_Uid);
Matteo Martincighab173e92019-09-05 12:02:04 +01001546}
1547
Ferran Balaguer1b941722019-08-28 16:57:18 +01001548BOOST_AUTO_TEST_CASE(CounterSelectionCommandHandlerParseData)
1549{
1550 using boost::numeric_cast;
1551
1552 class TestCaptureThread : public IPeriodicCounterCapture
1553 {
1554 void Start() override {};
1555 };
1556
1557 const uint32_t packetId = 0x40000;
1558
1559 uint32_t version = 1;
1560 Holder holder;
1561 TestCaptureThread captureThread;
1562 MockBuffer mockBuffer(512);
1563 SendCounterPacket sendCounterPacket(mockBuffer);
1564
1565 uint32_t sizeOfUint32 = numeric_cast<uint32_t>(sizeof(uint32_t));
1566 uint32_t sizeOfUint16 = numeric_cast<uint32_t>(sizeof(uint16_t));
1567
1568 // Data with period and counters
1569 uint32_t period1 = 10;
1570 uint32_t dataLength1 = 8;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001571 uint32_t offset = 0;
1572
FinnWilliamsArma0c78712019-09-16 12:06:47 +01001573 std::unique_ptr<char[]> uniqueData1 = std::make_unique<char[]>(dataLength1);
1574 unsigned char* data1 = reinterpret_cast<unsigned char*>(uniqueData1.get());
1575
Ferran Balaguer1b941722019-08-28 16:57:18 +01001576 WriteUint32(data1, offset, period1);
1577 offset += sizeOfUint32;
1578 WriteUint16(data1, offset, 4000);
1579 offset += sizeOfUint16;
1580 WriteUint16(data1, offset, 5000);
1581
FinnWilliamsArma0c78712019-09-16 12:06:47 +01001582 Packet packetA(packetId, dataLength1, uniqueData1);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001583
1584 PeriodicCounterSelectionCommandHandler commandHandler(packetId, version, holder, captureThread,
1585 sendCounterPacket);
1586 commandHandler(packetA);
1587
1588 std::vector<uint16_t> counterIds = holder.GetCaptureData().GetCounterIds();
1589
1590 BOOST_TEST(holder.GetCaptureData().GetCapturePeriod() == period1);
1591 BOOST_TEST(counterIds.size() == 2);
1592 BOOST_TEST(counterIds[0] == 4000);
1593 BOOST_TEST(counterIds[1] == 5000);
1594
1595 unsigned int size = 0;
1596
1597 const unsigned char* readBuffer = mockBuffer.GetReadBuffer(size);
1598
1599 offset = 0;
1600
1601 uint32_t headerWord0 = ReadUint32(readBuffer, offset);
1602 offset += sizeOfUint32;
1603 uint32_t headerWord1 = ReadUint32(readBuffer, offset);
1604 offset += sizeOfUint32;
1605 uint32_t period = ReadUint32(readBuffer, offset);
1606
1607 BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family
1608 BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 4); // packet id
1609 BOOST_TEST(headerWord1 == 8); // data lenght
1610 BOOST_TEST(period == 10); // capture period
1611
1612 uint16_t counterId = 0;
1613 offset += sizeOfUint32;
1614 counterId = ReadUint16(readBuffer, offset);
1615 BOOST_TEST(counterId == 4000);
1616 offset += sizeOfUint16;
1617 counterId = ReadUint16(readBuffer, offset);
1618 BOOST_TEST(counterId == 5000);
1619
1620 // Data with period only
1621 uint32_t period2 = 11;
1622 uint32_t dataLength2 = 4;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001623
FinnWilliamsArma0c78712019-09-16 12:06:47 +01001624 std::unique_ptr<char[]> uniqueData2 = std::make_unique<char[]>(dataLength2);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001625
FinnWilliamsArma0c78712019-09-16 12:06:47 +01001626 WriteUint32(reinterpret_cast<unsigned char*>(uniqueData2.get()), 0, period2);
1627
1628 Packet packetB(packetId, dataLength2, uniqueData2);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001629
1630 commandHandler(packetB);
1631
1632 counterIds = holder.GetCaptureData().GetCounterIds();
1633
1634 BOOST_TEST(holder.GetCaptureData().GetCapturePeriod() == period2);
1635 BOOST_TEST(counterIds.size() == 0);
1636
1637 readBuffer = mockBuffer.GetReadBuffer(size);
1638
1639 offset = 0;
1640
1641 headerWord0 = ReadUint32(readBuffer, offset);
1642 offset += sizeOfUint32;
1643 headerWord1 = ReadUint32(readBuffer, offset);
1644 offset += sizeOfUint32;
1645 period = ReadUint32(readBuffer, offset);
1646
1647 BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family
1648 BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 4); // packet id
1649 BOOST_TEST(headerWord1 == 4); // data lenght
1650 BOOST_TEST(period == 11); // capture period
Ferran Balaguer1b941722019-08-28 16:57:18 +01001651}
1652
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001653BOOST_AUTO_TEST_CASE(CheckConnectionAcknowledged)
1654{
1655 using boost::numeric_cast;
1656
1657 const uint32_t connectionPacketId = 0x10000;
1658 const uint32_t version = 1;
1659
1660 uint32_t sizeOfUint32 = numeric_cast<uint32_t>(sizeof(uint32_t));
1661 uint32_t sizeOfUint16 = numeric_cast<uint32_t>(sizeof(uint16_t));
1662
1663 // Data with period and counters
1664 uint32_t period1 = 10;
1665 uint32_t dataLength1 = 8;
1666 uint32_t offset = 0;
1667
1668 std::unique_ptr<char[]> uniqueData1 = std::make_unique<char[]>(dataLength1);
1669 unsigned char* data1 = reinterpret_cast<unsigned char*>(uniqueData1.get());
1670
1671 WriteUint32(data1, offset, period1);
1672 offset += sizeOfUint32;
1673 WriteUint16(data1, offset, 4000);
1674 offset += sizeOfUint16;
1675 WriteUint16(data1, offset, 5000);
1676
1677 Packet packetA(connectionPacketId, dataLength1, uniqueData1);
1678
1679 ProfilingStateMachine profilingState(ProfilingState::Uninitialised);
1680 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::Uninitialised);
1681
1682 ConnectionAcknowledgedCommandHandler commandHandler(connectionPacketId, version, profilingState);
1683
1684 // command handler received packet on ProfilingState::Uninitialised
1685 BOOST_CHECK_THROW(commandHandler(packetA), armnn::Exception);
1686
1687 profilingState.TransitionToState(ProfilingState::NotConnected);
1688 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::NotConnected);
1689 // command handler received packet on ProfilingState::NotConnected
1690 BOOST_CHECK_THROW(commandHandler(packetA), armnn::Exception);
1691
1692 profilingState.TransitionToState(ProfilingState::WaitingForAck);
1693 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::WaitingForAck);
1694 // command handler received packet on ProfilingState::WaitingForAck
1695 commandHandler(packetA);
1696 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::Active);
1697
1698 // command handler received packet on ProfilingState::Active
1699 commandHandler(packetA);
1700 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::Active);
1701
1702 // command handler received different packet
1703 const uint32_t differentPacketId = 0x40000;
1704 Packet packetB(differentPacketId, dataLength1, uniqueData1);
1705 ConnectionAcknowledgedCommandHandler differentCommandHandler(differentPacketId, version, profilingState);
1706 BOOST_CHECK_THROW(differentCommandHandler(packetB), armnn::Exception);
1707}
1708
Teresa Charlin9bab4962019-09-06 12:28:35 +01001709BOOST_AUTO_TEST_CASE(CheckSocketProfilingConnection)
1710{
1711 // Check that creating a SocketProfilingConnection results in an exception as the Gator UDS doesn't exist.
1712 BOOST_CHECK_THROW(new SocketProfilingConnection(), armnn::Exception);
1713}
1714
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001715BOOST_AUTO_TEST_CASE(SwTraceIsValidCharTest)
1716{
1717 // Only ASCII 7-bit encoding supported
1718 for (unsigned char c = 0; c < 128; c++)
1719 {
1720 BOOST_CHECK(SwTraceCharPolicy::IsValidChar(c));
1721 }
1722
1723 // Not ASCII
1724 for (unsigned char c = 255; c >= 128; c++)
1725 {
1726 BOOST_CHECK(!SwTraceCharPolicy::IsValidChar(c));
1727 }
1728}
1729
1730BOOST_AUTO_TEST_CASE(SwTraceIsValidNameCharTest)
1731{
1732 // Only alpha-numeric and underscore ASCII 7-bit encoding supported
1733 const unsigned char validChars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
1734 for (unsigned char i = 0; i < sizeof(validChars) / sizeof(validChars[0]) - 1; i++)
1735 {
1736 BOOST_CHECK(SwTraceNameCharPolicy::IsValidChar(validChars[i]));
1737 }
1738
1739 // Non alpha-numeric chars
1740 for (unsigned char c = 0; c < 48; c++)
1741 {
1742 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1743 }
1744 for (unsigned char c = 58; c < 65; c++)
1745 {
1746 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1747 }
1748 for (unsigned char c = 91; c < 95; c++)
1749 {
1750 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1751 }
1752 for (unsigned char c = 96; c < 97; c++)
1753 {
1754 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1755 }
1756 for (unsigned char c = 123; c < 128; c++)
1757 {
1758 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1759 }
1760
1761 // Not ASCII
1762 for (unsigned char c = 255; c >= 128; c++)
1763 {
1764 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1765 }
1766}
1767
1768BOOST_AUTO_TEST_CASE(IsValidSwTraceStringTest)
1769{
1770 // Valid SWTrace strings
1771 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>(""));
1772 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("_"));
1773 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("0123"));
1774 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("valid_string"));
1775 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("VALID_string_456"));
1776 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>(" "));
1777 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("valid string"));
1778 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("!$%"));
1779 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("valid|\\~string#123"));
1780
1781 // Invalid SWTrace strings
1782 BOOST_CHECK(!IsValidSwTraceString<SwTraceCharPolicy>("€£"));
1783 BOOST_CHECK(!IsValidSwTraceString<SwTraceCharPolicy>("invalid‡string"));
1784 BOOST_CHECK(!IsValidSwTraceString<SwTraceCharPolicy>("12Ž34"));
1785}
1786
1787BOOST_AUTO_TEST_CASE(IsValidSwTraceNameStringTest)
1788{
1789 // Valid SWTrace name strings
1790 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>(""));
1791 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>("_"));
1792 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>("0123"));
1793 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>("valid_string"));
1794 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>("VALID_string_456"));
1795
1796 // Invalid SWTrace name strings
1797 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>(" "));
1798 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("invalid string"));
1799 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("!$%"));
1800 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("invalid|\\~string#123"));
1801 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("€£"));
1802 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("invalid‡string"));
1803 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("12Ž34"));
1804}
1805
1806template <typename SwTracePolicy>
1807void StringToSwTraceStringTestHelper(const std::string& testString, std::vector<uint32_t> buffer, size_t expectedSize)
1808{
1809 // Convert the test string to a SWTrace string
1810 BOOST_CHECK(StringToSwTraceString<SwTracePolicy>(testString, buffer));
1811
1812 // The buffer must contain at least the length of the string
1813 BOOST_CHECK(!buffer.empty());
1814
1815 // The buffer must be of the expected size (in words)
1816 BOOST_CHECK(buffer.size() == expectedSize);
1817
1818 // The first word of the byte must be the length of the string including the null-terminator
1819 BOOST_CHECK(buffer[0] == testString.size() + 1);
1820
1821 // The contents of the buffer must match the test string
1822 BOOST_CHECK(std::memcmp(testString.data(), buffer.data() + 1, testString.size()) == 0);
1823
1824 // The buffer must include the null-terminator at the end of the string
1825 size_t nullTerminatorIndex = sizeof(uint32_t) + testString.size();
1826 BOOST_CHECK(reinterpret_cast<unsigned char*>(buffer.data())[nullTerminatorIndex] == '\0');
1827}
1828
1829BOOST_AUTO_TEST_CASE(StringToSwTraceStringTest)
1830{
1831 std::vector<uint32_t> buffer;
1832
1833 // Valid SWTrace strings (expected size in words)
1834 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("", buffer, 2);
1835 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("_", buffer, 2);
1836 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("0123", buffer, 3);
1837 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("valid_string", buffer, 5);
1838 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("VALID_string_456", buffer, 6);
1839 StringToSwTraceStringTestHelper<SwTraceCharPolicy>(" ", buffer, 2);
1840 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("valid string", buffer, 5);
1841 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("!$%", buffer, 2);
1842 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("valid|\\~string#123", buffer, 6);
1843
1844 // Invalid SWTrace strings
1845 BOOST_CHECK(!StringToSwTraceString<SwTraceCharPolicy>("€£", buffer));
1846 BOOST_CHECK(buffer.empty());
1847 BOOST_CHECK(!StringToSwTraceString<SwTraceCharPolicy>("invalid‡string", buffer));
1848 BOOST_CHECK(buffer.empty());
1849 BOOST_CHECK(!StringToSwTraceString<SwTraceCharPolicy>("12Ž34", buffer));
1850 BOOST_CHECK(buffer.empty());
1851}
1852
1853BOOST_AUTO_TEST_CASE(StringToSwTraceNameStringTest)
1854{
1855 std::vector<uint32_t> buffer;
1856
1857 // Valid SWTrace namestrings (expected size in words)
1858 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("", buffer, 2);
1859 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("_", buffer, 2);
1860 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("0123", buffer, 3);
1861 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("valid_string", buffer, 5);
1862 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("VALID_string_456", buffer, 6);
1863
1864 // Invalid SWTrace namestrings
1865 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>(" ", buffer));
1866 BOOST_CHECK(buffer.empty());
1867 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("invalid string", buffer));
1868 BOOST_CHECK(buffer.empty());
1869 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("!$%", buffer));
1870 BOOST_CHECK(buffer.empty());
1871 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("invalid|\\~string#123", buffer));
1872 BOOST_CHECK(buffer.empty());
1873 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("€£", buffer));
1874 BOOST_CHECK(buffer.empty());
1875 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("invalid‡string", buffer));
1876 BOOST_CHECK(buffer.empty());
1877 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("12Ž34", buffer));
1878 BOOST_CHECK(buffer.empty());
1879}
1880
Francis Murtagh1f7db452019-08-14 09:49:34 +01001881BOOST_AUTO_TEST_SUITE_END()