blob: 9dd7cd3d646dc098e817c5b696cd1dc90ff03982 [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"
FinnWilliamsArm4833cea2019-09-17 16:53:53 +01007#include "../CommandThread.hpp"
Teresa Charlin9bab4962019-09-06 12:28:35 +01008
Matteo Martincigh6db5f202019-09-05 12:02:04 +01009#include <CommandHandlerKey.hpp>
10#include <CommandHandlerFunctor.hpp>
11#include <CommandHandlerRegistry.hpp>
Sadik Armaganb5f01b22019-09-18 17:29:00 +010012#include <ConnectionAcknowledgedCommandHandler.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010013#include <CounterDirectory.hpp>
14#include <EncodeVersion.hpp>
15#include <Holder.hpp>
16#include <Packet.hpp>
17#include <PacketVersionResolver.hpp>
Francis Murtaghfcb8ef62019-09-20 15:40:09 +010018#include <PeriodicCounterCapture.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010019#include <PeriodicCounterSelectionCommandHandler.hpp>
20#include <ProfilingStateMachine.hpp>
21#include <ProfilingService.hpp>
22#include <ProfilingUtils.hpp>
Narumol Prangnawarat48033692019-09-20 12:04:55 +010023#include <RequestCounterDirectoryCommandHandler.hpp>
Teresa Charlin9bab4962019-09-06 12:28:35 +010024#include <Runtime.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010025#include <SocketProfilingConnection.hpp>
Francis Murtaghfcb8ef62019-09-20 15:40:09 +010026#include <IReadCounterValue.hpp>
Keith Davis02356de2019-08-26 18:28:17 +010027
Matteo Martincigh6db5f202019-09-05 12:02:04 +010028#include <armnn/Conversion.hpp>
Ferran Balaguer1b941722019-08-28 16:57:18 +010029
Sadik Armaganbd9e2c52019-09-26 23:13:31 +010030#include <boost/algorithm/string.hpp>
Ferran Balaguer1b941722019-08-28 16:57:18 +010031#include <boost/numeric/conversion/cast.hpp>
Sadik Armaganbd9e2c52019-09-26 23:13:31 +010032#include <boost/test/unit_test.hpp>
Francis Murtagh1f7db452019-08-14 09:49:34 +010033
Nikhil Rajbc626052019-08-15 15:49:45 +010034#include <cstdint>
35#include <cstring>
Sadik Armaganbd9e2c52019-09-26 23:13:31 +010036#include <iostream>
Aron Virginas-Tare898db92019-08-22 12:56:34 +010037#include <limits>
Francis Murtagh11f99b42019-08-16 11:28:52 +010038#include <map>
Aron Virginas-Tare898db92019-08-22 12:56:34 +010039#include <random>
Nikhil Raj3ecc5102019-09-03 15:55:33 +010040#include <thread>
Matteo Martincigh24e8f922019-09-19 11:57:46 +010041#include <chrono>
Francis Murtagh1f7db452019-08-14 09:49:34 +010042
43BOOST_AUTO_TEST_SUITE(ExternalProfiling)
44
Aron Virginas-Tare898db92019-08-22 12:56:34 +010045using namespace armnn::profiling;
46
Francis Murtagh1f7db452019-08-14 09:49:34 +010047BOOST_AUTO_TEST_CASE(CheckCommandHandlerKeyComparisons)
48{
49 CommandHandlerKey testKey0(1, 1);
50 CommandHandlerKey testKey1(1, 1);
51 CommandHandlerKey testKey2(1, 1);
52 CommandHandlerKey testKey3(0, 0);
53 CommandHandlerKey testKey4(2, 2);
54 CommandHandlerKey testKey5(0, 2);
55
56 BOOST_CHECK(testKey1<testKey4);
57 BOOST_CHECK(testKey1>testKey3);
58 BOOST_CHECK(testKey1<=testKey4);
59 BOOST_CHECK(testKey1>=testKey3);
60 BOOST_CHECK(testKey1<=testKey2);
61 BOOST_CHECK(testKey1>=testKey2);
62 BOOST_CHECK(testKey1==testKey2);
63 BOOST_CHECK(testKey1==testKey1);
64
65 BOOST_CHECK(!(testKey1==testKey5));
66 BOOST_CHECK(!(testKey1!=testKey1));
67 BOOST_CHECK(testKey1!=testKey5);
68
69 BOOST_CHECK(testKey1==testKey2 && testKey2==testKey1);
70 BOOST_CHECK(testKey0==testKey1 && testKey1==testKey2 && testKey0==testKey2);
71
72 BOOST_CHECK(testKey1.GetPacketId()==1);
73 BOOST_CHECK(testKey1.GetVersion()==1);
74
75 std::vector<CommandHandlerKey> vect =
Aron Virginas-Tare898db92019-08-22 12:56:34 +010076 {
77 CommandHandlerKey(0,1), CommandHandlerKey(2,0), CommandHandlerKey(1,0),
78 CommandHandlerKey(2,1), CommandHandlerKey(1,1), CommandHandlerKey(0,1),
79 CommandHandlerKey(2,0), CommandHandlerKey(0,0)
80 };
Francis Murtagh1f7db452019-08-14 09:49:34 +010081
82 std::sort(vect.begin(), vect.end());
83
84 std::vector<CommandHandlerKey> expectedVect =
Aron Virginas-Tare898db92019-08-22 12:56:34 +010085 {
86 CommandHandlerKey(0,0), CommandHandlerKey(0,1), CommandHandlerKey(0,1),
87 CommandHandlerKey(1,0), CommandHandlerKey(1,1), CommandHandlerKey(2,0),
88 CommandHandlerKey(2,0), CommandHandlerKey(2,1)
89 };
Francis Murtagh1f7db452019-08-14 09:49:34 +010090
91 BOOST_CHECK(vect == expectedVect);
92}
93
FinnWilliamsArm4833cea2019-09-17 16:53:53 +010094class TestProfilingConnectionBase :public IProfilingConnection
95{
96public:
97 TestProfilingConnectionBase() = default;
98 ~TestProfilingConnectionBase() = default;
99
100 bool IsOpen()
101 {
102 return true;
103 }
104
105 void Close(){}
106
Matteo Martincigh24e8f922019-09-19 11:57:46 +0100107 bool WritePacket(const unsigned char* buffer, uint32_t length)
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100108 {
109 return false;
110 }
111
112 Packet ReadPacket(uint32_t timeout)
113 {
114 std::this_thread::sleep_for(std::chrono::milliseconds(timeout));
115 std::unique_ptr<char[]> packetData;
116 //Return connection acknowledged packet
117 return {65536 ,0 , packetData};
118 }
119};
120
121class TestProfilingConnectionTimeoutError :public TestProfilingConnectionBase
122{
123 int readRequests = 0;
124public:
125 Packet ReadPacket(uint32_t timeout) {
126 if (readRequests < 3)
127 {
128 readRequests++;
129 throw armnn::TimeoutException(": Simulate a timeout");
130 }
131 std::this_thread::sleep_for(std::chrono::milliseconds(timeout));
132 std::unique_ptr<char[]> packetData;
133 //Return connection acknowledged packet after three timeouts
134 return {65536 ,0 , packetData};
135 }
136};
137
138class TestProfilingConnectionArmnnError :public TestProfilingConnectionBase
139{
140public:
141
142 Packet ReadPacket(uint32_t timeout)
143 {
144 std::this_thread::sleep_for(std::chrono::milliseconds(timeout));
145 throw armnn::Exception(": Simulate a non timeout error");
146 }
147};
148
149BOOST_AUTO_TEST_CASE(CheckCommandThread)
150{
151 PacketVersionResolver packetVersionResolver;
152 ProfilingStateMachine profilingStateMachine;
153
154 TestProfilingConnectionBase testProfilingConnectionBase;
155 TestProfilingConnectionTimeoutError testProfilingConnectionTimeOutError;
156 TestProfilingConnectionArmnnError testProfilingConnectionArmnnError;
157
158 ConnectionAcknowledgedCommandHandler connectionAcknowledgedCommandHandler(1, 4194304, profilingStateMachine);
159 CommandHandlerRegistry commandHandlerRegistry;
160
161 commandHandlerRegistry.RegisterFunctor(&connectionAcknowledgedCommandHandler, 1, 4194304);
162
163 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
164 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
165
166 CommandThread commandThread0(1,
167 true,
168 commandHandlerRegistry,
169 packetVersionResolver,
170 testProfilingConnectionBase);
171
172 commandThread0.Start();
173 commandThread0.Start();
174 commandThread0.Start();
175
176 commandThread0.Stop();
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100177
178 BOOST_CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::Active);
179
180 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
181 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
182 //commandThread1 should give up after one timeout
183 CommandThread commandThread1(1,
184 true,
185 commandHandlerRegistry,
186 packetVersionResolver,
187 testProfilingConnectionTimeOutError);
188
189 commandThread1.Start();
Matteo Martincigh88813932019-10-04 14:40:04 +0100190
191 std::this_thread::sleep_for(std::chrono::milliseconds(100));
192
193 BOOST_CHECK(!commandThread1.IsRunning());
194 commandThread1.Stop();
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100195
196 BOOST_CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::WaitingForAck);
197 //now commandThread1 should persist after a timeout
Matteo Martincigh88813932019-10-04 14:40:04 +0100198 commandThread1.SetStopAfterTimeout(false);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100199 commandThread1.Start();
200
201 for (int i = 0; i < 100; i++)
202 {
203 if (profilingStateMachine.GetCurrentState() == ProfilingState::Active)
204 {
205 break;
206 }
207 else
208 {
209 std::this_thread::sleep_for(std::chrono::milliseconds(5));
210 }
211 }
212
213 commandThread1.Stop();
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100214
215 BOOST_CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::Active);
216
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100217 CommandThread commandThread2(1,
218 false,
219 commandHandlerRegistry,
220 packetVersionResolver,
221 testProfilingConnectionArmnnError);
222
223 commandThread2.Start();
224
225 for (int i = 0; i < 100; i++)
226 {
227 if (!commandThread2.IsRunning())
228 {
229 //commandThread2 should stop once it encounters a non timing error
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100230 return;
231 }
232 std::this_thread::sleep_for(std::chrono::milliseconds(5));
233 }
234
235 BOOST_ERROR("commandThread2 has failed to stop");
Matteo Martincigh88813932019-10-04 14:40:04 +0100236 commandThread2.Stop();
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100237}
238
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100239BOOST_AUTO_TEST_CASE(CheckEncodeVersion)
240{
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100241 Version version1(12);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100242
243 BOOST_CHECK(version1.GetMajor() == 0);
244 BOOST_CHECK(version1.GetMinor() == 0);
245 BOOST_CHECK(version1.GetPatch() == 12);
246
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100247 Version version2(4108);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100248
249 BOOST_CHECK(version2.GetMajor() == 0);
250 BOOST_CHECK(version2.GetMinor() == 1);
251 BOOST_CHECK(version2.GetPatch() == 12);
252
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100253 Version version3(4198412);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100254
255 BOOST_CHECK(version3.GetMajor() == 1);
256 BOOST_CHECK(version3.GetMinor() == 1);
257 BOOST_CHECK(version3.GetPatch() == 12);
258
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100259 Version version4(0);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100260
261 BOOST_CHECK(version4.GetMajor() == 0);
262 BOOST_CHECK(version4.GetMinor() == 0);
263 BOOST_CHECK(version4.GetPatch() == 0);
264
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100265 Version version5(1, 0, 0);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100266 BOOST_CHECK(version5.GetEncodedValue() == 4194304);
267}
268
Nikhil Rajbc626052019-08-15 15:49:45 +0100269BOOST_AUTO_TEST_CASE(CheckPacketClass)
270{
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100271 uint32_t length = 4;
272 std::unique_ptr<char[]> packetData0 = std::make_unique<char[]>(length);
273 std::unique_ptr<char[]> packetData1 = std::make_unique<char[]>(0);
274 std::unique_ptr<char[]> nullPacketData;
Nikhil Rajbc626052019-08-15 15:49:45 +0100275
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100276 Packet packetTest0(472580096, length, packetData0);
Nikhil Rajbc626052019-08-15 15:49:45 +0100277
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100278 BOOST_CHECK(packetTest0.GetHeader() == 472580096);
279 BOOST_CHECK(packetTest0.GetPacketFamily() == 7);
280 BOOST_CHECK(packetTest0.GetPacketId() == 43);
281 BOOST_CHECK(packetTest0.GetLength() == length);
282 BOOST_CHECK(packetTest0.GetPacketType() == 3);
283 BOOST_CHECK(packetTest0.GetPacketClass() == 5);
Nikhil Rajbc626052019-08-15 15:49:45 +0100284
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100285 BOOST_CHECK_THROW(Packet packetTest1(472580096, 0, packetData1), armnn::Exception);
286 BOOST_CHECK_NO_THROW(Packet packetTest2(472580096, 0, nullPacketData));
Nikhil Rajbc626052019-08-15 15:49:45 +0100287
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100288 Packet packetTest3(472580096, 0, nullPacketData);
289 BOOST_CHECK(packetTest3.GetLength() == 0);
290 BOOST_CHECK(packetTest3.GetData() == nullptr);
291
292 const char* packetTest0Data = packetTest0.GetData();
293 Packet packetTest4(std::move(packetTest0));
294
295 BOOST_CHECK(packetTest0.GetData() == nullptr);
296 BOOST_CHECK(packetTest4.GetData() == packetTest0Data);
297
298 BOOST_CHECK(packetTest4.GetHeader() == 472580096);
299 BOOST_CHECK(packetTest4.GetPacketFamily() == 7);
300 BOOST_CHECK(packetTest4.GetPacketId() == 43);
301 BOOST_CHECK(packetTest4.GetLength() == length);
302 BOOST_CHECK(packetTest4.GetPacketType() == 3);
303 BOOST_CHECK(packetTest4.GetPacketClass() == 5);
Nikhil Rajbc626052019-08-15 15:49:45 +0100304}
305
Francis Murtagh94d79152019-08-16 17:45:07 +0100306// Create Derived Classes
307class TestFunctorA : public CommandHandlerFunctor
308{
309public:
310 using CommandHandlerFunctor::CommandHandlerFunctor;
311
312 int GetCount() { return m_Count; }
313
314 void operator()(const Packet& packet) override
315 {
316 m_Count++;
317 }
318
319private:
320 int m_Count = 0;
321};
322
323class TestFunctorB : public TestFunctorA
324{
325 using TestFunctorA::TestFunctorA;
326};
327
328class TestFunctorC : public TestFunctorA
329{
330 using TestFunctorA::TestFunctorA;
331};
332
Francis Murtagh11f99b42019-08-16 11:28:52 +0100333BOOST_AUTO_TEST_CASE(CheckCommandHandlerFunctor)
334{
Francis Murtagh11f99b42019-08-16 11:28:52 +0100335 // Hard code the version as it will be the same during a single profiling session
336 uint32_t version = 1;
337
338 TestFunctorA testFunctorA(461, version);
339 TestFunctorB testFunctorB(963, version);
340 TestFunctorC testFunctorC(983, version);
341
342 CommandHandlerKey keyA(testFunctorA.GetPacketId(), testFunctorA.GetVersion());
343 CommandHandlerKey keyB(testFunctorB.GetPacketId(), testFunctorB.GetVersion());
344 CommandHandlerKey keyC(testFunctorC.GetPacketId(), testFunctorC.GetVersion());
345
346 // Create the unwrapped map to simulate the Command Handler Registry
347 std::map<CommandHandlerKey, CommandHandlerFunctor*> registry;
348
349 registry.insert(std::make_pair(keyB, &testFunctorB));
350 registry.insert(std::make_pair(keyA, &testFunctorA));
351 registry.insert(std::make_pair(keyC, &testFunctorC));
352
353 // Check the order of the map is correct
354 auto it = registry.begin();
355 BOOST_CHECK(it->first==keyA);
356 it++;
357 BOOST_CHECK(it->first==keyB);
358 it++;
359 BOOST_CHECK(it->first==keyC);
360
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100361 std::unique_ptr<char[]> packetDataA;
362 std::unique_ptr<char[]> packetDataB;
363 std::unique_ptr<char[]> packetDataC;
364
365 Packet packetA(500000000, 0, packetDataA);
366 Packet packetB(600000000, 0, packetDataB);
367 Packet packetC(400000000, 0, packetDataC);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100368
369 // Check the correct operator of derived class is called
370 registry.at(CommandHandlerKey(packetA.GetPacketId(), version))->operator()(packetA);
371 BOOST_CHECK(testFunctorA.GetCount() == 1);
372 BOOST_CHECK(testFunctorB.GetCount() == 0);
373 BOOST_CHECK(testFunctorC.GetCount() == 0);
374
375 registry.at(CommandHandlerKey(packetB.GetPacketId(), version))->operator()(packetB);
376 BOOST_CHECK(testFunctorA.GetCount() == 1);
377 BOOST_CHECK(testFunctorB.GetCount() == 1);
378 BOOST_CHECK(testFunctorC.GetCount() == 0);
379
380 registry.at(CommandHandlerKey(packetC.GetPacketId(), version))->operator()(packetC);
381 BOOST_CHECK(testFunctorA.GetCount() == 1);
382 BOOST_CHECK(testFunctorB.GetCount() == 1);
383 BOOST_CHECK(testFunctorC.GetCount() == 1);
384}
385
Francis Murtagh94d79152019-08-16 17:45:07 +0100386BOOST_AUTO_TEST_CASE(CheckCommandHandlerRegistry)
387{
388 // Hard code the version as it will be the same during a single profiling session
389 uint32_t version = 1;
390
391 TestFunctorA testFunctorA(461, version);
392 TestFunctorB testFunctorB(963, version);
393 TestFunctorC testFunctorC(983, version);
394
395 // Create the Command Handler Registry
396 CommandHandlerRegistry registry;
397
398 // Register multiple different derived classes
399 registry.RegisterFunctor(&testFunctorA, testFunctorA.GetPacketId(), testFunctorA.GetVersion());
400 registry.RegisterFunctor(&testFunctorB, testFunctorB.GetPacketId(), testFunctorB.GetVersion());
401 registry.RegisterFunctor(&testFunctorC, testFunctorC.GetPacketId(), testFunctorC.GetVersion());
402
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100403 std::unique_ptr<char[]> packetDataA;
404 std::unique_ptr<char[]> packetDataB;
405 std::unique_ptr<char[]> packetDataC;
406
407 Packet packetA(500000000, 0, packetDataA);
408 Packet packetB(600000000, 0, packetDataB);
409 Packet packetC(400000000, 0, packetDataC);
Francis Murtagh94d79152019-08-16 17:45:07 +0100410
411 // Check the correct operator of derived class is called
412 registry.GetFunctor(packetA.GetPacketId(), version)->operator()(packetA);
413 BOOST_CHECK(testFunctorA.GetCount() == 1);
414 BOOST_CHECK(testFunctorB.GetCount() == 0);
415 BOOST_CHECK(testFunctorC.GetCount() == 0);
416
417 registry.GetFunctor(packetB.GetPacketId(), version)->operator()(packetB);
418 BOOST_CHECK(testFunctorA.GetCount() == 1);
419 BOOST_CHECK(testFunctorB.GetCount() == 1);
420 BOOST_CHECK(testFunctorC.GetCount() == 0);
421
422 registry.GetFunctor(packetC.GetPacketId(), version)->operator()(packetC);
423 BOOST_CHECK(testFunctorA.GetCount() == 1);
424 BOOST_CHECK(testFunctorB.GetCount() == 1);
425 BOOST_CHECK(testFunctorC.GetCount() == 1);
426
427 // Re-register an existing key with a new function
428 registry.RegisterFunctor(&testFunctorC, testFunctorA.GetPacketId(), version);
429 registry.GetFunctor(packetA.GetPacketId(), version)->operator()(packetC);
430 BOOST_CHECK(testFunctorA.GetCount() == 1);
431 BOOST_CHECK(testFunctorB.GetCount() == 1);
432 BOOST_CHECK(testFunctorC.GetCount() == 2);
433
434 // Check that non-existent key returns nullptr for its functor
435 BOOST_CHECK_THROW(registry.GetFunctor(0, 0), armnn::Exception);
436}
437
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100438BOOST_AUTO_TEST_CASE(CheckPacketVersionResolver)
439{
440 // Set up random number generator for generating packetId values
441 std::random_device device;
442 std::mt19937 generator(device());
443 std::uniform_int_distribution<uint32_t> distribution(std::numeric_limits<uint32_t>::min(),
444 std::numeric_limits<uint32_t>::max());
445
446 // NOTE: Expected version is always 1.0.0, regardless of packetId
447 const Version expectedVersion(1, 0, 0);
448
449 PacketVersionResolver packetVersionResolver;
450
451 constexpr unsigned int numTests = 10u;
452
453 for (unsigned int i = 0u; i < numTests; ++i)
454 {
455 const uint32_t packetId = distribution(generator);
456 Version resolvedVersion = packetVersionResolver.ResolvePacketVersion(packetId);
457
458 BOOST_TEST(resolvedVersion == expectedVersion);
459 }
460}
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100461void ProfilingCurrentStateThreadImpl(ProfilingStateMachine& states)
462{
463 ProfilingState newState = ProfilingState::NotConnected;
464 states.GetCurrentState();
465 states.TransitionToState(newState);
466}
467
468BOOST_AUTO_TEST_CASE(CheckProfilingStateMachine)
469{
470 ProfilingStateMachine profilingState1(ProfilingState::Uninitialised);
471 profilingState1.TransitionToState(ProfilingState::Uninitialised);
472 BOOST_CHECK(profilingState1.GetCurrentState() == ProfilingState::Uninitialised);
473
474 ProfilingStateMachine profilingState2(ProfilingState::Uninitialised);
475 profilingState2.TransitionToState(ProfilingState::NotConnected);
476 BOOST_CHECK(profilingState2.GetCurrentState() == ProfilingState::NotConnected);
477
478 ProfilingStateMachine profilingState3(ProfilingState::NotConnected);
479 profilingState3.TransitionToState(ProfilingState::NotConnected);
480 BOOST_CHECK(profilingState3.GetCurrentState() == ProfilingState::NotConnected);
481
482 ProfilingStateMachine profilingState4(ProfilingState::NotConnected);
483 profilingState4.TransitionToState(ProfilingState::WaitingForAck);
484 BOOST_CHECK(profilingState4.GetCurrentState() == ProfilingState::WaitingForAck);
485
486 ProfilingStateMachine profilingState5(ProfilingState::WaitingForAck);
487 profilingState5.TransitionToState(ProfilingState::WaitingForAck);
488 BOOST_CHECK(profilingState5.GetCurrentState() == ProfilingState::WaitingForAck);
489
490 ProfilingStateMachine profilingState6(ProfilingState::WaitingForAck);
491 profilingState6.TransitionToState(ProfilingState::Active);
492 BOOST_CHECK(profilingState6.GetCurrentState() == ProfilingState::Active);
493
494 ProfilingStateMachine profilingState7(ProfilingState::Active);
495 profilingState7.TransitionToState(ProfilingState::NotConnected);
496 BOOST_CHECK(profilingState7.GetCurrentState() == ProfilingState::NotConnected);
497
498 ProfilingStateMachine profilingState8(ProfilingState::Active);
499 profilingState8.TransitionToState(ProfilingState::Active);
500 BOOST_CHECK(profilingState8.GetCurrentState() == ProfilingState::Active);
501
502 ProfilingStateMachine profilingState9(ProfilingState::Uninitialised);
503 BOOST_CHECK_THROW(profilingState9.TransitionToState(ProfilingState::WaitingForAck),
504 armnn::Exception);
505
506 ProfilingStateMachine profilingState10(ProfilingState::Uninitialised);
507 BOOST_CHECK_THROW(profilingState10.TransitionToState(ProfilingState::Active),
508 armnn::Exception);
509
510 ProfilingStateMachine profilingState11(ProfilingState::NotConnected);
511 BOOST_CHECK_THROW(profilingState11.TransitionToState(ProfilingState::Uninitialised),
512 armnn::Exception);
513
514 ProfilingStateMachine profilingState12(ProfilingState::NotConnected);
515 BOOST_CHECK_THROW(profilingState12.TransitionToState(ProfilingState::Active),
516 armnn::Exception);
517
518 ProfilingStateMachine profilingState13(ProfilingState::WaitingForAck);
519 BOOST_CHECK_THROW(profilingState13.TransitionToState(ProfilingState::Uninitialised),
520 armnn::Exception);
521
522 ProfilingStateMachine profilingState14(ProfilingState::WaitingForAck);
523 BOOST_CHECK_THROW(profilingState14.TransitionToState(ProfilingState::NotConnected),
524 armnn::Exception);
525
526 ProfilingStateMachine profilingState15(ProfilingState::Active);
527 BOOST_CHECK_THROW(profilingState15.TransitionToState(ProfilingState::Uninitialised),
528 armnn::Exception);
529
530 ProfilingStateMachine profilingState16(armnn::profiling::ProfilingState::Active);
531 BOOST_CHECK_THROW(profilingState16.TransitionToState(ProfilingState::WaitingForAck),
532 armnn::Exception);
533
534 ProfilingStateMachine profilingState17(ProfilingState::Uninitialised);
535
536 std::thread thread1 (ProfilingCurrentStateThreadImpl,std::ref(profilingState17));
537 std::thread thread2 (ProfilingCurrentStateThreadImpl,std::ref(profilingState17));
538 std::thread thread3 (ProfilingCurrentStateThreadImpl,std::ref(profilingState17));
539 std::thread thread4 (ProfilingCurrentStateThreadImpl,std::ref(profilingState17));
540 std::thread thread5 (ProfilingCurrentStateThreadImpl,std::ref(profilingState17));
541
542 thread1.join();
543 thread2.join();
544 thread3.join();
545 thread4.join();
546 thread5.join();
547
548 BOOST_TEST((profilingState17.GetCurrentState() == ProfilingState::NotConnected));
549}
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100550
Jim Flynn8355ec92019-09-17 12:29:50 +0100551void CaptureDataWriteThreadImpl(Holder& holder, uint32_t capturePeriod, const std::vector<uint16_t>& counterIds)
Francis Murtagh68f78d82019-09-04 16:42:29 +0100552{
553 holder.SetCaptureData(capturePeriod, counterIds);
554}
555
Francis Murtaghbd707162019-09-09 11:26:44 +0100556void CaptureDataReadThreadImpl(const Holder& holder, CaptureData& captureData)
Francis Murtagh68f78d82019-09-04 16:42:29 +0100557{
558 captureData = holder.GetCaptureData();
559}
560
561BOOST_AUTO_TEST_CASE(CheckCaptureDataHolder)
562{
Francis Murtaghbd707162019-09-09 11:26:44 +0100563 std::map<uint32_t, std::vector<uint16_t>> periodIdMap;
564 std::vector<uint16_t> counterIds;
Jim Flynn8355ec92019-09-17 12:29:50 +0100565 uint32_t numThreads = 10;
566 for (uint32_t i = 0; i < numThreads; ++i)
Francis Murtaghbd707162019-09-09 11:26:44 +0100567 {
568 counterIds.emplace_back(i);
569 periodIdMap.insert(std::make_pair(i, counterIds));
570 }
Francis Murtagh68f78d82019-09-04 16:42:29 +0100571
Jim Flynn8355ec92019-09-17 12:29:50 +0100572 // Verify the read and write threads set the holder correctly
573 // and retrieve the expected values
Francis Murtagh68f78d82019-09-04 16:42:29 +0100574 Holder holder;
575 BOOST_CHECK((holder.GetCaptureData()).GetCapturePeriod() == 0);
576 BOOST_CHECK(((holder.GetCaptureData()).GetCounterIds()).empty());
577
578 // Check Holder functions
Francis Murtaghbd707162019-09-09 11:26:44 +0100579 std::thread thread1(CaptureDataWriteThreadImpl, std::ref(holder), 2, std::ref(periodIdMap[2]));
Francis Murtagh68f78d82019-09-04 16:42:29 +0100580 thread1.join();
Francis Murtaghbd707162019-09-09 11:26:44 +0100581 BOOST_CHECK((holder.GetCaptureData()).GetCapturePeriod() == 2);
582 BOOST_CHECK((holder.GetCaptureData()).GetCounterIds() == periodIdMap[2]);
Jim Flynn8355ec92019-09-17 12:29:50 +0100583 // NOTE: now that we have some initial values in the holder we don't have to worry
584 // in the multi-threaded section below about a read thread accessing the holder
585 // before any write thread has gotten to it so we read period = 0, counterIds empty
586 // instead of period = 0, counterIds = {0} as will the case when write thread 0
587 // has executed.
Francis Murtagh68f78d82019-09-04 16:42:29 +0100588
589 CaptureData captureData;
590 std::thread thread2(CaptureDataReadThreadImpl, std::ref(holder), std::ref(captureData));
591 thread2.join();
Jim Flynn8355ec92019-09-17 12:29:50 +0100592 BOOST_CHECK(captureData.GetCapturePeriod() == 2);
Francis Murtaghbd707162019-09-09 11:26:44 +0100593 BOOST_CHECK(captureData.GetCounterIds() == periodIdMap[2]);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100594
Jim Flynn8355ec92019-09-17 12:29:50 +0100595 std::map<uint32_t, CaptureData> captureDataIdMap;
596 for (uint32_t i = 0; i < numThreads; ++i)
597 {
598 CaptureData perThreadCaptureData;
599 captureDataIdMap.insert(std::make_pair(i, perThreadCaptureData));
600 }
601
Francis Murtaghbd707162019-09-09 11:26:44 +0100602 std::vector<std::thread> threadsVect;
Jim Flynn8355ec92019-09-17 12:29:50 +0100603 std::vector<std::thread> readThreadsVect;
604 for (uint32_t i = 0; i < numThreads; ++i)
Francis Murtaghbd707162019-09-09 11:26:44 +0100605 {
606 threadsVect.emplace_back(std::thread(CaptureDataWriteThreadImpl,
607 std::ref(holder),
608 i,
Jim Flynn8355ec92019-09-17 12:29:50 +0100609 std::ref(periodIdMap[i])));
Francis Murtagh06965692019-09-05 16:29:01 +0100610
Jim Flynn8355ec92019-09-17 12:29:50 +0100611 // Verify that the CaptureData goes into the thread in a virgin state
612 BOOST_CHECK(captureDataIdMap.at(i).GetCapturePeriod() == 0);
613 BOOST_CHECK(captureDataIdMap.at(i).GetCounterIds().empty());
614 readThreadsVect.emplace_back(std::thread(CaptureDataReadThreadImpl,
615 std::ref(holder),
616 std::ref(captureDataIdMap.at(i))));
Francis Murtaghbd707162019-09-09 11:26:44 +0100617 }
618
Jim Flynn8355ec92019-09-17 12:29:50 +0100619 for (uint32_t i = 0; i < numThreads; ++i)
Francis Murtaghbd707162019-09-09 11:26:44 +0100620 {
621 threadsVect[i].join();
Francis Murtaghbd707162019-09-09 11:26:44 +0100622 readThreadsVect[i].join();
623 }
Francis Murtagh68f78d82019-09-04 16:42:29 +0100624
Jim Flynn8355ec92019-09-17 12:29:50 +0100625 // Look at the CaptureData that each read thread has filled
626 // the capture period it read should match the counter ids entry
627 for (uint32_t i = 0; i < numThreads; ++i)
628 {
629 CaptureData perThreadCaptureData = captureDataIdMap.at(i);
630 BOOST_CHECK(perThreadCaptureData.GetCounterIds() == periodIdMap.at(perThreadCaptureData.GetCapturePeriod()));
631 }
Matthew Bentham46d1c622019-09-13 12:45:04 +0100632}
Francis Murtagh68f78d82019-09-04 16:42:29 +0100633
Matthew Bentham46d1c622019-09-13 12:45:04 +0100634BOOST_AUTO_TEST_CASE(CaptureDataMethods)
635{
Jim Flynn8355ec92019-09-17 12:29:50 +0100636 // Check CaptureData setter and getter functions
Matthew Bentham46d1c622019-09-13 12:45:04 +0100637 std::vector<uint16_t> counterIds = {42, 29, 13};
Jim Flynn8355ec92019-09-17 12:29:50 +0100638 CaptureData captureData;
639 BOOST_CHECK(captureData.GetCapturePeriod() == 0);
640 BOOST_CHECK((captureData.GetCounterIds()).empty());
641 captureData.SetCapturePeriod(150);
642 captureData.SetCounterIds(counterIds);
643 BOOST_CHECK(captureData.GetCapturePeriod() == 150);
644 BOOST_CHECK(captureData.GetCounterIds() == counterIds);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100645
Jim Flynn8355ec92019-09-17 12:29:50 +0100646 // Check assignment operator
Francis Murtagh68f78d82019-09-04 16:42:29 +0100647 CaptureData secondCaptureData;
Francis Murtagh68f78d82019-09-04 16:42:29 +0100648
Jim Flynn8355ec92019-09-17 12:29:50 +0100649 secondCaptureData = captureData;
650 BOOST_CHECK(secondCaptureData.GetCapturePeriod() == 150);
Matthew Bentham46d1c622019-09-13 12:45:04 +0100651 BOOST_CHECK(secondCaptureData.GetCounterIds() == counterIds);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100652
653 // Check copy constructor
Jim Flynn8355ec92019-09-17 12:29:50 +0100654 CaptureData copyConstructedCaptureData(captureData);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100655
Jim Flynn8355ec92019-09-17 12:29:50 +0100656 BOOST_CHECK(copyConstructedCaptureData.GetCapturePeriod() == 150);
Matthew Bentham46d1c622019-09-13 12:45:04 +0100657 BOOST_CHECK(copyConstructedCaptureData.GetCounterIds() == counterIds);
Keith Davis02356de2019-08-26 18:28:17 +0100658}
Francis Murtagh68f78d82019-09-04 16:42:29 +0100659
Keith Davis02356de2019-08-26 18:28:17 +0100660BOOST_AUTO_TEST_CASE(CheckProfilingServiceDisabled)
661{
662 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Matteo Martincigha84edee2019-10-02 12:50:57 +0100663 ProfilingService& profilingService = ProfilingService::Instance();
664 profilingService.ResetExternalProfilingOptions(options, true);
665 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
666 profilingService.Run();
667 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis02356de2019-08-26 18:28:17 +0100668}
669
Matteo Martincigha84edee2019-10-02 12:50:57 +0100670struct cerr_redirect
671{
Sadik Armaganbd9e2c52019-09-26 23:13:31 +0100672 cerr_redirect(std::streambuf* new_buffer)
Matteo Martincigha84edee2019-10-02 12:50:57 +0100673 : old(std::cerr.rdbuf(new_buffer)) {}
674 ~cerr_redirect() { std::cerr.rdbuf(old); }
Sadik Armaganbd9e2c52019-09-26 23:13:31 +0100675
676private:
677 std::streambuf* old;
678};
679
Keith Davis02356de2019-08-26 18:28:17 +0100680BOOST_AUTO_TEST_CASE(CheckProfilingServiceEnabled)
681{
682 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
683 options.m_EnableProfiling = true;
Matteo Martincigha84edee2019-10-02 12:50:57 +0100684 ProfilingService& profilingService = ProfilingService::Instance();
685 profilingService.ResetExternalProfilingOptions(options, true);
686 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis02356de2019-08-26 18:28:17 +0100687
Sadik Armaganbd9e2c52019-09-26 23:13:31 +0100688 // As there is no daemon running a connection cannot be made so expect a std::cerr to console
689 std::stringstream ss;
690 cerr_redirect guard(ss.rdbuf());
Matteo Martincigha84edee2019-10-02 12:50:57 +0100691 profilingService.Run();
Sadik Armaganbd9e2c52019-09-26 23:13:31 +0100692 BOOST_CHECK(boost::contains(ss.str(), "Cannot connect to stream socket: Connection refused"));
693}
Keith Davis02356de2019-08-26 18:28:17 +0100694
695BOOST_AUTO_TEST_CASE(CheckProfilingServiceEnabledRuntime)
696{
697 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Matteo Martincigha84edee2019-10-02 12:50:57 +0100698 ProfilingService& profilingService = ProfilingService::Instance();
699 profilingService.ResetExternalProfilingOptions(options, true);
700 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
701 profilingService.Run();
702 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100703 options.m_EnableProfiling = true;
Matteo Martincigha84edee2019-10-02 12:50:57 +0100704 profilingService.ResetExternalProfilingOptions(options);
705 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Sadik Armaganbd9e2c52019-09-26 23:13:31 +0100706
707 // As there is no daemon running a connection cannot be made so expect a std::cerr to console
708 std::stringstream ss;
709 cerr_redirect guard(ss.rdbuf());
Matteo Martincigha84edee2019-10-02 12:50:57 +0100710 profilingService.Run();
Sadik Armaganbd9e2c52019-09-26 23:13:31 +0100711 BOOST_CHECK(boost::contains(ss.str(), "Cannot connect to stream socket: Connection refused"));
Francis Murtagh68f78d82019-09-04 16:42:29 +0100712}
713
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100714BOOST_AUTO_TEST_CASE(CheckProfilingServiceCounterDirectory)
715{
716 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Matteo Martincigha84edee2019-10-02 12:50:57 +0100717 ProfilingService& profilingService = ProfilingService::Instance();
718 profilingService.ResetExternalProfilingOptions(options, true);
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100719
Matteo Martincigha84edee2019-10-02 12:50:57 +0100720 const ICounterDirectory& counterDirectory0 = profilingService.GetCounterDirectory();
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100721 BOOST_CHECK(counterDirectory0.GetCounterCount() == 0);
722
723 options.m_EnableProfiling = true;
Matteo Martincigha84edee2019-10-02 12:50:57 +0100724 profilingService.ResetExternalProfilingOptions(options);
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100725
Matteo Martincigha84edee2019-10-02 12:50:57 +0100726 const ICounterDirectory& counterDirectory1 = profilingService.GetCounterDirectory();
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100727 BOOST_CHECK(counterDirectory1.GetCounterCount() != 0);
728}
729
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100730BOOST_AUTO_TEST_CASE(CheckProfilingServiceCounterValues)
731{
732 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
733 options.m_EnableProfiling = true;
Matteo Martincigha84edee2019-10-02 12:50:57 +0100734 ProfilingService& profilingService = ProfilingService::Instance();
735 profilingService.ResetExternalProfilingOptions(options, true);
736
737 const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
738 const Counters& counters = counterDirectory.GetCounters();
739 BOOST_CHECK(!counters.empty());
740
741 // Get the UID of the first counter for testing
742 uint16_t counterUid = counters.begin()->first;
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100743
744 ProfilingService* profilingServicePtr = &profilingService;
745 std::vector<std::thread> writers;
746
Matteo Martincigha84edee2019-10-02 12:50:57 +0100747 for (int i = 0; i < 100 ; ++i)
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100748 {
Matteo Martincigha84edee2019-10-02 12:50:57 +0100749 // Increment and decrement the first counter
750 writers.push_back(std::thread(&ProfilingService::IncrementCounterValue, profilingServicePtr, counterUid));
751 writers.push_back(std::thread(&ProfilingService::DecrementCounterValue, profilingServicePtr, counterUid));
752 // Add 10 and subtract 5 from the first counter
753 writers.push_back(std::thread(&ProfilingService::AddCounterValue, profilingServicePtr, counterUid, 10));
754 writers.push_back(std::thread(&ProfilingService::SubtractCounterValue, profilingServicePtr, counterUid, 5));
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100755 }
756
757 std::for_each(writers.begin(), writers.end(), mem_fn(&std::thread::join));
758
Matteo Martincigha84edee2019-10-02 12:50:57 +0100759 uint32_t counterValue = 0;
760 BOOST_CHECK_NO_THROW(counterValue = profilingService.GetCounterValue(counterUid));
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100761 BOOST_CHECK(counterValue == 500);
762
Matteo Martincigha84edee2019-10-02 12:50:57 +0100763 BOOST_CHECK_NO_THROW(profilingService.SetCounterValue(counterUid, 0));
764 BOOST_CHECK_NO_THROW(counterValue = profilingService.GetCounterValue(counterUid));
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100765 BOOST_CHECK(counterValue == 0);
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100766}
767
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100768BOOST_AUTO_TEST_CASE(CheckProfilingObjectUids)
Matteo Martincighab173e92019-09-05 12:02:04 +0100769{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100770 uint16_t uid = 0;
771 BOOST_CHECK_NO_THROW(uid = GetNextUid());
772 BOOST_CHECK(uid >= 1);
773
774 uint16_t nextUid = 0;
775 BOOST_CHECK_NO_THROW(nextUid = GetNextUid());
776 BOOST_CHECK(nextUid > uid);
777
778 std::vector<uint16_t> counterUids;
779 BOOST_CHECK_NO_THROW(counterUids = GetNextCounterUids(0));
780 BOOST_CHECK(counterUids.size() == 1);
781 BOOST_CHECK(counterUids[0] >= 0);
782
783 std::vector<uint16_t> nextCounterUids;
784 BOOST_CHECK_NO_THROW(nextCounterUids = GetNextCounterUids(1));
785 BOOST_CHECK(nextCounterUids.size() == 1);
786 BOOST_CHECK(nextCounterUids[0] > counterUids[0]);
787
788 std::vector<uint16_t> counterUidsMultiCore;
789 uint16_t numberOfCores = 13;
790 BOOST_CHECK_NO_THROW(counterUidsMultiCore = GetNextCounterUids(numberOfCores));
791 BOOST_CHECK(counterUidsMultiCore.size() == numberOfCores);
792 BOOST_CHECK(counterUidsMultiCore.front() >= nextCounterUids[0]);
793 for (size_t i = 1; i < numberOfCores; i ++)
794 {
795 BOOST_CHECK(counterUidsMultiCore[i] == counterUidsMultiCore[i - 1] + 1);
796 }
797 BOOST_CHECK(counterUidsMultiCore.back() == counterUidsMultiCore.front() + numberOfCores - 1);
Matteo Martincighab173e92019-09-05 12:02:04 +0100798}
799
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100800BOOST_AUTO_TEST_CASE(CheckCounterDirectoryRegisterCategory)
Matteo Martincighab173e92019-09-05 12:02:04 +0100801{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100802 CounterDirectory counterDirectory;
803 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
804 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
805 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
806 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
Matteo Martincighab173e92019-09-05 12:02:04 +0100807
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100808 // Register a category with an invalid name
809 const Category* noCategory = nullptr;
810 BOOST_CHECK_THROW(noCategory = counterDirectory.RegisterCategory(""), armnn::InvalidArgumentException);
811 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
812 BOOST_CHECK(!noCategory);
Matteo Martincighab173e92019-09-05 12:02:04 +0100813
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100814 // Register a category with an invalid name
815 BOOST_CHECK_THROW(noCategory = counterDirectory.RegisterCategory("invalid category"),
816 armnn::InvalidArgumentException);
817 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
818 BOOST_CHECK(!noCategory);
819
820 // Register a new category
821 const std::string categoryName = "some_category";
822 const Category* category = nullptr;
823 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
824 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
825 BOOST_CHECK(category);
826 BOOST_CHECK(category->m_Name == categoryName);
827 BOOST_CHECK(category->m_Counters.empty());
828 BOOST_CHECK(category->m_DeviceUid == 0);
829 BOOST_CHECK(category->m_CounterSetUid == 0);
830
831 // Get the registered category
832 const Category* registeredCategory = counterDirectory.GetCategory(categoryName);
833 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
834 BOOST_CHECK(registeredCategory);
835 BOOST_CHECK(registeredCategory == category);
836
837 // Try to get a category not registered
838 const Category* notRegisteredCategory = counterDirectory.GetCategory("not_registered_category");
839 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
840 BOOST_CHECK(!notRegisteredCategory);
841
842 // Register a category already registered
843 const Category* anotherCategory = nullptr;
844 BOOST_CHECK_THROW(anotherCategory = counterDirectory.RegisterCategory(categoryName),
845 armnn::InvalidArgumentException);
846 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
847 BOOST_CHECK(!anotherCategory);
848
849 // Register a device for testing
850 const std::string deviceName = "some_device";
851 const Device* device = nullptr;
852 BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName));
853 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
854 BOOST_CHECK(device);
855 BOOST_CHECK(device->m_Uid >= 1);
856 BOOST_CHECK(device->m_Name == deviceName);
857 BOOST_CHECK(device->m_Cores == 0);
858
859 // Register a new category not associated to any device
860 const std::string categoryWoDeviceName = "some_category_without_device";
861 const Category* categoryWoDevice = nullptr;
862 BOOST_CHECK_NO_THROW(categoryWoDevice = counterDirectory.RegisterCategory(categoryWoDeviceName, 0));
863 BOOST_CHECK(counterDirectory.GetCategoryCount() == 2);
864 BOOST_CHECK(categoryWoDevice);
865 BOOST_CHECK(categoryWoDevice->m_Name == categoryWoDeviceName);
866 BOOST_CHECK(categoryWoDevice->m_Counters.empty());
867 BOOST_CHECK(categoryWoDevice->m_DeviceUid == 0);
868 BOOST_CHECK(categoryWoDevice->m_CounterSetUid == 0);
869
870 // Register a new category associated to an invalid device
871 const std::string categoryWInvalidDeviceName = "some_category_with_invalid_device";
872
873 ARMNN_NO_CONVERSION_WARN_BEGIN
874 uint16_t invalidDeviceUid = device->m_Uid + 10;
875 ARMNN_NO_CONVERSION_WARN_END
876
877 const Category* categoryWInvalidDevice = nullptr;
878 BOOST_CHECK_THROW(categoryWInvalidDevice
879 = counterDirectory.RegisterCategory(categoryWInvalidDeviceName,
880 invalidDeviceUid),
881 armnn::InvalidArgumentException);
882 BOOST_CHECK(counterDirectory.GetCategoryCount() == 2);
883 BOOST_CHECK(!categoryWInvalidDevice);
884
885 // Register a new category associated to a valid device
886 const std::string categoryWValidDeviceName = "some_category_with_valid_device";
887 const Category* categoryWValidDevice = nullptr;
888 BOOST_CHECK_NO_THROW(categoryWValidDevice
889 = counterDirectory.RegisterCategory(categoryWValidDeviceName,
890 device->m_Uid));
891 BOOST_CHECK(counterDirectory.GetCategoryCount() == 3);
892 BOOST_CHECK(categoryWValidDevice);
893 BOOST_CHECK(categoryWValidDevice != category);
894 BOOST_CHECK(categoryWValidDevice->m_Name == categoryWValidDeviceName);
895 BOOST_CHECK(categoryWValidDevice->m_DeviceUid == device->m_Uid);
896 BOOST_CHECK(categoryWValidDevice->m_CounterSetUid == 0);
897
898 // Register a counter set for testing
899 const std::string counterSetName = "some_counter_set";
900 const CounterSet* counterSet = nullptr;
901 BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
902 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
903 BOOST_CHECK(counterSet);
904 BOOST_CHECK(counterSet->m_Uid >= 1);
905 BOOST_CHECK(counterSet->m_Name == counterSetName);
906 BOOST_CHECK(counterSet->m_Count == 0);
907
908 // Register a new category not associated to any counter set
909 const std::string categoryWoCounterSetName = "some_category_without_counter_set";
910 const Category* categoryWoCounterSet = nullptr;
911 BOOST_CHECK_NO_THROW(categoryWoCounterSet
912 = counterDirectory.RegisterCategory(categoryWoCounterSetName,
913 armnn::EmptyOptional(),
914 0));
915 BOOST_CHECK(counterDirectory.GetCategoryCount() == 4);
916 BOOST_CHECK(categoryWoCounterSet);
917 BOOST_CHECK(categoryWoCounterSet->m_Name == categoryWoCounterSetName);
918 BOOST_CHECK(categoryWoCounterSet->m_DeviceUid == 0);
919 BOOST_CHECK(categoryWoCounterSet->m_CounterSetUid == 0);
920
921 // Register a new category associated to an invalid counter set
922 const std::string categoryWInvalidCounterSetName = "some_category_with_invalid_counter_set";
923
924 ARMNN_NO_CONVERSION_WARN_BEGIN
925 uint16_t invalidCunterSetUid = counterSet->m_Uid + 10;
926 ARMNN_NO_CONVERSION_WARN_END
927
928 const Category* categoryWInvalidCounterSet = nullptr;
929 BOOST_CHECK_THROW(categoryWInvalidCounterSet
930 = counterDirectory.RegisterCategory(categoryWInvalidCounterSetName,
931 armnn::EmptyOptional(),
932 invalidCunterSetUid),
933 armnn::InvalidArgumentException);
934 BOOST_CHECK(counterDirectory.GetCategoryCount() == 4);
935 BOOST_CHECK(!categoryWInvalidCounterSet);
936
937 // Register a new category associated to a valid counter set
938 const std::string categoryWValidCounterSetName = "some_category_with_valid_counter_set";
939 const Category* categoryWValidCounterSet = nullptr;
940 BOOST_CHECK_NO_THROW(categoryWValidCounterSet
941 = counterDirectory.RegisterCategory(categoryWValidCounterSetName,
942 armnn::EmptyOptional(),
943 counterSet->m_Uid));
944 BOOST_CHECK(counterDirectory.GetCategoryCount() == 5);
945 BOOST_CHECK(categoryWValidCounterSet);
946 BOOST_CHECK(categoryWValidCounterSet != category);
947 BOOST_CHECK(categoryWValidCounterSet->m_Name == categoryWValidCounterSetName);
948 BOOST_CHECK(categoryWValidCounterSet->m_DeviceUid == 0);
949 BOOST_CHECK(categoryWValidCounterSet->m_CounterSetUid == counterSet->m_Uid);
950
951 // Register a new category associated to a valid device and counter set
952 const std::string categoryWValidDeviceAndValidCounterSetName = "some_category_with_valid_device_and_counter_set";
953 const Category* categoryWValidDeviceAndValidCounterSet = nullptr;
954 BOOST_CHECK_NO_THROW(categoryWValidDeviceAndValidCounterSet
955 = counterDirectory.RegisterCategory(categoryWValidDeviceAndValidCounterSetName,
956 device->m_Uid,
957 counterSet->m_Uid));
958 BOOST_CHECK(counterDirectory.GetCategoryCount() == 6);
959 BOOST_CHECK(categoryWValidDeviceAndValidCounterSet);
960 BOOST_CHECK(categoryWValidDeviceAndValidCounterSet != category);
961 BOOST_CHECK(categoryWValidDeviceAndValidCounterSet->m_Name == categoryWValidDeviceAndValidCounterSetName);
962 BOOST_CHECK(categoryWValidDeviceAndValidCounterSet->m_DeviceUid == device->m_Uid);
963 BOOST_CHECK(categoryWValidDeviceAndValidCounterSet->m_CounterSetUid == counterSet->m_Uid);
964}
965
966BOOST_AUTO_TEST_CASE(CheckCounterDirectoryRegisterDevice)
967{
968 CounterDirectory counterDirectory;
969 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
970 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
971 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
972 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
973
974 // Register a device with an invalid name
975 const Device* noDevice = nullptr;
976 BOOST_CHECK_THROW(noDevice = counterDirectory.RegisterDevice(""), armnn::InvalidArgumentException);
977 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
978 BOOST_CHECK(!noDevice);
979
980 // Register a device with an invalid name
981 BOOST_CHECK_THROW(noDevice = counterDirectory.RegisterDevice("inv@lid nam€"), armnn::InvalidArgumentException);
982 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
983 BOOST_CHECK(!noDevice);
984
985 // Register a new device with no cores or parent category
986 const std::string deviceName = "some_device";
987 const Device* device = nullptr;
988 BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName));
989 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
990 BOOST_CHECK(device);
991 BOOST_CHECK(device->m_Name == deviceName);
992 BOOST_CHECK(device->m_Uid >= 1);
993 BOOST_CHECK(device->m_Cores == 0);
994
Matteo Martincigh657ab2d2019-09-18 10:53:24 +0100995 // Try getting an unregistered device
996 const Device* unregisteredDevice = counterDirectory.GetDevice(9999);
997 BOOST_CHECK(!unregisteredDevice);
998
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100999 // Get the registered device
1000 const Device* registeredDevice = counterDirectory.GetDevice(device->m_Uid);
1001 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
1002 BOOST_CHECK(registeredDevice);
1003 BOOST_CHECK(registeredDevice == device);
1004
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001005 // Register a device with the name of a device already registered
1006 const Device* deviceSameName = nullptr;
1007 BOOST_CHECK_THROW(deviceSameName = counterDirectory.RegisterDevice(deviceName), armnn::InvalidArgumentException);
1008 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
1009 BOOST_CHECK(!deviceSameName);
1010
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001011 // Register a new device with cores and no parent category
1012 const std::string deviceWCoresName = "some_device_with_cores";
1013 const Device* deviceWCores = nullptr;
1014 BOOST_CHECK_NO_THROW(deviceWCores = counterDirectory.RegisterDevice(deviceWCoresName, 2));
1015 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
1016 BOOST_CHECK(deviceWCores);
1017 BOOST_CHECK(deviceWCores->m_Name == deviceWCoresName);
1018 BOOST_CHECK(deviceWCores->m_Uid >= 1);
1019 BOOST_CHECK(deviceWCores->m_Uid > device->m_Uid);
1020 BOOST_CHECK(deviceWCores->m_Cores == 2);
1021
1022 // Get the registered device
1023 const Device* registeredDeviceWCores = counterDirectory.GetDevice(deviceWCores->m_Uid);
1024 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
1025 BOOST_CHECK(registeredDeviceWCores);
1026 BOOST_CHECK(registeredDeviceWCores == deviceWCores);
1027 BOOST_CHECK(registeredDeviceWCores != device);
1028
1029 // Register a new device with cores and invalid parent category
1030 const std::string deviceWCoresWInvalidParentCategoryName = "some_device_with_cores_with_invalid_parent_category";
1031 const Device* deviceWCoresWInvalidParentCategory = nullptr;
1032 BOOST_CHECK_THROW(deviceWCoresWInvalidParentCategory
1033 = counterDirectory.RegisterDevice(deviceWCoresWInvalidParentCategoryName,
1034 3,
1035 std::string("")),
1036 armnn::InvalidArgumentException);
1037 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
1038 BOOST_CHECK(!deviceWCoresWInvalidParentCategory);
1039
1040 // Register a new device with cores and invalid parent category
1041 const std::string deviceWCoresWInvalidParentCategoryName2 = "some_device_with_cores_with_invalid_parent_category2";
1042 const Device* deviceWCoresWInvalidParentCategory2 = nullptr;
1043 BOOST_CHECK_THROW(deviceWCoresWInvalidParentCategory2
1044 = counterDirectory.RegisterDevice(deviceWCoresWInvalidParentCategoryName2,
1045 3,
1046 std::string("invalid_parent_category")),
1047 armnn::InvalidArgumentException);
1048 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
1049 BOOST_CHECK(!deviceWCoresWInvalidParentCategory2);
1050
1051 // Register a category for testing
1052 const std::string categoryName = "some_category";
1053 const Category* category = nullptr;
1054 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
1055 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
1056 BOOST_CHECK(category);
1057 BOOST_CHECK(category->m_Name == categoryName);
1058 BOOST_CHECK(category->m_Counters.empty());
1059 BOOST_CHECK(category->m_DeviceUid == 0);
1060 BOOST_CHECK(category->m_CounterSetUid == 0);
1061
1062 // Register a new device with cores and valid parent category
1063 const std::string deviceWCoresWValidParentCategoryName = "some_device_with_cores_with_valid_parent_category";
1064 const Device* deviceWCoresWValidParentCategory = nullptr;
1065 BOOST_CHECK_NO_THROW(deviceWCoresWValidParentCategory
1066 = counterDirectory.RegisterDevice(deviceWCoresWValidParentCategoryName,
1067 4,
1068 categoryName));
1069 BOOST_CHECK(counterDirectory.GetDeviceCount() == 3);
1070 BOOST_CHECK(deviceWCoresWValidParentCategory);
1071 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Name == deviceWCoresWValidParentCategoryName);
1072 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Uid >= 1);
1073 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Uid > device->m_Uid);
1074 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Uid > deviceWCores->m_Uid);
1075 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Cores == 4);
1076 BOOST_CHECK(category->m_DeviceUid == deviceWCoresWValidParentCategory->m_Uid);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001077
1078 // Register a device associated to a category already associated to a different device
1079 const std::string deviceSameCategoryName = "some_device_with_invalid_parent_category";
1080 const Device* deviceSameCategory = nullptr;
1081 BOOST_CHECK_THROW(deviceSameCategory = counterDirectory.RegisterDevice(deviceSameCategoryName, 0, categoryName),
1082 armnn::InvalidArgumentException);
1083 BOOST_CHECK(counterDirectory.GetDeviceCount() == 3);
1084 BOOST_CHECK(!deviceSameCategory);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001085}
1086
1087BOOST_AUTO_TEST_CASE(CheckCounterDirectoryRegisterCounterSet)
1088{
1089 CounterDirectory counterDirectory;
1090 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
1091 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
1092 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
1093 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1094
1095 // Register a counter set with an invalid name
1096 const CounterSet* noCounterSet = nullptr;
1097 BOOST_CHECK_THROW(noCounterSet = counterDirectory.RegisterCounterSet(""), armnn::InvalidArgumentException);
1098 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
1099 BOOST_CHECK(!noCounterSet);
1100
1101 // Register a counter set with an invalid name
1102 BOOST_CHECK_THROW(noCounterSet = counterDirectory.RegisterCounterSet("invalid name"),
1103 armnn::InvalidArgumentException);
1104 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
1105 BOOST_CHECK(!noCounterSet);
1106
1107 // Register a new counter set with no count or parent category
1108 const std::string counterSetName = "some_counter_set";
1109 const CounterSet* counterSet = nullptr;
1110 BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
1111 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1112 BOOST_CHECK(counterSet);
1113 BOOST_CHECK(counterSet->m_Name == counterSetName);
1114 BOOST_CHECK(counterSet->m_Uid >= 1);
1115 BOOST_CHECK(counterSet->m_Count == 0);
1116
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001117 // Try getting an unregistered counter set
1118 const CounterSet* unregisteredCounterSet = counterDirectory.GetCounterSet(9999);
1119 BOOST_CHECK(!unregisteredCounterSet);
1120
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001121 // Get the registered counter set
1122 const CounterSet* registeredCounterSet = counterDirectory.GetCounterSet(counterSet->m_Uid);
1123 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1124 BOOST_CHECK(registeredCounterSet);
1125 BOOST_CHECK(registeredCounterSet == counterSet);
1126
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001127 // Register a counter set with the name of a counter set already registered
1128 const CounterSet* counterSetSameName = nullptr;
1129 BOOST_CHECK_THROW(counterSetSameName = counterDirectory.RegisterCounterSet(counterSetName),
1130 armnn::InvalidArgumentException);
1131 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1132 BOOST_CHECK(!counterSetSameName);
1133
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001134 // Register a new counter set with count and no parent category
1135 const std::string counterSetWCountName = "some_counter_set_with_count";
1136 const CounterSet* counterSetWCount = nullptr;
1137 BOOST_CHECK_NO_THROW(counterSetWCount = counterDirectory.RegisterCounterSet(counterSetWCountName, 37));
1138 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 2);
1139 BOOST_CHECK(counterSetWCount);
1140 BOOST_CHECK(counterSetWCount->m_Name == counterSetWCountName);
1141 BOOST_CHECK(counterSetWCount->m_Uid >= 1);
1142 BOOST_CHECK(counterSetWCount->m_Uid > counterSet->m_Uid);
1143 BOOST_CHECK(counterSetWCount->m_Count == 37);
1144
1145 // Get the registered counter set
1146 const CounterSet* registeredCounterSetWCount = counterDirectory.GetCounterSet(counterSetWCount->m_Uid);
1147 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 2);
1148 BOOST_CHECK(registeredCounterSetWCount);
1149 BOOST_CHECK(registeredCounterSetWCount == counterSetWCount);
1150 BOOST_CHECK(registeredCounterSetWCount != counterSet);
1151
1152 // Register a new counter set with count and invalid parent category
1153 const std::string counterSetWCountWInvalidParentCategoryName = "some_counter_set_with_count_"
1154 "with_invalid_parent_category";
1155 const CounterSet* counterSetWCountWInvalidParentCategory = nullptr;
1156 BOOST_CHECK_THROW(counterSetWCountWInvalidParentCategory
1157 = counterDirectory.RegisterCounterSet(counterSetWCountWInvalidParentCategoryName,
1158 42,
1159 std::string("")),
1160 armnn::InvalidArgumentException);
1161 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 2);
1162 BOOST_CHECK(!counterSetWCountWInvalidParentCategory);
1163
1164 // Register a new counter set with count and invalid parent category
1165 const std::string counterSetWCountWInvalidParentCategoryName2 = "some_counter_set_with_count_"
1166 "with_invalid_parent_category2";
1167 const CounterSet* counterSetWCountWInvalidParentCategory2 = nullptr;
1168 BOOST_CHECK_THROW(counterSetWCountWInvalidParentCategory2
1169 = counterDirectory.RegisterCounterSet(counterSetWCountWInvalidParentCategoryName2,
1170 42,
1171 std::string("invalid_parent_category")),
1172 armnn::InvalidArgumentException);
1173 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 2);
1174 BOOST_CHECK(!counterSetWCountWInvalidParentCategory2);
1175
1176 // Register a category for testing
1177 const std::string categoryName = "some_category";
1178 const Category* category = nullptr;
1179 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
1180 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
1181 BOOST_CHECK(category);
1182 BOOST_CHECK(category->m_Name == categoryName);
1183 BOOST_CHECK(category->m_Counters.empty());
1184 BOOST_CHECK(category->m_DeviceUid == 0);
1185 BOOST_CHECK(category->m_CounterSetUid == 0);
1186
1187 // Register a new counter set with count and valid parent category
1188 const std::string counterSetWCountWValidParentCategoryName = "some_counter_set_with_count_"
1189 "with_valid_parent_category";
1190 const CounterSet* counterSetWCountWValidParentCategory = nullptr;
1191 BOOST_CHECK_NO_THROW(counterSetWCountWValidParentCategory
1192 = counterDirectory.RegisterCounterSet(counterSetWCountWValidParentCategoryName,
1193 42,
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001194 categoryName));
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001195 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 3);
1196 BOOST_CHECK(counterSetWCountWValidParentCategory);
1197 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Name == counterSetWCountWValidParentCategoryName);
1198 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Uid >= 1);
1199 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Uid > counterSet->m_Uid);
1200 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Uid > counterSetWCount->m_Uid);
1201 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Count == 42);
1202 BOOST_CHECK(category->m_CounterSetUid == counterSetWCountWValidParentCategory->m_Uid);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001203
1204 // Register a counter set associated to a category already associated to a different counter set
1205 const std::string counterSetSameCategoryName = "some_counter_set_with_invalid_parent_category";
1206 const CounterSet* counterSetSameCategory = nullptr;
1207 BOOST_CHECK_THROW(counterSetSameCategory = counterDirectory.RegisterCounterSet(counterSetSameCategoryName,
1208 0,
1209 categoryName),
1210 armnn::InvalidArgumentException);
1211 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 3);
1212 BOOST_CHECK(!counterSetSameCategory);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001213}
1214
1215BOOST_AUTO_TEST_CASE(CheckCounterDirectoryRegisterCounter)
1216{
1217 CounterDirectory counterDirectory;
1218 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
1219 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
1220 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
1221 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1222
1223 // Register a counter with an invalid parent category name
1224 const Counter* noCounter = nullptr;
1225 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("",
1226 0,
1227 1,
1228 123.45f,
1229 "valid name",
1230 "valid description"),
1231 armnn::InvalidArgumentException);
1232 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1233 BOOST_CHECK(!noCounter);
1234
1235 // Register a counter with an invalid parent category name
1236 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("invalid parent category",
1237 0,
1238 1,
1239 123.45f,
1240 "valid name",
1241 "valid description"),
1242 armnn::InvalidArgumentException);
1243 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1244 BOOST_CHECK(!noCounter);
1245
1246 // Register a counter with an invalid class
1247 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("valid_parent_category",
1248 2,
1249 1,
1250 123.45f,
1251 "valid name",
1252 "valid description"),
1253 armnn::InvalidArgumentException);
1254 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1255 BOOST_CHECK(!noCounter);
1256
1257 // Register a counter with an invalid interpolation
1258 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("valid_parent_category",
1259 0,
1260 3,
1261 123.45f,
1262 "valid name",
1263 "valid description"),
1264 armnn::InvalidArgumentException);
1265 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1266 BOOST_CHECK(!noCounter);
1267
1268 // Register a counter with an invalid multiplier
1269 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("valid_parent_category",
1270 0,
1271 1,
1272 .0f,
1273 "valid name",
1274 "valid description"),
1275 armnn::InvalidArgumentException);
1276 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1277 BOOST_CHECK(!noCounter);
1278
1279 // Register a counter with an invalid name
1280 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("valid_parent_category",
1281 0,
1282 1,
1283 123.45f,
1284 "",
1285 "valid description"),
1286 armnn::InvalidArgumentException);
1287 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1288 BOOST_CHECK(!noCounter);
1289
1290 // Register a counter with an invalid name
1291 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("valid_parent_category",
1292 0,
1293 1,
1294 123.45f,
1295 "invalid nam€",
1296 "valid description"),
1297 armnn::InvalidArgumentException);
1298 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1299 BOOST_CHECK(!noCounter);
1300
1301 // Register a counter with an invalid description
1302 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("valid_parent_category",
1303 0,
1304 1,
1305 123.45f,
1306 "valid name",
1307 ""),
1308 armnn::InvalidArgumentException);
1309 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1310 BOOST_CHECK(!noCounter);
1311
1312 // Register a counter with an invalid description
1313 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("valid_parent_category",
1314 0,
1315 1,
1316 123.45f,
1317 "valid name",
1318 "inv@lid description"),
1319 armnn::InvalidArgumentException);
1320 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1321 BOOST_CHECK(!noCounter);
1322
1323 // Register a counter with an invalid unit2
1324 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("valid_parent_category",
1325 0,
1326 1,
1327 123.45f,
1328 "valid name",
1329 "valid description",
1330 std::string("Mb/s2")),
1331 armnn::InvalidArgumentException);
1332 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1333 BOOST_CHECK(!noCounter);
1334
1335 // Register a counter with a non-existing parent category name
1336 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("invalid_parent_category",
1337 0,
1338 1,
1339 123.45f,
1340 "valid name",
1341 "valid description"),
1342 armnn::InvalidArgumentException);
1343 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1344 BOOST_CHECK(!noCounter);
1345
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001346 // Try getting an unregistered counter
1347 const Counter* unregisteredCounter = counterDirectory.GetCounter(9999);
1348 BOOST_CHECK(!unregisteredCounter);
1349
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001350 // Register a category for testing
1351 const std::string categoryName = "some_category";
1352 const Category* category = nullptr;
1353 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
1354 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
1355 BOOST_CHECK(category);
1356 BOOST_CHECK(category->m_Name == categoryName);
1357 BOOST_CHECK(category->m_Counters.empty());
1358 BOOST_CHECK(category->m_DeviceUid == 0);
1359 BOOST_CHECK(category->m_CounterSetUid == 0);
1360
1361 // Register a counter with a valid parent category name
1362 const Counter* counter = nullptr;
1363 BOOST_CHECK_NO_THROW(counter = counterDirectory.RegisterCounter(categoryName,
1364 0,
1365 1,
1366 123.45f,
1367 "valid name",
1368 "valid description"));
1369 BOOST_CHECK(counterDirectory.GetCounterCount() == 1);
1370 BOOST_CHECK(counter);
1371 BOOST_CHECK(counter->m_Uid >= 0);
1372 BOOST_CHECK(counter->m_MaxCounterUid == counter->m_Uid);
1373 BOOST_CHECK(counter->m_Class == 0);
1374 BOOST_CHECK(counter->m_Interpolation == 1);
1375 BOOST_CHECK(counter->m_Multiplier == 123.45f);
1376 BOOST_CHECK(counter->m_Name == "valid name");
1377 BOOST_CHECK(counter->m_Description == "valid description");
1378 BOOST_CHECK(counter->m_Units == "");
1379 BOOST_CHECK(counter->m_DeviceUid == 0);
1380 BOOST_CHECK(counter->m_CounterSetUid == 0);
1381 BOOST_CHECK(category->m_Counters.size() == 1);
1382 BOOST_CHECK(category->m_Counters.back() == counter->m_Uid);
1383
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001384 // Register a counter with a name of a counter already registered for the given parent category name
1385 const Counter* counterSameName = nullptr;
1386 BOOST_CHECK_THROW(counterSameName = counterDirectory.RegisterCounter(categoryName,
1387 0,
1388 0,
1389 1.0f,
1390 "valid name",
1391 "valid description"),
1392 armnn::InvalidArgumentException);
1393 BOOST_CHECK(counterDirectory.GetCounterCount() == 1);
1394 BOOST_CHECK(!counterSameName);
1395
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001396 // Register a counter with a valid parent category name and units
1397 const Counter* counterWUnits = nullptr;
1398 BOOST_CHECK_NO_THROW(counterWUnits = counterDirectory.RegisterCounter(categoryName,
1399 0,
1400 1,
1401 123.45f,
1402 "valid name 2",
1403 "valid description",
1404 std::string("Mnnsq2"))); // Units
1405 BOOST_CHECK(counterDirectory.GetCounterCount() == 2);
1406 BOOST_CHECK(counterWUnits);
1407 BOOST_CHECK(counterWUnits->m_Uid >= 0);
1408 BOOST_CHECK(counterWUnits->m_Uid > counter->m_Uid);
1409 BOOST_CHECK(counterWUnits->m_MaxCounterUid == counterWUnits->m_Uid);
1410 BOOST_CHECK(counterWUnits->m_Class == 0);
1411 BOOST_CHECK(counterWUnits->m_Interpolation == 1);
1412 BOOST_CHECK(counterWUnits->m_Multiplier == 123.45f);
1413 BOOST_CHECK(counterWUnits->m_Name == "valid name 2");
1414 BOOST_CHECK(counterWUnits->m_Description == "valid description");
1415 BOOST_CHECK(counterWUnits->m_Units == "Mnnsq2");
1416 BOOST_CHECK(counterWUnits->m_DeviceUid == 0);
1417 BOOST_CHECK(counterWUnits->m_CounterSetUid == 0);
1418 BOOST_CHECK(category->m_Counters.size() == 2);
1419 BOOST_CHECK(category->m_Counters.back() == counterWUnits->m_Uid);
1420
1421 // Register a counter with a valid parent category name and not associated with a device
1422 const Counter* counterWoDevice = nullptr;
1423 BOOST_CHECK_NO_THROW(counterWoDevice = counterDirectory.RegisterCounter(categoryName,
1424 0,
1425 1,
1426 123.45f,
1427 "valid name 3",
1428 "valid description",
1429 armnn::EmptyOptional(), // Units
1430 armnn::EmptyOptional(), // Number of cores
1431 0)); // Device UID
1432 BOOST_CHECK(counterDirectory.GetCounterCount() == 3);
1433 BOOST_CHECK(counterWoDevice);
1434 BOOST_CHECK(counterWoDevice->m_Uid >= 0);
1435 BOOST_CHECK(counterWoDevice->m_Uid > counter->m_Uid);
1436 BOOST_CHECK(counterWoDevice->m_MaxCounterUid == counterWoDevice->m_Uid);
1437 BOOST_CHECK(counterWoDevice->m_Class == 0);
1438 BOOST_CHECK(counterWoDevice->m_Interpolation == 1);
1439 BOOST_CHECK(counterWoDevice->m_Multiplier == 123.45f);
1440 BOOST_CHECK(counterWoDevice->m_Name == "valid name 3");
1441 BOOST_CHECK(counterWoDevice->m_Description == "valid description");
1442 BOOST_CHECK(counterWoDevice->m_Units == "");
1443 BOOST_CHECK(counterWoDevice->m_DeviceUid == 0);
1444 BOOST_CHECK(counterWoDevice->m_CounterSetUid == 0);
1445 BOOST_CHECK(category->m_Counters.size() == 3);
1446 BOOST_CHECK(category->m_Counters.back() == counterWoDevice->m_Uid);
1447
1448 // Register a counter with a valid parent category name and associated to an invalid device
1449 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(categoryName,
1450 0,
1451 1,
1452 123.45f,
1453 "valid name 4",
1454 "valid description",
1455 armnn::EmptyOptional(), // Units
1456 armnn::EmptyOptional(), // Number of cores
1457 100), // Device UID
1458 armnn::InvalidArgumentException);
1459 BOOST_CHECK(counterDirectory.GetCounterCount() == 3);
1460 BOOST_CHECK(!noCounter);
1461
1462 // Register a device for testing
1463 const std::string deviceName = "some_device";
1464 const Device* device = nullptr;
1465 BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName));
1466 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
1467 BOOST_CHECK(device);
1468 BOOST_CHECK(device->m_Name == deviceName);
1469 BOOST_CHECK(device->m_Uid >= 1);
1470 BOOST_CHECK(device->m_Cores == 0);
1471
1472 // Register a counter with a valid parent category name and associated to a device
1473 const Counter* counterWDevice = nullptr;
1474 BOOST_CHECK_NO_THROW(counterWDevice = counterDirectory.RegisterCounter(categoryName,
1475 0,
1476 1,
1477 123.45f,
1478 "valid name 5",
1479 "valid description",
1480 armnn::EmptyOptional(), // Units
1481 armnn::EmptyOptional(), // Number of cores
1482 device->m_Uid)); // Device UID
1483 BOOST_CHECK(counterDirectory.GetCounterCount() == 4);
1484 BOOST_CHECK(counterWDevice);
1485 BOOST_CHECK(counterWDevice->m_Uid >= 0);
1486 BOOST_CHECK(counterWDevice->m_Uid > counter->m_Uid);
1487 BOOST_CHECK(counterWDevice->m_MaxCounterUid == counterWDevice->m_Uid);
1488 BOOST_CHECK(counterWDevice->m_Class == 0);
1489 BOOST_CHECK(counterWDevice->m_Interpolation == 1);
1490 BOOST_CHECK(counterWDevice->m_Multiplier == 123.45f);
1491 BOOST_CHECK(counterWDevice->m_Name == "valid name 5");
1492 BOOST_CHECK(counterWDevice->m_Description == "valid description");
1493 BOOST_CHECK(counterWDevice->m_Units == "");
1494 BOOST_CHECK(counterWDevice->m_DeviceUid == device->m_Uid);
1495 BOOST_CHECK(counterWDevice->m_CounterSetUid == 0);
1496 BOOST_CHECK(category->m_Counters.size() == 4);
1497 BOOST_CHECK(category->m_Counters.back() == counterWDevice->m_Uid);
1498
1499 // Register a counter with a valid parent category name and not associated with a counter set
1500 const Counter* counterWoCounterSet = nullptr;
1501 BOOST_CHECK_NO_THROW(counterWoCounterSet
1502 = counterDirectory.RegisterCounter(categoryName,
1503 0,
1504 1,
1505 123.45f,
1506 "valid name 6",
1507 "valid description",
1508 armnn::EmptyOptional(), // Units
1509 armnn::EmptyOptional(), // Number of cores
1510 armnn::EmptyOptional(), // Device UID
1511 0)); // Counter set UID
1512 BOOST_CHECK(counterDirectory.GetCounterCount() == 5);
1513 BOOST_CHECK(counterWoCounterSet);
1514 BOOST_CHECK(counterWoCounterSet->m_Uid >= 0);
1515 BOOST_CHECK(counterWoCounterSet->m_Uid > counter->m_Uid);
1516 BOOST_CHECK(counterWoCounterSet->m_MaxCounterUid == counterWoCounterSet->m_Uid);
1517 BOOST_CHECK(counterWoCounterSet->m_Class == 0);
1518 BOOST_CHECK(counterWoCounterSet->m_Interpolation == 1);
1519 BOOST_CHECK(counterWoCounterSet->m_Multiplier == 123.45f);
1520 BOOST_CHECK(counterWoCounterSet->m_Name == "valid name 6");
1521 BOOST_CHECK(counterWoCounterSet->m_Description == "valid description");
1522 BOOST_CHECK(counterWoCounterSet->m_Units == "");
1523 BOOST_CHECK(counterWoCounterSet->m_DeviceUid == 0);
1524 BOOST_CHECK(counterWoCounterSet->m_CounterSetUid == 0);
1525 BOOST_CHECK(category->m_Counters.size() == 5);
1526 BOOST_CHECK(category->m_Counters.back() == counterWoCounterSet->m_Uid);
1527
1528 // Register a counter with a valid parent category name and associated to an invalid counter set
1529 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(categoryName,
1530 0,
1531 1,
1532 123.45f,
1533 "valid name 7",
1534 "valid description",
1535 armnn::EmptyOptional(), // Units
1536 armnn::EmptyOptional(), // Number of cores
1537 armnn::EmptyOptional(), // Device UID
1538 100), // Counter set UID
1539 armnn::InvalidArgumentException);
1540 BOOST_CHECK(counterDirectory.GetCounterCount() == 5);
1541 BOOST_CHECK(!noCounter);
1542
1543 // Register a counter with a valid parent category name and with a given number of cores
1544 const Counter* counterWNumberOfCores = nullptr;
1545 uint16_t numberOfCores = 15;
1546 BOOST_CHECK_NO_THROW(counterWNumberOfCores
1547 = counterDirectory.RegisterCounter(categoryName,
1548 0,
1549 1,
1550 123.45f,
1551 "valid name 8",
1552 "valid description",
1553 armnn::EmptyOptional(), // Units
1554 numberOfCores, // Number of cores
1555 armnn::EmptyOptional(), // Device UID
1556 armnn::EmptyOptional())); // Counter set UID
1557 BOOST_CHECK(counterDirectory.GetCounterCount() == 20);
1558 BOOST_CHECK(counterWNumberOfCores);
1559 BOOST_CHECK(counterWNumberOfCores->m_Uid >= 0);
1560 BOOST_CHECK(counterWNumberOfCores->m_Uid > counter->m_Uid);
1561 BOOST_CHECK(counterWNumberOfCores->m_MaxCounterUid == counterWNumberOfCores->m_Uid + numberOfCores - 1);
1562 BOOST_CHECK(counterWNumberOfCores->m_Class == 0);
1563 BOOST_CHECK(counterWNumberOfCores->m_Interpolation == 1);
1564 BOOST_CHECK(counterWNumberOfCores->m_Multiplier == 123.45f);
1565 BOOST_CHECK(counterWNumberOfCores->m_Name == "valid name 8");
1566 BOOST_CHECK(counterWNumberOfCores->m_Description == "valid description");
1567 BOOST_CHECK(counterWNumberOfCores->m_Units == "");
1568 BOOST_CHECK(counterWNumberOfCores->m_DeviceUid == 0);
1569 BOOST_CHECK(counterWNumberOfCores->m_CounterSetUid == 0);
1570 BOOST_CHECK(category->m_Counters.size() == 20);
1571 for (size_t i = 0; i < numberOfCores; i ++)
1572 {
1573 BOOST_CHECK(category->m_Counters[category->m_Counters.size() - numberOfCores + i] ==
1574 counterWNumberOfCores->m_Uid + i);
1575 }
1576
1577 // Register a multi-core device for testing
1578 const std::string multiCoreDeviceName = "some_multi_core_device";
1579 const Device* multiCoreDevice = nullptr;
1580 BOOST_CHECK_NO_THROW(multiCoreDevice = counterDirectory.RegisterDevice(multiCoreDeviceName, 4));
1581 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
1582 BOOST_CHECK(multiCoreDevice);
1583 BOOST_CHECK(multiCoreDevice->m_Name == multiCoreDeviceName);
1584 BOOST_CHECK(multiCoreDevice->m_Uid >= 1);
1585 BOOST_CHECK(multiCoreDevice->m_Cores == 4);
1586
1587 // Register a counter with a valid parent category name and associated to the multi-core device
1588 const Counter* counterWMultiCoreDevice = nullptr;
1589 BOOST_CHECK_NO_THROW(counterWMultiCoreDevice
1590 = counterDirectory.RegisterCounter(categoryName,
1591 0,
1592 1,
1593 123.45f,
1594 "valid name 9",
1595 "valid description",
1596 armnn::EmptyOptional(), // Units
1597 armnn::EmptyOptional(), // Number of cores
1598 multiCoreDevice->m_Uid, // Device UID
1599 armnn::EmptyOptional())); // Counter set UID
1600 BOOST_CHECK(counterDirectory.GetCounterCount() == 24);
1601 BOOST_CHECK(counterWMultiCoreDevice);
1602 BOOST_CHECK(counterWMultiCoreDevice->m_Uid >= 0);
1603 BOOST_CHECK(counterWMultiCoreDevice->m_Uid > counter->m_Uid);
1604 BOOST_CHECK(counterWMultiCoreDevice->m_MaxCounterUid ==
1605 counterWMultiCoreDevice->m_Uid + multiCoreDevice->m_Cores - 1);
1606 BOOST_CHECK(counterWMultiCoreDevice->m_Class == 0);
1607 BOOST_CHECK(counterWMultiCoreDevice->m_Interpolation == 1);
1608 BOOST_CHECK(counterWMultiCoreDevice->m_Multiplier == 123.45f);
1609 BOOST_CHECK(counterWMultiCoreDevice->m_Name == "valid name 9");
1610 BOOST_CHECK(counterWMultiCoreDevice->m_Description == "valid description");
1611 BOOST_CHECK(counterWMultiCoreDevice->m_Units == "");
1612 BOOST_CHECK(counterWMultiCoreDevice->m_DeviceUid == multiCoreDevice->m_Uid);
1613 BOOST_CHECK(counterWMultiCoreDevice->m_CounterSetUid == 0);
1614 BOOST_CHECK(category->m_Counters.size() == 24);
1615 for (size_t i = 0; i < 4; i ++)
1616 {
1617 BOOST_CHECK(category->m_Counters[category->m_Counters.size() - 4 + i] == counterWMultiCoreDevice->m_Uid + i);
1618 }
1619
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001620 // Register a multi-core device associate to a parent category for testing
1621 const std::string multiCoreDeviceNameWParentCategory = "some_multi_core_device_with_parent_category";
1622 const Device* multiCoreDeviceWParentCategory = nullptr;
1623 BOOST_CHECK_NO_THROW(multiCoreDeviceWParentCategory =
1624 counterDirectory.RegisterDevice(multiCoreDeviceNameWParentCategory, 2, categoryName));
1625 BOOST_CHECK(counterDirectory.GetDeviceCount() == 3);
1626 BOOST_CHECK(multiCoreDeviceWParentCategory);
1627 BOOST_CHECK(multiCoreDeviceWParentCategory->m_Name == multiCoreDeviceNameWParentCategory);
1628 BOOST_CHECK(multiCoreDeviceWParentCategory->m_Uid >= 1);
1629 BOOST_CHECK(multiCoreDeviceWParentCategory->m_Cores == 2);
1630
1631 // Register a counter with a valid parent category name and getting the number of cores of the multi-core device
1632 // associated to that category
1633 const Counter* counterWMultiCoreDeviceWParentCategory = nullptr;
1634 BOOST_CHECK_NO_THROW(counterWMultiCoreDeviceWParentCategory
1635 = counterDirectory.RegisterCounter(categoryName,
1636 0,
1637 1,
1638 123.45f,
1639 "valid name 10",
1640 "valid description",
1641 armnn::EmptyOptional(), // Units
1642 armnn::EmptyOptional(), // Number of cores
1643 armnn::EmptyOptional(), // Device UID
1644 armnn::EmptyOptional())); // Counter set UID
1645 BOOST_CHECK(counterDirectory.GetCounterCount() == 26);
1646 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory);
1647 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Uid >= 0);
1648 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Uid > counter->m_Uid);
1649 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_MaxCounterUid ==
1650 counterWMultiCoreDeviceWParentCategory->m_Uid + multiCoreDeviceWParentCategory->m_Cores - 1);
1651 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Class == 0);
1652 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Interpolation == 1);
1653 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Multiplier == 123.45f);
1654 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Name == "valid name 10");
1655 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Description == "valid description");
1656 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Units == "");
1657 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_DeviceUid == 0);
1658 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_CounterSetUid == 0);
1659 BOOST_CHECK(category->m_Counters.size() == 26);
1660 for (size_t i = 0; i < 2; i ++)
1661 {
1662 BOOST_CHECK(category->m_Counters[category->m_Counters.size() - 2 + i] ==
1663 counterWMultiCoreDeviceWParentCategory->m_Uid + i);
1664 }
1665
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001666 // Register a counter set for testing
1667 const std::string counterSetName = "some_counter_set";
1668 const CounterSet* counterSet = nullptr;
1669 BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
1670 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1671 BOOST_CHECK(counterSet);
1672 BOOST_CHECK(counterSet->m_Name == counterSetName);
1673 BOOST_CHECK(counterSet->m_Uid >= 1);
1674 BOOST_CHECK(counterSet->m_Count == 0);
1675
1676 // Register a counter with a valid parent category name and associated to a counter set
1677 const Counter* counterWCounterSet = nullptr;
1678 BOOST_CHECK_NO_THROW(counterWCounterSet
1679 = counterDirectory.RegisterCounter(categoryName,
1680 0,
1681 1,
1682 123.45f,
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001683 "valid name 11",
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001684 "valid description",
1685 armnn::EmptyOptional(), // Units
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001686 0, // Number of cores
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001687 armnn::EmptyOptional(), // Device UID
1688 counterSet->m_Uid)); // Counter set UID
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001689 BOOST_CHECK(counterDirectory.GetCounterCount() == 27);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001690 BOOST_CHECK(counterWCounterSet);
1691 BOOST_CHECK(counterWCounterSet->m_Uid >= 0);
1692 BOOST_CHECK(counterWCounterSet->m_Uid > counter->m_Uid);
1693 BOOST_CHECK(counterWCounterSet->m_MaxCounterUid == counterWCounterSet->m_Uid);
1694 BOOST_CHECK(counterWCounterSet->m_Class == 0);
1695 BOOST_CHECK(counterWCounterSet->m_Interpolation == 1);
1696 BOOST_CHECK(counterWCounterSet->m_Multiplier == 123.45f);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001697 BOOST_CHECK(counterWCounterSet->m_Name == "valid name 11");
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001698 BOOST_CHECK(counterWCounterSet->m_Description == "valid description");
1699 BOOST_CHECK(counterWCounterSet->m_Units == "");
1700 BOOST_CHECK(counterWCounterSet->m_DeviceUid == 0);
1701 BOOST_CHECK(counterWCounterSet->m_CounterSetUid == counterSet->m_Uid);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001702 BOOST_CHECK(category->m_Counters.size() == 27);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001703 BOOST_CHECK(category->m_Counters.back() == counterWCounterSet->m_Uid);
1704
1705 // Register a counter with a valid parent category name and associated to a device and a counter set
1706 const Counter* counterWDeviceWCounterSet = nullptr;
1707 BOOST_CHECK_NO_THROW(counterWDeviceWCounterSet
1708 = counterDirectory.RegisterCounter(categoryName,
1709 0,
1710 1,
1711 123.45f,
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001712 "valid name 12",
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001713 "valid description",
1714 armnn::EmptyOptional(), // Units
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001715 1, // Number of cores
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001716 device->m_Uid, // Device UID
1717 counterSet->m_Uid)); // Counter set UID
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001718 BOOST_CHECK(counterDirectory.GetCounterCount() == 28);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001719 BOOST_CHECK(counterWDeviceWCounterSet);
1720 BOOST_CHECK(counterWDeviceWCounterSet->m_Uid >= 0);
1721 BOOST_CHECK(counterWDeviceWCounterSet->m_Uid > counter->m_Uid);
1722 BOOST_CHECK(counterWDeviceWCounterSet->m_MaxCounterUid == counterWDeviceWCounterSet->m_Uid);
1723 BOOST_CHECK(counterWDeviceWCounterSet->m_Class == 0);
1724 BOOST_CHECK(counterWDeviceWCounterSet->m_Interpolation == 1);
1725 BOOST_CHECK(counterWDeviceWCounterSet->m_Multiplier == 123.45f);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001726 BOOST_CHECK(counterWDeviceWCounterSet->m_Name == "valid name 12");
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001727 BOOST_CHECK(counterWDeviceWCounterSet->m_Description == "valid description");
1728 BOOST_CHECK(counterWDeviceWCounterSet->m_Units == "");
1729 BOOST_CHECK(counterWDeviceWCounterSet->m_DeviceUid == device->m_Uid);
1730 BOOST_CHECK(counterWDeviceWCounterSet->m_CounterSetUid == counterSet->m_Uid);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001731 BOOST_CHECK(category->m_Counters.size() == 28);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001732 BOOST_CHECK(category->m_Counters.back() == counterWDeviceWCounterSet->m_Uid);
1733
1734 // Register another category for testing
1735 const std::string anotherCategoryName = "some_other_category";
1736 const Category* anotherCategory = nullptr;
1737 BOOST_CHECK_NO_THROW(anotherCategory = counterDirectory.RegisterCategory(anotherCategoryName));
1738 BOOST_CHECK(counterDirectory.GetCategoryCount() == 2);
1739 BOOST_CHECK(anotherCategory);
1740 BOOST_CHECK(anotherCategory != category);
1741 BOOST_CHECK(anotherCategory->m_Name == anotherCategoryName);
1742 BOOST_CHECK(anotherCategory->m_Counters.empty());
1743 BOOST_CHECK(anotherCategory->m_DeviceUid == 0);
1744 BOOST_CHECK(anotherCategory->m_CounterSetUid == 0);
1745
1746 // Register a counter to the other category
1747 const Counter* anotherCounter = nullptr;
1748 BOOST_CHECK_NO_THROW(anotherCounter = counterDirectory.RegisterCounter(anotherCategoryName,
1749 1,
1750 0,
1751 .00043f,
1752 "valid name",
1753 "valid description",
1754 armnn::EmptyOptional(), // Units
1755 armnn::EmptyOptional(), // Number of cores
1756 device->m_Uid, // Device UID
1757 counterSet->m_Uid)); // Counter set UID
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001758 BOOST_CHECK(counterDirectory.GetCounterCount() == 29);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001759 BOOST_CHECK(anotherCounter);
1760 BOOST_CHECK(anotherCounter->m_Uid >= 0);
1761 BOOST_CHECK(anotherCounter->m_MaxCounterUid == anotherCounter->m_Uid);
1762 BOOST_CHECK(anotherCounter->m_Class == 1);
1763 BOOST_CHECK(anotherCounter->m_Interpolation == 0);
1764 BOOST_CHECK(anotherCounter->m_Multiplier == .00043f);
1765 BOOST_CHECK(anotherCounter->m_Name == "valid name");
1766 BOOST_CHECK(anotherCounter->m_Description == "valid description");
1767 BOOST_CHECK(anotherCounter->m_Units == "");
1768 BOOST_CHECK(anotherCounter->m_DeviceUid == device->m_Uid);
1769 BOOST_CHECK(anotherCounter->m_CounterSetUid == counterSet->m_Uid);
1770 BOOST_CHECK(anotherCategory->m_Counters.size() == 1);
1771 BOOST_CHECK(anotherCategory->m_Counters.back() == anotherCounter->m_Uid);
Matteo Martincighab173e92019-09-05 12:02:04 +01001772}
1773
Ferran Balaguer1b941722019-08-28 16:57:18 +01001774BOOST_AUTO_TEST_CASE(CounterSelectionCommandHandlerParseData)
1775{
1776 using boost::numeric_cast;
1777
1778 class TestCaptureThread : public IPeriodicCounterCapture
1779 {
1780 void Start() override {};
1781 };
1782
1783 const uint32_t packetId = 0x40000;
1784
1785 uint32_t version = 1;
1786 Holder holder;
1787 TestCaptureThread captureThread;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001788 MockProfilingConnection mockProfilingConnection;
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001789 MockBufferManager mockBuffer(512);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001790 SendCounterPacket sendCounterPacket(mockProfilingConnection, mockBuffer);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001791
1792 uint32_t sizeOfUint32 = numeric_cast<uint32_t>(sizeof(uint32_t));
1793 uint32_t sizeOfUint16 = numeric_cast<uint32_t>(sizeof(uint16_t));
1794
1795 // Data with period and counters
1796 uint32_t period1 = 10;
1797 uint32_t dataLength1 = 8;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001798 uint32_t offset = 0;
1799
FinnWilliamsArma0c78712019-09-16 12:06:47 +01001800 std::unique_ptr<char[]> uniqueData1 = std::make_unique<char[]>(dataLength1);
1801 unsigned char* data1 = reinterpret_cast<unsigned char*>(uniqueData1.get());
1802
Ferran Balaguer1b941722019-08-28 16:57:18 +01001803 WriteUint32(data1, offset, period1);
1804 offset += sizeOfUint32;
1805 WriteUint16(data1, offset, 4000);
1806 offset += sizeOfUint16;
1807 WriteUint16(data1, offset, 5000);
1808
FinnWilliamsArma0c78712019-09-16 12:06:47 +01001809 Packet packetA(packetId, dataLength1, uniqueData1);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001810
1811 PeriodicCounterSelectionCommandHandler commandHandler(packetId, version, holder, captureThread,
1812 sendCounterPacket);
1813 commandHandler(packetA);
1814
1815 std::vector<uint16_t> counterIds = holder.GetCaptureData().GetCounterIds();
1816
1817 BOOST_TEST(holder.GetCaptureData().GetCapturePeriod() == period1);
1818 BOOST_TEST(counterIds.size() == 2);
1819 BOOST_TEST(counterIds[0] == 4000);
1820 BOOST_TEST(counterIds[1] == 5000);
1821
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001822 auto readBuffer = mockBuffer.GetReadableBuffer();
Ferran Balaguer1b941722019-08-28 16:57:18 +01001823
1824 offset = 0;
1825
1826 uint32_t headerWord0 = ReadUint32(readBuffer, offset);
1827 offset += sizeOfUint32;
1828 uint32_t headerWord1 = ReadUint32(readBuffer, offset);
1829 offset += sizeOfUint32;
1830 uint32_t period = ReadUint32(readBuffer, offset);
1831
1832 BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family
1833 BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 4); // packet id
1834 BOOST_TEST(headerWord1 == 8); // data lenght
1835 BOOST_TEST(period == 10); // capture period
1836
1837 uint16_t counterId = 0;
1838 offset += sizeOfUint32;
1839 counterId = ReadUint16(readBuffer, offset);
1840 BOOST_TEST(counterId == 4000);
1841 offset += sizeOfUint16;
1842 counterId = ReadUint16(readBuffer, offset);
1843 BOOST_TEST(counterId == 5000);
1844
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001845 mockBuffer.MarkRead(readBuffer);
1846
Ferran Balaguer1b941722019-08-28 16:57:18 +01001847 // Data with period only
1848 uint32_t period2 = 11;
1849 uint32_t dataLength2 = 4;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001850
FinnWilliamsArma0c78712019-09-16 12:06:47 +01001851 std::unique_ptr<char[]> uniqueData2 = std::make_unique<char[]>(dataLength2);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001852
FinnWilliamsArma0c78712019-09-16 12:06:47 +01001853 WriteUint32(reinterpret_cast<unsigned char*>(uniqueData2.get()), 0, period2);
1854
1855 Packet packetB(packetId, dataLength2, uniqueData2);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001856
1857 commandHandler(packetB);
1858
1859 counterIds = holder.GetCaptureData().GetCounterIds();
1860
1861 BOOST_TEST(holder.GetCaptureData().GetCapturePeriod() == period2);
1862 BOOST_TEST(counterIds.size() == 0);
1863
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001864 readBuffer = mockBuffer.GetReadableBuffer();
Ferran Balaguer1b941722019-08-28 16:57:18 +01001865
1866 offset = 0;
1867
1868 headerWord0 = ReadUint32(readBuffer, offset);
1869 offset += sizeOfUint32;
1870 headerWord1 = ReadUint32(readBuffer, offset);
1871 offset += sizeOfUint32;
1872 period = ReadUint32(readBuffer, offset);
1873
1874 BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family
1875 BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 4); // packet id
1876 BOOST_TEST(headerWord1 == 4); // data lenght
1877 BOOST_TEST(period == 11); // capture period
Ferran Balaguer1b941722019-08-28 16:57:18 +01001878}
1879
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001880BOOST_AUTO_TEST_CASE(CheckConnectionAcknowledged)
1881{
1882 using boost::numeric_cast;
1883
1884 const uint32_t connectionPacketId = 0x10000;
1885 const uint32_t version = 1;
1886
1887 uint32_t sizeOfUint32 = numeric_cast<uint32_t>(sizeof(uint32_t));
1888 uint32_t sizeOfUint16 = numeric_cast<uint32_t>(sizeof(uint16_t));
1889
1890 // Data with period and counters
1891 uint32_t period1 = 10;
1892 uint32_t dataLength1 = 8;
1893 uint32_t offset = 0;
1894
1895 std::unique_ptr<char[]> uniqueData1 = std::make_unique<char[]>(dataLength1);
1896 unsigned char* data1 = reinterpret_cast<unsigned char*>(uniqueData1.get());
1897
1898 WriteUint32(data1, offset, period1);
1899 offset += sizeOfUint32;
1900 WriteUint16(data1, offset, 4000);
1901 offset += sizeOfUint16;
1902 WriteUint16(data1, offset, 5000);
1903
1904 Packet packetA(connectionPacketId, dataLength1, uniqueData1);
1905
1906 ProfilingStateMachine profilingState(ProfilingState::Uninitialised);
1907 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::Uninitialised);
1908
1909 ConnectionAcknowledgedCommandHandler commandHandler(connectionPacketId, version, profilingState);
1910
1911 // command handler received packet on ProfilingState::Uninitialised
1912 BOOST_CHECK_THROW(commandHandler(packetA), armnn::Exception);
1913
1914 profilingState.TransitionToState(ProfilingState::NotConnected);
1915 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::NotConnected);
1916 // command handler received packet on ProfilingState::NotConnected
1917 BOOST_CHECK_THROW(commandHandler(packetA), armnn::Exception);
1918
1919 profilingState.TransitionToState(ProfilingState::WaitingForAck);
1920 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::WaitingForAck);
1921 // command handler received packet on ProfilingState::WaitingForAck
1922 commandHandler(packetA);
1923 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::Active);
1924
1925 // command handler received packet on ProfilingState::Active
1926 commandHandler(packetA);
1927 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::Active);
1928
1929 // command handler received different packet
1930 const uint32_t differentPacketId = 0x40000;
1931 Packet packetB(differentPacketId, dataLength1, uniqueData1);
1932 ConnectionAcknowledgedCommandHandler differentCommandHandler(differentPacketId, version, profilingState);
1933 BOOST_CHECK_THROW(differentCommandHandler(packetB), armnn::Exception);
1934}
1935
Teresa Charlin9bab4962019-09-06 12:28:35 +01001936BOOST_AUTO_TEST_CASE(CheckSocketProfilingConnection)
1937{
1938 // Check that creating a SocketProfilingConnection results in an exception as the Gator UDS doesn't exist.
1939 BOOST_CHECK_THROW(new SocketProfilingConnection(), armnn::Exception);
1940}
1941
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001942BOOST_AUTO_TEST_CASE(SwTraceIsValidCharTest)
1943{
1944 // Only ASCII 7-bit encoding supported
1945 for (unsigned char c = 0; c < 128; c++)
1946 {
1947 BOOST_CHECK(SwTraceCharPolicy::IsValidChar(c));
1948 }
1949
1950 // Not ASCII
1951 for (unsigned char c = 255; c >= 128; c++)
1952 {
1953 BOOST_CHECK(!SwTraceCharPolicy::IsValidChar(c));
1954 }
1955}
1956
1957BOOST_AUTO_TEST_CASE(SwTraceIsValidNameCharTest)
1958{
1959 // Only alpha-numeric and underscore ASCII 7-bit encoding supported
1960 const unsigned char validChars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
1961 for (unsigned char i = 0; i < sizeof(validChars) / sizeof(validChars[0]) - 1; i++)
1962 {
1963 BOOST_CHECK(SwTraceNameCharPolicy::IsValidChar(validChars[i]));
1964 }
1965
1966 // Non alpha-numeric chars
1967 for (unsigned char c = 0; c < 48; c++)
1968 {
1969 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1970 }
1971 for (unsigned char c = 58; c < 65; c++)
1972 {
1973 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1974 }
1975 for (unsigned char c = 91; c < 95; c++)
1976 {
1977 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1978 }
1979 for (unsigned char c = 96; c < 97; c++)
1980 {
1981 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1982 }
1983 for (unsigned char c = 123; c < 128; c++)
1984 {
1985 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1986 }
1987
1988 // Not ASCII
1989 for (unsigned char c = 255; c >= 128; c++)
1990 {
1991 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1992 }
1993}
1994
1995BOOST_AUTO_TEST_CASE(IsValidSwTraceStringTest)
1996{
1997 // Valid SWTrace strings
1998 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>(""));
1999 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("_"));
2000 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("0123"));
2001 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("valid_string"));
2002 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("VALID_string_456"));
2003 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>(" "));
2004 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("valid string"));
2005 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("!$%"));
2006 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("valid|\\~string#123"));
2007
2008 // Invalid SWTrace strings
2009 BOOST_CHECK(!IsValidSwTraceString<SwTraceCharPolicy>("€£"));
2010 BOOST_CHECK(!IsValidSwTraceString<SwTraceCharPolicy>("invalid‡string"));
2011 BOOST_CHECK(!IsValidSwTraceString<SwTraceCharPolicy>("12Ž34"));
2012}
2013
2014BOOST_AUTO_TEST_CASE(IsValidSwTraceNameStringTest)
2015{
2016 // Valid SWTrace name strings
2017 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>(""));
2018 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>("_"));
2019 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>("0123"));
2020 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>("valid_string"));
2021 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>("VALID_string_456"));
2022
2023 // Invalid SWTrace name strings
2024 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>(" "));
2025 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("invalid string"));
2026 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("!$%"));
2027 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("invalid|\\~string#123"));
2028 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("€£"));
2029 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("invalid‡string"));
2030 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("12Ž34"));
2031}
2032
2033template <typename SwTracePolicy>
2034void StringToSwTraceStringTestHelper(const std::string& testString, std::vector<uint32_t> buffer, size_t expectedSize)
2035{
2036 // Convert the test string to a SWTrace string
2037 BOOST_CHECK(StringToSwTraceString<SwTracePolicy>(testString, buffer));
2038
2039 // The buffer must contain at least the length of the string
2040 BOOST_CHECK(!buffer.empty());
2041
2042 // The buffer must be of the expected size (in words)
2043 BOOST_CHECK(buffer.size() == expectedSize);
2044
2045 // The first word of the byte must be the length of the string including the null-terminator
2046 BOOST_CHECK(buffer[0] == testString.size() + 1);
2047
2048 // The contents of the buffer must match the test string
2049 BOOST_CHECK(std::memcmp(testString.data(), buffer.data() + 1, testString.size()) == 0);
2050
2051 // The buffer must include the null-terminator at the end of the string
2052 size_t nullTerminatorIndex = sizeof(uint32_t) + testString.size();
2053 BOOST_CHECK(reinterpret_cast<unsigned char*>(buffer.data())[nullTerminatorIndex] == '\0');
2054}
2055
2056BOOST_AUTO_TEST_CASE(StringToSwTraceStringTest)
2057{
2058 std::vector<uint32_t> buffer;
2059
2060 // Valid SWTrace strings (expected size in words)
2061 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("", buffer, 2);
2062 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("_", buffer, 2);
2063 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("0123", buffer, 3);
2064 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("valid_string", buffer, 5);
2065 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("VALID_string_456", buffer, 6);
2066 StringToSwTraceStringTestHelper<SwTraceCharPolicy>(" ", buffer, 2);
2067 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("valid string", buffer, 5);
2068 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("!$%", buffer, 2);
2069 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("valid|\\~string#123", buffer, 6);
2070
2071 // Invalid SWTrace strings
2072 BOOST_CHECK(!StringToSwTraceString<SwTraceCharPolicy>("€£", buffer));
2073 BOOST_CHECK(buffer.empty());
2074 BOOST_CHECK(!StringToSwTraceString<SwTraceCharPolicy>("invalid‡string", buffer));
2075 BOOST_CHECK(buffer.empty());
2076 BOOST_CHECK(!StringToSwTraceString<SwTraceCharPolicy>("12Ž34", buffer));
2077 BOOST_CHECK(buffer.empty());
2078}
2079
2080BOOST_AUTO_TEST_CASE(StringToSwTraceNameStringTest)
2081{
2082 std::vector<uint32_t> buffer;
2083
2084 // Valid SWTrace namestrings (expected size in words)
2085 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("", buffer, 2);
2086 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("_", buffer, 2);
2087 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("0123", buffer, 3);
2088 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("valid_string", buffer, 5);
2089 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("VALID_string_456", buffer, 6);
2090
2091 // Invalid SWTrace namestrings
2092 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>(" ", buffer));
2093 BOOST_CHECK(buffer.empty());
2094 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("invalid string", buffer));
2095 BOOST_CHECK(buffer.empty());
2096 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("!$%", buffer));
2097 BOOST_CHECK(buffer.empty());
2098 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("invalid|\\~string#123", buffer));
2099 BOOST_CHECK(buffer.empty());
2100 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("€£", buffer));
2101 BOOST_CHECK(buffer.empty());
2102 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("invalid‡string", buffer));
2103 BOOST_CHECK(buffer.empty());
2104 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("12Ž34", buffer));
2105 BOOST_CHECK(buffer.empty());
2106}
2107
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002108BOOST_AUTO_TEST_CASE(CheckPeriodicCounterCaptureThread)
2109{
2110 class CaptureReader : public IReadCounterValue
2111 {
2112 public:
2113 CaptureReader() {}
2114
2115 void GetCounterValue(uint16_t index, uint32_t &value) const override
2116 {
2117 if (m_Data.count(index))
2118 {
2119 value = m_Data.at(index);
2120 }
2121 else
2122 {
2123 value = 0;
2124 }
2125 }
2126
2127 void SetCounterValue(uint16_t index, uint32_t value)
2128 {
2129 if (!m_Data.count(index))
2130 {
2131 m_Data.insert(std::pair<uint16_t, uint32_t>(index, value));
2132 }
2133 else
2134 {
2135 m_Data.at(index) = value;
2136 }
2137 }
2138
2139 private:
2140 std::map<uint16_t, uint32_t> m_Data;
2141 };
2142
2143 Holder data;
2144 std::vector<uint16_t> captureIds1 = { 0, 1 };
2145 std::vector<uint16_t> captureIds2;
2146
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002147 MockProfilingConnection mockProfilingConnection;
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002148 MockBufferManager mockBuffer(512);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002149 SendCounterPacket sendCounterPacket(mockProfilingConnection, mockBuffer);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002150
2151 std::vector<uint16_t> counterIds;
2152 CaptureReader captureReader;
2153
2154 unsigned int valueA = 10;
2155 unsigned int valueB = 15;
2156 unsigned int numSteps = 5;
2157
2158 PeriodicCounterCapture periodicCounterCapture(std::ref(data), std::ref(sendCounterPacket), captureReader);
2159
2160 for(unsigned int i = 0; i < numSteps; ++i)
2161 {
2162 data.SetCaptureData(1, captureIds1);
2163 captureReader.SetCounterValue(0, valueA * (i + 1));
2164 captureReader.SetCounterValue(1, valueB * (i + 1));
2165
2166 periodicCounterCapture.Start();
2167
2168 std::this_thread::sleep_for(std::chrono::milliseconds(200));
2169
2170 periodicCounterCapture.Start();
2171
2172 data.SetCaptureData(0, captureIds2);
2173
2174 periodicCounterCapture.Start();
2175 }
2176
2177 periodicCounterCapture.Join();
2178
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002179 auto buffer = mockBuffer.GetReadableBuffer();
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002180
2181 uint32_t headerWord0 = ReadUint32(buffer, 0);
2182 uint32_t headerWord1 = ReadUint32(buffer, 4);
2183
2184 BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 1); // packet family
2185 BOOST_TEST(((headerWord0 >> 19) & 0x3F) == 0); // packet class
2186 BOOST_TEST(((headerWord0 >> 16) & 0x3) == 0); // packet type
2187 BOOST_TEST(headerWord1 == 20); // data length
2188
2189 uint32_t offset = 16;
2190 uint16_t readIndex = ReadUint16(buffer, offset);
2191 BOOST_TEST(0 == readIndex);
2192
2193 offset += 2;
2194 uint32_t readValue = ReadUint32(buffer, offset);
2195 BOOST_TEST((valueA * numSteps) == readValue);
2196
2197 offset += 4;
2198 readIndex = ReadUint16(buffer, offset);
2199 BOOST_TEST(1 == readIndex);
2200
2201 offset += 2;
2202 readValue = ReadUint32(buffer, offset);
2203 BOOST_TEST((valueB * numSteps) == readValue);
2204}
2205
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002206BOOST_AUTO_TEST_CASE(RequestCounterDirectoryCommandHandlerTest0)
2207{
2208 using boost::numeric_cast;
2209
2210 const uint32_t packetId = 0x30000;
2211 const uint32_t version = 1;
2212
2213 std::unique_ptr<char[]> packetData;
2214
2215 Packet packetA(packetId, 0, packetData);
2216
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002217 MockProfilingConnection mockProfilingConnection;
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002218 MockBufferManager mockBuffer(1024);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002219 SendCounterPacket sendCounterPacket(mockProfilingConnection, mockBuffer);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002220
2221 CounterDirectory counterDirectory;
2222
2223 RequestCounterDirectoryCommandHandler commandHandler(packetId, version, counterDirectory, sendCounterPacket);
2224 commandHandler(packetA);
2225
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002226 auto readBuffer = mockBuffer.GetReadableBuffer();
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002227
2228 uint32_t headerWord0 = ReadUint32(readBuffer, 0);
2229 uint32_t headerWord1 = ReadUint32(readBuffer, 4);
2230
2231 BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family
2232 BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 2); // packet id
Matteo Martincighf74ff2f2019-09-24 11:38:32 +01002233 BOOST_TEST(headerWord1 == 24); // data length
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002234
2235 uint32_t bodyHeaderWord0 = ReadUint32(readBuffer, 8);
2236 uint16_t deviceRecordCount = numeric_cast<uint16_t>(bodyHeaderWord0 >> 16);
2237 BOOST_TEST(deviceRecordCount == 0); // device_records_count
2238}
2239
2240BOOST_AUTO_TEST_CASE(RequestCounterDirectoryCommandHandlerTest1)
2241{
2242 using boost::numeric_cast;
2243
2244 const uint32_t packetId = 0x30000;
2245 const uint32_t version = 1;
2246
2247 std::unique_ptr<char[]> packetData;
2248
2249 Packet packetA(packetId, 0, packetData);
2250
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002251 MockProfilingConnection mockProfilingConnection;
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002252 MockBufferManager mockBuffer(1024);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002253 SendCounterPacket sendCounterPacket(mockProfilingConnection, mockBuffer);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002254
2255 CounterDirectory counterDirectory;
2256 const Device* device = counterDirectory.RegisterDevice("deviceA", 1);
2257 const CounterSet* counterSet = counterDirectory.RegisterCounterSet("countersetA");
2258 counterDirectory.RegisterCategory("categoryA", device->m_Uid, counterSet->m_Uid);
2259 counterDirectory.RegisterCounter("categoryA", 0, 1, 2.0f, "counterA", "descA");
2260 counterDirectory.RegisterCounter("categoryA", 1, 1, 3.0f, "counterB", "descB");
2261
2262 RequestCounterDirectoryCommandHandler commandHandler(packetId, version, counterDirectory, sendCounterPacket);
2263 commandHandler(packetA);
2264
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002265 auto readBuffer = mockBuffer.GetReadableBuffer();
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002266
2267 uint32_t headerWord0 = ReadUint32(readBuffer, 0);
2268 uint32_t headerWord1 = ReadUint32(readBuffer, 4);
2269
2270 BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family
2271 BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 2); // packet id
Matteo Martincighf74ff2f2019-09-24 11:38:32 +01002272 BOOST_TEST(headerWord1 == 240); // data length
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002273
2274 uint32_t bodyHeaderWord0 = ReadUint32(readBuffer, 8);
2275 uint32_t bodyHeaderWord1 = ReadUint32(readBuffer, 12);
2276 uint32_t bodyHeaderWord2 = ReadUint32(readBuffer, 16);
2277 uint32_t bodyHeaderWord3 = ReadUint32(readBuffer, 20);
2278 uint32_t bodyHeaderWord4 = ReadUint32(readBuffer, 24);
2279 uint32_t bodyHeaderWord5 = ReadUint32(readBuffer, 28);
2280 uint16_t deviceRecordCount = numeric_cast<uint16_t>(bodyHeaderWord0 >> 16);
2281 uint16_t counterSetRecordCount = numeric_cast<uint16_t>(bodyHeaderWord2 >> 16);
2282 uint16_t categoryRecordCount = numeric_cast<uint16_t>(bodyHeaderWord4 >> 16);
2283 BOOST_TEST(deviceRecordCount == 1); // device_records_count
2284 BOOST_TEST(bodyHeaderWord1 == 0); // device_records_pointer_table_offset
2285 BOOST_TEST(counterSetRecordCount == 1); // counter_set_count
2286 BOOST_TEST(bodyHeaderWord3 == 4); // counter_set_pointer_table_offset
2287 BOOST_TEST(categoryRecordCount == 1); // categories_count
2288 BOOST_TEST(bodyHeaderWord5 == 8); // categories_pointer_table_offset
2289
2290 uint32_t deviceRecordOffset = ReadUint32(readBuffer, 32);
2291 BOOST_TEST(deviceRecordOffset == 0);
2292
2293 uint32_t counterSetRecordOffset = ReadUint32(readBuffer, 36);
2294 BOOST_TEST(counterSetRecordOffset == 20);
2295
2296 uint32_t categoryRecordOffset = ReadUint32(readBuffer, 40);
2297 BOOST_TEST(categoryRecordOffset == 44);
2298}
2299
Francis Murtagh1f7db452019-08-14 09:49:34 +01002300BOOST_AUTO_TEST_SUITE_END()