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