blob: f252579022cc00ef3fcdb4e2337754e4f08221e7 [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"
Keith Davis33ed2212020-03-30 10:43:41 +01007#include "ProfilingTestUtils.hpp"
Teresa Charlin9bab4962019-09-06 12:28:35 +01008
James Conroy2dcd3fe2020-02-06 18:34:52 +00009#include <backends/BackendProfiling.hpp>
Matteo Martincigh8a837172019-10-04 17:01:07 +010010#include <CommandHandler.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010011#include <CommandHandlerKey.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010012#include <CommandHandlerRegistry.hpp>
Sadik Armagana97a0be2020-03-03 10:44:56 +000013#include <common/include/SocketConnectionException.hpp>
Sadik Armaganb5f01b22019-09-18 17:29:00 +010014#include <ConnectionAcknowledgedCommandHandler.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010015#include <CounterDirectory.hpp>
David Monahande803072020-01-30 12:44:23 +000016#include <CounterIdMap.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010017#include <EncodeVersion.hpp>
18#include <Holder.hpp>
Matteo Martincighe0e6efc2019-10-04 17:17:42 +010019#include <ICounterValues.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010020#include <Packet.hpp>
21#include <PacketVersionResolver.hpp>
Francis Murtaghfcb8ef62019-09-20 15:40:09 +010022#include <PeriodicCounterCapture.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010023#include <PeriodicCounterSelectionCommandHandler.hpp>
24#include <ProfilingStateMachine.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010025#include <ProfilingUtils.hpp>
James Conroy2dcd3fe2020-02-06 18:34:52 +000026#include <RegisterBackendCounters.hpp>
Narumol Prangnawarat48033692019-09-20 12:04:55 +010027#include <RequestCounterDirectoryCommandHandler.hpp>
Teresa Charlin9bab4962019-09-06 12:28:35 +010028#include <Runtime.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010029#include <SocketProfilingConnection.hpp>
Matteo Martincighcdfb9412019-11-08 11:23:06 +000030#include <SendCounterPacket.hpp>
Sadik Armagan3896b472020-02-10 12:24:15 +000031#include <SendThread.hpp>
Matteo Martincighcdfb9412019-11-08 11:23:06 +000032#include <SendTimelinePacket.hpp>
Keith Davis02356de2019-08-26 18:28:17 +010033
Matteo Martincigh6db5f202019-09-05 12:02:04 +010034#include <armnn/Conversion.hpp>
Colm Donelan02705242019-11-14 14:19:07 +000035#include <armnn/Types.hpp>
Ferran Balaguer1b941722019-08-28 16:57:18 +010036
Matteo Martincigh54fb9572019-10-02 12:50:57 +010037#include <armnn/Utils.hpp>
Jan Eilers8eb25602020-03-09 12:13:48 +000038#include <armnn/utility/IgnoreUnused.hpp>
Matteo Martincigh54fb9572019-10-02 12:50:57 +010039
Sadik Armaganbd9e2c52019-09-26 23:13:31 +010040#include <boost/algorithm/string.hpp>
Ferran Balaguer1b941722019-08-28 16:57:18 +010041#include <boost/numeric/conversion/cast.hpp>
Francis Murtagh1f7db452019-08-14 09:49:34 +010042
Nikhil Rajbc626052019-08-15 15:49:45 +010043#include <cstdint>
44#include <cstring>
Keith Davis3201eea2019-10-24 17:30:41 +010045#include <iostream>
Aron Virginas-Tare898db92019-08-22 12:56:34 +010046#include <limits>
Francis Murtagh11f99b42019-08-16 11:28:52 +010047#include <map>
Aron Virginas-Tare898db92019-08-22 12:56:34 +010048#include <random>
James Conroy2dcd3fe2020-02-06 18:34:52 +000049
Francis Murtagh1f7db452019-08-14 09:49:34 +010050
Aron Virginas-Tare898db92019-08-22 12:56:34 +010051using namespace armnn::profiling;
Finn Williams09ad6f92019-12-19 17:05:18 +000052using PacketType = MockProfilingConnection::PacketType;
Aron Virginas-Tare898db92019-08-22 12:56:34 +010053
Matteo Martincigh8a837172019-10-04 17:01:07 +010054BOOST_AUTO_TEST_SUITE(ExternalProfiling)
55
Francis Murtagh1f7db452019-08-14 09:49:34 +010056BOOST_AUTO_TEST_CASE(CheckCommandHandlerKeyComparisons)
57{
Jim Flynn397043f2019-10-17 17:37:10 +010058 CommandHandlerKey testKey1_0(1, 1, 1);
59 CommandHandlerKey testKey1_1(1, 1, 1);
60 CommandHandlerKey testKey1_2(1, 2, 1);
61
62 CommandHandlerKey testKey0(0, 1, 1);
63 CommandHandlerKey testKey1(0, 1, 1);
64 CommandHandlerKey testKey2(0, 1, 1);
65 CommandHandlerKey testKey3(0, 0, 0);
66 CommandHandlerKey testKey4(0, 2, 2);
67 CommandHandlerKey testKey5(0, 0, 2);
68
69 BOOST_CHECK(testKey1_0 > testKey0);
70 BOOST_CHECK(testKey1_0 == testKey1_1);
71 BOOST_CHECK(testKey1_0 < testKey1_2);
Francis Murtagh1f7db452019-08-14 09:49:34 +010072
Keith Davis3201eea2019-10-24 17:30:41 +010073 BOOST_CHECK(testKey1 < testKey4);
74 BOOST_CHECK(testKey1 > testKey3);
75 BOOST_CHECK(testKey1 <= testKey4);
76 BOOST_CHECK(testKey1 >= testKey3);
77 BOOST_CHECK(testKey1 <= testKey2);
78 BOOST_CHECK(testKey1 >= testKey2);
79 BOOST_CHECK(testKey1 == testKey2);
80 BOOST_CHECK(testKey1 == testKey1);
Francis Murtagh1f7db452019-08-14 09:49:34 +010081
Keith Davis3201eea2019-10-24 17:30:41 +010082 BOOST_CHECK(!(testKey1 == testKey5));
83 BOOST_CHECK(!(testKey1 != testKey1));
84 BOOST_CHECK(testKey1 != testKey5);
Francis Murtagh1f7db452019-08-14 09:49:34 +010085
Keith Davis3201eea2019-10-24 17:30:41 +010086 BOOST_CHECK(testKey1 == testKey2 && testKey2 == testKey1);
87 BOOST_CHECK(testKey0 == testKey1 && testKey1 == testKey2 && testKey0 == testKey2);
Francis Murtagh1f7db452019-08-14 09:49:34 +010088
Keith Davis3201eea2019-10-24 17:30:41 +010089 BOOST_CHECK(testKey1.GetPacketId() == 1);
90 BOOST_CHECK(testKey1.GetVersion() == 1);
Francis Murtagh1f7db452019-08-14 09:49:34 +010091
Keith Davis3201eea2019-10-24 17:30:41 +010092 std::vector<CommandHandlerKey> vect = { CommandHandlerKey(0, 0, 1), CommandHandlerKey(0, 2, 0),
93 CommandHandlerKey(0, 1, 0), CommandHandlerKey(0, 2, 1),
94 CommandHandlerKey(0, 1, 1), CommandHandlerKey(0, 0, 1),
95 CommandHandlerKey(0, 2, 0), CommandHandlerKey(0, 0, 0) };
Francis Murtagh1f7db452019-08-14 09:49:34 +010096
97 std::sort(vect.begin(), vect.end());
98
Keith Davis3201eea2019-10-24 17:30:41 +010099 std::vector<CommandHandlerKey> expectedVect = { CommandHandlerKey(0, 0, 0), CommandHandlerKey(0, 0, 1),
100 CommandHandlerKey(0, 0, 1), CommandHandlerKey(0, 1, 0),
101 CommandHandlerKey(0, 1, 1), CommandHandlerKey(0, 2, 0),
102 CommandHandlerKey(0, 2, 0), CommandHandlerKey(0, 2, 1) };
Francis Murtagh1f7db452019-08-14 09:49:34 +0100103
104 BOOST_CHECK(vect == expectedVect);
105}
106
Jim Flynned25e0e2019-10-18 13:21:43 +0100107BOOST_AUTO_TEST_CASE(CheckPacketKeyComparisons)
108{
Keith Davis3201eea2019-10-24 17:30:41 +0100109 PacketKey key0(0, 0);
110 PacketKey key1(0, 0);
111 PacketKey key2(0, 1);
112 PacketKey key3(0, 2);
113 PacketKey key4(1, 0);
114 PacketKey key5(1, 0);
115 PacketKey key6(1, 1);
Jim Flynned25e0e2019-10-18 13:21:43 +0100116
117 BOOST_CHECK(!(key0 < key1));
118 BOOST_CHECK(!(key0 > key1));
119 BOOST_CHECK(key0 <= key1);
120 BOOST_CHECK(key0 >= key1);
121 BOOST_CHECK(key0 == key1);
122 BOOST_CHECK(key0 < key2);
123 BOOST_CHECK(key2 < key3);
124 BOOST_CHECK(key3 > key0);
125 BOOST_CHECK(key4 == key5);
126 BOOST_CHECK(key4 > key0);
127 BOOST_CHECK(key5 < key6);
128 BOOST_CHECK(key5 <= key6);
129 BOOST_CHECK(key5 != key6);
130}
131
Matteo Martincigh8a837172019-10-04 17:01:07 +0100132BOOST_AUTO_TEST_CASE(CheckCommandHandler)
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100133{
Matteo Martincigh8a837172019-10-04 17:01:07 +0100134 PacketVersionResolver packetVersionResolver;
135 ProfilingStateMachine profilingStateMachine;
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100136
Matteo Martincigh8a837172019-10-04 17:01:07 +0100137 TestProfilingConnectionBase testProfilingConnectionBase;
138 TestProfilingConnectionTimeoutError testProfilingConnectionTimeOutError;
139 TestProfilingConnectionArmnnError testProfilingConnectionArmnnError;
Keith Davis3201eea2019-10-24 17:30:41 +0100140 CounterDirectory counterDirectory;
141 MockBufferManager mockBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +0000142 SendCounterPacket sendCounterPacket(mockBuffer);
143 SendThread sendThread(profilingStateMachine, mockBuffer, sendCounterPacket);
Matteo Martincighcdfb9412019-11-08 11:23:06 +0000144 SendTimelinePacket sendTimelinePacket(mockBuffer);
145
Keith Davis3201eea2019-10-24 17:30:41 +0100146 ConnectionAcknowledgedCommandHandler connectionAcknowledgedCommandHandler(0, 1, 4194304, counterDirectory,
Matteo Martincighcdfb9412019-11-08 11:23:06 +0000147 sendCounterPacket, sendTimelinePacket,
148 profilingStateMachine);
Matteo Martincigh8a837172019-10-04 17:01:07 +0100149 CommandHandlerRegistry commandHandlerRegistry;
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100150
Matteo Martincighc2728f92019-10-07 12:35:21 +0100151 commandHandlerRegistry.RegisterFunctor(&connectionAcknowledgedCommandHandler);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100152
Matteo Martincigh8a837172019-10-04 17:01:07 +0100153 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
154 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100155
Keith Davis3201eea2019-10-24 17:30:41 +0100156 CommandHandler commandHandler0(1, true, commandHandlerRegistry, packetVersionResolver);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100157
Colm Donelan2ba48d22019-11-29 09:10:59 +0000158 // This should start the command handler thread return the connection ack and put the profiling
159 // service into active state.
Matteo Martincigh8a837172019-10-04 17:01:07 +0100160 commandHandler0.Start(testProfilingConnectionBase);
Colm Donelan2ba48d22019-11-29 09:10:59 +0000161 // Try to start the send thread many times, it must only start once
Matteo Martincigh8a837172019-10-04 17:01:07 +0100162 commandHandler0.Start(testProfilingConnectionBase);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100163
Colm Donelan2ba48d22019-11-29 09:10:59 +0000164 // This could take up to 20mSec but we'll check often.
165 for (int i = 0; i < 10; i++)
Matteo Martincigh8a837172019-10-04 17:01:07 +0100166 {
167 if (profilingStateMachine.GetCurrentState() == ProfilingState::Active)
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100168 {
Matteo Martincigh8a837172019-10-04 17:01:07 +0100169 break;
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100170 }
Colm Donelan2ba48d22019-11-29 09:10:59 +0000171 std::this_thread::sleep_for(std::chrono::milliseconds(2));
Matteo Martincigh8a837172019-10-04 17:01:07 +0100172 }
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100173
Matteo Martincigh8a837172019-10-04 17:01:07 +0100174 BOOST_CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::Active);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100175
Colm Donelan2ba48d22019-11-29 09:10:59 +0000176 // Close the thread again.
177 commandHandler0.Stop();
178
179 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
180 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
181
182 // In this test we'll simulate a timeout without a connection ack packet being received.
183 // Stop after timeout is set so we expect the command handler to stop almost immediately.
184 CommandHandler commandHandler1(1, true, commandHandlerRegistry, packetVersionResolver);
185
186 commandHandler1.Start(testProfilingConnectionTimeOutError);
187 // Wait until we know a timeout exception has been sent at least once.
188 for (int i = 0; i < 10; i++)
189 {
190 if (testProfilingConnectionTimeOutError.ReadCalledCount())
191 {
192 break;
193 }
194 std::this_thread::sleep_for(std::chrono::milliseconds(2));
195 }
Colm Donelan2ba48d22019-11-29 09:10:59 +0000196
197 // The command handler loop should have stopped after the timeout.
Finn Williams09ad6f92019-12-19 17:05:18 +0000198 // wait for the timeout exception to be processed and the loop to break.
199 uint32_t timeout = 50;
200 uint32_t timeSlept = 0;
201 while (commandHandler1.IsRunning())
202 {
203 if (timeSlept >= timeout)
204 {
205 BOOST_FAIL("Timeout: The command handler loop did not stop after the timeout");
206 }
207 std::this_thread::sleep_for(std::chrono::milliseconds(1));
208 timeSlept ++;
209 }
Colm Donelan2ba48d22019-11-29 09:10:59 +0000210
211 commandHandler1.Stop();
212 // The state machine should never have received the ack so will still be in WaitingForAck.
213 BOOST_CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::WaitingForAck);
214
Finn Williams09ad6f92019-12-19 17:05:18 +0000215 // Now try sending a bad connection acknowledged packet
216 TestProfilingConnectionBadAckPacket testProfilingConnectionBadAckPacket;
217 commandHandler1.Start(testProfilingConnectionBadAckPacket);
218 commandHandler1.Stop();
219 // This should also not change the state machine
220 BOOST_CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::WaitingForAck);
221
Colm Donelan2ba48d22019-11-29 09:10:59 +0000222 // Disable stop after timeout and now commandHandler1 should persist after a timeout
223 commandHandler1.SetStopAfterTimeout(false);
224 // Restart the thread.
225 commandHandler1.Start(testProfilingConnectionTimeOutError);
226
227 // Wait for at the three timeouts and the ack to be sent.
228 for (int i = 0; i < 10; i++)
229 {
230 if (testProfilingConnectionTimeOutError.ReadCalledCount() > 3)
231 {
232 break;
233 }
234 std::this_thread::sleep_for(std::chrono::milliseconds(2));
235 }
236 commandHandler1.Stop();
237
238 // Even after the 3 exceptions the ack packet should have transitioned the command handler to active.
239 BOOST_CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::Active);
240
241 // A command handler that gets exceptions other than timeouts should keep going.
242 CommandHandler commandHandler2(1, false, commandHandlerRegistry, packetVersionResolver);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100243
Matteo Martincigh8a837172019-10-04 17:01:07 +0100244 commandHandler2.Start(testProfilingConnectionArmnnError);
245
Colm Donelan2ba48d22019-11-29 09:10:59 +0000246 // Wait for two exceptions to be thrown.
247 for (int i = 0; i < 10; i++)
248 {
249 if (testProfilingConnectionTimeOutError.ReadCalledCount() >= 2)
250 {
251 break;
252 }
253 std::this_thread::sleep_for(std::chrono::milliseconds(2));
254 }
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100255
Matteo Martincighd0613b52019-10-09 16:47:04 +0100256 BOOST_CHECK(commandHandler2.IsRunning());
Matteo Martincigh8a837172019-10-04 17:01:07 +0100257 commandHandler2.Stop();
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100258}
259
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100260BOOST_AUTO_TEST_CASE(CheckEncodeVersion)
261{
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100262 Version version1(12);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100263
264 BOOST_CHECK(version1.GetMajor() == 0);
265 BOOST_CHECK(version1.GetMinor() == 0);
266 BOOST_CHECK(version1.GetPatch() == 12);
267
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100268 Version version2(4108);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100269
270 BOOST_CHECK(version2.GetMajor() == 0);
271 BOOST_CHECK(version2.GetMinor() == 1);
272 BOOST_CHECK(version2.GetPatch() == 12);
273
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100274 Version version3(4198412);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100275
276 BOOST_CHECK(version3.GetMajor() == 1);
277 BOOST_CHECK(version3.GetMinor() == 1);
278 BOOST_CHECK(version3.GetPatch() == 12);
279
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100280 Version version4(0);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100281
282 BOOST_CHECK(version4.GetMajor() == 0);
283 BOOST_CHECK(version4.GetMinor() == 0);
284 BOOST_CHECK(version4.GetPatch() == 0);
285
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100286 Version version5(1, 0, 0);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100287 BOOST_CHECK(version5.GetEncodedValue() == 4194304);
288}
289
Nikhil Rajbc626052019-08-15 15:49:45 +0100290BOOST_AUTO_TEST_CASE(CheckPacketClass)
291{
Keith Davis3201eea2019-10-24 17:30:41 +0100292 uint32_t length = 4;
Matteo Martincigh67ef2a52019-10-10 13:29:02 +0100293 std::unique_ptr<unsigned char[]> packetData0 = std::make_unique<unsigned char[]>(length);
294 std::unique_ptr<unsigned char[]> packetData1 = std::make_unique<unsigned char[]>(0);
295 std::unique_ptr<unsigned char[]> nullPacketData;
Nikhil Rajbc626052019-08-15 15:49:45 +0100296
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100297 Packet packetTest0(472580096, length, packetData0);
Nikhil Rajbc626052019-08-15 15:49:45 +0100298
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100299 BOOST_CHECK(packetTest0.GetHeader() == 472580096);
300 BOOST_CHECK(packetTest0.GetPacketFamily() == 7);
301 BOOST_CHECK(packetTest0.GetPacketId() == 43);
302 BOOST_CHECK(packetTest0.GetLength() == length);
303 BOOST_CHECK(packetTest0.GetPacketType() == 3);
304 BOOST_CHECK(packetTest0.GetPacketClass() == 5);
Nikhil Rajbc626052019-08-15 15:49:45 +0100305
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100306 BOOST_CHECK_THROW(Packet packetTest1(472580096, 0, packetData1), armnn::Exception);
307 BOOST_CHECK_NO_THROW(Packet packetTest2(472580096, 0, nullPacketData));
Nikhil Rajbc626052019-08-15 15:49:45 +0100308
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100309 Packet packetTest3(472580096, 0, nullPacketData);
310 BOOST_CHECK(packetTest3.GetLength() == 0);
311 BOOST_CHECK(packetTest3.GetData() == nullptr);
312
Matteo Martincigh67ef2a52019-10-10 13:29:02 +0100313 const unsigned char* packetTest0Data = packetTest0.GetData();
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100314 Packet packetTest4(std::move(packetTest0));
315
316 BOOST_CHECK(packetTest0.GetData() == nullptr);
317 BOOST_CHECK(packetTest4.GetData() == packetTest0Data);
318
319 BOOST_CHECK(packetTest4.GetHeader() == 472580096);
320 BOOST_CHECK(packetTest4.GetPacketFamily() == 7);
321 BOOST_CHECK(packetTest4.GetPacketId() == 43);
322 BOOST_CHECK(packetTest4.GetLength() == length);
323 BOOST_CHECK(packetTest4.GetPacketType() == 3);
324 BOOST_CHECK(packetTest4.GetPacketClass() == 5);
Nikhil Rajbc626052019-08-15 15:49:45 +0100325}
326
Francis Murtagh11f99b42019-08-16 11:28:52 +0100327BOOST_AUTO_TEST_CASE(CheckCommandHandlerFunctor)
328{
Francis Murtagh11f99b42019-08-16 11:28:52 +0100329 // Hard code the version as it will be the same during a single profiling session
330 uint32_t version = 1;
331
Jim Flynn397043f2019-10-17 17:37:10 +0100332 TestFunctorA testFunctorA(7, 461, version);
333 TestFunctorB testFunctorB(8, 963, version);
334 TestFunctorC testFunctorC(5, 983, version);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100335
Jim Flynn397043f2019-10-17 17:37:10 +0100336 CommandHandlerKey keyA(testFunctorA.GetFamilyId(), testFunctorA.GetPacketId(), testFunctorA.GetVersion());
337 CommandHandlerKey keyB(testFunctorB.GetFamilyId(), testFunctorB.GetPacketId(), testFunctorB.GetVersion());
338 CommandHandlerKey keyC(testFunctorC.GetFamilyId(), testFunctorC.GetPacketId(), testFunctorC.GetVersion());
Francis Murtagh11f99b42019-08-16 11:28:52 +0100339
340 // Create the unwrapped map to simulate the Command Handler Registry
341 std::map<CommandHandlerKey, CommandHandlerFunctor*> registry;
342
343 registry.insert(std::make_pair(keyB, &testFunctorB));
344 registry.insert(std::make_pair(keyA, &testFunctorA));
345 registry.insert(std::make_pair(keyC, &testFunctorC));
346
347 // Check the order of the map is correct
348 auto it = registry.begin();
Keith Davis3201eea2019-10-24 17:30:41 +0100349 BOOST_CHECK(it->first == keyC); // familyId == 5
Francis Murtagh11f99b42019-08-16 11:28:52 +0100350 it++;
Keith Davis3201eea2019-10-24 17:30:41 +0100351 BOOST_CHECK(it->first == keyA); // familyId == 7
Francis Murtagh11f99b42019-08-16 11:28:52 +0100352 it++;
Keith Davis3201eea2019-10-24 17:30:41 +0100353 BOOST_CHECK(it->first == keyB); // familyId == 8
Francis Murtagh11f99b42019-08-16 11:28:52 +0100354
Matteo Martincigh67ef2a52019-10-10 13:29:02 +0100355 std::unique_ptr<unsigned char[]> packetDataA;
356 std::unique_ptr<unsigned char[]> packetDataB;
357 std::unique_ptr<unsigned char[]> packetDataC;
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100358
359 Packet packetA(500000000, 0, packetDataA);
360 Packet packetB(600000000, 0, packetDataB);
361 Packet packetC(400000000, 0, packetDataC);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100362
363 // Check the correct operator of derived class is called
Jim Flynn397043f2019-10-17 17:37:10 +0100364 registry.at(CommandHandlerKey(packetA.GetPacketFamily(), packetA.GetPacketId(), version))->operator()(packetA);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100365 BOOST_CHECK(testFunctorA.GetCount() == 1);
366 BOOST_CHECK(testFunctorB.GetCount() == 0);
367 BOOST_CHECK(testFunctorC.GetCount() == 0);
368
Jim Flynn397043f2019-10-17 17:37:10 +0100369 registry.at(CommandHandlerKey(packetB.GetPacketFamily(), packetB.GetPacketId(), version))->operator()(packetB);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100370 BOOST_CHECK(testFunctorA.GetCount() == 1);
371 BOOST_CHECK(testFunctorB.GetCount() == 1);
372 BOOST_CHECK(testFunctorC.GetCount() == 0);
373
Jim Flynn397043f2019-10-17 17:37:10 +0100374 registry.at(CommandHandlerKey(packetC.GetPacketFamily(), packetC.GetPacketId(), version))->operator()(packetC);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100375 BOOST_CHECK(testFunctorA.GetCount() == 1);
376 BOOST_CHECK(testFunctorB.GetCount() == 1);
377 BOOST_CHECK(testFunctorC.GetCount() == 1);
378}
379
Francis Murtagh94d79152019-08-16 17:45:07 +0100380BOOST_AUTO_TEST_CASE(CheckCommandHandlerRegistry)
381{
382 // Hard code the version as it will be the same during a single profiling session
383 uint32_t version = 1;
384
Jim Flynn397043f2019-10-17 17:37:10 +0100385 TestFunctorA testFunctorA(7, 461, version);
386 TestFunctorB testFunctorB(8, 963, version);
387 TestFunctorC testFunctorC(5, 983, version);
Francis Murtagh94d79152019-08-16 17:45:07 +0100388
389 // Create the Command Handler Registry
390 CommandHandlerRegistry registry;
391
392 // Register multiple different derived classes
Matteo Martincighc2728f92019-10-07 12:35:21 +0100393 registry.RegisterFunctor(&testFunctorA);
394 registry.RegisterFunctor(&testFunctorB);
395 registry.RegisterFunctor(&testFunctorC);
Francis Murtagh94d79152019-08-16 17:45:07 +0100396
Matteo Martincigh67ef2a52019-10-10 13:29:02 +0100397 std::unique_ptr<unsigned char[]> packetDataA;
398 std::unique_ptr<unsigned char[]> packetDataB;
399 std::unique_ptr<unsigned char[]> packetDataC;
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100400
401 Packet packetA(500000000, 0, packetDataA);
402 Packet packetB(600000000, 0, packetDataB);
403 Packet packetC(400000000, 0, packetDataC);
Francis Murtagh94d79152019-08-16 17:45:07 +0100404
405 // Check the correct operator of derived class is called
Jim Flynn397043f2019-10-17 17:37:10 +0100406 registry.GetFunctor(packetA.GetPacketFamily(), packetA.GetPacketId(), version)->operator()(packetA);
Francis Murtagh94d79152019-08-16 17:45:07 +0100407 BOOST_CHECK(testFunctorA.GetCount() == 1);
408 BOOST_CHECK(testFunctorB.GetCount() == 0);
409 BOOST_CHECK(testFunctorC.GetCount() == 0);
410
Jim Flynn397043f2019-10-17 17:37:10 +0100411 registry.GetFunctor(packetB.GetPacketFamily(), packetB.GetPacketId(), version)->operator()(packetB);
Francis Murtagh94d79152019-08-16 17:45:07 +0100412 BOOST_CHECK(testFunctorA.GetCount() == 1);
413 BOOST_CHECK(testFunctorB.GetCount() == 1);
414 BOOST_CHECK(testFunctorC.GetCount() == 0);
415
Jim Flynn397043f2019-10-17 17:37:10 +0100416 registry.GetFunctor(packetC.GetPacketFamily(), packetC.GetPacketId(), version)->operator()(packetC);
Francis Murtagh94d79152019-08-16 17:45:07 +0100417 BOOST_CHECK(testFunctorA.GetCount() == 1);
418 BOOST_CHECK(testFunctorB.GetCount() == 1);
419 BOOST_CHECK(testFunctorC.GetCount() == 1);
420
421 // Re-register an existing key with a new function
Jim Flynn397043f2019-10-17 17:37:10 +0100422 registry.RegisterFunctor(&testFunctorC, testFunctorA.GetFamilyId(), testFunctorA.GetPacketId(), version);
423 registry.GetFunctor(packetA.GetPacketFamily(), packetA.GetPacketId(), version)->operator()(packetC);
Francis Murtagh94d79152019-08-16 17:45:07 +0100424 BOOST_CHECK(testFunctorA.GetCount() == 1);
425 BOOST_CHECK(testFunctorB.GetCount() == 1);
426 BOOST_CHECK(testFunctorC.GetCount() == 2);
427
428 // Check that non-existent key returns nullptr for its functor
Jim Flynn397043f2019-10-17 17:37:10 +0100429 BOOST_CHECK_THROW(registry.GetFunctor(0, 0, 0), armnn::Exception);
Francis Murtagh94d79152019-08-16 17:45:07 +0100430}
431
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100432BOOST_AUTO_TEST_CASE(CheckPacketVersionResolver)
433{
434 // Set up random number generator for generating packetId values
435 std::random_device device;
436 std::mt19937 generator(device());
437 std::uniform_int_distribution<uint32_t> distribution(std::numeric_limits<uint32_t>::min(),
438 std::numeric_limits<uint32_t>::max());
439
440 // NOTE: Expected version is always 1.0.0, regardless of packetId
441 const Version expectedVersion(1, 0, 0);
442
443 PacketVersionResolver packetVersionResolver;
444
445 constexpr unsigned int numTests = 10u;
446
447 for (unsigned int i = 0u; i < numTests; ++i)
448 {
Jim Flynned25e0e2019-10-18 13:21:43 +0100449 const uint32_t familyId = distribution(generator);
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100450 const uint32_t packetId = distribution(generator);
Jim Flynned25e0e2019-10-18 13:21:43 +0100451 Version resolvedVersion = packetVersionResolver.ResolvePacketVersion(familyId, packetId);
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100452
453 BOOST_TEST(resolvedVersion == expectedVersion);
454 }
455}
Matteo Martincighd0613b52019-10-09 16:47:04 +0100456
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100457void ProfilingCurrentStateThreadImpl(ProfilingStateMachine& states)
458{
459 ProfilingState newState = ProfilingState::NotConnected;
460 states.GetCurrentState();
461 states.TransitionToState(newState);
462}
463
464BOOST_AUTO_TEST_CASE(CheckProfilingStateMachine)
465{
466 ProfilingStateMachine profilingState1(ProfilingState::Uninitialised);
467 profilingState1.TransitionToState(ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +0100468 BOOST_CHECK(profilingState1.GetCurrentState() == ProfilingState::Uninitialised);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100469
470 ProfilingStateMachine profilingState2(ProfilingState::Uninitialised);
471 profilingState2.TransitionToState(ProfilingState::NotConnected);
472 BOOST_CHECK(profilingState2.GetCurrentState() == ProfilingState::NotConnected);
473
474 ProfilingStateMachine profilingState3(ProfilingState::NotConnected);
475 profilingState3.TransitionToState(ProfilingState::NotConnected);
476 BOOST_CHECK(profilingState3.GetCurrentState() == ProfilingState::NotConnected);
477
478 ProfilingStateMachine profilingState4(ProfilingState::NotConnected);
479 profilingState4.TransitionToState(ProfilingState::WaitingForAck);
480 BOOST_CHECK(profilingState4.GetCurrentState() == ProfilingState::WaitingForAck);
481
482 ProfilingStateMachine profilingState5(ProfilingState::WaitingForAck);
483 profilingState5.TransitionToState(ProfilingState::WaitingForAck);
484 BOOST_CHECK(profilingState5.GetCurrentState() == ProfilingState::WaitingForAck);
485
486 ProfilingStateMachine profilingState6(ProfilingState::WaitingForAck);
487 profilingState6.TransitionToState(ProfilingState::Active);
488 BOOST_CHECK(profilingState6.GetCurrentState() == ProfilingState::Active);
489
490 ProfilingStateMachine profilingState7(ProfilingState::Active);
491 profilingState7.TransitionToState(ProfilingState::NotConnected);
492 BOOST_CHECK(profilingState7.GetCurrentState() == ProfilingState::NotConnected);
493
494 ProfilingStateMachine profilingState8(ProfilingState::Active);
495 profilingState8.TransitionToState(ProfilingState::Active);
496 BOOST_CHECK(profilingState8.GetCurrentState() == ProfilingState::Active);
497
498 ProfilingStateMachine profilingState9(ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +0100499 BOOST_CHECK_THROW(profilingState9.TransitionToState(ProfilingState::WaitingForAck), armnn::Exception);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100500
501 ProfilingStateMachine profilingState10(ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +0100502 BOOST_CHECK_THROW(profilingState10.TransitionToState(ProfilingState::Active), armnn::Exception);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100503
504 ProfilingStateMachine profilingState11(ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +0100505 BOOST_CHECK_THROW(profilingState11.TransitionToState(ProfilingState::Uninitialised), armnn::Exception);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100506
507 ProfilingStateMachine profilingState12(ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +0100508 BOOST_CHECK_THROW(profilingState12.TransitionToState(ProfilingState::Active), armnn::Exception);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100509
510 ProfilingStateMachine profilingState13(ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +0100511 BOOST_CHECK_THROW(profilingState13.TransitionToState(ProfilingState::Uninitialised), armnn::Exception);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100512
513 ProfilingStateMachine profilingState14(ProfilingState::WaitingForAck);
Jim Flynn53e46992019-10-14 12:31:10 +0100514 profilingState14.TransitionToState(ProfilingState::NotConnected);
515 BOOST_CHECK(profilingState14.GetCurrentState() == ProfilingState::NotConnected);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100516
517 ProfilingStateMachine profilingState15(ProfilingState::Active);
Keith Davis3201eea2019-10-24 17:30:41 +0100518 BOOST_CHECK_THROW(profilingState15.TransitionToState(ProfilingState::Uninitialised), armnn::Exception);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100519
520 ProfilingStateMachine profilingState16(armnn::profiling::ProfilingState::Active);
Keith Davis3201eea2019-10-24 17:30:41 +0100521 BOOST_CHECK_THROW(profilingState16.TransitionToState(ProfilingState::WaitingForAck), armnn::Exception);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100522
523 ProfilingStateMachine profilingState17(ProfilingState::Uninitialised);
524
Keith Davis3201eea2019-10-24 17:30:41 +0100525 std::thread thread1(ProfilingCurrentStateThreadImpl, std::ref(profilingState17));
526 std::thread thread2(ProfilingCurrentStateThreadImpl, std::ref(profilingState17));
527 std::thread thread3(ProfilingCurrentStateThreadImpl, std::ref(profilingState17));
528 std::thread thread4(ProfilingCurrentStateThreadImpl, std::ref(profilingState17));
529 std::thread thread5(ProfilingCurrentStateThreadImpl, std::ref(profilingState17));
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100530
531 thread1.join();
532 thread2.join();
533 thread3.join();
534 thread4.join();
535 thread5.join();
536
537 BOOST_TEST((profilingState17.GetCurrentState() == ProfilingState::NotConnected));
538}
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100539
Jim Flynn8355ec92019-09-17 12:29:50 +0100540void CaptureDataWriteThreadImpl(Holder& holder, uint32_t capturePeriod, const std::vector<uint16_t>& counterIds)
Francis Murtagh68f78d82019-09-04 16:42:29 +0100541{
Finn Williams032bc742020-02-12 11:02:34 +0000542 holder.SetCaptureData(capturePeriod, counterIds, {});
Francis Murtagh68f78d82019-09-04 16:42:29 +0100543}
544
Francis Murtaghbd707162019-09-09 11:26:44 +0100545void CaptureDataReadThreadImpl(const Holder& holder, CaptureData& captureData)
Francis Murtagh68f78d82019-09-04 16:42:29 +0100546{
547 captureData = holder.GetCaptureData();
548}
549
550BOOST_AUTO_TEST_CASE(CheckCaptureDataHolder)
551{
Francis Murtaghbd707162019-09-09 11:26:44 +0100552 std::map<uint32_t, std::vector<uint16_t>> periodIdMap;
553 std::vector<uint16_t> counterIds;
Jim Flynn8355ec92019-09-17 12:29:50 +0100554 uint32_t numThreads = 10;
555 for (uint32_t i = 0; i < numThreads; ++i)
Francis Murtaghbd707162019-09-09 11:26:44 +0100556 {
557 counterIds.emplace_back(i);
558 periodIdMap.insert(std::make_pair(i, counterIds));
559 }
Francis Murtagh68f78d82019-09-04 16:42:29 +0100560
Jim Flynn8355ec92019-09-17 12:29:50 +0100561 // Verify the read and write threads set the holder correctly
562 // and retrieve the expected values
Francis Murtagh68f78d82019-09-04 16:42:29 +0100563 Holder holder;
564 BOOST_CHECK((holder.GetCaptureData()).GetCapturePeriod() == 0);
565 BOOST_CHECK(((holder.GetCaptureData()).GetCounterIds()).empty());
566
567 // Check Holder functions
Francis Murtaghbd707162019-09-09 11:26:44 +0100568 std::thread thread1(CaptureDataWriteThreadImpl, std::ref(holder), 2, std::ref(periodIdMap[2]));
Francis Murtagh68f78d82019-09-04 16:42:29 +0100569 thread1.join();
Francis Murtaghbd707162019-09-09 11:26:44 +0100570 BOOST_CHECK((holder.GetCaptureData()).GetCapturePeriod() == 2);
571 BOOST_CHECK((holder.GetCaptureData()).GetCounterIds() == periodIdMap[2]);
Jim Flynn8355ec92019-09-17 12:29:50 +0100572 // NOTE: now that we have some initial values in the holder we don't have to worry
573 // in the multi-threaded section below about a read thread accessing the holder
574 // before any write thread has gotten to it so we read period = 0, counterIds empty
575 // instead of period = 0, counterIds = {0} as will the case when write thread 0
576 // has executed.
Francis Murtagh68f78d82019-09-04 16:42:29 +0100577
578 CaptureData captureData;
579 std::thread thread2(CaptureDataReadThreadImpl, std::ref(holder), std::ref(captureData));
580 thread2.join();
Jim Flynn8355ec92019-09-17 12:29:50 +0100581 BOOST_CHECK(captureData.GetCapturePeriod() == 2);
Francis Murtaghbd707162019-09-09 11:26:44 +0100582 BOOST_CHECK(captureData.GetCounterIds() == periodIdMap[2]);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100583
Jim Flynn8355ec92019-09-17 12:29:50 +0100584 std::map<uint32_t, CaptureData> captureDataIdMap;
585 for (uint32_t i = 0; i < numThreads; ++i)
586 {
587 CaptureData perThreadCaptureData;
588 captureDataIdMap.insert(std::make_pair(i, perThreadCaptureData));
589 }
590
Francis Murtaghbd707162019-09-09 11:26:44 +0100591 std::vector<std::thread> threadsVect;
Jim Flynn8355ec92019-09-17 12:29:50 +0100592 std::vector<std::thread> readThreadsVect;
593 for (uint32_t i = 0; i < numThreads; ++i)
Francis Murtaghbd707162019-09-09 11:26:44 +0100594 {
Keith Davis3201eea2019-10-24 17:30:41 +0100595 threadsVect.emplace_back(
596 std::thread(CaptureDataWriteThreadImpl, std::ref(holder), i, std::ref(periodIdMap[i])));
Francis Murtagh06965692019-09-05 16:29:01 +0100597
Jim Flynn8355ec92019-09-17 12:29:50 +0100598 // Verify that the CaptureData goes into the thread in a virgin state
599 BOOST_CHECK(captureDataIdMap.at(i).GetCapturePeriod() == 0);
600 BOOST_CHECK(captureDataIdMap.at(i).GetCounterIds().empty());
Keith Davis3201eea2019-10-24 17:30:41 +0100601 readThreadsVect.emplace_back(
602 std::thread(CaptureDataReadThreadImpl, std::ref(holder), std::ref(captureDataIdMap.at(i))));
Francis Murtaghbd707162019-09-09 11:26:44 +0100603 }
604
Jim Flynn8355ec92019-09-17 12:29:50 +0100605 for (uint32_t i = 0; i < numThreads; ++i)
Francis Murtaghbd707162019-09-09 11:26:44 +0100606 {
607 threadsVect[i].join();
Francis Murtaghbd707162019-09-09 11:26:44 +0100608 readThreadsVect[i].join();
609 }
Francis Murtagh68f78d82019-09-04 16:42:29 +0100610
Jim Flynn8355ec92019-09-17 12:29:50 +0100611 // Look at the CaptureData that each read thread has filled
612 // the capture period it read should match the counter ids entry
613 for (uint32_t i = 0; i < numThreads; ++i)
614 {
615 CaptureData perThreadCaptureData = captureDataIdMap.at(i);
616 BOOST_CHECK(perThreadCaptureData.GetCounterIds() == periodIdMap.at(perThreadCaptureData.GetCapturePeriod()));
617 }
Matthew Bentham46d1c622019-09-13 12:45:04 +0100618}
Francis Murtagh68f78d82019-09-04 16:42:29 +0100619
Matthew Bentham46d1c622019-09-13 12:45:04 +0100620BOOST_AUTO_TEST_CASE(CaptureDataMethods)
621{
Jim Flynn8355ec92019-09-17 12:29:50 +0100622 // Check CaptureData setter and getter functions
Keith Davis3201eea2019-10-24 17:30:41 +0100623 std::vector<uint16_t> counterIds = { 42, 29, 13 };
Jim Flynn8355ec92019-09-17 12:29:50 +0100624 CaptureData captureData;
625 BOOST_CHECK(captureData.GetCapturePeriod() == 0);
626 BOOST_CHECK((captureData.GetCounterIds()).empty());
627 captureData.SetCapturePeriod(150);
628 captureData.SetCounterIds(counterIds);
629 BOOST_CHECK(captureData.GetCapturePeriod() == 150);
630 BOOST_CHECK(captureData.GetCounterIds() == counterIds);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100631
Jim Flynn8355ec92019-09-17 12:29:50 +0100632 // Check assignment operator
Francis Murtagh68f78d82019-09-04 16:42:29 +0100633 CaptureData secondCaptureData;
Francis Murtagh68f78d82019-09-04 16:42:29 +0100634
Jim Flynn8355ec92019-09-17 12:29:50 +0100635 secondCaptureData = captureData;
636 BOOST_CHECK(secondCaptureData.GetCapturePeriod() == 150);
Matthew Bentham46d1c622019-09-13 12:45:04 +0100637 BOOST_CHECK(secondCaptureData.GetCounterIds() == counterIds);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100638
639 // Check copy constructor
Jim Flynn8355ec92019-09-17 12:29:50 +0100640 CaptureData copyConstructedCaptureData(captureData);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100641
Jim Flynn8355ec92019-09-17 12:29:50 +0100642 BOOST_CHECK(copyConstructedCaptureData.GetCapturePeriod() == 150);
Matthew Bentham46d1c622019-09-13 12:45:04 +0100643 BOOST_CHECK(copyConstructedCaptureData.GetCounterIds() == counterIds);
Keith Davis02356de2019-08-26 18:28:17 +0100644}
Francis Murtagh68f78d82019-09-04 16:42:29 +0100645
Keith Davis02356de2019-08-26 18:28:17 +0100646BOOST_AUTO_TEST_CASE(CheckProfilingServiceDisabled)
647{
648 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Sadik Armagan3184c902020-03-18 10:57:30 +0000649 armnn::profiling::ProfilingService profilingService;
Matteo Martincigha84edee2019-10-02 12:50:57 +0100650 profilingService.ResetExternalProfilingOptions(options, true);
651 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100652 profilingService.Update();
Matteo Martincigha84edee2019-10-02 12:50:57 +0100653 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis02356de2019-08-26 18:28:17 +0100654}
655
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100656BOOST_AUTO_TEST_CASE(CheckProfilingServiceCounterDirectory)
657{
658 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Sadik Armagan3184c902020-03-18 10:57:30 +0000659 armnn::profiling::ProfilingService profilingService;
Matteo Martincigha84edee2019-10-02 12:50:57 +0100660 profilingService.ResetExternalProfilingOptions(options, true);
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100661
Matteo Martincigha84edee2019-10-02 12:50:57 +0100662 const ICounterDirectory& counterDirectory0 = profilingService.GetCounterDirectory();
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100663 BOOST_CHECK(counterDirectory0.GetCounterCount() == 0);
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100664 profilingService.Update();
665 BOOST_CHECK(counterDirectory0.GetCounterCount() == 0);
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100666
667 options.m_EnableProfiling = true;
Matteo Martincigha84edee2019-10-02 12:50:57 +0100668 profilingService.ResetExternalProfilingOptions(options);
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100669
Matteo Martincigha84edee2019-10-02 12:50:57 +0100670 const ICounterDirectory& counterDirectory1 = profilingService.GetCounterDirectory();
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100671 BOOST_CHECK(counterDirectory1.GetCounterCount() == 0);
672 profilingService.Update();
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100673 BOOST_CHECK(counterDirectory1.GetCounterCount() != 0);
Colm Donelan2ba48d22019-11-29 09:10:59 +0000674 // Reset the profiling service to stop any running thread
675 options.m_EnableProfiling = false;
676 profilingService.ResetExternalProfilingOptions(options, true);
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100677}
678
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100679BOOST_AUTO_TEST_CASE(CheckProfilingServiceCounterValues)
680{
681 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +0100682 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +0000683 armnn::profiling::ProfilingService profilingService;
Matteo Martincigha84edee2019-10-02 12:50:57 +0100684 profilingService.ResetExternalProfilingOptions(options, true);
685
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100686 profilingService.Update();
Matteo Martincigha84edee2019-10-02 12:50:57 +0100687 const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
Keith Davis3201eea2019-10-24 17:30:41 +0100688 const Counters& counters = counterDirectory.GetCounters();
Matteo Martincigha84edee2019-10-02 12:50:57 +0100689 BOOST_CHECK(!counters.empty());
690
Keith Davise394bd92019-12-02 15:12:19 +0000691 // Get the UID of the first counter for testing;
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100692
693 ProfilingService* profilingServicePtr = &profilingService;
694 std::vector<std::thread> writers;
695
Jan Eilersa10e2a22020-03-26 12:04:54 +0000696 for (int i = 0; i < 10; ++i)
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100697 {
Matteo Martincigha84edee2019-10-02 12:50:57 +0100698 // Increment and decrement the first counter
Keith Davise394bd92019-12-02 15:12:19 +0000699 writers.push_back(std::thread(&ProfilingService::IncrementCounterValue,
700 profilingServicePtr,
701 armnn::profiling::REGISTERED_BACKENDS));
702
703 writers.push_back(std::thread(&ProfilingService::IncrementCounterValue,
704 profilingServicePtr,
705 armnn::profiling::UNREGISTERED_BACKENDS));
706
Matteo Martincigha84edee2019-10-02 12:50:57 +0100707 // Add 10 and subtract 5 from the first counter
Keith Davise394bd92019-12-02 15:12:19 +0000708 writers.push_back(std::thread(&ProfilingService::AddCounterValue,
709 profilingServicePtr,
710 armnn::profiling::INFERENCES_RUN,
711 10));
712 writers.push_back(std::thread(&ProfilingService::SubtractCounterValue,
713 profilingServicePtr,
714 armnn::profiling::INFERENCES_RUN,
715 5));
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100716 }
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100717 std::for_each(writers.begin(), writers.end(), mem_fn(&std::thread::join));
718
Matteo Martincigha84edee2019-10-02 12:50:57 +0100719 uint32_t counterValue = 0;
Keith Davise394bd92019-12-02 15:12:19 +0000720 BOOST_CHECK(counterValue ==
721 (profilingService.GetCounterValue(armnn::profiling::UNREGISTERED_BACKENDS)
722 - profilingService.GetCounterValue(armnn::profiling::REGISTERED_BACKENDS)));
Jan Eilersa10e2a22020-03-26 12:04:54 +0000723 BOOST_CHECK(profilingService.GetCounterValue(armnn::profiling::INFERENCES_RUN) == 50);
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100724
Keith Davise394bd92019-12-02 15:12:19 +0000725 BOOST_CHECK_NO_THROW(profilingService.SetCounterValue(armnn::profiling::UNREGISTERED_BACKENDS, 4));
726 BOOST_CHECK_NO_THROW(counterValue = profilingService.GetCounterValue(armnn::profiling::UNREGISTERED_BACKENDS));
727 BOOST_CHECK(counterValue == 4);
Colm Donelan2ba48d22019-11-29 09:10:59 +0000728 // Reset the profiling service to stop any running thread
729 options.m_EnableProfiling = false;
730 profilingService.ResetExternalProfilingOptions(options, true);
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100731}
732
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100733BOOST_AUTO_TEST_CASE(CheckProfilingObjectUids)
Matteo Martincighab173e92019-09-05 12:02:04 +0100734{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100735 uint16_t uid = 0;
736 BOOST_CHECK_NO_THROW(uid = GetNextUid());
737 BOOST_CHECK(uid >= 1);
738
739 uint16_t nextUid = 0;
740 BOOST_CHECK_NO_THROW(nextUid = GetNextUid());
741 BOOST_CHECK(nextUid > uid);
742
743 std::vector<uint16_t> counterUids;
Keith Davise394bd92019-12-02 15:12:19 +0000744 BOOST_CHECK_NO_THROW(counterUids = GetNextCounterUids(uid,0));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100745 BOOST_CHECK(counterUids.size() == 1);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100746
747 std::vector<uint16_t> nextCounterUids;
Keith Davise394bd92019-12-02 15:12:19 +0000748 BOOST_CHECK_NO_THROW(nextCounterUids = GetNextCounterUids(nextUid, 2));
749 BOOST_CHECK(nextCounterUids.size() == 2);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100750 BOOST_CHECK(nextCounterUids[0] > counterUids[0]);
751
752 std::vector<uint16_t> counterUidsMultiCore;
Keith Davise394bd92019-12-02 15:12:19 +0000753 uint16_t thirdUid = 4;
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100754 uint16_t numberOfCores = 13;
Keith Davise394bd92019-12-02 15:12:19 +0000755 BOOST_CHECK_NO_THROW(counterUidsMultiCore = GetNextCounterUids(thirdUid, numberOfCores));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100756 BOOST_CHECK(counterUidsMultiCore.size() == numberOfCores);
757 BOOST_CHECK(counterUidsMultiCore.front() >= nextCounterUids[0]);
Keith Davis3201eea2019-10-24 17:30:41 +0100758 for (size_t i = 1; i < numberOfCores; i++)
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100759 {
760 BOOST_CHECK(counterUidsMultiCore[i] == counterUidsMultiCore[i - 1] + 1);
761 }
762 BOOST_CHECK(counterUidsMultiCore.back() == counterUidsMultiCore.front() + numberOfCores - 1);
Matteo Martincighab173e92019-09-05 12:02:04 +0100763}
764
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100765BOOST_AUTO_TEST_CASE(CheckCounterDirectoryRegisterCategory)
Matteo Martincighab173e92019-09-05 12:02:04 +0100766{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100767 CounterDirectory counterDirectory;
Keith Davis3201eea2019-10-24 17:30:41 +0100768 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
769 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100770 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
Keith Davis3201eea2019-10-24 17:30:41 +0100771 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
Matteo Martincighab173e92019-09-05 12:02:04 +0100772
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100773 // Register a category with an invalid name
774 const Category* noCategory = nullptr;
775 BOOST_CHECK_THROW(noCategory = counterDirectory.RegisterCategory(""), armnn::InvalidArgumentException);
776 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
777 BOOST_CHECK(!noCategory);
Matteo Martincighab173e92019-09-05 12:02:04 +0100778
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100779 // Register a category with an invalid name
780 BOOST_CHECK_THROW(noCategory = counterDirectory.RegisterCategory("invalid category"),
781 armnn::InvalidArgumentException);
782 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
783 BOOST_CHECK(!noCategory);
784
785 // Register a new category
786 const std::string categoryName = "some_category";
Keith Davis3201eea2019-10-24 17:30:41 +0100787 const Category* category = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100788 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
789 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
790 BOOST_CHECK(category);
791 BOOST_CHECK(category->m_Name == categoryName);
792 BOOST_CHECK(category->m_Counters.empty());
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100793
794 // Get the registered category
795 const Category* registeredCategory = counterDirectory.GetCategory(categoryName);
796 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
797 BOOST_CHECK(registeredCategory);
798 BOOST_CHECK(registeredCategory == category);
799
800 // Try to get a category not registered
801 const Category* notRegisteredCategory = counterDirectory.GetCategory("not_registered_category");
802 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
803 BOOST_CHECK(!notRegisteredCategory);
804
805 // Register a category already registered
806 const Category* anotherCategory = nullptr;
807 BOOST_CHECK_THROW(anotherCategory = counterDirectory.RegisterCategory(categoryName),
808 armnn::InvalidArgumentException);
809 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
810 BOOST_CHECK(!anotherCategory);
811
812 // Register a device for testing
813 const std::string deviceName = "some_device";
Keith Davis3201eea2019-10-24 17:30:41 +0100814 const Device* device = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100815 BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName));
816 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
817 BOOST_CHECK(device);
818 BOOST_CHECK(device->m_Uid >= 1);
819 BOOST_CHECK(device->m_Name == deviceName);
820 BOOST_CHECK(device->m_Cores == 0);
821
822 // Register a new category not associated to any device
823 const std::string categoryWoDeviceName = "some_category_without_device";
Keith Davis3201eea2019-10-24 17:30:41 +0100824 const Category* categoryWoDevice = nullptr;
Sadik Armagan4c998992020-02-25 12:44:44 +0000825 BOOST_CHECK_NO_THROW(categoryWoDevice = counterDirectory.RegisterCategory(categoryWoDeviceName));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100826 BOOST_CHECK(counterDirectory.GetCategoryCount() == 2);
827 BOOST_CHECK(categoryWoDevice);
828 BOOST_CHECK(categoryWoDevice->m_Name == categoryWoDeviceName);
829 BOOST_CHECK(categoryWoDevice->m_Counters.empty());
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100830
Sadik Armagan4c998992020-02-25 12:44:44 +0000831 // Register a new category associated to an invalid device name (already exist)
832 const Category* categoryInvalidDeviceName = nullptr;
833 BOOST_CHECK_THROW(categoryInvalidDeviceName =
834 counterDirectory.RegisterCategory(categoryWoDeviceName),
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100835 armnn::InvalidArgumentException);
836 BOOST_CHECK(counterDirectory.GetCategoryCount() == 2);
Sadik Armagan4c998992020-02-25 12:44:44 +0000837 BOOST_CHECK(!categoryInvalidDeviceName);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100838
839 // Register a new category associated to a valid device
840 const std::string categoryWValidDeviceName = "some_category_with_valid_device";
Keith Davis3201eea2019-10-24 17:30:41 +0100841 const Category* categoryWValidDevice = nullptr;
842 BOOST_CHECK_NO_THROW(categoryWValidDevice =
Sadik Armagan4c998992020-02-25 12:44:44 +0000843 counterDirectory.RegisterCategory(categoryWValidDeviceName));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100844 BOOST_CHECK(counterDirectory.GetCategoryCount() == 3);
845 BOOST_CHECK(categoryWValidDevice);
846 BOOST_CHECK(categoryWValidDevice != category);
847 BOOST_CHECK(categoryWValidDevice->m_Name == categoryWValidDeviceName);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100848
849 // Register a counter set for testing
850 const std::string counterSetName = "some_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +0100851 const CounterSet* counterSet = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100852 BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
853 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
854 BOOST_CHECK(counterSet);
855 BOOST_CHECK(counterSet->m_Uid >= 1);
856 BOOST_CHECK(counterSet->m_Name == counterSetName);
857 BOOST_CHECK(counterSet->m_Count == 0);
858
859 // Register a new category not associated to any counter set
860 const std::string categoryWoCounterSetName = "some_category_without_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +0100861 const Category* categoryWoCounterSet = nullptr;
862 BOOST_CHECK_NO_THROW(categoryWoCounterSet =
Sadik Armagan4c998992020-02-25 12:44:44 +0000863 counterDirectory.RegisterCategory(categoryWoCounterSetName));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100864 BOOST_CHECK(counterDirectory.GetCategoryCount() == 4);
865 BOOST_CHECK(categoryWoCounterSet);
866 BOOST_CHECK(categoryWoCounterSet->m_Name == categoryWoCounterSetName);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100867
868 // Register a new category associated to a valid counter set
869 const std::string categoryWValidCounterSetName = "some_category_with_valid_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +0100870 const Category* categoryWValidCounterSet = nullptr;
Sadik Armagan4c998992020-02-25 12:44:44 +0000871 BOOST_CHECK_NO_THROW(categoryWValidCounterSet = counterDirectory.RegisterCategory(categoryWValidCounterSetName));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100872 BOOST_CHECK(counterDirectory.GetCategoryCount() == 5);
873 BOOST_CHECK(categoryWValidCounterSet);
874 BOOST_CHECK(categoryWValidCounterSet != category);
875 BOOST_CHECK(categoryWValidCounterSet->m_Name == categoryWValidCounterSetName);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100876
877 // Register a new category associated to a valid device and counter set
878 const std::string categoryWValidDeviceAndValidCounterSetName = "some_category_with_valid_device_and_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +0100879 const Category* categoryWValidDeviceAndValidCounterSet = nullptr;
880 BOOST_CHECK_NO_THROW(categoryWValidDeviceAndValidCounterSet = counterDirectory.RegisterCategory(
Sadik Armagan4c998992020-02-25 12:44:44 +0000881 categoryWValidDeviceAndValidCounterSetName));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100882 BOOST_CHECK(counterDirectory.GetCategoryCount() == 6);
883 BOOST_CHECK(categoryWValidDeviceAndValidCounterSet);
884 BOOST_CHECK(categoryWValidDeviceAndValidCounterSet != category);
885 BOOST_CHECK(categoryWValidDeviceAndValidCounterSet->m_Name == categoryWValidDeviceAndValidCounterSetName);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100886}
887
888BOOST_AUTO_TEST_CASE(CheckCounterDirectoryRegisterDevice)
889{
890 CounterDirectory counterDirectory;
Keith Davis3201eea2019-10-24 17:30:41 +0100891 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
892 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100893 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
Keith Davis3201eea2019-10-24 17:30:41 +0100894 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100895
896 // Register a device with an invalid name
897 const Device* noDevice = nullptr;
898 BOOST_CHECK_THROW(noDevice = counterDirectory.RegisterDevice(""), armnn::InvalidArgumentException);
899 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
900 BOOST_CHECK(!noDevice);
901
902 // Register a device with an invalid name
903 BOOST_CHECK_THROW(noDevice = counterDirectory.RegisterDevice("inv@lid nam€"), armnn::InvalidArgumentException);
904 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
905 BOOST_CHECK(!noDevice);
906
907 // Register a new device with no cores or parent category
908 const std::string deviceName = "some_device";
Keith Davis3201eea2019-10-24 17:30:41 +0100909 const Device* device = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100910 BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName));
911 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
912 BOOST_CHECK(device);
913 BOOST_CHECK(device->m_Name == deviceName);
914 BOOST_CHECK(device->m_Uid >= 1);
915 BOOST_CHECK(device->m_Cores == 0);
916
Matteo Martincigh657ab2d2019-09-18 10:53:24 +0100917 // Try getting an unregistered device
918 const Device* unregisteredDevice = counterDirectory.GetDevice(9999);
919 BOOST_CHECK(!unregisteredDevice);
920
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100921 // Get the registered device
922 const Device* registeredDevice = counterDirectory.GetDevice(device->m_Uid);
923 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
924 BOOST_CHECK(registeredDevice);
925 BOOST_CHECK(registeredDevice == device);
926
Matteo Martincigh657ab2d2019-09-18 10:53:24 +0100927 // Register a device with the name of a device already registered
928 const Device* deviceSameName = nullptr;
929 BOOST_CHECK_THROW(deviceSameName = counterDirectory.RegisterDevice(deviceName), armnn::InvalidArgumentException);
930 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
931 BOOST_CHECK(!deviceSameName);
932
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100933 // Register a new device with cores and no parent category
934 const std::string deviceWCoresName = "some_device_with_cores";
Keith Davis3201eea2019-10-24 17:30:41 +0100935 const Device* deviceWCores = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100936 BOOST_CHECK_NO_THROW(deviceWCores = counterDirectory.RegisterDevice(deviceWCoresName, 2));
937 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
938 BOOST_CHECK(deviceWCores);
939 BOOST_CHECK(deviceWCores->m_Name == deviceWCoresName);
940 BOOST_CHECK(deviceWCores->m_Uid >= 1);
941 BOOST_CHECK(deviceWCores->m_Uid > device->m_Uid);
942 BOOST_CHECK(deviceWCores->m_Cores == 2);
943
944 // Get the registered device
945 const Device* registeredDeviceWCores = counterDirectory.GetDevice(deviceWCores->m_Uid);
946 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
947 BOOST_CHECK(registeredDeviceWCores);
948 BOOST_CHECK(registeredDeviceWCores == deviceWCores);
949 BOOST_CHECK(registeredDeviceWCores != device);
950
951 // Register a new device with cores and invalid parent category
952 const std::string deviceWCoresWInvalidParentCategoryName = "some_device_with_cores_with_invalid_parent_category";
Keith Davis3201eea2019-10-24 17:30:41 +0100953 const Device* deviceWCoresWInvalidParentCategory = nullptr;
954 BOOST_CHECK_THROW(deviceWCoresWInvalidParentCategory =
955 counterDirectory.RegisterDevice(deviceWCoresWInvalidParentCategoryName, 3, std::string("")),
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100956 armnn::InvalidArgumentException);
957 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
958 BOOST_CHECK(!deviceWCoresWInvalidParentCategory);
959
960 // Register a new device with cores and invalid parent category
961 const std::string deviceWCoresWInvalidParentCategoryName2 = "some_device_with_cores_with_invalid_parent_category2";
Keith Davis3201eea2019-10-24 17:30:41 +0100962 const Device* deviceWCoresWInvalidParentCategory2 = nullptr;
963 BOOST_CHECK_THROW(deviceWCoresWInvalidParentCategory2 = counterDirectory.RegisterDevice(
964 deviceWCoresWInvalidParentCategoryName2, 3, std::string("invalid_parent_category")),
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100965 armnn::InvalidArgumentException);
966 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
967 BOOST_CHECK(!deviceWCoresWInvalidParentCategory2);
968
969 // Register a category for testing
970 const std::string categoryName = "some_category";
Keith Davis3201eea2019-10-24 17:30:41 +0100971 const Category* category = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100972 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
973 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
974 BOOST_CHECK(category);
975 BOOST_CHECK(category->m_Name == categoryName);
976 BOOST_CHECK(category->m_Counters.empty());
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100977
978 // Register a new device with cores and valid parent category
979 const std::string deviceWCoresWValidParentCategoryName = "some_device_with_cores_with_valid_parent_category";
Keith Davis3201eea2019-10-24 17:30:41 +0100980 const Device* deviceWCoresWValidParentCategory = nullptr;
981 BOOST_CHECK_NO_THROW(deviceWCoresWValidParentCategory =
982 counterDirectory.RegisterDevice(deviceWCoresWValidParentCategoryName, 4, categoryName));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100983 BOOST_CHECK(counterDirectory.GetDeviceCount() == 3);
984 BOOST_CHECK(deviceWCoresWValidParentCategory);
985 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Name == deviceWCoresWValidParentCategoryName);
986 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Uid >= 1);
987 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Uid > device->m_Uid);
988 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Uid > deviceWCores->m_Uid);
989 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Cores == 4);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100990}
991
992BOOST_AUTO_TEST_CASE(CheckCounterDirectoryRegisterCounterSet)
993{
994 CounterDirectory counterDirectory;
Keith Davis3201eea2019-10-24 17:30:41 +0100995 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
996 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100997 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
Keith Davis3201eea2019-10-24 17:30:41 +0100998 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100999
1000 // Register a counter set with an invalid name
1001 const CounterSet* noCounterSet = nullptr;
1002 BOOST_CHECK_THROW(noCounterSet = counterDirectory.RegisterCounterSet(""), armnn::InvalidArgumentException);
1003 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
1004 BOOST_CHECK(!noCounterSet);
1005
1006 // Register a counter set with an invalid name
1007 BOOST_CHECK_THROW(noCounterSet = counterDirectory.RegisterCounterSet("invalid name"),
1008 armnn::InvalidArgumentException);
1009 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
1010 BOOST_CHECK(!noCounterSet);
1011
1012 // Register a new counter set with no count or parent category
1013 const std::string counterSetName = "some_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +01001014 const CounterSet* counterSet = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001015 BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
1016 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1017 BOOST_CHECK(counterSet);
1018 BOOST_CHECK(counterSet->m_Name == counterSetName);
1019 BOOST_CHECK(counterSet->m_Uid >= 1);
1020 BOOST_CHECK(counterSet->m_Count == 0);
1021
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001022 // Try getting an unregistered counter set
1023 const CounterSet* unregisteredCounterSet = counterDirectory.GetCounterSet(9999);
1024 BOOST_CHECK(!unregisteredCounterSet);
1025
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001026 // Get the registered counter set
1027 const CounterSet* registeredCounterSet = counterDirectory.GetCounterSet(counterSet->m_Uid);
1028 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1029 BOOST_CHECK(registeredCounterSet);
1030 BOOST_CHECK(registeredCounterSet == counterSet);
1031
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001032 // Register a counter set with the name of a counter set already registered
1033 const CounterSet* counterSetSameName = nullptr;
1034 BOOST_CHECK_THROW(counterSetSameName = counterDirectory.RegisterCounterSet(counterSetName),
1035 armnn::InvalidArgumentException);
1036 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1037 BOOST_CHECK(!counterSetSameName);
1038
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001039 // Register a new counter set with count and no parent category
1040 const std::string counterSetWCountName = "some_counter_set_with_count";
Keith Davis3201eea2019-10-24 17:30:41 +01001041 const CounterSet* counterSetWCount = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001042 BOOST_CHECK_NO_THROW(counterSetWCount = counterDirectory.RegisterCounterSet(counterSetWCountName, 37));
1043 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 2);
1044 BOOST_CHECK(counterSetWCount);
1045 BOOST_CHECK(counterSetWCount->m_Name == counterSetWCountName);
1046 BOOST_CHECK(counterSetWCount->m_Uid >= 1);
1047 BOOST_CHECK(counterSetWCount->m_Uid > counterSet->m_Uid);
1048 BOOST_CHECK(counterSetWCount->m_Count == 37);
1049
1050 // Get the registered counter set
1051 const CounterSet* registeredCounterSetWCount = counterDirectory.GetCounterSet(counterSetWCount->m_Uid);
1052 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 2);
1053 BOOST_CHECK(registeredCounterSetWCount);
1054 BOOST_CHECK(registeredCounterSetWCount == counterSetWCount);
1055 BOOST_CHECK(registeredCounterSetWCount != counterSet);
1056
1057 // Register a new counter set with count and invalid parent category
1058 const std::string counterSetWCountWInvalidParentCategoryName = "some_counter_set_with_count_"
1059 "with_invalid_parent_category";
1060 const CounterSet* counterSetWCountWInvalidParentCategory = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001061 BOOST_CHECK_THROW(counterSetWCountWInvalidParentCategory = counterDirectory.RegisterCounterSet(
1062 counterSetWCountWInvalidParentCategoryName, 42, std::string("")),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001063 armnn::InvalidArgumentException);
1064 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 2);
1065 BOOST_CHECK(!counterSetWCountWInvalidParentCategory);
1066
1067 // Register a new counter set with count and invalid parent category
1068 const std::string counterSetWCountWInvalidParentCategoryName2 = "some_counter_set_with_count_"
1069 "with_invalid_parent_category2";
1070 const CounterSet* counterSetWCountWInvalidParentCategory2 = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001071 BOOST_CHECK_THROW(counterSetWCountWInvalidParentCategory2 = counterDirectory.RegisterCounterSet(
1072 counterSetWCountWInvalidParentCategoryName2, 42, std::string("invalid_parent_category")),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001073 armnn::InvalidArgumentException);
1074 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 2);
1075 BOOST_CHECK(!counterSetWCountWInvalidParentCategory2);
1076
1077 // Register a category for testing
1078 const std::string categoryName = "some_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001079 const Category* category = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001080 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
1081 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
1082 BOOST_CHECK(category);
1083 BOOST_CHECK(category->m_Name == categoryName);
1084 BOOST_CHECK(category->m_Counters.empty());
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001085
1086 // Register a new counter set with count and valid parent category
1087 const std::string counterSetWCountWValidParentCategoryName = "some_counter_set_with_count_"
1088 "with_valid_parent_category";
1089 const CounterSet* counterSetWCountWValidParentCategory = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001090 BOOST_CHECK_NO_THROW(counterSetWCountWValidParentCategory = counterDirectory.RegisterCounterSet(
1091 counterSetWCountWValidParentCategoryName, 42, categoryName));
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001092 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 3);
1093 BOOST_CHECK(counterSetWCountWValidParentCategory);
1094 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Name == counterSetWCountWValidParentCategoryName);
1095 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Uid >= 1);
1096 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Uid > counterSet->m_Uid);
1097 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Uid > counterSetWCount->m_Uid);
1098 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Count == 42);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001099
Sadik Armagan4c998992020-02-25 12:44:44 +00001100 // Register a counter set associated to a category with invalid name
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001101 const std::string counterSetSameCategoryName = "some_counter_set_with_invalid_parent_category";
Sadik Armagan4c998992020-02-25 12:44:44 +00001102 const std::string invalidCategoryName = "";
Keith Davis3201eea2019-10-24 17:30:41 +01001103 const CounterSet* counterSetSameCategory = nullptr;
1104 BOOST_CHECK_THROW(counterSetSameCategory =
Sadik Armagan4c998992020-02-25 12:44:44 +00001105 counterDirectory.RegisterCounterSet(counterSetSameCategoryName, 0, invalidCategoryName),
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001106 armnn::InvalidArgumentException);
1107 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 3);
1108 BOOST_CHECK(!counterSetSameCategory);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001109}
1110
1111BOOST_AUTO_TEST_CASE(CheckCounterDirectoryRegisterCounter)
1112{
1113 CounterDirectory counterDirectory;
Keith Davis3201eea2019-10-24 17:30:41 +01001114 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
1115 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001116 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
Keith Davis3201eea2019-10-24 17:30:41 +01001117 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001118
1119 // Register a counter with an invalid parent category name
1120 const Counter* noCounter = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001121 BOOST_CHECK_THROW(noCounter =
Keith Davise394bd92019-12-02 15:12:19 +00001122 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1123 0,
1124 "",
1125 0,
1126 1,
1127 123.45f,
1128 "valid ",
1129 "name"),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001130 armnn::InvalidArgumentException);
1131 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1132 BOOST_CHECK(!noCounter);
1133
1134 // Register a counter with an invalid parent category name
Keith Davise394bd92019-12-02 15:12:19 +00001135 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1136 1,
1137 "invalid parent category",
1138 0,
1139 1,
1140 123.45f,
1141 "valid name",
1142 "valid description"),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001143 armnn::InvalidArgumentException);
1144 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1145 BOOST_CHECK(!noCounter);
1146
1147 // Register a counter with an invalid class
Keith Davise394bd92019-12-02 15:12:19 +00001148 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1149 2,
1150 "valid_parent_category",
1151 2,
1152 1,
1153 123.45f,
1154 "valid "
1155 "name",
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001156 "valid description"),
1157 armnn::InvalidArgumentException);
1158 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1159 BOOST_CHECK(!noCounter);
1160
1161 // Register a counter with an invalid interpolation
Keith Davise394bd92019-12-02 15:12:19 +00001162 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1163 4,
1164 "valid_parent_category",
1165 0,
1166 3,
1167 123.45f,
1168 "valid "
1169 "name",
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001170 "valid description"),
1171 armnn::InvalidArgumentException);
1172 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1173 BOOST_CHECK(!noCounter);
1174
1175 // Register a counter with an invalid multiplier
Keith Davise394bd92019-12-02 15:12:19 +00001176 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1177 5,
1178 "valid_parent_category",
1179 0,
1180 1,
1181 .0f,
1182 "valid "
1183 "name",
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001184 "valid description"),
1185 armnn::InvalidArgumentException);
1186 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1187 BOOST_CHECK(!noCounter);
1188
1189 // Register a counter with an invalid name
Keith Davis3201eea2019-10-24 17:30:41 +01001190 BOOST_CHECK_THROW(
Keith Davise394bd92019-12-02 15:12:19 +00001191 noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1192 6,
1193 "valid_parent_category",
1194 0,
1195 1,
1196 123.45f,
1197 "",
1198 "valid description"),
Keith Davis3201eea2019-10-24 17:30:41 +01001199 armnn::InvalidArgumentException);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001200 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1201 BOOST_CHECK(!noCounter);
1202
1203 // Register a counter with an invalid name
Keith Davise394bd92019-12-02 15:12:19 +00001204 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1205 7,
1206 "valid_parent_category",
1207 0,
1208 1,
1209 123.45f,
1210 "invalid nam€",
1211 "valid description"),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001212 armnn::InvalidArgumentException);
1213 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1214 BOOST_CHECK(!noCounter);
1215
1216 // Register a counter with an invalid description
Keith Davis3201eea2019-10-24 17:30:41 +01001217 BOOST_CHECK_THROW(noCounter =
Keith Davise394bd92019-12-02 15:12:19 +00001218 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1219 8,
1220 "valid_parent_category",
1221 0,
1222 1,
1223 123.45f,
1224 "valid name",
1225 ""),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001226 armnn::InvalidArgumentException);
1227 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1228 BOOST_CHECK(!noCounter);
1229
1230 // Register a counter with an invalid description
Keith Davise394bd92019-12-02 15:12:19 +00001231 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1232 9,
1233 "valid_parent_category",
1234 0,
1235 1,
1236 123.45f,
1237 "valid "
1238 "name",
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001239 "inv@lid description"),
1240 armnn::InvalidArgumentException);
1241 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1242 BOOST_CHECK(!noCounter);
1243
1244 // Register a counter with an invalid unit2
Keith Davise394bd92019-12-02 15:12:19 +00001245 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1246 10,
1247 "valid_parent_category",
1248 0,
1249 1,
1250 123.45f,
1251 "valid name",
1252 "valid description",
1253 std::string("Mb/s2")),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001254 armnn::InvalidArgumentException);
1255 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1256 BOOST_CHECK(!noCounter);
1257
1258 // Register a counter with a non-existing parent category name
Keith Davise394bd92019-12-02 15:12:19 +00001259 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1260 11,
1261 "invalid_parent_category",
1262 0,
1263 1,
1264 123.45f,
1265 "valid name",
1266 "valid description"),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001267 armnn::InvalidArgumentException);
1268 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1269 BOOST_CHECK(!noCounter);
1270
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001271 // Try getting an unregistered counter
1272 const Counter* unregisteredCounter = counterDirectory.GetCounter(9999);
1273 BOOST_CHECK(!unregisteredCounter);
1274
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001275 // Register a category for testing
1276 const std::string categoryName = "some_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001277 const Category* category = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001278 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
1279 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
1280 BOOST_CHECK(category);
1281 BOOST_CHECK(category->m_Name == categoryName);
1282 BOOST_CHECK(category->m_Counters.empty());
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001283
1284 // Register a counter with a valid parent category name
1285 const Counter* counter = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001286 BOOST_CHECK_NO_THROW(
Keith Davise394bd92019-12-02 15:12:19 +00001287 counter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1288 12,
1289 categoryName,
1290 0,
1291 1,
1292 123.45f,
1293 "valid name",
1294 "valid description"));
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001295 BOOST_CHECK(counterDirectory.GetCounterCount() == 1);
1296 BOOST_CHECK(counter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001297 BOOST_CHECK(counter->m_MaxCounterUid == counter->m_Uid);
1298 BOOST_CHECK(counter->m_Class == 0);
1299 BOOST_CHECK(counter->m_Interpolation == 1);
1300 BOOST_CHECK(counter->m_Multiplier == 123.45f);
1301 BOOST_CHECK(counter->m_Name == "valid name");
1302 BOOST_CHECK(counter->m_Description == "valid description");
1303 BOOST_CHECK(counter->m_Units == "");
1304 BOOST_CHECK(counter->m_DeviceUid == 0);
1305 BOOST_CHECK(counter->m_CounterSetUid == 0);
1306 BOOST_CHECK(category->m_Counters.size() == 1);
1307 BOOST_CHECK(category->m_Counters.back() == counter->m_Uid);
1308
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001309 // Register a counter with a name of a counter already registered for the given parent category name
1310 const Counter* counterSameName = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001311 BOOST_CHECK_THROW(counterSameName =
Keith Davise394bd92019-12-02 15:12:19 +00001312 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1313 13,
1314 categoryName,
1315 0,
1316 0,
1317 1.0f,
1318 "valid name",
1319 "valid description",
1320 std::string("description")),
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001321 armnn::InvalidArgumentException);
1322 BOOST_CHECK(counterDirectory.GetCounterCount() == 1);
1323 BOOST_CHECK(!counterSameName);
1324
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001325 // Register a counter with a valid parent category name and units
1326 const Counter* counterWUnits = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001327 BOOST_CHECK_NO_THROW(counterWUnits = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1328 14,
1329 categoryName,
1330 0,
1331 1,
1332 123.45f,
1333 "valid name 2",
1334 "valid description",
1335 std::string("Mnnsq2"))); // Units
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001336 BOOST_CHECK(counterDirectory.GetCounterCount() == 2);
1337 BOOST_CHECK(counterWUnits);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001338 BOOST_CHECK(counterWUnits->m_Uid > counter->m_Uid);
1339 BOOST_CHECK(counterWUnits->m_MaxCounterUid == counterWUnits->m_Uid);
1340 BOOST_CHECK(counterWUnits->m_Class == 0);
1341 BOOST_CHECK(counterWUnits->m_Interpolation == 1);
1342 BOOST_CHECK(counterWUnits->m_Multiplier == 123.45f);
1343 BOOST_CHECK(counterWUnits->m_Name == "valid name 2");
1344 BOOST_CHECK(counterWUnits->m_Description == "valid description");
1345 BOOST_CHECK(counterWUnits->m_Units == "Mnnsq2");
1346 BOOST_CHECK(counterWUnits->m_DeviceUid == 0);
1347 BOOST_CHECK(counterWUnits->m_CounterSetUid == 0);
1348 BOOST_CHECK(category->m_Counters.size() == 2);
1349 BOOST_CHECK(category->m_Counters.back() == counterWUnits->m_Uid);
1350
1351 // Register a counter with a valid parent category name and not associated with a device
1352 const Counter* counterWoDevice = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001353 BOOST_CHECK_NO_THROW(counterWoDevice = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1354 26,
1355 categoryName,
1356 0,
1357 1,
1358 123.45f,
1359 "valid name 3",
1360 "valid description",
1361 armnn::EmptyOptional(),// Units
1362 armnn::EmptyOptional(),// Number of cores
1363 0)); // Device UID
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001364 BOOST_CHECK(counterDirectory.GetCounterCount() == 3);
1365 BOOST_CHECK(counterWoDevice);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001366 BOOST_CHECK(counterWoDevice->m_Uid > counter->m_Uid);
1367 BOOST_CHECK(counterWoDevice->m_MaxCounterUid == counterWoDevice->m_Uid);
1368 BOOST_CHECK(counterWoDevice->m_Class == 0);
1369 BOOST_CHECK(counterWoDevice->m_Interpolation == 1);
1370 BOOST_CHECK(counterWoDevice->m_Multiplier == 123.45f);
1371 BOOST_CHECK(counterWoDevice->m_Name == "valid name 3");
1372 BOOST_CHECK(counterWoDevice->m_Description == "valid description");
1373 BOOST_CHECK(counterWoDevice->m_Units == "");
1374 BOOST_CHECK(counterWoDevice->m_DeviceUid == 0);
1375 BOOST_CHECK(counterWoDevice->m_CounterSetUid == 0);
1376 BOOST_CHECK(category->m_Counters.size() == 3);
1377 BOOST_CHECK(category->m_Counters.back() == counterWoDevice->m_Uid);
1378
1379 // Register a counter with a valid parent category name and associated to an invalid device
Keith Davise394bd92019-12-02 15:12:19 +00001380 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1381 15,
1382 categoryName,
1383 0,
1384 1,
1385 123.45f,
1386 "valid name 4",
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001387 "valid description",
Keith Davis3201eea2019-10-24 17:30:41 +01001388 armnn::EmptyOptional(), // Units
1389 armnn::EmptyOptional(), // Number of cores
1390 100), // Device UID
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001391 armnn::InvalidArgumentException);
1392 BOOST_CHECK(counterDirectory.GetCounterCount() == 3);
1393 BOOST_CHECK(!noCounter);
1394
1395 // Register a device for testing
1396 const std::string deviceName = "some_device";
Keith Davis3201eea2019-10-24 17:30:41 +01001397 const Device* device = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001398 BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName));
1399 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
1400 BOOST_CHECK(device);
1401 BOOST_CHECK(device->m_Name == deviceName);
1402 BOOST_CHECK(device->m_Uid >= 1);
1403 BOOST_CHECK(device->m_Cores == 0);
1404
1405 // Register a counter with a valid parent category name and associated to a device
1406 const Counter* counterWDevice = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001407 BOOST_CHECK_NO_THROW(counterWDevice = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1408 16,
1409 categoryName,
1410 0,
1411 1,
1412 123.45f,
1413 "valid name 5",
1414 std::string("valid description"),
Keith Davis3201eea2019-10-24 17:30:41 +01001415 armnn::EmptyOptional(), // Units
1416 armnn::EmptyOptional(), // Number of cores
1417 device->m_Uid)); // Device UID
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001418 BOOST_CHECK(counterDirectory.GetCounterCount() == 4);
1419 BOOST_CHECK(counterWDevice);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001420 BOOST_CHECK(counterWDevice->m_Uid > counter->m_Uid);
1421 BOOST_CHECK(counterWDevice->m_MaxCounterUid == counterWDevice->m_Uid);
1422 BOOST_CHECK(counterWDevice->m_Class == 0);
1423 BOOST_CHECK(counterWDevice->m_Interpolation == 1);
1424 BOOST_CHECK(counterWDevice->m_Multiplier == 123.45f);
1425 BOOST_CHECK(counterWDevice->m_Name == "valid name 5");
1426 BOOST_CHECK(counterWDevice->m_Description == "valid description");
1427 BOOST_CHECK(counterWDevice->m_Units == "");
1428 BOOST_CHECK(counterWDevice->m_DeviceUid == device->m_Uid);
1429 BOOST_CHECK(counterWDevice->m_CounterSetUid == 0);
1430 BOOST_CHECK(category->m_Counters.size() == 4);
1431 BOOST_CHECK(category->m_Counters.back() == counterWDevice->m_Uid);
1432
1433 // Register a counter with a valid parent category name and not associated with a counter set
1434 const Counter* counterWoCounterSet = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001435 BOOST_CHECK_NO_THROW(counterWoCounterSet = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1436 17,
1437 categoryName,
1438 0,
1439 1,
1440 123.45f,
1441 "valid name 6",
1442 "valid description",
1443 armnn::EmptyOptional(),// Units
1444 armnn::EmptyOptional(),// No of cores
1445 armnn::EmptyOptional(),// Device UID
1446 0)); // CounterSet UID
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001447 BOOST_CHECK(counterDirectory.GetCounterCount() == 5);
1448 BOOST_CHECK(counterWoCounterSet);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001449 BOOST_CHECK(counterWoCounterSet->m_Uid > counter->m_Uid);
1450 BOOST_CHECK(counterWoCounterSet->m_MaxCounterUid == counterWoCounterSet->m_Uid);
1451 BOOST_CHECK(counterWoCounterSet->m_Class == 0);
1452 BOOST_CHECK(counterWoCounterSet->m_Interpolation == 1);
1453 BOOST_CHECK(counterWoCounterSet->m_Multiplier == 123.45f);
1454 BOOST_CHECK(counterWoCounterSet->m_Name == "valid name 6");
1455 BOOST_CHECK(counterWoCounterSet->m_Description == "valid description");
1456 BOOST_CHECK(counterWoCounterSet->m_Units == "");
1457 BOOST_CHECK(counterWoCounterSet->m_DeviceUid == 0);
1458 BOOST_CHECK(counterWoCounterSet->m_CounterSetUid == 0);
1459 BOOST_CHECK(category->m_Counters.size() == 5);
1460 BOOST_CHECK(category->m_Counters.back() == counterWoCounterSet->m_Uid);
1461
1462 // Register a counter with a valid parent category name and associated to an invalid counter set
Keith Davise394bd92019-12-02 15:12:19 +00001463 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1464 18,
1465 categoryName,
1466 0,
1467 1,
1468 123.45f,
1469 "valid ",
1470 "name 7",
1471 std::string("valid description"),
Keith Davis3201eea2019-10-24 17:30:41 +01001472 armnn::EmptyOptional(), // Units
1473 armnn::EmptyOptional(), // Number of cores
Keith Davise394bd92019-12-02 15:12:19 +00001474 100), // Counter set UID
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001475 armnn::InvalidArgumentException);
1476 BOOST_CHECK(counterDirectory.GetCounterCount() == 5);
1477 BOOST_CHECK(!noCounter);
1478
1479 // Register a counter with a valid parent category name and with a given number of cores
1480 const Counter* counterWNumberOfCores = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001481 uint16_t numberOfCores = 15;
1482 BOOST_CHECK_NO_THROW(counterWNumberOfCores = counterDirectory.RegisterCounter(
Keith Davise394bd92019-12-02 15:12:19 +00001483 armnn::profiling::BACKEND_ID, 50,
Keith Davis3201eea2019-10-24 17:30:41 +01001484 categoryName, 0, 1, 123.45f, "valid name 8", "valid description",
1485 armnn::EmptyOptional(), // Units
1486 numberOfCores, // Number of cores
1487 armnn::EmptyOptional(), // Device UID
1488 armnn::EmptyOptional())); // Counter set UID
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001489 BOOST_CHECK(counterDirectory.GetCounterCount() == 20);
1490 BOOST_CHECK(counterWNumberOfCores);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001491 BOOST_CHECK(counterWNumberOfCores->m_Uid > counter->m_Uid);
1492 BOOST_CHECK(counterWNumberOfCores->m_MaxCounterUid == counterWNumberOfCores->m_Uid + numberOfCores - 1);
1493 BOOST_CHECK(counterWNumberOfCores->m_Class == 0);
1494 BOOST_CHECK(counterWNumberOfCores->m_Interpolation == 1);
1495 BOOST_CHECK(counterWNumberOfCores->m_Multiplier == 123.45f);
1496 BOOST_CHECK(counterWNumberOfCores->m_Name == "valid name 8");
1497 BOOST_CHECK(counterWNumberOfCores->m_Description == "valid description");
1498 BOOST_CHECK(counterWNumberOfCores->m_Units == "");
1499 BOOST_CHECK(counterWNumberOfCores->m_DeviceUid == 0);
1500 BOOST_CHECK(counterWNumberOfCores->m_CounterSetUid == 0);
1501 BOOST_CHECK(category->m_Counters.size() == 20);
Keith Davis3201eea2019-10-24 17:30:41 +01001502 for (size_t i = 0; i < numberOfCores; i++)
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001503 {
1504 BOOST_CHECK(category->m_Counters[category->m_Counters.size() - numberOfCores + i] ==
1505 counterWNumberOfCores->m_Uid + i);
1506 }
1507
1508 // Register a multi-core device for testing
1509 const std::string multiCoreDeviceName = "some_multi_core_device";
Keith Davis3201eea2019-10-24 17:30:41 +01001510 const Device* multiCoreDevice = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001511 BOOST_CHECK_NO_THROW(multiCoreDevice = counterDirectory.RegisterDevice(multiCoreDeviceName, 4));
1512 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
1513 BOOST_CHECK(multiCoreDevice);
1514 BOOST_CHECK(multiCoreDevice->m_Name == multiCoreDeviceName);
1515 BOOST_CHECK(multiCoreDevice->m_Uid >= 1);
1516 BOOST_CHECK(multiCoreDevice->m_Cores == 4);
1517
1518 // Register a counter with a valid parent category name and associated to the multi-core device
1519 const Counter* counterWMultiCoreDevice = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001520 BOOST_CHECK_NO_THROW(counterWMultiCoreDevice = counterDirectory.RegisterCounter(
Keith Davise394bd92019-12-02 15:12:19 +00001521 armnn::profiling::BACKEND_ID, 19, categoryName, 0, 1,
1522 123.45f, "valid name 9", "valid description",
Keith Davis3201eea2019-10-24 17:30:41 +01001523 armnn::EmptyOptional(), // Units
1524 armnn::EmptyOptional(), // Number of cores
1525 multiCoreDevice->m_Uid, // Device UID
1526 armnn::EmptyOptional())); // Counter set UID
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001527 BOOST_CHECK(counterDirectory.GetCounterCount() == 24);
1528 BOOST_CHECK(counterWMultiCoreDevice);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001529 BOOST_CHECK(counterWMultiCoreDevice->m_Uid > counter->m_Uid);
1530 BOOST_CHECK(counterWMultiCoreDevice->m_MaxCounterUid ==
1531 counterWMultiCoreDevice->m_Uid + multiCoreDevice->m_Cores - 1);
1532 BOOST_CHECK(counterWMultiCoreDevice->m_Class == 0);
1533 BOOST_CHECK(counterWMultiCoreDevice->m_Interpolation == 1);
1534 BOOST_CHECK(counterWMultiCoreDevice->m_Multiplier == 123.45f);
1535 BOOST_CHECK(counterWMultiCoreDevice->m_Name == "valid name 9");
1536 BOOST_CHECK(counterWMultiCoreDevice->m_Description == "valid description");
1537 BOOST_CHECK(counterWMultiCoreDevice->m_Units == "");
1538 BOOST_CHECK(counterWMultiCoreDevice->m_DeviceUid == multiCoreDevice->m_Uid);
1539 BOOST_CHECK(counterWMultiCoreDevice->m_CounterSetUid == 0);
1540 BOOST_CHECK(category->m_Counters.size() == 24);
Keith Davis3201eea2019-10-24 17:30:41 +01001541 for (size_t i = 0; i < 4; i++)
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001542 {
1543 BOOST_CHECK(category->m_Counters[category->m_Counters.size() - 4 + i] == counterWMultiCoreDevice->m_Uid + i);
1544 }
1545
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001546 // Register a multi-core device associate to a parent category for testing
1547 const std::string multiCoreDeviceNameWParentCategory = "some_multi_core_device_with_parent_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001548 const Device* multiCoreDeviceWParentCategory = nullptr;
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001549 BOOST_CHECK_NO_THROW(multiCoreDeviceWParentCategory =
Keith Davis3201eea2019-10-24 17:30:41 +01001550 counterDirectory.RegisterDevice(multiCoreDeviceNameWParentCategory, 2, categoryName));
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001551 BOOST_CHECK(counterDirectory.GetDeviceCount() == 3);
1552 BOOST_CHECK(multiCoreDeviceWParentCategory);
1553 BOOST_CHECK(multiCoreDeviceWParentCategory->m_Name == multiCoreDeviceNameWParentCategory);
1554 BOOST_CHECK(multiCoreDeviceWParentCategory->m_Uid >= 1);
1555 BOOST_CHECK(multiCoreDeviceWParentCategory->m_Cores == 2);
1556
1557 // Register a counter with a valid parent category name and getting the number of cores of the multi-core device
1558 // associated to that category
1559 const Counter* counterWMultiCoreDeviceWParentCategory = nullptr;
Sadik Armagan4c998992020-02-25 12:44:44 +00001560 uint16_t numberOfCourse = multiCoreDeviceWParentCategory->m_Cores;
Keith Davise394bd92019-12-02 15:12:19 +00001561 BOOST_CHECK_NO_THROW(counterWMultiCoreDeviceWParentCategory =
1562 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1563 100,
1564 categoryName,
1565 0,
1566 1,
1567 123.45f,
1568 "valid name 10",
1569 "valid description",
Sadik Armagan4c998992020-02-25 12:44:44 +00001570 armnn::EmptyOptional(), // Units
1571 numberOfCourse, // Number of cores
1572 armnn::EmptyOptional(), // Device UID
Keith Davise394bd92019-12-02 15:12:19 +00001573 armnn::EmptyOptional()));// Counter set UID
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001574 BOOST_CHECK(counterDirectory.GetCounterCount() == 26);
1575 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001576 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Uid > counter->m_Uid);
1577 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_MaxCounterUid ==
1578 counterWMultiCoreDeviceWParentCategory->m_Uid + multiCoreDeviceWParentCategory->m_Cores - 1);
1579 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Class == 0);
1580 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Interpolation == 1);
1581 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Multiplier == 123.45f);
1582 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Name == "valid name 10");
1583 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Description == "valid description");
1584 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Units == "");
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001585 BOOST_CHECK(category->m_Counters.size() == 26);
Keith Davis3201eea2019-10-24 17:30:41 +01001586 for (size_t i = 0; i < 2; i++)
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001587 {
1588 BOOST_CHECK(category->m_Counters[category->m_Counters.size() - 2 + i] ==
1589 counterWMultiCoreDeviceWParentCategory->m_Uid + i);
1590 }
1591
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001592 // Register a counter set for testing
1593 const std::string counterSetName = "some_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +01001594 const CounterSet* counterSet = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001595 BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
1596 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1597 BOOST_CHECK(counterSet);
1598 BOOST_CHECK(counterSet->m_Name == counterSetName);
1599 BOOST_CHECK(counterSet->m_Uid >= 1);
1600 BOOST_CHECK(counterSet->m_Count == 0);
1601
1602 // Register a counter with a valid parent category name and associated to a counter set
1603 const Counter* counterWCounterSet = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001604 BOOST_CHECK_NO_THROW(counterWCounterSet = counterDirectory.RegisterCounter(
Keith Davise394bd92019-12-02 15:12:19 +00001605 armnn::profiling::BACKEND_ID, 300,
Keith Davis3201eea2019-10-24 17:30:41 +01001606 categoryName, 0, 1, 123.45f, "valid name 11", "valid description",
1607 armnn::EmptyOptional(), // Units
1608 0, // Number of cores
1609 armnn::EmptyOptional(), // Device UID
1610 counterSet->m_Uid)); // Counter set UID
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001611 BOOST_CHECK(counterDirectory.GetCounterCount() == 27);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001612 BOOST_CHECK(counterWCounterSet);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001613 BOOST_CHECK(counterWCounterSet->m_Uid > counter->m_Uid);
1614 BOOST_CHECK(counterWCounterSet->m_MaxCounterUid == counterWCounterSet->m_Uid);
1615 BOOST_CHECK(counterWCounterSet->m_Class == 0);
1616 BOOST_CHECK(counterWCounterSet->m_Interpolation == 1);
1617 BOOST_CHECK(counterWCounterSet->m_Multiplier == 123.45f);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001618 BOOST_CHECK(counterWCounterSet->m_Name == "valid name 11");
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001619 BOOST_CHECK(counterWCounterSet->m_Description == "valid description");
1620 BOOST_CHECK(counterWCounterSet->m_Units == "");
1621 BOOST_CHECK(counterWCounterSet->m_DeviceUid == 0);
1622 BOOST_CHECK(counterWCounterSet->m_CounterSetUid == counterSet->m_Uid);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001623 BOOST_CHECK(category->m_Counters.size() == 27);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001624 BOOST_CHECK(category->m_Counters.back() == counterWCounterSet->m_Uid);
1625
1626 // Register a counter with a valid parent category name and associated to a device and a counter set
1627 const Counter* counterWDeviceWCounterSet = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001628 BOOST_CHECK_NO_THROW(counterWDeviceWCounterSet = counterDirectory.RegisterCounter(
Keith Davise394bd92019-12-02 15:12:19 +00001629 armnn::profiling::BACKEND_ID, 23,
Keith Davis3201eea2019-10-24 17:30:41 +01001630 categoryName, 0, 1, 123.45f, "valid name 12", "valid description",
1631 armnn::EmptyOptional(), // Units
1632 1, // Number of cores
1633 device->m_Uid, // Device UID
1634 counterSet->m_Uid)); // Counter set UID
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001635 BOOST_CHECK(counterDirectory.GetCounterCount() == 28);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001636 BOOST_CHECK(counterWDeviceWCounterSet);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001637 BOOST_CHECK(counterWDeviceWCounterSet->m_Uid > counter->m_Uid);
1638 BOOST_CHECK(counterWDeviceWCounterSet->m_MaxCounterUid == counterWDeviceWCounterSet->m_Uid);
1639 BOOST_CHECK(counterWDeviceWCounterSet->m_Class == 0);
1640 BOOST_CHECK(counterWDeviceWCounterSet->m_Interpolation == 1);
1641 BOOST_CHECK(counterWDeviceWCounterSet->m_Multiplier == 123.45f);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001642 BOOST_CHECK(counterWDeviceWCounterSet->m_Name == "valid name 12");
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001643 BOOST_CHECK(counterWDeviceWCounterSet->m_Description == "valid description");
1644 BOOST_CHECK(counterWDeviceWCounterSet->m_Units == "");
1645 BOOST_CHECK(counterWDeviceWCounterSet->m_DeviceUid == device->m_Uid);
1646 BOOST_CHECK(counterWDeviceWCounterSet->m_CounterSetUid == counterSet->m_Uid);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001647 BOOST_CHECK(category->m_Counters.size() == 28);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001648 BOOST_CHECK(category->m_Counters.back() == counterWDeviceWCounterSet->m_Uid);
1649
1650 // Register another category for testing
1651 const std::string anotherCategoryName = "some_other_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001652 const Category* anotherCategory = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001653 BOOST_CHECK_NO_THROW(anotherCategory = counterDirectory.RegisterCategory(anotherCategoryName));
1654 BOOST_CHECK(counterDirectory.GetCategoryCount() == 2);
1655 BOOST_CHECK(anotherCategory);
1656 BOOST_CHECK(anotherCategory != category);
1657 BOOST_CHECK(anotherCategory->m_Name == anotherCategoryName);
1658 BOOST_CHECK(anotherCategory->m_Counters.empty());
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001659
1660 // Register a counter to the other category
1661 const Counter* anotherCounter = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001662 BOOST_CHECK_NO_THROW(anotherCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID, 24,
1663 anotherCategoryName, 1, 0, .00043f,
Keith Davis3201eea2019-10-24 17:30:41 +01001664 "valid name", "valid description",
1665 armnn::EmptyOptional(), // Units
1666 armnn::EmptyOptional(), // Number of cores
1667 device->m_Uid, // Device UID
1668 counterSet->m_Uid)); // Counter set UID
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001669 BOOST_CHECK(counterDirectory.GetCounterCount() == 29);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001670 BOOST_CHECK(anotherCounter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001671 BOOST_CHECK(anotherCounter->m_MaxCounterUid == anotherCounter->m_Uid);
1672 BOOST_CHECK(anotherCounter->m_Class == 1);
1673 BOOST_CHECK(anotherCounter->m_Interpolation == 0);
1674 BOOST_CHECK(anotherCounter->m_Multiplier == .00043f);
1675 BOOST_CHECK(anotherCounter->m_Name == "valid name");
1676 BOOST_CHECK(anotherCounter->m_Description == "valid description");
1677 BOOST_CHECK(anotherCounter->m_Units == "");
1678 BOOST_CHECK(anotherCounter->m_DeviceUid == device->m_Uid);
1679 BOOST_CHECK(anotherCounter->m_CounterSetUid == counterSet->m_Uid);
1680 BOOST_CHECK(anotherCategory->m_Counters.size() == 1);
1681 BOOST_CHECK(anotherCategory->m_Counters.back() == anotherCounter->m_Uid);
Matteo Martincighab173e92019-09-05 12:02:04 +01001682}
1683
Ferran Balaguer1b941722019-08-28 16:57:18 +01001684BOOST_AUTO_TEST_CASE(CounterSelectionCommandHandlerParseData)
1685{
1686 using boost::numeric_cast;
1687
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01001688 ProfilingStateMachine profilingStateMachine;
1689
Ferran Balaguer1b941722019-08-28 16:57:18 +01001690 class TestCaptureThread : public IPeriodicCounterCapture
1691 {
Keith Davis3201eea2019-10-24 17:30:41 +01001692 void Start() override
1693 {}
1694 void Stop() override
1695 {}
Ferran Balaguer1b941722019-08-28 16:57:18 +01001696 };
1697
Matteo Martincighe8485382019-10-10 14:08:21 +01001698 class TestReadCounterValues : public IReadCounterValues
1699 {
Keith Davis3201eea2019-10-24 17:30:41 +01001700 bool IsCounterRegistered(uint16_t counterUid) const override
1701 {
Jan Eilers8eb25602020-03-09 12:13:48 +00001702 armnn::IgnoreUnused(counterUid);
Keith Davis3201eea2019-10-24 17:30:41 +01001703 return true;
1704 }
1705 uint16_t GetCounterCount() const override
1706 {
1707 return 0;
1708 }
1709 uint32_t GetCounterValue(uint16_t counterUid) const override
1710 {
Jan Eilers8eb25602020-03-09 12:13:48 +00001711 armnn::IgnoreUnused(counterUid);
Keith Davis3201eea2019-10-24 17:30:41 +01001712 return 0;
1713 }
Matteo Martincighe8485382019-10-10 14:08:21 +01001714 };
Jim Flynn397043f2019-10-17 17:37:10 +01001715 const uint32_t familyId = 0;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001716 const uint32_t packetId = 0x40000;
1717
1718 uint32_t version = 1;
Finn Williams032bc742020-02-12 11:02:34 +00001719 const std::unordered_map<armnn::BackendId,
1720 std::shared_ptr<armnn::profiling::IBackendProfilingContext>> backendProfilingContext;
1721 CounterIdMap counterIdMap;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001722 Holder holder;
1723 TestCaptureThread captureThread;
Matteo Martincighe8485382019-10-10 14:08:21 +01001724 TestReadCounterValues readCounterValues;
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001725 MockBufferManager mockBuffer(512);
Sadik Armagan3896b472020-02-10 12:24:15 +00001726 SendCounterPacket sendCounterPacket(mockBuffer);
1727 SendThread sendThread(profilingStateMachine, mockBuffer, sendCounterPacket);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001728
1729 uint32_t sizeOfUint32 = numeric_cast<uint32_t>(sizeof(uint32_t));
1730 uint32_t sizeOfUint16 = numeric_cast<uint32_t>(sizeof(uint16_t));
1731
1732 // Data with period and counters
Colm Donelan02705242019-11-14 14:19:07 +00001733 uint32_t period1 = armnn::LOWEST_CAPTURE_PERIOD;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001734 uint32_t dataLength1 = 8;
Keith Davis3201eea2019-10-24 17:30:41 +01001735 uint32_t offset = 0;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001736
Matteo Martincigh67ef2a52019-10-10 13:29:02 +01001737 std::unique_ptr<unsigned char[]> uniqueData1 = std::make_unique<unsigned char[]>(dataLength1);
Keith Davis3201eea2019-10-24 17:30:41 +01001738 unsigned char* data1 = reinterpret_cast<unsigned char*>(uniqueData1.get());
FinnWilliamsArma0c78712019-09-16 12:06:47 +01001739
Ferran Balaguer1b941722019-08-28 16:57:18 +01001740 WriteUint32(data1, offset, period1);
1741 offset += sizeOfUint32;
1742 WriteUint16(data1, offset, 4000);
1743 offset += sizeOfUint16;
1744 WriteUint16(data1, offset, 5000);
1745
FinnWilliamsArma0c78712019-09-16 12:06:47 +01001746 Packet packetA(packetId, dataLength1, uniqueData1);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001747
Finn Williams032bc742020-02-12 11:02:34 +00001748 PeriodicCounterSelectionCommandHandler commandHandler(familyId, packetId, version, backendProfilingContext,
1749 counterIdMap, holder, 10000u, captureThread,
Keith Davis3201eea2019-10-24 17:30:41 +01001750 readCounterValues, sendCounterPacket, profilingStateMachine);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001751
Matteo Martincighe8485382019-10-10 14:08:21 +01001752 profilingStateMachine.TransitionToState(ProfilingState::Uninitialised);
1753 BOOST_CHECK_THROW(commandHandler(packetA), armnn::RuntimeException);
1754 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
1755 BOOST_CHECK_THROW(commandHandler(packetA), armnn::RuntimeException);
1756 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
1757 BOOST_CHECK_THROW(commandHandler(packetA), armnn::RuntimeException);
1758 profilingStateMachine.TransitionToState(ProfilingState::Active);
1759 BOOST_CHECK_NO_THROW(commandHandler(packetA));
1760
1761 const std::vector<uint16_t> counterIdsA = holder.GetCaptureData().GetCounterIds();
Ferran Balaguer1b941722019-08-28 16:57:18 +01001762
1763 BOOST_TEST(holder.GetCaptureData().GetCapturePeriod() == period1);
Matteo Martincighe8485382019-10-10 14:08:21 +01001764 BOOST_TEST(counterIdsA.size() == 2);
1765 BOOST_TEST(counterIdsA[0] == 4000);
1766 BOOST_TEST(counterIdsA[1] == 5000);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001767
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001768 auto readBuffer = mockBuffer.GetReadableBuffer();
Ferran Balaguer1b941722019-08-28 16:57:18 +01001769
1770 offset = 0;
1771
1772 uint32_t headerWord0 = ReadUint32(readBuffer, offset);
1773 offset += sizeOfUint32;
1774 uint32_t headerWord1 = ReadUint32(readBuffer, offset);
1775 offset += sizeOfUint32;
1776 uint32_t period = ReadUint32(readBuffer, offset);
1777
Colm Donelan02705242019-11-14 14:19:07 +00001778 BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family
1779 BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 4); // packet id
1780 BOOST_TEST(headerWord1 == 8); // data length
1781 BOOST_TEST(period == armnn::LOWEST_CAPTURE_PERIOD); // capture period
Ferran Balaguer1b941722019-08-28 16:57:18 +01001782
1783 uint16_t counterId = 0;
1784 offset += sizeOfUint32;
1785 counterId = ReadUint16(readBuffer, offset);
1786 BOOST_TEST(counterId == 4000);
1787 offset += sizeOfUint16;
1788 counterId = ReadUint16(readBuffer, offset);
1789 BOOST_TEST(counterId == 5000);
1790
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001791 mockBuffer.MarkRead(readBuffer);
1792
Ferran Balaguer1b941722019-08-28 16:57:18 +01001793 // Data with period only
Colm Donelan02705242019-11-14 14:19:07 +00001794 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 +01001795 uint32_t dataLength2 = 4;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001796
Matteo Martincigh67ef2a52019-10-10 13:29:02 +01001797 std::unique_ptr<unsigned char[]> uniqueData2 = std::make_unique<unsigned char[]>(dataLength2);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001798
FinnWilliamsArma0c78712019-09-16 12:06:47 +01001799 WriteUint32(reinterpret_cast<unsigned char*>(uniqueData2.get()), 0, period2);
1800
1801 Packet packetB(packetId, dataLength2, uniqueData2);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001802
1803 commandHandler(packetB);
1804
Matteo Martincighe8485382019-10-10 14:08:21 +01001805 const std::vector<uint16_t> counterIdsB = holder.GetCaptureData().GetCounterIds();
Ferran Balaguer1b941722019-08-28 16:57:18 +01001806
Colm Donelan02705242019-11-14 14:19:07 +00001807 // Value should have been pulled up from 9000 to LOWEST_CAPTURE_PERIOD.
1808 BOOST_TEST(holder.GetCaptureData().GetCapturePeriod() == armnn::LOWEST_CAPTURE_PERIOD);
Matteo Martincighe8485382019-10-10 14:08:21 +01001809 BOOST_TEST(counterIdsB.size() == 0);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001810
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001811 readBuffer = mockBuffer.GetReadableBuffer();
Ferran Balaguer1b941722019-08-28 16:57:18 +01001812
1813 offset = 0;
1814
1815 headerWord0 = ReadUint32(readBuffer, offset);
1816 offset += sizeOfUint32;
1817 headerWord1 = ReadUint32(readBuffer, offset);
1818 offset += sizeOfUint32;
1819 period = ReadUint32(readBuffer, offset);
1820
Colm Donelan02705242019-11-14 14:19:07 +00001821 BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family
1822 BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 4); // packet id
1823 BOOST_TEST(headerWord1 == 4); // data length
1824 BOOST_TEST(period == armnn::LOWEST_CAPTURE_PERIOD); // capture period
Ferran Balaguer1b941722019-08-28 16:57:18 +01001825}
1826
Keith Davis33ed2212020-03-30 10:43:41 +01001827BOOST_AUTO_TEST_CASE(CheckTimelineActivationAndDeactivation)
1828{
1829 class TestReportStructure : public IReportStructure
1830 {
1831 public:
1832 virtual void ReportStructure() override
1833 {
1834 m_ReportStructureCalled = true;
1835 }
1836
1837 bool m_ReportStructureCalled = false;
1838 };
1839
1840 class TestNotifyBackends : public INotifyBackends
1841 {
1842 public:
1843 TestNotifyBackends() : m_timelineReporting(false) {}
1844 virtual void NotifyBackendsForTimelineReporting() override
1845 {
1846 m_TestNotifyBackendsCalled = m_timelineReporting.load();
1847 }
1848
1849 bool m_TestNotifyBackendsCalled = false;
1850 std::atomic<bool> m_timelineReporting;
1851 };
1852
1853 PacketVersionResolver packetVersionResolver;
1854
1855 BufferManager bufferManager(512);
1856 SendTimelinePacket sendTimelinePacket(bufferManager);
1857 ProfilingStateMachine stateMachine;
1858 TestReportStructure testReportStructure;
1859 TestNotifyBackends testNotifyBackends;
1860
1861 profiling::ActivateTimelineReportingCommandHandler activateTimelineReportingCommandHandler(0,
1862 6,
1863 packetVersionResolver.ResolvePacketVersion(0, 6)
1864 .GetEncodedValue(),
1865 sendTimelinePacket,
1866 stateMachine,
1867 testReportStructure,
1868 testNotifyBackends.m_timelineReporting,
1869 testNotifyBackends);
1870
1871 // Write an "ActivateTimelineReporting" packet into the mock profiling connection, to simulate an input from an
1872 // external profiling service
1873 const uint32_t packetFamily1 = 0;
1874 const uint32_t packetId1 = 6;
1875 uint32_t packetHeader1 = ConstructHeader(packetFamily1, packetId1);
1876
1877 // Create the ActivateTimelineReportingPacket
1878 Packet ActivateTimelineReportingPacket(packetHeader1); // Length == 0
1879
1880 BOOST_CHECK_THROW(
1881 activateTimelineReportingCommandHandler.operator()(ActivateTimelineReportingPacket), armnn::Exception);
1882
1883 stateMachine.TransitionToState(ProfilingState::NotConnected);
1884 BOOST_CHECK_THROW(
1885 activateTimelineReportingCommandHandler.operator()(ActivateTimelineReportingPacket), armnn::Exception);
1886
1887 stateMachine.TransitionToState(ProfilingState::WaitingForAck);
1888 BOOST_CHECK_THROW(
1889 activateTimelineReportingCommandHandler.operator()(ActivateTimelineReportingPacket), armnn::Exception);
1890
1891 stateMachine.TransitionToState(ProfilingState::Active);
1892 activateTimelineReportingCommandHandler.operator()(ActivateTimelineReportingPacket);
1893
1894 BOOST_CHECK(testReportStructure.m_ReportStructureCalled);
1895 BOOST_CHECK(testNotifyBackends.m_TestNotifyBackendsCalled);
1896 BOOST_CHECK(testNotifyBackends.m_timelineReporting.load());
1897
1898 DeactivateTimelineReportingCommandHandler deactivateTimelineReportingCommandHandler(0,
1899 7,
1900 packetVersionResolver.ResolvePacketVersion(0, 7).GetEncodedValue(),
1901 testNotifyBackends.m_timelineReporting,
1902 stateMachine,
1903 testNotifyBackends);
1904
1905 const uint32_t packetFamily2 = 0;
1906 const uint32_t packetId2 = 7;
1907 uint32_t packetHeader2 = ConstructHeader(packetFamily2, packetId2);
1908
1909 // Create the DeactivateTimelineReportingPacket
1910 Packet deactivateTimelineReportingPacket(packetHeader2); // Length == 0
1911
1912 stateMachine.Reset();
1913 BOOST_CHECK_THROW(
1914 deactivateTimelineReportingCommandHandler.operator()(deactivateTimelineReportingPacket), armnn::Exception);
1915
1916 stateMachine.TransitionToState(ProfilingState::NotConnected);
1917 BOOST_CHECK_THROW(
1918 deactivateTimelineReportingCommandHandler.operator()(deactivateTimelineReportingPacket), armnn::Exception);
1919
1920 stateMachine.TransitionToState(ProfilingState::WaitingForAck);
1921 BOOST_CHECK_THROW(
1922 deactivateTimelineReportingCommandHandler.operator()(deactivateTimelineReportingPacket), armnn::Exception);
1923
1924 stateMachine.TransitionToState(ProfilingState::Active);
1925 deactivateTimelineReportingCommandHandler.operator()(deactivateTimelineReportingPacket);
1926
1927 BOOST_CHECK(!testNotifyBackends.m_TestNotifyBackendsCalled);
1928 BOOST_CHECK(!testNotifyBackends.m_timelineReporting.load());
1929}
1930
1931BOOST_AUTO_TEST_CASE(CheckProfilingServiceNotActive)
1932{
1933 using namespace armnn;
1934 using namespace armnn::profiling;
1935
1936 // Create runtime in which the test will run
1937 armnn::IRuntime::CreationOptions options;
1938 options.m_ProfilingOptions.m_EnableProfiling = true;
1939
1940 armnn::Runtime runtime(options);
1941 profiling::ProfilingServiceRuntimeHelper profilingServiceHelper(GetProfilingService(&runtime));
1942 profilingServiceHelper.ForceTransitionToState(ProfilingState::NotConnected);
1943 profilingServiceHelper.ForceTransitionToState(ProfilingState::WaitingForAck);
1944 profilingServiceHelper.ForceTransitionToState(ProfilingState::Active);
1945
1946 profiling::BufferManager& bufferManager = profilingServiceHelper.GetProfilingBufferManager();
1947 auto readableBuffer = bufferManager.GetReadableBuffer();
1948
1949 // Profiling is enabled, the post-optimisation structure should be created
1950 BOOST_CHECK(readableBuffer == nullptr);
1951}
1952
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001953BOOST_AUTO_TEST_CASE(CheckConnectionAcknowledged)
1954{
1955 using boost::numeric_cast;
1956
Keith Davis3201eea2019-10-24 17:30:41 +01001957 const uint32_t packetFamilyId = 0;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001958 const uint32_t connectionPacketId = 0x10000;
Keith Davis3201eea2019-10-24 17:30:41 +01001959 const uint32_t version = 1;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001960
1961 uint32_t sizeOfUint32 = numeric_cast<uint32_t>(sizeof(uint32_t));
1962 uint32_t sizeOfUint16 = numeric_cast<uint32_t>(sizeof(uint16_t));
1963
1964 // Data with period and counters
Keith Davis3201eea2019-10-24 17:30:41 +01001965 uint32_t period1 = 10;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001966 uint32_t dataLength1 = 8;
Keith Davis3201eea2019-10-24 17:30:41 +01001967 uint32_t offset = 0;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001968
Matteo Martincigh67ef2a52019-10-10 13:29:02 +01001969 std::unique_ptr<unsigned char[]> uniqueData1 = std::make_unique<unsigned char[]>(dataLength1);
Keith Davis3201eea2019-10-24 17:30:41 +01001970 unsigned char* data1 = reinterpret_cast<unsigned char*>(uniqueData1.get());
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001971
1972 WriteUint32(data1, offset, period1);
1973 offset += sizeOfUint32;
1974 WriteUint16(data1, offset, 4000);
1975 offset += sizeOfUint16;
1976 WriteUint16(data1, offset, 5000);
1977
1978 Packet packetA(connectionPacketId, dataLength1, uniqueData1);
1979
1980 ProfilingStateMachine profilingState(ProfilingState::Uninitialised);
1981 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01001982 CounterDirectory counterDirectory;
1983 MockBufferManager mockBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00001984 SendCounterPacket sendCounterPacket(mockBuffer);
1985 SendThread sendThread(profilingState, mockBuffer, sendCounterPacket);
Matteo Martincighcdfb9412019-11-08 11:23:06 +00001986 SendTimelinePacket sendTimelinePacket(mockBuffer);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001987
Keith Davis3201eea2019-10-24 17:30:41 +01001988 ConnectionAcknowledgedCommandHandler commandHandler(packetFamilyId, connectionPacketId, version, counterDirectory,
Matteo Martincighcdfb9412019-11-08 11:23:06 +00001989 sendCounterPacket, sendTimelinePacket, profilingState);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001990
1991 // command handler received packet on ProfilingState::Uninitialised
1992 BOOST_CHECK_THROW(commandHandler(packetA), armnn::Exception);
1993
1994 profilingState.TransitionToState(ProfilingState::NotConnected);
1995 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::NotConnected);
1996 // command handler received packet on ProfilingState::NotConnected
1997 BOOST_CHECK_THROW(commandHandler(packetA), armnn::Exception);
1998
1999 profilingState.TransitionToState(ProfilingState::WaitingForAck);
2000 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::WaitingForAck);
2001 // command handler received packet on ProfilingState::WaitingForAck
Matteo Martincighd0613b52019-10-09 16:47:04 +01002002 BOOST_CHECK_NO_THROW(commandHandler(packetA));
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002003 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::Active);
2004
2005 // command handler received packet on ProfilingState::Active
Matteo Martincighd0613b52019-10-09 16:47:04 +01002006 BOOST_CHECK_NO_THROW(commandHandler(packetA));
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002007 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::Active);
2008
2009 // command handler received different packet
2010 const uint32_t differentPacketId = 0x40000;
2011 Packet packetB(differentPacketId, dataLength1, uniqueData1);
Matteo Martincighd0613b52019-10-09 16:47:04 +01002012 profilingState.TransitionToState(ProfilingState::NotConnected);
2013 profilingState.TransitionToState(ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002014 ConnectionAcknowledgedCommandHandler differentCommandHandler(packetFamilyId, differentPacketId, version,
Matteo Martincighcdfb9412019-11-08 11:23:06 +00002015 counterDirectory, sendCounterPacket,
2016 sendTimelinePacket, profilingState);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002017 BOOST_CHECK_THROW(differentCommandHandler(packetB), armnn::Exception);
2018}
2019
Sadik Armagana97a0be2020-03-03 10:44:56 +00002020BOOST_AUTO_TEST_CASE(CheckSocketConnectionException)
Teresa Charlin9bab4962019-09-06 12:28:35 +01002021{
Sadik Armagana97a0be2020-03-03 10:44:56 +00002022 // Check that creating a SocketProfilingConnection armnnProfiling in an exception as the Gator UDS doesn't exist.
2023 BOOST_CHECK_THROW(new SocketProfilingConnection(), armnnProfiling::SocketConnectionException);
2024}
2025
2026BOOST_AUTO_TEST_CASE(CheckSocketConnectionException2)
2027{
2028 try
2029 {
2030 new SocketProfilingConnection();
2031 }
2032 catch (armnnProfiling::SocketConnectionException& ex)
2033 {
2034 BOOST_CHECK(ex.GetSocketFd() == 0);
2035 BOOST_CHECK(ex.GetErrorNo() == 111);
2036 BOOST_CHECK(ex.what()
2037 == std::string("SocketProfilingConnection: Cannot connect to stream socket: Connection refused"));
2038 }
Teresa Charlin9bab4962019-09-06 12:28:35 +01002039}
2040
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002041BOOST_AUTO_TEST_CASE(SwTraceIsValidCharTest)
2042{
2043 // Only ASCII 7-bit encoding supported
2044 for (unsigned char c = 0; c < 128; c++)
2045 {
2046 BOOST_CHECK(SwTraceCharPolicy::IsValidChar(c));
2047 }
2048
2049 // Not ASCII
2050 for (unsigned char c = 255; c >= 128; c++)
2051 {
2052 BOOST_CHECK(!SwTraceCharPolicy::IsValidChar(c));
2053 }
2054}
2055
2056BOOST_AUTO_TEST_CASE(SwTraceIsValidNameCharTest)
2057{
2058 // Only alpha-numeric and underscore ASCII 7-bit encoding supported
2059 const unsigned char validChars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
2060 for (unsigned char i = 0; i < sizeof(validChars) / sizeof(validChars[0]) - 1; i++)
2061 {
2062 BOOST_CHECK(SwTraceNameCharPolicy::IsValidChar(validChars[i]));
2063 }
2064
2065 // Non alpha-numeric chars
2066 for (unsigned char c = 0; c < 48; c++)
2067 {
2068 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
2069 }
2070 for (unsigned char c = 58; c < 65; c++)
2071 {
2072 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
2073 }
2074 for (unsigned char c = 91; c < 95; c++)
2075 {
2076 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
2077 }
2078 for (unsigned char c = 96; c < 97; c++)
2079 {
2080 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
2081 }
2082 for (unsigned char c = 123; c < 128; c++)
2083 {
2084 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
2085 }
2086
2087 // Not ASCII
2088 for (unsigned char c = 255; c >= 128; c++)
2089 {
2090 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
2091 }
2092}
2093
2094BOOST_AUTO_TEST_CASE(IsValidSwTraceStringTest)
2095{
2096 // Valid SWTrace strings
2097 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>(""));
2098 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("_"));
2099 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("0123"));
2100 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("valid_string"));
2101 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("VALID_string_456"));
2102 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>(" "));
2103 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("valid string"));
2104 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("!$%"));
2105 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("valid|\\~string#123"));
2106
2107 // Invalid SWTrace strings
2108 BOOST_CHECK(!IsValidSwTraceString<SwTraceCharPolicy>("€£"));
2109 BOOST_CHECK(!IsValidSwTraceString<SwTraceCharPolicy>("invalid‡string"));
2110 BOOST_CHECK(!IsValidSwTraceString<SwTraceCharPolicy>("12Ž34"));
2111}
2112
2113BOOST_AUTO_TEST_CASE(IsValidSwTraceNameStringTest)
2114{
2115 // Valid SWTrace name strings
2116 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>(""));
2117 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>("_"));
2118 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>("0123"));
2119 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>("valid_string"));
2120 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>("VALID_string_456"));
2121
2122 // Invalid SWTrace name strings
2123 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>(" "));
2124 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("invalid string"));
2125 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("!$%"));
2126 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("invalid|\\~string#123"));
2127 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("€£"));
2128 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("invalid‡string"));
2129 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("12Ž34"));
2130}
2131
2132template <typename SwTracePolicy>
2133void StringToSwTraceStringTestHelper(const std::string& testString, std::vector<uint32_t> buffer, size_t expectedSize)
2134{
2135 // Convert the test string to a SWTrace string
2136 BOOST_CHECK(StringToSwTraceString<SwTracePolicy>(testString, buffer));
2137
2138 // The buffer must contain at least the length of the string
2139 BOOST_CHECK(!buffer.empty());
2140
2141 // The buffer must be of the expected size (in words)
2142 BOOST_CHECK(buffer.size() == expectedSize);
2143
2144 // The first word of the byte must be the length of the string including the null-terminator
2145 BOOST_CHECK(buffer[0] == testString.size() + 1);
2146
2147 // The contents of the buffer must match the test string
2148 BOOST_CHECK(std::memcmp(testString.data(), buffer.data() + 1, testString.size()) == 0);
2149
2150 // The buffer must include the null-terminator at the end of the string
2151 size_t nullTerminatorIndex = sizeof(uint32_t) + testString.size();
Keith Davis3201eea2019-10-24 17:30:41 +01002152 BOOST_CHECK(reinterpret_cast<unsigned char*>(buffer.data())[nullTerminatorIndex] == '\0');
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002153}
2154
2155BOOST_AUTO_TEST_CASE(StringToSwTraceStringTest)
2156{
2157 std::vector<uint32_t> buffer;
2158
2159 // Valid SWTrace strings (expected size in words)
2160 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("", buffer, 2);
2161 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("_", buffer, 2);
2162 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("0123", buffer, 3);
2163 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("valid_string", buffer, 5);
2164 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("VALID_string_456", buffer, 6);
2165 StringToSwTraceStringTestHelper<SwTraceCharPolicy>(" ", buffer, 2);
2166 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("valid string", buffer, 5);
2167 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("!$%", buffer, 2);
2168 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("valid|\\~string#123", buffer, 6);
2169
2170 // Invalid SWTrace strings
2171 BOOST_CHECK(!StringToSwTraceString<SwTraceCharPolicy>("€£", buffer));
2172 BOOST_CHECK(buffer.empty());
2173 BOOST_CHECK(!StringToSwTraceString<SwTraceCharPolicy>("invalid‡string", buffer));
2174 BOOST_CHECK(buffer.empty());
2175 BOOST_CHECK(!StringToSwTraceString<SwTraceCharPolicy>("12Ž34", buffer));
2176 BOOST_CHECK(buffer.empty());
2177}
2178
2179BOOST_AUTO_TEST_CASE(StringToSwTraceNameStringTest)
2180{
2181 std::vector<uint32_t> buffer;
2182
2183 // Valid SWTrace namestrings (expected size in words)
2184 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("", buffer, 2);
2185 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("_", buffer, 2);
2186 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("0123", buffer, 3);
2187 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("valid_string", buffer, 5);
2188 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("VALID_string_456", buffer, 6);
2189
2190 // Invalid SWTrace namestrings
2191 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>(" ", buffer));
2192 BOOST_CHECK(buffer.empty());
2193 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("invalid string", buffer));
2194 BOOST_CHECK(buffer.empty());
2195 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("!$%", buffer));
2196 BOOST_CHECK(buffer.empty());
2197 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("invalid|\\~string#123", buffer));
2198 BOOST_CHECK(buffer.empty());
2199 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("€£", buffer));
2200 BOOST_CHECK(buffer.empty());
2201 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("invalid‡string", buffer));
2202 BOOST_CHECK(buffer.empty());
2203 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("12Ž34", buffer));
2204 BOOST_CHECK(buffer.empty());
2205}
2206
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002207BOOST_AUTO_TEST_CASE(CheckPeriodicCounterCaptureThread)
2208{
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002209 class CaptureReader : public IReadCounterValues
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002210 {
2211 public:
Finn Williamsf4d59a62019-10-14 15:55:18 +01002212 CaptureReader(uint16_t counterSize)
2213 {
Keith Davis3201eea2019-10-24 17:30:41 +01002214 for (uint16_t i = 0; i < counterSize; ++i)
Finn Williamsf4d59a62019-10-14 15:55:18 +01002215 {
2216 m_Data[i] = 0;
2217 }
2218 m_CounterSize = counterSize;
2219 }
2220 //not used
Matteo Martincighe8485382019-10-10 14:08:21 +01002221 bool IsCounterRegistered(uint16_t counterUid) const override
2222 {
Jan Eilers8eb25602020-03-09 12:13:48 +00002223 armnn::IgnoreUnused(counterUid);
Finn Williamsf4d59a62019-10-14 15:55:18 +01002224 return false;
Matteo Martincighe8485382019-10-10 14:08:21 +01002225 }
2226
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002227 uint16_t GetCounterCount() const override
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002228 {
Finn Williamsf4d59a62019-10-14 15:55:18 +01002229 return m_CounterSize;
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002230 }
2231
Matteo Martincighe8485382019-10-10 14:08:21 +01002232 uint32_t GetCounterValue(uint16_t counterUid) const override
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002233 {
Keith Davis3201eea2019-10-24 17:30:41 +01002234 if (counterUid > m_CounterSize)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002235 {
Finn Williamsf4d59a62019-10-14 15:55:18 +01002236 BOOST_FAIL("Invalid counter Uid");
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002237 }
Matteo Martincighe8485382019-10-10 14:08:21 +01002238 return m_Data.at(counterUid).load();
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002239 }
2240
Matteo Martincighe8485382019-10-10 14:08:21 +01002241 void SetCounterValue(uint16_t counterUid, uint32_t value)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002242 {
Keith Davis3201eea2019-10-24 17:30:41 +01002243 if (counterUid > m_CounterSize)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002244 {
Finn Williamsf4d59a62019-10-14 15:55:18 +01002245 BOOST_FAIL("Invalid counter Uid");
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002246 }
Finn Williamsf4d59a62019-10-14 15:55:18 +01002247 m_Data.at(counterUid).store(value);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002248 }
2249
2250 private:
Matteo Martincighe8485382019-10-10 14:08:21 +01002251 std::unordered_map<uint16_t, std::atomic<uint32_t>> m_Data;
Finn Williamsf4d59a62019-10-14 15:55:18 +01002252 uint16_t m_CounterSize;
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002253 };
2254
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002255 ProfilingStateMachine profilingStateMachine;
2256
Finn Williams032bc742020-02-12 11:02:34 +00002257 const std::unordered_map<armnn::BackendId,
2258 std::shared_ptr<armnn::profiling::IBackendProfilingContext>> backendProfilingContext;
2259 CounterIdMap counterIdMap;
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002260 Holder data;
2261 std::vector<uint16_t> captureIds1 = { 0, 1 };
2262 std::vector<uint16_t> captureIds2;
2263
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002264 MockBufferManager mockBuffer(512);
Sadik Armagan3896b472020-02-10 12:24:15 +00002265 SendCounterPacket sendCounterPacket(mockBuffer);
2266 SendThread sendThread(profilingStateMachine, mockBuffer, sendCounterPacket);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002267
2268 std::vector<uint16_t> counterIds;
Finn Williamsf4d59a62019-10-14 15:55:18 +01002269 CaptureReader captureReader(2);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002270
Keith Davis3201eea2019-10-24 17:30:41 +01002271 unsigned int valueA = 10;
2272 unsigned int valueB = 15;
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002273 unsigned int numSteps = 5;
2274
Finn Williams032bc742020-02-12 11:02:34 +00002275 PeriodicCounterCapture periodicCounterCapture(std::ref(data), std::ref(sendCounterPacket), captureReader,
2276 counterIdMap, backendProfilingContext);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002277
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002278 for (unsigned int i = 0; i < numSteps; ++i)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002279 {
Finn Williams032bc742020-02-12 11:02:34 +00002280 data.SetCaptureData(1, captureIds1, {});
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002281 captureReader.SetCounterValue(0, valueA * (i + 1));
2282 captureReader.SetCounterValue(1, valueB * (i + 1));
2283
2284 periodicCounterCapture.Start();
Finn Williamsf4d59a62019-10-14 15:55:18 +01002285 periodicCounterCapture.Stop();
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002286 }
2287
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002288 auto buffer = mockBuffer.GetReadableBuffer();
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002289
2290 uint32_t headerWord0 = ReadUint32(buffer, 0);
2291 uint32_t headerWord1 = ReadUint32(buffer, 4);
2292
Jim Flynnfc365622019-12-04 10:07:20 +00002293 BOOST_TEST(((headerWord0 >> 26) & 0x0000003F) == 3); // packet family
Keith Davis3201eea2019-10-24 17:30:41 +01002294 BOOST_TEST(((headerWord0 >> 19) & 0x0000007F) == 0); // packet class
2295 BOOST_TEST(((headerWord0 >> 16) & 0x00000007) == 0); // packet type
Matteo Martincigh8d9590e2019-10-15 09:35:29 +01002296 BOOST_TEST(headerWord1 == 20);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002297
Keith Davis3201eea2019-10-24 17:30:41 +01002298 uint32_t offset = 16;
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002299 uint16_t readIndex = ReadUint16(buffer, offset);
2300 BOOST_TEST(0 == readIndex);
2301
2302 offset += 2;
2303 uint32_t readValue = ReadUint32(buffer, offset);
2304 BOOST_TEST((valueA * numSteps) == readValue);
2305
2306 offset += 4;
2307 readIndex = ReadUint16(buffer, offset);
2308 BOOST_TEST(1 == readIndex);
2309
2310 offset += 2;
2311 readValue = ReadUint32(buffer, offset);
2312 BOOST_TEST((valueB * numSteps) == readValue);
2313}
2314
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002315BOOST_AUTO_TEST_CASE(RequestCounterDirectoryCommandHandlerTest1)
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002316{
2317 using boost::numeric_cast;
2318
Jim Flynn397043f2019-10-17 17:37:10 +01002319 const uint32_t familyId = 0;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002320 const uint32_t packetId = 3;
Keith Davis3201eea2019-10-24 17:30:41 +01002321 const uint32_t version = 1;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002322 ProfilingStateMachine profilingStateMachine;
2323 CounterDirectory counterDirectory;
Matteo Martincigh9723d022019-11-13 10:56:41 +00002324 MockBufferManager mockBuffer1(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002325 SendCounterPacket sendCounterPacket(mockBuffer1);
2326 SendThread sendThread(profilingStateMachine, mockBuffer1, sendCounterPacket);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002327 MockBufferManager mockBuffer2(1024);
2328 SendTimelinePacket sendTimelinePacket(mockBuffer2);
Keith Davis3201eea2019-10-24 17:30:41 +01002329 RequestCounterDirectoryCommandHandler commandHandler(familyId, packetId, version, counterDirectory,
Matteo Martincigh9723d022019-11-13 10:56:41 +00002330 sendCounterPacket, sendTimelinePacket, profilingStateMachine);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002331
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002332 const uint32_t wrongPacketId = 47;
Keith Davis3201eea2019-10-24 17:30:41 +01002333 const uint32_t wrongHeader = (wrongPacketId & 0x000003FF) << 16;
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002334
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002335 Packet wrongPacket(wrongHeader);
2336
2337 profilingStateMachine.TransitionToState(ProfilingState::Uninitialised);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002338 BOOST_CHECK_THROW(commandHandler(wrongPacket), armnn::RuntimeException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002339 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002340 BOOST_CHECK_THROW(commandHandler(wrongPacket), armnn::RuntimeException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002341 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002342 BOOST_CHECK_THROW(commandHandler(wrongPacket), armnn::RuntimeException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002343 profilingStateMachine.TransitionToState(ProfilingState::Active);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002344 BOOST_CHECK_THROW(commandHandler(wrongPacket), armnn::InvalidArgumentException); // Wrong packet
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002345
2346 const uint32_t rightHeader = (packetId & 0x000003FF) << 16;
2347
2348 Packet rightPacket(rightHeader);
2349
Matteo Martincigh9723d022019-11-13 10:56:41 +00002350 BOOST_CHECK_NO_THROW(commandHandler(rightPacket)); // Right packet
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002351
Matteo Martincigh9723d022019-11-13 10:56:41 +00002352 auto readBuffer1 = mockBuffer1.GetReadableBuffer();
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002353
Matteo Martincigh9723d022019-11-13 10:56:41 +00002354 uint32_t header1Word0 = ReadUint32(readBuffer1, 0);
2355 uint32_t header1Word1 = ReadUint32(readBuffer1, 4);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002356
Matteo Martincigh9723d022019-11-13 10:56:41 +00002357 // Counter directory packet
2358 BOOST_TEST(((header1Word0 >> 26) & 0x0000003F) == 0); // packet family
2359 BOOST_TEST(((header1Word0 >> 16) & 0x000003FF) == 2); // packet id
2360 BOOST_TEST(header1Word1 == 24); // data length
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002361
Matteo Martincigh9723d022019-11-13 10:56:41 +00002362 uint32_t bodyHeader1Word0 = ReadUint32(readBuffer1, 8);
2363 uint16_t deviceRecordCount = numeric_cast<uint16_t>(bodyHeader1Word0 >> 16);
2364 BOOST_TEST(deviceRecordCount == 0); // device_records_count
2365
2366 auto readBuffer2 = mockBuffer2.GetReadableBuffer();
2367
2368 uint32_t header2Word0 = ReadUint32(readBuffer2, 0);
2369 uint32_t header2Word1 = ReadUint32(readBuffer2, 4);
2370
2371 // Timeline message directory packet
2372 BOOST_TEST(((header2Word0 >> 26) & 0x0000003F) == 1); // packet family
2373 BOOST_TEST(((header2Word0 >> 16) & 0x000003FF) == 0); // packet id
2374 BOOST_TEST(header2Word1 == 419); // data length
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002375}
2376
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002377BOOST_AUTO_TEST_CASE(RequestCounterDirectoryCommandHandlerTest2)
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002378{
2379 using boost::numeric_cast;
2380
Jim Flynn397043f2019-10-17 17:37:10 +01002381 const uint32_t familyId = 0;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002382 const uint32_t packetId = 3;
Keith Davis3201eea2019-10-24 17:30:41 +01002383 const uint32_t version = 1;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002384 ProfilingStateMachine profilingStateMachine;
2385 CounterDirectory counterDirectory;
Matteo Martincigh9723d022019-11-13 10:56:41 +00002386 MockBufferManager mockBuffer1(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002387 SendCounterPacket sendCounterPacket(mockBuffer1);
2388 SendThread sendThread(profilingStateMachine, mockBuffer1, sendCounterPacket);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002389 MockBufferManager mockBuffer2(1024);
2390 SendTimelinePacket sendTimelinePacket(mockBuffer2);
Keith Davis3201eea2019-10-24 17:30:41 +01002391 RequestCounterDirectoryCommandHandler commandHandler(familyId, packetId, version, counterDirectory,
Matteo Martincigh9723d022019-11-13 10:56:41 +00002392 sendCounterPacket, sendTimelinePacket, profilingStateMachine);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002393 const uint32_t header = (packetId & 0x000003FF) << 16;
2394 Packet packet(header);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002395
Matteo Martincigh9723d022019-11-13 10:56:41 +00002396 const Device* device = counterDirectory.RegisterDevice("deviceA", 1);
2397 BOOST_CHECK(device != nullptr);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002398 const CounterSet* counterSet = counterDirectory.RegisterCounterSet("countersetA");
Matteo Martincigh9723d022019-11-13 10:56:41 +00002399 BOOST_CHECK(counterSet != nullptr);
Sadik Armagan4c998992020-02-25 12:44:44 +00002400 counterDirectory.RegisterCategory("categoryA");
Keith Davise394bd92019-12-02 15:12:19 +00002401 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID, 24,
2402 "categoryA", 0, 1, 2.0f, "counterA", "descA");
2403 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID, 25,
2404 "categoryA", 1, 1, 3.0f, "counterB", "descB");
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002405
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002406 profilingStateMachine.TransitionToState(ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002407 BOOST_CHECK_THROW(commandHandler(packet), armnn::RuntimeException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002408 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002409 BOOST_CHECK_THROW(commandHandler(packet), armnn::RuntimeException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002410 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002411 BOOST_CHECK_THROW(commandHandler(packet), armnn::RuntimeException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002412 profilingStateMachine.TransitionToState(ProfilingState::Active);
2413 BOOST_CHECK_NO_THROW(commandHandler(packet));
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002414
Matteo Martincigh9723d022019-11-13 10:56:41 +00002415 auto readBuffer1 = mockBuffer1.GetReadableBuffer();
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002416
Matteo Martincigh9723d022019-11-13 10:56:41 +00002417 uint32_t header1Word0 = ReadUint32(readBuffer1, 0);
2418 uint32_t header1Word1 = ReadUint32(readBuffer1, 4);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002419
Matteo Martincigh9723d022019-11-13 10:56:41 +00002420 BOOST_TEST(((header1Word0 >> 26) & 0x0000003F) == 0); // packet family
2421 BOOST_TEST(((header1Word0 >> 16) & 0x000003FF) == 2); // packet id
Sadik Armagan4c998992020-02-25 12:44:44 +00002422 BOOST_TEST(header1Word1 == 236); // data length
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002423
Matteo Martincigh9723d022019-11-13 10:56:41 +00002424 uint32_t bodyHeader1Word0 = ReadUint32(readBuffer1, 8);
2425 uint32_t bodyHeader1Word1 = ReadUint32(readBuffer1, 12);
2426 uint32_t bodyHeader1Word2 = ReadUint32(readBuffer1, 16);
2427 uint32_t bodyHeader1Word3 = ReadUint32(readBuffer1, 20);
2428 uint32_t bodyHeader1Word4 = ReadUint32(readBuffer1, 24);
2429 uint32_t bodyHeader1Word5 = ReadUint32(readBuffer1, 28);
2430 uint16_t deviceRecordCount = numeric_cast<uint16_t>(bodyHeader1Word0 >> 16);
2431 uint16_t counterSetRecordCount = numeric_cast<uint16_t>(bodyHeader1Word2 >> 16);
2432 uint16_t categoryRecordCount = numeric_cast<uint16_t>(bodyHeader1Word4 >> 16);
2433 BOOST_TEST(deviceRecordCount == 1); // device_records_count
2434 BOOST_TEST(bodyHeader1Word1 == 0); // device_records_pointer_table_offset
2435 BOOST_TEST(counterSetRecordCount == 1); // counter_set_count
2436 BOOST_TEST(bodyHeader1Word3 == 4); // counter_set_pointer_table_offset
2437 BOOST_TEST(categoryRecordCount == 1); // categories_count
2438 BOOST_TEST(bodyHeader1Word5 == 8); // categories_pointer_table_offset
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002439
Matteo Martincigh9723d022019-11-13 10:56:41 +00002440 uint32_t deviceRecordOffset = ReadUint32(readBuffer1, 32);
Keith Davis3201eea2019-10-24 17:30:41 +01002441 BOOST_TEST(deviceRecordOffset == 0);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002442
Matteo Martincigh9723d022019-11-13 10:56:41 +00002443 uint32_t counterSetRecordOffset = ReadUint32(readBuffer1, 36);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002444 BOOST_TEST(counterSetRecordOffset == 20);
2445
Matteo Martincigh9723d022019-11-13 10:56:41 +00002446 uint32_t categoryRecordOffset = ReadUint32(readBuffer1, 40);
Keith Davis3201eea2019-10-24 17:30:41 +01002447 BOOST_TEST(categoryRecordOffset == 44);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002448
2449 auto readBuffer2 = mockBuffer2.GetReadableBuffer();
2450
2451 uint32_t header2Word0 = ReadUint32(readBuffer2, 0);
2452 uint32_t header2Word1 = ReadUint32(readBuffer2, 4);
2453
2454 // Timeline message directory packet
2455 BOOST_TEST(((header2Word0 >> 26) & 0x0000003F) == 1); // packet family
2456 BOOST_TEST(((header2Word0 >> 16) & 0x000003FF) == 0); // packet id
2457 BOOST_TEST(header2Word1 == 419); // data length
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002458}
2459
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002460BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodConnectionAcknowledgedPacket)
2461{
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002462 // Calculate the size of a Stream Metadata packet
Keith Davis3201eea2019-10-24 17:30:41 +01002463 std::string processName = GetProcessName().substr(0, 60);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002464 unsigned int processNameSize = processName.empty() ? 0 : boost::numeric_cast<unsigned int>(processName.size()) + 1;
2465 unsigned int streamMetadataPacketsize = 118 + processNameSize;
2466
Jim Flynn53e46992019-10-14 12:31:10 +01002467 // Reset the profiling service to the uninitialized state
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002468 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002469 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +00002470 armnn::profiling::ProfilingService profilingService;
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002471 profilingService.ResetExternalProfilingOptions(options, true);
2472
Sadik Armagan3184c902020-03-18 10:57:30 +00002473 // Swap the profiling connection factory in the profiling service instance with our mock one
2474 SwapProfilingConnectionFactoryHelper helper(profilingService);
2475
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002476 // Bring the profiling service to the "WaitingForAck" state
2477 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002478 profilingService.Update(); // Initialize the counter directory
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002479 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002480 profilingService.Update(); // Create the profiling connection
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002481
Matteo Martincighd0613b52019-10-09 16:47:04 +01002482 // Get the mock profiling connection
2483 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2484 BOOST_CHECK(mockProfilingConnection);
2485
Matteo Martincighe8485382019-10-10 14:08:21 +01002486 // Remove the packets received so far
2487 mockProfilingConnection->Clear();
2488
2489 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002490 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002491
2492 // Wait for the Stream Metadata packet to be sent
Finn Williams09ad6f92019-12-19 17:05:18 +00002493 BOOST_CHECK(helper.WaitForPacketsSent(
2494 mockProfilingConnection, PacketType::StreamMetaData, streamMetadataPacketsize) >= 1);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002495
2496 // Write a valid "Connection Acknowledged" packet into the mock profiling connection, to simulate a valid
2497 // reply from an external profiling service
2498
2499 // Connection Acknowledged Packet header (word 0, word 1 is always zero):
2500 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2501 // 16:25 [10] packet_id: Packet identifier, value 0b0000000001
2502 // 8:15 [8] reserved: Reserved, value 0b00000000
2503 // 0:7 [8] reserved: Reserved, value 0b00000000
2504 uint32_t packetFamily = 0;
2505 uint32_t packetId = 1;
Keith Davis3201eea2019-10-24 17:30:41 +01002506 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002507
Matteo Martincighd0613b52019-10-09 16:47:04 +01002508 // Create the Connection Acknowledged Packet
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002509 Packet connectionAcknowledgedPacket(header);
2510
2511 // Write the packet to the mock profiling connection
2512 mockProfilingConnection->WritePacket(std::move(connectionAcknowledgedPacket));
2513
Colm Donelan2ba48d22019-11-29 09:10:59 +00002514 // Wait for the counter directory packet to ensure the ConnectionAcknowledgedCommandHandler has run.
Finn Williams09ad6f92019-12-19 17:05:18 +00002515 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::CounterDirectory) == 1);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002516
2517 // The Connection Acknowledged Command Handler should have updated the profiling state accordingly
2518 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincighd0613b52019-10-09 16:47:04 +01002519
2520 // Reset the profiling service to stop any running thread
2521 options.m_EnableProfiling = false;
2522 profilingService.ResetExternalProfilingOptions(options, true);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002523}
2524
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002525BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodRequestCounterDirectoryPacket)
2526{
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002527 // Reset the profiling service to the uninitialized state
2528 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002529 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +00002530 armnn::profiling::ProfilingService profilingService;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002531 profilingService.ResetExternalProfilingOptions(options, true);
2532
Sadik Armagan3184c902020-03-18 10:57:30 +00002533 // Swap the profiling connection factory in the profiling service instance with our mock one
2534 SwapProfilingConnectionFactoryHelper helper(profilingService);
2535
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002536 // Bring the profiling service to the "Active" state
2537 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002538 profilingService.Update(); // Initialize the counter directory
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002539 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002540 profilingService.Update(); // Create the profiling connection
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002541 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002542 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002543
Colm Donelan2ba48d22019-11-29 09:10:59 +00002544 // Get the mock profiling connection
2545 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2546 BOOST_CHECK(mockProfilingConnection);
2547
Matteo Martincighe8485382019-10-10 14:08:21 +01002548 // Force the profiling service to the "Active" state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002549 helper.ForceTransitionToState(ProfilingState::Active);
2550 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2551
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002552 // Write a valid "Request Counter Directory" packet into the mock profiling connection, to simulate a valid
2553 // reply from an external profiling service
2554
2555 // Request Counter Directory packet header (word 0, word 1 is always zero):
2556 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2557 // 16:25 [10] packet_id: Packet identifier, value 0b0000000011
2558 // 8:15 [8] reserved: Reserved, value 0b00000000
2559 // 0:7 [8] reserved: Reserved, value 0b00000000
2560 uint32_t packetFamily = 0;
2561 uint32_t packetId = 3;
Keith Davis3201eea2019-10-24 17:30:41 +01002562 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002563
2564 // Create the Request Counter Directory packet
2565 Packet requestCounterDirectoryPacket(header);
2566
2567 // Write the packet to the mock profiling connection
2568 mockProfilingConnection->WritePacket(std::move(requestCounterDirectoryPacket));
2569
Sadik Armagan4c998992020-02-25 12:44:44 +00002570 // Expecting one CounterDirectory Packet of length 652
Finn Williams09ad6f92019-12-19 17:05:18 +00002571 // and one TimelineMessageDirectory packet of length 427
Sadik Armagan4c998992020-02-25 12:44:44 +00002572 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::CounterDirectory, 652) == 1);
Finn Williams09ad6f92019-12-19 17:05:18 +00002573 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::TimelineMessageDirectory, 427) == 1);
Matteo Martincighe8485382019-10-10 14:08:21 +01002574
2575 // The Request Counter Directory Command Handler should not have updated the profiling state
2576 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2577
2578 // Reset the profiling service to stop any running thread
2579 options.m_EnableProfiling = false;
2580 profilingService.ResetExternalProfilingOptions(options, true);
2581}
2582
Matteo Martincighe8485382019-10-10 14:08:21 +01002583BOOST_AUTO_TEST_CASE(CheckProfilingServiceBadPeriodicCounterSelectionPacketInvalidCounterUid)
2584{
Matteo Martincighe8485382019-10-10 14:08:21 +01002585 // Reset the profiling service to the uninitialized state
2586 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002587 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +00002588 armnn::profiling::ProfilingService profilingService;
Matteo Martincighe8485382019-10-10 14:08:21 +01002589 profilingService.ResetExternalProfilingOptions(options, true);
2590
Sadik Armagan3184c902020-03-18 10:57:30 +00002591 // Swap the profiling connection factory in the profiling service instance with our mock one
2592 SwapProfilingConnectionFactoryHelper helper(profilingService);
2593
Matteo Martincighe8485382019-10-10 14:08:21 +01002594 // Bring the profiling service to the "Active" state
2595 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002596 profilingService.Update(); // Initialize the counter directory
Matteo Martincighe8485382019-10-10 14:08:21 +01002597 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002598 profilingService.Update(); // Create the profiling connection
Matteo Martincighe8485382019-10-10 14:08:21 +01002599 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002600 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002601
Colm Donelan2ba48d22019-11-29 09:10:59 +00002602 // Get the mock profiling connection
2603 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2604 BOOST_CHECK(mockProfilingConnection);
2605
Matteo Martincighe8485382019-10-10 14:08:21 +01002606 // Force the profiling service to the "Active" state
2607 helper.ForceTransitionToState(ProfilingState::Active);
2608 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2609
Matteo Martincighe8485382019-10-10 14:08:21 +01002610 // Remove the packets received so far
2611 mockProfilingConnection->Clear();
2612
2613 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2614 // external profiling service
2615
2616 // Periodic Counter Selection packet header:
2617 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2618 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2619 // 8:15 [8] reserved: Reserved, value 0b00000000
2620 // 0:7 [8] reserved: Reserved, value 0b00000000
2621 uint32_t packetFamily = 0;
2622 uint32_t packetId = 4;
Keith Davis3201eea2019-10-24 17:30:41 +01002623 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincighe8485382019-10-10 14:08:21 +01002624
Keith Davis3201eea2019-10-24 17:30:41 +01002625 uint32_t capturePeriod = 123456; // Some capture period (microseconds)
Matteo Martincighe8485382019-10-10 14:08:21 +01002626
2627 // Get the first valid counter UID
2628 const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
Keith Davis3201eea2019-10-24 17:30:41 +01002629 const Counters& counters = counterDirectory.GetCounters();
Matteo Martincighe8485382019-10-10 14:08:21 +01002630 BOOST_CHECK(counters.size() > 1);
Keith Davis3201eea2019-10-24 17:30:41 +01002631 uint16_t counterUidA = counters.begin()->first; // First valid counter UID
2632 uint16_t counterUidB = 9999; // Second invalid counter UID
Matteo Martincighe8485382019-10-10 14:08:21 +01002633
2634 uint32_t length = 8;
2635
2636 auto data = std::make_unique<unsigned char[]>(length);
2637 WriteUint32(data.get(), 0, capturePeriod);
2638 WriteUint16(data.get(), 4, counterUidA);
2639 WriteUint16(data.get(), 6, counterUidB);
2640
2641 // Create the Periodic Counter Selection packet
Keith Davis3201eea2019-10-24 17:30:41 +01002642 Packet periodicCounterSelectionPacket(header, length, data); // Length > 0, this will start the Period Counter
2643 // Capture thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002644
2645 // Write the packet to the mock profiling connection
2646 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2647
Finn Williams09ad6f92019-12-19 17:05:18 +00002648 // Expecting one Periodic Counter Selection packet of length 14
2649 // and at least one Periodic Counter Capture packet of length 22
2650 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterSelection, 14) == 1);
2651 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterCapture, 22) >= 1);
Matteo Martincighe8485382019-10-10 14:08:21 +01002652
2653 // The Periodic Counter Selection Handler should not have updated the profiling state
2654 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2655
2656 // Reset the profiling service to stop any running thread
2657 options.m_EnableProfiling = false;
2658 profilingService.ResetExternalProfilingOptions(options, true);
2659}
2660
2661BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodPeriodicCounterSelectionPacketNoCounters)
2662{
Matteo Martincighe8485382019-10-10 14:08:21 +01002663 // Reset the profiling service to the uninitialized state
2664 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002665 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +00002666 armnn::profiling::ProfilingService profilingService;
Matteo Martincighe8485382019-10-10 14:08:21 +01002667 profilingService.ResetExternalProfilingOptions(options, true);
2668
Sadik Armagan3184c902020-03-18 10:57:30 +00002669 // Swap the profiling connection factory in the profiling service instance with our mock one
2670 SwapProfilingConnectionFactoryHelper helper(profilingService);
2671
Matteo Martincighe8485382019-10-10 14:08:21 +01002672 // Bring the profiling service to the "Active" state
2673 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002674 profilingService.Update(); // Initialize the counter directory
Matteo Martincighe8485382019-10-10 14:08:21 +01002675 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002676 profilingService.Update(); // Create the profiling connection
Matteo Martincighe8485382019-10-10 14:08:21 +01002677 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002678 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002679
Colm Donelan2ba48d22019-11-29 09:10:59 +00002680 // Get the mock profiling connection
2681 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2682 BOOST_CHECK(mockProfilingConnection);
2683
Matteo Martincighe8485382019-10-10 14:08:21 +01002684 // Wait for the Stream Metadata packet the be sent
2685 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Finn Williams09ad6f92019-12-19 17:05:18 +00002686 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData);
Matteo Martincighe8485382019-10-10 14:08:21 +01002687
2688 // Force the profiling service to the "Active" state
2689 helper.ForceTransitionToState(ProfilingState::Active);
2690 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2691
Matteo Martincighe8485382019-10-10 14:08:21 +01002692 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2693 // external profiling service
2694
2695 // Periodic Counter Selection packet header:
2696 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2697 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2698 // 8:15 [8] reserved: Reserved, value 0b00000000
2699 // 0:7 [8] reserved: Reserved, value 0b00000000
2700 uint32_t packetFamily = 0;
2701 uint32_t packetId = 4;
Keith Davis3201eea2019-10-24 17:30:41 +01002702 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincighe8485382019-10-10 14:08:21 +01002703
2704 // Create the Periodic Counter Selection packet
Keith Davis3201eea2019-10-24 17:30:41 +01002705 Packet periodicCounterSelectionPacket(header); // Length == 0, this will disable the collection of counters
Matteo Martincighe8485382019-10-10 14:08:21 +01002706
2707 // Write the packet to the mock profiling connection
2708 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2709
Finn Williams09ad6f92019-12-19 17:05:18 +00002710 // Wait for the Periodic Counter Selection packet of length 12 to be sent
2711 // The size of the expected Periodic Counter Selection (echos the sent one)
2712 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterSelection, 12) == 1);
Matteo Martincighe8485382019-10-10 14:08:21 +01002713
2714 // The Periodic Counter Selection Handler should not have updated the profiling state
2715 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2716
Finn Williams09ad6f92019-12-19 17:05:18 +00002717 // No Periodic Counter packets are expected
2718 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterCapture, 0, 0) == 0);
Matteo Martincighe8485382019-10-10 14:08:21 +01002719
2720 // Reset the profiling service to stop any running thread
2721 options.m_EnableProfiling = false;
2722 profilingService.ResetExternalProfilingOptions(options, true);
2723}
2724
2725BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodPeriodicCounterSelectionPacketSingleCounter)
2726{
Matteo Martincighe8485382019-10-10 14:08:21 +01002727 // Reset the profiling service to the uninitialized state
2728 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002729 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +00002730 armnn::profiling::ProfilingService profilingService;
Matteo Martincighe8485382019-10-10 14:08:21 +01002731 profilingService.ResetExternalProfilingOptions(options, true);
2732
Sadik Armagan3184c902020-03-18 10:57:30 +00002733 // Swap the profiling connection factory in the profiling service instance with our mock one
2734 SwapProfilingConnectionFactoryHelper helper(profilingService);
2735
Matteo Martincighe8485382019-10-10 14:08:21 +01002736 // Bring the profiling service to the "Active" state
2737 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002738 profilingService.Update(); // Initialize the counter directory
Matteo Martincighe8485382019-10-10 14:08:21 +01002739 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002740 profilingService.Update(); // Create the profiling connection
Matteo Martincighe8485382019-10-10 14:08:21 +01002741 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002742 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002743
Colm Donelan2ba48d22019-11-29 09:10:59 +00002744 // Get the mock profiling connection
2745 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2746 BOOST_CHECK(mockProfilingConnection);
2747
Finn Williams09ad6f92019-12-19 17:05:18 +00002748 // Wait for the Stream Metadata packet to be sent
Matteo Martincighe8485382019-10-10 14:08:21 +01002749 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Finn Williams09ad6f92019-12-19 17:05:18 +00002750 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData);
Matteo Martincighe8485382019-10-10 14:08:21 +01002751
2752 // Force the profiling service to the "Active" state
2753 helper.ForceTransitionToState(ProfilingState::Active);
2754 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2755
Matteo Martincighe8485382019-10-10 14:08:21 +01002756 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2757 // external profiling service
2758
2759 // Periodic Counter Selection packet header:
2760 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2761 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2762 // 8:15 [8] reserved: Reserved, value 0b00000000
2763 // 0:7 [8] reserved: Reserved, value 0b00000000
2764 uint32_t packetFamily = 0;
2765 uint32_t packetId = 4;
Keith Davis3201eea2019-10-24 17:30:41 +01002766 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincighe8485382019-10-10 14:08:21 +01002767
Keith Davis3201eea2019-10-24 17:30:41 +01002768 uint32_t capturePeriod = 123456; // Some capture period (microseconds)
Matteo Martincighe8485382019-10-10 14:08:21 +01002769
2770 // Get the first valid counter UID
2771 const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
Keith Davis3201eea2019-10-24 17:30:41 +01002772 const Counters& counters = counterDirectory.GetCounters();
Matteo Martincighe8485382019-10-10 14:08:21 +01002773 BOOST_CHECK(!counters.empty());
Keith Davis3201eea2019-10-24 17:30:41 +01002774 uint16_t counterUid = counters.begin()->first; // Valid counter UID
Matteo Martincighe8485382019-10-10 14:08:21 +01002775
2776 uint32_t length = 6;
2777
2778 auto data = std::make_unique<unsigned char[]>(length);
2779 WriteUint32(data.get(), 0, capturePeriod);
2780 WriteUint16(data.get(), 4, counterUid);
2781
2782 // Create the Periodic Counter Selection packet
Keith Davis3201eea2019-10-24 17:30:41 +01002783 Packet periodicCounterSelectionPacket(header, length, data); // Length > 0, this will start the Period Counter
2784 // Capture thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002785
2786 // Write the packet to the mock profiling connection
2787 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2788
Finn Williams09ad6f92019-12-19 17:05:18 +00002789 // Expecting one Periodic Counter Selection packet of length 14
2790 // and at least one Periodic Counter Capture packet of length 22
2791 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterSelection, 14) == 1);
2792 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterCapture, 22) >= 1);
Matteo Martincighe8485382019-10-10 14:08:21 +01002793
2794 // The Periodic Counter Selection Handler should not have updated the profiling state
2795 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2796
2797 // Reset the profiling service to stop any running thread
2798 options.m_EnableProfiling = false;
2799 profilingService.ResetExternalProfilingOptions(options, true);
2800}
2801
2802BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodPeriodicCounterSelectionPacketMultipleCounters)
2803{
Matteo Martincighe8485382019-10-10 14:08:21 +01002804 // Reset the profiling service to the uninitialized state
2805 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002806 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +00002807 armnn::profiling::ProfilingService profilingService;
Matteo Martincighe8485382019-10-10 14:08:21 +01002808 profilingService.ResetExternalProfilingOptions(options, true);
2809
Sadik Armagan3184c902020-03-18 10:57:30 +00002810 // Swap the profiling connection factory in the profiling service instance with our mock one
2811 SwapProfilingConnectionFactoryHelper helper(profilingService);
2812
Matteo Martincighe8485382019-10-10 14:08:21 +01002813 // Bring the profiling service to the "Active" state
2814 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002815 profilingService.Update(); // Initialize the counter directory
Matteo Martincighe8485382019-10-10 14:08:21 +01002816 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002817 profilingService.Update(); // Create the profiling connection
Matteo Martincighe8485382019-10-10 14:08:21 +01002818 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002819 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002820
Colm Donelan2ba48d22019-11-29 09:10:59 +00002821 // Get the mock profiling connection
2822 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2823 BOOST_CHECK(mockProfilingConnection);
2824
Matteo Martincighe8485382019-10-10 14:08:21 +01002825 // Wait for the Stream Metadata packet the be sent
2826 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Finn Williams09ad6f92019-12-19 17:05:18 +00002827 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData);
Matteo Martincighe8485382019-10-10 14:08:21 +01002828
2829 // Force the profiling service to the "Active" state
2830 helper.ForceTransitionToState(ProfilingState::Active);
2831 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2832
Matteo Martincighe8485382019-10-10 14:08:21 +01002833 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2834 // external profiling service
2835
2836 // Periodic Counter Selection packet header:
2837 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2838 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2839 // 8:15 [8] reserved: Reserved, value 0b00000000
2840 // 0:7 [8] reserved: Reserved, value 0b00000000
2841 uint32_t packetFamily = 0;
2842 uint32_t packetId = 4;
Keith Davis3201eea2019-10-24 17:30:41 +01002843 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincighe8485382019-10-10 14:08:21 +01002844
Keith Davis3201eea2019-10-24 17:30:41 +01002845 uint32_t capturePeriod = 123456; // Some capture period (microseconds)
Matteo Martincighe8485382019-10-10 14:08:21 +01002846
2847 // Get the first valid counter UID
2848 const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
Keith Davis3201eea2019-10-24 17:30:41 +01002849 const Counters& counters = counterDirectory.GetCounters();
Matteo Martincighe8485382019-10-10 14:08:21 +01002850 BOOST_CHECK(counters.size() > 1);
Keith Davis3201eea2019-10-24 17:30:41 +01002851 uint16_t counterUidA = counters.begin()->first; // First valid counter UID
2852 uint16_t counterUidB = (counters.begin()++)->first; // Second valid counter UID
Matteo Martincighe8485382019-10-10 14:08:21 +01002853
2854 uint32_t length = 8;
2855
2856 auto data = std::make_unique<unsigned char[]>(length);
2857 WriteUint32(data.get(), 0, capturePeriod);
2858 WriteUint16(data.get(), 4, counterUidA);
2859 WriteUint16(data.get(), 6, counterUidB);
2860
2861 // Create the Periodic Counter Selection packet
Keith Davis3201eea2019-10-24 17:30:41 +01002862 Packet periodicCounterSelectionPacket(header, length, data); // Length > 0, this will start the Period Counter
2863 // Capture thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002864
2865 // Write the packet to the mock profiling connection
2866 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2867
Finn Williams09ad6f92019-12-19 17:05:18 +00002868 // Expecting one PeriodicCounterSelection Packet with a length of 16
2869 // And at least one PeriodicCounterCapture Packet with a length of 28
2870 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterSelection, 16) == 1);
2871 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterCapture, 28) >= 1);
Matteo Martincighe8485382019-10-10 14:08:21 +01002872
2873 // The Periodic Counter Selection Handler should not have updated the profiling state
2874 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002875
2876 // Reset the profiling service to stop any running thread
2877 options.m_EnableProfiling = false;
2878 profilingService.ResetExternalProfilingOptions(options, true);
2879}
2880
Jim Flynn53e46992019-10-14 12:31:10 +01002881BOOST_AUTO_TEST_CASE(CheckProfilingServiceDisconnect)
2882{
Jim Flynn53e46992019-10-14 12:31:10 +01002883 // Reset the profiling service to the uninitialized state
2884 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002885 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +00002886 armnn::profiling::ProfilingService profilingService;
Jim Flynn53e46992019-10-14 12:31:10 +01002887 profilingService.ResetExternalProfilingOptions(options, true);
2888
Sadik Armagan3184c902020-03-18 10:57:30 +00002889 // Swap the profiling connection factory in the profiling service instance with our mock one
2890 SwapProfilingConnectionFactoryHelper helper(profilingService);
2891
Jim Flynn53e46992019-10-14 12:31:10 +01002892 // Try to disconnect the profiling service while in the "Uninitialised" state
2893 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
2894 profilingService.Disconnect();
Keith Davis3201eea2019-10-24 17:30:41 +01002895 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised); // The state should not change
Jim Flynn53e46992019-10-14 12:31:10 +01002896
2897 // Try to disconnect the profiling service while in the "NotConnected" state
Keith Davis3201eea2019-10-24 17:30:41 +01002898 profilingService.Update(); // Initialize the counter directory
Jim Flynn53e46992019-10-14 12:31:10 +01002899 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
2900 profilingService.Disconnect();
Keith Davis3201eea2019-10-24 17:30:41 +01002901 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected); // The state should not change
Jim Flynn53e46992019-10-14 12:31:10 +01002902
2903 // Try to disconnect the profiling service while in the "WaitingForAck" state
Keith Davis3201eea2019-10-24 17:30:41 +01002904 profilingService.Update(); // Create the profiling connection
Jim Flynn53e46992019-10-14 12:31:10 +01002905 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
2906 profilingService.Disconnect();
Keith Davis3201eea2019-10-24 17:30:41 +01002907 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck); // The state should not change
Jim Flynn53e46992019-10-14 12:31:10 +01002908
2909 // Try to disconnect the profiling service while in the "Active" state
Keith Davis3201eea2019-10-24 17:30:41 +01002910 profilingService.Update(); // Start the command handler and the send thread
Jim Flynn53e46992019-10-14 12:31:10 +01002911
Colm Donelan2ba48d22019-11-29 09:10:59 +00002912 // Get the mock profiling connection
2913 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2914 BOOST_CHECK(mockProfilingConnection);
2915
Jim Flynn53e46992019-10-14 12:31:10 +01002916 // Wait for the Stream Metadata packet the be sent
2917 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Finn Williams09ad6f92019-12-19 17:05:18 +00002918 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData);
Jim Flynn53e46992019-10-14 12:31:10 +01002919
2920 // Force the profiling service to the "Active" state
2921 helper.ForceTransitionToState(ProfilingState::Active);
2922 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2923
Jim Flynn53e46992019-10-14 12:31:10 +01002924 // Check that the profiling connection is open
2925 BOOST_CHECK(mockProfilingConnection->IsOpen());
2926
2927 profilingService.Disconnect();
Colm Donelan2ba48d22019-11-29 09:10:59 +00002928 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected); // The state should have changed
Jim Flynn53e46992019-10-14 12:31:10 +01002929
2930 // Check that the profiling connection has been reset
2931 mockProfilingConnection = helper.GetMockProfilingConnection();
2932 BOOST_CHECK(mockProfilingConnection == nullptr);
2933
2934 // Reset the profiling service to stop any running thread
2935 options.m_EnableProfiling = false;
2936 profilingService.ResetExternalProfilingOptions(options, true);
2937}
2938
Matteo Martincigh994b5342019-10-11 17:19:56 +01002939BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodPerJobCounterSelectionPacket)
2940{
Matteo Martincigh994b5342019-10-11 17:19:56 +01002941 // Reset the profiling service to the uninitialized state
2942 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002943 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +00002944 armnn::profiling::ProfilingService profilingService;
Matteo Martincigh994b5342019-10-11 17:19:56 +01002945 profilingService.ResetExternalProfilingOptions(options, true);
2946
Sadik Armagan3184c902020-03-18 10:57:30 +00002947 // Swap the profiling connection factory in the profiling service instance with our mock one
2948 SwapProfilingConnectionFactoryHelper helper(profilingService);
2949
Matteo Martincigh994b5342019-10-11 17:19:56 +01002950 // Bring the profiling service to the "Active" state
2951 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002952 profilingService.Update(); // Initialize the counter directory
Matteo Martincigh994b5342019-10-11 17:19:56 +01002953 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002954 profilingService.Update(); // Create the profiling connection
Matteo Martincigh994b5342019-10-11 17:19:56 +01002955 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002956 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincigh994b5342019-10-11 17:19:56 +01002957
Colm Donelan2ba48d22019-11-29 09:10:59 +00002958 // Get the mock profiling connection
2959 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2960 BOOST_CHECK(mockProfilingConnection);
2961
Matteo Martincigh994b5342019-10-11 17:19:56 +01002962 // Wait for the Stream Metadata packet the be sent
2963 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Finn Williams09ad6f92019-12-19 17:05:18 +00002964 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData);
Matteo Martincigh994b5342019-10-11 17:19:56 +01002965
2966 // Force the profiling service to the "Active" state
2967 helper.ForceTransitionToState(ProfilingState::Active);
2968 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2969
Matteo Martincigh994b5342019-10-11 17:19:56 +01002970 // Write a "Per-Job Counter Selection" packet into the mock profiling connection, to simulate an input from an
2971 // external profiling service
2972
2973 // Per-Job Counter Selection packet header:
2974 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2975 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2976 // 8:15 [8] reserved: Reserved, value 0b00000000
2977 // 0:7 [8] reserved: Reserved, value 0b00000000
2978 uint32_t packetFamily = 0;
2979 uint32_t packetId = 5;
Keith Davis3201eea2019-10-24 17:30:41 +01002980 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincigh994b5342019-10-11 17:19:56 +01002981
2982 // Create the Per-Job Counter Selection packet
Keith Davis3201eea2019-10-24 17:30:41 +01002983 Packet periodicCounterSelectionPacket(header); // Length == 0, this will disable the collection of counters
Matteo Martincigh994b5342019-10-11 17:19:56 +01002984
2985 // Write the packet to the mock profiling connection
2986 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2987
2988 // Wait for a bit (must at least be the delay value of the mock profiling connection) to make sure that
2989 // the Per-Job Counter Selection packet gets processed by the profiling service
Colm Donelan2ba48d22019-11-29 09:10:59 +00002990 std::this_thread::sleep_for(std::chrono::milliseconds(5));
Matteo Martincigh994b5342019-10-11 17:19:56 +01002991
Matteo Martincigh994b5342019-10-11 17:19:56 +01002992 // The Per-Job Counter Selection Command Handler should not have updated the profiling state
2993 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2994
Finn Williams09ad6f92019-12-19 17:05:18 +00002995 // The Per-Job Counter Selection packets are dropped silently, so there should be no reply coming
2996 // from the profiling service
2997 const auto StreamMetaDataSize = static_cast<unsigned long>(
2998 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData, 0, 0));
2999 BOOST_CHECK(StreamMetaDataSize == mockProfilingConnection->GetWrittenDataSize());
3000
Matteo Martincigh994b5342019-10-11 17:19:56 +01003001 // Reset the profiling service to stop any running thread
3002 options.m_EnableProfiling = false;
3003 profilingService.ResetExternalProfilingOptions(options, true);
3004}
3005
Jim Flynn672d06e2019-10-15 10:18:11 +01003006BOOST_AUTO_TEST_CASE(CheckConfigureProfilingServiceOn)
3007{
3008 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01003009 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +00003010 armnn::profiling::ProfilingService profilingService;
Jim Flynn672d06e2019-10-15 10:18:11 +01003011 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3012 profilingService.ConfigureProfilingService(options);
3013 // should get as far as NOT_CONNECTED
3014 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
3015 // Reset the profiling service to stop any running thread
3016 options.m_EnableProfiling = false;
3017 profilingService.ResetExternalProfilingOptions(options, true);
3018}
3019
3020BOOST_AUTO_TEST_CASE(CheckConfigureProfilingServiceOff)
3021{
3022 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Sadik Armagan3184c902020-03-18 10:57:30 +00003023 armnn::profiling::ProfilingService profilingService;
Jim Flynn672d06e2019-10-15 10:18:11 +01003024 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3025 profilingService.ConfigureProfilingService(options);
3026 // should not move from Uninitialised
3027 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3028 // Reset the profiling service to stop any running thread
3029 options.m_EnableProfiling = false;
3030 profilingService.ResetExternalProfilingOptions(options, true);
3031}
3032
Colm Donelan2ba48d22019-11-29 09:10:59 +00003033BOOST_AUTO_TEST_CASE(CheckProfilingServiceEnabled)
3034{
3035 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
3036 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
3037 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
3038 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +00003039 armnn::profiling::ProfilingService profilingService;
Colm Donelan2ba48d22019-11-29 09:10:59 +00003040 profilingService.ResetExternalProfilingOptions(options, true);
3041 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3042 profilingService.Update();
3043 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
3044
3045 // Redirect the output to a local stream so that we can parse the warning message
3046 std::stringstream ss;
3047 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3048 profilingService.Update();
Finn Williams09ad6f92019-12-19 17:05:18 +00003049
3050 // Reset the profiling service to stop any running thread
3051 options.m_EnableProfiling = false;
3052 profilingService.ResetExternalProfilingOptions(options, true);
3053
Colm Donelan2ba48d22019-11-29 09:10:59 +00003054 streamRedirector.CancelRedirect();
3055
3056 // Check that the expected error has occurred and logged to the standard output
3057 if (!boost::contains(ss.str(), "Cannot connect to stream socket: Connection refused"))
3058 {
3059 std::cout << ss.str();
3060 BOOST_FAIL("Expected string not found.");
3061 }
Colm Donelan2ba48d22019-11-29 09:10:59 +00003062}
3063
3064BOOST_AUTO_TEST_CASE(CheckProfilingServiceEnabledRuntime)
3065{
3066 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
3067 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
3068 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Sadik Armagan3184c902020-03-18 10:57:30 +00003069 armnn::profiling::ProfilingService profilingService;
Colm Donelan2ba48d22019-11-29 09:10:59 +00003070 profilingService.ResetExternalProfilingOptions(options, true);
3071 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3072 profilingService.Update();
3073 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3074 options.m_EnableProfiling = true;
3075 profilingService.ResetExternalProfilingOptions(options);
3076 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3077 profilingService.Update();
3078 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
3079
3080 // Redirect the output to a local stream so that we can parse the warning message
3081 std::stringstream ss;
3082 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3083 profilingService.Update();
3084
Finn Williams09ad6f92019-12-19 17:05:18 +00003085 // Reset the profiling service to stop any running thread
3086 options.m_EnableProfiling = false;
3087 profilingService.ResetExternalProfilingOptions(options, true);
3088
Colm Donelan2ba48d22019-11-29 09:10:59 +00003089 streamRedirector.CancelRedirect();
3090
3091 // Check that the expected error has occurred and logged to the standard output
3092 if (!boost::contains(ss.str(), "Cannot connect to stream socket: Connection refused"))
3093 {
3094 std::cout << ss.str();
3095 BOOST_FAIL("Expected string not found.");
3096 }
Colm Donelan2ba48d22019-11-29 09:10:59 +00003097}
3098
3099BOOST_AUTO_TEST_CASE(CheckProfilingServiceBadConnectionAcknowledgedPacket)
3100{
3101 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
3102 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
Sadik Armagan3184c902020-03-18 10:57:30 +00003103
Colm Donelan2ba48d22019-11-29 09:10:59 +00003104
3105 // Redirect the standard output to a local stream so that we can parse the warning message
3106 std::stringstream ss;
3107 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3108
Colm Donelan2ba48d22019-11-29 09:10:59 +00003109 // Reset the profiling service to the uninitialized state
3110 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
3111 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +00003112 armnn::profiling::ProfilingService profilingService;
Colm Donelan2ba48d22019-11-29 09:10:59 +00003113 profilingService.ResetExternalProfilingOptions(options, true);
3114
Sadik Armagan3184c902020-03-18 10:57:30 +00003115 // Swap the profiling connection factory in the profiling service instance with our mock one
3116 SwapProfilingConnectionFactoryHelper helper(profilingService);
3117
Colm Donelan2ba48d22019-11-29 09:10:59 +00003118 // Bring the profiling service to the "WaitingForAck" state
3119 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3120 profilingService.Update(); // Initialize the counter directory
3121 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
3122 profilingService.Update(); // Create the profiling connection
3123
3124 // Get the mock profiling connection
3125 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
3126 BOOST_CHECK(mockProfilingConnection);
3127
Colm Donelan2ba48d22019-11-29 09:10:59 +00003128 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003129
3130 // Connection Acknowledged Packet header (word 0, word 1 is always zero):
3131 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
3132 // 16:25 [10] packet_id: Packet identifier, value 0b0000000001
3133 // 8:15 [8] reserved: Reserved, value 0b00000000
3134 // 0:7 [8] reserved: Reserved, value 0b00000000
3135 uint32_t packetFamily = 0;
3136 uint32_t packetId = 37; // Wrong packet id!!!
3137 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
3138
3139 // Create the Connection Acknowledged Packet
3140 Packet connectionAcknowledgedPacket(header);
Finn Williams09ad6f92019-12-19 17:05:18 +00003141 // Write an invalid "Connection Acknowledged" packet into the mock profiling connection, to simulate an invalid
3142 // reply from an external profiling service
Colm Donelan2ba48d22019-11-29 09:10:59 +00003143 mockProfilingConnection->WritePacket(std::move(connectionAcknowledgedPacket));
3144
Finn Williams09ad6f92019-12-19 17:05:18 +00003145 // Start the command thread
3146 profilingService.Update();
3147
3148 // Wait for the command thread to join
3149 options.m_EnableProfiling = false;
3150 profilingService.ResetExternalProfilingOptions(options, true);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003151
3152 streamRedirector.CancelRedirect();
3153
3154 // Check that the expected error has occurred and logged to the standard output
3155 if (!boost::contains(ss.str(), "Functor with requested PacketId=37 and Version=4194304 does not exist"))
3156 {
3157 std::cout << ss.str();
3158 BOOST_FAIL("Expected string not found.");
3159 }
Colm Donelan2ba48d22019-11-29 09:10:59 +00003160}
3161
3162BOOST_AUTO_TEST_CASE(CheckProfilingServiceBadRequestCounterDirectoryPacket)
3163{
3164 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
3165 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003166
3167 // Redirect the standard output to a local stream so that we can parse the warning message
3168 std::stringstream ss;
3169 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3170
3171 // Reset the profiling service to the uninitialized state
3172 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
3173 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +00003174 armnn::profiling::ProfilingService profilingService;
Colm Donelan2ba48d22019-11-29 09:10:59 +00003175 profilingService.ResetExternalProfilingOptions(options, true);
3176
Sadik Armagan3184c902020-03-18 10:57:30 +00003177 // Swap the profiling connection factory in the profiling service instance with our mock one
3178 SwapProfilingConnectionFactoryHelper helper(profilingService);
3179
Colm Donelan2ba48d22019-11-29 09:10:59 +00003180 // Bring the profiling service to the "Active" state
3181 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3182 helper.ForceTransitionToState(ProfilingState::NotConnected);
3183 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
3184 profilingService.Update(); // Create the profiling connection
3185 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003186
3187 // Get the mock profiling connection
3188 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
3189 BOOST_CHECK(mockProfilingConnection);
3190
Colm Donelan2ba48d22019-11-29 09:10:59 +00003191 // Write a valid "Request Counter Directory" packet into the mock profiling connection, to simulate a valid
3192 // reply from an external profiling service
3193
3194 // Request Counter Directory packet header (word 0, word 1 is always zero):
3195 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
3196 // 16:25 [10] packet_id: Packet identifier, value 0b0000000011
3197 // 8:15 [8] reserved: Reserved, value 0b00000000
3198 // 0:7 [8] reserved: Reserved, value 0b00000000
3199 uint32_t packetFamily = 0;
3200 uint32_t packetId = 123; // Wrong packet id!!!
3201 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
3202
3203 // Create the Request Counter Directory packet
3204 Packet requestCounterDirectoryPacket(header);
3205
3206 // Write the packet to the mock profiling connection
3207 mockProfilingConnection->WritePacket(std::move(requestCounterDirectoryPacket));
3208
Finn Williams09ad6f92019-12-19 17:05:18 +00003209 // Start the command handler and the send thread
3210 profilingService.Update();
3211
3212 // Reset the profiling service to stop and join any running thread
3213 options.m_EnableProfiling = false;
3214 profilingService.ResetExternalProfilingOptions(options, true);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003215
3216 streamRedirector.CancelRedirect();
3217
3218 // Check that the expected error has occurred and logged to the standard output
3219 if (!boost::contains(ss.str(), "Functor with requested PacketId=123 and Version=4194304 does not exist"))
3220 {
3221 std::cout << ss.str();
3222 BOOST_FAIL("Expected string not found.");
3223 }
Colm Donelan2ba48d22019-11-29 09:10:59 +00003224}
3225
3226BOOST_AUTO_TEST_CASE(CheckProfilingServiceBadPeriodicCounterSelectionPacket)
3227{
3228 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
3229 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003230
3231 // Redirect the standard output to a local stream so that we can parse the warning message
3232 std::stringstream ss;
3233 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3234
3235 // Reset the profiling service to the uninitialized state
3236 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
3237 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +00003238 armnn::profiling::ProfilingService profilingService;
Colm Donelan2ba48d22019-11-29 09:10:59 +00003239 profilingService.ResetExternalProfilingOptions(options, true);
3240
Sadik Armagan3184c902020-03-18 10:57:30 +00003241 // Swap the profiling connection factory in the profiling service instance with our mock one
3242 SwapProfilingConnectionFactoryHelper helper(profilingService);
3243
Colm Donelan2ba48d22019-11-29 09:10:59 +00003244 // Bring the profiling service to the "Active" state
3245 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3246 profilingService.Update(); // Initialize the counter directory
3247 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
3248 profilingService.Update(); // Create the profiling connection
3249 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
3250 profilingService.Update(); // Start the command handler and the send thread
3251
3252 // Get the mock profiling connection
3253 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
3254 BOOST_CHECK(mockProfilingConnection);
3255
Colm Donelan2ba48d22019-11-29 09:10:59 +00003256 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
3257 // external profiling service
3258
3259 // Periodic Counter Selection packet header:
3260 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
3261 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
3262 // 8:15 [8] reserved: Reserved, value 0b00000000
3263 // 0:7 [8] reserved: Reserved, value 0b00000000
3264 uint32_t packetFamily = 0;
3265 uint32_t packetId = 999; // Wrong packet id!!!
3266 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
3267
3268 // Create the Periodic Counter Selection packet
3269 Packet periodicCounterSelectionPacket(header); // Length == 0, this will disable the collection of counters
3270
3271 // Write the packet to the mock profiling connection
3272 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
Finn Williams09ad6f92019-12-19 17:05:18 +00003273 profilingService.Update();
Colm Donelan2ba48d22019-11-29 09:10:59 +00003274
Finn Williams09ad6f92019-12-19 17:05:18 +00003275 // Reset the profiling service to stop any running thread
3276 options.m_EnableProfiling = false;
3277 profilingService.ResetExternalProfilingOptions(options, true);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003278
3279 // Check that the expected error has occurred and logged to the standard output
3280 streamRedirector.CancelRedirect();
3281
3282 // Check that the expected error has occurred and logged to the standard output
3283 if (!boost::contains(ss.str(), "Functor with requested PacketId=999 and Version=4194304 does not exist"))
3284 {
3285 std::cout << ss.str();
3286 BOOST_FAIL("Expected string not found.");
3287 }
Colm Donelan2ba48d22019-11-29 09:10:59 +00003288}
Jim Flynn97897022020-02-02 12:52:59 +00003289
David Monahande803072020-01-30 12:44:23 +00003290BOOST_AUTO_TEST_CASE(CheckCounterIdMap)
3291{
3292 CounterIdMap counterIdMap;
3293 BOOST_CHECK_THROW(counterIdMap.GetBackendId(0), armnn::Exception);
3294 BOOST_CHECK_THROW(counterIdMap.GetGlobalId(0, armnn::profiling::BACKEND_ID), armnn::Exception);
3295
3296 uint16_t globalCounterIds = 0;
3297
3298 armnn::BackendId cpuRefId(armnn::Compute::CpuRef);
3299 armnn::BackendId cpuAccId(armnn::Compute::CpuAcc);
3300
3301 std::vector<uint16_t> cpuRefCounters = {0, 1, 2, 3};
3302 std::vector<uint16_t> cpuAccCounters = {0, 1};
3303
3304 for (uint16_t backendCounterId : cpuRefCounters)
3305 {
3306 counterIdMap.RegisterMapping(globalCounterIds, backendCounterId, cpuRefId);
3307 ++globalCounterIds;
3308 }
3309 for (uint16_t backendCounterId : cpuAccCounters)
3310 {
3311 counterIdMap.RegisterMapping(globalCounterIds, backendCounterId, cpuAccId);
3312 ++globalCounterIds;
3313 }
3314
3315 BOOST_CHECK(counterIdMap.GetBackendId(0) == (std::pair<uint16_t, armnn::BackendId>(0, cpuRefId)));
3316 BOOST_CHECK(counterIdMap.GetBackendId(1) == (std::pair<uint16_t, armnn::BackendId>(1, cpuRefId)));
3317 BOOST_CHECK(counterIdMap.GetBackendId(2) == (std::pair<uint16_t, armnn::BackendId>(2, cpuRefId)));
3318 BOOST_CHECK(counterIdMap.GetBackendId(3) == (std::pair<uint16_t, armnn::BackendId>(3, cpuRefId)));
3319 BOOST_CHECK(counterIdMap.GetBackendId(4) == (std::pair<uint16_t, armnn::BackendId>(0, cpuAccId)));
3320 BOOST_CHECK(counterIdMap.GetBackendId(5) == (std::pair<uint16_t, armnn::BackendId>(1, cpuAccId)));
3321
3322 BOOST_CHECK(counterIdMap.GetGlobalId(0, cpuRefId) == 0);
3323 BOOST_CHECK(counterIdMap.GetGlobalId(1, cpuRefId) == 1);
3324 BOOST_CHECK(counterIdMap.GetGlobalId(2, cpuRefId) == 2);
3325 BOOST_CHECK(counterIdMap.GetGlobalId(3, cpuRefId) == 3);
3326 BOOST_CHECK(counterIdMap.GetGlobalId(0, cpuAccId) == 4);
3327 BOOST_CHECK(counterIdMap.GetGlobalId(1, cpuAccId) == 5);
3328}
Colm Donelan2ba48d22019-11-29 09:10:59 +00003329
Jim Flynn97897022020-02-02 12:52:59 +00003330BOOST_AUTO_TEST_CASE(CheckRegisterBackendCounters)
3331{
3332 uint16_t globalCounterIds = armnn::profiling::INFERENCES_RUN;
3333 armnn::BackendId cpuRefId(armnn::Compute::CpuRef);
3334
Jim Flynn97897022020-02-02 12:52:59 +00003335 // Reset the profiling service to the uninitialized state
3336 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
3337 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +00003338 ProfilingService profilingService;
Jim Flynn97897022020-02-02 12:52:59 +00003339 profilingService.ResetExternalProfilingOptions(options, true);
3340
Sadik Armagan3184c902020-03-18 10:57:30 +00003341 RegisterBackendCounters registerBackendCounters(globalCounterIds, cpuRefId, profilingService);
3342
3343
3344
Jim Flynn97897022020-02-02 12:52:59 +00003345 BOOST_CHECK(profilingService.GetCounterDirectory().GetCategories().empty());
3346 registerBackendCounters.RegisterCategory("categoryOne");
3347 auto categoryOnePtr = profilingService.GetCounterDirectory().GetCategory("categoryOne");
3348 BOOST_CHECK(categoryOnePtr);
3349
3350 BOOST_CHECK(profilingService.GetCounterDirectory().GetDevices().empty());
3351 globalCounterIds = registerBackendCounters.RegisterDevice("deviceOne");
3352 auto deviceOnePtr = profilingService.GetCounterDirectory().GetDevice(globalCounterIds);
3353 BOOST_CHECK(deviceOnePtr);
3354 BOOST_CHECK(deviceOnePtr->m_Name == "deviceOne");
3355
3356 BOOST_CHECK(profilingService.GetCounterDirectory().GetCounterSets().empty());
3357 globalCounterIds = registerBackendCounters.RegisterCounterSet("counterSetOne");
3358 auto counterSetOnePtr = profilingService.GetCounterDirectory().GetCounterSet(globalCounterIds);
3359 BOOST_CHECK(counterSetOnePtr);
3360 BOOST_CHECK(counterSetOnePtr->m_Name == "counterSetOne");
3361
3362 uint16_t newGlobalCounterId = registerBackendCounters.RegisterCounter(0,
3363 "categoryOne",
3364 0,
3365 0,
3366 1.f,
3367 "CounterOne",
3368 "first test counter");
3369 BOOST_CHECK(newGlobalCounterId = armnn::profiling::INFERENCES_RUN + 1);
3370 uint16_t mappedGlobalId = profilingService.GetCounterMappings().GetGlobalId(0, cpuRefId);
3371 BOOST_CHECK(mappedGlobalId == newGlobalCounterId);
3372 auto backendMapping = profilingService.GetCounterMappings().GetBackendId(newGlobalCounterId);
3373 BOOST_CHECK(backendMapping.first == 0);
3374 BOOST_CHECK(backendMapping.second == cpuRefId);
3375
3376 // Reset the profiling service to stop any running thread
3377 options.m_EnableProfiling = false;
3378 profilingService.ResetExternalProfilingOptions(options, true);
3379}
3380
James Conroy2dcd3fe2020-02-06 18:34:52 +00003381BOOST_AUTO_TEST_CASE(CheckCounterStatusQuery)
3382{
3383 armnn::IRuntime::CreationOptions options;
3384 options.m_ProfilingOptions.m_EnableProfiling = true;
3385
3386 // Reset the profiling service to the uninitialized state
Sadik Armagan3184c902020-03-18 10:57:30 +00003387 ProfilingService profilingService;
James Conroy2dcd3fe2020-02-06 18:34:52 +00003388 profilingService.ResetExternalProfilingOptions(options.m_ProfilingOptions, true);
3389
3390 const armnn::BackendId cpuRefId(armnn::Compute::CpuRef);
3391 const armnn::BackendId cpuAccId(armnn::Compute::CpuAcc);
3392
3393 // Create BackendProfiling for each backend
3394 BackendProfiling backendProfilingCpuRef(options, profilingService, cpuRefId);
3395 BackendProfiling backendProfilingCpuAcc(options, profilingService, cpuAccId);
3396
3397 uint16_t initialNumGlobalCounterIds = armnn::profiling::INFERENCES_RUN;
3398
3399 // Create RegisterBackendCounters for CpuRef
Sadik Armagan3184c902020-03-18 10:57:30 +00003400 RegisterBackendCounters registerBackendCountersCpuRef(initialNumGlobalCounterIds, cpuRefId, profilingService);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003401
3402 // Create 'testCategory' in CounterDirectory (backend agnostic)
3403 BOOST_CHECK(profilingService.GetCounterDirectory().GetCategories().empty());
3404 registerBackendCountersCpuRef.RegisterCategory("testCategory");
3405 auto categoryOnePtr = profilingService.GetCounterDirectory().GetCategory("testCategory");
3406 BOOST_CHECK(categoryOnePtr);
3407
3408 // Counters:
3409 // Global | Local | Backend
3410 // 5 | 0 | CpuRef
3411 // 6 | 1 | CpuRef
3412 // 7 | 1 | CpuAcc
3413
3414 std::vector<uint16_t> cpuRefCounters = {0, 1};
3415 std::vector<uint16_t> cpuAccCounters = {0};
3416
3417 // Register the backend counters for CpuRef and validate GetGlobalId and GetBackendId
3418 uint16_t currentNumGlobalCounterIds = registerBackendCountersCpuRef.RegisterCounter(
3419 0, "testCategory", 0, 0, 1.f, "CpuRefCounter0", "Zeroth CpuRef Counter");
3420 BOOST_CHECK(currentNumGlobalCounterIds == initialNumGlobalCounterIds + 1);
3421 uint16_t mappedGlobalId = profilingService.GetCounterMappings().GetGlobalId(0, cpuRefId);
3422 BOOST_CHECK(mappedGlobalId == currentNumGlobalCounterIds);
3423 auto backendMapping = profilingService.GetCounterMappings().GetBackendId(currentNumGlobalCounterIds);
3424 BOOST_CHECK(backendMapping.first == 0);
3425 BOOST_CHECK(backendMapping.second == cpuRefId);
3426
3427 currentNumGlobalCounterIds = registerBackendCountersCpuRef.RegisterCounter(
3428 1, "testCategory", 0, 0, 1.f, "CpuRefCounter1", "First CpuRef Counter");
3429 BOOST_CHECK(currentNumGlobalCounterIds == initialNumGlobalCounterIds + 2);
3430 mappedGlobalId = profilingService.GetCounterMappings().GetGlobalId(1, cpuRefId);
3431 BOOST_CHECK(mappedGlobalId == currentNumGlobalCounterIds);
3432 backendMapping = profilingService.GetCounterMappings().GetBackendId(currentNumGlobalCounterIds);
3433 BOOST_CHECK(backendMapping.first == 1);
3434 BOOST_CHECK(backendMapping.second == cpuRefId);
3435
3436 // Create RegisterBackendCounters for CpuAcc
Sadik Armagan3184c902020-03-18 10:57:30 +00003437 RegisterBackendCounters registerBackendCountersCpuAcc(currentNumGlobalCounterIds, cpuAccId, profilingService);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003438
3439 // Register the backend counter for CpuAcc and validate GetGlobalId and GetBackendId
3440 currentNumGlobalCounterIds = registerBackendCountersCpuAcc.RegisterCounter(
3441 0, "testCategory", 0, 0, 1.f, "CpuAccCounter0", "Zeroth CpuAcc Counter");
3442 BOOST_CHECK(currentNumGlobalCounterIds == initialNumGlobalCounterIds + 3);
3443 mappedGlobalId = profilingService.GetCounterMappings().GetGlobalId(0, cpuAccId);
3444 BOOST_CHECK(mappedGlobalId == currentNumGlobalCounterIds);
3445 backendMapping = profilingService.GetCounterMappings().GetBackendId(currentNumGlobalCounterIds);
3446 BOOST_CHECK(backendMapping.first == 0);
3447 BOOST_CHECK(backendMapping.second == cpuAccId);
3448
3449 // Create vectors for active counters
3450 const std::vector<uint16_t> activeGlobalCounterIds = {5}; // CpuRef(0) activated
3451 const std::vector<uint16_t> newActiveGlobalCounterIds = {6, 7}; // CpuRef(0) and CpuAcc(1) activated
3452
3453 const uint32_t capturePeriod = 200;
3454 const uint32_t newCapturePeriod = 100;
3455
3456 // Set capture period and active counters in CaptureData
Finn Williams032bc742020-02-12 11:02:34 +00003457 profilingService.SetCaptureData(capturePeriod, activeGlobalCounterIds, {});
James Conroy2dcd3fe2020-02-06 18:34:52 +00003458
3459 // Get vector of active counters for CpuRef and CpuAcc backends
3460 std::vector<CounterStatus> cpuRefCounterStatus = backendProfilingCpuRef.GetActiveCounters();
3461 std::vector<CounterStatus> cpuAccCounterStatus = backendProfilingCpuAcc.GetActiveCounters();
3462 BOOST_CHECK_EQUAL(cpuRefCounterStatus.size(), 1);
3463 BOOST_CHECK_EQUAL(cpuAccCounterStatus.size(), 0);
3464
3465 // Check active CpuRef counter
3466 BOOST_CHECK_EQUAL(cpuRefCounterStatus[0].m_GlobalCounterId, activeGlobalCounterIds[0]);
3467 BOOST_CHECK_EQUAL(cpuRefCounterStatus[0].m_BackendCounterId, cpuRefCounters[0]);
3468 BOOST_CHECK_EQUAL(cpuRefCounterStatus[0].m_SamplingRateInMicroseconds, capturePeriod);
3469 BOOST_CHECK_EQUAL(cpuRefCounterStatus[0].m_Enabled, true);
3470
3471 // Check inactive CpuRef counter
3472 CounterStatus inactiveCpuRefCounter = backendProfilingCpuRef.GetCounterStatus(cpuRefCounters[1]);
3473 BOOST_CHECK_EQUAL(inactiveCpuRefCounter.m_GlobalCounterId, 6);
3474 BOOST_CHECK_EQUAL(inactiveCpuRefCounter.m_BackendCounterId, cpuRefCounters[1]);
3475 BOOST_CHECK_EQUAL(inactiveCpuRefCounter.m_SamplingRateInMicroseconds, 0);
3476 BOOST_CHECK_EQUAL(inactiveCpuRefCounter.m_Enabled, false);
3477
3478 // Check inactive CpuAcc counter
3479 CounterStatus inactiveCpuAccCounter = backendProfilingCpuAcc.GetCounterStatus(cpuAccCounters[0]);
3480 BOOST_CHECK_EQUAL(inactiveCpuAccCounter.m_GlobalCounterId, 7);
3481 BOOST_CHECK_EQUAL(inactiveCpuAccCounter.m_BackendCounterId, cpuAccCounters[0]);
3482 BOOST_CHECK_EQUAL(inactiveCpuAccCounter.m_SamplingRateInMicroseconds, 0);
3483 BOOST_CHECK_EQUAL(inactiveCpuAccCounter.m_Enabled, false);
3484
3485 // Set new capture period and new active counters in CaptureData
Finn Williams032bc742020-02-12 11:02:34 +00003486 profilingService.SetCaptureData(newCapturePeriod, newActiveGlobalCounterIds, {});
James Conroy2dcd3fe2020-02-06 18:34:52 +00003487
3488 // Get vector of active counters for CpuRef and CpuAcc backends
3489 cpuRefCounterStatus = backendProfilingCpuRef.GetActiveCounters();
3490 cpuAccCounterStatus = backendProfilingCpuAcc.GetActiveCounters();
3491 BOOST_CHECK_EQUAL(cpuRefCounterStatus.size(), 1);
3492 BOOST_CHECK_EQUAL(cpuAccCounterStatus.size(), 1);
3493
3494 // Check active CpuRef counter
3495 BOOST_CHECK_EQUAL(cpuRefCounterStatus[0].m_GlobalCounterId, newActiveGlobalCounterIds[0]);
3496 BOOST_CHECK_EQUAL(cpuRefCounterStatus[0].m_BackendCounterId, cpuRefCounters[1]);
3497 BOOST_CHECK_EQUAL(cpuRefCounterStatus[0].m_SamplingRateInMicroseconds, newCapturePeriod);
3498 BOOST_CHECK_EQUAL(cpuRefCounterStatus[0].m_Enabled, true);
3499
3500 // Check active CpuAcc counter
3501 BOOST_CHECK_EQUAL(cpuAccCounterStatus[0].m_GlobalCounterId, newActiveGlobalCounterIds[1]);
3502 BOOST_CHECK_EQUAL(cpuAccCounterStatus[0].m_BackendCounterId, cpuAccCounters[0]);
3503 BOOST_CHECK_EQUAL(cpuAccCounterStatus[0].m_SamplingRateInMicroseconds, newCapturePeriod);
3504 BOOST_CHECK_EQUAL(cpuAccCounterStatus[0].m_Enabled, true);
3505
3506 // Check inactive CpuRef counter
3507 inactiveCpuRefCounter = backendProfilingCpuRef.GetCounterStatus(cpuRefCounters[0]);
3508 BOOST_CHECK_EQUAL(inactiveCpuRefCounter.m_GlobalCounterId, 5);
3509 BOOST_CHECK_EQUAL(inactiveCpuRefCounter.m_BackendCounterId, cpuRefCounters[0]);
3510 BOOST_CHECK_EQUAL(inactiveCpuRefCounter.m_SamplingRateInMicroseconds, 0);
3511 BOOST_CHECK_EQUAL(inactiveCpuRefCounter.m_Enabled, false);
3512
3513 // Reset the profiling service to stop any running thread
3514 options.m_ProfilingOptions.m_EnableProfiling = false;
3515 profilingService.ResetExternalProfilingOptions(options.m_ProfilingOptions, true);
3516}
3517
Sadik Armagancab588a2020-02-17 11:33:31 +00003518BOOST_AUTO_TEST_CASE(CheckRegisterCounters)
3519{
3520 armnn::Runtime::CreationOptions options;
3521 options.m_ProfilingOptions.m_EnableProfiling = true;
3522 MockBufferManager mockBuffer(1024);
Sadik Armagan3184c902020-03-18 10:57:30 +00003523
Sadik Armagancab588a2020-02-17 11:33:31 +00003524 CaptureData captureData;
Keith Davis33ed2212020-03-30 10:43:41 +01003525 MockProfilingService mockProfilingService(mockBuffer, options.m_ProfilingOptions.m_EnableProfiling, captureData);
Sadik Armagancab588a2020-02-17 11:33:31 +00003526 armnn::BackendId cpuRefId(armnn::Compute::CpuRef);
3527
3528 mockProfilingService.RegisterMapping(6, 0, cpuRefId);
3529 mockProfilingService.RegisterMapping(7, 1, cpuRefId);
3530 mockProfilingService.RegisterMapping(8, 2, cpuRefId);
3531
3532 armnn::profiling::BackendProfiling backendProfiling(options,
3533 mockProfilingService,
3534 cpuRefId);
3535
3536 armnn::profiling::Timestamp timestamp;
3537 timestamp.timestamp = 1000998;
3538 timestamp.counterValues.emplace_back(0, 700);
3539 timestamp.counterValues.emplace_back(2, 93);
3540 std::vector<armnn::profiling::Timestamp> timestamps;
3541 timestamps.push_back(timestamp);
3542 backendProfiling.ReportCounters(timestamps);
3543
3544 auto readBuffer = mockBuffer.GetReadableBuffer();
3545
3546 uint32_t headerWord0 = ReadUint32(readBuffer, 0);
3547 uint32_t headerWord1 = ReadUint32(readBuffer, 4);
3548 uint64_t readTimestamp = ReadUint64(readBuffer, 8);
3549
3550 BOOST_TEST(((headerWord0 >> 26) & 0x0000003F) == 3); // packet family
3551 BOOST_TEST(((headerWord0 >> 19) & 0x0000007F) == 0); // packet class
3552 BOOST_TEST(((headerWord0 >> 16) & 0x00000007) == 0); // packet type
3553 BOOST_TEST(headerWord1 == 20); // data length
3554 BOOST_TEST(1000998 == readTimestamp); // capture period
3555
3556 uint32_t offset = 16;
3557 // Check Counter Index
3558 uint16_t readIndex = ReadUint16(readBuffer, offset);
3559 BOOST_TEST(6 == readIndex);
3560
3561 // Check Counter Value
3562 offset += 2;
3563 uint32_t readValue = ReadUint32(readBuffer, offset);
3564 BOOST_TEST(700 == readValue);
3565
3566 // Check Counter Index
3567 offset += 4;
3568 readIndex = ReadUint16(readBuffer, offset);
3569 BOOST_TEST(8 == readIndex);
3570
3571 // Check Counter Value
3572 offset += 2;
3573 readValue = ReadUint32(readBuffer, offset);
3574 BOOST_TEST(93 == readValue);
3575}
3576
Isabella Gottardia0687ee2020-03-11 18:04:20 +00003577BOOST_AUTO_TEST_CASE(CheckFileFormat) {
3578 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
3579 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
3580
3581 // Create profiling options.
3582 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
3583 options.m_EnableProfiling = true;
3584 // Check the default value set to binary
3585 BOOST_CHECK(options.m_FileFormat == "binary");
3586
3587 // Change file format to an unsupported value
3588 options.m_FileFormat = "json";
3589 // Enable the profiling service
3590 armnn::profiling::ProfilingService profilingService;
3591 profilingService.ResetExternalProfilingOptions(options, true);
3592 // Start the command handler and the send thread
3593 profilingService.Update();
3594 BOOST_CHECK(profilingService.GetCurrentState()==ProfilingState::NotConnected);
3595
3596 // Redirect the output to a local stream so that we can parse the warning message
3597 std::stringstream ss;
3598 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3599
3600 // When Update is called and the current state is ProfilingState::NotConnected
3601 // an exception will be raised from GetProfilingConnection and displayed as warning in the output local stream
3602 profilingService.Update();
3603
3604 streamRedirector.CancelRedirect();
3605
3606 // Check that the expected error has occurred and logged to the standard output
3607 if (!boost::contains(ss.str(), "Unsupported profiling file format, only binary is supported"))
3608 {
3609 std::cout << ss.str();
3610 BOOST_FAIL("Expected string not found.");
3611 }
3612}
3613
Francis Murtagh1f7db452019-08-14 09:49:34 +01003614BOOST_AUTO_TEST_SUITE_END()