blob: c025aa2e3e03b3127aded4d3a26ce7fdc35efe76 [file] [log] [blame]
Francis Murtagh1f7db452019-08-14 09:49:34 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
Matteo Martincighd0613b52019-10-09 16:47:04 +01006#include "ProfilingTests.hpp"
Teresa Charlin9bab4962019-09-06 12:28:35 +01007
James Conroy2dcd3fe2020-02-06 18:34:52 +00008#include <backends/BackendProfiling.hpp>
Matteo Martincigh8a837172019-10-04 17:01:07 +01009#include <CommandHandler.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010010#include <CommandHandlerKey.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010011#include <CommandHandlerRegistry.hpp>
Sadik Armaganb5f01b22019-09-18 17:29:00 +010012#include <ConnectionAcknowledgedCommandHandler.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010013#include <CounterDirectory.hpp>
David Monahande803072020-01-30 12:44:23 +000014#include <CounterIdMap.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010015#include <EncodeVersion.hpp>
16#include <Holder.hpp>
Matteo Martincighe0e6efc2019-10-04 17:17:42 +010017#include <ICounterValues.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010018#include <Packet.hpp>
19#include <PacketVersionResolver.hpp>
Francis Murtaghfcb8ef62019-09-20 15:40:09 +010020#include <PeriodicCounterCapture.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010021#include <PeriodicCounterSelectionCommandHandler.hpp>
22#include <ProfilingStateMachine.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010023#include <ProfilingUtils.hpp>
James Conroy2dcd3fe2020-02-06 18:34:52 +000024#include <RegisterBackendCounters.hpp>
Narumol Prangnawarat48033692019-09-20 12:04:55 +010025#include <RequestCounterDirectoryCommandHandler.hpp>
Teresa Charlin9bab4962019-09-06 12:28:35 +010026#include <Runtime.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010027#include <SocketProfilingConnection.hpp>
Matteo Martincighcdfb9412019-11-08 11:23:06 +000028#include <SendCounterPacket.hpp>
Sadik Armagan3896b472020-02-10 12:24:15 +000029#include <SendThread.hpp>
Matteo Martincighcdfb9412019-11-08 11:23:06 +000030#include <SendTimelinePacket.hpp>
Keith Davis02356de2019-08-26 18:28:17 +010031
Matteo Martincigh6db5f202019-09-05 12:02:04 +010032#include <armnn/Conversion.hpp>
Colm Donelan02705242019-11-14 14:19:07 +000033#include <armnn/Types.hpp>
Ferran Balaguer1b941722019-08-28 16:57:18 +010034
Matteo Martincigh54fb9572019-10-02 12:50:57 +010035#include <armnn/Utils.hpp>
36
Sadik Armaganbd9e2c52019-09-26 23:13:31 +010037#include <boost/algorithm/string.hpp>
Ferran Balaguer1b941722019-08-28 16:57:18 +010038#include <boost/numeric/conversion/cast.hpp>
Francis Murtagh1f7db452019-08-14 09:49:34 +010039
Nikhil Rajbc626052019-08-15 15:49:45 +010040#include <cstdint>
41#include <cstring>
Keith Davis3201eea2019-10-24 17:30:41 +010042#include <iostream>
Aron Virginas-Tare898db92019-08-22 12:56:34 +010043#include <limits>
Francis Murtagh11f99b42019-08-16 11:28:52 +010044#include <map>
Aron Virginas-Tare898db92019-08-22 12:56:34 +010045#include <random>
James Conroy2dcd3fe2020-02-06 18:34:52 +000046
Francis Murtagh1f7db452019-08-14 09:49:34 +010047
Aron Virginas-Tare898db92019-08-22 12:56:34 +010048using namespace armnn::profiling;
Finn Williams09ad6f92019-12-19 17:05:18 +000049using PacketType = MockProfilingConnection::PacketType;
Aron Virginas-Tare898db92019-08-22 12:56:34 +010050
Matteo Martincigh8a837172019-10-04 17:01:07 +010051BOOST_AUTO_TEST_SUITE(ExternalProfiling)
52
Francis Murtagh1f7db452019-08-14 09:49:34 +010053BOOST_AUTO_TEST_CASE(CheckCommandHandlerKeyComparisons)
54{
Jim Flynn397043f2019-10-17 17:37:10 +010055 CommandHandlerKey testKey1_0(1, 1, 1);
56 CommandHandlerKey testKey1_1(1, 1, 1);
57 CommandHandlerKey testKey1_2(1, 2, 1);
58
59 CommandHandlerKey testKey0(0, 1, 1);
60 CommandHandlerKey testKey1(0, 1, 1);
61 CommandHandlerKey testKey2(0, 1, 1);
62 CommandHandlerKey testKey3(0, 0, 0);
63 CommandHandlerKey testKey4(0, 2, 2);
64 CommandHandlerKey testKey5(0, 0, 2);
65
66 BOOST_CHECK(testKey1_0 > testKey0);
67 BOOST_CHECK(testKey1_0 == testKey1_1);
68 BOOST_CHECK(testKey1_0 < testKey1_2);
Francis Murtagh1f7db452019-08-14 09:49:34 +010069
Keith Davis3201eea2019-10-24 17:30:41 +010070 BOOST_CHECK(testKey1 < testKey4);
71 BOOST_CHECK(testKey1 > testKey3);
72 BOOST_CHECK(testKey1 <= testKey4);
73 BOOST_CHECK(testKey1 >= testKey3);
74 BOOST_CHECK(testKey1 <= testKey2);
75 BOOST_CHECK(testKey1 >= testKey2);
76 BOOST_CHECK(testKey1 == testKey2);
77 BOOST_CHECK(testKey1 == testKey1);
Francis Murtagh1f7db452019-08-14 09:49:34 +010078
Keith Davis3201eea2019-10-24 17:30:41 +010079 BOOST_CHECK(!(testKey1 == testKey5));
80 BOOST_CHECK(!(testKey1 != testKey1));
81 BOOST_CHECK(testKey1 != testKey5);
Francis Murtagh1f7db452019-08-14 09:49:34 +010082
Keith Davis3201eea2019-10-24 17:30:41 +010083 BOOST_CHECK(testKey1 == testKey2 && testKey2 == testKey1);
84 BOOST_CHECK(testKey0 == testKey1 && testKey1 == testKey2 && testKey0 == testKey2);
Francis Murtagh1f7db452019-08-14 09:49:34 +010085
Keith Davis3201eea2019-10-24 17:30:41 +010086 BOOST_CHECK(testKey1.GetPacketId() == 1);
87 BOOST_CHECK(testKey1.GetVersion() == 1);
Francis Murtagh1f7db452019-08-14 09:49:34 +010088
Keith Davis3201eea2019-10-24 17:30:41 +010089 std::vector<CommandHandlerKey> vect = { CommandHandlerKey(0, 0, 1), CommandHandlerKey(0, 2, 0),
90 CommandHandlerKey(0, 1, 0), CommandHandlerKey(0, 2, 1),
91 CommandHandlerKey(0, 1, 1), CommandHandlerKey(0, 0, 1),
92 CommandHandlerKey(0, 2, 0), CommandHandlerKey(0, 0, 0) };
Francis Murtagh1f7db452019-08-14 09:49:34 +010093
94 std::sort(vect.begin(), vect.end());
95
Keith Davis3201eea2019-10-24 17:30:41 +010096 std::vector<CommandHandlerKey> expectedVect = { CommandHandlerKey(0, 0, 0), CommandHandlerKey(0, 0, 1),
97 CommandHandlerKey(0, 0, 1), CommandHandlerKey(0, 1, 0),
98 CommandHandlerKey(0, 1, 1), CommandHandlerKey(0, 2, 0),
99 CommandHandlerKey(0, 2, 0), CommandHandlerKey(0, 2, 1) };
Francis Murtagh1f7db452019-08-14 09:49:34 +0100100
101 BOOST_CHECK(vect == expectedVect);
102}
103
Jim Flynned25e0e2019-10-18 13:21:43 +0100104BOOST_AUTO_TEST_CASE(CheckPacketKeyComparisons)
105{
Keith Davis3201eea2019-10-24 17:30:41 +0100106 PacketKey key0(0, 0);
107 PacketKey key1(0, 0);
108 PacketKey key2(0, 1);
109 PacketKey key3(0, 2);
110 PacketKey key4(1, 0);
111 PacketKey key5(1, 0);
112 PacketKey key6(1, 1);
Jim Flynned25e0e2019-10-18 13:21:43 +0100113
114 BOOST_CHECK(!(key0 < key1));
115 BOOST_CHECK(!(key0 > key1));
116 BOOST_CHECK(key0 <= key1);
117 BOOST_CHECK(key0 >= key1);
118 BOOST_CHECK(key0 == key1);
119 BOOST_CHECK(key0 < key2);
120 BOOST_CHECK(key2 < key3);
121 BOOST_CHECK(key3 > key0);
122 BOOST_CHECK(key4 == key5);
123 BOOST_CHECK(key4 > key0);
124 BOOST_CHECK(key5 < key6);
125 BOOST_CHECK(key5 <= key6);
126 BOOST_CHECK(key5 != key6);
127}
128
Matteo Martincigh8a837172019-10-04 17:01:07 +0100129BOOST_AUTO_TEST_CASE(CheckCommandHandler)
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100130{
Matteo Martincigh8a837172019-10-04 17:01:07 +0100131 PacketVersionResolver packetVersionResolver;
132 ProfilingStateMachine profilingStateMachine;
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100133
Matteo Martincigh8a837172019-10-04 17:01:07 +0100134 TestProfilingConnectionBase testProfilingConnectionBase;
135 TestProfilingConnectionTimeoutError testProfilingConnectionTimeOutError;
136 TestProfilingConnectionArmnnError testProfilingConnectionArmnnError;
Keith Davis3201eea2019-10-24 17:30:41 +0100137 CounterDirectory counterDirectory;
138 MockBufferManager mockBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +0000139 SendCounterPacket sendCounterPacket(mockBuffer);
140 SendThread sendThread(profilingStateMachine, mockBuffer, sendCounterPacket);
Matteo Martincighcdfb9412019-11-08 11:23:06 +0000141 SendTimelinePacket sendTimelinePacket(mockBuffer);
142
Keith Davis3201eea2019-10-24 17:30:41 +0100143 ConnectionAcknowledgedCommandHandler connectionAcknowledgedCommandHandler(0, 1, 4194304, counterDirectory,
Matteo Martincighcdfb9412019-11-08 11:23:06 +0000144 sendCounterPacket, sendTimelinePacket,
145 profilingStateMachine);
Matteo Martincigh8a837172019-10-04 17:01:07 +0100146 CommandHandlerRegistry commandHandlerRegistry;
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100147
Matteo Martincighc2728f92019-10-07 12:35:21 +0100148 commandHandlerRegistry.RegisterFunctor(&connectionAcknowledgedCommandHandler);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100149
Matteo Martincigh8a837172019-10-04 17:01:07 +0100150 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
151 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100152
Keith Davis3201eea2019-10-24 17:30:41 +0100153 CommandHandler commandHandler0(1, true, commandHandlerRegistry, packetVersionResolver);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100154
Colm Donelan2ba48d22019-11-29 09:10:59 +0000155 // This should start the command handler thread return the connection ack and put the profiling
156 // service into active state.
Matteo Martincigh8a837172019-10-04 17:01:07 +0100157 commandHandler0.Start(testProfilingConnectionBase);
Colm Donelan2ba48d22019-11-29 09:10:59 +0000158 // Try to start the send thread many times, it must only start once
Matteo Martincigh8a837172019-10-04 17:01:07 +0100159 commandHandler0.Start(testProfilingConnectionBase);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100160
Colm Donelan2ba48d22019-11-29 09:10:59 +0000161 // This could take up to 20mSec but we'll check often.
162 for (int i = 0; i < 10; i++)
Matteo Martincigh8a837172019-10-04 17:01:07 +0100163 {
164 if (profilingStateMachine.GetCurrentState() == ProfilingState::Active)
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100165 {
Matteo Martincigh8a837172019-10-04 17:01:07 +0100166 break;
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100167 }
Colm Donelan2ba48d22019-11-29 09:10:59 +0000168 std::this_thread::sleep_for(std::chrono::milliseconds(2));
Matteo Martincigh8a837172019-10-04 17:01:07 +0100169 }
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100170
Matteo Martincigh8a837172019-10-04 17:01:07 +0100171 BOOST_CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::Active);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100172
Colm Donelan2ba48d22019-11-29 09:10:59 +0000173 // Close the thread again.
174 commandHandler0.Stop();
175
176 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
177 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
178
179 // In this test we'll simulate a timeout without a connection ack packet being received.
180 // Stop after timeout is set so we expect the command handler to stop almost immediately.
181 CommandHandler commandHandler1(1, true, commandHandlerRegistry, packetVersionResolver);
182
183 commandHandler1.Start(testProfilingConnectionTimeOutError);
184 // Wait until we know a timeout exception has been sent at least once.
185 for (int i = 0; i < 10; i++)
186 {
187 if (testProfilingConnectionTimeOutError.ReadCalledCount())
188 {
189 break;
190 }
191 std::this_thread::sleep_for(std::chrono::milliseconds(2));
192 }
Colm Donelan2ba48d22019-11-29 09:10:59 +0000193
194 // The command handler loop should have stopped after the timeout.
Finn Williams09ad6f92019-12-19 17:05:18 +0000195 // wait for the timeout exception to be processed and the loop to break.
196 uint32_t timeout = 50;
197 uint32_t timeSlept = 0;
198 while (commandHandler1.IsRunning())
199 {
200 if (timeSlept >= timeout)
201 {
202 BOOST_FAIL("Timeout: The command handler loop did not stop after the timeout");
203 }
204 std::this_thread::sleep_for(std::chrono::milliseconds(1));
205 timeSlept ++;
206 }
Colm Donelan2ba48d22019-11-29 09:10:59 +0000207
208 commandHandler1.Stop();
209 // The state machine should never have received the ack so will still be in WaitingForAck.
210 BOOST_CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::WaitingForAck);
211
Finn Williams09ad6f92019-12-19 17:05:18 +0000212 // Now try sending a bad connection acknowledged packet
213 TestProfilingConnectionBadAckPacket testProfilingConnectionBadAckPacket;
214 commandHandler1.Start(testProfilingConnectionBadAckPacket);
215 commandHandler1.Stop();
216 // This should also not change the state machine
217 BOOST_CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::WaitingForAck);
218
Colm Donelan2ba48d22019-11-29 09:10:59 +0000219 // Disable stop after timeout and now commandHandler1 should persist after a timeout
220 commandHandler1.SetStopAfterTimeout(false);
221 // Restart the thread.
222 commandHandler1.Start(testProfilingConnectionTimeOutError);
223
224 // Wait for at the three timeouts and the ack to be sent.
225 for (int i = 0; i < 10; i++)
226 {
227 if (testProfilingConnectionTimeOutError.ReadCalledCount() > 3)
228 {
229 break;
230 }
231 std::this_thread::sleep_for(std::chrono::milliseconds(2));
232 }
233 commandHandler1.Stop();
234
235 // Even after the 3 exceptions the ack packet should have transitioned the command handler to active.
236 BOOST_CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::Active);
237
238 // A command handler that gets exceptions other than timeouts should keep going.
239 CommandHandler commandHandler2(1, false, commandHandlerRegistry, packetVersionResolver);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100240
Matteo Martincigh8a837172019-10-04 17:01:07 +0100241 commandHandler2.Start(testProfilingConnectionArmnnError);
242
Colm Donelan2ba48d22019-11-29 09:10:59 +0000243 // Wait for two exceptions to be thrown.
244 for (int i = 0; i < 10; i++)
245 {
246 if (testProfilingConnectionTimeOutError.ReadCalledCount() >= 2)
247 {
248 break;
249 }
250 std::this_thread::sleep_for(std::chrono::milliseconds(2));
251 }
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100252
Matteo Martincighd0613b52019-10-09 16:47:04 +0100253 BOOST_CHECK(commandHandler2.IsRunning());
Matteo Martincigh8a837172019-10-04 17:01:07 +0100254 commandHandler2.Stop();
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100255}
256
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100257BOOST_AUTO_TEST_CASE(CheckEncodeVersion)
258{
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100259 Version version1(12);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100260
261 BOOST_CHECK(version1.GetMajor() == 0);
262 BOOST_CHECK(version1.GetMinor() == 0);
263 BOOST_CHECK(version1.GetPatch() == 12);
264
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100265 Version version2(4108);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100266
267 BOOST_CHECK(version2.GetMajor() == 0);
268 BOOST_CHECK(version2.GetMinor() == 1);
269 BOOST_CHECK(version2.GetPatch() == 12);
270
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100271 Version version3(4198412);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100272
273 BOOST_CHECK(version3.GetMajor() == 1);
274 BOOST_CHECK(version3.GetMinor() == 1);
275 BOOST_CHECK(version3.GetPatch() == 12);
276
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100277 Version version4(0);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100278
279 BOOST_CHECK(version4.GetMajor() == 0);
280 BOOST_CHECK(version4.GetMinor() == 0);
281 BOOST_CHECK(version4.GetPatch() == 0);
282
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100283 Version version5(1, 0, 0);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100284 BOOST_CHECK(version5.GetEncodedValue() == 4194304);
285}
286
Nikhil Rajbc626052019-08-15 15:49:45 +0100287BOOST_AUTO_TEST_CASE(CheckPacketClass)
288{
Keith Davis3201eea2019-10-24 17:30:41 +0100289 uint32_t length = 4;
Matteo Martincigh67ef2a52019-10-10 13:29:02 +0100290 std::unique_ptr<unsigned char[]> packetData0 = std::make_unique<unsigned char[]>(length);
291 std::unique_ptr<unsigned char[]> packetData1 = std::make_unique<unsigned char[]>(0);
292 std::unique_ptr<unsigned char[]> nullPacketData;
Nikhil Rajbc626052019-08-15 15:49:45 +0100293
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100294 Packet packetTest0(472580096, length, packetData0);
Nikhil Rajbc626052019-08-15 15:49:45 +0100295
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100296 BOOST_CHECK(packetTest0.GetHeader() == 472580096);
297 BOOST_CHECK(packetTest0.GetPacketFamily() == 7);
298 BOOST_CHECK(packetTest0.GetPacketId() == 43);
299 BOOST_CHECK(packetTest0.GetLength() == length);
300 BOOST_CHECK(packetTest0.GetPacketType() == 3);
301 BOOST_CHECK(packetTest0.GetPacketClass() == 5);
Nikhil Rajbc626052019-08-15 15:49:45 +0100302
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100303 BOOST_CHECK_THROW(Packet packetTest1(472580096, 0, packetData1), armnn::Exception);
304 BOOST_CHECK_NO_THROW(Packet packetTest2(472580096, 0, nullPacketData));
Nikhil Rajbc626052019-08-15 15:49:45 +0100305
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100306 Packet packetTest3(472580096, 0, nullPacketData);
307 BOOST_CHECK(packetTest3.GetLength() == 0);
308 BOOST_CHECK(packetTest3.GetData() == nullptr);
309
Matteo Martincigh67ef2a52019-10-10 13:29:02 +0100310 const unsigned char* packetTest0Data = packetTest0.GetData();
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100311 Packet packetTest4(std::move(packetTest0));
312
313 BOOST_CHECK(packetTest0.GetData() == nullptr);
314 BOOST_CHECK(packetTest4.GetData() == packetTest0Data);
315
316 BOOST_CHECK(packetTest4.GetHeader() == 472580096);
317 BOOST_CHECK(packetTest4.GetPacketFamily() == 7);
318 BOOST_CHECK(packetTest4.GetPacketId() == 43);
319 BOOST_CHECK(packetTest4.GetLength() == length);
320 BOOST_CHECK(packetTest4.GetPacketType() == 3);
321 BOOST_CHECK(packetTest4.GetPacketClass() == 5);
Nikhil Rajbc626052019-08-15 15:49:45 +0100322}
323
Francis Murtagh11f99b42019-08-16 11:28:52 +0100324BOOST_AUTO_TEST_CASE(CheckCommandHandlerFunctor)
325{
Francis Murtagh11f99b42019-08-16 11:28:52 +0100326 // Hard code the version as it will be the same during a single profiling session
327 uint32_t version = 1;
328
Jim Flynn397043f2019-10-17 17:37:10 +0100329 TestFunctorA testFunctorA(7, 461, version);
330 TestFunctorB testFunctorB(8, 963, version);
331 TestFunctorC testFunctorC(5, 983, version);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100332
Jim Flynn397043f2019-10-17 17:37:10 +0100333 CommandHandlerKey keyA(testFunctorA.GetFamilyId(), testFunctorA.GetPacketId(), testFunctorA.GetVersion());
334 CommandHandlerKey keyB(testFunctorB.GetFamilyId(), testFunctorB.GetPacketId(), testFunctorB.GetVersion());
335 CommandHandlerKey keyC(testFunctorC.GetFamilyId(), testFunctorC.GetPacketId(), testFunctorC.GetVersion());
Francis Murtagh11f99b42019-08-16 11:28:52 +0100336
337 // Create the unwrapped map to simulate the Command Handler Registry
338 std::map<CommandHandlerKey, CommandHandlerFunctor*> registry;
339
340 registry.insert(std::make_pair(keyB, &testFunctorB));
341 registry.insert(std::make_pair(keyA, &testFunctorA));
342 registry.insert(std::make_pair(keyC, &testFunctorC));
343
344 // Check the order of the map is correct
345 auto it = registry.begin();
Keith Davis3201eea2019-10-24 17:30:41 +0100346 BOOST_CHECK(it->first == keyC); // familyId == 5
Francis Murtagh11f99b42019-08-16 11:28:52 +0100347 it++;
Keith Davis3201eea2019-10-24 17:30:41 +0100348 BOOST_CHECK(it->first == keyA); // familyId == 7
Francis Murtagh11f99b42019-08-16 11:28:52 +0100349 it++;
Keith Davis3201eea2019-10-24 17:30:41 +0100350 BOOST_CHECK(it->first == keyB); // familyId == 8
Francis Murtagh11f99b42019-08-16 11:28:52 +0100351
Matteo Martincigh67ef2a52019-10-10 13:29:02 +0100352 std::unique_ptr<unsigned char[]> packetDataA;
353 std::unique_ptr<unsigned char[]> packetDataB;
354 std::unique_ptr<unsigned char[]> packetDataC;
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100355
356 Packet packetA(500000000, 0, packetDataA);
357 Packet packetB(600000000, 0, packetDataB);
358 Packet packetC(400000000, 0, packetDataC);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100359
360 // Check the correct operator of derived class is called
Jim Flynn397043f2019-10-17 17:37:10 +0100361 registry.at(CommandHandlerKey(packetA.GetPacketFamily(), packetA.GetPacketId(), version))->operator()(packetA);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100362 BOOST_CHECK(testFunctorA.GetCount() == 1);
363 BOOST_CHECK(testFunctorB.GetCount() == 0);
364 BOOST_CHECK(testFunctorC.GetCount() == 0);
365
Jim Flynn397043f2019-10-17 17:37:10 +0100366 registry.at(CommandHandlerKey(packetB.GetPacketFamily(), packetB.GetPacketId(), version))->operator()(packetB);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100367 BOOST_CHECK(testFunctorA.GetCount() == 1);
368 BOOST_CHECK(testFunctorB.GetCount() == 1);
369 BOOST_CHECK(testFunctorC.GetCount() == 0);
370
Jim Flynn397043f2019-10-17 17:37:10 +0100371 registry.at(CommandHandlerKey(packetC.GetPacketFamily(), packetC.GetPacketId(), version))->operator()(packetC);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100372 BOOST_CHECK(testFunctorA.GetCount() == 1);
373 BOOST_CHECK(testFunctorB.GetCount() == 1);
374 BOOST_CHECK(testFunctorC.GetCount() == 1);
375}
376
Francis Murtagh94d79152019-08-16 17:45:07 +0100377BOOST_AUTO_TEST_CASE(CheckCommandHandlerRegistry)
378{
379 // Hard code the version as it will be the same during a single profiling session
380 uint32_t version = 1;
381
Jim Flynn397043f2019-10-17 17:37:10 +0100382 TestFunctorA testFunctorA(7, 461, version);
383 TestFunctorB testFunctorB(8, 963, version);
384 TestFunctorC testFunctorC(5, 983, version);
Francis Murtagh94d79152019-08-16 17:45:07 +0100385
386 // Create the Command Handler Registry
387 CommandHandlerRegistry registry;
388
389 // Register multiple different derived classes
Matteo Martincighc2728f92019-10-07 12:35:21 +0100390 registry.RegisterFunctor(&testFunctorA);
391 registry.RegisterFunctor(&testFunctorB);
392 registry.RegisterFunctor(&testFunctorC);
Francis Murtagh94d79152019-08-16 17:45:07 +0100393
Matteo Martincigh67ef2a52019-10-10 13:29:02 +0100394 std::unique_ptr<unsigned char[]> packetDataA;
395 std::unique_ptr<unsigned char[]> packetDataB;
396 std::unique_ptr<unsigned char[]> packetDataC;
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100397
398 Packet packetA(500000000, 0, packetDataA);
399 Packet packetB(600000000, 0, packetDataB);
400 Packet packetC(400000000, 0, packetDataC);
Francis Murtagh94d79152019-08-16 17:45:07 +0100401
402 // Check the correct operator of derived class is called
Jim Flynn397043f2019-10-17 17:37:10 +0100403 registry.GetFunctor(packetA.GetPacketFamily(), packetA.GetPacketId(), version)->operator()(packetA);
Francis Murtagh94d79152019-08-16 17:45:07 +0100404 BOOST_CHECK(testFunctorA.GetCount() == 1);
405 BOOST_CHECK(testFunctorB.GetCount() == 0);
406 BOOST_CHECK(testFunctorC.GetCount() == 0);
407
Jim Flynn397043f2019-10-17 17:37:10 +0100408 registry.GetFunctor(packetB.GetPacketFamily(), packetB.GetPacketId(), version)->operator()(packetB);
Francis Murtagh94d79152019-08-16 17:45:07 +0100409 BOOST_CHECK(testFunctorA.GetCount() == 1);
410 BOOST_CHECK(testFunctorB.GetCount() == 1);
411 BOOST_CHECK(testFunctorC.GetCount() == 0);
412
Jim Flynn397043f2019-10-17 17:37:10 +0100413 registry.GetFunctor(packetC.GetPacketFamily(), packetC.GetPacketId(), version)->operator()(packetC);
Francis Murtagh94d79152019-08-16 17:45:07 +0100414 BOOST_CHECK(testFunctorA.GetCount() == 1);
415 BOOST_CHECK(testFunctorB.GetCount() == 1);
416 BOOST_CHECK(testFunctorC.GetCount() == 1);
417
418 // Re-register an existing key with a new function
Jim Flynn397043f2019-10-17 17:37:10 +0100419 registry.RegisterFunctor(&testFunctorC, testFunctorA.GetFamilyId(), testFunctorA.GetPacketId(), version);
420 registry.GetFunctor(packetA.GetPacketFamily(), packetA.GetPacketId(), version)->operator()(packetC);
Francis Murtagh94d79152019-08-16 17:45:07 +0100421 BOOST_CHECK(testFunctorA.GetCount() == 1);
422 BOOST_CHECK(testFunctorB.GetCount() == 1);
423 BOOST_CHECK(testFunctorC.GetCount() == 2);
424
425 // Check that non-existent key returns nullptr for its functor
Jim Flynn397043f2019-10-17 17:37:10 +0100426 BOOST_CHECK_THROW(registry.GetFunctor(0, 0, 0), armnn::Exception);
Francis Murtagh94d79152019-08-16 17:45:07 +0100427}
428
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100429BOOST_AUTO_TEST_CASE(CheckPacketVersionResolver)
430{
431 // Set up random number generator for generating packetId values
432 std::random_device device;
433 std::mt19937 generator(device());
434 std::uniform_int_distribution<uint32_t> distribution(std::numeric_limits<uint32_t>::min(),
435 std::numeric_limits<uint32_t>::max());
436
437 // NOTE: Expected version is always 1.0.0, regardless of packetId
438 const Version expectedVersion(1, 0, 0);
439
440 PacketVersionResolver packetVersionResolver;
441
442 constexpr unsigned int numTests = 10u;
443
444 for (unsigned int i = 0u; i < numTests; ++i)
445 {
Jim Flynned25e0e2019-10-18 13:21:43 +0100446 const uint32_t familyId = distribution(generator);
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100447 const uint32_t packetId = distribution(generator);
Jim Flynned25e0e2019-10-18 13:21:43 +0100448 Version resolvedVersion = packetVersionResolver.ResolvePacketVersion(familyId, packetId);
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100449
450 BOOST_TEST(resolvedVersion == expectedVersion);
451 }
452}
Matteo Martincighd0613b52019-10-09 16:47:04 +0100453
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100454void ProfilingCurrentStateThreadImpl(ProfilingStateMachine& states)
455{
456 ProfilingState newState = ProfilingState::NotConnected;
457 states.GetCurrentState();
458 states.TransitionToState(newState);
459}
460
461BOOST_AUTO_TEST_CASE(CheckProfilingStateMachine)
462{
463 ProfilingStateMachine profilingState1(ProfilingState::Uninitialised);
464 profilingState1.TransitionToState(ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +0100465 BOOST_CHECK(profilingState1.GetCurrentState() == ProfilingState::Uninitialised);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100466
467 ProfilingStateMachine profilingState2(ProfilingState::Uninitialised);
468 profilingState2.TransitionToState(ProfilingState::NotConnected);
469 BOOST_CHECK(profilingState2.GetCurrentState() == ProfilingState::NotConnected);
470
471 ProfilingStateMachine profilingState3(ProfilingState::NotConnected);
472 profilingState3.TransitionToState(ProfilingState::NotConnected);
473 BOOST_CHECK(profilingState3.GetCurrentState() == ProfilingState::NotConnected);
474
475 ProfilingStateMachine profilingState4(ProfilingState::NotConnected);
476 profilingState4.TransitionToState(ProfilingState::WaitingForAck);
477 BOOST_CHECK(profilingState4.GetCurrentState() == ProfilingState::WaitingForAck);
478
479 ProfilingStateMachine profilingState5(ProfilingState::WaitingForAck);
480 profilingState5.TransitionToState(ProfilingState::WaitingForAck);
481 BOOST_CHECK(profilingState5.GetCurrentState() == ProfilingState::WaitingForAck);
482
483 ProfilingStateMachine profilingState6(ProfilingState::WaitingForAck);
484 profilingState6.TransitionToState(ProfilingState::Active);
485 BOOST_CHECK(profilingState6.GetCurrentState() == ProfilingState::Active);
486
487 ProfilingStateMachine profilingState7(ProfilingState::Active);
488 profilingState7.TransitionToState(ProfilingState::NotConnected);
489 BOOST_CHECK(profilingState7.GetCurrentState() == ProfilingState::NotConnected);
490
491 ProfilingStateMachine profilingState8(ProfilingState::Active);
492 profilingState8.TransitionToState(ProfilingState::Active);
493 BOOST_CHECK(profilingState8.GetCurrentState() == ProfilingState::Active);
494
495 ProfilingStateMachine profilingState9(ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +0100496 BOOST_CHECK_THROW(profilingState9.TransitionToState(ProfilingState::WaitingForAck), armnn::Exception);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100497
498 ProfilingStateMachine profilingState10(ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +0100499 BOOST_CHECK_THROW(profilingState10.TransitionToState(ProfilingState::Active), armnn::Exception);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100500
501 ProfilingStateMachine profilingState11(ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +0100502 BOOST_CHECK_THROW(profilingState11.TransitionToState(ProfilingState::Uninitialised), armnn::Exception);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100503
504 ProfilingStateMachine profilingState12(ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +0100505 BOOST_CHECK_THROW(profilingState12.TransitionToState(ProfilingState::Active), armnn::Exception);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100506
507 ProfilingStateMachine profilingState13(ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +0100508 BOOST_CHECK_THROW(profilingState13.TransitionToState(ProfilingState::Uninitialised), armnn::Exception);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100509
510 ProfilingStateMachine profilingState14(ProfilingState::WaitingForAck);
Jim Flynn53e46992019-10-14 12:31:10 +0100511 profilingState14.TransitionToState(ProfilingState::NotConnected);
512 BOOST_CHECK(profilingState14.GetCurrentState() == ProfilingState::NotConnected);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100513
514 ProfilingStateMachine profilingState15(ProfilingState::Active);
Keith Davis3201eea2019-10-24 17:30:41 +0100515 BOOST_CHECK_THROW(profilingState15.TransitionToState(ProfilingState::Uninitialised), armnn::Exception);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100516
517 ProfilingStateMachine profilingState16(armnn::profiling::ProfilingState::Active);
Keith Davis3201eea2019-10-24 17:30:41 +0100518 BOOST_CHECK_THROW(profilingState16.TransitionToState(ProfilingState::WaitingForAck), armnn::Exception);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100519
520 ProfilingStateMachine profilingState17(ProfilingState::Uninitialised);
521
Keith Davis3201eea2019-10-24 17:30:41 +0100522 std::thread thread1(ProfilingCurrentStateThreadImpl, std::ref(profilingState17));
523 std::thread thread2(ProfilingCurrentStateThreadImpl, std::ref(profilingState17));
524 std::thread thread3(ProfilingCurrentStateThreadImpl, std::ref(profilingState17));
525 std::thread thread4(ProfilingCurrentStateThreadImpl, std::ref(profilingState17));
526 std::thread thread5(ProfilingCurrentStateThreadImpl, std::ref(profilingState17));
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100527
528 thread1.join();
529 thread2.join();
530 thread3.join();
531 thread4.join();
532 thread5.join();
533
534 BOOST_TEST((profilingState17.GetCurrentState() == ProfilingState::NotConnected));
535}
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100536
Jim Flynn8355ec92019-09-17 12:29:50 +0100537void CaptureDataWriteThreadImpl(Holder& holder, uint32_t capturePeriod, const std::vector<uint16_t>& counterIds)
Francis Murtagh68f78d82019-09-04 16:42:29 +0100538{
Finn Williams032bc742020-02-12 11:02:34 +0000539 holder.SetCaptureData(capturePeriod, counterIds, {});
Francis Murtagh68f78d82019-09-04 16:42:29 +0100540}
541
Francis Murtaghbd707162019-09-09 11:26:44 +0100542void CaptureDataReadThreadImpl(const Holder& holder, CaptureData& captureData)
Francis Murtagh68f78d82019-09-04 16:42:29 +0100543{
544 captureData = holder.GetCaptureData();
545}
546
547BOOST_AUTO_TEST_CASE(CheckCaptureDataHolder)
548{
Francis Murtaghbd707162019-09-09 11:26:44 +0100549 std::map<uint32_t, std::vector<uint16_t>> periodIdMap;
550 std::vector<uint16_t> counterIds;
Jim Flynn8355ec92019-09-17 12:29:50 +0100551 uint32_t numThreads = 10;
552 for (uint32_t i = 0; i < numThreads; ++i)
Francis Murtaghbd707162019-09-09 11:26:44 +0100553 {
554 counterIds.emplace_back(i);
555 periodIdMap.insert(std::make_pair(i, counterIds));
556 }
Francis Murtagh68f78d82019-09-04 16:42:29 +0100557
Jim Flynn8355ec92019-09-17 12:29:50 +0100558 // Verify the read and write threads set the holder correctly
559 // and retrieve the expected values
Francis Murtagh68f78d82019-09-04 16:42:29 +0100560 Holder holder;
561 BOOST_CHECK((holder.GetCaptureData()).GetCapturePeriod() == 0);
562 BOOST_CHECK(((holder.GetCaptureData()).GetCounterIds()).empty());
563
564 // Check Holder functions
Francis Murtaghbd707162019-09-09 11:26:44 +0100565 std::thread thread1(CaptureDataWriteThreadImpl, std::ref(holder), 2, std::ref(periodIdMap[2]));
Francis Murtagh68f78d82019-09-04 16:42:29 +0100566 thread1.join();
Francis Murtaghbd707162019-09-09 11:26:44 +0100567 BOOST_CHECK((holder.GetCaptureData()).GetCapturePeriod() == 2);
568 BOOST_CHECK((holder.GetCaptureData()).GetCounterIds() == periodIdMap[2]);
Jim Flynn8355ec92019-09-17 12:29:50 +0100569 // NOTE: now that we have some initial values in the holder we don't have to worry
570 // in the multi-threaded section below about a read thread accessing the holder
571 // before any write thread has gotten to it so we read period = 0, counterIds empty
572 // instead of period = 0, counterIds = {0} as will the case when write thread 0
573 // has executed.
Francis Murtagh68f78d82019-09-04 16:42:29 +0100574
575 CaptureData captureData;
576 std::thread thread2(CaptureDataReadThreadImpl, std::ref(holder), std::ref(captureData));
577 thread2.join();
Jim Flynn8355ec92019-09-17 12:29:50 +0100578 BOOST_CHECK(captureData.GetCapturePeriod() == 2);
Francis Murtaghbd707162019-09-09 11:26:44 +0100579 BOOST_CHECK(captureData.GetCounterIds() == periodIdMap[2]);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100580
Jim Flynn8355ec92019-09-17 12:29:50 +0100581 std::map<uint32_t, CaptureData> captureDataIdMap;
582 for (uint32_t i = 0; i < numThreads; ++i)
583 {
584 CaptureData perThreadCaptureData;
585 captureDataIdMap.insert(std::make_pair(i, perThreadCaptureData));
586 }
587
Francis Murtaghbd707162019-09-09 11:26:44 +0100588 std::vector<std::thread> threadsVect;
Jim Flynn8355ec92019-09-17 12:29:50 +0100589 std::vector<std::thread> readThreadsVect;
590 for (uint32_t i = 0; i < numThreads; ++i)
Francis Murtaghbd707162019-09-09 11:26:44 +0100591 {
Keith Davis3201eea2019-10-24 17:30:41 +0100592 threadsVect.emplace_back(
593 std::thread(CaptureDataWriteThreadImpl, std::ref(holder), i, std::ref(periodIdMap[i])));
Francis Murtagh06965692019-09-05 16:29:01 +0100594
Jim Flynn8355ec92019-09-17 12:29:50 +0100595 // Verify that the CaptureData goes into the thread in a virgin state
596 BOOST_CHECK(captureDataIdMap.at(i).GetCapturePeriod() == 0);
597 BOOST_CHECK(captureDataIdMap.at(i).GetCounterIds().empty());
Keith Davis3201eea2019-10-24 17:30:41 +0100598 readThreadsVect.emplace_back(
599 std::thread(CaptureDataReadThreadImpl, std::ref(holder), std::ref(captureDataIdMap.at(i))));
Francis Murtaghbd707162019-09-09 11:26:44 +0100600 }
601
Jim Flynn8355ec92019-09-17 12:29:50 +0100602 for (uint32_t i = 0; i < numThreads; ++i)
Francis Murtaghbd707162019-09-09 11:26:44 +0100603 {
604 threadsVect[i].join();
Francis Murtaghbd707162019-09-09 11:26:44 +0100605 readThreadsVect[i].join();
606 }
Francis Murtagh68f78d82019-09-04 16:42:29 +0100607
Jim Flynn8355ec92019-09-17 12:29:50 +0100608 // Look at the CaptureData that each read thread has filled
609 // the capture period it read should match the counter ids entry
610 for (uint32_t i = 0; i < numThreads; ++i)
611 {
612 CaptureData perThreadCaptureData = captureDataIdMap.at(i);
613 BOOST_CHECK(perThreadCaptureData.GetCounterIds() == periodIdMap.at(perThreadCaptureData.GetCapturePeriod()));
614 }
Matthew Bentham46d1c622019-09-13 12:45:04 +0100615}
Francis Murtagh68f78d82019-09-04 16:42:29 +0100616
Matthew Bentham46d1c622019-09-13 12:45:04 +0100617BOOST_AUTO_TEST_CASE(CaptureDataMethods)
618{
Jim Flynn8355ec92019-09-17 12:29:50 +0100619 // Check CaptureData setter and getter functions
Keith Davis3201eea2019-10-24 17:30:41 +0100620 std::vector<uint16_t> counterIds = { 42, 29, 13 };
Jim Flynn8355ec92019-09-17 12:29:50 +0100621 CaptureData captureData;
622 BOOST_CHECK(captureData.GetCapturePeriod() == 0);
623 BOOST_CHECK((captureData.GetCounterIds()).empty());
624 captureData.SetCapturePeriod(150);
625 captureData.SetCounterIds(counterIds);
626 BOOST_CHECK(captureData.GetCapturePeriod() == 150);
627 BOOST_CHECK(captureData.GetCounterIds() == counterIds);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100628
Jim Flynn8355ec92019-09-17 12:29:50 +0100629 // Check assignment operator
Francis Murtagh68f78d82019-09-04 16:42:29 +0100630 CaptureData secondCaptureData;
Francis Murtagh68f78d82019-09-04 16:42:29 +0100631
Jim Flynn8355ec92019-09-17 12:29:50 +0100632 secondCaptureData = captureData;
633 BOOST_CHECK(secondCaptureData.GetCapturePeriod() == 150);
Matthew Bentham46d1c622019-09-13 12:45:04 +0100634 BOOST_CHECK(secondCaptureData.GetCounterIds() == counterIds);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100635
636 // Check copy constructor
Jim Flynn8355ec92019-09-17 12:29:50 +0100637 CaptureData copyConstructedCaptureData(captureData);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100638
Jim Flynn8355ec92019-09-17 12:29:50 +0100639 BOOST_CHECK(copyConstructedCaptureData.GetCapturePeriod() == 150);
Matthew Bentham46d1c622019-09-13 12:45:04 +0100640 BOOST_CHECK(copyConstructedCaptureData.GetCounterIds() == counterIds);
Keith Davis02356de2019-08-26 18:28:17 +0100641}
Francis Murtagh68f78d82019-09-04 16:42:29 +0100642
Keith Davis02356de2019-08-26 18:28:17 +0100643BOOST_AUTO_TEST_CASE(CheckProfilingServiceDisabled)
644{
645 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Matteo Martincigha84edee2019-10-02 12:50:57 +0100646 ProfilingService& profilingService = ProfilingService::Instance();
647 profilingService.ResetExternalProfilingOptions(options, true);
648 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100649 profilingService.Update();
Matteo Martincigha84edee2019-10-02 12:50:57 +0100650 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis02356de2019-08-26 18:28:17 +0100651}
652
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100653BOOST_AUTO_TEST_CASE(CheckProfilingServiceCounterDirectory)
654{
655 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Matteo Martincigha84edee2019-10-02 12:50:57 +0100656 ProfilingService& profilingService = ProfilingService::Instance();
657 profilingService.ResetExternalProfilingOptions(options, true);
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100658
Matteo Martincigha84edee2019-10-02 12:50:57 +0100659 const ICounterDirectory& counterDirectory0 = profilingService.GetCounterDirectory();
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100660 BOOST_CHECK(counterDirectory0.GetCounterCount() == 0);
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100661 profilingService.Update();
662 BOOST_CHECK(counterDirectory0.GetCounterCount() == 0);
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100663
664 options.m_EnableProfiling = true;
Matteo Martincigha84edee2019-10-02 12:50:57 +0100665 profilingService.ResetExternalProfilingOptions(options);
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100666
Matteo Martincigha84edee2019-10-02 12:50:57 +0100667 const ICounterDirectory& counterDirectory1 = profilingService.GetCounterDirectory();
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100668 BOOST_CHECK(counterDirectory1.GetCounterCount() == 0);
669 profilingService.Update();
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100670 BOOST_CHECK(counterDirectory1.GetCounterCount() != 0);
Colm Donelan2ba48d22019-11-29 09:10:59 +0000671 // Reset the profiling service to stop any running thread
672 options.m_EnableProfiling = false;
673 profilingService.ResetExternalProfilingOptions(options, true);
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100674}
675
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100676BOOST_AUTO_TEST_CASE(CheckProfilingServiceCounterValues)
677{
678 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +0100679 options.m_EnableProfiling = true;
Matteo Martincigha84edee2019-10-02 12:50:57 +0100680 ProfilingService& profilingService = ProfilingService::Instance();
681 profilingService.ResetExternalProfilingOptions(options, true);
682
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100683 profilingService.Update();
Matteo Martincigha84edee2019-10-02 12:50:57 +0100684 const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
Keith Davis3201eea2019-10-24 17:30:41 +0100685 const Counters& counters = counterDirectory.GetCounters();
Matteo Martincigha84edee2019-10-02 12:50:57 +0100686 BOOST_CHECK(!counters.empty());
687
Keith Davise394bd92019-12-02 15:12:19 +0000688 // Get the UID of the first counter for testing;
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100689
690 ProfilingService* profilingServicePtr = &profilingService;
691 std::vector<std::thread> writers;
692
Keith Davis3201eea2019-10-24 17:30:41 +0100693 for (int i = 0; i < 100; ++i)
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100694 {
Matteo Martincigha84edee2019-10-02 12:50:57 +0100695 // Increment and decrement the first counter
Keith Davise394bd92019-12-02 15:12:19 +0000696 writers.push_back(std::thread(&ProfilingService::IncrementCounterValue,
697 profilingServicePtr,
698 armnn::profiling::REGISTERED_BACKENDS));
699
700 writers.push_back(std::thread(&ProfilingService::IncrementCounterValue,
701 profilingServicePtr,
702 armnn::profiling::UNREGISTERED_BACKENDS));
703
Matteo Martincigha84edee2019-10-02 12:50:57 +0100704 // Add 10 and subtract 5 from the first counter
Keith Davise394bd92019-12-02 15:12:19 +0000705 writers.push_back(std::thread(&ProfilingService::AddCounterValue,
706 profilingServicePtr,
707 armnn::profiling::INFERENCES_RUN,
708 10));
709 writers.push_back(std::thread(&ProfilingService::SubtractCounterValue,
710 profilingServicePtr,
711 armnn::profiling::INFERENCES_RUN,
712 5));
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100713 }
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100714 std::for_each(writers.begin(), writers.end(), mem_fn(&std::thread::join));
715
Matteo Martincigha84edee2019-10-02 12:50:57 +0100716 uint32_t counterValue = 0;
Keith Davise394bd92019-12-02 15:12:19 +0000717 BOOST_CHECK(counterValue ==
718 (profilingService.GetCounterValue(armnn::profiling::UNREGISTERED_BACKENDS)
719 - profilingService.GetCounterValue(armnn::profiling::REGISTERED_BACKENDS)));
720 BOOST_CHECK(profilingService.GetCounterValue(armnn::profiling::INFERENCES_RUN) == 500);
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100721
Keith Davise394bd92019-12-02 15:12:19 +0000722 BOOST_CHECK_NO_THROW(profilingService.SetCounterValue(armnn::profiling::UNREGISTERED_BACKENDS, 4));
723 BOOST_CHECK_NO_THROW(counterValue = profilingService.GetCounterValue(armnn::profiling::UNREGISTERED_BACKENDS));
724 BOOST_CHECK(counterValue == 4);
Colm Donelan2ba48d22019-11-29 09:10:59 +0000725 // Reset the profiling service to stop any running thread
726 options.m_EnableProfiling = false;
727 profilingService.ResetExternalProfilingOptions(options, true);
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100728}
729
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100730BOOST_AUTO_TEST_CASE(CheckProfilingObjectUids)
Matteo Martincighab173e92019-09-05 12:02:04 +0100731{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100732 uint16_t uid = 0;
733 BOOST_CHECK_NO_THROW(uid = GetNextUid());
734 BOOST_CHECK(uid >= 1);
735
736 uint16_t nextUid = 0;
737 BOOST_CHECK_NO_THROW(nextUid = GetNextUid());
738 BOOST_CHECK(nextUid > uid);
739
740 std::vector<uint16_t> counterUids;
Keith Davise394bd92019-12-02 15:12:19 +0000741 BOOST_CHECK_NO_THROW(counterUids = GetNextCounterUids(uid,0));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100742 BOOST_CHECK(counterUids.size() == 1);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100743
744 std::vector<uint16_t> nextCounterUids;
Keith Davise394bd92019-12-02 15:12:19 +0000745 BOOST_CHECK_NO_THROW(nextCounterUids = GetNextCounterUids(nextUid, 2));
746 BOOST_CHECK(nextCounterUids.size() == 2);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100747 BOOST_CHECK(nextCounterUids[0] > counterUids[0]);
748
749 std::vector<uint16_t> counterUidsMultiCore;
Keith Davise394bd92019-12-02 15:12:19 +0000750 uint16_t thirdUid = 4;
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100751 uint16_t numberOfCores = 13;
Keith Davise394bd92019-12-02 15:12:19 +0000752 BOOST_CHECK_NO_THROW(counterUidsMultiCore = GetNextCounterUids(thirdUid, numberOfCores));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100753 BOOST_CHECK(counterUidsMultiCore.size() == numberOfCores);
754 BOOST_CHECK(counterUidsMultiCore.front() >= nextCounterUids[0]);
Keith Davis3201eea2019-10-24 17:30:41 +0100755 for (size_t i = 1; i < numberOfCores; i++)
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100756 {
757 BOOST_CHECK(counterUidsMultiCore[i] == counterUidsMultiCore[i - 1] + 1);
758 }
759 BOOST_CHECK(counterUidsMultiCore.back() == counterUidsMultiCore.front() + numberOfCores - 1);
Matteo Martincighab173e92019-09-05 12:02:04 +0100760}
761
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100762BOOST_AUTO_TEST_CASE(CheckCounterDirectoryRegisterCategory)
Matteo Martincighab173e92019-09-05 12:02:04 +0100763{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100764 CounterDirectory counterDirectory;
Keith Davis3201eea2019-10-24 17:30:41 +0100765 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
766 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100767 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
Keith Davis3201eea2019-10-24 17:30:41 +0100768 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
Matteo Martincighab173e92019-09-05 12:02:04 +0100769
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100770 // Register a category with an invalid name
771 const Category* noCategory = nullptr;
772 BOOST_CHECK_THROW(noCategory = counterDirectory.RegisterCategory(""), armnn::InvalidArgumentException);
773 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
774 BOOST_CHECK(!noCategory);
Matteo Martincighab173e92019-09-05 12:02:04 +0100775
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100776 // Register a category with an invalid name
777 BOOST_CHECK_THROW(noCategory = counterDirectory.RegisterCategory("invalid category"),
778 armnn::InvalidArgumentException);
779 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
780 BOOST_CHECK(!noCategory);
781
782 // Register a new category
783 const std::string categoryName = "some_category";
Keith Davis3201eea2019-10-24 17:30:41 +0100784 const Category* category = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100785 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
786 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
787 BOOST_CHECK(category);
788 BOOST_CHECK(category->m_Name == categoryName);
789 BOOST_CHECK(category->m_Counters.empty());
790 BOOST_CHECK(category->m_DeviceUid == 0);
791 BOOST_CHECK(category->m_CounterSetUid == 0);
792
793 // Get the registered category
794 const Category* registeredCategory = counterDirectory.GetCategory(categoryName);
795 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
796 BOOST_CHECK(registeredCategory);
797 BOOST_CHECK(registeredCategory == category);
798
799 // Try to get a category not registered
800 const Category* notRegisteredCategory = counterDirectory.GetCategory("not_registered_category");
801 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
802 BOOST_CHECK(!notRegisteredCategory);
803
804 // Register a category already registered
805 const Category* anotherCategory = nullptr;
806 BOOST_CHECK_THROW(anotherCategory = counterDirectory.RegisterCategory(categoryName),
807 armnn::InvalidArgumentException);
808 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
809 BOOST_CHECK(!anotherCategory);
810
811 // Register a device for testing
812 const std::string deviceName = "some_device";
Keith Davis3201eea2019-10-24 17:30:41 +0100813 const Device* device = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100814 BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName));
815 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
816 BOOST_CHECK(device);
817 BOOST_CHECK(device->m_Uid >= 1);
818 BOOST_CHECK(device->m_Name == deviceName);
819 BOOST_CHECK(device->m_Cores == 0);
820
821 // Register a new category not associated to any device
822 const std::string categoryWoDeviceName = "some_category_without_device";
Keith Davis3201eea2019-10-24 17:30:41 +0100823 const Category* categoryWoDevice = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100824 BOOST_CHECK_NO_THROW(categoryWoDevice = counterDirectory.RegisterCategory(categoryWoDeviceName, 0));
825 BOOST_CHECK(counterDirectory.GetCategoryCount() == 2);
826 BOOST_CHECK(categoryWoDevice);
827 BOOST_CHECK(categoryWoDevice->m_Name == categoryWoDeviceName);
828 BOOST_CHECK(categoryWoDevice->m_Counters.empty());
829 BOOST_CHECK(categoryWoDevice->m_DeviceUid == 0);
830 BOOST_CHECK(categoryWoDevice->m_CounterSetUid == 0);
831
832 // Register a new category associated to an invalid device
833 const std::string categoryWInvalidDeviceName = "some_category_with_invalid_device";
834
835 ARMNN_NO_CONVERSION_WARN_BEGIN
836 uint16_t invalidDeviceUid = device->m_Uid + 10;
837 ARMNN_NO_CONVERSION_WARN_END
838
839 const Category* categoryWInvalidDevice = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +0100840 BOOST_CHECK_THROW(categoryWInvalidDevice =
841 counterDirectory.RegisterCategory(categoryWInvalidDeviceName, invalidDeviceUid),
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100842 armnn::InvalidArgumentException);
843 BOOST_CHECK(counterDirectory.GetCategoryCount() == 2);
844 BOOST_CHECK(!categoryWInvalidDevice);
845
846 // Register a new category associated to a valid device
847 const std::string categoryWValidDeviceName = "some_category_with_valid_device";
Keith Davis3201eea2019-10-24 17:30:41 +0100848 const Category* categoryWValidDevice = nullptr;
849 BOOST_CHECK_NO_THROW(categoryWValidDevice =
850 counterDirectory.RegisterCategory(categoryWValidDeviceName, device->m_Uid));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100851 BOOST_CHECK(counterDirectory.GetCategoryCount() == 3);
852 BOOST_CHECK(categoryWValidDevice);
853 BOOST_CHECK(categoryWValidDevice != category);
854 BOOST_CHECK(categoryWValidDevice->m_Name == categoryWValidDeviceName);
855 BOOST_CHECK(categoryWValidDevice->m_DeviceUid == device->m_Uid);
856 BOOST_CHECK(categoryWValidDevice->m_CounterSetUid == 0);
857
858 // Register a counter set for testing
859 const std::string counterSetName = "some_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +0100860 const CounterSet* counterSet = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100861 BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
862 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
863 BOOST_CHECK(counterSet);
864 BOOST_CHECK(counterSet->m_Uid >= 1);
865 BOOST_CHECK(counterSet->m_Name == counterSetName);
866 BOOST_CHECK(counterSet->m_Count == 0);
867
868 // Register a new category not associated to any counter set
869 const std::string categoryWoCounterSetName = "some_category_without_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +0100870 const Category* categoryWoCounterSet = nullptr;
871 BOOST_CHECK_NO_THROW(categoryWoCounterSet =
872 counterDirectory.RegisterCategory(categoryWoCounterSetName, armnn::EmptyOptional(), 0));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100873 BOOST_CHECK(counterDirectory.GetCategoryCount() == 4);
874 BOOST_CHECK(categoryWoCounterSet);
875 BOOST_CHECK(categoryWoCounterSet->m_Name == categoryWoCounterSetName);
876 BOOST_CHECK(categoryWoCounterSet->m_DeviceUid == 0);
877 BOOST_CHECK(categoryWoCounterSet->m_CounterSetUid == 0);
878
879 // Register a new category associated to an invalid counter set
880 const std::string categoryWInvalidCounterSetName = "some_category_with_invalid_counter_set";
881
882 ARMNN_NO_CONVERSION_WARN_BEGIN
883 uint16_t invalidCunterSetUid = counterSet->m_Uid + 10;
884 ARMNN_NO_CONVERSION_WARN_END
885
886 const Category* categoryWInvalidCounterSet = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +0100887 BOOST_CHECK_THROW(categoryWInvalidCounterSet = counterDirectory.RegisterCategory(
888 categoryWInvalidCounterSetName, armnn::EmptyOptional(), invalidCunterSetUid),
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100889 armnn::InvalidArgumentException);
890 BOOST_CHECK(counterDirectory.GetCategoryCount() == 4);
891 BOOST_CHECK(!categoryWInvalidCounterSet);
892
893 // Register a new category associated to a valid counter set
894 const std::string categoryWValidCounterSetName = "some_category_with_valid_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +0100895 const Category* categoryWValidCounterSet = nullptr;
896 BOOST_CHECK_NO_THROW(categoryWValidCounterSet = counterDirectory.RegisterCategory(
897 categoryWValidCounterSetName, armnn::EmptyOptional(), counterSet->m_Uid));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100898 BOOST_CHECK(counterDirectory.GetCategoryCount() == 5);
899 BOOST_CHECK(categoryWValidCounterSet);
900 BOOST_CHECK(categoryWValidCounterSet != category);
901 BOOST_CHECK(categoryWValidCounterSet->m_Name == categoryWValidCounterSetName);
902 BOOST_CHECK(categoryWValidCounterSet->m_DeviceUid == 0);
903 BOOST_CHECK(categoryWValidCounterSet->m_CounterSetUid == counterSet->m_Uid);
904
905 // Register a new category associated to a valid device and counter set
906 const std::string categoryWValidDeviceAndValidCounterSetName = "some_category_with_valid_device_and_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +0100907 const Category* categoryWValidDeviceAndValidCounterSet = nullptr;
908 BOOST_CHECK_NO_THROW(categoryWValidDeviceAndValidCounterSet = counterDirectory.RegisterCategory(
909 categoryWValidDeviceAndValidCounterSetName, device->m_Uid, counterSet->m_Uid));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100910 BOOST_CHECK(counterDirectory.GetCategoryCount() == 6);
911 BOOST_CHECK(categoryWValidDeviceAndValidCounterSet);
912 BOOST_CHECK(categoryWValidDeviceAndValidCounterSet != category);
913 BOOST_CHECK(categoryWValidDeviceAndValidCounterSet->m_Name == categoryWValidDeviceAndValidCounterSetName);
914 BOOST_CHECK(categoryWValidDeviceAndValidCounterSet->m_DeviceUid == device->m_Uid);
915 BOOST_CHECK(categoryWValidDeviceAndValidCounterSet->m_CounterSetUid == counterSet->m_Uid);
916}
917
918BOOST_AUTO_TEST_CASE(CheckCounterDirectoryRegisterDevice)
919{
920 CounterDirectory counterDirectory;
Keith Davis3201eea2019-10-24 17:30:41 +0100921 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
922 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100923 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
Keith Davis3201eea2019-10-24 17:30:41 +0100924 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100925
926 // Register a device with an invalid name
927 const Device* noDevice = nullptr;
928 BOOST_CHECK_THROW(noDevice = counterDirectory.RegisterDevice(""), armnn::InvalidArgumentException);
929 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
930 BOOST_CHECK(!noDevice);
931
932 // Register a device with an invalid name
933 BOOST_CHECK_THROW(noDevice = counterDirectory.RegisterDevice("inv@lid nam€"), armnn::InvalidArgumentException);
934 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
935 BOOST_CHECK(!noDevice);
936
937 // Register a new device with no cores or parent category
938 const std::string deviceName = "some_device";
Keith Davis3201eea2019-10-24 17:30:41 +0100939 const Device* device = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100940 BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName));
941 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
942 BOOST_CHECK(device);
943 BOOST_CHECK(device->m_Name == deviceName);
944 BOOST_CHECK(device->m_Uid >= 1);
945 BOOST_CHECK(device->m_Cores == 0);
946
Matteo Martincigh657ab2d2019-09-18 10:53:24 +0100947 // Try getting an unregistered device
948 const Device* unregisteredDevice = counterDirectory.GetDevice(9999);
949 BOOST_CHECK(!unregisteredDevice);
950
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100951 // Get the registered device
952 const Device* registeredDevice = counterDirectory.GetDevice(device->m_Uid);
953 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
954 BOOST_CHECK(registeredDevice);
955 BOOST_CHECK(registeredDevice == device);
956
Matteo Martincigh657ab2d2019-09-18 10:53:24 +0100957 // Register a device with the name of a device already registered
958 const Device* deviceSameName = nullptr;
959 BOOST_CHECK_THROW(deviceSameName = counterDirectory.RegisterDevice(deviceName), armnn::InvalidArgumentException);
960 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
961 BOOST_CHECK(!deviceSameName);
962
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100963 // Register a new device with cores and no parent category
964 const std::string deviceWCoresName = "some_device_with_cores";
Keith Davis3201eea2019-10-24 17:30:41 +0100965 const Device* deviceWCores = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100966 BOOST_CHECK_NO_THROW(deviceWCores = counterDirectory.RegisterDevice(deviceWCoresName, 2));
967 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
968 BOOST_CHECK(deviceWCores);
969 BOOST_CHECK(deviceWCores->m_Name == deviceWCoresName);
970 BOOST_CHECK(deviceWCores->m_Uid >= 1);
971 BOOST_CHECK(deviceWCores->m_Uid > device->m_Uid);
972 BOOST_CHECK(deviceWCores->m_Cores == 2);
973
974 // Get the registered device
975 const Device* registeredDeviceWCores = counterDirectory.GetDevice(deviceWCores->m_Uid);
976 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
977 BOOST_CHECK(registeredDeviceWCores);
978 BOOST_CHECK(registeredDeviceWCores == deviceWCores);
979 BOOST_CHECK(registeredDeviceWCores != device);
980
981 // Register a new device with cores and invalid parent category
982 const std::string deviceWCoresWInvalidParentCategoryName = "some_device_with_cores_with_invalid_parent_category";
Keith Davis3201eea2019-10-24 17:30:41 +0100983 const Device* deviceWCoresWInvalidParentCategory = nullptr;
984 BOOST_CHECK_THROW(deviceWCoresWInvalidParentCategory =
985 counterDirectory.RegisterDevice(deviceWCoresWInvalidParentCategoryName, 3, std::string("")),
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100986 armnn::InvalidArgumentException);
987 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
988 BOOST_CHECK(!deviceWCoresWInvalidParentCategory);
989
990 // Register a new device with cores and invalid parent category
991 const std::string deviceWCoresWInvalidParentCategoryName2 = "some_device_with_cores_with_invalid_parent_category2";
Keith Davis3201eea2019-10-24 17:30:41 +0100992 const Device* deviceWCoresWInvalidParentCategory2 = nullptr;
993 BOOST_CHECK_THROW(deviceWCoresWInvalidParentCategory2 = counterDirectory.RegisterDevice(
994 deviceWCoresWInvalidParentCategoryName2, 3, std::string("invalid_parent_category")),
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100995 armnn::InvalidArgumentException);
996 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
997 BOOST_CHECK(!deviceWCoresWInvalidParentCategory2);
998
999 // Register a category for testing
1000 const std::string categoryName = "some_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001001 const Category* category = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001002 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
1003 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
1004 BOOST_CHECK(category);
1005 BOOST_CHECK(category->m_Name == categoryName);
1006 BOOST_CHECK(category->m_Counters.empty());
1007 BOOST_CHECK(category->m_DeviceUid == 0);
1008 BOOST_CHECK(category->m_CounterSetUid == 0);
1009
1010 // Register a new device with cores and valid parent category
1011 const std::string deviceWCoresWValidParentCategoryName = "some_device_with_cores_with_valid_parent_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001012 const Device* deviceWCoresWValidParentCategory = nullptr;
1013 BOOST_CHECK_NO_THROW(deviceWCoresWValidParentCategory =
1014 counterDirectory.RegisterDevice(deviceWCoresWValidParentCategoryName, 4, categoryName));
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001015 BOOST_CHECK(counterDirectory.GetDeviceCount() == 3);
1016 BOOST_CHECK(deviceWCoresWValidParentCategory);
1017 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Name == deviceWCoresWValidParentCategoryName);
1018 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Uid >= 1);
1019 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Uid > device->m_Uid);
1020 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Uid > deviceWCores->m_Uid);
1021 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Cores == 4);
1022 BOOST_CHECK(category->m_DeviceUid == deviceWCoresWValidParentCategory->m_Uid);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001023
1024 // Register a device associated to a category already associated to a different device
1025 const std::string deviceSameCategoryName = "some_device_with_invalid_parent_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001026 const Device* deviceSameCategory = nullptr;
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001027 BOOST_CHECK_THROW(deviceSameCategory = counterDirectory.RegisterDevice(deviceSameCategoryName, 0, categoryName),
1028 armnn::InvalidArgumentException);
1029 BOOST_CHECK(counterDirectory.GetDeviceCount() == 3);
1030 BOOST_CHECK(!deviceSameCategory);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001031}
1032
1033BOOST_AUTO_TEST_CASE(CheckCounterDirectoryRegisterCounterSet)
1034{
1035 CounterDirectory counterDirectory;
Keith Davis3201eea2019-10-24 17:30:41 +01001036 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
1037 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001038 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
Keith Davis3201eea2019-10-24 17:30:41 +01001039 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001040
1041 // Register a counter set with an invalid name
1042 const CounterSet* noCounterSet = nullptr;
1043 BOOST_CHECK_THROW(noCounterSet = counterDirectory.RegisterCounterSet(""), armnn::InvalidArgumentException);
1044 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
1045 BOOST_CHECK(!noCounterSet);
1046
1047 // Register a counter set with an invalid name
1048 BOOST_CHECK_THROW(noCounterSet = counterDirectory.RegisterCounterSet("invalid name"),
1049 armnn::InvalidArgumentException);
1050 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
1051 BOOST_CHECK(!noCounterSet);
1052
1053 // Register a new counter set with no count or parent category
1054 const std::string counterSetName = "some_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +01001055 const CounterSet* counterSet = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001056 BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
1057 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1058 BOOST_CHECK(counterSet);
1059 BOOST_CHECK(counterSet->m_Name == counterSetName);
1060 BOOST_CHECK(counterSet->m_Uid >= 1);
1061 BOOST_CHECK(counterSet->m_Count == 0);
1062
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001063 // Try getting an unregistered counter set
1064 const CounterSet* unregisteredCounterSet = counterDirectory.GetCounterSet(9999);
1065 BOOST_CHECK(!unregisteredCounterSet);
1066
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001067 // Get the registered counter set
1068 const CounterSet* registeredCounterSet = counterDirectory.GetCounterSet(counterSet->m_Uid);
1069 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1070 BOOST_CHECK(registeredCounterSet);
1071 BOOST_CHECK(registeredCounterSet == counterSet);
1072
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001073 // Register a counter set with the name of a counter set already registered
1074 const CounterSet* counterSetSameName = nullptr;
1075 BOOST_CHECK_THROW(counterSetSameName = counterDirectory.RegisterCounterSet(counterSetName),
1076 armnn::InvalidArgumentException);
1077 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1078 BOOST_CHECK(!counterSetSameName);
1079
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001080 // Register a new counter set with count and no parent category
1081 const std::string counterSetWCountName = "some_counter_set_with_count";
Keith Davis3201eea2019-10-24 17:30:41 +01001082 const CounterSet* counterSetWCount = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001083 BOOST_CHECK_NO_THROW(counterSetWCount = counterDirectory.RegisterCounterSet(counterSetWCountName, 37));
1084 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 2);
1085 BOOST_CHECK(counterSetWCount);
1086 BOOST_CHECK(counterSetWCount->m_Name == counterSetWCountName);
1087 BOOST_CHECK(counterSetWCount->m_Uid >= 1);
1088 BOOST_CHECK(counterSetWCount->m_Uid > counterSet->m_Uid);
1089 BOOST_CHECK(counterSetWCount->m_Count == 37);
1090
1091 // Get the registered counter set
1092 const CounterSet* registeredCounterSetWCount = counterDirectory.GetCounterSet(counterSetWCount->m_Uid);
1093 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 2);
1094 BOOST_CHECK(registeredCounterSetWCount);
1095 BOOST_CHECK(registeredCounterSetWCount == counterSetWCount);
1096 BOOST_CHECK(registeredCounterSetWCount != counterSet);
1097
1098 // Register a new counter set with count and invalid parent category
1099 const std::string counterSetWCountWInvalidParentCategoryName = "some_counter_set_with_count_"
1100 "with_invalid_parent_category";
1101 const CounterSet* counterSetWCountWInvalidParentCategory = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001102 BOOST_CHECK_THROW(counterSetWCountWInvalidParentCategory = counterDirectory.RegisterCounterSet(
1103 counterSetWCountWInvalidParentCategoryName, 42, std::string("")),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001104 armnn::InvalidArgumentException);
1105 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 2);
1106 BOOST_CHECK(!counterSetWCountWInvalidParentCategory);
1107
1108 // Register a new counter set with count and invalid parent category
1109 const std::string counterSetWCountWInvalidParentCategoryName2 = "some_counter_set_with_count_"
1110 "with_invalid_parent_category2";
1111 const CounterSet* counterSetWCountWInvalidParentCategory2 = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001112 BOOST_CHECK_THROW(counterSetWCountWInvalidParentCategory2 = counterDirectory.RegisterCounterSet(
1113 counterSetWCountWInvalidParentCategoryName2, 42, std::string("invalid_parent_category")),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001114 armnn::InvalidArgumentException);
1115 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 2);
1116 BOOST_CHECK(!counterSetWCountWInvalidParentCategory2);
1117
1118 // Register a category for testing
1119 const std::string categoryName = "some_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001120 const Category* category = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001121 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
1122 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
1123 BOOST_CHECK(category);
1124 BOOST_CHECK(category->m_Name == categoryName);
1125 BOOST_CHECK(category->m_Counters.empty());
1126 BOOST_CHECK(category->m_DeviceUid == 0);
1127 BOOST_CHECK(category->m_CounterSetUid == 0);
1128
1129 // Register a new counter set with count and valid parent category
1130 const std::string counterSetWCountWValidParentCategoryName = "some_counter_set_with_count_"
1131 "with_valid_parent_category";
1132 const CounterSet* counterSetWCountWValidParentCategory = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001133 BOOST_CHECK_NO_THROW(counterSetWCountWValidParentCategory = counterDirectory.RegisterCounterSet(
1134 counterSetWCountWValidParentCategoryName, 42, categoryName));
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001135 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 3);
1136 BOOST_CHECK(counterSetWCountWValidParentCategory);
1137 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Name == counterSetWCountWValidParentCategoryName);
1138 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Uid >= 1);
1139 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Uid > counterSet->m_Uid);
1140 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Uid > counterSetWCount->m_Uid);
1141 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Count == 42);
1142 BOOST_CHECK(category->m_CounterSetUid == counterSetWCountWValidParentCategory->m_Uid);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001143
1144 // Register a counter set associated to a category already associated to a different counter set
1145 const std::string counterSetSameCategoryName = "some_counter_set_with_invalid_parent_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001146 const CounterSet* counterSetSameCategory = nullptr;
1147 BOOST_CHECK_THROW(counterSetSameCategory =
1148 counterDirectory.RegisterCounterSet(counterSetSameCategoryName, 0, categoryName),
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001149 armnn::InvalidArgumentException);
1150 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 3);
1151 BOOST_CHECK(!counterSetSameCategory);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001152}
1153
1154BOOST_AUTO_TEST_CASE(CheckCounterDirectoryRegisterCounter)
1155{
1156 CounterDirectory counterDirectory;
Keith Davis3201eea2019-10-24 17:30:41 +01001157 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
1158 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001159 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
Keith Davis3201eea2019-10-24 17:30:41 +01001160 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001161
1162 // Register a counter with an invalid parent category name
1163 const Counter* noCounter = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001164 BOOST_CHECK_THROW(noCounter =
Keith Davise394bd92019-12-02 15:12:19 +00001165 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1166 0,
1167 "",
1168 0,
1169 1,
1170 123.45f,
1171 "valid ",
1172 "name"),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001173 armnn::InvalidArgumentException);
1174 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1175 BOOST_CHECK(!noCounter);
1176
1177 // Register a counter with an invalid parent category name
Keith Davise394bd92019-12-02 15:12:19 +00001178 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1179 1,
1180 "invalid parent category",
1181 0,
1182 1,
1183 123.45f,
1184 "valid name",
1185 "valid description"),
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 class
Keith Davise394bd92019-12-02 15:12:19 +00001191 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1192 2,
1193 "valid_parent_category",
1194 2,
1195 1,
1196 123.45f,
1197 "valid "
1198 "name",
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001199 "valid description"),
1200 armnn::InvalidArgumentException);
1201 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1202 BOOST_CHECK(!noCounter);
1203
1204 // Register a counter with an invalid interpolation
Keith Davise394bd92019-12-02 15:12:19 +00001205 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1206 4,
1207 "valid_parent_category",
1208 0,
1209 3,
1210 123.45f,
1211 "valid "
1212 "name",
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001213 "valid description"),
1214 armnn::InvalidArgumentException);
1215 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1216 BOOST_CHECK(!noCounter);
1217
1218 // Register a counter with an invalid multiplier
Keith Davise394bd92019-12-02 15:12:19 +00001219 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1220 5,
1221 "valid_parent_category",
1222 0,
1223 1,
1224 .0f,
1225 "valid "
1226 "name",
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001227 "valid description"),
1228 armnn::InvalidArgumentException);
1229 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1230 BOOST_CHECK(!noCounter);
1231
1232 // Register a counter with an invalid name
Keith Davis3201eea2019-10-24 17:30:41 +01001233 BOOST_CHECK_THROW(
Keith Davise394bd92019-12-02 15:12:19 +00001234 noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1235 6,
1236 "valid_parent_category",
1237 0,
1238 1,
1239 123.45f,
1240 "",
1241 "valid description"),
Keith Davis3201eea2019-10-24 17:30:41 +01001242 armnn::InvalidArgumentException);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001243 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1244 BOOST_CHECK(!noCounter);
1245
1246 // Register a counter with an invalid name
Keith Davise394bd92019-12-02 15:12:19 +00001247 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1248 7,
1249 "valid_parent_category",
1250 0,
1251 1,
1252 123.45f,
1253 "invalid nam€",
1254 "valid description"),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001255 armnn::InvalidArgumentException);
1256 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1257 BOOST_CHECK(!noCounter);
1258
1259 // Register a counter with an invalid description
Keith Davis3201eea2019-10-24 17:30:41 +01001260 BOOST_CHECK_THROW(noCounter =
Keith Davise394bd92019-12-02 15:12:19 +00001261 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1262 8,
1263 "valid_parent_category",
1264 0,
1265 1,
1266 123.45f,
1267 "valid name",
1268 ""),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001269 armnn::InvalidArgumentException);
1270 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1271 BOOST_CHECK(!noCounter);
1272
1273 // Register a counter with an invalid description
Keith Davise394bd92019-12-02 15:12:19 +00001274 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1275 9,
1276 "valid_parent_category",
1277 0,
1278 1,
1279 123.45f,
1280 "valid "
1281 "name",
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001282 "inv@lid description"),
1283 armnn::InvalidArgumentException);
1284 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1285 BOOST_CHECK(!noCounter);
1286
1287 // Register a counter with an invalid unit2
Keith Davise394bd92019-12-02 15:12:19 +00001288 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1289 10,
1290 "valid_parent_category",
1291 0,
1292 1,
1293 123.45f,
1294 "valid name",
1295 "valid description",
1296 std::string("Mb/s2")),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001297 armnn::InvalidArgumentException);
1298 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1299 BOOST_CHECK(!noCounter);
1300
1301 // Register a counter with a non-existing parent category name
Keith Davise394bd92019-12-02 15:12:19 +00001302 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1303 11,
1304 "invalid_parent_category",
1305 0,
1306 1,
1307 123.45f,
1308 "valid name",
1309 "valid description"),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001310 armnn::InvalidArgumentException);
1311 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1312 BOOST_CHECK(!noCounter);
1313
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001314 // Try getting an unregistered counter
1315 const Counter* unregisteredCounter = counterDirectory.GetCounter(9999);
1316 BOOST_CHECK(!unregisteredCounter);
1317
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001318 // Register a category for testing
1319 const std::string categoryName = "some_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001320 const Category* category = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001321 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
1322 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
1323 BOOST_CHECK(category);
1324 BOOST_CHECK(category->m_Name == categoryName);
1325 BOOST_CHECK(category->m_Counters.empty());
1326 BOOST_CHECK(category->m_DeviceUid == 0);
1327 BOOST_CHECK(category->m_CounterSetUid == 0);
1328
1329 // Register a counter with a valid parent category name
1330 const Counter* counter = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001331 BOOST_CHECK_NO_THROW(
Keith Davise394bd92019-12-02 15:12:19 +00001332 counter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1333 12,
1334 categoryName,
1335 0,
1336 1,
1337 123.45f,
1338 "valid name",
1339 "valid description"));
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001340 BOOST_CHECK(counterDirectory.GetCounterCount() == 1);
1341 BOOST_CHECK(counter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001342 BOOST_CHECK(counter->m_MaxCounterUid == counter->m_Uid);
1343 BOOST_CHECK(counter->m_Class == 0);
1344 BOOST_CHECK(counter->m_Interpolation == 1);
1345 BOOST_CHECK(counter->m_Multiplier == 123.45f);
1346 BOOST_CHECK(counter->m_Name == "valid name");
1347 BOOST_CHECK(counter->m_Description == "valid description");
1348 BOOST_CHECK(counter->m_Units == "");
1349 BOOST_CHECK(counter->m_DeviceUid == 0);
1350 BOOST_CHECK(counter->m_CounterSetUid == 0);
1351 BOOST_CHECK(category->m_Counters.size() == 1);
1352 BOOST_CHECK(category->m_Counters.back() == counter->m_Uid);
1353
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001354 // Register a counter with a name of a counter already registered for the given parent category name
1355 const Counter* counterSameName = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001356 BOOST_CHECK_THROW(counterSameName =
Keith Davise394bd92019-12-02 15:12:19 +00001357 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1358 13,
1359 categoryName,
1360 0,
1361 0,
1362 1.0f,
1363 "valid name",
1364 "valid description",
1365 std::string("description")),
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001366 armnn::InvalidArgumentException);
1367 BOOST_CHECK(counterDirectory.GetCounterCount() == 1);
1368 BOOST_CHECK(!counterSameName);
1369
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001370 // Register a counter with a valid parent category name and units
1371 const Counter* counterWUnits = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001372 BOOST_CHECK_NO_THROW(counterWUnits = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1373 14,
1374 categoryName,
1375 0,
1376 1,
1377 123.45f,
1378 "valid name 2",
1379 "valid description",
1380 std::string("Mnnsq2"))); // Units
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001381 BOOST_CHECK(counterDirectory.GetCounterCount() == 2);
1382 BOOST_CHECK(counterWUnits);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001383 BOOST_CHECK(counterWUnits->m_Uid > counter->m_Uid);
1384 BOOST_CHECK(counterWUnits->m_MaxCounterUid == counterWUnits->m_Uid);
1385 BOOST_CHECK(counterWUnits->m_Class == 0);
1386 BOOST_CHECK(counterWUnits->m_Interpolation == 1);
1387 BOOST_CHECK(counterWUnits->m_Multiplier == 123.45f);
1388 BOOST_CHECK(counterWUnits->m_Name == "valid name 2");
1389 BOOST_CHECK(counterWUnits->m_Description == "valid description");
1390 BOOST_CHECK(counterWUnits->m_Units == "Mnnsq2");
1391 BOOST_CHECK(counterWUnits->m_DeviceUid == 0);
1392 BOOST_CHECK(counterWUnits->m_CounterSetUid == 0);
1393 BOOST_CHECK(category->m_Counters.size() == 2);
1394 BOOST_CHECK(category->m_Counters.back() == counterWUnits->m_Uid);
1395
1396 // Register a counter with a valid parent category name and not associated with a device
1397 const Counter* counterWoDevice = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001398 BOOST_CHECK_NO_THROW(counterWoDevice = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1399 26,
1400 categoryName,
1401 0,
1402 1,
1403 123.45f,
1404 "valid name 3",
1405 "valid description",
1406 armnn::EmptyOptional(),// Units
1407 armnn::EmptyOptional(),// Number of cores
1408 0)); // Device UID
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001409 BOOST_CHECK(counterDirectory.GetCounterCount() == 3);
1410 BOOST_CHECK(counterWoDevice);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001411 BOOST_CHECK(counterWoDevice->m_Uid > counter->m_Uid);
1412 BOOST_CHECK(counterWoDevice->m_MaxCounterUid == counterWoDevice->m_Uid);
1413 BOOST_CHECK(counterWoDevice->m_Class == 0);
1414 BOOST_CHECK(counterWoDevice->m_Interpolation == 1);
1415 BOOST_CHECK(counterWoDevice->m_Multiplier == 123.45f);
1416 BOOST_CHECK(counterWoDevice->m_Name == "valid name 3");
1417 BOOST_CHECK(counterWoDevice->m_Description == "valid description");
1418 BOOST_CHECK(counterWoDevice->m_Units == "");
1419 BOOST_CHECK(counterWoDevice->m_DeviceUid == 0);
1420 BOOST_CHECK(counterWoDevice->m_CounterSetUid == 0);
1421 BOOST_CHECK(category->m_Counters.size() == 3);
1422 BOOST_CHECK(category->m_Counters.back() == counterWoDevice->m_Uid);
1423
1424 // Register a counter with a valid parent category name and associated to an invalid device
Keith Davise394bd92019-12-02 15:12:19 +00001425 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1426 15,
1427 categoryName,
1428 0,
1429 1,
1430 123.45f,
1431 "valid name 4",
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001432 "valid description",
Keith Davis3201eea2019-10-24 17:30:41 +01001433 armnn::EmptyOptional(), // Units
1434 armnn::EmptyOptional(), // Number of cores
1435 100), // Device UID
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001436 armnn::InvalidArgumentException);
1437 BOOST_CHECK(counterDirectory.GetCounterCount() == 3);
1438 BOOST_CHECK(!noCounter);
1439
1440 // Register a device for testing
1441 const std::string deviceName = "some_device";
Keith Davis3201eea2019-10-24 17:30:41 +01001442 const Device* device = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001443 BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName));
1444 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
1445 BOOST_CHECK(device);
1446 BOOST_CHECK(device->m_Name == deviceName);
1447 BOOST_CHECK(device->m_Uid >= 1);
1448 BOOST_CHECK(device->m_Cores == 0);
1449
1450 // Register a counter with a valid parent category name and associated to a device
1451 const Counter* counterWDevice = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001452 BOOST_CHECK_NO_THROW(counterWDevice = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1453 16,
1454 categoryName,
1455 0,
1456 1,
1457 123.45f,
1458 "valid name 5",
1459 std::string("valid description"),
Keith Davis3201eea2019-10-24 17:30:41 +01001460 armnn::EmptyOptional(), // Units
1461 armnn::EmptyOptional(), // Number of cores
1462 device->m_Uid)); // Device UID
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001463 BOOST_CHECK(counterDirectory.GetCounterCount() == 4);
1464 BOOST_CHECK(counterWDevice);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001465 BOOST_CHECK(counterWDevice->m_Uid > counter->m_Uid);
1466 BOOST_CHECK(counterWDevice->m_MaxCounterUid == counterWDevice->m_Uid);
1467 BOOST_CHECK(counterWDevice->m_Class == 0);
1468 BOOST_CHECK(counterWDevice->m_Interpolation == 1);
1469 BOOST_CHECK(counterWDevice->m_Multiplier == 123.45f);
1470 BOOST_CHECK(counterWDevice->m_Name == "valid name 5");
1471 BOOST_CHECK(counterWDevice->m_Description == "valid description");
1472 BOOST_CHECK(counterWDevice->m_Units == "");
1473 BOOST_CHECK(counterWDevice->m_DeviceUid == device->m_Uid);
1474 BOOST_CHECK(counterWDevice->m_CounterSetUid == 0);
1475 BOOST_CHECK(category->m_Counters.size() == 4);
1476 BOOST_CHECK(category->m_Counters.back() == counterWDevice->m_Uid);
1477
1478 // Register a counter with a valid parent category name and not associated with a counter set
1479 const Counter* counterWoCounterSet = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001480 BOOST_CHECK_NO_THROW(counterWoCounterSet = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1481 17,
1482 categoryName,
1483 0,
1484 1,
1485 123.45f,
1486 "valid name 6",
1487 "valid description",
1488 armnn::EmptyOptional(),// Units
1489 armnn::EmptyOptional(),// No of cores
1490 armnn::EmptyOptional(),// Device UID
1491 0)); // CounterSet UID
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001492 BOOST_CHECK(counterDirectory.GetCounterCount() == 5);
1493 BOOST_CHECK(counterWoCounterSet);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001494 BOOST_CHECK(counterWoCounterSet->m_Uid > counter->m_Uid);
1495 BOOST_CHECK(counterWoCounterSet->m_MaxCounterUid == counterWoCounterSet->m_Uid);
1496 BOOST_CHECK(counterWoCounterSet->m_Class == 0);
1497 BOOST_CHECK(counterWoCounterSet->m_Interpolation == 1);
1498 BOOST_CHECK(counterWoCounterSet->m_Multiplier == 123.45f);
1499 BOOST_CHECK(counterWoCounterSet->m_Name == "valid name 6");
1500 BOOST_CHECK(counterWoCounterSet->m_Description == "valid description");
1501 BOOST_CHECK(counterWoCounterSet->m_Units == "");
1502 BOOST_CHECK(counterWoCounterSet->m_DeviceUid == 0);
1503 BOOST_CHECK(counterWoCounterSet->m_CounterSetUid == 0);
1504 BOOST_CHECK(category->m_Counters.size() == 5);
1505 BOOST_CHECK(category->m_Counters.back() == counterWoCounterSet->m_Uid);
1506
1507 // Register a counter with a valid parent category name and associated to an invalid counter set
Keith Davise394bd92019-12-02 15:12:19 +00001508 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1509 18,
1510 categoryName,
1511 0,
1512 1,
1513 123.45f,
1514 "valid ",
1515 "name 7",
1516 std::string("valid description"),
Keith Davis3201eea2019-10-24 17:30:41 +01001517 armnn::EmptyOptional(), // Units
1518 armnn::EmptyOptional(), // Number of cores
Keith Davise394bd92019-12-02 15:12:19 +00001519 100), // Counter set UID
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001520 armnn::InvalidArgumentException);
1521 BOOST_CHECK(counterDirectory.GetCounterCount() == 5);
1522 BOOST_CHECK(!noCounter);
1523
1524 // Register a counter with a valid parent category name and with a given number of cores
1525 const Counter* counterWNumberOfCores = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001526 uint16_t numberOfCores = 15;
1527 BOOST_CHECK_NO_THROW(counterWNumberOfCores = counterDirectory.RegisterCounter(
Keith Davise394bd92019-12-02 15:12:19 +00001528 armnn::profiling::BACKEND_ID, 50,
Keith Davis3201eea2019-10-24 17:30:41 +01001529 categoryName, 0, 1, 123.45f, "valid name 8", "valid description",
1530 armnn::EmptyOptional(), // Units
1531 numberOfCores, // Number of cores
1532 armnn::EmptyOptional(), // Device UID
1533 armnn::EmptyOptional())); // Counter set UID
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001534 BOOST_CHECK(counterDirectory.GetCounterCount() == 20);
1535 BOOST_CHECK(counterWNumberOfCores);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001536 BOOST_CHECK(counterWNumberOfCores->m_Uid > counter->m_Uid);
1537 BOOST_CHECK(counterWNumberOfCores->m_MaxCounterUid == counterWNumberOfCores->m_Uid + numberOfCores - 1);
1538 BOOST_CHECK(counterWNumberOfCores->m_Class == 0);
1539 BOOST_CHECK(counterWNumberOfCores->m_Interpolation == 1);
1540 BOOST_CHECK(counterWNumberOfCores->m_Multiplier == 123.45f);
1541 BOOST_CHECK(counterWNumberOfCores->m_Name == "valid name 8");
1542 BOOST_CHECK(counterWNumberOfCores->m_Description == "valid description");
1543 BOOST_CHECK(counterWNumberOfCores->m_Units == "");
1544 BOOST_CHECK(counterWNumberOfCores->m_DeviceUid == 0);
1545 BOOST_CHECK(counterWNumberOfCores->m_CounterSetUid == 0);
1546 BOOST_CHECK(category->m_Counters.size() == 20);
Keith Davis3201eea2019-10-24 17:30:41 +01001547 for (size_t i = 0; i < numberOfCores; i++)
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001548 {
1549 BOOST_CHECK(category->m_Counters[category->m_Counters.size() - numberOfCores + i] ==
1550 counterWNumberOfCores->m_Uid + i);
1551 }
1552
1553 // Register a multi-core device for testing
1554 const std::string multiCoreDeviceName = "some_multi_core_device";
Keith Davis3201eea2019-10-24 17:30:41 +01001555 const Device* multiCoreDevice = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001556 BOOST_CHECK_NO_THROW(multiCoreDevice = counterDirectory.RegisterDevice(multiCoreDeviceName, 4));
1557 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
1558 BOOST_CHECK(multiCoreDevice);
1559 BOOST_CHECK(multiCoreDevice->m_Name == multiCoreDeviceName);
1560 BOOST_CHECK(multiCoreDevice->m_Uid >= 1);
1561 BOOST_CHECK(multiCoreDevice->m_Cores == 4);
1562
1563 // Register a counter with a valid parent category name and associated to the multi-core device
1564 const Counter* counterWMultiCoreDevice = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001565 BOOST_CHECK_NO_THROW(counterWMultiCoreDevice = counterDirectory.RegisterCounter(
Keith Davise394bd92019-12-02 15:12:19 +00001566 armnn::profiling::BACKEND_ID, 19, categoryName, 0, 1,
1567 123.45f, "valid name 9", "valid description",
Keith Davis3201eea2019-10-24 17:30:41 +01001568 armnn::EmptyOptional(), // Units
1569 armnn::EmptyOptional(), // Number of cores
1570 multiCoreDevice->m_Uid, // Device UID
1571 armnn::EmptyOptional())); // Counter set UID
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001572 BOOST_CHECK(counterDirectory.GetCounterCount() == 24);
1573 BOOST_CHECK(counterWMultiCoreDevice);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001574 BOOST_CHECK(counterWMultiCoreDevice->m_Uid > counter->m_Uid);
1575 BOOST_CHECK(counterWMultiCoreDevice->m_MaxCounterUid ==
1576 counterWMultiCoreDevice->m_Uid + multiCoreDevice->m_Cores - 1);
1577 BOOST_CHECK(counterWMultiCoreDevice->m_Class == 0);
1578 BOOST_CHECK(counterWMultiCoreDevice->m_Interpolation == 1);
1579 BOOST_CHECK(counterWMultiCoreDevice->m_Multiplier == 123.45f);
1580 BOOST_CHECK(counterWMultiCoreDevice->m_Name == "valid name 9");
1581 BOOST_CHECK(counterWMultiCoreDevice->m_Description == "valid description");
1582 BOOST_CHECK(counterWMultiCoreDevice->m_Units == "");
1583 BOOST_CHECK(counterWMultiCoreDevice->m_DeviceUid == multiCoreDevice->m_Uid);
1584 BOOST_CHECK(counterWMultiCoreDevice->m_CounterSetUid == 0);
1585 BOOST_CHECK(category->m_Counters.size() == 24);
Keith Davis3201eea2019-10-24 17:30:41 +01001586 for (size_t i = 0; i < 4; i++)
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001587 {
1588 BOOST_CHECK(category->m_Counters[category->m_Counters.size() - 4 + i] == counterWMultiCoreDevice->m_Uid + i);
1589 }
1590
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001591 // Register a multi-core device associate to a parent category for testing
1592 const std::string multiCoreDeviceNameWParentCategory = "some_multi_core_device_with_parent_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001593 const Device* multiCoreDeviceWParentCategory = nullptr;
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001594 BOOST_CHECK_NO_THROW(multiCoreDeviceWParentCategory =
Keith Davis3201eea2019-10-24 17:30:41 +01001595 counterDirectory.RegisterDevice(multiCoreDeviceNameWParentCategory, 2, categoryName));
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001596 BOOST_CHECK(counterDirectory.GetDeviceCount() == 3);
1597 BOOST_CHECK(multiCoreDeviceWParentCategory);
1598 BOOST_CHECK(multiCoreDeviceWParentCategory->m_Name == multiCoreDeviceNameWParentCategory);
1599 BOOST_CHECK(multiCoreDeviceWParentCategory->m_Uid >= 1);
1600 BOOST_CHECK(multiCoreDeviceWParentCategory->m_Cores == 2);
1601
1602 // Register a counter with a valid parent category name and getting the number of cores of the multi-core device
1603 // associated to that category
1604 const Counter* counterWMultiCoreDeviceWParentCategory = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001605 BOOST_CHECK_NO_THROW(counterWMultiCoreDeviceWParentCategory =
1606 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1607 100,
1608 categoryName,
1609 0,
1610 1,
1611 123.45f,
1612 "valid name 10",
1613 "valid description",
1614 armnn::EmptyOptional(),// Units
1615 armnn::EmptyOptional(),// Number of cores
1616 armnn::EmptyOptional(),// Device UID
1617 armnn::EmptyOptional()));// Counter set UID
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001618 BOOST_CHECK(counterDirectory.GetCounterCount() == 26);
1619 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001620 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Uid > counter->m_Uid);
1621 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_MaxCounterUid ==
1622 counterWMultiCoreDeviceWParentCategory->m_Uid + multiCoreDeviceWParentCategory->m_Cores - 1);
1623 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Class == 0);
1624 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Interpolation == 1);
1625 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Multiplier == 123.45f);
1626 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Name == "valid name 10");
1627 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Description == "valid description");
1628 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Units == "");
1629 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_DeviceUid == 0);
1630 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_CounterSetUid == 0);
1631 BOOST_CHECK(category->m_Counters.size() == 26);
Keith Davis3201eea2019-10-24 17:30:41 +01001632 for (size_t i = 0; i < 2; i++)
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001633 {
1634 BOOST_CHECK(category->m_Counters[category->m_Counters.size() - 2 + i] ==
1635 counterWMultiCoreDeviceWParentCategory->m_Uid + i);
1636 }
1637
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001638 // Register a counter set for testing
1639 const std::string counterSetName = "some_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +01001640 const CounterSet* counterSet = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001641 BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
1642 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1643 BOOST_CHECK(counterSet);
1644 BOOST_CHECK(counterSet->m_Name == counterSetName);
1645 BOOST_CHECK(counterSet->m_Uid >= 1);
1646 BOOST_CHECK(counterSet->m_Count == 0);
1647
1648 // Register a counter with a valid parent category name and associated to a counter set
1649 const Counter* counterWCounterSet = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001650 BOOST_CHECK_NO_THROW(counterWCounterSet = counterDirectory.RegisterCounter(
Keith Davise394bd92019-12-02 15:12:19 +00001651 armnn::profiling::BACKEND_ID, 300,
Keith Davis3201eea2019-10-24 17:30:41 +01001652 categoryName, 0, 1, 123.45f, "valid name 11", "valid description",
1653 armnn::EmptyOptional(), // Units
1654 0, // Number of cores
1655 armnn::EmptyOptional(), // Device UID
1656 counterSet->m_Uid)); // Counter set UID
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001657 BOOST_CHECK(counterDirectory.GetCounterCount() == 27);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001658 BOOST_CHECK(counterWCounterSet);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001659 BOOST_CHECK(counterWCounterSet->m_Uid > counter->m_Uid);
1660 BOOST_CHECK(counterWCounterSet->m_MaxCounterUid == counterWCounterSet->m_Uid);
1661 BOOST_CHECK(counterWCounterSet->m_Class == 0);
1662 BOOST_CHECK(counterWCounterSet->m_Interpolation == 1);
1663 BOOST_CHECK(counterWCounterSet->m_Multiplier == 123.45f);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001664 BOOST_CHECK(counterWCounterSet->m_Name == "valid name 11");
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001665 BOOST_CHECK(counterWCounterSet->m_Description == "valid description");
1666 BOOST_CHECK(counterWCounterSet->m_Units == "");
1667 BOOST_CHECK(counterWCounterSet->m_DeviceUid == 0);
1668 BOOST_CHECK(counterWCounterSet->m_CounterSetUid == counterSet->m_Uid);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001669 BOOST_CHECK(category->m_Counters.size() == 27);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001670 BOOST_CHECK(category->m_Counters.back() == counterWCounterSet->m_Uid);
1671
1672 // Register a counter with a valid parent category name and associated to a device and a counter set
1673 const Counter* counterWDeviceWCounterSet = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001674 BOOST_CHECK_NO_THROW(counterWDeviceWCounterSet = counterDirectory.RegisterCounter(
Keith Davise394bd92019-12-02 15:12:19 +00001675 armnn::profiling::BACKEND_ID, 23,
Keith Davis3201eea2019-10-24 17:30:41 +01001676 categoryName, 0, 1, 123.45f, "valid name 12", "valid description",
1677 armnn::EmptyOptional(), // Units
1678 1, // Number of cores
1679 device->m_Uid, // Device UID
1680 counterSet->m_Uid)); // Counter set UID
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001681 BOOST_CHECK(counterDirectory.GetCounterCount() == 28);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001682 BOOST_CHECK(counterWDeviceWCounterSet);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001683 BOOST_CHECK(counterWDeviceWCounterSet->m_Uid > counter->m_Uid);
1684 BOOST_CHECK(counterWDeviceWCounterSet->m_MaxCounterUid == counterWDeviceWCounterSet->m_Uid);
1685 BOOST_CHECK(counterWDeviceWCounterSet->m_Class == 0);
1686 BOOST_CHECK(counterWDeviceWCounterSet->m_Interpolation == 1);
1687 BOOST_CHECK(counterWDeviceWCounterSet->m_Multiplier == 123.45f);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001688 BOOST_CHECK(counterWDeviceWCounterSet->m_Name == "valid name 12");
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001689 BOOST_CHECK(counterWDeviceWCounterSet->m_Description == "valid description");
1690 BOOST_CHECK(counterWDeviceWCounterSet->m_Units == "");
1691 BOOST_CHECK(counterWDeviceWCounterSet->m_DeviceUid == device->m_Uid);
1692 BOOST_CHECK(counterWDeviceWCounterSet->m_CounterSetUid == counterSet->m_Uid);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001693 BOOST_CHECK(category->m_Counters.size() == 28);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001694 BOOST_CHECK(category->m_Counters.back() == counterWDeviceWCounterSet->m_Uid);
1695
1696 // Register another category for testing
1697 const std::string anotherCategoryName = "some_other_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001698 const Category* anotherCategory = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001699 BOOST_CHECK_NO_THROW(anotherCategory = counterDirectory.RegisterCategory(anotherCategoryName));
1700 BOOST_CHECK(counterDirectory.GetCategoryCount() == 2);
1701 BOOST_CHECK(anotherCategory);
1702 BOOST_CHECK(anotherCategory != category);
1703 BOOST_CHECK(anotherCategory->m_Name == anotherCategoryName);
1704 BOOST_CHECK(anotherCategory->m_Counters.empty());
1705 BOOST_CHECK(anotherCategory->m_DeviceUid == 0);
1706 BOOST_CHECK(anotherCategory->m_CounterSetUid == 0);
1707
1708 // Register a counter to the other category
1709 const Counter* anotherCounter = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001710 BOOST_CHECK_NO_THROW(anotherCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID, 24,
1711 anotherCategoryName, 1, 0, .00043f,
Keith Davis3201eea2019-10-24 17:30:41 +01001712 "valid name", "valid description",
1713 armnn::EmptyOptional(), // Units
1714 armnn::EmptyOptional(), // Number of cores
1715 device->m_Uid, // Device UID
1716 counterSet->m_Uid)); // Counter set UID
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001717 BOOST_CHECK(counterDirectory.GetCounterCount() == 29);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001718 BOOST_CHECK(anotherCounter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001719 BOOST_CHECK(anotherCounter->m_MaxCounterUid == anotherCounter->m_Uid);
1720 BOOST_CHECK(anotherCounter->m_Class == 1);
1721 BOOST_CHECK(anotherCounter->m_Interpolation == 0);
1722 BOOST_CHECK(anotherCounter->m_Multiplier == .00043f);
1723 BOOST_CHECK(anotherCounter->m_Name == "valid name");
1724 BOOST_CHECK(anotherCounter->m_Description == "valid description");
1725 BOOST_CHECK(anotherCounter->m_Units == "");
1726 BOOST_CHECK(anotherCounter->m_DeviceUid == device->m_Uid);
1727 BOOST_CHECK(anotherCounter->m_CounterSetUid == counterSet->m_Uid);
1728 BOOST_CHECK(anotherCategory->m_Counters.size() == 1);
1729 BOOST_CHECK(anotherCategory->m_Counters.back() == anotherCounter->m_Uid);
Matteo Martincighab173e92019-09-05 12:02:04 +01001730}
1731
Ferran Balaguer1b941722019-08-28 16:57:18 +01001732BOOST_AUTO_TEST_CASE(CounterSelectionCommandHandlerParseData)
1733{
1734 using boost::numeric_cast;
1735
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01001736 ProfilingStateMachine profilingStateMachine;
1737
Ferran Balaguer1b941722019-08-28 16:57:18 +01001738 class TestCaptureThread : public IPeriodicCounterCapture
1739 {
Keith Davis3201eea2019-10-24 17:30:41 +01001740 void Start() override
1741 {}
1742 void Stop() override
1743 {}
Ferran Balaguer1b941722019-08-28 16:57:18 +01001744 };
1745
Matteo Martincighe8485382019-10-10 14:08:21 +01001746 class TestReadCounterValues : public IReadCounterValues
1747 {
Keith Davis3201eea2019-10-24 17:30:41 +01001748 bool IsCounterRegistered(uint16_t counterUid) const override
1749 {
Derek Lamberti1dd75b32019-12-10 21:23:23 +00001750 boost::ignore_unused(counterUid);
Keith Davis3201eea2019-10-24 17:30:41 +01001751 return true;
1752 }
1753 uint16_t GetCounterCount() const override
1754 {
1755 return 0;
1756 }
1757 uint32_t GetCounterValue(uint16_t counterUid) const override
1758 {
Derek Lamberti1dd75b32019-12-10 21:23:23 +00001759 boost::ignore_unused(counterUid);
Keith Davis3201eea2019-10-24 17:30:41 +01001760 return 0;
1761 }
Matteo Martincighe8485382019-10-10 14:08:21 +01001762 };
Jim Flynn397043f2019-10-17 17:37:10 +01001763 const uint32_t familyId = 0;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001764 const uint32_t packetId = 0x40000;
1765
1766 uint32_t version = 1;
Finn Williams032bc742020-02-12 11:02:34 +00001767 const std::unordered_map<armnn::BackendId,
1768 std::shared_ptr<armnn::profiling::IBackendProfilingContext>> backendProfilingContext;
1769 CounterIdMap counterIdMap;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001770 Holder holder;
1771 TestCaptureThread captureThread;
Matteo Martincighe8485382019-10-10 14:08:21 +01001772 TestReadCounterValues readCounterValues;
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001773 MockBufferManager mockBuffer(512);
Sadik Armagan3896b472020-02-10 12:24:15 +00001774 SendCounterPacket sendCounterPacket(mockBuffer);
1775 SendThread sendThread(profilingStateMachine, mockBuffer, sendCounterPacket);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001776
1777 uint32_t sizeOfUint32 = numeric_cast<uint32_t>(sizeof(uint32_t));
1778 uint32_t sizeOfUint16 = numeric_cast<uint32_t>(sizeof(uint16_t));
1779
1780 // Data with period and counters
Colm Donelan02705242019-11-14 14:19:07 +00001781 uint32_t period1 = armnn::LOWEST_CAPTURE_PERIOD;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001782 uint32_t dataLength1 = 8;
Keith Davis3201eea2019-10-24 17:30:41 +01001783 uint32_t offset = 0;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001784
Matteo Martincigh67ef2a52019-10-10 13:29:02 +01001785 std::unique_ptr<unsigned char[]> uniqueData1 = std::make_unique<unsigned char[]>(dataLength1);
Keith Davis3201eea2019-10-24 17:30:41 +01001786 unsigned char* data1 = reinterpret_cast<unsigned char*>(uniqueData1.get());
FinnWilliamsArma0c78712019-09-16 12:06:47 +01001787
Ferran Balaguer1b941722019-08-28 16:57:18 +01001788 WriteUint32(data1, offset, period1);
1789 offset += sizeOfUint32;
1790 WriteUint16(data1, offset, 4000);
1791 offset += sizeOfUint16;
1792 WriteUint16(data1, offset, 5000);
1793
FinnWilliamsArma0c78712019-09-16 12:06:47 +01001794 Packet packetA(packetId, dataLength1, uniqueData1);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001795
Finn Williams032bc742020-02-12 11:02:34 +00001796 PeriodicCounterSelectionCommandHandler commandHandler(familyId, packetId, version, backendProfilingContext,
1797 counterIdMap, holder, 10000u, captureThread,
Keith Davis3201eea2019-10-24 17:30:41 +01001798 readCounterValues, sendCounterPacket, profilingStateMachine);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001799
Matteo Martincighe8485382019-10-10 14:08:21 +01001800 profilingStateMachine.TransitionToState(ProfilingState::Uninitialised);
1801 BOOST_CHECK_THROW(commandHandler(packetA), armnn::RuntimeException);
1802 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
1803 BOOST_CHECK_THROW(commandHandler(packetA), armnn::RuntimeException);
1804 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
1805 BOOST_CHECK_THROW(commandHandler(packetA), armnn::RuntimeException);
1806 profilingStateMachine.TransitionToState(ProfilingState::Active);
1807 BOOST_CHECK_NO_THROW(commandHandler(packetA));
1808
1809 const std::vector<uint16_t> counterIdsA = holder.GetCaptureData().GetCounterIds();
Ferran Balaguer1b941722019-08-28 16:57:18 +01001810
1811 BOOST_TEST(holder.GetCaptureData().GetCapturePeriod() == period1);
Matteo Martincighe8485382019-10-10 14:08:21 +01001812 BOOST_TEST(counterIdsA.size() == 2);
1813 BOOST_TEST(counterIdsA[0] == 4000);
1814 BOOST_TEST(counterIdsA[1] == 5000);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001815
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001816 auto readBuffer = mockBuffer.GetReadableBuffer();
Ferran Balaguer1b941722019-08-28 16:57:18 +01001817
1818 offset = 0;
1819
1820 uint32_t headerWord0 = ReadUint32(readBuffer, offset);
1821 offset += sizeOfUint32;
1822 uint32_t headerWord1 = ReadUint32(readBuffer, offset);
1823 offset += sizeOfUint32;
1824 uint32_t period = ReadUint32(readBuffer, offset);
1825
Colm Donelan02705242019-11-14 14:19:07 +00001826 BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family
1827 BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 4); // packet id
1828 BOOST_TEST(headerWord1 == 8); // data length
1829 BOOST_TEST(period == armnn::LOWEST_CAPTURE_PERIOD); // capture period
Ferran Balaguer1b941722019-08-28 16:57:18 +01001830
1831 uint16_t counterId = 0;
1832 offset += sizeOfUint32;
1833 counterId = ReadUint16(readBuffer, offset);
1834 BOOST_TEST(counterId == 4000);
1835 offset += sizeOfUint16;
1836 counterId = ReadUint16(readBuffer, offset);
1837 BOOST_TEST(counterId == 5000);
1838
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001839 mockBuffer.MarkRead(readBuffer);
1840
Ferran Balaguer1b941722019-08-28 16:57:18 +01001841 // Data with period only
Colm Donelan02705242019-11-14 14:19:07 +00001842 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 +01001843 uint32_t dataLength2 = 4;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001844
Matteo Martincigh67ef2a52019-10-10 13:29:02 +01001845 std::unique_ptr<unsigned char[]> uniqueData2 = std::make_unique<unsigned char[]>(dataLength2);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001846
FinnWilliamsArma0c78712019-09-16 12:06:47 +01001847 WriteUint32(reinterpret_cast<unsigned char*>(uniqueData2.get()), 0, period2);
1848
1849 Packet packetB(packetId, dataLength2, uniqueData2);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001850
1851 commandHandler(packetB);
1852
Matteo Martincighe8485382019-10-10 14:08:21 +01001853 const std::vector<uint16_t> counterIdsB = holder.GetCaptureData().GetCounterIds();
Ferran Balaguer1b941722019-08-28 16:57:18 +01001854
Colm Donelan02705242019-11-14 14:19:07 +00001855 // Value should have been pulled up from 9000 to LOWEST_CAPTURE_PERIOD.
1856 BOOST_TEST(holder.GetCaptureData().GetCapturePeriod() == armnn::LOWEST_CAPTURE_PERIOD);
Matteo Martincighe8485382019-10-10 14:08:21 +01001857 BOOST_TEST(counterIdsB.size() == 0);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001858
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001859 readBuffer = mockBuffer.GetReadableBuffer();
Ferran Balaguer1b941722019-08-28 16:57:18 +01001860
1861 offset = 0;
1862
1863 headerWord0 = ReadUint32(readBuffer, offset);
1864 offset += sizeOfUint32;
1865 headerWord1 = ReadUint32(readBuffer, offset);
1866 offset += sizeOfUint32;
1867 period = ReadUint32(readBuffer, offset);
1868
Colm Donelan02705242019-11-14 14:19:07 +00001869 BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family
1870 BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 4); // packet id
1871 BOOST_TEST(headerWord1 == 4); // data length
1872 BOOST_TEST(period == armnn::LOWEST_CAPTURE_PERIOD); // capture period
Ferran Balaguer1b941722019-08-28 16:57:18 +01001873}
1874
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001875BOOST_AUTO_TEST_CASE(CheckConnectionAcknowledged)
1876{
1877 using boost::numeric_cast;
1878
Keith Davis3201eea2019-10-24 17:30:41 +01001879 const uint32_t packetFamilyId = 0;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001880 const uint32_t connectionPacketId = 0x10000;
Keith Davis3201eea2019-10-24 17:30:41 +01001881 const uint32_t version = 1;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001882
1883 uint32_t sizeOfUint32 = numeric_cast<uint32_t>(sizeof(uint32_t));
1884 uint32_t sizeOfUint16 = numeric_cast<uint32_t>(sizeof(uint16_t));
1885
1886 // Data with period and counters
Keith Davis3201eea2019-10-24 17:30:41 +01001887 uint32_t period1 = 10;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001888 uint32_t dataLength1 = 8;
Keith Davis3201eea2019-10-24 17:30:41 +01001889 uint32_t offset = 0;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001890
Matteo Martincigh67ef2a52019-10-10 13:29:02 +01001891 std::unique_ptr<unsigned char[]> uniqueData1 = std::make_unique<unsigned char[]>(dataLength1);
Keith Davis3201eea2019-10-24 17:30:41 +01001892 unsigned char* data1 = reinterpret_cast<unsigned char*>(uniqueData1.get());
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001893
1894 WriteUint32(data1, offset, period1);
1895 offset += sizeOfUint32;
1896 WriteUint16(data1, offset, 4000);
1897 offset += sizeOfUint16;
1898 WriteUint16(data1, offset, 5000);
1899
1900 Packet packetA(connectionPacketId, dataLength1, uniqueData1);
1901
1902 ProfilingStateMachine profilingState(ProfilingState::Uninitialised);
1903 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01001904 CounterDirectory counterDirectory;
1905 MockBufferManager mockBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00001906 SendCounterPacket sendCounterPacket(mockBuffer);
1907 SendThread sendThread(profilingState, mockBuffer, sendCounterPacket);
Matteo Martincighcdfb9412019-11-08 11:23:06 +00001908 SendTimelinePacket sendTimelinePacket(mockBuffer);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001909
Keith Davis3201eea2019-10-24 17:30:41 +01001910 ConnectionAcknowledgedCommandHandler commandHandler(packetFamilyId, connectionPacketId, version, counterDirectory,
Matteo Martincighcdfb9412019-11-08 11:23:06 +00001911 sendCounterPacket, sendTimelinePacket, profilingState);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001912
1913 // command handler received packet on ProfilingState::Uninitialised
1914 BOOST_CHECK_THROW(commandHandler(packetA), armnn::Exception);
1915
1916 profilingState.TransitionToState(ProfilingState::NotConnected);
1917 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::NotConnected);
1918 // command handler received packet on ProfilingState::NotConnected
1919 BOOST_CHECK_THROW(commandHandler(packetA), armnn::Exception);
1920
1921 profilingState.TransitionToState(ProfilingState::WaitingForAck);
1922 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::WaitingForAck);
1923 // command handler received packet on ProfilingState::WaitingForAck
Matteo Martincighd0613b52019-10-09 16:47:04 +01001924 BOOST_CHECK_NO_THROW(commandHandler(packetA));
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001925 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::Active);
1926
1927 // command handler received packet on ProfilingState::Active
Matteo Martincighd0613b52019-10-09 16:47:04 +01001928 BOOST_CHECK_NO_THROW(commandHandler(packetA));
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001929 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::Active);
1930
1931 // command handler received different packet
1932 const uint32_t differentPacketId = 0x40000;
1933 Packet packetB(differentPacketId, dataLength1, uniqueData1);
Matteo Martincighd0613b52019-10-09 16:47:04 +01001934 profilingState.TransitionToState(ProfilingState::NotConnected);
1935 profilingState.TransitionToState(ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01001936 ConnectionAcknowledgedCommandHandler differentCommandHandler(packetFamilyId, differentPacketId, version,
Matteo Martincighcdfb9412019-11-08 11:23:06 +00001937 counterDirectory, sendCounterPacket,
1938 sendTimelinePacket, profilingState);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001939 BOOST_CHECK_THROW(differentCommandHandler(packetB), armnn::Exception);
1940}
1941
Teresa Charlin9bab4962019-09-06 12:28:35 +01001942BOOST_AUTO_TEST_CASE(CheckSocketProfilingConnection)
1943{
1944 // Check that creating a SocketProfilingConnection results in an exception as the Gator UDS doesn't exist.
1945 BOOST_CHECK_THROW(new SocketProfilingConnection(), armnn::Exception);
1946}
1947
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001948BOOST_AUTO_TEST_CASE(SwTraceIsValidCharTest)
1949{
1950 // Only ASCII 7-bit encoding supported
1951 for (unsigned char c = 0; c < 128; c++)
1952 {
1953 BOOST_CHECK(SwTraceCharPolicy::IsValidChar(c));
1954 }
1955
1956 // Not ASCII
1957 for (unsigned char c = 255; c >= 128; c++)
1958 {
1959 BOOST_CHECK(!SwTraceCharPolicy::IsValidChar(c));
1960 }
1961}
1962
1963BOOST_AUTO_TEST_CASE(SwTraceIsValidNameCharTest)
1964{
1965 // Only alpha-numeric and underscore ASCII 7-bit encoding supported
1966 const unsigned char validChars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
1967 for (unsigned char i = 0; i < sizeof(validChars) / sizeof(validChars[0]) - 1; i++)
1968 {
1969 BOOST_CHECK(SwTraceNameCharPolicy::IsValidChar(validChars[i]));
1970 }
1971
1972 // Non alpha-numeric chars
1973 for (unsigned char c = 0; c < 48; c++)
1974 {
1975 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1976 }
1977 for (unsigned char c = 58; c < 65; c++)
1978 {
1979 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1980 }
1981 for (unsigned char c = 91; c < 95; c++)
1982 {
1983 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1984 }
1985 for (unsigned char c = 96; c < 97; c++)
1986 {
1987 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1988 }
1989 for (unsigned char c = 123; c < 128; c++)
1990 {
1991 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1992 }
1993
1994 // Not ASCII
1995 for (unsigned char c = 255; c >= 128; c++)
1996 {
1997 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1998 }
1999}
2000
2001BOOST_AUTO_TEST_CASE(IsValidSwTraceStringTest)
2002{
2003 // Valid SWTrace strings
2004 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>(""));
2005 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("_"));
2006 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("0123"));
2007 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("valid_string"));
2008 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("VALID_string_456"));
2009 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>(" "));
2010 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("valid string"));
2011 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("!$%"));
2012 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("valid|\\~string#123"));
2013
2014 // Invalid SWTrace strings
2015 BOOST_CHECK(!IsValidSwTraceString<SwTraceCharPolicy>("€£"));
2016 BOOST_CHECK(!IsValidSwTraceString<SwTraceCharPolicy>("invalid‡string"));
2017 BOOST_CHECK(!IsValidSwTraceString<SwTraceCharPolicy>("12Ž34"));
2018}
2019
2020BOOST_AUTO_TEST_CASE(IsValidSwTraceNameStringTest)
2021{
2022 // Valid SWTrace name strings
2023 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>(""));
2024 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>("_"));
2025 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>("0123"));
2026 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>("valid_string"));
2027 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>("VALID_string_456"));
2028
2029 // Invalid SWTrace name strings
2030 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>(" "));
2031 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("invalid string"));
2032 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("!$%"));
2033 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("invalid|\\~string#123"));
2034 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("€£"));
2035 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("invalid‡string"));
2036 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("12Ž34"));
2037}
2038
2039template <typename SwTracePolicy>
2040void StringToSwTraceStringTestHelper(const std::string& testString, std::vector<uint32_t> buffer, size_t expectedSize)
2041{
2042 // Convert the test string to a SWTrace string
2043 BOOST_CHECK(StringToSwTraceString<SwTracePolicy>(testString, buffer));
2044
2045 // The buffer must contain at least the length of the string
2046 BOOST_CHECK(!buffer.empty());
2047
2048 // The buffer must be of the expected size (in words)
2049 BOOST_CHECK(buffer.size() == expectedSize);
2050
2051 // The first word of the byte must be the length of the string including the null-terminator
2052 BOOST_CHECK(buffer[0] == testString.size() + 1);
2053
2054 // The contents of the buffer must match the test string
2055 BOOST_CHECK(std::memcmp(testString.data(), buffer.data() + 1, testString.size()) == 0);
2056
2057 // The buffer must include the null-terminator at the end of the string
2058 size_t nullTerminatorIndex = sizeof(uint32_t) + testString.size();
Keith Davis3201eea2019-10-24 17:30:41 +01002059 BOOST_CHECK(reinterpret_cast<unsigned char*>(buffer.data())[nullTerminatorIndex] == '\0');
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002060}
2061
2062BOOST_AUTO_TEST_CASE(StringToSwTraceStringTest)
2063{
2064 std::vector<uint32_t> buffer;
2065
2066 // Valid SWTrace strings (expected size in words)
2067 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("", buffer, 2);
2068 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("_", buffer, 2);
2069 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("0123", buffer, 3);
2070 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("valid_string", buffer, 5);
2071 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("VALID_string_456", buffer, 6);
2072 StringToSwTraceStringTestHelper<SwTraceCharPolicy>(" ", buffer, 2);
2073 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("valid string", buffer, 5);
2074 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("!$%", buffer, 2);
2075 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("valid|\\~string#123", buffer, 6);
2076
2077 // Invalid SWTrace strings
2078 BOOST_CHECK(!StringToSwTraceString<SwTraceCharPolicy>("€£", buffer));
2079 BOOST_CHECK(buffer.empty());
2080 BOOST_CHECK(!StringToSwTraceString<SwTraceCharPolicy>("invalid‡string", buffer));
2081 BOOST_CHECK(buffer.empty());
2082 BOOST_CHECK(!StringToSwTraceString<SwTraceCharPolicy>("12Ž34", buffer));
2083 BOOST_CHECK(buffer.empty());
2084}
2085
2086BOOST_AUTO_TEST_CASE(StringToSwTraceNameStringTest)
2087{
2088 std::vector<uint32_t> buffer;
2089
2090 // Valid SWTrace namestrings (expected size in words)
2091 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("", buffer, 2);
2092 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("_", buffer, 2);
2093 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("0123", buffer, 3);
2094 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("valid_string", buffer, 5);
2095 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("VALID_string_456", buffer, 6);
2096
2097 // Invalid SWTrace namestrings
2098 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>(" ", buffer));
2099 BOOST_CHECK(buffer.empty());
2100 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("invalid string", buffer));
2101 BOOST_CHECK(buffer.empty());
2102 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("!$%", buffer));
2103 BOOST_CHECK(buffer.empty());
2104 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("invalid|\\~string#123", buffer));
2105 BOOST_CHECK(buffer.empty());
2106 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("€£", buffer));
2107 BOOST_CHECK(buffer.empty());
2108 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("invalid‡string", buffer));
2109 BOOST_CHECK(buffer.empty());
2110 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("12Ž34", buffer));
2111 BOOST_CHECK(buffer.empty());
2112}
2113
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002114BOOST_AUTO_TEST_CASE(CheckPeriodicCounterCaptureThread)
2115{
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002116 class CaptureReader : public IReadCounterValues
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002117 {
2118 public:
Finn Williamsf4d59a62019-10-14 15:55:18 +01002119 CaptureReader(uint16_t counterSize)
2120 {
Keith Davis3201eea2019-10-24 17:30:41 +01002121 for (uint16_t i = 0; i < counterSize; ++i)
Finn Williamsf4d59a62019-10-14 15:55:18 +01002122 {
2123 m_Data[i] = 0;
2124 }
2125 m_CounterSize = counterSize;
2126 }
2127 //not used
Matteo Martincighe8485382019-10-10 14:08:21 +01002128 bool IsCounterRegistered(uint16_t counterUid) const override
2129 {
Derek Lamberti1dd75b32019-12-10 21:23:23 +00002130 boost::ignore_unused(counterUid);
Finn Williamsf4d59a62019-10-14 15:55:18 +01002131 return false;
Matteo Martincighe8485382019-10-10 14:08:21 +01002132 }
2133
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002134 uint16_t GetCounterCount() const override
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002135 {
Finn Williamsf4d59a62019-10-14 15:55:18 +01002136 return m_CounterSize;
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002137 }
2138
Matteo Martincighe8485382019-10-10 14:08:21 +01002139 uint32_t GetCounterValue(uint16_t counterUid) const override
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002140 {
Keith Davis3201eea2019-10-24 17:30:41 +01002141 if (counterUid > m_CounterSize)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002142 {
Finn Williamsf4d59a62019-10-14 15:55:18 +01002143 BOOST_FAIL("Invalid counter Uid");
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002144 }
Matteo Martincighe8485382019-10-10 14:08:21 +01002145 return m_Data.at(counterUid).load();
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002146 }
2147
Matteo Martincighe8485382019-10-10 14:08:21 +01002148 void SetCounterValue(uint16_t counterUid, uint32_t value)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002149 {
Keith Davis3201eea2019-10-24 17:30:41 +01002150 if (counterUid > m_CounterSize)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002151 {
Finn Williamsf4d59a62019-10-14 15:55:18 +01002152 BOOST_FAIL("Invalid counter Uid");
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002153 }
Finn Williamsf4d59a62019-10-14 15:55:18 +01002154 m_Data.at(counterUid).store(value);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002155 }
2156
2157 private:
Matteo Martincighe8485382019-10-10 14:08:21 +01002158 std::unordered_map<uint16_t, std::atomic<uint32_t>> m_Data;
Finn Williamsf4d59a62019-10-14 15:55:18 +01002159 uint16_t m_CounterSize;
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002160 };
2161
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002162 ProfilingStateMachine profilingStateMachine;
2163
Finn Williams032bc742020-02-12 11:02:34 +00002164 const std::unordered_map<armnn::BackendId,
2165 std::shared_ptr<armnn::profiling::IBackendProfilingContext>> backendProfilingContext;
2166 CounterIdMap counterIdMap;
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002167 Holder data;
2168 std::vector<uint16_t> captureIds1 = { 0, 1 };
2169 std::vector<uint16_t> captureIds2;
2170
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002171 MockBufferManager mockBuffer(512);
Sadik Armagan3896b472020-02-10 12:24:15 +00002172 SendCounterPacket sendCounterPacket(mockBuffer);
2173 SendThread sendThread(profilingStateMachine, mockBuffer, sendCounterPacket);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002174
2175 std::vector<uint16_t> counterIds;
Finn Williamsf4d59a62019-10-14 15:55:18 +01002176 CaptureReader captureReader(2);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002177
Keith Davis3201eea2019-10-24 17:30:41 +01002178 unsigned int valueA = 10;
2179 unsigned int valueB = 15;
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002180 unsigned int numSteps = 5;
2181
Finn Williams032bc742020-02-12 11:02:34 +00002182 PeriodicCounterCapture periodicCounterCapture(std::ref(data), std::ref(sendCounterPacket), captureReader,
2183 counterIdMap, backendProfilingContext);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002184
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002185 for (unsigned int i = 0; i < numSteps; ++i)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002186 {
Finn Williams032bc742020-02-12 11:02:34 +00002187 data.SetCaptureData(1, captureIds1, {});
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002188 captureReader.SetCounterValue(0, valueA * (i + 1));
2189 captureReader.SetCounterValue(1, valueB * (i + 1));
2190
2191 periodicCounterCapture.Start();
Finn Williamsf4d59a62019-10-14 15:55:18 +01002192 periodicCounterCapture.Stop();
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002193 }
2194
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002195 auto buffer = mockBuffer.GetReadableBuffer();
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002196
2197 uint32_t headerWord0 = ReadUint32(buffer, 0);
2198 uint32_t headerWord1 = ReadUint32(buffer, 4);
2199
Jim Flynnfc365622019-12-04 10:07:20 +00002200 BOOST_TEST(((headerWord0 >> 26) & 0x0000003F) == 3); // packet family
Keith Davis3201eea2019-10-24 17:30:41 +01002201 BOOST_TEST(((headerWord0 >> 19) & 0x0000007F) == 0); // packet class
2202 BOOST_TEST(((headerWord0 >> 16) & 0x00000007) == 0); // packet type
Matteo Martincigh8d9590e2019-10-15 09:35:29 +01002203 BOOST_TEST(headerWord1 == 20);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002204
Keith Davis3201eea2019-10-24 17:30:41 +01002205 uint32_t offset = 16;
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002206 uint16_t readIndex = ReadUint16(buffer, offset);
2207 BOOST_TEST(0 == readIndex);
2208
2209 offset += 2;
2210 uint32_t readValue = ReadUint32(buffer, offset);
2211 BOOST_TEST((valueA * numSteps) == readValue);
2212
2213 offset += 4;
2214 readIndex = ReadUint16(buffer, offset);
2215 BOOST_TEST(1 == readIndex);
2216
2217 offset += 2;
2218 readValue = ReadUint32(buffer, offset);
2219 BOOST_TEST((valueB * numSteps) == readValue);
2220}
2221
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002222BOOST_AUTO_TEST_CASE(RequestCounterDirectoryCommandHandlerTest1)
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002223{
2224 using boost::numeric_cast;
2225
Jim Flynn397043f2019-10-17 17:37:10 +01002226 const uint32_t familyId = 0;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002227 const uint32_t packetId = 3;
Keith Davis3201eea2019-10-24 17:30:41 +01002228 const uint32_t version = 1;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002229 ProfilingStateMachine profilingStateMachine;
2230 CounterDirectory counterDirectory;
Matteo Martincigh9723d022019-11-13 10:56:41 +00002231 MockBufferManager mockBuffer1(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002232 SendCounterPacket sendCounterPacket(mockBuffer1);
2233 SendThread sendThread(profilingStateMachine, mockBuffer1, sendCounterPacket);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002234 MockBufferManager mockBuffer2(1024);
2235 SendTimelinePacket sendTimelinePacket(mockBuffer2);
Keith Davis3201eea2019-10-24 17:30:41 +01002236 RequestCounterDirectoryCommandHandler commandHandler(familyId, packetId, version, counterDirectory,
Matteo Martincigh9723d022019-11-13 10:56:41 +00002237 sendCounterPacket, sendTimelinePacket, profilingStateMachine);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002238
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002239 const uint32_t wrongPacketId = 47;
Keith Davis3201eea2019-10-24 17:30:41 +01002240 const uint32_t wrongHeader = (wrongPacketId & 0x000003FF) << 16;
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002241
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002242 Packet wrongPacket(wrongHeader);
2243
2244 profilingStateMachine.TransitionToState(ProfilingState::Uninitialised);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002245 BOOST_CHECK_THROW(commandHandler(wrongPacket), armnn::RuntimeException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002246 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002247 BOOST_CHECK_THROW(commandHandler(wrongPacket), armnn::RuntimeException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002248 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002249 BOOST_CHECK_THROW(commandHandler(wrongPacket), armnn::RuntimeException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002250 profilingStateMachine.TransitionToState(ProfilingState::Active);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002251 BOOST_CHECK_THROW(commandHandler(wrongPacket), armnn::InvalidArgumentException); // Wrong packet
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002252
2253 const uint32_t rightHeader = (packetId & 0x000003FF) << 16;
2254
2255 Packet rightPacket(rightHeader);
2256
Matteo Martincigh9723d022019-11-13 10:56:41 +00002257 BOOST_CHECK_NO_THROW(commandHandler(rightPacket)); // Right packet
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002258
Matteo Martincigh9723d022019-11-13 10:56:41 +00002259 auto readBuffer1 = mockBuffer1.GetReadableBuffer();
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002260
Matteo Martincigh9723d022019-11-13 10:56:41 +00002261 uint32_t header1Word0 = ReadUint32(readBuffer1, 0);
2262 uint32_t header1Word1 = ReadUint32(readBuffer1, 4);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002263
Matteo Martincigh9723d022019-11-13 10:56:41 +00002264 // Counter directory packet
2265 BOOST_TEST(((header1Word0 >> 26) & 0x0000003F) == 0); // packet family
2266 BOOST_TEST(((header1Word0 >> 16) & 0x000003FF) == 2); // packet id
2267 BOOST_TEST(header1Word1 == 24); // data length
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002268
Matteo Martincigh9723d022019-11-13 10:56:41 +00002269 uint32_t bodyHeader1Word0 = ReadUint32(readBuffer1, 8);
2270 uint16_t deviceRecordCount = numeric_cast<uint16_t>(bodyHeader1Word0 >> 16);
2271 BOOST_TEST(deviceRecordCount == 0); // device_records_count
2272
2273 auto readBuffer2 = mockBuffer2.GetReadableBuffer();
2274
2275 uint32_t header2Word0 = ReadUint32(readBuffer2, 0);
2276 uint32_t header2Word1 = ReadUint32(readBuffer2, 4);
2277
2278 // Timeline message directory packet
2279 BOOST_TEST(((header2Word0 >> 26) & 0x0000003F) == 1); // packet family
2280 BOOST_TEST(((header2Word0 >> 16) & 0x000003FF) == 0); // packet id
2281 BOOST_TEST(header2Word1 == 419); // data length
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002282}
2283
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002284BOOST_AUTO_TEST_CASE(RequestCounterDirectoryCommandHandlerTest2)
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002285{
2286 using boost::numeric_cast;
2287
Jim Flynn397043f2019-10-17 17:37:10 +01002288 const uint32_t familyId = 0;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002289 const uint32_t packetId = 3;
Keith Davis3201eea2019-10-24 17:30:41 +01002290 const uint32_t version = 1;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002291 ProfilingStateMachine profilingStateMachine;
2292 CounterDirectory counterDirectory;
Matteo Martincigh9723d022019-11-13 10:56:41 +00002293 MockBufferManager mockBuffer1(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002294 SendCounterPacket sendCounterPacket(mockBuffer1);
2295 SendThread sendThread(profilingStateMachine, mockBuffer1, sendCounterPacket);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002296 MockBufferManager mockBuffer2(1024);
2297 SendTimelinePacket sendTimelinePacket(mockBuffer2);
Keith Davis3201eea2019-10-24 17:30:41 +01002298 RequestCounterDirectoryCommandHandler commandHandler(familyId, packetId, version, counterDirectory,
Matteo Martincigh9723d022019-11-13 10:56:41 +00002299 sendCounterPacket, sendTimelinePacket, profilingStateMachine);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002300 const uint32_t header = (packetId & 0x000003FF) << 16;
2301 Packet packet(header);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002302
Matteo Martincigh9723d022019-11-13 10:56:41 +00002303 const Device* device = counterDirectory.RegisterDevice("deviceA", 1);
2304 BOOST_CHECK(device != nullptr);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002305 const CounterSet* counterSet = counterDirectory.RegisterCounterSet("countersetA");
Matteo Martincigh9723d022019-11-13 10:56:41 +00002306 BOOST_CHECK(counterSet != nullptr);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002307 counterDirectory.RegisterCategory("categoryA", device->m_Uid, counterSet->m_Uid);
Keith Davise394bd92019-12-02 15:12:19 +00002308 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID, 24,
2309 "categoryA", 0, 1, 2.0f, "counterA", "descA");
2310 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID, 25,
2311 "categoryA", 1, 1, 3.0f, "counterB", "descB");
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002312
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002313 profilingStateMachine.TransitionToState(ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002314 BOOST_CHECK_THROW(commandHandler(packet), armnn::RuntimeException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002315 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002316 BOOST_CHECK_THROW(commandHandler(packet), armnn::RuntimeException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002317 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002318 BOOST_CHECK_THROW(commandHandler(packet), armnn::RuntimeException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002319 profilingStateMachine.TransitionToState(ProfilingState::Active);
2320 BOOST_CHECK_NO_THROW(commandHandler(packet));
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002321
Matteo Martincigh9723d022019-11-13 10:56:41 +00002322 auto readBuffer1 = mockBuffer1.GetReadableBuffer();
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002323
Matteo Martincigh9723d022019-11-13 10:56:41 +00002324 uint32_t header1Word0 = ReadUint32(readBuffer1, 0);
2325 uint32_t header1Word1 = ReadUint32(readBuffer1, 4);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002326
Matteo Martincigh9723d022019-11-13 10:56:41 +00002327 BOOST_TEST(((header1Word0 >> 26) & 0x0000003F) == 0); // packet family
2328 BOOST_TEST(((header1Word0 >> 16) & 0x000003FF) == 2); // packet id
2329 BOOST_TEST(header1Word1 == 240); // data length
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002330
Matteo Martincigh9723d022019-11-13 10:56:41 +00002331 uint32_t bodyHeader1Word0 = ReadUint32(readBuffer1, 8);
2332 uint32_t bodyHeader1Word1 = ReadUint32(readBuffer1, 12);
2333 uint32_t bodyHeader1Word2 = ReadUint32(readBuffer1, 16);
2334 uint32_t bodyHeader1Word3 = ReadUint32(readBuffer1, 20);
2335 uint32_t bodyHeader1Word4 = ReadUint32(readBuffer1, 24);
2336 uint32_t bodyHeader1Word5 = ReadUint32(readBuffer1, 28);
2337 uint16_t deviceRecordCount = numeric_cast<uint16_t>(bodyHeader1Word0 >> 16);
2338 uint16_t counterSetRecordCount = numeric_cast<uint16_t>(bodyHeader1Word2 >> 16);
2339 uint16_t categoryRecordCount = numeric_cast<uint16_t>(bodyHeader1Word4 >> 16);
2340 BOOST_TEST(deviceRecordCount == 1); // device_records_count
2341 BOOST_TEST(bodyHeader1Word1 == 0); // device_records_pointer_table_offset
2342 BOOST_TEST(counterSetRecordCount == 1); // counter_set_count
2343 BOOST_TEST(bodyHeader1Word3 == 4); // counter_set_pointer_table_offset
2344 BOOST_TEST(categoryRecordCount == 1); // categories_count
2345 BOOST_TEST(bodyHeader1Word5 == 8); // categories_pointer_table_offset
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002346
Matteo Martincigh9723d022019-11-13 10:56:41 +00002347 uint32_t deviceRecordOffset = ReadUint32(readBuffer1, 32);
Keith Davis3201eea2019-10-24 17:30:41 +01002348 BOOST_TEST(deviceRecordOffset == 0);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002349
Matteo Martincigh9723d022019-11-13 10:56:41 +00002350 uint32_t counterSetRecordOffset = ReadUint32(readBuffer1, 36);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002351 BOOST_TEST(counterSetRecordOffset == 20);
2352
Matteo Martincigh9723d022019-11-13 10:56:41 +00002353 uint32_t categoryRecordOffset = ReadUint32(readBuffer1, 40);
Keith Davis3201eea2019-10-24 17:30:41 +01002354 BOOST_TEST(categoryRecordOffset == 44);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002355
2356 auto readBuffer2 = mockBuffer2.GetReadableBuffer();
2357
2358 uint32_t header2Word0 = ReadUint32(readBuffer2, 0);
2359 uint32_t header2Word1 = ReadUint32(readBuffer2, 4);
2360
2361 // Timeline message directory packet
2362 BOOST_TEST(((header2Word0 >> 26) & 0x0000003F) == 1); // packet family
2363 BOOST_TEST(((header2Word0 >> 16) & 0x000003FF) == 0); // packet id
2364 BOOST_TEST(header2Word1 == 419); // data length
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002365}
2366
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002367BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodConnectionAcknowledgedPacket)
2368{
Matteo Martincighd0613b52019-10-09 16:47:04 +01002369 // Swap the profiling connection factory in the profiling service instance with our mock one
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002370 SwapProfilingConnectionFactoryHelper helper;
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002371
2372 // Calculate the size of a Stream Metadata packet
Keith Davis3201eea2019-10-24 17:30:41 +01002373 std::string processName = GetProcessName().substr(0, 60);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002374 unsigned int processNameSize = processName.empty() ? 0 : boost::numeric_cast<unsigned int>(processName.size()) + 1;
2375 unsigned int streamMetadataPacketsize = 118 + processNameSize;
2376
Jim Flynn53e46992019-10-14 12:31:10 +01002377 // Reset the profiling service to the uninitialized state
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002378 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002379 options.m_EnableProfiling = true;
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002380 ProfilingService& profilingService = ProfilingService::Instance();
2381 profilingService.ResetExternalProfilingOptions(options, true);
2382
2383 // Bring the profiling service to the "WaitingForAck" state
2384 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002385 profilingService.Update(); // Initialize the counter directory
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002386 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002387 profilingService.Update(); // Create the profiling connection
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002388
Matteo Martincighd0613b52019-10-09 16:47:04 +01002389 // Get the mock profiling connection
2390 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2391 BOOST_CHECK(mockProfilingConnection);
2392
Matteo Martincighe8485382019-10-10 14:08:21 +01002393 // Remove the packets received so far
2394 mockProfilingConnection->Clear();
2395
2396 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002397 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002398
2399 // Wait for the Stream Metadata packet to be sent
Finn Williams09ad6f92019-12-19 17:05:18 +00002400 BOOST_CHECK(helper.WaitForPacketsSent(
2401 mockProfilingConnection, PacketType::StreamMetaData, streamMetadataPacketsize) >= 1);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002402
2403 // Write a valid "Connection Acknowledged" packet into the mock profiling connection, to simulate a valid
2404 // reply from an external profiling service
2405
2406 // Connection Acknowledged Packet header (word 0, word 1 is always zero):
2407 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2408 // 16:25 [10] packet_id: Packet identifier, value 0b0000000001
2409 // 8:15 [8] reserved: Reserved, value 0b00000000
2410 // 0:7 [8] reserved: Reserved, value 0b00000000
2411 uint32_t packetFamily = 0;
2412 uint32_t packetId = 1;
Keith Davis3201eea2019-10-24 17:30:41 +01002413 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002414
Matteo Martincighd0613b52019-10-09 16:47:04 +01002415 // Create the Connection Acknowledged Packet
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002416 Packet connectionAcknowledgedPacket(header);
2417
2418 // Write the packet to the mock profiling connection
2419 mockProfilingConnection->WritePacket(std::move(connectionAcknowledgedPacket));
2420
Colm Donelan2ba48d22019-11-29 09:10:59 +00002421 // Wait for the counter directory packet to ensure the ConnectionAcknowledgedCommandHandler has run.
Finn Williams09ad6f92019-12-19 17:05:18 +00002422 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::CounterDirectory) == 1);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002423
2424 // The Connection Acknowledged Command Handler should have updated the profiling state accordingly
2425 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincighd0613b52019-10-09 16:47:04 +01002426
2427 // Reset the profiling service to stop any running thread
2428 options.m_EnableProfiling = false;
2429 profilingService.ResetExternalProfilingOptions(options, true);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002430}
2431
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002432BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodRequestCounterDirectoryPacket)
2433{
2434 // Swap the profiling connection factory in the profiling service instance with our mock one
2435 SwapProfilingConnectionFactoryHelper helper;
2436
2437 // Reset the profiling service to the uninitialized state
2438 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002439 options.m_EnableProfiling = true;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002440 ProfilingService& profilingService = ProfilingService::Instance();
2441 profilingService.ResetExternalProfilingOptions(options, true);
2442
2443 // Bring the profiling service to the "Active" state
2444 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002445 profilingService.Update(); // Initialize the counter directory
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002446 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002447 profilingService.Update(); // Create the profiling connection
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002448 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002449 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002450
Colm Donelan2ba48d22019-11-29 09:10:59 +00002451 // Get the mock profiling connection
2452 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2453 BOOST_CHECK(mockProfilingConnection);
2454
Matteo Martincighe8485382019-10-10 14:08:21 +01002455 // Force the profiling service to the "Active" state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002456 helper.ForceTransitionToState(ProfilingState::Active);
2457 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2458
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002459 // Write a valid "Request Counter Directory" packet into the mock profiling connection, to simulate a valid
2460 // reply from an external profiling service
2461
2462 // Request Counter Directory packet header (word 0, word 1 is always zero):
2463 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2464 // 16:25 [10] packet_id: Packet identifier, value 0b0000000011
2465 // 8:15 [8] reserved: Reserved, value 0b00000000
2466 // 0:7 [8] reserved: Reserved, value 0b00000000
2467 uint32_t packetFamily = 0;
2468 uint32_t packetId = 3;
Keith Davis3201eea2019-10-24 17:30:41 +01002469 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002470
2471 // Create the Request Counter Directory packet
2472 Packet requestCounterDirectoryPacket(header);
2473
2474 // Write the packet to the mock profiling connection
2475 mockProfilingConnection->WritePacket(std::move(requestCounterDirectoryPacket));
2476
Finn Williams09ad6f92019-12-19 17:05:18 +00002477 // Expecting one CounterDirectory Packet of length 656
2478 // and one TimelineMessageDirectory packet of length 427
2479 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::CounterDirectory, 656) == 1);
2480 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::TimelineMessageDirectory, 427) == 1);
Matteo Martincighe8485382019-10-10 14:08:21 +01002481
2482 // The Request Counter Directory Command Handler should not have updated the profiling state
2483 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2484
2485 // Reset the profiling service to stop any running thread
2486 options.m_EnableProfiling = false;
2487 profilingService.ResetExternalProfilingOptions(options, true);
2488}
2489
Matteo Martincighe8485382019-10-10 14:08:21 +01002490BOOST_AUTO_TEST_CASE(CheckProfilingServiceBadPeriodicCounterSelectionPacketInvalidCounterUid)
2491{
Matteo Martincighe8485382019-10-10 14:08:21 +01002492 // Swap the profiling connection factory in the profiling service instance with our mock one
2493 SwapProfilingConnectionFactoryHelper helper;
2494
2495 // Reset the profiling service to the uninitialized state
2496 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002497 options.m_EnableProfiling = true;
Matteo Martincighe8485382019-10-10 14:08:21 +01002498 ProfilingService& profilingService = ProfilingService::Instance();
2499 profilingService.ResetExternalProfilingOptions(options, true);
2500
2501 // Bring the profiling service to the "Active" state
2502 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002503 profilingService.Update(); // Initialize the counter directory
Matteo Martincighe8485382019-10-10 14:08:21 +01002504 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002505 profilingService.Update(); // Create the profiling connection
Matteo Martincighe8485382019-10-10 14:08:21 +01002506 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002507 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002508
Colm Donelan2ba48d22019-11-29 09:10:59 +00002509 // Get the mock profiling connection
2510 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2511 BOOST_CHECK(mockProfilingConnection);
2512
Matteo Martincighe8485382019-10-10 14:08:21 +01002513 // Force the profiling service to the "Active" state
2514 helper.ForceTransitionToState(ProfilingState::Active);
2515 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2516
Matteo Martincighe8485382019-10-10 14:08:21 +01002517 // Remove the packets received so far
2518 mockProfilingConnection->Clear();
2519
2520 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2521 // external profiling service
2522
2523 // Periodic Counter Selection packet header:
2524 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2525 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2526 // 8:15 [8] reserved: Reserved, value 0b00000000
2527 // 0:7 [8] reserved: Reserved, value 0b00000000
2528 uint32_t packetFamily = 0;
2529 uint32_t packetId = 4;
Keith Davis3201eea2019-10-24 17:30:41 +01002530 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincighe8485382019-10-10 14:08:21 +01002531
Keith Davis3201eea2019-10-24 17:30:41 +01002532 uint32_t capturePeriod = 123456; // Some capture period (microseconds)
Matteo Martincighe8485382019-10-10 14:08:21 +01002533
2534 // Get the first valid counter UID
2535 const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
Keith Davis3201eea2019-10-24 17:30:41 +01002536 const Counters& counters = counterDirectory.GetCounters();
Matteo Martincighe8485382019-10-10 14:08:21 +01002537 BOOST_CHECK(counters.size() > 1);
Keith Davis3201eea2019-10-24 17:30:41 +01002538 uint16_t counterUidA = counters.begin()->first; // First valid counter UID
2539 uint16_t counterUidB = 9999; // Second invalid counter UID
Matteo Martincighe8485382019-10-10 14:08:21 +01002540
2541 uint32_t length = 8;
2542
2543 auto data = std::make_unique<unsigned char[]>(length);
2544 WriteUint32(data.get(), 0, capturePeriod);
2545 WriteUint16(data.get(), 4, counterUidA);
2546 WriteUint16(data.get(), 6, counterUidB);
2547
2548 // Create the Periodic Counter Selection packet
Keith Davis3201eea2019-10-24 17:30:41 +01002549 Packet periodicCounterSelectionPacket(header, length, data); // Length > 0, this will start the Period Counter
2550 // Capture thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002551
2552 // Write the packet to the mock profiling connection
2553 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2554
Finn Williams09ad6f92019-12-19 17:05:18 +00002555 // Expecting one Periodic Counter Selection packet of length 14
2556 // and at least one Periodic Counter Capture packet of length 22
2557 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterSelection, 14) == 1);
2558 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterCapture, 22) >= 1);
Matteo Martincighe8485382019-10-10 14:08:21 +01002559
2560 // The Periodic Counter Selection Handler should not have updated the profiling state
2561 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2562
2563 // Reset the profiling service to stop any running thread
2564 options.m_EnableProfiling = false;
2565 profilingService.ResetExternalProfilingOptions(options, true);
2566}
2567
2568BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodPeriodicCounterSelectionPacketNoCounters)
2569{
2570 // Swap the profiling connection factory in the profiling service instance with our mock one
2571 SwapProfilingConnectionFactoryHelper helper;
2572
2573 // Reset the profiling service to the uninitialized state
2574 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002575 options.m_EnableProfiling = true;
Matteo Martincighe8485382019-10-10 14:08:21 +01002576 ProfilingService& profilingService = ProfilingService::Instance();
2577 profilingService.ResetExternalProfilingOptions(options, true);
2578
2579 // Bring the profiling service to the "Active" state
2580 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002581 profilingService.Update(); // Initialize the counter directory
Matteo Martincighe8485382019-10-10 14:08:21 +01002582 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002583 profilingService.Update(); // Create the profiling connection
Matteo Martincighe8485382019-10-10 14:08:21 +01002584 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002585 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002586
Colm Donelan2ba48d22019-11-29 09:10:59 +00002587 // Get the mock profiling connection
2588 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2589 BOOST_CHECK(mockProfilingConnection);
2590
Matteo Martincighe8485382019-10-10 14:08:21 +01002591 // Wait for the Stream Metadata packet the be sent
2592 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Finn Williams09ad6f92019-12-19 17:05:18 +00002593 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData);
Matteo Martincighe8485382019-10-10 14:08:21 +01002594
2595 // Force the profiling service to the "Active" state
2596 helper.ForceTransitionToState(ProfilingState::Active);
2597 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2598
Matteo Martincighe8485382019-10-10 14:08:21 +01002599 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2600 // external profiling service
2601
2602 // Periodic Counter Selection packet header:
2603 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2604 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2605 // 8:15 [8] reserved: Reserved, value 0b00000000
2606 // 0:7 [8] reserved: Reserved, value 0b00000000
2607 uint32_t packetFamily = 0;
2608 uint32_t packetId = 4;
Keith Davis3201eea2019-10-24 17:30:41 +01002609 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincighe8485382019-10-10 14:08:21 +01002610
2611 // Create the Periodic Counter Selection packet
Keith Davis3201eea2019-10-24 17:30:41 +01002612 Packet periodicCounterSelectionPacket(header); // Length == 0, this will disable the collection of counters
Matteo Martincighe8485382019-10-10 14:08:21 +01002613
2614 // Write the packet to the mock profiling connection
2615 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2616
Finn Williams09ad6f92019-12-19 17:05:18 +00002617 // Wait for the Periodic Counter Selection packet of length 12 to be sent
2618 // The size of the expected Periodic Counter Selection (echos the sent one)
2619 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterSelection, 12) == 1);
Matteo Martincighe8485382019-10-10 14:08:21 +01002620
2621 // The Periodic Counter Selection Handler should not have updated the profiling state
2622 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2623
Finn Williams09ad6f92019-12-19 17:05:18 +00002624 // No Periodic Counter packets are expected
2625 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterCapture, 0, 0) == 0);
Matteo Martincighe8485382019-10-10 14:08:21 +01002626
2627 // Reset the profiling service to stop any running thread
2628 options.m_EnableProfiling = false;
2629 profilingService.ResetExternalProfilingOptions(options, true);
2630}
2631
2632BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodPeriodicCounterSelectionPacketSingleCounter)
2633{
2634 // Swap the profiling connection factory in the profiling service instance with our mock one
2635 SwapProfilingConnectionFactoryHelper helper;
2636
2637 // Reset the profiling service to the uninitialized state
2638 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002639 options.m_EnableProfiling = true;
Matteo Martincighe8485382019-10-10 14:08:21 +01002640 ProfilingService& profilingService = ProfilingService::Instance();
2641 profilingService.ResetExternalProfilingOptions(options, true);
2642
2643 // Bring the profiling service to the "Active" state
2644 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002645 profilingService.Update(); // Initialize the counter directory
Matteo Martincighe8485382019-10-10 14:08:21 +01002646 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002647 profilingService.Update(); // Create the profiling connection
Matteo Martincighe8485382019-10-10 14:08:21 +01002648 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002649 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002650
Colm Donelan2ba48d22019-11-29 09:10:59 +00002651 // Get the mock profiling connection
2652 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2653 BOOST_CHECK(mockProfilingConnection);
2654
Finn Williams09ad6f92019-12-19 17:05:18 +00002655 // Wait for the Stream Metadata packet to be sent
Matteo Martincighe8485382019-10-10 14:08:21 +01002656 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Finn Williams09ad6f92019-12-19 17:05:18 +00002657 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData);
Matteo Martincighe8485382019-10-10 14:08:21 +01002658
2659 // Force the profiling service to the "Active" state
2660 helper.ForceTransitionToState(ProfilingState::Active);
2661 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2662
Matteo Martincighe8485382019-10-10 14:08:21 +01002663 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2664 // external profiling service
2665
2666 // Periodic Counter Selection packet header:
2667 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2668 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2669 // 8:15 [8] reserved: Reserved, value 0b00000000
2670 // 0:7 [8] reserved: Reserved, value 0b00000000
2671 uint32_t packetFamily = 0;
2672 uint32_t packetId = 4;
Keith Davis3201eea2019-10-24 17:30:41 +01002673 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincighe8485382019-10-10 14:08:21 +01002674
Keith Davis3201eea2019-10-24 17:30:41 +01002675 uint32_t capturePeriod = 123456; // Some capture period (microseconds)
Matteo Martincighe8485382019-10-10 14:08:21 +01002676
2677 // Get the first valid counter UID
2678 const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
Keith Davis3201eea2019-10-24 17:30:41 +01002679 const Counters& counters = counterDirectory.GetCounters();
Matteo Martincighe8485382019-10-10 14:08:21 +01002680 BOOST_CHECK(!counters.empty());
Keith Davis3201eea2019-10-24 17:30:41 +01002681 uint16_t counterUid = counters.begin()->first; // Valid counter UID
Matteo Martincighe8485382019-10-10 14:08:21 +01002682
2683 uint32_t length = 6;
2684
2685 auto data = std::make_unique<unsigned char[]>(length);
2686 WriteUint32(data.get(), 0, capturePeriod);
2687 WriteUint16(data.get(), 4, counterUid);
2688
2689 // Create the Periodic Counter Selection packet
Keith Davis3201eea2019-10-24 17:30:41 +01002690 Packet periodicCounterSelectionPacket(header, length, data); // Length > 0, this will start the Period Counter
2691 // Capture thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002692
2693 // Write the packet to the mock profiling connection
2694 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2695
Finn Williams09ad6f92019-12-19 17:05:18 +00002696 // Expecting one Periodic Counter Selection packet of length 14
2697 // and at least one Periodic Counter Capture packet of length 22
2698 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterSelection, 14) == 1);
2699 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterCapture, 22) >= 1);
Matteo Martincighe8485382019-10-10 14:08:21 +01002700
2701 // The Periodic Counter Selection Handler should not have updated the profiling state
2702 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2703
2704 // Reset the profiling service to stop any running thread
2705 options.m_EnableProfiling = false;
2706 profilingService.ResetExternalProfilingOptions(options, true);
2707}
2708
2709BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodPeriodicCounterSelectionPacketMultipleCounters)
2710{
2711 // Swap the profiling connection factory in the profiling service instance with our mock one
2712 SwapProfilingConnectionFactoryHelper helper;
Matteo Martincighe8485382019-10-10 14:08:21 +01002713 // Reset the profiling service to the uninitialized state
2714 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002715 options.m_EnableProfiling = true;
Matteo Martincighe8485382019-10-10 14:08:21 +01002716 ProfilingService& profilingService = ProfilingService::Instance();
2717 profilingService.ResetExternalProfilingOptions(options, true);
2718
2719 // Bring the profiling service to the "Active" state
2720 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002721 profilingService.Update(); // Initialize the counter directory
Matteo Martincighe8485382019-10-10 14:08:21 +01002722 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002723 profilingService.Update(); // Create the profiling connection
Matteo Martincighe8485382019-10-10 14:08:21 +01002724 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002725 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002726
Colm Donelan2ba48d22019-11-29 09:10:59 +00002727 // Get the mock profiling connection
2728 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2729 BOOST_CHECK(mockProfilingConnection);
2730
Matteo Martincighe8485382019-10-10 14:08:21 +01002731 // Wait for the Stream Metadata packet the be sent
2732 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Finn Williams09ad6f92019-12-19 17:05:18 +00002733 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData);
Matteo Martincighe8485382019-10-10 14:08:21 +01002734
2735 // Force the profiling service to the "Active" state
2736 helper.ForceTransitionToState(ProfilingState::Active);
2737 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2738
Matteo Martincighe8485382019-10-10 14:08:21 +01002739 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2740 // external profiling service
2741
2742 // Periodic Counter Selection packet header:
2743 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2744 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2745 // 8:15 [8] reserved: Reserved, value 0b00000000
2746 // 0:7 [8] reserved: Reserved, value 0b00000000
2747 uint32_t packetFamily = 0;
2748 uint32_t packetId = 4;
Keith Davis3201eea2019-10-24 17:30:41 +01002749 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincighe8485382019-10-10 14:08:21 +01002750
Keith Davis3201eea2019-10-24 17:30:41 +01002751 uint32_t capturePeriod = 123456; // Some capture period (microseconds)
Matteo Martincighe8485382019-10-10 14:08:21 +01002752
2753 // Get the first valid counter UID
2754 const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
Keith Davis3201eea2019-10-24 17:30:41 +01002755 const Counters& counters = counterDirectory.GetCounters();
Matteo Martincighe8485382019-10-10 14:08:21 +01002756 BOOST_CHECK(counters.size() > 1);
Keith Davis3201eea2019-10-24 17:30:41 +01002757 uint16_t counterUidA = counters.begin()->first; // First valid counter UID
2758 uint16_t counterUidB = (counters.begin()++)->first; // Second valid counter UID
Matteo Martincighe8485382019-10-10 14:08:21 +01002759
2760 uint32_t length = 8;
2761
2762 auto data = std::make_unique<unsigned char[]>(length);
2763 WriteUint32(data.get(), 0, capturePeriod);
2764 WriteUint16(data.get(), 4, counterUidA);
2765 WriteUint16(data.get(), 6, counterUidB);
2766
2767 // Create the Periodic Counter Selection packet
Keith Davis3201eea2019-10-24 17:30:41 +01002768 Packet periodicCounterSelectionPacket(header, length, data); // Length > 0, this will start the Period Counter
2769 // Capture thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002770
2771 // Write the packet to the mock profiling connection
2772 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2773
Finn Williams09ad6f92019-12-19 17:05:18 +00002774 // Expecting one PeriodicCounterSelection Packet with a length of 16
2775 // And at least one PeriodicCounterCapture Packet with a length of 28
2776 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterSelection, 16) == 1);
2777 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterCapture, 28) >= 1);
Matteo Martincighe8485382019-10-10 14:08:21 +01002778
2779 // The Periodic Counter Selection Handler should not have updated the profiling state
2780 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002781
2782 // Reset the profiling service to stop any running thread
2783 options.m_EnableProfiling = false;
2784 profilingService.ResetExternalProfilingOptions(options, true);
2785}
2786
Jim Flynn53e46992019-10-14 12:31:10 +01002787BOOST_AUTO_TEST_CASE(CheckProfilingServiceDisconnect)
2788{
2789 // Swap the profiling connection factory in the profiling service instance with our mock one
2790 SwapProfilingConnectionFactoryHelper helper;
Jim Flynn53e46992019-10-14 12:31:10 +01002791 // Reset the profiling service to the uninitialized state
2792 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002793 options.m_EnableProfiling = true;
Jim Flynn53e46992019-10-14 12:31:10 +01002794 ProfilingService& profilingService = ProfilingService::Instance();
2795 profilingService.ResetExternalProfilingOptions(options, true);
2796
2797 // Try to disconnect the profiling service while in the "Uninitialised" state
2798 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
2799 profilingService.Disconnect();
Keith Davis3201eea2019-10-24 17:30:41 +01002800 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised); // The state should not change
Jim Flynn53e46992019-10-14 12:31:10 +01002801
2802 // Try to disconnect the profiling service while in the "NotConnected" state
Keith Davis3201eea2019-10-24 17:30:41 +01002803 profilingService.Update(); // Initialize the counter directory
Jim Flynn53e46992019-10-14 12:31:10 +01002804 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
2805 profilingService.Disconnect();
Keith Davis3201eea2019-10-24 17:30:41 +01002806 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected); // The state should not change
Jim Flynn53e46992019-10-14 12:31:10 +01002807
2808 // Try to disconnect the profiling service while in the "WaitingForAck" state
Keith Davis3201eea2019-10-24 17:30:41 +01002809 profilingService.Update(); // Create the profiling connection
Jim Flynn53e46992019-10-14 12:31:10 +01002810 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
2811 profilingService.Disconnect();
Keith Davis3201eea2019-10-24 17:30:41 +01002812 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck); // The state should not change
Jim Flynn53e46992019-10-14 12:31:10 +01002813
2814 // Try to disconnect the profiling service while in the "Active" state
Keith Davis3201eea2019-10-24 17:30:41 +01002815 profilingService.Update(); // Start the command handler and the send thread
Jim Flynn53e46992019-10-14 12:31:10 +01002816
Colm Donelan2ba48d22019-11-29 09:10:59 +00002817 // Get the mock profiling connection
2818 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2819 BOOST_CHECK(mockProfilingConnection);
2820
Jim Flynn53e46992019-10-14 12:31:10 +01002821 // Wait for the Stream Metadata packet the be sent
2822 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Finn Williams09ad6f92019-12-19 17:05:18 +00002823 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData);
Jim Flynn53e46992019-10-14 12:31:10 +01002824
2825 // Force the profiling service to the "Active" state
2826 helper.ForceTransitionToState(ProfilingState::Active);
2827 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2828
Jim Flynn53e46992019-10-14 12:31:10 +01002829 // Check that the profiling connection is open
2830 BOOST_CHECK(mockProfilingConnection->IsOpen());
2831
2832 profilingService.Disconnect();
Colm Donelan2ba48d22019-11-29 09:10:59 +00002833 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected); // The state should have changed
Jim Flynn53e46992019-10-14 12:31:10 +01002834
2835 // Check that the profiling connection has been reset
2836 mockProfilingConnection = helper.GetMockProfilingConnection();
2837 BOOST_CHECK(mockProfilingConnection == nullptr);
2838
2839 // Reset the profiling service to stop any running thread
2840 options.m_EnableProfiling = false;
2841 profilingService.ResetExternalProfilingOptions(options, true);
2842}
2843
Matteo Martincigh994b5342019-10-11 17:19:56 +01002844BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodPerJobCounterSelectionPacket)
2845{
2846 // Swap the profiling connection factory in the profiling service instance with our mock one
2847 SwapProfilingConnectionFactoryHelper helper;
Matteo Martincigh994b5342019-10-11 17:19:56 +01002848 // Reset the profiling service to the uninitialized state
2849 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002850 options.m_EnableProfiling = true;
Matteo Martincigh994b5342019-10-11 17:19:56 +01002851 ProfilingService& profilingService = ProfilingService::Instance();
2852 profilingService.ResetExternalProfilingOptions(options, true);
2853
2854 // Bring the profiling service to the "Active" state
2855 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002856 profilingService.Update(); // Initialize the counter directory
Matteo Martincigh994b5342019-10-11 17:19:56 +01002857 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002858 profilingService.Update(); // Create the profiling connection
Matteo Martincigh994b5342019-10-11 17:19:56 +01002859 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002860 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincigh994b5342019-10-11 17:19:56 +01002861
Colm Donelan2ba48d22019-11-29 09:10:59 +00002862 // Get the mock profiling connection
2863 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2864 BOOST_CHECK(mockProfilingConnection);
2865
Matteo Martincigh994b5342019-10-11 17:19:56 +01002866 // Wait for the Stream Metadata packet the be sent
2867 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Finn Williams09ad6f92019-12-19 17:05:18 +00002868 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData);
Matteo Martincigh994b5342019-10-11 17:19:56 +01002869
2870 // Force the profiling service to the "Active" state
2871 helper.ForceTransitionToState(ProfilingState::Active);
2872 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2873
Matteo Martincigh994b5342019-10-11 17:19:56 +01002874 // Write a "Per-Job Counter Selection" packet into the mock profiling connection, to simulate an input from an
2875 // external profiling service
2876
2877 // Per-Job Counter Selection packet header:
2878 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2879 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2880 // 8:15 [8] reserved: Reserved, value 0b00000000
2881 // 0:7 [8] reserved: Reserved, value 0b00000000
2882 uint32_t packetFamily = 0;
2883 uint32_t packetId = 5;
Keith Davis3201eea2019-10-24 17:30:41 +01002884 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincigh994b5342019-10-11 17:19:56 +01002885
2886 // Create the Per-Job Counter Selection packet
Keith Davis3201eea2019-10-24 17:30:41 +01002887 Packet periodicCounterSelectionPacket(header); // Length == 0, this will disable the collection of counters
Matteo Martincigh994b5342019-10-11 17:19:56 +01002888
2889 // Write the packet to the mock profiling connection
2890 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2891
2892 // Wait for a bit (must at least be the delay value of the mock profiling connection) to make sure that
2893 // the Per-Job Counter Selection packet gets processed by the profiling service
Colm Donelan2ba48d22019-11-29 09:10:59 +00002894 std::this_thread::sleep_for(std::chrono::milliseconds(5));
Matteo Martincigh994b5342019-10-11 17:19:56 +01002895
Matteo Martincigh994b5342019-10-11 17:19:56 +01002896 // The Per-Job Counter Selection Command Handler should not have updated the profiling state
2897 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2898
Finn Williams09ad6f92019-12-19 17:05:18 +00002899 // The Per-Job Counter Selection packets are dropped silently, so there should be no reply coming
2900 // from the profiling service
2901 const auto StreamMetaDataSize = static_cast<unsigned long>(
2902 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData, 0, 0));
2903 BOOST_CHECK(StreamMetaDataSize == mockProfilingConnection->GetWrittenDataSize());
2904
Matteo Martincigh994b5342019-10-11 17:19:56 +01002905 // Reset the profiling service to stop any running thread
2906 options.m_EnableProfiling = false;
2907 profilingService.ResetExternalProfilingOptions(options, true);
2908}
2909
Jim Flynn672d06e2019-10-15 10:18:11 +01002910BOOST_AUTO_TEST_CASE(CheckConfigureProfilingServiceOn)
2911{
2912 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002913 options.m_EnableProfiling = true;
Jim Flynn672d06e2019-10-15 10:18:11 +01002914 ProfilingService& profilingService = ProfilingService::Instance();
2915 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
2916 profilingService.ConfigureProfilingService(options);
2917 // should get as far as NOT_CONNECTED
2918 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
2919 // Reset the profiling service to stop any running thread
2920 options.m_EnableProfiling = false;
2921 profilingService.ResetExternalProfilingOptions(options, true);
2922}
2923
2924BOOST_AUTO_TEST_CASE(CheckConfigureProfilingServiceOff)
2925{
2926 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
2927 ProfilingService& profilingService = ProfilingService::Instance();
2928 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
2929 profilingService.ConfigureProfilingService(options);
2930 // should not move from Uninitialised
2931 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
2932 // Reset the profiling service to stop any running thread
2933 options.m_EnableProfiling = false;
2934 profilingService.ResetExternalProfilingOptions(options, true);
2935}
2936
Colm Donelan2ba48d22019-11-29 09:10:59 +00002937BOOST_AUTO_TEST_CASE(CheckProfilingServiceEnabled)
2938{
2939 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
2940 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
2941 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
2942 options.m_EnableProfiling = true;
2943 ProfilingService& profilingService = ProfilingService::Instance();
2944 profilingService.ResetExternalProfilingOptions(options, true);
2945 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
2946 profilingService.Update();
2947 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
2948
2949 // Redirect the output to a local stream so that we can parse the warning message
2950 std::stringstream ss;
2951 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
2952 profilingService.Update();
Finn Williams09ad6f92019-12-19 17:05:18 +00002953
2954 // Reset the profiling service to stop any running thread
2955 options.m_EnableProfiling = false;
2956 profilingService.ResetExternalProfilingOptions(options, true);
2957
Colm Donelan2ba48d22019-11-29 09:10:59 +00002958 streamRedirector.CancelRedirect();
2959
2960 // Check that the expected error has occurred and logged to the standard output
2961 if (!boost::contains(ss.str(), "Cannot connect to stream socket: Connection refused"))
2962 {
2963 std::cout << ss.str();
2964 BOOST_FAIL("Expected string not found.");
2965 }
Colm Donelan2ba48d22019-11-29 09:10:59 +00002966}
2967
2968BOOST_AUTO_TEST_CASE(CheckProfilingServiceEnabledRuntime)
2969{
2970 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
2971 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
2972 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
2973 ProfilingService& profilingService = ProfilingService::Instance();
2974 profilingService.ResetExternalProfilingOptions(options, true);
2975 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
2976 profilingService.Update();
2977 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
2978 options.m_EnableProfiling = true;
2979 profilingService.ResetExternalProfilingOptions(options);
2980 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
2981 profilingService.Update();
2982 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
2983
2984 // Redirect the output to a local stream so that we can parse the warning message
2985 std::stringstream ss;
2986 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
2987 profilingService.Update();
2988
Finn Williams09ad6f92019-12-19 17:05:18 +00002989 // Reset the profiling service to stop any running thread
2990 options.m_EnableProfiling = false;
2991 profilingService.ResetExternalProfilingOptions(options, true);
2992
Colm Donelan2ba48d22019-11-29 09:10:59 +00002993 streamRedirector.CancelRedirect();
2994
2995 // Check that the expected error has occurred and logged to the standard output
2996 if (!boost::contains(ss.str(), "Cannot connect to stream socket: Connection refused"))
2997 {
2998 std::cout << ss.str();
2999 BOOST_FAIL("Expected string not found.");
3000 }
Colm Donelan2ba48d22019-11-29 09:10:59 +00003001}
3002
3003BOOST_AUTO_TEST_CASE(CheckProfilingServiceBadConnectionAcknowledgedPacket)
3004{
3005 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
3006 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
3007 // Swap the profiling connection factory in the profiling service instance with our mock one
3008 SwapProfilingConnectionFactoryHelper helper;
3009
3010 // Redirect the standard output to a local stream so that we can parse the warning message
3011 std::stringstream ss;
3012 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3013
Colm Donelan2ba48d22019-11-29 09:10:59 +00003014 // Reset the profiling service to the uninitialized state
3015 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
3016 options.m_EnableProfiling = true;
3017 ProfilingService& profilingService = ProfilingService::Instance();
3018 profilingService.ResetExternalProfilingOptions(options, true);
3019
3020 // Bring the profiling service to the "WaitingForAck" state
3021 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3022 profilingService.Update(); // Initialize the counter directory
3023 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
3024 profilingService.Update(); // Create the profiling connection
3025
3026 // Get the mock profiling connection
3027 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
3028 BOOST_CHECK(mockProfilingConnection);
3029
Colm Donelan2ba48d22019-11-29 09:10:59 +00003030 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003031
3032 // Connection Acknowledged Packet header (word 0, word 1 is always zero):
3033 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
3034 // 16:25 [10] packet_id: Packet identifier, value 0b0000000001
3035 // 8:15 [8] reserved: Reserved, value 0b00000000
3036 // 0:7 [8] reserved: Reserved, value 0b00000000
3037 uint32_t packetFamily = 0;
3038 uint32_t packetId = 37; // Wrong packet id!!!
3039 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
3040
3041 // Create the Connection Acknowledged Packet
3042 Packet connectionAcknowledgedPacket(header);
Finn Williams09ad6f92019-12-19 17:05:18 +00003043 // Write an invalid "Connection Acknowledged" packet into the mock profiling connection, to simulate an invalid
3044 // reply from an external profiling service
Colm Donelan2ba48d22019-11-29 09:10:59 +00003045 mockProfilingConnection->WritePacket(std::move(connectionAcknowledgedPacket));
3046
Finn Williams09ad6f92019-12-19 17:05:18 +00003047 // Start the command thread
3048 profilingService.Update();
3049
3050 // Wait for the command thread to join
3051 options.m_EnableProfiling = false;
3052 profilingService.ResetExternalProfilingOptions(options, true);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003053
3054 streamRedirector.CancelRedirect();
3055
3056 // Check that the expected error has occurred and logged to the standard output
3057 if (!boost::contains(ss.str(), "Functor with requested PacketId=37 and Version=4194304 does not exist"))
3058 {
3059 std::cout << ss.str();
3060 BOOST_FAIL("Expected string not found.");
3061 }
Colm Donelan2ba48d22019-11-29 09:10:59 +00003062}
3063
3064BOOST_AUTO_TEST_CASE(CheckProfilingServiceBadRequestCounterDirectoryPacket)
3065{
3066 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
3067 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
3068 // Swap the profiling connection factory in the profiling service instance with our mock one
3069 SwapProfilingConnectionFactoryHelper helper;
3070
3071 // Redirect the standard output to a local stream so that we can parse the warning message
3072 std::stringstream ss;
3073 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3074
3075 // Reset the profiling service to the uninitialized state
3076 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
3077 options.m_EnableProfiling = true;
3078 ProfilingService& profilingService = ProfilingService::Instance();
3079 profilingService.ResetExternalProfilingOptions(options, true);
3080
3081 // Bring the profiling service to the "Active" state
3082 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3083 helper.ForceTransitionToState(ProfilingState::NotConnected);
3084 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
3085 profilingService.Update(); // Create the profiling connection
3086 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003087
3088 // Get the mock profiling connection
3089 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
3090 BOOST_CHECK(mockProfilingConnection);
3091
Colm Donelan2ba48d22019-11-29 09:10:59 +00003092 // Write a valid "Request Counter Directory" packet into the mock profiling connection, to simulate a valid
3093 // reply from an external profiling service
3094
3095 // Request Counter Directory packet header (word 0, word 1 is always zero):
3096 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
3097 // 16:25 [10] packet_id: Packet identifier, value 0b0000000011
3098 // 8:15 [8] reserved: Reserved, value 0b00000000
3099 // 0:7 [8] reserved: Reserved, value 0b00000000
3100 uint32_t packetFamily = 0;
3101 uint32_t packetId = 123; // Wrong packet id!!!
3102 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
3103
3104 // Create the Request Counter Directory packet
3105 Packet requestCounterDirectoryPacket(header);
3106
3107 // Write the packet to the mock profiling connection
3108 mockProfilingConnection->WritePacket(std::move(requestCounterDirectoryPacket));
3109
Finn Williams09ad6f92019-12-19 17:05:18 +00003110 // Start the command handler and the send thread
3111 profilingService.Update();
3112
3113 // Reset the profiling service to stop and join any running thread
3114 options.m_EnableProfiling = false;
3115 profilingService.ResetExternalProfilingOptions(options, true);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003116
3117 streamRedirector.CancelRedirect();
3118
3119 // Check that the expected error has occurred and logged to the standard output
3120 if (!boost::contains(ss.str(), "Functor with requested PacketId=123 and Version=4194304 does not exist"))
3121 {
3122 std::cout << ss.str();
3123 BOOST_FAIL("Expected string not found.");
3124 }
Colm Donelan2ba48d22019-11-29 09:10:59 +00003125}
3126
3127BOOST_AUTO_TEST_CASE(CheckProfilingServiceBadPeriodicCounterSelectionPacket)
3128{
3129 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
3130 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
3131 // Swap the profiling connection factory in the profiling service instance with our mock one
3132 SwapProfilingConnectionFactoryHelper helper;
3133
3134 // Redirect the standard output to a local stream so that we can parse the warning message
3135 std::stringstream ss;
3136 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3137
3138 // Reset the profiling service to the uninitialized state
3139 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
3140 options.m_EnableProfiling = true;
3141 ProfilingService& profilingService = ProfilingService::Instance();
3142 profilingService.ResetExternalProfilingOptions(options, true);
3143
3144 // Bring the profiling service to the "Active" state
3145 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3146 profilingService.Update(); // Initialize the counter directory
3147 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
3148 profilingService.Update(); // Create the profiling connection
3149 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
3150 profilingService.Update(); // Start the command handler and the send thread
3151
3152 // Get the mock profiling connection
3153 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
3154 BOOST_CHECK(mockProfilingConnection);
3155
Colm Donelan2ba48d22019-11-29 09:10:59 +00003156 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
3157 // external profiling service
3158
3159 // Periodic Counter Selection packet header:
3160 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
3161 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
3162 // 8:15 [8] reserved: Reserved, value 0b00000000
3163 // 0:7 [8] reserved: Reserved, value 0b00000000
3164 uint32_t packetFamily = 0;
3165 uint32_t packetId = 999; // Wrong packet id!!!
3166 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
3167
3168 // Create the Periodic Counter Selection packet
3169 Packet periodicCounterSelectionPacket(header); // Length == 0, this will disable the collection of counters
3170
3171 // Write the packet to the mock profiling connection
3172 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
Finn Williams09ad6f92019-12-19 17:05:18 +00003173 profilingService.Update();
Colm Donelan2ba48d22019-11-29 09:10:59 +00003174
Finn Williams09ad6f92019-12-19 17:05:18 +00003175 // Reset the profiling service to stop any running thread
3176 options.m_EnableProfiling = false;
3177 profilingService.ResetExternalProfilingOptions(options, true);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003178
3179 // Check that the expected error has occurred and logged to the standard output
3180 streamRedirector.CancelRedirect();
3181
3182 // Check that the expected error has occurred and logged to the standard output
3183 if (!boost::contains(ss.str(), "Functor with requested PacketId=999 and Version=4194304 does not exist"))
3184 {
3185 std::cout << ss.str();
3186 BOOST_FAIL("Expected string not found.");
3187 }
Colm Donelan2ba48d22019-11-29 09:10:59 +00003188}
Jim Flynn97897022020-02-02 12:52:59 +00003189
David Monahande803072020-01-30 12:44:23 +00003190BOOST_AUTO_TEST_CASE(CheckCounterIdMap)
3191{
3192 CounterIdMap counterIdMap;
3193 BOOST_CHECK_THROW(counterIdMap.GetBackendId(0), armnn::Exception);
3194 BOOST_CHECK_THROW(counterIdMap.GetGlobalId(0, armnn::profiling::BACKEND_ID), armnn::Exception);
3195
3196 uint16_t globalCounterIds = 0;
3197
3198 armnn::BackendId cpuRefId(armnn::Compute::CpuRef);
3199 armnn::BackendId cpuAccId(armnn::Compute::CpuAcc);
3200
3201 std::vector<uint16_t> cpuRefCounters = {0, 1, 2, 3};
3202 std::vector<uint16_t> cpuAccCounters = {0, 1};
3203
3204 for (uint16_t backendCounterId : cpuRefCounters)
3205 {
3206 counterIdMap.RegisterMapping(globalCounterIds, backendCounterId, cpuRefId);
3207 ++globalCounterIds;
3208 }
3209 for (uint16_t backendCounterId : cpuAccCounters)
3210 {
3211 counterIdMap.RegisterMapping(globalCounterIds, backendCounterId, cpuAccId);
3212 ++globalCounterIds;
3213 }
3214
3215 BOOST_CHECK(counterIdMap.GetBackendId(0) == (std::pair<uint16_t, armnn::BackendId>(0, cpuRefId)));
3216 BOOST_CHECK(counterIdMap.GetBackendId(1) == (std::pair<uint16_t, armnn::BackendId>(1, cpuRefId)));
3217 BOOST_CHECK(counterIdMap.GetBackendId(2) == (std::pair<uint16_t, armnn::BackendId>(2, cpuRefId)));
3218 BOOST_CHECK(counterIdMap.GetBackendId(3) == (std::pair<uint16_t, armnn::BackendId>(3, cpuRefId)));
3219 BOOST_CHECK(counterIdMap.GetBackendId(4) == (std::pair<uint16_t, armnn::BackendId>(0, cpuAccId)));
3220 BOOST_CHECK(counterIdMap.GetBackendId(5) == (std::pair<uint16_t, armnn::BackendId>(1, cpuAccId)));
3221
3222 BOOST_CHECK(counterIdMap.GetGlobalId(0, cpuRefId) == 0);
3223 BOOST_CHECK(counterIdMap.GetGlobalId(1, cpuRefId) == 1);
3224 BOOST_CHECK(counterIdMap.GetGlobalId(2, cpuRefId) == 2);
3225 BOOST_CHECK(counterIdMap.GetGlobalId(3, cpuRefId) == 3);
3226 BOOST_CHECK(counterIdMap.GetGlobalId(0, cpuAccId) == 4);
3227 BOOST_CHECK(counterIdMap.GetGlobalId(1, cpuAccId) == 5);
3228}
Colm Donelan2ba48d22019-11-29 09:10:59 +00003229
Jim Flynn97897022020-02-02 12:52:59 +00003230BOOST_AUTO_TEST_CASE(CheckRegisterBackendCounters)
3231{
3232 uint16_t globalCounterIds = armnn::profiling::INFERENCES_RUN;
3233 armnn::BackendId cpuRefId(armnn::Compute::CpuRef);
3234
3235 RegisterBackendCounters registerBackendCounters(globalCounterIds, cpuRefId);
3236
3237 // Reset the profiling service to the uninitialized state
3238 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
3239 options.m_EnableProfiling = true;
3240 ProfilingService& profilingService = ProfilingService::Instance();
3241 profilingService.ResetExternalProfilingOptions(options, true);
3242
3243 BOOST_CHECK(profilingService.GetCounterDirectory().GetCategories().empty());
3244 registerBackendCounters.RegisterCategory("categoryOne");
3245 auto categoryOnePtr = profilingService.GetCounterDirectory().GetCategory("categoryOne");
3246 BOOST_CHECK(categoryOnePtr);
3247
3248 BOOST_CHECK(profilingService.GetCounterDirectory().GetDevices().empty());
3249 globalCounterIds = registerBackendCounters.RegisterDevice("deviceOne");
3250 auto deviceOnePtr = profilingService.GetCounterDirectory().GetDevice(globalCounterIds);
3251 BOOST_CHECK(deviceOnePtr);
3252 BOOST_CHECK(deviceOnePtr->m_Name == "deviceOne");
3253
3254 BOOST_CHECK(profilingService.GetCounterDirectory().GetCounterSets().empty());
3255 globalCounterIds = registerBackendCounters.RegisterCounterSet("counterSetOne");
3256 auto counterSetOnePtr = profilingService.GetCounterDirectory().GetCounterSet(globalCounterIds);
3257 BOOST_CHECK(counterSetOnePtr);
3258 BOOST_CHECK(counterSetOnePtr->m_Name == "counterSetOne");
3259
3260 uint16_t newGlobalCounterId = registerBackendCounters.RegisterCounter(0,
3261 "categoryOne",
3262 0,
3263 0,
3264 1.f,
3265 "CounterOne",
3266 "first test counter");
3267 BOOST_CHECK(newGlobalCounterId = armnn::profiling::INFERENCES_RUN + 1);
3268 uint16_t mappedGlobalId = profilingService.GetCounterMappings().GetGlobalId(0, cpuRefId);
3269 BOOST_CHECK(mappedGlobalId == newGlobalCounterId);
3270 auto backendMapping = profilingService.GetCounterMappings().GetBackendId(newGlobalCounterId);
3271 BOOST_CHECK(backendMapping.first == 0);
3272 BOOST_CHECK(backendMapping.second == cpuRefId);
3273
3274 // Reset the profiling service to stop any running thread
3275 options.m_EnableProfiling = false;
3276 profilingService.ResetExternalProfilingOptions(options, true);
3277}
3278
James Conroy2dcd3fe2020-02-06 18:34:52 +00003279BOOST_AUTO_TEST_CASE(CheckCounterStatusQuery)
3280{
3281 armnn::IRuntime::CreationOptions options;
3282 options.m_ProfilingOptions.m_EnableProfiling = true;
3283
3284 // Reset the profiling service to the uninitialized state
3285 ProfilingService& profilingService = ProfilingService::Instance();
3286 profilingService.ResetExternalProfilingOptions(options.m_ProfilingOptions, true);
3287
3288 const armnn::BackendId cpuRefId(armnn::Compute::CpuRef);
3289 const armnn::BackendId cpuAccId(armnn::Compute::CpuAcc);
3290
3291 // Create BackendProfiling for each backend
3292 BackendProfiling backendProfilingCpuRef(options, profilingService, cpuRefId);
3293 BackendProfiling backendProfilingCpuAcc(options, profilingService, cpuAccId);
3294
3295 uint16_t initialNumGlobalCounterIds = armnn::profiling::INFERENCES_RUN;
3296
3297 // Create RegisterBackendCounters for CpuRef
3298 RegisterBackendCounters registerBackendCountersCpuRef(initialNumGlobalCounterIds, cpuRefId);
3299
3300 // Create 'testCategory' in CounterDirectory (backend agnostic)
3301 BOOST_CHECK(profilingService.GetCounterDirectory().GetCategories().empty());
3302 registerBackendCountersCpuRef.RegisterCategory("testCategory");
3303 auto categoryOnePtr = profilingService.GetCounterDirectory().GetCategory("testCategory");
3304 BOOST_CHECK(categoryOnePtr);
3305
3306 // Counters:
3307 // Global | Local | Backend
3308 // 5 | 0 | CpuRef
3309 // 6 | 1 | CpuRef
3310 // 7 | 1 | CpuAcc
3311
3312 std::vector<uint16_t> cpuRefCounters = {0, 1};
3313 std::vector<uint16_t> cpuAccCounters = {0};
3314
3315 // Register the backend counters for CpuRef and validate GetGlobalId and GetBackendId
3316 uint16_t currentNumGlobalCounterIds = registerBackendCountersCpuRef.RegisterCounter(
3317 0, "testCategory", 0, 0, 1.f, "CpuRefCounter0", "Zeroth CpuRef Counter");
3318 BOOST_CHECK(currentNumGlobalCounterIds == initialNumGlobalCounterIds + 1);
3319 uint16_t mappedGlobalId = profilingService.GetCounterMappings().GetGlobalId(0, cpuRefId);
3320 BOOST_CHECK(mappedGlobalId == currentNumGlobalCounterIds);
3321 auto backendMapping = profilingService.GetCounterMappings().GetBackendId(currentNumGlobalCounterIds);
3322 BOOST_CHECK(backendMapping.first == 0);
3323 BOOST_CHECK(backendMapping.second == cpuRefId);
3324
3325 currentNumGlobalCounterIds = registerBackendCountersCpuRef.RegisterCounter(
3326 1, "testCategory", 0, 0, 1.f, "CpuRefCounter1", "First CpuRef Counter");
3327 BOOST_CHECK(currentNumGlobalCounterIds == initialNumGlobalCounterIds + 2);
3328 mappedGlobalId = profilingService.GetCounterMappings().GetGlobalId(1, cpuRefId);
3329 BOOST_CHECK(mappedGlobalId == currentNumGlobalCounterIds);
3330 backendMapping = profilingService.GetCounterMappings().GetBackendId(currentNumGlobalCounterIds);
3331 BOOST_CHECK(backendMapping.first == 1);
3332 BOOST_CHECK(backendMapping.second == cpuRefId);
3333
3334 // Create RegisterBackendCounters for CpuAcc
3335 RegisterBackendCounters registerBackendCountersCpuAcc(currentNumGlobalCounterIds, cpuAccId);
3336
3337 // Register the backend counter for CpuAcc and validate GetGlobalId and GetBackendId
3338 currentNumGlobalCounterIds = registerBackendCountersCpuAcc.RegisterCounter(
3339 0, "testCategory", 0, 0, 1.f, "CpuAccCounter0", "Zeroth CpuAcc Counter");
3340 BOOST_CHECK(currentNumGlobalCounterIds == initialNumGlobalCounterIds + 3);
3341 mappedGlobalId = profilingService.GetCounterMappings().GetGlobalId(0, cpuAccId);
3342 BOOST_CHECK(mappedGlobalId == currentNumGlobalCounterIds);
3343 backendMapping = profilingService.GetCounterMappings().GetBackendId(currentNumGlobalCounterIds);
3344 BOOST_CHECK(backendMapping.first == 0);
3345 BOOST_CHECK(backendMapping.second == cpuAccId);
3346
3347 // Create vectors for active counters
3348 const std::vector<uint16_t> activeGlobalCounterIds = {5}; // CpuRef(0) activated
3349 const std::vector<uint16_t> newActiveGlobalCounterIds = {6, 7}; // CpuRef(0) and CpuAcc(1) activated
3350
3351 const uint32_t capturePeriod = 200;
3352 const uint32_t newCapturePeriod = 100;
3353
3354 // Set capture period and active counters in CaptureData
Finn Williams032bc742020-02-12 11:02:34 +00003355 profilingService.SetCaptureData(capturePeriod, activeGlobalCounterIds, {});
James Conroy2dcd3fe2020-02-06 18:34:52 +00003356
3357 // Get vector of active counters for CpuRef and CpuAcc backends
3358 std::vector<CounterStatus> cpuRefCounterStatus = backendProfilingCpuRef.GetActiveCounters();
3359 std::vector<CounterStatus> cpuAccCounterStatus = backendProfilingCpuAcc.GetActiveCounters();
3360 BOOST_CHECK_EQUAL(cpuRefCounterStatus.size(), 1);
3361 BOOST_CHECK_EQUAL(cpuAccCounterStatus.size(), 0);
3362
3363 // Check active CpuRef counter
3364 BOOST_CHECK_EQUAL(cpuRefCounterStatus[0].m_GlobalCounterId, activeGlobalCounterIds[0]);
3365 BOOST_CHECK_EQUAL(cpuRefCounterStatus[0].m_BackendCounterId, cpuRefCounters[0]);
3366 BOOST_CHECK_EQUAL(cpuRefCounterStatus[0].m_SamplingRateInMicroseconds, capturePeriod);
3367 BOOST_CHECK_EQUAL(cpuRefCounterStatus[0].m_Enabled, true);
3368
3369 // Check inactive CpuRef counter
3370 CounterStatus inactiveCpuRefCounter = backendProfilingCpuRef.GetCounterStatus(cpuRefCounters[1]);
3371 BOOST_CHECK_EQUAL(inactiveCpuRefCounter.m_GlobalCounterId, 6);
3372 BOOST_CHECK_EQUAL(inactiveCpuRefCounter.m_BackendCounterId, cpuRefCounters[1]);
3373 BOOST_CHECK_EQUAL(inactiveCpuRefCounter.m_SamplingRateInMicroseconds, 0);
3374 BOOST_CHECK_EQUAL(inactiveCpuRefCounter.m_Enabled, false);
3375
3376 // Check inactive CpuAcc counter
3377 CounterStatus inactiveCpuAccCounter = backendProfilingCpuAcc.GetCounterStatus(cpuAccCounters[0]);
3378 BOOST_CHECK_EQUAL(inactiveCpuAccCounter.m_GlobalCounterId, 7);
3379 BOOST_CHECK_EQUAL(inactiveCpuAccCounter.m_BackendCounterId, cpuAccCounters[0]);
3380 BOOST_CHECK_EQUAL(inactiveCpuAccCounter.m_SamplingRateInMicroseconds, 0);
3381 BOOST_CHECK_EQUAL(inactiveCpuAccCounter.m_Enabled, false);
3382
3383 // Set new capture period and new active counters in CaptureData
Finn Williams032bc742020-02-12 11:02:34 +00003384 profilingService.SetCaptureData(newCapturePeriod, newActiveGlobalCounterIds, {});
James Conroy2dcd3fe2020-02-06 18:34:52 +00003385
3386 // Get vector of active counters for CpuRef and CpuAcc backends
3387 cpuRefCounterStatus = backendProfilingCpuRef.GetActiveCounters();
3388 cpuAccCounterStatus = backendProfilingCpuAcc.GetActiveCounters();
3389 BOOST_CHECK_EQUAL(cpuRefCounterStatus.size(), 1);
3390 BOOST_CHECK_EQUAL(cpuAccCounterStatus.size(), 1);
3391
3392 // Check active CpuRef counter
3393 BOOST_CHECK_EQUAL(cpuRefCounterStatus[0].m_GlobalCounterId, newActiveGlobalCounterIds[0]);
3394 BOOST_CHECK_EQUAL(cpuRefCounterStatus[0].m_BackendCounterId, cpuRefCounters[1]);
3395 BOOST_CHECK_EQUAL(cpuRefCounterStatus[0].m_SamplingRateInMicroseconds, newCapturePeriod);
3396 BOOST_CHECK_EQUAL(cpuRefCounterStatus[0].m_Enabled, true);
3397
3398 // Check active CpuAcc counter
3399 BOOST_CHECK_EQUAL(cpuAccCounterStatus[0].m_GlobalCounterId, newActiveGlobalCounterIds[1]);
3400 BOOST_CHECK_EQUAL(cpuAccCounterStatus[0].m_BackendCounterId, cpuAccCounters[0]);
3401 BOOST_CHECK_EQUAL(cpuAccCounterStatus[0].m_SamplingRateInMicroseconds, newCapturePeriod);
3402 BOOST_CHECK_EQUAL(cpuAccCounterStatus[0].m_Enabled, true);
3403
3404 // Check inactive CpuRef counter
3405 inactiveCpuRefCounter = backendProfilingCpuRef.GetCounterStatus(cpuRefCounters[0]);
3406 BOOST_CHECK_EQUAL(inactiveCpuRefCounter.m_GlobalCounterId, 5);
3407 BOOST_CHECK_EQUAL(inactiveCpuRefCounter.m_BackendCounterId, cpuRefCounters[0]);
3408 BOOST_CHECK_EQUAL(inactiveCpuRefCounter.m_SamplingRateInMicroseconds, 0);
3409 BOOST_CHECK_EQUAL(inactiveCpuRefCounter.m_Enabled, false);
3410
3411 // Reset the profiling service to stop any running thread
3412 options.m_ProfilingOptions.m_EnableProfiling = false;
3413 profilingService.ResetExternalProfilingOptions(options.m_ProfilingOptions, true);
3414}
3415
Sadik Armagancab588a2020-02-17 11:33:31 +00003416BOOST_AUTO_TEST_CASE(CheckRegisterCounters)
3417{
3418 armnn::Runtime::CreationOptions options;
3419 options.m_ProfilingOptions.m_EnableProfiling = true;
3420 MockBufferManager mockBuffer(1024);
3421 CaptureData captureData;
3422 MockProfilingService mockProfilingService(
3423 mockBuffer, options.m_ProfilingOptions.m_EnableProfiling, captureData);
3424 armnn::BackendId cpuRefId(armnn::Compute::CpuRef);
3425
3426 mockProfilingService.RegisterMapping(6, 0, cpuRefId);
3427 mockProfilingService.RegisterMapping(7, 1, cpuRefId);
3428 mockProfilingService.RegisterMapping(8, 2, cpuRefId);
3429
3430 armnn::profiling::BackendProfiling backendProfiling(options,
3431 mockProfilingService,
3432 cpuRefId);
3433
3434 armnn::profiling::Timestamp timestamp;
3435 timestamp.timestamp = 1000998;
3436 timestamp.counterValues.emplace_back(0, 700);
3437 timestamp.counterValues.emplace_back(2, 93);
3438 std::vector<armnn::profiling::Timestamp> timestamps;
3439 timestamps.push_back(timestamp);
3440 backendProfiling.ReportCounters(timestamps);
3441
3442 auto readBuffer = mockBuffer.GetReadableBuffer();
3443
3444 uint32_t headerWord0 = ReadUint32(readBuffer, 0);
3445 uint32_t headerWord1 = ReadUint32(readBuffer, 4);
3446 uint64_t readTimestamp = ReadUint64(readBuffer, 8);
3447
3448 BOOST_TEST(((headerWord0 >> 26) & 0x0000003F) == 3); // packet family
3449 BOOST_TEST(((headerWord0 >> 19) & 0x0000007F) == 0); // packet class
3450 BOOST_TEST(((headerWord0 >> 16) & 0x00000007) == 0); // packet type
3451 BOOST_TEST(headerWord1 == 20); // data length
3452 BOOST_TEST(1000998 == readTimestamp); // capture period
3453
3454 uint32_t offset = 16;
3455 // Check Counter Index
3456 uint16_t readIndex = ReadUint16(readBuffer, offset);
3457 BOOST_TEST(6 == readIndex);
3458
3459 // Check Counter Value
3460 offset += 2;
3461 uint32_t readValue = ReadUint32(readBuffer, offset);
3462 BOOST_TEST(700 == readValue);
3463
3464 // Check Counter Index
3465 offset += 4;
3466 readIndex = ReadUint16(readBuffer, offset);
3467 BOOST_TEST(8 == readIndex);
3468
3469 // Check Counter Value
3470 offset += 2;
3471 readValue = ReadUint32(readBuffer, offset);
3472 BOOST_TEST(93 == readValue);
3473}
3474
Francis Murtagh1f7db452019-08-14 09:49:34 +01003475BOOST_AUTO_TEST_SUITE_END()