blob: 1741160f9677c23f00cbe271475160003017e198 [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
Aron Virginas-Tare898db92019-08-22 12:56:34 +010030#include <boost/test/unit_test.hpp>
Ferran Balaguer1b941722019-08-28 16:57:18 +010031#include <boost/numeric/conversion/cast.hpp>
Francis Murtagh1f7db452019-08-14 09:49:34 +010032
Nikhil Rajbc626052019-08-15 15:49:45 +010033#include <cstdint>
34#include <cstring>
Aron Virginas-Tare898db92019-08-22 12:56:34 +010035#include <limits>
Francis Murtagh11f99b42019-08-16 11:28:52 +010036#include <map>
Aron Virginas-Tare898db92019-08-22 12:56:34 +010037#include <random>
Nikhil Raj3ecc5102019-09-03 15:55:33 +010038#include <thread>
Matteo Martincigh24e8f922019-09-19 11:57:46 +010039#include <chrono>
Francis Murtagh1f7db452019-08-14 09:49:34 +010040
41BOOST_AUTO_TEST_SUITE(ExternalProfiling)
42
Aron Virginas-Tare898db92019-08-22 12:56:34 +010043using namespace armnn::profiling;
44
Francis Murtagh1f7db452019-08-14 09:49:34 +010045BOOST_AUTO_TEST_CASE(CheckCommandHandlerKeyComparisons)
46{
47 CommandHandlerKey testKey0(1, 1);
48 CommandHandlerKey testKey1(1, 1);
49 CommandHandlerKey testKey2(1, 1);
50 CommandHandlerKey testKey3(0, 0);
51 CommandHandlerKey testKey4(2, 2);
52 CommandHandlerKey testKey5(0, 2);
53
54 BOOST_CHECK(testKey1<testKey4);
55 BOOST_CHECK(testKey1>testKey3);
56 BOOST_CHECK(testKey1<=testKey4);
57 BOOST_CHECK(testKey1>=testKey3);
58 BOOST_CHECK(testKey1<=testKey2);
59 BOOST_CHECK(testKey1>=testKey2);
60 BOOST_CHECK(testKey1==testKey2);
61 BOOST_CHECK(testKey1==testKey1);
62
63 BOOST_CHECK(!(testKey1==testKey5));
64 BOOST_CHECK(!(testKey1!=testKey1));
65 BOOST_CHECK(testKey1!=testKey5);
66
67 BOOST_CHECK(testKey1==testKey2 && testKey2==testKey1);
68 BOOST_CHECK(testKey0==testKey1 && testKey1==testKey2 && testKey0==testKey2);
69
70 BOOST_CHECK(testKey1.GetPacketId()==1);
71 BOOST_CHECK(testKey1.GetVersion()==1);
72
73 std::vector<CommandHandlerKey> vect =
Aron Virginas-Tare898db92019-08-22 12:56:34 +010074 {
75 CommandHandlerKey(0,1), CommandHandlerKey(2,0), CommandHandlerKey(1,0),
76 CommandHandlerKey(2,1), CommandHandlerKey(1,1), CommandHandlerKey(0,1),
77 CommandHandlerKey(2,0), CommandHandlerKey(0,0)
78 };
Francis Murtagh1f7db452019-08-14 09:49:34 +010079
80 std::sort(vect.begin(), vect.end());
81
82 std::vector<CommandHandlerKey> expectedVect =
Aron Virginas-Tare898db92019-08-22 12:56:34 +010083 {
84 CommandHandlerKey(0,0), CommandHandlerKey(0,1), CommandHandlerKey(0,1),
85 CommandHandlerKey(1,0), CommandHandlerKey(1,1), CommandHandlerKey(2,0),
86 CommandHandlerKey(2,0), CommandHandlerKey(2,1)
87 };
Francis Murtagh1f7db452019-08-14 09:49:34 +010088
89 BOOST_CHECK(vect == expectedVect);
90}
91
FinnWilliamsArm4833cea2019-09-17 16:53:53 +010092class TestProfilingConnectionBase :public IProfilingConnection
93{
94public:
95 TestProfilingConnectionBase() = default;
96 ~TestProfilingConnectionBase() = default;
97
98 bool IsOpen()
99 {
100 return true;
101 }
102
103 void Close(){}
104
Matteo Martincigh24e8f922019-09-19 11:57:46 +0100105 bool WritePacket(const unsigned char* buffer, uint32_t length)
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100106 {
107 return false;
108 }
109
110 Packet ReadPacket(uint32_t timeout)
111 {
112 std::this_thread::sleep_for(std::chrono::milliseconds(timeout));
113 std::unique_ptr<char[]> packetData;
114 //Return connection acknowledged packet
115 return {65536 ,0 , packetData};
116 }
117};
118
119class TestProfilingConnectionTimeoutError :public TestProfilingConnectionBase
120{
121 int readRequests = 0;
122public:
123 Packet ReadPacket(uint32_t timeout) {
124 if (readRequests < 3)
125 {
126 readRequests++;
127 throw armnn::TimeoutException(": Simulate a timeout");
128 }
129 std::this_thread::sleep_for(std::chrono::milliseconds(timeout));
130 std::unique_ptr<char[]> packetData;
131 //Return connection acknowledged packet after three timeouts
132 return {65536 ,0 , packetData};
133 }
134};
135
136class TestProfilingConnectionArmnnError :public TestProfilingConnectionBase
137{
138public:
139
140 Packet ReadPacket(uint32_t timeout)
141 {
142 std::this_thread::sleep_for(std::chrono::milliseconds(timeout));
143 throw armnn::Exception(": Simulate a non timeout error");
144 }
145};
146
147BOOST_AUTO_TEST_CASE(CheckCommandThread)
148{
149 PacketVersionResolver packetVersionResolver;
150 ProfilingStateMachine profilingStateMachine;
151
152 TestProfilingConnectionBase testProfilingConnectionBase;
153 TestProfilingConnectionTimeoutError testProfilingConnectionTimeOutError;
154 TestProfilingConnectionArmnnError testProfilingConnectionArmnnError;
155
156 ConnectionAcknowledgedCommandHandler connectionAcknowledgedCommandHandler(1, 4194304, profilingStateMachine);
157 CommandHandlerRegistry commandHandlerRegistry;
158
159 commandHandlerRegistry.RegisterFunctor(&connectionAcknowledgedCommandHandler, 1, 4194304);
160
161 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
162 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
163
164 CommandThread commandThread0(1,
165 true,
166 commandHandlerRegistry,
167 packetVersionResolver,
168 testProfilingConnectionBase);
169
170 commandThread0.Start();
171 commandThread0.Start();
172 commandThread0.Start();
173
174 commandThread0.Stop();
175 commandThread0.Join();
176
177 BOOST_CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::Active);
178
179 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
180 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
181 //commandThread1 should give up after one timeout
182 CommandThread commandThread1(1,
183 true,
184 commandHandlerRegistry,
185 packetVersionResolver,
186 testProfilingConnectionTimeOutError);
187
188 commandThread1.Start();
189 commandThread1.Join();
190
191 BOOST_CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::WaitingForAck);
192 //now commandThread1 should persist after a timeout
193 commandThread1.StopAfterTimeout(false);
194 commandThread1.Start();
195
196 for (int i = 0; i < 100; i++)
197 {
198 if (profilingStateMachine.GetCurrentState() == ProfilingState::Active)
199 {
200 break;
201 }
202 else
203 {
204 std::this_thread::sleep_for(std::chrono::milliseconds(5));
205 }
206 }
207
208 commandThread1.Stop();
209 commandThread1.Join();
210
211 BOOST_CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::Active);
212
213
214 CommandThread commandThread2(1,
215 false,
216 commandHandlerRegistry,
217 packetVersionResolver,
218 testProfilingConnectionArmnnError);
219
220 commandThread2.Start();
221
222 for (int i = 0; i < 100; i++)
223 {
224 if (!commandThread2.IsRunning())
225 {
226 //commandThread2 should stop once it encounters a non timing error
227 commandThread2.Join();
228 return;
229 }
230 std::this_thread::sleep_for(std::chrono::milliseconds(5));
231 }
232
233 BOOST_ERROR("commandThread2 has failed to stop");
234}
235
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100236BOOST_AUTO_TEST_CASE(CheckEncodeVersion)
237{
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100238 Version version1(12);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100239
240 BOOST_CHECK(version1.GetMajor() == 0);
241 BOOST_CHECK(version1.GetMinor() == 0);
242 BOOST_CHECK(version1.GetPatch() == 12);
243
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100244 Version version2(4108);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100245
246 BOOST_CHECK(version2.GetMajor() == 0);
247 BOOST_CHECK(version2.GetMinor() == 1);
248 BOOST_CHECK(version2.GetPatch() == 12);
249
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100250 Version version3(4198412);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100251
252 BOOST_CHECK(version3.GetMajor() == 1);
253 BOOST_CHECK(version3.GetMinor() == 1);
254 BOOST_CHECK(version3.GetPatch() == 12);
255
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100256 Version version4(0);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100257
258 BOOST_CHECK(version4.GetMajor() == 0);
259 BOOST_CHECK(version4.GetMinor() == 0);
260 BOOST_CHECK(version4.GetPatch() == 0);
261
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100262 Version version5(1, 0, 0);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100263 BOOST_CHECK(version5.GetEncodedValue() == 4194304);
264}
265
Nikhil Rajbc626052019-08-15 15:49:45 +0100266BOOST_AUTO_TEST_CASE(CheckPacketClass)
267{
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100268 uint32_t length = 4;
269 std::unique_ptr<char[]> packetData0 = std::make_unique<char[]>(length);
270 std::unique_ptr<char[]> packetData1 = std::make_unique<char[]>(0);
271 std::unique_ptr<char[]> nullPacketData;
Nikhil Rajbc626052019-08-15 15:49:45 +0100272
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100273 Packet packetTest0(472580096, length, packetData0);
Nikhil Rajbc626052019-08-15 15:49:45 +0100274
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100275 BOOST_CHECK(packetTest0.GetHeader() == 472580096);
276 BOOST_CHECK(packetTest0.GetPacketFamily() == 7);
277 BOOST_CHECK(packetTest0.GetPacketId() == 43);
278 BOOST_CHECK(packetTest0.GetLength() == length);
279 BOOST_CHECK(packetTest0.GetPacketType() == 3);
280 BOOST_CHECK(packetTest0.GetPacketClass() == 5);
Nikhil Rajbc626052019-08-15 15:49:45 +0100281
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100282 BOOST_CHECK_THROW(Packet packetTest1(472580096, 0, packetData1), armnn::Exception);
283 BOOST_CHECK_NO_THROW(Packet packetTest2(472580096, 0, nullPacketData));
Nikhil Rajbc626052019-08-15 15:49:45 +0100284
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100285 Packet packetTest3(472580096, 0, nullPacketData);
286 BOOST_CHECK(packetTest3.GetLength() == 0);
287 BOOST_CHECK(packetTest3.GetData() == nullptr);
288
289 const char* packetTest0Data = packetTest0.GetData();
290 Packet packetTest4(std::move(packetTest0));
291
292 BOOST_CHECK(packetTest0.GetData() == nullptr);
293 BOOST_CHECK(packetTest4.GetData() == packetTest0Data);
294
295 BOOST_CHECK(packetTest4.GetHeader() == 472580096);
296 BOOST_CHECK(packetTest4.GetPacketFamily() == 7);
297 BOOST_CHECK(packetTest4.GetPacketId() == 43);
298 BOOST_CHECK(packetTest4.GetLength() == length);
299 BOOST_CHECK(packetTest4.GetPacketType() == 3);
300 BOOST_CHECK(packetTest4.GetPacketClass() == 5);
Nikhil Rajbc626052019-08-15 15:49:45 +0100301}
302
Francis Murtagh94d79152019-08-16 17:45:07 +0100303// Create Derived Classes
304class TestFunctorA : public CommandHandlerFunctor
305{
306public:
307 using CommandHandlerFunctor::CommandHandlerFunctor;
308
309 int GetCount() { return m_Count; }
310
311 void operator()(const Packet& packet) override
312 {
313 m_Count++;
314 }
315
316private:
317 int m_Count = 0;
318};
319
320class TestFunctorB : public TestFunctorA
321{
322 using TestFunctorA::TestFunctorA;
323};
324
325class TestFunctorC : public TestFunctorA
326{
327 using TestFunctorA::TestFunctorA;
328};
329
Francis Murtagh11f99b42019-08-16 11:28:52 +0100330BOOST_AUTO_TEST_CASE(CheckCommandHandlerFunctor)
331{
Francis Murtagh11f99b42019-08-16 11:28:52 +0100332 // Hard code the version as it will be the same during a single profiling session
333 uint32_t version = 1;
334
335 TestFunctorA testFunctorA(461, version);
336 TestFunctorB testFunctorB(963, version);
337 TestFunctorC testFunctorC(983, version);
338
339 CommandHandlerKey keyA(testFunctorA.GetPacketId(), testFunctorA.GetVersion());
340 CommandHandlerKey keyB(testFunctorB.GetPacketId(), testFunctorB.GetVersion());
341 CommandHandlerKey keyC(testFunctorC.GetPacketId(), testFunctorC.GetVersion());
342
343 // Create the unwrapped map to simulate the Command Handler Registry
344 std::map<CommandHandlerKey, CommandHandlerFunctor*> registry;
345
346 registry.insert(std::make_pair(keyB, &testFunctorB));
347 registry.insert(std::make_pair(keyA, &testFunctorA));
348 registry.insert(std::make_pair(keyC, &testFunctorC));
349
350 // Check the order of the map is correct
351 auto it = registry.begin();
352 BOOST_CHECK(it->first==keyA);
353 it++;
354 BOOST_CHECK(it->first==keyB);
355 it++;
356 BOOST_CHECK(it->first==keyC);
357
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100358 std::unique_ptr<char[]> packetDataA;
359 std::unique_ptr<char[]> packetDataB;
360 std::unique_ptr<char[]> packetDataC;
361
362 Packet packetA(500000000, 0, packetDataA);
363 Packet packetB(600000000, 0, packetDataB);
364 Packet packetC(400000000, 0, packetDataC);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100365
366 // Check the correct operator of derived class is called
367 registry.at(CommandHandlerKey(packetA.GetPacketId(), version))->operator()(packetA);
368 BOOST_CHECK(testFunctorA.GetCount() == 1);
369 BOOST_CHECK(testFunctorB.GetCount() == 0);
370 BOOST_CHECK(testFunctorC.GetCount() == 0);
371
372 registry.at(CommandHandlerKey(packetB.GetPacketId(), version))->operator()(packetB);
373 BOOST_CHECK(testFunctorA.GetCount() == 1);
374 BOOST_CHECK(testFunctorB.GetCount() == 1);
375 BOOST_CHECK(testFunctorC.GetCount() == 0);
376
377 registry.at(CommandHandlerKey(packetC.GetPacketId(), version))->operator()(packetC);
378 BOOST_CHECK(testFunctorA.GetCount() == 1);
379 BOOST_CHECK(testFunctorB.GetCount() == 1);
380 BOOST_CHECK(testFunctorC.GetCount() == 1);
381}
382
Francis Murtagh94d79152019-08-16 17:45:07 +0100383BOOST_AUTO_TEST_CASE(CheckCommandHandlerRegistry)
384{
385 // Hard code the version as it will be the same during a single profiling session
386 uint32_t version = 1;
387
388 TestFunctorA testFunctorA(461, version);
389 TestFunctorB testFunctorB(963, version);
390 TestFunctorC testFunctorC(983, version);
391
392 // Create the Command Handler Registry
393 CommandHandlerRegistry registry;
394
395 // Register multiple different derived classes
396 registry.RegisterFunctor(&testFunctorA, testFunctorA.GetPacketId(), testFunctorA.GetVersion());
397 registry.RegisterFunctor(&testFunctorB, testFunctorB.GetPacketId(), testFunctorB.GetVersion());
398 registry.RegisterFunctor(&testFunctorC, testFunctorC.GetPacketId(), testFunctorC.GetVersion());
399
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100400 std::unique_ptr<char[]> packetDataA;
401 std::unique_ptr<char[]> packetDataB;
402 std::unique_ptr<char[]> packetDataC;
403
404 Packet packetA(500000000, 0, packetDataA);
405 Packet packetB(600000000, 0, packetDataB);
406 Packet packetC(400000000, 0, packetDataC);
Francis Murtagh94d79152019-08-16 17:45:07 +0100407
408 // Check the correct operator of derived class is called
409 registry.GetFunctor(packetA.GetPacketId(), version)->operator()(packetA);
410 BOOST_CHECK(testFunctorA.GetCount() == 1);
411 BOOST_CHECK(testFunctorB.GetCount() == 0);
412 BOOST_CHECK(testFunctorC.GetCount() == 0);
413
414 registry.GetFunctor(packetB.GetPacketId(), version)->operator()(packetB);
415 BOOST_CHECK(testFunctorA.GetCount() == 1);
416 BOOST_CHECK(testFunctorB.GetCount() == 1);
417 BOOST_CHECK(testFunctorC.GetCount() == 0);
418
419 registry.GetFunctor(packetC.GetPacketId(), version)->operator()(packetC);
420 BOOST_CHECK(testFunctorA.GetCount() == 1);
421 BOOST_CHECK(testFunctorB.GetCount() == 1);
422 BOOST_CHECK(testFunctorC.GetCount() == 1);
423
424 // Re-register an existing key with a new function
425 registry.RegisterFunctor(&testFunctorC, testFunctorA.GetPacketId(), version);
426 registry.GetFunctor(packetA.GetPacketId(), version)->operator()(packetC);
427 BOOST_CHECK(testFunctorA.GetCount() == 1);
428 BOOST_CHECK(testFunctorB.GetCount() == 1);
429 BOOST_CHECK(testFunctorC.GetCount() == 2);
430
431 // Check that non-existent key returns nullptr for its functor
432 BOOST_CHECK_THROW(registry.GetFunctor(0, 0), armnn::Exception);
433}
434
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100435BOOST_AUTO_TEST_CASE(CheckPacketVersionResolver)
436{
437 // Set up random number generator for generating packetId values
438 std::random_device device;
439 std::mt19937 generator(device());
440 std::uniform_int_distribution<uint32_t> distribution(std::numeric_limits<uint32_t>::min(),
441 std::numeric_limits<uint32_t>::max());
442
443 // NOTE: Expected version is always 1.0.0, regardless of packetId
444 const Version expectedVersion(1, 0, 0);
445
446 PacketVersionResolver packetVersionResolver;
447
448 constexpr unsigned int numTests = 10u;
449
450 for (unsigned int i = 0u; i < numTests; ++i)
451 {
452 const uint32_t packetId = distribution(generator);
453 Version resolvedVersion = packetVersionResolver.ResolvePacketVersion(packetId);
454
455 BOOST_TEST(resolvedVersion == expectedVersion);
456 }
457}
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100458void ProfilingCurrentStateThreadImpl(ProfilingStateMachine& states)
459{
460 ProfilingState newState = ProfilingState::NotConnected;
461 states.GetCurrentState();
462 states.TransitionToState(newState);
463}
464
465BOOST_AUTO_TEST_CASE(CheckProfilingStateMachine)
466{
467 ProfilingStateMachine profilingState1(ProfilingState::Uninitialised);
468 profilingState1.TransitionToState(ProfilingState::Uninitialised);
469 BOOST_CHECK(profilingState1.GetCurrentState() == ProfilingState::Uninitialised);
470
471 ProfilingStateMachine profilingState2(ProfilingState::Uninitialised);
472 profilingState2.TransitionToState(ProfilingState::NotConnected);
473 BOOST_CHECK(profilingState2.GetCurrentState() == ProfilingState::NotConnected);
474
475 ProfilingStateMachine profilingState3(ProfilingState::NotConnected);
476 profilingState3.TransitionToState(ProfilingState::NotConnected);
477 BOOST_CHECK(profilingState3.GetCurrentState() == ProfilingState::NotConnected);
478
479 ProfilingStateMachine profilingState4(ProfilingState::NotConnected);
480 profilingState4.TransitionToState(ProfilingState::WaitingForAck);
481 BOOST_CHECK(profilingState4.GetCurrentState() == ProfilingState::WaitingForAck);
482
483 ProfilingStateMachine profilingState5(ProfilingState::WaitingForAck);
484 profilingState5.TransitionToState(ProfilingState::WaitingForAck);
485 BOOST_CHECK(profilingState5.GetCurrentState() == ProfilingState::WaitingForAck);
486
487 ProfilingStateMachine profilingState6(ProfilingState::WaitingForAck);
488 profilingState6.TransitionToState(ProfilingState::Active);
489 BOOST_CHECK(profilingState6.GetCurrentState() == ProfilingState::Active);
490
491 ProfilingStateMachine profilingState7(ProfilingState::Active);
492 profilingState7.TransitionToState(ProfilingState::NotConnected);
493 BOOST_CHECK(profilingState7.GetCurrentState() == ProfilingState::NotConnected);
494
495 ProfilingStateMachine profilingState8(ProfilingState::Active);
496 profilingState8.TransitionToState(ProfilingState::Active);
497 BOOST_CHECK(profilingState8.GetCurrentState() == ProfilingState::Active);
498
499 ProfilingStateMachine profilingState9(ProfilingState::Uninitialised);
500 BOOST_CHECK_THROW(profilingState9.TransitionToState(ProfilingState::WaitingForAck),
501 armnn::Exception);
502
503 ProfilingStateMachine profilingState10(ProfilingState::Uninitialised);
504 BOOST_CHECK_THROW(profilingState10.TransitionToState(ProfilingState::Active),
505 armnn::Exception);
506
507 ProfilingStateMachine profilingState11(ProfilingState::NotConnected);
508 BOOST_CHECK_THROW(profilingState11.TransitionToState(ProfilingState::Uninitialised),
509 armnn::Exception);
510
511 ProfilingStateMachine profilingState12(ProfilingState::NotConnected);
512 BOOST_CHECK_THROW(profilingState12.TransitionToState(ProfilingState::Active),
513 armnn::Exception);
514
515 ProfilingStateMachine profilingState13(ProfilingState::WaitingForAck);
516 BOOST_CHECK_THROW(profilingState13.TransitionToState(ProfilingState::Uninitialised),
517 armnn::Exception);
518
519 ProfilingStateMachine profilingState14(ProfilingState::WaitingForAck);
520 BOOST_CHECK_THROW(profilingState14.TransitionToState(ProfilingState::NotConnected),
521 armnn::Exception);
522
523 ProfilingStateMachine profilingState15(ProfilingState::Active);
524 BOOST_CHECK_THROW(profilingState15.TransitionToState(ProfilingState::Uninitialised),
525 armnn::Exception);
526
527 ProfilingStateMachine profilingState16(armnn::profiling::ProfilingState::Active);
528 BOOST_CHECK_THROW(profilingState16.TransitionToState(ProfilingState::WaitingForAck),
529 armnn::Exception);
530
531 ProfilingStateMachine profilingState17(ProfilingState::Uninitialised);
532
533 std::thread thread1 (ProfilingCurrentStateThreadImpl,std::ref(profilingState17));
534 std::thread thread2 (ProfilingCurrentStateThreadImpl,std::ref(profilingState17));
535 std::thread thread3 (ProfilingCurrentStateThreadImpl,std::ref(profilingState17));
536 std::thread thread4 (ProfilingCurrentStateThreadImpl,std::ref(profilingState17));
537 std::thread thread5 (ProfilingCurrentStateThreadImpl,std::ref(profilingState17));
538
539 thread1.join();
540 thread2.join();
541 thread3.join();
542 thread4.join();
543 thread5.join();
544
545 BOOST_TEST((profilingState17.GetCurrentState() == ProfilingState::NotConnected));
546}
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100547
Jim Flynn8355ec92019-09-17 12:29:50 +0100548void CaptureDataWriteThreadImpl(Holder& holder, uint32_t capturePeriod, const std::vector<uint16_t>& counterIds)
Francis Murtagh68f78d82019-09-04 16:42:29 +0100549{
550 holder.SetCaptureData(capturePeriod, counterIds);
551}
552
Francis Murtaghbd707162019-09-09 11:26:44 +0100553void CaptureDataReadThreadImpl(const Holder& holder, CaptureData& captureData)
Francis Murtagh68f78d82019-09-04 16:42:29 +0100554{
555 captureData = holder.GetCaptureData();
556}
557
558BOOST_AUTO_TEST_CASE(CheckCaptureDataHolder)
559{
Francis Murtaghbd707162019-09-09 11:26:44 +0100560 std::map<uint32_t, std::vector<uint16_t>> periodIdMap;
561 std::vector<uint16_t> counterIds;
Jim Flynn8355ec92019-09-17 12:29:50 +0100562 uint32_t numThreads = 10;
563 for (uint32_t i = 0; i < numThreads; ++i)
Francis Murtaghbd707162019-09-09 11:26:44 +0100564 {
565 counterIds.emplace_back(i);
566 periodIdMap.insert(std::make_pair(i, counterIds));
567 }
Francis Murtagh68f78d82019-09-04 16:42:29 +0100568
Jim Flynn8355ec92019-09-17 12:29:50 +0100569 // Verify the read and write threads set the holder correctly
570 // and retrieve the expected values
Francis Murtagh68f78d82019-09-04 16:42:29 +0100571 Holder holder;
572 BOOST_CHECK((holder.GetCaptureData()).GetCapturePeriod() == 0);
573 BOOST_CHECK(((holder.GetCaptureData()).GetCounterIds()).empty());
574
575 // Check Holder functions
Francis Murtaghbd707162019-09-09 11:26:44 +0100576 std::thread thread1(CaptureDataWriteThreadImpl, std::ref(holder), 2, std::ref(periodIdMap[2]));
Francis Murtagh68f78d82019-09-04 16:42:29 +0100577 thread1.join();
Francis Murtaghbd707162019-09-09 11:26:44 +0100578 BOOST_CHECK((holder.GetCaptureData()).GetCapturePeriod() == 2);
579 BOOST_CHECK((holder.GetCaptureData()).GetCounterIds() == periodIdMap[2]);
Jim Flynn8355ec92019-09-17 12:29:50 +0100580 // NOTE: now that we have some initial values in the holder we don't have to worry
581 // in the multi-threaded section below about a read thread accessing the holder
582 // before any write thread has gotten to it so we read period = 0, counterIds empty
583 // instead of period = 0, counterIds = {0} as will the case when write thread 0
584 // has executed.
Francis Murtagh68f78d82019-09-04 16:42:29 +0100585
586 CaptureData captureData;
587 std::thread thread2(CaptureDataReadThreadImpl, std::ref(holder), std::ref(captureData));
588 thread2.join();
Jim Flynn8355ec92019-09-17 12:29:50 +0100589 BOOST_CHECK(captureData.GetCapturePeriod() == 2);
Francis Murtaghbd707162019-09-09 11:26:44 +0100590 BOOST_CHECK(captureData.GetCounterIds() == periodIdMap[2]);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100591
Jim Flynn8355ec92019-09-17 12:29:50 +0100592 std::map<uint32_t, CaptureData> captureDataIdMap;
593 for (uint32_t i = 0; i < numThreads; ++i)
594 {
595 CaptureData perThreadCaptureData;
596 captureDataIdMap.insert(std::make_pair(i, perThreadCaptureData));
597 }
598
Francis Murtaghbd707162019-09-09 11:26:44 +0100599 std::vector<std::thread> threadsVect;
Jim Flynn8355ec92019-09-17 12:29:50 +0100600 std::vector<std::thread> readThreadsVect;
601 for (uint32_t i = 0; i < numThreads; ++i)
Francis Murtaghbd707162019-09-09 11:26:44 +0100602 {
603 threadsVect.emplace_back(std::thread(CaptureDataWriteThreadImpl,
604 std::ref(holder),
605 i,
Jim Flynn8355ec92019-09-17 12:29:50 +0100606 std::ref(periodIdMap[i])));
Francis Murtagh06965692019-09-05 16:29:01 +0100607
Jim Flynn8355ec92019-09-17 12:29:50 +0100608 // Verify that the CaptureData goes into the thread in a virgin state
609 BOOST_CHECK(captureDataIdMap.at(i).GetCapturePeriod() == 0);
610 BOOST_CHECK(captureDataIdMap.at(i).GetCounterIds().empty());
611 readThreadsVect.emplace_back(std::thread(CaptureDataReadThreadImpl,
612 std::ref(holder),
613 std::ref(captureDataIdMap.at(i))));
Francis Murtaghbd707162019-09-09 11:26:44 +0100614 }
615
Jim Flynn8355ec92019-09-17 12:29:50 +0100616 for (uint32_t i = 0; i < numThreads; ++i)
Francis Murtaghbd707162019-09-09 11:26:44 +0100617 {
618 threadsVect[i].join();
Francis Murtaghbd707162019-09-09 11:26:44 +0100619 readThreadsVect[i].join();
620 }
Francis Murtagh68f78d82019-09-04 16:42:29 +0100621
Jim Flynn8355ec92019-09-17 12:29:50 +0100622 // Look at the CaptureData that each read thread has filled
623 // the capture period it read should match the counter ids entry
624 for (uint32_t i = 0; i < numThreads; ++i)
625 {
626 CaptureData perThreadCaptureData = captureDataIdMap.at(i);
627 BOOST_CHECK(perThreadCaptureData.GetCounterIds() == periodIdMap.at(perThreadCaptureData.GetCapturePeriod()));
628 }
Matthew Bentham46d1c622019-09-13 12:45:04 +0100629}
Francis Murtagh68f78d82019-09-04 16:42:29 +0100630
Matthew Bentham46d1c622019-09-13 12:45:04 +0100631BOOST_AUTO_TEST_CASE(CaptureDataMethods)
632{
Jim Flynn8355ec92019-09-17 12:29:50 +0100633 // Check CaptureData setter and getter functions
Matthew Bentham46d1c622019-09-13 12:45:04 +0100634 std::vector<uint16_t> counterIds = {42, 29, 13};
Jim Flynn8355ec92019-09-17 12:29:50 +0100635 CaptureData captureData;
636 BOOST_CHECK(captureData.GetCapturePeriod() == 0);
637 BOOST_CHECK((captureData.GetCounterIds()).empty());
638 captureData.SetCapturePeriod(150);
639 captureData.SetCounterIds(counterIds);
640 BOOST_CHECK(captureData.GetCapturePeriod() == 150);
641 BOOST_CHECK(captureData.GetCounterIds() == counterIds);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100642
Jim Flynn8355ec92019-09-17 12:29:50 +0100643 // Check assignment operator
Francis Murtagh68f78d82019-09-04 16:42:29 +0100644 CaptureData secondCaptureData;
Francis Murtagh68f78d82019-09-04 16:42:29 +0100645
Jim Flynn8355ec92019-09-17 12:29:50 +0100646 secondCaptureData = captureData;
647 BOOST_CHECK(secondCaptureData.GetCapturePeriod() == 150);
Matthew Bentham46d1c622019-09-13 12:45:04 +0100648 BOOST_CHECK(secondCaptureData.GetCounterIds() == counterIds);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100649
650 // Check copy constructor
Jim Flynn8355ec92019-09-17 12:29:50 +0100651 CaptureData copyConstructedCaptureData(captureData);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100652
Jim Flynn8355ec92019-09-17 12:29:50 +0100653 BOOST_CHECK(copyConstructedCaptureData.GetCapturePeriod() == 150);
Matthew Bentham46d1c622019-09-13 12:45:04 +0100654 BOOST_CHECK(copyConstructedCaptureData.GetCounterIds() == counterIds);
Keith Davis02356de2019-08-26 18:28:17 +0100655}
Francis Murtagh68f78d82019-09-04 16:42:29 +0100656
Keith Davis02356de2019-08-26 18:28:17 +0100657BOOST_AUTO_TEST_CASE(CheckProfilingServiceDisabled)
658{
659 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
660 ProfilingService service(options);
661 BOOST_CHECK(service.GetCurrentState() == ProfilingState::Uninitialised);
662 service.Run();
663 BOOST_CHECK(service.GetCurrentState() == ProfilingState::Uninitialised);
664}
665
Keith Davis02356de2019-08-26 18:28:17 +0100666BOOST_AUTO_TEST_CASE(CheckProfilingServiceEnabled)
667{
668 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
669 options.m_EnableProfiling = true;
670 ProfilingService service(options);
671 BOOST_CHECK(service.GetCurrentState() == ProfilingState::NotConnected);
672 service.Run();
673 BOOST_CHECK(service.GetCurrentState() == ProfilingState::WaitingForAck);
674}
675
676
677BOOST_AUTO_TEST_CASE(CheckProfilingServiceEnabledRuntime)
678{
679 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
680 ProfilingService service(options);
681 BOOST_CHECK(service.GetCurrentState() == ProfilingState::Uninitialised);
682 service.Run();
683 BOOST_CHECK(service.GetCurrentState() == ProfilingState::Uninitialised);
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100684 options.m_EnableProfiling = true;
685 service.ResetExternalProfilingOptions(options);
Keith Davis02356de2019-08-26 18:28:17 +0100686 BOOST_CHECK(service.GetCurrentState() == ProfilingState::NotConnected);
687 service.Run();
688 BOOST_CHECK(service.GetCurrentState() == ProfilingState::WaitingForAck);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100689}
690
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100691BOOST_AUTO_TEST_CASE(CheckProfilingServiceCounterDirectory)
692{
693 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
694 ProfilingService service(options);
695
696 const ICounterDirectory& counterDirectory0 = service.GetCounterDirectory();
697 BOOST_CHECK(counterDirectory0.GetCounterCount() == 0);
698
699 options.m_EnableProfiling = true;
700 service.ResetExternalProfilingOptions(options);
701
702 const ICounterDirectory& counterDirectory1 = service.GetCounterDirectory();
703 BOOST_CHECK(counterDirectory1.GetCounterCount() != 0);
704}
705
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100706BOOST_AUTO_TEST_CASE(CheckProfilingServiceCounterValues)
707{
708 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
709 options.m_EnableProfiling = true;
710 ProfilingService profilingService(options);
711
712 ProfilingService* profilingServicePtr = &profilingService;
713 std::vector<std::thread> writers;
714
715 for(int i = 0; i < 100 ; ++i)
716 {
717 // Increment and decrement counter 0
718 writers.push_back(std::thread(&ProfilingService::IncrementCounterValue, profilingServicePtr, 0));
719 writers.push_back(std::thread(&ProfilingService::DecrementCounterValue, profilingServicePtr, 0));
720 // Add 10 to counter 0 and subtract 5 from counter 0
721 writers.push_back(std::thread(&ProfilingService::AddCounterValue, profilingServicePtr, 0, 10));
722 writers.push_back(std::thread(&ProfilingService::SubtractCounterValue, profilingServicePtr, 0, 5));
723 }
724
725 std::for_each(writers.begin(), writers.end(), mem_fn(&std::thread::join));
726
727 uint32_t counterValue;
728 profilingService.GetCounterValue(0, counterValue);
729 BOOST_CHECK(counterValue == 500);
730
731 profilingService.SetCounterValue(0, 0);
732 profilingService.GetCounterValue(0, counterValue);
733 BOOST_CHECK(counterValue == 0);
734
735 BOOST_CHECK_THROW(profilingService.SetCounterValue(profilingService.GetCounterCount(), 1), armnn::Exception);
736}
737
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100738BOOST_AUTO_TEST_CASE(CheckProfilingObjectUids)
Matteo Martincighab173e92019-09-05 12:02:04 +0100739{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100740 uint16_t uid = 0;
741 BOOST_CHECK_NO_THROW(uid = GetNextUid());
742 BOOST_CHECK(uid >= 1);
743
744 uint16_t nextUid = 0;
745 BOOST_CHECK_NO_THROW(nextUid = GetNextUid());
746 BOOST_CHECK(nextUid > uid);
747
748 std::vector<uint16_t> counterUids;
749 BOOST_CHECK_NO_THROW(counterUids = GetNextCounterUids(0));
750 BOOST_CHECK(counterUids.size() == 1);
751 BOOST_CHECK(counterUids[0] >= 0);
752
753 std::vector<uint16_t> nextCounterUids;
754 BOOST_CHECK_NO_THROW(nextCounterUids = GetNextCounterUids(1));
755 BOOST_CHECK(nextCounterUids.size() == 1);
756 BOOST_CHECK(nextCounterUids[0] > counterUids[0]);
757
758 std::vector<uint16_t> counterUidsMultiCore;
759 uint16_t numberOfCores = 13;
760 BOOST_CHECK_NO_THROW(counterUidsMultiCore = GetNextCounterUids(numberOfCores));
761 BOOST_CHECK(counterUidsMultiCore.size() == numberOfCores);
762 BOOST_CHECK(counterUidsMultiCore.front() >= nextCounterUids[0]);
763 for (size_t i = 1; i < numberOfCores; i ++)
764 {
765 BOOST_CHECK(counterUidsMultiCore[i] == counterUidsMultiCore[i - 1] + 1);
766 }
767 BOOST_CHECK(counterUidsMultiCore.back() == counterUidsMultiCore.front() + numberOfCores - 1);
Matteo Martincighab173e92019-09-05 12:02:04 +0100768}
769
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100770BOOST_AUTO_TEST_CASE(CheckCounterDirectoryRegisterCategory)
Matteo Martincighab173e92019-09-05 12:02:04 +0100771{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100772 CounterDirectory counterDirectory;
773 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
774 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
775 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
776 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
Matteo Martincighab173e92019-09-05 12:02:04 +0100777
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100778 // Register a category with an invalid name
779 const Category* noCategory = nullptr;
780 BOOST_CHECK_THROW(noCategory = counterDirectory.RegisterCategory(""), armnn::InvalidArgumentException);
781 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
782 BOOST_CHECK(!noCategory);
Matteo Martincighab173e92019-09-05 12:02:04 +0100783
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100784 // Register a category with an invalid name
785 BOOST_CHECK_THROW(noCategory = counterDirectory.RegisterCategory("invalid category"),
786 armnn::InvalidArgumentException);
787 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
788 BOOST_CHECK(!noCategory);
789
790 // Register a new category
791 const std::string categoryName = "some_category";
792 const Category* category = nullptr;
793 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
794 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
795 BOOST_CHECK(category);
796 BOOST_CHECK(category->m_Name == categoryName);
797 BOOST_CHECK(category->m_Counters.empty());
798 BOOST_CHECK(category->m_DeviceUid == 0);
799 BOOST_CHECK(category->m_CounterSetUid == 0);
800
801 // Get the registered category
802 const Category* registeredCategory = counterDirectory.GetCategory(categoryName);
803 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
804 BOOST_CHECK(registeredCategory);
805 BOOST_CHECK(registeredCategory == category);
806
807 // Try to get a category not registered
808 const Category* notRegisteredCategory = counterDirectory.GetCategory("not_registered_category");
809 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
810 BOOST_CHECK(!notRegisteredCategory);
811
812 // Register a category already registered
813 const Category* anotherCategory = nullptr;
814 BOOST_CHECK_THROW(anotherCategory = counterDirectory.RegisterCategory(categoryName),
815 armnn::InvalidArgumentException);
816 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
817 BOOST_CHECK(!anotherCategory);
818
819 // Register a device for testing
820 const std::string deviceName = "some_device";
821 const Device* device = nullptr;
822 BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName));
823 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
824 BOOST_CHECK(device);
825 BOOST_CHECK(device->m_Uid >= 1);
826 BOOST_CHECK(device->m_Name == deviceName);
827 BOOST_CHECK(device->m_Cores == 0);
828
829 // Register a new category not associated to any device
830 const std::string categoryWoDeviceName = "some_category_without_device";
831 const Category* categoryWoDevice = nullptr;
832 BOOST_CHECK_NO_THROW(categoryWoDevice = counterDirectory.RegisterCategory(categoryWoDeviceName, 0));
833 BOOST_CHECK(counterDirectory.GetCategoryCount() == 2);
834 BOOST_CHECK(categoryWoDevice);
835 BOOST_CHECK(categoryWoDevice->m_Name == categoryWoDeviceName);
836 BOOST_CHECK(categoryWoDevice->m_Counters.empty());
837 BOOST_CHECK(categoryWoDevice->m_DeviceUid == 0);
838 BOOST_CHECK(categoryWoDevice->m_CounterSetUid == 0);
839
840 // Register a new category associated to an invalid device
841 const std::string categoryWInvalidDeviceName = "some_category_with_invalid_device";
842
843 ARMNN_NO_CONVERSION_WARN_BEGIN
844 uint16_t invalidDeviceUid = device->m_Uid + 10;
845 ARMNN_NO_CONVERSION_WARN_END
846
847 const Category* categoryWInvalidDevice = nullptr;
848 BOOST_CHECK_THROW(categoryWInvalidDevice
849 = counterDirectory.RegisterCategory(categoryWInvalidDeviceName,
850 invalidDeviceUid),
851 armnn::InvalidArgumentException);
852 BOOST_CHECK(counterDirectory.GetCategoryCount() == 2);
853 BOOST_CHECK(!categoryWInvalidDevice);
854
855 // Register a new category associated to a valid device
856 const std::string categoryWValidDeviceName = "some_category_with_valid_device";
857 const Category* categoryWValidDevice = nullptr;
858 BOOST_CHECK_NO_THROW(categoryWValidDevice
859 = counterDirectory.RegisterCategory(categoryWValidDeviceName,
860 device->m_Uid));
861 BOOST_CHECK(counterDirectory.GetCategoryCount() == 3);
862 BOOST_CHECK(categoryWValidDevice);
863 BOOST_CHECK(categoryWValidDevice != category);
864 BOOST_CHECK(categoryWValidDevice->m_Name == categoryWValidDeviceName);
865 BOOST_CHECK(categoryWValidDevice->m_DeviceUid == device->m_Uid);
866 BOOST_CHECK(categoryWValidDevice->m_CounterSetUid == 0);
867
868 // Register a counter set for testing
869 const std::string counterSetName = "some_counter_set";
870 const CounterSet* counterSet = nullptr;
871 BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
872 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
873 BOOST_CHECK(counterSet);
874 BOOST_CHECK(counterSet->m_Uid >= 1);
875 BOOST_CHECK(counterSet->m_Name == counterSetName);
876 BOOST_CHECK(counterSet->m_Count == 0);
877
878 // Register a new category not associated to any counter set
879 const std::string categoryWoCounterSetName = "some_category_without_counter_set";
880 const Category* categoryWoCounterSet = nullptr;
881 BOOST_CHECK_NO_THROW(categoryWoCounterSet
882 = counterDirectory.RegisterCategory(categoryWoCounterSetName,
883 armnn::EmptyOptional(),
884 0));
885 BOOST_CHECK(counterDirectory.GetCategoryCount() == 4);
886 BOOST_CHECK(categoryWoCounterSet);
887 BOOST_CHECK(categoryWoCounterSet->m_Name == categoryWoCounterSetName);
888 BOOST_CHECK(categoryWoCounterSet->m_DeviceUid == 0);
889 BOOST_CHECK(categoryWoCounterSet->m_CounterSetUid == 0);
890
891 // Register a new category associated to an invalid counter set
892 const std::string categoryWInvalidCounterSetName = "some_category_with_invalid_counter_set";
893
894 ARMNN_NO_CONVERSION_WARN_BEGIN
895 uint16_t invalidCunterSetUid = counterSet->m_Uid + 10;
896 ARMNN_NO_CONVERSION_WARN_END
897
898 const Category* categoryWInvalidCounterSet = nullptr;
899 BOOST_CHECK_THROW(categoryWInvalidCounterSet
900 = counterDirectory.RegisterCategory(categoryWInvalidCounterSetName,
901 armnn::EmptyOptional(),
902 invalidCunterSetUid),
903 armnn::InvalidArgumentException);
904 BOOST_CHECK(counterDirectory.GetCategoryCount() == 4);
905 BOOST_CHECK(!categoryWInvalidCounterSet);
906
907 // Register a new category associated to a valid counter set
908 const std::string categoryWValidCounterSetName = "some_category_with_valid_counter_set";
909 const Category* categoryWValidCounterSet = nullptr;
910 BOOST_CHECK_NO_THROW(categoryWValidCounterSet
911 = counterDirectory.RegisterCategory(categoryWValidCounterSetName,
912 armnn::EmptyOptional(),
913 counterSet->m_Uid));
914 BOOST_CHECK(counterDirectory.GetCategoryCount() == 5);
915 BOOST_CHECK(categoryWValidCounterSet);
916 BOOST_CHECK(categoryWValidCounterSet != category);
917 BOOST_CHECK(categoryWValidCounterSet->m_Name == categoryWValidCounterSetName);
918 BOOST_CHECK(categoryWValidCounterSet->m_DeviceUid == 0);
919 BOOST_CHECK(categoryWValidCounterSet->m_CounterSetUid == counterSet->m_Uid);
920
921 // Register a new category associated to a valid device and counter set
922 const std::string categoryWValidDeviceAndValidCounterSetName = "some_category_with_valid_device_and_counter_set";
923 const Category* categoryWValidDeviceAndValidCounterSet = nullptr;
924 BOOST_CHECK_NO_THROW(categoryWValidDeviceAndValidCounterSet
925 = counterDirectory.RegisterCategory(categoryWValidDeviceAndValidCounterSetName,
926 device->m_Uid,
927 counterSet->m_Uid));
928 BOOST_CHECK(counterDirectory.GetCategoryCount() == 6);
929 BOOST_CHECK(categoryWValidDeviceAndValidCounterSet);
930 BOOST_CHECK(categoryWValidDeviceAndValidCounterSet != category);
931 BOOST_CHECK(categoryWValidDeviceAndValidCounterSet->m_Name == categoryWValidDeviceAndValidCounterSetName);
932 BOOST_CHECK(categoryWValidDeviceAndValidCounterSet->m_DeviceUid == device->m_Uid);
933 BOOST_CHECK(categoryWValidDeviceAndValidCounterSet->m_CounterSetUid == counterSet->m_Uid);
934}
935
936BOOST_AUTO_TEST_CASE(CheckCounterDirectoryRegisterDevice)
937{
938 CounterDirectory counterDirectory;
939 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
940 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
941 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
942 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
943
944 // Register a device with an invalid name
945 const Device* noDevice = nullptr;
946 BOOST_CHECK_THROW(noDevice = counterDirectory.RegisterDevice(""), armnn::InvalidArgumentException);
947 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
948 BOOST_CHECK(!noDevice);
949
950 // Register a device with an invalid name
951 BOOST_CHECK_THROW(noDevice = counterDirectory.RegisterDevice("inv@lid nam€"), armnn::InvalidArgumentException);
952 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
953 BOOST_CHECK(!noDevice);
954
955 // Register a new device with no cores or parent category
956 const std::string deviceName = "some_device";
957 const Device* device = nullptr;
958 BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName));
959 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
960 BOOST_CHECK(device);
961 BOOST_CHECK(device->m_Name == deviceName);
962 BOOST_CHECK(device->m_Uid >= 1);
963 BOOST_CHECK(device->m_Cores == 0);
964
Matteo Martincigh657ab2d2019-09-18 10:53:24 +0100965 // Try getting an unregistered device
966 const Device* unregisteredDevice = counterDirectory.GetDevice(9999);
967 BOOST_CHECK(!unregisteredDevice);
968
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100969 // Get the registered device
970 const Device* registeredDevice = counterDirectory.GetDevice(device->m_Uid);
971 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
972 BOOST_CHECK(registeredDevice);
973 BOOST_CHECK(registeredDevice == device);
974
Matteo Martincigh657ab2d2019-09-18 10:53:24 +0100975 // Register a device with the name of a device already registered
976 const Device* deviceSameName = nullptr;
977 BOOST_CHECK_THROW(deviceSameName = counterDirectory.RegisterDevice(deviceName), armnn::InvalidArgumentException);
978 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
979 BOOST_CHECK(!deviceSameName);
980
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100981 // Register a new device with cores and no parent category
982 const std::string deviceWCoresName = "some_device_with_cores";
983 const Device* deviceWCores = nullptr;
984 BOOST_CHECK_NO_THROW(deviceWCores = counterDirectory.RegisterDevice(deviceWCoresName, 2));
985 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
986 BOOST_CHECK(deviceWCores);
987 BOOST_CHECK(deviceWCores->m_Name == deviceWCoresName);
988 BOOST_CHECK(deviceWCores->m_Uid >= 1);
989 BOOST_CHECK(deviceWCores->m_Uid > device->m_Uid);
990 BOOST_CHECK(deviceWCores->m_Cores == 2);
991
992 // Get the registered device
993 const Device* registeredDeviceWCores = counterDirectory.GetDevice(deviceWCores->m_Uid);
994 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
995 BOOST_CHECK(registeredDeviceWCores);
996 BOOST_CHECK(registeredDeviceWCores == deviceWCores);
997 BOOST_CHECK(registeredDeviceWCores != device);
998
999 // Register a new device with cores and invalid parent category
1000 const std::string deviceWCoresWInvalidParentCategoryName = "some_device_with_cores_with_invalid_parent_category";
1001 const Device* deviceWCoresWInvalidParentCategory = nullptr;
1002 BOOST_CHECK_THROW(deviceWCoresWInvalidParentCategory
1003 = counterDirectory.RegisterDevice(deviceWCoresWInvalidParentCategoryName,
1004 3,
1005 std::string("")),
1006 armnn::InvalidArgumentException);
1007 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
1008 BOOST_CHECK(!deviceWCoresWInvalidParentCategory);
1009
1010 // Register a new device with cores and invalid parent category
1011 const std::string deviceWCoresWInvalidParentCategoryName2 = "some_device_with_cores_with_invalid_parent_category2";
1012 const Device* deviceWCoresWInvalidParentCategory2 = nullptr;
1013 BOOST_CHECK_THROW(deviceWCoresWInvalidParentCategory2
1014 = counterDirectory.RegisterDevice(deviceWCoresWInvalidParentCategoryName2,
1015 3,
1016 std::string("invalid_parent_category")),
1017 armnn::InvalidArgumentException);
1018 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
1019 BOOST_CHECK(!deviceWCoresWInvalidParentCategory2);
1020
1021 // Register a category for testing
1022 const std::string categoryName = "some_category";
1023 const Category* category = nullptr;
1024 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
1025 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
1026 BOOST_CHECK(category);
1027 BOOST_CHECK(category->m_Name == categoryName);
1028 BOOST_CHECK(category->m_Counters.empty());
1029 BOOST_CHECK(category->m_DeviceUid == 0);
1030 BOOST_CHECK(category->m_CounterSetUid == 0);
1031
1032 // Register a new device with cores and valid parent category
1033 const std::string deviceWCoresWValidParentCategoryName = "some_device_with_cores_with_valid_parent_category";
1034 const Device* deviceWCoresWValidParentCategory = nullptr;
1035 BOOST_CHECK_NO_THROW(deviceWCoresWValidParentCategory
1036 = counterDirectory.RegisterDevice(deviceWCoresWValidParentCategoryName,
1037 4,
1038 categoryName));
1039 BOOST_CHECK(counterDirectory.GetDeviceCount() == 3);
1040 BOOST_CHECK(deviceWCoresWValidParentCategory);
1041 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Name == deviceWCoresWValidParentCategoryName);
1042 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Uid >= 1);
1043 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Uid > device->m_Uid);
1044 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Uid > deviceWCores->m_Uid);
1045 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Cores == 4);
1046 BOOST_CHECK(category->m_DeviceUid == deviceWCoresWValidParentCategory->m_Uid);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001047
1048 // Register a device associated to a category already associated to a different device
1049 const std::string deviceSameCategoryName = "some_device_with_invalid_parent_category";
1050 const Device* deviceSameCategory = nullptr;
1051 BOOST_CHECK_THROW(deviceSameCategory = counterDirectory.RegisterDevice(deviceSameCategoryName, 0, categoryName),
1052 armnn::InvalidArgumentException);
1053 BOOST_CHECK(counterDirectory.GetDeviceCount() == 3);
1054 BOOST_CHECK(!deviceSameCategory);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001055}
1056
1057BOOST_AUTO_TEST_CASE(CheckCounterDirectoryRegisterCounterSet)
1058{
1059 CounterDirectory counterDirectory;
1060 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
1061 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
1062 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
1063 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1064
1065 // Register a counter set with an invalid name
1066 const CounterSet* noCounterSet = nullptr;
1067 BOOST_CHECK_THROW(noCounterSet = counterDirectory.RegisterCounterSet(""), armnn::InvalidArgumentException);
1068 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
1069 BOOST_CHECK(!noCounterSet);
1070
1071 // Register a counter set with an invalid name
1072 BOOST_CHECK_THROW(noCounterSet = counterDirectory.RegisterCounterSet("invalid name"),
1073 armnn::InvalidArgumentException);
1074 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
1075 BOOST_CHECK(!noCounterSet);
1076
1077 // Register a new counter set with no count or parent category
1078 const std::string counterSetName = "some_counter_set";
1079 const CounterSet* counterSet = nullptr;
1080 BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
1081 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1082 BOOST_CHECK(counterSet);
1083 BOOST_CHECK(counterSet->m_Name == counterSetName);
1084 BOOST_CHECK(counterSet->m_Uid >= 1);
1085 BOOST_CHECK(counterSet->m_Count == 0);
1086
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001087 // Try getting an unregistered counter set
1088 const CounterSet* unregisteredCounterSet = counterDirectory.GetCounterSet(9999);
1089 BOOST_CHECK(!unregisteredCounterSet);
1090
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001091 // Get the registered counter set
1092 const CounterSet* registeredCounterSet = counterDirectory.GetCounterSet(counterSet->m_Uid);
1093 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1094 BOOST_CHECK(registeredCounterSet);
1095 BOOST_CHECK(registeredCounterSet == counterSet);
1096
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001097 // Register a counter set with the name of a counter set already registered
1098 const CounterSet* counterSetSameName = nullptr;
1099 BOOST_CHECK_THROW(counterSetSameName = counterDirectory.RegisterCounterSet(counterSetName),
1100 armnn::InvalidArgumentException);
1101 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1102 BOOST_CHECK(!counterSetSameName);
1103
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001104 // Register a new counter set with count and no parent category
1105 const std::string counterSetWCountName = "some_counter_set_with_count";
1106 const CounterSet* counterSetWCount = nullptr;
1107 BOOST_CHECK_NO_THROW(counterSetWCount = counterDirectory.RegisterCounterSet(counterSetWCountName, 37));
1108 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 2);
1109 BOOST_CHECK(counterSetWCount);
1110 BOOST_CHECK(counterSetWCount->m_Name == counterSetWCountName);
1111 BOOST_CHECK(counterSetWCount->m_Uid >= 1);
1112 BOOST_CHECK(counterSetWCount->m_Uid > counterSet->m_Uid);
1113 BOOST_CHECK(counterSetWCount->m_Count == 37);
1114
1115 // Get the registered counter set
1116 const CounterSet* registeredCounterSetWCount = counterDirectory.GetCounterSet(counterSetWCount->m_Uid);
1117 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 2);
1118 BOOST_CHECK(registeredCounterSetWCount);
1119 BOOST_CHECK(registeredCounterSetWCount == counterSetWCount);
1120 BOOST_CHECK(registeredCounterSetWCount != counterSet);
1121
1122 // Register a new counter set with count and invalid parent category
1123 const std::string counterSetWCountWInvalidParentCategoryName = "some_counter_set_with_count_"
1124 "with_invalid_parent_category";
1125 const CounterSet* counterSetWCountWInvalidParentCategory = nullptr;
1126 BOOST_CHECK_THROW(counterSetWCountWInvalidParentCategory
1127 = counterDirectory.RegisterCounterSet(counterSetWCountWInvalidParentCategoryName,
1128 42,
1129 std::string("")),
1130 armnn::InvalidArgumentException);
1131 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 2);
1132 BOOST_CHECK(!counterSetWCountWInvalidParentCategory);
1133
1134 // Register a new counter set with count and invalid parent category
1135 const std::string counterSetWCountWInvalidParentCategoryName2 = "some_counter_set_with_count_"
1136 "with_invalid_parent_category2";
1137 const CounterSet* counterSetWCountWInvalidParentCategory2 = nullptr;
1138 BOOST_CHECK_THROW(counterSetWCountWInvalidParentCategory2
1139 = counterDirectory.RegisterCounterSet(counterSetWCountWInvalidParentCategoryName2,
1140 42,
1141 std::string("invalid_parent_category")),
1142 armnn::InvalidArgumentException);
1143 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 2);
1144 BOOST_CHECK(!counterSetWCountWInvalidParentCategory2);
1145
1146 // Register a category for testing
1147 const std::string categoryName = "some_category";
1148 const Category* category = nullptr;
1149 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
1150 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
1151 BOOST_CHECK(category);
1152 BOOST_CHECK(category->m_Name == categoryName);
1153 BOOST_CHECK(category->m_Counters.empty());
1154 BOOST_CHECK(category->m_DeviceUid == 0);
1155 BOOST_CHECK(category->m_CounterSetUid == 0);
1156
1157 // Register a new counter set with count and valid parent category
1158 const std::string counterSetWCountWValidParentCategoryName = "some_counter_set_with_count_"
1159 "with_valid_parent_category";
1160 const CounterSet* counterSetWCountWValidParentCategory = nullptr;
1161 BOOST_CHECK_NO_THROW(counterSetWCountWValidParentCategory
1162 = counterDirectory.RegisterCounterSet(counterSetWCountWValidParentCategoryName,
1163 42,
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001164 categoryName));
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001165 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 3);
1166 BOOST_CHECK(counterSetWCountWValidParentCategory);
1167 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Name == counterSetWCountWValidParentCategoryName);
1168 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Uid >= 1);
1169 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Uid > counterSet->m_Uid);
1170 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Uid > counterSetWCount->m_Uid);
1171 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Count == 42);
1172 BOOST_CHECK(category->m_CounterSetUid == counterSetWCountWValidParentCategory->m_Uid);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001173
1174 // Register a counter set associated to a category already associated to a different counter set
1175 const std::string counterSetSameCategoryName = "some_counter_set_with_invalid_parent_category";
1176 const CounterSet* counterSetSameCategory = nullptr;
1177 BOOST_CHECK_THROW(counterSetSameCategory = counterDirectory.RegisterCounterSet(counterSetSameCategoryName,
1178 0,
1179 categoryName),
1180 armnn::InvalidArgumentException);
1181 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 3);
1182 BOOST_CHECK(!counterSetSameCategory);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001183}
1184
1185BOOST_AUTO_TEST_CASE(CheckCounterDirectoryRegisterCounter)
1186{
1187 CounterDirectory counterDirectory;
1188 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
1189 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
1190 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
1191 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1192
1193 // Register a counter with an invalid parent category name
1194 const Counter* noCounter = nullptr;
1195 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("",
1196 0,
1197 1,
1198 123.45f,
1199 "valid name",
1200 "valid description"),
1201 armnn::InvalidArgumentException);
1202 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1203 BOOST_CHECK(!noCounter);
1204
1205 // Register a counter with an invalid parent category name
1206 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("invalid parent category",
1207 0,
1208 1,
1209 123.45f,
1210 "valid name",
1211 "valid description"),
1212 armnn::InvalidArgumentException);
1213 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1214 BOOST_CHECK(!noCounter);
1215
1216 // Register a counter with an invalid class
1217 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("valid_parent_category",
1218 2,
1219 1,
1220 123.45f,
1221 "valid name",
1222 "valid description"),
1223 armnn::InvalidArgumentException);
1224 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1225 BOOST_CHECK(!noCounter);
1226
1227 // Register a counter with an invalid interpolation
1228 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("valid_parent_category",
1229 0,
1230 3,
1231 123.45f,
1232 "valid name",
1233 "valid description"),
1234 armnn::InvalidArgumentException);
1235 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1236 BOOST_CHECK(!noCounter);
1237
1238 // Register a counter with an invalid multiplier
1239 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("valid_parent_category",
1240 0,
1241 1,
1242 .0f,
1243 "valid name",
1244 "valid description"),
1245 armnn::InvalidArgumentException);
1246 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1247 BOOST_CHECK(!noCounter);
1248
1249 // Register a counter with an invalid name
1250 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("valid_parent_category",
1251 0,
1252 1,
1253 123.45f,
1254 "",
1255 "valid description"),
1256 armnn::InvalidArgumentException);
1257 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1258 BOOST_CHECK(!noCounter);
1259
1260 // Register a counter with an invalid name
1261 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("valid_parent_category",
1262 0,
1263 1,
1264 123.45f,
1265 "invalid nam€",
1266 "valid description"),
1267 armnn::InvalidArgumentException);
1268 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1269 BOOST_CHECK(!noCounter);
1270
1271 // Register a counter with an invalid description
1272 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("valid_parent_category",
1273 0,
1274 1,
1275 123.45f,
1276 "valid name",
1277 ""),
1278 armnn::InvalidArgumentException);
1279 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1280 BOOST_CHECK(!noCounter);
1281
1282 // Register a counter with an invalid description
1283 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("valid_parent_category",
1284 0,
1285 1,
1286 123.45f,
1287 "valid name",
1288 "inv@lid description"),
1289 armnn::InvalidArgumentException);
1290 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1291 BOOST_CHECK(!noCounter);
1292
1293 // Register a counter with an invalid unit2
1294 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("valid_parent_category",
1295 0,
1296 1,
1297 123.45f,
1298 "valid name",
1299 "valid description",
1300 std::string("Mb/s2")),
1301 armnn::InvalidArgumentException);
1302 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1303 BOOST_CHECK(!noCounter);
1304
1305 // Register a counter with a non-existing parent category name
1306 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("invalid_parent_category",
1307 0,
1308 1,
1309 123.45f,
1310 "valid name",
1311 "valid description"),
1312 armnn::InvalidArgumentException);
1313 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1314 BOOST_CHECK(!noCounter);
1315
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001316 // Try getting an unregistered counter
1317 const Counter* unregisteredCounter = counterDirectory.GetCounter(9999);
1318 BOOST_CHECK(!unregisteredCounter);
1319
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001320 // Register a category for testing
1321 const std::string categoryName = "some_category";
1322 const Category* category = nullptr;
1323 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
1324 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
1325 BOOST_CHECK(category);
1326 BOOST_CHECK(category->m_Name == categoryName);
1327 BOOST_CHECK(category->m_Counters.empty());
1328 BOOST_CHECK(category->m_DeviceUid == 0);
1329 BOOST_CHECK(category->m_CounterSetUid == 0);
1330
1331 // Register a counter with a valid parent category name
1332 const Counter* counter = nullptr;
1333 BOOST_CHECK_NO_THROW(counter = counterDirectory.RegisterCounter(categoryName,
1334 0,
1335 1,
1336 123.45f,
1337 "valid name",
1338 "valid description"));
1339 BOOST_CHECK(counterDirectory.GetCounterCount() == 1);
1340 BOOST_CHECK(counter);
1341 BOOST_CHECK(counter->m_Uid >= 0);
1342 BOOST_CHECK(counter->m_MaxCounterUid == counter->m_Uid);
1343 BOOST_CHECK(counter->m_Class == 0);
1344 BOOST_CHECK(counter->m_Interpolation == 1);
1345 BOOST_CHECK(counter->m_Multiplier == 123.45f);
1346 BOOST_CHECK(counter->m_Name == "valid name");
1347 BOOST_CHECK(counter->m_Description == "valid description");
1348 BOOST_CHECK(counter->m_Units == "");
1349 BOOST_CHECK(counter->m_DeviceUid == 0);
1350 BOOST_CHECK(counter->m_CounterSetUid == 0);
1351 BOOST_CHECK(category->m_Counters.size() == 1);
1352 BOOST_CHECK(category->m_Counters.back() == counter->m_Uid);
1353
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001354 // Register a counter with a name of a counter already registered for the given parent category name
1355 const Counter* counterSameName = nullptr;
1356 BOOST_CHECK_THROW(counterSameName = counterDirectory.RegisterCounter(categoryName,
1357 0,
1358 0,
1359 1.0f,
1360 "valid name",
1361 "valid description"),
1362 armnn::InvalidArgumentException);
1363 BOOST_CHECK(counterDirectory.GetCounterCount() == 1);
1364 BOOST_CHECK(!counterSameName);
1365
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001366 // Register a counter with a valid parent category name and units
1367 const Counter* counterWUnits = nullptr;
1368 BOOST_CHECK_NO_THROW(counterWUnits = counterDirectory.RegisterCounter(categoryName,
1369 0,
1370 1,
1371 123.45f,
1372 "valid name 2",
1373 "valid description",
1374 std::string("Mnnsq2"))); // Units
1375 BOOST_CHECK(counterDirectory.GetCounterCount() == 2);
1376 BOOST_CHECK(counterWUnits);
1377 BOOST_CHECK(counterWUnits->m_Uid >= 0);
1378 BOOST_CHECK(counterWUnits->m_Uid > counter->m_Uid);
1379 BOOST_CHECK(counterWUnits->m_MaxCounterUid == counterWUnits->m_Uid);
1380 BOOST_CHECK(counterWUnits->m_Class == 0);
1381 BOOST_CHECK(counterWUnits->m_Interpolation == 1);
1382 BOOST_CHECK(counterWUnits->m_Multiplier == 123.45f);
1383 BOOST_CHECK(counterWUnits->m_Name == "valid name 2");
1384 BOOST_CHECK(counterWUnits->m_Description == "valid description");
1385 BOOST_CHECK(counterWUnits->m_Units == "Mnnsq2");
1386 BOOST_CHECK(counterWUnits->m_DeviceUid == 0);
1387 BOOST_CHECK(counterWUnits->m_CounterSetUid == 0);
1388 BOOST_CHECK(category->m_Counters.size() == 2);
1389 BOOST_CHECK(category->m_Counters.back() == counterWUnits->m_Uid);
1390
1391 // Register a counter with a valid parent category name and not associated with a device
1392 const Counter* counterWoDevice = nullptr;
1393 BOOST_CHECK_NO_THROW(counterWoDevice = counterDirectory.RegisterCounter(categoryName,
1394 0,
1395 1,
1396 123.45f,
1397 "valid name 3",
1398 "valid description",
1399 armnn::EmptyOptional(), // Units
1400 armnn::EmptyOptional(), // Number of cores
1401 0)); // Device UID
1402 BOOST_CHECK(counterDirectory.GetCounterCount() == 3);
1403 BOOST_CHECK(counterWoDevice);
1404 BOOST_CHECK(counterWoDevice->m_Uid >= 0);
1405 BOOST_CHECK(counterWoDevice->m_Uid > counter->m_Uid);
1406 BOOST_CHECK(counterWoDevice->m_MaxCounterUid == counterWoDevice->m_Uid);
1407 BOOST_CHECK(counterWoDevice->m_Class == 0);
1408 BOOST_CHECK(counterWoDevice->m_Interpolation == 1);
1409 BOOST_CHECK(counterWoDevice->m_Multiplier == 123.45f);
1410 BOOST_CHECK(counterWoDevice->m_Name == "valid name 3");
1411 BOOST_CHECK(counterWoDevice->m_Description == "valid description");
1412 BOOST_CHECK(counterWoDevice->m_Units == "");
1413 BOOST_CHECK(counterWoDevice->m_DeviceUid == 0);
1414 BOOST_CHECK(counterWoDevice->m_CounterSetUid == 0);
1415 BOOST_CHECK(category->m_Counters.size() == 3);
1416 BOOST_CHECK(category->m_Counters.back() == counterWoDevice->m_Uid);
1417
1418 // Register a counter with a valid parent category name and associated to an invalid device
1419 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(categoryName,
1420 0,
1421 1,
1422 123.45f,
1423 "valid name 4",
1424 "valid description",
1425 armnn::EmptyOptional(), // Units
1426 armnn::EmptyOptional(), // Number of cores
1427 100), // Device UID
1428 armnn::InvalidArgumentException);
1429 BOOST_CHECK(counterDirectory.GetCounterCount() == 3);
1430 BOOST_CHECK(!noCounter);
1431
1432 // Register a device for testing
1433 const std::string deviceName = "some_device";
1434 const Device* device = nullptr;
1435 BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName));
1436 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
1437 BOOST_CHECK(device);
1438 BOOST_CHECK(device->m_Name == deviceName);
1439 BOOST_CHECK(device->m_Uid >= 1);
1440 BOOST_CHECK(device->m_Cores == 0);
1441
1442 // Register a counter with a valid parent category name and associated to a device
1443 const Counter* counterWDevice = nullptr;
1444 BOOST_CHECK_NO_THROW(counterWDevice = counterDirectory.RegisterCounter(categoryName,
1445 0,
1446 1,
1447 123.45f,
1448 "valid name 5",
1449 "valid description",
1450 armnn::EmptyOptional(), // Units
1451 armnn::EmptyOptional(), // Number of cores
1452 device->m_Uid)); // Device UID
1453 BOOST_CHECK(counterDirectory.GetCounterCount() == 4);
1454 BOOST_CHECK(counterWDevice);
1455 BOOST_CHECK(counterWDevice->m_Uid >= 0);
1456 BOOST_CHECK(counterWDevice->m_Uid > counter->m_Uid);
1457 BOOST_CHECK(counterWDevice->m_MaxCounterUid == counterWDevice->m_Uid);
1458 BOOST_CHECK(counterWDevice->m_Class == 0);
1459 BOOST_CHECK(counterWDevice->m_Interpolation == 1);
1460 BOOST_CHECK(counterWDevice->m_Multiplier == 123.45f);
1461 BOOST_CHECK(counterWDevice->m_Name == "valid name 5");
1462 BOOST_CHECK(counterWDevice->m_Description == "valid description");
1463 BOOST_CHECK(counterWDevice->m_Units == "");
1464 BOOST_CHECK(counterWDevice->m_DeviceUid == device->m_Uid);
1465 BOOST_CHECK(counterWDevice->m_CounterSetUid == 0);
1466 BOOST_CHECK(category->m_Counters.size() == 4);
1467 BOOST_CHECK(category->m_Counters.back() == counterWDevice->m_Uid);
1468
1469 // Register a counter with a valid parent category name and not associated with a counter set
1470 const Counter* counterWoCounterSet = nullptr;
1471 BOOST_CHECK_NO_THROW(counterWoCounterSet
1472 = counterDirectory.RegisterCounter(categoryName,
1473 0,
1474 1,
1475 123.45f,
1476 "valid name 6",
1477 "valid description",
1478 armnn::EmptyOptional(), // Units
1479 armnn::EmptyOptional(), // Number of cores
1480 armnn::EmptyOptional(), // Device UID
1481 0)); // Counter set UID
1482 BOOST_CHECK(counterDirectory.GetCounterCount() == 5);
1483 BOOST_CHECK(counterWoCounterSet);
1484 BOOST_CHECK(counterWoCounterSet->m_Uid >= 0);
1485 BOOST_CHECK(counterWoCounterSet->m_Uid > counter->m_Uid);
1486 BOOST_CHECK(counterWoCounterSet->m_MaxCounterUid == counterWoCounterSet->m_Uid);
1487 BOOST_CHECK(counterWoCounterSet->m_Class == 0);
1488 BOOST_CHECK(counterWoCounterSet->m_Interpolation == 1);
1489 BOOST_CHECK(counterWoCounterSet->m_Multiplier == 123.45f);
1490 BOOST_CHECK(counterWoCounterSet->m_Name == "valid name 6");
1491 BOOST_CHECK(counterWoCounterSet->m_Description == "valid description");
1492 BOOST_CHECK(counterWoCounterSet->m_Units == "");
1493 BOOST_CHECK(counterWoCounterSet->m_DeviceUid == 0);
1494 BOOST_CHECK(counterWoCounterSet->m_CounterSetUid == 0);
1495 BOOST_CHECK(category->m_Counters.size() == 5);
1496 BOOST_CHECK(category->m_Counters.back() == counterWoCounterSet->m_Uid);
1497
1498 // Register a counter with a valid parent category name and associated to an invalid counter set
1499 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(categoryName,
1500 0,
1501 1,
1502 123.45f,
1503 "valid name 7",
1504 "valid description",
1505 armnn::EmptyOptional(), // Units
1506 armnn::EmptyOptional(), // Number of cores
1507 armnn::EmptyOptional(), // Device UID
1508 100), // Counter set UID
1509 armnn::InvalidArgumentException);
1510 BOOST_CHECK(counterDirectory.GetCounterCount() == 5);
1511 BOOST_CHECK(!noCounter);
1512
1513 // Register a counter with a valid parent category name and with a given number of cores
1514 const Counter* counterWNumberOfCores = nullptr;
1515 uint16_t numberOfCores = 15;
1516 BOOST_CHECK_NO_THROW(counterWNumberOfCores
1517 = counterDirectory.RegisterCounter(categoryName,
1518 0,
1519 1,
1520 123.45f,
1521 "valid name 8",
1522 "valid description",
1523 armnn::EmptyOptional(), // Units
1524 numberOfCores, // Number of cores
1525 armnn::EmptyOptional(), // Device UID
1526 armnn::EmptyOptional())); // Counter set UID
1527 BOOST_CHECK(counterDirectory.GetCounterCount() == 20);
1528 BOOST_CHECK(counterWNumberOfCores);
1529 BOOST_CHECK(counterWNumberOfCores->m_Uid >= 0);
1530 BOOST_CHECK(counterWNumberOfCores->m_Uid > counter->m_Uid);
1531 BOOST_CHECK(counterWNumberOfCores->m_MaxCounterUid == counterWNumberOfCores->m_Uid + numberOfCores - 1);
1532 BOOST_CHECK(counterWNumberOfCores->m_Class == 0);
1533 BOOST_CHECK(counterWNumberOfCores->m_Interpolation == 1);
1534 BOOST_CHECK(counterWNumberOfCores->m_Multiplier == 123.45f);
1535 BOOST_CHECK(counterWNumberOfCores->m_Name == "valid name 8");
1536 BOOST_CHECK(counterWNumberOfCores->m_Description == "valid description");
1537 BOOST_CHECK(counterWNumberOfCores->m_Units == "");
1538 BOOST_CHECK(counterWNumberOfCores->m_DeviceUid == 0);
1539 BOOST_CHECK(counterWNumberOfCores->m_CounterSetUid == 0);
1540 BOOST_CHECK(category->m_Counters.size() == 20);
1541 for (size_t i = 0; i < numberOfCores; i ++)
1542 {
1543 BOOST_CHECK(category->m_Counters[category->m_Counters.size() - numberOfCores + i] ==
1544 counterWNumberOfCores->m_Uid + i);
1545 }
1546
1547 // Register a multi-core device for testing
1548 const std::string multiCoreDeviceName = "some_multi_core_device";
1549 const Device* multiCoreDevice = nullptr;
1550 BOOST_CHECK_NO_THROW(multiCoreDevice = counterDirectory.RegisterDevice(multiCoreDeviceName, 4));
1551 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
1552 BOOST_CHECK(multiCoreDevice);
1553 BOOST_CHECK(multiCoreDevice->m_Name == multiCoreDeviceName);
1554 BOOST_CHECK(multiCoreDevice->m_Uid >= 1);
1555 BOOST_CHECK(multiCoreDevice->m_Cores == 4);
1556
1557 // Register a counter with a valid parent category name and associated to the multi-core device
1558 const Counter* counterWMultiCoreDevice = nullptr;
1559 BOOST_CHECK_NO_THROW(counterWMultiCoreDevice
1560 = counterDirectory.RegisterCounter(categoryName,
1561 0,
1562 1,
1563 123.45f,
1564 "valid name 9",
1565 "valid description",
1566 armnn::EmptyOptional(), // Units
1567 armnn::EmptyOptional(), // Number of cores
1568 multiCoreDevice->m_Uid, // Device UID
1569 armnn::EmptyOptional())); // Counter set UID
1570 BOOST_CHECK(counterDirectory.GetCounterCount() == 24);
1571 BOOST_CHECK(counterWMultiCoreDevice);
1572 BOOST_CHECK(counterWMultiCoreDevice->m_Uid >= 0);
1573 BOOST_CHECK(counterWMultiCoreDevice->m_Uid > counter->m_Uid);
1574 BOOST_CHECK(counterWMultiCoreDevice->m_MaxCounterUid ==
1575 counterWMultiCoreDevice->m_Uid + multiCoreDevice->m_Cores - 1);
1576 BOOST_CHECK(counterWMultiCoreDevice->m_Class == 0);
1577 BOOST_CHECK(counterWMultiCoreDevice->m_Interpolation == 1);
1578 BOOST_CHECK(counterWMultiCoreDevice->m_Multiplier == 123.45f);
1579 BOOST_CHECK(counterWMultiCoreDevice->m_Name == "valid name 9");
1580 BOOST_CHECK(counterWMultiCoreDevice->m_Description == "valid description");
1581 BOOST_CHECK(counterWMultiCoreDevice->m_Units == "");
1582 BOOST_CHECK(counterWMultiCoreDevice->m_DeviceUid == multiCoreDevice->m_Uid);
1583 BOOST_CHECK(counterWMultiCoreDevice->m_CounterSetUid == 0);
1584 BOOST_CHECK(category->m_Counters.size() == 24);
1585 for (size_t i = 0; i < 4; i ++)
1586 {
1587 BOOST_CHECK(category->m_Counters[category->m_Counters.size() - 4 + i] == counterWMultiCoreDevice->m_Uid + i);
1588 }
1589
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001590 // Register a multi-core device associate to a parent category for testing
1591 const std::string multiCoreDeviceNameWParentCategory = "some_multi_core_device_with_parent_category";
1592 const Device* multiCoreDeviceWParentCategory = nullptr;
1593 BOOST_CHECK_NO_THROW(multiCoreDeviceWParentCategory =
1594 counterDirectory.RegisterDevice(multiCoreDeviceNameWParentCategory, 2, categoryName));
1595 BOOST_CHECK(counterDirectory.GetDeviceCount() == 3);
1596 BOOST_CHECK(multiCoreDeviceWParentCategory);
1597 BOOST_CHECK(multiCoreDeviceWParentCategory->m_Name == multiCoreDeviceNameWParentCategory);
1598 BOOST_CHECK(multiCoreDeviceWParentCategory->m_Uid >= 1);
1599 BOOST_CHECK(multiCoreDeviceWParentCategory->m_Cores == 2);
1600
1601 // Register a counter with a valid parent category name and getting the number of cores of the multi-core device
1602 // associated to that category
1603 const Counter* counterWMultiCoreDeviceWParentCategory = nullptr;
1604 BOOST_CHECK_NO_THROW(counterWMultiCoreDeviceWParentCategory
1605 = counterDirectory.RegisterCounter(categoryName,
1606 0,
1607 1,
1608 123.45f,
1609 "valid name 10",
1610 "valid description",
1611 armnn::EmptyOptional(), // Units
1612 armnn::EmptyOptional(), // Number of cores
1613 armnn::EmptyOptional(), // Device UID
1614 armnn::EmptyOptional())); // Counter set UID
1615 BOOST_CHECK(counterDirectory.GetCounterCount() == 26);
1616 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory);
1617 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Uid >= 0);
1618 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Uid > counter->m_Uid);
1619 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_MaxCounterUid ==
1620 counterWMultiCoreDeviceWParentCategory->m_Uid + multiCoreDeviceWParentCategory->m_Cores - 1);
1621 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Class == 0);
1622 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Interpolation == 1);
1623 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Multiplier == 123.45f);
1624 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Name == "valid name 10");
1625 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Description == "valid description");
1626 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Units == "");
1627 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_DeviceUid == 0);
1628 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_CounterSetUid == 0);
1629 BOOST_CHECK(category->m_Counters.size() == 26);
1630 for (size_t i = 0; i < 2; i ++)
1631 {
1632 BOOST_CHECK(category->m_Counters[category->m_Counters.size() - 2 + i] ==
1633 counterWMultiCoreDeviceWParentCategory->m_Uid + i);
1634 }
1635
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001636 // Register a counter set for testing
1637 const std::string counterSetName = "some_counter_set";
1638 const CounterSet* counterSet = nullptr;
1639 BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
1640 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1641 BOOST_CHECK(counterSet);
1642 BOOST_CHECK(counterSet->m_Name == counterSetName);
1643 BOOST_CHECK(counterSet->m_Uid >= 1);
1644 BOOST_CHECK(counterSet->m_Count == 0);
1645
1646 // Register a counter with a valid parent category name and associated to a counter set
1647 const Counter* counterWCounterSet = nullptr;
1648 BOOST_CHECK_NO_THROW(counterWCounterSet
1649 = counterDirectory.RegisterCounter(categoryName,
1650 0,
1651 1,
1652 123.45f,
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001653 "valid name 11",
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001654 "valid description",
1655 armnn::EmptyOptional(), // Units
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001656 0, // Number of cores
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001657 armnn::EmptyOptional(), // Device UID
1658 counterSet->m_Uid)); // Counter set UID
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001659 BOOST_CHECK(counterDirectory.GetCounterCount() == 27);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001660 BOOST_CHECK(counterWCounterSet);
1661 BOOST_CHECK(counterWCounterSet->m_Uid >= 0);
1662 BOOST_CHECK(counterWCounterSet->m_Uid > counter->m_Uid);
1663 BOOST_CHECK(counterWCounterSet->m_MaxCounterUid == counterWCounterSet->m_Uid);
1664 BOOST_CHECK(counterWCounterSet->m_Class == 0);
1665 BOOST_CHECK(counterWCounterSet->m_Interpolation == 1);
1666 BOOST_CHECK(counterWCounterSet->m_Multiplier == 123.45f);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001667 BOOST_CHECK(counterWCounterSet->m_Name == "valid name 11");
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001668 BOOST_CHECK(counterWCounterSet->m_Description == "valid description");
1669 BOOST_CHECK(counterWCounterSet->m_Units == "");
1670 BOOST_CHECK(counterWCounterSet->m_DeviceUid == 0);
1671 BOOST_CHECK(counterWCounterSet->m_CounterSetUid == counterSet->m_Uid);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001672 BOOST_CHECK(category->m_Counters.size() == 27);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001673 BOOST_CHECK(category->m_Counters.back() == counterWCounterSet->m_Uid);
1674
1675 // Register a counter with a valid parent category name and associated to a device and a counter set
1676 const Counter* counterWDeviceWCounterSet = nullptr;
1677 BOOST_CHECK_NO_THROW(counterWDeviceWCounterSet
1678 = counterDirectory.RegisterCounter(categoryName,
1679 0,
1680 1,
1681 123.45f,
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001682 "valid name 12",
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001683 "valid description",
1684 armnn::EmptyOptional(), // Units
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001685 1, // Number of cores
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001686 device->m_Uid, // Device UID
1687 counterSet->m_Uid)); // Counter set UID
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001688 BOOST_CHECK(counterDirectory.GetCounterCount() == 28);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001689 BOOST_CHECK(counterWDeviceWCounterSet);
1690 BOOST_CHECK(counterWDeviceWCounterSet->m_Uid >= 0);
1691 BOOST_CHECK(counterWDeviceWCounterSet->m_Uid > counter->m_Uid);
1692 BOOST_CHECK(counterWDeviceWCounterSet->m_MaxCounterUid == counterWDeviceWCounterSet->m_Uid);
1693 BOOST_CHECK(counterWDeviceWCounterSet->m_Class == 0);
1694 BOOST_CHECK(counterWDeviceWCounterSet->m_Interpolation == 1);
1695 BOOST_CHECK(counterWDeviceWCounterSet->m_Multiplier == 123.45f);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001696 BOOST_CHECK(counterWDeviceWCounterSet->m_Name == "valid name 12");
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001697 BOOST_CHECK(counterWDeviceWCounterSet->m_Description == "valid description");
1698 BOOST_CHECK(counterWDeviceWCounterSet->m_Units == "");
1699 BOOST_CHECK(counterWDeviceWCounterSet->m_DeviceUid == device->m_Uid);
1700 BOOST_CHECK(counterWDeviceWCounterSet->m_CounterSetUid == counterSet->m_Uid);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001701 BOOST_CHECK(category->m_Counters.size() == 28);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001702 BOOST_CHECK(category->m_Counters.back() == counterWDeviceWCounterSet->m_Uid);
1703
1704 // Register another category for testing
1705 const std::string anotherCategoryName = "some_other_category";
1706 const Category* anotherCategory = nullptr;
1707 BOOST_CHECK_NO_THROW(anotherCategory = counterDirectory.RegisterCategory(anotherCategoryName));
1708 BOOST_CHECK(counterDirectory.GetCategoryCount() == 2);
1709 BOOST_CHECK(anotherCategory);
1710 BOOST_CHECK(anotherCategory != category);
1711 BOOST_CHECK(anotherCategory->m_Name == anotherCategoryName);
1712 BOOST_CHECK(anotherCategory->m_Counters.empty());
1713 BOOST_CHECK(anotherCategory->m_DeviceUid == 0);
1714 BOOST_CHECK(anotherCategory->m_CounterSetUid == 0);
1715
1716 // Register a counter to the other category
1717 const Counter* anotherCounter = nullptr;
1718 BOOST_CHECK_NO_THROW(anotherCounter = counterDirectory.RegisterCounter(anotherCategoryName,
1719 1,
1720 0,
1721 .00043f,
1722 "valid name",
1723 "valid description",
1724 armnn::EmptyOptional(), // Units
1725 armnn::EmptyOptional(), // Number of cores
1726 device->m_Uid, // Device UID
1727 counterSet->m_Uid)); // Counter set UID
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001728 BOOST_CHECK(counterDirectory.GetCounterCount() == 29);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001729 BOOST_CHECK(anotherCounter);
1730 BOOST_CHECK(anotherCounter->m_Uid >= 0);
1731 BOOST_CHECK(anotherCounter->m_MaxCounterUid == anotherCounter->m_Uid);
1732 BOOST_CHECK(anotherCounter->m_Class == 1);
1733 BOOST_CHECK(anotherCounter->m_Interpolation == 0);
1734 BOOST_CHECK(anotherCounter->m_Multiplier == .00043f);
1735 BOOST_CHECK(anotherCounter->m_Name == "valid name");
1736 BOOST_CHECK(anotherCounter->m_Description == "valid description");
1737 BOOST_CHECK(anotherCounter->m_Units == "");
1738 BOOST_CHECK(anotherCounter->m_DeviceUid == device->m_Uid);
1739 BOOST_CHECK(anotherCounter->m_CounterSetUid == counterSet->m_Uid);
1740 BOOST_CHECK(anotherCategory->m_Counters.size() == 1);
1741 BOOST_CHECK(anotherCategory->m_Counters.back() == anotherCounter->m_Uid);
Matteo Martincighab173e92019-09-05 12:02:04 +01001742}
1743
Ferran Balaguer1b941722019-08-28 16:57:18 +01001744BOOST_AUTO_TEST_CASE(CounterSelectionCommandHandlerParseData)
1745{
1746 using boost::numeric_cast;
1747
1748 class TestCaptureThread : public IPeriodicCounterCapture
1749 {
1750 void Start() override {};
1751 };
1752
1753 const uint32_t packetId = 0x40000;
1754
1755 uint32_t version = 1;
1756 Holder holder;
1757 TestCaptureThread captureThread;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001758 MockProfilingConnection mockProfilingConnection;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001759 MockBuffer mockBuffer(512);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001760 SendCounterPacket sendCounterPacket(mockProfilingConnection, mockBuffer);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001761
1762 uint32_t sizeOfUint32 = numeric_cast<uint32_t>(sizeof(uint32_t));
1763 uint32_t sizeOfUint16 = numeric_cast<uint32_t>(sizeof(uint16_t));
1764
1765 // Data with period and counters
1766 uint32_t period1 = 10;
1767 uint32_t dataLength1 = 8;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001768 uint32_t offset = 0;
1769
FinnWilliamsArma0c78712019-09-16 12:06:47 +01001770 std::unique_ptr<char[]> uniqueData1 = std::make_unique<char[]>(dataLength1);
1771 unsigned char* data1 = reinterpret_cast<unsigned char*>(uniqueData1.get());
1772
Ferran Balaguer1b941722019-08-28 16:57:18 +01001773 WriteUint32(data1, offset, period1);
1774 offset += sizeOfUint32;
1775 WriteUint16(data1, offset, 4000);
1776 offset += sizeOfUint16;
1777 WriteUint16(data1, offset, 5000);
1778
FinnWilliamsArma0c78712019-09-16 12:06:47 +01001779 Packet packetA(packetId, dataLength1, uniqueData1);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001780
1781 PeriodicCounterSelectionCommandHandler commandHandler(packetId, version, holder, captureThread,
1782 sendCounterPacket);
1783 commandHandler(packetA);
1784
1785 std::vector<uint16_t> counterIds = holder.GetCaptureData().GetCounterIds();
1786
1787 BOOST_TEST(holder.GetCaptureData().GetCapturePeriod() == period1);
1788 BOOST_TEST(counterIds.size() == 2);
1789 BOOST_TEST(counterIds[0] == 4000);
1790 BOOST_TEST(counterIds[1] == 5000);
1791
1792 unsigned int size = 0;
1793
1794 const unsigned char* readBuffer = mockBuffer.GetReadBuffer(size);
1795
1796 offset = 0;
1797
1798 uint32_t headerWord0 = ReadUint32(readBuffer, offset);
1799 offset += sizeOfUint32;
1800 uint32_t headerWord1 = ReadUint32(readBuffer, offset);
1801 offset += sizeOfUint32;
1802 uint32_t period = ReadUint32(readBuffer, offset);
1803
1804 BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family
1805 BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 4); // packet id
1806 BOOST_TEST(headerWord1 == 8); // data lenght
1807 BOOST_TEST(period == 10); // capture period
1808
1809 uint16_t counterId = 0;
1810 offset += sizeOfUint32;
1811 counterId = ReadUint16(readBuffer, offset);
1812 BOOST_TEST(counterId == 4000);
1813 offset += sizeOfUint16;
1814 counterId = ReadUint16(readBuffer, offset);
1815 BOOST_TEST(counterId == 5000);
1816
1817 // Data with period only
1818 uint32_t period2 = 11;
1819 uint32_t dataLength2 = 4;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001820
FinnWilliamsArma0c78712019-09-16 12:06:47 +01001821 std::unique_ptr<char[]> uniqueData2 = std::make_unique<char[]>(dataLength2);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001822
FinnWilliamsArma0c78712019-09-16 12:06:47 +01001823 WriteUint32(reinterpret_cast<unsigned char*>(uniqueData2.get()), 0, period2);
1824
1825 Packet packetB(packetId, dataLength2, uniqueData2);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001826
1827 commandHandler(packetB);
1828
1829 counterIds = holder.GetCaptureData().GetCounterIds();
1830
1831 BOOST_TEST(holder.GetCaptureData().GetCapturePeriod() == period2);
1832 BOOST_TEST(counterIds.size() == 0);
1833
1834 readBuffer = mockBuffer.GetReadBuffer(size);
1835
1836 offset = 0;
1837
1838 headerWord0 = ReadUint32(readBuffer, offset);
1839 offset += sizeOfUint32;
1840 headerWord1 = ReadUint32(readBuffer, offset);
1841 offset += sizeOfUint32;
1842 period = ReadUint32(readBuffer, offset);
1843
1844 BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family
1845 BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 4); // packet id
1846 BOOST_TEST(headerWord1 == 4); // data lenght
1847 BOOST_TEST(period == 11); // capture period
Ferran Balaguer1b941722019-08-28 16:57:18 +01001848}
1849
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001850BOOST_AUTO_TEST_CASE(CheckConnectionAcknowledged)
1851{
1852 using boost::numeric_cast;
1853
1854 const uint32_t connectionPacketId = 0x10000;
1855 const uint32_t version = 1;
1856
1857 uint32_t sizeOfUint32 = numeric_cast<uint32_t>(sizeof(uint32_t));
1858 uint32_t sizeOfUint16 = numeric_cast<uint32_t>(sizeof(uint16_t));
1859
1860 // Data with period and counters
1861 uint32_t period1 = 10;
1862 uint32_t dataLength1 = 8;
1863 uint32_t offset = 0;
1864
1865 std::unique_ptr<char[]> uniqueData1 = std::make_unique<char[]>(dataLength1);
1866 unsigned char* data1 = reinterpret_cast<unsigned char*>(uniqueData1.get());
1867
1868 WriteUint32(data1, offset, period1);
1869 offset += sizeOfUint32;
1870 WriteUint16(data1, offset, 4000);
1871 offset += sizeOfUint16;
1872 WriteUint16(data1, offset, 5000);
1873
1874 Packet packetA(connectionPacketId, dataLength1, uniqueData1);
1875
1876 ProfilingStateMachine profilingState(ProfilingState::Uninitialised);
1877 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::Uninitialised);
1878
1879 ConnectionAcknowledgedCommandHandler commandHandler(connectionPacketId, version, profilingState);
1880
1881 // command handler received packet on ProfilingState::Uninitialised
1882 BOOST_CHECK_THROW(commandHandler(packetA), armnn::Exception);
1883
1884 profilingState.TransitionToState(ProfilingState::NotConnected);
1885 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::NotConnected);
1886 // command handler received packet on ProfilingState::NotConnected
1887 BOOST_CHECK_THROW(commandHandler(packetA), armnn::Exception);
1888
1889 profilingState.TransitionToState(ProfilingState::WaitingForAck);
1890 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::WaitingForAck);
1891 // command handler received packet on ProfilingState::WaitingForAck
1892 commandHandler(packetA);
1893 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::Active);
1894
1895 // command handler received packet on ProfilingState::Active
1896 commandHandler(packetA);
1897 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::Active);
1898
1899 // command handler received different packet
1900 const uint32_t differentPacketId = 0x40000;
1901 Packet packetB(differentPacketId, dataLength1, uniqueData1);
1902 ConnectionAcknowledgedCommandHandler differentCommandHandler(differentPacketId, version, profilingState);
1903 BOOST_CHECK_THROW(differentCommandHandler(packetB), armnn::Exception);
1904}
1905
Teresa Charlin9bab4962019-09-06 12:28:35 +01001906BOOST_AUTO_TEST_CASE(CheckSocketProfilingConnection)
1907{
1908 // Check that creating a SocketProfilingConnection results in an exception as the Gator UDS doesn't exist.
1909 BOOST_CHECK_THROW(new SocketProfilingConnection(), armnn::Exception);
1910}
1911
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001912BOOST_AUTO_TEST_CASE(SwTraceIsValidCharTest)
1913{
1914 // Only ASCII 7-bit encoding supported
1915 for (unsigned char c = 0; c < 128; c++)
1916 {
1917 BOOST_CHECK(SwTraceCharPolicy::IsValidChar(c));
1918 }
1919
1920 // Not ASCII
1921 for (unsigned char c = 255; c >= 128; c++)
1922 {
1923 BOOST_CHECK(!SwTraceCharPolicy::IsValidChar(c));
1924 }
1925}
1926
1927BOOST_AUTO_TEST_CASE(SwTraceIsValidNameCharTest)
1928{
1929 // Only alpha-numeric and underscore ASCII 7-bit encoding supported
1930 const unsigned char validChars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
1931 for (unsigned char i = 0; i < sizeof(validChars) / sizeof(validChars[0]) - 1; i++)
1932 {
1933 BOOST_CHECK(SwTraceNameCharPolicy::IsValidChar(validChars[i]));
1934 }
1935
1936 // Non alpha-numeric chars
1937 for (unsigned char c = 0; c < 48; c++)
1938 {
1939 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1940 }
1941 for (unsigned char c = 58; c < 65; c++)
1942 {
1943 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1944 }
1945 for (unsigned char c = 91; c < 95; c++)
1946 {
1947 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1948 }
1949 for (unsigned char c = 96; c < 97; c++)
1950 {
1951 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1952 }
1953 for (unsigned char c = 123; c < 128; c++)
1954 {
1955 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1956 }
1957
1958 // Not ASCII
1959 for (unsigned char c = 255; c >= 128; c++)
1960 {
1961 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1962 }
1963}
1964
1965BOOST_AUTO_TEST_CASE(IsValidSwTraceStringTest)
1966{
1967 // Valid SWTrace strings
1968 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>(""));
1969 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("_"));
1970 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("0123"));
1971 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("valid_string"));
1972 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("VALID_string_456"));
1973 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>(" "));
1974 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("valid string"));
1975 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("!$%"));
1976 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("valid|\\~string#123"));
1977
1978 // Invalid SWTrace strings
1979 BOOST_CHECK(!IsValidSwTraceString<SwTraceCharPolicy>("€£"));
1980 BOOST_CHECK(!IsValidSwTraceString<SwTraceCharPolicy>("invalid‡string"));
1981 BOOST_CHECK(!IsValidSwTraceString<SwTraceCharPolicy>("12Ž34"));
1982}
1983
1984BOOST_AUTO_TEST_CASE(IsValidSwTraceNameStringTest)
1985{
1986 // Valid SWTrace name strings
1987 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>(""));
1988 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>("_"));
1989 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>("0123"));
1990 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>("valid_string"));
1991 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>("VALID_string_456"));
1992
1993 // Invalid SWTrace name strings
1994 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>(" "));
1995 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("invalid string"));
1996 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("!$%"));
1997 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("invalid|\\~string#123"));
1998 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("€£"));
1999 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("invalid‡string"));
2000 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("12Ž34"));
2001}
2002
2003template <typename SwTracePolicy>
2004void StringToSwTraceStringTestHelper(const std::string& testString, std::vector<uint32_t> buffer, size_t expectedSize)
2005{
2006 // Convert the test string to a SWTrace string
2007 BOOST_CHECK(StringToSwTraceString<SwTracePolicy>(testString, buffer));
2008
2009 // The buffer must contain at least the length of the string
2010 BOOST_CHECK(!buffer.empty());
2011
2012 // The buffer must be of the expected size (in words)
2013 BOOST_CHECK(buffer.size() == expectedSize);
2014
2015 // The first word of the byte must be the length of the string including the null-terminator
2016 BOOST_CHECK(buffer[0] == testString.size() + 1);
2017
2018 // The contents of the buffer must match the test string
2019 BOOST_CHECK(std::memcmp(testString.data(), buffer.data() + 1, testString.size()) == 0);
2020
2021 // The buffer must include the null-terminator at the end of the string
2022 size_t nullTerminatorIndex = sizeof(uint32_t) + testString.size();
2023 BOOST_CHECK(reinterpret_cast<unsigned char*>(buffer.data())[nullTerminatorIndex] == '\0');
2024}
2025
2026BOOST_AUTO_TEST_CASE(StringToSwTraceStringTest)
2027{
2028 std::vector<uint32_t> buffer;
2029
2030 // Valid SWTrace strings (expected size in words)
2031 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("", buffer, 2);
2032 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("_", buffer, 2);
2033 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("0123", buffer, 3);
2034 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("valid_string", buffer, 5);
2035 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("VALID_string_456", buffer, 6);
2036 StringToSwTraceStringTestHelper<SwTraceCharPolicy>(" ", buffer, 2);
2037 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("valid string", buffer, 5);
2038 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("!$%", buffer, 2);
2039 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("valid|\\~string#123", buffer, 6);
2040
2041 // Invalid SWTrace strings
2042 BOOST_CHECK(!StringToSwTraceString<SwTraceCharPolicy>("€£", buffer));
2043 BOOST_CHECK(buffer.empty());
2044 BOOST_CHECK(!StringToSwTraceString<SwTraceCharPolicy>("invalid‡string", buffer));
2045 BOOST_CHECK(buffer.empty());
2046 BOOST_CHECK(!StringToSwTraceString<SwTraceCharPolicy>("12Ž34", buffer));
2047 BOOST_CHECK(buffer.empty());
2048}
2049
2050BOOST_AUTO_TEST_CASE(StringToSwTraceNameStringTest)
2051{
2052 std::vector<uint32_t> buffer;
2053
2054 // Valid SWTrace namestrings (expected size in words)
2055 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("", buffer, 2);
2056 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("_", buffer, 2);
2057 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("0123", buffer, 3);
2058 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("valid_string", buffer, 5);
2059 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("VALID_string_456", buffer, 6);
2060
2061 // Invalid SWTrace namestrings
2062 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>(" ", buffer));
2063 BOOST_CHECK(buffer.empty());
2064 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("invalid string", buffer));
2065 BOOST_CHECK(buffer.empty());
2066 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("!$%", buffer));
2067 BOOST_CHECK(buffer.empty());
2068 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("invalid|\\~string#123", buffer));
2069 BOOST_CHECK(buffer.empty());
2070 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("€£", buffer));
2071 BOOST_CHECK(buffer.empty());
2072 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("invalid‡string", buffer));
2073 BOOST_CHECK(buffer.empty());
2074 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("12Ž34", buffer));
2075 BOOST_CHECK(buffer.empty());
2076}
2077
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002078BOOST_AUTO_TEST_CASE(CheckPeriodicCounterCaptureThread)
2079{
2080 class CaptureReader : public IReadCounterValue
2081 {
2082 public:
2083 CaptureReader() {}
2084
2085 void GetCounterValue(uint16_t index, uint32_t &value) const override
2086 {
2087 if (m_Data.count(index))
2088 {
2089 value = m_Data.at(index);
2090 }
2091 else
2092 {
2093 value = 0;
2094 }
2095 }
2096
2097 void SetCounterValue(uint16_t index, uint32_t value)
2098 {
2099 if (!m_Data.count(index))
2100 {
2101 m_Data.insert(std::pair<uint16_t, uint32_t>(index, value));
2102 }
2103 else
2104 {
2105 m_Data.at(index) = value;
2106 }
2107 }
2108
2109 private:
2110 std::map<uint16_t, uint32_t> m_Data;
2111 };
2112
2113 Holder data;
2114 std::vector<uint16_t> captureIds1 = { 0, 1 };
2115 std::vector<uint16_t> captureIds2;
2116
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002117 MockProfilingConnection mockProfilingConnection;
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002118 MockBuffer mockBuffer(512);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002119 SendCounterPacket sendCounterPacket(mockProfilingConnection, mockBuffer);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002120
2121 std::vector<uint16_t> counterIds;
2122 CaptureReader captureReader;
2123
2124 unsigned int valueA = 10;
2125 unsigned int valueB = 15;
2126 unsigned int numSteps = 5;
2127
2128 PeriodicCounterCapture periodicCounterCapture(std::ref(data), std::ref(sendCounterPacket), captureReader);
2129
2130 for(unsigned int i = 0; i < numSteps; ++i)
2131 {
2132 data.SetCaptureData(1, captureIds1);
2133 captureReader.SetCounterValue(0, valueA * (i + 1));
2134 captureReader.SetCounterValue(1, valueB * (i + 1));
2135
2136 periodicCounterCapture.Start();
2137
2138 std::this_thread::sleep_for(std::chrono::milliseconds(200));
2139
2140 periodicCounterCapture.Start();
2141
2142 data.SetCaptureData(0, captureIds2);
2143
2144 periodicCounterCapture.Start();
2145 }
2146
2147 periodicCounterCapture.Join();
2148
2149 unsigned int size = 0;
2150
2151 const unsigned char* buffer = mockBuffer.GetReadBuffer(size);
2152
2153 uint32_t headerWord0 = ReadUint32(buffer, 0);
2154 uint32_t headerWord1 = ReadUint32(buffer, 4);
2155
2156 BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 1); // packet family
2157 BOOST_TEST(((headerWord0 >> 19) & 0x3F) == 0); // packet class
2158 BOOST_TEST(((headerWord0 >> 16) & 0x3) == 0); // packet type
2159 BOOST_TEST(headerWord1 == 20); // data length
2160
2161 uint32_t offset = 16;
2162 uint16_t readIndex = ReadUint16(buffer, offset);
2163 BOOST_TEST(0 == readIndex);
2164
2165 offset += 2;
2166 uint32_t readValue = ReadUint32(buffer, offset);
2167 BOOST_TEST((valueA * numSteps) == readValue);
2168
2169 offset += 4;
2170 readIndex = ReadUint16(buffer, offset);
2171 BOOST_TEST(1 == readIndex);
2172
2173 offset += 2;
2174 readValue = ReadUint32(buffer, offset);
2175 BOOST_TEST((valueB * numSteps) == readValue);
2176}
2177
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002178BOOST_AUTO_TEST_CASE(RequestCounterDirectoryCommandHandlerTest0)
2179{
2180 using boost::numeric_cast;
2181
2182 const uint32_t packetId = 0x30000;
2183 const uint32_t version = 1;
2184
2185 std::unique_ptr<char[]> packetData;
2186
2187 Packet packetA(packetId, 0, packetData);
2188
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002189 MockProfilingConnection mockProfilingConnection;
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002190 MockBuffer mockBuffer(1024);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002191 SendCounterPacket sendCounterPacket(mockProfilingConnection, mockBuffer);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002192
2193 CounterDirectory counterDirectory;
2194
2195 RequestCounterDirectoryCommandHandler commandHandler(packetId, version, counterDirectory, sendCounterPacket);
2196 commandHandler(packetA);
2197
2198 unsigned int size = 0;
2199 const unsigned char* readBuffer = mockBuffer.GetReadBuffer(size);
2200
2201 uint32_t headerWord0 = ReadUint32(readBuffer, 0);
2202 uint32_t headerWord1 = ReadUint32(readBuffer, 4);
2203
2204 BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family
2205 BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 2); // packet id
Matteo Martincighf74ff2f2019-09-24 11:38:32 +01002206 BOOST_TEST(headerWord1 == 24); // data length
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002207
2208 uint32_t bodyHeaderWord0 = ReadUint32(readBuffer, 8);
2209 uint16_t deviceRecordCount = numeric_cast<uint16_t>(bodyHeaderWord0 >> 16);
2210 BOOST_TEST(deviceRecordCount == 0); // device_records_count
2211}
2212
2213BOOST_AUTO_TEST_CASE(RequestCounterDirectoryCommandHandlerTest1)
2214{
2215 using boost::numeric_cast;
2216
2217 const uint32_t packetId = 0x30000;
2218 const uint32_t version = 1;
2219
2220 std::unique_ptr<char[]> packetData;
2221
2222 Packet packetA(packetId, 0, packetData);
2223
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002224 MockProfilingConnection mockProfilingConnection;
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002225 MockBuffer mockBuffer(1024);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002226 SendCounterPacket sendCounterPacket(mockProfilingConnection, mockBuffer);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002227
2228 CounterDirectory counterDirectory;
2229 const Device* device = counterDirectory.RegisterDevice("deviceA", 1);
2230 const CounterSet* counterSet = counterDirectory.RegisterCounterSet("countersetA");
2231 counterDirectory.RegisterCategory("categoryA", device->m_Uid, counterSet->m_Uid);
2232 counterDirectory.RegisterCounter("categoryA", 0, 1, 2.0f, "counterA", "descA");
2233 counterDirectory.RegisterCounter("categoryA", 1, 1, 3.0f, "counterB", "descB");
2234
2235 RequestCounterDirectoryCommandHandler commandHandler(packetId, version, counterDirectory, sendCounterPacket);
2236 commandHandler(packetA);
2237
2238 unsigned int size = 0;
2239 const unsigned char* readBuffer = mockBuffer.GetReadBuffer(size);
2240
2241 uint32_t headerWord0 = ReadUint32(readBuffer, 0);
2242 uint32_t headerWord1 = ReadUint32(readBuffer, 4);
2243
2244 BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family
2245 BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 2); // packet id
Matteo Martincighf74ff2f2019-09-24 11:38:32 +01002246 BOOST_TEST(headerWord1 == 240); // data length
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002247
2248 uint32_t bodyHeaderWord0 = ReadUint32(readBuffer, 8);
2249 uint32_t bodyHeaderWord1 = ReadUint32(readBuffer, 12);
2250 uint32_t bodyHeaderWord2 = ReadUint32(readBuffer, 16);
2251 uint32_t bodyHeaderWord3 = ReadUint32(readBuffer, 20);
2252 uint32_t bodyHeaderWord4 = ReadUint32(readBuffer, 24);
2253 uint32_t bodyHeaderWord5 = ReadUint32(readBuffer, 28);
2254 uint16_t deviceRecordCount = numeric_cast<uint16_t>(bodyHeaderWord0 >> 16);
2255 uint16_t counterSetRecordCount = numeric_cast<uint16_t>(bodyHeaderWord2 >> 16);
2256 uint16_t categoryRecordCount = numeric_cast<uint16_t>(bodyHeaderWord4 >> 16);
2257 BOOST_TEST(deviceRecordCount == 1); // device_records_count
2258 BOOST_TEST(bodyHeaderWord1 == 0); // device_records_pointer_table_offset
2259 BOOST_TEST(counterSetRecordCount == 1); // counter_set_count
2260 BOOST_TEST(bodyHeaderWord3 == 4); // counter_set_pointer_table_offset
2261 BOOST_TEST(categoryRecordCount == 1); // categories_count
2262 BOOST_TEST(bodyHeaderWord5 == 8); // categories_pointer_table_offset
2263
2264 uint32_t deviceRecordOffset = ReadUint32(readBuffer, 32);
2265 BOOST_TEST(deviceRecordOffset == 0);
2266
2267 uint32_t counterSetRecordOffset = ReadUint32(readBuffer, 36);
2268 BOOST_TEST(counterSetRecordOffset == 20);
2269
2270 uint32_t categoryRecordOffset = ReadUint32(readBuffer, 40);
2271 BOOST_TEST(categoryRecordOffset == 44);
2272}
2273
Francis Murtagh1f7db452019-08-14 09:49:34 +01002274BOOST_AUTO_TEST_SUITE_END()