blob: 0bad66fb1c67494b485caa2a6aa3278288840d40 [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{
539 holder.SetCaptureData(capturePeriod, counterIds);
540}
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;
1767 Holder holder;
1768 TestCaptureThread captureThread;
Matteo Martincighe8485382019-10-10 14:08:21 +01001769 TestReadCounterValues readCounterValues;
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001770 MockBufferManager mockBuffer(512);
Sadik Armagan3896b472020-02-10 12:24:15 +00001771 SendCounterPacket sendCounterPacket(mockBuffer);
1772 SendThread sendThread(profilingStateMachine, mockBuffer, sendCounterPacket);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001773
1774 uint32_t sizeOfUint32 = numeric_cast<uint32_t>(sizeof(uint32_t));
1775 uint32_t sizeOfUint16 = numeric_cast<uint32_t>(sizeof(uint16_t));
1776
1777 // Data with period and counters
Colm Donelan02705242019-11-14 14:19:07 +00001778 uint32_t period1 = armnn::LOWEST_CAPTURE_PERIOD;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001779 uint32_t dataLength1 = 8;
Keith Davis3201eea2019-10-24 17:30:41 +01001780 uint32_t offset = 0;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001781
Matteo Martincigh67ef2a52019-10-10 13:29:02 +01001782 std::unique_ptr<unsigned char[]> uniqueData1 = std::make_unique<unsigned char[]>(dataLength1);
Keith Davis3201eea2019-10-24 17:30:41 +01001783 unsigned char* data1 = reinterpret_cast<unsigned char*>(uniqueData1.get());
FinnWilliamsArma0c78712019-09-16 12:06:47 +01001784
Ferran Balaguer1b941722019-08-28 16:57:18 +01001785 WriteUint32(data1, offset, period1);
1786 offset += sizeOfUint32;
1787 WriteUint16(data1, offset, 4000);
1788 offset += sizeOfUint16;
1789 WriteUint16(data1, offset, 5000);
1790
FinnWilliamsArma0c78712019-09-16 12:06:47 +01001791 Packet packetA(packetId, dataLength1, uniqueData1);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001792
Keith Davis3201eea2019-10-24 17:30:41 +01001793 PeriodicCounterSelectionCommandHandler commandHandler(familyId, packetId, version, holder, captureThread,
1794 readCounterValues, sendCounterPacket, profilingStateMachine);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001795
Matteo Martincighe8485382019-10-10 14:08:21 +01001796 profilingStateMachine.TransitionToState(ProfilingState::Uninitialised);
1797 BOOST_CHECK_THROW(commandHandler(packetA), armnn::RuntimeException);
1798 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
1799 BOOST_CHECK_THROW(commandHandler(packetA), armnn::RuntimeException);
1800 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
1801 BOOST_CHECK_THROW(commandHandler(packetA), armnn::RuntimeException);
1802 profilingStateMachine.TransitionToState(ProfilingState::Active);
1803 BOOST_CHECK_NO_THROW(commandHandler(packetA));
1804
1805 const std::vector<uint16_t> counterIdsA = holder.GetCaptureData().GetCounterIds();
Ferran Balaguer1b941722019-08-28 16:57:18 +01001806
1807 BOOST_TEST(holder.GetCaptureData().GetCapturePeriod() == period1);
Matteo Martincighe8485382019-10-10 14:08:21 +01001808 BOOST_TEST(counterIdsA.size() == 2);
1809 BOOST_TEST(counterIdsA[0] == 4000);
1810 BOOST_TEST(counterIdsA[1] == 5000);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001811
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001812 auto readBuffer = mockBuffer.GetReadableBuffer();
Ferran Balaguer1b941722019-08-28 16:57:18 +01001813
1814 offset = 0;
1815
1816 uint32_t headerWord0 = ReadUint32(readBuffer, offset);
1817 offset += sizeOfUint32;
1818 uint32_t headerWord1 = ReadUint32(readBuffer, offset);
1819 offset += sizeOfUint32;
1820 uint32_t period = ReadUint32(readBuffer, offset);
1821
Colm Donelan02705242019-11-14 14:19:07 +00001822 BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family
1823 BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 4); // packet id
1824 BOOST_TEST(headerWord1 == 8); // data length
1825 BOOST_TEST(period == armnn::LOWEST_CAPTURE_PERIOD); // capture period
Ferran Balaguer1b941722019-08-28 16:57:18 +01001826
1827 uint16_t counterId = 0;
1828 offset += sizeOfUint32;
1829 counterId = ReadUint16(readBuffer, offset);
1830 BOOST_TEST(counterId == 4000);
1831 offset += sizeOfUint16;
1832 counterId = ReadUint16(readBuffer, offset);
1833 BOOST_TEST(counterId == 5000);
1834
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001835 mockBuffer.MarkRead(readBuffer);
1836
Ferran Balaguer1b941722019-08-28 16:57:18 +01001837 // Data with period only
Colm Donelan02705242019-11-14 14:19:07 +00001838 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 +01001839 uint32_t dataLength2 = 4;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001840
Matteo Martincigh67ef2a52019-10-10 13:29:02 +01001841 std::unique_ptr<unsigned char[]> uniqueData2 = std::make_unique<unsigned char[]>(dataLength2);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001842
FinnWilliamsArma0c78712019-09-16 12:06:47 +01001843 WriteUint32(reinterpret_cast<unsigned char*>(uniqueData2.get()), 0, period2);
1844
1845 Packet packetB(packetId, dataLength2, uniqueData2);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001846
1847 commandHandler(packetB);
1848
Matteo Martincighe8485382019-10-10 14:08:21 +01001849 const std::vector<uint16_t> counterIdsB = holder.GetCaptureData().GetCounterIds();
Ferran Balaguer1b941722019-08-28 16:57:18 +01001850
Colm Donelan02705242019-11-14 14:19:07 +00001851 // Value should have been pulled up from 9000 to LOWEST_CAPTURE_PERIOD.
1852 BOOST_TEST(holder.GetCaptureData().GetCapturePeriod() == armnn::LOWEST_CAPTURE_PERIOD);
Matteo Martincighe8485382019-10-10 14:08:21 +01001853 BOOST_TEST(counterIdsB.size() == 0);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001854
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001855 readBuffer = mockBuffer.GetReadableBuffer();
Ferran Balaguer1b941722019-08-28 16:57:18 +01001856
1857 offset = 0;
1858
1859 headerWord0 = ReadUint32(readBuffer, offset);
1860 offset += sizeOfUint32;
1861 headerWord1 = ReadUint32(readBuffer, offset);
1862 offset += sizeOfUint32;
1863 period = ReadUint32(readBuffer, offset);
1864
Colm Donelan02705242019-11-14 14:19:07 +00001865 BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family
1866 BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 4); // packet id
1867 BOOST_TEST(headerWord1 == 4); // data length
1868 BOOST_TEST(period == armnn::LOWEST_CAPTURE_PERIOD); // capture period
Ferran Balaguer1b941722019-08-28 16:57:18 +01001869}
1870
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001871BOOST_AUTO_TEST_CASE(CheckConnectionAcknowledged)
1872{
1873 using boost::numeric_cast;
1874
Keith Davis3201eea2019-10-24 17:30:41 +01001875 const uint32_t packetFamilyId = 0;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001876 const uint32_t connectionPacketId = 0x10000;
Keith Davis3201eea2019-10-24 17:30:41 +01001877 const uint32_t version = 1;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001878
1879 uint32_t sizeOfUint32 = numeric_cast<uint32_t>(sizeof(uint32_t));
1880 uint32_t sizeOfUint16 = numeric_cast<uint32_t>(sizeof(uint16_t));
1881
1882 // Data with period and counters
Keith Davis3201eea2019-10-24 17:30:41 +01001883 uint32_t period1 = 10;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001884 uint32_t dataLength1 = 8;
Keith Davis3201eea2019-10-24 17:30:41 +01001885 uint32_t offset = 0;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001886
Matteo Martincigh67ef2a52019-10-10 13:29:02 +01001887 std::unique_ptr<unsigned char[]> uniqueData1 = std::make_unique<unsigned char[]>(dataLength1);
Keith Davis3201eea2019-10-24 17:30:41 +01001888 unsigned char* data1 = reinterpret_cast<unsigned char*>(uniqueData1.get());
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001889
1890 WriteUint32(data1, offset, period1);
1891 offset += sizeOfUint32;
1892 WriteUint16(data1, offset, 4000);
1893 offset += sizeOfUint16;
1894 WriteUint16(data1, offset, 5000);
1895
1896 Packet packetA(connectionPacketId, dataLength1, uniqueData1);
1897
1898 ProfilingStateMachine profilingState(ProfilingState::Uninitialised);
1899 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01001900 CounterDirectory counterDirectory;
1901 MockBufferManager mockBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00001902 SendCounterPacket sendCounterPacket(mockBuffer);
1903 SendThread sendThread(profilingState, mockBuffer, sendCounterPacket);
Matteo Martincighcdfb9412019-11-08 11:23:06 +00001904 SendTimelinePacket sendTimelinePacket(mockBuffer);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001905
Keith Davis3201eea2019-10-24 17:30:41 +01001906 ConnectionAcknowledgedCommandHandler commandHandler(packetFamilyId, connectionPacketId, version, counterDirectory,
Matteo Martincighcdfb9412019-11-08 11:23:06 +00001907 sendCounterPacket, sendTimelinePacket, profilingState);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001908
1909 // command handler received packet on ProfilingState::Uninitialised
1910 BOOST_CHECK_THROW(commandHandler(packetA), armnn::Exception);
1911
1912 profilingState.TransitionToState(ProfilingState::NotConnected);
1913 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::NotConnected);
1914 // command handler received packet on ProfilingState::NotConnected
1915 BOOST_CHECK_THROW(commandHandler(packetA), armnn::Exception);
1916
1917 profilingState.TransitionToState(ProfilingState::WaitingForAck);
1918 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::WaitingForAck);
1919 // command handler received packet on ProfilingState::WaitingForAck
Matteo Martincighd0613b52019-10-09 16:47:04 +01001920 BOOST_CHECK_NO_THROW(commandHandler(packetA));
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001921 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::Active);
1922
1923 // command handler received packet on ProfilingState::Active
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 different packet
1928 const uint32_t differentPacketId = 0x40000;
1929 Packet packetB(differentPacketId, dataLength1, uniqueData1);
Matteo Martincighd0613b52019-10-09 16:47:04 +01001930 profilingState.TransitionToState(ProfilingState::NotConnected);
1931 profilingState.TransitionToState(ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01001932 ConnectionAcknowledgedCommandHandler differentCommandHandler(packetFamilyId, differentPacketId, version,
Matteo Martincighcdfb9412019-11-08 11:23:06 +00001933 counterDirectory, sendCounterPacket,
1934 sendTimelinePacket, profilingState);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001935 BOOST_CHECK_THROW(differentCommandHandler(packetB), armnn::Exception);
1936}
1937
Teresa Charlin9bab4962019-09-06 12:28:35 +01001938BOOST_AUTO_TEST_CASE(CheckSocketProfilingConnection)
1939{
1940 // Check that creating a SocketProfilingConnection results in an exception as the Gator UDS doesn't exist.
1941 BOOST_CHECK_THROW(new SocketProfilingConnection(), armnn::Exception);
1942}
1943
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001944BOOST_AUTO_TEST_CASE(SwTraceIsValidCharTest)
1945{
1946 // Only ASCII 7-bit encoding supported
1947 for (unsigned char c = 0; c < 128; c++)
1948 {
1949 BOOST_CHECK(SwTraceCharPolicy::IsValidChar(c));
1950 }
1951
1952 // Not ASCII
1953 for (unsigned char c = 255; c >= 128; c++)
1954 {
1955 BOOST_CHECK(!SwTraceCharPolicy::IsValidChar(c));
1956 }
1957}
1958
1959BOOST_AUTO_TEST_CASE(SwTraceIsValidNameCharTest)
1960{
1961 // Only alpha-numeric and underscore ASCII 7-bit encoding supported
1962 const unsigned char validChars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
1963 for (unsigned char i = 0; i < sizeof(validChars) / sizeof(validChars[0]) - 1; i++)
1964 {
1965 BOOST_CHECK(SwTraceNameCharPolicy::IsValidChar(validChars[i]));
1966 }
1967
1968 // Non alpha-numeric chars
1969 for (unsigned char c = 0; c < 48; c++)
1970 {
1971 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1972 }
1973 for (unsigned char c = 58; c < 65; c++)
1974 {
1975 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1976 }
1977 for (unsigned char c = 91; c < 95; c++)
1978 {
1979 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1980 }
1981 for (unsigned char c = 96; c < 97; c++)
1982 {
1983 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1984 }
1985 for (unsigned char c = 123; c < 128; c++)
1986 {
1987 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1988 }
1989
1990 // Not ASCII
1991 for (unsigned char c = 255; c >= 128; c++)
1992 {
1993 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1994 }
1995}
1996
1997BOOST_AUTO_TEST_CASE(IsValidSwTraceStringTest)
1998{
1999 // Valid SWTrace strings
2000 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>(""));
2001 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("_"));
2002 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("0123"));
2003 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("valid_string"));
2004 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("VALID_string_456"));
2005 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>(" "));
2006 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("valid string"));
2007 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("!$%"));
2008 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("valid|\\~string#123"));
2009
2010 // Invalid SWTrace strings
2011 BOOST_CHECK(!IsValidSwTraceString<SwTraceCharPolicy>("€£"));
2012 BOOST_CHECK(!IsValidSwTraceString<SwTraceCharPolicy>("invalid‡string"));
2013 BOOST_CHECK(!IsValidSwTraceString<SwTraceCharPolicy>("12Ž34"));
2014}
2015
2016BOOST_AUTO_TEST_CASE(IsValidSwTraceNameStringTest)
2017{
2018 // Valid SWTrace name strings
2019 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>(""));
2020 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>("_"));
2021 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>("0123"));
2022 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>("valid_string"));
2023 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>("VALID_string_456"));
2024
2025 // Invalid SWTrace name strings
2026 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>(" "));
2027 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("invalid string"));
2028 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("!$%"));
2029 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("invalid|\\~string#123"));
2030 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("€£"));
2031 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("invalid‡string"));
2032 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("12Ž34"));
2033}
2034
2035template <typename SwTracePolicy>
2036void StringToSwTraceStringTestHelper(const std::string& testString, std::vector<uint32_t> buffer, size_t expectedSize)
2037{
2038 // Convert the test string to a SWTrace string
2039 BOOST_CHECK(StringToSwTraceString<SwTracePolicy>(testString, buffer));
2040
2041 // The buffer must contain at least the length of the string
2042 BOOST_CHECK(!buffer.empty());
2043
2044 // The buffer must be of the expected size (in words)
2045 BOOST_CHECK(buffer.size() == expectedSize);
2046
2047 // The first word of the byte must be the length of the string including the null-terminator
2048 BOOST_CHECK(buffer[0] == testString.size() + 1);
2049
2050 // The contents of the buffer must match the test string
2051 BOOST_CHECK(std::memcmp(testString.data(), buffer.data() + 1, testString.size()) == 0);
2052
2053 // The buffer must include the null-terminator at the end of the string
2054 size_t nullTerminatorIndex = sizeof(uint32_t) + testString.size();
Keith Davis3201eea2019-10-24 17:30:41 +01002055 BOOST_CHECK(reinterpret_cast<unsigned char*>(buffer.data())[nullTerminatorIndex] == '\0');
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002056}
2057
2058BOOST_AUTO_TEST_CASE(StringToSwTraceStringTest)
2059{
2060 std::vector<uint32_t> buffer;
2061
2062 // Valid SWTrace strings (expected size in words)
2063 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("", buffer, 2);
2064 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("_", buffer, 2);
2065 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("0123", buffer, 3);
2066 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("valid_string", buffer, 5);
2067 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("VALID_string_456", buffer, 6);
2068 StringToSwTraceStringTestHelper<SwTraceCharPolicy>(" ", buffer, 2);
2069 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("valid string", buffer, 5);
2070 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("!$%", buffer, 2);
2071 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("valid|\\~string#123", buffer, 6);
2072
2073 // Invalid SWTrace strings
2074 BOOST_CHECK(!StringToSwTraceString<SwTraceCharPolicy>("€£", buffer));
2075 BOOST_CHECK(buffer.empty());
2076 BOOST_CHECK(!StringToSwTraceString<SwTraceCharPolicy>("invalid‡string", buffer));
2077 BOOST_CHECK(buffer.empty());
2078 BOOST_CHECK(!StringToSwTraceString<SwTraceCharPolicy>("12Ž34", buffer));
2079 BOOST_CHECK(buffer.empty());
2080}
2081
2082BOOST_AUTO_TEST_CASE(StringToSwTraceNameStringTest)
2083{
2084 std::vector<uint32_t> buffer;
2085
2086 // Valid SWTrace namestrings (expected size in words)
2087 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("", buffer, 2);
2088 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("_", buffer, 2);
2089 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("0123", buffer, 3);
2090 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("valid_string", buffer, 5);
2091 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("VALID_string_456", buffer, 6);
2092
2093 // Invalid SWTrace namestrings
2094 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>(" ", buffer));
2095 BOOST_CHECK(buffer.empty());
2096 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("invalid string", buffer));
2097 BOOST_CHECK(buffer.empty());
2098 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("!$%", buffer));
2099 BOOST_CHECK(buffer.empty());
2100 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("invalid|\\~string#123", buffer));
2101 BOOST_CHECK(buffer.empty());
2102 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("€£", buffer));
2103 BOOST_CHECK(buffer.empty());
2104 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("invalid‡string", buffer));
2105 BOOST_CHECK(buffer.empty());
2106 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("12Ž34", buffer));
2107 BOOST_CHECK(buffer.empty());
2108}
2109
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002110BOOST_AUTO_TEST_CASE(CheckPeriodicCounterCaptureThread)
2111{
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002112 class CaptureReader : public IReadCounterValues
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002113 {
2114 public:
Finn Williamsf4d59a62019-10-14 15:55:18 +01002115 CaptureReader(uint16_t counterSize)
2116 {
Keith Davis3201eea2019-10-24 17:30:41 +01002117 for (uint16_t i = 0; i < counterSize; ++i)
Finn Williamsf4d59a62019-10-14 15:55:18 +01002118 {
2119 m_Data[i] = 0;
2120 }
2121 m_CounterSize = counterSize;
2122 }
2123 //not used
Matteo Martincighe8485382019-10-10 14:08:21 +01002124 bool IsCounterRegistered(uint16_t counterUid) const override
2125 {
Derek Lamberti1dd75b32019-12-10 21:23:23 +00002126 boost::ignore_unused(counterUid);
Finn Williamsf4d59a62019-10-14 15:55:18 +01002127 return false;
Matteo Martincighe8485382019-10-10 14:08:21 +01002128 }
2129
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002130 uint16_t GetCounterCount() const override
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002131 {
Finn Williamsf4d59a62019-10-14 15:55:18 +01002132 return m_CounterSize;
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002133 }
2134
Matteo Martincighe8485382019-10-10 14:08:21 +01002135 uint32_t GetCounterValue(uint16_t counterUid) const override
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002136 {
Keith Davis3201eea2019-10-24 17:30:41 +01002137 if (counterUid > m_CounterSize)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002138 {
Finn Williamsf4d59a62019-10-14 15:55:18 +01002139 BOOST_FAIL("Invalid counter Uid");
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002140 }
Matteo Martincighe8485382019-10-10 14:08:21 +01002141 return m_Data.at(counterUid).load();
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002142 }
2143
Matteo Martincighe8485382019-10-10 14:08:21 +01002144 void SetCounterValue(uint16_t counterUid, uint32_t value)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002145 {
Keith Davis3201eea2019-10-24 17:30:41 +01002146 if (counterUid > m_CounterSize)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002147 {
Finn Williamsf4d59a62019-10-14 15:55:18 +01002148 BOOST_FAIL("Invalid counter Uid");
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002149 }
Finn Williamsf4d59a62019-10-14 15:55:18 +01002150 m_Data.at(counterUid).store(value);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002151 }
2152
2153 private:
Matteo Martincighe8485382019-10-10 14:08:21 +01002154 std::unordered_map<uint16_t, std::atomic<uint32_t>> m_Data;
Finn Williamsf4d59a62019-10-14 15:55:18 +01002155 uint16_t m_CounterSize;
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002156 };
2157
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002158 ProfilingStateMachine profilingStateMachine;
2159
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002160 Holder data;
2161 std::vector<uint16_t> captureIds1 = { 0, 1 };
2162 std::vector<uint16_t> captureIds2;
2163
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002164 MockBufferManager mockBuffer(512);
Sadik Armagan3896b472020-02-10 12:24:15 +00002165 SendCounterPacket sendCounterPacket(mockBuffer);
2166 SendThread sendThread(profilingStateMachine, mockBuffer, sendCounterPacket);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002167
2168 std::vector<uint16_t> counterIds;
Finn Williamsf4d59a62019-10-14 15:55:18 +01002169 CaptureReader captureReader(2);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002170
Keith Davis3201eea2019-10-24 17:30:41 +01002171 unsigned int valueA = 10;
2172 unsigned int valueB = 15;
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002173 unsigned int numSteps = 5;
2174
2175 PeriodicCounterCapture periodicCounterCapture(std::ref(data), std::ref(sendCounterPacket), captureReader);
2176
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002177 for (unsigned int i = 0; i < numSteps; ++i)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002178 {
2179 data.SetCaptureData(1, captureIds1);
2180 captureReader.SetCounterValue(0, valueA * (i + 1));
2181 captureReader.SetCounterValue(1, valueB * (i + 1));
2182
2183 periodicCounterCapture.Start();
Finn Williamsf4d59a62019-10-14 15:55:18 +01002184 periodicCounterCapture.Stop();
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002185 }
2186
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002187 auto buffer = mockBuffer.GetReadableBuffer();
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002188
2189 uint32_t headerWord0 = ReadUint32(buffer, 0);
2190 uint32_t headerWord1 = ReadUint32(buffer, 4);
2191
Jim Flynnfc365622019-12-04 10:07:20 +00002192 BOOST_TEST(((headerWord0 >> 26) & 0x0000003F) == 3); // packet family
Keith Davis3201eea2019-10-24 17:30:41 +01002193 BOOST_TEST(((headerWord0 >> 19) & 0x0000007F) == 0); // packet class
2194 BOOST_TEST(((headerWord0 >> 16) & 0x00000007) == 0); // packet type
Matteo Martincigh8d9590e2019-10-15 09:35:29 +01002195 BOOST_TEST(headerWord1 == 20);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002196
Keith Davis3201eea2019-10-24 17:30:41 +01002197 uint32_t offset = 16;
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002198 uint16_t readIndex = ReadUint16(buffer, offset);
2199 BOOST_TEST(0 == readIndex);
2200
2201 offset += 2;
2202 uint32_t readValue = ReadUint32(buffer, offset);
2203 BOOST_TEST((valueA * numSteps) == readValue);
2204
2205 offset += 4;
2206 readIndex = ReadUint16(buffer, offset);
2207 BOOST_TEST(1 == readIndex);
2208
2209 offset += 2;
2210 readValue = ReadUint32(buffer, offset);
2211 BOOST_TEST((valueB * numSteps) == readValue);
2212}
2213
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002214BOOST_AUTO_TEST_CASE(RequestCounterDirectoryCommandHandlerTest1)
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002215{
2216 using boost::numeric_cast;
2217
Jim Flynn397043f2019-10-17 17:37:10 +01002218 const uint32_t familyId = 0;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002219 const uint32_t packetId = 3;
Keith Davis3201eea2019-10-24 17:30:41 +01002220 const uint32_t version = 1;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002221 ProfilingStateMachine profilingStateMachine;
2222 CounterDirectory counterDirectory;
Matteo Martincigh9723d022019-11-13 10:56:41 +00002223 MockBufferManager mockBuffer1(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002224 SendCounterPacket sendCounterPacket(mockBuffer1);
2225 SendThread sendThread(profilingStateMachine, mockBuffer1, sendCounterPacket);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002226 MockBufferManager mockBuffer2(1024);
2227 SendTimelinePacket sendTimelinePacket(mockBuffer2);
Keith Davis3201eea2019-10-24 17:30:41 +01002228 RequestCounterDirectoryCommandHandler commandHandler(familyId, packetId, version, counterDirectory,
Matteo Martincigh9723d022019-11-13 10:56:41 +00002229 sendCounterPacket, sendTimelinePacket, profilingStateMachine);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002230
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002231 const uint32_t wrongPacketId = 47;
Keith Davis3201eea2019-10-24 17:30:41 +01002232 const uint32_t wrongHeader = (wrongPacketId & 0x000003FF) << 16;
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002233
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002234 Packet wrongPacket(wrongHeader);
2235
2236 profilingStateMachine.TransitionToState(ProfilingState::Uninitialised);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002237 BOOST_CHECK_THROW(commandHandler(wrongPacket), armnn::RuntimeException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002238 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002239 BOOST_CHECK_THROW(commandHandler(wrongPacket), armnn::RuntimeException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002240 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002241 BOOST_CHECK_THROW(commandHandler(wrongPacket), armnn::RuntimeException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002242 profilingStateMachine.TransitionToState(ProfilingState::Active);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002243 BOOST_CHECK_THROW(commandHandler(wrongPacket), armnn::InvalidArgumentException); // Wrong packet
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002244
2245 const uint32_t rightHeader = (packetId & 0x000003FF) << 16;
2246
2247 Packet rightPacket(rightHeader);
2248
Matteo Martincigh9723d022019-11-13 10:56:41 +00002249 BOOST_CHECK_NO_THROW(commandHandler(rightPacket)); // Right packet
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002250
Matteo Martincigh9723d022019-11-13 10:56:41 +00002251 auto readBuffer1 = mockBuffer1.GetReadableBuffer();
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002252
Matteo Martincigh9723d022019-11-13 10:56:41 +00002253 uint32_t header1Word0 = ReadUint32(readBuffer1, 0);
2254 uint32_t header1Word1 = ReadUint32(readBuffer1, 4);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002255
Matteo Martincigh9723d022019-11-13 10:56:41 +00002256 // Counter directory packet
2257 BOOST_TEST(((header1Word0 >> 26) & 0x0000003F) == 0); // packet family
2258 BOOST_TEST(((header1Word0 >> 16) & 0x000003FF) == 2); // packet id
2259 BOOST_TEST(header1Word1 == 24); // data length
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002260
Matteo Martincigh9723d022019-11-13 10:56:41 +00002261 uint32_t bodyHeader1Word0 = ReadUint32(readBuffer1, 8);
2262 uint16_t deviceRecordCount = numeric_cast<uint16_t>(bodyHeader1Word0 >> 16);
2263 BOOST_TEST(deviceRecordCount == 0); // device_records_count
2264
2265 auto readBuffer2 = mockBuffer2.GetReadableBuffer();
2266
2267 uint32_t header2Word0 = ReadUint32(readBuffer2, 0);
2268 uint32_t header2Word1 = ReadUint32(readBuffer2, 4);
2269
2270 // Timeline message directory packet
2271 BOOST_TEST(((header2Word0 >> 26) & 0x0000003F) == 1); // packet family
2272 BOOST_TEST(((header2Word0 >> 16) & 0x000003FF) == 0); // packet id
2273 BOOST_TEST(header2Word1 == 419); // data length
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002274}
2275
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002276BOOST_AUTO_TEST_CASE(RequestCounterDirectoryCommandHandlerTest2)
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002277{
2278 using boost::numeric_cast;
2279
Jim Flynn397043f2019-10-17 17:37:10 +01002280 const uint32_t familyId = 0;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002281 const uint32_t packetId = 3;
Keith Davis3201eea2019-10-24 17:30:41 +01002282 const uint32_t version = 1;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002283 ProfilingStateMachine profilingStateMachine;
2284 CounterDirectory counterDirectory;
Matteo Martincigh9723d022019-11-13 10:56:41 +00002285 MockBufferManager mockBuffer1(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002286 SendCounterPacket sendCounterPacket(mockBuffer1);
2287 SendThread sendThread(profilingStateMachine, mockBuffer1, sendCounterPacket);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002288 MockBufferManager mockBuffer2(1024);
2289 SendTimelinePacket sendTimelinePacket(mockBuffer2);
Keith Davis3201eea2019-10-24 17:30:41 +01002290 RequestCounterDirectoryCommandHandler commandHandler(familyId, packetId, version, counterDirectory,
Matteo Martincigh9723d022019-11-13 10:56:41 +00002291 sendCounterPacket, sendTimelinePacket, profilingStateMachine);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002292 const uint32_t header = (packetId & 0x000003FF) << 16;
2293 Packet packet(header);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002294
Matteo Martincigh9723d022019-11-13 10:56:41 +00002295 const Device* device = counterDirectory.RegisterDevice("deviceA", 1);
2296 BOOST_CHECK(device != nullptr);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002297 const CounterSet* counterSet = counterDirectory.RegisterCounterSet("countersetA");
Matteo Martincigh9723d022019-11-13 10:56:41 +00002298 BOOST_CHECK(counterSet != nullptr);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002299 counterDirectory.RegisterCategory("categoryA", device->m_Uid, counterSet->m_Uid);
Keith Davise394bd92019-12-02 15:12:19 +00002300 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID, 24,
2301 "categoryA", 0, 1, 2.0f, "counterA", "descA");
2302 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID, 25,
2303 "categoryA", 1, 1, 3.0f, "counterB", "descB");
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002304
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002305 profilingStateMachine.TransitionToState(ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002306 BOOST_CHECK_THROW(commandHandler(packet), armnn::RuntimeException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002307 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002308 BOOST_CHECK_THROW(commandHandler(packet), armnn::RuntimeException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002309 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002310 BOOST_CHECK_THROW(commandHandler(packet), armnn::RuntimeException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002311 profilingStateMachine.TransitionToState(ProfilingState::Active);
2312 BOOST_CHECK_NO_THROW(commandHandler(packet));
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002313
Matteo Martincigh9723d022019-11-13 10:56:41 +00002314 auto readBuffer1 = mockBuffer1.GetReadableBuffer();
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002315
Matteo Martincigh9723d022019-11-13 10:56:41 +00002316 uint32_t header1Word0 = ReadUint32(readBuffer1, 0);
2317 uint32_t header1Word1 = ReadUint32(readBuffer1, 4);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002318
Matteo Martincigh9723d022019-11-13 10:56:41 +00002319 BOOST_TEST(((header1Word0 >> 26) & 0x0000003F) == 0); // packet family
2320 BOOST_TEST(((header1Word0 >> 16) & 0x000003FF) == 2); // packet id
2321 BOOST_TEST(header1Word1 == 240); // data length
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002322
Matteo Martincigh9723d022019-11-13 10:56:41 +00002323 uint32_t bodyHeader1Word0 = ReadUint32(readBuffer1, 8);
2324 uint32_t bodyHeader1Word1 = ReadUint32(readBuffer1, 12);
2325 uint32_t bodyHeader1Word2 = ReadUint32(readBuffer1, 16);
2326 uint32_t bodyHeader1Word3 = ReadUint32(readBuffer1, 20);
2327 uint32_t bodyHeader1Word4 = ReadUint32(readBuffer1, 24);
2328 uint32_t bodyHeader1Word5 = ReadUint32(readBuffer1, 28);
2329 uint16_t deviceRecordCount = numeric_cast<uint16_t>(bodyHeader1Word0 >> 16);
2330 uint16_t counterSetRecordCount = numeric_cast<uint16_t>(bodyHeader1Word2 >> 16);
2331 uint16_t categoryRecordCount = numeric_cast<uint16_t>(bodyHeader1Word4 >> 16);
2332 BOOST_TEST(deviceRecordCount == 1); // device_records_count
2333 BOOST_TEST(bodyHeader1Word1 == 0); // device_records_pointer_table_offset
2334 BOOST_TEST(counterSetRecordCount == 1); // counter_set_count
2335 BOOST_TEST(bodyHeader1Word3 == 4); // counter_set_pointer_table_offset
2336 BOOST_TEST(categoryRecordCount == 1); // categories_count
2337 BOOST_TEST(bodyHeader1Word5 == 8); // categories_pointer_table_offset
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002338
Matteo Martincigh9723d022019-11-13 10:56:41 +00002339 uint32_t deviceRecordOffset = ReadUint32(readBuffer1, 32);
Keith Davis3201eea2019-10-24 17:30:41 +01002340 BOOST_TEST(deviceRecordOffset == 0);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002341
Matteo Martincigh9723d022019-11-13 10:56:41 +00002342 uint32_t counterSetRecordOffset = ReadUint32(readBuffer1, 36);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002343 BOOST_TEST(counterSetRecordOffset == 20);
2344
Matteo Martincigh9723d022019-11-13 10:56:41 +00002345 uint32_t categoryRecordOffset = ReadUint32(readBuffer1, 40);
Keith Davis3201eea2019-10-24 17:30:41 +01002346 BOOST_TEST(categoryRecordOffset == 44);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002347
2348 auto readBuffer2 = mockBuffer2.GetReadableBuffer();
2349
2350 uint32_t header2Word0 = ReadUint32(readBuffer2, 0);
2351 uint32_t header2Word1 = ReadUint32(readBuffer2, 4);
2352
2353 // Timeline message directory packet
2354 BOOST_TEST(((header2Word0 >> 26) & 0x0000003F) == 1); // packet family
2355 BOOST_TEST(((header2Word0 >> 16) & 0x000003FF) == 0); // packet id
2356 BOOST_TEST(header2Word1 == 419); // data length
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002357}
2358
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002359BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodConnectionAcknowledgedPacket)
2360{
Matteo Martincighd0613b52019-10-09 16:47:04 +01002361 // Swap the profiling connection factory in the profiling service instance with our mock one
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002362 SwapProfilingConnectionFactoryHelper helper;
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002363
2364 // Calculate the size of a Stream Metadata packet
Keith Davis3201eea2019-10-24 17:30:41 +01002365 std::string processName = GetProcessName().substr(0, 60);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002366 unsigned int processNameSize = processName.empty() ? 0 : boost::numeric_cast<unsigned int>(processName.size()) + 1;
2367 unsigned int streamMetadataPacketsize = 118 + processNameSize;
2368
Jim Flynn53e46992019-10-14 12:31:10 +01002369 // Reset the profiling service to the uninitialized state
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002370 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002371 options.m_EnableProfiling = true;
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002372 ProfilingService& profilingService = ProfilingService::Instance();
2373 profilingService.ResetExternalProfilingOptions(options, true);
2374
2375 // Bring the profiling service to the "WaitingForAck" state
2376 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002377 profilingService.Update(); // Initialize the counter directory
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002378 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002379 profilingService.Update(); // Create the profiling connection
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002380
Matteo Martincighd0613b52019-10-09 16:47:04 +01002381 // Get the mock profiling connection
2382 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2383 BOOST_CHECK(mockProfilingConnection);
2384
Matteo Martincighe8485382019-10-10 14:08:21 +01002385 // Remove the packets received so far
2386 mockProfilingConnection->Clear();
2387
2388 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002389 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002390
2391 // Wait for the Stream Metadata packet to be sent
Finn Williams09ad6f92019-12-19 17:05:18 +00002392 BOOST_CHECK(helper.WaitForPacketsSent(
2393 mockProfilingConnection, PacketType::StreamMetaData, streamMetadataPacketsize) >= 1);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002394
2395 // Write a valid "Connection Acknowledged" packet into the mock profiling connection, to simulate a valid
2396 // reply from an external profiling service
2397
2398 // Connection Acknowledged Packet header (word 0, word 1 is always zero):
2399 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2400 // 16:25 [10] packet_id: Packet identifier, value 0b0000000001
2401 // 8:15 [8] reserved: Reserved, value 0b00000000
2402 // 0:7 [8] reserved: Reserved, value 0b00000000
2403 uint32_t packetFamily = 0;
2404 uint32_t packetId = 1;
Keith Davis3201eea2019-10-24 17:30:41 +01002405 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002406
Matteo Martincighd0613b52019-10-09 16:47:04 +01002407 // Create the Connection Acknowledged Packet
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002408 Packet connectionAcknowledgedPacket(header);
2409
2410 // Write the packet to the mock profiling connection
2411 mockProfilingConnection->WritePacket(std::move(connectionAcknowledgedPacket));
2412
Colm Donelan2ba48d22019-11-29 09:10:59 +00002413 // Wait for the counter directory packet to ensure the ConnectionAcknowledgedCommandHandler has run.
Finn Williams09ad6f92019-12-19 17:05:18 +00002414 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::CounterDirectory) == 1);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002415
2416 // The Connection Acknowledged Command Handler should have updated the profiling state accordingly
2417 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincighd0613b52019-10-09 16:47:04 +01002418
2419 // Reset the profiling service to stop any running thread
2420 options.m_EnableProfiling = false;
2421 profilingService.ResetExternalProfilingOptions(options, true);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002422}
2423
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002424BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodRequestCounterDirectoryPacket)
2425{
2426 // Swap the profiling connection factory in the profiling service instance with our mock one
2427 SwapProfilingConnectionFactoryHelper helper;
2428
2429 // Reset the profiling service to the uninitialized state
2430 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002431 options.m_EnableProfiling = true;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002432 ProfilingService& profilingService = ProfilingService::Instance();
2433 profilingService.ResetExternalProfilingOptions(options, true);
2434
2435 // Bring the profiling service to the "Active" state
2436 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002437 profilingService.Update(); // Initialize the counter directory
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002438 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002439 profilingService.Update(); // Create the profiling connection
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002440 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002441 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002442
Colm Donelan2ba48d22019-11-29 09:10:59 +00002443 // Get the mock profiling connection
2444 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2445 BOOST_CHECK(mockProfilingConnection);
2446
Matteo Martincighe8485382019-10-10 14:08:21 +01002447 // Force the profiling service to the "Active" state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002448 helper.ForceTransitionToState(ProfilingState::Active);
2449 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2450
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002451 // Write a valid "Request Counter Directory" packet into the mock profiling connection, to simulate a valid
2452 // reply from an external profiling service
2453
2454 // Request Counter Directory packet header (word 0, word 1 is always zero):
2455 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2456 // 16:25 [10] packet_id: Packet identifier, value 0b0000000011
2457 // 8:15 [8] reserved: Reserved, value 0b00000000
2458 // 0:7 [8] reserved: Reserved, value 0b00000000
2459 uint32_t packetFamily = 0;
2460 uint32_t packetId = 3;
Keith Davis3201eea2019-10-24 17:30:41 +01002461 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002462
2463 // Create the Request Counter Directory packet
2464 Packet requestCounterDirectoryPacket(header);
2465
2466 // Write the packet to the mock profiling connection
2467 mockProfilingConnection->WritePacket(std::move(requestCounterDirectoryPacket));
2468
Finn Williams09ad6f92019-12-19 17:05:18 +00002469 // Expecting one CounterDirectory Packet of length 656
2470 // and one TimelineMessageDirectory packet of length 427
2471 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::CounterDirectory, 656) == 1);
2472 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::TimelineMessageDirectory, 427) == 1);
Matteo Martincighe8485382019-10-10 14:08:21 +01002473
2474 // The Request Counter Directory Command Handler should not have updated the profiling state
2475 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2476
2477 // Reset the profiling service to stop any running thread
2478 options.m_EnableProfiling = false;
2479 profilingService.ResetExternalProfilingOptions(options, true);
2480}
2481
Matteo Martincighe8485382019-10-10 14:08:21 +01002482BOOST_AUTO_TEST_CASE(CheckProfilingServiceBadPeriodicCounterSelectionPacketInvalidCounterUid)
2483{
Matteo Martincighe8485382019-10-10 14:08:21 +01002484 // Swap the profiling connection factory in the profiling service instance with our mock one
2485 SwapProfilingConnectionFactoryHelper helper;
2486
2487 // Reset the profiling service to the uninitialized state
2488 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002489 options.m_EnableProfiling = true;
Matteo Martincighe8485382019-10-10 14:08:21 +01002490 ProfilingService& profilingService = ProfilingService::Instance();
2491 profilingService.ResetExternalProfilingOptions(options, true);
2492
2493 // Bring the profiling service to the "Active" state
2494 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002495 profilingService.Update(); // Initialize the counter directory
Matteo Martincighe8485382019-10-10 14:08:21 +01002496 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002497 profilingService.Update(); // Create the profiling connection
Matteo Martincighe8485382019-10-10 14:08:21 +01002498 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002499 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002500
Colm Donelan2ba48d22019-11-29 09:10:59 +00002501 // Get the mock profiling connection
2502 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2503 BOOST_CHECK(mockProfilingConnection);
2504
Matteo Martincighe8485382019-10-10 14:08:21 +01002505 // Force the profiling service to the "Active" state
2506 helper.ForceTransitionToState(ProfilingState::Active);
2507 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2508
Matteo Martincighe8485382019-10-10 14:08:21 +01002509 // Remove the packets received so far
2510 mockProfilingConnection->Clear();
2511
2512 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2513 // external profiling service
2514
2515 // Periodic Counter Selection packet header:
2516 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2517 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2518 // 8:15 [8] reserved: Reserved, value 0b00000000
2519 // 0:7 [8] reserved: Reserved, value 0b00000000
2520 uint32_t packetFamily = 0;
2521 uint32_t packetId = 4;
Keith Davis3201eea2019-10-24 17:30:41 +01002522 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincighe8485382019-10-10 14:08:21 +01002523
Keith Davis3201eea2019-10-24 17:30:41 +01002524 uint32_t capturePeriod = 123456; // Some capture period (microseconds)
Matteo Martincighe8485382019-10-10 14:08:21 +01002525
2526 // Get the first valid counter UID
2527 const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
Keith Davis3201eea2019-10-24 17:30:41 +01002528 const Counters& counters = counterDirectory.GetCounters();
Matteo Martincighe8485382019-10-10 14:08:21 +01002529 BOOST_CHECK(counters.size() > 1);
Keith Davis3201eea2019-10-24 17:30:41 +01002530 uint16_t counterUidA = counters.begin()->first; // First valid counter UID
2531 uint16_t counterUidB = 9999; // Second invalid counter UID
Matteo Martincighe8485382019-10-10 14:08:21 +01002532
2533 uint32_t length = 8;
2534
2535 auto data = std::make_unique<unsigned char[]>(length);
2536 WriteUint32(data.get(), 0, capturePeriod);
2537 WriteUint16(data.get(), 4, counterUidA);
2538 WriteUint16(data.get(), 6, counterUidB);
2539
2540 // Create the Periodic Counter Selection packet
Keith Davis3201eea2019-10-24 17:30:41 +01002541 Packet periodicCounterSelectionPacket(header, length, data); // Length > 0, this will start the Period Counter
2542 // Capture thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002543
2544 // Write the packet to the mock profiling connection
2545 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2546
Finn Williams09ad6f92019-12-19 17:05:18 +00002547 // Expecting one Periodic Counter Selection packet of length 14
2548 // and at least one Periodic Counter Capture packet of length 22
2549 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterSelection, 14) == 1);
2550 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterCapture, 22) >= 1);
Matteo Martincighe8485382019-10-10 14:08:21 +01002551
2552 // The Periodic Counter Selection Handler should not have updated the profiling state
2553 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2554
2555 // Reset the profiling service to stop any running thread
2556 options.m_EnableProfiling = false;
2557 profilingService.ResetExternalProfilingOptions(options, true);
2558}
2559
2560BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodPeriodicCounterSelectionPacketNoCounters)
2561{
2562 // Swap the profiling connection factory in the profiling service instance with our mock one
2563 SwapProfilingConnectionFactoryHelper helper;
2564
2565 // Reset the profiling service to the uninitialized state
2566 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002567 options.m_EnableProfiling = true;
Matteo Martincighe8485382019-10-10 14:08:21 +01002568 ProfilingService& profilingService = ProfilingService::Instance();
2569 profilingService.ResetExternalProfilingOptions(options, true);
2570
2571 // Bring the profiling service to the "Active" state
2572 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002573 profilingService.Update(); // Initialize the counter directory
Matteo Martincighe8485382019-10-10 14:08:21 +01002574 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002575 profilingService.Update(); // Create the profiling connection
Matteo Martincighe8485382019-10-10 14:08:21 +01002576 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002577 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002578
Colm Donelan2ba48d22019-11-29 09:10:59 +00002579 // Get the mock profiling connection
2580 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2581 BOOST_CHECK(mockProfilingConnection);
2582
Matteo Martincighe8485382019-10-10 14:08:21 +01002583 // Wait for the Stream Metadata packet the be sent
2584 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Finn Williams09ad6f92019-12-19 17:05:18 +00002585 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData);
Matteo Martincighe8485382019-10-10 14:08:21 +01002586
2587 // Force the profiling service to the "Active" state
2588 helper.ForceTransitionToState(ProfilingState::Active);
2589 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2590
Matteo Martincighe8485382019-10-10 14:08:21 +01002591 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2592 // external profiling service
2593
2594 // Periodic Counter Selection packet header:
2595 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2596 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2597 // 8:15 [8] reserved: Reserved, value 0b00000000
2598 // 0:7 [8] reserved: Reserved, value 0b00000000
2599 uint32_t packetFamily = 0;
2600 uint32_t packetId = 4;
Keith Davis3201eea2019-10-24 17:30:41 +01002601 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincighe8485382019-10-10 14:08:21 +01002602
2603 // Create the Periodic Counter Selection packet
Keith Davis3201eea2019-10-24 17:30:41 +01002604 Packet periodicCounterSelectionPacket(header); // Length == 0, this will disable the collection of counters
Matteo Martincighe8485382019-10-10 14:08:21 +01002605
2606 // Write the packet to the mock profiling connection
2607 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2608
Finn Williams09ad6f92019-12-19 17:05:18 +00002609 // Wait for the Periodic Counter Selection packet of length 12 to be sent
2610 // The size of the expected Periodic Counter Selection (echos the sent one)
2611 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterSelection, 12) == 1);
Matteo Martincighe8485382019-10-10 14:08:21 +01002612
2613 // The Periodic Counter Selection Handler should not have updated the profiling state
2614 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2615
Finn Williams09ad6f92019-12-19 17:05:18 +00002616 // No Periodic Counter packets are expected
2617 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterCapture, 0, 0) == 0);
Matteo Martincighe8485382019-10-10 14:08:21 +01002618
2619 // Reset the profiling service to stop any running thread
2620 options.m_EnableProfiling = false;
2621 profilingService.ResetExternalProfilingOptions(options, true);
2622}
2623
2624BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodPeriodicCounterSelectionPacketSingleCounter)
2625{
2626 // Swap the profiling connection factory in the profiling service instance with our mock one
2627 SwapProfilingConnectionFactoryHelper helper;
2628
2629 // Reset the profiling service to the uninitialized state
2630 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002631 options.m_EnableProfiling = true;
Matteo Martincighe8485382019-10-10 14:08:21 +01002632 ProfilingService& profilingService = ProfilingService::Instance();
2633 profilingService.ResetExternalProfilingOptions(options, true);
2634
2635 // Bring the profiling service to the "Active" state
2636 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002637 profilingService.Update(); // Initialize the counter directory
Matteo Martincighe8485382019-10-10 14:08:21 +01002638 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002639 profilingService.Update(); // Create the profiling connection
Matteo Martincighe8485382019-10-10 14:08:21 +01002640 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002641 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002642
Colm Donelan2ba48d22019-11-29 09:10:59 +00002643 // Get the mock profiling connection
2644 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2645 BOOST_CHECK(mockProfilingConnection);
2646
Finn Williams09ad6f92019-12-19 17:05:18 +00002647 // Wait for the Stream Metadata packet to be sent
Matteo Martincighe8485382019-10-10 14:08:21 +01002648 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Finn Williams09ad6f92019-12-19 17:05:18 +00002649 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData);
Matteo Martincighe8485382019-10-10 14:08:21 +01002650
2651 // Force the profiling service to the "Active" state
2652 helper.ForceTransitionToState(ProfilingState::Active);
2653 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2654
Matteo Martincighe8485382019-10-10 14:08:21 +01002655 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2656 // external profiling service
2657
2658 // Periodic Counter Selection packet header:
2659 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2660 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2661 // 8:15 [8] reserved: Reserved, value 0b00000000
2662 // 0:7 [8] reserved: Reserved, value 0b00000000
2663 uint32_t packetFamily = 0;
2664 uint32_t packetId = 4;
Keith Davis3201eea2019-10-24 17:30:41 +01002665 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincighe8485382019-10-10 14:08:21 +01002666
Keith Davis3201eea2019-10-24 17:30:41 +01002667 uint32_t capturePeriod = 123456; // Some capture period (microseconds)
Matteo Martincighe8485382019-10-10 14:08:21 +01002668
2669 // Get the first valid counter UID
2670 const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
Keith Davis3201eea2019-10-24 17:30:41 +01002671 const Counters& counters = counterDirectory.GetCounters();
Matteo Martincighe8485382019-10-10 14:08:21 +01002672 BOOST_CHECK(!counters.empty());
Keith Davis3201eea2019-10-24 17:30:41 +01002673 uint16_t counterUid = counters.begin()->first; // Valid counter UID
Matteo Martincighe8485382019-10-10 14:08:21 +01002674
2675 uint32_t length = 6;
2676
2677 auto data = std::make_unique<unsigned char[]>(length);
2678 WriteUint32(data.get(), 0, capturePeriod);
2679 WriteUint16(data.get(), 4, counterUid);
2680
2681 // Create the Periodic Counter Selection packet
Keith Davis3201eea2019-10-24 17:30:41 +01002682 Packet periodicCounterSelectionPacket(header, length, data); // Length > 0, this will start the Period Counter
2683 // Capture thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002684
2685 // Write the packet to the mock profiling connection
2686 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2687
Finn Williams09ad6f92019-12-19 17:05:18 +00002688 // Expecting one Periodic Counter Selection packet of length 14
2689 // and at least one Periodic Counter Capture packet of length 22
2690 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterSelection, 14) == 1);
2691 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterCapture, 22) >= 1);
Matteo Martincighe8485382019-10-10 14:08:21 +01002692
2693 // The Periodic Counter Selection Handler should not have updated the profiling state
2694 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2695
2696 // Reset the profiling service to stop any running thread
2697 options.m_EnableProfiling = false;
2698 profilingService.ResetExternalProfilingOptions(options, true);
2699}
2700
2701BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodPeriodicCounterSelectionPacketMultipleCounters)
2702{
2703 // Swap the profiling connection factory in the profiling service instance with our mock one
2704 SwapProfilingConnectionFactoryHelper helper;
Matteo Martincighe8485382019-10-10 14:08:21 +01002705 // Reset the profiling service to the uninitialized state
2706 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002707 options.m_EnableProfiling = true;
Matteo Martincighe8485382019-10-10 14:08:21 +01002708 ProfilingService& profilingService = ProfilingService::Instance();
2709 profilingService.ResetExternalProfilingOptions(options, true);
2710
2711 // Bring the profiling service to the "Active" state
2712 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002713 profilingService.Update(); // Initialize the counter directory
Matteo Martincighe8485382019-10-10 14:08:21 +01002714 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002715 profilingService.Update(); // Create the profiling connection
Matteo Martincighe8485382019-10-10 14:08:21 +01002716 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002717 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002718
Colm Donelan2ba48d22019-11-29 09:10:59 +00002719 // Get the mock profiling connection
2720 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2721 BOOST_CHECK(mockProfilingConnection);
2722
Matteo Martincighe8485382019-10-10 14:08:21 +01002723 // Wait for the Stream Metadata packet the be sent
2724 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Finn Williams09ad6f92019-12-19 17:05:18 +00002725 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData);
Matteo Martincighe8485382019-10-10 14:08:21 +01002726
2727 // Force the profiling service to the "Active" state
2728 helper.ForceTransitionToState(ProfilingState::Active);
2729 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2730
Matteo Martincighe8485382019-10-10 14:08:21 +01002731 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2732 // external profiling service
2733
2734 // Periodic Counter Selection packet header:
2735 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2736 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2737 // 8:15 [8] reserved: Reserved, value 0b00000000
2738 // 0:7 [8] reserved: Reserved, value 0b00000000
2739 uint32_t packetFamily = 0;
2740 uint32_t packetId = 4;
Keith Davis3201eea2019-10-24 17:30:41 +01002741 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincighe8485382019-10-10 14:08:21 +01002742
Keith Davis3201eea2019-10-24 17:30:41 +01002743 uint32_t capturePeriod = 123456; // Some capture period (microseconds)
Matteo Martincighe8485382019-10-10 14:08:21 +01002744
2745 // Get the first valid counter UID
2746 const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
Keith Davis3201eea2019-10-24 17:30:41 +01002747 const Counters& counters = counterDirectory.GetCounters();
Matteo Martincighe8485382019-10-10 14:08:21 +01002748 BOOST_CHECK(counters.size() > 1);
Keith Davis3201eea2019-10-24 17:30:41 +01002749 uint16_t counterUidA = counters.begin()->first; // First valid counter UID
2750 uint16_t counterUidB = (counters.begin()++)->first; // Second valid counter UID
Matteo Martincighe8485382019-10-10 14:08:21 +01002751
2752 uint32_t length = 8;
2753
2754 auto data = std::make_unique<unsigned char[]>(length);
2755 WriteUint32(data.get(), 0, capturePeriod);
2756 WriteUint16(data.get(), 4, counterUidA);
2757 WriteUint16(data.get(), 6, counterUidB);
2758
2759 // Create the Periodic Counter Selection packet
Keith Davis3201eea2019-10-24 17:30:41 +01002760 Packet periodicCounterSelectionPacket(header, length, data); // Length > 0, this will start the Period Counter
2761 // Capture thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002762
2763 // Write the packet to the mock profiling connection
2764 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2765
Finn Williams09ad6f92019-12-19 17:05:18 +00002766 // Expecting one PeriodicCounterSelection Packet with a length of 16
2767 // And at least one PeriodicCounterCapture Packet with a length of 28
2768 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterSelection, 16) == 1);
2769 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterCapture, 28) >= 1);
Matteo Martincighe8485382019-10-10 14:08:21 +01002770
2771 // The Periodic Counter Selection Handler should not have updated the profiling state
2772 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002773
2774 // Reset the profiling service to stop any running thread
2775 options.m_EnableProfiling = false;
2776 profilingService.ResetExternalProfilingOptions(options, true);
2777}
2778
Jim Flynn53e46992019-10-14 12:31:10 +01002779BOOST_AUTO_TEST_CASE(CheckProfilingServiceDisconnect)
2780{
2781 // Swap the profiling connection factory in the profiling service instance with our mock one
2782 SwapProfilingConnectionFactoryHelper helper;
Jim Flynn53e46992019-10-14 12:31:10 +01002783 // Reset the profiling service to the uninitialized state
2784 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002785 options.m_EnableProfiling = true;
Jim Flynn53e46992019-10-14 12:31:10 +01002786 ProfilingService& profilingService = ProfilingService::Instance();
2787 profilingService.ResetExternalProfilingOptions(options, true);
2788
2789 // Try to disconnect the profiling service while in the "Uninitialised" state
2790 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
2791 profilingService.Disconnect();
Keith Davis3201eea2019-10-24 17:30:41 +01002792 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised); // The state should not change
Jim Flynn53e46992019-10-14 12:31:10 +01002793
2794 // Try to disconnect the profiling service while in the "NotConnected" state
Keith Davis3201eea2019-10-24 17:30:41 +01002795 profilingService.Update(); // Initialize the counter directory
Jim Flynn53e46992019-10-14 12:31:10 +01002796 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
2797 profilingService.Disconnect();
Keith Davis3201eea2019-10-24 17:30:41 +01002798 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected); // The state should not change
Jim Flynn53e46992019-10-14 12:31:10 +01002799
2800 // Try to disconnect the profiling service while in the "WaitingForAck" state
Keith Davis3201eea2019-10-24 17:30:41 +01002801 profilingService.Update(); // Create the profiling connection
Jim Flynn53e46992019-10-14 12:31:10 +01002802 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
2803 profilingService.Disconnect();
Keith Davis3201eea2019-10-24 17:30:41 +01002804 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck); // The state should not change
Jim Flynn53e46992019-10-14 12:31:10 +01002805
2806 // Try to disconnect the profiling service while in the "Active" state
Keith Davis3201eea2019-10-24 17:30:41 +01002807 profilingService.Update(); // Start the command handler and the send thread
Jim Flynn53e46992019-10-14 12:31:10 +01002808
Colm Donelan2ba48d22019-11-29 09:10:59 +00002809 // Get the mock profiling connection
2810 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2811 BOOST_CHECK(mockProfilingConnection);
2812
Jim Flynn53e46992019-10-14 12:31:10 +01002813 // Wait for the Stream Metadata packet the be sent
2814 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Finn Williams09ad6f92019-12-19 17:05:18 +00002815 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData);
Jim Flynn53e46992019-10-14 12:31:10 +01002816
2817 // Force the profiling service to the "Active" state
2818 helper.ForceTransitionToState(ProfilingState::Active);
2819 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2820
Jim Flynn53e46992019-10-14 12:31:10 +01002821 // Check that the profiling connection is open
2822 BOOST_CHECK(mockProfilingConnection->IsOpen());
2823
2824 profilingService.Disconnect();
Colm Donelan2ba48d22019-11-29 09:10:59 +00002825 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected); // The state should have changed
Jim Flynn53e46992019-10-14 12:31:10 +01002826
2827 // Check that the profiling connection has been reset
2828 mockProfilingConnection = helper.GetMockProfilingConnection();
2829 BOOST_CHECK(mockProfilingConnection == nullptr);
2830
2831 // Reset the profiling service to stop any running thread
2832 options.m_EnableProfiling = false;
2833 profilingService.ResetExternalProfilingOptions(options, true);
2834}
2835
Matteo Martincigh994b5342019-10-11 17:19:56 +01002836BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodPerJobCounterSelectionPacket)
2837{
2838 // Swap the profiling connection factory in the profiling service instance with our mock one
2839 SwapProfilingConnectionFactoryHelper helper;
Matteo Martincigh994b5342019-10-11 17:19:56 +01002840 // Reset the profiling service to the uninitialized state
2841 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002842 options.m_EnableProfiling = true;
Matteo Martincigh994b5342019-10-11 17:19:56 +01002843 ProfilingService& profilingService = ProfilingService::Instance();
2844 profilingService.ResetExternalProfilingOptions(options, true);
2845
2846 // Bring the profiling service to the "Active" state
2847 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002848 profilingService.Update(); // Initialize the counter directory
Matteo Martincigh994b5342019-10-11 17:19:56 +01002849 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002850 profilingService.Update(); // Create the profiling connection
Matteo Martincigh994b5342019-10-11 17:19:56 +01002851 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002852 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincigh994b5342019-10-11 17:19:56 +01002853
Colm Donelan2ba48d22019-11-29 09:10:59 +00002854 // Get the mock profiling connection
2855 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2856 BOOST_CHECK(mockProfilingConnection);
2857
Matteo Martincigh994b5342019-10-11 17:19:56 +01002858 // Wait for the Stream Metadata packet the be sent
2859 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Finn Williams09ad6f92019-12-19 17:05:18 +00002860 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData);
Matteo Martincigh994b5342019-10-11 17:19:56 +01002861
2862 // Force the profiling service to the "Active" state
2863 helper.ForceTransitionToState(ProfilingState::Active);
2864 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2865
Matteo Martincigh994b5342019-10-11 17:19:56 +01002866 // Write a "Per-Job Counter Selection" packet into the mock profiling connection, to simulate an input from an
2867 // external profiling service
2868
2869 // Per-Job Counter Selection packet header:
2870 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2871 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2872 // 8:15 [8] reserved: Reserved, value 0b00000000
2873 // 0:7 [8] reserved: Reserved, value 0b00000000
2874 uint32_t packetFamily = 0;
2875 uint32_t packetId = 5;
Keith Davis3201eea2019-10-24 17:30:41 +01002876 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincigh994b5342019-10-11 17:19:56 +01002877
2878 // Create the Per-Job Counter Selection packet
Keith Davis3201eea2019-10-24 17:30:41 +01002879 Packet periodicCounterSelectionPacket(header); // Length == 0, this will disable the collection of counters
Matteo Martincigh994b5342019-10-11 17:19:56 +01002880
2881 // Write the packet to the mock profiling connection
2882 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2883
2884 // Wait for a bit (must at least be the delay value of the mock profiling connection) to make sure that
2885 // the Per-Job Counter Selection packet gets processed by the profiling service
Colm Donelan2ba48d22019-11-29 09:10:59 +00002886 std::this_thread::sleep_for(std::chrono::milliseconds(5));
Matteo Martincigh994b5342019-10-11 17:19:56 +01002887
Matteo Martincigh994b5342019-10-11 17:19:56 +01002888 // The Per-Job Counter Selection Command Handler should not have updated the profiling state
2889 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2890
Finn Williams09ad6f92019-12-19 17:05:18 +00002891 // The Per-Job Counter Selection packets are dropped silently, so there should be no reply coming
2892 // from the profiling service
2893 const auto StreamMetaDataSize = static_cast<unsigned long>(
2894 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData, 0, 0));
2895 BOOST_CHECK(StreamMetaDataSize == mockProfilingConnection->GetWrittenDataSize());
2896
Matteo Martincigh994b5342019-10-11 17:19:56 +01002897 // Reset the profiling service to stop any running thread
2898 options.m_EnableProfiling = false;
2899 profilingService.ResetExternalProfilingOptions(options, true);
2900}
2901
Jim Flynn672d06e2019-10-15 10:18:11 +01002902BOOST_AUTO_TEST_CASE(CheckConfigureProfilingServiceOn)
2903{
2904 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002905 options.m_EnableProfiling = true;
Jim Flynn672d06e2019-10-15 10:18:11 +01002906 ProfilingService& profilingService = ProfilingService::Instance();
2907 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
2908 profilingService.ConfigureProfilingService(options);
2909 // should get as far as NOT_CONNECTED
2910 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
2911 // Reset the profiling service to stop any running thread
2912 options.m_EnableProfiling = false;
2913 profilingService.ResetExternalProfilingOptions(options, true);
2914}
2915
2916BOOST_AUTO_TEST_CASE(CheckConfigureProfilingServiceOff)
2917{
2918 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
2919 ProfilingService& profilingService = ProfilingService::Instance();
2920 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
2921 profilingService.ConfigureProfilingService(options);
2922 // should not move from Uninitialised
2923 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
2924 // Reset the profiling service to stop any running thread
2925 options.m_EnableProfiling = false;
2926 profilingService.ResetExternalProfilingOptions(options, true);
2927}
2928
Colm Donelan2ba48d22019-11-29 09:10:59 +00002929BOOST_AUTO_TEST_CASE(CheckProfilingServiceEnabled)
2930{
2931 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
2932 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
2933 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
2934 options.m_EnableProfiling = true;
2935 ProfilingService& profilingService = ProfilingService::Instance();
2936 profilingService.ResetExternalProfilingOptions(options, true);
2937 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
2938 profilingService.Update();
2939 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
2940
2941 // Redirect the output to a local stream so that we can parse the warning message
2942 std::stringstream ss;
2943 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
2944 profilingService.Update();
Finn Williams09ad6f92019-12-19 17:05:18 +00002945
2946 // Reset the profiling service to stop any running thread
2947 options.m_EnableProfiling = false;
2948 profilingService.ResetExternalProfilingOptions(options, true);
2949
Colm Donelan2ba48d22019-11-29 09:10:59 +00002950 streamRedirector.CancelRedirect();
2951
2952 // Check that the expected error has occurred and logged to the standard output
2953 if (!boost::contains(ss.str(), "Cannot connect to stream socket: Connection refused"))
2954 {
2955 std::cout << ss.str();
2956 BOOST_FAIL("Expected string not found.");
2957 }
Colm Donelan2ba48d22019-11-29 09:10:59 +00002958}
2959
2960BOOST_AUTO_TEST_CASE(CheckProfilingServiceEnabledRuntime)
2961{
2962 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
2963 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
2964 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
2965 ProfilingService& profilingService = ProfilingService::Instance();
2966 profilingService.ResetExternalProfilingOptions(options, true);
2967 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
2968 profilingService.Update();
2969 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
2970 options.m_EnableProfiling = true;
2971 profilingService.ResetExternalProfilingOptions(options);
2972 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
2973 profilingService.Update();
2974 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
2975
2976 // Redirect the output to a local stream so that we can parse the warning message
2977 std::stringstream ss;
2978 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
2979 profilingService.Update();
2980
Finn Williams09ad6f92019-12-19 17:05:18 +00002981 // Reset the profiling service to stop any running thread
2982 options.m_EnableProfiling = false;
2983 profilingService.ResetExternalProfilingOptions(options, true);
2984
Colm Donelan2ba48d22019-11-29 09:10:59 +00002985 streamRedirector.CancelRedirect();
2986
2987 // Check that the expected error has occurred and logged to the standard output
2988 if (!boost::contains(ss.str(), "Cannot connect to stream socket: Connection refused"))
2989 {
2990 std::cout << ss.str();
2991 BOOST_FAIL("Expected string not found.");
2992 }
Colm Donelan2ba48d22019-11-29 09:10:59 +00002993}
2994
2995BOOST_AUTO_TEST_CASE(CheckProfilingServiceBadConnectionAcknowledgedPacket)
2996{
2997 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
2998 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
2999 // Swap the profiling connection factory in the profiling service instance with our mock one
3000 SwapProfilingConnectionFactoryHelper helper;
3001
3002 // Redirect the standard output to a local stream so that we can parse the warning message
3003 std::stringstream ss;
3004 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3005
Colm Donelan2ba48d22019-11-29 09:10:59 +00003006 // Reset the profiling service to the uninitialized state
3007 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
3008 options.m_EnableProfiling = true;
3009 ProfilingService& profilingService = ProfilingService::Instance();
3010 profilingService.ResetExternalProfilingOptions(options, true);
3011
3012 // Bring the profiling service to the "WaitingForAck" state
3013 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3014 profilingService.Update(); // Initialize the counter directory
3015 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
3016 profilingService.Update(); // Create the profiling connection
3017
3018 // Get the mock profiling connection
3019 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
3020 BOOST_CHECK(mockProfilingConnection);
3021
Colm Donelan2ba48d22019-11-29 09:10:59 +00003022 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003023
3024 // Connection Acknowledged Packet header (word 0, word 1 is always zero):
3025 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
3026 // 16:25 [10] packet_id: Packet identifier, value 0b0000000001
3027 // 8:15 [8] reserved: Reserved, value 0b00000000
3028 // 0:7 [8] reserved: Reserved, value 0b00000000
3029 uint32_t packetFamily = 0;
3030 uint32_t packetId = 37; // Wrong packet id!!!
3031 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
3032
3033 // Create the Connection Acknowledged Packet
3034 Packet connectionAcknowledgedPacket(header);
Finn Williams09ad6f92019-12-19 17:05:18 +00003035 // Write an invalid "Connection Acknowledged" packet into the mock profiling connection, to simulate an invalid
3036 // reply from an external profiling service
Colm Donelan2ba48d22019-11-29 09:10:59 +00003037 mockProfilingConnection->WritePacket(std::move(connectionAcknowledgedPacket));
3038
Finn Williams09ad6f92019-12-19 17:05:18 +00003039 // Start the command thread
3040 profilingService.Update();
3041
3042 // Wait for the command thread to join
3043 options.m_EnableProfiling = false;
3044 profilingService.ResetExternalProfilingOptions(options, true);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003045
3046 streamRedirector.CancelRedirect();
3047
3048 // Check that the expected error has occurred and logged to the standard output
3049 if (!boost::contains(ss.str(), "Functor with requested PacketId=37 and Version=4194304 does not exist"))
3050 {
3051 std::cout << ss.str();
3052 BOOST_FAIL("Expected string not found.");
3053 }
Colm Donelan2ba48d22019-11-29 09:10:59 +00003054}
3055
3056BOOST_AUTO_TEST_CASE(CheckProfilingServiceBadRequestCounterDirectoryPacket)
3057{
3058 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
3059 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
3060 // Swap the profiling connection factory in the profiling service instance with our mock one
3061 SwapProfilingConnectionFactoryHelper helper;
3062
3063 // Redirect the standard output to a local stream so that we can parse the warning message
3064 std::stringstream ss;
3065 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3066
3067 // Reset the profiling service to the uninitialized state
3068 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
3069 options.m_EnableProfiling = true;
3070 ProfilingService& profilingService = ProfilingService::Instance();
3071 profilingService.ResetExternalProfilingOptions(options, true);
3072
3073 // Bring the profiling service to the "Active" state
3074 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3075 helper.ForceTransitionToState(ProfilingState::NotConnected);
3076 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
3077 profilingService.Update(); // Create the profiling connection
3078 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003079
3080 // Get the mock profiling connection
3081 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
3082 BOOST_CHECK(mockProfilingConnection);
3083
Colm Donelan2ba48d22019-11-29 09:10:59 +00003084 // Write a valid "Request Counter Directory" packet into the mock profiling connection, to simulate a valid
3085 // reply from an external profiling service
3086
3087 // Request Counter Directory packet header (word 0, word 1 is always zero):
3088 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
3089 // 16:25 [10] packet_id: Packet identifier, value 0b0000000011
3090 // 8:15 [8] reserved: Reserved, value 0b00000000
3091 // 0:7 [8] reserved: Reserved, value 0b00000000
3092 uint32_t packetFamily = 0;
3093 uint32_t packetId = 123; // Wrong packet id!!!
3094 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
3095
3096 // Create the Request Counter Directory packet
3097 Packet requestCounterDirectoryPacket(header);
3098
3099 // Write the packet to the mock profiling connection
3100 mockProfilingConnection->WritePacket(std::move(requestCounterDirectoryPacket));
3101
Finn Williams09ad6f92019-12-19 17:05:18 +00003102 // Start the command handler and the send thread
3103 profilingService.Update();
3104
3105 // Reset the profiling service to stop and join any running thread
3106 options.m_EnableProfiling = false;
3107 profilingService.ResetExternalProfilingOptions(options, true);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003108
3109 streamRedirector.CancelRedirect();
3110
3111 // Check that the expected error has occurred and logged to the standard output
3112 if (!boost::contains(ss.str(), "Functor with requested PacketId=123 and Version=4194304 does not exist"))
3113 {
3114 std::cout << ss.str();
3115 BOOST_FAIL("Expected string not found.");
3116 }
Colm Donelan2ba48d22019-11-29 09:10:59 +00003117}
3118
3119BOOST_AUTO_TEST_CASE(CheckProfilingServiceBadPeriodicCounterSelectionPacket)
3120{
3121 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
3122 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
3123 // Swap the profiling connection factory in the profiling service instance with our mock one
3124 SwapProfilingConnectionFactoryHelper helper;
3125
3126 // Redirect the standard output to a local stream so that we can parse the warning message
3127 std::stringstream ss;
3128 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3129
3130 // Reset the profiling service to the uninitialized state
3131 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
3132 options.m_EnableProfiling = true;
3133 ProfilingService& profilingService = ProfilingService::Instance();
3134 profilingService.ResetExternalProfilingOptions(options, true);
3135
3136 // Bring the profiling service to the "Active" state
3137 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3138 profilingService.Update(); // Initialize the counter directory
3139 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
3140 profilingService.Update(); // Create the profiling connection
3141 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
3142 profilingService.Update(); // Start the command handler and the send thread
3143
3144 // Get the mock profiling connection
3145 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
3146 BOOST_CHECK(mockProfilingConnection);
3147
Colm Donelan2ba48d22019-11-29 09:10:59 +00003148 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
3149 // external profiling service
3150
3151 // Periodic Counter Selection packet header:
3152 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
3153 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
3154 // 8:15 [8] reserved: Reserved, value 0b00000000
3155 // 0:7 [8] reserved: Reserved, value 0b00000000
3156 uint32_t packetFamily = 0;
3157 uint32_t packetId = 999; // Wrong packet id!!!
3158 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
3159
3160 // Create the Periodic Counter Selection packet
3161 Packet periodicCounterSelectionPacket(header); // Length == 0, this will disable the collection of counters
3162
3163 // Write the packet to the mock profiling connection
3164 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
Finn Williams09ad6f92019-12-19 17:05:18 +00003165 profilingService.Update();
Colm Donelan2ba48d22019-11-29 09:10:59 +00003166
Finn Williams09ad6f92019-12-19 17:05:18 +00003167 // Reset the profiling service to stop any running thread
3168 options.m_EnableProfiling = false;
3169 profilingService.ResetExternalProfilingOptions(options, true);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003170
3171 // Check that the expected error has occurred and logged to the standard output
3172 streamRedirector.CancelRedirect();
3173
3174 // Check that the expected error has occurred and logged to the standard output
3175 if (!boost::contains(ss.str(), "Functor with requested PacketId=999 and Version=4194304 does not exist"))
3176 {
3177 std::cout << ss.str();
3178 BOOST_FAIL("Expected string not found.");
3179 }
Colm Donelan2ba48d22019-11-29 09:10:59 +00003180}
Jim Flynn97897022020-02-02 12:52:59 +00003181
David Monahande803072020-01-30 12:44:23 +00003182BOOST_AUTO_TEST_CASE(CheckCounterIdMap)
3183{
3184 CounterIdMap counterIdMap;
3185 BOOST_CHECK_THROW(counterIdMap.GetBackendId(0), armnn::Exception);
3186 BOOST_CHECK_THROW(counterIdMap.GetGlobalId(0, armnn::profiling::BACKEND_ID), armnn::Exception);
3187
3188 uint16_t globalCounterIds = 0;
3189
3190 armnn::BackendId cpuRefId(armnn::Compute::CpuRef);
3191 armnn::BackendId cpuAccId(armnn::Compute::CpuAcc);
3192
3193 std::vector<uint16_t> cpuRefCounters = {0, 1, 2, 3};
3194 std::vector<uint16_t> cpuAccCounters = {0, 1};
3195
3196 for (uint16_t backendCounterId : cpuRefCounters)
3197 {
3198 counterIdMap.RegisterMapping(globalCounterIds, backendCounterId, cpuRefId);
3199 ++globalCounterIds;
3200 }
3201 for (uint16_t backendCounterId : cpuAccCounters)
3202 {
3203 counterIdMap.RegisterMapping(globalCounterIds, backendCounterId, cpuAccId);
3204 ++globalCounterIds;
3205 }
3206
3207 BOOST_CHECK(counterIdMap.GetBackendId(0) == (std::pair<uint16_t, armnn::BackendId>(0, cpuRefId)));
3208 BOOST_CHECK(counterIdMap.GetBackendId(1) == (std::pair<uint16_t, armnn::BackendId>(1, cpuRefId)));
3209 BOOST_CHECK(counterIdMap.GetBackendId(2) == (std::pair<uint16_t, armnn::BackendId>(2, cpuRefId)));
3210 BOOST_CHECK(counterIdMap.GetBackendId(3) == (std::pair<uint16_t, armnn::BackendId>(3, cpuRefId)));
3211 BOOST_CHECK(counterIdMap.GetBackendId(4) == (std::pair<uint16_t, armnn::BackendId>(0, cpuAccId)));
3212 BOOST_CHECK(counterIdMap.GetBackendId(5) == (std::pair<uint16_t, armnn::BackendId>(1, cpuAccId)));
3213
3214 BOOST_CHECK(counterIdMap.GetGlobalId(0, cpuRefId) == 0);
3215 BOOST_CHECK(counterIdMap.GetGlobalId(1, cpuRefId) == 1);
3216 BOOST_CHECK(counterIdMap.GetGlobalId(2, cpuRefId) == 2);
3217 BOOST_CHECK(counterIdMap.GetGlobalId(3, cpuRefId) == 3);
3218 BOOST_CHECK(counterIdMap.GetGlobalId(0, cpuAccId) == 4);
3219 BOOST_CHECK(counterIdMap.GetGlobalId(1, cpuAccId) == 5);
3220}
Colm Donelan2ba48d22019-11-29 09:10:59 +00003221
Jim Flynn97897022020-02-02 12:52:59 +00003222BOOST_AUTO_TEST_CASE(CheckRegisterBackendCounters)
3223{
3224 uint16_t globalCounterIds = armnn::profiling::INFERENCES_RUN;
3225 armnn::BackendId cpuRefId(armnn::Compute::CpuRef);
3226
3227 RegisterBackendCounters registerBackendCounters(globalCounterIds, cpuRefId);
3228
3229 // Reset the profiling service to the uninitialized state
3230 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
3231 options.m_EnableProfiling = true;
3232 ProfilingService& profilingService = ProfilingService::Instance();
3233 profilingService.ResetExternalProfilingOptions(options, true);
3234
3235 BOOST_CHECK(profilingService.GetCounterDirectory().GetCategories().empty());
3236 registerBackendCounters.RegisterCategory("categoryOne");
3237 auto categoryOnePtr = profilingService.GetCounterDirectory().GetCategory("categoryOne");
3238 BOOST_CHECK(categoryOnePtr);
3239
3240 BOOST_CHECK(profilingService.GetCounterDirectory().GetDevices().empty());
3241 globalCounterIds = registerBackendCounters.RegisterDevice("deviceOne");
3242 auto deviceOnePtr = profilingService.GetCounterDirectory().GetDevice(globalCounterIds);
3243 BOOST_CHECK(deviceOnePtr);
3244 BOOST_CHECK(deviceOnePtr->m_Name == "deviceOne");
3245
3246 BOOST_CHECK(profilingService.GetCounterDirectory().GetCounterSets().empty());
3247 globalCounterIds = registerBackendCounters.RegisterCounterSet("counterSetOne");
3248 auto counterSetOnePtr = profilingService.GetCounterDirectory().GetCounterSet(globalCounterIds);
3249 BOOST_CHECK(counterSetOnePtr);
3250 BOOST_CHECK(counterSetOnePtr->m_Name == "counterSetOne");
3251
3252 uint16_t newGlobalCounterId = registerBackendCounters.RegisterCounter(0,
3253 "categoryOne",
3254 0,
3255 0,
3256 1.f,
3257 "CounterOne",
3258 "first test counter");
3259 BOOST_CHECK(newGlobalCounterId = armnn::profiling::INFERENCES_RUN + 1);
3260 uint16_t mappedGlobalId = profilingService.GetCounterMappings().GetGlobalId(0, cpuRefId);
3261 BOOST_CHECK(mappedGlobalId == newGlobalCounterId);
3262 auto backendMapping = profilingService.GetCounterMappings().GetBackendId(newGlobalCounterId);
3263 BOOST_CHECK(backendMapping.first == 0);
3264 BOOST_CHECK(backendMapping.second == cpuRefId);
3265
3266 // Reset the profiling service to stop any running thread
3267 options.m_EnableProfiling = false;
3268 profilingService.ResetExternalProfilingOptions(options, true);
3269}
3270
James Conroy2dcd3fe2020-02-06 18:34:52 +00003271BOOST_AUTO_TEST_CASE(CheckCounterStatusQuery)
3272{
3273 armnn::IRuntime::CreationOptions options;
3274 options.m_ProfilingOptions.m_EnableProfiling = true;
3275
3276 // Reset the profiling service to the uninitialized state
3277 ProfilingService& profilingService = ProfilingService::Instance();
3278 profilingService.ResetExternalProfilingOptions(options.m_ProfilingOptions, true);
3279
3280 const armnn::BackendId cpuRefId(armnn::Compute::CpuRef);
3281 const armnn::BackendId cpuAccId(armnn::Compute::CpuAcc);
3282
3283 // Create BackendProfiling for each backend
3284 BackendProfiling backendProfilingCpuRef(options, profilingService, cpuRefId);
3285 BackendProfiling backendProfilingCpuAcc(options, profilingService, cpuAccId);
3286
3287 uint16_t initialNumGlobalCounterIds = armnn::profiling::INFERENCES_RUN;
3288
3289 // Create RegisterBackendCounters for CpuRef
3290 RegisterBackendCounters registerBackendCountersCpuRef(initialNumGlobalCounterIds, cpuRefId);
3291
3292 // Create 'testCategory' in CounterDirectory (backend agnostic)
3293 BOOST_CHECK(profilingService.GetCounterDirectory().GetCategories().empty());
3294 registerBackendCountersCpuRef.RegisterCategory("testCategory");
3295 auto categoryOnePtr = profilingService.GetCounterDirectory().GetCategory("testCategory");
3296 BOOST_CHECK(categoryOnePtr);
3297
3298 // Counters:
3299 // Global | Local | Backend
3300 // 5 | 0 | CpuRef
3301 // 6 | 1 | CpuRef
3302 // 7 | 1 | CpuAcc
3303
3304 std::vector<uint16_t> cpuRefCounters = {0, 1};
3305 std::vector<uint16_t> cpuAccCounters = {0};
3306
3307 // Register the backend counters for CpuRef and validate GetGlobalId and GetBackendId
3308 uint16_t currentNumGlobalCounterIds = registerBackendCountersCpuRef.RegisterCounter(
3309 0, "testCategory", 0, 0, 1.f, "CpuRefCounter0", "Zeroth CpuRef Counter");
3310 BOOST_CHECK(currentNumGlobalCounterIds == initialNumGlobalCounterIds + 1);
3311 uint16_t mappedGlobalId = profilingService.GetCounterMappings().GetGlobalId(0, cpuRefId);
3312 BOOST_CHECK(mappedGlobalId == currentNumGlobalCounterIds);
3313 auto backendMapping = profilingService.GetCounterMappings().GetBackendId(currentNumGlobalCounterIds);
3314 BOOST_CHECK(backendMapping.first == 0);
3315 BOOST_CHECK(backendMapping.second == cpuRefId);
3316
3317 currentNumGlobalCounterIds = registerBackendCountersCpuRef.RegisterCounter(
3318 1, "testCategory", 0, 0, 1.f, "CpuRefCounter1", "First CpuRef Counter");
3319 BOOST_CHECK(currentNumGlobalCounterIds == initialNumGlobalCounterIds + 2);
3320 mappedGlobalId = profilingService.GetCounterMappings().GetGlobalId(1, cpuRefId);
3321 BOOST_CHECK(mappedGlobalId == currentNumGlobalCounterIds);
3322 backendMapping = profilingService.GetCounterMappings().GetBackendId(currentNumGlobalCounterIds);
3323 BOOST_CHECK(backendMapping.first == 1);
3324 BOOST_CHECK(backendMapping.second == cpuRefId);
3325
3326 // Create RegisterBackendCounters for CpuAcc
3327 RegisterBackendCounters registerBackendCountersCpuAcc(currentNumGlobalCounterIds, cpuAccId);
3328
3329 // Register the backend counter for CpuAcc and validate GetGlobalId and GetBackendId
3330 currentNumGlobalCounterIds = registerBackendCountersCpuAcc.RegisterCounter(
3331 0, "testCategory", 0, 0, 1.f, "CpuAccCounter0", "Zeroth CpuAcc Counter");
3332 BOOST_CHECK(currentNumGlobalCounterIds == initialNumGlobalCounterIds + 3);
3333 mappedGlobalId = profilingService.GetCounterMappings().GetGlobalId(0, cpuAccId);
3334 BOOST_CHECK(mappedGlobalId == currentNumGlobalCounterIds);
3335 backendMapping = profilingService.GetCounterMappings().GetBackendId(currentNumGlobalCounterIds);
3336 BOOST_CHECK(backendMapping.first == 0);
3337 BOOST_CHECK(backendMapping.second == cpuAccId);
3338
3339 // Create vectors for active counters
3340 const std::vector<uint16_t> activeGlobalCounterIds = {5}; // CpuRef(0) activated
3341 const std::vector<uint16_t> newActiveGlobalCounterIds = {6, 7}; // CpuRef(0) and CpuAcc(1) activated
3342
3343 const uint32_t capturePeriod = 200;
3344 const uint32_t newCapturePeriod = 100;
3345
3346 // Set capture period and active counters in CaptureData
3347 profilingService.SetCaptureData(capturePeriod, activeGlobalCounterIds);
3348
3349 // Get vector of active counters for CpuRef and CpuAcc backends
3350 std::vector<CounterStatus> cpuRefCounterStatus = backendProfilingCpuRef.GetActiveCounters();
3351 std::vector<CounterStatus> cpuAccCounterStatus = backendProfilingCpuAcc.GetActiveCounters();
3352 BOOST_CHECK_EQUAL(cpuRefCounterStatus.size(), 1);
3353 BOOST_CHECK_EQUAL(cpuAccCounterStatus.size(), 0);
3354
3355 // Check active CpuRef counter
3356 BOOST_CHECK_EQUAL(cpuRefCounterStatus[0].m_GlobalCounterId, activeGlobalCounterIds[0]);
3357 BOOST_CHECK_EQUAL(cpuRefCounterStatus[0].m_BackendCounterId, cpuRefCounters[0]);
3358 BOOST_CHECK_EQUAL(cpuRefCounterStatus[0].m_SamplingRateInMicroseconds, capturePeriod);
3359 BOOST_CHECK_EQUAL(cpuRefCounterStatus[0].m_Enabled, true);
3360
3361 // Check inactive CpuRef counter
3362 CounterStatus inactiveCpuRefCounter = backendProfilingCpuRef.GetCounterStatus(cpuRefCounters[1]);
3363 BOOST_CHECK_EQUAL(inactiveCpuRefCounter.m_GlobalCounterId, 6);
3364 BOOST_CHECK_EQUAL(inactiveCpuRefCounter.m_BackendCounterId, cpuRefCounters[1]);
3365 BOOST_CHECK_EQUAL(inactiveCpuRefCounter.m_SamplingRateInMicroseconds, 0);
3366 BOOST_CHECK_EQUAL(inactiveCpuRefCounter.m_Enabled, false);
3367
3368 // Check inactive CpuAcc counter
3369 CounterStatus inactiveCpuAccCounter = backendProfilingCpuAcc.GetCounterStatus(cpuAccCounters[0]);
3370 BOOST_CHECK_EQUAL(inactiveCpuAccCounter.m_GlobalCounterId, 7);
3371 BOOST_CHECK_EQUAL(inactiveCpuAccCounter.m_BackendCounterId, cpuAccCounters[0]);
3372 BOOST_CHECK_EQUAL(inactiveCpuAccCounter.m_SamplingRateInMicroseconds, 0);
3373 BOOST_CHECK_EQUAL(inactiveCpuAccCounter.m_Enabled, false);
3374
3375 // Set new capture period and new active counters in CaptureData
3376 profilingService.SetCaptureData(newCapturePeriod, newActiveGlobalCounterIds);
3377
3378 // Get vector of active counters for CpuRef and CpuAcc backends
3379 cpuRefCounterStatus = backendProfilingCpuRef.GetActiveCounters();
3380 cpuAccCounterStatus = backendProfilingCpuAcc.GetActiveCounters();
3381 BOOST_CHECK_EQUAL(cpuRefCounterStatus.size(), 1);
3382 BOOST_CHECK_EQUAL(cpuAccCounterStatus.size(), 1);
3383
3384 // Check active CpuRef counter
3385 BOOST_CHECK_EQUAL(cpuRefCounterStatus[0].m_GlobalCounterId, newActiveGlobalCounterIds[0]);
3386 BOOST_CHECK_EQUAL(cpuRefCounterStatus[0].m_BackendCounterId, cpuRefCounters[1]);
3387 BOOST_CHECK_EQUAL(cpuRefCounterStatus[0].m_SamplingRateInMicroseconds, newCapturePeriod);
3388 BOOST_CHECK_EQUAL(cpuRefCounterStatus[0].m_Enabled, true);
3389
3390 // Check active CpuAcc counter
3391 BOOST_CHECK_EQUAL(cpuAccCounterStatus[0].m_GlobalCounterId, newActiveGlobalCounterIds[1]);
3392 BOOST_CHECK_EQUAL(cpuAccCounterStatus[0].m_BackendCounterId, cpuAccCounters[0]);
3393 BOOST_CHECK_EQUAL(cpuAccCounterStatus[0].m_SamplingRateInMicroseconds, newCapturePeriod);
3394 BOOST_CHECK_EQUAL(cpuAccCounterStatus[0].m_Enabled, true);
3395
3396 // Check inactive CpuRef counter
3397 inactiveCpuRefCounter = backendProfilingCpuRef.GetCounterStatus(cpuRefCounters[0]);
3398 BOOST_CHECK_EQUAL(inactiveCpuRefCounter.m_GlobalCounterId, 5);
3399 BOOST_CHECK_EQUAL(inactiveCpuRefCounter.m_BackendCounterId, cpuRefCounters[0]);
3400 BOOST_CHECK_EQUAL(inactiveCpuRefCounter.m_SamplingRateInMicroseconds, 0);
3401 BOOST_CHECK_EQUAL(inactiveCpuRefCounter.m_Enabled, false);
3402
3403 // Reset the profiling service to stop any running thread
3404 options.m_ProfilingOptions.m_EnableProfiling = false;
3405 profilingService.ResetExternalProfilingOptions(options.m_ProfilingOptions, true);
3406}
3407
Francis Murtagh1f7db452019-08-14 09:49:34 +01003408BOOST_AUTO_TEST_SUITE_END()