blob: 79a241b5404c186092985415d1a01c2194a1cc52 [file] [log] [blame]
Francis Murtagh1f7db452019-08-14 09:49:34 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
Matteo Martincighd0613b52019-10-09 16:47:04 +01006#include "ProfilingTests.hpp"
Teresa Charlin9bab4962019-09-06 12:28:35 +01007
Matteo Martincigh8a837172019-10-04 17:01:07 +01008#include <CommandHandler.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +01009#include <CommandHandlerKey.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010010#include <CommandHandlerRegistry.hpp>
Sadik Armaganb5f01b22019-09-18 17:29:00 +010011#include <ConnectionAcknowledgedCommandHandler.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010012#include <CounterDirectory.hpp>
13#include <EncodeVersion.hpp>
14#include <Holder.hpp>
Matteo Martincighe0e6efc2019-10-04 17:17:42 +010015#include <ICounterValues.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010016#include <Packet.hpp>
17#include <PacketVersionResolver.hpp>
Francis Murtaghfcb8ef62019-09-20 15:40:09 +010018#include <PeriodicCounterCapture.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010019#include <PeriodicCounterSelectionCommandHandler.hpp>
20#include <ProfilingStateMachine.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010021#include <ProfilingUtils.hpp>
Narumol Prangnawarat48033692019-09-20 12:04:55 +010022#include <RequestCounterDirectoryCommandHandler.hpp>
Teresa Charlin9bab4962019-09-06 12:28:35 +010023#include <Runtime.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010024#include <SocketProfilingConnection.hpp>
Keith Davis02356de2019-08-26 18:28:17 +010025
Matteo Martincigh6db5f202019-09-05 12:02:04 +010026#include <armnn/Conversion.hpp>
Ferran Balaguer1b941722019-08-28 16:57:18 +010027
Matteo Martincigh54fb9572019-10-02 12:50:57 +010028#include <armnn/Utils.hpp>
29
Sadik Armaganbd9e2c52019-09-26 23:13:31 +010030#include <boost/algorithm/string.hpp>
Ferran Balaguer1b941722019-08-28 16:57:18 +010031#include <boost/numeric/conversion/cast.hpp>
Francis Murtagh1f7db452019-08-14 09:49:34 +010032
Nikhil Rajbc626052019-08-15 15:49:45 +010033#include <cstdint>
34#include <cstring>
Keith Davis3201eea2019-10-24 17:30:41 +010035#include <iostream>
Aron Virginas-Tare898db92019-08-22 12:56:34 +010036#include <limits>
Francis Murtagh11f99b42019-08-16 11:28:52 +010037#include <map>
Aron Virginas-Tare898db92019-08-22 12:56:34 +010038#include <random>
Francis Murtagh1f7db452019-08-14 09:49:34 +010039
Aron Virginas-Tare898db92019-08-22 12:56:34 +010040using namespace armnn::profiling;
41
Matteo Martincigh8a837172019-10-04 17:01:07 +010042BOOST_AUTO_TEST_SUITE(ExternalProfiling)
43
Francis Murtagh1f7db452019-08-14 09:49:34 +010044BOOST_AUTO_TEST_CASE(CheckCommandHandlerKeyComparisons)
45{
Jim Flynn397043f2019-10-17 17:37:10 +010046 CommandHandlerKey testKey1_0(1, 1, 1);
47 CommandHandlerKey testKey1_1(1, 1, 1);
48 CommandHandlerKey testKey1_2(1, 2, 1);
49
50 CommandHandlerKey testKey0(0, 1, 1);
51 CommandHandlerKey testKey1(0, 1, 1);
52 CommandHandlerKey testKey2(0, 1, 1);
53 CommandHandlerKey testKey3(0, 0, 0);
54 CommandHandlerKey testKey4(0, 2, 2);
55 CommandHandlerKey testKey5(0, 0, 2);
56
57 BOOST_CHECK(testKey1_0 > testKey0);
58 BOOST_CHECK(testKey1_0 == testKey1_1);
59 BOOST_CHECK(testKey1_0 < testKey1_2);
Francis Murtagh1f7db452019-08-14 09:49:34 +010060
Keith Davis3201eea2019-10-24 17:30:41 +010061 BOOST_CHECK(testKey1 < testKey4);
62 BOOST_CHECK(testKey1 > testKey3);
63 BOOST_CHECK(testKey1 <= testKey4);
64 BOOST_CHECK(testKey1 >= testKey3);
65 BOOST_CHECK(testKey1 <= testKey2);
66 BOOST_CHECK(testKey1 >= testKey2);
67 BOOST_CHECK(testKey1 == testKey2);
68 BOOST_CHECK(testKey1 == testKey1);
Francis Murtagh1f7db452019-08-14 09:49:34 +010069
Keith Davis3201eea2019-10-24 17:30:41 +010070 BOOST_CHECK(!(testKey1 == testKey5));
71 BOOST_CHECK(!(testKey1 != testKey1));
72 BOOST_CHECK(testKey1 != testKey5);
Francis Murtagh1f7db452019-08-14 09:49:34 +010073
Keith Davis3201eea2019-10-24 17:30:41 +010074 BOOST_CHECK(testKey1 == testKey2 && testKey2 == testKey1);
75 BOOST_CHECK(testKey0 == testKey1 && testKey1 == testKey2 && testKey0 == testKey2);
Francis Murtagh1f7db452019-08-14 09:49:34 +010076
Keith Davis3201eea2019-10-24 17:30:41 +010077 BOOST_CHECK(testKey1.GetPacketId() == 1);
78 BOOST_CHECK(testKey1.GetVersion() == 1);
Francis Murtagh1f7db452019-08-14 09:49:34 +010079
Keith Davis3201eea2019-10-24 17:30:41 +010080 std::vector<CommandHandlerKey> vect = { CommandHandlerKey(0, 0, 1), CommandHandlerKey(0, 2, 0),
81 CommandHandlerKey(0, 1, 0), CommandHandlerKey(0, 2, 1),
82 CommandHandlerKey(0, 1, 1), CommandHandlerKey(0, 0, 1),
83 CommandHandlerKey(0, 2, 0), CommandHandlerKey(0, 0, 0) };
Francis Murtagh1f7db452019-08-14 09:49:34 +010084
85 std::sort(vect.begin(), vect.end());
86
Keith Davis3201eea2019-10-24 17:30:41 +010087 std::vector<CommandHandlerKey> expectedVect = { CommandHandlerKey(0, 0, 0), CommandHandlerKey(0, 0, 1),
88 CommandHandlerKey(0, 0, 1), CommandHandlerKey(0, 1, 0),
89 CommandHandlerKey(0, 1, 1), CommandHandlerKey(0, 2, 0),
90 CommandHandlerKey(0, 2, 0), CommandHandlerKey(0, 2, 1) };
Francis Murtagh1f7db452019-08-14 09:49:34 +010091
92 BOOST_CHECK(vect == expectedVect);
93}
94
Jim Flynned25e0e2019-10-18 13:21:43 +010095BOOST_AUTO_TEST_CASE(CheckPacketKeyComparisons)
96{
Keith Davis3201eea2019-10-24 17:30:41 +010097 PacketKey key0(0, 0);
98 PacketKey key1(0, 0);
99 PacketKey key2(0, 1);
100 PacketKey key3(0, 2);
101 PacketKey key4(1, 0);
102 PacketKey key5(1, 0);
103 PacketKey key6(1, 1);
Jim Flynned25e0e2019-10-18 13:21:43 +0100104
105 BOOST_CHECK(!(key0 < key1));
106 BOOST_CHECK(!(key0 > key1));
107 BOOST_CHECK(key0 <= key1);
108 BOOST_CHECK(key0 >= key1);
109 BOOST_CHECK(key0 == key1);
110 BOOST_CHECK(key0 < key2);
111 BOOST_CHECK(key2 < key3);
112 BOOST_CHECK(key3 > key0);
113 BOOST_CHECK(key4 == key5);
114 BOOST_CHECK(key4 > key0);
115 BOOST_CHECK(key5 < key6);
116 BOOST_CHECK(key5 <= key6);
117 BOOST_CHECK(key5 != key6);
118}
119
Matteo Martincigh8a837172019-10-04 17:01:07 +0100120BOOST_AUTO_TEST_CASE(CheckCommandHandler)
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100121{
Matteo Martincigh8a837172019-10-04 17:01:07 +0100122 PacketVersionResolver packetVersionResolver;
123 ProfilingStateMachine profilingStateMachine;
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100124
Matteo Martincigh8a837172019-10-04 17:01:07 +0100125 TestProfilingConnectionBase testProfilingConnectionBase;
126 TestProfilingConnectionTimeoutError testProfilingConnectionTimeOutError;
127 TestProfilingConnectionArmnnError testProfilingConnectionArmnnError;
Keith Davis3201eea2019-10-24 17:30:41 +0100128 CounterDirectory counterDirectory;
129 MockBufferManager mockBuffer(1024);
130 SendCounterPacket sendCounterPacket(profilingStateMachine, mockBuffer);
131 ConnectionAcknowledgedCommandHandler connectionAcknowledgedCommandHandler(0, 1, 4194304, counterDirectory,
132 sendCounterPacket, profilingStateMachine);
Matteo Martincigh8a837172019-10-04 17:01:07 +0100133 CommandHandlerRegistry commandHandlerRegistry;
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100134
Matteo Martincighc2728f92019-10-07 12:35:21 +0100135 commandHandlerRegistry.RegisterFunctor(&connectionAcknowledgedCommandHandler);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100136
Matteo Martincigh8a837172019-10-04 17:01:07 +0100137 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
138 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100139
Keith Davis3201eea2019-10-24 17:30:41 +0100140 CommandHandler commandHandler0(1, true, commandHandlerRegistry, packetVersionResolver);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100141
Matteo Martincigh8a837172019-10-04 17:01:07 +0100142 commandHandler0.Start(testProfilingConnectionBase);
143 commandHandler0.Start(testProfilingConnectionBase);
144 commandHandler0.Start(testProfilingConnectionBase);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100145
Matteo Martincigh8a837172019-10-04 17:01:07 +0100146 commandHandler0.Stop();
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100147
Matteo Martincigh8a837172019-10-04 17:01:07 +0100148 BOOST_CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::Active);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100149
Matteo Martincigh8a837172019-10-04 17:01:07 +0100150 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
151 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
152 // commandHandler1 should give up after one timeout
Keith Davis3201eea2019-10-24 17:30:41 +0100153 CommandHandler commandHandler1(10, true, commandHandlerRegistry, packetVersionResolver);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100154
Matteo Martincigh8a837172019-10-04 17:01:07 +0100155 commandHandler1.Start(testProfilingConnectionTimeOutError);
Matteo Martincigh88813932019-10-04 14:40:04 +0100156
Matteo Martincigh8a837172019-10-04 17:01:07 +0100157 std::this_thread::sleep_for(std::chrono::milliseconds(100));
Matteo Martincigh88813932019-10-04 14:40:04 +0100158
Matteo Martincigh8a837172019-10-04 17:01:07 +0100159 BOOST_CHECK(!commandHandler1.IsRunning());
160 commandHandler1.Stop();
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100161
Matteo Martincigh8a837172019-10-04 17:01:07 +0100162 BOOST_CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::WaitingForAck);
163 // Now commandHandler1 should persist after a timeout
164 commandHandler1.SetStopAfterTimeout(false);
165 commandHandler1.Start(testProfilingConnectionTimeOutError);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100166
Matteo Martincigh8a837172019-10-04 17:01:07 +0100167 for (int i = 0; i < 100; i++)
168 {
169 if (profilingStateMachine.GetCurrentState() == ProfilingState::Active)
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100170 {
Matteo Martincigh8a837172019-10-04 17:01:07 +0100171 break;
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100172 }
173
Matteo Martincighd0613b52019-10-09 16:47:04 +0100174 std::this_thread::sleep_for(std::chrono::milliseconds(100));
Matteo Martincigh8a837172019-10-04 17:01:07 +0100175 }
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100176
Matteo Martincigh8a837172019-10-04 17:01:07 +0100177 commandHandler1.Stop();
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100178
Matteo Martincigh8a837172019-10-04 17:01:07 +0100179 BOOST_CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::Active);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100180
Keith Davis3201eea2019-10-24 17:30:41 +0100181 CommandHandler commandHandler2(100, false, commandHandlerRegistry, packetVersionResolver);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100182
Matteo Martincigh8a837172019-10-04 17:01:07 +0100183 commandHandler2.Start(testProfilingConnectionArmnnError);
184
Matteo Martincighd0613b52019-10-09 16:47:04 +0100185 // commandHandler2 should not stop once it encounters a non timing error
186 std::this_thread::sleep_for(std::chrono::milliseconds(500));
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100187
Matteo Martincighd0613b52019-10-09 16:47:04 +0100188 BOOST_CHECK(commandHandler2.IsRunning());
Matteo Martincigh8a837172019-10-04 17:01:07 +0100189 commandHandler2.Stop();
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100190}
191
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100192BOOST_AUTO_TEST_CASE(CheckEncodeVersion)
193{
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100194 Version version1(12);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100195
196 BOOST_CHECK(version1.GetMajor() == 0);
197 BOOST_CHECK(version1.GetMinor() == 0);
198 BOOST_CHECK(version1.GetPatch() == 12);
199
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100200 Version version2(4108);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100201
202 BOOST_CHECK(version2.GetMajor() == 0);
203 BOOST_CHECK(version2.GetMinor() == 1);
204 BOOST_CHECK(version2.GetPatch() == 12);
205
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100206 Version version3(4198412);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100207
208 BOOST_CHECK(version3.GetMajor() == 1);
209 BOOST_CHECK(version3.GetMinor() == 1);
210 BOOST_CHECK(version3.GetPatch() == 12);
211
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100212 Version version4(0);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100213
214 BOOST_CHECK(version4.GetMajor() == 0);
215 BOOST_CHECK(version4.GetMinor() == 0);
216 BOOST_CHECK(version4.GetPatch() == 0);
217
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100218 Version version5(1, 0, 0);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100219 BOOST_CHECK(version5.GetEncodedValue() == 4194304);
220}
221
Nikhil Rajbc626052019-08-15 15:49:45 +0100222BOOST_AUTO_TEST_CASE(CheckPacketClass)
223{
Keith Davis3201eea2019-10-24 17:30:41 +0100224 uint32_t length = 4;
Matteo Martincigh67ef2a52019-10-10 13:29:02 +0100225 std::unique_ptr<unsigned char[]> packetData0 = std::make_unique<unsigned char[]>(length);
226 std::unique_ptr<unsigned char[]> packetData1 = std::make_unique<unsigned char[]>(0);
227 std::unique_ptr<unsigned char[]> nullPacketData;
Nikhil Rajbc626052019-08-15 15:49:45 +0100228
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100229 Packet packetTest0(472580096, length, packetData0);
Nikhil Rajbc626052019-08-15 15:49:45 +0100230
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100231 BOOST_CHECK(packetTest0.GetHeader() == 472580096);
232 BOOST_CHECK(packetTest0.GetPacketFamily() == 7);
233 BOOST_CHECK(packetTest0.GetPacketId() == 43);
234 BOOST_CHECK(packetTest0.GetLength() == length);
235 BOOST_CHECK(packetTest0.GetPacketType() == 3);
236 BOOST_CHECK(packetTest0.GetPacketClass() == 5);
Nikhil Rajbc626052019-08-15 15:49:45 +0100237
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100238 BOOST_CHECK_THROW(Packet packetTest1(472580096, 0, packetData1), armnn::Exception);
239 BOOST_CHECK_NO_THROW(Packet packetTest2(472580096, 0, nullPacketData));
Nikhil Rajbc626052019-08-15 15:49:45 +0100240
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100241 Packet packetTest3(472580096, 0, nullPacketData);
242 BOOST_CHECK(packetTest3.GetLength() == 0);
243 BOOST_CHECK(packetTest3.GetData() == nullptr);
244
Matteo Martincigh67ef2a52019-10-10 13:29:02 +0100245 const unsigned char* packetTest0Data = packetTest0.GetData();
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100246 Packet packetTest4(std::move(packetTest0));
247
248 BOOST_CHECK(packetTest0.GetData() == nullptr);
249 BOOST_CHECK(packetTest4.GetData() == packetTest0Data);
250
251 BOOST_CHECK(packetTest4.GetHeader() == 472580096);
252 BOOST_CHECK(packetTest4.GetPacketFamily() == 7);
253 BOOST_CHECK(packetTest4.GetPacketId() == 43);
254 BOOST_CHECK(packetTest4.GetLength() == length);
255 BOOST_CHECK(packetTest4.GetPacketType() == 3);
256 BOOST_CHECK(packetTest4.GetPacketClass() == 5);
Nikhil Rajbc626052019-08-15 15:49:45 +0100257}
258
Francis Murtagh11f99b42019-08-16 11:28:52 +0100259BOOST_AUTO_TEST_CASE(CheckCommandHandlerFunctor)
260{
Francis Murtagh11f99b42019-08-16 11:28:52 +0100261 // Hard code the version as it will be the same during a single profiling session
262 uint32_t version = 1;
263
Jim Flynn397043f2019-10-17 17:37:10 +0100264 TestFunctorA testFunctorA(7, 461, version);
265 TestFunctorB testFunctorB(8, 963, version);
266 TestFunctorC testFunctorC(5, 983, version);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100267
Jim Flynn397043f2019-10-17 17:37:10 +0100268 CommandHandlerKey keyA(testFunctorA.GetFamilyId(), testFunctorA.GetPacketId(), testFunctorA.GetVersion());
269 CommandHandlerKey keyB(testFunctorB.GetFamilyId(), testFunctorB.GetPacketId(), testFunctorB.GetVersion());
270 CommandHandlerKey keyC(testFunctorC.GetFamilyId(), testFunctorC.GetPacketId(), testFunctorC.GetVersion());
Francis Murtagh11f99b42019-08-16 11:28:52 +0100271
272 // Create the unwrapped map to simulate the Command Handler Registry
273 std::map<CommandHandlerKey, CommandHandlerFunctor*> registry;
274
275 registry.insert(std::make_pair(keyB, &testFunctorB));
276 registry.insert(std::make_pair(keyA, &testFunctorA));
277 registry.insert(std::make_pair(keyC, &testFunctorC));
278
279 // Check the order of the map is correct
280 auto it = registry.begin();
Keith Davis3201eea2019-10-24 17:30:41 +0100281 BOOST_CHECK(it->first == keyC); // familyId == 5
Francis Murtagh11f99b42019-08-16 11:28:52 +0100282 it++;
Keith Davis3201eea2019-10-24 17:30:41 +0100283 BOOST_CHECK(it->first == keyA); // familyId == 7
Francis Murtagh11f99b42019-08-16 11:28:52 +0100284 it++;
Keith Davis3201eea2019-10-24 17:30:41 +0100285 BOOST_CHECK(it->first == keyB); // familyId == 8
Francis Murtagh11f99b42019-08-16 11:28:52 +0100286
Matteo Martincigh67ef2a52019-10-10 13:29:02 +0100287 std::unique_ptr<unsigned char[]> packetDataA;
288 std::unique_ptr<unsigned char[]> packetDataB;
289 std::unique_ptr<unsigned char[]> packetDataC;
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100290
291 Packet packetA(500000000, 0, packetDataA);
292 Packet packetB(600000000, 0, packetDataB);
293 Packet packetC(400000000, 0, packetDataC);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100294
295 // Check the correct operator of derived class is called
Jim Flynn397043f2019-10-17 17:37:10 +0100296 registry.at(CommandHandlerKey(packetA.GetPacketFamily(), packetA.GetPacketId(), version))->operator()(packetA);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100297 BOOST_CHECK(testFunctorA.GetCount() == 1);
298 BOOST_CHECK(testFunctorB.GetCount() == 0);
299 BOOST_CHECK(testFunctorC.GetCount() == 0);
300
Jim Flynn397043f2019-10-17 17:37:10 +0100301 registry.at(CommandHandlerKey(packetB.GetPacketFamily(), packetB.GetPacketId(), version))->operator()(packetB);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100302 BOOST_CHECK(testFunctorA.GetCount() == 1);
303 BOOST_CHECK(testFunctorB.GetCount() == 1);
304 BOOST_CHECK(testFunctorC.GetCount() == 0);
305
Jim Flynn397043f2019-10-17 17:37:10 +0100306 registry.at(CommandHandlerKey(packetC.GetPacketFamily(), packetC.GetPacketId(), version))->operator()(packetC);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100307 BOOST_CHECK(testFunctorA.GetCount() == 1);
308 BOOST_CHECK(testFunctorB.GetCount() == 1);
309 BOOST_CHECK(testFunctorC.GetCount() == 1);
310}
311
Francis Murtagh94d79152019-08-16 17:45:07 +0100312BOOST_AUTO_TEST_CASE(CheckCommandHandlerRegistry)
313{
314 // Hard code the version as it will be the same during a single profiling session
315 uint32_t version = 1;
316
Jim Flynn397043f2019-10-17 17:37:10 +0100317 TestFunctorA testFunctorA(7, 461, version);
318 TestFunctorB testFunctorB(8, 963, version);
319 TestFunctorC testFunctorC(5, 983, version);
Francis Murtagh94d79152019-08-16 17:45:07 +0100320
321 // Create the Command Handler Registry
322 CommandHandlerRegistry registry;
323
324 // Register multiple different derived classes
Matteo Martincighc2728f92019-10-07 12:35:21 +0100325 registry.RegisterFunctor(&testFunctorA);
326 registry.RegisterFunctor(&testFunctorB);
327 registry.RegisterFunctor(&testFunctorC);
Francis Murtagh94d79152019-08-16 17:45:07 +0100328
Matteo Martincigh67ef2a52019-10-10 13:29:02 +0100329 std::unique_ptr<unsigned char[]> packetDataA;
330 std::unique_ptr<unsigned char[]> packetDataB;
331 std::unique_ptr<unsigned char[]> packetDataC;
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100332
333 Packet packetA(500000000, 0, packetDataA);
334 Packet packetB(600000000, 0, packetDataB);
335 Packet packetC(400000000, 0, packetDataC);
Francis Murtagh94d79152019-08-16 17:45:07 +0100336
337 // Check the correct operator of derived class is called
Jim Flynn397043f2019-10-17 17:37:10 +0100338 registry.GetFunctor(packetA.GetPacketFamily(), packetA.GetPacketId(), version)->operator()(packetA);
Francis Murtagh94d79152019-08-16 17:45:07 +0100339 BOOST_CHECK(testFunctorA.GetCount() == 1);
340 BOOST_CHECK(testFunctorB.GetCount() == 0);
341 BOOST_CHECK(testFunctorC.GetCount() == 0);
342
Jim Flynn397043f2019-10-17 17:37:10 +0100343 registry.GetFunctor(packetB.GetPacketFamily(), packetB.GetPacketId(), version)->operator()(packetB);
Francis Murtagh94d79152019-08-16 17:45:07 +0100344 BOOST_CHECK(testFunctorA.GetCount() == 1);
345 BOOST_CHECK(testFunctorB.GetCount() == 1);
346 BOOST_CHECK(testFunctorC.GetCount() == 0);
347
Jim Flynn397043f2019-10-17 17:37:10 +0100348 registry.GetFunctor(packetC.GetPacketFamily(), packetC.GetPacketId(), version)->operator()(packetC);
Francis Murtagh94d79152019-08-16 17:45:07 +0100349 BOOST_CHECK(testFunctorA.GetCount() == 1);
350 BOOST_CHECK(testFunctorB.GetCount() == 1);
351 BOOST_CHECK(testFunctorC.GetCount() == 1);
352
353 // Re-register an existing key with a new function
Jim Flynn397043f2019-10-17 17:37:10 +0100354 registry.RegisterFunctor(&testFunctorC, testFunctorA.GetFamilyId(), testFunctorA.GetPacketId(), version);
355 registry.GetFunctor(packetA.GetPacketFamily(), packetA.GetPacketId(), version)->operator()(packetC);
Francis Murtagh94d79152019-08-16 17:45:07 +0100356 BOOST_CHECK(testFunctorA.GetCount() == 1);
357 BOOST_CHECK(testFunctorB.GetCount() == 1);
358 BOOST_CHECK(testFunctorC.GetCount() == 2);
359
360 // Check that non-existent key returns nullptr for its functor
Jim Flynn397043f2019-10-17 17:37:10 +0100361 BOOST_CHECK_THROW(registry.GetFunctor(0, 0, 0), armnn::Exception);
Francis Murtagh94d79152019-08-16 17:45:07 +0100362}
363
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100364BOOST_AUTO_TEST_CASE(CheckPacketVersionResolver)
365{
366 // Set up random number generator for generating packetId values
367 std::random_device device;
368 std::mt19937 generator(device());
369 std::uniform_int_distribution<uint32_t> distribution(std::numeric_limits<uint32_t>::min(),
370 std::numeric_limits<uint32_t>::max());
371
372 // NOTE: Expected version is always 1.0.0, regardless of packetId
373 const Version expectedVersion(1, 0, 0);
374
375 PacketVersionResolver packetVersionResolver;
376
377 constexpr unsigned int numTests = 10u;
378
379 for (unsigned int i = 0u; i < numTests; ++i)
380 {
Jim Flynned25e0e2019-10-18 13:21:43 +0100381 const uint32_t familyId = distribution(generator);
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100382 const uint32_t packetId = distribution(generator);
Jim Flynned25e0e2019-10-18 13:21:43 +0100383 Version resolvedVersion = packetVersionResolver.ResolvePacketVersion(familyId, packetId);
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100384
385 BOOST_TEST(resolvedVersion == expectedVersion);
386 }
387}
Matteo Martincighd0613b52019-10-09 16:47:04 +0100388
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100389void ProfilingCurrentStateThreadImpl(ProfilingStateMachine& states)
390{
391 ProfilingState newState = ProfilingState::NotConnected;
392 states.GetCurrentState();
393 states.TransitionToState(newState);
394}
395
396BOOST_AUTO_TEST_CASE(CheckProfilingStateMachine)
397{
398 ProfilingStateMachine profilingState1(ProfilingState::Uninitialised);
399 profilingState1.TransitionToState(ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +0100400 BOOST_CHECK(profilingState1.GetCurrentState() == ProfilingState::Uninitialised);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100401
402 ProfilingStateMachine profilingState2(ProfilingState::Uninitialised);
403 profilingState2.TransitionToState(ProfilingState::NotConnected);
404 BOOST_CHECK(profilingState2.GetCurrentState() == ProfilingState::NotConnected);
405
406 ProfilingStateMachine profilingState3(ProfilingState::NotConnected);
407 profilingState3.TransitionToState(ProfilingState::NotConnected);
408 BOOST_CHECK(profilingState3.GetCurrentState() == ProfilingState::NotConnected);
409
410 ProfilingStateMachine profilingState4(ProfilingState::NotConnected);
411 profilingState4.TransitionToState(ProfilingState::WaitingForAck);
412 BOOST_CHECK(profilingState4.GetCurrentState() == ProfilingState::WaitingForAck);
413
414 ProfilingStateMachine profilingState5(ProfilingState::WaitingForAck);
415 profilingState5.TransitionToState(ProfilingState::WaitingForAck);
416 BOOST_CHECK(profilingState5.GetCurrentState() == ProfilingState::WaitingForAck);
417
418 ProfilingStateMachine profilingState6(ProfilingState::WaitingForAck);
419 profilingState6.TransitionToState(ProfilingState::Active);
420 BOOST_CHECK(profilingState6.GetCurrentState() == ProfilingState::Active);
421
422 ProfilingStateMachine profilingState7(ProfilingState::Active);
423 profilingState7.TransitionToState(ProfilingState::NotConnected);
424 BOOST_CHECK(profilingState7.GetCurrentState() == ProfilingState::NotConnected);
425
426 ProfilingStateMachine profilingState8(ProfilingState::Active);
427 profilingState8.TransitionToState(ProfilingState::Active);
428 BOOST_CHECK(profilingState8.GetCurrentState() == ProfilingState::Active);
429
430 ProfilingStateMachine profilingState9(ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +0100431 BOOST_CHECK_THROW(profilingState9.TransitionToState(ProfilingState::WaitingForAck), armnn::Exception);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100432
433 ProfilingStateMachine profilingState10(ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +0100434 BOOST_CHECK_THROW(profilingState10.TransitionToState(ProfilingState::Active), armnn::Exception);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100435
436 ProfilingStateMachine profilingState11(ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +0100437 BOOST_CHECK_THROW(profilingState11.TransitionToState(ProfilingState::Uninitialised), armnn::Exception);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100438
439 ProfilingStateMachine profilingState12(ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +0100440 BOOST_CHECK_THROW(profilingState12.TransitionToState(ProfilingState::Active), armnn::Exception);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100441
442 ProfilingStateMachine profilingState13(ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +0100443 BOOST_CHECK_THROW(profilingState13.TransitionToState(ProfilingState::Uninitialised), armnn::Exception);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100444
445 ProfilingStateMachine profilingState14(ProfilingState::WaitingForAck);
Jim Flynn53e46992019-10-14 12:31:10 +0100446 profilingState14.TransitionToState(ProfilingState::NotConnected);
447 BOOST_CHECK(profilingState14.GetCurrentState() == ProfilingState::NotConnected);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100448
449 ProfilingStateMachine profilingState15(ProfilingState::Active);
Keith Davis3201eea2019-10-24 17:30:41 +0100450 BOOST_CHECK_THROW(profilingState15.TransitionToState(ProfilingState::Uninitialised), armnn::Exception);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100451
452 ProfilingStateMachine profilingState16(armnn::profiling::ProfilingState::Active);
Keith Davis3201eea2019-10-24 17:30:41 +0100453 BOOST_CHECK_THROW(profilingState16.TransitionToState(ProfilingState::WaitingForAck), armnn::Exception);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100454
455 ProfilingStateMachine profilingState17(ProfilingState::Uninitialised);
456
Keith Davis3201eea2019-10-24 17:30:41 +0100457 std::thread thread1(ProfilingCurrentStateThreadImpl, std::ref(profilingState17));
458 std::thread thread2(ProfilingCurrentStateThreadImpl, std::ref(profilingState17));
459 std::thread thread3(ProfilingCurrentStateThreadImpl, std::ref(profilingState17));
460 std::thread thread4(ProfilingCurrentStateThreadImpl, std::ref(profilingState17));
461 std::thread thread5(ProfilingCurrentStateThreadImpl, std::ref(profilingState17));
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100462
463 thread1.join();
464 thread2.join();
465 thread3.join();
466 thread4.join();
467 thread5.join();
468
469 BOOST_TEST((profilingState17.GetCurrentState() == ProfilingState::NotConnected));
470}
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100471
Jim Flynn8355ec92019-09-17 12:29:50 +0100472void CaptureDataWriteThreadImpl(Holder& holder, uint32_t capturePeriod, const std::vector<uint16_t>& counterIds)
Francis Murtagh68f78d82019-09-04 16:42:29 +0100473{
474 holder.SetCaptureData(capturePeriod, counterIds);
475}
476
Francis Murtaghbd707162019-09-09 11:26:44 +0100477void CaptureDataReadThreadImpl(const Holder& holder, CaptureData& captureData)
Francis Murtagh68f78d82019-09-04 16:42:29 +0100478{
479 captureData = holder.GetCaptureData();
480}
481
482BOOST_AUTO_TEST_CASE(CheckCaptureDataHolder)
483{
Francis Murtaghbd707162019-09-09 11:26:44 +0100484 std::map<uint32_t, std::vector<uint16_t>> periodIdMap;
485 std::vector<uint16_t> counterIds;
Jim Flynn8355ec92019-09-17 12:29:50 +0100486 uint32_t numThreads = 10;
487 for (uint32_t i = 0; i < numThreads; ++i)
Francis Murtaghbd707162019-09-09 11:26:44 +0100488 {
489 counterIds.emplace_back(i);
490 periodIdMap.insert(std::make_pair(i, counterIds));
491 }
Francis Murtagh68f78d82019-09-04 16:42:29 +0100492
Jim Flynn8355ec92019-09-17 12:29:50 +0100493 // Verify the read and write threads set the holder correctly
494 // and retrieve the expected values
Francis Murtagh68f78d82019-09-04 16:42:29 +0100495 Holder holder;
496 BOOST_CHECK((holder.GetCaptureData()).GetCapturePeriod() == 0);
497 BOOST_CHECK(((holder.GetCaptureData()).GetCounterIds()).empty());
498
499 // Check Holder functions
Francis Murtaghbd707162019-09-09 11:26:44 +0100500 std::thread thread1(CaptureDataWriteThreadImpl, std::ref(holder), 2, std::ref(periodIdMap[2]));
Francis Murtagh68f78d82019-09-04 16:42:29 +0100501 thread1.join();
Francis Murtaghbd707162019-09-09 11:26:44 +0100502 BOOST_CHECK((holder.GetCaptureData()).GetCapturePeriod() == 2);
503 BOOST_CHECK((holder.GetCaptureData()).GetCounterIds() == periodIdMap[2]);
Jim Flynn8355ec92019-09-17 12:29:50 +0100504 // NOTE: now that we have some initial values in the holder we don't have to worry
505 // in the multi-threaded section below about a read thread accessing the holder
506 // before any write thread has gotten to it so we read period = 0, counterIds empty
507 // instead of period = 0, counterIds = {0} as will the case when write thread 0
508 // has executed.
Francis Murtagh68f78d82019-09-04 16:42:29 +0100509
510 CaptureData captureData;
511 std::thread thread2(CaptureDataReadThreadImpl, std::ref(holder), std::ref(captureData));
512 thread2.join();
Jim Flynn8355ec92019-09-17 12:29:50 +0100513 BOOST_CHECK(captureData.GetCapturePeriod() == 2);
Francis Murtaghbd707162019-09-09 11:26:44 +0100514 BOOST_CHECK(captureData.GetCounterIds() == periodIdMap[2]);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100515
Jim Flynn8355ec92019-09-17 12:29:50 +0100516 std::map<uint32_t, CaptureData> captureDataIdMap;
517 for (uint32_t i = 0; i < numThreads; ++i)
518 {
519 CaptureData perThreadCaptureData;
520 captureDataIdMap.insert(std::make_pair(i, perThreadCaptureData));
521 }
522
Francis Murtaghbd707162019-09-09 11:26:44 +0100523 std::vector<std::thread> threadsVect;
Jim Flynn8355ec92019-09-17 12:29:50 +0100524 std::vector<std::thread> readThreadsVect;
525 for (uint32_t i = 0; i < numThreads; ++i)
Francis Murtaghbd707162019-09-09 11:26:44 +0100526 {
Keith Davis3201eea2019-10-24 17:30:41 +0100527 threadsVect.emplace_back(
528 std::thread(CaptureDataWriteThreadImpl, std::ref(holder), i, std::ref(periodIdMap[i])));
Francis Murtagh06965692019-09-05 16:29:01 +0100529
Jim Flynn8355ec92019-09-17 12:29:50 +0100530 // Verify that the CaptureData goes into the thread in a virgin state
531 BOOST_CHECK(captureDataIdMap.at(i).GetCapturePeriod() == 0);
532 BOOST_CHECK(captureDataIdMap.at(i).GetCounterIds().empty());
Keith Davis3201eea2019-10-24 17:30:41 +0100533 readThreadsVect.emplace_back(
534 std::thread(CaptureDataReadThreadImpl, std::ref(holder), std::ref(captureDataIdMap.at(i))));
Francis Murtaghbd707162019-09-09 11:26:44 +0100535 }
536
Jim Flynn8355ec92019-09-17 12:29:50 +0100537 for (uint32_t i = 0; i < numThreads; ++i)
Francis Murtaghbd707162019-09-09 11:26:44 +0100538 {
539 threadsVect[i].join();
Francis Murtaghbd707162019-09-09 11:26:44 +0100540 readThreadsVect[i].join();
541 }
Francis Murtagh68f78d82019-09-04 16:42:29 +0100542
Jim Flynn8355ec92019-09-17 12:29:50 +0100543 // Look at the CaptureData that each read thread has filled
544 // the capture period it read should match the counter ids entry
545 for (uint32_t i = 0; i < numThreads; ++i)
546 {
547 CaptureData perThreadCaptureData = captureDataIdMap.at(i);
548 BOOST_CHECK(perThreadCaptureData.GetCounterIds() == periodIdMap.at(perThreadCaptureData.GetCapturePeriod()));
549 }
Matthew Bentham46d1c622019-09-13 12:45:04 +0100550}
Francis Murtagh68f78d82019-09-04 16:42:29 +0100551
Matthew Bentham46d1c622019-09-13 12:45:04 +0100552BOOST_AUTO_TEST_CASE(CaptureDataMethods)
553{
Jim Flynn8355ec92019-09-17 12:29:50 +0100554 // Check CaptureData setter and getter functions
Keith Davis3201eea2019-10-24 17:30:41 +0100555 std::vector<uint16_t> counterIds = { 42, 29, 13 };
Jim Flynn8355ec92019-09-17 12:29:50 +0100556 CaptureData captureData;
557 BOOST_CHECK(captureData.GetCapturePeriod() == 0);
558 BOOST_CHECK((captureData.GetCounterIds()).empty());
559 captureData.SetCapturePeriod(150);
560 captureData.SetCounterIds(counterIds);
561 BOOST_CHECK(captureData.GetCapturePeriod() == 150);
562 BOOST_CHECK(captureData.GetCounterIds() == counterIds);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100563
Jim Flynn8355ec92019-09-17 12:29:50 +0100564 // Check assignment operator
Francis Murtagh68f78d82019-09-04 16:42:29 +0100565 CaptureData secondCaptureData;
Francis Murtagh68f78d82019-09-04 16:42:29 +0100566
Jim Flynn8355ec92019-09-17 12:29:50 +0100567 secondCaptureData = captureData;
568 BOOST_CHECK(secondCaptureData.GetCapturePeriod() == 150);
Matthew Bentham46d1c622019-09-13 12:45:04 +0100569 BOOST_CHECK(secondCaptureData.GetCounterIds() == counterIds);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100570
571 // Check copy constructor
Jim Flynn8355ec92019-09-17 12:29:50 +0100572 CaptureData copyConstructedCaptureData(captureData);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100573
Jim Flynn8355ec92019-09-17 12:29:50 +0100574 BOOST_CHECK(copyConstructedCaptureData.GetCapturePeriod() == 150);
Matthew Bentham46d1c622019-09-13 12:45:04 +0100575 BOOST_CHECK(copyConstructedCaptureData.GetCounterIds() == counterIds);
Keith Davis02356de2019-08-26 18:28:17 +0100576}
Francis Murtagh68f78d82019-09-04 16:42:29 +0100577
Keith Davis02356de2019-08-26 18:28:17 +0100578BOOST_AUTO_TEST_CASE(CheckProfilingServiceDisabled)
579{
580 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Matteo Martincigha84edee2019-10-02 12:50:57 +0100581 ProfilingService& profilingService = ProfilingService::Instance();
582 profilingService.ResetExternalProfilingOptions(options, true);
583 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100584 profilingService.Update();
Matteo Martincigha84edee2019-10-02 12:50:57 +0100585 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis02356de2019-08-26 18:28:17 +0100586}
587
Keith Davis02356de2019-08-26 18:28:17 +0100588BOOST_AUTO_TEST_CASE(CheckProfilingServiceEnabled)
589{
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100590 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
591 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
592
Keith Davis02356de2019-08-26 18:28:17 +0100593 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +0100594 options.m_EnableProfiling = true;
Matteo Martincigha84edee2019-10-02 12:50:57 +0100595 ProfilingService& profilingService = ProfilingService::Instance();
596 profilingService.ResetExternalProfilingOptions(options, true);
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100597 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
598 profilingService.Update();
Matteo Martincigha84edee2019-10-02 12:50:57 +0100599 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis02356de2019-08-26 18:28:17 +0100600
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100601 // Redirect the output to a local stream so that we can parse the warning message
Sadik Armaganbd9e2c52019-09-26 23:13:31 +0100602 std::stringstream ss;
Matteo Martincighd0613b52019-10-09 16:47:04 +0100603 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100604 profilingService.Update();
Sadik Armaganbd9e2c52019-09-26 23:13:31 +0100605 BOOST_CHECK(boost::contains(ss.str(), "Cannot connect to stream socket: Connection refused"));
606}
Keith Davis02356de2019-08-26 18:28:17 +0100607
608BOOST_AUTO_TEST_CASE(CheckProfilingServiceEnabledRuntime)
609{
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100610 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
611 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
612
Keith Davis02356de2019-08-26 18:28:17 +0100613 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Matteo Martincigha84edee2019-10-02 12:50:57 +0100614 ProfilingService& profilingService = ProfilingService::Instance();
615 profilingService.ResetExternalProfilingOptions(options, true);
616 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100617 profilingService.Update();
Matteo Martincigha84edee2019-10-02 12:50:57 +0100618 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100619 options.m_EnableProfiling = true;
Matteo Martincigha84edee2019-10-02 12:50:57 +0100620 profilingService.ResetExternalProfilingOptions(options);
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100621 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
622 profilingService.Update();
Matteo Martincigha84edee2019-10-02 12:50:57 +0100623 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Sadik Armaganbd9e2c52019-09-26 23:13:31 +0100624
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100625 // Redirect the output to a local stream so that we can parse the warning message
Sadik Armaganbd9e2c52019-09-26 23:13:31 +0100626 std::stringstream ss;
Matteo Martincighd0613b52019-10-09 16:47:04 +0100627 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100628 profilingService.Update();
Sadik Armaganbd9e2c52019-09-26 23:13:31 +0100629 BOOST_CHECK(boost::contains(ss.str(), "Cannot connect to stream socket: Connection refused"));
Francis Murtagh68f78d82019-09-04 16:42:29 +0100630}
631
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100632BOOST_AUTO_TEST_CASE(CheckProfilingServiceCounterDirectory)
633{
634 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Matteo Martincigha84edee2019-10-02 12:50:57 +0100635 ProfilingService& profilingService = ProfilingService::Instance();
636 profilingService.ResetExternalProfilingOptions(options, true);
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100637
Matteo Martincigha84edee2019-10-02 12:50:57 +0100638 const ICounterDirectory& counterDirectory0 = profilingService.GetCounterDirectory();
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100639 BOOST_CHECK(counterDirectory0.GetCounterCount() == 0);
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100640 profilingService.Update();
641 BOOST_CHECK(counterDirectory0.GetCounterCount() == 0);
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100642
643 options.m_EnableProfiling = true;
Matteo Martincigha84edee2019-10-02 12:50:57 +0100644 profilingService.ResetExternalProfilingOptions(options);
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100645
Matteo Martincigha84edee2019-10-02 12:50:57 +0100646 const ICounterDirectory& counterDirectory1 = profilingService.GetCounterDirectory();
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100647 BOOST_CHECK(counterDirectory1.GetCounterCount() == 0);
648 profilingService.Update();
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100649 BOOST_CHECK(counterDirectory1.GetCounterCount() != 0);
650}
651
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100652BOOST_AUTO_TEST_CASE(CheckProfilingServiceCounterValues)
653{
654 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +0100655 options.m_EnableProfiling = true;
Matteo Martincigha84edee2019-10-02 12:50:57 +0100656 ProfilingService& profilingService = ProfilingService::Instance();
657 profilingService.ResetExternalProfilingOptions(options, true);
658
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100659 profilingService.Update();
Matteo Martincigha84edee2019-10-02 12:50:57 +0100660 const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
Keith Davis3201eea2019-10-24 17:30:41 +0100661 const Counters& counters = counterDirectory.GetCounters();
Matteo Martincigha84edee2019-10-02 12:50:57 +0100662 BOOST_CHECK(!counters.empty());
663
664 // Get the UID of the first counter for testing
665 uint16_t counterUid = counters.begin()->first;
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100666
667 ProfilingService* profilingServicePtr = &profilingService;
668 std::vector<std::thread> writers;
669
Keith Davis3201eea2019-10-24 17:30:41 +0100670 for (int i = 0; i < 100; ++i)
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100671 {
Matteo Martincigha84edee2019-10-02 12:50:57 +0100672 // Increment and decrement the first counter
673 writers.push_back(std::thread(&ProfilingService::IncrementCounterValue, profilingServicePtr, counterUid));
674 writers.push_back(std::thread(&ProfilingService::DecrementCounterValue, profilingServicePtr, counterUid));
675 // Add 10 and subtract 5 from the first counter
Keith Davis3201eea2019-10-24 17:30:41 +0100676 writers.push_back(std::thread(&ProfilingService::AddCounterValue, profilingServicePtr, counterUid, 10));
Matteo Martincigha84edee2019-10-02 12:50:57 +0100677 writers.push_back(std::thread(&ProfilingService::SubtractCounterValue, profilingServicePtr, counterUid, 5));
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100678 }
679
680 std::for_each(writers.begin(), writers.end(), mem_fn(&std::thread::join));
681
Matteo Martincigha84edee2019-10-02 12:50:57 +0100682 uint32_t counterValue = 0;
683 BOOST_CHECK_NO_THROW(counterValue = profilingService.GetCounterValue(counterUid));
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100684 BOOST_CHECK(counterValue == 500);
685
Matteo Martincigha84edee2019-10-02 12:50:57 +0100686 BOOST_CHECK_NO_THROW(profilingService.SetCounterValue(counterUid, 0));
687 BOOST_CHECK_NO_THROW(counterValue = profilingService.GetCounterValue(counterUid));
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100688 BOOST_CHECK(counterValue == 0);
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100689}
690
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100691BOOST_AUTO_TEST_CASE(CheckProfilingObjectUids)
Matteo Martincighab173e92019-09-05 12:02:04 +0100692{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100693 uint16_t uid = 0;
694 BOOST_CHECK_NO_THROW(uid = GetNextUid());
695 BOOST_CHECK(uid >= 1);
696
697 uint16_t nextUid = 0;
698 BOOST_CHECK_NO_THROW(nextUid = GetNextUid());
699 BOOST_CHECK(nextUid > uid);
700
701 std::vector<uint16_t> counterUids;
702 BOOST_CHECK_NO_THROW(counterUids = GetNextCounterUids(0));
703 BOOST_CHECK(counterUids.size() == 1);
704 BOOST_CHECK(counterUids[0] >= 0);
705
706 std::vector<uint16_t> nextCounterUids;
707 BOOST_CHECK_NO_THROW(nextCounterUids = GetNextCounterUids(1));
708 BOOST_CHECK(nextCounterUids.size() == 1);
709 BOOST_CHECK(nextCounterUids[0] > counterUids[0]);
710
711 std::vector<uint16_t> counterUidsMultiCore;
712 uint16_t numberOfCores = 13;
713 BOOST_CHECK_NO_THROW(counterUidsMultiCore = GetNextCounterUids(numberOfCores));
714 BOOST_CHECK(counterUidsMultiCore.size() == numberOfCores);
715 BOOST_CHECK(counterUidsMultiCore.front() >= nextCounterUids[0]);
Keith Davis3201eea2019-10-24 17:30:41 +0100716 for (size_t i = 1; i < numberOfCores; i++)
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100717 {
718 BOOST_CHECK(counterUidsMultiCore[i] == counterUidsMultiCore[i - 1] + 1);
719 }
720 BOOST_CHECK(counterUidsMultiCore.back() == counterUidsMultiCore.front() + numberOfCores - 1);
Matteo Martincighab173e92019-09-05 12:02:04 +0100721}
722
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100723BOOST_AUTO_TEST_CASE(CheckCounterDirectoryRegisterCategory)
Matteo Martincighab173e92019-09-05 12:02:04 +0100724{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100725 CounterDirectory counterDirectory;
Keith Davis3201eea2019-10-24 17:30:41 +0100726 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
727 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100728 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
Keith Davis3201eea2019-10-24 17:30:41 +0100729 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
Matteo Martincighab173e92019-09-05 12:02:04 +0100730
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100731 // Register a category with an invalid name
732 const Category* noCategory = nullptr;
733 BOOST_CHECK_THROW(noCategory = counterDirectory.RegisterCategory(""), armnn::InvalidArgumentException);
734 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
735 BOOST_CHECK(!noCategory);
Matteo Martincighab173e92019-09-05 12:02:04 +0100736
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100737 // Register a category with an invalid name
738 BOOST_CHECK_THROW(noCategory = counterDirectory.RegisterCategory("invalid category"),
739 armnn::InvalidArgumentException);
740 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
741 BOOST_CHECK(!noCategory);
742
743 // Register a new category
744 const std::string categoryName = "some_category";
Keith Davis3201eea2019-10-24 17:30:41 +0100745 const Category* category = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100746 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
747 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
748 BOOST_CHECK(category);
749 BOOST_CHECK(category->m_Name == categoryName);
750 BOOST_CHECK(category->m_Counters.empty());
751 BOOST_CHECK(category->m_DeviceUid == 0);
752 BOOST_CHECK(category->m_CounterSetUid == 0);
753
754 // Get the registered category
755 const Category* registeredCategory = counterDirectory.GetCategory(categoryName);
756 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
757 BOOST_CHECK(registeredCategory);
758 BOOST_CHECK(registeredCategory == category);
759
760 // Try to get a category not registered
761 const Category* notRegisteredCategory = counterDirectory.GetCategory("not_registered_category");
762 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
763 BOOST_CHECK(!notRegisteredCategory);
764
765 // Register a category already registered
766 const Category* anotherCategory = nullptr;
767 BOOST_CHECK_THROW(anotherCategory = counterDirectory.RegisterCategory(categoryName),
768 armnn::InvalidArgumentException);
769 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
770 BOOST_CHECK(!anotherCategory);
771
772 // Register a device for testing
773 const std::string deviceName = "some_device";
Keith Davis3201eea2019-10-24 17:30:41 +0100774 const Device* device = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100775 BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName));
776 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
777 BOOST_CHECK(device);
778 BOOST_CHECK(device->m_Uid >= 1);
779 BOOST_CHECK(device->m_Name == deviceName);
780 BOOST_CHECK(device->m_Cores == 0);
781
782 // Register a new category not associated to any device
783 const std::string categoryWoDeviceName = "some_category_without_device";
Keith Davis3201eea2019-10-24 17:30:41 +0100784 const Category* categoryWoDevice = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100785 BOOST_CHECK_NO_THROW(categoryWoDevice = counterDirectory.RegisterCategory(categoryWoDeviceName, 0));
786 BOOST_CHECK(counterDirectory.GetCategoryCount() == 2);
787 BOOST_CHECK(categoryWoDevice);
788 BOOST_CHECK(categoryWoDevice->m_Name == categoryWoDeviceName);
789 BOOST_CHECK(categoryWoDevice->m_Counters.empty());
790 BOOST_CHECK(categoryWoDevice->m_DeviceUid == 0);
791 BOOST_CHECK(categoryWoDevice->m_CounterSetUid == 0);
792
793 // Register a new category associated to an invalid device
794 const std::string categoryWInvalidDeviceName = "some_category_with_invalid_device";
795
796 ARMNN_NO_CONVERSION_WARN_BEGIN
797 uint16_t invalidDeviceUid = device->m_Uid + 10;
798 ARMNN_NO_CONVERSION_WARN_END
799
800 const Category* categoryWInvalidDevice = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +0100801 BOOST_CHECK_THROW(categoryWInvalidDevice =
802 counterDirectory.RegisterCategory(categoryWInvalidDeviceName, invalidDeviceUid),
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100803 armnn::InvalidArgumentException);
804 BOOST_CHECK(counterDirectory.GetCategoryCount() == 2);
805 BOOST_CHECK(!categoryWInvalidDevice);
806
807 // Register a new category associated to a valid device
808 const std::string categoryWValidDeviceName = "some_category_with_valid_device";
Keith Davis3201eea2019-10-24 17:30:41 +0100809 const Category* categoryWValidDevice = nullptr;
810 BOOST_CHECK_NO_THROW(categoryWValidDevice =
811 counterDirectory.RegisterCategory(categoryWValidDeviceName, device->m_Uid));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100812 BOOST_CHECK(counterDirectory.GetCategoryCount() == 3);
813 BOOST_CHECK(categoryWValidDevice);
814 BOOST_CHECK(categoryWValidDevice != category);
815 BOOST_CHECK(categoryWValidDevice->m_Name == categoryWValidDeviceName);
816 BOOST_CHECK(categoryWValidDevice->m_DeviceUid == device->m_Uid);
817 BOOST_CHECK(categoryWValidDevice->m_CounterSetUid == 0);
818
819 // Register a counter set for testing
820 const std::string counterSetName = "some_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +0100821 const CounterSet* counterSet = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100822 BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
823 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
824 BOOST_CHECK(counterSet);
825 BOOST_CHECK(counterSet->m_Uid >= 1);
826 BOOST_CHECK(counterSet->m_Name == counterSetName);
827 BOOST_CHECK(counterSet->m_Count == 0);
828
829 // Register a new category not associated to any counter set
830 const std::string categoryWoCounterSetName = "some_category_without_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +0100831 const Category* categoryWoCounterSet = nullptr;
832 BOOST_CHECK_NO_THROW(categoryWoCounterSet =
833 counterDirectory.RegisterCategory(categoryWoCounterSetName, armnn::EmptyOptional(), 0));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100834 BOOST_CHECK(counterDirectory.GetCategoryCount() == 4);
835 BOOST_CHECK(categoryWoCounterSet);
836 BOOST_CHECK(categoryWoCounterSet->m_Name == categoryWoCounterSetName);
837 BOOST_CHECK(categoryWoCounterSet->m_DeviceUid == 0);
838 BOOST_CHECK(categoryWoCounterSet->m_CounterSetUid == 0);
839
840 // Register a new category associated to an invalid counter set
841 const std::string categoryWInvalidCounterSetName = "some_category_with_invalid_counter_set";
842
843 ARMNN_NO_CONVERSION_WARN_BEGIN
844 uint16_t invalidCunterSetUid = counterSet->m_Uid + 10;
845 ARMNN_NO_CONVERSION_WARN_END
846
847 const Category* categoryWInvalidCounterSet = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +0100848 BOOST_CHECK_THROW(categoryWInvalidCounterSet = counterDirectory.RegisterCategory(
849 categoryWInvalidCounterSetName, armnn::EmptyOptional(), invalidCunterSetUid),
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100850 armnn::InvalidArgumentException);
851 BOOST_CHECK(counterDirectory.GetCategoryCount() == 4);
852 BOOST_CHECK(!categoryWInvalidCounterSet);
853
854 // Register a new category associated to a valid counter set
855 const std::string categoryWValidCounterSetName = "some_category_with_valid_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +0100856 const Category* categoryWValidCounterSet = nullptr;
857 BOOST_CHECK_NO_THROW(categoryWValidCounterSet = counterDirectory.RegisterCategory(
858 categoryWValidCounterSetName, armnn::EmptyOptional(), counterSet->m_Uid));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100859 BOOST_CHECK(counterDirectory.GetCategoryCount() == 5);
860 BOOST_CHECK(categoryWValidCounterSet);
861 BOOST_CHECK(categoryWValidCounterSet != category);
862 BOOST_CHECK(categoryWValidCounterSet->m_Name == categoryWValidCounterSetName);
863 BOOST_CHECK(categoryWValidCounterSet->m_DeviceUid == 0);
864 BOOST_CHECK(categoryWValidCounterSet->m_CounterSetUid == counterSet->m_Uid);
865
866 // Register a new category associated to a valid device and counter set
867 const std::string categoryWValidDeviceAndValidCounterSetName = "some_category_with_valid_device_and_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +0100868 const Category* categoryWValidDeviceAndValidCounterSet = nullptr;
869 BOOST_CHECK_NO_THROW(categoryWValidDeviceAndValidCounterSet = counterDirectory.RegisterCategory(
870 categoryWValidDeviceAndValidCounterSetName, device->m_Uid, counterSet->m_Uid));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100871 BOOST_CHECK(counterDirectory.GetCategoryCount() == 6);
872 BOOST_CHECK(categoryWValidDeviceAndValidCounterSet);
873 BOOST_CHECK(categoryWValidDeviceAndValidCounterSet != category);
874 BOOST_CHECK(categoryWValidDeviceAndValidCounterSet->m_Name == categoryWValidDeviceAndValidCounterSetName);
875 BOOST_CHECK(categoryWValidDeviceAndValidCounterSet->m_DeviceUid == device->m_Uid);
876 BOOST_CHECK(categoryWValidDeviceAndValidCounterSet->m_CounterSetUid == counterSet->m_Uid);
877}
878
879BOOST_AUTO_TEST_CASE(CheckCounterDirectoryRegisterDevice)
880{
881 CounterDirectory counterDirectory;
Keith Davis3201eea2019-10-24 17:30:41 +0100882 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
883 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100884 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
Keith Davis3201eea2019-10-24 17:30:41 +0100885 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100886
887 // Register a device with an invalid name
888 const Device* noDevice = nullptr;
889 BOOST_CHECK_THROW(noDevice = counterDirectory.RegisterDevice(""), armnn::InvalidArgumentException);
890 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
891 BOOST_CHECK(!noDevice);
892
893 // Register a device with an invalid name
894 BOOST_CHECK_THROW(noDevice = counterDirectory.RegisterDevice("inv@lid nam€"), armnn::InvalidArgumentException);
895 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
896 BOOST_CHECK(!noDevice);
897
898 // Register a new device with no cores or parent category
899 const std::string deviceName = "some_device";
Keith Davis3201eea2019-10-24 17:30:41 +0100900 const Device* device = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100901 BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName));
902 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
903 BOOST_CHECK(device);
904 BOOST_CHECK(device->m_Name == deviceName);
905 BOOST_CHECK(device->m_Uid >= 1);
906 BOOST_CHECK(device->m_Cores == 0);
907
Matteo Martincigh657ab2d2019-09-18 10:53:24 +0100908 // Try getting an unregistered device
909 const Device* unregisteredDevice = counterDirectory.GetDevice(9999);
910 BOOST_CHECK(!unregisteredDevice);
911
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100912 // Get the registered device
913 const Device* registeredDevice = counterDirectory.GetDevice(device->m_Uid);
914 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
915 BOOST_CHECK(registeredDevice);
916 BOOST_CHECK(registeredDevice == device);
917
Matteo Martincigh657ab2d2019-09-18 10:53:24 +0100918 // Register a device with the name of a device already registered
919 const Device* deviceSameName = nullptr;
920 BOOST_CHECK_THROW(deviceSameName = counterDirectory.RegisterDevice(deviceName), armnn::InvalidArgumentException);
921 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
922 BOOST_CHECK(!deviceSameName);
923
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100924 // Register a new device with cores and no parent category
925 const std::string deviceWCoresName = "some_device_with_cores";
Keith Davis3201eea2019-10-24 17:30:41 +0100926 const Device* deviceWCores = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100927 BOOST_CHECK_NO_THROW(deviceWCores = counterDirectory.RegisterDevice(deviceWCoresName, 2));
928 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
929 BOOST_CHECK(deviceWCores);
930 BOOST_CHECK(deviceWCores->m_Name == deviceWCoresName);
931 BOOST_CHECK(deviceWCores->m_Uid >= 1);
932 BOOST_CHECK(deviceWCores->m_Uid > device->m_Uid);
933 BOOST_CHECK(deviceWCores->m_Cores == 2);
934
935 // Get the registered device
936 const Device* registeredDeviceWCores = counterDirectory.GetDevice(deviceWCores->m_Uid);
937 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
938 BOOST_CHECK(registeredDeviceWCores);
939 BOOST_CHECK(registeredDeviceWCores == deviceWCores);
940 BOOST_CHECK(registeredDeviceWCores != device);
941
942 // Register a new device with cores and invalid parent category
943 const std::string deviceWCoresWInvalidParentCategoryName = "some_device_with_cores_with_invalid_parent_category";
Keith Davis3201eea2019-10-24 17:30:41 +0100944 const Device* deviceWCoresWInvalidParentCategory = nullptr;
945 BOOST_CHECK_THROW(deviceWCoresWInvalidParentCategory =
946 counterDirectory.RegisterDevice(deviceWCoresWInvalidParentCategoryName, 3, std::string("")),
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100947 armnn::InvalidArgumentException);
948 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
949 BOOST_CHECK(!deviceWCoresWInvalidParentCategory);
950
951 // Register a new device with cores and invalid parent category
952 const std::string deviceWCoresWInvalidParentCategoryName2 = "some_device_with_cores_with_invalid_parent_category2";
Keith Davis3201eea2019-10-24 17:30:41 +0100953 const Device* deviceWCoresWInvalidParentCategory2 = nullptr;
954 BOOST_CHECK_THROW(deviceWCoresWInvalidParentCategory2 = counterDirectory.RegisterDevice(
955 deviceWCoresWInvalidParentCategoryName2, 3, std::string("invalid_parent_category")),
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100956 armnn::InvalidArgumentException);
957 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
958 BOOST_CHECK(!deviceWCoresWInvalidParentCategory2);
959
960 // Register a category for testing
961 const std::string categoryName = "some_category";
Keith Davis3201eea2019-10-24 17:30:41 +0100962 const Category* category = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100963 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
964 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
965 BOOST_CHECK(category);
966 BOOST_CHECK(category->m_Name == categoryName);
967 BOOST_CHECK(category->m_Counters.empty());
968 BOOST_CHECK(category->m_DeviceUid == 0);
969 BOOST_CHECK(category->m_CounterSetUid == 0);
970
971 // Register a new device with cores and valid parent category
972 const std::string deviceWCoresWValidParentCategoryName = "some_device_with_cores_with_valid_parent_category";
Keith Davis3201eea2019-10-24 17:30:41 +0100973 const Device* deviceWCoresWValidParentCategory = nullptr;
974 BOOST_CHECK_NO_THROW(deviceWCoresWValidParentCategory =
975 counterDirectory.RegisterDevice(deviceWCoresWValidParentCategoryName, 4, categoryName));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100976 BOOST_CHECK(counterDirectory.GetDeviceCount() == 3);
977 BOOST_CHECK(deviceWCoresWValidParentCategory);
978 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Name == deviceWCoresWValidParentCategoryName);
979 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Uid >= 1);
980 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Uid > device->m_Uid);
981 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Uid > deviceWCores->m_Uid);
982 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Cores == 4);
983 BOOST_CHECK(category->m_DeviceUid == deviceWCoresWValidParentCategory->m_Uid);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +0100984
985 // Register a device associated to a category already associated to a different device
986 const std::string deviceSameCategoryName = "some_device_with_invalid_parent_category";
Keith Davis3201eea2019-10-24 17:30:41 +0100987 const Device* deviceSameCategory = nullptr;
Matteo Martincigh657ab2d2019-09-18 10:53:24 +0100988 BOOST_CHECK_THROW(deviceSameCategory = counterDirectory.RegisterDevice(deviceSameCategoryName, 0, categoryName),
989 armnn::InvalidArgumentException);
990 BOOST_CHECK(counterDirectory.GetDeviceCount() == 3);
991 BOOST_CHECK(!deviceSameCategory);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100992}
993
994BOOST_AUTO_TEST_CASE(CheckCounterDirectoryRegisterCounterSet)
995{
996 CounterDirectory counterDirectory;
Keith Davis3201eea2019-10-24 17:30:41 +0100997 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
998 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100999 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
Keith Davis3201eea2019-10-24 17:30:41 +01001000 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001001
1002 // Register a counter set with an invalid name
1003 const CounterSet* noCounterSet = nullptr;
1004 BOOST_CHECK_THROW(noCounterSet = counterDirectory.RegisterCounterSet(""), armnn::InvalidArgumentException);
1005 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
1006 BOOST_CHECK(!noCounterSet);
1007
1008 // Register a counter set with an invalid name
1009 BOOST_CHECK_THROW(noCounterSet = counterDirectory.RegisterCounterSet("invalid name"),
1010 armnn::InvalidArgumentException);
1011 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
1012 BOOST_CHECK(!noCounterSet);
1013
1014 // Register a new counter set with no count or parent category
1015 const std::string counterSetName = "some_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +01001016 const CounterSet* counterSet = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001017 BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
1018 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1019 BOOST_CHECK(counterSet);
1020 BOOST_CHECK(counterSet->m_Name == counterSetName);
1021 BOOST_CHECK(counterSet->m_Uid >= 1);
1022 BOOST_CHECK(counterSet->m_Count == 0);
1023
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001024 // Try getting an unregistered counter set
1025 const CounterSet* unregisteredCounterSet = counterDirectory.GetCounterSet(9999);
1026 BOOST_CHECK(!unregisteredCounterSet);
1027
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001028 // Get the registered counter set
1029 const CounterSet* registeredCounterSet = counterDirectory.GetCounterSet(counterSet->m_Uid);
1030 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1031 BOOST_CHECK(registeredCounterSet);
1032 BOOST_CHECK(registeredCounterSet == counterSet);
1033
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001034 // Register a counter set with the name of a counter set already registered
1035 const CounterSet* counterSetSameName = nullptr;
1036 BOOST_CHECK_THROW(counterSetSameName = counterDirectory.RegisterCounterSet(counterSetName),
1037 armnn::InvalidArgumentException);
1038 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1039 BOOST_CHECK(!counterSetSameName);
1040
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001041 // Register a new counter set with count and no parent category
1042 const std::string counterSetWCountName = "some_counter_set_with_count";
Keith Davis3201eea2019-10-24 17:30:41 +01001043 const CounterSet* counterSetWCount = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001044 BOOST_CHECK_NO_THROW(counterSetWCount = counterDirectory.RegisterCounterSet(counterSetWCountName, 37));
1045 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 2);
1046 BOOST_CHECK(counterSetWCount);
1047 BOOST_CHECK(counterSetWCount->m_Name == counterSetWCountName);
1048 BOOST_CHECK(counterSetWCount->m_Uid >= 1);
1049 BOOST_CHECK(counterSetWCount->m_Uid > counterSet->m_Uid);
1050 BOOST_CHECK(counterSetWCount->m_Count == 37);
1051
1052 // Get the registered counter set
1053 const CounterSet* registeredCounterSetWCount = counterDirectory.GetCounterSet(counterSetWCount->m_Uid);
1054 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 2);
1055 BOOST_CHECK(registeredCounterSetWCount);
1056 BOOST_CHECK(registeredCounterSetWCount == counterSetWCount);
1057 BOOST_CHECK(registeredCounterSetWCount != counterSet);
1058
1059 // Register a new counter set with count and invalid parent category
1060 const std::string counterSetWCountWInvalidParentCategoryName = "some_counter_set_with_count_"
1061 "with_invalid_parent_category";
1062 const CounterSet* counterSetWCountWInvalidParentCategory = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001063 BOOST_CHECK_THROW(counterSetWCountWInvalidParentCategory = counterDirectory.RegisterCounterSet(
1064 counterSetWCountWInvalidParentCategoryName, 42, std::string("")),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001065 armnn::InvalidArgumentException);
1066 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 2);
1067 BOOST_CHECK(!counterSetWCountWInvalidParentCategory);
1068
1069 // Register a new counter set with count and invalid parent category
1070 const std::string counterSetWCountWInvalidParentCategoryName2 = "some_counter_set_with_count_"
1071 "with_invalid_parent_category2";
1072 const CounterSet* counterSetWCountWInvalidParentCategory2 = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001073 BOOST_CHECK_THROW(counterSetWCountWInvalidParentCategory2 = counterDirectory.RegisterCounterSet(
1074 counterSetWCountWInvalidParentCategoryName2, 42, std::string("invalid_parent_category")),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001075 armnn::InvalidArgumentException);
1076 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 2);
1077 BOOST_CHECK(!counterSetWCountWInvalidParentCategory2);
1078
1079 // Register a category for testing
1080 const std::string categoryName = "some_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001081 const Category* category = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001082 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
1083 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
1084 BOOST_CHECK(category);
1085 BOOST_CHECK(category->m_Name == categoryName);
1086 BOOST_CHECK(category->m_Counters.empty());
1087 BOOST_CHECK(category->m_DeviceUid == 0);
1088 BOOST_CHECK(category->m_CounterSetUid == 0);
1089
1090 // Register a new counter set with count and valid parent category
1091 const std::string counterSetWCountWValidParentCategoryName = "some_counter_set_with_count_"
1092 "with_valid_parent_category";
1093 const CounterSet* counterSetWCountWValidParentCategory = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001094 BOOST_CHECK_NO_THROW(counterSetWCountWValidParentCategory = counterDirectory.RegisterCounterSet(
1095 counterSetWCountWValidParentCategoryName, 42, categoryName));
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001096 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 3);
1097 BOOST_CHECK(counterSetWCountWValidParentCategory);
1098 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Name == counterSetWCountWValidParentCategoryName);
1099 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Uid >= 1);
1100 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Uid > counterSet->m_Uid);
1101 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Uid > counterSetWCount->m_Uid);
1102 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Count == 42);
1103 BOOST_CHECK(category->m_CounterSetUid == counterSetWCountWValidParentCategory->m_Uid);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001104
1105 // Register a counter set associated to a category already associated to a different counter set
1106 const std::string counterSetSameCategoryName = "some_counter_set_with_invalid_parent_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001107 const CounterSet* counterSetSameCategory = nullptr;
1108 BOOST_CHECK_THROW(counterSetSameCategory =
1109 counterDirectory.RegisterCounterSet(counterSetSameCategoryName, 0, categoryName),
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001110 armnn::InvalidArgumentException);
1111 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 3);
1112 BOOST_CHECK(!counterSetSameCategory);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001113}
1114
1115BOOST_AUTO_TEST_CASE(CheckCounterDirectoryRegisterCounter)
1116{
1117 CounterDirectory counterDirectory;
Keith Davis3201eea2019-10-24 17:30:41 +01001118 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
1119 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001120 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
Keith Davis3201eea2019-10-24 17:30:41 +01001121 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001122
1123 // Register a counter with an invalid parent category name
1124 const Counter* noCounter = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001125 BOOST_CHECK_THROW(noCounter =
1126 counterDirectory.RegisterCounter("", 0, 1, 123.45f, "valid name", "valid description"),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001127 armnn::InvalidArgumentException);
1128 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1129 BOOST_CHECK(!noCounter);
1130
1131 // Register a counter with an invalid parent category name
Keith Davis3201eea2019-10-24 17:30:41 +01001132 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("invalid parent category", 0, 1, 123.45f,
1133 "valid name", "valid description"),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001134 armnn::InvalidArgumentException);
1135 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1136 BOOST_CHECK(!noCounter);
1137
1138 // Register a counter with an invalid class
Keith Davis3201eea2019-10-24 17:30:41 +01001139 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("valid_parent_category", 2, 1, 123.45f, "valid name",
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001140 "valid description"),
1141 armnn::InvalidArgumentException);
1142 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1143 BOOST_CHECK(!noCounter);
1144
1145 // Register a counter with an invalid interpolation
Keith Davis3201eea2019-10-24 17:30:41 +01001146 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("valid_parent_category", 0, 3, 123.45f, "valid name",
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001147 "valid description"),
1148 armnn::InvalidArgumentException);
1149 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1150 BOOST_CHECK(!noCounter);
1151
1152 // Register a counter with an invalid multiplier
Keith Davis3201eea2019-10-24 17:30:41 +01001153 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("valid_parent_category", 0, 1, .0f, "valid name",
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001154 "valid description"),
1155 armnn::InvalidArgumentException);
1156 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1157 BOOST_CHECK(!noCounter);
1158
1159 // Register a counter with an invalid name
Keith Davis3201eea2019-10-24 17:30:41 +01001160 BOOST_CHECK_THROW(
1161 noCounter = counterDirectory.RegisterCounter("valid_parent_category", 0, 1, 123.45f, "", "valid description"),
1162 armnn::InvalidArgumentException);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001163 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1164 BOOST_CHECK(!noCounter);
1165
1166 // Register a counter with an invalid name
Keith Davis3201eea2019-10-24 17:30:41 +01001167 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("valid_parent_category", 0, 1, 123.45f,
1168 "invalid nam€", "valid description"),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001169 armnn::InvalidArgumentException);
1170 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1171 BOOST_CHECK(!noCounter);
1172
1173 // Register a counter with an invalid description
Keith Davis3201eea2019-10-24 17:30:41 +01001174 BOOST_CHECK_THROW(noCounter =
1175 counterDirectory.RegisterCounter("valid_parent_category", 0, 1, 123.45f, "valid name", ""),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001176 armnn::InvalidArgumentException);
1177 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1178 BOOST_CHECK(!noCounter);
1179
1180 // Register a counter with an invalid description
Keith Davis3201eea2019-10-24 17:30:41 +01001181 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("valid_parent_category", 0, 1, 123.45f, "valid name",
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001182 "inv@lid description"),
1183 armnn::InvalidArgumentException);
1184 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1185 BOOST_CHECK(!noCounter);
1186
1187 // Register a counter with an invalid unit2
Keith Davis3201eea2019-10-24 17:30:41 +01001188 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("valid_parent_category", 0, 1, 123.45f, "valid name",
1189 "valid description", std::string("Mb/s2")),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001190 armnn::InvalidArgumentException);
1191 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1192 BOOST_CHECK(!noCounter);
1193
1194 // Register a counter with a non-existing parent category name
Keith Davis3201eea2019-10-24 17:30:41 +01001195 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter("invalid_parent_category", 0, 1, 123.45f,
1196 "valid name", "valid description"),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001197 armnn::InvalidArgumentException);
1198 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1199 BOOST_CHECK(!noCounter);
1200
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001201 // Try getting an unregistered counter
1202 const Counter* unregisteredCounter = counterDirectory.GetCounter(9999);
1203 BOOST_CHECK(!unregisteredCounter);
1204
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001205 // Register a category for testing
1206 const std::string categoryName = "some_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001207 const Category* category = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001208 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
1209 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
1210 BOOST_CHECK(category);
1211 BOOST_CHECK(category->m_Name == categoryName);
1212 BOOST_CHECK(category->m_Counters.empty());
1213 BOOST_CHECK(category->m_DeviceUid == 0);
1214 BOOST_CHECK(category->m_CounterSetUid == 0);
1215
1216 // Register a counter with a valid parent category name
1217 const Counter* counter = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001218 BOOST_CHECK_NO_THROW(
1219 counter = counterDirectory.RegisterCounter(categoryName, 0, 1, 123.45f, "valid name", "valid description"));
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001220 BOOST_CHECK(counterDirectory.GetCounterCount() == 1);
1221 BOOST_CHECK(counter);
1222 BOOST_CHECK(counter->m_Uid >= 0);
1223 BOOST_CHECK(counter->m_MaxCounterUid == counter->m_Uid);
1224 BOOST_CHECK(counter->m_Class == 0);
1225 BOOST_CHECK(counter->m_Interpolation == 1);
1226 BOOST_CHECK(counter->m_Multiplier == 123.45f);
1227 BOOST_CHECK(counter->m_Name == "valid name");
1228 BOOST_CHECK(counter->m_Description == "valid description");
1229 BOOST_CHECK(counter->m_Units == "");
1230 BOOST_CHECK(counter->m_DeviceUid == 0);
1231 BOOST_CHECK(counter->m_CounterSetUid == 0);
1232 BOOST_CHECK(category->m_Counters.size() == 1);
1233 BOOST_CHECK(category->m_Counters.back() == counter->m_Uid);
1234
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001235 // Register a counter with a name of a counter already registered for the given parent category name
1236 const Counter* counterSameName = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001237 BOOST_CHECK_THROW(counterSameName =
1238 counterDirectory.RegisterCounter(categoryName, 0, 0, 1.0f, "valid name", "valid description"),
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001239 armnn::InvalidArgumentException);
1240 BOOST_CHECK(counterDirectory.GetCounterCount() == 1);
1241 BOOST_CHECK(!counterSameName);
1242
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001243 // Register a counter with a valid parent category name and units
1244 const Counter* counterWUnits = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001245 BOOST_CHECK_NO_THROW(counterWUnits = counterDirectory.RegisterCounter(categoryName, 0, 1, 123.45f, "valid name 2",
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001246 "valid description",
Keith Davis3201eea2019-10-24 17:30:41 +01001247 std::string("Mnnsq2"))); // Units
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001248 BOOST_CHECK(counterDirectory.GetCounterCount() == 2);
1249 BOOST_CHECK(counterWUnits);
1250 BOOST_CHECK(counterWUnits->m_Uid >= 0);
1251 BOOST_CHECK(counterWUnits->m_Uid > counter->m_Uid);
1252 BOOST_CHECK(counterWUnits->m_MaxCounterUid == counterWUnits->m_Uid);
1253 BOOST_CHECK(counterWUnits->m_Class == 0);
1254 BOOST_CHECK(counterWUnits->m_Interpolation == 1);
1255 BOOST_CHECK(counterWUnits->m_Multiplier == 123.45f);
1256 BOOST_CHECK(counterWUnits->m_Name == "valid name 2");
1257 BOOST_CHECK(counterWUnits->m_Description == "valid description");
1258 BOOST_CHECK(counterWUnits->m_Units == "Mnnsq2");
1259 BOOST_CHECK(counterWUnits->m_DeviceUid == 0);
1260 BOOST_CHECK(counterWUnits->m_CounterSetUid == 0);
1261 BOOST_CHECK(category->m_Counters.size() == 2);
1262 BOOST_CHECK(category->m_Counters.back() == counterWUnits->m_Uid);
1263
1264 // Register a counter with a valid parent category name and not associated with a device
1265 const Counter* counterWoDevice = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001266 BOOST_CHECK_NO_THROW(counterWoDevice = counterDirectory.RegisterCounter(
1267 categoryName, 0, 1, 123.45f, "valid name 3", "valid description",
1268 armnn::EmptyOptional(), // Units
1269 armnn::EmptyOptional(), // Number of cores
1270 0)); // Device UID
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001271 BOOST_CHECK(counterDirectory.GetCounterCount() == 3);
1272 BOOST_CHECK(counterWoDevice);
1273 BOOST_CHECK(counterWoDevice->m_Uid >= 0);
1274 BOOST_CHECK(counterWoDevice->m_Uid > counter->m_Uid);
1275 BOOST_CHECK(counterWoDevice->m_MaxCounterUid == counterWoDevice->m_Uid);
1276 BOOST_CHECK(counterWoDevice->m_Class == 0);
1277 BOOST_CHECK(counterWoDevice->m_Interpolation == 1);
1278 BOOST_CHECK(counterWoDevice->m_Multiplier == 123.45f);
1279 BOOST_CHECK(counterWoDevice->m_Name == "valid name 3");
1280 BOOST_CHECK(counterWoDevice->m_Description == "valid description");
1281 BOOST_CHECK(counterWoDevice->m_Units == "");
1282 BOOST_CHECK(counterWoDevice->m_DeviceUid == 0);
1283 BOOST_CHECK(counterWoDevice->m_CounterSetUid == 0);
1284 BOOST_CHECK(category->m_Counters.size() == 3);
1285 BOOST_CHECK(category->m_Counters.back() == counterWoDevice->m_Uid);
1286
1287 // Register a counter with a valid parent category name and associated to an invalid device
Keith Davis3201eea2019-10-24 17:30:41 +01001288 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(categoryName, 0, 1, 123.45f, "valid name 4",
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001289 "valid description",
Keith Davis3201eea2019-10-24 17:30:41 +01001290 armnn::EmptyOptional(), // Units
1291 armnn::EmptyOptional(), // Number of cores
1292 100), // Device UID
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001293 armnn::InvalidArgumentException);
1294 BOOST_CHECK(counterDirectory.GetCounterCount() == 3);
1295 BOOST_CHECK(!noCounter);
1296
1297 // Register a device for testing
1298 const std::string deviceName = "some_device";
Keith Davis3201eea2019-10-24 17:30:41 +01001299 const Device* device = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001300 BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName));
1301 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
1302 BOOST_CHECK(device);
1303 BOOST_CHECK(device->m_Name == deviceName);
1304 BOOST_CHECK(device->m_Uid >= 1);
1305 BOOST_CHECK(device->m_Cores == 0);
1306
1307 // Register a counter with a valid parent category name and associated to a device
1308 const Counter* counterWDevice = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001309 BOOST_CHECK_NO_THROW(counterWDevice = counterDirectory.RegisterCounter(categoryName, 0, 1, 123.45f, "valid name 5",
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001310 "valid description",
Keith Davis3201eea2019-10-24 17:30:41 +01001311 armnn::EmptyOptional(), // Units
1312 armnn::EmptyOptional(), // Number of cores
1313 device->m_Uid)); // Device UID
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001314 BOOST_CHECK(counterDirectory.GetCounterCount() == 4);
1315 BOOST_CHECK(counterWDevice);
1316 BOOST_CHECK(counterWDevice->m_Uid >= 0);
1317 BOOST_CHECK(counterWDevice->m_Uid > counter->m_Uid);
1318 BOOST_CHECK(counterWDevice->m_MaxCounterUid == counterWDevice->m_Uid);
1319 BOOST_CHECK(counterWDevice->m_Class == 0);
1320 BOOST_CHECK(counterWDevice->m_Interpolation == 1);
1321 BOOST_CHECK(counterWDevice->m_Multiplier == 123.45f);
1322 BOOST_CHECK(counterWDevice->m_Name == "valid name 5");
1323 BOOST_CHECK(counterWDevice->m_Description == "valid description");
1324 BOOST_CHECK(counterWDevice->m_Units == "");
1325 BOOST_CHECK(counterWDevice->m_DeviceUid == device->m_Uid);
1326 BOOST_CHECK(counterWDevice->m_CounterSetUid == 0);
1327 BOOST_CHECK(category->m_Counters.size() == 4);
1328 BOOST_CHECK(category->m_Counters.back() == counterWDevice->m_Uid);
1329
1330 // Register a counter with a valid parent category name and not associated with a counter set
1331 const Counter* counterWoCounterSet = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001332 BOOST_CHECK_NO_THROW(counterWoCounterSet = counterDirectory.RegisterCounter(
1333 categoryName, 0, 1, 123.45f, "valid name 6", "valid description",
1334 armnn::EmptyOptional(), // Units
1335 armnn::EmptyOptional(), // Number of cores
1336 armnn::EmptyOptional(), // Device UID
1337 0)); // Counter set UID
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001338 BOOST_CHECK(counterDirectory.GetCounterCount() == 5);
1339 BOOST_CHECK(counterWoCounterSet);
1340 BOOST_CHECK(counterWoCounterSet->m_Uid >= 0);
1341 BOOST_CHECK(counterWoCounterSet->m_Uid > counter->m_Uid);
1342 BOOST_CHECK(counterWoCounterSet->m_MaxCounterUid == counterWoCounterSet->m_Uid);
1343 BOOST_CHECK(counterWoCounterSet->m_Class == 0);
1344 BOOST_CHECK(counterWoCounterSet->m_Interpolation == 1);
1345 BOOST_CHECK(counterWoCounterSet->m_Multiplier == 123.45f);
1346 BOOST_CHECK(counterWoCounterSet->m_Name == "valid name 6");
1347 BOOST_CHECK(counterWoCounterSet->m_Description == "valid description");
1348 BOOST_CHECK(counterWoCounterSet->m_Units == "");
1349 BOOST_CHECK(counterWoCounterSet->m_DeviceUid == 0);
1350 BOOST_CHECK(counterWoCounterSet->m_CounterSetUid == 0);
1351 BOOST_CHECK(category->m_Counters.size() == 5);
1352 BOOST_CHECK(category->m_Counters.back() == counterWoCounterSet->m_Uid);
1353
1354 // Register a counter with a valid parent category name and associated to an invalid counter set
Keith Davis3201eea2019-10-24 17:30:41 +01001355 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(categoryName, 0, 1, 123.45f, "valid name 7",
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001356 "valid description",
Keith Davis3201eea2019-10-24 17:30:41 +01001357 armnn::EmptyOptional(), // Units
1358 armnn::EmptyOptional(), // Number of cores
1359 armnn::EmptyOptional(), // Device UID
1360 100), // Counter set UID
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001361 armnn::InvalidArgumentException);
1362 BOOST_CHECK(counterDirectory.GetCounterCount() == 5);
1363 BOOST_CHECK(!noCounter);
1364
1365 // Register a counter with a valid parent category name and with a given number of cores
1366 const Counter* counterWNumberOfCores = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001367 uint16_t numberOfCores = 15;
1368 BOOST_CHECK_NO_THROW(counterWNumberOfCores = counterDirectory.RegisterCounter(
1369 categoryName, 0, 1, 123.45f, "valid name 8", "valid description",
1370 armnn::EmptyOptional(), // Units
1371 numberOfCores, // Number of cores
1372 armnn::EmptyOptional(), // Device UID
1373 armnn::EmptyOptional())); // Counter set UID
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001374 BOOST_CHECK(counterDirectory.GetCounterCount() == 20);
1375 BOOST_CHECK(counterWNumberOfCores);
1376 BOOST_CHECK(counterWNumberOfCores->m_Uid >= 0);
1377 BOOST_CHECK(counterWNumberOfCores->m_Uid > counter->m_Uid);
1378 BOOST_CHECK(counterWNumberOfCores->m_MaxCounterUid == counterWNumberOfCores->m_Uid + numberOfCores - 1);
1379 BOOST_CHECK(counterWNumberOfCores->m_Class == 0);
1380 BOOST_CHECK(counterWNumberOfCores->m_Interpolation == 1);
1381 BOOST_CHECK(counterWNumberOfCores->m_Multiplier == 123.45f);
1382 BOOST_CHECK(counterWNumberOfCores->m_Name == "valid name 8");
1383 BOOST_CHECK(counterWNumberOfCores->m_Description == "valid description");
1384 BOOST_CHECK(counterWNumberOfCores->m_Units == "");
1385 BOOST_CHECK(counterWNumberOfCores->m_DeviceUid == 0);
1386 BOOST_CHECK(counterWNumberOfCores->m_CounterSetUid == 0);
1387 BOOST_CHECK(category->m_Counters.size() == 20);
Keith Davis3201eea2019-10-24 17:30:41 +01001388 for (size_t i = 0; i < numberOfCores; i++)
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001389 {
1390 BOOST_CHECK(category->m_Counters[category->m_Counters.size() - numberOfCores + i] ==
1391 counterWNumberOfCores->m_Uid + i);
1392 }
1393
1394 // Register a multi-core device for testing
1395 const std::string multiCoreDeviceName = "some_multi_core_device";
Keith Davis3201eea2019-10-24 17:30:41 +01001396 const Device* multiCoreDevice = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001397 BOOST_CHECK_NO_THROW(multiCoreDevice = counterDirectory.RegisterDevice(multiCoreDeviceName, 4));
1398 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
1399 BOOST_CHECK(multiCoreDevice);
1400 BOOST_CHECK(multiCoreDevice->m_Name == multiCoreDeviceName);
1401 BOOST_CHECK(multiCoreDevice->m_Uid >= 1);
1402 BOOST_CHECK(multiCoreDevice->m_Cores == 4);
1403
1404 // Register a counter with a valid parent category name and associated to the multi-core device
1405 const Counter* counterWMultiCoreDevice = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001406 BOOST_CHECK_NO_THROW(counterWMultiCoreDevice = counterDirectory.RegisterCounter(
1407 categoryName, 0, 1, 123.45f, "valid name 9", "valid description",
1408 armnn::EmptyOptional(), // Units
1409 armnn::EmptyOptional(), // Number of cores
1410 multiCoreDevice->m_Uid, // Device UID
1411 armnn::EmptyOptional())); // Counter set UID
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001412 BOOST_CHECK(counterDirectory.GetCounterCount() == 24);
1413 BOOST_CHECK(counterWMultiCoreDevice);
1414 BOOST_CHECK(counterWMultiCoreDevice->m_Uid >= 0);
1415 BOOST_CHECK(counterWMultiCoreDevice->m_Uid > counter->m_Uid);
1416 BOOST_CHECK(counterWMultiCoreDevice->m_MaxCounterUid ==
1417 counterWMultiCoreDevice->m_Uid + multiCoreDevice->m_Cores - 1);
1418 BOOST_CHECK(counterWMultiCoreDevice->m_Class == 0);
1419 BOOST_CHECK(counterWMultiCoreDevice->m_Interpolation == 1);
1420 BOOST_CHECK(counterWMultiCoreDevice->m_Multiplier == 123.45f);
1421 BOOST_CHECK(counterWMultiCoreDevice->m_Name == "valid name 9");
1422 BOOST_CHECK(counterWMultiCoreDevice->m_Description == "valid description");
1423 BOOST_CHECK(counterWMultiCoreDevice->m_Units == "");
1424 BOOST_CHECK(counterWMultiCoreDevice->m_DeviceUid == multiCoreDevice->m_Uid);
1425 BOOST_CHECK(counterWMultiCoreDevice->m_CounterSetUid == 0);
1426 BOOST_CHECK(category->m_Counters.size() == 24);
Keith Davis3201eea2019-10-24 17:30:41 +01001427 for (size_t i = 0; i < 4; i++)
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001428 {
1429 BOOST_CHECK(category->m_Counters[category->m_Counters.size() - 4 + i] == counterWMultiCoreDevice->m_Uid + i);
1430 }
1431
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001432 // Register a multi-core device associate to a parent category for testing
1433 const std::string multiCoreDeviceNameWParentCategory = "some_multi_core_device_with_parent_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001434 const Device* multiCoreDeviceWParentCategory = nullptr;
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001435 BOOST_CHECK_NO_THROW(multiCoreDeviceWParentCategory =
Keith Davis3201eea2019-10-24 17:30:41 +01001436 counterDirectory.RegisterDevice(multiCoreDeviceNameWParentCategory, 2, categoryName));
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001437 BOOST_CHECK(counterDirectory.GetDeviceCount() == 3);
1438 BOOST_CHECK(multiCoreDeviceWParentCategory);
1439 BOOST_CHECK(multiCoreDeviceWParentCategory->m_Name == multiCoreDeviceNameWParentCategory);
1440 BOOST_CHECK(multiCoreDeviceWParentCategory->m_Uid >= 1);
1441 BOOST_CHECK(multiCoreDeviceWParentCategory->m_Cores == 2);
1442
1443 // Register a counter with a valid parent category name and getting the number of cores of the multi-core device
1444 // associated to that category
1445 const Counter* counterWMultiCoreDeviceWParentCategory = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001446 BOOST_CHECK_NO_THROW(counterWMultiCoreDeviceWParentCategory = counterDirectory.RegisterCounter(
1447 categoryName, 0, 1, 123.45f, "valid name 10", "valid description",
1448 armnn::EmptyOptional(), // Units
1449 armnn::EmptyOptional(), // Number of cores
1450 armnn::EmptyOptional(), // Device UID
1451 armnn::EmptyOptional())); // Counter set UID
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001452 BOOST_CHECK(counterDirectory.GetCounterCount() == 26);
1453 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory);
1454 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Uid >= 0);
1455 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Uid > counter->m_Uid);
1456 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_MaxCounterUid ==
1457 counterWMultiCoreDeviceWParentCategory->m_Uid + multiCoreDeviceWParentCategory->m_Cores - 1);
1458 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Class == 0);
1459 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Interpolation == 1);
1460 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Multiplier == 123.45f);
1461 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Name == "valid name 10");
1462 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Description == "valid description");
1463 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Units == "");
1464 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_DeviceUid == 0);
1465 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_CounterSetUid == 0);
1466 BOOST_CHECK(category->m_Counters.size() == 26);
Keith Davis3201eea2019-10-24 17:30:41 +01001467 for (size_t i = 0; i < 2; i++)
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001468 {
1469 BOOST_CHECK(category->m_Counters[category->m_Counters.size() - 2 + i] ==
1470 counterWMultiCoreDeviceWParentCategory->m_Uid + i);
1471 }
1472
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001473 // Register a counter set for testing
1474 const std::string counterSetName = "some_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +01001475 const CounterSet* counterSet = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001476 BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
1477 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1478 BOOST_CHECK(counterSet);
1479 BOOST_CHECK(counterSet->m_Name == counterSetName);
1480 BOOST_CHECK(counterSet->m_Uid >= 1);
1481 BOOST_CHECK(counterSet->m_Count == 0);
1482
1483 // Register a counter with a valid parent category name and associated to a counter set
1484 const Counter* counterWCounterSet = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001485 BOOST_CHECK_NO_THROW(counterWCounterSet = counterDirectory.RegisterCounter(
1486 categoryName, 0, 1, 123.45f, "valid name 11", "valid description",
1487 armnn::EmptyOptional(), // Units
1488 0, // Number of cores
1489 armnn::EmptyOptional(), // Device UID
1490 counterSet->m_Uid)); // Counter set UID
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001491 BOOST_CHECK(counterDirectory.GetCounterCount() == 27);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001492 BOOST_CHECK(counterWCounterSet);
1493 BOOST_CHECK(counterWCounterSet->m_Uid >= 0);
1494 BOOST_CHECK(counterWCounterSet->m_Uid > counter->m_Uid);
1495 BOOST_CHECK(counterWCounterSet->m_MaxCounterUid == counterWCounterSet->m_Uid);
1496 BOOST_CHECK(counterWCounterSet->m_Class == 0);
1497 BOOST_CHECK(counterWCounterSet->m_Interpolation == 1);
1498 BOOST_CHECK(counterWCounterSet->m_Multiplier == 123.45f);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001499 BOOST_CHECK(counterWCounterSet->m_Name == "valid name 11");
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001500 BOOST_CHECK(counterWCounterSet->m_Description == "valid description");
1501 BOOST_CHECK(counterWCounterSet->m_Units == "");
1502 BOOST_CHECK(counterWCounterSet->m_DeviceUid == 0);
1503 BOOST_CHECK(counterWCounterSet->m_CounterSetUid == counterSet->m_Uid);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001504 BOOST_CHECK(category->m_Counters.size() == 27);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001505 BOOST_CHECK(category->m_Counters.back() == counterWCounterSet->m_Uid);
1506
1507 // Register a counter with a valid parent category name and associated to a device and a counter set
1508 const Counter* counterWDeviceWCounterSet = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001509 BOOST_CHECK_NO_THROW(counterWDeviceWCounterSet = counterDirectory.RegisterCounter(
1510 categoryName, 0, 1, 123.45f, "valid name 12", "valid description",
1511 armnn::EmptyOptional(), // Units
1512 1, // Number of cores
1513 device->m_Uid, // Device UID
1514 counterSet->m_Uid)); // Counter set UID
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001515 BOOST_CHECK(counterDirectory.GetCounterCount() == 28);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001516 BOOST_CHECK(counterWDeviceWCounterSet);
1517 BOOST_CHECK(counterWDeviceWCounterSet->m_Uid >= 0);
1518 BOOST_CHECK(counterWDeviceWCounterSet->m_Uid > counter->m_Uid);
1519 BOOST_CHECK(counterWDeviceWCounterSet->m_MaxCounterUid == counterWDeviceWCounterSet->m_Uid);
1520 BOOST_CHECK(counterWDeviceWCounterSet->m_Class == 0);
1521 BOOST_CHECK(counterWDeviceWCounterSet->m_Interpolation == 1);
1522 BOOST_CHECK(counterWDeviceWCounterSet->m_Multiplier == 123.45f);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001523 BOOST_CHECK(counterWDeviceWCounterSet->m_Name == "valid name 12");
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001524 BOOST_CHECK(counterWDeviceWCounterSet->m_Description == "valid description");
1525 BOOST_CHECK(counterWDeviceWCounterSet->m_Units == "");
1526 BOOST_CHECK(counterWDeviceWCounterSet->m_DeviceUid == device->m_Uid);
1527 BOOST_CHECK(counterWDeviceWCounterSet->m_CounterSetUid == counterSet->m_Uid);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001528 BOOST_CHECK(category->m_Counters.size() == 28);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001529 BOOST_CHECK(category->m_Counters.back() == counterWDeviceWCounterSet->m_Uid);
1530
1531 // Register another category for testing
1532 const std::string anotherCategoryName = "some_other_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001533 const Category* anotherCategory = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001534 BOOST_CHECK_NO_THROW(anotherCategory = counterDirectory.RegisterCategory(anotherCategoryName));
1535 BOOST_CHECK(counterDirectory.GetCategoryCount() == 2);
1536 BOOST_CHECK(anotherCategory);
1537 BOOST_CHECK(anotherCategory != category);
1538 BOOST_CHECK(anotherCategory->m_Name == anotherCategoryName);
1539 BOOST_CHECK(anotherCategory->m_Counters.empty());
1540 BOOST_CHECK(anotherCategory->m_DeviceUid == 0);
1541 BOOST_CHECK(anotherCategory->m_CounterSetUid == 0);
1542
1543 // Register a counter to the other category
1544 const Counter* anotherCounter = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001545 BOOST_CHECK_NO_THROW(anotherCounter = counterDirectory.RegisterCounter(anotherCategoryName, 1, 0, .00043f,
1546 "valid name", "valid description",
1547 armnn::EmptyOptional(), // Units
1548 armnn::EmptyOptional(), // Number of cores
1549 device->m_Uid, // Device UID
1550 counterSet->m_Uid)); // Counter set UID
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001551 BOOST_CHECK(counterDirectory.GetCounterCount() == 29);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001552 BOOST_CHECK(anotherCounter);
1553 BOOST_CHECK(anotherCounter->m_Uid >= 0);
1554 BOOST_CHECK(anotherCounter->m_MaxCounterUid == anotherCounter->m_Uid);
1555 BOOST_CHECK(anotherCounter->m_Class == 1);
1556 BOOST_CHECK(anotherCounter->m_Interpolation == 0);
1557 BOOST_CHECK(anotherCounter->m_Multiplier == .00043f);
1558 BOOST_CHECK(anotherCounter->m_Name == "valid name");
1559 BOOST_CHECK(anotherCounter->m_Description == "valid description");
1560 BOOST_CHECK(anotherCounter->m_Units == "");
1561 BOOST_CHECK(anotherCounter->m_DeviceUid == device->m_Uid);
1562 BOOST_CHECK(anotherCounter->m_CounterSetUid == counterSet->m_Uid);
1563 BOOST_CHECK(anotherCategory->m_Counters.size() == 1);
1564 BOOST_CHECK(anotherCategory->m_Counters.back() == anotherCounter->m_Uid);
Matteo Martincighab173e92019-09-05 12:02:04 +01001565}
1566
Ferran Balaguer1b941722019-08-28 16:57:18 +01001567BOOST_AUTO_TEST_CASE(CounterSelectionCommandHandlerParseData)
1568{
1569 using boost::numeric_cast;
1570
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01001571 ProfilingStateMachine profilingStateMachine;
1572
Ferran Balaguer1b941722019-08-28 16:57:18 +01001573 class TestCaptureThread : public IPeriodicCounterCapture
1574 {
Keith Davis3201eea2019-10-24 17:30:41 +01001575 void Start() override
1576 {}
1577 void Stop() override
1578 {}
Ferran Balaguer1b941722019-08-28 16:57:18 +01001579 };
1580
Matteo Martincighe8485382019-10-10 14:08:21 +01001581 class TestReadCounterValues : public IReadCounterValues
1582 {
Keith Davis3201eea2019-10-24 17:30:41 +01001583 bool IsCounterRegistered(uint16_t counterUid) const override
1584 {
1585 return true;
1586 }
1587 uint16_t GetCounterCount() const override
1588 {
1589 return 0;
1590 }
1591 uint32_t GetCounterValue(uint16_t counterUid) const override
1592 {
1593 return 0;
1594 }
Matteo Martincighe8485382019-10-10 14:08:21 +01001595 };
Jim Flynn397043f2019-10-17 17:37:10 +01001596 const uint32_t familyId = 0;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001597 const uint32_t packetId = 0x40000;
1598
1599 uint32_t version = 1;
1600 Holder holder;
1601 TestCaptureThread captureThread;
Matteo Martincighe8485382019-10-10 14:08:21 +01001602 TestReadCounterValues readCounterValues;
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001603 MockBufferManager mockBuffer(512);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01001604 SendCounterPacket sendCounterPacket(profilingStateMachine, mockBuffer);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001605
1606 uint32_t sizeOfUint32 = numeric_cast<uint32_t>(sizeof(uint32_t));
1607 uint32_t sizeOfUint16 = numeric_cast<uint32_t>(sizeof(uint16_t));
1608
1609 // Data with period and counters
Keith Davis3201eea2019-10-24 17:30:41 +01001610 uint32_t period1 = 10;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001611 uint32_t dataLength1 = 8;
Keith Davis3201eea2019-10-24 17:30:41 +01001612 uint32_t offset = 0;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001613
Matteo Martincigh67ef2a52019-10-10 13:29:02 +01001614 std::unique_ptr<unsigned char[]> uniqueData1 = std::make_unique<unsigned char[]>(dataLength1);
Keith Davis3201eea2019-10-24 17:30:41 +01001615 unsigned char* data1 = reinterpret_cast<unsigned char*>(uniqueData1.get());
FinnWilliamsArma0c78712019-09-16 12:06:47 +01001616
Ferran Balaguer1b941722019-08-28 16:57:18 +01001617 WriteUint32(data1, offset, period1);
1618 offset += sizeOfUint32;
1619 WriteUint16(data1, offset, 4000);
1620 offset += sizeOfUint16;
1621 WriteUint16(data1, offset, 5000);
1622
FinnWilliamsArma0c78712019-09-16 12:06:47 +01001623 Packet packetA(packetId, dataLength1, uniqueData1);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001624
Keith Davis3201eea2019-10-24 17:30:41 +01001625 PeriodicCounterSelectionCommandHandler commandHandler(familyId, packetId, version, holder, captureThread,
1626 readCounterValues, sendCounterPacket, profilingStateMachine);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001627
Matteo Martincighe8485382019-10-10 14:08:21 +01001628 profilingStateMachine.TransitionToState(ProfilingState::Uninitialised);
1629 BOOST_CHECK_THROW(commandHandler(packetA), armnn::RuntimeException);
1630 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
1631 BOOST_CHECK_THROW(commandHandler(packetA), armnn::RuntimeException);
1632 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
1633 BOOST_CHECK_THROW(commandHandler(packetA), armnn::RuntimeException);
1634 profilingStateMachine.TransitionToState(ProfilingState::Active);
1635 BOOST_CHECK_NO_THROW(commandHandler(packetA));
1636
1637 const std::vector<uint16_t> counterIdsA = holder.GetCaptureData().GetCounterIds();
Ferran Balaguer1b941722019-08-28 16:57:18 +01001638
1639 BOOST_TEST(holder.GetCaptureData().GetCapturePeriod() == period1);
Matteo Martincighe8485382019-10-10 14:08:21 +01001640 BOOST_TEST(counterIdsA.size() == 2);
1641 BOOST_TEST(counterIdsA[0] == 4000);
1642 BOOST_TEST(counterIdsA[1] == 5000);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001643
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001644 auto readBuffer = mockBuffer.GetReadableBuffer();
Ferran Balaguer1b941722019-08-28 16:57:18 +01001645
1646 offset = 0;
1647
1648 uint32_t headerWord0 = ReadUint32(readBuffer, offset);
1649 offset += sizeOfUint32;
1650 uint32_t headerWord1 = ReadUint32(readBuffer, offset);
1651 offset += sizeOfUint32;
1652 uint32_t period = ReadUint32(readBuffer, offset);
1653
Keith Davis3201eea2019-10-24 17:30:41 +01001654 BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family
1655 BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 4); // packet id
1656 BOOST_TEST(headerWord1 == 8); // data lenght
1657 BOOST_TEST(period == 10); // capture period
Ferran Balaguer1b941722019-08-28 16:57:18 +01001658
1659 uint16_t counterId = 0;
1660 offset += sizeOfUint32;
1661 counterId = ReadUint16(readBuffer, offset);
1662 BOOST_TEST(counterId == 4000);
1663 offset += sizeOfUint16;
1664 counterId = ReadUint16(readBuffer, offset);
1665 BOOST_TEST(counterId == 5000);
1666
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001667 mockBuffer.MarkRead(readBuffer);
1668
Ferran Balaguer1b941722019-08-28 16:57:18 +01001669 // Data with period only
Keith Davis3201eea2019-10-24 17:30:41 +01001670 uint32_t period2 = 11;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001671 uint32_t dataLength2 = 4;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001672
Matteo Martincigh67ef2a52019-10-10 13:29:02 +01001673 std::unique_ptr<unsigned char[]> uniqueData2 = std::make_unique<unsigned char[]>(dataLength2);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001674
FinnWilliamsArma0c78712019-09-16 12:06:47 +01001675 WriteUint32(reinterpret_cast<unsigned char*>(uniqueData2.get()), 0, period2);
1676
1677 Packet packetB(packetId, dataLength2, uniqueData2);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001678
1679 commandHandler(packetB);
1680
Matteo Martincighe8485382019-10-10 14:08:21 +01001681 const std::vector<uint16_t> counterIdsB = holder.GetCaptureData().GetCounterIds();
Ferran Balaguer1b941722019-08-28 16:57:18 +01001682
1683 BOOST_TEST(holder.GetCaptureData().GetCapturePeriod() == period2);
Matteo Martincighe8485382019-10-10 14:08:21 +01001684 BOOST_TEST(counterIdsB.size() == 0);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001685
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001686 readBuffer = mockBuffer.GetReadableBuffer();
Ferran Balaguer1b941722019-08-28 16:57:18 +01001687
1688 offset = 0;
1689
1690 headerWord0 = ReadUint32(readBuffer, offset);
1691 offset += sizeOfUint32;
1692 headerWord1 = ReadUint32(readBuffer, offset);
1693 offset += sizeOfUint32;
1694 period = ReadUint32(readBuffer, offset);
1695
Keith Davis3201eea2019-10-24 17:30:41 +01001696 BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family
1697 BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 4); // packet id
1698 BOOST_TEST(headerWord1 == 4); // data length
1699 BOOST_TEST(period == 11); // capture period
Ferran Balaguer1b941722019-08-28 16:57:18 +01001700}
1701
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001702BOOST_AUTO_TEST_CASE(CheckConnectionAcknowledged)
1703{
1704 using boost::numeric_cast;
1705
Keith Davis3201eea2019-10-24 17:30:41 +01001706 const uint32_t packetFamilyId = 0;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001707 const uint32_t connectionPacketId = 0x10000;
Keith Davis3201eea2019-10-24 17:30:41 +01001708 const uint32_t version = 1;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001709
1710 uint32_t sizeOfUint32 = numeric_cast<uint32_t>(sizeof(uint32_t));
1711 uint32_t sizeOfUint16 = numeric_cast<uint32_t>(sizeof(uint16_t));
1712
1713 // Data with period and counters
Keith Davis3201eea2019-10-24 17:30:41 +01001714 uint32_t period1 = 10;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001715 uint32_t dataLength1 = 8;
Keith Davis3201eea2019-10-24 17:30:41 +01001716 uint32_t offset = 0;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001717
Matteo Martincigh67ef2a52019-10-10 13:29:02 +01001718 std::unique_ptr<unsigned char[]> uniqueData1 = std::make_unique<unsigned char[]>(dataLength1);
Keith Davis3201eea2019-10-24 17:30:41 +01001719 unsigned char* data1 = reinterpret_cast<unsigned char*>(uniqueData1.get());
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001720
1721 WriteUint32(data1, offset, period1);
1722 offset += sizeOfUint32;
1723 WriteUint16(data1, offset, 4000);
1724 offset += sizeOfUint16;
1725 WriteUint16(data1, offset, 5000);
1726
1727 Packet packetA(connectionPacketId, dataLength1, uniqueData1);
1728
1729 ProfilingStateMachine profilingState(ProfilingState::Uninitialised);
1730 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01001731 CounterDirectory counterDirectory;
1732 MockBufferManager mockBuffer(1024);
1733 SendCounterPacket sendCounterPacket(profilingState, mockBuffer);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001734
Keith Davis3201eea2019-10-24 17:30:41 +01001735 ConnectionAcknowledgedCommandHandler commandHandler(packetFamilyId, connectionPacketId, version, counterDirectory,
1736 sendCounterPacket, profilingState);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001737
1738 // command handler received packet on ProfilingState::Uninitialised
1739 BOOST_CHECK_THROW(commandHandler(packetA), armnn::Exception);
1740
1741 profilingState.TransitionToState(ProfilingState::NotConnected);
1742 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::NotConnected);
1743 // command handler received packet on ProfilingState::NotConnected
1744 BOOST_CHECK_THROW(commandHandler(packetA), armnn::Exception);
1745
1746 profilingState.TransitionToState(ProfilingState::WaitingForAck);
1747 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::WaitingForAck);
1748 // command handler received packet on ProfilingState::WaitingForAck
Matteo Martincighd0613b52019-10-09 16:47:04 +01001749 BOOST_CHECK_NO_THROW(commandHandler(packetA));
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001750 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::Active);
1751
1752 // command handler received packet on ProfilingState::Active
Matteo Martincighd0613b52019-10-09 16:47:04 +01001753 BOOST_CHECK_NO_THROW(commandHandler(packetA));
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001754 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::Active);
1755
1756 // command handler received different packet
1757 const uint32_t differentPacketId = 0x40000;
1758 Packet packetB(differentPacketId, dataLength1, uniqueData1);
Matteo Martincighd0613b52019-10-09 16:47:04 +01001759 profilingState.TransitionToState(ProfilingState::NotConnected);
1760 profilingState.TransitionToState(ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01001761 ConnectionAcknowledgedCommandHandler differentCommandHandler(packetFamilyId, differentPacketId, version,
1762 counterDirectory, sendCounterPacket, profilingState);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001763 BOOST_CHECK_THROW(differentCommandHandler(packetB), armnn::Exception);
1764}
1765
Teresa Charlin9bab4962019-09-06 12:28:35 +01001766BOOST_AUTO_TEST_CASE(CheckSocketProfilingConnection)
1767{
1768 // Check that creating a SocketProfilingConnection results in an exception as the Gator UDS doesn't exist.
1769 BOOST_CHECK_THROW(new SocketProfilingConnection(), armnn::Exception);
1770}
1771
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001772BOOST_AUTO_TEST_CASE(SwTraceIsValidCharTest)
1773{
1774 // Only ASCII 7-bit encoding supported
1775 for (unsigned char c = 0; c < 128; c++)
1776 {
1777 BOOST_CHECK(SwTraceCharPolicy::IsValidChar(c));
1778 }
1779
1780 // Not ASCII
1781 for (unsigned char c = 255; c >= 128; c++)
1782 {
1783 BOOST_CHECK(!SwTraceCharPolicy::IsValidChar(c));
1784 }
1785}
1786
1787BOOST_AUTO_TEST_CASE(SwTraceIsValidNameCharTest)
1788{
1789 // Only alpha-numeric and underscore ASCII 7-bit encoding supported
1790 const unsigned char validChars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
1791 for (unsigned char i = 0; i < sizeof(validChars) / sizeof(validChars[0]) - 1; i++)
1792 {
1793 BOOST_CHECK(SwTraceNameCharPolicy::IsValidChar(validChars[i]));
1794 }
1795
1796 // Non alpha-numeric chars
1797 for (unsigned char c = 0; c < 48; c++)
1798 {
1799 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1800 }
1801 for (unsigned char c = 58; c < 65; c++)
1802 {
1803 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1804 }
1805 for (unsigned char c = 91; c < 95; c++)
1806 {
1807 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1808 }
1809 for (unsigned char c = 96; c < 97; c++)
1810 {
1811 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1812 }
1813 for (unsigned char c = 123; c < 128; c++)
1814 {
1815 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1816 }
1817
1818 // Not ASCII
1819 for (unsigned char c = 255; c >= 128; c++)
1820 {
1821 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1822 }
1823}
1824
1825BOOST_AUTO_TEST_CASE(IsValidSwTraceStringTest)
1826{
1827 // Valid SWTrace strings
1828 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>(""));
1829 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("_"));
1830 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("0123"));
1831 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("valid_string"));
1832 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("VALID_string_456"));
1833 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>(" "));
1834 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("valid string"));
1835 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("!$%"));
1836 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("valid|\\~string#123"));
1837
1838 // Invalid SWTrace strings
1839 BOOST_CHECK(!IsValidSwTraceString<SwTraceCharPolicy>("€£"));
1840 BOOST_CHECK(!IsValidSwTraceString<SwTraceCharPolicy>("invalid‡string"));
1841 BOOST_CHECK(!IsValidSwTraceString<SwTraceCharPolicy>("12Ž34"));
1842}
1843
1844BOOST_AUTO_TEST_CASE(IsValidSwTraceNameStringTest)
1845{
1846 // Valid SWTrace name strings
1847 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>(""));
1848 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>("_"));
1849 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>("0123"));
1850 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>("valid_string"));
1851 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>("VALID_string_456"));
1852
1853 // Invalid SWTrace name strings
1854 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>(" "));
1855 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("invalid string"));
1856 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("!$%"));
1857 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("invalid|\\~string#123"));
1858 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("€£"));
1859 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("invalid‡string"));
1860 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("12Ž34"));
1861}
1862
1863template <typename SwTracePolicy>
1864void StringToSwTraceStringTestHelper(const std::string& testString, std::vector<uint32_t> buffer, size_t expectedSize)
1865{
1866 // Convert the test string to a SWTrace string
1867 BOOST_CHECK(StringToSwTraceString<SwTracePolicy>(testString, buffer));
1868
1869 // The buffer must contain at least the length of the string
1870 BOOST_CHECK(!buffer.empty());
1871
1872 // The buffer must be of the expected size (in words)
1873 BOOST_CHECK(buffer.size() == expectedSize);
1874
1875 // The first word of the byte must be the length of the string including the null-terminator
1876 BOOST_CHECK(buffer[0] == testString.size() + 1);
1877
1878 // The contents of the buffer must match the test string
1879 BOOST_CHECK(std::memcmp(testString.data(), buffer.data() + 1, testString.size()) == 0);
1880
1881 // The buffer must include the null-terminator at the end of the string
1882 size_t nullTerminatorIndex = sizeof(uint32_t) + testString.size();
Keith Davis3201eea2019-10-24 17:30:41 +01001883 BOOST_CHECK(reinterpret_cast<unsigned char*>(buffer.data())[nullTerminatorIndex] == '\0');
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001884}
1885
1886BOOST_AUTO_TEST_CASE(StringToSwTraceStringTest)
1887{
1888 std::vector<uint32_t> buffer;
1889
1890 // Valid SWTrace strings (expected size in words)
1891 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("", buffer, 2);
1892 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("_", buffer, 2);
1893 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("0123", buffer, 3);
1894 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("valid_string", buffer, 5);
1895 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("VALID_string_456", buffer, 6);
1896 StringToSwTraceStringTestHelper<SwTraceCharPolicy>(" ", buffer, 2);
1897 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("valid string", buffer, 5);
1898 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("!$%", buffer, 2);
1899 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("valid|\\~string#123", buffer, 6);
1900
1901 // Invalid SWTrace strings
1902 BOOST_CHECK(!StringToSwTraceString<SwTraceCharPolicy>("€£", buffer));
1903 BOOST_CHECK(buffer.empty());
1904 BOOST_CHECK(!StringToSwTraceString<SwTraceCharPolicy>("invalid‡string", buffer));
1905 BOOST_CHECK(buffer.empty());
1906 BOOST_CHECK(!StringToSwTraceString<SwTraceCharPolicy>("12Ž34", buffer));
1907 BOOST_CHECK(buffer.empty());
1908}
1909
1910BOOST_AUTO_TEST_CASE(StringToSwTraceNameStringTest)
1911{
1912 std::vector<uint32_t> buffer;
1913
1914 // Valid SWTrace namestrings (expected size in words)
1915 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("", buffer, 2);
1916 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("_", buffer, 2);
1917 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("0123", buffer, 3);
1918 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("valid_string", buffer, 5);
1919 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("VALID_string_456", buffer, 6);
1920
1921 // Invalid SWTrace namestrings
1922 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>(" ", buffer));
1923 BOOST_CHECK(buffer.empty());
1924 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("invalid string", buffer));
1925 BOOST_CHECK(buffer.empty());
1926 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("!$%", buffer));
1927 BOOST_CHECK(buffer.empty());
1928 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("invalid|\\~string#123", buffer));
1929 BOOST_CHECK(buffer.empty());
1930 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("€£", buffer));
1931 BOOST_CHECK(buffer.empty());
1932 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("invalid‡string", buffer));
1933 BOOST_CHECK(buffer.empty());
1934 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("12Ž34", buffer));
1935 BOOST_CHECK(buffer.empty());
1936}
1937
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01001938BOOST_AUTO_TEST_CASE(CheckPeriodicCounterCaptureThread)
1939{
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01001940 class CaptureReader : public IReadCounterValues
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01001941 {
1942 public:
Finn Williamsf4d59a62019-10-14 15:55:18 +01001943 CaptureReader(uint16_t counterSize)
1944 {
Keith Davis3201eea2019-10-24 17:30:41 +01001945 for (uint16_t i = 0; i < counterSize; ++i)
Finn Williamsf4d59a62019-10-14 15:55:18 +01001946 {
1947 m_Data[i] = 0;
1948 }
1949 m_CounterSize = counterSize;
1950 }
1951 //not used
Matteo Martincighe8485382019-10-10 14:08:21 +01001952 bool IsCounterRegistered(uint16_t counterUid) const override
1953 {
Finn Williamsf4d59a62019-10-14 15:55:18 +01001954 return false;
Matteo Martincighe8485382019-10-10 14:08:21 +01001955 }
1956
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01001957 uint16_t GetCounterCount() const override
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01001958 {
Finn Williamsf4d59a62019-10-14 15:55:18 +01001959 return m_CounterSize;
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01001960 }
1961
Matteo Martincighe8485382019-10-10 14:08:21 +01001962 uint32_t GetCounterValue(uint16_t counterUid) const override
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01001963 {
Keith Davis3201eea2019-10-24 17:30:41 +01001964 if (counterUid > m_CounterSize)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01001965 {
Finn Williamsf4d59a62019-10-14 15:55:18 +01001966 BOOST_FAIL("Invalid counter Uid");
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01001967 }
Matteo Martincighe8485382019-10-10 14:08:21 +01001968 return m_Data.at(counterUid).load();
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01001969 }
1970
Matteo Martincighe8485382019-10-10 14:08:21 +01001971 void SetCounterValue(uint16_t counterUid, uint32_t value)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01001972 {
Keith Davis3201eea2019-10-24 17:30:41 +01001973 if (counterUid > m_CounterSize)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01001974 {
Finn Williamsf4d59a62019-10-14 15:55:18 +01001975 BOOST_FAIL("Invalid counter Uid");
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01001976 }
Finn Williamsf4d59a62019-10-14 15:55:18 +01001977 m_Data.at(counterUid).store(value);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01001978 }
1979
1980 private:
Matteo Martincighe8485382019-10-10 14:08:21 +01001981 std::unordered_map<uint16_t, std::atomic<uint32_t>> m_Data;
Finn Williamsf4d59a62019-10-14 15:55:18 +01001982 uint16_t m_CounterSize;
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01001983 };
1984
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01001985 ProfilingStateMachine profilingStateMachine;
1986
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01001987 Holder data;
1988 std::vector<uint16_t> captureIds1 = { 0, 1 };
1989 std::vector<uint16_t> captureIds2;
1990
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001991 MockBufferManager mockBuffer(512);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01001992 SendCounterPacket sendCounterPacket(profilingStateMachine, mockBuffer);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01001993
1994 std::vector<uint16_t> counterIds;
Finn Williamsf4d59a62019-10-14 15:55:18 +01001995 CaptureReader captureReader(2);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01001996
Keith Davis3201eea2019-10-24 17:30:41 +01001997 unsigned int valueA = 10;
1998 unsigned int valueB = 15;
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01001999 unsigned int numSteps = 5;
2000
2001 PeriodicCounterCapture periodicCounterCapture(std::ref(data), std::ref(sendCounterPacket), captureReader);
2002
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002003 for (unsigned int i = 0; i < numSteps; ++i)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002004 {
2005 data.SetCaptureData(1, captureIds1);
2006 captureReader.SetCounterValue(0, valueA * (i + 1));
2007 captureReader.SetCounterValue(1, valueB * (i + 1));
2008
2009 periodicCounterCapture.Start();
Finn Williamsf4d59a62019-10-14 15:55:18 +01002010 periodicCounterCapture.Stop();
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002011 }
2012
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002013 auto buffer = mockBuffer.GetReadableBuffer();
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002014
2015 uint32_t headerWord0 = ReadUint32(buffer, 0);
2016 uint32_t headerWord1 = ReadUint32(buffer, 4);
2017
Keith Davis3201eea2019-10-24 17:30:41 +01002018 BOOST_TEST(((headerWord0 >> 26) & 0x0000003F) == 1); // packet family
2019 BOOST_TEST(((headerWord0 >> 19) & 0x0000007F) == 0); // packet class
2020 BOOST_TEST(((headerWord0 >> 16) & 0x00000007) == 0); // packet type
Matteo Martincigh8d9590e2019-10-15 09:35:29 +01002021 BOOST_TEST(headerWord1 == 20);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002022
Keith Davis3201eea2019-10-24 17:30:41 +01002023 uint32_t offset = 16;
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002024 uint16_t readIndex = ReadUint16(buffer, offset);
2025 BOOST_TEST(0 == readIndex);
2026
2027 offset += 2;
2028 uint32_t readValue = ReadUint32(buffer, offset);
2029 BOOST_TEST((valueA * numSteps) == readValue);
2030
2031 offset += 4;
2032 readIndex = ReadUint16(buffer, offset);
2033 BOOST_TEST(1 == readIndex);
2034
2035 offset += 2;
2036 readValue = ReadUint32(buffer, offset);
2037 BOOST_TEST((valueB * numSteps) == readValue);
2038}
2039
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002040BOOST_AUTO_TEST_CASE(RequestCounterDirectoryCommandHandlerTest1)
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002041{
2042 using boost::numeric_cast;
2043
Jim Flynn397043f2019-10-17 17:37:10 +01002044 const uint32_t familyId = 0;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002045 const uint32_t packetId = 3;
Keith Davis3201eea2019-10-24 17:30:41 +01002046 const uint32_t version = 1;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002047 ProfilingStateMachine profilingStateMachine;
2048 CounterDirectory counterDirectory;
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002049 MockBufferManager mockBuffer(1024);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002050 SendCounterPacket sendCounterPacket(profilingStateMachine, mockBuffer);
Keith Davis3201eea2019-10-24 17:30:41 +01002051 RequestCounterDirectoryCommandHandler commandHandler(familyId, packetId, version, counterDirectory,
2052 sendCounterPacket, profilingStateMachine);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002053
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002054 const uint32_t wrongPacketId = 47;
Keith Davis3201eea2019-10-24 17:30:41 +01002055 const uint32_t wrongHeader = (wrongPacketId & 0x000003FF) << 16;
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002056
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002057 Packet wrongPacket(wrongHeader);
2058
2059 profilingStateMachine.TransitionToState(ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002060 BOOST_CHECK_THROW(commandHandler(wrongPacket), armnn::RuntimeException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002061 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002062 BOOST_CHECK_THROW(commandHandler(wrongPacket), armnn::RuntimeException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002063 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002064 BOOST_CHECK_THROW(commandHandler(wrongPacket), armnn::RuntimeException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002065 profilingStateMachine.TransitionToState(ProfilingState::Active);
Keith Davis3201eea2019-10-24 17:30:41 +01002066 BOOST_CHECK_THROW(commandHandler(wrongPacket), armnn::InvalidArgumentException); // Wrong packet
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002067
2068 const uint32_t rightHeader = (packetId & 0x000003FF) << 16;
2069
2070 Packet rightPacket(rightHeader);
2071
Keith Davis3201eea2019-10-24 17:30:41 +01002072 BOOST_CHECK_NO_THROW(commandHandler(rightPacket)); // Right packet
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002073
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002074 auto readBuffer = mockBuffer.GetReadableBuffer();
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002075
2076 uint32_t headerWord0 = ReadUint32(readBuffer, 0);
2077 uint32_t headerWord1 = ReadUint32(readBuffer, 4);
2078
Keith Davis3201eea2019-10-24 17:30:41 +01002079 BOOST_TEST(((headerWord0 >> 26) & 0x0000003F) == 0); // packet family
2080 BOOST_TEST(((headerWord0 >> 16) & 0x000003FF) == 2); // packet id
2081 BOOST_TEST(headerWord1 == 24); // data length
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002082
Keith Davis3201eea2019-10-24 17:30:41 +01002083 uint32_t bodyHeaderWord0 = ReadUint32(readBuffer, 8);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002084 uint16_t deviceRecordCount = numeric_cast<uint16_t>(bodyHeaderWord0 >> 16);
Keith Davis3201eea2019-10-24 17:30:41 +01002085 BOOST_TEST(deviceRecordCount == 0); // device_records_count
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002086}
2087
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002088BOOST_AUTO_TEST_CASE(RequestCounterDirectoryCommandHandlerTest2)
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002089{
2090 using boost::numeric_cast;
2091
Jim Flynn397043f2019-10-17 17:37:10 +01002092 const uint32_t familyId = 0;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002093 const uint32_t packetId = 3;
Keith Davis3201eea2019-10-24 17:30:41 +01002094 const uint32_t version = 1;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002095 ProfilingStateMachine profilingStateMachine;
2096 CounterDirectory counterDirectory;
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002097 MockBufferManager mockBuffer(1024);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002098 SendCounterPacket sendCounterPacket(profilingStateMachine, mockBuffer);
Keith Davis3201eea2019-10-24 17:30:41 +01002099 RequestCounterDirectoryCommandHandler commandHandler(familyId, packetId, version, counterDirectory,
2100 sendCounterPacket, profilingStateMachine);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002101 const uint32_t header = (packetId & 0x000003FF) << 16;
2102 Packet packet(header);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002103
Keith Davis3201eea2019-10-24 17:30:41 +01002104 const Device* device = counterDirectory.RegisterDevice("deviceA", 1);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002105 const CounterSet* counterSet = counterDirectory.RegisterCounterSet("countersetA");
2106 counterDirectory.RegisterCategory("categoryA", device->m_Uid, counterSet->m_Uid);
2107 counterDirectory.RegisterCounter("categoryA", 0, 1, 2.0f, "counterA", "descA");
2108 counterDirectory.RegisterCounter("categoryA", 1, 1, 3.0f, "counterB", "descB");
2109
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002110 profilingStateMachine.TransitionToState(ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002111 BOOST_CHECK_THROW(commandHandler(packet), armnn::RuntimeException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002112 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002113 BOOST_CHECK_THROW(commandHandler(packet), armnn::RuntimeException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002114 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002115 BOOST_CHECK_THROW(commandHandler(packet), armnn::RuntimeException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002116 profilingStateMachine.TransitionToState(ProfilingState::Active);
2117 BOOST_CHECK_NO_THROW(commandHandler(packet));
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002118
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002119 auto readBuffer = mockBuffer.GetReadableBuffer();
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002120
2121 uint32_t headerWord0 = ReadUint32(readBuffer, 0);
2122 uint32_t headerWord1 = ReadUint32(readBuffer, 4);
2123
Keith Davis3201eea2019-10-24 17:30:41 +01002124 BOOST_TEST(((headerWord0 >> 26) & 0x0000003F) == 0); // packet family
2125 BOOST_TEST(((headerWord0 >> 16) & 0x000003FF) == 2); // packet id
2126 BOOST_TEST(headerWord1 == 240); // data length
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002127
Keith Davis3201eea2019-10-24 17:30:41 +01002128 uint32_t bodyHeaderWord0 = ReadUint32(readBuffer, 8);
2129 uint32_t bodyHeaderWord1 = ReadUint32(readBuffer, 12);
2130 uint32_t bodyHeaderWord2 = ReadUint32(readBuffer, 16);
2131 uint32_t bodyHeaderWord3 = ReadUint32(readBuffer, 20);
2132 uint32_t bodyHeaderWord4 = ReadUint32(readBuffer, 24);
2133 uint32_t bodyHeaderWord5 = ReadUint32(readBuffer, 28);
2134 uint16_t deviceRecordCount = numeric_cast<uint16_t>(bodyHeaderWord0 >> 16);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002135 uint16_t counterSetRecordCount = numeric_cast<uint16_t>(bodyHeaderWord2 >> 16);
Keith Davis3201eea2019-10-24 17:30:41 +01002136 uint16_t categoryRecordCount = numeric_cast<uint16_t>(bodyHeaderWord4 >> 16);
2137 BOOST_TEST(deviceRecordCount == 1); // device_records_count
2138 BOOST_TEST(bodyHeaderWord1 == 0); // device_records_pointer_table_offset
2139 BOOST_TEST(counterSetRecordCount == 1); // counter_set_count
2140 BOOST_TEST(bodyHeaderWord3 == 4); // counter_set_pointer_table_offset
2141 BOOST_TEST(categoryRecordCount == 1); // categories_count
2142 BOOST_TEST(bodyHeaderWord5 == 8); // categories_pointer_table_offset
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002143
2144 uint32_t deviceRecordOffset = ReadUint32(readBuffer, 32);
Keith Davis3201eea2019-10-24 17:30:41 +01002145 BOOST_TEST(deviceRecordOffset == 0);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002146
2147 uint32_t counterSetRecordOffset = ReadUint32(readBuffer, 36);
2148 BOOST_TEST(counterSetRecordOffset == 20);
2149
2150 uint32_t categoryRecordOffset = ReadUint32(readBuffer, 40);
Keith Davis3201eea2019-10-24 17:30:41 +01002151 BOOST_TEST(categoryRecordOffset == 44);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002152}
2153
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002154BOOST_AUTO_TEST_CASE(CheckProfilingServiceBadConnectionAcknowledgedPacket)
2155{
2156 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
2157 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
2158
Matteo Martincighd0613b52019-10-09 16:47:04 +01002159 // Swap the profiling connection factory in the profiling service instance with our mock one
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002160 SwapProfilingConnectionFactoryHelper helper;
Matteo Martincighd0613b52019-10-09 16:47:04 +01002161
2162 // Redirect the standard output to a local stream so that we can parse the warning message
2163 std::stringstream ss;
2164 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002165
2166 // Calculate the size of a Stream Metadata packet
Keith Davis3201eea2019-10-24 17:30:41 +01002167 std::string processName = GetProcessName().substr(0, 60);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002168 unsigned int processNameSize = processName.empty() ? 0 : boost::numeric_cast<unsigned int>(processName.size()) + 1;
2169 unsigned int streamMetadataPacketsize = 118 + processNameSize;
2170
Jim Flynn53e46992019-10-14 12:31:10 +01002171 // Reset the profiling service to the uninitialized state
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002172 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002173 options.m_EnableProfiling = true;
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002174 ProfilingService& profilingService = ProfilingService::Instance();
2175 profilingService.ResetExternalProfilingOptions(options, true);
2176
2177 // Bring the profiling service to the "WaitingForAck" state
2178 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002179 profilingService.Update(); // Initialize the counter directory
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002180 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002181 profilingService.Update(); // Create the profiling connection
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002182
Matteo Martincighd0613b52019-10-09 16:47:04 +01002183 // Get the mock profiling connection
2184 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2185 BOOST_CHECK(mockProfilingConnection);
2186
Matteo Martincighe8485382019-10-10 14:08:21 +01002187 // Remove the packets received so far
2188 mockProfilingConnection->Clear();
2189
2190 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
2191 profilingService.Update();
2192
2193 // Wait for the Stream Metadata packet to be sent
2194 helper.WaitForProfilingPacketsSent();
2195
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002196 // Check that the mock profiling connection contains one Stream Metadata packet
Matteo Martincighd0613b52019-10-09 16:47:04 +01002197 const std::vector<uint32_t> writtenData = mockProfilingConnection->GetWrittenData();
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002198 BOOST_TEST(writtenData.size() == 1);
2199 BOOST_TEST(writtenData[0] == streamMetadataPacketsize);
2200
2201 // Write a valid "Connection Acknowledged" packet into the mock profiling connection, to simulate a valid
2202 // reply from an external profiling service
2203
2204 // Connection Acknowledged Packet header (word 0, word 1 is always zero):
2205 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2206 // 16:25 [10] packet_id: Packet identifier, value 0b0000000001
2207 // 8:15 [8] reserved: Reserved, value 0b00000000
2208 // 0:7 [8] reserved: Reserved, value 0b00000000
2209 uint32_t packetFamily = 0;
Keith Davis3201eea2019-10-24 17:30:41 +01002210 uint32_t packetId = 37; // Wrong packet id!!!
2211 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002212
Matteo Martincighd0613b52019-10-09 16:47:04 +01002213 // Create the Connection Acknowledged Packet
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002214 Packet connectionAcknowledgedPacket(header);
2215
2216 // Write the packet to the mock profiling connection
2217 mockProfilingConnection->WritePacket(std::move(connectionAcknowledgedPacket));
2218
2219 // Wait for a bit (must at least be the delay value of the mock profiling connection) to make sure that
2220 // the Connection Acknowledged packet gets processed by the profiling service
Matteo Martincighd0613b52019-10-09 16:47:04 +01002221 std::this_thread::sleep_for(std::chrono::seconds(2));
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002222
2223 // Check that the expected error has occurred and logged to the standard output
2224 BOOST_CHECK(boost::contains(ss.str(), "Functor with requested PacketId=37 and Version=4194304 does not exist"));
2225
2226 // The Connection Acknowledged Command Handler should not have updated the profiling state
2227 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Matteo Martincighd0613b52019-10-09 16:47:04 +01002228
2229 // Reset the profiling service to stop any running thread
2230 options.m_EnableProfiling = false;
2231 profilingService.ResetExternalProfilingOptions(options, true);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002232}
2233
2234BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodConnectionAcknowledgedPacket)
2235{
Matteo Martincighd0613b52019-10-09 16:47:04 +01002236 // Swap the profiling connection factory in the profiling service instance with our mock one
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002237 SwapProfilingConnectionFactoryHelper helper;
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002238
2239 // Calculate the size of a Stream Metadata packet
Keith Davis3201eea2019-10-24 17:30:41 +01002240 std::string processName = GetProcessName().substr(0, 60);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002241 unsigned int processNameSize = processName.empty() ? 0 : boost::numeric_cast<unsigned int>(processName.size()) + 1;
2242 unsigned int streamMetadataPacketsize = 118 + processNameSize;
2243
Jim Flynn53e46992019-10-14 12:31:10 +01002244 // Reset the profiling service to the uninitialized state
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002245 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002246 options.m_EnableProfiling = true;
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002247 ProfilingService& profilingService = ProfilingService::Instance();
2248 profilingService.ResetExternalProfilingOptions(options, true);
2249
2250 // Bring the profiling service to the "WaitingForAck" state
2251 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002252 profilingService.Update(); // Initialize the counter directory
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002253 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002254 profilingService.Update(); // Create the profiling connection
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002255
Matteo Martincighd0613b52019-10-09 16:47:04 +01002256 // Get the mock profiling connection
2257 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2258 BOOST_CHECK(mockProfilingConnection);
2259
Matteo Martincighe8485382019-10-10 14:08:21 +01002260 // Remove the packets received so far
2261 mockProfilingConnection->Clear();
2262
2263 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002264 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002265
2266 // Wait for the Stream Metadata packet to be sent
2267 helper.WaitForProfilingPacketsSent();
2268
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002269 // Check that the mock profiling connection contains one Stream Metadata packet
Matteo Martincighd0613b52019-10-09 16:47:04 +01002270 const std::vector<uint32_t> writtenData = mockProfilingConnection->GetWrittenData();
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002271 BOOST_TEST(writtenData.size() == 1);
2272 BOOST_TEST(writtenData[0] == streamMetadataPacketsize);
2273
2274 // Write a valid "Connection Acknowledged" packet into the mock profiling connection, to simulate a valid
2275 // reply from an external profiling service
2276
2277 // Connection Acknowledged Packet header (word 0, word 1 is always zero):
2278 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2279 // 16:25 [10] packet_id: Packet identifier, value 0b0000000001
2280 // 8:15 [8] reserved: Reserved, value 0b00000000
2281 // 0:7 [8] reserved: Reserved, value 0b00000000
2282 uint32_t packetFamily = 0;
2283 uint32_t packetId = 1;
Keith Davis3201eea2019-10-24 17:30:41 +01002284 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002285
Matteo Martincighd0613b52019-10-09 16:47:04 +01002286 // Create the Connection Acknowledged Packet
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002287 Packet connectionAcknowledgedPacket(header);
2288
2289 // Write the packet to the mock profiling connection
2290 mockProfilingConnection->WritePacket(std::move(connectionAcknowledgedPacket));
2291
2292 // Wait for a bit (must at least be the delay value of the mock profiling connection) to make sure that
2293 // the Connection Acknowledged packet gets processed by the profiling service
Matteo Martincighd0613b52019-10-09 16:47:04 +01002294 std::this_thread::sleep_for(std::chrono::seconds(2));
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002295
2296 // The Connection Acknowledged Command Handler should have updated the profiling state accordingly
2297 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincighd0613b52019-10-09 16:47:04 +01002298
2299 // Reset the profiling service to stop any running thread
2300 options.m_EnableProfiling = false;
2301 profilingService.ResetExternalProfilingOptions(options, true);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002302}
2303
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002304BOOST_AUTO_TEST_CASE(CheckProfilingServiceBadRequestCounterDirectoryPacket)
2305{
2306 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
2307 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
2308
2309 // Swap the profiling connection factory in the profiling service instance with our mock one
2310 SwapProfilingConnectionFactoryHelper helper;
2311
2312 // Redirect the standard output to a local stream so that we can parse the warning message
2313 std::stringstream ss;
2314 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
2315
2316 // Reset the profiling service to the uninitialized state
2317 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002318 options.m_EnableProfiling = true;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002319 ProfilingService& profilingService = ProfilingService::Instance();
2320 profilingService.ResetExternalProfilingOptions(options, true);
2321
2322 // Bring the profiling service to the "Active" state
2323 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
2324 helper.ForceTransitionToState(ProfilingState::NotConnected);
2325 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002326 profilingService.Update(); // Create the profiling connection
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002327 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002328 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002329
2330 // Wait for the Stream Metadata packet the be sent
2331 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
2332 helper.WaitForProfilingPacketsSent();
2333
2334 // Force the profiling service to the "Active" state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002335 helper.ForceTransitionToState(ProfilingState::Active);
2336 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2337
2338 // Get the mock profiling connection
2339 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2340 BOOST_CHECK(mockProfilingConnection);
2341
Matteo Martincighe8485382019-10-10 14:08:21 +01002342 // Remove the packets received so far
2343 mockProfilingConnection->Clear();
2344
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002345 // Write a valid "Request Counter Directory" packet into the mock profiling connection, to simulate a valid
2346 // reply from an external profiling service
2347
2348 // Request Counter Directory packet header (word 0, word 1 is always zero):
2349 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2350 // 16:25 [10] packet_id: Packet identifier, value 0b0000000011
2351 // 8:15 [8] reserved: Reserved, value 0b00000000
2352 // 0:7 [8] reserved: Reserved, value 0b00000000
2353 uint32_t packetFamily = 0;
Keith Davis3201eea2019-10-24 17:30:41 +01002354 uint32_t packetId = 123; // Wrong packet id!!!
2355 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002356
2357 // Create the Request Counter Directory packet
2358 Packet requestCounterDirectoryPacket(header);
2359
2360 // Write the packet to the mock profiling connection
2361 mockProfilingConnection->WritePacket(std::move(requestCounterDirectoryPacket));
2362
2363 // Wait for a bit (must at least be the delay value of the mock profiling connection) to make sure that
2364 // the Create the Request Counter packet gets processed by the profiling service
2365 std::this_thread::sleep_for(std::chrono::seconds(2));
2366
2367 // Check that the expected error has occurred and logged to the standard output
2368 BOOST_CHECK(boost::contains(ss.str(), "Functor with requested PacketId=123 and Version=4194304 does not exist"));
2369
Matteo Martincighe8485382019-10-10 14:08:21 +01002370 // The Request Counter Directory Command Handler should not have updated the profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002371 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2372
2373 // Reset the profiling service to stop any running thread
2374 options.m_EnableProfiling = false;
2375 profilingService.ResetExternalProfilingOptions(options, true);
2376}
2377
2378BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodRequestCounterDirectoryPacket)
2379{
2380 // Swap the profiling connection factory in the profiling service instance with our mock one
2381 SwapProfilingConnectionFactoryHelper helper;
2382
2383 // Reset the profiling service to the uninitialized state
2384 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002385 options.m_EnableProfiling = true;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002386 ProfilingService& profilingService = ProfilingService::Instance();
2387 profilingService.ResetExternalProfilingOptions(options, true);
2388
2389 // Bring the profiling service to the "Active" state
2390 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002391 profilingService.Update(); // Initialize the counter directory
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002392 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002393 profilingService.Update(); // Create the profiling connection
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002394 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002395 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002396
2397 // Wait for the Stream Metadata packet the be sent
2398 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
2399 helper.WaitForProfilingPacketsSent();
2400
2401 // Force the profiling service to the "Active" state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002402 helper.ForceTransitionToState(ProfilingState::Active);
2403 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2404
2405 // Get the mock profiling connection
2406 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2407 BOOST_CHECK(mockProfilingConnection);
2408
Matteo Martincighe8485382019-10-10 14:08:21 +01002409 // Remove the packets received so far
2410 mockProfilingConnection->Clear();
2411
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002412 // Write a valid "Request Counter Directory" packet into the mock profiling connection, to simulate a valid
2413 // reply from an external profiling service
2414
2415 // Request Counter Directory packet header (word 0, word 1 is always zero):
2416 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2417 // 16:25 [10] packet_id: Packet identifier, value 0b0000000011
2418 // 8:15 [8] reserved: Reserved, value 0b00000000
2419 // 0:7 [8] reserved: Reserved, value 0b00000000
2420 uint32_t packetFamily = 0;
2421 uint32_t packetId = 3;
Keith Davis3201eea2019-10-24 17:30:41 +01002422 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002423
2424 // Create the Request Counter Directory packet
2425 Packet requestCounterDirectoryPacket(header);
2426
2427 // Write the packet to the mock profiling connection
2428 mockProfilingConnection->WritePacket(std::move(requestCounterDirectoryPacket));
2429
Matteo Martincighe8485382019-10-10 14:08:21 +01002430 // Wait for the Counter Directory packet to be sent
2431 helper.WaitForProfilingPacketsSent();
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002432
2433 // Check that the mock profiling connection contains one Counter Directory packet
2434 const std::vector<uint32_t> writtenData = mockProfilingConnection->GetWrittenData();
2435 BOOST_TEST(writtenData.size() == 1);
Keith Davis3201eea2019-10-24 17:30:41 +01002436 BOOST_TEST(writtenData[0] == 416); // The size of the expected Counter Directory packet
Matteo Martincighe8485382019-10-10 14:08:21 +01002437
2438 // The Request Counter Directory Command Handler should not have updated the profiling state
2439 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2440
2441 // Reset the profiling service to stop any running thread
2442 options.m_EnableProfiling = false;
2443 profilingService.ResetExternalProfilingOptions(options, true);
2444}
2445
2446BOOST_AUTO_TEST_CASE(CheckProfilingServiceBadPeriodicCounterSelectionPacket)
2447{
2448 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
2449 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
2450
2451 // Swap the profiling connection factory in the profiling service instance with our mock one
2452 SwapProfilingConnectionFactoryHelper helper;
2453
2454 // Redirect the standard output to a local stream so that we can parse the warning message
2455 std::stringstream ss;
2456 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
2457
2458 // Reset the profiling service to the uninitialized state
2459 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002460 options.m_EnableProfiling = true;
Matteo Martincighe8485382019-10-10 14:08:21 +01002461 ProfilingService& profilingService = ProfilingService::Instance();
2462 profilingService.ResetExternalProfilingOptions(options, true);
2463
2464 // Bring the profiling service to the "Active" state
2465 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002466 profilingService.Update(); // Initialize the counter directory
Matteo Martincighe8485382019-10-10 14:08:21 +01002467 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002468 profilingService.Update(); // Create the profiling connection
Matteo Martincighe8485382019-10-10 14:08:21 +01002469 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002470 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002471
2472 // Wait for the Stream Metadata packet the be sent
2473 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
2474 helper.WaitForProfilingPacketsSent();
2475
2476 // Force the profiling service to the "Active" state
2477 helper.ForceTransitionToState(ProfilingState::Active);
2478 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2479
2480 // Get the mock profiling connection
2481 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2482 BOOST_CHECK(mockProfilingConnection);
2483
2484 // Remove the packets received so far
2485 mockProfilingConnection->Clear();
2486
2487 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2488 // external profiling service
2489
2490 // Periodic Counter Selection packet header:
2491 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2492 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2493 // 8:15 [8] reserved: Reserved, value 0b00000000
2494 // 0:7 [8] reserved: Reserved, value 0b00000000
2495 uint32_t packetFamily = 0;
Keith Davis3201eea2019-10-24 17:30:41 +01002496 uint32_t packetId = 999; // Wrong packet id!!!
2497 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincighe8485382019-10-10 14:08:21 +01002498
2499 // Create the Periodic Counter Selection packet
Keith Davis3201eea2019-10-24 17:30:41 +01002500 Packet periodicCounterSelectionPacket(header); // Length == 0, this will disable the collection of counters
Matteo Martincighe8485382019-10-10 14:08:21 +01002501
2502 // Write the packet to the mock profiling connection
2503 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2504
2505 // Wait for a bit (must at least be the delay value of the mock profiling connection) to make sure that
2506 // the Periodic Counter Selection packet gets processed by the profiling service
2507 std::this_thread::sleep_for(std::chrono::seconds(2));
2508
2509 // Check that the expected error has occurred and logged to the standard output
2510 BOOST_CHECK(boost::contains(ss.str(), "Functor with requested PacketId=999 and Version=4194304 does not exist"));
2511
2512 // The Periodic Counter Selection Handler should not have updated the profiling state
2513 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2514
2515 // Reset the profiling service to stop any running thread
2516 options.m_EnableProfiling = false;
2517 profilingService.ResetExternalProfilingOptions(options, true);
2518}
2519
2520BOOST_AUTO_TEST_CASE(CheckProfilingServiceBadPeriodicCounterSelectionPacketInvalidCounterUid)
2521{
2522 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
2523 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
2524
2525 // Swap the profiling connection factory in the profiling service instance with our mock one
2526 SwapProfilingConnectionFactoryHelper helper;
2527
2528 // Reset the profiling service to the uninitialized state
2529 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002530 options.m_EnableProfiling = true;
Matteo Martincighe8485382019-10-10 14:08:21 +01002531 ProfilingService& profilingService = ProfilingService::Instance();
2532 profilingService.ResetExternalProfilingOptions(options, true);
2533
2534 // Bring the profiling service to the "Active" state
2535 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002536 profilingService.Update(); // Initialize the counter directory
Matteo Martincighe8485382019-10-10 14:08:21 +01002537 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002538 profilingService.Update(); // Create the profiling connection
Matteo Martincighe8485382019-10-10 14:08:21 +01002539 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002540 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002541
2542 // Wait for the Stream Metadata packet the be sent
2543 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
2544 helper.WaitForProfilingPacketsSent();
2545
2546 // Force the profiling service to the "Active" state
2547 helper.ForceTransitionToState(ProfilingState::Active);
2548 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2549
2550 // Get the mock profiling connection
2551 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2552 BOOST_CHECK(mockProfilingConnection);
2553
2554 // Remove the packets received so far
2555 mockProfilingConnection->Clear();
2556
2557 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2558 // external profiling service
2559
2560 // Periodic Counter Selection packet header:
2561 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2562 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2563 // 8:15 [8] reserved: Reserved, value 0b00000000
2564 // 0:7 [8] reserved: Reserved, value 0b00000000
2565 uint32_t packetFamily = 0;
2566 uint32_t packetId = 4;
Keith Davis3201eea2019-10-24 17:30:41 +01002567 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincighe8485382019-10-10 14:08:21 +01002568
Keith Davis3201eea2019-10-24 17:30:41 +01002569 uint32_t capturePeriod = 123456; // Some capture period (microseconds)
Matteo Martincighe8485382019-10-10 14:08:21 +01002570
2571 // Get the first valid counter UID
2572 const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
Keith Davis3201eea2019-10-24 17:30:41 +01002573 const Counters& counters = counterDirectory.GetCounters();
Matteo Martincighe8485382019-10-10 14:08:21 +01002574 BOOST_CHECK(counters.size() > 1);
Keith Davis3201eea2019-10-24 17:30:41 +01002575 uint16_t counterUidA = counters.begin()->first; // First valid counter UID
2576 uint16_t counterUidB = 9999; // Second invalid counter UID
Matteo Martincighe8485382019-10-10 14:08:21 +01002577
2578 uint32_t length = 8;
2579
2580 auto data = std::make_unique<unsigned char[]>(length);
2581 WriteUint32(data.get(), 0, capturePeriod);
2582 WriteUint16(data.get(), 4, counterUidA);
2583 WriteUint16(data.get(), 6, counterUidB);
2584
2585 // Create the Periodic Counter Selection packet
Keith Davis3201eea2019-10-24 17:30:41 +01002586 Packet periodicCounterSelectionPacket(header, length, data); // Length > 0, this will start the Period Counter
2587 // Capture thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002588
2589 // Write the packet to the mock profiling connection
2590 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2591
2592 // Expecting one Periodic Counter Selection packet and at least one Periodic Counter Capture packet
2593 int expectedPackets = 2;
2594 std::vector<uint32_t> receivedPackets;
2595
2596 // Keep waiting until all the expected packets have been received
2597 do
2598 {
2599 helper.WaitForProfilingPacketsSent();
2600 const std::vector<uint32_t> writtenData = mockProfilingConnection->GetWrittenData();
2601 if (writtenData.empty())
2602 {
2603 BOOST_ERROR("Packets should be available for reading at this point");
2604 return;
2605 }
2606 receivedPackets.insert(receivedPackets.end(), writtenData.begin(), writtenData.end());
2607 expectedPackets -= boost::numeric_cast<int>(writtenData.size());
Keith Davis3201eea2019-10-24 17:30:41 +01002608 } while (expectedPackets > 0);
Matteo Martincighe8485382019-10-10 14:08:21 +01002609 BOOST_TEST(!receivedPackets.empty());
2610
2611 // The size of the expected Periodic Counter Selection packet
2612 BOOST_TEST((std::find(receivedPackets.begin(), receivedPackets.end(), 14) != receivedPackets.end()));
2613 // The size of the expected Periodic Counter Capture packet
2614 BOOST_TEST((std::find(receivedPackets.begin(), receivedPackets.end(), 22) != receivedPackets.end()));
2615
2616 // The Periodic Counter Selection Handler should not have updated the profiling state
2617 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2618
2619 // Reset the profiling service to stop any running thread
2620 options.m_EnableProfiling = false;
2621 profilingService.ResetExternalProfilingOptions(options, true);
2622}
2623
2624BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodPeriodicCounterSelectionPacketNoCounters)
2625{
2626 // Swap the profiling connection factory in the profiling service instance with our mock one
2627 SwapProfilingConnectionFactoryHelper helper;
2628
2629 // Reset the profiling service to the uninitialized state
2630 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002631 options.m_EnableProfiling = true;
Matteo Martincighe8485382019-10-10 14:08:21 +01002632 ProfilingService& profilingService = ProfilingService::Instance();
2633 profilingService.ResetExternalProfilingOptions(options, true);
2634
2635 // Bring the profiling service to the "Active" state
2636 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002637 profilingService.Update(); // Initialize the counter directory
Matteo Martincighe8485382019-10-10 14:08:21 +01002638 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002639 profilingService.Update(); // Create the profiling connection
Matteo Martincighe8485382019-10-10 14:08:21 +01002640 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002641 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002642
2643 // Wait for the Stream Metadata packet the be sent
2644 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
2645 helper.WaitForProfilingPacketsSent();
2646
2647 // Force the profiling service to the "Active" state
2648 helper.ForceTransitionToState(ProfilingState::Active);
2649 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2650
2651 // Get the mock profiling connection
2652 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2653 BOOST_CHECK(mockProfilingConnection);
2654
2655 // Remove the packets received so far
2656 mockProfilingConnection->Clear();
2657
2658 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2659 // external profiling service
2660
2661 // Periodic Counter Selection packet header:
2662 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2663 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2664 // 8:15 [8] reserved: Reserved, value 0b00000000
2665 // 0:7 [8] reserved: Reserved, value 0b00000000
2666 uint32_t packetFamily = 0;
2667 uint32_t packetId = 4;
Keith Davis3201eea2019-10-24 17:30:41 +01002668 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincighe8485382019-10-10 14:08:21 +01002669
2670 // Create the Periodic Counter Selection packet
Keith Davis3201eea2019-10-24 17:30:41 +01002671 Packet periodicCounterSelectionPacket(header); // Length == 0, this will disable the collection of counters
Matteo Martincighe8485382019-10-10 14:08:21 +01002672
2673 // Write the packet to the mock profiling connection
2674 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2675
2676 // Wait for the Periodic Counter Selection packet to be sent
2677 helper.WaitForProfilingPacketsSent();
2678
2679 // The Periodic Counter Selection Handler should not have updated the profiling state
2680 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2681
2682 // Check that the mock profiling connection contains one Periodic Counter Selection
2683 const std::vector<uint32_t> writtenData = mockProfilingConnection->GetWrittenData();
Keith Davis3201eea2019-10-24 17:30:41 +01002684 BOOST_TEST(writtenData.size() == 1); // Only one packet is expected (no Periodic Counter packets)
2685 BOOST_TEST(writtenData[0] == 12); // The size of the expected Periodic Counter Selection (echos the sent one)
Matteo Martincighe8485382019-10-10 14:08:21 +01002686
2687 // Reset the profiling service to stop any running thread
2688 options.m_EnableProfiling = false;
2689 profilingService.ResetExternalProfilingOptions(options, true);
2690}
2691
2692BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodPeriodicCounterSelectionPacketSingleCounter)
2693{
2694 // Swap the profiling connection factory in the profiling service instance with our mock one
2695 SwapProfilingConnectionFactoryHelper helper;
2696
2697 // Reset the profiling service to the uninitialized state
2698 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002699 options.m_EnableProfiling = true;
Matteo Martincighe8485382019-10-10 14:08:21 +01002700 ProfilingService& profilingService = ProfilingService::Instance();
2701 profilingService.ResetExternalProfilingOptions(options, true);
2702
2703 // Bring the profiling service to the "Active" state
2704 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002705 profilingService.Update(); // Initialize the counter directory
Matteo Martincighe8485382019-10-10 14:08:21 +01002706 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002707 profilingService.Update(); // Create the profiling connection
Matteo Martincighe8485382019-10-10 14:08:21 +01002708 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002709 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002710
2711 // Wait for the Stream Metadata packet the be sent
2712 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
2713 helper.WaitForProfilingPacketsSent();
2714
2715 // Force the profiling service to the "Active" state
2716 helper.ForceTransitionToState(ProfilingState::Active);
2717 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2718
2719 // Get the mock profiling connection
2720 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2721 BOOST_CHECK(mockProfilingConnection);
2722
2723 // Remove the packets received so far
2724 mockProfilingConnection->Clear();
2725
2726 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2727 // external profiling service
2728
2729 // Periodic Counter Selection packet header:
2730 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2731 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2732 // 8:15 [8] reserved: Reserved, value 0b00000000
2733 // 0:7 [8] reserved: Reserved, value 0b00000000
2734 uint32_t packetFamily = 0;
2735 uint32_t packetId = 4;
Keith Davis3201eea2019-10-24 17:30:41 +01002736 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincighe8485382019-10-10 14:08:21 +01002737
Keith Davis3201eea2019-10-24 17:30:41 +01002738 uint32_t capturePeriod = 123456; // Some capture period (microseconds)
Matteo Martincighe8485382019-10-10 14:08:21 +01002739
2740 // Get the first valid counter UID
2741 const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
Keith Davis3201eea2019-10-24 17:30:41 +01002742 const Counters& counters = counterDirectory.GetCounters();
Matteo Martincighe8485382019-10-10 14:08:21 +01002743 BOOST_CHECK(!counters.empty());
Keith Davis3201eea2019-10-24 17:30:41 +01002744 uint16_t counterUid = counters.begin()->first; // Valid counter UID
Matteo Martincighe8485382019-10-10 14:08:21 +01002745
2746 uint32_t length = 6;
2747
2748 auto data = std::make_unique<unsigned char[]>(length);
2749 WriteUint32(data.get(), 0, capturePeriod);
2750 WriteUint16(data.get(), 4, counterUid);
2751
2752 // Create the Periodic Counter Selection packet
Keith Davis3201eea2019-10-24 17:30:41 +01002753 Packet periodicCounterSelectionPacket(header, length, data); // Length > 0, this will start the Period Counter
2754 // Capture thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002755
2756 // Write the packet to the mock profiling connection
2757 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2758
2759 // Expecting one Periodic Counter Selection packet and at least one Periodic Counter Capture packet
2760 int expectedPackets = 2;
2761 std::vector<uint32_t> receivedPackets;
2762
2763 // Keep waiting until all the expected packets have been received
2764 do
2765 {
2766 helper.WaitForProfilingPacketsSent();
2767 const std::vector<uint32_t> writtenData = mockProfilingConnection->GetWrittenData();
2768 if (writtenData.empty())
2769 {
2770 BOOST_ERROR("Packets should be available for reading at this point");
2771 return;
2772 }
2773 receivedPackets.insert(receivedPackets.end(), writtenData.begin(), writtenData.end());
2774 expectedPackets -= boost::numeric_cast<int>(writtenData.size());
Keith Davis3201eea2019-10-24 17:30:41 +01002775 } while (expectedPackets > 0);
Matteo Martincighe8485382019-10-10 14:08:21 +01002776 BOOST_TEST(!receivedPackets.empty());
2777
2778 // The size of the expected Periodic Counter Selection packet (echos the sent one)
2779 BOOST_TEST((std::find(receivedPackets.begin(), receivedPackets.end(), 14) != receivedPackets.end()));
2780 // The size of the expected Periodic Counter Capture packet
2781 BOOST_TEST((std::find(receivedPackets.begin(), receivedPackets.end(), 22) != receivedPackets.end()));
2782
2783 // The Periodic Counter Selection Handler should not have updated the profiling state
2784 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2785
2786 // Reset the profiling service to stop any running thread
2787 options.m_EnableProfiling = false;
2788 profilingService.ResetExternalProfilingOptions(options, true);
2789}
2790
2791BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodPeriodicCounterSelectionPacketMultipleCounters)
2792{
2793 // Swap the profiling connection factory in the profiling service instance with our mock one
2794 SwapProfilingConnectionFactoryHelper helper;
2795
2796 // Reset the profiling service to the uninitialized state
2797 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002798 options.m_EnableProfiling = true;
Matteo Martincighe8485382019-10-10 14:08:21 +01002799 ProfilingService& profilingService = ProfilingService::Instance();
2800 profilingService.ResetExternalProfilingOptions(options, true);
2801
2802 // Bring the profiling service to the "Active" state
2803 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002804 profilingService.Update(); // Initialize the counter directory
Matteo Martincighe8485382019-10-10 14:08:21 +01002805 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002806 profilingService.Update(); // Create the profiling connection
Matteo Martincighe8485382019-10-10 14:08:21 +01002807 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002808 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002809
2810 // Wait for the Stream Metadata packet the be sent
2811 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
2812 helper.WaitForProfilingPacketsSent();
2813
2814 // Force the profiling service to the "Active" state
2815 helper.ForceTransitionToState(ProfilingState::Active);
2816 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2817
2818 // Get the mock profiling connection
2819 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2820 BOOST_CHECK(mockProfilingConnection);
2821
2822 // Remove the packets received so far
2823 mockProfilingConnection->Clear();
2824
2825 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2826 // external profiling service
2827
2828 // Periodic Counter Selection packet header:
2829 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2830 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2831 // 8:15 [8] reserved: Reserved, value 0b00000000
2832 // 0:7 [8] reserved: Reserved, value 0b00000000
2833 uint32_t packetFamily = 0;
2834 uint32_t packetId = 4;
Keith Davis3201eea2019-10-24 17:30:41 +01002835 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincighe8485382019-10-10 14:08:21 +01002836
Keith Davis3201eea2019-10-24 17:30:41 +01002837 uint32_t capturePeriod = 123456; // Some capture period (microseconds)
Matteo Martincighe8485382019-10-10 14:08:21 +01002838
2839 // Get the first valid counter UID
2840 const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
Keith Davis3201eea2019-10-24 17:30:41 +01002841 const Counters& counters = counterDirectory.GetCounters();
Matteo Martincighe8485382019-10-10 14:08:21 +01002842 BOOST_CHECK(counters.size() > 1);
Keith Davis3201eea2019-10-24 17:30:41 +01002843 uint16_t counterUidA = counters.begin()->first; // First valid counter UID
2844 uint16_t counterUidB = (counters.begin()++)->first; // Second valid counter UID
Matteo Martincighe8485382019-10-10 14:08:21 +01002845
2846 uint32_t length = 8;
2847
2848 auto data = std::make_unique<unsigned char[]>(length);
2849 WriteUint32(data.get(), 0, capturePeriod);
2850 WriteUint16(data.get(), 4, counterUidA);
2851 WriteUint16(data.get(), 6, counterUidB);
2852
2853 // Create the Periodic Counter Selection packet
Keith Davis3201eea2019-10-24 17:30:41 +01002854 Packet periodicCounterSelectionPacket(header, length, data); // Length > 0, this will start the Period Counter
2855 // Capture thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002856
2857 // Write the packet to the mock profiling connection
2858 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2859
2860 // Expecting one Periodic Counter Selection packet and at least one Periodic Counter Capture packet
2861 int expectedPackets = 2;
2862 std::vector<uint32_t> receivedPackets;
2863
2864 // Keep waiting until all the expected packets have been received
2865 do
2866 {
2867 helper.WaitForProfilingPacketsSent();
2868 const std::vector<uint32_t> writtenData = mockProfilingConnection->GetWrittenData();
2869 if (writtenData.empty())
2870 {
2871 BOOST_ERROR("Packets should be available for reading at this point");
2872 return;
2873 }
2874 receivedPackets.insert(receivedPackets.end(), writtenData.begin(), writtenData.end());
2875 expectedPackets -= boost::numeric_cast<int>(writtenData.size());
Keith Davis3201eea2019-10-24 17:30:41 +01002876 } while (expectedPackets > 0);
Matteo Martincighe8485382019-10-10 14:08:21 +01002877 BOOST_TEST(!receivedPackets.empty());
2878
2879 // The size of the expected Periodic Counter Selection packet (echos the sent one)
2880 BOOST_TEST((std::find(receivedPackets.begin(), receivedPackets.end(), 16) != receivedPackets.end()));
2881 // The size of the expected Periodic Counter Capture packet
2882 BOOST_TEST((std::find(receivedPackets.begin(), receivedPackets.end(), 28) != receivedPackets.end()));
2883
2884 // The Periodic Counter Selection Handler should not have updated the profiling state
2885 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002886
2887 // Reset the profiling service to stop any running thread
2888 options.m_EnableProfiling = false;
2889 profilingService.ResetExternalProfilingOptions(options, true);
2890}
2891
Jim Flynn53e46992019-10-14 12:31:10 +01002892BOOST_AUTO_TEST_CASE(CheckProfilingServiceDisconnect)
2893{
2894 // Swap the profiling connection factory in the profiling service instance with our mock one
2895 SwapProfilingConnectionFactoryHelper helper;
2896
2897 // Reset the profiling service to the uninitialized state
2898 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002899 options.m_EnableProfiling = true;
Jim Flynn53e46992019-10-14 12:31:10 +01002900 ProfilingService& profilingService = ProfilingService::Instance();
2901 profilingService.ResetExternalProfilingOptions(options, true);
2902
2903 // Try to disconnect the profiling service while in the "Uninitialised" state
2904 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
2905 profilingService.Disconnect();
Keith Davis3201eea2019-10-24 17:30:41 +01002906 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised); // The state should not change
Jim Flynn53e46992019-10-14 12:31:10 +01002907
2908 // Try to disconnect the profiling service while in the "NotConnected" state
Keith Davis3201eea2019-10-24 17:30:41 +01002909 profilingService.Update(); // Initialize the counter directory
Jim Flynn53e46992019-10-14 12:31:10 +01002910 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
2911 profilingService.Disconnect();
Keith Davis3201eea2019-10-24 17:30:41 +01002912 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected); // The state should not change
Jim Flynn53e46992019-10-14 12:31:10 +01002913
2914 // Try to disconnect the profiling service while in the "WaitingForAck" state
Keith Davis3201eea2019-10-24 17:30:41 +01002915 profilingService.Update(); // Create the profiling connection
Jim Flynn53e46992019-10-14 12:31:10 +01002916 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
2917 profilingService.Disconnect();
Keith Davis3201eea2019-10-24 17:30:41 +01002918 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck); // The state should not change
Jim Flynn53e46992019-10-14 12:31:10 +01002919
2920 // Try to disconnect the profiling service while in the "Active" state
Keith Davis3201eea2019-10-24 17:30:41 +01002921 profilingService.Update(); // Start the command handler and the send thread
Jim Flynn53e46992019-10-14 12:31:10 +01002922
2923 // Wait for the Stream Metadata packet the be sent
2924 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
2925 helper.WaitForProfilingPacketsSent();
2926
2927 // Force the profiling service to the "Active" state
2928 helper.ForceTransitionToState(ProfilingState::Active);
2929 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2930
2931 // Get the mock profiling connection
2932 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2933 BOOST_CHECK(mockProfilingConnection);
2934
2935 // Check that the profiling connection is open
2936 BOOST_CHECK(mockProfilingConnection->IsOpen());
2937
2938 profilingService.Disconnect();
Keith Davis3201eea2019-10-24 17:30:41 +01002939 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected); // The state should have changed
Jim Flynn53e46992019-10-14 12:31:10 +01002940
2941 // Check that the profiling connection has been reset
2942 mockProfilingConnection = helper.GetMockProfilingConnection();
2943 BOOST_CHECK(mockProfilingConnection == nullptr);
2944
2945 // Reset the profiling service to stop any running thread
2946 options.m_EnableProfiling = false;
2947 profilingService.ResetExternalProfilingOptions(options, true);
2948}
2949
Matteo Martincigh994b5342019-10-11 17:19:56 +01002950BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodPerJobCounterSelectionPacket)
2951{
2952 // Swap the profiling connection factory in the profiling service instance with our mock one
2953 SwapProfilingConnectionFactoryHelper helper;
2954
2955 // Reset the profiling service to the uninitialized state
2956 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002957 options.m_EnableProfiling = true;
Matteo Martincigh994b5342019-10-11 17:19:56 +01002958 ProfilingService& profilingService = ProfilingService::Instance();
2959 profilingService.ResetExternalProfilingOptions(options, true);
2960
2961 // Bring the profiling service to the "Active" state
2962 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002963 profilingService.Update(); // Initialize the counter directory
Matteo Martincigh994b5342019-10-11 17:19:56 +01002964 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002965 profilingService.Update(); // Create the profiling connection
Matteo Martincigh994b5342019-10-11 17:19:56 +01002966 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002967 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincigh994b5342019-10-11 17:19:56 +01002968
2969 // Wait for the Stream Metadata packet the be sent
2970 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
2971 helper.WaitForProfilingPacketsSent();
2972
2973 // Force the profiling service to the "Active" state
2974 helper.ForceTransitionToState(ProfilingState::Active);
2975 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2976
2977 // Get the mock profiling connection
2978 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2979 BOOST_CHECK(mockProfilingConnection);
2980
2981 // Remove the packets received so far
2982 mockProfilingConnection->Clear();
2983
2984 // Write a "Per-Job Counter Selection" packet into the mock profiling connection, to simulate an input from an
2985 // external profiling service
2986
2987 // Per-Job Counter Selection packet header:
2988 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2989 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2990 // 8:15 [8] reserved: Reserved, value 0b00000000
2991 // 0:7 [8] reserved: Reserved, value 0b00000000
2992 uint32_t packetFamily = 0;
2993 uint32_t packetId = 5;
Keith Davis3201eea2019-10-24 17:30:41 +01002994 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincigh994b5342019-10-11 17:19:56 +01002995
2996 // Create the Per-Job Counter Selection packet
Keith Davis3201eea2019-10-24 17:30:41 +01002997 Packet periodicCounterSelectionPacket(header); // Length == 0, this will disable the collection of counters
Matteo Martincigh994b5342019-10-11 17:19:56 +01002998
2999 // Write the packet to the mock profiling connection
3000 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
3001
3002 // Wait for a bit (must at least be the delay value of the mock profiling connection) to make sure that
3003 // the Per-Job Counter Selection packet gets processed by the profiling service
3004 std::this_thread::sleep_for(std::chrono::seconds(2));
3005
3006 // The Per-Job Counter Selection packets are dropped silently, so there should be no reply coming
3007 // from the profiling service
3008 const std::vector<uint32_t> writtenData = mockProfilingConnection->GetWrittenData();
3009 BOOST_TEST(writtenData.empty());
3010
3011 // The Per-Job Counter Selection Command Handler should not have updated the profiling state
3012 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
3013
3014 // Reset the profiling service to stop any running thread
3015 options.m_EnableProfiling = false;
3016 profilingService.ResetExternalProfilingOptions(options, true);
3017}
3018
Jim Flynn672d06e2019-10-15 10:18:11 +01003019BOOST_AUTO_TEST_CASE(CheckConfigureProfilingServiceOn)
3020{
3021 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01003022 options.m_EnableProfiling = true;
Jim Flynn672d06e2019-10-15 10:18:11 +01003023 ProfilingService& profilingService = ProfilingService::Instance();
3024 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3025 profilingService.ConfigureProfilingService(options);
3026 // should get as far as NOT_CONNECTED
3027 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
3028 // Reset the profiling service to stop any running thread
3029 options.m_EnableProfiling = false;
3030 profilingService.ResetExternalProfilingOptions(options, true);
3031}
3032
3033BOOST_AUTO_TEST_CASE(CheckConfigureProfilingServiceOff)
3034{
3035 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
3036 ProfilingService& profilingService = ProfilingService::Instance();
3037 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3038 profilingService.ConfigureProfilingService(options);
3039 // should not move from Uninitialised
3040 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3041 // Reset the profiling service to stop any running thread
3042 options.m_EnableProfiling = false;
3043 profilingService.ResetExternalProfilingOptions(options, true);
3044}
3045
Francis Murtagh1f7db452019-08-14 09:49:34 +01003046BOOST_AUTO_TEST_SUITE_END()