79a241b5404c186092985415d1a01c2194a1cc52
[platform/upstream/armnn.git] / src / profiling / test / ProfilingTests.cpp
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5
6 #include "ProfilingTests.hpp"
7
8 #include <CommandHandler.hpp>
9 #include <CommandHandlerKey.hpp>
10 #include <CommandHandlerRegistry.hpp>
11 #include <ConnectionAcknowledgedCommandHandler.hpp>
12 #include <CounterDirectory.hpp>
13 #include <EncodeVersion.hpp>
14 #include <Holder.hpp>
15 #include <ICounterValues.hpp>
16 #include <Packet.hpp>
17 #include <PacketVersionResolver.hpp>
18 #include <PeriodicCounterCapture.hpp>
19 #include <PeriodicCounterSelectionCommandHandler.hpp>
20 #include <ProfilingStateMachine.hpp>
21 #include <ProfilingUtils.hpp>
22 #include <RequestCounterDirectoryCommandHandler.hpp>
23 #include <Runtime.hpp>
24 #include <SocketProfilingConnection.hpp>
25
26 #include <armnn/Conversion.hpp>
27
28 #include <armnn/Utils.hpp>
29
30 #include <boost/algorithm/string.hpp>
31 #include <boost/numeric/conversion/cast.hpp>
32
33 #include <cstdint>
34 #include <cstring>
35 #include <iostream>
36 #include <limits>
37 #include <map>
38 #include <random>
39
40 using namespace armnn::profiling;
41
42 BOOST_AUTO_TEST_SUITE(ExternalProfiling)
43
44 BOOST_AUTO_TEST_CASE(CheckCommandHandlerKeyComparisons)
45 {
46     CommandHandlerKey testKey1_0(1, 1, 1);
47     CommandHandlerKey testKey1_1(1, 1, 1);
48     CommandHandlerKey testKey1_2(1, 2, 1);
49
50     CommandHandlerKey testKey0(0, 1, 1);
51     CommandHandlerKey testKey1(0, 1, 1);
52     CommandHandlerKey testKey2(0, 1, 1);
53     CommandHandlerKey testKey3(0, 0, 0);
54     CommandHandlerKey testKey4(0, 2, 2);
55     CommandHandlerKey testKey5(0, 0, 2);
56
57     BOOST_CHECK(testKey1_0 > testKey0);
58     BOOST_CHECK(testKey1_0 == testKey1_1);
59     BOOST_CHECK(testKey1_0 < testKey1_2);
60
61     BOOST_CHECK(testKey1 < testKey4);
62     BOOST_CHECK(testKey1 > testKey3);
63     BOOST_CHECK(testKey1 <= testKey4);
64     BOOST_CHECK(testKey1 >= testKey3);
65     BOOST_CHECK(testKey1 <= testKey2);
66     BOOST_CHECK(testKey1 >= testKey2);
67     BOOST_CHECK(testKey1 == testKey2);
68     BOOST_CHECK(testKey1 == testKey1);
69
70     BOOST_CHECK(!(testKey1 == testKey5));
71     BOOST_CHECK(!(testKey1 != testKey1));
72     BOOST_CHECK(testKey1 != testKey5);
73
74     BOOST_CHECK(testKey1 == testKey2 && testKey2 == testKey1);
75     BOOST_CHECK(testKey0 == testKey1 && testKey1 == testKey2 && testKey0 == testKey2);
76
77     BOOST_CHECK(testKey1.GetPacketId() == 1);
78     BOOST_CHECK(testKey1.GetVersion() == 1);
79
80     std::vector<CommandHandlerKey> vect = { CommandHandlerKey(0, 0, 1), CommandHandlerKey(0, 2, 0),
81                                             CommandHandlerKey(0, 1, 0), CommandHandlerKey(0, 2, 1),
82                                             CommandHandlerKey(0, 1, 1), CommandHandlerKey(0, 0, 1),
83                                             CommandHandlerKey(0, 2, 0), CommandHandlerKey(0, 0, 0) };
84
85     std::sort(vect.begin(), vect.end());
86
87     std::vector<CommandHandlerKey> expectedVect = { CommandHandlerKey(0, 0, 0), CommandHandlerKey(0, 0, 1),
88                                                     CommandHandlerKey(0, 0, 1), CommandHandlerKey(0, 1, 0),
89                                                     CommandHandlerKey(0, 1, 1), CommandHandlerKey(0, 2, 0),
90                                                     CommandHandlerKey(0, 2, 0), CommandHandlerKey(0, 2, 1) };
91
92     BOOST_CHECK(vect == expectedVect);
93 }
94
95 BOOST_AUTO_TEST_CASE(CheckPacketKeyComparisons)
96 {
97     PacketKey key0(0, 0);
98     PacketKey key1(0, 0);
99     PacketKey key2(0, 1);
100     PacketKey key3(0, 2);
101     PacketKey key4(1, 0);
102     PacketKey key5(1, 0);
103     PacketKey key6(1, 1);
104
105     BOOST_CHECK(!(key0 < key1));
106     BOOST_CHECK(!(key0 > key1));
107     BOOST_CHECK(key0 <= key1);
108     BOOST_CHECK(key0 >= key1);
109     BOOST_CHECK(key0 == key1);
110     BOOST_CHECK(key0 < key2);
111     BOOST_CHECK(key2 < key3);
112     BOOST_CHECK(key3 > key0);
113     BOOST_CHECK(key4 == key5);
114     BOOST_CHECK(key4 > key0);
115     BOOST_CHECK(key5 < key6);
116     BOOST_CHECK(key5 <= key6);
117     BOOST_CHECK(key5 != key6);
118 }
119
120 BOOST_AUTO_TEST_CASE(CheckCommandHandler)
121 {
122     PacketVersionResolver packetVersionResolver;
123     ProfilingStateMachine profilingStateMachine;
124
125     TestProfilingConnectionBase testProfilingConnectionBase;
126     TestProfilingConnectionTimeoutError testProfilingConnectionTimeOutError;
127     TestProfilingConnectionArmnnError testProfilingConnectionArmnnError;
128     CounterDirectory counterDirectory;
129     MockBufferManager mockBuffer(1024);
130     SendCounterPacket sendCounterPacket(profilingStateMachine, mockBuffer);
131     ConnectionAcknowledgedCommandHandler connectionAcknowledgedCommandHandler(0, 1, 4194304, counterDirectory,
132                                                                               sendCounterPacket, profilingStateMachine);
133     CommandHandlerRegistry commandHandlerRegistry;
134
135     commandHandlerRegistry.RegisterFunctor(&connectionAcknowledgedCommandHandler);
136
137     profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
138     profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
139
140     CommandHandler commandHandler0(1, true, commandHandlerRegistry, packetVersionResolver);
141
142     commandHandler0.Start(testProfilingConnectionBase);
143     commandHandler0.Start(testProfilingConnectionBase);
144     commandHandler0.Start(testProfilingConnectionBase);
145
146     commandHandler0.Stop();
147
148     BOOST_CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::Active);
149
150     profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
151     profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
152     // commandHandler1 should give up after one timeout
153     CommandHandler commandHandler1(10, true, commandHandlerRegistry, packetVersionResolver);
154
155     commandHandler1.Start(testProfilingConnectionTimeOutError);
156
157     std::this_thread::sleep_for(std::chrono::milliseconds(100));
158
159     BOOST_CHECK(!commandHandler1.IsRunning());
160     commandHandler1.Stop();
161
162     BOOST_CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::WaitingForAck);
163     // Now commandHandler1 should persist after a timeout
164     commandHandler1.SetStopAfterTimeout(false);
165     commandHandler1.Start(testProfilingConnectionTimeOutError);
166
167     for (int i = 0; i < 100; i++)
168     {
169         if (profilingStateMachine.GetCurrentState() == ProfilingState::Active)
170         {
171             break;
172         }
173
174         std::this_thread::sleep_for(std::chrono::milliseconds(100));
175     }
176
177     commandHandler1.Stop();
178
179     BOOST_CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::Active);
180
181     CommandHandler commandHandler2(100, false, commandHandlerRegistry, packetVersionResolver);
182
183     commandHandler2.Start(testProfilingConnectionArmnnError);
184
185     // commandHandler2 should not stop once it encounters a non timing error
186     std::this_thread::sleep_for(std::chrono::milliseconds(500));
187
188     BOOST_CHECK(commandHandler2.IsRunning());
189     commandHandler2.Stop();
190 }
191
192 BOOST_AUTO_TEST_CASE(CheckEncodeVersion)
193 {
194     Version version1(12);
195
196     BOOST_CHECK(version1.GetMajor() == 0);
197     BOOST_CHECK(version1.GetMinor() == 0);
198     BOOST_CHECK(version1.GetPatch() == 12);
199
200     Version version2(4108);
201
202     BOOST_CHECK(version2.GetMajor() == 0);
203     BOOST_CHECK(version2.GetMinor() == 1);
204     BOOST_CHECK(version2.GetPatch() == 12);
205
206     Version version3(4198412);
207
208     BOOST_CHECK(version3.GetMajor() == 1);
209     BOOST_CHECK(version3.GetMinor() == 1);
210     BOOST_CHECK(version3.GetPatch() == 12);
211
212     Version version4(0);
213
214     BOOST_CHECK(version4.GetMajor() == 0);
215     BOOST_CHECK(version4.GetMinor() == 0);
216     BOOST_CHECK(version4.GetPatch() == 0);
217
218     Version version5(1, 0, 0);
219     BOOST_CHECK(version5.GetEncodedValue() == 4194304);
220 }
221
222 BOOST_AUTO_TEST_CASE(CheckPacketClass)
223 {
224     uint32_t length                              = 4;
225     std::unique_ptr<unsigned char[]> packetData0 = std::make_unique<unsigned char[]>(length);
226     std::unique_ptr<unsigned char[]> packetData1 = std::make_unique<unsigned char[]>(0);
227     std::unique_ptr<unsigned char[]> nullPacketData;
228
229     Packet packetTest0(472580096, length, packetData0);
230
231     BOOST_CHECK(packetTest0.GetHeader() == 472580096);
232     BOOST_CHECK(packetTest0.GetPacketFamily() == 7);
233     BOOST_CHECK(packetTest0.GetPacketId() == 43);
234     BOOST_CHECK(packetTest0.GetLength() == length);
235     BOOST_CHECK(packetTest0.GetPacketType() == 3);
236     BOOST_CHECK(packetTest0.GetPacketClass() == 5);
237
238     BOOST_CHECK_THROW(Packet packetTest1(472580096, 0, packetData1), armnn::Exception);
239     BOOST_CHECK_NO_THROW(Packet packetTest2(472580096, 0, nullPacketData));
240
241     Packet packetTest3(472580096, 0, nullPacketData);
242     BOOST_CHECK(packetTest3.GetLength() == 0);
243     BOOST_CHECK(packetTest3.GetData() == nullptr);
244
245     const unsigned char* packetTest0Data = packetTest0.GetData();
246     Packet packetTest4(std::move(packetTest0));
247
248     BOOST_CHECK(packetTest0.GetData() == nullptr);
249     BOOST_CHECK(packetTest4.GetData() == packetTest0Data);
250
251     BOOST_CHECK(packetTest4.GetHeader() == 472580096);
252     BOOST_CHECK(packetTest4.GetPacketFamily() == 7);
253     BOOST_CHECK(packetTest4.GetPacketId() == 43);
254     BOOST_CHECK(packetTest4.GetLength() == length);
255     BOOST_CHECK(packetTest4.GetPacketType() == 3);
256     BOOST_CHECK(packetTest4.GetPacketClass() == 5);
257 }
258
259 BOOST_AUTO_TEST_CASE(CheckCommandHandlerFunctor)
260 {
261     // Hard code the version as it will be the same during a single profiling session
262     uint32_t version = 1;
263
264     TestFunctorA testFunctorA(7, 461, version);
265     TestFunctorB testFunctorB(8, 963, version);
266     TestFunctorC testFunctorC(5, 983, version);
267
268     CommandHandlerKey keyA(testFunctorA.GetFamilyId(), testFunctorA.GetPacketId(), testFunctorA.GetVersion());
269     CommandHandlerKey keyB(testFunctorB.GetFamilyId(), testFunctorB.GetPacketId(), testFunctorB.GetVersion());
270     CommandHandlerKey keyC(testFunctorC.GetFamilyId(), testFunctorC.GetPacketId(), testFunctorC.GetVersion());
271
272     // Create the unwrapped map to simulate the Command Handler Registry
273     std::map<CommandHandlerKey, CommandHandlerFunctor*> registry;
274
275     registry.insert(std::make_pair(keyB, &testFunctorB));
276     registry.insert(std::make_pair(keyA, &testFunctorA));
277     registry.insert(std::make_pair(keyC, &testFunctorC));
278
279     // Check the order of the map is correct
280     auto it = registry.begin();
281     BOOST_CHECK(it->first == keyC);    // familyId == 5
282     it++;
283     BOOST_CHECK(it->first == keyA);    // familyId == 7
284     it++;
285     BOOST_CHECK(it->first == keyB);    // familyId == 8
286
287     std::unique_ptr<unsigned char[]> packetDataA;
288     std::unique_ptr<unsigned char[]> packetDataB;
289     std::unique_ptr<unsigned char[]> packetDataC;
290
291     Packet packetA(500000000, 0, packetDataA);
292     Packet packetB(600000000, 0, packetDataB);
293     Packet packetC(400000000, 0, packetDataC);
294
295     // Check the correct operator of derived class is called
296     registry.at(CommandHandlerKey(packetA.GetPacketFamily(), packetA.GetPacketId(), version))->operator()(packetA);
297     BOOST_CHECK(testFunctorA.GetCount() == 1);
298     BOOST_CHECK(testFunctorB.GetCount() == 0);
299     BOOST_CHECK(testFunctorC.GetCount() == 0);
300
301     registry.at(CommandHandlerKey(packetB.GetPacketFamily(), packetB.GetPacketId(), version))->operator()(packetB);
302     BOOST_CHECK(testFunctorA.GetCount() == 1);
303     BOOST_CHECK(testFunctorB.GetCount() == 1);
304     BOOST_CHECK(testFunctorC.GetCount() == 0);
305
306     registry.at(CommandHandlerKey(packetC.GetPacketFamily(), packetC.GetPacketId(), version))->operator()(packetC);
307     BOOST_CHECK(testFunctorA.GetCount() == 1);
308     BOOST_CHECK(testFunctorB.GetCount() == 1);
309     BOOST_CHECK(testFunctorC.GetCount() == 1);
310 }
311
312 BOOST_AUTO_TEST_CASE(CheckCommandHandlerRegistry)
313 {
314     // Hard code the version as it will be the same during a single profiling session
315     uint32_t version = 1;
316
317     TestFunctorA testFunctorA(7, 461, version);
318     TestFunctorB testFunctorB(8, 963, version);
319     TestFunctorC testFunctorC(5, 983, version);
320
321     // Create the Command Handler Registry
322     CommandHandlerRegistry registry;
323
324     // Register multiple different derived classes
325     registry.RegisterFunctor(&testFunctorA);
326     registry.RegisterFunctor(&testFunctorB);
327     registry.RegisterFunctor(&testFunctorC);
328
329     std::unique_ptr<unsigned char[]> packetDataA;
330     std::unique_ptr<unsigned char[]> packetDataB;
331     std::unique_ptr<unsigned char[]> packetDataC;
332
333     Packet packetA(500000000, 0, packetDataA);
334     Packet packetB(600000000, 0, packetDataB);
335     Packet packetC(400000000, 0, packetDataC);
336
337     // Check the correct operator of derived class is called
338     registry.GetFunctor(packetA.GetPacketFamily(), packetA.GetPacketId(), version)->operator()(packetA);
339     BOOST_CHECK(testFunctorA.GetCount() == 1);
340     BOOST_CHECK(testFunctorB.GetCount() == 0);
341     BOOST_CHECK(testFunctorC.GetCount() == 0);
342
343     registry.GetFunctor(packetB.GetPacketFamily(), packetB.GetPacketId(), version)->operator()(packetB);
344     BOOST_CHECK(testFunctorA.GetCount() == 1);
345     BOOST_CHECK(testFunctorB.GetCount() == 1);
346     BOOST_CHECK(testFunctorC.GetCount() == 0);
347
348     registry.GetFunctor(packetC.GetPacketFamily(), packetC.GetPacketId(), version)->operator()(packetC);
349     BOOST_CHECK(testFunctorA.GetCount() == 1);
350     BOOST_CHECK(testFunctorB.GetCount() == 1);
351     BOOST_CHECK(testFunctorC.GetCount() == 1);
352
353     // Re-register an existing key with a new function
354     registry.RegisterFunctor(&testFunctorC, testFunctorA.GetFamilyId(), testFunctorA.GetPacketId(), version);
355     registry.GetFunctor(packetA.GetPacketFamily(), packetA.GetPacketId(), version)->operator()(packetC);
356     BOOST_CHECK(testFunctorA.GetCount() == 1);
357     BOOST_CHECK(testFunctorB.GetCount() == 1);
358     BOOST_CHECK(testFunctorC.GetCount() == 2);
359
360     // Check that non-existent key returns nullptr for its functor
361     BOOST_CHECK_THROW(registry.GetFunctor(0, 0, 0), armnn::Exception);
362 }
363
364 BOOST_AUTO_TEST_CASE(CheckPacketVersionResolver)
365 {
366     // Set up random number generator for generating packetId values
367     std::random_device device;
368     std::mt19937 generator(device());
369     std::uniform_int_distribution<uint32_t> distribution(std::numeric_limits<uint32_t>::min(),
370                                                          std::numeric_limits<uint32_t>::max());
371
372     // NOTE: Expected version is always 1.0.0, regardless of packetId
373     const Version expectedVersion(1, 0, 0);
374
375     PacketVersionResolver packetVersionResolver;
376
377     constexpr unsigned int numTests = 10u;
378
379     for (unsigned int i = 0u; i < numTests; ++i)
380     {
381         const uint32_t familyId = distribution(generator);
382         const uint32_t packetId = distribution(generator);
383         Version resolvedVersion = packetVersionResolver.ResolvePacketVersion(familyId, packetId);
384
385         BOOST_TEST(resolvedVersion == expectedVersion);
386     }
387 }
388
389 void ProfilingCurrentStateThreadImpl(ProfilingStateMachine& states)
390 {
391     ProfilingState newState = ProfilingState::NotConnected;
392     states.GetCurrentState();
393     states.TransitionToState(newState);
394 }
395
396 BOOST_AUTO_TEST_CASE(CheckProfilingStateMachine)
397 {
398     ProfilingStateMachine profilingState1(ProfilingState::Uninitialised);
399     profilingState1.TransitionToState(ProfilingState::Uninitialised);
400     BOOST_CHECK(profilingState1.GetCurrentState() == ProfilingState::Uninitialised);
401
402     ProfilingStateMachine profilingState2(ProfilingState::Uninitialised);
403     profilingState2.TransitionToState(ProfilingState::NotConnected);
404     BOOST_CHECK(profilingState2.GetCurrentState() == ProfilingState::NotConnected);
405
406     ProfilingStateMachine profilingState3(ProfilingState::NotConnected);
407     profilingState3.TransitionToState(ProfilingState::NotConnected);
408     BOOST_CHECK(profilingState3.GetCurrentState() == ProfilingState::NotConnected);
409
410     ProfilingStateMachine profilingState4(ProfilingState::NotConnected);
411     profilingState4.TransitionToState(ProfilingState::WaitingForAck);
412     BOOST_CHECK(profilingState4.GetCurrentState() == ProfilingState::WaitingForAck);
413
414     ProfilingStateMachine profilingState5(ProfilingState::WaitingForAck);
415     profilingState5.TransitionToState(ProfilingState::WaitingForAck);
416     BOOST_CHECK(profilingState5.GetCurrentState() == ProfilingState::WaitingForAck);
417
418     ProfilingStateMachine profilingState6(ProfilingState::WaitingForAck);
419     profilingState6.TransitionToState(ProfilingState::Active);
420     BOOST_CHECK(profilingState6.GetCurrentState() == ProfilingState::Active);
421
422     ProfilingStateMachine profilingState7(ProfilingState::Active);
423     profilingState7.TransitionToState(ProfilingState::NotConnected);
424     BOOST_CHECK(profilingState7.GetCurrentState() == ProfilingState::NotConnected);
425
426     ProfilingStateMachine profilingState8(ProfilingState::Active);
427     profilingState8.TransitionToState(ProfilingState::Active);
428     BOOST_CHECK(profilingState8.GetCurrentState() == ProfilingState::Active);
429
430     ProfilingStateMachine profilingState9(ProfilingState::Uninitialised);
431     BOOST_CHECK_THROW(profilingState9.TransitionToState(ProfilingState::WaitingForAck), armnn::Exception);
432
433     ProfilingStateMachine profilingState10(ProfilingState::Uninitialised);
434     BOOST_CHECK_THROW(profilingState10.TransitionToState(ProfilingState::Active), armnn::Exception);
435
436     ProfilingStateMachine profilingState11(ProfilingState::NotConnected);
437     BOOST_CHECK_THROW(profilingState11.TransitionToState(ProfilingState::Uninitialised), armnn::Exception);
438
439     ProfilingStateMachine profilingState12(ProfilingState::NotConnected);
440     BOOST_CHECK_THROW(profilingState12.TransitionToState(ProfilingState::Active), armnn::Exception);
441
442     ProfilingStateMachine profilingState13(ProfilingState::WaitingForAck);
443     BOOST_CHECK_THROW(profilingState13.TransitionToState(ProfilingState::Uninitialised), armnn::Exception);
444
445     ProfilingStateMachine profilingState14(ProfilingState::WaitingForAck);
446     profilingState14.TransitionToState(ProfilingState::NotConnected);
447     BOOST_CHECK(profilingState14.GetCurrentState() == ProfilingState::NotConnected);
448
449     ProfilingStateMachine profilingState15(ProfilingState::Active);
450     BOOST_CHECK_THROW(profilingState15.TransitionToState(ProfilingState::Uninitialised), armnn::Exception);
451
452     ProfilingStateMachine profilingState16(armnn::profiling::ProfilingState::Active);
453     BOOST_CHECK_THROW(profilingState16.TransitionToState(ProfilingState::WaitingForAck), armnn::Exception);
454
455     ProfilingStateMachine profilingState17(ProfilingState::Uninitialised);
456
457     std::thread thread1(ProfilingCurrentStateThreadImpl, std::ref(profilingState17));
458     std::thread thread2(ProfilingCurrentStateThreadImpl, std::ref(profilingState17));
459     std::thread thread3(ProfilingCurrentStateThreadImpl, std::ref(profilingState17));
460     std::thread thread4(ProfilingCurrentStateThreadImpl, std::ref(profilingState17));
461     std::thread thread5(ProfilingCurrentStateThreadImpl, std::ref(profilingState17));
462
463     thread1.join();
464     thread2.join();
465     thread3.join();
466     thread4.join();
467     thread5.join();
468
469     BOOST_TEST((profilingState17.GetCurrentState() == ProfilingState::NotConnected));
470 }
471
472 void CaptureDataWriteThreadImpl(Holder& holder, uint32_t capturePeriod, const std::vector<uint16_t>& counterIds)
473 {
474     holder.SetCaptureData(capturePeriod, counterIds);
475 }
476
477 void CaptureDataReadThreadImpl(const Holder& holder, CaptureData& captureData)
478 {
479     captureData = holder.GetCaptureData();
480 }
481
482 BOOST_AUTO_TEST_CASE(CheckCaptureDataHolder)
483 {
484     std::map<uint32_t, std::vector<uint16_t>> periodIdMap;
485     std::vector<uint16_t> counterIds;
486     uint32_t numThreads = 10;
487     for (uint32_t i = 0; i < numThreads; ++i)
488     {
489         counterIds.emplace_back(i);
490         periodIdMap.insert(std::make_pair(i, counterIds));
491     }
492
493     // Verify the read and write threads set the holder correctly
494     // and retrieve the expected values
495     Holder holder;
496     BOOST_CHECK((holder.GetCaptureData()).GetCapturePeriod() == 0);
497     BOOST_CHECK(((holder.GetCaptureData()).GetCounterIds()).empty());
498
499     // Check Holder functions
500     std::thread thread1(CaptureDataWriteThreadImpl, std::ref(holder), 2, std::ref(periodIdMap[2]));
501     thread1.join();
502     BOOST_CHECK((holder.GetCaptureData()).GetCapturePeriod() == 2);
503     BOOST_CHECK((holder.GetCaptureData()).GetCounterIds() == periodIdMap[2]);
504     // NOTE: now that we have some initial values in the holder we don't have to worry
505     //       in the multi-threaded section below about a read thread accessing the holder
506     //       before any write thread has gotten to it so we read period = 0, counterIds empty
507     //       instead of period = 0, counterIds = {0} as will the case when write thread 0
508     //       has executed.
509
510     CaptureData captureData;
511     std::thread thread2(CaptureDataReadThreadImpl, std::ref(holder), std::ref(captureData));
512     thread2.join();
513     BOOST_CHECK(captureData.GetCapturePeriod() == 2);
514     BOOST_CHECK(captureData.GetCounterIds() == periodIdMap[2]);
515
516     std::map<uint32_t, CaptureData> captureDataIdMap;
517     for (uint32_t i = 0; i < numThreads; ++i)
518     {
519         CaptureData perThreadCaptureData;
520         captureDataIdMap.insert(std::make_pair(i, perThreadCaptureData));
521     }
522
523     std::vector<std::thread> threadsVect;
524     std::vector<std::thread> readThreadsVect;
525     for (uint32_t i = 0; i < numThreads; ++i)
526     {
527         threadsVect.emplace_back(
528             std::thread(CaptureDataWriteThreadImpl, std::ref(holder), i, std::ref(periodIdMap[i])));
529
530         // Verify that the CaptureData goes into the thread in a virgin state
531         BOOST_CHECK(captureDataIdMap.at(i).GetCapturePeriod() == 0);
532         BOOST_CHECK(captureDataIdMap.at(i).GetCounterIds().empty());
533         readThreadsVect.emplace_back(
534             std::thread(CaptureDataReadThreadImpl, std::ref(holder), std::ref(captureDataIdMap.at(i))));
535     }
536
537     for (uint32_t i = 0; i < numThreads; ++i)
538     {
539         threadsVect[i].join();
540         readThreadsVect[i].join();
541     }
542
543     // Look at the CaptureData that each read thread has filled
544     // the capture period it read should match the counter ids entry
545     for (uint32_t i = 0; i < numThreads; ++i)
546     {
547         CaptureData perThreadCaptureData = captureDataIdMap.at(i);
548         BOOST_CHECK(perThreadCaptureData.GetCounterIds() == periodIdMap.at(perThreadCaptureData.GetCapturePeriod()));
549     }
550 }
551
552 BOOST_AUTO_TEST_CASE(CaptureDataMethods)
553 {
554     // Check CaptureData setter and getter functions
555     std::vector<uint16_t> counterIds = { 42, 29, 13 };
556     CaptureData captureData;
557     BOOST_CHECK(captureData.GetCapturePeriod() == 0);
558     BOOST_CHECK((captureData.GetCounterIds()).empty());
559     captureData.SetCapturePeriod(150);
560     captureData.SetCounterIds(counterIds);
561     BOOST_CHECK(captureData.GetCapturePeriod() == 150);
562     BOOST_CHECK(captureData.GetCounterIds() == counterIds);
563
564     // Check assignment operator
565     CaptureData secondCaptureData;
566
567     secondCaptureData = captureData;
568     BOOST_CHECK(secondCaptureData.GetCapturePeriod() == 150);
569     BOOST_CHECK(secondCaptureData.GetCounterIds() == counterIds);
570
571     // Check copy constructor
572     CaptureData copyConstructedCaptureData(captureData);
573
574     BOOST_CHECK(copyConstructedCaptureData.GetCapturePeriod() == 150);
575     BOOST_CHECK(copyConstructedCaptureData.GetCounterIds() == counterIds);
576 }
577
578 BOOST_AUTO_TEST_CASE(CheckProfilingServiceDisabled)
579 {
580     armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
581     ProfilingService& profilingService = ProfilingService::Instance();
582     profilingService.ResetExternalProfilingOptions(options, true);
583     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
584     profilingService.Update();
585     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
586 }
587
588 BOOST_AUTO_TEST_CASE(CheckProfilingServiceEnabled)
589 {
590     // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
591     LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
592
593     armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
594     options.m_EnableProfiling          = true;
595     ProfilingService& profilingService = ProfilingService::Instance();
596     profilingService.ResetExternalProfilingOptions(options, true);
597     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
598     profilingService.Update();
599     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
600
601     // Redirect the output to a local stream so that we can parse the warning message
602     std::stringstream ss;
603     StreamRedirector streamRedirector(std::cout, ss.rdbuf());
604     profilingService.Update();
605     BOOST_CHECK(boost::contains(ss.str(), "Cannot connect to stream socket: Connection refused"));
606 }
607
608 BOOST_AUTO_TEST_CASE(CheckProfilingServiceEnabledRuntime)
609 {
610     // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
611     LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
612
613     armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
614     ProfilingService& profilingService = ProfilingService::Instance();
615     profilingService.ResetExternalProfilingOptions(options, true);
616     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
617     profilingService.Update();
618     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
619     options.m_EnableProfiling = true;
620     profilingService.ResetExternalProfilingOptions(options);
621     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
622     profilingService.Update();
623     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
624
625     // Redirect the output to a local stream so that we can parse the warning message
626     std::stringstream ss;
627     StreamRedirector streamRedirector(std::cout, ss.rdbuf());
628     profilingService.Update();
629     BOOST_CHECK(boost::contains(ss.str(), "Cannot connect to stream socket: Connection refused"));
630 }
631
632 BOOST_AUTO_TEST_CASE(CheckProfilingServiceCounterDirectory)
633 {
634     armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
635     ProfilingService& profilingService = ProfilingService::Instance();
636     profilingService.ResetExternalProfilingOptions(options, true);
637
638     const ICounterDirectory& counterDirectory0 = profilingService.GetCounterDirectory();
639     BOOST_CHECK(counterDirectory0.GetCounterCount() == 0);
640     profilingService.Update();
641     BOOST_CHECK(counterDirectory0.GetCounterCount() == 0);
642
643     options.m_EnableProfiling = true;
644     profilingService.ResetExternalProfilingOptions(options);
645
646     const ICounterDirectory& counterDirectory1 = profilingService.GetCounterDirectory();
647     BOOST_CHECK(counterDirectory1.GetCounterCount() == 0);
648     profilingService.Update();
649     BOOST_CHECK(counterDirectory1.GetCounterCount() != 0);
650 }
651
652 BOOST_AUTO_TEST_CASE(CheckProfilingServiceCounterValues)
653 {
654     armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
655     options.m_EnableProfiling          = true;
656     ProfilingService& profilingService = ProfilingService::Instance();
657     profilingService.ResetExternalProfilingOptions(options, true);
658
659     profilingService.Update();
660     const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
661     const Counters& counters                  = counterDirectory.GetCounters();
662     BOOST_CHECK(!counters.empty());
663
664     // Get the UID of the first counter for testing
665     uint16_t counterUid = counters.begin()->first;
666
667     ProfilingService* profilingServicePtr = &profilingService;
668     std::vector<std::thread> writers;
669
670     for (int i = 0; i < 100; ++i)
671     {
672         // Increment and decrement the first counter
673         writers.push_back(std::thread(&ProfilingService::IncrementCounterValue, profilingServicePtr, counterUid));
674         writers.push_back(std::thread(&ProfilingService::DecrementCounterValue, profilingServicePtr, counterUid));
675         // Add 10 and subtract 5 from the first counter
676         writers.push_back(std::thread(&ProfilingService::AddCounterValue, profilingServicePtr, counterUid, 10));
677         writers.push_back(std::thread(&ProfilingService::SubtractCounterValue, profilingServicePtr, counterUid, 5));
678     }
679
680     std::for_each(writers.begin(), writers.end(), mem_fn(&std::thread::join));
681
682     uint32_t counterValue = 0;
683     BOOST_CHECK_NO_THROW(counterValue = profilingService.GetCounterValue(counterUid));
684     BOOST_CHECK(counterValue == 500);
685
686     BOOST_CHECK_NO_THROW(profilingService.SetCounterValue(counterUid, 0));
687     BOOST_CHECK_NO_THROW(counterValue = profilingService.GetCounterValue(counterUid));
688     BOOST_CHECK(counterValue == 0);
689 }
690
691 BOOST_AUTO_TEST_CASE(CheckProfilingObjectUids)
692 {
693     uint16_t uid = 0;
694     BOOST_CHECK_NO_THROW(uid = GetNextUid());
695     BOOST_CHECK(uid >= 1);
696
697     uint16_t nextUid = 0;
698     BOOST_CHECK_NO_THROW(nextUid = GetNextUid());
699     BOOST_CHECK(nextUid > uid);
700
701     std::vector<uint16_t> counterUids;
702     BOOST_CHECK_NO_THROW(counterUids = GetNextCounterUids(0));
703     BOOST_CHECK(counterUids.size() == 1);
704     BOOST_CHECK(counterUids[0] >= 0);
705
706     std::vector<uint16_t> nextCounterUids;
707     BOOST_CHECK_NO_THROW(nextCounterUids = GetNextCounterUids(1));
708     BOOST_CHECK(nextCounterUids.size() == 1);
709     BOOST_CHECK(nextCounterUids[0] > counterUids[0]);
710
711     std::vector<uint16_t> counterUidsMultiCore;
712     uint16_t numberOfCores = 13;
713     BOOST_CHECK_NO_THROW(counterUidsMultiCore = GetNextCounterUids(numberOfCores));
714     BOOST_CHECK(counterUidsMultiCore.size() == numberOfCores);
715     BOOST_CHECK(counterUidsMultiCore.front() >= nextCounterUids[0]);
716     for (size_t i = 1; i < numberOfCores; i++)
717     {
718         BOOST_CHECK(counterUidsMultiCore[i] == counterUidsMultiCore[i - 1] + 1);
719     }
720     BOOST_CHECK(counterUidsMultiCore.back() == counterUidsMultiCore.front() + numberOfCores - 1);
721 }
722
723 BOOST_AUTO_TEST_CASE(CheckCounterDirectoryRegisterCategory)
724 {
725     CounterDirectory counterDirectory;
726     BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
727     BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
728     BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
729     BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
730
731     // Register a category with an invalid name
732     const Category* noCategory = nullptr;
733     BOOST_CHECK_THROW(noCategory = counterDirectory.RegisterCategory(""), armnn::InvalidArgumentException);
734     BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
735     BOOST_CHECK(!noCategory);
736
737     // Register a category with an invalid name
738     BOOST_CHECK_THROW(noCategory = counterDirectory.RegisterCategory("invalid category"),
739                       armnn::InvalidArgumentException);
740     BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
741     BOOST_CHECK(!noCategory);
742
743     // Register a new category
744     const std::string categoryName = "some_category";
745     const Category* category       = nullptr;
746     BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
747     BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
748     BOOST_CHECK(category);
749     BOOST_CHECK(category->m_Name == categoryName);
750     BOOST_CHECK(category->m_Counters.empty());
751     BOOST_CHECK(category->m_DeviceUid == 0);
752     BOOST_CHECK(category->m_CounterSetUid == 0);
753
754     // Get the registered category
755     const Category* registeredCategory = counterDirectory.GetCategory(categoryName);
756     BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
757     BOOST_CHECK(registeredCategory);
758     BOOST_CHECK(registeredCategory == category);
759
760     // Try to get a category not registered
761     const Category* notRegisteredCategory = counterDirectory.GetCategory("not_registered_category");
762     BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
763     BOOST_CHECK(!notRegisteredCategory);
764
765     // Register a category already registered
766     const Category* anotherCategory = nullptr;
767     BOOST_CHECK_THROW(anotherCategory = counterDirectory.RegisterCategory(categoryName),
768                       armnn::InvalidArgumentException);
769     BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
770     BOOST_CHECK(!anotherCategory);
771
772     // Register a device for testing
773     const std::string deviceName = "some_device";
774     const Device* device         = nullptr;
775     BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName));
776     BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
777     BOOST_CHECK(device);
778     BOOST_CHECK(device->m_Uid >= 1);
779     BOOST_CHECK(device->m_Name == deviceName);
780     BOOST_CHECK(device->m_Cores == 0);
781
782     // Register a new category not associated to any device
783     const std::string categoryWoDeviceName = "some_category_without_device";
784     const Category* categoryWoDevice       = nullptr;
785     BOOST_CHECK_NO_THROW(categoryWoDevice = counterDirectory.RegisterCategory(categoryWoDeviceName, 0));
786     BOOST_CHECK(counterDirectory.GetCategoryCount() == 2);
787     BOOST_CHECK(categoryWoDevice);
788     BOOST_CHECK(categoryWoDevice->m_Name == categoryWoDeviceName);
789     BOOST_CHECK(categoryWoDevice->m_Counters.empty());
790     BOOST_CHECK(categoryWoDevice->m_DeviceUid == 0);
791     BOOST_CHECK(categoryWoDevice->m_CounterSetUid == 0);
792
793     // Register a new category associated to an invalid device
794     const std::string categoryWInvalidDeviceName = "some_category_with_invalid_device";
795
796     ARMNN_NO_CONVERSION_WARN_BEGIN
797     uint16_t invalidDeviceUid = device->m_Uid + 10;
798     ARMNN_NO_CONVERSION_WARN_END
799
800     const Category* categoryWInvalidDevice = nullptr;
801     BOOST_CHECK_THROW(categoryWInvalidDevice =
802                           counterDirectory.RegisterCategory(categoryWInvalidDeviceName, invalidDeviceUid),
803                       armnn::InvalidArgumentException);
804     BOOST_CHECK(counterDirectory.GetCategoryCount() == 2);
805     BOOST_CHECK(!categoryWInvalidDevice);
806
807     // Register a new category associated to a valid device
808     const std::string categoryWValidDeviceName = "some_category_with_valid_device";
809     const Category* categoryWValidDevice       = nullptr;
810     BOOST_CHECK_NO_THROW(categoryWValidDevice =
811                              counterDirectory.RegisterCategory(categoryWValidDeviceName, device->m_Uid));
812     BOOST_CHECK(counterDirectory.GetCategoryCount() == 3);
813     BOOST_CHECK(categoryWValidDevice);
814     BOOST_CHECK(categoryWValidDevice != category);
815     BOOST_CHECK(categoryWValidDevice->m_Name == categoryWValidDeviceName);
816     BOOST_CHECK(categoryWValidDevice->m_DeviceUid == device->m_Uid);
817     BOOST_CHECK(categoryWValidDevice->m_CounterSetUid == 0);
818
819     // Register a counter set for testing
820     const std::string counterSetName = "some_counter_set";
821     const CounterSet* counterSet     = nullptr;
822     BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
823     BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
824     BOOST_CHECK(counterSet);
825     BOOST_CHECK(counterSet->m_Uid >= 1);
826     BOOST_CHECK(counterSet->m_Name == counterSetName);
827     BOOST_CHECK(counterSet->m_Count == 0);
828
829     // Register a new category not associated to any counter set
830     const std::string categoryWoCounterSetName = "some_category_without_counter_set";
831     const Category* categoryWoCounterSet       = nullptr;
832     BOOST_CHECK_NO_THROW(categoryWoCounterSet =
833                              counterDirectory.RegisterCategory(categoryWoCounterSetName, armnn::EmptyOptional(), 0));
834     BOOST_CHECK(counterDirectory.GetCategoryCount() == 4);
835     BOOST_CHECK(categoryWoCounterSet);
836     BOOST_CHECK(categoryWoCounterSet->m_Name == categoryWoCounterSetName);
837     BOOST_CHECK(categoryWoCounterSet->m_DeviceUid == 0);
838     BOOST_CHECK(categoryWoCounterSet->m_CounterSetUid == 0);
839
840     // Register a new category associated to an invalid counter set
841     const std::string categoryWInvalidCounterSetName = "some_category_with_invalid_counter_set";
842
843     ARMNN_NO_CONVERSION_WARN_BEGIN
844     uint16_t invalidCunterSetUid = counterSet->m_Uid + 10;
845     ARMNN_NO_CONVERSION_WARN_END
846
847     const Category* categoryWInvalidCounterSet = nullptr;
848     BOOST_CHECK_THROW(categoryWInvalidCounterSet = counterDirectory.RegisterCategory(
849                           categoryWInvalidCounterSetName, armnn::EmptyOptional(), invalidCunterSetUid),
850                       armnn::InvalidArgumentException);
851     BOOST_CHECK(counterDirectory.GetCategoryCount() == 4);
852     BOOST_CHECK(!categoryWInvalidCounterSet);
853
854     // Register a new category associated to a valid counter set
855     const std::string categoryWValidCounterSetName = "some_category_with_valid_counter_set";
856     const Category* categoryWValidCounterSet       = nullptr;
857     BOOST_CHECK_NO_THROW(categoryWValidCounterSet = counterDirectory.RegisterCategory(
858                              categoryWValidCounterSetName, armnn::EmptyOptional(), counterSet->m_Uid));
859     BOOST_CHECK(counterDirectory.GetCategoryCount() == 5);
860     BOOST_CHECK(categoryWValidCounterSet);
861     BOOST_CHECK(categoryWValidCounterSet != category);
862     BOOST_CHECK(categoryWValidCounterSet->m_Name == categoryWValidCounterSetName);
863     BOOST_CHECK(categoryWValidCounterSet->m_DeviceUid == 0);
864     BOOST_CHECK(categoryWValidCounterSet->m_CounterSetUid == counterSet->m_Uid);
865
866     // Register a new category associated to a valid device and counter set
867     const std::string categoryWValidDeviceAndValidCounterSetName = "some_category_with_valid_device_and_counter_set";
868     const Category* categoryWValidDeviceAndValidCounterSet       = nullptr;
869     BOOST_CHECK_NO_THROW(categoryWValidDeviceAndValidCounterSet = counterDirectory.RegisterCategory(
870                              categoryWValidDeviceAndValidCounterSetName, device->m_Uid, counterSet->m_Uid));
871     BOOST_CHECK(counterDirectory.GetCategoryCount() == 6);
872     BOOST_CHECK(categoryWValidDeviceAndValidCounterSet);
873     BOOST_CHECK(categoryWValidDeviceAndValidCounterSet != category);
874     BOOST_CHECK(categoryWValidDeviceAndValidCounterSet->m_Name == categoryWValidDeviceAndValidCounterSetName);
875     BOOST_CHECK(categoryWValidDeviceAndValidCounterSet->m_DeviceUid == device->m_Uid);
876     BOOST_CHECK(categoryWValidDeviceAndValidCounterSet->m_CounterSetUid == counterSet->m_Uid);
877 }
878
879 BOOST_AUTO_TEST_CASE(CheckCounterDirectoryRegisterDevice)
880 {
881     CounterDirectory counterDirectory;
882     BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
883     BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
884     BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
885     BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
886
887     // Register a device with an invalid name
888     const Device* noDevice = nullptr;
889     BOOST_CHECK_THROW(noDevice = counterDirectory.RegisterDevice(""), armnn::InvalidArgumentException);
890     BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
891     BOOST_CHECK(!noDevice);
892
893     // Register a device with an invalid name
894     BOOST_CHECK_THROW(noDevice = counterDirectory.RegisterDevice("inv@lid nam€"), armnn::InvalidArgumentException);
895     BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
896     BOOST_CHECK(!noDevice);
897
898     // Register a new device with no cores or parent category
899     const std::string deviceName = "some_device";
900     const Device* device         = nullptr;
901     BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName));
902     BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
903     BOOST_CHECK(device);
904     BOOST_CHECK(device->m_Name == deviceName);
905     BOOST_CHECK(device->m_Uid >= 1);
906     BOOST_CHECK(device->m_Cores == 0);
907
908     // Try getting an unregistered device
909     const Device* unregisteredDevice = counterDirectory.GetDevice(9999);
910     BOOST_CHECK(!unregisteredDevice);
911
912     // Get the registered device
913     const Device* registeredDevice = counterDirectory.GetDevice(device->m_Uid);
914     BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
915     BOOST_CHECK(registeredDevice);
916     BOOST_CHECK(registeredDevice == device);
917
918     // Register a device with the name of a device already registered
919     const Device* deviceSameName = nullptr;
920     BOOST_CHECK_THROW(deviceSameName = counterDirectory.RegisterDevice(deviceName), armnn::InvalidArgumentException);
921     BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
922     BOOST_CHECK(!deviceSameName);
923
924     // Register a new device with cores and no parent category
925     const std::string deviceWCoresName = "some_device_with_cores";
926     const Device* deviceWCores         = nullptr;
927     BOOST_CHECK_NO_THROW(deviceWCores = counterDirectory.RegisterDevice(deviceWCoresName, 2));
928     BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
929     BOOST_CHECK(deviceWCores);
930     BOOST_CHECK(deviceWCores->m_Name == deviceWCoresName);
931     BOOST_CHECK(deviceWCores->m_Uid >= 1);
932     BOOST_CHECK(deviceWCores->m_Uid > device->m_Uid);
933     BOOST_CHECK(deviceWCores->m_Cores == 2);
934
935     // Get the registered device
936     const Device* registeredDeviceWCores = counterDirectory.GetDevice(deviceWCores->m_Uid);
937     BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
938     BOOST_CHECK(registeredDeviceWCores);
939     BOOST_CHECK(registeredDeviceWCores == deviceWCores);
940     BOOST_CHECK(registeredDeviceWCores != device);
941
942     // Register a new device with cores and invalid parent category
943     const std::string deviceWCoresWInvalidParentCategoryName = "some_device_with_cores_with_invalid_parent_category";
944     const Device* deviceWCoresWInvalidParentCategory         = nullptr;
945     BOOST_CHECK_THROW(deviceWCoresWInvalidParentCategory =
946                           counterDirectory.RegisterDevice(deviceWCoresWInvalidParentCategoryName, 3, std::string("")),
947                       armnn::InvalidArgumentException);
948     BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
949     BOOST_CHECK(!deviceWCoresWInvalidParentCategory);
950
951     // Register a new device with cores and invalid parent category
952     const std::string deviceWCoresWInvalidParentCategoryName2 = "some_device_with_cores_with_invalid_parent_category2";
953     const Device* deviceWCoresWInvalidParentCategory2         = nullptr;
954     BOOST_CHECK_THROW(deviceWCoresWInvalidParentCategory2 = counterDirectory.RegisterDevice(
955                           deviceWCoresWInvalidParentCategoryName2, 3, std::string("invalid_parent_category")),
956                       armnn::InvalidArgumentException);
957     BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
958     BOOST_CHECK(!deviceWCoresWInvalidParentCategory2);
959
960     // Register a category for testing
961     const std::string categoryName = "some_category";
962     const Category* category       = nullptr;
963     BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
964     BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
965     BOOST_CHECK(category);
966     BOOST_CHECK(category->m_Name == categoryName);
967     BOOST_CHECK(category->m_Counters.empty());
968     BOOST_CHECK(category->m_DeviceUid == 0);
969     BOOST_CHECK(category->m_CounterSetUid == 0);
970
971     // Register a new device with cores and valid parent category
972     const std::string deviceWCoresWValidParentCategoryName = "some_device_with_cores_with_valid_parent_category";
973     const Device* deviceWCoresWValidParentCategory         = nullptr;
974     BOOST_CHECK_NO_THROW(deviceWCoresWValidParentCategory =
975                              counterDirectory.RegisterDevice(deviceWCoresWValidParentCategoryName, 4, categoryName));
976     BOOST_CHECK(counterDirectory.GetDeviceCount() == 3);
977     BOOST_CHECK(deviceWCoresWValidParentCategory);
978     BOOST_CHECK(deviceWCoresWValidParentCategory->m_Name == deviceWCoresWValidParentCategoryName);
979     BOOST_CHECK(deviceWCoresWValidParentCategory->m_Uid >= 1);
980     BOOST_CHECK(deviceWCoresWValidParentCategory->m_Uid > device->m_Uid);
981     BOOST_CHECK(deviceWCoresWValidParentCategory->m_Uid > deviceWCores->m_Uid);
982     BOOST_CHECK(deviceWCoresWValidParentCategory->m_Cores == 4);
983     BOOST_CHECK(category->m_DeviceUid == deviceWCoresWValidParentCategory->m_Uid);
984
985     // Register a device associated to a category already associated to a different device
986     const std::string deviceSameCategoryName = "some_device_with_invalid_parent_category";
987     const Device* deviceSameCategory         = nullptr;
988     BOOST_CHECK_THROW(deviceSameCategory = counterDirectory.RegisterDevice(deviceSameCategoryName, 0, categoryName),
989                       armnn::InvalidArgumentException);
990     BOOST_CHECK(counterDirectory.GetDeviceCount() == 3);
991     BOOST_CHECK(!deviceSameCategory);
992 }
993
994 BOOST_AUTO_TEST_CASE(CheckCounterDirectoryRegisterCounterSet)
995 {
996     CounterDirectory counterDirectory;
997     BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
998     BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
999     BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
1000     BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1001
1002     // Register a counter set with an invalid name
1003     const CounterSet* noCounterSet = nullptr;
1004     BOOST_CHECK_THROW(noCounterSet = counterDirectory.RegisterCounterSet(""), armnn::InvalidArgumentException);
1005     BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
1006     BOOST_CHECK(!noCounterSet);
1007
1008     // Register a counter set with an invalid name
1009     BOOST_CHECK_THROW(noCounterSet = counterDirectory.RegisterCounterSet("invalid name"),
1010                       armnn::InvalidArgumentException);
1011     BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
1012     BOOST_CHECK(!noCounterSet);
1013
1014     // Register a new counter set with no count or parent category
1015     const std::string counterSetName = "some_counter_set";
1016     const CounterSet* counterSet     = nullptr;
1017     BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
1018     BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1019     BOOST_CHECK(counterSet);
1020     BOOST_CHECK(counterSet->m_Name == counterSetName);
1021     BOOST_CHECK(counterSet->m_Uid >= 1);
1022     BOOST_CHECK(counterSet->m_Count == 0);
1023
1024     // Try getting an unregistered counter set
1025     const CounterSet* unregisteredCounterSet = counterDirectory.GetCounterSet(9999);
1026     BOOST_CHECK(!unregisteredCounterSet);
1027
1028     // Get the registered counter set
1029     const CounterSet* registeredCounterSet = counterDirectory.GetCounterSet(counterSet->m_Uid);
1030     BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1031     BOOST_CHECK(registeredCounterSet);
1032     BOOST_CHECK(registeredCounterSet == counterSet);
1033
1034     // Register a counter set with the name of a counter set already registered
1035     const CounterSet* counterSetSameName = nullptr;
1036     BOOST_CHECK_THROW(counterSetSameName = counterDirectory.RegisterCounterSet(counterSetName),
1037                       armnn::InvalidArgumentException);
1038     BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1039     BOOST_CHECK(!counterSetSameName);
1040
1041     // Register a new counter set with count and no parent category
1042     const std::string counterSetWCountName = "some_counter_set_with_count";
1043     const CounterSet* counterSetWCount     = nullptr;
1044     BOOST_CHECK_NO_THROW(counterSetWCount = counterDirectory.RegisterCounterSet(counterSetWCountName, 37));
1045     BOOST_CHECK(counterDirectory.GetCounterSetCount() == 2);
1046     BOOST_CHECK(counterSetWCount);
1047     BOOST_CHECK(counterSetWCount->m_Name == counterSetWCountName);
1048     BOOST_CHECK(counterSetWCount->m_Uid >= 1);
1049     BOOST_CHECK(counterSetWCount->m_Uid > counterSet->m_Uid);
1050     BOOST_CHECK(counterSetWCount->m_Count == 37);
1051
1052     // Get the registered counter set
1053     const CounterSet* registeredCounterSetWCount = counterDirectory.GetCounterSet(counterSetWCount->m_Uid);
1054     BOOST_CHECK(counterDirectory.GetCounterSetCount() == 2);
1055     BOOST_CHECK(registeredCounterSetWCount);
1056     BOOST_CHECK(registeredCounterSetWCount == counterSetWCount);
1057     BOOST_CHECK(registeredCounterSetWCount != counterSet);
1058
1059     // Register a new counter set with count and invalid parent category
1060     const std::string counterSetWCountWInvalidParentCategoryName = "some_counter_set_with_count_"
1061                                                                    "with_invalid_parent_category";
1062     const CounterSet* counterSetWCountWInvalidParentCategory = nullptr;
1063     BOOST_CHECK_THROW(counterSetWCountWInvalidParentCategory = counterDirectory.RegisterCounterSet(
1064                           counterSetWCountWInvalidParentCategoryName, 42, std::string("")),
1065                       armnn::InvalidArgumentException);
1066     BOOST_CHECK(counterDirectory.GetCounterSetCount() == 2);
1067     BOOST_CHECK(!counterSetWCountWInvalidParentCategory);
1068
1069     // Register a new counter set with count and invalid parent category
1070     const std::string counterSetWCountWInvalidParentCategoryName2 = "some_counter_set_with_count_"
1071                                                                     "with_invalid_parent_category2";
1072     const CounterSet* counterSetWCountWInvalidParentCategory2 = nullptr;
1073     BOOST_CHECK_THROW(counterSetWCountWInvalidParentCategory2 = counterDirectory.RegisterCounterSet(
1074                           counterSetWCountWInvalidParentCategoryName2, 42, std::string("invalid_parent_category")),
1075                       armnn::InvalidArgumentException);
1076     BOOST_CHECK(counterDirectory.GetCounterSetCount() == 2);
1077     BOOST_CHECK(!counterSetWCountWInvalidParentCategory2);
1078
1079     // Register a category for testing
1080     const std::string categoryName = "some_category";
1081     const Category* category       = nullptr;
1082     BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
1083     BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
1084     BOOST_CHECK(category);
1085     BOOST_CHECK(category->m_Name == categoryName);
1086     BOOST_CHECK(category->m_Counters.empty());
1087     BOOST_CHECK(category->m_DeviceUid == 0);
1088     BOOST_CHECK(category->m_CounterSetUid == 0);
1089
1090     // Register a new counter set with count and valid parent category
1091     const std::string counterSetWCountWValidParentCategoryName = "some_counter_set_with_count_"
1092                                                                  "with_valid_parent_category";
1093     const CounterSet* counterSetWCountWValidParentCategory = nullptr;
1094     BOOST_CHECK_NO_THROW(counterSetWCountWValidParentCategory = counterDirectory.RegisterCounterSet(
1095                              counterSetWCountWValidParentCategoryName, 42, categoryName));
1096     BOOST_CHECK(counterDirectory.GetCounterSetCount() == 3);
1097     BOOST_CHECK(counterSetWCountWValidParentCategory);
1098     BOOST_CHECK(counterSetWCountWValidParentCategory->m_Name == counterSetWCountWValidParentCategoryName);
1099     BOOST_CHECK(counterSetWCountWValidParentCategory->m_Uid >= 1);
1100     BOOST_CHECK(counterSetWCountWValidParentCategory->m_Uid > counterSet->m_Uid);
1101     BOOST_CHECK(counterSetWCountWValidParentCategory->m_Uid > counterSetWCount->m_Uid);
1102     BOOST_CHECK(counterSetWCountWValidParentCategory->m_Count == 42);
1103     BOOST_CHECK(category->m_CounterSetUid == counterSetWCountWValidParentCategory->m_Uid);
1104
1105     // Register a counter set associated to a category already associated to a different counter set
1106     const std::string counterSetSameCategoryName = "some_counter_set_with_invalid_parent_category";
1107     const CounterSet* counterSetSameCategory     = nullptr;
1108     BOOST_CHECK_THROW(counterSetSameCategory =
1109                           counterDirectory.RegisterCounterSet(counterSetSameCategoryName, 0, categoryName),
1110                       armnn::InvalidArgumentException);
1111     BOOST_CHECK(counterDirectory.GetCounterSetCount() == 3);
1112     BOOST_CHECK(!counterSetSameCategory);
1113 }
1114
1115 BOOST_AUTO_TEST_CASE(CheckCounterDirectoryRegisterCounter)
1116 {
1117     CounterDirectory counterDirectory;
1118     BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
1119     BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
1120     BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
1121     BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1122
1123     // Register a counter with an invalid parent category name
1124     const Counter* noCounter = nullptr;
1125     BOOST_CHECK_THROW(noCounter =
1126                           counterDirectory.RegisterCounter("", 0, 1, 123.45f, "valid name", "valid description"),
1127                       armnn::InvalidArgumentException);
1128     BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1129     BOOST_CHECK(!noCounter);
1130
1131     // Register a counter with an invalid parent category name
1132     BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("invalid parent category", 0, 1, 123.45f,
1133                                                                    "valid name", "valid description"),
1134                       armnn::InvalidArgumentException);
1135     BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1136     BOOST_CHECK(!noCounter);
1137
1138     // Register a counter with an invalid class
1139     BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("valid_parent_category", 2, 1, 123.45f, "valid name",
1140                                                                    "valid description"),
1141                       armnn::InvalidArgumentException);
1142     BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1143     BOOST_CHECK(!noCounter);
1144
1145     // Register a counter with an invalid interpolation
1146     BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("valid_parent_category", 0, 3, 123.45f, "valid name",
1147                                                                    "valid description"),
1148                       armnn::InvalidArgumentException);
1149     BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1150     BOOST_CHECK(!noCounter);
1151
1152     // Register a counter with an invalid multiplier
1153     BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("valid_parent_category", 0, 1, .0f, "valid name",
1154                                                                    "valid description"),
1155                       armnn::InvalidArgumentException);
1156     BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1157     BOOST_CHECK(!noCounter);
1158
1159     // Register a counter with an invalid name
1160     BOOST_CHECK_THROW(
1161         noCounter = counterDirectory.RegisterCounter("valid_parent_category", 0, 1, 123.45f, "", "valid description"),
1162         armnn::InvalidArgumentException);
1163     BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1164     BOOST_CHECK(!noCounter);
1165
1166     // Register a counter with an invalid name
1167     BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("valid_parent_category", 0, 1, 123.45f,
1168                                                                    "invalid nam€", "valid description"),
1169                       armnn::InvalidArgumentException);
1170     BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1171     BOOST_CHECK(!noCounter);
1172
1173     // Register a counter with an invalid description
1174     BOOST_CHECK_THROW(noCounter =
1175                           counterDirectory.RegisterCounter("valid_parent_category", 0, 1, 123.45f, "valid name", ""),
1176                       armnn::InvalidArgumentException);
1177     BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1178     BOOST_CHECK(!noCounter);
1179
1180     // Register a counter with an invalid description
1181     BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("valid_parent_category", 0, 1, 123.45f, "valid name",
1182                                                                    "inv@lid description"),
1183                       armnn::InvalidArgumentException);
1184     BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1185     BOOST_CHECK(!noCounter);
1186
1187     // Register a counter with an invalid unit2
1188     BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("valid_parent_category", 0, 1, 123.45f, "valid name",
1189                                                                    "valid description", std::string("Mb/s2")),
1190                       armnn::InvalidArgumentException);
1191     BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1192     BOOST_CHECK(!noCounter);
1193
1194     // Register a counter with a non-existing parent category name
1195     BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("invalid_parent_category", 0, 1, 123.45f,
1196                                                                    "valid name", "valid description"),
1197                       armnn::InvalidArgumentException);
1198     BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1199     BOOST_CHECK(!noCounter);
1200
1201     // Try getting an unregistered counter
1202     const Counter* unregisteredCounter = counterDirectory.GetCounter(9999);
1203     BOOST_CHECK(!unregisteredCounter);
1204
1205     // Register a category for testing
1206     const std::string categoryName = "some_category";
1207     const Category* category       = nullptr;
1208     BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
1209     BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
1210     BOOST_CHECK(category);
1211     BOOST_CHECK(category->m_Name == categoryName);
1212     BOOST_CHECK(category->m_Counters.empty());
1213     BOOST_CHECK(category->m_DeviceUid == 0);
1214     BOOST_CHECK(category->m_CounterSetUid == 0);
1215
1216     // Register a counter with a valid parent category name
1217     const Counter* counter = nullptr;
1218     BOOST_CHECK_NO_THROW(
1219         counter = counterDirectory.RegisterCounter(categoryName, 0, 1, 123.45f, "valid name", "valid description"));
1220     BOOST_CHECK(counterDirectory.GetCounterCount() == 1);
1221     BOOST_CHECK(counter);
1222     BOOST_CHECK(counter->m_Uid >= 0);
1223     BOOST_CHECK(counter->m_MaxCounterUid == counter->m_Uid);
1224     BOOST_CHECK(counter->m_Class == 0);
1225     BOOST_CHECK(counter->m_Interpolation == 1);
1226     BOOST_CHECK(counter->m_Multiplier == 123.45f);
1227     BOOST_CHECK(counter->m_Name == "valid name");
1228     BOOST_CHECK(counter->m_Description == "valid description");
1229     BOOST_CHECK(counter->m_Units == "");
1230     BOOST_CHECK(counter->m_DeviceUid == 0);
1231     BOOST_CHECK(counter->m_CounterSetUid == 0);
1232     BOOST_CHECK(category->m_Counters.size() == 1);
1233     BOOST_CHECK(category->m_Counters.back() == counter->m_Uid);
1234
1235     // Register a counter with a name of a counter already registered for the given parent category name
1236     const Counter* counterSameName = nullptr;
1237     BOOST_CHECK_THROW(counterSameName =
1238                           counterDirectory.RegisterCounter(categoryName, 0, 0, 1.0f, "valid name", "valid description"),
1239                       armnn::InvalidArgumentException);
1240     BOOST_CHECK(counterDirectory.GetCounterCount() == 1);
1241     BOOST_CHECK(!counterSameName);
1242
1243     // Register a counter with a valid parent category name and units
1244     const Counter* counterWUnits = nullptr;
1245     BOOST_CHECK_NO_THROW(counterWUnits = counterDirectory.RegisterCounter(categoryName, 0, 1, 123.45f, "valid name 2",
1246                                                                           "valid description",
1247                                                                           std::string("Mnnsq2")));    // Units
1248     BOOST_CHECK(counterDirectory.GetCounterCount() == 2);
1249     BOOST_CHECK(counterWUnits);
1250     BOOST_CHECK(counterWUnits->m_Uid >= 0);
1251     BOOST_CHECK(counterWUnits->m_Uid > counter->m_Uid);
1252     BOOST_CHECK(counterWUnits->m_MaxCounterUid == counterWUnits->m_Uid);
1253     BOOST_CHECK(counterWUnits->m_Class == 0);
1254     BOOST_CHECK(counterWUnits->m_Interpolation == 1);
1255     BOOST_CHECK(counterWUnits->m_Multiplier == 123.45f);
1256     BOOST_CHECK(counterWUnits->m_Name == "valid name 2");
1257     BOOST_CHECK(counterWUnits->m_Description == "valid description");
1258     BOOST_CHECK(counterWUnits->m_Units == "Mnnsq2");
1259     BOOST_CHECK(counterWUnits->m_DeviceUid == 0);
1260     BOOST_CHECK(counterWUnits->m_CounterSetUid == 0);
1261     BOOST_CHECK(category->m_Counters.size() == 2);
1262     BOOST_CHECK(category->m_Counters.back() == counterWUnits->m_Uid);
1263
1264     // Register a counter with a valid parent category name and not associated with a device
1265     const Counter* counterWoDevice = nullptr;
1266     BOOST_CHECK_NO_THROW(counterWoDevice = counterDirectory.RegisterCounter(
1267                              categoryName, 0, 1, 123.45f, "valid name 3", "valid description",
1268                              armnn::EmptyOptional(),    // Units
1269                              armnn::EmptyOptional(),    // Number of cores
1270                              0));                       // Device UID
1271     BOOST_CHECK(counterDirectory.GetCounterCount() == 3);
1272     BOOST_CHECK(counterWoDevice);
1273     BOOST_CHECK(counterWoDevice->m_Uid >= 0);
1274     BOOST_CHECK(counterWoDevice->m_Uid > counter->m_Uid);
1275     BOOST_CHECK(counterWoDevice->m_MaxCounterUid == counterWoDevice->m_Uid);
1276     BOOST_CHECK(counterWoDevice->m_Class == 0);
1277     BOOST_CHECK(counterWoDevice->m_Interpolation == 1);
1278     BOOST_CHECK(counterWoDevice->m_Multiplier == 123.45f);
1279     BOOST_CHECK(counterWoDevice->m_Name == "valid name 3");
1280     BOOST_CHECK(counterWoDevice->m_Description == "valid description");
1281     BOOST_CHECK(counterWoDevice->m_Units == "");
1282     BOOST_CHECK(counterWoDevice->m_DeviceUid == 0);
1283     BOOST_CHECK(counterWoDevice->m_CounterSetUid == 0);
1284     BOOST_CHECK(category->m_Counters.size() == 3);
1285     BOOST_CHECK(category->m_Counters.back() == counterWoDevice->m_Uid);
1286
1287     // Register a counter with a valid parent category name and associated to an invalid device
1288     BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(categoryName, 0, 1, 123.45f, "valid name 4",
1289                                                                    "valid description",
1290                                                                    armnn::EmptyOptional(),    // Units
1291                                                                    armnn::EmptyOptional(),    // Number of cores
1292                                                                    100),                      // Device UID
1293                       armnn::InvalidArgumentException);
1294     BOOST_CHECK(counterDirectory.GetCounterCount() == 3);
1295     BOOST_CHECK(!noCounter);
1296
1297     // Register a device for testing
1298     const std::string deviceName = "some_device";
1299     const Device* device         = nullptr;
1300     BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName));
1301     BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
1302     BOOST_CHECK(device);
1303     BOOST_CHECK(device->m_Name == deviceName);
1304     BOOST_CHECK(device->m_Uid >= 1);
1305     BOOST_CHECK(device->m_Cores == 0);
1306
1307     // Register a counter with a valid parent category name and associated to a device
1308     const Counter* counterWDevice = nullptr;
1309     BOOST_CHECK_NO_THROW(counterWDevice = counterDirectory.RegisterCounter(categoryName, 0, 1, 123.45f, "valid name 5",
1310                                                                            "valid description",
1311                                                                            armnn::EmptyOptional(),    // Units
1312                                                                            armnn::EmptyOptional(),    // Number of cores
1313                                                                            device->m_Uid));           // Device UID
1314     BOOST_CHECK(counterDirectory.GetCounterCount() == 4);
1315     BOOST_CHECK(counterWDevice);
1316     BOOST_CHECK(counterWDevice->m_Uid >= 0);
1317     BOOST_CHECK(counterWDevice->m_Uid > counter->m_Uid);
1318     BOOST_CHECK(counterWDevice->m_MaxCounterUid == counterWDevice->m_Uid);
1319     BOOST_CHECK(counterWDevice->m_Class == 0);
1320     BOOST_CHECK(counterWDevice->m_Interpolation == 1);
1321     BOOST_CHECK(counterWDevice->m_Multiplier == 123.45f);
1322     BOOST_CHECK(counterWDevice->m_Name == "valid name 5");
1323     BOOST_CHECK(counterWDevice->m_Description == "valid description");
1324     BOOST_CHECK(counterWDevice->m_Units == "");
1325     BOOST_CHECK(counterWDevice->m_DeviceUid == device->m_Uid);
1326     BOOST_CHECK(counterWDevice->m_CounterSetUid == 0);
1327     BOOST_CHECK(category->m_Counters.size() == 4);
1328     BOOST_CHECK(category->m_Counters.back() == counterWDevice->m_Uid);
1329
1330     // Register a counter with a valid parent category name and not associated with a counter set
1331     const Counter* counterWoCounterSet = nullptr;
1332     BOOST_CHECK_NO_THROW(counterWoCounterSet = counterDirectory.RegisterCounter(
1333                              categoryName, 0, 1, 123.45f, "valid name 6", "valid description",
1334                              armnn::EmptyOptional(),    // Units
1335                              armnn::EmptyOptional(),    // Number of cores
1336                              armnn::EmptyOptional(),    // Device UID
1337                              0));                       // Counter set UID
1338     BOOST_CHECK(counterDirectory.GetCounterCount() == 5);
1339     BOOST_CHECK(counterWoCounterSet);
1340     BOOST_CHECK(counterWoCounterSet->m_Uid >= 0);
1341     BOOST_CHECK(counterWoCounterSet->m_Uid > counter->m_Uid);
1342     BOOST_CHECK(counterWoCounterSet->m_MaxCounterUid == counterWoCounterSet->m_Uid);
1343     BOOST_CHECK(counterWoCounterSet->m_Class == 0);
1344     BOOST_CHECK(counterWoCounterSet->m_Interpolation == 1);
1345     BOOST_CHECK(counterWoCounterSet->m_Multiplier == 123.45f);
1346     BOOST_CHECK(counterWoCounterSet->m_Name == "valid name 6");
1347     BOOST_CHECK(counterWoCounterSet->m_Description == "valid description");
1348     BOOST_CHECK(counterWoCounterSet->m_Units == "");
1349     BOOST_CHECK(counterWoCounterSet->m_DeviceUid == 0);
1350     BOOST_CHECK(counterWoCounterSet->m_CounterSetUid == 0);
1351     BOOST_CHECK(category->m_Counters.size() == 5);
1352     BOOST_CHECK(category->m_Counters.back() == counterWoCounterSet->m_Uid);
1353
1354     // Register a counter with a valid parent category name and associated to an invalid counter set
1355     BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(categoryName, 0, 1, 123.45f, "valid name 7",
1356                                                                    "valid description",
1357                                                                    armnn::EmptyOptional(),    // Units
1358                                                                    armnn::EmptyOptional(),    // Number of cores
1359                                                                    armnn::EmptyOptional(),    // Device UID
1360                                                                    100),                      // Counter set UID
1361                       armnn::InvalidArgumentException);
1362     BOOST_CHECK(counterDirectory.GetCounterCount() == 5);
1363     BOOST_CHECK(!noCounter);
1364
1365     // Register a counter with a valid parent category name and with a given number of cores
1366     const Counter* counterWNumberOfCores = nullptr;
1367     uint16_t numberOfCores               = 15;
1368     BOOST_CHECK_NO_THROW(counterWNumberOfCores = counterDirectory.RegisterCounter(
1369                              categoryName, 0, 1, 123.45f, "valid name 8", "valid description",
1370                              armnn::EmptyOptional(),      // Units
1371                              numberOfCores,               // Number of cores
1372                              armnn::EmptyOptional(),      // Device UID
1373                              armnn::EmptyOptional()));    // Counter set UID
1374     BOOST_CHECK(counterDirectory.GetCounterCount() == 20);
1375     BOOST_CHECK(counterWNumberOfCores);
1376     BOOST_CHECK(counterWNumberOfCores->m_Uid >= 0);
1377     BOOST_CHECK(counterWNumberOfCores->m_Uid > counter->m_Uid);
1378     BOOST_CHECK(counterWNumberOfCores->m_MaxCounterUid == counterWNumberOfCores->m_Uid + numberOfCores - 1);
1379     BOOST_CHECK(counterWNumberOfCores->m_Class == 0);
1380     BOOST_CHECK(counterWNumberOfCores->m_Interpolation == 1);
1381     BOOST_CHECK(counterWNumberOfCores->m_Multiplier == 123.45f);
1382     BOOST_CHECK(counterWNumberOfCores->m_Name == "valid name 8");
1383     BOOST_CHECK(counterWNumberOfCores->m_Description == "valid description");
1384     BOOST_CHECK(counterWNumberOfCores->m_Units == "");
1385     BOOST_CHECK(counterWNumberOfCores->m_DeviceUid == 0);
1386     BOOST_CHECK(counterWNumberOfCores->m_CounterSetUid == 0);
1387     BOOST_CHECK(category->m_Counters.size() == 20);
1388     for (size_t i = 0; i < numberOfCores; i++)
1389     {
1390         BOOST_CHECK(category->m_Counters[category->m_Counters.size() - numberOfCores + i] ==
1391                     counterWNumberOfCores->m_Uid + i);
1392     }
1393
1394     // Register a multi-core device for testing
1395     const std::string multiCoreDeviceName = "some_multi_core_device";
1396     const Device* multiCoreDevice         = nullptr;
1397     BOOST_CHECK_NO_THROW(multiCoreDevice = counterDirectory.RegisterDevice(multiCoreDeviceName, 4));
1398     BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
1399     BOOST_CHECK(multiCoreDevice);
1400     BOOST_CHECK(multiCoreDevice->m_Name == multiCoreDeviceName);
1401     BOOST_CHECK(multiCoreDevice->m_Uid >= 1);
1402     BOOST_CHECK(multiCoreDevice->m_Cores == 4);
1403
1404     // Register a counter with a valid parent category name and associated to the multi-core device
1405     const Counter* counterWMultiCoreDevice = nullptr;
1406     BOOST_CHECK_NO_THROW(counterWMultiCoreDevice = counterDirectory.RegisterCounter(
1407                              categoryName, 0, 1, 123.45f, "valid name 9", "valid description",
1408                              armnn::EmptyOptional(),      // Units
1409                              armnn::EmptyOptional(),      // Number of cores
1410                              multiCoreDevice->m_Uid,      // Device UID
1411                              armnn::EmptyOptional()));    // Counter set UID
1412     BOOST_CHECK(counterDirectory.GetCounterCount() == 24);
1413     BOOST_CHECK(counterWMultiCoreDevice);
1414     BOOST_CHECK(counterWMultiCoreDevice->m_Uid >= 0);
1415     BOOST_CHECK(counterWMultiCoreDevice->m_Uid > counter->m_Uid);
1416     BOOST_CHECK(counterWMultiCoreDevice->m_MaxCounterUid ==
1417                 counterWMultiCoreDevice->m_Uid + multiCoreDevice->m_Cores - 1);
1418     BOOST_CHECK(counterWMultiCoreDevice->m_Class == 0);
1419     BOOST_CHECK(counterWMultiCoreDevice->m_Interpolation == 1);
1420     BOOST_CHECK(counterWMultiCoreDevice->m_Multiplier == 123.45f);
1421     BOOST_CHECK(counterWMultiCoreDevice->m_Name == "valid name 9");
1422     BOOST_CHECK(counterWMultiCoreDevice->m_Description == "valid description");
1423     BOOST_CHECK(counterWMultiCoreDevice->m_Units == "");
1424     BOOST_CHECK(counterWMultiCoreDevice->m_DeviceUid == multiCoreDevice->m_Uid);
1425     BOOST_CHECK(counterWMultiCoreDevice->m_CounterSetUid == 0);
1426     BOOST_CHECK(category->m_Counters.size() == 24);
1427     for (size_t i = 0; i < 4; i++)
1428     {
1429         BOOST_CHECK(category->m_Counters[category->m_Counters.size() - 4 + i] == counterWMultiCoreDevice->m_Uid + i);
1430     }
1431
1432     // Register a multi-core device associate to a parent category for testing
1433     const std::string multiCoreDeviceNameWParentCategory = "some_multi_core_device_with_parent_category";
1434     const Device* multiCoreDeviceWParentCategory         = nullptr;
1435     BOOST_CHECK_NO_THROW(multiCoreDeviceWParentCategory =
1436                              counterDirectory.RegisterDevice(multiCoreDeviceNameWParentCategory, 2, categoryName));
1437     BOOST_CHECK(counterDirectory.GetDeviceCount() == 3);
1438     BOOST_CHECK(multiCoreDeviceWParentCategory);
1439     BOOST_CHECK(multiCoreDeviceWParentCategory->m_Name == multiCoreDeviceNameWParentCategory);
1440     BOOST_CHECK(multiCoreDeviceWParentCategory->m_Uid >= 1);
1441     BOOST_CHECK(multiCoreDeviceWParentCategory->m_Cores == 2);
1442
1443     // Register a counter with a valid parent category name and getting the number of cores of the multi-core device
1444     // associated to that category
1445     const Counter* counterWMultiCoreDeviceWParentCategory = nullptr;
1446     BOOST_CHECK_NO_THROW(counterWMultiCoreDeviceWParentCategory = counterDirectory.RegisterCounter(
1447                              categoryName, 0, 1, 123.45f, "valid name 10", "valid description",
1448                              armnn::EmptyOptional(),      // Units
1449                              armnn::EmptyOptional(),      // Number of cores
1450                              armnn::EmptyOptional(),      // Device UID
1451                              armnn::EmptyOptional()));    // Counter set UID
1452     BOOST_CHECK(counterDirectory.GetCounterCount() == 26);
1453     BOOST_CHECK(counterWMultiCoreDeviceWParentCategory);
1454     BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Uid >= 0);
1455     BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Uid > counter->m_Uid);
1456     BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_MaxCounterUid ==
1457                 counterWMultiCoreDeviceWParentCategory->m_Uid + multiCoreDeviceWParentCategory->m_Cores - 1);
1458     BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Class == 0);
1459     BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Interpolation == 1);
1460     BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Multiplier == 123.45f);
1461     BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Name == "valid name 10");
1462     BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Description == "valid description");
1463     BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Units == "");
1464     BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_DeviceUid == 0);
1465     BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_CounterSetUid == 0);
1466     BOOST_CHECK(category->m_Counters.size() == 26);
1467     for (size_t i = 0; i < 2; i++)
1468     {
1469         BOOST_CHECK(category->m_Counters[category->m_Counters.size() - 2 + i] ==
1470                     counterWMultiCoreDeviceWParentCategory->m_Uid + i);
1471     }
1472
1473     // Register a counter set for testing
1474     const std::string counterSetName = "some_counter_set";
1475     const CounterSet* counterSet     = nullptr;
1476     BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
1477     BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1478     BOOST_CHECK(counterSet);
1479     BOOST_CHECK(counterSet->m_Name == counterSetName);
1480     BOOST_CHECK(counterSet->m_Uid >= 1);
1481     BOOST_CHECK(counterSet->m_Count == 0);
1482
1483     // Register a counter with a valid parent category name and associated to a counter set
1484     const Counter* counterWCounterSet = nullptr;
1485     BOOST_CHECK_NO_THROW(counterWCounterSet = counterDirectory.RegisterCounter(
1486                              categoryName, 0, 1, 123.45f, "valid name 11", "valid description",
1487                              armnn::EmptyOptional(),    // Units
1488                              0,                         // Number of cores
1489                              armnn::EmptyOptional(),    // Device UID
1490                              counterSet->m_Uid));       // Counter set UID
1491     BOOST_CHECK(counterDirectory.GetCounterCount() == 27);
1492     BOOST_CHECK(counterWCounterSet);
1493     BOOST_CHECK(counterWCounterSet->m_Uid >= 0);
1494     BOOST_CHECK(counterWCounterSet->m_Uid > counter->m_Uid);
1495     BOOST_CHECK(counterWCounterSet->m_MaxCounterUid == counterWCounterSet->m_Uid);
1496     BOOST_CHECK(counterWCounterSet->m_Class == 0);
1497     BOOST_CHECK(counterWCounterSet->m_Interpolation == 1);
1498     BOOST_CHECK(counterWCounterSet->m_Multiplier == 123.45f);
1499     BOOST_CHECK(counterWCounterSet->m_Name == "valid name 11");
1500     BOOST_CHECK(counterWCounterSet->m_Description == "valid description");
1501     BOOST_CHECK(counterWCounterSet->m_Units == "");
1502     BOOST_CHECK(counterWCounterSet->m_DeviceUid == 0);
1503     BOOST_CHECK(counterWCounterSet->m_CounterSetUid == counterSet->m_Uid);
1504     BOOST_CHECK(category->m_Counters.size() == 27);
1505     BOOST_CHECK(category->m_Counters.back() == counterWCounterSet->m_Uid);
1506
1507     // Register a counter with a valid parent category name and associated to a device and a counter set
1508     const Counter* counterWDeviceWCounterSet = nullptr;
1509     BOOST_CHECK_NO_THROW(counterWDeviceWCounterSet = counterDirectory.RegisterCounter(
1510                              categoryName, 0, 1, 123.45f, "valid name 12", "valid description",
1511                              armnn::EmptyOptional(),    // Units
1512                              1,                         // Number of cores
1513                              device->m_Uid,             // Device UID
1514                              counterSet->m_Uid));       // Counter set UID
1515     BOOST_CHECK(counterDirectory.GetCounterCount() == 28);
1516     BOOST_CHECK(counterWDeviceWCounterSet);
1517     BOOST_CHECK(counterWDeviceWCounterSet->m_Uid >= 0);
1518     BOOST_CHECK(counterWDeviceWCounterSet->m_Uid > counter->m_Uid);
1519     BOOST_CHECK(counterWDeviceWCounterSet->m_MaxCounterUid == counterWDeviceWCounterSet->m_Uid);
1520     BOOST_CHECK(counterWDeviceWCounterSet->m_Class == 0);
1521     BOOST_CHECK(counterWDeviceWCounterSet->m_Interpolation == 1);
1522     BOOST_CHECK(counterWDeviceWCounterSet->m_Multiplier == 123.45f);
1523     BOOST_CHECK(counterWDeviceWCounterSet->m_Name == "valid name 12");
1524     BOOST_CHECK(counterWDeviceWCounterSet->m_Description == "valid description");
1525     BOOST_CHECK(counterWDeviceWCounterSet->m_Units == "");
1526     BOOST_CHECK(counterWDeviceWCounterSet->m_DeviceUid == device->m_Uid);
1527     BOOST_CHECK(counterWDeviceWCounterSet->m_CounterSetUid == counterSet->m_Uid);
1528     BOOST_CHECK(category->m_Counters.size() == 28);
1529     BOOST_CHECK(category->m_Counters.back() == counterWDeviceWCounterSet->m_Uid);
1530
1531     // Register another category for testing
1532     const std::string anotherCategoryName = "some_other_category";
1533     const Category* anotherCategory       = nullptr;
1534     BOOST_CHECK_NO_THROW(anotherCategory = counterDirectory.RegisterCategory(anotherCategoryName));
1535     BOOST_CHECK(counterDirectory.GetCategoryCount() == 2);
1536     BOOST_CHECK(anotherCategory);
1537     BOOST_CHECK(anotherCategory != category);
1538     BOOST_CHECK(anotherCategory->m_Name == anotherCategoryName);
1539     BOOST_CHECK(anotherCategory->m_Counters.empty());
1540     BOOST_CHECK(anotherCategory->m_DeviceUid == 0);
1541     BOOST_CHECK(anotherCategory->m_CounterSetUid == 0);
1542
1543     // Register a counter to the other category
1544     const Counter* anotherCounter = nullptr;
1545     BOOST_CHECK_NO_THROW(anotherCounter = counterDirectory.RegisterCounter(anotherCategoryName, 1, 0, .00043f,
1546                                                                            "valid name", "valid description",
1547                                                                            armnn::EmptyOptional(),    // Units
1548                                                                            armnn::EmptyOptional(),    // Number of cores
1549                                                                            device->m_Uid,             // Device UID
1550                                                                            counterSet->m_Uid));       // Counter set UID
1551     BOOST_CHECK(counterDirectory.GetCounterCount() == 29);
1552     BOOST_CHECK(anotherCounter);
1553     BOOST_CHECK(anotherCounter->m_Uid >= 0);
1554     BOOST_CHECK(anotherCounter->m_MaxCounterUid == anotherCounter->m_Uid);
1555     BOOST_CHECK(anotherCounter->m_Class == 1);
1556     BOOST_CHECK(anotherCounter->m_Interpolation == 0);
1557     BOOST_CHECK(anotherCounter->m_Multiplier == .00043f);
1558     BOOST_CHECK(anotherCounter->m_Name == "valid name");
1559     BOOST_CHECK(anotherCounter->m_Description == "valid description");
1560     BOOST_CHECK(anotherCounter->m_Units == "");
1561     BOOST_CHECK(anotherCounter->m_DeviceUid == device->m_Uid);
1562     BOOST_CHECK(anotherCounter->m_CounterSetUid == counterSet->m_Uid);
1563     BOOST_CHECK(anotherCategory->m_Counters.size() == 1);
1564     BOOST_CHECK(anotherCategory->m_Counters.back() == anotherCounter->m_Uid);
1565 }
1566
1567 BOOST_AUTO_TEST_CASE(CounterSelectionCommandHandlerParseData)
1568 {
1569     using boost::numeric_cast;
1570
1571     ProfilingStateMachine profilingStateMachine;
1572
1573     class TestCaptureThread : public IPeriodicCounterCapture
1574     {
1575         void Start() override
1576         {}
1577         void Stop() override
1578         {}
1579     };
1580
1581     class TestReadCounterValues : public IReadCounterValues
1582     {
1583         bool IsCounterRegistered(uint16_t counterUid) const override
1584         {
1585             return true;
1586         }
1587         uint16_t GetCounterCount() const override
1588         {
1589             return 0;
1590         }
1591         uint32_t GetCounterValue(uint16_t counterUid) const override
1592         {
1593             return 0;
1594         }
1595     };
1596     const uint32_t familyId = 0;
1597     const uint32_t packetId = 0x40000;
1598
1599     uint32_t version = 1;
1600     Holder holder;
1601     TestCaptureThread captureThread;
1602     TestReadCounterValues readCounterValues;
1603     MockBufferManager mockBuffer(512);
1604     SendCounterPacket sendCounterPacket(profilingStateMachine, mockBuffer);
1605
1606     uint32_t sizeOfUint32 = numeric_cast<uint32_t>(sizeof(uint32_t));
1607     uint32_t sizeOfUint16 = numeric_cast<uint32_t>(sizeof(uint16_t));
1608
1609     // Data with period and counters
1610     uint32_t period1     = 10;
1611     uint32_t dataLength1 = 8;
1612     uint32_t offset      = 0;
1613
1614     std::unique_ptr<unsigned char[]> uniqueData1 = std::make_unique<unsigned char[]>(dataLength1);
1615     unsigned char* data1                         = reinterpret_cast<unsigned char*>(uniqueData1.get());
1616
1617     WriteUint32(data1, offset, period1);
1618     offset += sizeOfUint32;
1619     WriteUint16(data1, offset, 4000);
1620     offset += sizeOfUint16;
1621     WriteUint16(data1, offset, 5000);
1622
1623     Packet packetA(packetId, dataLength1, uniqueData1);
1624
1625     PeriodicCounterSelectionCommandHandler commandHandler(familyId, packetId, version, holder, captureThread,
1626                                                           readCounterValues, sendCounterPacket, profilingStateMachine);
1627
1628     profilingStateMachine.TransitionToState(ProfilingState::Uninitialised);
1629     BOOST_CHECK_THROW(commandHandler(packetA), armnn::RuntimeException);
1630     profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
1631     BOOST_CHECK_THROW(commandHandler(packetA), armnn::RuntimeException);
1632     profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
1633     BOOST_CHECK_THROW(commandHandler(packetA), armnn::RuntimeException);
1634     profilingStateMachine.TransitionToState(ProfilingState::Active);
1635     BOOST_CHECK_NO_THROW(commandHandler(packetA));
1636
1637     const std::vector<uint16_t> counterIdsA = holder.GetCaptureData().GetCounterIds();
1638
1639     BOOST_TEST(holder.GetCaptureData().GetCapturePeriod() == period1);
1640     BOOST_TEST(counterIdsA.size() == 2);
1641     BOOST_TEST(counterIdsA[0] == 4000);
1642     BOOST_TEST(counterIdsA[1] == 5000);
1643
1644     auto readBuffer = mockBuffer.GetReadableBuffer();
1645
1646     offset = 0;
1647
1648     uint32_t headerWord0 = ReadUint32(readBuffer, offset);
1649     offset += sizeOfUint32;
1650     uint32_t headerWord1 = ReadUint32(readBuffer, offset);
1651     offset += sizeOfUint32;
1652     uint32_t period = ReadUint32(readBuffer, offset);
1653
1654     BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0);     // packet family
1655     BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 4);    // packet id
1656     BOOST_TEST(headerWord1 == 8);                      // data lenght
1657     BOOST_TEST(period == 10);                          // capture period
1658
1659     uint16_t counterId = 0;
1660     offset += sizeOfUint32;
1661     counterId = ReadUint16(readBuffer, offset);
1662     BOOST_TEST(counterId == 4000);
1663     offset += sizeOfUint16;
1664     counterId = ReadUint16(readBuffer, offset);
1665     BOOST_TEST(counterId == 5000);
1666
1667     mockBuffer.MarkRead(readBuffer);
1668
1669     // Data with period only
1670     uint32_t period2     = 11;
1671     uint32_t dataLength2 = 4;
1672
1673     std::unique_ptr<unsigned char[]> uniqueData2 = std::make_unique<unsigned char[]>(dataLength2);
1674
1675     WriteUint32(reinterpret_cast<unsigned char*>(uniqueData2.get()), 0, period2);
1676
1677     Packet packetB(packetId, dataLength2, uniqueData2);
1678
1679     commandHandler(packetB);
1680
1681     const std::vector<uint16_t> counterIdsB = holder.GetCaptureData().GetCounterIds();
1682
1683     BOOST_TEST(holder.GetCaptureData().GetCapturePeriod() == period2);
1684     BOOST_TEST(counterIdsB.size() == 0);
1685
1686     readBuffer = mockBuffer.GetReadableBuffer();
1687
1688     offset = 0;
1689
1690     headerWord0 = ReadUint32(readBuffer, offset);
1691     offset += sizeOfUint32;
1692     headerWord1 = ReadUint32(readBuffer, offset);
1693     offset += sizeOfUint32;
1694     period = ReadUint32(readBuffer, offset);
1695
1696     BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0);     // packet family
1697     BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 4);    // packet id
1698     BOOST_TEST(headerWord1 == 4);                      // data length
1699     BOOST_TEST(period == 11);                          // capture period
1700 }
1701
1702 BOOST_AUTO_TEST_CASE(CheckConnectionAcknowledged)
1703 {
1704     using boost::numeric_cast;
1705
1706     const uint32_t packetFamilyId     = 0;
1707     const uint32_t connectionPacketId = 0x10000;
1708     const uint32_t version            = 1;
1709
1710     uint32_t sizeOfUint32 = numeric_cast<uint32_t>(sizeof(uint32_t));
1711     uint32_t sizeOfUint16 = numeric_cast<uint32_t>(sizeof(uint16_t));
1712
1713     // Data with period and counters
1714     uint32_t period1     = 10;
1715     uint32_t dataLength1 = 8;
1716     uint32_t offset      = 0;
1717
1718     std::unique_ptr<unsigned char[]> uniqueData1 = std::make_unique<unsigned char[]>(dataLength1);
1719     unsigned char* data1                         = reinterpret_cast<unsigned char*>(uniqueData1.get());
1720
1721     WriteUint32(data1, offset, period1);
1722     offset += sizeOfUint32;
1723     WriteUint16(data1, offset, 4000);
1724     offset += sizeOfUint16;
1725     WriteUint16(data1, offset, 5000);
1726
1727     Packet packetA(connectionPacketId, dataLength1, uniqueData1);
1728
1729     ProfilingStateMachine profilingState(ProfilingState::Uninitialised);
1730     BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::Uninitialised);
1731     CounterDirectory counterDirectory;
1732     MockBufferManager mockBuffer(1024);
1733     SendCounterPacket sendCounterPacket(profilingState, mockBuffer);
1734
1735     ConnectionAcknowledgedCommandHandler commandHandler(packetFamilyId, connectionPacketId, version, counterDirectory,
1736                                                         sendCounterPacket, profilingState);
1737
1738     // command handler received packet on ProfilingState::Uninitialised
1739     BOOST_CHECK_THROW(commandHandler(packetA), armnn::Exception);
1740
1741     profilingState.TransitionToState(ProfilingState::NotConnected);
1742     BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::NotConnected);
1743     // command handler received packet on ProfilingState::NotConnected
1744     BOOST_CHECK_THROW(commandHandler(packetA), armnn::Exception);
1745
1746     profilingState.TransitionToState(ProfilingState::WaitingForAck);
1747     BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::WaitingForAck);
1748     // command handler received packet on ProfilingState::WaitingForAck
1749     BOOST_CHECK_NO_THROW(commandHandler(packetA));
1750     BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::Active);
1751
1752     // command handler received packet on ProfilingState::Active
1753     BOOST_CHECK_NO_THROW(commandHandler(packetA));
1754     BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::Active);
1755
1756     // command handler received different packet
1757     const uint32_t differentPacketId = 0x40000;
1758     Packet packetB(differentPacketId, dataLength1, uniqueData1);
1759     profilingState.TransitionToState(ProfilingState::NotConnected);
1760     profilingState.TransitionToState(ProfilingState::WaitingForAck);
1761     ConnectionAcknowledgedCommandHandler differentCommandHandler(packetFamilyId, differentPacketId, version,
1762                                                                  counterDirectory, sendCounterPacket, profilingState);
1763     BOOST_CHECK_THROW(differentCommandHandler(packetB), armnn::Exception);
1764 }
1765
1766 BOOST_AUTO_TEST_CASE(CheckSocketProfilingConnection)
1767 {
1768     // Check that creating a SocketProfilingConnection results in an exception as the Gator UDS doesn't exist.
1769     BOOST_CHECK_THROW(new SocketProfilingConnection(), armnn::Exception);
1770 }
1771
1772 BOOST_AUTO_TEST_CASE(SwTraceIsValidCharTest)
1773 {
1774     // Only ASCII 7-bit encoding supported
1775     for (unsigned char c = 0; c < 128; c++)
1776     {
1777         BOOST_CHECK(SwTraceCharPolicy::IsValidChar(c));
1778     }
1779
1780     // Not ASCII
1781     for (unsigned char c = 255; c >= 128; c++)
1782     {
1783         BOOST_CHECK(!SwTraceCharPolicy::IsValidChar(c));
1784     }
1785 }
1786
1787 BOOST_AUTO_TEST_CASE(SwTraceIsValidNameCharTest)
1788 {
1789     // Only alpha-numeric and underscore ASCII 7-bit encoding supported
1790     const unsigned char validChars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
1791     for (unsigned char i = 0; i < sizeof(validChars) / sizeof(validChars[0]) - 1; i++)
1792     {
1793         BOOST_CHECK(SwTraceNameCharPolicy::IsValidChar(validChars[i]));
1794     }
1795
1796     // Non alpha-numeric chars
1797     for (unsigned char c = 0; c < 48; c++)
1798     {
1799         BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1800     }
1801     for (unsigned char c = 58; c < 65; c++)
1802     {
1803         BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1804     }
1805     for (unsigned char c = 91; c < 95; c++)
1806     {
1807         BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1808     }
1809     for (unsigned char c = 96; c < 97; c++)
1810     {
1811         BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1812     }
1813     for (unsigned char c = 123; c < 128; c++)
1814     {
1815         BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1816     }
1817
1818     // Not ASCII
1819     for (unsigned char c = 255; c >= 128; c++)
1820     {
1821         BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1822     }
1823 }
1824
1825 BOOST_AUTO_TEST_CASE(IsValidSwTraceStringTest)
1826 {
1827     // Valid SWTrace strings
1828     BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>(""));
1829     BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("_"));
1830     BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("0123"));
1831     BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("valid_string"));
1832     BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("VALID_string_456"));
1833     BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>(" "));
1834     BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("valid string"));
1835     BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("!$%"));
1836     BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("valid|\\~string#123"));
1837
1838     // Invalid SWTrace strings
1839     BOOST_CHECK(!IsValidSwTraceString<SwTraceCharPolicy>("€£"));
1840     BOOST_CHECK(!IsValidSwTraceString<SwTraceCharPolicy>("invalid‡string"));
1841     BOOST_CHECK(!IsValidSwTraceString<SwTraceCharPolicy>("12Ž34"));
1842 }
1843
1844 BOOST_AUTO_TEST_CASE(IsValidSwTraceNameStringTest)
1845 {
1846     // Valid SWTrace name strings
1847     BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>(""));
1848     BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>("_"));
1849     BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>("0123"));
1850     BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>("valid_string"));
1851     BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>("VALID_string_456"));
1852
1853     // Invalid SWTrace name strings
1854     BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>(" "));
1855     BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("invalid string"));
1856     BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("!$%"));
1857     BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("invalid|\\~string#123"));
1858     BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("€£"));
1859     BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("invalid‡string"));
1860     BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("12Ž34"));
1861 }
1862
1863 template <typename SwTracePolicy>
1864 void StringToSwTraceStringTestHelper(const std::string& testString, std::vector<uint32_t> buffer, size_t expectedSize)
1865 {
1866     // Convert the test string to a SWTrace string
1867     BOOST_CHECK(StringToSwTraceString<SwTracePolicy>(testString, buffer));
1868
1869     // The buffer must contain at least the length of the string
1870     BOOST_CHECK(!buffer.empty());
1871
1872     // The buffer must be of the expected size (in words)
1873     BOOST_CHECK(buffer.size() == expectedSize);
1874
1875     // The first word of the byte must be the length of the string including the null-terminator
1876     BOOST_CHECK(buffer[0] == testString.size() + 1);
1877
1878     // The contents of the buffer must match the test string
1879     BOOST_CHECK(std::memcmp(testString.data(), buffer.data() + 1, testString.size()) == 0);
1880
1881     // The buffer must include the null-terminator at the end of the string
1882     size_t nullTerminatorIndex = sizeof(uint32_t) + testString.size();
1883     BOOST_CHECK(reinterpret_cast<unsigned char*>(buffer.data())[nullTerminatorIndex] == '\0');
1884 }
1885
1886 BOOST_AUTO_TEST_CASE(StringToSwTraceStringTest)
1887 {
1888     std::vector<uint32_t> buffer;
1889
1890     // Valid SWTrace strings (expected size in words)
1891     StringToSwTraceStringTestHelper<SwTraceCharPolicy>("", buffer, 2);
1892     StringToSwTraceStringTestHelper<SwTraceCharPolicy>("_", buffer, 2);
1893     StringToSwTraceStringTestHelper<SwTraceCharPolicy>("0123", buffer, 3);
1894     StringToSwTraceStringTestHelper<SwTraceCharPolicy>("valid_string", buffer, 5);
1895     StringToSwTraceStringTestHelper<SwTraceCharPolicy>("VALID_string_456", buffer, 6);
1896     StringToSwTraceStringTestHelper<SwTraceCharPolicy>(" ", buffer, 2);
1897     StringToSwTraceStringTestHelper<SwTraceCharPolicy>("valid string", buffer, 5);
1898     StringToSwTraceStringTestHelper<SwTraceCharPolicy>("!$%", buffer, 2);
1899     StringToSwTraceStringTestHelper<SwTraceCharPolicy>("valid|\\~string#123", buffer, 6);
1900
1901     // Invalid SWTrace strings
1902     BOOST_CHECK(!StringToSwTraceString<SwTraceCharPolicy>("€£", buffer));
1903     BOOST_CHECK(buffer.empty());
1904     BOOST_CHECK(!StringToSwTraceString<SwTraceCharPolicy>("invalid‡string", buffer));
1905     BOOST_CHECK(buffer.empty());
1906     BOOST_CHECK(!StringToSwTraceString<SwTraceCharPolicy>("12Ž34", buffer));
1907     BOOST_CHECK(buffer.empty());
1908 }
1909
1910 BOOST_AUTO_TEST_CASE(StringToSwTraceNameStringTest)
1911 {
1912     std::vector<uint32_t> buffer;
1913
1914     // Valid SWTrace namestrings (expected size in words)
1915     StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("", buffer, 2);
1916     StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("_", buffer, 2);
1917     StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("0123", buffer, 3);
1918     StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("valid_string", buffer, 5);
1919     StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("VALID_string_456", buffer, 6);
1920
1921     // Invalid SWTrace namestrings
1922     BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>(" ", buffer));
1923     BOOST_CHECK(buffer.empty());
1924     BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("invalid string", buffer));
1925     BOOST_CHECK(buffer.empty());
1926     BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("!$%", buffer));
1927     BOOST_CHECK(buffer.empty());
1928     BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("invalid|\\~string#123", buffer));
1929     BOOST_CHECK(buffer.empty());
1930     BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("€£", buffer));
1931     BOOST_CHECK(buffer.empty());
1932     BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("invalid‡string", buffer));
1933     BOOST_CHECK(buffer.empty());
1934     BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("12Ž34", buffer));
1935     BOOST_CHECK(buffer.empty());
1936 }
1937
1938 BOOST_AUTO_TEST_CASE(CheckPeriodicCounterCaptureThread)
1939 {
1940     class CaptureReader : public IReadCounterValues
1941     {
1942     public:
1943         CaptureReader(uint16_t counterSize)
1944         {
1945             for (uint16_t i = 0; i < counterSize; ++i)
1946             {
1947                 m_Data[i] = 0;
1948             }
1949             m_CounterSize = counterSize;
1950         }
1951         //not used
1952         bool IsCounterRegistered(uint16_t counterUid) const override
1953         {
1954             return false;
1955         }
1956
1957         uint16_t GetCounterCount() const override
1958         {
1959             return m_CounterSize;
1960         }
1961
1962         uint32_t GetCounterValue(uint16_t counterUid) const override
1963         {
1964             if (counterUid > m_CounterSize)
1965             {
1966                 BOOST_FAIL("Invalid counter Uid");
1967             }
1968             return m_Data.at(counterUid).load();
1969         }
1970
1971         void SetCounterValue(uint16_t counterUid, uint32_t value)
1972         {
1973             if (counterUid > m_CounterSize)
1974             {
1975                 BOOST_FAIL("Invalid counter Uid");
1976             }
1977             m_Data.at(counterUid).store(value);
1978         }
1979
1980     private:
1981         std::unordered_map<uint16_t, std::atomic<uint32_t>> m_Data;
1982         uint16_t m_CounterSize;
1983     };
1984
1985     ProfilingStateMachine profilingStateMachine;
1986
1987     Holder data;
1988     std::vector<uint16_t> captureIds1 = { 0, 1 };
1989     std::vector<uint16_t> captureIds2;
1990
1991     MockBufferManager mockBuffer(512);
1992     SendCounterPacket sendCounterPacket(profilingStateMachine, mockBuffer);
1993
1994     std::vector<uint16_t> counterIds;
1995     CaptureReader captureReader(2);
1996
1997     unsigned int valueA   = 10;
1998     unsigned int valueB   = 15;
1999     unsigned int numSteps = 5;
2000
2001     PeriodicCounterCapture periodicCounterCapture(std::ref(data), std::ref(sendCounterPacket), captureReader);
2002
2003     for (unsigned int i = 0; i < numSteps; ++i)
2004     {
2005         data.SetCaptureData(1, captureIds1);
2006         captureReader.SetCounterValue(0, valueA * (i + 1));
2007         captureReader.SetCounterValue(1, valueB * (i + 1));
2008
2009         periodicCounterCapture.Start();
2010         periodicCounterCapture.Stop();
2011     }
2012
2013     auto buffer = mockBuffer.GetReadableBuffer();
2014
2015     uint32_t headerWord0 = ReadUint32(buffer, 0);
2016     uint32_t headerWord1 = ReadUint32(buffer, 4);
2017
2018     BOOST_TEST(((headerWord0 >> 26) & 0x0000003F) == 1);    // packet family
2019     BOOST_TEST(((headerWord0 >> 19) & 0x0000007F) == 0);    // packet class
2020     BOOST_TEST(((headerWord0 >> 16) & 0x00000007) == 0);    // packet type
2021     BOOST_TEST(headerWord1 == 20);
2022
2023     uint32_t offset    = 16;
2024     uint16_t readIndex = ReadUint16(buffer, offset);
2025     BOOST_TEST(0 == readIndex);
2026
2027     offset += 2;
2028     uint32_t readValue = ReadUint32(buffer, offset);
2029     BOOST_TEST((valueA * numSteps) == readValue);
2030
2031     offset += 4;
2032     readIndex = ReadUint16(buffer, offset);
2033     BOOST_TEST(1 == readIndex);
2034
2035     offset += 2;
2036     readValue = ReadUint32(buffer, offset);
2037     BOOST_TEST((valueB * numSteps) == readValue);
2038 }
2039
2040 BOOST_AUTO_TEST_CASE(RequestCounterDirectoryCommandHandlerTest1)
2041 {
2042     using boost::numeric_cast;
2043
2044     const uint32_t familyId = 0;
2045     const uint32_t packetId = 3;
2046     const uint32_t version  = 1;
2047     ProfilingStateMachine profilingStateMachine;
2048     CounterDirectory counterDirectory;
2049     MockBufferManager mockBuffer(1024);
2050     SendCounterPacket sendCounterPacket(profilingStateMachine, mockBuffer);
2051     RequestCounterDirectoryCommandHandler commandHandler(familyId, packetId, version, counterDirectory,
2052                                                          sendCounterPacket, profilingStateMachine);
2053
2054     const uint32_t wrongPacketId = 47;
2055     const uint32_t wrongHeader   = (wrongPacketId & 0x000003FF) << 16;
2056
2057     Packet wrongPacket(wrongHeader);
2058
2059     profilingStateMachine.TransitionToState(ProfilingState::Uninitialised);
2060     BOOST_CHECK_THROW(commandHandler(wrongPacket), armnn::RuntimeException);    // Wrong profiling state
2061     profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
2062     BOOST_CHECK_THROW(commandHandler(wrongPacket), armnn::RuntimeException);    // Wrong profiling state
2063     profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
2064     BOOST_CHECK_THROW(commandHandler(wrongPacket), armnn::RuntimeException);    // Wrong profiling state
2065     profilingStateMachine.TransitionToState(ProfilingState::Active);
2066     BOOST_CHECK_THROW(commandHandler(wrongPacket), armnn::InvalidArgumentException);    // Wrong packet
2067
2068     const uint32_t rightHeader = (packetId & 0x000003FF) << 16;
2069
2070     Packet rightPacket(rightHeader);
2071
2072     BOOST_CHECK_NO_THROW(commandHandler(rightPacket));    // Right packet
2073
2074     auto readBuffer = mockBuffer.GetReadableBuffer();
2075
2076     uint32_t headerWord0 = ReadUint32(readBuffer, 0);
2077     uint32_t headerWord1 = ReadUint32(readBuffer, 4);
2078
2079     BOOST_TEST(((headerWord0 >> 26) & 0x0000003F) == 0);    // packet family
2080     BOOST_TEST(((headerWord0 >> 16) & 0x000003FF) == 2);    // packet id
2081     BOOST_TEST(headerWord1 == 24);                          // data length
2082
2083     uint32_t bodyHeaderWord0   = ReadUint32(readBuffer, 8);
2084     uint16_t deviceRecordCount = numeric_cast<uint16_t>(bodyHeaderWord0 >> 16);
2085     BOOST_TEST(deviceRecordCount == 0);    // device_records_count
2086 }
2087
2088 BOOST_AUTO_TEST_CASE(RequestCounterDirectoryCommandHandlerTest2)
2089 {
2090     using boost::numeric_cast;
2091
2092     const uint32_t familyId = 0;
2093     const uint32_t packetId = 3;
2094     const uint32_t version  = 1;
2095     ProfilingStateMachine profilingStateMachine;
2096     CounterDirectory counterDirectory;
2097     MockBufferManager mockBuffer(1024);
2098     SendCounterPacket sendCounterPacket(profilingStateMachine, mockBuffer);
2099     RequestCounterDirectoryCommandHandler commandHandler(familyId, packetId, version, counterDirectory,
2100                                                          sendCounterPacket, profilingStateMachine);
2101     const uint32_t header = (packetId & 0x000003FF) << 16;
2102     Packet packet(header);
2103
2104     const Device* device         = counterDirectory.RegisterDevice("deviceA", 1);
2105     const CounterSet* counterSet = counterDirectory.RegisterCounterSet("countersetA");
2106     counterDirectory.RegisterCategory("categoryA", device->m_Uid, counterSet->m_Uid);
2107     counterDirectory.RegisterCounter("categoryA", 0, 1, 2.0f, "counterA", "descA");
2108     counterDirectory.RegisterCounter("categoryA", 1, 1, 3.0f, "counterB", "descB");
2109
2110     profilingStateMachine.TransitionToState(ProfilingState::Uninitialised);
2111     BOOST_CHECK_THROW(commandHandler(packet), armnn::RuntimeException);    // Wrong profiling state
2112     profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
2113     BOOST_CHECK_THROW(commandHandler(packet), armnn::RuntimeException);    // Wrong profiling state
2114     profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
2115     BOOST_CHECK_THROW(commandHandler(packet), armnn::RuntimeException);    // Wrong profiling state
2116     profilingStateMachine.TransitionToState(ProfilingState::Active);
2117     BOOST_CHECK_NO_THROW(commandHandler(packet));
2118
2119     auto readBuffer = mockBuffer.GetReadableBuffer();
2120
2121     uint32_t headerWord0 = ReadUint32(readBuffer, 0);
2122     uint32_t headerWord1 = ReadUint32(readBuffer, 4);
2123
2124     BOOST_TEST(((headerWord0 >> 26) & 0x0000003F) == 0);    // packet family
2125     BOOST_TEST(((headerWord0 >> 16) & 0x000003FF) == 2);    // packet id
2126     BOOST_TEST(headerWord1 == 240);                         // data length
2127
2128     uint32_t bodyHeaderWord0       = ReadUint32(readBuffer, 8);
2129     uint32_t bodyHeaderWord1       = ReadUint32(readBuffer, 12);
2130     uint32_t bodyHeaderWord2       = ReadUint32(readBuffer, 16);
2131     uint32_t bodyHeaderWord3       = ReadUint32(readBuffer, 20);
2132     uint32_t bodyHeaderWord4       = ReadUint32(readBuffer, 24);
2133     uint32_t bodyHeaderWord5       = ReadUint32(readBuffer, 28);
2134     uint16_t deviceRecordCount     = numeric_cast<uint16_t>(bodyHeaderWord0 >> 16);
2135     uint16_t counterSetRecordCount = numeric_cast<uint16_t>(bodyHeaderWord2 >> 16);
2136     uint16_t categoryRecordCount   = numeric_cast<uint16_t>(bodyHeaderWord4 >> 16);
2137     BOOST_TEST(deviceRecordCount == 1);        // device_records_count
2138     BOOST_TEST(bodyHeaderWord1 == 0);          // device_records_pointer_table_offset
2139     BOOST_TEST(counterSetRecordCount == 1);    // counter_set_count
2140     BOOST_TEST(bodyHeaderWord3 == 4);          // counter_set_pointer_table_offset
2141     BOOST_TEST(categoryRecordCount == 1);      // categories_count
2142     BOOST_TEST(bodyHeaderWord5 == 8);          // categories_pointer_table_offset
2143
2144     uint32_t deviceRecordOffset = ReadUint32(readBuffer, 32);
2145     BOOST_TEST(deviceRecordOffset == 0);
2146
2147     uint32_t counterSetRecordOffset = ReadUint32(readBuffer, 36);
2148     BOOST_TEST(counterSetRecordOffset == 20);
2149
2150     uint32_t categoryRecordOffset = ReadUint32(readBuffer, 40);
2151     BOOST_TEST(categoryRecordOffset == 44);
2152 }
2153
2154 BOOST_AUTO_TEST_CASE(CheckProfilingServiceBadConnectionAcknowledgedPacket)
2155 {
2156     // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
2157     LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
2158
2159     // Swap the profiling connection factory in the profiling service instance with our mock one
2160     SwapProfilingConnectionFactoryHelper helper;
2161
2162     // Redirect the standard output to a local stream so that we can parse the warning message
2163     std::stringstream ss;
2164     StreamRedirector streamRedirector(std::cout, ss.rdbuf());
2165
2166     // Calculate the size of a Stream Metadata packet
2167     std::string processName      = GetProcessName().substr(0, 60);
2168     unsigned int processNameSize = processName.empty() ? 0 : boost::numeric_cast<unsigned int>(processName.size()) + 1;
2169     unsigned int streamMetadataPacketsize = 118 + processNameSize;
2170
2171     // Reset the profiling service to the uninitialized state
2172     armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
2173     options.m_EnableProfiling          = true;
2174     ProfilingService& profilingService = ProfilingService::Instance();
2175     profilingService.ResetExternalProfilingOptions(options, true);
2176
2177     // Bring the profiling service to the "WaitingForAck" state
2178     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
2179     profilingService.Update();    // Initialize the counter directory
2180     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
2181     profilingService.Update();    // Create the profiling connection
2182
2183     // Get the mock profiling connection
2184     MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2185     BOOST_CHECK(mockProfilingConnection);
2186
2187     // Remove the packets received so far
2188     mockProfilingConnection->Clear();
2189
2190     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
2191     profilingService.Update();
2192
2193     // Wait for the Stream Metadata packet to be sent
2194     helper.WaitForProfilingPacketsSent();
2195
2196     // Check that the mock profiling connection contains one Stream Metadata packet
2197     const std::vector<uint32_t> writtenData = mockProfilingConnection->GetWrittenData();
2198     BOOST_TEST(writtenData.size() == 1);
2199     BOOST_TEST(writtenData[0] == streamMetadataPacketsize);
2200
2201     // Write a valid "Connection Acknowledged" packet into the mock profiling connection, to simulate a valid
2202     // reply from an external profiling service
2203
2204     // Connection Acknowledged Packet header (word 0, word 1 is always zero):
2205     // 26:31 [6]  packet_family: Control Packet Family, value 0b000000
2206     // 16:25 [10] packet_id: Packet identifier, value 0b0000000001
2207     // 8:15  [8]  reserved: Reserved, value 0b00000000
2208     // 0:7   [8]  reserved: Reserved, value 0b00000000
2209     uint32_t packetFamily = 0;
2210     uint32_t packetId     = 37;    // Wrong packet id!!!
2211     uint32_t header       = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
2212
2213     // Create the Connection Acknowledged Packet
2214     Packet connectionAcknowledgedPacket(header);
2215
2216     // Write the packet to the mock profiling connection
2217     mockProfilingConnection->WritePacket(std::move(connectionAcknowledgedPacket));
2218
2219     // Wait for a bit (must at least be the delay value of the mock profiling connection) to make sure that
2220     // the Connection Acknowledged packet gets processed by the profiling service
2221     std::this_thread::sleep_for(std::chrono::seconds(2));
2222
2223     // Check that the expected error has occurred and logged to the standard output
2224     BOOST_CHECK(boost::contains(ss.str(), "Functor with requested PacketId=37 and Version=4194304 does not exist"));
2225
2226     // The Connection Acknowledged Command Handler should not have updated the profiling state
2227     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
2228
2229     // Reset the profiling service to stop any running thread
2230     options.m_EnableProfiling = false;
2231     profilingService.ResetExternalProfilingOptions(options, true);
2232 }
2233
2234 BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodConnectionAcknowledgedPacket)
2235 {
2236     // Swap the profiling connection factory in the profiling service instance with our mock one
2237     SwapProfilingConnectionFactoryHelper helper;
2238
2239     // Calculate the size of a Stream Metadata packet
2240     std::string processName      = GetProcessName().substr(0, 60);
2241     unsigned int processNameSize = processName.empty() ? 0 : boost::numeric_cast<unsigned int>(processName.size()) + 1;
2242     unsigned int streamMetadataPacketsize = 118 + processNameSize;
2243
2244     // Reset the profiling service to the uninitialized state
2245     armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
2246     options.m_EnableProfiling          = true;
2247     ProfilingService& profilingService = ProfilingService::Instance();
2248     profilingService.ResetExternalProfilingOptions(options, true);
2249
2250     // Bring the profiling service to the "WaitingForAck" state
2251     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
2252     profilingService.Update();    // Initialize the counter directory
2253     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
2254     profilingService.Update();    // Create the profiling connection
2255
2256     // Get the mock profiling connection
2257     MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2258     BOOST_CHECK(mockProfilingConnection);
2259
2260     // Remove the packets received so far
2261     mockProfilingConnection->Clear();
2262
2263     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
2264     profilingService.Update();    // Start the command handler and the send thread
2265
2266     // Wait for the Stream Metadata packet to be sent
2267     helper.WaitForProfilingPacketsSent();
2268
2269     // Check that the mock profiling connection contains one Stream Metadata packet
2270     const std::vector<uint32_t> writtenData = mockProfilingConnection->GetWrittenData();
2271     BOOST_TEST(writtenData.size() == 1);
2272     BOOST_TEST(writtenData[0] == streamMetadataPacketsize);
2273
2274     // Write a valid "Connection Acknowledged" packet into the mock profiling connection, to simulate a valid
2275     // reply from an external profiling service
2276
2277     // Connection Acknowledged Packet header (word 0, word 1 is always zero):
2278     // 26:31 [6]  packet_family: Control Packet Family, value 0b000000
2279     // 16:25 [10] packet_id: Packet identifier, value 0b0000000001
2280     // 8:15  [8]  reserved: Reserved, value 0b00000000
2281     // 0:7   [8]  reserved: Reserved, value 0b00000000
2282     uint32_t packetFamily = 0;
2283     uint32_t packetId     = 1;
2284     uint32_t header       = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
2285
2286     // Create the Connection Acknowledged Packet
2287     Packet connectionAcknowledgedPacket(header);
2288
2289     // Write the packet to the mock profiling connection
2290     mockProfilingConnection->WritePacket(std::move(connectionAcknowledgedPacket));
2291
2292     // Wait for a bit (must at least be the delay value of the mock profiling connection) to make sure that
2293     // the Connection Acknowledged packet gets processed by the profiling service
2294     std::this_thread::sleep_for(std::chrono::seconds(2));
2295
2296     // The Connection Acknowledged Command Handler should have updated the profiling state accordingly
2297     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2298
2299     // Reset the profiling service to stop any running thread
2300     options.m_EnableProfiling = false;
2301     profilingService.ResetExternalProfilingOptions(options, true);
2302 }
2303
2304 BOOST_AUTO_TEST_CASE(CheckProfilingServiceBadRequestCounterDirectoryPacket)
2305 {
2306     // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
2307     LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
2308
2309     // Swap the profiling connection factory in the profiling service instance with our mock one
2310     SwapProfilingConnectionFactoryHelper helper;
2311
2312     // Redirect the standard output to a local stream so that we can parse the warning message
2313     std::stringstream ss;
2314     StreamRedirector streamRedirector(std::cout, ss.rdbuf());
2315
2316     // Reset the profiling service to the uninitialized state
2317     armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
2318     options.m_EnableProfiling          = true;
2319     ProfilingService& profilingService = ProfilingService::Instance();
2320     profilingService.ResetExternalProfilingOptions(options, true);
2321
2322     // Bring the profiling service to the "Active" state
2323     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
2324     helper.ForceTransitionToState(ProfilingState::NotConnected);
2325     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
2326     profilingService.Update();    // Create the profiling connection
2327     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
2328     profilingService.Update();    // Start the command handler and the send thread
2329
2330     // Wait for the Stream Metadata packet the be sent
2331     // (we are not testing the connection acknowledgement here so it will be ignored by this test)
2332     helper.WaitForProfilingPacketsSent();
2333
2334     // Force the profiling service to the "Active" state
2335     helper.ForceTransitionToState(ProfilingState::Active);
2336     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2337
2338     // Get the mock profiling connection
2339     MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2340     BOOST_CHECK(mockProfilingConnection);
2341
2342     // Remove the packets received so far
2343     mockProfilingConnection->Clear();
2344
2345     // Write a valid "Request Counter Directory" packet into the mock profiling connection, to simulate a valid
2346     // reply from an external profiling service
2347
2348     // Request Counter Directory packet header (word 0, word 1 is always zero):
2349     // 26:31 [6]  packet_family: Control Packet Family, value 0b000000
2350     // 16:25 [10] packet_id: Packet identifier, value 0b0000000011
2351     // 8:15  [8]  reserved: Reserved, value 0b00000000
2352     // 0:7   [8]  reserved: Reserved, value 0b00000000
2353     uint32_t packetFamily = 0;
2354     uint32_t packetId     = 123;    // Wrong packet id!!!
2355     uint32_t header       = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
2356
2357     // Create the Request Counter Directory packet
2358     Packet requestCounterDirectoryPacket(header);
2359
2360     // Write the packet to the mock profiling connection
2361     mockProfilingConnection->WritePacket(std::move(requestCounterDirectoryPacket));
2362
2363     // Wait for a bit (must at least be the delay value of the mock profiling connection) to make sure that
2364     // the Create the Request Counter packet gets processed by the profiling service
2365     std::this_thread::sleep_for(std::chrono::seconds(2));
2366
2367     // Check that the expected error has occurred and logged to the standard output
2368     BOOST_CHECK(boost::contains(ss.str(), "Functor with requested PacketId=123 and Version=4194304 does not exist"));
2369
2370     // The Request Counter Directory Command Handler should not have updated the profiling state
2371     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2372
2373     // Reset the profiling service to stop any running thread
2374     options.m_EnableProfiling = false;
2375     profilingService.ResetExternalProfilingOptions(options, true);
2376 }
2377
2378 BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodRequestCounterDirectoryPacket)
2379 {
2380     // Swap the profiling connection factory in the profiling service instance with our mock one
2381     SwapProfilingConnectionFactoryHelper helper;
2382
2383     // Reset the profiling service to the uninitialized state
2384     armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
2385     options.m_EnableProfiling          = true;
2386     ProfilingService& profilingService = ProfilingService::Instance();
2387     profilingService.ResetExternalProfilingOptions(options, true);
2388
2389     // Bring the profiling service to the "Active" state
2390     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
2391     profilingService.Update();    // Initialize the counter directory
2392     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
2393     profilingService.Update();    // Create the profiling connection
2394     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
2395     profilingService.Update();    // Start the command handler and the send thread
2396
2397     // Wait for the Stream Metadata packet the be sent
2398     // (we are not testing the connection acknowledgement here so it will be ignored by this test)
2399     helper.WaitForProfilingPacketsSent();
2400
2401     // Force the profiling service to the "Active" state
2402     helper.ForceTransitionToState(ProfilingState::Active);
2403     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2404
2405     // Get the mock profiling connection
2406     MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2407     BOOST_CHECK(mockProfilingConnection);
2408
2409     // Remove the packets received so far
2410     mockProfilingConnection->Clear();
2411
2412     // Write a valid "Request Counter Directory" packet into the mock profiling connection, to simulate a valid
2413     // reply from an external profiling service
2414
2415     // Request Counter Directory packet header (word 0, word 1 is always zero):
2416     // 26:31 [6]  packet_family: Control Packet Family, value 0b000000
2417     // 16:25 [10] packet_id: Packet identifier, value 0b0000000011
2418     // 8:15  [8]  reserved: Reserved, value 0b00000000
2419     // 0:7   [8]  reserved: Reserved, value 0b00000000
2420     uint32_t packetFamily = 0;
2421     uint32_t packetId     = 3;
2422     uint32_t header       = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
2423
2424     // Create the Request Counter Directory packet
2425     Packet requestCounterDirectoryPacket(header);
2426
2427     // Write the packet to the mock profiling connection
2428     mockProfilingConnection->WritePacket(std::move(requestCounterDirectoryPacket));
2429
2430     // Wait for the Counter Directory packet to be sent
2431     helper.WaitForProfilingPacketsSent();
2432
2433     // Check that the mock profiling connection contains one Counter Directory packet
2434     const std::vector<uint32_t> writtenData = mockProfilingConnection->GetWrittenData();
2435     BOOST_TEST(writtenData.size() == 1);
2436     BOOST_TEST(writtenData[0] == 416);    // The size of the expected Counter Directory packet
2437
2438     // The Request Counter Directory Command Handler should not have updated the profiling state
2439     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2440
2441     // Reset the profiling service to stop any running thread
2442     options.m_EnableProfiling = false;
2443     profilingService.ResetExternalProfilingOptions(options, true);
2444 }
2445
2446 BOOST_AUTO_TEST_CASE(CheckProfilingServiceBadPeriodicCounterSelectionPacket)
2447 {
2448     // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
2449     LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
2450
2451     // Swap the profiling connection factory in the profiling service instance with our mock one
2452     SwapProfilingConnectionFactoryHelper helper;
2453
2454     // Redirect the standard output to a local stream so that we can parse the warning message
2455     std::stringstream ss;
2456     StreamRedirector streamRedirector(std::cout, ss.rdbuf());
2457
2458     // Reset the profiling service to the uninitialized state
2459     armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
2460     options.m_EnableProfiling          = true;
2461     ProfilingService& profilingService = ProfilingService::Instance();
2462     profilingService.ResetExternalProfilingOptions(options, true);
2463
2464     // Bring the profiling service to the "Active" state
2465     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
2466     profilingService.Update();    // Initialize the counter directory
2467     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
2468     profilingService.Update();    // Create the profiling connection
2469     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
2470     profilingService.Update();    // Start the command handler and the send thread
2471
2472     // Wait for the Stream Metadata packet the be sent
2473     // (we are not testing the connection acknowledgement here so it will be ignored by this test)
2474     helper.WaitForProfilingPacketsSent();
2475
2476     // Force the profiling service to the "Active" state
2477     helper.ForceTransitionToState(ProfilingState::Active);
2478     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2479
2480     // Get the mock profiling connection
2481     MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2482     BOOST_CHECK(mockProfilingConnection);
2483
2484     // Remove the packets received so far
2485     mockProfilingConnection->Clear();
2486
2487     // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2488     // external profiling service
2489
2490     // Periodic Counter Selection packet header:
2491     // 26:31 [6]  packet_family: Control Packet Family, value 0b000000
2492     // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2493     // 8:15  [8]  reserved: Reserved, value 0b00000000
2494     // 0:7   [8]  reserved: Reserved, value 0b00000000
2495     uint32_t packetFamily = 0;
2496     uint32_t packetId     = 999;    // Wrong packet id!!!
2497     uint32_t header       = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
2498
2499     // Create the Periodic Counter Selection packet
2500     Packet periodicCounterSelectionPacket(header);    // Length == 0, this will disable the collection of counters
2501
2502     // Write the packet to the mock profiling connection
2503     mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2504
2505     // Wait for a bit (must at least be the delay value of the mock profiling connection) to make sure that
2506     // the Periodic Counter Selection packet gets processed by the profiling service
2507     std::this_thread::sleep_for(std::chrono::seconds(2));
2508
2509     // Check that the expected error has occurred and logged to the standard output
2510     BOOST_CHECK(boost::contains(ss.str(), "Functor with requested PacketId=999 and Version=4194304 does not exist"));
2511
2512     // The Periodic Counter Selection Handler should not have updated the profiling state
2513     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2514
2515     // Reset the profiling service to stop any running thread
2516     options.m_EnableProfiling = false;
2517     profilingService.ResetExternalProfilingOptions(options, true);
2518 }
2519
2520 BOOST_AUTO_TEST_CASE(CheckProfilingServiceBadPeriodicCounterSelectionPacketInvalidCounterUid)
2521 {
2522     // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
2523     LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
2524
2525     // Swap the profiling connection factory in the profiling service instance with our mock one
2526     SwapProfilingConnectionFactoryHelper helper;
2527
2528     // Reset the profiling service to the uninitialized state
2529     armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
2530     options.m_EnableProfiling          = true;
2531     ProfilingService& profilingService = ProfilingService::Instance();
2532     profilingService.ResetExternalProfilingOptions(options, true);
2533
2534     // Bring the profiling service to the "Active" state
2535     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
2536     profilingService.Update();    // Initialize the counter directory
2537     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
2538     profilingService.Update();    // Create the profiling connection
2539     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
2540     profilingService.Update();    // Start the command handler and the send thread
2541
2542     // Wait for the Stream Metadata packet the be sent
2543     // (we are not testing the connection acknowledgement here so it will be ignored by this test)
2544     helper.WaitForProfilingPacketsSent();
2545
2546     // Force the profiling service to the "Active" state
2547     helper.ForceTransitionToState(ProfilingState::Active);
2548     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2549
2550     // Get the mock profiling connection
2551     MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2552     BOOST_CHECK(mockProfilingConnection);
2553
2554     // Remove the packets received so far
2555     mockProfilingConnection->Clear();
2556
2557     // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2558     // external profiling service
2559
2560     // Periodic Counter Selection packet header:
2561     // 26:31 [6]  packet_family: Control Packet Family, value 0b000000
2562     // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2563     // 8:15  [8]  reserved: Reserved, value 0b00000000
2564     // 0:7   [8]  reserved: Reserved, value 0b00000000
2565     uint32_t packetFamily = 0;
2566     uint32_t packetId     = 4;
2567     uint32_t header       = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
2568
2569     uint32_t capturePeriod = 123456;    // Some capture period (microseconds)
2570
2571     // Get the first valid counter UID
2572     const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
2573     const Counters& counters                  = counterDirectory.GetCounters();
2574     BOOST_CHECK(counters.size() > 1);
2575     uint16_t counterUidA = counters.begin()->first;    // First valid counter UID
2576     uint16_t counterUidB = 9999;                       // Second invalid counter UID
2577
2578     uint32_t length = 8;
2579
2580     auto data = std::make_unique<unsigned char[]>(length);
2581     WriteUint32(data.get(), 0, capturePeriod);
2582     WriteUint16(data.get(), 4, counterUidA);
2583     WriteUint16(data.get(), 6, counterUidB);
2584
2585     // Create the Periodic Counter Selection packet
2586     Packet periodicCounterSelectionPacket(header, length, data);    // Length > 0, this will start the Period Counter
2587                                                                     // Capture thread
2588
2589     // Write the packet to the mock profiling connection
2590     mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2591
2592     // Expecting one Periodic Counter Selection packet and at least one Periodic Counter Capture packet
2593     int expectedPackets = 2;
2594     std::vector<uint32_t> receivedPackets;
2595
2596     // Keep waiting until all the expected packets have been received
2597     do
2598     {
2599         helper.WaitForProfilingPacketsSent();
2600         const std::vector<uint32_t> writtenData = mockProfilingConnection->GetWrittenData();
2601         if (writtenData.empty())
2602         {
2603             BOOST_ERROR("Packets should be available for reading at this point");
2604             return;
2605         }
2606         receivedPackets.insert(receivedPackets.end(), writtenData.begin(), writtenData.end());
2607         expectedPackets -= boost::numeric_cast<int>(writtenData.size());
2608     } while (expectedPackets > 0);
2609     BOOST_TEST(!receivedPackets.empty());
2610
2611     // The size of the expected Periodic Counter Selection packet
2612     BOOST_TEST((std::find(receivedPackets.begin(), receivedPackets.end(), 14) != receivedPackets.end()));
2613     // The size of the expected Periodic Counter Capture packet
2614     BOOST_TEST((std::find(receivedPackets.begin(), receivedPackets.end(), 22) != receivedPackets.end()));
2615
2616     // The Periodic Counter Selection Handler should not have updated the profiling state
2617     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2618
2619     // Reset the profiling service to stop any running thread
2620     options.m_EnableProfiling = false;
2621     profilingService.ResetExternalProfilingOptions(options, true);
2622 }
2623
2624 BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodPeriodicCounterSelectionPacketNoCounters)
2625 {
2626     // Swap the profiling connection factory in the profiling service instance with our mock one
2627     SwapProfilingConnectionFactoryHelper helper;
2628
2629     // Reset the profiling service to the uninitialized state
2630     armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
2631     options.m_EnableProfiling          = true;
2632     ProfilingService& profilingService = ProfilingService::Instance();
2633     profilingService.ResetExternalProfilingOptions(options, true);
2634
2635     // Bring the profiling service to the "Active" state
2636     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
2637     profilingService.Update();    // Initialize the counter directory
2638     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
2639     profilingService.Update();    // Create the profiling connection
2640     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
2641     profilingService.Update();    // Start the command handler and the send thread
2642
2643     // Wait for the Stream Metadata packet the be sent
2644     // (we are not testing the connection acknowledgement here so it will be ignored by this test)
2645     helper.WaitForProfilingPacketsSent();
2646
2647     // Force the profiling service to the "Active" state
2648     helper.ForceTransitionToState(ProfilingState::Active);
2649     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2650
2651     // Get the mock profiling connection
2652     MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2653     BOOST_CHECK(mockProfilingConnection);
2654
2655     // Remove the packets received so far
2656     mockProfilingConnection->Clear();
2657
2658     // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2659     // external profiling service
2660
2661     // Periodic Counter Selection packet header:
2662     // 26:31 [6]  packet_family: Control Packet Family, value 0b000000
2663     // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2664     // 8:15  [8]  reserved: Reserved, value 0b00000000
2665     // 0:7   [8]  reserved: Reserved, value 0b00000000
2666     uint32_t packetFamily = 0;
2667     uint32_t packetId     = 4;
2668     uint32_t header       = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
2669
2670     // Create the Periodic Counter Selection packet
2671     Packet periodicCounterSelectionPacket(header);    // Length == 0, this will disable the collection of counters
2672
2673     // Write the packet to the mock profiling connection
2674     mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2675
2676     // Wait for the Periodic Counter Selection packet to be sent
2677     helper.WaitForProfilingPacketsSent();
2678
2679     // The Periodic Counter Selection Handler should not have updated the profiling state
2680     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2681
2682     // Check that the mock profiling connection contains one Periodic Counter Selection
2683     const std::vector<uint32_t> writtenData = mockProfilingConnection->GetWrittenData();
2684     BOOST_TEST(writtenData.size() == 1);    // Only one packet is expected (no Periodic Counter packets)
2685     BOOST_TEST(writtenData[0] == 12);       // The size of the expected Periodic Counter Selection (echos the sent one)
2686
2687     // Reset the profiling service to stop any running thread
2688     options.m_EnableProfiling = false;
2689     profilingService.ResetExternalProfilingOptions(options, true);
2690 }
2691
2692 BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodPeriodicCounterSelectionPacketSingleCounter)
2693 {
2694     // Swap the profiling connection factory in the profiling service instance with our mock one
2695     SwapProfilingConnectionFactoryHelper helper;
2696
2697     // Reset the profiling service to the uninitialized state
2698     armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
2699     options.m_EnableProfiling          = true;
2700     ProfilingService& profilingService = ProfilingService::Instance();
2701     profilingService.ResetExternalProfilingOptions(options, true);
2702
2703     // Bring the profiling service to the "Active" state
2704     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
2705     profilingService.Update();    // Initialize the counter directory
2706     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
2707     profilingService.Update();    // Create the profiling connection
2708     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
2709     profilingService.Update();    // Start the command handler and the send thread
2710
2711     // Wait for the Stream Metadata packet the be sent
2712     // (we are not testing the connection acknowledgement here so it will be ignored by this test)
2713     helper.WaitForProfilingPacketsSent();
2714
2715     // Force the profiling service to the "Active" state
2716     helper.ForceTransitionToState(ProfilingState::Active);
2717     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2718
2719     // Get the mock profiling connection
2720     MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2721     BOOST_CHECK(mockProfilingConnection);
2722
2723     // Remove the packets received so far
2724     mockProfilingConnection->Clear();
2725
2726     // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2727     // external profiling service
2728
2729     // Periodic Counter Selection packet header:
2730     // 26:31 [6]  packet_family: Control Packet Family, value 0b000000
2731     // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2732     // 8:15  [8]  reserved: Reserved, value 0b00000000
2733     // 0:7   [8]  reserved: Reserved, value 0b00000000
2734     uint32_t packetFamily = 0;
2735     uint32_t packetId     = 4;
2736     uint32_t header       = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
2737
2738     uint32_t capturePeriod = 123456;    // Some capture period (microseconds)
2739
2740     // Get the first valid counter UID
2741     const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
2742     const Counters& counters                  = counterDirectory.GetCounters();
2743     BOOST_CHECK(!counters.empty());
2744     uint16_t counterUid = counters.begin()->first;    // Valid counter UID
2745
2746     uint32_t length = 6;
2747
2748     auto data = std::make_unique<unsigned char[]>(length);
2749     WriteUint32(data.get(), 0, capturePeriod);
2750     WriteUint16(data.get(), 4, counterUid);
2751
2752     // Create the Periodic Counter Selection packet
2753     Packet periodicCounterSelectionPacket(header, length, data);    // Length > 0, this will start the Period Counter
2754                                                                     // Capture thread
2755
2756     // Write the packet to the mock profiling connection
2757     mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2758
2759     // Expecting one Periodic Counter Selection packet and at least one Periodic Counter Capture packet
2760     int expectedPackets = 2;
2761     std::vector<uint32_t> receivedPackets;
2762
2763     // Keep waiting until all the expected packets have been received
2764     do
2765     {
2766         helper.WaitForProfilingPacketsSent();
2767         const std::vector<uint32_t> writtenData = mockProfilingConnection->GetWrittenData();
2768         if (writtenData.empty())
2769         {
2770             BOOST_ERROR("Packets should be available for reading at this point");
2771             return;
2772         }
2773         receivedPackets.insert(receivedPackets.end(), writtenData.begin(), writtenData.end());
2774         expectedPackets -= boost::numeric_cast<int>(writtenData.size());
2775     } while (expectedPackets > 0);
2776     BOOST_TEST(!receivedPackets.empty());
2777
2778     // The size of the expected Periodic Counter Selection packet (echos the sent one)
2779     BOOST_TEST((std::find(receivedPackets.begin(), receivedPackets.end(), 14) != receivedPackets.end()));
2780     // The size of the expected Periodic Counter Capture packet
2781     BOOST_TEST((std::find(receivedPackets.begin(), receivedPackets.end(), 22) != receivedPackets.end()));
2782
2783     // The Periodic Counter Selection Handler should not have updated the profiling state
2784     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2785
2786     // Reset the profiling service to stop any running thread
2787     options.m_EnableProfiling = false;
2788     profilingService.ResetExternalProfilingOptions(options, true);
2789 }
2790
2791 BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodPeriodicCounterSelectionPacketMultipleCounters)
2792 {
2793     // Swap the profiling connection factory in the profiling service instance with our mock one
2794     SwapProfilingConnectionFactoryHelper helper;
2795
2796     // Reset the profiling service to the uninitialized state
2797     armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
2798     options.m_EnableProfiling          = true;
2799     ProfilingService& profilingService = ProfilingService::Instance();
2800     profilingService.ResetExternalProfilingOptions(options, true);
2801
2802     // Bring the profiling service to the "Active" state
2803     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
2804     profilingService.Update();    // Initialize the counter directory
2805     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
2806     profilingService.Update();    // Create the profiling connection
2807     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
2808     profilingService.Update();    // Start the command handler and the send thread
2809
2810     // Wait for the Stream Metadata packet the be sent
2811     // (we are not testing the connection acknowledgement here so it will be ignored by this test)
2812     helper.WaitForProfilingPacketsSent();
2813
2814     // Force the profiling service to the "Active" state
2815     helper.ForceTransitionToState(ProfilingState::Active);
2816     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2817
2818     // Get the mock profiling connection
2819     MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2820     BOOST_CHECK(mockProfilingConnection);
2821
2822     // Remove the packets received so far
2823     mockProfilingConnection->Clear();
2824
2825     // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2826     // external profiling service
2827
2828     // Periodic Counter Selection packet header:
2829     // 26:31 [6]  packet_family: Control Packet Family, value 0b000000
2830     // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2831     // 8:15  [8]  reserved: Reserved, value 0b00000000
2832     // 0:7   [8]  reserved: Reserved, value 0b00000000
2833     uint32_t packetFamily = 0;
2834     uint32_t packetId     = 4;
2835     uint32_t header       = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
2836
2837     uint32_t capturePeriod = 123456;    // Some capture period (microseconds)
2838
2839     // Get the first valid counter UID
2840     const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
2841     const Counters& counters                  = counterDirectory.GetCounters();
2842     BOOST_CHECK(counters.size() > 1);
2843     uint16_t counterUidA = counters.begin()->first;        // First valid counter UID
2844     uint16_t counterUidB = (counters.begin()++)->first;    // Second valid counter UID
2845
2846     uint32_t length = 8;
2847
2848     auto data = std::make_unique<unsigned char[]>(length);
2849     WriteUint32(data.get(), 0, capturePeriod);
2850     WriteUint16(data.get(), 4, counterUidA);
2851     WriteUint16(data.get(), 6, counterUidB);
2852
2853     // Create the Periodic Counter Selection packet
2854     Packet periodicCounterSelectionPacket(header, length, data);    // Length > 0, this will start the Period Counter
2855                                                                     // Capture thread
2856
2857     // Write the packet to the mock profiling connection
2858     mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2859
2860     // Expecting one Periodic Counter Selection packet and at least one Periodic Counter Capture packet
2861     int expectedPackets = 2;
2862     std::vector<uint32_t> receivedPackets;
2863
2864     // Keep waiting until all the expected packets have been received
2865     do
2866     {
2867         helper.WaitForProfilingPacketsSent();
2868         const std::vector<uint32_t> writtenData = mockProfilingConnection->GetWrittenData();
2869         if (writtenData.empty())
2870         {
2871             BOOST_ERROR("Packets should be available for reading at this point");
2872             return;
2873         }
2874         receivedPackets.insert(receivedPackets.end(), writtenData.begin(), writtenData.end());
2875         expectedPackets -= boost::numeric_cast<int>(writtenData.size());
2876     } while (expectedPackets > 0);
2877     BOOST_TEST(!receivedPackets.empty());
2878
2879     // The size of the expected Periodic Counter Selection packet (echos the sent one)
2880     BOOST_TEST((std::find(receivedPackets.begin(), receivedPackets.end(), 16) != receivedPackets.end()));
2881     // The size of the expected Periodic Counter Capture packet
2882     BOOST_TEST((std::find(receivedPackets.begin(), receivedPackets.end(), 28) != receivedPackets.end()));
2883
2884     // The Periodic Counter Selection Handler should not have updated the profiling state
2885     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2886
2887     // Reset the profiling service to stop any running thread
2888     options.m_EnableProfiling = false;
2889     profilingService.ResetExternalProfilingOptions(options, true);
2890 }
2891
2892 BOOST_AUTO_TEST_CASE(CheckProfilingServiceDisconnect)
2893 {
2894     // Swap the profiling connection factory in the profiling service instance with our mock one
2895     SwapProfilingConnectionFactoryHelper helper;
2896
2897     // Reset the profiling service to the uninitialized state
2898     armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
2899     options.m_EnableProfiling          = true;
2900     ProfilingService& profilingService = ProfilingService::Instance();
2901     profilingService.ResetExternalProfilingOptions(options, true);
2902
2903     // Try to disconnect the profiling service while in the "Uninitialised" state
2904     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
2905     profilingService.Disconnect();
2906     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);    // The state should not change
2907
2908     // Try to disconnect the profiling service while in the "NotConnected" state
2909     profilingService.Update();    // Initialize the counter directory
2910     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
2911     profilingService.Disconnect();
2912     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);    // The state should not change
2913
2914     // Try to disconnect the profiling service while in the "WaitingForAck" state
2915     profilingService.Update();    // Create the profiling connection
2916     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
2917     profilingService.Disconnect();
2918     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);    // The state should not change
2919
2920     // Try to disconnect the profiling service while in the "Active" state
2921     profilingService.Update();    // Start the command handler and the send thread
2922
2923     // Wait for the Stream Metadata packet the be sent
2924     // (we are not testing the connection acknowledgement here so it will be ignored by this test)
2925     helper.WaitForProfilingPacketsSent();
2926
2927     // Force the profiling service to the "Active" state
2928     helper.ForceTransitionToState(ProfilingState::Active);
2929     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2930
2931     // Get the mock profiling connection
2932     MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2933     BOOST_CHECK(mockProfilingConnection);
2934
2935     // Check that the profiling connection is open
2936     BOOST_CHECK(mockProfilingConnection->IsOpen());
2937
2938     profilingService.Disconnect();
2939     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);    // The state should have changed
2940
2941     // Check that the profiling connection has been reset
2942     mockProfilingConnection = helper.GetMockProfilingConnection();
2943     BOOST_CHECK(mockProfilingConnection == nullptr);
2944
2945     // Reset the profiling service to stop any running thread
2946     options.m_EnableProfiling = false;
2947     profilingService.ResetExternalProfilingOptions(options, true);
2948 }
2949
2950 BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodPerJobCounterSelectionPacket)
2951 {
2952     // Swap the profiling connection factory in the profiling service instance with our mock one
2953     SwapProfilingConnectionFactoryHelper helper;
2954
2955     // Reset the profiling service to the uninitialized state
2956     armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
2957     options.m_EnableProfiling          = true;
2958     ProfilingService& profilingService = ProfilingService::Instance();
2959     profilingService.ResetExternalProfilingOptions(options, true);
2960
2961     // Bring the profiling service to the "Active" state
2962     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
2963     profilingService.Update();    // Initialize the counter directory
2964     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
2965     profilingService.Update();    // Create the profiling connection
2966     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
2967     profilingService.Update();    // Start the command handler and the send thread
2968
2969     // Wait for the Stream Metadata packet the be sent
2970     // (we are not testing the connection acknowledgement here so it will be ignored by this test)
2971     helper.WaitForProfilingPacketsSent();
2972
2973     // Force the profiling service to the "Active" state
2974     helper.ForceTransitionToState(ProfilingState::Active);
2975     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2976
2977     // Get the mock profiling connection
2978     MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2979     BOOST_CHECK(mockProfilingConnection);
2980
2981     // Remove the packets received so far
2982     mockProfilingConnection->Clear();
2983
2984     // Write a "Per-Job Counter Selection" packet into the mock profiling connection, to simulate an input from an
2985     // external profiling service
2986
2987     // Per-Job Counter Selection packet header:
2988     // 26:31 [6]  packet_family: Control Packet Family, value 0b000000
2989     // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2990     // 8:15  [8]  reserved: Reserved, value 0b00000000
2991     // 0:7   [8]  reserved: Reserved, value 0b00000000
2992     uint32_t packetFamily = 0;
2993     uint32_t packetId     = 5;
2994     uint32_t header       = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
2995
2996     // Create the Per-Job Counter Selection packet
2997     Packet periodicCounterSelectionPacket(header);    // Length == 0, this will disable the collection of counters
2998
2999     // Write the packet to the mock profiling connection
3000     mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
3001
3002     // Wait for a bit (must at least be the delay value of the mock profiling connection) to make sure that
3003     // the Per-Job Counter Selection packet gets processed by the profiling service
3004     std::this_thread::sleep_for(std::chrono::seconds(2));
3005
3006     // The Per-Job Counter Selection packets are dropped silently, so there should be no reply coming
3007     // from the profiling service
3008     const std::vector<uint32_t> writtenData = mockProfilingConnection->GetWrittenData();
3009     BOOST_TEST(writtenData.empty());
3010
3011     // The Per-Job Counter Selection Command Handler should not have updated the profiling state
3012     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
3013
3014     // Reset the profiling service to stop any running thread
3015     options.m_EnableProfiling = false;
3016     profilingService.ResetExternalProfilingOptions(options, true);
3017 }
3018
3019 BOOST_AUTO_TEST_CASE(CheckConfigureProfilingServiceOn)
3020 {
3021     armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
3022     options.m_EnableProfiling          = true;
3023     ProfilingService& profilingService = ProfilingService::Instance();
3024     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3025     profilingService.ConfigureProfilingService(options);
3026     // should get as far as NOT_CONNECTED
3027     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
3028     // Reset the profiling service to stop any running thread
3029     options.m_EnableProfiling = false;
3030     profilingService.ResetExternalProfilingOptions(options, true);
3031 }
3032
3033 BOOST_AUTO_TEST_CASE(CheckConfigureProfilingServiceOff)
3034 {
3035     armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
3036     ProfilingService& profilingService = ProfilingService::Instance();
3037     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3038     profilingService.ConfigureProfilingService(options);
3039     // should not move from Uninitialised
3040     BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3041     // Reset the profiling service to stop any running thread
3042     options.m_EnableProfiling = false;
3043     profilingService.ResetExternalProfilingOptions(options, true);
3044 }
3045
3046 BOOST_AUTO_TEST_SUITE_END()