blob: bc16bb952b19821150e9e22d8c1e0049873bd6e3 [file] [log] [blame]
Francis Murtagh1f7db452019-08-14 09:49:34 +01001//
Jim Flynn6398a982020-05-27 17:05:21 +01002// Copyright © 2019 Arm Ltd and Contributors. All rights reserved.
Francis Murtagh1f7db452019-08-14 09:49:34 +01003// SPDX-License-Identifier: MIT
4//
5
Matteo Martincighd0613b52019-10-09 16:47:04 +01006#include "ProfilingTests.hpp"
Keith Davis33ed2212020-03-30 10:43:41 +01007#include "ProfilingTestUtils.hpp"
Teresa Charlin9bab4962019-09-06 12:28:35 +01008
James Conroy2dcd3fe2020-02-06 18:34:52 +00009#include <backends/BackendProfiling.hpp>
Matteo Martincigh8a837172019-10-04 17:01:07 +010010#include <CommandHandler.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010011#include <CommandHandlerKey.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010012#include <CommandHandlerRegistry.hpp>
Sadik Armagana97a0be2020-03-03 10:44:56 +000013#include <common/include/SocketConnectionException.hpp>
Sadik Armaganb5f01b22019-09-18 17:29:00 +010014#include <ConnectionAcknowledgedCommandHandler.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010015#include <CounterDirectory.hpp>
David Monahande803072020-01-30 12:44:23 +000016#include <CounterIdMap.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010017#include <EncodeVersion.hpp>
18#include <Holder.hpp>
Matteo Martincighe0e6efc2019-10-04 17:17:42 +010019#include <ICounterValues.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010020#include <Packet.hpp>
21#include <PacketVersionResolver.hpp>
Francis Murtaghfcb8ef62019-09-20 15:40:09 +010022#include <PeriodicCounterCapture.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010023#include <PeriodicCounterSelectionCommandHandler.hpp>
24#include <ProfilingStateMachine.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010025#include <ProfilingUtils.hpp>
James Conroy2dcd3fe2020-02-06 18:34:52 +000026#include <RegisterBackendCounters.hpp>
Narumol Prangnawarat48033692019-09-20 12:04:55 +010027#include <RequestCounterDirectoryCommandHandler.hpp>
Teresa Charlin9bab4962019-09-06 12:28:35 +010028#include <Runtime.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010029#include <SocketProfilingConnection.hpp>
Matteo Martincighcdfb9412019-11-08 11:23:06 +000030#include <SendCounterPacket.hpp>
Sadik Armagan3896b472020-02-10 12:24:15 +000031#include <SendThread.hpp>
Matteo Martincighcdfb9412019-11-08 11:23:06 +000032#include <SendTimelinePacket.hpp>
Keith Davis02356de2019-08-26 18:28:17 +010033
Matteo Martincigh6db5f202019-09-05 12:02:04 +010034#include <armnn/Conversion.hpp>
Colm Donelan02705242019-11-14 14:19:07 +000035#include <armnn/Types.hpp>
Ferran Balaguer1b941722019-08-28 16:57:18 +010036
Matteo Martincigh54fb9572019-10-02 12:50:57 +010037#include <armnn/Utils.hpp>
Jan Eilers8eb25602020-03-09 12:13:48 +000038#include <armnn/utility/IgnoreUnused.hpp>
Matteo Martincigh54fb9572019-10-02 12:50:57 +010039
Ferran Balaguer1b941722019-08-28 16:57:18 +010040#include <boost/numeric/conversion/cast.hpp>
Francis Murtagh1f7db452019-08-14 09:49:34 +010041
Nikhil Rajbc626052019-08-15 15:49:45 +010042#include <cstdint>
43#include <cstring>
Keith Davis3201eea2019-10-24 17:30:41 +010044#include <iostream>
Aron Virginas-Tare898db92019-08-22 12:56:34 +010045#include <limits>
Francis Murtagh11f99b42019-08-16 11:28:52 +010046#include <map>
Aron Virginas-Tare898db92019-08-22 12:56:34 +010047#include <random>
James Conroy2dcd3fe2020-02-06 18:34:52 +000048
Francis Murtagh1f7db452019-08-14 09:49:34 +010049
Aron Virginas-Tare898db92019-08-22 12:56:34 +010050using namespace armnn::profiling;
Finn Williams09ad6f92019-12-19 17:05:18 +000051using PacketType = MockProfilingConnection::PacketType;
Aron Virginas-Tare898db92019-08-22 12:56:34 +010052
Matteo Martincigh8a837172019-10-04 17:01:07 +010053BOOST_AUTO_TEST_SUITE(ExternalProfiling)
54
Francis Murtagh1f7db452019-08-14 09:49:34 +010055BOOST_AUTO_TEST_CASE(CheckCommandHandlerKeyComparisons)
56{
Jim Flynn397043f2019-10-17 17:37:10 +010057 CommandHandlerKey testKey1_0(1, 1, 1);
58 CommandHandlerKey testKey1_1(1, 1, 1);
59 CommandHandlerKey testKey1_2(1, 2, 1);
60
61 CommandHandlerKey testKey0(0, 1, 1);
62 CommandHandlerKey testKey1(0, 1, 1);
63 CommandHandlerKey testKey2(0, 1, 1);
64 CommandHandlerKey testKey3(0, 0, 0);
65 CommandHandlerKey testKey4(0, 2, 2);
66 CommandHandlerKey testKey5(0, 0, 2);
67
68 BOOST_CHECK(testKey1_0 > testKey0);
69 BOOST_CHECK(testKey1_0 == testKey1_1);
70 BOOST_CHECK(testKey1_0 < testKey1_2);
Francis Murtagh1f7db452019-08-14 09:49:34 +010071
Keith Davis3201eea2019-10-24 17:30:41 +010072 BOOST_CHECK(testKey1 < testKey4);
73 BOOST_CHECK(testKey1 > testKey3);
74 BOOST_CHECK(testKey1 <= testKey4);
75 BOOST_CHECK(testKey1 >= testKey3);
76 BOOST_CHECK(testKey1 <= testKey2);
77 BOOST_CHECK(testKey1 >= testKey2);
78 BOOST_CHECK(testKey1 == testKey2);
79 BOOST_CHECK(testKey1 == testKey1);
Francis Murtagh1f7db452019-08-14 09:49:34 +010080
Keith Davis3201eea2019-10-24 17:30:41 +010081 BOOST_CHECK(!(testKey1 == testKey5));
82 BOOST_CHECK(!(testKey1 != testKey1));
83 BOOST_CHECK(testKey1 != testKey5);
Francis Murtagh1f7db452019-08-14 09:49:34 +010084
Keith Davis3201eea2019-10-24 17:30:41 +010085 BOOST_CHECK(testKey1 == testKey2 && testKey2 == testKey1);
86 BOOST_CHECK(testKey0 == testKey1 && testKey1 == testKey2 && testKey0 == testKey2);
Francis Murtagh1f7db452019-08-14 09:49:34 +010087
Keith Davis3201eea2019-10-24 17:30:41 +010088 BOOST_CHECK(testKey1.GetPacketId() == 1);
89 BOOST_CHECK(testKey1.GetVersion() == 1);
Francis Murtagh1f7db452019-08-14 09:49:34 +010090
Keith Davis3201eea2019-10-24 17:30:41 +010091 std::vector<CommandHandlerKey> vect = { CommandHandlerKey(0, 0, 1), CommandHandlerKey(0, 2, 0),
92 CommandHandlerKey(0, 1, 0), CommandHandlerKey(0, 2, 1),
93 CommandHandlerKey(0, 1, 1), CommandHandlerKey(0, 0, 1),
94 CommandHandlerKey(0, 2, 0), CommandHandlerKey(0, 0, 0) };
Francis Murtagh1f7db452019-08-14 09:49:34 +010095
96 std::sort(vect.begin(), vect.end());
97
Keith Davis3201eea2019-10-24 17:30:41 +010098 std::vector<CommandHandlerKey> expectedVect = { CommandHandlerKey(0, 0, 0), CommandHandlerKey(0, 0, 1),
99 CommandHandlerKey(0, 0, 1), CommandHandlerKey(0, 1, 0),
100 CommandHandlerKey(0, 1, 1), CommandHandlerKey(0, 2, 0),
101 CommandHandlerKey(0, 2, 0), CommandHandlerKey(0, 2, 1) };
Francis Murtagh1f7db452019-08-14 09:49:34 +0100102
103 BOOST_CHECK(vect == expectedVect);
104}
105
Jim Flynned25e0e2019-10-18 13:21:43 +0100106BOOST_AUTO_TEST_CASE(CheckPacketKeyComparisons)
107{
Keith Davis3201eea2019-10-24 17:30:41 +0100108 PacketKey key0(0, 0);
109 PacketKey key1(0, 0);
110 PacketKey key2(0, 1);
111 PacketKey key3(0, 2);
112 PacketKey key4(1, 0);
113 PacketKey key5(1, 0);
114 PacketKey key6(1, 1);
Jim Flynned25e0e2019-10-18 13:21:43 +0100115
116 BOOST_CHECK(!(key0 < key1));
117 BOOST_CHECK(!(key0 > key1));
118 BOOST_CHECK(key0 <= key1);
119 BOOST_CHECK(key0 >= key1);
120 BOOST_CHECK(key0 == key1);
121 BOOST_CHECK(key0 < key2);
122 BOOST_CHECK(key2 < key3);
123 BOOST_CHECK(key3 > key0);
124 BOOST_CHECK(key4 == key5);
125 BOOST_CHECK(key4 > key0);
126 BOOST_CHECK(key5 < key6);
127 BOOST_CHECK(key5 <= key6);
128 BOOST_CHECK(key5 != key6);
129}
130
Matteo Martincigh8a837172019-10-04 17:01:07 +0100131BOOST_AUTO_TEST_CASE(CheckCommandHandler)
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100132{
Matteo Martincigh8a837172019-10-04 17:01:07 +0100133 PacketVersionResolver packetVersionResolver;
134 ProfilingStateMachine profilingStateMachine;
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100135
Matteo Martincigh8a837172019-10-04 17:01:07 +0100136 TestProfilingConnectionBase testProfilingConnectionBase;
137 TestProfilingConnectionTimeoutError testProfilingConnectionTimeOutError;
138 TestProfilingConnectionArmnnError testProfilingConnectionArmnnError;
Keith Davis3201eea2019-10-24 17:30:41 +0100139 CounterDirectory counterDirectory;
140 MockBufferManager mockBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +0000141 SendCounterPacket sendCounterPacket(mockBuffer);
142 SendThread sendThread(profilingStateMachine, mockBuffer, sendCounterPacket);
Matteo Martincighcdfb9412019-11-08 11:23:06 +0000143 SendTimelinePacket sendTimelinePacket(mockBuffer);
Jim Flynn6398a982020-05-27 17:05:21 +0100144 MockProfilingServiceStatus mockProfilingServiceStatus;
Matteo Martincighcdfb9412019-11-08 11:23:06 +0000145
Keith Davis3201eea2019-10-24 17:30:41 +0100146 ConnectionAcknowledgedCommandHandler connectionAcknowledgedCommandHandler(0, 1, 4194304, counterDirectory,
Matteo Martincighcdfb9412019-11-08 11:23:06 +0000147 sendCounterPacket, sendTimelinePacket,
Jim Flynn6398a982020-05-27 17:05:21 +0100148 profilingStateMachine,
149 mockProfilingServiceStatus);
Matteo Martincigh8a837172019-10-04 17:01:07 +0100150 CommandHandlerRegistry commandHandlerRegistry;
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100151
Matteo Martincighc2728f92019-10-07 12:35:21 +0100152 commandHandlerRegistry.RegisterFunctor(&connectionAcknowledgedCommandHandler);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100153
Matteo Martincigh8a837172019-10-04 17:01:07 +0100154 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
155 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100156
Keith Davis3201eea2019-10-24 17:30:41 +0100157 CommandHandler commandHandler0(1, true, commandHandlerRegistry, packetVersionResolver);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100158
Colm Donelan2ba48d22019-11-29 09:10:59 +0000159 // This should start the command handler thread return the connection ack and put the profiling
160 // service into active state.
Matteo Martincigh8a837172019-10-04 17:01:07 +0100161 commandHandler0.Start(testProfilingConnectionBase);
Colm Donelan2ba48d22019-11-29 09:10:59 +0000162 // Try to start the send thread many times, it must only start once
Matteo Martincigh8a837172019-10-04 17:01:07 +0100163 commandHandler0.Start(testProfilingConnectionBase);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100164
Colm Donelan2ba48d22019-11-29 09:10:59 +0000165 // This could take up to 20mSec but we'll check often.
166 for (int i = 0; i < 10; i++)
Matteo Martincigh8a837172019-10-04 17:01:07 +0100167 {
168 if (profilingStateMachine.GetCurrentState() == ProfilingState::Active)
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100169 {
Matteo Martincigh8a837172019-10-04 17:01:07 +0100170 break;
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100171 }
Colm Donelan2ba48d22019-11-29 09:10:59 +0000172 std::this_thread::sleep_for(std::chrono::milliseconds(2));
Matteo Martincigh8a837172019-10-04 17:01:07 +0100173 }
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100174
Matteo Martincigh8a837172019-10-04 17:01:07 +0100175 BOOST_CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::Active);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100176
Colm Donelan2ba48d22019-11-29 09:10:59 +0000177 // Close the thread again.
178 commandHandler0.Stop();
179
180 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
181 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
182
183 // In this test we'll simulate a timeout without a connection ack packet being received.
184 // Stop after timeout is set so we expect the command handler to stop almost immediately.
185 CommandHandler commandHandler1(1, true, commandHandlerRegistry, packetVersionResolver);
186
187 commandHandler1.Start(testProfilingConnectionTimeOutError);
188 // Wait until we know a timeout exception has been sent at least once.
189 for (int i = 0; i < 10; i++)
190 {
191 if (testProfilingConnectionTimeOutError.ReadCalledCount())
192 {
193 break;
194 }
195 std::this_thread::sleep_for(std::chrono::milliseconds(2));
196 }
Colm Donelan2ba48d22019-11-29 09:10:59 +0000197
198 // The command handler loop should have stopped after the timeout.
Finn Williams09ad6f92019-12-19 17:05:18 +0000199 // wait for the timeout exception to be processed and the loop to break.
200 uint32_t timeout = 50;
201 uint32_t timeSlept = 0;
202 while (commandHandler1.IsRunning())
203 {
204 if (timeSlept >= timeout)
205 {
206 BOOST_FAIL("Timeout: The command handler loop did not stop after the timeout");
207 }
208 std::this_thread::sleep_for(std::chrono::milliseconds(1));
209 timeSlept ++;
210 }
Colm Donelan2ba48d22019-11-29 09:10:59 +0000211
212 commandHandler1.Stop();
213 // The state machine should never have received the ack so will still be in WaitingForAck.
214 BOOST_CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::WaitingForAck);
215
Finn Williams09ad6f92019-12-19 17:05:18 +0000216 // Now try sending a bad connection acknowledged packet
217 TestProfilingConnectionBadAckPacket testProfilingConnectionBadAckPacket;
218 commandHandler1.Start(testProfilingConnectionBadAckPacket);
219 commandHandler1.Stop();
220 // This should also not change the state machine
221 BOOST_CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::WaitingForAck);
222
Colm Donelan2ba48d22019-11-29 09:10:59 +0000223 // Disable stop after timeout and now commandHandler1 should persist after a timeout
224 commandHandler1.SetStopAfterTimeout(false);
225 // Restart the thread.
226 commandHandler1.Start(testProfilingConnectionTimeOutError);
227
228 // Wait for at the three timeouts and the ack to be sent.
229 for (int i = 0; i < 10; i++)
230 {
231 if (testProfilingConnectionTimeOutError.ReadCalledCount() > 3)
232 {
233 break;
234 }
235 std::this_thread::sleep_for(std::chrono::milliseconds(2));
236 }
237 commandHandler1.Stop();
238
239 // Even after the 3 exceptions the ack packet should have transitioned the command handler to active.
240 BOOST_CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::Active);
241
242 // A command handler that gets exceptions other than timeouts should keep going.
243 CommandHandler commandHandler2(1, false, commandHandlerRegistry, packetVersionResolver);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100244
Matteo Martincigh8a837172019-10-04 17:01:07 +0100245 commandHandler2.Start(testProfilingConnectionArmnnError);
246
Colm Donelan2ba48d22019-11-29 09:10:59 +0000247 // Wait for two exceptions to be thrown.
248 for (int i = 0; i < 10; i++)
249 {
250 if (testProfilingConnectionTimeOutError.ReadCalledCount() >= 2)
251 {
252 break;
253 }
254 std::this_thread::sleep_for(std::chrono::milliseconds(2));
255 }
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100256
Matteo Martincighd0613b52019-10-09 16:47:04 +0100257 BOOST_CHECK(commandHandler2.IsRunning());
Matteo Martincigh8a837172019-10-04 17:01:07 +0100258 commandHandler2.Stop();
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100259}
260
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100261BOOST_AUTO_TEST_CASE(CheckEncodeVersion)
262{
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100263 Version version1(12);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100264
265 BOOST_CHECK(version1.GetMajor() == 0);
266 BOOST_CHECK(version1.GetMinor() == 0);
267 BOOST_CHECK(version1.GetPatch() == 12);
268
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100269 Version version2(4108);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100270
271 BOOST_CHECK(version2.GetMajor() == 0);
272 BOOST_CHECK(version2.GetMinor() == 1);
273 BOOST_CHECK(version2.GetPatch() == 12);
274
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100275 Version version3(4198412);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100276
277 BOOST_CHECK(version3.GetMajor() == 1);
278 BOOST_CHECK(version3.GetMinor() == 1);
279 BOOST_CHECK(version3.GetPatch() == 12);
280
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100281 Version version4(0);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100282
283 BOOST_CHECK(version4.GetMajor() == 0);
284 BOOST_CHECK(version4.GetMinor() == 0);
285 BOOST_CHECK(version4.GetPatch() == 0);
286
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100287 Version version5(1, 0, 0);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100288 BOOST_CHECK(version5.GetEncodedValue() == 4194304);
289}
290
Nikhil Rajbc626052019-08-15 15:49:45 +0100291BOOST_AUTO_TEST_CASE(CheckPacketClass)
292{
Keith Davis3201eea2019-10-24 17:30:41 +0100293 uint32_t length = 4;
Matteo Martincigh67ef2a52019-10-10 13:29:02 +0100294 std::unique_ptr<unsigned char[]> packetData0 = std::make_unique<unsigned char[]>(length);
295 std::unique_ptr<unsigned char[]> packetData1 = std::make_unique<unsigned char[]>(0);
296 std::unique_ptr<unsigned char[]> nullPacketData;
Nikhil Rajbc626052019-08-15 15:49:45 +0100297
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100298 Packet packetTest0(472580096, length, packetData0);
Nikhil Rajbc626052019-08-15 15:49:45 +0100299
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100300 BOOST_CHECK(packetTest0.GetHeader() == 472580096);
301 BOOST_CHECK(packetTest0.GetPacketFamily() == 7);
302 BOOST_CHECK(packetTest0.GetPacketId() == 43);
303 BOOST_CHECK(packetTest0.GetLength() == length);
304 BOOST_CHECK(packetTest0.GetPacketType() == 3);
305 BOOST_CHECK(packetTest0.GetPacketClass() == 5);
Nikhil Rajbc626052019-08-15 15:49:45 +0100306
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100307 BOOST_CHECK_THROW(Packet packetTest1(472580096, 0, packetData1), armnn::Exception);
308 BOOST_CHECK_NO_THROW(Packet packetTest2(472580096, 0, nullPacketData));
Nikhil Rajbc626052019-08-15 15:49:45 +0100309
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100310 Packet packetTest3(472580096, 0, nullPacketData);
311 BOOST_CHECK(packetTest3.GetLength() == 0);
312 BOOST_CHECK(packetTest3.GetData() == nullptr);
313
Matteo Martincigh67ef2a52019-10-10 13:29:02 +0100314 const unsigned char* packetTest0Data = packetTest0.GetData();
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100315 Packet packetTest4(std::move(packetTest0));
316
317 BOOST_CHECK(packetTest0.GetData() == nullptr);
318 BOOST_CHECK(packetTest4.GetData() == packetTest0Data);
319
320 BOOST_CHECK(packetTest4.GetHeader() == 472580096);
321 BOOST_CHECK(packetTest4.GetPacketFamily() == 7);
322 BOOST_CHECK(packetTest4.GetPacketId() == 43);
323 BOOST_CHECK(packetTest4.GetLength() == length);
324 BOOST_CHECK(packetTest4.GetPacketType() == 3);
325 BOOST_CHECK(packetTest4.GetPacketClass() == 5);
Nikhil Rajbc626052019-08-15 15:49:45 +0100326}
327
Francis Murtagh11f99b42019-08-16 11:28:52 +0100328BOOST_AUTO_TEST_CASE(CheckCommandHandlerFunctor)
329{
Francis Murtagh11f99b42019-08-16 11:28:52 +0100330 // Hard code the version as it will be the same during a single profiling session
331 uint32_t version = 1;
332
Jim Flynn397043f2019-10-17 17:37:10 +0100333 TestFunctorA testFunctorA(7, 461, version);
334 TestFunctorB testFunctorB(8, 963, version);
335 TestFunctorC testFunctorC(5, 983, version);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100336
Jim Flynn397043f2019-10-17 17:37:10 +0100337 CommandHandlerKey keyA(testFunctorA.GetFamilyId(), testFunctorA.GetPacketId(), testFunctorA.GetVersion());
338 CommandHandlerKey keyB(testFunctorB.GetFamilyId(), testFunctorB.GetPacketId(), testFunctorB.GetVersion());
339 CommandHandlerKey keyC(testFunctorC.GetFamilyId(), testFunctorC.GetPacketId(), testFunctorC.GetVersion());
Francis Murtagh11f99b42019-08-16 11:28:52 +0100340
341 // Create the unwrapped map to simulate the Command Handler Registry
342 std::map<CommandHandlerKey, CommandHandlerFunctor*> registry;
343
344 registry.insert(std::make_pair(keyB, &testFunctorB));
345 registry.insert(std::make_pair(keyA, &testFunctorA));
346 registry.insert(std::make_pair(keyC, &testFunctorC));
347
348 // Check the order of the map is correct
349 auto it = registry.begin();
Keith Davis3201eea2019-10-24 17:30:41 +0100350 BOOST_CHECK(it->first == keyC); // familyId == 5
Francis Murtagh11f99b42019-08-16 11:28:52 +0100351 it++;
Keith Davis3201eea2019-10-24 17:30:41 +0100352 BOOST_CHECK(it->first == keyA); // familyId == 7
Francis Murtagh11f99b42019-08-16 11:28:52 +0100353 it++;
Keith Davis3201eea2019-10-24 17:30:41 +0100354 BOOST_CHECK(it->first == keyB); // familyId == 8
Francis Murtagh11f99b42019-08-16 11:28:52 +0100355
Matteo Martincigh67ef2a52019-10-10 13:29:02 +0100356 std::unique_ptr<unsigned char[]> packetDataA;
357 std::unique_ptr<unsigned char[]> packetDataB;
358 std::unique_ptr<unsigned char[]> packetDataC;
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100359
360 Packet packetA(500000000, 0, packetDataA);
361 Packet packetB(600000000, 0, packetDataB);
362 Packet packetC(400000000, 0, packetDataC);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100363
364 // Check the correct operator of derived class is called
Jim Flynn397043f2019-10-17 17:37:10 +0100365 registry.at(CommandHandlerKey(packetA.GetPacketFamily(), packetA.GetPacketId(), version))->operator()(packetA);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100366 BOOST_CHECK(testFunctorA.GetCount() == 1);
367 BOOST_CHECK(testFunctorB.GetCount() == 0);
368 BOOST_CHECK(testFunctorC.GetCount() == 0);
369
Jim Flynn397043f2019-10-17 17:37:10 +0100370 registry.at(CommandHandlerKey(packetB.GetPacketFamily(), packetB.GetPacketId(), version))->operator()(packetB);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100371 BOOST_CHECK(testFunctorA.GetCount() == 1);
372 BOOST_CHECK(testFunctorB.GetCount() == 1);
373 BOOST_CHECK(testFunctorC.GetCount() == 0);
374
Jim Flynn397043f2019-10-17 17:37:10 +0100375 registry.at(CommandHandlerKey(packetC.GetPacketFamily(), packetC.GetPacketId(), version))->operator()(packetC);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100376 BOOST_CHECK(testFunctorA.GetCount() == 1);
377 BOOST_CHECK(testFunctorB.GetCount() == 1);
378 BOOST_CHECK(testFunctorC.GetCount() == 1);
379}
380
Francis Murtagh94d79152019-08-16 17:45:07 +0100381BOOST_AUTO_TEST_CASE(CheckCommandHandlerRegistry)
382{
383 // Hard code the version as it will be the same during a single profiling session
384 uint32_t version = 1;
385
Jim Flynn397043f2019-10-17 17:37:10 +0100386 TestFunctorA testFunctorA(7, 461, version);
387 TestFunctorB testFunctorB(8, 963, version);
388 TestFunctorC testFunctorC(5, 983, version);
Francis Murtagh94d79152019-08-16 17:45:07 +0100389
390 // Create the Command Handler Registry
391 CommandHandlerRegistry registry;
392
393 // Register multiple different derived classes
Matteo Martincighc2728f92019-10-07 12:35:21 +0100394 registry.RegisterFunctor(&testFunctorA);
395 registry.RegisterFunctor(&testFunctorB);
396 registry.RegisterFunctor(&testFunctorC);
Francis Murtagh94d79152019-08-16 17:45:07 +0100397
Matteo Martincigh67ef2a52019-10-10 13:29:02 +0100398 std::unique_ptr<unsigned char[]> packetDataA;
399 std::unique_ptr<unsigned char[]> packetDataB;
400 std::unique_ptr<unsigned char[]> packetDataC;
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100401
402 Packet packetA(500000000, 0, packetDataA);
403 Packet packetB(600000000, 0, packetDataB);
404 Packet packetC(400000000, 0, packetDataC);
Francis Murtagh94d79152019-08-16 17:45:07 +0100405
406 // Check the correct operator of derived class is called
Jim Flynn397043f2019-10-17 17:37:10 +0100407 registry.GetFunctor(packetA.GetPacketFamily(), packetA.GetPacketId(), version)->operator()(packetA);
Francis Murtagh94d79152019-08-16 17:45:07 +0100408 BOOST_CHECK(testFunctorA.GetCount() == 1);
409 BOOST_CHECK(testFunctorB.GetCount() == 0);
410 BOOST_CHECK(testFunctorC.GetCount() == 0);
411
Jim Flynn397043f2019-10-17 17:37:10 +0100412 registry.GetFunctor(packetB.GetPacketFamily(), packetB.GetPacketId(), version)->operator()(packetB);
Francis Murtagh94d79152019-08-16 17:45:07 +0100413 BOOST_CHECK(testFunctorA.GetCount() == 1);
414 BOOST_CHECK(testFunctorB.GetCount() == 1);
415 BOOST_CHECK(testFunctorC.GetCount() == 0);
416
Jim Flynn397043f2019-10-17 17:37:10 +0100417 registry.GetFunctor(packetC.GetPacketFamily(), packetC.GetPacketId(), version)->operator()(packetC);
Francis Murtagh94d79152019-08-16 17:45:07 +0100418 BOOST_CHECK(testFunctorA.GetCount() == 1);
419 BOOST_CHECK(testFunctorB.GetCount() == 1);
420 BOOST_CHECK(testFunctorC.GetCount() == 1);
421
422 // Re-register an existing key with a new function
Jim Flynn397043f2019-10-17 17:37:10 +0100423 registry.RegisterFunctor(&testFunctorC, testFunctorA.GetFamilyId(), testFunctorA.GetPacketId(), version);
424 registry.GetFunctor(packetA.GetPacketFamily(), packetA.GetPacketId(), version)->operator()(packetC);
Francis Murtagh94d79152019-08-16 17:45:07 +0100425 BOOST_CHECK(testFunctorA.GetCount() == 1);
426 BOOST_CHECK(testFunctorB.GetCount() == 1);
427 BOOST_CHECK(testFunctorC.GetCount() == 2);
428
429 // Check that non-existent key returns nullptr for its functor
Jim Flynn397043f2019-10-17 17:37:10 +0100430 BOOST_CHECK_THROW(registry.GetFunctor(0, 0, 0), armnn::Exception);
Francis Murtagh94d79152019-08-16 17:45:07 +0100431}
432
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100433BOOST_AUTO_TEST_CASE(CheckPacketVersionResolver)
434{
435 // Set up random number generator for generating packetId values
436 std::random_device device;
437 std::mt19937 generator(device());
438 std::uniform_int_distribution<uint32_t> distribution(std::numeric_limits<uint32_t>::min(),
439 std::numeric_limits<uint32_t>::max());
440
441 // NOTE: Expected version is always 1.0.0, regardless of packetId
442 const Version expectedVersion(1, 0, 0);
443
444 PacketVersionResolver packetVersionResolver;
445
446 constexpr unsigned int numTests = 10u;
447
448 for (unsigned int i = 0u; i < numTests; ++i)
449 {
Jim Flynned25e0e2019-10-18 13:21:43 +0100450 const uint32_t familyId = distribution(generator);
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100451 const uint32_t packetId = distribution(generator);
Jim Flynned25e0e2019-10-18 13:21:43 +0100452 Version resolvedVersion = packetVersionResolver.ResolvePacketVersion(familyId, packetId);
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100453
454 BOOST_TEST(resolvedVersion == expectedVersion);
455 }
456}
Matteo Martincighd0613b52019-10-09 16:47:04 +0100457
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100458void ProfilingCurrentStateThreadImpl(ProfilingStateMachine& states)
459{
460 ProfilingState newState = ProfilingState::NotConnected;
461 states.GetCurrentState();
462 states.TransitionToState(newState);
463}
464
465BOOST_AUTO_TEST_CASE(CheckProfilingStateMachine)
466{
467 ProfilingStateMachine profilingState1(ProfilingState::Uninitialised);
468 profilingState1.TransitionToState(ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +0100469 BOOST_CHECK(profilingState1.GetCurrentState() == ProfilingState::Uninitialised);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100470
471 ProfilingStateMachine profilingState2(ProfilingState::Uninitialised);
472 profilingState2.TransitionToState(ProfilingState::NotConnected);
473 BOOST_CHECK(profilingState2.GetCurrentState() == ProfilingState::NotConnected);
474
475 ProfilingStateMachine profilingState3(ProfilingState::NotConnected);
476 profilingState3.TransitionToState(ProfilingState::NotConnected);
477 BOOST_CHECK(profilingState3.GetCurrentState() == ProfilingState::NotConnected);
478
479 ProfilingStateMachine profilingState4(ProfilingState::NotConnected);
480 profilingState4.TransitionToState(ProfilingState::WaitingForAck);
481 BOOST_CHECK(profilingState4.GetCurrentState() == ProfilingState::WaitingForAck);
482
483 ProfilingStateMachine profilingState5(ProfilingState::WaitingForAck);
484 profilingState5.TransitionToState(ProfilingState::WaitingForAck);
485 BOOST_CHECK(profilingState5.GetCurrentState() == ProfilingState::WaitingForAck);
486
487 ProfilingStateMachine profilingState6(ProfilingState::WaitingForAck);
488 profilingState6.TransitionToState(ProfilingState::Active);
489 BOOST_CHECK(profilingState6.GetCurrentState() == ProfilingState::Active);
490
491 ProfilingStateMachine profilingState7(ProfilingState::Active);
492 profilingState7.TransitionToState(ProfilingState::NotConnected);
493 BOOST_CHECK(profilingState7.GetCurrentState() == ProfilingState::NotConnected);
494
495 ProfilingStateMachine profilingState8(ProfilingState::Active);
496 profilingState8.TransitionToState(ProfilingState::Active);
497 BOOST_CHECK(profilingState8.GetCurrentState() == ProfilingState::Active);
498
499 ProfilingStateMachine profilingState9(ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +0100500 BOOST_CHECK_THROW(profilingState9.TransitionToState(ProfilingState::WaitingForAck), armnn::Exception);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100501
502 ProfilingStateMachine profilingState10(ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +0100503 BOOST_CHECK_THROW(profilingState10.TransitionToState(ProfilingState::Active), armnn::Exception);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100504
505 ProfilingStateMachine profilingState11(ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +0100506 BOOST_CHECK_THROW(profilingState11.TransitionToState(ProfilingState::Uninitialised), armnn::Exception);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100507
508 ProfilingStateMachine profilingState12(ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +0100509 BOOST_CHECK_THROW(profilingState12.TransitionToState(ProfilingState::Active), armnn::Exception);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100510
511 ProfilingStateMachine profilingState13(ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +0100512 BOOST_CHECK_THROW(profilingState13.TransitionToState(ProfilingState::Uninitialised), armnn::Exception);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100513
514 ProfilingStateMachine profilingState14(ProfilingState::WaitingForAck);
Jim Flynn53e46992019-10-14 12:31:10 +0100515 profilingState14.TransitionToState(ProfilingState::NotConnected);
516 BOOST_CHECK(profilingState14.GetCurrentState() == ProfilingState::NotConnected);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100517
518 ProfilingStateMachine profilingState15(ProfilingState::Active);
Keith Davis3201eea2019-10-24 17:30:41 +0100519 BOOST_CHECK_THROW(profilingState15.TransitionToState(ProfilingState::Uninitialised), armnn::Exception);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100520
521 ProfilingStateMachine profilingState16(armnn::profiling::ProfilingState::Active);
Keith Davis3201eea2019-10-24 17:30:41 +0100522 BOOST_CHECK_THROW(profilingState16.TransitionToState(ProfilingState::WaitingForAck), armnn::Exception);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100523
524 ProfilingStateMachine profilingState17(ProfilingState::Uninitialised);
525
Keith Davis3201eea2019-10-24 17:30:41 +0100526 std::thread thread1(ProfilingCurrentStateThreadImpl, std::ref(profilingState17));
527 std::thread thread2(ProfilingCurrentStateThreadImpl, std::ref(profilingState17));
528 std::thread thread3(ProfilingCurrentStateThreadImpl, std::ref(profilingState17));
529 std::thread thread4(ProfilingCurrentStateThreadImpl, std::ref(profilingState17));
530 std::thread thread5(ProfilingCurrentStateThreadImpl, std::ref(profilingState17));
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100531
532 thread1.join();
533 thread2.join();
534 thread3.join();
535 thread4.join();
536 thread5.join();
537
538 BOOST_TEST((profilingState17.GetCurrentState() == ProfilingState::NotConnected));
539}
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100540
Jim Flynn8355ec92019-09-17 12:29:50 +0100541void CaptureDataWriteThreadImpl(Holder& holder, uint32_t capturePeriod, const std::vector<uint16_t>& counterIds)
Francis Murtagh68f78d82019-09-04 16:42:29 +0100542{
Finn Williams032bc742020-02-12 11:02:34 +0000543 holder.SetCaptureData(capturePeriod, counterIds, {});
Francis Murtagh68f78d82019-09-04 16:42:29 +0100544}
545
Francis Murtaghbd707162019-09-09 11:26:44 +0100546void CaptureDataReadThreadImpl(const Holder& holder, CaptureData& captureData)
Francis Murtagh68f78d82019-09-04 16:42:29 +0100547{
548 captureData = holder.GetCaptureData();
549}
550
551BOOST_AUTO_TEST_CASE(CheckCaptureDataHolder)
552{
Francis Murtaghbd707162019-09-09 11:26:44 +0100553 std::map<uint32_t, std::vector<uint16_t>> periodIdMap;
554 std::vector<uint16_t> counterIds;
Jim Flynn8355ec92019-09-17 12:29:50 +0100555 uint32_t numThreads = 10;
556 for (uint32_t i = 0; i < numThreads; ++i)
Francis Murtaghbd707162019-09-09 11:26:44 +0100557 {
558 counterIds.emplace_back(i);
559 periodIdMap.insert(std::make_pair(i, counterIds));
560 }
Francis Murtagh68f78d82019-09-04 16:42:29 +0100561
Jim Flynn8355ec92019-09-17 12:29:50 +0100562 // Verify the read and write threads set the holder correctly
563 // and retrieve the expected values
Francis Murtagh68f78d82019-09-04 16:42:29 +0100564 Holder holder;
565 BOOST_CHECK((holder.GetCaptureData()).GetCapturePeriod() == 0);
566 BOOST_CHECK(((holder.GetCaptureData()).GetCounterIds()).empty());
567
568 // Check Holder functions
Francis Murtaghbd707162019-09-09 11:26:44 +0100569 std::thread thread1(CaptureDataWriteThreadImpl, std::ref(holder), 2, std::ref(periodIdMap[2]));
Francis Murtagh68f78d82019-09-04 16:42:29 +0100570 thread1.join();
Francis Murtaghbd707162019-09-09 11:26:44 +0100571 BOOST_CHECK((holder.GetCaptureData()).GetCapturePeriod() == 2);
572 BOOST_CHECK((holder.GetCaptureData()).GetCounterIds() == periodIdMap[2]);
Jim Flynn8355ec92019-09-17 12:29:50 +0100573 // NOTE: now that we have some initial values in the holder we don't have to worry
574 // in the multi-threaded section below about a read thread accessing the holder
575 // before any write thread has gotten to it so we read period = 0, counterIds empty
576 // instead of period = 0, counterIds = {0} as will the case when write thread 0
577 // has executed.
Francis Murtagh68f78d82019-09-04 16:42:29 +0100578
579 CaptureData captureData;
580 std::thread thread2(CaptureDataReadThreadImpl, std::ref(holder), std::ref(captureData));
581 thread2.join();
Jim Flynn8355ec92019-09-17 12:29:50 +0100582 BOOST_CHECK(captureData.GetCapturePeriod() == 2);
Francis Murtaghbd707162019-09-09 11:26:44 +0100583 BOOST_CHECK(captureData.GetCounterIds() == periodIdMap[2]);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100584
Jim Flynn8355ec92019-09-17 12:29:50 +0100585 std::map<uint32_t, CaptureData> captureDataIdMap;
586 for (uint32_t i = 0; i < numThreads; ++i)
587 {
588 CaptureData perThreadCaptureData;
589 captureDataIdMap.insert(std::make_pair(i, perThreadCaptureData));
590 }
591
Francis Murtaghbd707162019-09-09 11:26:44 +0100592 std::vector<std::thread> threadsVect;
Jim Flynn8355ec92019-09-17 12:29:50 +0100593 std::vector<std::thread> readThreadsVect;
594 for (uint32_t i = 0; i < numThreads; ++i)
Francis Murtaghbd707162019-09-09 11:26:44 +0100595 {
Keith Davis3201eea2019-10-24 17:30:41 +0100596 threadsVect.emplace_back(
597 std::thread(CaptureDataWriteThreadImpl, std::ref(holder), i, std::ref(periodIdMap[i])));
Francis Murtagh06965692019-09-05 16:29:01 +0100598
Jim Flynn8355ec92019-09-17 12:29:50 +0100599 // Verify that the CaptureData goes into the thread in a virgin state
600 BOOST_CHECK(captureDataIdMap.at(i).GetCapturePeriod() == 0);
601 BOOST_CHECK(captureDataIdMap.at(i).GetCounterIds().empty());
Keith Davis3201eea2019-10-24 17:30:41 +0100602 readThreadsVect.emplace_back(
603 std::thread(CaptureDataReadThreadImpl, std::ref(holder), std::ref(captureDataIdMap.at(i))));
Francis Murtaghbd707162019-09-09 11:26:44 +0100604 }
605
Jim Flynn8355ec92019-09-17 12:29:50 +0100606 for (uint32_t i = 0; i < numThreads; ++i)
Francis Murtaghbd707162019-09-09 11:26:44 +0100607 {
608 threadsVect[i].join();
Francis Murtaghbd707162019-09-09 11:26:44 +0100609 readThreadsVect[i].join();
610 }
Francis Murtagh68f78d82019-09-04 16:42:29 +0100611
Jim Flynn8355ec92019-09-17 12:29:50 +0100612 // Look at the CaptureData that each read thread has filled
613 // the capture period it read should match the counter ids entry
614 for (uint32_t i = 0; i < numThreads; ++i)
615 {
616 CaptureData perThreadCaptureData = captureDataIdMap.at(i);
617 BOOST_CHECK(perThreadCaptureData.GetCounterIds() == periodIdMap.at(perThreadCaptureData.GetCapturePeriod()));
618 }
Matthew Bentham46d1c622019-09-13 12:45:04 +0100619}
Francis Murtagh68f78d82019-09-04 16:42:29 +0100620
Matthew Bentham46d1c622019-09-13 12:45:04 +0100621BOOST_AUTO_TEST_CASE(CaptureDataMethods)
622{
Jim Flynn8355ec92019-09-17 12:29:50 +0100623 // Check CaptureData setter and getter functions
Keith Davis3201eea2019-10-24 17:30:41 +0100624 std::vector<uint16_t> counterIds = { 42, 29, 13 };
Jim Flynn8355ec92019-09-17 12:29:50 +0100625 CaptureData captureData;
626 BOOST_CHECK(captureData.GetCapturePeriod() == 0);
627 BOOST_CHECK((captureData.GetCounterIds()).empty());
628 captureData.SetCapturePeriod(150);
629 captureData.SetCounterIds(counterIds);
630 BOOST_CHECK(captureData.GetCapturePeriod() == 150);
631 BOOST_CHECK(captureData.GetCounterIds() == counterIds);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100632
Jim Flynn8355ec92019-09-17 12:29:50 +0100633 // Check assignment operator
Francis Murtagh68f78d82019-09-04 16:42:29 +0100634 CaptureData secondCaptureData;
Francis Murtagh68f78d82019-09-04 16:42:29 +0100635
Jim Flynn8355ec92019-09-17 12:29:50 +0100636 secondCaptureData = captureData;
637 BOOST_CHECK(secondCaptureData.GetCapturePeriod() == 150);
Matthew Bentham46d1c622019-09-13 12:45:04 +0100638 BOOST_CHECK(secondCaptureData.GetCounterIds() == counterIds);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100639
640 // Check copy constructor
Jim Flynn8355ec92019-09-17 12:29:50 +0100641 CaptureData copyConstructedCaptureData(captureData);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100642
Jim Flynn8355ec92019-09-17 12:29:50 +0100643 BOOST_CHECK(copyConstructedCaptureData.GetCapturePeriod() == 150);
Matthew Bentham46d1c622019-09-13 12:45:04 +0100644 BOOST_CHECK(copyConstructedCaptureData.GetCounterIds() == counterIds);
Keith Davis02356de2019-08-26 18:28:17 +0100645}
Francis Murtagh68f78d82019-09-04 16:42:29 +0100646
Keith Davis02356de2019-08-26 18:28:17 +0100647BOOST_AUTO_TEST_CASE(CheckProfilingServiceDisabled)
648{
649 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Sadik Armagan3184c902020-03-18 10:57:30 +0000650 armnn::profiling::ProfilingService profilingService;
Matteo Martincigha84edee2019-10-02 12:50:57 +0100651 profilingService.ResetExternalProfilingOptions(options, true);
652 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100653 profilingService.Update();
Matteo Martincigha84edee2019-10-02 12:50:57 +0100654 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis02356de2019-08-26 18:28:17 +0100655}
656
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100657BOOST_AUTO_TEST_CASE(CheckProfilingServiceCounterDirectory)
658{
659 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Sadik Armagan3184c902020-03-18 10:57:30 +0000660 armnn::profiling::ProfilingService profilingService;
Matteo Martincigha84edee2019-10-02 12:50:57 +0100661 profilingService.ResetExternalProfilingOptions(options, true);
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100662
Matteo Martincigha84edee2019-10-02 12:50:57 +0100663 const ICounterDirectory& counterDirectory0 = profilingService.GetCounterDirectory();
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100664 BOOST_CHECK(counterDirectory0.GetCounterCount() == 0);
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100665 profilingService.Update();
666 BOOST_CHECK(counterDirectory0.GetCounterCount() == 0);
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100667
668 options.m_EnableProfiling = true;
Matteo Martincigha84edee2019-10-02 12:50:57 +0100669 profilingService.ResetExternalProfilingOptions(options);
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100670
Matteo Martincigha84edee2019-10-02 12:50:57 +0100671 const ICounterDirectory& counterDirectory1 = profilingService.GetCounterDirectory();
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100672 BOOST_CHECK(counterDirectory1.GetCounterCount() == 0);
673 profilingService.Update();
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100674 BOOST_CHECK(counterDirectory1.GetCounterCount() != 0);
Colm Donelan2ba48d22019-11-29 09:10:59 +0000675 // Reset the profiling service to stop any running thread
676 options.m_EnableProfiling = false;
677 profilingService.ResetExternalProfilingOptions(options, true);
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100678}
679
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100680BOOST_AUTO_TEST_CASE(CheckProfilingServiceCounterValues)
681{
682 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +0100683 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +0000684 armnn::profiling::ProfilingService profilingService;
Matteo Martincigha84edee2019-10-02 12:50:57 +0100685 profilingService.ResetExternalProfilingOptions(options, true);
686
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100687 profilingService.Update();
Matteo Martincigha84edee2019-10-02 12:50:57 +0100688 const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
Keith Davis3201eea2019-10-24 17:30:41 +0100689 const Counters& counters = counterDirectory.GetCounters();
Matteo Martincigha84edee2019-10-02 12:50:57 +0100690 BOOST_CHECK(!counters.empty());
691
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100692 std::vector<std::thread> writers;
693
Finn Williamsf3fcf322020-05-11 14:38:02 +0100694 BOOST_CHECK(!counters.empty());
695
696 // Test GetAbsoluteCounterValue
697 for (int i = 0; i < 4; ++i)
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100698 {
Finn Williamsf3fcf322020-05-11 14:38:02 +0100699 // Increment and decrement the INFERENCES_RUN counter 250 times
700 writers.push_back(std::thread([&profilingService]()
701 {
702 for (int i = 0; i < 250; ++i)
703 {
704 profilingService.IncrementCounterValue(INFERENCES_RUN);
705 }
706 }));
707 // Add 10 to the INFERENCES_RUN counter 200 times
708 writers.push_back(std::thread([&profilingService]()
709 {
710 for (int i = 0; i < 200; ++i)
711 {
712 profilingService.AddCounterValue(INFERENCES_RUN, 10);
713 }
714 }));
715 // Subtract 5 from the INFERENCES_RUN counter 200 times
716 writers.push_back(std::thread([&profilingService]()
717 {
718 for (int i = 0; i < 200; ++i)
719 {
720 profilingService.SubtractCounterValue(INFERENCES_RUN, 5);
721 }
722 }));
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100723 }
Finn Williamsf3fcf322020-05-11 14:38:02 +0100724
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100725 std::for_each(writers.begin(), writers.end(), mem_fn(&std::thread::join));
726
Finn Williamsf3fcf322020-05-11 14:38:02 +0100727 uint32_t absoluteCounterValue = 0;
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100728
Finn Williamsf3fcf322020-05-11 14:38:02 +0100729 BOOST_CHECK_NO_THROW(absoluteCounterValue = profilingService.GetAbsoluteCounterValue(INFERENCES_RUN));
730 BOOST_CHECK(absoluteCounterValue = 5000);
731
732 // Test SetCounterValue
733 BOOST_CHECK_NO_THROW(profilingService.SetCounterValue(INFERENCES_RUN, 0));
734 BOOST_CHECK_NO_THROW(absoluteCounterValue = profilingService.GetAbsoluteCounterValue(INFERENCES_RUN));
735 BOOST_CHECK(absoluteCounterValue == 0);
736
737 // Test GetDeltaCounterValue
738 writers.clear();
739 uint32_t deltaCounterValue = 0;
740 //Start a reading thread to randomly read the INFERENCES_RUN counter value
741 std::thread reader([&profilingService](uint32_t& deltaCounterValue)
742 {
743 for (int i = 0; i < 300; ++i)
744 {
745 deltaCounterValue += profilingService.GetDeltaCounterValue(INFERENCES_RUN);
746 }
747 }, std::ref(deltaCounterValue));
748
749 for (int i = 0; i < 4; ++i)
750 {
751 // Increment and decrement the INFERENCES_RUN counter 250 times
752 writers.push_back(std::thread([&profilingService]()
753 {
754 for (int i = 0; i < 250; ++i)
755 {
756 profilingService.IncrementCounterValue(INFERENCES_RUN);
757 }
758 }));
759 // Add 10 to the INFERENCES_RUN counter 200 times
760 writers.push_back(std::thread([&profilingService]()
761 {
762 for (int i = 0; i < 200; ++i)
763 {
764 profilingService.AddCounterValue(INFERENCES_RUN, 10);
765 }
766 }));
767 // Subtract 5 from the INFERENCES_RUN counter 200 times
768 writers.push_back(std::thread([&profilingService]()
769 {
770 for (int i = 0; i < 200; ++i)
771 {
772 profilingService.SubtractCounterValue(INFERENCES_RUN, 5);
773 }
774 }));
775 }
776
777 std::for_each(writers.begin(), writers.end(), mem_fn(&std::thread::join));
778 reader.join();
779
780 // Do one last read in case the reader stopped early
781 deltaCounterValue += profilingService.GetDeltaCounterValue(INFERENCES_RUN);
782 BOOST_CHECK(deltaCounterValue == 5000);
783
Colm Donelan2ba48d22019-11-29 09:10:59 +0000784 // Reset the profiling service to stop any running thread
785 options.m_EnableProfiling = false;
786 profilingService.ResetExternalProfilingOptions(options, true);
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100787}
788
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100789BOOST_AUTO_TEST_CASE(CheckProfilingObjectUids)
Matteo Martincighab173e92019-09-05 12:02:04 +0100790{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100791 uint16_t uid = 0;
792 BOOST_CHECK_NO_THROW(uid = GetNextUid());
793 BOOST_CHECK(uid >= 1);
794
795 uint16_t nextUid = 0;
796 BOOST_CHECK_NO_THROW(nextUid = GetNextUid());
797 BOOST_CHECK(nextUid > uid);
798
799 std::vector<uint16_t> counterUids;
Keith Davise394bd92019-12-02 15:12:19 +0000800 BOOST_CHECK_NO_THROW(counterUids = GetNextCounterUids(uid,0));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100801 BOOST_CHECK(counterUids.size() == 1);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100802
803 std::vector<uint16_t> nextCounterUids;
Keith Davise394bd92019-12-02 15:12:19 +0000804 BOOST_CHECK_NO_THROW(nextCounterUids = GetNextCounterUids(nextUid, 2));
805 BOOST_CHECK(nextCounterUids.size() == 2);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100806 BOOST_CHECK(nextCounterUids[0] > counterUids[0]);
807
808 std::vector<uint16_t> counterUidsMultiCore;
Keith Davise394bd92019-12-02 15:12:19 +0000809 uint16_t thirdUid = 4;
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100810 uint16_t numberOfCores = 13;
Keith Davise394bd92019-12-02 15:12:19 +0000811 BOOST_CHECK_NO_THROW(counterUidsMultiCore = GetNextCounterUids(thirdUid, numberOfCores));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100812 BOOST_CHECK(counterUidsMultiCore.size() == numberOfCores);
813 BOOST_CHECK(counterUidsMultiCore.front() >= nextCounterUids[0]);
Keith Davis3201eea2019-10-24 17:30:41 +0100814 for (size_t i = 1; i < numberOfCores; i++)
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100815 {
816 BOOST_CHECK(counterUidsMultiCore[i] == counterUidsMultiCore[i - 1] + 1);
817 }
818 BOOST_CHECK(counterUidsMultiCore.back() == counterUidsMultiCore.front() + numberOfCores - 1);
Matteo Martincighab173e92019-09-05 12:02:04 +0100819}
820
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100821BOOST_AUTO_TEST_CASE(CheckCounterDirectoryRegisterCategory)
Matteo Martincighab173e92019-09-05 12:02:04 +0100822{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100823 CounterDirectory counterDirectory;
Keith Davis3201eea2019-10-24 17:30:41 +0100824 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
825 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100826 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
Keith Davis3201eea2019-10-24 17:30:41 +0100827 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
Matteo Martincighab173e92019-09-05 12:02:04 +0100828
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100829 // Register a category with an invalid name
830 const Category* noCategory = nullptr;
831 BOOST_CHECK_THROW(noCategory = counterDirectory.RegisterCategory(""), armnn::InvalidArgumentException);
832 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
833 BOOST_CHECK(!noCategory);
Matteo Martincighab173e92019-09-05 12:02:04 +0100834
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100835 // Register a category with an invalid name
836 BOOST_CHECK_THROW(noCategory = counterDirectory.RegisterCategory("invalid category"),
837 armnn::InvalidArgumentException);
838 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
839 BOOST_CHECK(!noCategory);
840
841 // Register a new category
842 const std::string categoryName = "some_category";
Keith Davis3201eea2019-10-24 17:30:41 +0100843 const Category* category = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100844 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
845 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
846 BOOST_CHECK(category);
847 BOOST_CHECK(category->m_Name == categoryName);
848 BOOST_CHECK(category->m_Counters.empty());
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100849
850 // Get the registered category
851 const Category* registeredCategory = counterDirectory.GetCategory(categoryName);
852 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
853 BOOST_CHECK(registeredCategory);
854 BOOST_CHECK(registeredCategory == category);
855
856 // Try to get a category not registered
857 const Category* notRegisteredCategory = counterDirectory.GetCategory("not_registered_category");
858 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
859 BOOST_CHECK(!notRegisteredCategory);
860
861 // Register a category already registered
862 const Category* anotherCategory = nullptr;
863 BOOST_CHECK_THROW(anotherCategory = counterDirectory.RegisterCategory(categoryName),
864 armnn::InvalidArgumentException);
865 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
866 BOOST_CHECK(!anotherCategory);
867
868 // Register a device for testing
869 const std::string deviceName = "some_device";
Keith Davis3201eea2019-10-24 17:30:41 +0100870 const Device* device = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100871 BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName));
872 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
873 BOOST_CHECK(device);
874 BOOST_CHECK(device->m_Uid >= 1);
875 BOOST_CHECK(device->m_Name == deviceName);
876 BOOST_CHECK(device->m_Cores == 0);
877
878 // Register a new category not associated to any device
879 const std::string categoryWoDeviceName = "some_category_without_device";
Keith Davis3201eea2019-10-24 17:30:41 +0100880 const Category* categoryWoDevice = nullptr;
Sadik Armagan4c998992020-02-25 12:44:44 +0000881 BOOST_CHECK_NO_THROW(categoryWoDevice = counterDirectory.RegisterCategory(categoryWoDeviceName));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100882 BOOST_CHECK(counterDirectory.GetCategoryCount() == 2);
883 BOOST_CHECK(categoryWoDevice);
884 BOOST_CHECK(categoryWoDevice->m_Name == categoryWoDeviceName);
885 BOOST_CHECK(categoryWoDevice->m_Counters.empty());
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100886
Sadik Armagan4c998992020-02-25 12:44:44 +0000887 // Register a new category associated to an invalid device name (already exist)
888 const Category* categoryInvalidDeviceName = nullptr;
889 BOOST_CHECK_THROW(categoryInvalidDeviceName =
890 counterDirectory.RegisterCategory(categoryWoDeviceName),
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100891 armnn::InvalidArgumentException);
892 BOOST_CHECK(counterDirectory.GetCategoryCount() == 2);
Sadik Armagan4c998992020-02-25 12:44:44 +0000893 BOOST_CHECK(!categoryInvalidDeviceName);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100894
895 // Register a new category associated to a valid device
896 const std::string categoryWValidDeviceName = "some_category_with_valid_device";
Keith Davis3201eea2019-10-24 17:30:41 +0100897 const Category* categoryWValidDevice = nullptr;
898 BOOST_CHECK_NO_THROW(categoryWValidDevice =
Sadik Armagan4c998992020-02-25 12:44:44 +0000899 counterDirectory.RegisterCategory(categoryWValidDeviceName));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100900 BOOST_CHECK(counterDirectory.GetCategoryCount() == 3);
901 BOOST_CHECK(categoryWValidDevice);
902 BOOST_CHECK(categoryWValidDevice != category);
903 BOOST_CHECK(categoryWValidDevice->m_Name == categoryWValidDeviceName);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100904
905 // Register a counter set for testing
906 const std::string counterSetName = "some_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +0100907 const CounterSet* counterSet = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100908 BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
909 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
910 BOOST_CHECK(counterSet);
911 BOOST_CHECK(counterSet->m_Uid >= 1);
912 BOOST_CHECK(counterSet->m_Name == counterSetName);
913 BOOST_CHECK(counterSet->m_Count == 0);
914
915 // Register a new category not associated to any counter set
916 const std::string categoryWoCounterSetName = "some_category_without_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +0100917 const Category* categoryWoCounterSet = nullptr;
918 BOOST_CHECK_NO_THROW(categoryWoCounterSet =
Sadik Armagan4c998992020-02-25 12:44:44 +0000919 counterDirectory.RegisterCategory(categoryWoCounterSetName));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100920 BOOST_CHECK(counterDirectory.GetCategoryCount() == 4);
921 BOOST_CHECK(categoryWoCounterSet);
922 BOOST_CHECK(categoryWoCounterSet->m_Name == categoryWoCounterSetName);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100923
924 // Register a new category associated to a valid counter set
925 const std::string categoryWValidCounterSetName = "some_category_with_valid_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +0100926 const Category* categoryWValidCounterSet = nullptr;
Sadik Armagan4c998992020-02-25 12:44:44 +0000927 BOOST_CHECK_NO_THROW(categoryWValidCounterSet = counterDirectory.RegisterCategory(categoryWValidCounterSetName));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100928 BOOST_CHECK(counterDirectory.GetCategoryCount() == 5);
929 BOOST_CHECK(categoryWValidCounterSet);
930 BOOST_CHECK(categoryWValidCounterSet != category);
931 BOOST_CHECK(categoryWValidCounterSet->m_Name == categoryWValidCounterSetName);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100932
933 // Register a new category associated to a valid device and counter set
934 const std::string categoryWValidDeviceAndValidCounterSetName = "some_category_with_valid_device_and_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +0100935 const Category* categoryWValidDeviceAndValidCounterSet = nullptr;
936 BOOST_CHECK_NO_THROW(categoryWValidDeviceAndValidCounterSet = counterDirectory.RegisterCategory(
Sadik Armagan4c998992020-02-25 12:44:44 +0000937 categoryWValidDeviceAndValidCounterSetName));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100938 BOOST_CHECK(counterDirectory.GetCategoryCount() == 6);
939 BOOST_CHECK(categoryWValidDeviceAndValidCounterSet);
940 BOOST_CHECK(categoryWValidDeviceAndValidCounterSet != category);
941 BOOST_CHECK(categoryWValidDeviceAndValidCounterSet->m_Name == categoryWValidDeviceAndValidCounterSetName);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100942}
943
944BOOST_AUTO_TEST_CASE(CheckCounterDirectoryRegisterDevice)
945{
946 CounterDirectory counterDirectory;
Keith Davis3201eea2019-10-24 17:30:41 +0100947 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
948 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100949 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
Keith Davis3201eea2019-10-24 17:30:41 +0100950 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100951
952 // Register a device with an invalid name
953 const Device* noDevice = nullptr;
954 BOOST_CHECK_THROW(noDevice = counterDirectory.RegisterDevice(""), armnn::InvalidArgumentException);
955 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
956 BOOST_CHECK(!noDevice);
957
958 // Register a device with an invalid name
959 BOOST_CHECK_THROW(noDevice = counterDirectory.RegisterDevice("inv@lid nam€"), armnn::InvalidArgumentException);
960 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
961 BOOST_CHECK(!noDevice);
962
963 // Register a new device with no cores or parent category
964 const std::string deviceName = "some_device";
Keith Davis3201eea2019-10-24 17:30:41 +0100965 const Device* device = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100966 BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName));
967 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
968 BOOST_CHECK(device);
969 BOOST_CHECK(device->m_Name == deviceName);
970 BOOST_CHECK(device->m_Uid >= 1);
971 BOOST_CHECK(device->m_Cores == 0);
972
Matteo Martincigh657ab2d2019-09-18 10:53:24 +0100973 // Try getting an unregistered device
974 const Device* unregisteredDevice = counterDirectory.GetDevice(9999);
975 BOOST_CHECK(!unregisteredDevice);
976
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100977 // Get the registered device
978 const Device* registeredDevice = counterDirectory.GetDevice(device->m_Uid);
979 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
980 BOOST_CHECK(registeredDevice);
981 BOOST_CHECK(registeredDevice == device);
982
Matteo Martincigh657ab2d2019-09-18 10:53:24 +0100983 // Register a device with the name of a device already registered
984 const Device* deviceSameName = nullptr;
985 BOOST_CHECK_THROW(deviceSameName = counterDirectory.RegisterDevice(deviceName), armnn::InvalidArgumentException);
986 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
987 BOOST_CHECK(!deviceSameName);
988
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100989 // Register a new device with cores and no parent category
990 const std::string deviceWCoresName = "some_device_with_cores";
Keith Davis3201eea2019-10-24 17:30:41 +0100991 const Device* deviceWCores = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100992 BOOST_CHECK_NO_THROW(deviceWCores = counterDirectory.RegisterDevice(deviceWCoresName, 2));
993 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
994 BOOST_CHECK(deviceWCores);
995 BOOST_CHECK(deviceWCores->m_Name == deviceWCoresName);
996 BOOST_CHECK(deviceWCores->m_Uid >= 1);
997 BOOST_CHECK(deviceWCores->m_Uid > device->m_Uid);
998 BOOST_CHECK(deviceWCores->m_Cores == 2);
999
1000 // Get the registered device
1001 const Device* registeredDeviceWCores = counterDirectory.GetDevice(deviceWCores->m_Uid);
1002 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
1003 BOOST_CHECK(registeredDeviceWCores);
1004 BOOST_CHECK(registeredDeviceWCores == deviceWCores);
1005 BOOST_CHECK(registeredDeviceWCores != device);
1006
1007 // Register a new device with cores and invalid parent category
1008 const std::string deviceWCoresWInvalidParentCategoryName = "some_device_with_cores_with_invalid_parent_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001009 const Device* deviceWCoresWInvalidParentCategory = nullptr;
1010 BOOST_CHECK_THROW(deviceWCoresWInvalidParentCategory =
1011 counterDirectory.RegisterDevice(deviceWCoresWInvalidParentCategoryName, 3, std::string("")),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001012 armnn::InvalidArgumentException);
1013 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
1014 BOOST_CHECK(!deviceWCoresWInvalidParentCategory);
1015
1016 // Register a new device with cores and invalid parent category
1017 const std::string deviceWCoresWInvalidParentCategoryName2 = "some_device_with_cores_with_invalid_parent_category2";
Keith Davis3201eea2019-10-24 17:30:41 +01001018 const Device* deviceWCoresWInvalidParentCategory2 = nullptr;
1019 BOOST_CHECK_THROW(deviceWCoresWInvalidParentCategory2 = counterDirectory.RegisterDevice(
1020 deviceWCoresWInvalidParentCategoryName2, 3, std::string("invalid_parent_category")),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001021 armnn::InvalidArgumentException);
1022 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
1023 BOOST_CHECK(!deviceWCoresWInvalidParentCategory2);
1024
1025 // Register a category for testing
1026 const std::string categoryName = "some_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001027 const Category* category = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001028 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
1029 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
1030 BOOST_CHECK(category);
1031 BOOST_CHECK(category->m_Name == categoryName);
1032 BOOST_CHECK(category->m_Counters.empty());
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001033
1034 // Register a new device with cores and valid parent category
1035 const std::string deviceWCoresWValidParentCategoryName = "some_device_with_cores_with_valid_parent_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001036 const Device* deviceWCoresWValidParentCategory = nullptr;
1037 BOOST_CHECK_NO_THROW(deviceWCoresWValidParentCategory =
1038 counterDirectory.RegisterDevice(deviceWCoresWValidParentCategoryName, 4, categoryName));
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001039 BOOST_CHECK(counterDirectory.GetDeviceCount() == 3);
1040 BOOST_CHECK(deviceWCoresWValidParentCategory);
1041 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Name == deviceWCoresWValidParentCategoryName);
1042 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Uid >= 1);
1043 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Uid > device->m_Uid);
1044 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Uid > deviceWCores->m_Uid);
1045 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Cores == 4);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001046}
1047
1048BOOST_AUTO_TEST_CASE(CheckCounterDirectoryRegisterCounterSet)
1049{
1050 CounterDirectory counterDirectory;
Keith Davis3201eea2019-10-24 17:30:41 +01001051 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
1052 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001053 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
Keith Davis3201eea2019-10-24 17:30:41 +01001054 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001055
1056 // Register a counter set with an invalid name
1057 const CounterSet* noCounterSet = nullptr;
1058 BOOST_CHECK_THROW(noCounterSet = counterDirectory.RegisterCounterSet(""), armnn::InvalidArgumentException);
1059 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
1060 BOOST_CHECK(!noCounterSet);
1061
1062 // Register a counter set with an invalid name
1063 BOOST_CHECK_THROW(noCounterSet = counterDirectory.RegisterCounterSet("invalid name"),
1064 armnn::InvalidArgumentException);
1065 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
1066 BOOST_CHECK(!noCounterSet);
1067
1068 // Register a new counter set with no count or parent category
1069 const std::string counterSetName = "some_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +01001070 const CounterSet* counterSet = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001071 BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
1072 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1073 BOOST_CHECK(counterSet);
1074 BOOST_CHECK(counterSet->m_Name == counterSetName);
1075 BOOST_CHECK(counterSet->m_Uid >= 1);
1076 BOOST_CHECK(counterSet->m_Count == 0);
1077
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001078 // Try getting an unregistered counter set
1079 const CounterSet* unregisteredCounterSet = counterDirectory.GetCounterSet(9999);
1080 BOOST_CHECK(!unregisteredCounterSet);
1081
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001082 // Get the registered counter set
1083 const CounterSet* registeredCounterSet = counterDirectory.GetCounterSet(counterSet->m_Uid);
1084 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1085 BOOST_CHECK(registeredCounterSet);
1086 BOOST_CHECK(registeredCounterSet == counterSet);
1087
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001088 // Register a counter set with the name of a counter set already registered
1089 const CounterSet* counterSetSameName = nullptr;
1090 BOOST_CHECK_THROW(counterSetSameName = counterDirectory.RegisterCounterSet(counterSetName),
1091 armnn::InvalidArgumentException);
1092 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1093 BOOST_CHECK(!counterSetSameName);
1094
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001095 // Register a new counter set with count and no parent category
1096 const std::string counterSetWCountName = "some_counter_set_with_count";
Keith Davis3201eea2019-10-24 17:30:41 +01001097 const CounterSet* counterSetWCount = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001098 BOOST_CHECK_NO_THROW(counterSetWCount = counterDirectory.RegisterCounterSet(counterSetWCountName, 37));
1099 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 2);
1100 BOOST_CHECK(counterSetWCount);
1101 BOOST_CHECK(counterSetWCount->m_Name == counterSetWCountName);
1102 BOOST_CHECK(counterSetWCount->m_Uid >= 1);
1103 BOOST_CHECK(counterSetWCount->m_Uid > counterSet->m_Uid);
1104 BOOST_CHECK(counterSetWCount->m_Count == 37);
1105
1106 // Get the registered counter set
1107 const CounterSet* registeredCounterSetWCount = counterDirectory.GetCounterSet(counterSetWCount->m_Uid);
1108 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 2);
1109 BOOST_CHECK(registeredCounterSetWCount);
1110 BOOST_CHECK(registeredCounterSetWCount == counterSetWCount);
1111 BOOST_CHECK(registeredCounterSetWCount != counterSet);
1112
1113 // Register a new counter set with count and invalid parent category
1114 const std::string counterSetWCountWInvalidParentCategoryName = "some_counter_set_with_count_"
1115 "with_invalid_parent_category";
1116 const CounterSet* counterSetWCountWInvalidParentCategory = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001117 BOOST_CHECK_THROW(counterSetWCountWInvalidParentCategory = counterDirectory.RegisterCounterSet(
1118 counterSetWCountWInvalidParentCategoryName, 42, std::string("")),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001119 armnn::InvalidArgumentException);
1120 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 2);
1121 BOOST_CHECK(!counterSetWCountWInvalidParentCategory);
1122
1123 // Register a new counter set with count and invalid parent category
1124 const std::string counterSetWCountWInvalidParentCategoryName2 = "some_counter_set_with_count_"
1125 "with_invalid_parent_category2";
1126 const CounterSet* counterSetWCountWInvalidParentCategory2 = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001127 BOOST_CHECK_THROW(counterSetWCountWInvalidParentCategory2 = counterDirectory.RegisterCounterSet(
1128 counterSetWCountWInvalidParentCategoryName2, 42, std::string("invalid_parent_category")),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001129 armnn::InvalidArgumentException);
1130 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 2);
1131 BOOST_CHECK(!counterSetWCountWInvalidParentCategory2);
1132
1133 // Register a category for testing
1134 const std::string categoryName = "some_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001135 const Category* category = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001136 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
1137 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
1138 BOOST_CHECK(category);
1139 BOOST_CHECK(category->m_Name == categoryName);
1140 BOOST_CHECK(category->m_Counters.empty());
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001141
1142 // Register a new counter set with count and valid parent category
1143 const std::string counterSetWCountWValidParentCategoryName = "some_counter_set_with_count_"
1144 "with_valid_parent_category";
1145 const CounterSet* counterSetWCountWValidParentCategory = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001146 BOOST_CHECK_NO_THROW(counterSetWCountWValidParentCategory = counterDirectory.RegisterCounterSet(
1147 counterSetWCountWValidParentCategoryName, 42, categoryName));
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001148 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 3);
1149 BOOST_CHECK(counterSetWCountWValidParentCategory);
1150 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Name == counterSetWCountWValidParentCategoryName);
1151 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Uid >= 1);
1152 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Uid > counterSet->m_Uid);
1153 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Uid > counterSetWCount->m_Uid);
1154 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Count == 42);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001155
Sadik Armagan4c998992020-02-25 12:44:44 +00001156 // Register a counter set associated to a category with invalid name
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001157 const std::string counterSetSameCategoryName = "some_counter_set_with_invalid_parent_category";
Sadik Armagan4c998992020-02-25 12:44:44 +00001158 const std::string invalidCategoryName = "";
Keith Davis3201eea2019-10-24 17:30:41 +01001159 const CounterSet* counterSetSameCategory = nullptr;
1160 BOOST_CHECK_THROW(counterSetSameCategory =
Sadik Armagan4c998992020-02-25 12:44:44 +00001161 counterDirectory.RegisterCounterSet(counterSetSameCategoryName, 0, invalidCategoryName),
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001162 armnn::InvalidArgumentException);
1163 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 3);
1164 BOOST_CHECK(!counterSetSameCategory);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001165}
1166
1167BOOST_AUTO_TEST_CASE(CheckCounterDirectoryRegisterCounter)
1168{
1169 CounterDirectory counterDirectory;
Keith Davis3201eea2019-10-24 17:30:41 +01001170 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
1171 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001172 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
Keith Davis3201eea2019-10-24 17:30:41 +01001173 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001174
1175 // Register a counter with an invalid parent category name
1176 const Counter* noCounter = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001177 BOOST_CHECK_THROW(noCounter =
Keith Davise394bd92019-12-02 15:12:19 +00001178 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1179 0,
1180 "",
1181 0,
1182 1,
1183 123.45f,
1184 "valid ",
1185 "name"),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001186 armnn::InvalidArgumentException);
1187 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1188 BOOST_CHECK(!noCounter);
1189
1190 // Register a counter with an invalid parent category name
Keith Davise394bd92019-12-02 15:12:19 +00001191 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1192 1,
1193 "invalid parent category",
1194 0,
1195 1,
1196 123.45f,
1197 "valid name",
1198 "valid description"),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001199 armnn::InvalidArgumentException);
1200 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1201 BOOST_CHECK(!noCounter);
1202
1203 // Register a counter with an invalid class
Keith Davise394bd92019-12-02 15:12:19 +00001204 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1205 2,
1206 "valid_parent_category",
1207 2,
1208 1,
1209 123.45f,
1210 "valid "
1211 "name",
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001212 "valid description"),
1213 armnn::InvalidArgumentException);
1214 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1215 BOOST_CHECK(!noCounter);
1216
1217 // Register a counter with an invalid interpolation
Keith Davise394bd92019-12-02 15:12:19 +00001218 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1219 4,
1220 "valid_parent_category",
1221 0,
1222 3,
1223 123.45f,
1224 "valid "
1225 "name",
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001226 "valid description"),
1227 armnn::InvalidArgumentException);
1228 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1229 BOOST_CHECK(!noCounter);
1230
1231 // Register a counter with an invalid multiplier
Keith Davise394bd92019-12-02 15:12:19 +00001232 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1233 5,
1234 "valid_parent_category",
1235 0,
1236 1,
1237 .0f,
1238 "valid "
1239 "name",
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001240 "valid description"),
1241 armnn::InvalidArgumentException);
1242 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1243 BOOST_CHECK(!noCounter);
1244
1245 // Register a counter with an invalid name
Keith Davis3201eea2019-10-24 17:30:41 +01001246 BOOST_CHECK_THROW(
Keith Davise394bd92019-12-02 15:12:19 +00001247 noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1248 6,
1249 "valid_parent_category",
1250 0,
1251 1,
1252 123.45f,
1253 "",
1254 "valid description"),
Keith Davis3201eea2019-10-24 17:30:41 +01001255 armnn::InvalidArgumentException);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001256 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1257 BOOST_CHECK(!noCounter);
1258
1259 // Register a counter with an invalid name
Keith Davise394bd92019-12-02 15:12:19 +00001260 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1261 7,
1262 "valid_parent_category",
1263 0,
1264 1,
1265 123.45f,
1266 "invalid nam€",
1267 "valid description"),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001268 armnn::InvalidArgumentException);
1269 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1270 BOOST_CHECK(!noCounter);
1271
1272 // Register a counter with an invalid description
Keith Davis3201eea2019-10-24 17:30:41 +01001273 BOOST_CHECK_THROW(noCounter =
Keith Davise394bd92019-12-02 15:12:19 +00001274 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1275 8,
1276 "valid_parent_category",
1277 0,
1278 1,
1279 123.45f,
1280 "valid name",
1281 ""),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001282 armnn::InvalidArgumentException);
1283 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1284 BOOST_CHECK(!noCounter);
1285
1286 // Register a counter with an invalid description
Keith Davise394bd92019-12-02 15:12:19 +00001287 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1288 9,
1289 "valid_parent_category",
1290 0,
1291 1,
1292 123.45f,
1293 "valid "
1294 "name",
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001295 "inv@lid description"),
1296 armnn::InvalidArgumentException);
1297 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1298 BOOST_CHECK(!noCounter);
1299
1300 // Register a counter with an invalid unit2
Keith Davise394bd92019-12-02 15:12:19 +00001301 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1302 10,
1303 "valid_parent_category",
1304 0,
1305 1,
1306 123.45f,
1307 "valid name",
1308 "valid description",
1309 std::string("Mb/s2")),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001310 armnn::InvalidArgumentException);
1311 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1312 BOOST_CHECK(!noCounter);
1313
1314 // Register a counter with a non-existing parent category name
Keith Davise394bd92019-12-02 15:12:19 +00001315 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1316 11,
1317 "invalid_parent_category",
1318 0,
1319 1,
1320 123.45f,
1321 "valid name",
1322 "valid description"),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001323 armnn::InvalidArgumentException);
1324 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1325 BOOST_CHECK(!noCounter);
1326
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001327 // Try getting an unregistered counter
1328 const Counter* unregisteredCounter = counterDirectory.GetCounter(9999);
1329 BOOST_CHECK(!unregisteredCounter);
1330
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001331 // Register a category for testing
1332 const std::string categoryName = "some_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001333 const Category* category = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001334 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
1335 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
1336 BOOST_CHECK(category);
1337 BOOST_CHECK(category->m_Name == categoryName);
1338 BOOST_CHECK(category->m_Counters.empty());
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001339
1340 // Register a counter with a valid parent category name
1341 const Counter* counter = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001342 BOOST_CHECK_NO_THROW(
Keith Davise394bd92019-12-02 15:12:19 +00001343 counter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1344 12,
1345 categoryName,
1346 0,
1347 1,
1348 123.45f,
1349 "valid name",
1350 "valid description"));
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001351 BOOST_CHECK(counterDirectory.GetCounterCount() == 1);
1352 BOOST_CHECK(counter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001353 BOOST_CHECK(counter->m_MaxCounterUid == counter->m_Uid);
1354 BOOST_CHECK(counter->m_Class == 0);
1355 BOOST_CHECK(counter->m_Interpolation == 1);
1356 BOOST_CHECK(counter->m_Multiplier == 123.45f);
1357 BOOST_CHECK(counter->m_Name == "valid name");
1358 BOOST_CHECK(counter->m_Description == "valid description");
1359 BOOST_CHECK(counter->m_Units == "");
1360 BOOST_CHECK(counter->m_DeviceUid == 0);
1361 BOOST_CHECK(counter->m_CounterSetUid == 0);
1362 BOOST_CHECK(category->m_Counters.size() == 1);
1363 BOOST_CHECK(category->m_Counters.back() == counter->m_Uid);
1364
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001365 // Register a counter with a name of a counter already registered for the given parent category name
1366 const Counter* counterSameName = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001367 BOOST_CHECK_THROW(counterSameName =
Keith Davise394bd92019-12-02 15:12:19 +00001368 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1369 13,
1370 categoryName,
1371 0,
1372 0,
1373 1.0f,
1374 "valid name",
1375 "valid description",
1376 std::string("description")),
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001377 armnn::InvalidArgumentException);
1378 BOOST_CHECK(counterDirectory.GetCounterCount() == 1);
1379 BOOST_CHECK(!counterSameName);
1380
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001381 // Register a counter with a valid parent category name and units
1382 const Counter* counterWUnits = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001383 BOOST_CHECK_NO_THROW(counterWUnits = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1384 14,
1385 categoryName,
1386 0,
1387 1,
1388 123.45f,
1389 "valid name 2",
1390 "valid description",
1391 std::string("Mnnsq2"))); // Units
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001392 BOOST_CHECK(counterDirectory.GetCounterCount() == 2);
1393 BOOST_CHECK(counterWUnits);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001394 BOOST_CHECK(counterWUnits->m_Uid > counter->m_Uid);
1395 BOOST_CHECK(counterWUnits->m_MaxCounterUid == counterWUnits->m_Uid);
1396 BOOST_CHECK(counterWUnits->m_Class == 0);
1397 BOOST_CHECK(counterWUnits->m_Interpolation == 1);
1398 BOOST_CHECK(counterWUnits->m_Multiplier == 123.45f);
1399 BOOST_CHECK(counterWUnits->m_Name == "valid name 2");
1400 BOOST_CHECK(counterWUnits->m_Description == "valid description");
1401 BOOST_CHECK(counterWUnits->m_Units == "Mnnsq2");
1402 BOOST_CHECK(counterWUnits->m_DeviceUid == 0);
1403 BOOST_CHECK(counterWUnits->m_CounterSetUid == 0);
1404 BOOST_CHECK(category->m_Counters.size() == 2);
1405 BOOST_CHECK(category->m_Counters.back() == counterWUnits->m_Uid);
1406
1407 // Register a counter with a valid parent category name and not associated with a device
1408 const Counter* counterWoDevice = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001409 BOOST_CHECK_NO_THROW(counterWoDevice = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1410 26,
1411 categoryName,
1412 0,
1413 1,
1414 123.45f,
1415 "valid name 3",
1416 "valid description",
1417 armnn::EmptyOptional(),// Units
1418 armnn::EmptyOptional(),// Number of cores
1419 0)); // Device UID
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001420 BOOST_CHECK(counterDirectory.GetCounterCount() == 3);
1421 BOOST_CHECK(counterWoDevice);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001422 BOOST_CHECK(counterWoDevice->m_Uid > counter->m_Uid);
1423 BOOST_CHECK(counterWoDevice->m_MaxCounterUid == counterWoDevice->m_Uid);
1424 BOOST_CHECK(counterWoDevice->m_Class == 0);
1425 BOOST_CHECK(counterWoDevice->m_Interpolation == 1);
1426 BOOST_CHECK(counterWoDevice->m_Multiplier == 123.45f);
1427 BOOST_CHECK(counterWoDevice->m_Name == "valid name 3");
1428 BOOST_CHECK(counterWoDevice->m_Description == "valid description");
1429 BOOST_CHECK(counterWoDevice->m_Units == "");
1430 BOOST_CHECK(counterWoDevice->m_DeviceUid == 0);
1431 BOOST_CHECK(counterWoDevice->m_CounterSetUid == 0);
1432 BOOST_CHECK(category->m_Counters.size() == 3);
1433 BOOST_CHECK(category->m_Counters.back() == counterWoDevice->m_Uid);
1434
1435 // Register a counter with a valid parent category name and associated to an invalid device
Keith Davise394bd92019-12-02 15:12:19 +00001436 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1437 15,
1438 categoryName,
1439 0,
1440 1,
1441 123.45f,
1442 "valid name 4",
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001443 "valid description",
Keith Davis3201eea2019-10-24 17:30:41 +01001444 armnn::EmptyOptional(), // Units
1445 armnn::EmptyOptional(), // Number of cores
1446 100), // Device UID
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001447 armnn::InvalidArgumentException);
1448 BOOST_CHECK(counterDirectory.GetCounterCount() == 3);
1449 BOOST_CHECK(!noCounter);
1450
1451 // Register a device for testing
1452 const std::string deviceName = "some_device";
Keith Davis3201eea2019-10-24 17:30:41 +01001453 const Device* device = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001454 BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName));
1455 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
1456 BOOST_CHECK(device);
1457 BOOST_CHECK(device->m_Name == deviceName);
1458 BOOST_CHECK(device->m_Uid >= 1);
1459 BOOST_CHECK(device->m_Cores == 0);
1460
1461 // Register a counter with a valid parent category name and associated to a device
1462 const Counter* counterWDevice = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001463 BOOST_CHECK_NO_THROW(counterWDevice = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1464 16,
1465 categoryName,
1466 0,
1467 1,
1468 123.45f,
1469 "valid name 5",
1470 std::string("valid description"),
Keith Davis3201eea2019-10-24 17:30:41 +01001471 armnn::EmptyOptional(), // Units
1472 armnn::EmptyOptional(), // Number of cores
1473 device->m_Uid)); // Device UID
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001474 BOOST_CHECK(counterDirectory.GetCounterCount() == 4);
1475 BOOST_CHECK(counterWDevice);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001476 BOOST_CHECK(counterWDevice->m_Uid > counter->m_Uid);
1477 BOOST_CHECK(counterWDevice->m_MaxCounterUid == counterWDevice->m_Uid);
1478 BOOST_CHECK(counterWDevice->m_Class == 0);
1479 BOOST_CHECK(counterWDevice->m_Interpolation == 1);
1480 BOOST_CHECK(counterWDevice->m_Multiplier == 123.45f);
1481 BOOST_CHECK(counterWDevice->m_Name == "valid name 5");
1482 BOOST_CHECK(counterWDevice->m_Description == "valid description");
1483 BOOST_CHECK(counterWDevice->m_Units == "");
1484 BOOST_CHECK(counterWDevice->m_DeviceUid == device->m_Uid);
1485 BOOST_CHECK(counterWDevice->m_CounterSetUid == 0);
1486 BOOST_CHECK(category->m_Counters.size() == 4);
1487 BOOST_CHECK(category->m_Counters.back() == counterWDevice->m_Uid);
1488
1489 // Register a counter with a valid parent category name and not associated with a counter set
1490 const Counter* counterWoCounterSet = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001491 BOOST_CHECK_NO_THROW(counterWoCounterSet = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1492 17,
1493 categoryName,
1494 0,
1495 1,
1496 123.45f,
1497 "valid name 6",
1498 "valid description",
1499 armnn::EmptyOptional(),// Units
1500 armnn::EmptyOptional(),// No of cores
1501 armnn::EmptyOptional(),// Device UID
1502 0)); // CounterSet UID
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001503 BOOST_CHECK(counterDirectory.GetCounterCount() == 5);
1504 BOOST_CHECK(counterWoCounterSet);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001505 BOOST_CHECK(counterWoCounterSet->m_Uid > counter->m_Uid);
1506 BOOST_CHECK(counterWoCounterSet->m_MaxCounterUid == counterWoCounterSet->m_Uid);
1507 BOOST_CHECK(counterWoCounterSet->m_Class == 0);
1508 BOOST_CHECK(counterWoCounterSet->m_Interpolation == 1);
1509 BOOST_CHECK(counterWoCounterSet->m_Multiplier == 123.45f);
1510 BOOST_CHECK(counterWoCounterSet->m_Name == "valid name 6");
1511 BOOST_CHECK(counterWoCounterSet->m_Description == "valid description");
1512 BOOST_CHECK(counterWoCounterSet->m_Units == "");
1513 BOOST_CHECK(counterWoCounterSet->m_DeviceUid == 0);
1514 BOOST_CHECK(counterWoCounterSet->m_CounterSetUid == 0);
1515 BOOST_CHECK(category->m_Counters.size() == 5);
1516 BOOST_CHECK(category->m_Counters.back() == counterWoCounterSet->m_Uid);
1517
1518 // Register a counter with a valid parent category name and associated to an invalid counter set
Keith Davise394bd92019-12-02 15:12:19 +00001519 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1520 18,
1521 categoryName,
1522 0,
1523 1,
1524 123.45f,
1525 "valid ",
1526 "name 7",
1527 std::string("valid description"),
Keith Davis3201eea2019-10-24 17:30:41 +01001528 armnn::EmptyOptional(), // Units
1529 armnn::EmptyOptional(), // Number of cores
Keith Davise394bd92019-12-02 15:12:19 +00001530 100), // Counter set UID
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001531 armnn::InvalidArgumentException);
1532 BOOST_CHECK(counterDirectory.GetCounterCount() == 5);
1533 BOOST_CHECK(!noCounter);
1534
1535 // Register a counter with a valid parent category name and with a given number of cores
1536 const Counter* counterWNumberOfCores = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001537 uint16_t numberOfCores = 15;
1538 BOOST_CHECK_NO_THROW(counterWNumberOfCores = counterDirectory.RegisterCounter(
Keith Davise394bd92019-12-02 15:12:19 +00001539 armnn::profiling::BACKEND_ID, 50,
Keith Davis3201eea2019-10-24 17:30:41 +01001540 categoryName, 0, 1, 123.45f, "valid name 8", "valid description",
1541 armnn::EmptyOptional(), // Units
1542 numberOfCores, // Number of cores
1543 armnn::EmptyOptional(), // Device UID
1544 armnn::EmptyOptional())); // Counter set UID
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001545 BOOST_CHECK(counterDirectory.GetCounterCount() == 20);
1546 BOOST_CHECK(counterWNumberOfCores);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001547 BOOST_CHECK(counterWNumberOfCores->m_Uid > counter->m_Uid);
1548 BOOST_CHECK(counterWNumberOfCores->m_MaxCounterUid == counterWNumberOfCores->m_Uid + numberOfCores - 1);
1549 BOOST_CHECK(counterWNumberOfCores->m_Class == 0);
1550 BOOST_CHECK(counterWNumberOfCores->m_Interpolation == 1);
1551 BOOST_CHECK(counterWNumberOfCores->m_Multiplier == 123.45f);
1552 BOOST_CHECK(counterWNumberOfCores->m_Name == "valid name 8");
1553 BOOST_CHECK(counterWNumberOfCores->m_Description == "valid description");
1554 BOOST_CHECK(counterWNumberOfCores->m_Units == "");
1555 BOOST_CHECK(counterWNumberOfCores->m_DeviceUid == 0);
1556 BOOST_CHECK(counterWNumberOfCores->m_CounterSetUid == 0);
1557 BOOST_CHECK(category->m_Counters.size() == 20);
Keith Davis3201eea2019-10-24 17:30:41 +01001558 for (size_t i = 0; i < numberOfCores; i++)
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001559 {
1560 BOOST_CHECK(category->m_Counters[category->m_Counters.size() - numberOfCores + i] ==
1561 counterWNumberOfCores->m_Uid + i);
1562 }
1563
1564 // Register a multi-core device for testing
1565 const std::string multiCoreDeviceName = "some_multi_core_device";
Keith Davis3201eea2019-10-24 17:30:41 +01001566 const Device* multiCoreDevice = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001567 BOOST_CHECK_NO_THROW(multiCoreDevice = counterDirectory.RegisterDevice(multiCoreDeviceName, 4));
1568 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
1569 BOOST_CHECK(multiCoreDevice);
1570 BOOST_CHECK(multiCoreDevice->m_Name == multiCoreDeviceName);
1571 BOOST_CHECK(multiCoreDevice->m_Uid >= 1);
1572 BOOST_CHECK(multiCoreDevice->m_Cores == 4);
1573
1574 // Register a counter with a valid parent category name and associated to the multi-core device
1575 const Counter* counterWMultiCoreDevice = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001576 BOOST_CHECK_NO_THROW(counterWMultiCoreDevice = counterDirectory.RegisterCounter(
Keith Davise394bd92019-12-02 15:12:19 +00001577 armnn::profiling::BACKEND_ID, 19, categoryName, 0, 1,
1578 123.45f, "valid name 9", "valid description",
Keith Davis3201eea2019-10-24 17:30:41 +01001579 armnn::EmptyOptional(), // Units
1580 armnn::EmptyOptional(), // Number of cores
1581 multiCoreDevice->m_Uid, // Device UID
1582 armnn::EmptyOptional())); // Counter set UID
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001583 BOOST_CHECK(counterDirectory.GetCounterCount() == 24);
1584 BOOST_CHECK(counterWMultiCoreDevice);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001585 BOOST_CHECK(counterWMultiCoreDevice->m_Uid > counter->m_Uid);
1586 BOOST_CHECK(counterWMultiCoreDevice->m_MaxCounterUid ==
1587 counterWMultiCoreDevice->m_Uid + multiCoreDevice->m_Cores - 1);
1588 BOOST_CHECK(counterWMultiCoreDevice->m_Class == 0);
1589 BOOST_CHECK(counterWMultiCoreDevice->m_Interpolation == 1);
1590 BOOST_CHECK(counterWMultiCoreDevice->m_Multiplier == 123.45f);
1591 BOOST_CHECK(counterWMultiCoreDevice->m_Name == "valid name 9");
1592 BOOST_CHECK(counterWMultiCoreDevice->m_Description == "valid description");
1593 BOOST_CHECK(counterWMultiCoreDevice->m_Units == "");
1594 BOOST_CHECK(counterWMultiCoreDevice->m_DeviceUid == multiCoreDevice->m_Uid);
1595 BOOST_CHECK(counterWMultiCoreDevice->m_CounterSetUid == 0);
1596 BOOST_CHECK(category->m_Counters.size() == 24);
Keith Davis3201eea2019-10-24 17:30:41 +01001597 for (size_t i = 0; i < 4; i++)
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001598 {
1599 BOOST_CHECK(category->m_Counters[category->m_Counters.size() - 4 + i] == counterWMultiCoreDevice->m_Uid + i);
1600 }
1601
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001602 // Register a multi-core device associate to a parent category for testing
1603 const std::string multiCoreDeviceNameWParentCategory = "some_multi_core_device_with_parent_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001604 const Device* multiCoreDeviceWParentCategory = nullptr;
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001605 BOOST_CHECK_NO_THROW(multiCoreDeviceWParentCategory =
Keith Davis3201eea2019-10-24 17:30:41 +01001606 counterDirectory.RegisterDevice(multiCoreDeviceNameWParentCategory, 2, categoryName));
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001607 BOOST_CHECK(counterDirectory.GetDeviceCount() == 3);
1608 BOOST_CHECK(multiCoreDeviceWParentCategory);
1609 BOOST_CHECK(multiCoreDeviceWParentCategory->m_Name == multiCoreDeviceNameWParentCategory);
1610 BOOST_CHECK(multiCoreDeviceWParentCategory->m_Uid >= 1);
1611 BOOST_CHECK(multiCoreDeviceWParentCategory->m_Cores == 2);
1612
1613 // Register a counter with a valid parent category name and getting the number of cores of the multi-core device
1614 // associated to that category
1615 const Counter* counterWMultiCoreDeviceWParentCategory = nullptr;
Sadik Armagan4c998992020-02-25 12:44:44 +00001616 uint16_t numberOfCourse = multiCoreDeviceWParentCategory->m_Cores;
Keith Davise394bd92019-12-02 15:12:19 +00001617 BOOST_CHECK_NO_THROW(counterWMultiCoreDeviceWParentCategory =
1618 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1619 100,
1620 categoryName,
1621 0,
1622 1,
1623 123.45f,
1624 "valid name 10",
1625 "valid description",
Sadik Armagan4c998992020-02-25 12:44:44 +00001626 armnn::EmptyOptional(), // Units
1627 numberOfCourse, // Number of cores
1628 armnn::EmptyOptional(), // Device UID
Keith Davise394bd92019-12-02 15:12:19 +00001629 armnn::EmptyOptional()));// Counter set UID
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001630 BOOST_CHECK(counterDirectory.GetCounterCount() == 26);
1631 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001632 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Uid > counter->m_Uid);
1633 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_MaxCounterUid ==
1634 counterWMultiCoreDeviceWParentCategory->m_Uid + multiCoreDeviceWParentCategory->m_Cores - 1);
1635 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Class == 0);
1636 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Interpolation == 1);
1637 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Multiplier == 123.45f);
1638 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Name == "valid name 10");
1639 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Description == "valid description");
1640 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Units == "");
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001641 BOOST_CHECK(category->m_Counters.size() == 26);
Keith Davis3201eea2019-10-24 17:30:41 +01001642 for (size_t i = 0; i < 2; i++)
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001643 {
1644 BOOST_CHECK(category->m_Counters[category->m_Counters.size() - 2 + i] ==
1645 counterWMultiCoreDeviceWParentCategory->m_Uid + i);
1646 }
1647
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001648 // Register a counter set for testing
1649 const std::string counterSetName = "some_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +01001650 const CounterSet* counterSet = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001651 BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
1652 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1653 BOOST_CHECK(counterSet);
1654 BOOST_CHECK(counterSet->m_Name == counterSetName);
1655 BOOST_CHECK(counterSet->m_Uid >= 1);
1656 BOOST_CHECK(counterSet->m_Count == 0);
1657
1658 // Register a counter with a valid parent category name and associated to a counter set
1659 const Counter* counterWCounterSet = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001660 BOOST_CHECK_NO_THROW(counterWCounterSet = counterDirectory.RegisterCounter(
Keith Davise394bd92019-12-02 15:12:19 +00001661 armnn::profiling::BACKEND_ID, 300,
Keith Davis3201eea2019-10-24 17:30:41 +01001662 categoryName, 0, 1, 123.45f, "valid name 11", "valid description",
1663 armnn::EmptyOptional(), // Units
1664 0, // Number of cores
1665 armnn::EmptyOptional(), // Device UID
1666 counterSet->m_Uid)); // Counter set UID
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001667 BOOST_CHECK(counterDirectory.GetCounterCount() == 27);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001668 BOOST_CHECK(counterWCounterSet);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001669 BOOST_CHECK(counterWCounterSet->m_Uid > counter->m_Uid);
1670 BOOST_CHECK(counterWCounterSet->m_MaxCounterUid == counterWCounterSet->m_Uid);
1671 BOOST_CHECK(counterWCounterSet->m_Class == 0);
1672 BOOST_CHECK(counterWCounterSet->m_Interpolation == 1);
1673 BOOST_CHECK(counterWCounterSet->m_Multiplier == 123.45f);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001674 BOOST_CHECK(counterWCounterSet->m_Name == "valid name 11");
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001675 BOOST_CHECK(counterWCounterSet->m_Description == "valid description");
1676 BOOST_CHECK(counterWCounterSet->m_Units == "");
1677 BOOST_CHECK(counterWCounterSet->m_DeviceUid == 0);
1678 BOOST_CHECK(counterWCounterSet->m_CounterSetUid == counterSet->m_Uid);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001679 BOOST_CHECK(category->m_Counters.size() == 27);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001680 BOOST_CHECK(category->m_Counters.back() == counterWCounterSet->m_Uid);
1681
1682 // Register a counter with a valid parent category name and associated to a device and a counter set
1683 const Counter* counterWDeviceWCounterSet = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001684 BOOST_CHECK_NO_THROW(counterWDeviceWCounterSet = counterDirectory.RegisterCounter(
Keith Davise394bd92019-12-02 15:12:19 +00001685 armnn::profiling::BACKEND_ID, 23,
Keith Davis3201eea2019-10-24 17:30:41 +01001686 categoryName, 0, 1, 123.45f, "valid name 12", "valid description",
1687 armnn::EmptyOptional(), // Units
1688 1, // Number of cores
1689 device->m_Uid, // Device UID
1690 counterSet->m_Uid)); // Counter set UID
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001691 BOOST_CHECK(counterDirectory.GetCounterCount() == 28);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001692 BOOST_CHECK(counterWDeviceWCounterSet);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001693 BOOST_CHECK(counterWDeviceWCounterSet->m_Uid > counter->m_Uid);
1694 BOOST_CHECK(counterWDeviceWCounterSet->m_MaxCounterUid == counterWDeviceWCounterSet->m_Uid);
1695 BOOST_CHECK(counterWDeviceWCounterSet->m_Class == 0);
1696 BOOST_CHECK(counterWDeviceWCounterSet->m_Interpolation == 1);
1697 BOOST_CHECK(counterWDeviceWCounterSet->m_Multiplier == 123.45f);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001698 BOOST_CHECK(counterWDeviceWCounterSet->m_Name == "valid name 12");
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001699 BOOST_CHECK(counterWDeviceWCounterSet->m_Description == "valid description");
1700 BOOST_CHECK(counterWDeviceWCounterSet->m_Units == "");
1701 BOOST_CHECK(counterWDeviceWCounterSet->m_DeviceUid == device->m_Uid);
1702 BOOST_CHECK(counterWDeviceWCounterSet->m_CounterSetUid == counterSet->m_Uid);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001703 BOOST_CHECK(category->m_Counters.size() == 28);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001704 BOOST_CHECK(category->m_Counters.back() == counterWDeviceWCounterSet->m_Uid);
1705
1706 // Register another category for testing
1707 const std::string anotherCategoryName = "some_other_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001708 const Category* anotherCategory = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001709 BOOST_CHECK_NO_THROW(anotherCategory = counterDirectory.RegisterCategory(anotherCategoryName));
1710 BOOST_CHECK(counterDirectory.GetCategoryCount() == 2);
1711 BOOST_CHECK(anotherCategory);
1712 BOOST_CHECK(anotherCategory != category);
1713 BOOST_CHECK(anotherCategory->m_Name == anotherCategoryName);
1714 BOOST_CHECK(anotherCategory->m_Counters.empty());
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001715
1716 // Register a counter to the other category
1717 const Counter* anotherCounter = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001718 BOOST_CHECK_NO_THROW(anotherCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID, 24,
1719 anotherCategoryName, 1, 0, .00043f,
Keith Davis3201eea2019-10-24 17:30:41 +01001720 "valid name", "valid description",
1721 armnn::EmptyOptional(), // Units
1722 armnn::EmptyOptional(), // Number of cores
1723 device->m_Uid, // Device UID
1724 counterSet->m_Uid)); // Counter set UID
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001725 BOOST_CHECK(counterDirectory.GetCounterCount() == 29);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001726 BOOST_CHECK(anotherCounter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001727 BOOST_CHECK(anotherCounter->m_MaxCounterUid == anotherCounter->m_Uid);
1728 BOOST_CHECK(anotherCounter->m_Class == 1);
1729 BOOST_CHECK(anotherCounter->m_Interpolation == 0);
1730 BOOST_CHECK(anotherCounter->m_Multiplier == .00043f);
1731 BOOST_CHECK(anotherCounter->m_Name == "valid name");
1732 BOOST_CHECK(anotherCounter->m_Description == "valid description");
1733 BOOST_CHECK(anotherCounter->m_Units == "");
1734 BOOST_CHECK(anotherCounter->m_DeviceUid == device->m_Uid);
1735 BOOST_CHECK(anotherCounter->m_CounterSetUid == counterSet->m_Uid);
1736 BOOST_CHECK(anotherCategory->m_Counters.size() == 1);
1737 BOOST_CHECK(anotherCategory->m_Counters.back() == anotherCounter->m_Uid);
Matteo Martincighab173e92019-09-05 12:02:04 +01001738}
1739
Ferran Balaguer1b941722019-08-28 16:57:18 +01001740BOOST_AUTO_TEST_CASE(CounterSelectionCommandHandlerParseData)
1741{
1742 using boost::numeric_cast;
1743
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01001744 ProfilingStateMachine profilingStateMachine;
1745
Ferran Balaguer1b941722019-08-28 16:57:18 +01001746 class TestCaptureThread : public IPeriodicCounterCapture
1747 {
Keith Davis3201eea2019-10-24 17:30:41 +01001748 void Start() override
1749 {}
1750 void Stop() override
1751 {}
Ferran Balaguer1b941722019-08-28 16:57:18 +01001752 };
1753
Matteo Martincighe8485382019-10-10 14:08:21 +01001754 class TestReadCounterValues : public IReadCounterValues
1755 {
Keith Davis3201eea2019-10-24 17:30:41 +01001756 bool IsCounterRegistered(uint16_t counterUid) const override
1757 {
Jan Eilers8eb25602020-03-09 12:13:48 +00001758 armnn::IgnoreUnused(counterUid);
Keith Davis3201eea2019-10-24 17:30:41 +01001759 return true;
1760 }
1761 uint16_t GetCounterCount() const override
1762 {
1763 return 0;
1764 }
Finn Williamsf3fcf322020-05-11 14:38:02 +01001765 uint32_t GetAbsoluteCounterValue(uint16_t counterUid) const override
1766 {
1767 armnn::IgnoreUnused(counterUid);
1768 return 0;
1769 }
1770 uint32_t GetDeltaCounterValue(uint16_t counterUid) override
Keith Davis3201eea2019-10-24 17:30:41 +01001771 {
Jan Eilers8eb25602020-03-09 12:13:48 +00001772 armnn::IgnoreUnused(counterUid);
Keith Davis3201eea2019-10-24 17:30:41 +01001773 return 0;
1774 }
Matteo Martincighe8485382019-10-10 14:08:21 +01001775 };
Jim Flynn397043f2019-10-17 17:37:10 +01001776 const uint32_t familyId = 0;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001777 const uint32_t packetId = 0x40000;
1778
1779 uint32_t version = 1;
Finn Williams032bc742020-02-12 11:02:34 +00001780 const std::unordered_map<armnn::BackendId,
1781 std::shared_ptr<armnn::profiling::IBackendProfilingContext>> backendProfilingContext;
1782 CounterIdMap counterIdMap;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001783 Holder holder;
1784 TestCaptureThread captureThread;
Matteo Martincighe8485382019-10-10 14:08:21 +01001785 TestReadCounterValues readCounterValues;
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001786 MockBufferManager mockBuffer(512);
Sadik Armagan3896b472020-02-10 12:24:15 +00001787 SendCounterPacket sendCounterPacket(mockBuffer);
1788 SendThread sendThread(profilingStateMachine, mockBuffer, sendCounterPacket);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001789
1790 uint32_t sizeOfUint32 = numeric_cast<uint32_t>(sizeof(uint32_t));
1791 uint32_t sizeOfUint16 = numeric_cast<uint32_t>(sizeof(uint16_t));
1792
1793 // Data with period and counters
Colm Donelan02705242019-11-14 14:19:07 +00001794 uint32_t period1 = armnn::LOWEST_CAPTURE_PERIOD;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001795 uint32_t dataLength1 = 8;
Keith Davis3201eea2019-10-24 17:30:41 +01001796 uint32_t offset = 0;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001797
Matteo Martincigh67ef2a52019-10-10 13:29:02 +01001798 std::unique_ptr<unsigned char[]> uniqueData1 = std::make_unique<unsigned char[]>(dataLength1);
Keith Davis3201eea2019-10-24 17:30:41 +01001799 unsigned char* data1 = reinterpret_cast<unsigned char*>(uniqueData1.get());
FinnWilliamsArma0c78712019-09-16 12:06:47 +01001800
Ferran Balaguer1b941722019-08-28 16:57:18 +01001801 WriteUint32(data1, offset, period1);
1802 offset += sizeOfUint32;
1803 WriteUint16(data1, offset, 4000);
1804 offset += sizeOfUint16;
1805 WriteUint16(data1, offset, 5000);
1806
FinnWilliamsArma0c78712019-09-16 12:06:47 +01001807 Packet packetA(packetId, dataLength1, uniqueData1);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001808
Finn Williams032bc742020-02-12 11:02:34 +00001809 PeriodicCounterSelectionCommandHandler commandHandler(familyId, packetId, version, backendProfilingContext,
1810 counterIdMap, holder, 10000u, captureThread,
Keith Davis3201eea2019-10-24 17:30:41 +01001811 readCounterValues, sendCounterPacket, profilingStateMachine);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001812
Matteo Martincighe8485382019-10-10 14:08:21 +01001813 profilingStateMachine.TransitionToState(ProfilingState::Uninitialised);
1814 BOOST_CHECK_THROW(commandHandler(packetA), armnn::RuntimeException);
1815 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
1816 BOOST_CHECK_THROW(commandHandler(packetA), armnn::RuntimeException);
1817 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
1818 BOOST_CHECK_THROW(commandHandler(packetA), armnn::RuntimeException);
1819 profilingStateMachine.TransitionToState(ProfilingState::Active);
1820 BOOST_CHECK_NO_THROW(commandHandler(packetA));
1821
1822 const std::vector<uint16_t> counterIdsA = holder.GetCaptureData().GetCounterIds();
Ferran Balaguer1b941722019-08-28 16:57:18 +01001823
1824 BOOST_TEST(holder.GetCaptureData().GetCapturePeriod() == period1);
Matteo Martincighe8485382019-10-10 14:08:21 +01001825 BOOST_TEST(counterIdsA.size() == 2);
1826 BOOST_TEST(counterIdsA[0] == 4000);
1827 BOOST_TEST(counterIdsA[1] == 5000);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001828
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001829 auto readBuffer = mockBuffer.GetReadableBuffer();
Ferran Balaguer1b941722019-08-28 16:57:18 +01001830
1831 offset = 0;
1832
1833 uint32_t headerWord0 = ReadUint32(readBuffer, offset);
1834 offset += sizeOfUint32;
1835 uint32_t headerWord1 = ReadUint32(readBuffer, offset);
1836 offset += sizeOfUint32;
1837 uint32_t period = ReadUint32(readBuffer, offset);
1838
Colm Donelan02705242019-11-14 14:19:07 +00001839 BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family
1840 BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 4); // packet id
1841 BOOST_TEST(headerWord1 == 8); // data length
1842 BOOST_TEST(period == armnn::LOWEST_CAPTURE_PERIOD); // capture period
Ferran Balaguer1b941722019-08-28 16:57:18 +01001843
1844 uint16_t counterId = 0;
1845 offset += sizeOfUint32;
1846 counterId = ReadUint16(readBuffer, offset);
1847 BOOST_TEST(counterId == 4000);
1848 offset += sizeOfUint16;
1849 counterId = ReadUint16(readBuffer, offset);
1850 BOOST_TEST(counterId == 5000);
1851
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001852 mockBuffer.MarkRead(readBuffer);
1853
Ferran Balaguer1b941722019-08-28 16:57:18 +01001854 // Data with period only
Colm Donelan02705242019-11-14 14:19:07 +00001855 uint32_t period2 = 9000; // We'll specify a value below LOWEST_CAPTURE_PERIOD. It should be pulled upwards.
Ferran Balaguer1b941722019-08-28 16:57:18 +01001856 uint32_t dataLength2 = 4;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001857
Matteo Martincigh67ef2a52019-10-10 13:29:02 +01001858 std::unique_ptr<unsigned char[]> uniqueData2 = std::make_unique<unsigned char[]>(dataLength2);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001859
FinnWilliamsArma0c78712019-09-16 12:06:47 +01001860 WriteUint32(reinterpret_cast<unsigned char*>(uniqueData2.get()), 0, period2);
1861
1862 Packet packetB(packetId, dataLength2, uniqueData2);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001863
1864 commandHandler(packetB);
1865
Matteo Martincighe8485382019-10-10 14:08:21 +01001866 const std::vector<uint16_t> counterIdsB = holder.GetCaptureData().GetCounterIds();
Ferran Balaguer1b941722019-08-28 16:57:18 +01001867
Colm Donelan02705242019-11-14 14:19:07 +00001868 // Value should have been pulled up from 9000 to LOWEST_CAPTURE_PERIOD.
1869 BOOST_TEST(holder.GetCaptureData().GetCapturePeriod() == armnn::LOWEST_CAPTURE_PERIOD);
Matteo Martincighe8485382019-10-10 14:08:21 +01001870 BOOST_TEST(counterIdsB.size() == 0);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001871
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001872 readBuffer = mockBuffer.GetReadableBuffer();
Ferran Balaguer1b941722019-08-28 16:57:18 +01001873
1874 offset = 0;
1875
1876 headerWord0 = ReadUint32(readBuffer, offset);
1877 offset += sizeOfUint32;
1878 headerWord1 = ReadUint32(readBuffer, offset);
1879 offset += sizeOfUint32;
1880 period = ReadUint32(readBuffer, offset);
1881
Colm Donelan02705242019-11-14 14:19:07 +00001882 BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family
1883 BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 4); // packet id
1884 BOOST_TEST(headerWord1 == 4); // data length
1885 BOOST_TEST(period == armnn::LOWEST_CAPTURE_PERIOD); // capture period
Ferran Balaguer1b941722019-08-28 16:57:18 +01001886}
1887
Keith Davis33ed2212020-03-30 10:43:41 +01001888BOOST_AUTO_TEST_CASE(CheckTimelineActivationAndDeactivation)
1889{
1890 class TestReportStructure : public IReportStructure
1891 {
1892 public:
1893 virtual void ReportStructure() override
1894 {
1895 m_ReportStructureCalled = true;
1896 }
1897
1898 bool m_ReportStructureCalled = false;
1899 };
1900
1901 class TestNotifyBackends : public INotifyBackends
1902 {
1903 public:
1904 TestNotifyBackends() : m_timelineReporting(false) {}
1905 virtual void NotifyBackendsForTimelineReporting() override
1906 {
1907 m_TestNotifyBackendsCalled = m_timelineReporting.load();
1908 }
1909
1910 bool m_TestNotifyBackendsCalled = false;
1911 std::atomic<bool> m_timelineReporting;
1912 };
1913
1914 PacketVersionResolver packetVersionResolver;
1915
1916 BufferManager bufferManager(512);
1917 SendTimelinePacket sendTimelinePacket(bufferManager);
1918 ProfilingStateMachine stateMachine;
1919 TestReportStructure testReportStructure;
1920 TestNotifyBackends testNotifyBackends;
1921
1922 profiling::ActivateTimelineReportingCommandHandler activateTimelineReportingCommandHandler(0,
1923 6,
1924 packetVersionResolver.ResolvePacketVersion(0, 6)
1925 .GetEncodedValue(),
1926 sendTimelinePacket,
1927 stateMachine,
1928 testReportStructure,
1929 testNotifyBackends.m_timelineReporting,
1930 testNotifyBackends);
1931
1932 // Write an "ActivateTimelineReporting" packet into the mock profiling connection, to simulate an input from an
1933 // external profiling service
1934 const uint32_t packetFamily1 = 0;
1935 const uint32_t packetId1 = 6;
1936 uint32_t packetHeader1 = ConstructHeader(packetFamily1, packetId1);
1937
1938 // Create the ActivateTimelineReportingPacket
1939 Packet ActivateTimelineReportingPacket(packetHeader1); // Length == 0
1940
1941 BOOST_CHECK_THROW(
1942 activateTimelineReportingCommandHandler.operator()(ActivateTimelineReportingPacket), armnn::Exception);
1943
1944 stateMachine.TransitionToState(ProfilingState::NotConnected);
1945 BOOST_CHECK_THROW(
1946 activateTimelineReportingCommandHandler.operator()(ActivateTimelineReportingPacket), armnn::Exception);
1947
1948 stateMachine.TransitionToState(ProfilingState::WaitingForAck);
1949 BOOST_CHECK_THROW(
1950 activateTimelineReportingCommandHandler.operator()(ActivateTimelineReportingPacket), armnn::Exception);
1951
1952 stateMachine.TransitionToState(ProfilingState::Active);
1953 activateTimelineReportingCommandHandler.operator()(ActivateTimelineReportingPacket);
1954
1955 BOOST_CHECK(testReportStructure.m_ReportStructureCalled);
1956 BOOST_CHECK(testNotifyBackends.m_TestNotifyBackendsCalled);
1957 BOOST_CHECK(testNotifyBackends.m_timelineReporting.load());
1958
1959 DeactivateTimelineReportingCommandHandler deactivateTimelineReportingCommandHandler(0,
1960 7,
1961 packetVersionResolver.ResolvePacketVersion(0, 7).GetEncodedValue(),
1962 testNotifyBackends.m_timelineReporting,
1963 stateMachine,
1964 testNotifyBackends);
1965
1966 const uint32_t packetFamily2 = 0;
1967 const uint32_t packetId2 = 7;
1968 uint32_t packetHeader2 = ConstructHeader(packetFamily2, packetId2);
1969
1970 // Create the DeactivateTimelineReportingPacket
1971 Packet deactivateTimelineReportingPacket(packetHeader2); // Length == 0
1972
1973 stateMachine.Reset();
1974 BOOST_CHECK_THROW(
1975 deactivateTimelineReportingCommandHandler.operator()(deactivateTimelineReportingPacket), armnn::Exception);
1976
1977 stateMachine.TransitionToState(ProfilingState::NotConnected);
1978 BOOST_CHECK_THROW(
1979 deactivateTimelineReportingCommandHandler.operator()(deactivateTimelineReportingPacket), armnn::Exception);
1980
1981 stateMachine.TransitionToState(ProfilingState::WaitingForAck);
1982 BOOST_CHECK_THROW(
1983 deactivateTimelineReportingCommandHandler.operator()(deactivateTimelineReportingPacket), armnn::Exception);
1984
1985 stateMachine.TransitionToState(ProfilingState::Active);
1986 deactivateTimelineReportingCommandHandler.operator()(deactivateTimelineReportingPacket);
1987
1988 BOOST_CHECK(!testNotifyBackends.m_TestNotifyBackendsCalled);
1989 BOOST_CHECK(!testNotifyBackends.m_timelineReporting.load());
1990}
1991
1992BOOST_AUTO_TEST_CASE(CheckProfilingServiceNotActive)
1993{
1994 using namespace armnn;
1995 using namespace armnn::profiling;
1996
1997 // Create runtime in which the test will run
1998 armnn::IRuntime::CreationOptions options;
1999 options.m_ProfilingOptions.m_EnableProfiling = true;
2000
2001 armnn::Runtime runtime(options);
2002 profiling::ProfilingServiceRuntimeHelper profilingServiceHelper(GetProfilingService(&runtime));
2003 profilingServiceHelper.ForceTransitionToState(ProfilingState::NotConnected);
2004 profilingServiceHelper.ForceTransitionToState(ProfilingState::WaitingForAck);
2005 profilingServiceHelper.ForceTransitionToState(ProfilingState::Active);
2006
2007 profiling::BufferManager& bufferManager = profilingServiceHelper.GetProfilingBufferManager();
2008 auto readableBuffer = bufferManager.GetReadableBuffer();
2009
2010 // Profiling is enabled, the post-optimisation structure should be created
2011 BOOST_CHECK(readableBuffer == nullptr);
2012}
2013
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002014BOOST_AUTO_TEST_CASE(CheckConnectionAcknowledged)
2015{
2016 using boost::numeric_cast;
2017
Keith Davis3201eea2019-10-24 17:30:41 +01002018 const uint32_t packetFamilyId = 0;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002019 const uint32_t connectionPacketId = 0x10000;
Keith Davis3201eea2019-10-24 17:30:41 +01002020 const uint32_t version = 1;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002021
2022 uint32_t sizeOfUint32 = numeric_cast<uint32_t>(sizeof(uint32_t));
2023 uint32_t sizeOfUint16 = numeric_cast<uint32_t>(sizeof(uint16_t));
2024
2025 // Data with period and counters
Keith Davis3201eea2019-10-24 17:30:41 +01002026 uint32_t period1 = 10;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002027 uint32_t dataLength1 = 8;
Keith Davis3201eea2019-10-24 17:30:41 +01002028 uint32_t offset = 0;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002029
Matteo Martincigh67ef2a52019-10-10 13:29:02 +01002030 std::unique_ptr<unsigned char[]> uniqueData1 = std::make_unique<unsigned char[]>(dataLength1);
Keith Davis3201eea2019-10-24 17:30:41 +01002031 unsigned char* data1 = reinterpret_cast<unsigned char*>(uniqueData1.get());
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002032
2033 WriteUint32(data1, offset, period1);
2034 offset += sizeOfUint32;
2035 WriteUint16(data1, offset, 4000);
2036 offset += sizeOfUint16;
2037 WriteUint16(data1, offset, 5000);
2038
2039 Packet packetA(connectionPacketId, dataLength1, uniqueData1);
2040
2041 ProfilingStateMachine profilingState(ProfilingState::Uninitialised);
2042 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002043 CounterDirectory counterDirectory;
2044 MockBufferManager mockBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002045 SendCounterPacket sendCounterPacket(mockBuffer);
2046 SendThread sendThread(profilingState, mockBuffer, sendCounterPacket);
Matteo Martincighcdfb9412019-11-08 11:23:06 +00002047 SendTimelinePacket sendTimelinePacket(mockBuffer);
Jim Flynn6398a982020-05-27 17:05:21 +01002048 MockProfilingServiceStatus mockProfilingServiceStatus;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002049
Jim Flynn6398a982020-05-27 17:05:21 +01002050 ConnectionAcknowledgedCommandHandler commandHandler(packetFamilyId,
2051 connectionPacketId,
2052 version,
2053 counterDirectory,
2054 sendCounterPacket,
2055 sendTimelinePacket,
2056 profilingState,
2057 mockProfilingServiceStatus);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002058
2059 // command handler received packet on ProfilingState::Uninitialised
2060 BOOST_CHECK_THROW(commandHandler(packetA), armnn::Exception);
2061
2062 profilingState.TransitionToState(ProfilingState::NotConnected);
2063 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::NotConnected);
2064 // command handler received packet on ProfilingState::NotConnected
2065 BOOST_CHECK_THROW(commandHandler(packetA), armnn::Exception);
2066
2067 profilingState.TransitionToState(ProfilingState::WaitingForAck);
2068 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::WaitingForAck);
2069 // command handler received packet on ProfilingState::WaitingForAck
Matteo Martincighd0613b52019-10-09 16:47:04 +01002070 BOOST_CHECK_NO_THROW(commandHandler(packetA));
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002071 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::Active);
2072
2073 // command handler received packet on ProfilingState::Active
Matteo Martincighd0613b52019-10-09 16:47:04 +01002074 BOOST_CHECK_NO_THROW(commandHandler(packetA));
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002075 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::Active);
2076
2077 // command handler received different packet
2078 const uint32_t differentPacketId = 0x40000;
2079 Packet packetB(differentPacketId, dataLength1, uniqueData1);
Matteo Martincighd0613b52019-10-09 16:47:04 +01002080 profilingState.TransitionToState(ProfilingState::NotConnected);
2081 profilingState.TransitionToState(ProfilingState::WaitingForAck);
Jim Flynn6398a982020-05-27 17:05:21 +01002082 ConnectionAcknowledgedCommandHandler differentCommandHandler(packetFamilyId,
2083 differentPacketId,
2084 version,
2085 counterDirectory,
2086 sendCounterPacket,
2087 sendTimelinePacket,
2088 profilingState,
2089 mockProfilingServiceStatus);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002090 BOOST_CHECK_THROW(differentCommandHandler(packetB), armnn::Exception);
2091}
2092
Sadik Armagana97a0be2020-03-03 10:44:56 +00002093BOOST_AUTO_TEST_CASE(CheckSocketConnectionException)
Teresa Charlin9bab4962019-09-06 12:28:35 +01002094{
Sadik Armagana97a0be2020-03-03 10:44:56 +00002095 // Check that creating a SocketProfilingConnection armnnProfiling in an exception as the Gator UDS doesn't exist.
2096 BOOST_CHECK_THROW(new SocketProfilingConnection(), armnnProfiling::SocketConnectionException);
2097}
2098
2099BOOST_AUTO_TEST_CASE(CheckSocketConnectionException2)
2100{
2101 try
2102 {
2103 new SocketProfilingConnection();
2104 }
Pavel Macenauer855a47b2020-05-26 10:54:22 +00002105 catch (const armnnProfiling::SocketConnectionException& ex)
Sadik Armagana97a0be2020-03-03 10:44:56 +00002106 {
2107 BOOST_CHECK(ex.GetSocketFd() == 0);
2108 BOOST_CHECK(ex.GetErrorNo() == 111);
2109 BOOST_CHECK(ex.what()
2110 == std::string("SocketProfilingConnection: Cannot connect to stream socket: Connection refused"));
2111 }
Teresa Charlin9bab4962019-09-06 12:28:35 +01002112}
2113
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002114BOOST_AUTO_TEST_CASE(SwTraceIsValidCharTest)
2115{
2116 // Only ASCII 7-bit encoding supported
2117 for (unsigned char c = 0; c < 128; c++)
2118 {
2119 BOOST_CHECK(SwTraceCharPolicy::IsValidChar(c));
2120 }
2121
2122 // Not ASCII
2123 for (unsigned char c = 255; c >= 128; c++)
2124 {
2125 BOOST_CHECK(!SwTraceCharPolicy::IsValidChar(c));
2126 }
2127}
2128
2129BOOST_AUTO_TEST_CASE(SwTraceIsValidNameCharTest)
2130{
2131 // Only alpha-numeric and underscore ASCII 7-bit encoding supported
2132 const unsigned char validChars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
2133 for (unsigned char i = 0; i < sizeof(validChars) / sizeof(validChars[0]) - 1; i++)
2134 {
2135 BOOST_CHECK(SwTraceNameCharPolicy::IsValidChar(validChars[i]));
2136 }
2137
2138 // Non alpha-numeric chars
2139 for (unsigned char c = 0; c < 48; c++)
2140 {
2141 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
2142 }
2143 for (unsigned char c = 58; c < 65; c++)
2144 {
2145 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
2146 }
2147 for (unsigned char c = 91; c < 95; c++)
2148 {
2149 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
2150 }
2151 for (unsigned char c = 96; c < 97; c++)
2152 {
2153 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
2154 }
2155 for (unsigned char c = 123; c < 128; c++)
2156 {
2157 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
2158 }
2159
2160 // Not ASCII
2161 for (unsigned char c = 255; c >= 128; c++)
2162 {
2163 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
2164 }
2165}
2166
2167BOOST_AUTO_TEST_CASE(IsValidSwTraceStringTest)
2168{
2169 // Valid SWTrace strings
2170 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>(""));
2171 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("_"));
2172 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("0123"));
2173 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("valid_string"));
2174 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("VALID_string_456"));
2175 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>(" "));
2176 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("valid string"));
2177 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("!$%"));
2178 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("valid|\\~string#123"));
2179
2180 // Invalid SWTrace strings
2181 BOOST_CHECK(!IsValidSwTraceString<SwTraceCharPolicy>("€£"));
2182 BOOST_CHECK(!IsValidSwTraceString<SwTraceCharPolicy>("invalid‡string"));
2183 BOOST_CHECK(!IsValidSwTraceString<SwTraceCharPolicy>("12Ž34"));
2184}
2185
2186BOOST_AUTO_TEST_CASE(IsValidSwTraceNameStringTest)
2187{
2188 // Valid SWTrace name strings
2189 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>(""));
2190 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>("_"));
2191 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>("0123"));
2192 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>("valid_string"));
2193 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>("VALID_string_456"));
2194
2195 // Invalid SWTrace name strings
2196 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>(" "));
2197 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("invalid string"));
2198 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("!$%"));
2199 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("invalid|\\~string#123"));
2200 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("€£"));
2201 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("invalid‡string"));
2202 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("12Ž34"));
2203}
2204
2205template <typename SwTracePolicy>
2206void StringToSwTraceStringTestHelper(const std::string& testString, std::vector<uint32_t> buffer, size_t expectedSize)
2207{
2208 // Convert the test string to a SWTrace string
2209 BOOST_CHECK(StringToSwTraceString<SwTracePolicy>(testString, buffer));
2210
2211 // The buffer must contain at least the length of the string
2212 BOOST_CHECK(!buffer.empty());
2213
2214 // The buffer must be of the expected size (in words)
2215 BOOST_CHECK(buffer.size() == expectedSize);
2216
2217 // The first word of the byte must be the length of the string including the null-terminator
2218 BOOST_CHECK(buffer[0] == testString.size() + 1);
2219
2220 // The contents of the buffer must match the test string
2221 BOOST_CHECK(std::memcmp(testString.data(), buffer.data() + 1, testString.size()) == 0);
2222
2223 // The buffer must include the null-terminator at the end of the string
2224 size_t nullTerminatorIndex = sizeof(uint32_t) + testString.size();
Keith Davis3201eea2019-10-24 17:30:41 +01002225 BOOST_CHECK(reinterpret_cast<unsigned char*>(buffer.data())[nullTerminatorIndex] == '\0');
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002226}
2227
2228BOOST_AUTO_TEST_CASE(StringToSwTraceStringTest)
2229{
2230 std::vector<uint32_t> buffer;
2231
2232 // Valid SWTrace strings (expected size in words)
2233 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("", buffer, 2);
2234 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("_", buffer, 2);
2235 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("0123", buffer, 3);
2236 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("valid_string", buffer, 5);
2237 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("VALID_string_456", buffer, 6);
2238 StringToSwTraceStringTestHelper<SwTraceCharPolicy>(" ", buffer, 2);
2239 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("valid string", buffer, 5);
2240 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("!$%", buffer, 2);
2241 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("valid|\\~string#123", buffer, 6);
2242
2243 // Invalid SWTrace strings
2244 BOOST_CHECK(!StringToSwTraceString<SwTraceCharPolicy>("€£", buffer));
2245 BOOST_CHECK(buffer.empty());
2246 BOOST_CHECK(!StringToSwTraceString<SwTraceCharPolicy>("invalid‡string", buffer));
2247 BOOST_CHECK(buffer.empty());
2248 BOOST_CHECK(!StringToSwTraceString<SwTraceCharPolicy>("12Ž34", buffer));
2249 BOOST_CHECK(buffer.empty());
2250}
2251
2252BOOST_AUTO_TEST_CASE(StringToSwTraceNameStringTest)
2253{
2254 std::vector<uint32_t> buffer;
2255
2256 // Valid SWTrace namestrings (expected size in words)
2257 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("", buffer, 2);
2258 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("_", buffer, 2);
2259 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("0123", buffer, 3);
2260 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("valid_string", buffer, 5);
2261 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("VALID_string_456", buffer, 6);
2262
2263 // Invalid SWTrace namestrings
2264 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>(" ", buffer));
2265 BOOST_CHECK(buffer.empty());
2266 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("invalid string", buffer));
2267 BOOST_CHECK(buffer.empty());
2268 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("!$%", buffer));
2269 BOOST_CHECK(buffer.empty());
2270 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("invalid|\\~string#123", buffer));
2271 BOOST_CHECK(buffer.empty());
2272 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("€£", buffer));
2273 BOOST_CHECK(buffer.empty());
2274 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("invalid‡string", buffer));
2275 BOOST_CHECK(buffer.empty());
2276 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("12Ž34", buffer));
2277 BOOST_CHECK(buffer.empty());
2278}
2279
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002280BOOST_AUTO_TEST_CASE(CheckPeriodicCounterCaptureThread)
2281{
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002282 class CaptureReader : public IReadCounterValues
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002283 {
2284 public:
Finn Williamsf4d59a62019-10-14 15:55:18 +01002285 CaptureReader(uint16_t counterSize)
2286 {
Keith Davis3201eea2019-10-24 17:30:41 +01002287 for (uint16_t i = 0; i < counterSize; ++i)
Finn Williamsf4d59a62019-10-14 15:55:18 +01002288 {
2289 m_Data[i] = 0;
2290 }
2291 m_CounterSize = counterSize;
2292 }
2293 //not used
Matteo Martincighe8485382019-10-10 14:08:21 +01002294 bool IsCounterRegistered(uint16_t counterUid) const override
2295 {
Jan Eilers8eb25602020-03-09 12:13:48 +00002296 armnn::IgnoreUnused(counterUid);
Finn Williamsf4d59a62019-10-14 15:55:18 +01002297 return false;
Matteo Martincighe8485382019-10-10 14:08:21 +01002298 }
2299
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002300 uint16_t GetCounterCount() const override
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002301 {
Finn Williamsf4d59a62019-10-14 15:55:18 +01002302 return m_CounterSize;
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002303 }
2304
Finn Williamsf3fcf322020-05-11 14:38:02 +01002305 uint32_t GetAbsoluteCounterValue(uint16_t counterUid) const override
2306 {
2307 if (counterUid > m_CounterSize)
2308 {
2309 BOOST_FAIL("Invalid counter Uid");
2310 }
2311 return m_Data.at(counterUid).load();
2312 }
2313
2314 uint32_t GetDeltaCounterValue(uint16_t counterUid) override
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002315 {
Keith Davis3201eea2019-10-24 17:30:41 +01002316 if (counterUid > m_CounterSize)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002317 {
Finn Williamsf4d59a62019-10-14 15:55:18 +01002318 BOOST_FAIL("Invalid counter Uid");
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002319 }
Matteo Martincighe8485382019-10-10 14:08:21 +01002320 return m_Data.at(counterUid).load();
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002321 }
2322
Matteo Martincighe8485382019-10-10 14:08:21 +01002323 void SetCounterValue(uint16_t counterUid, uint32_t value)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002324 {
Keith Davis3201eea2019-10-24 17:30:41 +01002325 if (counterUid > m_CounterSize)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002326 {
Finn Williamsf4d59a62019-10-14 15:55:18 +01002327 BOOST_FAIL("Invalid counter Uid");
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002328 }
Finn Williamsf4d59a62019-10-14 15:55:18 +01002329 m_Data.at(counterUid).store(value);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002330 }
2331
2332 private:
Matteo Martincighe8485382019-10-10 14:08:21 +01002333 std::unordered_map<uint16_t, std::atomic<uint32_t>> m_Data;
Finn Williamsf4d59a62019-10-14 15:55:18 +01002334 uint16_t m_CounterSize;
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002335 };
2336
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002337 ProfilingStateMachine profilingStateMachine;
2338
Finn Williams032bc742020-02-12 11:02:34 +00002339 const std::unordered_map<armnn::BackendId,
2340 std::shared_ptr<armnn::profiling::IBackendProfilingContext>> backendProfilingContext;
2341 CounterIdMap counterIdMap;
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002342 Holder data;
2343 std::vector<uint16_t> captureIds1 = { 0, 1 };
2344 std::vector<uint16_t> captureIds2;
2345
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002346 MockBufferManager mockBuffer(512);
Sadik Armagan3896b472020-02-10 12:24:15 +00002347 SendCounterPacket sendCounterPacket(mockBuffer);
2348 SendThread sendThread(profilingStateMachine, mockBuffer, sendCounterPacket);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002349
2350 std::vector<uint16_t> counterIds;
Finn Williamsf4d59a62019-10-14 15:55:18 +01002351 CaptureReader captureReader(2);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002352
Keith Davis3201eea2019-10-24 17:30:41 +01002353 unsigned int valueA = 10;
2354 unsigned int valueB = 15;
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002355 unsigned int numSteps = 5;
2356
Finn Williams032bc742020-02-12 11:02:34 +00002357 PeriodicCounterCapture periodicCounterCapture(std::ref(data), std::ref(sendCounterPacket), captureReader,
2358 counterIdMap, backendProfilingContext);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002359
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002360 for (unsigned int i = 0; i < numSteps; ++i)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002361 {
Finn Williams032bc742020-02-12 11:02:34 +00002362 data.SetCaptureData(1, captureIds1, {});
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002363 captureReader.SetCounterValue(0, valueA * (i + 1));
2364 captureReader.SetCounterValue(1, valueB * (i + 1));
2365
2366 periodicCounterCapture.Start();
Finn Williamsf4d59a62019-10-14 15:55:18 +01002367 periodicCounterCapture.Stop();
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002368 }
2369
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002370 auto buffer = mockBuffer.GetReadableBuffer();
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002371
2372 uint32_t headerWord0 = ReadUint32(buffer, 0);
2373 uint32_t headerWord1 = ReadUint32(buffer, 4);
2374
Jim Flynnfc365622019-12-04 10:07:20 +00002375 BOOST_TEST(((headerWord0 >> 26) & 0x0000003F) == 3); // packet family
Keith Davis3201eea2019-10-24 17:30:41 +01002376 BOOST_TEST(((headerWord0 >> 19) & 0x0000007F) == 0); // packet class
2377 BOOST_TEST(((headerWord0 >> 16) & 0x00000007) == 0); // packet type
Matteo Martincigh8d9590e2019-10-15 09:35:29 +01002378 BOOST_TEST(headerWord1 == 20);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002379
Keith Davis3201eea2019-10-24 17:30:41 +01002380 uint32_t offset = 16;
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002381 uint16_t readIndex = ReadUint16(buffer, offset);
2382 BOOST_TEST(0 == readIndex);
2383
2384 offset += 2;
2385 uint32_t readValue = ReadUint32(buffer, offset);
2386 BOOST_TEST((valueA * numSteps) == readValue);
2387
2388 offset += 4;
2389 readIndex = ReadUint16(buffer, offset);
2390 BOOST_TEST(1 == readIndex);
2391
2392 offset += 2;
2393 readValue = ReadUint32(buffer, offset);
2394 BOOST_TEST((valueB * numSteps) == readValue);
2395}
2396
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002397BOOST_AUTO_TEST_CASE(RequestCounterDirectoryCommandHandlerTest1)
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002398{
2399 using boost::numeric_cast;
2400
Jim Flynn397043f2019-10-17 17:37:10 +01002401 const uint32_t familyId = 0;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002402 const uint32_t packetId = 3;
Keith Davis3201eea2019-10-24 17:30:41 +01002403 const uint32_t version = 1;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002404 ProfilingStateMachine profilingStateMachine;
2405 CounterDirectory counterDirectory;
Matteo Martincigh9723d022019-11-13 10:56:41 +00002406 MockBufferManager mockBuffer1(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002407 SendCounterPacket sendCounterPacket(mockBuffer1);
2408 SendThread sendThread(profilingStateMachine, mockBuffer1, sendCounterPacket);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002409 MockBufferManager mockBuffer2(1024);
2410 SendTimelinePacket sendTimelinePacket(mockBuffer2);
Keith Davis3201eea2019-10-24 17:30:41 +01002411 RequestCounterDirectoryCommandHandler commandHandler(familyId, packetId, version, counterDirectory,
Matteo Martincigh9723d022019-11-13 10:56:41 +00002412 sendCounterPacket, sendTimelinePacket, profilingStateMachine);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002413
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002414 const uint32_t wrongPacketId = 47;
Keith Davis3201eea2019-10-24 17:30:41 +01002415 const uint32_t wrongHeader = (wrongPacketId & 0x000003FF) << 16;
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002416
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002417 Packet wrongPacket(wrongHeader);
2418
2419 profilingStateMachine.TransitionToState(ProfilingState::Uninitialised);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002420 BOOST_CHECK_THROW(commandHandler(wrongPacket), armnn::RuntimeException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002421 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002422 BOOST_CHECK_THROW(commandHandler(wrongPacket), armnn::RuntimeException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002423 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002424 BOOST_CHECK_THROW(commandHandler(wrongPacket), armnn::RuntimeException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002425 profilingStateMachine.TransitionToState(ProfilingState::Active);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002426 BOOST_CHECK_THROW(commandHandler(wrongPacket), armnn::InvalidArgumentException); // Wrong packet
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002427
2428 const uint32_t rightHeader = (packetId & 0x000003FF) << 16;
2429
2430 Packet rightPacket(rightHeader);
2431
Matteo Martincigh9723d022019-11-13 10:56:41 +00002432 BOOST_CHECK_NO_THROW(commandHandler(rightPacket)); // Right packet
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002433
Matteo Martincigh9723d022019-11-13 10:56:41 +00002434 auto readBuffer1 = mockBuffer1.GetReadableBuffer();
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002435
Matteo Martincigh9723d022019-11-13 10:56:41 +00002436 uint32_t header1Word0 = ReadUint32(readBuffer1, 0);
2437 uint32_t header1Word1 = ReadUint32(readBuffer1, 4);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002438
Matteo Martincigh9723d022019-11-13 10:56:41 +00002439 // Counter directory packet
2440 BOOST_TEST(((header1Word0 >> 26) & 0x0000003F) == 0); // packet family
2441 BOOST_TEST(((header1Word0 >> 16) & 0x000003FF) == 2); // packet id
2442 BOOST_TEST(header1Word1 == 24); // data length
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002443
Matteo Martincigh9723d022019-11-13 10:56:41 +00002444 uint32_t bodyHeader1Word0 = ReadUint32(readBuffer1, 8);
2445 uint16_t deviceRecordCount = numeric_cast<uint16_t>(bodyHeader1Word0 >> 16);
2446 BOOST_TEST(deviceRecordCount == 0); // device_records_count
2447
2448 auto readBuffer2 = mockBuffer2.GetReadableBuffer();
2449
2450 uint32_t header2Word0 = ReadUint32(readBuffer2, 0);
2451 uint32_t header2Word1 = ReadUint32(readBuffer2, 4);
2452
2453 // Timeline message directory packet
2454 BOOST_TEST(((header2Word0 >> 26) & 0x0000003F) == 1); // packet family
2455 BOOST_TEST(((header2Word0 >> 16) & 0x000003FF) == 0); // packet id
Jim Flynn6398a982020-05-27 17:05:21 +01002456 BOOST_TEST(header2Word1 == 443); // data length
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002457}
2458
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002459BOOST_AUTO_TEST_CASE(RequestCounterDirectoryCommandHandlerTest2)
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002460{
2461 using boost::numeric_cast;
2462
Jim Flynn397043f2019-10-17 17:37:10 +01002463 const uint32_t familyId = 0;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002464 const uint32_t packetId = 3;
Keith Davis3201eea2019-10-24 17:30:41 +01002465 const uint32_t version = 1;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002466 ProfilingStateMachine profilingStateMachine;
2467 CounterDirectory counterDirectory;
Matteo Martincigh9723d022019-11-13 10:56:41 +00002468 MockBufferManager mockBuffer1(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002469 SendCounterPacket sendCounterPacket(mockBuffer1);
2470 SendThread sendThread(profilingStateMachine, mockBuffer1, sendCounterPacket);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002471 MockBufferManager mockBuffer2(1024);
2472 SendTimelinePacket sendTimelinePacket(mockBuffer2);
Keith Davis3201eea2019-10-24 17:30:41 +01002473 RequestCounterDirectoryCommandHandler commandHandler(familyId, packetId, version, counterDirectory,
Matteo Martincigh9723d022019-11-13 10:56:41 +00002474 sendCounterPacket, sendTimelinePacket, profilingStateMachine);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002475 const uint32_t header = (packetId & 0x000003FF) << 16;
Finn Williams985fecf2020-04-30 11:06:43 +01002476 const Packet packet(header);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002477
Matteo Martincigh9723d022019-11-13 10:56:41 +00002478 const Device* device = counterDirectory.RegisterDevice("deviceA", 1);
2479 BOOST_CHECK(device != nullptr);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002480 const CounterSet* counterSet = counterDirectory.RegisterCounterSet("countersetA");
Matteo Martincigh9723d022019-11-13 10:56:41 +00002481 BOOST_CHECK(counterSet != nullptr);
Sadik Armagan4c998992020-02-25 12:44:44 +00002482 counterDirectory.RegisterCategory("categoryA");
Keith Davise394bd92019-12-02 15:12:19 +00002483 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID, 24,
2484 "categoryA", 0, 1, 2.0f, "counterA", "descA");
2485 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID, 25,
2486 "categoryA", 1, 1, 3.0f, "counterB", "descB");
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002487
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002488 profilingStateMachine.TransitionToState(ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002489 BOOST_CHECK_THROW(commandHandler(packet), armnn::RuntimeException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002490 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002491 BOOST_CHECK_THROW(commandHandler(packet), armnn::RuntimeException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002492 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002493 BOOST_CHECK_THROW(commandHandler(packet), armnn::RuntimeException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002494 profilingStateMachine.TransitionToState(ProfilingState::Active);
2495 BOOST_CHECK_NO_THROW(commandHandler(packet));
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002496
Matteo Martincigh9723d022019-11-13 10:56:41 +00002497 auto readBuffer1 = mockBuffer1.GetReadableBuffer();
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002498
Finn Williams985fecf2020-04-30 11:06:43 +01002499 const uint32_t header1Word0 = ReadUint32(readBuffer1, 0);
2500 const uint32_t header1Word1 = ReadUint32(readBuffer1, 4);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002501
Matteo Martincigh9723d022019-11-13 10:56:41 +00002502 BOOST_TEST(((header1Word0 >> 26) & 0x0000003F) == 0); // packet family
2503 BOOST_TEST(((header1Word0 >> 16) & 0x000003FF) == 2); // packet id
Sadik Armagan4c998992020-02-25 12:44:44 +00002504 BOOST_TEST(header1Word1 == 236); // data length
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002505
Finn Williams985fecf2020-04-30 11:06:43 +01002506 const uint32_t bodyHeaderSizeBytes = bodyHeaderSize * sizeof(uint32_t);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002507
Finn Williams985fecf2020-04-30 11:06:43 +01002508 const uint32_t bodyHeader1Word0 = ReadUint32(readBuffer1, 8);
2509 const uint32_t bodyHeader1Word1 = ReadUint32(readBuffer1, 12);
2510 const uint32_t bodyHeader1Word2 = ReadUint32(readBuffer1, 16);
2511 const uint32_t bodyHeader1Word3 = ReadUint32(readBuffer1, 20);
2512 const uint32_t bodyHeader1Word4 = ReadUint32(readBuffer1, 24);
2513 const uint32_t bodyHeader1Word5 = ReadUint32(readBuffer1, 28);
2514 const uint16_t deviceRecordCount = numeric_cast<uint16_t>(bodyHeader1Word0 >> 16);
2515 const uint16_t counterSetRecordCount = numeric_cast<uint16_t>(bodyHeader1Word2 >> 16);
2516 const uint16_t categoryRecordCount = numeric_cast<uint16_t>(bodyHeader1Word4 >> 16);
2517 BOOST_TEST(deviceRecordCount == 1); // device_records_count
2518 BOOST_TEST(bodyHeader1Word1 == 0 + bodyHeaderSizeBytes); // device_records_pointer_table_offset
2519 BOOST_TEST(counterSetRecordCount == 1); // counter_set_count
2520 BOOST_TEST(bodyHeader1Word3 == 4 + bodyHeaderSizeBytes); // counter_set_pointer_table_offset
2521 BOOST_TEST(categoryRecordCount == 1); // categories_count
2522 BOOST_TEST(bodyHeader1Word5 == 8 + bodyHeaderSizeBytes); // categories_pointer_table_offset
2523
2524 const uint32_t deviceRecordOffset = ReadUint32(readBuffer1, 32);
Finn Williamsd44815f2020-05-01 13:25:55 +01002525 BOOST_TEST(deviceRecordOffset == 12);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002526
Finn Williams985fecf2020-04-30 11:06:43 +01002527 const uint32_t counterSetRecordOffset = ReadUint32(readBuffer1, 36);
Finn Williamsd44815f2020-05-01 13:25:55 +01002528 BOOST_TEST(counterSetRecordOffset == 28);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002529
Finn Williams985fecf2020-04-30 11:06:43 +01002530 const uint32_t categoryRecordOffset = ReadUint32(readBuffer1, 40);
Finn Williamsd44815f2020-05-01 13:25:55 +01002531 BOOST_TEST(categoryRecordOffset == 48);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002532
2533 auto readBuffer2 = mockBuffer2.GetReadableBuffer();
2534
Finn Williams985fecf2020-04-30 11:06:43 +01002535 const uint32_t header2Word0 = ReadUint32(readBuffer2, 0);
2536 const uint32_t header2Word1 = ReadUint32(readBuffer2, 4);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002537
2538 // Timeline message directory packet
2539 BOOST_TEST(((header2Word0 >> 26) & 0x0000003F) == 1); // packet family
2540 BOOST_TEST(((header2Word0 >> 16) & 0x000003FF) == 0); // packet id
Jim Flynn6398a982020-05-27 17:05:21 +01002541 BOOST_TEST(header2Word1 == 443); // data length
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002542}
2543
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002544BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodConnectionAcknowledgedPacket)
2545{
Finn Williamsa0de0562020-04-22 12:27:37 +01002546 unsigned int streamMetadataPacketsize = GetStreamMetaDataPacketSize();
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002547
Jim Flynn53e46992019-10-14 12:31:10 +01002548 // Reset the profiling service to the uninitialized state
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002549 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002550 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +00002551 armnn::profiling::ProfilingService profilingService;
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002552 profilingService.ResetExternalProfilingOptions(options, true);
2553
Sadik Armagan3184c902020-03-18 10:57:30 +00002554 // Swap the profiling connection factory in the profiling service instance with our mock one
2555 SwapProfilingConnectionFactoryHelper helper(profilingService);
2556
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002557 // Bring the profiling service to the "WaitingForAck" state
2558 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002559 profilingService.Update(); // Initialize the counter directory
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002560 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002561 profilingService.Update(); // Create the profiling connection
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002562
Matteo Martincighd0613b52019-10-09 16:47:04 +01002563 // Get the mock profiling connection
2564 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2565 BOOST_CHECK(mockProfilingConnection);
2566
Matteo Martincighe8485382019-10-10 14:08:21 +01002567 // Remove the packets received so far
2568 mockProfilingConnection->Clear();
2569
2570 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002571 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002572
2573 // Wait for the Stream Metadata packet to be sent
Finn Williams09ad6f92019-12-19 17:05:18 +00002574 BOOST_CHECK(helper.WaitForPacketsSent(
2575 mockProfilingConnection, PacketType::StreamMetaData, streamMetadataPacketsize) >= 1);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002576
2577 // Write a valid "Connection Acknowledged" packet into the mock profiling connection, to simulate a valid
2578 // reply from an external profiling service
2579
2580 // Connection Acknowledged Packet header (word 0, word 1 is always zero):
2581 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2582 // 16:25 [10] packet_id: Packet identifier, value 0b0000000001
2583 // 8:15 [8] reserved: Reserved, value 0b00000000
2584 // 0:7 [8] reserved: Reserved, value 0b00000000
2585 uint32_t packetFamily = 0;
2586 uint32_t packetId = 1;
Keith Davis3201eea2019-10-24 17:30:41 +01002587 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002588
Matteo Martincighd0613b52019-10-09 16:47:04 +01002589 // Create the Connection Acknowledged Packet
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002590 Packet connectionAcknowledgedPacket(header);
2591
2592 // Write the packet to the mock profiling connection
2593 mockProfilingConnection->WritePacket(std::move(connectionAcknowledgedPacket));
2594
Colm Donelan2ba48d22019-11-29 09:10:59 +00002595 // Wait for the counter directory packet to ensure the ConnectionAcknowledgedCommandHandler has run.
Finn Williams09ad6f92019-12-19 17:05:18 +00002596 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::CounterDirectory) == 1);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002597
2598 // The Connection Acknowledged Command Handler should have updated the profiling state accordingly
2599 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincighd0613b52019-10-09 16:47:04 +01002600
2601 // Reset the profiling service to stop any running thread
2602 options.m_EnableProfiling = false;
2603 profilingService.ResetExternalProfilingOptions(options, true);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002604}
2605
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002606BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodRequestCounterDirectoryPacket)
2607{
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002608 // Reset the profiling service to the uninitialized state
2609 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002610 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +00002611 armnn::profiling::ProfilingService profilingService;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002612 profilingService.ResetExternalProfilingOptions(options, true);
2613
Sadik Armagan3184c902020-03-18 10:57:30 +00002614 // Swap the profiling connection factory in the profiling service instance with our mock one
2615 SwapProfilingConnectionFactoryHelper helper(profilingService);
2616
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002617 // Bring the profiling service to the "Active" state
2618 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002619 profilingService.Update(); // Initialize the counter directory
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002620 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002621 profilingService.Update(); // Create the profiling connection
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002622 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002623 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002624
Colm Donelan2ba48d22019-11-29 09:10:59 +00002625 // Get the mock profiling connection
2626 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2627 BOOST_CHECK(mockProfilingConnection);
2628
Matteo Martincighe8485382019-10-10 14:08:21 +01002629 // Force the profiling service to the "Active" state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002630 helper.ForceTransitionToState(ProfilingState::Active);
2631 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2632
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002633 // Write a valid "Request Counter Directory" packet into the mock profiling connection, to simulate a valid
2634 // reply from an external profiling service
2635
2636 // Request Counter Directory packet header (word 0, word 1 is always zero):
2637 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2638 // 16:25 [10] packet_id: Packet identifier, value 0b0000000011
2639 // 8:15 [8] reserved: Reserved, value 0b00000000
2640 // 0:7 [8] reserved: Reserved, value 0b00000000
2641 uint32_t packetFamily = 0;
2642 uint32_t packetId = 3;
Keith Davis3201eea2019-10-24 17:30:41 +01002643 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002644
2645 // Create the Request Counter Directory packet
2646 Packet requestCounterDirectoryPacket(header);
2647
2648 // Write the packet to the mock profiling connection
2649 mockProfilingConnection->WritePacket(std::move(requestCounterDirectoryPacket));
2650
Sadik Armagan4c998992020-02-25 12:44:44 +00002651 // Expecting one CounterDirectory Packet of length 652
Jim Flynn6398a982020-05-27 17:05:21 +01002652 // and one TimelineMessageDirectory packet of length 451
Sadik Armagan4c998992020-02-25 12:44:44 +00002653 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::CounterDirectory, 652) == 1);
Jim Flynn6398a982020-05-27 17:05:21 +01002654 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::TimelineMessageDirectory, 451) == 1);
Matteo Martincighe8485382019-10-10 14:08:21 +01002655
2656 // The Request Counter Directory Command Handler should not have updated the profiling state
2657 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2658
2659 // Reset the profiling service to stop any running thread
2660 options.m_EnableProfiling = false;
2661 profilingService.ResetExternalProfilingOptions(options, true);
2662}
2663
Matteo Martincighe8485382019-10-10 14:08:21 +01002664BOOST_AUTO_TEST_CASE(CheckProfilingServiceBadPeriodicCounterSelectionPacketInvalidCounterUid)
2665{
Matteo Martincighe8485382019-10-10 14:08:21 +01002666 // Reset the profiling service to the uninitialized state
2667 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002668 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +00002669 armnn::profiling::ProfilingService profilingService;
Matteo Martincighe8485382019-10-10 14:08:21 +01002670 profilingService.ResetExternalProfilingOptions(options, true);
2671
Sadik Armagan3184c902020-03-18 10:57:30 +00002672 // Swap the profiling connection factory in the profiling service instance with our mock one
2673 SwapProfilingConnectionFactoryHelper helper(profilingService);
2674
Matteo Martincighe8485382019-10-10 14:08:21 +01002675 // Bring the profiling service to the "Active" state
2676 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002677 profilingService.Update(); // Initialize the counter directory
Matteo Martincighe8485382019-10-10 14:08:21 +01002678 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002679 profilingService.Update(); // Create the profiling connection
Matteo Martincighe8485382019-10-10 14:08:21 +01002680 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002681 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002682
Colm Donelan2ba48d22019-11-29 09:10:59 +00002683 // Get the mock profiling connection
2684 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2685 BOOST_CHECK(mockProfilingConnection);
2686
Matteo Martincighe8485382019-10-10 14:08:21 +01002687 // Force the profiling service to the "Active" state
2688 helper.ForceTransitionToState(ProfilingState::Active);
2689 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2690
Matteo Martincighe8485382019-10-10 14:08:21 +01002691 // Remove the packets received so far
2692 mockProfilingConnection->Clear();
2693
2694 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2695 // external profiling service
2696
2697 // Periodic Counter Selection packet header:
2698 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2699 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2700 // 8:15 [8] reserved: Reserved, value 0b00000000
2701 // 0:7 [8] reserved: Reserved, value 0b00000000
2702 uint32_t packetFamily = 0;
2703 uint32_t packetId = 4;
Keith Davis3201eea2019-10-24 17:30:41 +01002704 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincighe8485382019-10-10 14:08:21 +01002705
Keith Davis3201eea2019-10-24 17:30:41 +01002706 uint32_t capturePeriod = 123456; // Some capture period (microseconds)
Matteo Martincighe8485382019-10-10 14:08:21 +01002707
2708 // Get the first valid counter UID
2709 const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
Keith Davis3201eea2019-10-24 17:30:41 +01002710 const Counters& counters = counterDirectory.GetCounters();
Matteo Martincighe8485382019-10-10 14:08:21 +01002711 BOOST_CHECK(counters.size() > 1);
Keith Davis3201eea2019-10-24 17:30:41 +01002712 uint16_t counterUidA = counters.begin()->first; // First valid counter UID
2713 uint16_t counterUidB = 9999; // Second invalid counter UID
Matteo Martincighe8485382019-10-10 14:08:21 +01002714
2715 uint32_t length = 8;
2716
2717 auto data = std::make_unique<unsigned char[]>(length);
2718 WriteUint32(data.get(), 0, capturePeriod);
2719 WriteUint16(data.get(), 4, counterUidA);
2720 WriteUint16(data.get(), 6, counterUidB);
2721
2722 // Create the Periodic Counter Selection packet
Keith Davis3201eea2019-10-24 17:30:41 +01002723 Packet periodicCounterSelectionPacket(header, length, data); // Length > 0, this will start the Period Counter
2724 // Capture thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002725
2726 // Write the packet to the mock profiling connection
2727 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2728
Finn Williams09ad6f92019-12-19 17:05:18 +00002729 // Expecting one Periodic Counter Selection packet of length 14
2730 // and at least one Periodic Counter Capture packet of length 22
2731 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterSelection, 14) == 1);
2732 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterCapture, 22) >= 1);
Matteo Martincighe8485382019-10-10 14:08:21 +01002733
2734 // The Periodic Counter Selection Handler should not have updated the profiling state
2735 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2736
2737 // Reset the profiling service to stop any running thread
2738 options.m_EnableProfiling = false;
2739 profilingService.ResetExternalProfilingOptions(options, true);
2740}
2741
2742BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodPeriodicCounterSelectionPacketNoCounters)
2743{
Matteo Martincighe8485382019-10-10 14:08:21 +01002744 // Reset the profiling service to the uninitialized state
2745 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002746 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +00002747 armnn::profiling::ProfilingService profilingService;
Matteo Martincighe8485382019-10-10 14:08:21 +01002748 profilingService.ResetExternalProfilingOptions(options, true);
2749
Sadik Armagan3184c902020-03-18 10:57:30 +00002750 // Swap the profiling connection factory in the profiling service instance with our mock one
2751 SwapProfilingConnectionFactoryHelper helper(profilingService);
2752
Matteo Martincighe8485382019-10-10 14:08:21 +01002753 // Bring the profiling service to the "Active" state
2754 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002755 profilingService.Update(); // Initialize the counter directory
Matteo Martincighe8485382019-10-10 14:08:21 +01002756 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002757 profilingService.Update(); // Create the profiling connection
Matteo Martincighe8485382019-10-10 14:08:21 +01002758 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002759 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002760
Colm Donelan2ba48d22019-11-29 09:10:59 +00002761 // Get the mock profiling connection
2762 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2763 BOOST_CHECK(mockProfilingConnection);
2764
Matteo Martincighe8485382019-10-10 14:08:21 +01002765 // Wait for the Stream Metadata packet the be sent
2766 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Finn Williams09ad6f92019-12-19 17:05:18 +00002767 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData);
Matteo Martincighe8485382019-10-10 14:08:21 +01002768
2769 // Force the profiling service to the "Active" state
2770 helper.ForceTransitionToState(ProfilingState::Active);
2771 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2772
Matteo Martincighe8485382019-10-10 14:08:21 +01002773 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2774 // external profiling service
2775
2776 // Periodic Counter Selection packet header:
2777 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2778 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2779 // 8:15 [8] reserved: Reserved, value 0b00000000
2780 // 0:7 [8] reserved: Reserved, value 0b00000000
2781 uint32_t packetFamily = 0;
2782 uint32_t packetId = 4;
Keith Davis3201eea2019-10-24 17:30:41 +01002783 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincighe8485382019-10-10 14:08:21 +01002784
2785 // Create the Periodic Counter Selection packet
Keith Davis3201eea2019-10-24 17:30:41 +01002786 Packet periodicCounterSelectionPacket(header); // Length == 0, this will disable the collection of counters
Matteo Martincighe8485382019-10-10 14:08:21 +01002787
2788 // Write the packet to the mock profiling connection
2789 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2790
Finn Williams09ad6f92019-12-19 17:05:18 +00002791 // Wait for the Periodic Counter Selection packet of length 12 to be sent
2792 // The size of the expected Periodic Counter Selection (echos the sent one)
2793 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterSelection, 12) == 1);
Matteo Martincighe8485382019-10-10 14:08:21 +01002794
2795 // The Periodic Counter Selection Handler should not have updated the profiling state
2796 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2797
Finn Williams09ad6f92019-12-19 17:05:18 +00002798 // No Periodic Counter packets are expected
2799 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterCapture, 0, 0) == 0);
Matteo Martincighe8485382019-10-10 14:08:21 +01002800
2801 // Reset the profiling service to stop any running thread
2802 options.m_EnableProfiling = false;
2803 profilingService.ResetExternalProfilingOptions(options, true);
2804}
2805
2806BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodPeriodicCounterSelectionPacketSingleCounter)
2807{
Matteo Martincighe8485382019-10-10 14:08:21 +01002808 // Reset the profiling service to the uninitialized state
2809 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002810 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +00002811 armnn::profiling::ProfilingService profilingService;
Matteo Martincighe8485382019-10-10 14:08:21 +01002812 profilingService.ResetExternalProfilingOptions(options, true);
2813
Sadik Armagan3184c902020-03-18 10:57:30 +00002814 // Swap the profiling connection factory in the profiling service instance with our mock one
2815 SwapProfilingConnectionFactoryHelper helper(profilingService);
2816
Matteo Martincighe8485382019-10-10 14:08:21 +01002817 // Bring the profiling service to the "Active" state
2818 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002819 profilingService.Update(); // Initialize the counter directory
Matteo Martincighe8485382019-10-10 14:08:21 +01002820 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002821 profilingService.Update(); // Create the profiling connection
Matteo Martincighe8485382019-10-10 14:08:21 +01002822 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002823 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002824
Colm Donelan2ba48d22019-11-29 09:10:59 +00002825 // Get the mock profiling connection
2826 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2827 BOOST_CHECK(mockProfilingConnection);
2828
Finn Williams09ad6f92019-12-19 17:05:18 +00002829 // Wait for the Stream Metadata packet to be sent
Matteo Martincighe8485382019-10-10 14:08:21 +01002830 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Finn Williams09ad6f92019-12-19 17:05:18 +00002831 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData);
Matteo Martincighe8485382019-10-10 14:08:21 +01002832
2833 // Force the profiling service to the "Active" state
2834 helper.ForceTransitionToState(ProfilingState::Active);
2835 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2836
Matteo Martincighe8485382019-10-10 14:08:21 +01002837 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2838 // external profiling service
2839
2840 // Periodic Counter Selection packet header:
2841 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2842 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2843 // 8:15 [8] reserved: Reserved, value 0b00000000
2844 // 0:7 [8] reserved: Reserved, value 0b00000000
2845 uint32_t packetFamily = 0;
2846 uint32_t packetId = 4;
Keith Davis3201eea2019-10-24 17:30:41 +01002847 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincighe8485382019-10-10 14:08:21 +01002848
Keith Davis3201eea2019-10-24 17:30:41 +01002849 uint32_t capturePeriod = 123456; // Some capture period (microseconds)
Matteo Martincighe8485382019-10-10 14:08:21 +01002850
2851 // Get the first valid counter UID
2852 const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
Keith Davis3201eea2019-10-24 17:30:41 +01002853 const Counters& counters = counterDirectory.GetCounters();
Matteo Martincighe8485382019-10-10 14:08:21 +01002854 BOOST_CHECK(!counters.empty());
Keith Davis3201eea2019-10-24 17:30:41 +01002855 uint16_t counterUid = counters.begin()->first; // Valid counter UID
Matteo Martincighe8485382019-10-10 14:08:21 +01002856
2857 uint32_t length = 6;
2858
2859 auto data = std::make_unique<unsigned char[]>(length);
2860 WriteUint32(data.get(), 0, capturePeriod);
2861 WriteUint16(data.get(), 4, counterUid);
2862
2863 // Create the Periodic Counter Selection packet
Keith Davis3201eea2019-10-24 17:30:41 +01002864 Packet periodicCounterSelectionPacket(header, length, data); // Length > 0, this will start the Period Counter
2865 // Capture thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002866
2867 // Write the packet to the mock profiling connection
2868 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2869
Finn Williams09ad6f92019-12-19 17:05:18 +00002870 // Expecting one Periodic Counter Selection packet of length 14
2871 // and at least one Periodic Counter Capture packet of length 22
2872 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterSelection, 14) == 1);
2873 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterCapture, 22) >= 1);
Matteo Martincighe8485382019-10-10 14:08:21 +01002874
2875 // The Periodic Counter Selection Handler should not have updated the profiling state
2876 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2877
2878 // Reset the profiling service to stop any running thread
2879 options.m_EnableProfiling = false;
2880 profilingService.ResetExternalProfilingOptions(options, true);
2881}
2882
2883BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodPeriodicCounterSelectionPacketMultipleCounters)
2884{
Matteo Martincighe8485382019-10-10 14:08:21 +01002885 // Reset the profiling service to the uninitialized state
2886 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002887 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +00002888 armnn::profiling::ProfilingService profilingService;
Matteo Martincighe8485382019-10-10 14:08:21 +01002889 profilingService.ResetExternalProfilingOptions(options, true);
2890
Sadik Armagan3184c902020-03-18 10:57:30 +00002891 // Swap the profiling connection factory in the profiling service instance with our mock one
2892 SwapProfilingConnectionFactoryHelper helper(profilingService);
2893
Matteo Martincighe8485382019-10-10 14:08:21 +01002894 // Bring the profiling service to the "Active" state
2895 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002896 profilingService.Update(); // Initialize the counter directory
Matteo Martincighe8485382019-10-10 14:08:21 +01002897 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002898 profilingService.Update(); // Create the profiling connection
Matteo Martincighe8485382019-10-10 14:08:21 +01002899 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002900 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002901
Colm Donelan2ba48d22019-11-29 09:10:59 +00002902 // Get the mock profiling connection
2903 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2904 BOOST_CHECK(mockProfilingConnection);
2905
Matteo Martincighe8485382019-10-10 14:08:21 +01002906 // Wait for the Stream Metadata packet the be sent
2907 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Finn Williams09ad6f92019-12-19 17:05:18 +00002908 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData);
Matteo Martincighe8485382019-10-10 14:08:21 +01002909
2910 // Force the profiling service to the "Active" state
2911 helper.ForceTransitionToState(ProfilingState::Active);
2912 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2913
Matteo Martincighe8485382019-10-10 14:08:21 +01002914 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2915 // external profiling service
2916
2917 // Periodic Counter Selection packet header:
2918 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2919 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2920 // 8:15 [8] reserved: Reserved, value 0b00000000
2921 // 0:7 [8] reserved: Reserved, value 0b00000000
2922 uint32_t packetFamily = 0;
2923 uint32_t packetId = 4;
Keith Davis3201eea2019-10-24 17:30:41 +01002924 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincighe8485382019-10-10 14:08:21 +01002925
Keith Davis3201eea2019-10-24 17:30:41 +01002926 uint32_t capturePeriod = 123456; // Some capture period (microseconds)
Matteo Martincighe8485382019-10-10 14:08:21 +01002927
2928 // Get the first valid counter UID
2929 const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
Keith Davis3201eea2019-10-24 17:30:41 +01002930 const Counters& counters = counterDirectory.GetCounters();
Matteo Martincighe8485382019-10-10 14:08:21 +01002931 BOOST_CHECK(counters.size() > 1);
Keith Davis3201eea2019-10-24 17:30:41 +01002932 uint16_t counterUidA = counters.begin()->first; // First valid counter UID
2933 uint16_t counterUidB = (counters.begin()++)->first; // Second valid counter UID
Matteo Martincighe8485382019-10-10 14:08:21 +01002934
2935 uint32_t length = 8;
2936
2937 auto data = std::make_unique<unsigned char[]>(length);
2938 WriteUint32(data.get(), 0, capturePeriod);
2939 WriteUint16(data.get(), 4, counterUidA);
2940 WriteUint16(data.get(), 6, counterUidB);
2941
2942 // Create the Periodic Counter Selection packet
Keith Davis3201eea2019-10-24 17:30:41 +01002943 Packet periodicCounterSelectionPacket(header, length, data); // Length > 0, this will start the Period Counter
2944 // Capture thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002945
2946 // Write the packet to the mock profiling connection
2947 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2948
Finn Williams09ad6f92019-12-19 17:05:18 +00002949 // Expecting one PeriodicCounterSelection Packet with a length of 16
2950 // And at least one PeriodicCounterCapture Packet with a length of 28
2951 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterSelection, 16) == 1);
2952 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterCapture, 28) >= 1);
Matteo Martincighe8485382019-10-10 14:08:21 +01002953
2954 // The Periodic Counter Selection Handler should not have updated the profiling state
2955 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002956
2957 // Reset the profiling service to stop any running thread
2958 options.m_EnableProfiling = false;
2959 profilingService.ResetExternalProfilingOptions(options, true);
2960}
2961
Jim Flynn53e46992019-10-14 12:31:10 +01002962BOOST_AUTO_TEST_CASE(CheckProfilingServiceDisconnect)
2963{
Jim Flynn53e46992019-10-14 12:31:10 +01002964 // Reset the profiling service to the uninitialized state
2965 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002966 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +00002967 armnn::profiling::ProfilingService profilingService;
Jim Flynn53e46992019-10-14 12:31:10 +01002968 profilingService.ResetExternalProfilingOptions(options, true);
2969
Sadik Armagan3184c902020-03-18 10:57:30 +00002970 // Swap the profiling connection factory in the profiling service instance with our mock one
2971 SwapProfilingConnectionFactoryHelper helper(profilingService);
2972
Jim Flynn53e46992019-10-14 12:31:10 +01002973 // Try to disconnect the profiling service while in the "Uninitialised" state
2974 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
2975 profilingService.Disconnect();
Keith Davis3201eea2019-10-24 17:30:41 +01002976 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised); // The state should not change
Jim Flynn53e46992019-10-14 12:31:10 +01002977
2978 // Try to disconnect the profiling service while in the "NotConnected" state
Keith Davis3201eea2019-10-24 17:30:41 +01002979 profilingService.Update(); // Initialize the counter directory
Jim Flynn53e46992019-10-14 12:31:10 +01002980 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
2981 profilingService.Disconnect();
Keith Davis3201eea2019-10-24 17:30:41 +01002982 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected); // The state should not change
Jim Flynn53e46992019-10-14 12:31:10 +01002983
2984 // Try to disconnect the profiling service while in the "WaitingForAck" state
Keith Davis3201eea2019-10-24 17:30:41 +01002985 profilingService.Update(); // Create the profiling connection
Jim Flynn53e46992019-10-14 12:31:10 +01002986 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
2987 profilingService.Disconnect();
Keith Davis3201eea2019-10-24 17:30:41 +01002988 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck); // The state should not change
Jim Flynn53e46992019-10-14 12:31:10 +01002989
2990 // Try to disconnect the profiling service while in the "Active" state
Keith Davis3201eea2019-10-24 17:30:41 +01002991 profilingService.Update(); // Start the command handler and the send thread
Jim Flynn53e46992019-10-14 12:31:10 +01002992
Colm Donelan2ba48d22019-11-29 09:10:59 +00002993 // Get the mock profiling connection
2994 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2995 BOOST_CHECK(mockProfilingConnection);
2996
Jim Flynn53e46992019-10-14 12:31:10 +01002997 // Wait for the Stream Metadata packet the be sent
2998 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Finn Williams09ad6f92019-12-19 17:05:18 +00002999 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData);
Jim Flynn53e46992019-10-14 12:31:10 +01003000
3001 // Force the profiling service to the "Active" state
3002 helper.ForceTransitionToState(ProfilingState::Active);
3003 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
3004
Jim Flynn53e46992019-10-14 12:31:10 +01003005 // Check that the profiling connection is open
3006 BOOST_CHECK(mockProfilingConnection->IsOpen());
3007
3008 profilingService.Disconnect();
Colm Donelan2ba48d22019-11-29 09:10:59 +00003009 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected); // The state should have changed
Jim Flynn53e46992019-10-14 12:31:10 +01003010
3011 // Check that the profiling connection has been reset
3012 mockProfilingConnection = helper.GetMockProfilingConnection();
3013 BOOST_CHECK(mockProfilingConnection == nullptr);
3014
3015 // Reset the profiling service to stop any running thread
3016 options.m_EnableProfiling = false;
3017 profilingService.ResetExternalProfilingOptions(options, true);
3018}
3019
Matteo Martincigh994b5342019-10-11 17:19:56 +01003020BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodPerJobCounterSelectionPacket)
3021{
Matteo Martincigh994b5342019-10-11 17:19:56 +01003022 // Reset the profiling service to the uninitialized state
3023 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01003024 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +00003025 armnn::profiling::ProfilingService profilingService;
Matteo Martincigh994b5342019-10-11 17:19:56 +01003026 profilingService.ResetExternalProfilingOptions(options, true);
3027
Sadik Armagan3184c902020-03-18 10:57:30 +00003028 // Swap the profiling connection factory in the profiling service instance with our mock one
3029 SwapProfilingConnectionFactoryHelper helper(profilingService);
3030
Matteo Martincigh994b5342019-10-11 17:19:56 +01003031 // Bring the profiling service to the "Active" state
3032 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01003033 profilingService.Update(); // Initialize the counter directory
Matteo Martincigh994b5342019-10-11 17:19:56 +01003034 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01003035 profilingService.Update(); // Create the profiling connection
Matteo Martincigh994b5342019-10-11 17:19:56 +01003036 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01003037 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincigh994b5342019-10-11 17:19:56 +01003038
Colm Donelan2ba48d22019-11-29 09:10:59 +00003039 // Get the mock profiling connection
3040 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
3041 BOOST_CHECK(mockProfilingConnection);
3042
Matteo Martincigh994b5342019-10-11 17:19:56 +01003043 // Wait for the Stream Metadata packet the be sent
3044 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Finn Williams09ad6f92019-12-19 17:05:18 +00003045 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData);
Matteo Martincigh994b5342019-10-11 17:19:56 +01003046
3047 // Force the profiling service to the "Active" state
3048 helper.ForceTransitionToState(ProfilingState::Active);
3049 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
3050
Matteo Martincigh994b5342019-10-11 17:19:56 +01003051 // Write a "Per-Job Counter Selection" packet into the mock profiling connection, to simulate an input from an
3052 // external profiling service
3053
3054 // Per-Job Counter Selection packet header:
3055 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
3056 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
3057 // 8:15 [8] reserved: Reserved, value 0b00000000
3058 // 0:7 [8] reserved: Reserved, value 0b00000000
3059 uint32_t packetFamily = 0;
3060 uint32_t packetId = 5;
Keith Davis3201eea2019-10-24 17:30:41 +01003061 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincigh994b5342019-10-11 17:19:56 +01003062
3063 // Create the Per-Job Counter Selection packet
Keith Davis3201eea2019-10-24 17:30:41 +01003064 Packet periodicCounterSelectionPacket(header); // Length == 0, this will disable the collection of counters
Matteo Martincigh994b5342019-10-11 17:19:56 +01003065
3066 // Write the packet to the mock profiling connection
3067 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
3068
3069 // Wait for a bit (must at least be the delay value of the mock profiling connection) to make sure that
3070 // the Per-Job Counter Selection packet gets processed by the profiling service
Colm Donelan2ba48d22019-11-29 09:10:59 +00003071 std::this_thread::sleep_for(std::chrono::milliseconds(5));
Matteo Martincigh994b5342019-10-11 17:19:56 +01003072
Matteo Martincigh994b5342019-10-11 17:19:56 +01003073 // The Per-Job Counter Selection Command Handler should not have updated the profiling state
3074 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
3075
Finn Williams09ad6f92019-12-19 17:05:18 +00003076 // The Per-Job Counter Selection packets are dropped silently, so there should be no reply coming
3077 // from the profiling service
3078 const auto StreamMetaDataSize = static_cast<unsigned long>(
3079 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData, 0, 0));
3080 BOOST_CHECK(StreamMetaDataSize == mockProfilingConnection->GetWrittenDataSize());
3081
Matteo Martincigh994b5342019-10-11 17:19:56 +01003082 // Reset the profiling service to stop any running thread
3083 options.m_EnableProfiling = false;
3084 profilingService.ResetExternalProfilingOptions(options, true);
3085}
3086
Jim Flynn672d06e2019-10-15 10:18:11 +01003087BOOST_AUTO_TEST_CASE(CheckConfigureProfilingServiceOn)
3088{
3089 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01003090 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +00003091 armnn::profiling::ProfilingService profilingService;
Jim Flynn672d06e2019-10-15 10:18:11 +01003092 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3093 profilingService.ConfigureProfilingService(options);
3094 // should get as far as NOT_CONNECTED
3095 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
3096 // Reset the profiling service to stop any running thread
3097 options.m_EnableProfiling = false;
3098 profilingService.ResetExternalProfilingOptions(options, true);
3099}
3100
3101BOOST_AUTO_TEST_CASE(CheckConfigureProfilingServiceOff)
3102{
3103 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Sadik Armagan3184c902020-03-18 10:57:30 +00003104 armnn::profiling::ProfilingService profilingService;
Jim Flynn672d06e2019-10-15 10:18:11 +01003105 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3106 profilingService.ConfigureProfilingService(options);
3107 // should not move from Uninitialised
3108 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3109 // Reset the profiling service to stop any running thread
3110 options.m_EnableProfiling = false;
3111 profilingService.ResetExternalProfilingOptions(options, true);
3112}
3113
Colm Donelan2ba48d22019-11-29 09:10:59 +00003114BOOST_AUTO_TEST_CASE(CheckProfilingServiceEnabled)
3115{
3116 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
3117 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
3118 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
3119 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +00003120 armnn::profiling::ProfilingService profilingService;
Colm Donelan2ba48d22019-11-29 09:10:59 +00003121 profilingService.ResetExternalProfilingOptions(options, true);
3122 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3123 profilingService.Update();
3124 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
3125
3126 // Redirect the output to a local stream so that we can parse the warning message
3127 std::stringstream ss;
3128 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3129 profilingService.Update();
Finn Williams09ad6f92019-12-19 17:05:18 +00003130
3131 // Reset the profiling service to stop any running thread
3132 options.m_EnableProfiling = false;
3133 profilingService.ResetExternalProfilingOptions(options, true);
3134
Colm Donelan2ba48d22019-11-29 09:10:59 +00003135 streamRedirector.CancelRedirect();
3136
3137 // Check that the expected error has occurred and logged to the standard output
David Monahana8837bf2020-04-16 10:01:56 +01003138 if (ss.str().find("Cannot connect to stream socket: Connection refused") == std::string::npos)
Colm Donelan2ba48d22019-11-29 09:10:59 +00003139 {
3140 std::cout << ss.str();
3141 BOOST_FAIL("Expected string not found.");
3142 }
Colm Donelan2ba48d22019-11-29 09:10:59 +00003143}
3144
3145BOOST_AUTO_TEST_CASE(CheckProfilingServiceEnabledRuntime)
3146{
3147 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
3148 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
3149 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Sadik Armagan3184c902020-03-18 10:57:30 +00003150 armnn::profiling::ProfilingService profilingService;
Colm Donelan2ba48d22019-11-29 09:10:59 +00003151 profilingService.ResetExternalProfilingOptions(options, true);
3152 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3153 profilingService.Update();
3154 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3155 options.m_EnableProfiling = true;
3156 profilingService.ResetExternalProfilingOptions(options);
3157 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3158 profilingService.Update();
3159 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
3160
3161 // Redirect the output to a local stream so that we can parse the warning message
3162 std::stringstream ss;
3163 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3164 profilingService.Update();
3165
Finn Williams09ad6f92019-12-19 17:05:18 +00003166 // Reset the profiling service to stop any running thread
3167 options.m_EnableProfiling = false;
3168 profilingService.ResetExternalProfilingOptions(options, true);
3169
Colm Donelan2ba48d22019-11-29 09:10:59 +00003170 streamRedirector.CancelRedirect();
3171
3172 // Check that the expected error has occurred and logged to the standard output
David Monahana8837bf2020-04-16 10:01:56 +01003173 if (ss.str().find("Cannot connect to stream socket: Connection refused") == std::string::npos)
Colm Donelan2ba48d22019-11-29 09:10:59 +00003174 {
3175 std::cout << ss.str();
3176 BOOST_FAIL("Expected string not found.");
3177 }
Colm Donelan2ba48d22019-11-29 09:10:59 +00003178}
3179
3180BOOST_AUTO_TEST_CASE(CheckProfilingServiceBadConnectionAcknowledgedPacket)
3181{
3182 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
3183 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
Sadik Armagan3184c902020-03-18 10:57:30 +00003184
Colm Donelan2ba48d22019-11-29 09:10:59 +00003185
3186 // Redirect the standard output to a local stream so that we can parse the warning message
3187 std::stringstream ss;
3188 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3189
Colm Donelan2ba48d22019-11-29 09:10:59 +00003190 // Reset the profiling service to the uninitialized state
3191 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
3192 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +00003193 armnn::profiling::ProfilingService profilingService;
Colm Donelan2ba48d22019-11-29 09:10:59 +00003194 profilingService.ResetExternalProfilingOptions(options, true);
3195
Sadik Armagan3184c902020-03-18 10:57:30 +00003196 // Swap the profiling connection factory in the profiling service instance with our mock one
3197 SwapProfilingConnectionFactoryHelper helper(profilingService);
3198
Colm Donelan2ba48d22019-11-29 09:10:59 +00003199 // Bring the profiling service to the "WaitingForAck" state
3200 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3201 profilingService.Update(); // Initialize the counter directory
3202 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
3203 profilingService.Update(); // Create the profiling connection
3204
3205 // Get the mock profiling connection
3206 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
3207 BOOST_CHECK(mockProfilingConnection);
3208
Colm Donelan2ba48d22019-11-29 09:10:59 +00003209 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003210
3211 // Connection Acknowledged Packet header (word 0, word 1 is always zero):
3212 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
3213 // 16:25 [10] packet_id: Packet identifier, value 0b0000000001
3214 // 8:15 [8] reserved: Reserved, value 0b00000000
3215 // 0:7 [8] reserved: Reserved, value 0b00000000
3216 uint32_t packetFamily = 0;
3217 uint32_t packetId = 37; // Wrong packet id!!!
3218 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
3219
3220 // Create the Connection Acknowledged Packet
3221 Packet connectionAcknowledgedPacket(header);
Finn Williams09ad6f92019-12-19 17:05:18 +00003222 // Write an invalid "Connection Acknowledged" packet into the mock profiling connection, to simulate an invalid
3223 // reply from an external profiling service
Colm Donelan2ba48d22019-11-29 09:10:59 +00003224 mockProfilingConnection->WritePacket(std::move(connectionAcknowledgedPacket));
3225
Finn Williams09ad6f92019-12-19 17:05:18 +00003226 // Start the command thread
3227 profilingService.Update();
3228
3229 // Wait for the command thread to join
3230 options.m_EnableProfiling = false;
3231 profilingService.ResetExternalProfilingOptions(options, true);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003232
3233 streamRedirector.CancelRedirect();
3234
3235 // Check that the expected error has occurred and logged to the standard output
David Monahana8837bf2020-04-16 10:01:56 +01003236 if (ss.str().find("Functor with requested PacketId=37 and Version=4194304 does not exist") == std::string::npos)
Colm Donelan2ba48d22019-11-29 09:10:59 +00003237 {
3238 std::cout << ss.str();
3239 BOOST_FAIL("Expected string not found.");
3240 }
Colm Donelan2ba48d22019-11-29 09:10:59 +00003241}
3242
3243BOOST_AUTO_TEST_CASE(CheckProfilingServiceBadRequestCounterDirectoryPacket)
3244{
3245 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
3246 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003247
3248 // Redirect the standard output to a local stream so that we can parse the warning message
3249 std::stringstream ss;
3250 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3251
3252 // Reset the profiling service to the uninitialized state
3253 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
3254 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +00003255 armnn::profiling::ProfilingService profilingService;
Colm Donelan2ba48d22019-11-29 09:10:59 +00003256 profilingService.ResetExternalProfilingOptions(options, true);
3257
Sadik Armagan3184c902020-03-18 10:57:30 +00003258 // Swap the profiling connection factory in the profiling service instance with our mock one
3259 SwapProfilingConnectionFactoryHelper helper(profilingService);
3260
Colm Donelan2ba48d22019-11-29 09:10:59 +00003261 // Bring the profiling service to the "Active" state
3262 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3263 helper.ForceTransitionToState(ProfilingState::NotConnected);
3264 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
3265 profilingService.Update(); // Create the profiling connection
3266 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003267
3268 // Get the mock profiling connection
3269 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
3270 BOOST_CHECK(mockProfilingConnection);
3271
Colm Donelan2ba48d22019-11-29 09:10:59 +00003272 // Write a valid "Request Counter Directory" packet into the mock profiling connection, to simulate a valid
3273 // reply from an external profiling service
3274
3275 // Request Counter Directory packet header (word 0, word 1 is always zero):
3276 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
3277 // 16:25 [10] packet_id: Packet identifier, value 0b0000000011
3278 // 8:15 [8] reserved: Reserved, value 0b00000000
3279 // 0:7 [8] reserved: Reserved, value 0b00000000
3280 uint32_t packetFamily = 0;
3281 uint32_t packetId = 123; // Wrong packet id!!!
3282 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
3283
3284 // Create the Request Counter Directory packet
3285 Packet requestCounterDirectoryPacket(header);
3286
3287 // Write the packet to the mock profiling connection
3288 mockProfilingConnection->WritePacket(std::move(requestCounterDirectoryPacket));
3289
Finn Williams09ad6f92019-12-19 17:05:18 +00003290 // Start the command handler and the send thread
3291 profilingService.Update();
3292
3293 // Reset the profiling service to stop and join any running thread
3294 options.m_EnableProfiling = false;
3295 profilingService.ResetExternalProfilingOptions(options, true);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003296
3297 streamRedirector.CancelRedirect();
3298
3299 // Check that the expected error has occurred and logged to the standard output
David Monahana8837bf2020-04-16 10:01:56 +01003300 if (ss.str().find("Functor with requested PacketId=123 and Version=4194304 does not exist") == std::string::npos)
Colm Donelan2ba48d22019-11-29 09:10:59 +00003301 {
3302 std::cout << ss.str();
3303 BOOST_FAIL("Expected string not found.");
3304 }
Colm Donelan2ba48d22019-11-29 09:10:59 +00003305}
3306
3307BOOST_AUTO_TEST_CASE(CheckProfilingServiceBadPeriodicCounterSelectionPacket)
3308{
3309 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
3310 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003311
3312 // Redirect the standard output to a local stream so that we can parse the warning message
3313 std::stringstream ss;
3314 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3315
3316 // Reset the profiling service to the uninitialized state
3317 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
3318 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +00003319 armnn::profiling::ProfilingService profilingService;
Colm Donelan2ba48d22019-11-29 09:10:59 +00003320 profilingService.ResetExternalProfilingOptions(options, true);
3321
Sadik Armagan3184c902020-03-18 10:57:30 +00003322 // Swap the profiling connection factory in the profiling service instance with our mock one
3323 SwapProfilingConnectionFactoryHelper helper(profilingService);
3324
Colm Donelan2ba48d22019-11-29 09:10:59 +00003325 // Bring the profiling service to the "Active" state
3326 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3327 profilingService.Update(); // Initialize the counter directory
3328 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
3329 profilingService.Update(); // Create the profiling connection
3330 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
3331 profilingService.Update(); // Start the command handler and the send thread
3332
3333 // Get the mock profiling connection
3334 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
3335 BOOST_CHECK(mockProfilingConnection);
3336
Colm Donelan2ba48d22019-11-29 09:10:59 +00003337 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
3338 // external profiling service
3339
3340 // Periodic Counter Selection packet header:
3341 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
3342 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
3343 // 8:15 [8] reserved: Reserved, value 0b00000000
3344 // 0:7 [8] reserved: Reserved, value 0b00000000
3345 uint32_t packetFamily = 0;
3346 uint32_t packetId = 999; // Wrong packet id!!!
3347 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
3348
3349 // Create the Periodic Counter Selection packet
3350 Packet periodicCounterSelectionPacket(header); // Length == 0, this will disable the collection of counters
3351
3352 // Write the packet to the mock profiling connection
3353 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
Finn Williams09ad6f92019-12-19 17:05:18 +00003354 profilingService.Update();
Colm Donelan2ba48d22019-11-29 09:10:59 +00003355
Finn Williams09ad6f92019-12-19 17:05:18 +00003356 // Reset the profiling service to stop any running thread
3357 options.m_EnableProfiling = false;
3358 profilingService.ResetExternalProfilingOptions(options, true);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003359
3360 // Check that the expected error has occurred and logged to the standard output
3361 streamRedirector.CancelRedirect();
3362
3363 // Check that the expected error has occurred and logged to the standard output
David Monahana8837bf2020-04-16 10:01:56 +01003364 if (ss.str().find("Functor with requested PacketId=999 and Version=4194304 does not exist") == std::string::npos)
Colm Donelan2ba48d22019-11-29 09:10:59 +00003365 {
3366 std::cout << ss.str();
3367 BOOST_FAIL("Expected string not found.");
3368 }
Colm Donelan2ba48d22019-11-29 09:10:59 +00003369}
Jim Flynn97897022020-02-02 12:52:59 +00003370
David Monahande803072020-01-30 12:44:23 +00003371BOOST_AUTO_TEST_CASE(CheckCounterIdMap)
3372{
3373 CounterIdMap counterIdMap;
3374 BOOST_CHECK_THROW(counterIdMap.GetBackendId(0), armnn::Exception);
3375 BOOST_CHECK_THROW(counterIdMap.GetGlobalId(0, armnn::profiling::BACKEND_ID), armnn::Exception);
3376
3377 uint16_t globalCounterIds = 0;
3378
3379 armnn::BackendId cpuRefId(armnn::Compute::CpuRef);
3380 armnn::BackendId cpuAccId(armnn::Compute::CpuAcc);
3381
3382 std::vector<uint16_t> cpuRefCounters = {0, 1, 2, 3};
3383 std::vector<uint16_t> cpuAccCounters = {0, 1};
3384
3385 for (uint16_t backendCounterId : cpuRefCounters)
3386 {
3387 counterIdMap.RegisterMapping(globalCounterIds, backendCounterId, cpuRefId);
3388 ++globalCounterIds;
3389 }
3390 for (uint16_t backendCounterId : cpuAccCounters)
3391 {
3392 counterIdMap.RegisterMapping(globalCounterIds, backendCounterId, cpuAccId);
3393 ++globalCounterIds;
3394 }
3395
3396 BOOST_CHECK(counterIdMap.GetBackendId(0) == (std::pair<uint16_t, armnn::BackendId>(0, cpuRefId)));
3397 BOOST_CHECK(counterIdMap.GetBackendId(1) == (std::pair<uint16_t, armnn::BackendId>(1, cpuRefId)));
3398 BOOST_CHECK(counterIdMap.GetBackendId(2) == (std::pair<uint16_t, armnn::BackendId>(2, cpuRefId)));
3399 BOOST_CHECK(counterIdMap.GetBackendId(3) == (std::pair<uint16_t, armnn::BackendId>(3, cpuRefId)));
3400 BOOST_CHECK(counterIdMap.GetBackendId(4) == (std::pair<uint16_t, armnn::BackendId>(0, cpuAccId)));
3401 BOOST_CHECK(counterIdMap.GetBackendId(5) == (std::pair<uint16_t, armnn::BackendId>(1, cpuAccId)));
3402
3403 BOOST_CHECK(counterIdMap.GetGlobalId(0, cpuRefId) == 0);
3404 BOOST_CHECK(counterIdMap.GetGlobalId(1, cpuRefId) == 1);
3405 BOOST_CHECK(counterIdMap.GetGlobalId(2, cpuRefId) == 2);
3406 BOOST_CHECK(counterIdMap.GetGlobalId(3, cpuRefId) == 3);
3407 BOOST_CHECK(counterIdMap.GetGlobalId(0, cpuAccId) == 4);
3408 BOOST_CHECK(counterIdMap.GetGlobalId(1, cpuAccId) == 5);
3409}
Colm Donelan2ba48d22019-11-29 09:10:59 +00003410
Jim Flynn97897022020-02-02 12:52:59 +00003411BOOST_AUTO_TEST_CASE(CheckRegisterBackendCounters)
3412{
3413 uint16_t globalCounterIds = armnn::profiling::INFERENCES_RUN;
3414 armnn::BackendId cpuRefId(armnn::Compute::CpuRef);
3415
Jim Flynn97897022020-02-02 12:52:59 +00003416 // Reset the profiling service to the uninitialized state
3417 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
3418 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +00003419 ProfilingService profilingService;
Jim Flynn97897022020-02-02 12:52:59 +00003420 profilingService.ResetExternalProfilingOptions(options, true);
3421
Sadik Armagan3184c902020-03-18 10:57:30 +00003422 RegisterBackendCounters registerBackendCounters(globalCounterIds, cpuRefId, profilingService);
3423
3424
3425
Jim Flynn97897022020-02-02 12:52:59 +00003426 BOOST_CHECK(profilingService.GetCounterDirectory().GetCategories().empty());
3427 registerBackendCounters.RegisterCategory("categoryOne");
3428 auto categoryOnePtr = profilingService.GetCounterDirectory().GetCategory("categoryOne");
3429 BOOST_CHECK(categoryOnePtr);
3430
3431 BOOST_CHECK(profilingService.GetCounterDirectory().GetDevices().empty());
3432 globalCounterIds = registerBackendCounters.RegisterDevice("deviceOne");
3433 auto deviceOnePtr = profilingService.GetCounterDirectory().GetDevice(globalCounterIds);
3434 BOOST_CHECK(deviceOnePtr);
3435 BOOST_CHECK(deviceOnePtr->m_Name == "deviceOne");
3436
3437 BOOST_CHECK(profilingService.GetCounterDirectory().GetCounterSets().empty());
3438 globalCounterIds = registerBackendCounters.RegisterCounterSet("counterSetOne");
3439 auto counterSetOnePtr = profilingService.GetCounterDirectory().GetCounterSet(globalCounterIds);
3440 BOOST_CHECK(counterSetOnePtr);
3441 BOOST_CHECK(counterSetOnePtr->m_Name == "counterSetOne");
3442
3443 uint16_t newGlobalCounterId = registerBackendCounters.RegisterCounter(0,
3444 "categoryOne",
3445 0,
3446 0,
3447 1.f,
3448 "CounterOne",
3449 "first test counter");
3450 BOOST_CHECK(newGlobalCounterId = armnn::profiling::INFERENCES_RUN + 1);
3451 uint16_t mappedGlobalId = profilingService.GetCounterMappings().GetGlobalId(0, cpuRefId);
3452 BOOST_CHECK(mappedGlobalId == newGlobalCounterId);
3453 auto backendMapping = profilingService.GetCounterMappings().GetBackendId(newGlobalCounterId);
3454 BOOST_CHECK(backendMapping.first == 0);
3455 BOOST_CHECK(backendMapping.second == cpuRefId);
3456
3457 // Reset the profiling service to stop any running thread
3458 options.m_EnableProfiling = false;
3459 profilingService.ResetExternalProfilingOptions(options, true);
3460}
3461
James Conroy2dcd3fe2020-02-06 18:34:52 +00003462BOOST_AUTO_TEST_CASE(CheckCounterStatusQuery)
3463{
3464 armnn::IRuntime::CreationOptions options;
3465 options.m_ProfilingOptions.m_EnableProfiling = true;
3466
3467 // Reset the profiling service to the uninitialized state
Sadik Armagan3184c902020-03-18 10:57:30 +00003468 ProfilingService profilingService;
James Conroy2dcd3fe2020-02-06 18:34:52 +00003469 profilingService.ResetExternalProfilingOptions(options.m_ProfilingOptions, true);
3470
3471 const armnn::BackendId cpuRefId(armnn::Compute::CpuRef);
3472 const armnn::BackendId cpuAccId(armnn::Compute::CpuAcc);
3473
3474 // Create BackendProfiling for each backend
3475 BackendProfiling backendProfilingCpuRef(options, profilingService, cpuRefId);
3476 BackendProfiling backendProfilingCpuAcc(options, profilingService, cpuAccId);
3477
3478 uint16_t initialNumGlobalCounterIds = armnn::profiling::INFERENCES_RUN;
3479
3480 // Create RegisterBackendCounters for CpuRef
Sadik Armagan3184c902020-03-18 10:57:30 +00003481 RegisterBackendCounters registerBackendCountersCpuRef(initialNumGlobalCounterIds, cpuRefId, profilingService);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003482
3483 // Create 'testCategory' in CounterDirectory (backend agnostic)
3484 BOOST_CHECK(profilingService.GetCounterDirectory().GetCategories().empty());
3485 registerBackendCountersCpuRef.RegisterCategory("testCategory");
3486 auto categoryOnePtr = profilingService.GetCounterDirectory().GetCategory("testCategory");
3487 BOOST_CHECK(categoryOnePtr);
3488
3489 // Counters:
3490 // Global | Local | Backend
3491 // 5 | 0 | CpuRef
3492 // 6 | 1 | CpuRef
3493 // 7 | 1 | CpuAcc
3494
3495 std::vector<uint16_t> cpuRefCounters = {0, 1};
3496 std::vector<uint16_t> cpuAccCounters = {0};
3497
3498 // Register the backend counters for CpuRef and validate GetGlobalId and GetBackendId
3499 uint16_t currentNumGlobalCounterIds = registerBackendCountersCpuRef.RegisterCounter(
3500 0, "testCategory", 0, 0, 1.f, "CpuRefCounter0", "Zeroth CpuRef Counter");
3501 BOOST_CHECK(currentNumGlobalCounterIds == initialNumGlobalCounterIds + 1);
3502 uint16_t mappedGlobalId = profilingService.GetCounterMappings().GetGlobalId(0, cpuRefId);
3503 BOOST_CHECK(mappedGlobalId == currentNumGlobalCounterIds);
3504 auto backendMapping = profilingService.GetCounterMappings().GetBackendId(currentNumGlobalCounterIds);
3505 BOOST_CHECK(backendMapping.first == 0);
3506 BOOST_CHECK(backendMapping.second == cpuRefId);
3507
3508 currentNumGlobalCounterIds = registerBackendCountersCpuRef.RegisterCounter(
3509 1, "testCategory", 0, 0, 1.f, "CpuRefCounter1", "First CpuRef Counter");
3510 BOOST_CHECK(currentNumGlobalCounterIds == initialNumGlobalCounterIds + 2);
3511 mappedGlobalId = profilingService.GetCounterMappings().GetGlobalId(1, cpuRefId);
3512 BOOST_CHECK(mappedGlobalId == currentNumGlobalCounterIds);
3513 backendMapping = profilingService.GetCounterMappings().GetBackendId(currentNumGlobalCounterIds);
3514 BOOST_CHECK(backendMapping.first == 1);
3515 BOOST_CHECK(backendMapping.second == cpuRefId);
3516
3517 // Create RegisterBackendCounters for CpuAcc
Sadik Armagan3184c902020-03-18 10:57:30 +00003518 RegisterBackendCounters registerBackendCountersCpuAcc(currentNumGlobalCounterIds, cpuAccId, profilingService);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003519
3520 // Register the backend counter for CpuAcc and validate GetGlobalId and GetBackendId
3521 currentNumGlobalCounterIds = registerBackendCountersCpuAcc.RegisterCounter(
3522 0, "testCategory", 0, 0, 1.f, "CpuAccCounter0", "Zeroth CpuAcc Counter");
3523 BOOST_CHECK(currentNumGlobalCounterIds == initialNumGlobalCounterIds + 3);
3524 mappedGlobalId = profilingService.GetCounterMappings().GetGlobalId(0, cpuAccId);
3525 BOOST_CHECK(mappedGlobalId == currentNumGlobalCounterIds);
3526 backendMapping = profilingService.GetCounterMappings().GetBackendId(currentNumGlobalCounterIds);
3527 BOOST_CHECK(backendMapping.first == 0);
3528 BOOST_CHECK(backendMapping.second == cpuAccId);
3529
3530 // Create vectors for active counters
3531 const std::vector<uint16_t> activeGlobalCounterIds = {5}; // CpuRef(0) activated
3532 const std::vector<uint16_t> newActiveGlobalCounterIds = {6, 7}; // CpuRef(0) and CpuAcc(1) activated
3533
3534 const uint32_t capturePeriod = 200;
3535 const uint32_t newCapturePeriod = 100;
3536
3537 // Set capture period and active counters in CaptureData
Finn Williams032bc742020-02-12 11:02:34 +00003538 profilingService.SetCaptureData(capturePeriod, activeGlobalCounterIds, {});
James Conroy2dcd3fe2020-02-06 18:34:52 +00003539
3540 // Get vector of active counters for CpuRef and CpuAcc backends
3541 std::vector<CounterStatus> cpuRefCounterStatus = backendProfilingCpuRef.GetActiveCounters();
3542 std::vector<CounterStatus> cpuAccCounterStatus = backendProfilingCpuAcc.GetActiveCounters();
3543 BOOST_CHECK_EQUAL(cpuRefCounterStatus.size(), 1);
3544 BOOST_CHECK_EQUAL(cpuAccCounterStatus.size(), 0);
3545
3546 // Check active CpuRef counter
3547 BOOST_CHECK_EQUAL(cpuRefCounterStatus[0].m_GlobalCounterId, activeGlobalCounterIds[0]);
3548 BOOST_CHECK_EQUAL(cpuRefCounterStatus[0].m_BackendCounterId, cpuRefCounters[0]);
3549 BOOST_CHECK_EQUAL(cpuRefCounterStatus[0].m_SamplingRateInMicroseconds, capturePeriod);
3550 BOOST_CHECK_EQUAL(cpuRefCounterStatus[0].m_Enabled, true);
3551
3552 // Check inactive CpuRef counter
3553 CounterStatus inactiveCpuRefCounter = backendProfilingCpuRef.GetCounterStatus(cpuRefCounters[1]);
3554 BOOST_CHECK_EQUAL(inactiveCpuRefCounter.m_GlobalCounterId, 6);
3555 BOOST_CHECK_EQUAL(inactiveCpuRefCounter.m_BackendCounterId, cpuRefCounters[1]);
3556 BOOST_CHECK_EQUAL(inactiveCpuRefCounter.m_SamplingRateInMicroseconds, 0);
3557 BOOST_CHECK_EQUAL(inactiveCpuRefCounter.m_Enabled, false);
3558
3559 // Check inactive CpuAcc counter
3560 CounterStatus inactiveCpuAccCounter = backendProfilingCpuAcc.GetCounterStatus(cpuAccCounters[0]);
3561 BOOST_CHECK_EQUAL(inactiveCpuAccCounter.m_GlobalCounterId, 7);
3562 BOOST_CHECK_EQUAL(inactiveCpuAccCounter.m_BackendCounterId, cpuAccCounters[0]);
3563 BOOST_CHECK_EQUAL(inactiveCpuAccCounter.m_SamplingRateInMicroseconds, 0);
3564 BOOST_CHECK_EQUAL(inactiveCpuAccCounter.m_Enabled, false);
3565
3566 // Set new capture period and new active counters in CaptureData
Finn Williams032bc742020-02-12 11:02:34 +00003567 profilingService.SetCaptureData(newCapturePeriod, newActiveGlobalCounterIds, {});
James Conroy2dcd3fe2020-02-06 18:34:52 +00003568
3569 // Get vector of active counters for CpuRef and CpuAcc backends
3570 cpuRefCounterStatus = backendProfilingCpuRef.GetActiveCounters();
3571 cpuAccCounterStatus = backendProfilingCpuAcc.GetActiveCounters();
3572 BOOST_CHECK_EQUAL(cpuRefCounterStatus.size(), 1);
3573 BOOST_CHECK_EQUAL(cpuAccCounterStatus.size(), 1);
3574
3575 // Check active CpuRef counter
3576 BOOST_CHECK_EQUAL(cpuRefCounterStatus[0].m_GlobalCounterId, newActiveGlobalCounterIds[0]);
3577 BOOST_CHECK_EQUAL(cpuRefCounterStatus[0].m_BackendCounterId, cpuRefCounters[1]);
3578 BOOST_CHECK_EQUAL(cpuRefCounterStatus[0].m_SamplingRateInMicroseconds, newCapturePeriod);
3579 BOOST_CHECK_EQUAL(cpuRefCounterStatus[0].m_Enabled, true);
3580
3581 // Check active CpuAcc counter
3582 BOOST_CHECK_EQUAL(cpuAccCounterStatus[0].m_GlobalCounterId, newActiveGlobalCounterIds[1]);
3583 BOOST_CHECK_EQUAL(cpuAccCounterStatus[0].m_BackendCounterId, cpuAccCounters[0]);
3584 BOOST_CHECK_EQUAL(cpuAccCounterStatus[0].m_SamplingRateInMicroseconds, newCapturePeriod);
3585 BOOST_CHECK_EQUAL(cpuAccCounterStatus[0].m_Enabled, true);
3586
3587 // Check inactive CpuRef counter
3588 inactiveCpuRefCounter = backendProfilingCpuRef.GetCounterStatus(cpuRefCounters[0]);
3589 BOOST_CHECK_EQUAL(inactiveCpuRefCounter.m_GlobalCounterId, 5);
3590 BOOST_CHECK_EQUAL(inactiveCpuRefCounter.m_BackendCounterId, cpuRefCounters[0]);
3591 BOOST_CHECK_EQUAL(inactiveCpuRefCounter.m_SamplingRateInMicroseconds, 0);
3592 BOOST_CHECK_EQUAL(inactiveCpuRefCounter.m_Enabled, false);
3593
3594 // Reset the profiling service to stop any running thread
3595 options.m_ProfilingOptions.m_EnableProfiling = false;
3596 profilingService.ResetExternalProfilingOptions(options.m_ProfilingOptions, true);
3597}
3598
Sadik Armagancab588a2020-02-17 11:33:31 +00003599BOOST_AUTO_TEST_CASE(CheckRegisterCounters)
3600{
3601 armnn::Runtime::CreationOptions options;
3602 options.m_ProfilingOptions.m_EnableProfiling = true;
3603 MockBufferManager mockBuffer(1024);
Sadik Armagan3184c902020-03-18 10:57:30 +00003604
Sadik Armagancab588a2020-02-17 11:33:31 +00003605 CaptureData captureData;
Keith Davis33ed2212020-03-30 10:43:41 +01003606 MockProfilingService mockProfilingService(mockBuffer, options.m_ProfilingOptions.m_EnableProfiling, captureData);
Sadik Armagancab588a2020-02-17 11:33:31 +00003607 armnn::BackendId cpuRefId(armnn::Compute::CpuRef);
3608
3609 mockProfilingService.RegisterMapping(6, 0, cpuRefId);
3610 mockProfilingService.RegisterMapping(7, 1, cpuRefId);
3611 mockProfilingService.RegisterMapping(8, 2, cpuRefId);
3612
3613 armnn::profiling::BackendProfiling backendProfiling(options,
3614 mockProfilingService,
3615 cpuRefId);
3616
3617 armnn::profiling::Timestamp timestamp;
3618 timestamp.timestamp = 1000998;
3619 timestamp.counterValues.emplace_back(0, 700);
3620 timestamp.counterValues.emplace_back(2, 93);
3621 std::vector<armnn::profiling::Timestamp> timestamps;
3622 timestamps.push_back(timestamp);
3623 backendProfiling.ReportCounters(timestamps);
3624
3625 auto readBuffer = mockBuffer.GetReadableBuffer();
3626
3627 uint32_t headerWord0 = ReadUint32(readBuffer, 0);
3628 uint32_t headerWord1 = ReadUint32(readBuffer, 4);
3629 uint64_t readTimestamp = ReadUint64(readBuffer, 8);
3630
3631 BOOST_TEST(((headerWord0 >> 26) & 0x0000003F) == 3); // packet family
3632 BOOST_TEST(((headerWord0 >> 19) & 0x0000007F) == 0); // packet class
3633 BOOST_TEST(((headerWord0 >> 16) & 0x00000007) == 0); // packet type
3634 BOOST_TEST(headerWord1 == 20); // data length
3635 BOOST_TEST(1000998 == readTimestamp); // capture period
3636
3637 uint32_t offset = 16;
3638 // Check Counter Index
3639 uint16_t readIndex = ReadUint16(readBuffer, offset);
3640 BOOST_TEST(6 == readIndex);
3641
3642 // Check Counter Value
3643 offset += 2;
3644 uint32_t readValue = ReadUint32(readBuffer, offset);
3645 BOOST_TEST(700 == readValue);
3646
3647 // Check Counter Index
3648 offset += 4;
3649 readIndex = ReadUint16(readBuffer, offset);
3650 BOOST_TEST(8 == readIndex);
3651
3652 // Check Counter Value
3653 offset += 2;
3654 readValue = ReadUint32(readBuffer, offset);
3655 BOOST_TEST(93 == readValue);
3656}
3657
Isabella Gottardia0687ee2020-03-11 18:04:20 +00003658BOOST_AUTO_TEST_CASE(CheckFileFormat) {
3659 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
3660 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
3661
3662 // Create profiling options.
3663 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
3664 options.m_EnableProfiling = true;
3665 // Check the default value set to binary
3666 BOOST_CHECK(options.m_FileFormat == "binary");
3667
3668 // Change file format to an unsupported value
3669 options.m_FileFormat = "json";
3670 // Enable the profiling service
3671 armnn::profiling::ProfilingService profilingService;
3672 profilingService.ResetExternalProfilingOptions(options, true);
3673 // Start the command handler and the send thread
3674 profilingService.Update();
3675 BOOST_CHECK(profilingService.GetCurrentState()==ProfilingState::NotConnected);
3676
3677 // Redirect the output to a local stream so that we can parse the warning message
3678 std::stringstream ss;
3679 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3680
3681 // When Update is called and the current state is ProfilingState::NotConnected
3682 // an exception will be raised from GetProfilingConnection and displayed as warning in the output local stream
3683 profilingService.Update();
3684
3685 streamRedirector.CancelRedirect();
3686
3687 // Check that the expected error has occurred and logged to the standard output
David Monahana8837bf2020-04-16 10:01:56 +01003688 if (ss.str().find("Unsupported profiling file format, only binary is supported") == std::string::npos)
Isabella Gottardia0687ee2020-03-11 18:04:20 +00003689 {
3690 std::cout << ss.str();
3691 BOOST_FAIL("Expected string not found.");
3692 }
3693}
3694
Francis Murtagh1f7db452019-08-14 09:49:34 +01003695BOOST_AUTO_TEST_SUITE_END()