blob: a4a25a84ad37d6215a206063bfccc2f0bd0dca00 [file] [log] [blame]
Francis Murtagh1f7db452019-08-14 09:49:34 +01001//
Jim Flynn6398a982020-05-27 17:05:21 +01002// Copyright © 2019 Arm Ltd and Contributors. All rights reserved.
Francis Murtagh1f7db452019-08-14 09:49:34 +01003// SPDX-License-Identifier: MIT
4//
5
Matteo Martincighd0613b52019-10-09 16:47:04 +01006#include "ProfilingTests.hpp"
Keith Davis33ed2212020-03-30 10:43:41 +01007#include "ProfilingTestUtils.hpp"
Teresa Charlin9bab4962019-09-06 12:28:35 +01008
James Conroy2dcd3fe2020-02-06 18:34:52 +00009#include <backends/BackendProfiling.hpp>
Jim Flynnbbfe6032020-07-20 16:57:44 +010010#include <common/include/EncodeVersion.hpp>
11#include <common/include/PacketVersionResolver.hpp>
12#include <common/include/SwTrace.hpp>
Matteo Martincigh8a837172019-10-04 17:01:07 +010013#include <CommandHandler.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 <Holder.hpp>
Matteo Martincighe0e6efc2019-10-04 17:17:42 +010018#include <ICounterValues.hpp>
Francis Murtaghfcb8ef62019-09-20 15:40:09 +010019#include <PeriodicCounterCapture.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010020#include <PeriodicCounterSelectionCommandHandler.hpp>
21#include <ProfilingStateMachine.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010022#include <ProfilingUtils.hpp>
James Conroy2dcd3fe2020-02-06 18:34:52 +000023#include <RegisterBackendCounters.hpp>
Narumol Prangnawarat48033692019-09-20 12:04:55 +010024#include <RequestCounterDirectoryCommandHandler.hpp>
Teresa Charlin9bab4962019-09-06 12:28:35 +010025#include <Runtime.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010026#include <SocketProfilingConnection.hpp>
Matteo Martincighcdfb9412019-11-08 11:23:06 +000027#include <SendCounterPacket.hpp>
Sadik Armagan3896b472020-02-10 12:24:15 +000028#include <SendThread.hpp>
Matteo Martincighcdfb9412019-11-08 11:23:06 +000029#include <SendTimelinePacket.hpp>
Keith Davis02356de2019-08-26 18:28:17 +010030
Matteo Martincigh6db5f202019-09-05 12:02:04 +010031#include <armnn/Conversion.hpp>
Colm Donelan02705242019-11-14 14:19:07 +000032#include <armnn/Types.hpp>
Ferran Balaguer1b941722019-08-28 16:57:18 +010033
Matteo Martincigh54fb9572019-10-02 12:50:57 +010034#include <armnn/Utils.hpp>
Jan Eilers8eb25602020-03-09 12:13:48 +000035#include <armnn/utility/IgnoreUnused.hpp>
Matthew Sloyan371b70e2020-09-11 10:14:57 +010036#include <armnn/utility/NumericCast.hpp>
Matteo Martincigh54fb9572019-10-02 12:50:57 +010037
Jim Flynnbbfe6032020-07-20 16:57:44 +010038#include <common/include/CommandHandlerKey.hpp>
39#include <common/include/CommandHandlerRegistry.hpp>
40#include <common/include/SocketConnectionException.hpp>
41#include <common/include/Packet.hpp>
42
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 Flynnbbfe6032020-07-20 16:57:44 +010058 arm::pipe::CommandHandlerKey testKey1_0(1, 1, 1);
59 arm::pipe::CommandHandlerKey testKey1_1(1, 1, 1);
60 arm::pipe::CommandHandlerKey testKey1_2(1, 2, 1);
Jim Flynn397043f2019-10-17 17:37:10 +010061
Jim Flynnbbfe6032020-07-20 16:57:44 +010062 arm::pipe::CommandHandlerKey testKey0(0, 1, 1);
63 arm::pipe::CommandHandlerKey testKey1(0, 1, 1);
64 arm::pipe::CommandHandlerKey testKey2(0, 1, 1);
65 arm::pipe::CommandHandlerKey testKey3(0, 0, 0);
66 arm::pipe::CommandHandlerKey testKey4(0, 2, 2);
67 arm::pipe::CommandHandlerKey testKey5(0, 0, 2);
Jim Flynn397043f2019-10-17 17:37:10 +010068
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
Jim Flynnbbfe6032020-07-20 16:57:44 +010092 std::vector<arm::pipe::CommandHandlerKey> vect = {
93 arm::pipe::CommandHandlerKey(0, 0, 1), arm::pipe::CommandHandlerKey(0, 2, 0),
94 arm::pipe::CommandHandlerKey(0, 1, 0), arm::pipe::CommandHandlerKey(0, 2, 1),
95 arm::pipe::CommandHandlerKey(0, 1, 1), arm::pipe::CommandHandlerKey(0, 0, 1),
96 arm::pipe::CommandHandlerKey(0, 2, 0), arm::pipe::CommandHandlerKey(0, 0, 0) };
Francis Murtagh1f7db452019-08-14 09:49:34 +010097
98 std::sort(vect.begin(), vect.end());
99
Jim Flynnbbfe6032020-07-20 16:57:44 +0100100 std::vector<arm::pipe::CommandHandlerKey> expectedVect = {
101 arm::pipe::CommandHandlerKey(0, 0, 0), arm::pipe::CommandHandlerKey(0, 0, 1),
102 arm::pipe::CommandHandlerKey(0, 0, 1), arm::pipe::CommandHandlerKey(0, 1, 0),
103 arm::pipe::CommandHandlerKey(0, 1, 1), arm::pipe::CommandHandlerKey(0, 2, 0),
104 arm::pipe::CommandHandlerKey(0, 2, 0), arm::pipe::CommandHandlerKey(0, 2, 1) };
Francis Murtagh1f7db452019-08-14 09:49:34 +0100105
106 BOOST_CHECK(vect == expectedVect);
107}
108
Jim Flynned25e0e2019-10-18 13:21:43 +0100109BOOST_AUTO_TEST_CASE(CheckPacketKeyComparisons)
110{
Jim Flynnbbfe6032020-07-20 16:57:44 +0100111 arm::pipe::PacketKey key0(0, 0);
112 arm::pipe::PacketKey key1(0, 0);
113 arm::pipe::PacketKey key2(0, 1);
114 arm::pipe::PacketKey key3(0, 2);
115 arm::pipe::PacketKey key4(1, 0);
116 arm::pipe::PacketKey key5(1, 0);
117 arm::pipe::PacketKey key6(1, 1);
Jim Flynned25e0e2019-10-18 13:21:43 +0100118
119 BOOST_CHECK(!(key0 < key1));
120 BOOST_CHECK(!(key0 > key1));
121 BOOST_CHECK(key0 <= key1);
122 BOOST_CHECK(key0 >= key1);
123 BOOST_CHECK(key0 == key1);
124 BOOST_CHECK(key0 < key2);
125 BOOST_CHECK(key2 < key3);
126 BOOST_CHECK(key3 > key0);
127 BOOST_CHECK(key4 == key5);
128 BOOST_CHECK(key4 > key0);
129 BOOST_CHECK(key5 < key6);
130 BOOST_CHECK(key5 <= key6);
131 BOOST_CHECK(key5 != key6);
132}
133
Matteo Martincigh8a837172019-10-04 17:01:07 +0100134BOOST_AUTO_TEST_CASE(CheckCommandHandler)
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100135{
Jim Flynnbbfe6032020-07-20 16:57:44 +0100136 arm::pipe::PacketVersionResolver packetVersionResolver;
Matteo Martincigh8a837172019-10-04 17:01:07 +0100137 ProfilingStateMachine profilingStateMachine;
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100138
Matteo Martincigh8a837172019-10-04 17:01:07 +0100139 TestProfilingConnectionBase testProfilingConnectionBase;
140 TestProfilingConnectionTimeoutError testProfilingConnectionTimeOutError;
141 TestProfilingConnectionArmnnError testProfilingConnectionArmnnError;
Keith Davis3201eea2019-10-24 17:30:41 +0100142 CounterDirectory counterDirectory;
143 MockBufferManager mockBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +0000144 SendCounterPacket sendCounterPacket(mockBuffer);
145 SendThread sendThread(profilingStateMachine, mockBuffer, sendCounterPacket);
Matteo Martincighcdfb9412019-11-08 11:23:06 +0000146 SendTimelinePacket sendTimelinePacket(mockBuffer);
Jim Flynn6398a982020-05-27 17:05:21 +0100147 MockProfilingServiceStatus mockProfilingServiceStatus;
Matteo Martincighcdfb9412019-11-08 11:23:06 +0000148
Keith Davis3201eea2019-10-24 17:30:41 +0100149 ConnectionAcknowledgedCommandHandler connectionAcknowledgedCommandHandler(0, 1, 4194304, counterDirectory,
Matteo Martincighcdfb9412019-11-08 11:23:06 +0000150 sendCounterPacket, sendTimelinePacket,
Jim Flynn6398a982020-05-27 17:05:21 +0100151 profilingStateMachine,
152 mockProfilingServiceStatus);
Jim Flynnbbfe6032020-07-20 16:57:44 +0100153 arm::pipe::CommandHandlerRegistry commandHandlerRegistry;
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100154
Matteo Martincighc2728f92019-10-07 12:35:21 +0100155 commandHandlerRegistry.RegisterFunctor(&connectionAcknowledgedCommandHandler);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100156
Matteo Martincigh8a837172019-10-04 17:01:07 +0100157 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
158 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100159
Keith Davis3201eea2019-10-24 17:30:41 +0100160 CommandHandler commandHandler0(1, true, commandHandlerRegistry, packetVersionResolver);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100161
Colm Donelan2ba48d22019-11-29 09:10:59 +0000162 // This should start the command handler thread return the connection ack and put the profiling
163 // service into active state.
Matteo Martincigh8a837172019-10-04 17:01:07 +0100164 commandHandler0.Start(testProfilingConnectionBase);
Colm Donelan2ba48d22019-11-29 09:10:59 +0000165 // Try to start the send thread many times, it must only start once
Matteo Martincigh8a837172019-10-04 17:01:07 +0100166 commandHandler0.Start(testProfilingConnectionBase);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100167
Colm Donelan2ba48d22019-11-29 09:10:59 +0000168 // This could take up to 20mSec but we'll check often.
169 for (int i = 0; i < 10; i++)
Matteo Martincigh8a837172019-10-04 17:01:07 +0100170 {
171 if (profilingStateMachine.GetCurrentState() == ProfilingState::Active)
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100172 {
Matteo Martincigh8a837172019-10-04 17:01:07 +0100173 break;
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100174 }
Colm Donelan2ba48d22019-11-29 09:10:59 +0000175 std::this_thread::sleep_for(std::chrono::milliseconds(2));
Matteo Martincigh8a837172019-10-04 17:01:07 +0100176 }
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100177
Matteo Martincigh8a837172019-10-04 17:01:07 +0100178 BOOST_CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::Active);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100179
Colm Donelan2ba48d22019-11-29 09:10:59 +0000180 // Close the thread again.
181 commandHandler0.Stop();
182
183 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
184 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
185
186 // In this test we'll simulate a timeout without a connection ack packet being received.
187 // Stop after timeout is set so we expect the command handler to stop almost immediately.
188 CommandHandler commandHandler1(1, true, commandHandlerRegistry, packetVersionResolver);
189
190 commandHandler1.Start(testProfilingConnectionTimeOutError);
191 // Wait until we know a timeout exception has been sent at least once.
192 for (int i = 0; i < 10; i++)
193 {
194 if (testProfilingConnectionTimeOutError.ReadCalledCount())
195 {
196 break;
197 }
198 std::this_thread::sleep_for(std::chrono::milliseconds(2));
199 }
Colm Donelan2ba48d22019-11-29 09:10:59 +0000200
201 // The command handler loop should have stopped after the timeout.
Finn Williams09ad6f92019-12-19 17:05:18 +0000202 // wait for the timeout exception to be processed and the loop to break.
203 uint32_t timeout = 50;
204 uint32_t timeSlept = 0;
205 while (commandHandler1.IsRunning())
206 {
207 if (timeSlept >= timeout)
208 {
209 BOOST_FAIL("Timeout: The command handler loop did not stop after the timeout");
210 }
211 std::this_thread::sleep_for(std::chrono::milliseconds(1));
212 timeSlept ++;
213 }
Colm Donelan2ba48d22019-11-29 09:10:59 +0000214
215 commandHandler1.Stop();
216 // The state machine should never have received the ack so will still be in WaitingForAck.
217 BOOST_CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::WaitingForAck);
218
Finn Williams09ad6f92019-12-19 17:05:18 +0000219 // Now try sending a bad connection acknowledged packet
220 TestProfilingConnectionBadAckPacket testProfilingConnectionBadAckPacket;
221 commandHandler1.Start(testProfilingConnectionBadAckPacket);
222 commandHandler1.Stop();
223 // This should also not change the state machine
224 BOOST_CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::WaitingForAck);
225
Colm Donelan2ba48d22019-11-29 09:10:59 +0000226 // Disable stop after timeout and now commandHandler1 should persist after a timeout
227 commandHandler1.SetStopAfterTimeout(false);
228 // Restart the thread.
229 commandHandler1.Start(testProfilingConnectionTimeOutError);
230
231 // Wait for at the three timeouts and the ack to be sent.
232 for (int i = 0; i < 10; i++)
233 {
234 if (testProfilingConnectionTimeOutError.ReadCalledCount() > 3)
235 {
236 break;
237 }
238 std::this_thread::sleep_for(std::chrono::milliseconds(2));
239 }
240 commandHandler1.Stop();
241
242 // Even after the 3 exceptions the ack packet should have transitioned the command handler to active.
243 BOOST_CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::Active);
244
245 // A command handler that gets exceptions other than timeouts should keep going.
246 CommandHandler commandHandler2(1, false, commandHandlerRegistry, packetVersionResolver);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100247
Matteo Martincigh8a837172019-10-04 17:01:07 +0100248 commandHandler2.Start(testProfilingConnectionArmnnError);
249
Colm Donelan2ba48d22019-11-29 09:10:59 +0000250 // Wait for two exceptions to be thrown.
251 for (int i = 0; i < 10; i++)
252 {
253 if (testProfilingConnectionTimeOutError.ReadCalledCount() >= 2)
254 {
255 break;
256 }
257 std::this_thread::sleep_for(std::chrono::milliseconds(2));
258 }
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100259
Matteo Martincighd0613b52019-10-09 16:47:04 +0100260 BOOST_CHECK(commandHandler2.IsRunning());
Matteo Martincigh8a837172019-10-04 17:01:07 +0100261 commandHandler2.Stop();
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100262}
263
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100264BOOST_AUTO_TEST_CASE(CheckEncodeVersion)
265{
Jim Flynnbbfe6032020-07-20 16:57:44 +0100266 arm::pipe::Version version1(12);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100267
268 BOOST_CHECK(version1.GetMajor() == 0);
269 BOOST_CHECK(version1.GetMinor() == 0);
270 BOOST_CHECK(version1.GetPatch() == 12);
271
Jim Flynnbbfe6032020-07-20 16:57:44 +0100272 arm::pipe::Version version2(4108);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100273
274 BOOST_CHECK(version2.GetMajor() == 0);
275 BOOST_CHECK(version2.GetMinor() == 1);
276 BOOST_CHECK(version2.GetPatch() == 12);
277
Jim Flynnbbfe6032020-07-20 16:57:44 +0100278 arm::pipe::Version version3(4198412);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100279
280 BOOST_CHECK(version3.GetMajor() == 1);
281 BOOST_CHECK(version3.GetMinor() == 1);
282 BOOST_CHECK(version3.GetPatch() == 12);
283
Jim Flynnbbfe6032020-07-20 16:57:44 +0100284 arm::pipe::Version version4(0);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100285
286 BOOST_CHECK(version4.GetMajor() == 0);
287 BOOST_CHECK(version4.GetMinor() == 0);
288 BOOST_CHECK(version4.GetPatch() == 0);
289
Jim Flynnbbfe6032020-07-20 16:57:44 +0100290 arm::pipe::Version version5(1, 0, 0);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100291 BOOST_CHECK(version5.GetEncodedValue() == 4194304);
292}
293
Nikhil Rajbc626052019-08-15 15:49:45 +0100294BOOST_AUTO_TEST_CASE(CheckPacketClass)
295{
Keith Davis3201eea2019-10-24 17:30:41 +0100296 uint32_t length = 4;
Matteo Martincigh67ef2a52019-10-10 13:29:02 +0100297 std::unique_ptr<unsigned char[]> packetData0 = std::make_unique<unsigned char[]>(length);
298 std::unique_ptr<unsigned char[]> packetData1 = std::make_unique<unsigned char[]>(0);
299 std::unique_ptr<unsigned char[]> nullPacketData;
Nikhil Rajbc626052019-08-15 15:49:45 +0100300
Jim Flynnbbfe6032020-07-20 16:57:44 +0100301 arm::pipe::Packet packetTest0(472580096, length, packetData0);
Nikhil Rajbc626052019-08-15 15:49:45 +0100302
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100303 BOOST_CHECK(packetTest0.GetHeader() == 472580096);
304 BOOST_CHECK(packetTest0.GetPacketFamily() == 7);
305 BOOST_CHECK(packetTest0.GetPacketId() == 43);
306 BOOST_CHECK(packetTest0.GetLength() == length);
307 BOOST_CHECK(packetTest0.GetPacketType() == 3);
308 BOOST_CHECK(packetTest0.GetPacketClass() == 5);
Nikhil Rajbc626052019-08-15 15:49:45 +0100309
Jim Flynnbbfe6032020-07-20 16:57:44 +0100310 BOOST_CHECK_THROW(arm::pipe::Packet packetTest1(472580096, 0, packetData1), arm::pipe::InvalidArgumentException);
311 BOOST_CHECK_NO_THROW(arm::pipe::Packet packetTest2(472580096, 0, nullPacketData));
Nikhil Rajbc626052019-08-15 15:49:45 +0100312
Jim Flynnbbfe6032020-07-20 16:57:44 +0100313 arm::pipe::Packet packetTest3(472580096, 0, nullPacketData);
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100314 BOOST_CHECK(packetTest3.GetLength() == 0);
315 BOOST_CHECK(packetTest3.GetData() == nullptr);
316
Matteo Martincigh67ef2a52019-10-10 13:29:02 +0100317 const unsigned char* packetTest0Data = packetTest0.GetData();
Jim Flynnbbfe6032020-07-20 16:57:44 +0100318 arm::pipe::Packet packetTest4(std::move(packetTest0));
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100319
320 BOOST_CHECK(packetTest0.GetData() == nullptr);
321 BOOST_CHECK(packetTest4.GetData() == packetTest0Data);
322
323 BOOST_CHECK(packetTest4.GetHeader() == 472580096);
324 BOOST_CHECK(packetTest4.GetPacketFamily() == 7);
325 BOOST_CHECK(packetTest4.GetPacketId() == 43);
326 BOOST_CHECK(packetTest4.GetLength() == length);
327 BOOST_CHECK(packetTest4.GetPacketType() == 3);
328 BOOST_CHECK(packetTest4.GetPacketClass() == 5);
Nikhil Rajbc626052019-08-15 15:49:45 +0100329}
330
Francis Murtagh11f99b42019-08-16 11:28:52 +0100331BOOST_AUTO_TEST_CASE(CheckCommandHandlerFunctor)
332{
Francis Murtagh11f99b42019-08-16 11:28:52 +0100333 // Hard code the version as it will be the same during a single profiling session
334 uint32_t version = 1;
335
Jim Flynn397043f2019-10-17 17:37:10 +0100336 TestFunctorA testFunctorA(7, 461, version);
337 TestFunctorB testFunctorB(8, 963, version);
338 TestFunctorC testFunctorC(5, 983, version);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100339
Jim Flynnbbfe6032020-07-20 16:57:44 +0100340 arm::pipe::CommandHandlerKey keyA(
341 testFunctorA.GetFamilyId(), testFunctorA.GetPacketId(), testFunctorA.GetVersion());
342 arm::pipe::CommandHandlerKey keyB(
343 testFunctorB.GetFamilyId(), testFunctorB.GetPacketId(), testFunctorB.GetVersion());
344 arm::pipe::CommandHandlerKey keyC(
345 testFunctorC.GetFamilyId(), testFunctorC.GetPacketId(), testFunctorC.GetVersion());
Francis Murtagh11f99b42019-08-16 11:28:52 +0100346
347 // Create the unwrapped map to simulate the Command Handler Registry
Jim Flynnbbfe6032020-07-20 16:57:44 +0100348 std::map<arm::pipe::CommandHandlerKey, arm::pipe::CommandHandlerFunctor*> registry;
Francis Murtagh11f99b42019-08-16 11:28:52 +0100349
350 registry.insert(std::make_pair(keyB, &testFunctorB));
351 registry.insert(std::make_pair(keyA, &testFunctorA));
352 registry.insert(std::make_pair(keyC, &testFunctorC));
353
354 // Check the order of the map is correct
355 auto it = registry.begin();
Keith Davis3201eea2019-10-24 17:30:41 +0100356 BOOST_CHECK(it->first == keyC); // familyId == 5
Francis Murtagh11f99b42019-08-16 11:28:52 +0100357 it++;
Keith Davis3201eea2019-10-24 17:30:41 +0100358 BOOST_CHECK(it->first == keyA); // familyId == 7
Francis Murtagh11f99b42019-08-16 11:28:52 +0100359 it++;
Keith Davis3201eea2019-10-24 17:30:41 +0100360 BOOST_CHECK(it->first == keyB); // familyId == 8
Francis Murtagh11f99b42019-08-16 11:28:52 +0100361
Matteo Martincigh67ef2a52019-10-10 13:29:02 +0100362 std::unique_ptr<unsigned char[]> packetDataA;
363 std::unique_ptr<unsigned char[]> packetDataB;
364 std::unique_ptr<unsigned char[]> packetDataC;
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100365
Jim Flynnbbfe6032020-07-20 16:57:44 +0100366 arm::pipe::Packet packetA(500000000, 0, packetDataA);
367 arm::pipe::Packet packetB(600000000, 0, packetDataB);
368 arm::pipe::Packet packetC(400000000, 0, packetDataC);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100369
370 // Check the correct operator of derived class is called
Jim Flynnbbfe6032020-07-20 16:57:44 +0100371 registry.at(arm::pipe::CommandHandlerKey(
372 packetA.GetPacketFamily(), packetA.GetPacketId(), version))->operator()(packetA);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100373 BOOST_CHECK(testFunctorA.GetCount() == 1);
374 BOOST_CHECK(testFunctorB.GetCount() == 0);
375 BOOST_CHECK(testFunctorC.GetCount() == 0);
376
Jim Flynnbbfe6032020-07-20 16:57:44 +0100377 registry.at(arm::pipe::CommandHandlerKey(
378 packetB.GetPacketFamily(), packetB.GetPacketId(), version))->operator()(packetB);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100379 BOOST_CHECK(testFunctorA.GetCount() == 1);
380 BOOST_CHECK(testFunctorB.GetCount() == 1);
381 BOOST_CHECK(testFunctorC.GetCount() == 0);
382
Jim Flynnbbfe6032020-07-20 16:57:44 +0100383 registry.at(arm::pipe::CommandHandlerKey(
384 packetC.GetPacketFamily(), packetC.GetPacketId(), version))->operator()(packetC);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100385 BOOST_CHECK(testFunctorA.GetCount() == 1);
386 BOOST_CHECK(testFunctorB.GetCount() == 1);
387 BOOST_CHECK(testFunctorC.GetCount() == 1);
388}
389
Francis Murtagh94d79152019-08-16 17:45:07 +0100390BOOST_AUTO_TEST_CASE(CheckCommandHandlerRegistry)
391{
392 // Hard code the version as it will be the same during a single profiling session
393 uint32_t version = 1;
394
Jim Flynn397043f2019-10-17 17:37:10 +0100395 TestFunctorA testFunctorA(7, 461, version);
396 TestFunctorB testFunctorB(8, 963, version);
397 TestFunctorC testFunctorC(5, 983, version);
Francis Murtagh94d79152019-08-16 17:45:07 +0100398
399 // Create the Command Handler Registry
Jim Flynnbbfe6032020-07-20 16:57:44 +0100400 arm::pipe::CommandHandlerRegistry registry;
Francis Murtagh94d79152019-08-16 17:45:07 +0100401
402 // Register multiple different derived classes
Matteo Martincighc2728f92019-10-07 12:35:21 +0100403 registry.RegisterFunctor(&testFunctorA);
404 registry.RegisterFunctor(&testFunctorB);
405 registry.RegisterFunctor(&testFunctorC);
Francis Murtagh94d79152019-08-16 17:45:07 +0100406
Matteo Martincigh67ef2a52019-10-10 13:29:02 +0100407 std::unique_ptr<unsigned char[]> packetDataA;
408 std::unique_ptr<unsigned char[]> packetDataB;
409 std::unique_ptr<unsigned char[]> packetDataC;
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100410
Jim Flynnbbfe6032020-07-20 16:57:44 +0100411 arm::pipe::Packet packetA(500000000, 0, packetDataA);
412 arm::pipe::Packet packetB(600000000, 0, packetDataB);
413 arm::pipe::Packet packetC(400000000, 0, packetDataC);
Francis Murtagh94d79152019-08-16 17:45:07 +0100414
415 // Check the correct operator of derived class is called
Jim Flynn397043f2019-10-17 17:37:10 +0100416 registry.GetFunctor(packetA.GetPacketFamily(), packetA.GetPacketId(), version)->operator()(packetA);
Francis Murtagh94d79152019-08-16 17:45:07 +0100417 BOOST_CHECK(testFunctorA.GetCount() == 1);
418 BOOST_CHECK(testFunctorB.GetCount() == 0);
419 BOOST_CHECK(testFunctorC.GetCount() == 0);
420
Jim Flynn397043f2019-10-17 17:37:10 +0100421 registry.GetFunctor(packetB.GetPacketFamily(), packetB.GetPacketId(), version)->operator()(packetB);
Francis Murtagh94d79152019-08-16 17:45:07 +0100422 BOOST_CHECK(testFunctorA.GetCount() == 1);
423 BOOST_CHECK(testFunctorB.GetCount() == 1);
424 BOOST_CHECK(testFunctorC.GetCount() == 0);
425
Jim Flynn397043f2019-10-17 17:37:10 +0100426 registry.GetFunctor(packetC.GetPacketFamily(), packetC.GetPacketId(), version)->operator()(packetC);
Francis Murtagh94d79152019-08-16 17:45:07 +0100427 BOOST_CHECK(testFunctorA.GetCount() == 1);
428 BOOST_CHECK(testFunctorB.GetCount() == 1);
429 BOOST_CHECK(testFunctorC.GetCount() == 1);
430
431 // Re-register an existing key with a new function
Jim Flynn397043f2019-10-17 17:37:10 +0100432 registry.RegisterFunctor(&testFunctorC, testFunctorA.GetFamilyId(), testFunctorA.GetPacketId(), version);
433 registry.GetFunctor(packetA.GetPacketFamily(), packetA.GetPacketId(), version)->operator()(packetC);
Francis Murtagh94d79152019-08-16 17:45:07 +0100434 BOOST_CHECK(testFunctorA.GetCount() == 1);
435 BOOST_CHECK(testFunctorB.GetCount() == 1);
436 BOOST_CHECK(testFunctorC.GetCount() == 2);
437
438 // Check that non-existent key returns nullptr for its functor
Jim Flynnbbfe6032020-07-20 16:57:44 +0100439 BOOST_CHECK_THROW(registry.GetFunctor(0, 0, 0), arm::pipe::ProfilingException);
Francis Murtagh94d79152019-08-16 17:45:07 +0100440}
441
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100442BOOST_AUTO_TEST_CASE(CheckPacketVersionResolver)
443{
444 // Set up random number generator for generating packetId values
445 std::random_device device;
446 std::mt19937 generator(device());
447 std::uniform_int_distribution<uint32_t> distribution(std::numeric_limits<uint32_t>::min(),
448 std::numeric_limits<uint32_t>::max());
449
450 // NOTE: Expected version is always 1.0.0, regardless of packetId
Jim Flynnbbfe6032020-07-20 16:57:44 +0100451 const arm::pipe::Version expectedVersion(1, 0, 0);
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100452
Jim Flynnbbfe6032020-07-20 16:57:44 +0100453 arm::pipe::PacketVersionResolver packetVersionResolver;
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100454
455 constexpr unsigned int numTests = 10u;
456
457 for (unsigned int i = 0u; i < numTests; ++i)
458 {
Jim Flynned25e0e2019-10-18 13:21:43 +0100459 const uint32_t familyId = distribution(generator);
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100460 const uint32_t packetId = distribution(generator);
Jim Flynnbbfe6032020-07-20 16:57:44 +0100461 arm::pipe::Version resolvedVersion = packetVersionResolver.ResolvePacketVersion(familyId, packetId);
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100462
463 BOOST_TEST(resolvedVersion == expectedVersion);
464 }
465}
Matteo Martincighd0613b52019-10-09 16:47:04 +0100466
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100467void ProfilingCurrentStateThreadImpl(ProfilingStateMachine& states)
468{
469 ProfilingState newState = ProfilingState::NotConnected;
470 states.GetCurrentState();
471 states.TransitionToState(newState);
472}
473
474BOOST_AUTO_TEST_CASE(CheckProfilingStateMachine)
475{
476 ProfilingStateMachine profilingState1(ProfilingState::Uninitialised);
477 profilingState1.TransitionToState(ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +0100478 BOOST_CHECK(profilingState1.GetCurrentState() == ProfilingState::Uninitialised);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100479
480 ProfilingStateMachine profilingState2(ProfilingState::Uninitialised);
481 profilingState2.TransitionToState(ProfilingState::NotConnected);
482 BOOST_CHECK(profilingState2.GetCurrentState() == ProfilingState::NotConnected);
483
484 ProfilingStateMachine profilingState3(ProfilingState::NotConnected);
485 profilingState3.TransitionToState(ProfilingState::NotConnected);
486 BOOST_CHECK(profilingState3.GetCurrentState() == ProfilingState::NotConnected);
487
488 ProfilingStateMachine profilingState4(ProfilingState::NotConnected);
489 profilingState4.TransitionToState(ProfilingState::WaitingForAck);
490 BOOST_CHECK(profilingState4.GetCurrentState() == ProfilingState::WaitingForAck);
491
492 ProfilingStateMachine profilingState5(ProfilingState::WaitingForAck);
493 profilingState5.TransitionToState(ProfilingState::WaitingForAck);
494 BOOST_CHECK(profilingState5.GetCurrentState() == ProfilingState::WaitingForAck);
495
496 ProfilingStateMachine profilingState6(ProfilingState::WaitingForAck);
497 profilingState6.TransitionToState(ProfilingState::Active);
498 BOOST_CHECK(profilingState6.GetCurrentState() == ProfilingState::Active);
499
500 ProfilingStateMachine profilingState7(ProfilingState::Active);
501 profilingState7.TransitionToState(ProfilingState::NotConnected);
502 BOOST_CHECK(profilingState7.GetCurrentState() == ProfilingState::NotConnected);
503
504 ProfilingStateMachine profilingState8(ProfilingState::Active);
505 profilingState8.TransitionToState(ProfilingState::Active);
506 BOOST_CHECK(profilingState8.GetCurrentState() == ProfilingState::Active);
507
508 ProfilingStateMachine profilingState9(ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +0100509 BOOST_CHECK_THROW(profilingState9.TransitionToState(ProfilingState::WaitingForAck), armnn::Exception);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100510
511 ProfilingStateMachine profilingState10(ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +0100512 BOOST_CHECK_THROW(profilingState10.TransitionToState(ProfilingState::Active), armnn::Exception);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100513
514 ProfilingStateMachine profilingState11(ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +0100515 BOOST_CHECK_THROW(profilingState11.TransitionToState(ProfilingState::Uninitialised), armnn::Exception);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100516
517 ProfilingStateMachine profilingState12(ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +0100518 BOOST_CHECK_THROW(profilingState12.TransitionToState(ProfilingState::Active), armnn::Exception);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100519
520 ProfilingStateMachine profilingState13(ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +0100521 BOOST_CHECK_THROW(profilingState13.TransitionToState(ProfilingState::Uninitialised), armnn::Exception);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100522
523 ProfilingStateMachine profilingState14(ProfilingState::WaitingForAck);
Jim Flynn53e46992019-10-14 12:31:10 +0100524 profilingState14.TransitionToState(ProfilingState::NotConnected);
525 BOOST_CHECK(profilingState14.GetCurrentState() == ProfilingState::NotConnected);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100526
527 ProfilingStateMachine profilingState15(ProfilingState::Active);
Keith Davis3201eea2019-10-24 17:30:41 +0100528 BOOST_CHECK_THROW(profilingState15.TransitionToState(ProfilingState::Uninitialised), armnn::Exception);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100529
530 ProfilingStateMachine profilingState16(armnn::profiling::ProfilingState::Active);
Keith Davis3201eea2019-10-24 17:30:41 +0100531 BOOST_CHECK_THROW(profilingState16.TransitionToState(ProfilingState::WaitingForAck), armnn::Exception);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100532
533 ProfilingStateMachine profilingState17(ProfilingState::Uninitialised);
534
Keith Davis3201eea2019-10-24 17:30:41 +0100535 std::thread thread1(ProfilingCurrentStateThreadImpl, std::ref(profilingState17));
536 std::thread thread2(ProfilingCurrentStateThreadImpl, std::ref(profilingState17));
537 std::thread thread3(ProfilingCurrentStateThreadImpl, std::ref(profilingState17));
538 std::thread thread4(ProfilingCurrentStateThreadImpl, std::ref(profilingState17));
539 std::thread thread5(ProfilingCurrentStateThreadImpl, std::ref(profilingState17));
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100540
541 thread1.join();
542 thread2.join();
543 thread3.join();
544 thread4.join();
545 thread5.join();
546
547 BOOST_TEST((profilingState17.GetCurrentState() == ProfilingState::NotConnected));
548}
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100549
Jim Flynn8355ec92019-09-17 12:29:50 +0100550void CaptureDataWriteThreadImpl(Holder& holder, uint32_t capturePeriod, const std::vector<uint16_t>& counterIds)
Francis Murtagh68f78d82019-09-04 16:42:29 +0100551{
Finn Williams032bc742020-02-12 11:02:34 +0000552 holder.SetCaptureData(capturePeriod, counterIds, {});
Francis Murtagh68f78d82019-09-04 16:42:29 +0100553}
554
Francis Murtaghbd707162019-09-09 11:26:44 +0100555void CaptureDataReadThreadImpl(const Holder& holder, CaptureData& captureData)
Francis Murtagh68f78d82019-09-04 16:42:29 +0100556{
557 captureData = holder.GetCaptureData();
558}
559
560BOOST_AUTO_TEST_CASE(CheckCaptureDataHolder)
561{
Francis Murtaghbd707162019-09-09 11:26:44 +0100562 std::map<uint32_t, std::vector<uint16_t>> periodIdMap;
563 std::vector<uint16_t> counterIds;
Jim Flynn8355ec92019-09-17 12:29:50 +0100564 uint32_t numThreads = 10;
565 for (uint32_t i = 0; i < numThreads; ++i)
Francis Murtaghbd707162019-09-09 11:26:44 +0100566 {
567 counterIds.emplace_back(i);
568 periodIdMap.insert(std::make_pair(i, counterIds));
569 }
Francis Murtagh68f78d82019-09-04 16:42:29 +0100570
Jim Flynn8355ec92019-09-17 12:29:50 +0100571 // Verify the read and write threads set the holder correctly
572 // and retrieve the expected values
Francis Murtagh68f78d82019-09-04 16:42:29 +0100573 Holder holder;
574 BOOST_CHECK((holder.GetCaptureData()).GetCapturePeriod() == 0);
575 BOOST_CHECK(((holder.GetCaptureData()).GetCounterIds()).empty());
576
577 // Check Holder functions
Francis Murtaghbd707162019-09-09 11:26:44 +0100578 std::thread thread1(CaptureDataWriteThreadImpl, std::ref(holder), 2, std::ref(periodIdMap[2]));
Francis Murtagh68f78d82019-09-04 16:42:29 +0100579 thread1.join();
Francis Murtaghbd707162019-09-09 11:26:44 +0100580 BOOST_CHECK((holder.GetCaptureData()).GetCapturePeriod() == 2);
581 BOOST_CHECK((holder.GetCaptureData()).GetCounterIds() == periodIdMap[2]);
Jim Flynn8355ec92019-09-17 12:29:50 +0100582 // NOTE: now that we have some initial values in the holder we don't have to worry
583 // in the multi-threaded section below about a read thread accessing the holder
584 // before any write thread has gotten to it so we read period = 0, counterIds empty
585 // instead of period = 0, counterIds = {0} as will the case when write thread 0
586 // has executed.
Francis Murtagh68f78d82019-09-04 16:42:29 +0100587
588 CaptureData captureData;
589 std::thread thread2(CaptureDataReadThreadImpl, std::ref(holder), std::ref(captureData));
590 thread2.join();
Jim Flynn8355ec92019-09-17 12:29:50 +0100591 BOOST_CHECK(captureData.GetCapturePeriod() == 2);
Francis Murtaghbd707162019-09-09 11:26:44 +0100592 BOOST_CHECK(captureData.GetCounterIds() == periodIdMap[2]);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100593
Jim Flynn8355ec92019-09-17 12:29:50 +0100594 std::map<uint32_t, CaptureData> captureDataIdMap;
595 for (uint32_t i = 0; i < numThreads; ++i)
596 {
597 CaptureData perThreadCaptureData;
598 captureDataIdMap.insert(std::make_pair(i, perThreadCaptureData));
599 }
600
Francis Murtaghbd707162019-09-09 11:26:44 +0100601 std::vector<std::thread> threadsVect;
Jim Flynn8355ec92019-09-17 12:29:50 +0100602 std::vector<std::thread> readThreadsVect;
603 for (uint32_t i = 0; i < numThreads; ++i)
Francis Murtaghbd707162019-09-09 11:26:44 +0100604 {
Keith Davis3201eea2019-10-24 17:30:41 +0100605 threadsVect.emplace_back(
606 std::thread(CaptureDataWriteThreadImpl, std::ref(holder), i, std::ref(periodIdMap[i])));
Francis Murtagh06965692019-09-05 16:29:01 +0100607
Jim Flynn8355ec92019-09-17 12:29:50 +0100608 // Verify that the CaptureData goes into the thread in a virgin state
609 BOOST_CHECK(captureDataIdMap.at(i).GetCapturePeriod() == 0);
610 BOOST_CHECK(captureDataIdMap.at(i).GetCounterIds().empty());
Keith Davis3201eea2019-10-24 17:30:41 +0100611 readThreadsVect.emplace_back(
612 std::thread(CaptureDataReadThreadImpl, std::ref(holder), std::ref(captureDataIdMap.at(i))));
Francis Murtaghbd707162019-09-09 11:26:44 +0100613 }
614
Jim Flynn8355ec92019-09-17 12:29:50 +0100615 for (uint32_t i = 0; i < numThreads; ++i)
Francis Murtaghbd707162019-09-09 11:26:44 +0100616 {
617 threadsVect[i].join();
Francis Murtaghbd707162019-09-09 11:26:44 +0100618 readThreadsVect[i].join();
619 }
Francis Murtagh68f78d82019-09-04 16:42:29 +0100620
Jim Flynn8355ec92019-09-17 12:29:50 +0100621 // Look at the CaptureData that each read thread has filled
622 // the capture period it read should match the counter ids entry
623 for (uint32_t i = 0; i < numThreads; ++i)
624 {
625 CaptureData perThreadCaptureData = captureDataIdMap.at(i);
626 BOOST_CHECK(perThreadCaptureData.GetCounterIds() == periodIdMap.at(perThreadCaptureData.GetCapturePeriod()));
627 }
Matthew Bentham46d1c622019-09-13 12:45:04 +0100628}
Francis Murtagh68f78d82019-09-04 16:42:29 +0100629
Matthew Bentham46d1c622019-09-13 12:45:04 +0100630BOOST_AUTO_TEST_CASE(CaptureDataMethods)
631{
Jim Flynn8355ec92019-09-17 12:29:50 +0100632 // Check CaptureData setter and getter functions
Keith Davis3201eea2019-10-24 17:30:41 +0100633 std::vector<uint16_t> counterIds = { 42, 29, 13 };
Jim Flynn8355ec92019-09-17 12:29:50 +0100634 CaptureData captureData;
635 BOOST_CHECK(captureData.GetCapturePeriod() == 0);
636 BOOST_CHECK((captureData.GetCounterIds()).empty());
637 captureData.SetCapturePeriod(150);
638 captureData.SetCounterIds(counterIds);
639 BOOST_CHECK(captureData.GetCapturePeriod() == 150);
640 BOOST_CHECK(captureData.GetCounterIds() == counterIds);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100641
Jim Flynn8355ec92019-09-17 12:29:50 +0100642 // Check assignment operator
Francis Murtagh68f78d82019-09-04 16:42:29 +0100643 CaptureData secondCaptureData;
Francis Murtagh68f78d82019-09-04 16:42:29 +0100644
Jim Flynn8355ec92019-09-17 12:29:50 +0100645 secondCaptureData = captureData;
646 BOOST_CHECK(secondCaptureData.GetCapturePeriod() == 150);
Matthew Bentham46d1c622019-09-13 12:45:04 +0100647 BOOST_CHECK(secondCaptureData.GetCounterIds() == counterIds);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100648
649 // Check copy constructor
Jim Flynn8355ec92019-09-17 12:29:50 +0100650 CaptureData copyConstructedCaptureData(captureData);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100651
Jim Flynn8355ec92019-09-17 12:29:50 +0100652 BOOST_CHECK(copyConstructedCaptureData.GetCapturePeriod() == 150);
Matthew Bentham46d1c622019-09-13 12:45:04 +0100653 BOOST_CHECK(copyConstructedCaptureData.GetCounterIds() == counterIds);
Keith Davis02356de2019-08-26 18:28:17 +0100654}
Francis Murtagh68f78d82019-09-04 16:42:29 +0100655
Keith Davis02356de2019-08-26 18:28:17 +0100656BOOST_AUTO_TEST_CASE(CheckProfilingServiceDisabled)
657{
Kevin Mayd92a6e42021-02-04 10:27:41 +0000658 armnn::IRuntime::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);
661 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100662 profilingService.Update();
Matteo Martincigha84edee2019-10-02 12:50:57 +0100663 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis02356de2019-08-26 18:28:17 +0100664}
665
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100666BOOST_AUTO_TEST_CASE(CheckProfilingServiceCounterDirectory)
667{
Kevin Mayd92a6e42021-02-04 10:27:41 +0000668 armnn::IRuntime::CreationOptions::ExternalProfilingOptions options;
Sadik Armagan3184c902020-03-18 10:57:30 +0000669 armnn::profiling::ProfilingService profilingService;
Matteo Martincigha84edee2019-10-02 12:50:57 +0100670 profilingService.ResetExternalProfilingOptions(options, true);
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100671
Matteo Martincigha84edee2019-10-02 12:50:57 +0100672 const ICounterDirectory& counterDirectory0 = profilingService.GetCounterDirectory();
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100673 BOOST_CHECK(counterDirectory0.GetCounterCount() == 0);
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100674 profilingService.Update();
675 BOOST_CHECK(counterDirectory0.GetCounterCount() == 0);
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100676
677 options.m_EnableProfiling = true;
Matteo Martincigha84edee2019-10-02 12:50:57 +0100678 profilingService.ResetExternalProfilingOptions(options);
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100679
Matteo Martincigha84edee2019-10-02 12:50:57 +0100680 const ICounterDirectory& counterDirectory1 = profilingService.GetCounterDirectory();
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100681 BOOST_CHECK(counterDirectory1.GetCounterCount() == 0);
682 profilingService.Update();
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100683 BOOST_CHECK(counterDirectory1.GetCounterCount() != 0);
Colm Donelan2ba48d22019-11-29 09:10:59 +0000684 // Reset the profiling service to stop any running thread
685 options.m_EnableProfiling = false;
686 profilingService.ResetExternalProfilingOptions(options, true);
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100687}
688
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100689BOOST_AUTO_TEST_CASE(CheckProfilingServiceCounterValues)
690{
Kevin Mayd92a6e42021-02-04 10:27:41 +0000691 armnn::IRuntime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +0100692 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +0000693 armnn::profiling::ProfilingService profilingService;
Matteo Martincigha84edee2019-10-02 12:50:57 +0100694 profilingService.ResetExternalProfilingOptions(options, true);
695
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100696 profilingService.Update();
Matteo Martincigha84edee2019-10-02 12:50:57 +0100697 const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
Keith Davis3201eea2019-10-24 17:30:41 +0100698 const Counters& counters = counterDirectory.GetCounters();
Matteo Martincigha84edee2019-10-02 12:50:57 +0100699 BOOST_CHECK(!counters.empty());
700
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100701 std::vector<std::thread> writers;
702
Finn Williamsf3fcf322020-05-11 14:38:02 +0100703 BOOST_CHECK(!counters.empty());
704
705 // Test GetAbsoluteCounterValue
706 for (int i = 0; i < 4; ++i)
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100707 {
Finn Williamsf3fcf322020-05-11 14:38:02 +0100708 // Increment and decrement the INFERENCES_RUN counter 250 times
709 writers.push_back(std::thread([&profilingService]()
710 {
711 for (int i = 0; i < 250; ++i)
712 {
713 profilingService.IncrementCounterValue(INFERENCES_RUN);
714 }
715 }));
716 // Add 10 to the INFERENCES_RUN counter 200 times
717 writers.push_back(std::thread([&profilingService]()
718 {
719 for (int i = 0; i < 200; ++i)
720 {
721 profilingService.AddCounterValue(INFERENCES_RUN, 10);
722 }
723 }));
724 // Subtract 5 from the INFERENCES_RUN counter 200 times
725 writers.push_back(std::thread([&profilingService]()
726 {
727 for (int i = 0; i < 200; ++i)
728 {
729 profilingService.SubtractCounterValue(INFERENCES_RUN, 5);
730 }
731 }));
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100732 }
Finn Williamsf3fcf322020-05-11 14:38:02 +0100733
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100734 std::for_each(writers.begin(), writers.end(), mem_fn(&std::thread::join));
735
Finn Williamsf3fcf322020-05-11 14:38:02 +0100736 uint32_t absoluteCounterValue = 0;
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100737
Finn Williamsf3fcf322020-05-11 14:38:02 +0100738 BOOST_CHECK_NO_THROW(absoluteCounterValue = profilingService.GetAbsoluteCounterValue(INFERENCES_RUN));
739 BOOST_CHECK(absoluteCounterValue = 5000);
740
741 // Test SetCounterValue
742 BOOST_CHECK_NO_THROW(profilingService.SetCounterValue(INFERENCES_RUN, 0));
743 BOOST_CHECK_NO_THROW(absoluteCounterValue = profilingService.GetAbsoluteCounterValue(INFERENCES_RUN));
744 BOOST_CHECK(absoluteCounterValue == 0);
745
746 // Test GetDeltaCounterValue
747 writers.clear();
748 uint32_t deltaCounterValue = 0;
749 //Start a reading thread to randomly read the INFERENCES_RUN counter value
750 std::thread reader([&profilingService](uint32_t& deltaCounterValue)
751 {
752 for (int i = 0; i < 300; ++i)
753 {
754 deltaCounterValue += profilingService.GetDeltaCounterValue(INFERENCES_RUN);
755 }
756 }, std::ref(deltaCounterValue));
757
758 for (int i = 0; i < 4; ++i)
759 {
760 // Increment and decrement the INFERENCES_RUN counter 250 times
761 writers.push_back(std::thread([&profilingService]()
762 {
763 for (int i = 0; i < 250; ++i)
764 {
765 profilingService.IncrementCounterValue(INFERENCES_RUN);
766 }
767 }));
768 // Add 10 to the INFERENCES_RUN counter 200 times
769 writers.push_back(std::thread([&profilingService]()
770 {
771 for (int i = 0; i < 200; ++i)
772 {
773 profilingService.AddCounterValue(INFERENCES_RUN, 10);
774 }
775 }));
776 // Subtract 5 from the INFERENCES_RUN counter 200 times
777 writers.push_back(std::thread([&profilingService]()
778 {
779 for (int i = 0; i < 200; ++i)
780 {
781 profilingService.SubtractCounterValue(INFERENCES_RUN, 5);
782 }
783 }));
784 }
785
786 std::for_each(writers.begin(), writers.end(), mem_fn(&std::thread::join));
787 reader.join();
788
789 // Do one last read in case the reader stopped early
790 deltaCounterValue += profilingService.GetDeltaCounterValue(INFERENCES_RUN);
791 BOOST_CHECK(deltaCounterValue == 5000);
792
Colm Donelan2ba48d22019-11-29 09:10:59 +0000793 // Reset the profiling service to stop any running thread
794 options.m_EnableProfiling = false;
795 profilingService.ResetExternalProfilingOptions(options, true);
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100796}
797
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100798BOOST_AUTO_TEST_CASE(CheckProfilingObjectUids)
Matteo Martincighab173e92019-09-05 12:02:04 +0100799{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100800 uint16_t uid = 0;
801 BOOST_CHECK_NO_THROW(uid = GetNextUid());
802 BOOST_CHECK(uid >= 1);
803
804 uint16_t nextUid = 0;
805 BOOST_CHECK_NO_THROW(nextUid = GetNextUid());
806 BOOST_CHECK(nextUid > uid);
807
808 std::vector<uint16_t> counterUids;
Keith Davise394bd92019-12-02 15:12:19 +0000809 BOOST_CHECK_NO_THROW(counterUids = GetNextCounterUids(uid,0));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100810 BOOST_CHECK(counterUids.size() == 1);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100811
812 std::vector<uint16_t> nextCounterUids;
Keith Davise394bd92019-12-02 15:12:19 +0000813 BOOST_CHECK_NO_THROW(nextCounterUids = GetNextCounterUids(nextUid, 2));
814 BOOST_CHECK(nextCounterUids.size() == 2);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100815 BOOST_CHECK(nextCounterUids[0] > counterUids[0]);
816
817 std::vector<uint16_t> counterUidsMultiCore;
Francis Murtagh1e5afee2021-05-11 09:37:47 +0100818 uint16_t thirdUid = nextCounterUids[0];
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100819 uint16_t numberOfCores = 13;
Keith Davise394bd92019-12-02 15:12:19 +0000820 BOOST_CHECK_NO_THROW(counterUidsMultiCore = GetNextCounterUids(thirdUid, numberOfCores));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100821 BOOST_CHECK(counterUidsMultiCore.size() == numberOfCores);
822 BOOST_CHECK(counterUidsMultiCore.front() >= nextCounterUids[0]);
Keith Davis3201eea2019-10-24 17:30:41 +0100823 for (size_t i = 1; i < numberOfCores; i++)
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100824 {
825 BOOST_CHECK(counterUidsMultiCore[i] == counterUidsMultiCore[i - 1] + 1);
826 }
827 BOOST_CHECK(counterUidsMultiCore.back() == counterUidsMultiCore.front() + numberOfCores - 1);
Matteo Martincighab173e92019-09-05 12:02:04 +0100828}
829
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100830BOOST_AUTO_TEST_CASE(CheckCounterDirectoryRegisterCategory)
Matteo Martincighab173e92019-09-05 12:02:04 +0100831{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100832 CounterDirectory counterDirectory;
Keith Davis3201eea2019-10-24 17:30:41 +0100833 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
834 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100835 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
Keith Davis3201eea2019-10-24 17:30:41 +0100836 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
Matteo Martincighab173e92019-09-05 12:02:04 +0100837
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100838 // Register a category with an invalid name
839 const Category* noCategory = nullptr;
840 BOOST_CHECK_THROW(noCategory = counterDirectory.RegisterCategory(""), armnn::InvalidArgumentException);
841 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
842 BOOST_CHECK(!noCategory);
Matteo Martincighab173e92019-09-05 12:02:04 +0100843
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100844 // Register a category with an invalid name
845 BOOST_CHECK_THROW(noCategory = counterDirectory.RegisterCategory("invalid category"),
846 armnn::InvalidArgumentException);
847 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
848 BOOST_CHECK(!noCategory);
849
850 // Register a new category
851 const std::string categoryName = "some_category";
Keith Davis3201eea2019-10-24 17:30:41 +0100852 const Category* category = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100853 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
854 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
855 BOOST_CHECK(category);
856 BOOST_CHECK(category->m_Name == categoryName);
857 BOOST_CHECK(category->m_Counters.empty());
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100858
859 // Get the registered category
860 const Category* registeredCategory = counterDirectory.GetCategory(categoryName);
861 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
862 BOOST_CHECK(registeredCategory);
863 BOOST_CHECK(registeredCategory == category);
864
865 // Try to get a category not registered
866 const Category* notRegisteredCategory = counterDirectory.GetCategory("not_registered_category");
867 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
868 BOOST_CHECK(!notRegisteredCategory);
869
870 // Register a category already registered
871 const Category* anotherCategory = nullptr;
872 BOOST_CHECK_THROW(anotherCategory = counterDirectory.RegisterCategory(categoryName),
873 armnn::InvalidArgumentException);
874 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
875 BOOST_CHECK(!anotherCategory);
876
877 // Register a device for testing
878 const std::string deviceName = "some_device";
Keith Davis3201eea2019-10-24 17:30:41 +0100879 const Device* device = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100880 BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName));
881 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
882 BOOST_CHECK(device);
883 BOOST_CHECK(device->m_Uid >= 1);
884 BOOST_CHECK(device->m_Name == deviceName);
885 BOOST_CHECK(device->m_Cores == 0);
886
887 // Register a new category not associated to any device
888 const std::string categoryWoDeviceName = "some_category_without_device";
Keith Davis3201eea2019-10-24 17:30:41 +0100889 const Category* categoryWoDevice = nullptr;
Sadik Armagan4c998992020-02-25 12:44:44 +0000890 BOOST_CHECK_NO_THROW(categoryWoDevice = counterDirectory.RegisterCategory(categoryWoDeviceName));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100891 BOOST_CHECK(counterDirectory.GetCategoryCount() == 2);
892 BOOST_CHECK(categoryWoDevice);
893 BOOST_CHECK(categoryWoDevice->m_Name == categoryWoDeviceName);
894 BOOST_CHECK(categoryWoDevice->m_Counters.empty());
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100895
Sadik Armagan4c998992020-02-25 12:44:44 +0000896 // Register a new category associated to an invalid device name (already exist)
897 const Category* categoryInvalidDeviceName = nullptr;
898 BOOST_CHECK_THROW(categoryInvalidDeviceName =
899 counterDirectory.RegisterCategory(categoryWoDeviceName),
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100900 armnn::InvalidArgumentException);
901 BOOST_CHECK(counterDirectory.GetCategoryCount() == 2);
Sadik Armagan4c998992020-02-25 12:44:44 +0000902 BOOST_CHECK(!categoryInvalidDeviceName);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100903
904 // Register a new category associated to a valid device
905 const std::string categoryWValidDeviceName = "some_category_with_valid_device";
Keith Davis3201eea2019-10-24 17:30:41 +0100906 const Category* categoryWValidDevice = nullptr;
907 BOOST_CHECK_NO_THROW(categoryWValidDevice =
Sadik Armagan4c998992020-02-25 12:44:44 +0000908 counterDirectory.RegisterCategory(categoryWValidDeviceName));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100909 BOOST_CHECK(counterDirectory.GetCategoryCount() == 3);
910 BOOST_CHECK(categoryWValidDevice);
911 BOOST_CHECK(categoryWValidDevice != category);
912 BOOST_CHECK(categoryWValidDevice->m_Name == categoryWValidDeviceName);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100913
914 // Register a counter set for testing
915 const std::string counterSetName = "some_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +0100916 const CounterSet* counterSet = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100917 BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
918 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
919 BOOST_CHECK(counterSet);
920 BOOST_CHECK(counterSet->m_Uid >= 1);
921 BOOST_CHECK(counterSet->m_Name == counterSetName);
922 BOOST_CHECK(counterSet->m_Count == 0);
923
924 // Register a new category not associated to any counter set
925 const std::string categoryWoCounterSetName = "some_category_without_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +0100926 const Category* categoryWoCounterSet = nullptr;
927 BOOST_CHECK_NO_THROW(categoryWoCounterSet =
Sadik Armagan4c998992020-02-25 12:44:44 +0000928 counterDirectory.RegisterCategory(categoryWoCounterSetName));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100929 BOOST_CHECK(counterDirectory.GetCategoryCount() == 4);
930 BOOST_CHECK(categoryWoCounterSet);
931 BOOST_CHECK(categoryWoCounterSet->m_Name == categoryWoCounterSetName);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100932
933 // Register a new category associated to a valid counter set
934 const std::string categoryWValidCounterSetName = "some_category_with_valid_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +0100935 const Category* categoryWValidCounterSet = nullptr;
Sadik Armagan4c998992020-02-25 12:44:44 +0000936 BOOST_CHECK_NO_THROW(categoryWValidCounterSet = counterDirectory.RegisterCategory(categoryWValidCounterSetName));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100937 BOOST_CHECK(counterDirectory.GetCategoryCount() == 5);
938 BOOST_CHECK(categoryWValidCounterSet);
939 BOOST_CHECK(categoryWValidCounterSet != category);
940 BOOST_CHECK(categoryWValidCounterSet->m_Name == categoryWValidCounterSetName);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100941
942 // Register a new category associated to a valid device and counter set
943 const std::string categoryWValidDeviceAndValidCounterSetName = "some_category_with_valid_device_and_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +0100944 const Category* categoryWValidDeviceAndValidCounterSet = nullptr;
945 BOOST_CHECK_NO_THROW(categoryWValidDeviceAndValidCounterSet = counterDirectory.RegisterCategory(
Sadik Armagan4c998992020-02-25 12:44:44 +0000946 categoryWValidDeviceAndValidCounterSetName));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100947 BOOST_CHECK(counterDirectory.GetCategoryCount() == 6);
948 BOOST_CHECK(categoryWValidDeviceAndValidCounterSet);
949 BOOST_CHECK(categoryWValidDeviceAndValidCounterSet != category);
950 BOOST_CHECK(categoryWValidDeviceAndValidCounterSet->m_Name == categoryWValidDeviceAndValidCounterSetName);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100951}
952
953BOOST_AUTO_TEST_CASE(CheckCounterDirectoryRegisterDevice)
954{
955 CounterDirectory counterDirectory;
Keith Davis3201eea2019-10-24 17:30:41 +0100956 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
957 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100958 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
Keith Davis3201eea2019-10-24 17:30:41 +0100959 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100960
961 // Register a device with an invalid name
962 const Device* noDevice = nullptr;
963 BOOST_CHECK_THROW(noDevice = counterDirectory.RegisterDevice(""), armnn::InvalidArgumentException);
964 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
965 BOOST_CHECK(!noDevice);
966
967 // Register a device with an invalid name
968 BOOST_CHECK_THROW(noDevice = counterDirectory.RegisterDevice("inv@lid nam€"), armnn::InvalidArgumentException);
969 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
970 BOOST_CHECK(!noDevice);
971
972 // Register a new device with no cores or parent category
973 const std::string deviceName = "some_device";
Keith Davis3201eea2019-10-24 17:30:41 +0100974 const Device* device = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100975 BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName));
976 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
977 BOOST_CHECK(device);
978 BOOST_CHECK(device->m_Name == deviceName);
979 BOOST_CHECK(device->m_Uid >= 1);
980 BOOST_CHECK(device->m_Cores == 0);
981
Matteo Martincigh657ab2d2019-09-18 10:53:24 +0100982 // Try getting an unregistered device
983 const Device* unregisteredDevice = counterDirectory.GetDevice(9999);
984 BOOST_CHECK(!unregisteredDevice);
985
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100986 // Get the registered device
987 const Device* registeredDevice = counterDirectory.GetDevice(device->m_Uid);
988 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
989 BOOST_CHECK(registeredDevice);
990 BOOST_CHECK(registeredDevice == device);
991
Matteo Martincigh657ab2d2019-09-18 10:53:24 +0100992 // Register a device with the name of a device already registered
993 const Device* deviceSameName = nullptr;
994 BOOST_CHECK_THROW(deviceSameName = counterDirectory.RegisterDevice(deviceName), armnn::InvalidArgumentException);
995 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
996 BOOST_CHECK(!deviceSameName);
997
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100998 // Register a new device with cores and no parent category
999 const std::string deviceWCoresName = "some_device_with_cores";
Keith Davis3201eea2019-10-24 17:30:41 +01001000 const Device* deviceWCores = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001001 BOOST_CHECK_NO_THROW(deviceWCores = counterDirectory.RegisterDevice(deviceWCoresName, 2));
1002 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
1003 BOOST_CHECK(deviceWCores);
1004 BOOST_CHECK(deviceWCores->m_Name == deviceWCoresName);
1005 BOOST_CHECK(deviceWCores->m_Uid >= 1);
1006 BOOST_CHECK(deviceWCores->m_Uid > device->m_Uid);
1007 BOOST_CHECK(deviceWCores->m_Cores == 2);
1008
1009 // Get the registered device
1010 const Device* registeredDeviceWCores = counterDirectory.GetDevice(deviceWCores->m_Uid);
1011 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
1012 BOOST_CHECK(registeredDeviceWCores);
1013 BOOST_CHECK(registeredDeviceWCores == deviceWCores);
1014 BOOST_CHECK(registeredDeviceWCores != device);
1015
1016 // Register a new device with cores and invalid parent category
1017 const std::string deviceWCoresWInvalidParentCategoryName = "some_device_with_cores_with_invalid_parent_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001018 const Device* deviceWCoresWInvalidParentCategory = nullptr;
1019 BOOST_CHECK_THROW(deviceWCoresWInvalidParentCategory =
1020 counterDirectory.RegisterDevice(deviceWCoresWInvalidParentCategoryName, 3, std::string("")),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001021 armnn::InvalidArgumentException);
1022 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
1023 BOOST_CHECK(!deviceWCoresWInvalidParentCategory);
1024
1025 // Register a new device with cores and invalid parent category
1026 const std::string deviceWCoresWInvalidParentCategoryName2 = "some_device_with_cores_with_invalid_parent_category2";
Keith Davis3201eea2019-10-24 17:30:41 +01001027 const Device* deviceWCoresWInvalidParentCategory2 = nullptr;
1028 BOOST_CHECK_THROW(deviceWCoresWInvalidParentCategory2 = counterDirectory.RegisterDevice(
1029 deviceWCoresWInvalidParentCategoryName2, 3, std::string("invalid_parent_category")),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001030 armnn::InvalidArgumentException);
1031 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
1032 BOOST_CHECK(!deviceWCoresWInvalidParentCategory2);
1033
1034 // Register a category for testing
1035 const std::string categoryName = "some_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001036 const Category* category = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001037 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
1038 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
1039 BOOST_CHECK(category);
1040 BOOST_CHECK(category->m_Name == categoryName);
1041 BOOST_CHECK(category->m_Counters.empty());
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001042
1043 // Register a new device with cores and valid parent category
1044 const std::string deviceWCoresWValidParentCategoryName = "some_device_with_cores_with_valid_parent_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001045 const Device* deviceWCoresWValidParentCategory = nullptr;
1046 BOOST_CHECK_NO_THROW(deviceWCoresWValidParentCategory =
1047 counterDirectory.RegisterDevice(deviceWCoresWValidParentCategoryName, 4, categoryName));
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001048 BOOST_CHECK(counterDirectory.GetDeviceCount() == 3);
1049 BOOST_CHECK(deviceWCoresWValidParentCategory);
1050 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Name == deviceWCoresWValidParentCategoryName);
1051 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Uid >= 1);
1052 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Uid > device->m_Uid);
1053 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Uid > deviceWCores->m_Uid);
1054 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Cores == 4);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001055}
1056
1057BOOST_AUTO_TEST_CASE(CheckCounterDirectoryRegisterCounterSet)
1058{
1059 CounterDirectory counterDirectory;
Keith Davis3201eea2019-10-24 17:30:41 +01001060 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
1061 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001062 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
Keith Davis3201eea2019-10-24 17:30:41 +01001063 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001064
1065 // Register a counter set with an invalid name
1066 const CounterSet* noCounterSet = nullptr;
1067 BOOST_CHECK_THROW(noCounterSet = counterDirectory.RegisterCounterSet(""), armnn::InvalidArgumentException);
1068 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
1069 BOOST_CHECK(!noCounterSet);
1070
1071 // Register a counter set with an invalid name
1072 BOOST_CHECK_THROW(noCounterSet = counterDirectory.RegisterCounterSet("invalid name"),
1073 armnn::InvalidArgumentException);
1074 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
1075 BOOST_CHECK(!noCounterSet);
1076
1077 // Register a new counter set with no count or parent category
1078 const std::string counterSetName = "some_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +01001079 const CounterSet* counterSet = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001080 BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
1081 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1082 BOOST_CHECK(counterSet);
1083 BOOST_CHECK(counterSet->m_Name == counterSetName);
1084 BOOST_CHECK(counterSet->m_Uid >= 1);
1085 BOOST_CHECK(counterSet->m_Count == 0);
1086
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001087 // Try getting an unregistered counter set
1088 const CounterSet* unregisteredCounterSet = counterDirectory.GetCounterSet(9999);
1089 BOOST_CHECK(!unregisteredCounterSet);
1090
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001091 // Get the registered counter set
1092 const CounterSet* registeredCounterSet = counterDirectory.GetCounterSet(counterSet->m_Uid);
1093 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1094 BOOST_CHECK(registeredCounterSet);
1095 BOOST_CHECK(registeredCounterSet == counterSet);
1096
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001097 // Register a counter set with the name of a counter set already registered
1098 const CounterSet* counterSetSameName = nullptr;
1099 BOOST_CHECK_THROW(counterSetSameName = counterDirectory.RegisterCounterSet(counterSetName),
1100 armnn::InvalidArgumentException);
1101 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1102 BOOST_CHECK(!counterSetSameName);
1103
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001104 // Register a new counter set with count and no parent category
1105 const std::string counterSetWCountName = "some_counter_set_with_count";
Keith Davis3201eea2019-10-24 17:30:41 +01001106 const CounterSet* counterSetWCount = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001107 BOOST_CHECK_NO_THROW(counterSetWCount = counterDirectory.RegisterCounterSet(counterSetWCountName, 37));
1108 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 2);
1109 BOOST_CHECK(counterSetWCount);
1110 BOOST_CHECK(counterSetWCount->m_Name == counterSetWCountName);
1111 BOOST_CHECK(counterSetWCount->m_Uid >= 1);
1112 BOOST_CHECK(counterSetWCount->m_Uid > counterSet->m_Uid);
1113 BOOST_CHECK(counterSetWCount->m_Count == 37);
1114
1115 // Get the registered counter set
1116 const CounterSet* registeredCounterSetWCount = counterDirectory.GetCounterSet(counterSetWCount->m_Uid);
1117 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 2);
1118 BOOST_CHECK(registeredCounterSetWCount);
1119 BOOST_CHECK(registeredCounterSetWCount == counterSetWCount);
1120 BOOST_CHECK(registeredCounterSetWCount != counterSet);
1121
1122 // Register a new counter set with count and invalid parent category
1123 const std::string counterSetWCountWInvalidParentCategoryName = "some_counter_set_with_count_"
1124 "with_invalid_parent_category";
1125 const CounterSet* counterSetWCountWInvalidParentCategory = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001126 BOOST_CHECK_THROW(counterSetWCountWInvalidParentCategory = counterDirectory.RegisterCounterSet(
1127 counterSetWCountWInvalidParentCategoryName, 42, std::string("")),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001128 armnn::InvalidArgumentException);
1129 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 2);
1130 BOOST_CHECK(!counterSetWCountWInvalidParentCategory);
1131
1132 // Register a new counter set with count and invalid parent category
1133 const std::string counterSetWCountWInvalidParentCategoryName2 = "some_counter_set_with_count_"
1134 "with_invalid_parent_category2";
1135 const CounterSet* counterSetWCountWInvalidParentCategory2 = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001136 BOOST_CHECK_THROW(counterSetWCountWInvalidParentCategory2 = counterDirectory.RegisterCounterSet(
1137 counterSetWCountWInvalidParentCategoryName2, 42, std::string("invalid_parent_category")),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001138 armnn::InvalidArgumentException);
1139 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 2);
1140 BOOST_CHECK(!counterSetWCountWInvalidParentCategory2);
1141
1142 // Register a category for testing
1143 const std::string categoryName = "some_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001144 const Category* category = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001145 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
1146 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
1147 BOOST_CHECK(category);
1148 BOOST_CHECK(category->m_Name == categoryName);
1149 BOOST_CHECK(category->m_Counters.empty());
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001150
1151 // Register a new counter set with count and valid parent category
1152 const std::string counterSetWCountWValidParentCategoryName = "some_counter_set_with_count_"
1153 "with_valid_parent_category";
1154 const CounterSet* counterSetWCountWValidParentCategory = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001155 BOOST_CHECK_NO_THROW(counterSetWCountWValidParentCategory = counterDirectory.RegisterCounterSet(
1156 counterSetWCountWValidParentCategoryName, 42, categoryName));
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001157 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 3);
1158 BOOST_CHECK(counterSetWCountWValidParentCategory);
1159 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Name == counterSetWCountWValidParentCategoryName);
1160 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Uid >= 1);
1161 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Uid > counterSet->m_Uid);
1162 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Uid > counterSetWCount->m_Uid);
1163 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Count == 42);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001164
Sadik Armagan4c998992020-02-25 12:44:44 +00001165 // Register a counter set associated to a category with invalid name
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001166 const std::string counterSetSameCategoryName = "some_counter_set_with_invalid_parent_category";
Sadik Armagan4c998992020-02-25 12:44:44 +00001167 const std::string invalidCategoryName = "";
Keith Davis3201eea2019-10-24 17:30:41 +01001168 const CounterSet* counterSetSameCategory = nullptr;
1169 BOOST_CHECK_THROW(counterSetSameCategory =
Sadik Armagan4c998992020-02-25 12:44:44 +00001170 counterDirectory.RegisterCounterSet(counterSetSameCategoryName, 0, invalidCategoryName),
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001171 armnn::InvalidArgumentException);
1172 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 3);
1173 BOOST_CHECK(!counterSetSameCategory);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001174}
1175
1176BOOST_AUTO_TEST_CASE(CheckCounterDirectoryRegisterCounter)
1177{
1178 CounterDirectory counterDirectory;
Keith Davis3201eea2019-10-24 17:30:41 +01001179 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
1180 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001181 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
Keith Davis3201eea2019-10-24 17:30:41 +01001182 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001183
1184 // Register a counter with an invalid parent category name
1185 const Counter* noCounter = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001186 BOOST_CHECK_THROW(noCounter =
Keith Davise394bd92019-12-02 15:12:19 +00001187 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1188 0,
1189 "",
1190 0,
1191 1,
1192 123.45f,
1193 "valid ",
1194 "name"),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001195 armnn::InvalidArgumentException);
1196 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1197 BOOST_CHECK(!noCounter);
1198
1199 // Register a counter with an invalid parent category name
Keith Davise394bd92019-12-02 15:12:19 +00001200 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1201 1,
1202 "invalid parent category",
1203 0,
1204 1,
1205 123.45f,
1206 "valid name",
1207 "valid description"),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001208 armnn::InvalidArgumentException);
1209 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1210 BOOST_CHECK(!noCounter);
1211
1212 // Register a counter with an invalid class
Keith Davise394bd92019-12-02 15:12:19 +00001213 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1214 2,
1215 "valid_parent_category",
1216 2,
1217 1,
1218 123.45f,
1219 "valid "
1220 "name",
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001221 "valid description"),
1222 armnn::InvalidArgumentException);
1223 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1224 BOOST_CHECK(!noCounter);
1225
1226 // Register a counter with an invalid interpolation
Keith Davise394bd92019-12-02 15:12:19 +00001227 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1228 4,
1229 "valid_parent_category",
1230 0,
1231 3,
1232 123.45f,
1233 "valid "
1234 "name",
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001235 "valid description"),
1236 armnn::InvalidArgumentException);
1237 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1238 BOOST_CHECK(!noCounter);
1239
1240 // Register a counter with an invalid multiplier
Keith Davise394bd92019-12-02 15:12:19 +00001241 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1242 5,
1243 "valid_parent_category",
1244 0,
1245 1,
1246 .0f,
1247 "valid "
1248 "name",
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001249 "valid description"),
1250 armnn::InvalidArgumentException);
1251 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1252 BOOST_CHECK(!noCounter);
1253
1254 // Register a counter with an invalid name
Keith Davis3201eea2019-10-24 17:30:41 +01001255 BOOST_CHECK_THROW(
Keith Davise394bd92019-12-02 15:12:19 +00001256 noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1257 6,
1258 "valid_parent_category",
1259 0,
1260 1,
1261 123.45f,
1262 "",
1263 "valid description"),
Keith Davis3201eea2019-10-24 17:30:41 +01001264 armnn::InvalidArgumentException);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001265 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1266 BOOST_CHECK(!noCounter);
1267
1268 // Register a counter with an invalid name
Keith Davise394bd92019-12-02 15:12:19 +00001269 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1270 7,
1271 "valid_parent_category",
1272 0,
1273 1,
1274 123.45f,
1275 "invalid nam€",
1276 "valid description"),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001277 armnn::InvalidArgumentException);
1278 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1279 BOOST_CHECK(!noCounter);
1280
1281 // Register a counter with an invalid description
Keith Davis3201eea2019-10-24 17:30:41 +01001282 BOOST_CHECK_THROW(noCounter =
Keith Davise394bd92019-12-02 15:12:19 +00001283 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1284 8,
1285 "valid_parent_category",
1286 0,
1287 1,
1288 123.45f,
1289 "valid name",
1290 ""),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001291 armnn::InvalidArgumentException);
1292 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1293 BOOST_CHECK(!noCounter);
1294
1295 // Register a counter with an invalid description
Keith Davise394bd92019-12-02 15:12:19 +00001296 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1297 9,
1298 "valid_parent_category",
1299 0,
1300 1,
1301 123.45f,
1302 "valid "
1303 "name",
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001304 "inv@lid description"),
1305 armnn::InvalidArgumentException);
1306 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1307 BOOST_CHECK(!noCounter);
1308
1309 // Register a counter with an invalid unit2
Keith Davise394bd92019-12-02 15:12:19 +00001310 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1311 10,
1312 "valid_parent_category",
1313 0,
1314 1,
1315 123.45f,
1316 "valid name",
1317 "valid description",
1318 std::string("Mb/s2")),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001319 armnn::InvalidArgumentException);
1320 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1321 BOOST_CHECK(!noCounter);
1322
1323 // Register a counter with a non-existing parent category name
Keith Davise394bd92019-12-02 15:12:19 +00001324 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1325 11,
1326 "invalid_parent_category",
1327 0,
1328 1,
1329 123.45f,
1330 "valid name",
1331 "valid description"),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001332 armnn::InvalidArgumentException);
1333 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1334 BOOST_CHECK(!noCounter);
1335
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001336 // Try getting an unregistered counter
1337 const Counter* unregisteredCounter = counterDirectory.GetCounter(9999);
1338 BOOST_CHECK(!unregisteredCounter);
1339
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001340 // Register a category for testing
1341 const std::string categoryName = "some_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001342 const Category* category = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001343 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
1344 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
1345 BOOST_CHECK(category);
1346 BOOST_CHECK(category->m_Name == categoryName);
1347 BOOST_CHECK(category->m_Counters.empty());
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001348
1349 // Register a counter with a valid parent category name
1350 const Counter* counter = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001351 BOOST_CHECK_NO_THROW(
Keith Davise394bd92019-12-02 15:12:19 +00001352 counter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1353 12,
1354 categoryName,
1355 0,
1356 1,
1357 123.45f,
1358 "valid name",
1359 "valid description"));
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001360 BOOST_CHECK(counterDirectory.GetCounterCount() == 1);
1361 BOOST_CHECK(counter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001362 BOOST_CHECK(counter->m_MaxCounterUid == counter->m_Uid);
1363 BOOST_CHECK(counter->m_Class == 0);
1364 BOOST_CHECK(counter->m_Interpolation == 1);
1365 BOOST_CHECK(counter->m_Multiplier == 123.45f);
1366 BOOST_CHECK(counter->m_Name == "valid name");
1367 BOOST_CHECK(counter->m_Description == "valid description");
1368 BOOST_CHECK(counter->m_Units == "");
1369 BOOST_CHECK(counter->m_DeviceUid == 0);
1370 BOOST_CHECK(counter->m_CounterSetUid == 0);
1371 BOOST_CHECK(category->m_Counters.size() == 1);
1372 BOOST_CHECK(category->m_Counters.back() == counter->m_Uid);
1373
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001374 // Register a counter with a name of a counter already registered for the given parent category name
1375 const Counter* counterSameName = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001376 BOOST_CHECK_THROW(counterSameName =
Keith Davise394bd92019-12-02 15:12:19 +00001377 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1378 13,
1379 categoryName,
1380 0,
1381 0,
1382 1.0f,
1383 "valid name",
1384 "valid description",
1385 std::string("description")),
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001386 armnn::InvalidArgumentException);
1387 BOOST_CHECK(counterDirectory.GetCounterCount() == 1);
1388 BOOST_CHECK(!counterSameName);
1389
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001390 // Register a counter with a valid parent category name and units
1391 const Counter* counterWUnits = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001392 BOOST_CHECK_NO_THROW(counterWUnits = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1393 14,
1394 categoryName,
1395 0,
1396 1,
1397 123.45f,
1398 "valid name 2",
1399 "valid description",
1400 std::string("Mnnsq2"))); // Units
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001401 BOOST_CHECK(counterDirectory.GetCounterCount() == 2);
1402 BOOST_CHECK(counterWUnits);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001403 BOOST_CHECK(counterWUnits->m_Uid > counter->m_Uid);
1404 BOOST_CHECK(counterWUnits->m_MaxCounterUid == counterWUnits->m_Uid);
1405 BOOST_CHECK(counterWUnits->m_Class == 0);
1406 BOOST_CHECK(counterWUnits->m_Interpolation == 1);
1407 BOOST_CHECK(counterWUnits->m_Multiplier == 123.45f);
1408 BOOST_CHECK(counterWUnits->m_Name == "valid name 2");
1409 BOOST_CHECK(counterWUnits->m_Description == "valid description");
1410 BOOST_CHECK(counterWUnits->m_Units == "Mnnsq2");
1411 BOOST_CHECK(counterWUnits->m_DeviceUid == 0);
1412 BOOST_CHECK(counterWUnits->m_CounterSetUid == 0);
1413 BOOST_CHECK(category->m_Counters.size() == 2);
1414 BOOST_CHECK(category->m_Counters.back() == counterWUnits->m_Uid);
1415
1416 // Register a counter with a valid parent category name and not associated with a device
1417 const Counter* counterWoDevice = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001418 BOOST_CHECK_NO_THROW(counterWoDevice = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Jim Flynnbbfe6032020-07-20 16:57:44 +01001419 26,
1420 categoryName,
1421 0,
1422 1,
1423 123.45f,
1424 "valid name 3",
1425 "valid description",
1426 armnn::EmptyOptional(),// Units
1427 armnn::EmptyOptional(),// Number of cores
1428 0)); // Device UID
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001429 BOOST_CHECK(counterDirectory.GetCounterCount() == 3);
1430 BOOST_CHECK(counterWoDevice);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001431 BOOST_CHECK(counterWoDevice->m_Uid > counter->m_Uid);
1432 BOOST_CHECK(counterWoDevice->m_MaxCounterUid == counterWoDevice->m_Uid);
1433 BOOST_CHECK(counterWoDevice->m_Class == 0);
1434 BOOST_CHECK(counterWoDevice->m_Interpolation == 1);
1435 BOOST_CHECK(counterWoDevice->m_Multiplier == 123.45f);
1436 BOOST_CHECK(counterWoDevice->m_Name == "valid name 3");
1437 BOOST_CHECK(counterWoDevice->m_Description == "valid description");
1438 BOOST_CHECK(counterWoDevice->m_Units == "");
1439 BOOST_CHECK(counterWoDevice->m_DeviceUid == 0);
1440 BOOST_CHECK(counterWoDevice->m_CounterSetUid == 0);
1441 BOOST_CHECK(category->m_Counters.size() == 3);
1442 BOOST_CHECK(category->m_Counters.back() == counterWoDevice->m_Uid);
1443
1444 // Register a counter with a valid parent category name and associated to an invalid device
Keith Davise394bd92019-12-02 15:12:19 +00001445 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1446 15,
1447 categoryName,
1448 0,
1449 1,
1450 123.45f,
1451 "valid name 4",
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001452 "valid description",
Keith Davis3201eea2019-10-24 17:30:41 +01001453 armnn::EmptyOptional(), // Units
1454 armnn::EmptyOptional(), // Number of cores
1455 100), // Device UID
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001456 armnn::InvalidArgumentException);
1457 BOOST_CHECK(counterDirectory.GetCounterCount() == 3);
1458 BOOST_CHECK(!noCounter);
1459
1460 // Register a device for testing
1461 const std::string deviceName = "some_device";
Keith Davis3201eea2019-10-24 17:30:41 +01001462 const Device* device = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001463 BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName));
1464 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
1465 BOOST_CHECK(device);
1466 BOOST_CHECK(device->m_Name == deviceName);
1467 BOOST_CHECK(device->m_Uid >= 1);
1468 BOOST_CHECK(device->m_Cores == 0);
1469
1470 // Register a counter with a valid parent category name and associated to a device
1471 const Counter* counterWDevice = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001472 BOOST_CHECK_NO_THROW(counterWDevice = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1473 16,
1474 categoryName,
1475 0,
1476 1,
1477 123.45f,
1478 "valid name 5",
1479 std::string("valid description"),
Jim Flynnbbfe6032020-07-20 16:57:44 +01001480 armnn::EmptyOptional(), // Units
1481 armnn::EmptyOptional(), // Number of cores
1482 device->m_Uid)); // Device UID
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001483 BOOST_CHECK(counterDirectory.GetCounterCount() == 4);
1484 BOOST_CHECK(counterWDevice);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001485 BOOST_CHECK(counterWDevice->m_Uid > counter->m_Uid);
1486 BOOST_CHECK(counterWDevice->m_MaxCounterUid == counterWDevice->m_Uid);
1487 BOOST_CHECK(counterWDevice->m_Class == 0);
1488 BOOST_CHECK(counterWDevice->m_Interpolation == 1);
1489 BOOST_CHECK(counterWDevice->m_Multiplier == 123.45f);
1490 BOOST_CHECK(counterWDevice->m_Name == "valid name 5");
1491 BOOST_CHECK(counterWDevice->m_Description == "valid description");
1492 BOOST_CHECK(counterWDevice->m_Units == "");
1493 BOOST_CHECK(counterWDevice->m_DeviceUid == device->m_Uid);
1494 BOOST_CHECK(counterWDevice->m_CounterSetUid == 0);
1495 BOOST_CHECK(category->m_Counters.size() == 4);
1496 BOOST_CHECK(category->m_Counters.back() == counterWDevice->m_Uid);
1497
1498 // Register a counter with a valid parent category name and not associated with a counter set
1499 const Counter* counterWoCounterSet = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001500 BOOST_CHECK_NO_THROW(counterWoCounterSet = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1501 17,
1502 categoryName,
1503 0,
1504 1,
1505 123.45f,
1506 "valid name 6",
1507 "valid description",
1508 armnn::EmptyOptional(),// Units
1509 armnn::EmptyOptional(),// No of cores
1510 armnn::EmptyOptional(),// Device UID
Jim Flynnbbfe6032020-07-20 16:57:44 +01001511 0)); // CounterSet UID
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001512 BOOST_CHECK(counterDirectory.GetCounterCount() == 5);
1513 BOOST_CHECK(counterWoCounterSet);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001514 BOOST_CHECK(counterWoCounterSet->m_Uid > counter->m_Uid);
1515 BOOST_CHECK(counterWoCounterSet->m_MaxCounterUid == counterWoCounterSet->m_Uid);
1516 BOOST_CHECK(counterWoCounterSet->m_Class == 0);
1517 BOOST_CHECK(counterWoCounterSet->m_Interpolation == 1);
1518 BOOST_CHECK(counterWoCounterSet->m_Multiplier == 123.45f);
1519 BOOST_CHECK(counterWoCounterSet->m_Name == "valid name 6");
1520 BOOST_CHECK(counterWoCounterSet->m_Description == "valid description");
1521 BOOST_CHECK(counterWoCounterSet->m_Units == "");
1522 BOOST_CHECK(counterWoCounterSet->m_DeviceUid == 0);
1523 BOOST_CHECK(counterWoCounterSet->m_CounterSetUid == 0);
1524 BOOST_CHECK(category->m_Counters.size() == 5);
1525 BOOST_CHECK(category->m_Counters.back() == counterWoCounterSet->m_Uid);
1526
1527 // Register a counter with a valid parent category name and associated to an invalid counter set
Keith Davise394bd92019-12-02 15:12:19 +00001528 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1529 18,
1530 categoryName,
1531 0,
1532 1,
1533 123.45f,
1534 "valid ",
1535 "name 7",
1536 std::string("valid description"),
Keith Davis3201eea2019-10-24 17:30:41 +01001537 armnn::EmptyOptional(), // Units
1538 armnn::EmptyOptional(), // Number of cores
Keith Davise394bd92019-12-02 15:12:19 +00001539 100), // Counter set UID
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001540 armnn::InvalidArgumentException);
1541 BOOST_CHECK(counterDirectory.GetCounterCount() == 5);
1542 BOOST_CHECK(!noCounter);
1543
1544 // Register a counter with a valid parent category name and with a given number of cores
1545 const Counter* counterWNumberOfCores = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001546 uint16_t numberOfCores = 15;
1547 BOOST_CHECK_NO_THROW(counterWNumberOfCores = counterDirectory.RegisterCounter(
Keith Davise394bd92019-12-02 15:12:19 +00001548 armnn::profiling::BACKEND_ID, 50,
Keith Davis3201eea2019-10-24 17:30:41 +01001549 categoryName, 0, 1, 123.45f, "valid name 8", "valid description",
1550 armnn::EmptyOptional(), // Units
1551 numberOfCores, // Number of cores
1552 armnn::EmptyOptional(), // Device UID
1553 armnn::EmptyOptional())); // Counter set UID
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001554 BOOST_CHECK(counterDirectory.GetCounterCount() == 20);
1555 BOOST_CHECK(counterWNumberOfCores);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001556 BOOST_CHECK(counterWNumberOfCores->m_Uid > counter->m_Uid);
1557 BOOST_CHECK(counterWNumberOfCores->m_MaxCounterUid == counterWNumberOfCores->m_Uid + numberOfCores - 1);
1558 BOOST_CHECK(counterWNumberOfCores->m_Class == 0);
1559 BOOST_CHECK(counterWNumberOfCores->m_Interpolation == 1);
1560 BOOST_CHECK(counterWNumberOfCores->m_Multiplier == 123.45f);
1561 BOOST_CHECK(counterWNumberOfCores->m_Name == "valid name 8");
1562 BOOST_CHECK(counterWNumberOfCores->m_Description == "valid description");
1563 BOOST_CHECK(counterWNumberOfCores->m_Units == "");
1564 BOOST_CHECK(counterWNumberOfCores->m_DeviceUid == 0);
1565 BOOST_CHECK(counterWNumberOfCores->m_CounterSetUid == 0);
1566 BOOST_CHECK(category->m_Counters.size() == 20);
Keith Davis3201eea2019-10-24 17:30:41 +01001567 for (size_t i = 0; i < numberOfCores; i++)
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001568 {
1569 BOOST_CHECK(category->m_Counters[category->m_Counters.size() - numberOfCores + i] ==
1570 counterWNumberOfCores->m_Uid + i);
1571 }
1572
1573 // Register a multi-core device for testing
1574 const std::string multiCoreDeviceName = "some_multi_core_device";
Keith Davis3201eea2019-10-24 17:30:41 +01001575 const Device* multiCoreDevice = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001576 BOOST_CHECK_NO_THROW(multiCoreDevice = counterDirectory.RegisterDevice(multiCoreDeviceName, 4));
1577 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
1578 BOOST_CHECK(multiCoreDevice);
1579 BOOST_CHECK(multiCoreDevice->m_Name == multiCoreDeviceName);
1580 BOOST_CHECK(multiCoreDevice->m_Uid >= 1);
1581 BOOST_CHECK(multiCoreDevice->m_Cores == 4);
1582
1583 // Register a counter with a valid parent category name and associated to the multi-core device
1584 const Counter* counterWMultiCoreDevice = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001585 BOOST_CHECK_NO_THROW(counterWMultiCoreDevice = counterDirectory.RegisterCounter(
Keith Davise394bd92019-12-02 15:12:19 +00001586 armnn::profiling::BACKEND_ID, 19, categoryName, 0, 1,
1587 123.45f, "valid name 9", "valid description",
Keith Davis3201eea2019-10-24 17:30:41 +01001588 armnn::EmptyOptional(), // Units
1589 armnn::EmptyOptional(), // Number of cores
1590 multiCoreDevice->m_Uid, // Device UID
1591 armnn::EmptyOptional())); // Counter set UID
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001592 BOOST_CHECK(counterDirectory.GetCounterCount() == 24);
1593 BOOST_CHECK(counterWMultiCoreDevice);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001594 BOOST_CHECK(counterWMultiCoreDevice->m_Uid > counter->m_Uid);
1595 BOOST_CHECK(counterWMultiCoreDevice->m_MaxCounterUid ==
1596 counterWMultiCoreDevice->m_Uid + multiCoreDevice->m_Cores - 1);
1597 BOOST_CHECK(counterWMultiCoreDevice->m_Class == 0);
1598 BOOST_CHECK(counterWMultiCoreDevice->m_Interpolation == 1);
1599 BOOST_CHECK(counterWMultiCoreDevice->m_Multiplier == 123.45f);
1600 BOOST_CHECK(counterWMultiCoreDevice->m_Name == "valid name 9");
1601 BOOST_CHECK(counterWMultiCoreDevice->m_Description == "valid description");
1602 BOOST_CHECK(counterWMultiCoreDevice->m_Units == "");
1603 BOOST_CHECK(counterWMultiCoreDevice->m_DeviceUid == multiCoreDevice->m_Uid);
1604 BOOST_CHECK(counterWMultiCoreDevice->m_CounterSetUid == 0);
1605 BOOST_CHECK(category->m_Counters.size() == 24);
Keith Davis3201eea2019-10-24 17:30:41 +01001606 for (size_t i = 0; i < 4; i++)
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001607 {
1608 BOOST_CHECK(category->m_Counters[category->m_Counters.size() - 4 + i] == counterWMultiCoreDevice->m_Uid + i);
1609 }
1610
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001611 // Register a multi-core device associate to a parent category for testing
1612 const std::string multiCoreDeviceNameWParentCategory = "some_multi_core_device_with_parent_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001613 const Device* multiCoreDeviceWParentCategory = nullptr;
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001614 BOOST_CHECK_NO_THROW(multiCoreDeviceWParentCategory =
Keith Davis3201eea2019-10-24 17:30:41 +01001615 counterDirectory.RegisterDevice(multiCoreDeviceNameWParentCategory, 2, categoryName));
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001616 BOOST_CHECK(counterDirectory.GetDeviceCount() == 3);
1617 BOOST_CHECK(multiCoreDeviceWParentCategory);
1618 BOOST_CHECK(multiCoreDeviceWParentCategory->m_Name == multiCoreDeviceNameWParentCategory);
1619 BOOST_CHECK(multiCoreDeviceWParentCategory->m_Uid >= 1);
1620 BOOST_CHECK(multiCoreDeviceWParentCategory->m_Cores == 2);
1621
1622 // Register a counter with a valid parent category name and getting the number of cores of the multi-core device
1623 // associated to that category
1624 const Counter* counterWMultiCoreDeviceWParentCategory = nullptr;
Sadik Armagan4c998992020-02-25 12:44:44 +00001625 uint16_t numberOfCourse = multiCoreDeviceWParentCategory->m_Cores;
Keith Davise394bd92019-12-02 15:12:19 +00001626 BOOST_CHECK_NO_THROW(counterWMultiCoreDeviceWParentCategory =
Jim Flynnbbfe6032020-07-20 16:57:44 +01001627 counterDirectory.RegisterCounter(
1628 armnn::profiling::BACKEND_ID,
1629 100,
1630 categoryName,
1631 0,
1632 1,
1633 123.45f,
1634 "valid name 10",
1635 "valid description",
1636 armnn::EmptyOptional(), // Units
1637 numberOfCourse, // Number of cores
1638 armnn::EmptyOptional(), // Device UID
1639 armnn::EmptyOptional()));// Counter set UID
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001640 BOOST_CHECK(counterDirectory.GetCounterCount() == 26);
1641 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001642 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Uid > counter->m_Uid);
1643 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_MaxCounterUid ==
1644 counterWMultiCoreDeviceWParentCategory->m_Uid + multiCoreDeviceWParentCategory->m_Cores - 1);
1645 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Class == 0);
1646 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Interpolation == 1);
1647 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Multiplier == 123.45f);
1648 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Name == "valid name 10");
1649 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Description == "valid description");
1650 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Units == "");
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001651 BOOST_CHECK(category->m_Counters.size() == 26);
Keith Davis3201eea2019-10-24 17:30:41 +01001652 for (size_t i = 0; i < 2; i++)
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001653 {
1654 BOOST_CHECK(category->m_Counters[category->m_Counters.size() - 2 + i] ==
1655 counterWMultiCoreDeviceWParentCategory->m_Uid + i);
1656 }
1657
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001658 // Register a counter set for testing
1659 const std::string counterSetName = "some_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +01001660 const CounterSet* counterSet = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001661 BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
1662 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1663 BOOST_CHECK(counterSet);
1664 BOOST_CHECK(counterSet->m_Name == counterSetName);
1665 BOOST_CHECK(counterSet->m_Uid >= 1);
1666 BOOST_CHECK(counterSet->m_Count == 0);
1667
1668 // Register a counter with a valid parent category name and associated to a counter set
1669 const Counter* counterWCounterSet = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001670 BOOST_CHECK_NO_THROW(counterWCounterSet = counterDirectory.RegisterCounter(
Keith Davise394bd92019-12-02 15:12:19 +00001671 armnn::profiling::BACKEND_ID, 300,
Keith Davis3201eea2019-10-24 17:30:41 +01001672 categoryName, 0, 1, 123.45f, "valid name 11", "valid description",
1673 armnn::EmptyOptional(), // Units
1674 0, // Number of cores
1675 armnn::EmptyOptional(), // Device UID
1676 counterSet->m_Uid)); // Counter set UID
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001677 BOOST_CHECK(counterDirectory.GetCounterCount() == 27);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001678 BOOST_CHECK(counterWCounterSet);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001679 BOOST_CHECK(counterWCounterSet->m_Uid > counter->m_Uid);
1680 BOOST_CHECK(counterWCounterSet->m_MaxCounterUid == counterWCounterSet->m_Uid);
1681 BOOST_CHECK(counterWCounterSet->m_Class == 0);
1682 BOOST_CHECK(counterWCounterSet->m_Interpolation == 1);
1683 BOOST_CHECK(counterWCounterSet->m_Multiplier == 123.45f);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001684 BOOST_CHECK(counterWCounterSet->m_Name == "valid name 11");
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001685 BOOST_CHECK(counterWCounterSet->m_Description == "valid description");
1686 BOOST_CHECK(counterWCounterSet->m_Units == "");
1687 BOOST_CHECK(counterWCounterSet->m_DeviceUid == 0);
1688 BOOST_CHECK(counterWCounterSet->m_CounterSetUid == counterSet->m_Uid);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001689 BOOST_CHECK(category->m_Counters.size() == 27);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001690 BOOST_CHECK(category->m_Counters.back() == counterWCounterSet->m_Uid);
1691
1692 // Register a counter with a valid parent category name and associated to a device and a counter set
1693 const Counter* counterWDeviceWCounterSet = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001694 BOOST_CHECK_NO_THROW(counterWDeviceWCounterSet = counterDirectory.RegisterCounter(
Keith Davise394bd92019-12-02 15:12:19 +00001695 armnn::profiling::BACKEND_ID, 23,
Keith Davis3201eea2019-10-24 17:30:41 +01001696 categoryName, 0, 1, 123.45f, "valid name 12", "valid description",
1697 armnn::EmptyOptional(), // Units
1698 1, // Number of cores
1699 device->m_Uid, // Device UID
1700 counterSet->m_Uid)); // Counter set UID
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001701 BOOST_CHECK(counterDirectory.GetCounterCount() == 28);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001702 BOOST_CHECK(counterWDeviceWCounterSet);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001703 BOOST_CHECK(counterWDeviceWCounterSet->m_Uid > counter->m_Uid);
1704 BOOST_CHECK(counterWDeviceWCounterSet->m_MaxCounterUid == counterWDeviceWCounterSet->m_Uid);
1705 BOOST_CHECK(counterWDeviceWCounterSet->m_Class == 0);
1706 BOOST_CHECK(counterWDeviceWCounterSet->m_Interpolation == 1);
1707 BOOST_CHECK(counterWDeviceWCounterSet->m_Multiplier == 123.45f);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001708 BOOST_CHECK(counterWDeviceWCounterSet->m_Name == "valid name 12");
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001709 BOOST_CHECK(counterWDeviceWCounterSet->m_Description == "valid description");
1710 BOOST_CHECK(counterWDeviceWCounterSet->m_Units == "");
1711 BOOST_CHECK(counterWDeviceWCounterSet->m_DeviceUid == device->m_Uid);
1712 BOOST_CHECK(counterWDeviceWCounterSet->m_CounterSetUid == counterSet->m_Uid);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001713 BOOST_CHECK(category->m_Counters.size() == 28);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001714 BOOST_CHECK(category->m_Counters.back() == counterWDeviceWCounterSet->m_Uid);
1715
1716 // Register another category for testing
1717 const std::string anotherCategoryName = "some_other_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001718 const Category* anotherCategory = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001719 BOOST_CHECK_NO_THROW(anotherCategory = counterDirectory.RegisterCategory(anotherCategoryName));
1720 BOOST_CHECK(counterDirectory.GetCategoryCount() == 2);
1721 BOOST_CHECK(anotherCategory);
1722 BOOST_CHECK(anotherCategory != category);
1723 BOOST_CHECK(anotherCategory->m_Name == anotherCategoryName);
1724 BOOST_CHECK(anotherCategory->m_Counters.empty());
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001725
1726 // Register a counter to the other category
1727 const Counter* anotherCounter = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001728 BOOST_CHECK_NO_THROW(anotherCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID, 24,
1729 anotherCategoryName, 1, 0, .00043f,
Keith Davis3201eea2019-10-24 17:30:41 +01001730 "valid name", "valid description",
Jim Flynnbbfe6032020-07-20 16:57:44 +01001731 armnn::EmptyOptional(), // Units
1732 armnn::EmptyOptional(), // Number of cores
1733 device->m_Uid, // Device UID
1734 counterSet->m_Uid)); // Counter set UID
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001735 BOOST_CHECK(counterDirectory.GetCounterCount() == 29);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001736 BOOST_CHECK(anotherCounter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001737 BOOST_CHECK(anotherCounter->m_MaxCounterUid == anotherCounter->m_Uid);
1738 BOOST_CHECK(anotherCounter->m_Class == 1);
1739 BOOST_CHECK(anotherCounter->m_Interpolation == 0);
1740 BOOST_CHECK(anotherCounter->m_Multiplier == .00043f);
1741 BOOST_CHECK(anotherCounter->m_Name == "valid name");
1742 BOOST_CHECK(anotherCounter->m_Description == "valid description");
1743 BOOST_CHECK(anotherCounter->m_Units == "");
1744 BOOST_CHECK(anotherCounter->m_DeviceUid == device->m_Uid);
1745 BOOST_CHECK(anotherCounter->m_CounterSetUid == counterSet->m_Uid);
1746 BOOST_CHECK(anotherCategory->m_Counters.size() == 1);
1747 BOOST_CHECK(anotherCategory->m_Counters.back() == anotherCounter->m_Uid);
Matteo Martincighab173e92019-09-05 12:02:04 +01001748}
1749
Ferran Balaguer1b941722019-08-28 16:57:18 +01001750BOOST_AUTO_TEST_CASE(CounterSelectionCommandHandlerParseData)
1751{
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01001752 ProfilingStateMachine profilingStateMachine;
1753
Ferran Balaguer1b941722019-08-28 16:57:18 +01001754 class TestCaptureThread : public IPeriodicCounterCapture
1755 {
Keith Davis3201eea2019-10-24 17:30:41 +01001756 void Start() override
1757 {}
1758 void Stop() override
1759 {}
Ferran Balaguer1b941722019-08-28 16:57:18 +01001760 };
1761
Matteo Martincighe8485382019-10-10 14:08:21 +01001762 class TestReadCounterValues : public IReadCounterValues
1763 {
Keith Davis3201eea2019-10-24 17:30:41 +01001764 bool IsCounterRegistered(uint16_t counterUid) const override
1765 {
Jan Eilers8eb25602020-03-09 12:13:48 +00001766 armnn::IgnoreUnused(counterUid);
Keith Davis3201eea2019-10-24 17:30:41 +01001767 return true;
1768 }
1769 uint16_t GetCounterCount() const override
1770 {
1771 return 0;
1772 }
Finn Williamsf3fcf322020-05-11 14:38:02 +01001773 uint32_t GetAbsoluteCounterValue(uint16_t counterUid) const override
1774 {
1775 armnn::IgnoreUnused(counterUid);
1776 return 0;
1777 }
1778 uint32_t GetDeltaCounterValue(uint16_t counterUid) override
Keith Davis3201eea2019-10-24 17:30:41 +01001779 {
Jan Eilers8eb25602020-03-09 12:13:48 +00001780 armnn::IgnoreUnused(counterUid);
Keith Davis3201eea2019-10-24 17:30:41 +01001781 return 0;
1782 }
Matteo Martincighe8485382019-10-10 14:08:21 +01001783 };
Jim Flynn397043f2019-10-17 17:37:10 +01001784 const uint32_t familyId = 0;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001785 const uint32_t packetId = 0x40000;
1786
1787 uint32_t version = 1;
Finn Williams032bc742020-02-12 11:02:34 +00001788 const std::unordered_map<armnn::BackendId,
1789 std::shared_ptr<armnn::profiling::IBackendProfilingContext>> backendProfilingContext;
1790 CounterIdMap counterIdMap;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001791 Holder holder;
1792 TestCaptureThread captureThread;
Matteo Martincighe8485382019-10-10 14:08:21 +01001793 TestReadCounterValues readCounterValues;
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001794 MockBufferManager mockBuffer(512);
Sadik Armagan3896b472020-02-10 12:24:15 +00001795 SendCounterPacket sendCounterPacket(mockBuffer);
1796 SendThread sendThread(profilingStateMachine, mockBuffer, sendCounterPacket);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001797
Matthew Sloyan371b70e2020-09-11 10:14:57 +01001798 uint32_t sizeOfUint32 = armnn::numeric_cast<uint32_t>(sizeof(uint32_t));
1799 uint32_t sizeOfUint16 = armnn::numeric_cast<uint32_t>(sizeof(uint16_t));
Ferran Balaguer1b941722019-08-28 16:57:18 +01001800
1801 // Data with period and counters
Colm Donelan02705242019-11-14 14:19:07 +00001802 uint32_t period1 = armnn::LOWEST_CAPTURE_PERIOD;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001803 uint32_t dataLength1 = 8;
Keith Davis3201eea2019-10-24 17:30:41 +01001804 uint32_t offset = 0;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001805
Matteo Martincigh67ef2a52019-10-10 13:29:02 +01001806 std::unique_ptr<unsigned char[]> uniqueData1 = std::make_unique<unsigned char[]>(dataLength1);
Keith Davis3201eea2019-10-24 17:30:41 +01001807 unsigned char* data1 = reinterpret_cast<unsigned char*>(uniqueData1.get());
FinnWilliamsArma0c78712019-09-16 12:06:47 +01001808
Ferran Balaguer1b941722019-08-28 16:57:18 +01001809 WriteUint32(data1, offset, period1);
1810 offset += sizeOfUint32;
1811 WriteUint16(data1, offset, 4000);
1812 offset += sizeOfUint16;
1813 WriteUint16(data1, offset, 5000);
1814
Jim Flynnbbfe6032020-07-20 16:57:44 +01001815 arm::pipe::Packet packetA(packetId, dataLength1, uniqueData1);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001816
Finn Williams032bc742020-02-12 11:02:34 +00001817 PeriodicCounterSelectionCommandHandler commandHandler(familyId, packetId, version, backendProfilingContext,
1818 counterIdMap, holder, 10000u, captureThread,
Keith Davis3201eea2019-10-24 17:30:41 +01001819 readCounterValues, sendCounterPacket, profilingStateMachine);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001820
Matteo Martincighe8485382019-10-10 14:08:21 +01001821 profilingStateMachine.TransitionToState(ProfilingState::Uninitialised);
1822 BOOST_CHECK_THROW(commandHandler(packetA), armnn::RuntimeException);
1823 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
1824 BOOST_CHECK_THROW(commandHandler(packetA), armnn::RuntimeException);
1825 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
1826 BOOST_CHECK_THROW(commandHandler(packetA), armnn::RuntimeException);
1827 profilingStateMachine.TransitionToState(ProfilingState::Active);
1828 BOOST_CHECK_NO_THROW(commandHandler(packetA));
1829
1830 const std::vector<uint16_t> counterIdsA = holder.GetCaptureData().GetCounterIds();
Ferran Balaguer1b941722019-08-28 16:57:18 +01001831
1832 BOOST_TEST(holder.GetCaptureData().GetCapturePeriod() == period1);
Matteo Martincighe8485382019-10-10 14:08:21 +01001833 BOOST_TEST(counterIdsA.size() == 2);
1834 BOOST_TEST(counterIdsA[0] == 4000);
1835 BOOST_TEST(counterIdsA[1] == 5000);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001836
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001837 auto readBuffer = mockBuffer.GetReadableBuffer();
Ferran Balaguer1b941722019-08-28 16:57:18 +01001838
1839 offset = 0;
1840
1841 uint32_t headerWord0 = ReadUint32(readBuffer, offset);
1842 offset += sizeOfUint32;
1843 uint32_t headerWord1 = ReadUint32(readBuffer, offset);
1844 offset += sizeOfUint32;
1845 uint32_t period = ReadUint32(readBuffer, offset);
1846
Colm Donelan02705242019-11-14 14:19:07 +00001847 BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family
1848 BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 4); // packet id
1849 BOOST_TEST(headerWord1 == 8); // data length
1850 BOOST_TEST(period == armnn::LOWEST_CAPTURE_PERIOD); // capture period
Ferran Balaguer1b941722019-08-28 16:57:18 +01001851
1852 uint16_t counterId = 0;
1853 offset += sizeOfUint32;
1854 counterId = ReadUint16(readBuffer, offset);
1855 BOOST_TEST(counterId == 4000);
1856 offset += sizeOfUint16;
1857 counterId = ReadUint16(readBuffer, offset);
1858 BOOST_TEST(counterId == 5000);
1859
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001860 mockBuffer.MarkRead(readBuffer);
1861
Ferran Balaguer1b941722019-08-28 16:57:18 +01001862 // Data with period only
Colm Donelan02705242019-11-14 14:19:07 +00001863 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 +01001864 uint32_t dataLength2 = 4;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001865
Matteo Martincigh67ef2a52019-10-10 13:29:02 +01001866 std::unique_ptr<unsigned char[]> uniqueData2 = std::make_unique<unsigned char[]>(dataLength2);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001867
FinnWilliamsArma0c78712019-09-16 12:06:47 +01001868 WriteUint32(reinterpret_cast<unsigned char*>(uniqueData2.get()), 0, period2);
1869
Jim Flynnbbfe6032020-07-20 16:57:44 +01001870 arm::pipe::Packet packetB(packetId, dataLength2, uniqueData2);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001871
1872 commandHandler(packetB);
1873
Matteo Martincighe8485382019-10-10 14:08:21 +01001874 const std::vector<uint16_t> counterIdsB = holder.GetCaptureData().GetCounterIds();
Ferran Balaguer1b941722019-08-28 16:57:18 +01001875
Colm Donelan02705242019-11-14 14:19:07 +00001876 // Value should have been pulled up from 9000 to LOWEST_CAPTURE_PERIOD.
1877 BOOST_TEST(holder.GetCaptureData().GetCapturePeriod() == armnn::LOWEST_CAPTURE_PERIOD);
Matteo Martincighe8485382019-10-10 14:08:21 +01001878 BOOST_TEST(counterIdsB.size() == 0);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001879
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001880 readBuffer = mockBuffer.GetReadableBuffer();
Ferran Balaguer1b941722019-08-28 16:57:18 +01001881
1882 offset = 0;
1883
1884 headerWord0 = ReadUint32(readBuffer, offset);
1885 offset += sizeOfUint32;
1886 headerWord1 = ReadUint32(readBuffer, offset);
1887 offset += sizeOfUint32;
1888 period = ReadUint32(readBuffer, offset);
1889
Colm Donelan02705242019-11-14 14:19:07 +00001890 BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family
1891 BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 4); // packet id
1892 BOOST_TEST(headerWord1 == 4); // data length
1893 BOOST_TEST(period == armnn::LOWEST_CAPTURE_PERIOD); // capture period
Ferran Balaguer1b941722019-08-28 16:57:18 +01001894}
1895
Keith Davis33ed2212020-03-30 10:43:41 +01001896BOOST_AUTO_TEST_CASE(CheckTimelineActivationAndDeactivation)
1897{
1898 class TestReportStructure : public IReportStructure
1899 {
1900 public:
1901 virtual void ReportStructure() override
1902 {
1903 m_ReportStructureCalled = true;
1904 }
1905
1906 bool m_ReportStructureCalled = false;
1907 };
1908
1909 class TestNotifyBackends : public INotifyBackends
1910 {
1911 public:
1912 TestNotifyBackends() : m_timelineReporting(false) {}
1913 virtual void NotifyBackendsForTimelineReporting() override
1914 {
1915 m_TestNotifyBackendsCalled = m_timelineReporting.load();
1916 }
1917
1918 bool m_TestNotifyBackendsCalled = false;
1919 std::atomic<bool> m_timelineReporting;
1920 };
1921
Jim Flynnbbfe6032020-07-20 16:57:44 +01001922 arm::pipe::PacketVersionResolver packetVersionResolver;
Keith Davis33ed2212020-03-30 10:43:41 +01001923
1924 BufferManager bufferManager(512);
1925 SendTimelinePacket sendTimelinePacket(bufferManager);
1926 ProfilingStateMachine stateMachine;
1927 TestReportStructure testReportStructure;
1928 TestNotifyBackends testNotifyBackends;
1929
1930 profiling::ActivateTimelineReportingCommandHandler activateTimelineReportingCommandHandler(0,
1931 6,
1932 packetVersionResolver.ResolvePacketVersion(0, 6)
1933 .GetEncodedValue(),
1934 sendTimelinePacket,
1935 stateMachine,
1936 testReportStructure,
1937 testNotifyBackends.m_timelineReporting,
1938 testNotifyBackends);
1939
1940 // Write an "ActivateTimelineReporting" packet into the mock profiling connection, to simulate an input from an
1941 // external profiling service
1942 const uint32_t packetFamily1 = 0;
1943 const uint32_t packetId1 = 6;
1944 uint32_t packetHeader1 = ConstructHeader(packetFamily1, packetId1);
1945
1946 // Create the ActivateTimelineReportingPacket
Jim Flynnbbfe6032020-07-20 16:57:44 +01001947 arm::pipe::Packet ActivateTimelineReportingPacket(packetHeader1); // Length == 0
Keith Davis33ed2212020-03-30 10:43:41 +01001948
1949 BOOST_CHECK_THROW(
1950 activateTimelineReportingCommandHandler.operator()(ActivateTimelineReportingPacket), armnn::Exception);
1951
1952 stateMachine.TransitionToState(ProfilingState::NotConnected);
1953 BOOST_CHECK_THROW(
1954 activateTimelineReportingCommandHandler.operator()(ActivateTimelineReportingPacket), armnn::Exception);
1955
1956 stateMachine.TransitionToState(ProfilingState::WaitingForAck);
1957 BOOST_CHECK_THROW(
1958 activateTimelineReportingCommandHandler.operator()(ActivateTimelineReportingPacket), armnn::Exception);
1959
1960 stateMachine.TransitionToState(ProfilingState::Active);
1961 activateTimelineReportingCommandHandler.operator()(ActivateTimelineReportingPacket);
1962
1963 BOOST_CHECK(testReportStructure.m_ReportStructureCalled);
1964 BOOST_CHECK(testNotifyBackends.m_TestNotifyBackendsCalled);
1965 BOOST_CHECK(testNotifyBackends.m_timelineReporting.load());
1966
1967 DeactivateTimelineReportingCommandHandler deactivateTimelineReportingCommandHandler(0,
1968 7,
1969 packetVersionResolver.ResolvePacketVersion(0, 7).GetEncodedValue(),
1970 testNotifyBackends.m_timelineReporting,
1971 stateMachine,
1972 testNotifyBackends);
1973
1974 const uint32_t packetFamily2 = 0;
1975 const uint32_t packetId2 = 7;
1976 uint32_t packetHeader2 = ConstructHeader(packetFamily2, packetId2);
1977
1978 // Create the DeactivateTimelineReportingPacket
Jim Flynnbbfe6032020-07-20 16:57:44 +01001979 arm::pipe::Packet deactivateTimelineReportingPacket(packetHeader2); // Length == 0
Keith Davis33ed2212020-03-30 10:43:41 +01001980
1981 stateMachine.Reset();
1982 BOOST_CHECK_THROW(
1983 deactivateTimelineReportingCommandHandler.operator()(deactivateTimelineReportingPacket), armnn::Exception);
1984
1985 stateMachine.TransitionToState(ProfilingState::NotConnected);
1986 BOOST_CHECK_THROW(
1987 deactivateTimelineReportingCommandHandler.operator()(deactivateTimelineReportingPacket), armnn::Exception);
1988
1989 stateMachine.TransitionToState(ProfilingState::WaitingForAck);
1990 BOOST_CHECK_THROW(
1991 deactivateTimelineReportingCommandHandler.operator()(deactivateTimelineReportingPacket), armnn::Exception);
1992
1993 stateMachine.TransitionToState(ProfilingState::Active);
1994 deactivateTimelineReportingCommandHandler.operator()(deactivateTimelineReportingPacket);
1995
1996 BOOST_CHECK(!testNotifyBackends.m_TestNotifyBackendsCalled);
1997 BOOST_CHECK(!testNotifyBackends.m_timelineReporting.load());
1998}
1999
2000BOOST_AUTO_TEST_CASE(CheckProfilingServiceNotActive)
2001{
2002 using namespace armnn;
2003 using namespace armnn::profiling;
2004
2005 // Create runtime in which the test will run
2006 armnn::IRuntime::CreationOptions options;
2007 options.m_ProfilingOptions.m_EnableProfiling = true;
2008
Kevin Mayd92a6e42021-02-04 10:27:41 +00002009 armnn::RuntimeImpl runtime(options);
Keith Davis33ed2212020-03-30 10:43:41 +01002010 profiling::ProfilingServiceRuntimeHelper profilingServiceHelper(GetProfilingService(&runtime));
2011 profilingServiceHelper.ForceTransitionToState(ProfilingState::NotConnected);
2012 profilingServiceHelper.ForceTransitionToState(ProfilingState::WaitingForAck);
2013 profilingServiceHelper.ForceTransitionToState(ProfilingState::Active);
2014
2015 profiling::BufferManager& bufferManager = profilingServiceHelper.GetProfilingBufferManager();
2016 auto readableBuffer = bufferManager.GetReadableBuffer();
2017
2018 // Profiling is enabled, the post-optimisation structure should be created
2019 BOOST_CHECK(readableBuffer == nullptr);
2020}
2021
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002022BOOST_AUTO_TEST_CASE(CheckConnectionAcknowledged)
2023{
Keith Davis3201eea2019-10-24 17:30:41 +01002024 const uint32_t packetFamilyId = 0;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002025 const uint32_t connectionPacketId = 0x10000;
Keith Davis3201eea2019-10-24 17:30:41 +01002026 const uint32_t version = 1;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002027
Matthew Sloyan371b70e2020-09-11 10:14:57 +01002028 uint32_t sizeOfUint32 = armnn::numeric_cast<uint32_t>(sizeof(uint32_t));
2029 uint32_t sizeOfUint16 = armnn::numeric_cast<uint32_t>(sizeof(uint16_t));
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002030
2031 // Data with period and counters
Keith Davis3201eea2019-10-24 17:30:41 +01002032 uint32_t period1 = 10;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002033 uint32_t dataLength1 = 8;
Keith Davis3201eea2019-10-24 17:30:41 +01002034 uint32_t offset = 0;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002035
Matteo Martincigh67ef2a52019-10-10 13:29:02 +01002036 std::unique_ptr<unsigned char[]> uniqueData1 = std::make_unique<unsigned char[]>(dataLength1);
Keith Davis3201eea2019-10-24 17:30:41 +01002037 unsigned char* data1 = reinterpret_cast<unsigned char*>(uniqueData1.get());
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002038
2039 WriteUint32(data1, offset, period1);
2040 offset += sizeOfUint32;
2041 WriteUint16(data1, offset, 4000);
2042 offset += sizeOfUint16;
2043 WriteUint16(data1, offset, 5000);
2044
Jim Flynnbbfe6032020-07-20 16:57:44 +01002045 arm::pipe::Packet packetA(connectionPacketId, dataLength1, uniqueData1);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002046
2047 ProfilingStateMachine profilingState(ProfilingState::Uninitialised);
2048 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002049 CounterDirectory counterDirectory;
2050 MockBufferManager mockBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002051 SendCounterPacket sendCounterPacket(mockBuffer);
2052 SendThread sendThread(profilingState, mockBuffer, sendCounterPacket);
Matteo Martincighcdfb9412019-11-08 11:23:06 +00002053 SendTimelinePacket sendTimelinePacket(mockBuffer);
Jim Flynn6398a982020-05-27 17:05:21 +01002054 MockProfilingServiceStatus mockProfilingServiceStatus;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002055
Jim Flynn6398a982020-05-27 17:05:21 +01002056 ConnectionAcknowledgedCommandHandler commandHandler(packetFamilyId,
2057 connectionPacketId,
2058 version,
2059 counterDirectory,
2060 sendCounterPacket,
2061 sendTimelinePacket,
2062 profilingState,
2063 mockProfilingServiceStatus);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002064
2065 // command handler received packet on ProfilingState::Uninitialised
2066 BOOST_CHECK_THROW(commandHandler(packetA), armnn::Exception);
2067
2068 profilingState.TransitionToState(ProfilingState::NotConnected);
2069 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::NotConnected);
2070 // command handler received packet on ProfilingState::NotConnected
2071 BOOST_CHECK_THROW(commandHandler(packetA), armnn::Exception);
2072
2073 profilingState.TransitionToState(ProfilingState::WaitingForAck);
2074 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::WaitingForAck);
2075 // command handler received packet on ProfilingState::WaitingForAck
Matteo Martincighd0613b52019-10-09 16:47:04 +01002076 BOOST_CHECK_NO_THROW(commandHandler(packetA));
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002077 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::Active);
2078
2079 // command handler received packet on ProfilingState::Active
Matteo Martincighd0613b52019-10-09 16:47:04 +01002080 BOOST_CHECK_NO_THROW(commandHandler(packetA));
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002081 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::Active);
2082
2083 // command handler received different packet
2084 const uint32_t differentPacketId = 0x40000;
Jim Flynnbbfe6032020-07-20 16:57:44 +01002085 arm::pipe::Packet packetB(differentPacketId, dataLength1, uniqueData1);
Matteo Martincighd0613b52019-10-09 16:47:04 +01002086 profilingState.TransitionToState(ProfilingState::NotConnected);
2087 profilingState.TransitionToState(ProfilingState::WaitingForAck);
Jim Flynn6398a982020-05-27 17:05:21 +01002088 ConnectionAcknowledgedCommandHandler differentCommandHandler(packetFamilyId,
2089 differentPacketId,
2090 version,
2091 counterDirectory,
2092 sendCounterPacket,
2093 sendTimelinePacket,
2094 profilingState,
2095 mockProfilingServiceStatus);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002096 BOOST_CHECK_THROW(differentCommandHandler(packetB), armnn::Exception);
2097}
2098
Sadik Armagana97a0be2020-03-03 10:44:56 +00002099BOOST_AUTO_TEST_CASE(CheckSocketConnectionException)
Teresa Charlin9bab4962019-09-06 12:28:35 +01002100{
Sadik Armagana97a0be2020-03-03 10:44:56 +00002101 // Check that creating a SocketProfilingConnection armnnProfiling in an exception as the Gator UDS doesn't exist.
Jim Flynnbbfe6032020-07-20 16:57:44 +01002102 BOOST_CHECK_THROW(new SocketProfilingConnection(), arm::pipe::SocketConnectionException);
Sadik Armagana97a0be2020-03-03 10:44:56 +00002103}
2104
2105BOOST_AUTO_TEST_CASE(CheckSocketConnectionException2)
2106{
2107 try
2108 {
2109 new SocketProfilingConnection();
2110 }
Jim Flynnbbfe6032020-07-20 16:57:44 +01002111 catch (const arm::pipe::SocketConnectionException& ex)
Sadik Armagana97a0be2020-03-03 10:44:56 +00002112 {
2113 BOOST_CHECK(ex.GetSocketFd() == 0);
Francis Murtagh6f377b72021-01-05 14:42:00 +00002114 BOOST_CHECK(ex.GetErrorNo() == ECONNREFUSED);
Sadik Armagana97a0be2020-03-03 10:44:56 +00002115 BOOST_CHECK(ex.what()
2116 == std::string("SocketProfilingConnection: Cannot connect to stream socket: Connection refused"));
2117 }
Teresa Charlin9bab4962019-09-06 12:28:35 +01002118}
2119
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002120BOOST_AUTO_TEST_CASE(SwTraceIsValidCharTest)
2121{
2122 // Only ASCII 7-bit encoding supported
2123 for (unsigned char c = 0; c < 128; c++)
2124 {
Jim Flynnbbfe6032020-07-20 16:57:44 +01002125 BOOST_CHECK(arm::pipe::SwTraceCharPolicy::IsValidChar(c));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002126 }
2127
2128 // Not ASCII
2129 for (unsigned char c = 255; c >= 128; c++)
2130 {
Jim Flynnbbfe6032020-07-20 16:57:44 +01002131 BOOST_CHECK(!arm::pipe::SwTraceCharPolicy::IsValidChar(c));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002132 }
2133}
2134
2135BOOST_AUTO_TEST_CASE(SwTraceIsValidNameCharTest)
2136{
2137 // Only alpha-numeric and underscore ASCII 7-bit encoding supported
2138 const unsigned char validChars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
2139 for (unsigned char i = 0; i < sizeof(validChars) / sizeof(validChars[0]) - 1; i++)
2140 {
Jim Flynnbbfe6032020-07-20 16:57:44 +01002141 BOOST_CHECK(arm::pipe::SwTraceNameCharPolicy::IsValidChar(validChars[i]));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002142 }
2143
2144 // Non alpha-numeric chars
2145 for (unsigned char c = 0; c < 48; c++)
2146 {
Jim Flynnbbfe6032020-07-20 16:57:44 +01002147 BOOST_CHECK(!arm::pipe::SwTraceNameCharPolicy::IsValidChar(c));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002148 }
2149 for (unsigned char c = 58; c < 65; c++)
2150 {
Jim Flynnbbfe6032020-07-20 16:57:44 +01002151 BOOST_CHECK(!arm::pipe::SwTraceNameCharPolicy::IsValidChar(c));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002152 }
2153 for (unsigned char c = 91; c < 95; c++)
2154 {
Jim Flynnbbfe6032020-07-20 16:57:44 +01002155 BOOST_CHECK(!arm::pipe::SwTraceNameCharPolicy::IsValidChar(c));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002156 }
2157 for (unsigned char c = 96; c < 97; c++)
2158 {
Jim Flynnbbfe6032020-07-20 16:57:44 +01002159 BOOST_CHECK(!arm::pipe::SwTraceNameCharPolicy::IsValidChar(c));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002160 }
2161 for (unsigned char c = 123; c < 128; c++)
2162 {
Jim Flynnbbfe6032020-07-20 16:57:44 +01002163 BOOST_CHECK(!arm::pipe::SwTraceNameCharPolicy::IsValidChar(c));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002164 }
2165
2166 // Not ASCII
2167 for (unsigned char c = 255; c >= 128; c++)
2168 {
Jim Flynnbbfe6032020-07-20 16:57:44 +01002169 BOOST_CHECK(!arm::pipe::SwTraceNameCharPolicy::IsValidChar(c));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002170 }
2171}
2172
2173BOOST_AUTO_TEST_CASE(IsValidSwTraceStringTest)
2174{
2175 // Valid SWTrace strings
Jim Flynnbbfe6032020-07-20 16:57:44 +01002176 BOOST_CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>(""));
2177 BOOST_CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>("_"));
2178 BOOST_CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>("0123"));
2179 BOOST_CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>("valid_string"));
2180 BOOST_CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>("VALID_string_456"));
2181 BOOST_CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>(" "));
2182 BOOST_CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>("valid string"));
2183 BOOST_CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>("!$%"));
2184 BOOST_CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>("valid|\\~string#123"));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002185
2186 // Invalid SWTrace strings
Jim Flynnbbfe6032020-07-20 16:57:44 +01002187 BOOST_CHECK(!arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>("€£"));
2188 BOOST_CHECK(!arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>("invalid‡string"));
2189 BOOST_CHECK(!arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>("12Ž34"));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002190}
2191
2192BOOST_AUTO_TEST_CASE(IsValidSwTraceNameStringTest)
2193{
2194 // Valid SWTrace name strings
Jim Flynnbbfe6032020-07-20 16:57:44 +01002195 BOOST_CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>(""));
2196 BOOST_CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>("_"));
2197 BOOST_CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>("0123"));
2198 BOOST_CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>("valid_string"));
2199 BOOST_CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>("VALID_string_456"));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002200
2201 // Invalid SWTrace name strings
Jim Flynnbbfe6032020-07-20 16:57:44 +01002202 BOOST_CHECK(!arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>(" "));
2203 BOOST_CHECK(!arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>("invalid string"));
2204 BOOST_CHECK(!arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>("!$%"));
2205 BOOST_CHECK(!arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>("invalid|\\~string#123"));
2206 BOOST_CHECK(!arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>("€£"));
2207 BOOST_CHECK(!arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>("invalid‡string"));
2208 BOOST_CHECK(!arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>("12Ž34"));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002209}
2210
2211template <typename SwTracePolicy>
2212void StringToSwTraceStringTestHelper(const std::string& testString, std::vector<uint32_t> buffer, size_t expectedSize)
2213{
2214 // Convert the test string to a SWTrace string
Jim Flynnbbfe6032020-07-20 16:57:44 +01002215 BOOST_CHECK(arm::pipe::StringToSwTraceString<SwTracePolicy>(testString, buffer));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002216
2217 // The buffer must contain at least the length of the string
2218 BOOST_CHECK(!buffer.empty());
2219
2220 // The buffer must be of the expected size (in words)
2221 BOOST_CHECK(buffer.size() == expectedSize);
2222
2223 // The first word of the byte must be the length of the string including the null-terminator
2224 BOOST_CHECK(buffer[0] == testString.size() + 1);
2225
2226 // The contents of the buffer must match the test string
2227 BOOST_CHECK(std::memcmp(testString.data(), buffer.data() + 1, testString.size()) == 0);
2228
2229 // The buffer must include the null-terminator at the end of the string
2230 size_t nullTerminatorIndex = sizeof(uint32_t) + testString.size();
Keith Davis3201eea2019-10-24 17:30:41 +01002231 BOOST_CHECK(reinterpret_cast<unsigned char*>(buffer.data())[nullTerminatorIndex] == '\0');
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002232}
2233
2234BOOST_AUTO_TEST_CASE(StringToSwTraceStringTest)
2235{
2236 std::vector<uint32_t> buffer;
2237
2238 // Valid SWTrace strings (expected size in words)
Jim Flynnbbfe6032020-07-20 16:57:44 +01002239 StringToSwTraceStringTestHelper<arm::pipe::SwTraceCharPolicy>("", buffer, 2);
2240 StringToSwTraceStringTestHelper<arm::pipe::SwTraceCharPolicy>("_", buffer, 2);
2241 StringToSwTraceStringTestHelper<arm::pipe::SwTraceCharPolicy>("0123", buffer, 3);
2242 StringToSwTraceStringTestHelper<arm::pipe::SwTraceCharPolicy>("valid_string", buffer, 5);
2243 StringToSwTraceStringTestHelper<arm::pipe::SwTraceCharPolicy>("VALID_string_456", buffer, 6);
2244 StringToSwTraceStringTestHelper<arm::pipe::SwTraceCharPolicy>(" ", buffer, 2);
2245 StringToSwTraceStringTestHelper<arm::pipe::SwTraceCharPolicy>("valid string", buffer, 5);
2246 StringToSwTraceStringTestHelper<arm::pipe::SwTraceCharPolicy>("!$%", buffer, 2);
2247 StringToSwTraceStringTestHelper<arm::pipe::SwTraceCharPolicy>("valid|\\~string#123", buffer, 6);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002248
2249 // Invalid SWTrace strings
Jim Flynnbbfe6032020-07-20 16:57:44 +01002250 BOOST_CHECK(!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceCharPolicy>("€£", buffer));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002251 BOOST_CHECK(buffer.empty());
Jim Flynnbbfe6032020-07-20 16:57:44 +01002252 BOOST_CHECK(!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceCharPolicy>("invalid‡string", buffer));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002253 BOOST_CHECK(buffer.empty());
Jim Flynnbbfe6032020-07-20 16:57:44 +01002254 BOOST_CHECK(!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceCharPolicy>("12Ž34", buffer));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002255 BOOST_CHECK(buffer.empty());
2256}
2257
2258BOOST_AUTO_TEST_CASE(StringToSwTraceNameStringTest)
2259{
2260 std::vector<uint32_t> buffer;
2261
2262 // Valid SWTrace namestrings (expected size in words)
Jim Flynnbbfe6032020-07-20 16:57:44 +01002263 StringToSwTraceStringTestHelper<arm::pipe::SwTraceNameCharPolicy>("", buffer, 2);
2264 StringToSwTraceStringTestHelper<arm::pipe::SwTraceNameCharPolicy>("_", buffer, 2);
2265 StringToSwTraceStringTestHelper<arm::pipe::SwTraceNameCharPolicy>("0123", buffer, 3);
2266 StringToSwTraceStringTestHelper<arm::pipe::SwTraceNameCharPolicy>("valid_string", buffer, 5);
2267 StringToSwTraceStringTestHelper<arm::pipe::SwTraceNameCharPolicy>("VALID_string_456", buffer, 6);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002268
2269 // Invalid SWTrace namestrings
Jim Flynnbbfe6032020-07-20 16:57:44 +01002270 BOOST_CHECK(!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceNameCharPolicy>(" ", buffer));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002271 BOOST_CHECK(buffer.empty());
Jim Flynnbbfe6032020-07-20 16:57:44 +01002272 BOOST_CHECK(!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceNameCharPolicy>("invalid string", buffer));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002273 BOOST_CHECK(buffer.empty());
Jim Flynnbbfe6032020-07-20 16:57:44 +01002274 BOOST_CHECK(!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceNameCharPolicy>("!$%", buffer));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002275 BOOST_CHECK(buffer.empty());
Jim Flynnbbfe6032020-07-20 16:57:44 +01002276 BOOST_CHECK(!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceNameCharPolicy>("invalid|\\~string#123", buffer));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002277 BOOST_CHECK(buffer.empty());
Jim Flynnbbfe6032020-07-20 16:57:44 +01002278 BOOST_CHECK(!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceNameCharPolicy>("€£", buffer));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002279 BOOST_CHECK(buffer.empty());
Jim Flynnbbfe6032020-07-20 16:57:44 +01002280 BOOST_CHECK(!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceNameCharPolicy>("invalid‡string", buffer));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002281 BOOST_CHECK(buffer.empty());
Jim Flynnbbfe6032020-07-20 16:57:44 +01002282 BOOST_CHECK(!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceNameCharPolicy>("12Ž34", buffer));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002283 BOOST_CHECK(buffer.empty());
2284}
2285
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002286BOOST_AUTO_TEST_CASE(CheckPeriodicCounterCaptureThread)
2287{
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002288 class CaptureReader : public IReadCounterValues
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002289 {
2290 public:
Finn Williamsf4d59a62019-10-14 15:55:18 +01002291 CaptureReader(uint16_t counterSize)
2292 {
Keith Davis3201eea2019-10-24 17:30:41 +01002293 for (uint16_t i = 0; i < counterSize; ++i)
Finn Williamsf4d59a62019-10-14 15:55:18 +01002294 {
2295 m_Data[i] = 0;
2296 }
2297 m_CounterSize = counterSize;
2298 }
2299 //not used
Matteo Martincighe8485382019-10-10 14:08:21 +01002300 bool IsCounterRegistered(uint16_t counterUid) const override
2301 {
Jan Eilers8eb25602020-03-09 12:13:48 +00002302 armnn::IgnoreUnused(counterUid);
Finn Williamsf4d59a62019-10-14 15:55:18 +01002303 return false;
Matteo Martincighe8485382019-10-10 14:08:21 +01002304 }
2305
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002306 uint16_t GetCounterCount() const override
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002307 {
Finn Williamsf4d59a62019-10-14 15:55:18 +01002308 return m_CounterSize;
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002309 }
2310
Finn Williamsf3fcf322020-05-11 14:38:02 +01002311 uint32_t GetAbsoluteCounterValue(uint16_t counterUid) const override
2312 {
2313 if (counterUid > m_CounterSize)
2314 {
2315 BOOST_FAIL("Invalid counter Uid");
2316 }
2317 return m_Data.at(counterUid).load();
2318 }
2319
2320 uint32_t GetDeltaCounterValue(uint16_t counterUid) override
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002321 {
Keith Davis3201eea2019-10-24 17:30:41 +01002322 if (counterUid > m_CounterSize)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002323 {
Finn Williamsf4d59a62019-10-14 15:55:18 +01002324 BOOST_FAIL("Invalid counter Uid");
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002325 }
Matteo Martincighe8485382019-10-10 14:08:21 +01002326 return m_Data.at(counterUid).load();
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002327 }
2328
Matteo Martincighe8485382019-10-10 14:08:21 +01002329 void SetCounterValue(uint16_t counterUid, uint32_t value)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002330 {
Keith Davis3201eea2019-10-24 17:30:41 +01002331 if (counterUid > m_CounterSize)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002332 {
Finn Williamsf4d59a62019-10-14 15:55:18 +01002333 BOOST_FAIL("Invalid counter Uid");
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002334 }
Finn Williamsf4d59a62019-10-14 15:55:18 +01002335 m_Data.at(counterUid).store(value);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002336 }
2337
2338 private:
Matteo Martincighe8485382019-10-10 14:08:21 +01002339 std::unordered_map<uint16_t, std::atomic<uint32_t>> m_Data;
Finn Williamsf4d59a62019-10-14 15:55:18 +01002340 uint16_t m_CounterSize;
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002341 };
2342
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002343 ProfilingStateMachine profilingStateMachine;
2344
Finn Williams032bc742020-02-12 11:02:34 +00002345 const std::unordered_map<armnn::BackendId,
2346 std::shared_ptr<armnn::profiling::IBackendProfilingContext>> backendProfilingContext;
2347 CounterIdMap counterIdMap;
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002348 Holder data;
2349 std::vector<uint16_t> captureIds1 = { 0, 1 };
2350 std::vector<uint16_t> captureIds2;
2351
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002352 MockBufferManager mockBuffer(512);
Sadik Armagan3896b472020-02-10 12:24:15 +00002353 SendCounterPacket sendCounterPacket(mockBuffer);
2354 SendThread sendThread(profilingStateMachine, mockBuffer, sendCounterPacket);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002355
2356 std::vector<uint16_t> counterIds;
Finn Williamsf4d59a62019-10-14 15:55:18 +01002357 CaptureReader captureReader(2);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002358
Keith Davis3201eea2019-10-24 17:30:41 +01002359 unsigned int valueA = 10;
2360 unsigned int valueB = 15;
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002361 unsigned int numSteps = 5;
2362
Finn Williams032bc742020-02-12 11:02:34 +00002363 PeriodicCounterCapture periodicCounterCapture(std::ref(data), std::ref(sendCounterPacket), captureReader,
2364 counterIdMap, backendProfilingContext);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002365
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002366 for (unsigned int i = 0; i < numSteps; ++i)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002367 {
Finn Williams032bc742020-02-12 11:02:34 +00002368 data.SetCaptureData(1, captureIds1, {});
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002369 captureReader.SetCounterValue(0, valueA * (i + 1));
2370 captureReader.SetCounterValue(1, valueB * (i + 1));
2371
2372 periodicCounterCapture.Start();
Finn Williamsf4d59a62019-10-14 15:55:18 +01002373 periodicCounterCapture.Stop();
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002374 }
2375
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002376 auto buffer = mockBuffer.GetReadableBuffer();
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002377
2378 uint32_t headerWord0 = ReadUint32(buffer, 0);
2379 uint32_t headerWord1 = ReadUint32(buffer, 4);
2380
Jim Flynnfc365622019-12-04 10:07:20 +00002381 BOOST_TEST(((headerWord0 >> 26) & 0x0000003F) == 3); // packet family
Keith Davis3201eea2019-10-24 17:30:41 +01002382 BOOST_TEST(((headerWord0 >> 19) & 0x0000007F) == 0); // packet class
2383 BOOST_TEST(((headerWord0 >> 16) & 0x00000007) == 0); // packet type
Matteo Martincigh8d9590e2019-10-15 09:35:29 +01002384 BOOST_TEST(headerWord1 == 20);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002385
Keith Davis3201eea2019-10-24 17:30:41 +01002386 uint32_t offset = 16;
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002387 uint16_t readIndex = ReadUint16(buffer, offset);
2388 BOOST_TEST(0 == readIndex);
2389
2390 offset += 2;
2391 uint32_t readValue = ReadUint32(buffer, offset);
2392 BOOST_TEST((valueA * numSteps) == readValue);
2393
2394 offset += 4;
2395 readIndex = ReadUint16(buffer, offset);
2396 BOOST_TEST(1 == readIndex);
2397
2398 offset += 2;
2399 readValue = ReadUint32(buffer, offset);
2400 BOOST_TEST((valueB * numSteps) == readValue);
2401}
2402
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002403BOOST_AUTO_TEST_CASE(RequestCounterDirectoryCommandHandlerTest1)
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002404{
Jim Flynn397043f2019-10-17 17:37:10 +01002405 const uint32_t familyId = 0;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002406 const uint32_t packetId = 3;
Keith Davis3201eea2019-10-24 17:30:41 +01002407 const uint32_t version = 1;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002408 ProfilingStateMachine profilingStateMachine;
2409 CounterDirectory counterDirectory;
Matteo Martincigh9723d022019-11-13 10:56:41 +00002410 MockBufferManager mockBuffer1(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002411 SendCounterPacket sendCounterPacket(mockBuffer1);
2412 SendThread sendThread(profilingStateMachine, mockBuffer1, sendCounterPacket);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002413 MockBufferManager mockBuffer2(1024);
2414 SendTimelinePacket sendTimelinePacket(mockBuffer2);
Keith Davis3201eea2019-10-24 17:30:41 +01002415 RequestCounterDirectoryCommandHandler commandHandler(familyId, packetId, version, counterDirectory,
Matteo Martincigh9723d022019-11-13 10:56:41 +00002416 sendCounterPacket, sendTimelinePacket, profilingStateMachine);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002417
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002418 const uint32_t wrongPacketId = 47;
Keith Davis3201eea2019-10-24 17:30:41 +01002419 const uint32_t wrongHeader = (wrongPacketId & 0x000003FF) << 16;
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002420
Jim Flynnbbfe6032020-07-20 16:57:44 +01002421 arm::pipe::Packet wrongPacket(wrongHeader);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002422
2423 profilingStateMachine.TransitionToState(ProfilingState::Uninitialised);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002424 BOOST_CHECK_THROW(commandHandler(wrongPacket), armnn::RuntimeException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002425 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002426 BOOST_CHECK_THROW(commandHandler(wrongPacket), armnn::RuntimeException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002427 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002428 BOOST_CHECK_THROW(commandHandler(wrongPacket), armnn::RuntimeException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002429 profilingStateMachine.TransitionToState(ProfilingState::Active);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002430 BOOST_CHECK_THROW(commandHandler(wrongPacket), armnn::InvalidArgumentException); // Wrong packet
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002431
2432 const uint32_t rightHeader = (packetId & 0x000003FF) << 16;
2433
Jim Flynnbbfe6032020-07-20 16:57:44 +01002434 arm::pipe::Packet rightPacket(rightHeader);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002435
Matteo Martincigh9723d022019-11-13 10:56:41 +00002436 BOOST_CHECK_NO_THROW(commandHandler(rightPacket)); // Right packet
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002437
Matteo Martincigh9723d022019-11-13 10:56:41 +00002438 auto readBuffer1 = mockBuffer1.GetReadableBuffer();
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002439
Matteo Martincigh9723d022019-11-13 10:56:41 +00002440 uint32_t header1Word0 = ReadUint32(readBuffer1, 0);
2441 uint32_t header1Word1 = ReadUint32(readBuffer1, 4);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002442
Matteo Martincigh9723d022019-11-13 10:56:41 +00002443 // Counter directory packet
2444 BOOST_TEST(((header1Word0 >> 26) & 0x0000003F) == 0); // packet family
2445 BOOST_TEST(((header1Word0 >> 16) & 0x000003FF) == 2); // packet id
2446 BOOST_TEST(header1Word1 == 24); // data length
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002447
Matteo Martincigh9723d022019-11-13 10:56:41 +00002448 uint32_t bodyHeader1Word0 = ReadUint32(readBuffer1, 8);
Matthew Sloyan371b70e2020-09-11 10:14:57 +01002449 uint16_t deviceRecordCount = armnn::numeric_cast<uint16_t>(bodyHeader1Word0 >> 16);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002450 BOOST_TEST(deviceRecordCount == 0); // device_records_count
2451
2452 auto readBuffer2 = mockBuffer2.GetReadableBuffer();
2453
2454 uint32_t header2Word0 = ReadUint32(readBuffer2, 0);
2455 uint32_t header2Word1 = ReadUint32(readBuffer2, 4);
2456
2457 // Timeline message directory packet
2458 BOOST_TEST(((header2Word0 >> 26) & 0x0000003F) == 1); // packet family
2459 BOOST_TEST(((header2Word0 >> 16) & 0x000003FF) == 0); // packet id
Jim Flynn6398a982020-05-27 17:05:21 +01002460 BOOST_TEST(header2Word1 == 443); // data length
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002461}
2462
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002463BOOST_AUTO_TEST_CASE(RequestCounterDirectoryCommandHandlerTest2)
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002464{
Jim Flynn397043f2019-10-17 17:37:10 +01002465 const uint32_t familyId = 0;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002466 const uint32_t packetId = 3;
Keith Davis3201eea2019-10-24 17:30:41 +01002467 const uint32_t version = 1;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002468 ProfilingStateMachine profilingStateMachine;
2469 CounterDirectory counterDirectory;
Matteo Martincigh9723d022019-11-13 10:56:41 +00002470 MockBufferManager mockBuffer1(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002471 SendCounterPacket sendCounterPacket(mockBuffer1);
2472 SendThread sendThread(profilingStateMachine, mockBuffer1, sendCounterPacket);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002473 MockBufferManager mockBuffer2(1024);
2474 SendTimelinePacket sendTimelinePacket(mockBuffer2);
Keith Davis3201eea2019-10-24 17:30:41 +01002475 RequestCounterDirectoryCommandHandler commandHandler(familyId, packetId, version, counterDirectory,
Matteo Martincigh9723d022019-11-13 10:56:41 +00002476 sendCounterPacket, sendTimelinePacket, profilingStateMachine);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002477 const uint32_t header = (packetId & 0x000003FF) << 16;
Jim Flynnbbfe6032020-07-20 16:57:44 +01002478 const arm::pipe::Packet packet(header);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002479
Matteo Martincigh9723d022019-11-13 10:56:41 +00002480 const Device* device = counterDirectory.RegisterDevice("deviceA", 1);
2481 BOOST_CHECK(device != nullptr);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002482 const CounterSet* counterSet = counterDirectory.RegisterCounterSet("countersetA");
Matteo Martincigh9723d022019-11-13 10:56:41 +00002483 BOOST_CHECK(counterSet != nullptr);
Sadik Armagan4c998992020-02-25 12:44:44 +00002484 counterDirectory.RegisterCategory("categoryA");
Keith Davise394bd92019-12-02 15:12:19 +00002485 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID, 24,
2486 "categoryA", 0, 1, 2.0f, "counterA", "descA");
2487 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID, 25,
2488 "categoryA", 1, 1, 3.0f, "counterB", "descB");
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002489
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002490 profilingStateMachine.TransitionToState(ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002491 BOOST_CHECK_THROW(commandHandler(packet), armnn::RuntimeException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002492 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002493 BOOST_CHECK_THROW(commandHandler(packet), armnn::RuntimeException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002494 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002495 BOOST_CHECK_THROW(commandHandler(packet), armnn::RuntimeException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002496 profilingStateMachine.TransitionToState(ProfilingState::Active);
2497 BOOST_CHECK_NO_THROW(commandHandler(packet));
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002498
Matteo Martincigh9723d022019-11-13 10:56:41 +00002499 auto readBuffer1 = mockBuffer1.GetReadableBuffer();
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002500
Finn Williams985fecf2020-04-30 11:06:43 +01002501 const uint32_t header1Word0 = ReadUint32(readBuffer1, 0);
2502 const uint32_t header1Word1 = ReadUint32(readBuffer1, 4);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002503
Matteo Martincigh9723d022019-11-13 10:56:41 +00002504 BOOST_TEST(((header1Word0 >> 26) & 0x0000003F) == 0); // packet family
2505 BOOST_TEST(((header1Word0 >> 16) & 0x000003FF) == 2); // packet id
Sadik Armagan4c998992020-02-25 12:44:44 +00002506 BOOST_TEST(header1Word1 == 236); // data length
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002507
Finn Williams985fecf2020-04-30 11:06:43 +01002508 const uint32_t bodyHeaderSizeBytes = bodyHeaderSize * sizeof(uint32_t);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002509
Finn Williams985fecf2020-04-30 11:06:43 +01002510 const uint32_t bodyHeader1Word0 = ReadUint32(readBuffer1, 8);
2511 const uint32_t bodyHeader1Word1 = ReadUint32(readBuffer1, 12);
2512 const uint32_t bodyHeader1Word2 = ReadUint32(readBuffer1, 16);
2513 const uint32_t bodyHeader1Word3 = ReadUint32(readBuffer1, 20);
2514 const uint32_t bodyHeader1Word4 = ReadUint32(readBuffer1, 24);
2515 const uint32_t bodyHeader1Word5 = ReadUint32(readBuffer1, 28);
Matthew Sloyan371b70e2020-09-11 10:14:57 +01002516 const uint16_t deviceRecordCount = armnn::numeric_cast<uint16_t>(bodyHeader1Word0 >> 16);
2517 const uint16_t counterSetRecordCount = armnn::numeric_cast<uint16_t>(bodyHeader1Word2 >> 16);
2518 const uint16_t categoryRecordCount = armnn::numeric_cast<uint16_t>(bodyHeader1Word4 >> 16);
Finn Williams985fecf2020-04-30 11:06:43 +01002519 BOOST_TEST(deviceRecordCount == 1); // device_records_count
2520 BOOST_TEST(bodyHeader1Word1 == 0 + bodyHeaderSizeBytes); // device_records_pointer_table_offset
2521 BOOST_TEST(counterSetRecordCount == 1); // counter_set_count
2522 BOOST_TEST(bodyHeader1Word3 == 4 + bodyHeaderSizeBytes); // counter_set_pointer_table_offset
2523 BOOST_TEST(categoryRecordCount == 1); // categories_count
2524 BOOST_TEST(bodyHeader1Word5 == 8 + bodyHeaderSizeBytes); // categories_pointer_table_offset
2525
2526 const uint32_t deviceRecordOffset = ReadUint32(readBuffer1, 32);
Finn Williamsd44815f2020-05-01 13:25:55 +01002527 BOOST_TEST(deviceRecordOffset == 12);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002528
Finn Williams985fecf2020-04-30 11:06:43 +01002529 const uint32_t counterSetRecordOffset = ReadUint32(readBuffer1, 36);
Finn Williamsd44815f2020-05-01 13:25:55 +01002530 BOOST_TEST(counterSetRecordOffset == 28);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002531
Finn Williams985fecf2020-04-30 11:06:43 +01002532 const uint32_t categoryRecordOffset = ReadUint32(readBuffer1, 40);
Finn Williamsd44815f2020-05-01 13:25:55 +01002533 BOOST_TEST(categoryRecordOffset == 48);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002534
2535 auto readBuffer2 = mockBuffer2.GetReadableBuffer();
2536
Finn Williams985fecf2020-04-30 11:06:43 +01002537 const uint32_t header2Word0 = ReadUint32(readBuffer2, 0);
2538 const uint32_t header2Word1 = ReadUint32(readBuffer2, 4);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002539
2540 // Timeline message directory packet
2541 BOOST_TEST(((header2Word0 >> 26) & 0x0000003F) == 1); // packet family
2542 BOOST_TEST(((header2Word0 >> 16) & 0x000003FF) == 0); // packet id
Jim Flynn6398a982020-05-27 17:05:21 +01002543 BOOST_TEST(header2Word1 == 443); // data length
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002544}
2545
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002546BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodConnectionAcknowledgedPacket)
2547{
Finn Williamsa0de0562020-04-22 12:27:37 +01002548 unsigned int streamMetadataPacketsize = GetStreamMetaDataPacketSize();
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002549
Jim Flynn53e46992019-10-14 12:31:10 +01002550 // Reset the profiling service to the uninitialized state
Kevin Mayd92a6e42021-02-04 10:27:41 +00002551 armnn::IRuntime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002552 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +00002553 armnn::profiling::ProfilingService profilingService;
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002554 profilingService.ResetExternalProfilingOptions(options, true);
2555
Sadik Armagan3184c902020-03-18 10:57:30 +00002556 // Swap the profiling connection factory in the profiling service instance with our mock one
2557 SwapProfilingConnectionFactoryHelper helper(profilingService);
2558
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002559 // Bring the profiling service to the "WaitingForAck" state
2560 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002561 profilingService.Update(); // Initialize the counter directory
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002562 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002563 profilingService.Update(); // Create the profiling connection
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002564
Matteo Martincighd0613b52019-10-09 16:47:04 +01002565 // Get the mock profiling connection
2566 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2567 BOOST_CHECK(mockProfilingConnection);
2568
Matteo Martincighe8485382019-10-10 14:08:21 +01002569 // Remove the packets received so far
2570 mockProfilingConnection->Clear();
2571
2572 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002573 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002574
2575 // Wait for the Stream Metadata packet to be sent
Finn Williams09ad6f92019-12-19 17:05:18 +00002576 BOOST_CHECK(helper.WaitForPacketsSent(
2577 mockProfilingConnection, PacketType::StreamMetaData, streamMetadataPacketsize) >= 1);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002578
2579 // Write a valid "Connection Acknowledged" packet into the mock profiling connection, to simulate a valid
2580 // reply from an external profiling service
2581
2582 // Connection Acknowledged Packet header (word 0, word 1 is always zero):
2583 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2584 // 16:25 [10] packet_id: Packet identifier, value 0b0000000001
2585 // 8:15 [8] reserved: Reserved, value 0b00000000
2586 // 0:7 [8] reserved: Reserved, value 0b00000000
2587 uint32_t packetFamily = 0;
2588 uint32_t packetId = 1;
Keith Davis3201eea2019-10-24 17:30:41 +01002589 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002590
Matteo Martincighd0613b52019-10-09 16:47:04 +01002591 // Create the Connection Acknowledged Packet
Jim Flynnbbfe6032020-07-20 16:57:44 +01002592 arm::pipe::Packet connectionAcknowledgedPacket(header);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002593
2594 // Write the packet to the mock profiling connection
2595 mockProfilingConnection->WritePacket(std::move(connectionAcknowledgedPacket));
2596
Colm Donelan2ba48d22019-11-29 09:10:59 +00002597 // Wait for the counter directory packet to ensure the ConnectionAcknowledgedCommandHandler has run.
Finn Williams09ad6f92019-12-19 17:05:18 +00002598 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::CounterDirectory) == 1);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002599
2600 // The Connection Acknowledged Command Handler should have updated the profiling state accordingly
2601 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincighd0613b52019-10-09 16:47:04 +01002602
2603 // Reset the profiling service to stop any running thread
2604 options.m_EnableProfiling = false;
2605 profilingService.ResetExternalProfilingOptions(options, true);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002606}
2607
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002608BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodRequestCounterDirectoryPacket)
2609{
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002610 // Reset the profiling service to the uninitialized state
Kevin Mayd92a6e42021-02-04 10:27:41 +00002611 armnn::IRuntime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002612 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +00002613 armnn::profiling::ProfilingService profilingService;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002614 profilingService.ResetExternalProfilingOptions(options, true);
2615
Sadik Armagan3184c902020-03-18 10:57:30 +00002616 // Swap the profiling connection factory in the profiling service instance with our mock one
2617 SwapProfilingConnectionFactoryHelper helper(profilingService);
2618
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002619 // Bring the profiling service to the "Active" state
2620 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002621 profilingService.Update(); // Initialize the counter directory
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002622 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002623 profilingService.Update(); // Create the profiling connection
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002624 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002625 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002626
Colm Donelan2ba48d22019-11-29 09:10:59 +00002627 // Get the mock profiling connection
2628 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2629 BOOST_CHECK(mockProfilingConnection);
2630
Matteo Martincighe8485382019-10-10 14:08:21 +01002631 // Force the profiling service to the "Active" state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002632 helper.ForceTransitionToState(ProfilingState::Active);
2633 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2634
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002635 // Write a valid "Request Counter Directory" packet into the mock profiling connection, to simulate a valid
2636 // reply from an external profiling service
2637
2638 // Request Counter Directory packet header (word 0, word 1 is always zero):
2639 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2640 // 16:25 [10] packet_id: Packet identifier, value 0b0000000011
2641 // 8:15 [8] reserved: Reserved, value 0b00000000
2642 // 0:7 [8] reserved: Reserved, value 0b00000000
2643 uint32_t packetFamily = 0;
2644 uint32_t packetId = 3;
Keith Davis3201eea2019-10-24 17:30:41 +01002645 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002646
2647 // Create the Request Counter Directory packet
Jim Flynnbbfe6032020-07-20 16:57:44 +01002648 arm::pipe::Packet requestCounterDirectoryPacket(header);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002649
2650 // Write the packet to the mock profiling connection
2651 mockProfilingConnection->WritePacket(std::move(requestCounterDirectoryPacket));
2652
Sadik Armagan4c998992020-02-25 12:44:44 +00002653 // Expecting one CounterDirectory Packet of length 652
Jim Flynn6398a982020-05-27 17:05:21 +01002654 // and one TimelineMessageDirectory packet of length 451
Sadik Armagan4c998992020-02-25 12:44:44 +00002655 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::CounterDirectory, 652) == 1);
Jim Flynn6398a982020-05-27 17:05:21 +01002656 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::TimelineMessageDirectory, 451) == 1);
Matteo Martincighe8485382019-10-10 14:08:21 +01002657
2658 // The Request Counter Directory Command Handler should not have updated the profiling state
2659 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2660
2661 // Reset the profiling service to stop any running thread
2662 options.m_EnableProfiling = false;
2663 profilingService.ResetExternalProfilingOptions(options, true);
2664}
2665
Matteo Martincighe8485382019-10-10 14:08:21 +01002666BOOST_AUTO_TEST_CASE(CheckProfilingServiceBadPeriodicCounterSelectionPacketInvalidCounterUid)
2667{
Matteo Martincighe8485382019-10-10 14:08:21 +01002668 // Reset the profiling service to the uninitialized state
Kevin Mayd92a6e42021-02-04 10:27:41 +00002669 armnn::IRuntime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002670 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +00002671 armnn::profiling::ProfilingService profilingService;
Matteo Martincighe8485382019-10-10 14:08:21 +01002672 profilingService.ResetExternalProfilingOptions(options, true);
2673
Sadik Armagan3184c902020-03-18 10:57:30 +00002674 // Swap the profiling connection factory in the profiling service instance with our mock one
2675 SwapProfilingConnectionFactoryHelper helper(profilingService);
2676
Matteo Martincighe8485382019-10-10 14:08:21 +01002677 // Bring the profiling service to the "Active" state
2678 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002679 profilingService.Update(); // Initialize the counter directory
Matteo Martincighe8485382019-10-10 14:08:21 +01002680 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002681 profilingService.Update(); // Create the profiling connection
Matteo Martincighe8485382019-10-10 14:08:21 +01002682 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002683 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002684
Colm Donelan2ba48d22019-11-29 09:10:59 +00002685 // Get the mock profiling connection
2686 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2687 BOOST_CHECK(mockProfilingConnection);
2688
Matteo Martincighe8485382019-10-10 14:08:21 +01002689 // Force the profiling service to the "Active" state
2690 helper.ForceTransitionToState(ProfilingState::Active);
2691 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2692
Matteo Martincighe8485382019-10-10 14:08:21 +01002693 // Remove the packets received so far
2694 mockProfilingConnection->Clear();
2695
2696 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2697 // external profiling service
2698
2699 // Periodic Counter Selection packet header:
2700 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2701 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2702 // 8:15 [8] reserved: Reserved, value 0b00000000
2703 // 0:7 [8] reserved: Reserved, value 0b00000000
2704 uint32_t packetFamily = 0;
2705 uint32_t packetId = 4;
Keith Davis3201eea2019-10-24 17:30:41 +01002706 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincighe8485382019-10-10 14:08:21 +01002707
Keith Davis3201eea2019-10-24 17:30:41 +01002708 uint32_t capturePeriod = 123456; // Some capture period (microseconds)
Matteo Martincighe8485382019-10-10 14:08:21 +01002709
2710 // Get the first valid counter UID
2711 const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
Keith Davis3201eea2019-10-24 17:30:41 +01002712 const Counters& counters = counterDirectory.GetCounters();
Matteo Martincighe8485382019-10-10 14:08:21 +01002713 BOOST_CHECK(counters.size() > 1);
Keith Davis3201eea2019-10-24 17:30:41 +01002714 uint16_t counterUidA = counters.begin()->first; // First valid counter UID
2715 uint16_t counterUidB = 9999; // Second invalid counter UID
Matteo Martincighe8485382019-10-10 14:08:21 +01002716
2717 uint32_t length = 8;
2718
2719 auto data = std::make_unique<unsigned char[]>(length);
2720 WriteUint32(data.get(), 0, capturePeriod);
2721 WriteUint16(data.get(), 4, counterUidA);
2722 WriteUint16(data.get(), 6, counterUidB);
2723
2724 // Create the Periodic Counter Selection packet
Jim Flynnbbfe6032020-07-20 16:57:44 +01002725 // Length > 0, this will start the Period Counter Capture thread
2726 arm::pipe::Packet periodicCounterSelectionPacket(header, length, data);
2727
Matteo Martincighe8485382019-10-10 14:08:21 +01002728
2729 // Write the packet to the mock profiling connection
2730 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2731
Finn Williams09ad6f92019-12-19 17:05:18 +00002732 // Expecting one Periodic Counter Selection packet of length 14
2733 // and at least one Periodic Counter Capture packet of length 22
2734 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterSelection, 14) == 1);
2735 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterCapture, 22) >= 1);
Matteo Martincighe8485382019-10-10 14:08:21 +01002736
2737 // The Periodic Counter Selection Handler should not have updated the profiling state
2738 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2739
2740 // Reset the profiling service to stop any running thread
2741 options.m_EnableProfiling = false;
2742 profilingService.ResetExternalProfilingOptions(options, true);
2743}
2744
2745BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodPeriodicCounterSelectionPacketNoCounters)
2746{
Matteo Martincighe8485382019-10-10 14:08:21 +01002747 // Reset the profiling service to the uninitialized state
Kevin Mayd92a6e42021-02-04 10:27:41 +00002748 armnn::IRuntime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002749 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +00002750 armnn::profiling::ProfilingService profilingService;
Matteo Martincighe8485382019-10-10 14:08:21 +01002751 profilingService.ResetExternalProfilingOptions(options, true);
2752
Sadik Armagan3184c902020-03-18 10:57:30 +00002753 // Swap the profiling connection factory in the profiling service instance with our mock one
2754 SwapProfilingConnectionFactoryHelper helper(profilingService);
2755
Matteo Martincighe8485382019-10-10 14:08:21 +01002756 // Bring the profiling service to the "Active" state
2757 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002758 profilingService.Update(); // Initialize the counter directory
Matteo Martincighe8485382019-10-10 14:08:21 +01002759 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002760 profilingService.Update(); // Create the profiling connection
Matteo Martincighe8485382019-10-10 14:08:21 +01002761 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002762 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002763
Colm Donelan2ba48d22019-11-29 09:10:59 +00002764 // Get the mock profiling connection
2765 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2766 BOOST_CHECK(mockProfilingConnection);
2767
Matteo Martincighe8485382019-10-10 14:08:21 +01002768 // Wait for the Stream Metadata packet the be sent
2769 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Finn Williams09ad6f92019-12-19 17:05:18 +00002770 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData);
Matteo Martincighe8485382019-10-10 14:08:21 +01002771
2772 // Force the profiling service to the "Active" state
2773 helper.ForceTransitionToState(ProfilingState::Active);
2774 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2775
Matteo Martincighe8485382019-10-10 14:08:21 +01002776 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2777 // external profiling service
2778
2779 // Periodic Counter Selection packet header:
2780 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2781 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2782 // 8:15 [8] reserved: Reserved, value 0b00000000
2783 // 0:7 [8] reserved: Reserved, value 0b00000000
2784 uint32_t packetFamily = 0;
2785 uint32_t packetId = 4;
Keith Davis3201eea2019-10-24 17:30:41 +01002786 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincighe8485382019-10-10 14:08:21 +01002787
2788 // Create the Periodic Counter Selection packet
Jim Flynnbbfe6032020-07-20 16:57:44 +01002789 // Length == 0, this will disable the collection of counters
2790 arm::pipe::Packet periodicCounterSelectionPacket(header);
Matteo Martincighe8485382019-10-10 14:08:21 +01002791
2792 // Write the packet to the mock profiling connection
2793 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2794
Finn Williams09ad6f92019-12-19 17:05:18 +00002795 // Wait for the Periodic Counter Selection packet of length 12 to be sent
2796 // The size of the expected Periodic Counter Selection (echos the sent one)
2797 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterSelection, 12) == 1);
Matteo Martincighe8485382019-10-10 14:08:21 +01002798
2799 // The Periodic Counter Selection Handler should not have updated the profiling state
2800 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2801
Finn Williams09ad6f92019-12-19 17:05:18 +00002802 // No Periodic Counter packets are expected
2803 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterCapture, 0, 0) == 0);
Matteo Martincighe8485382019-10-10 14:08:21 +01002804
2805 // Reset the profiling service to stop any running thread
2806 options.m_EnableProfiling = false;
2807 profilingService.ResetExternalProfilingOptions(options, true);
2808}
2809
2810BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodPeriodicCounterSelectionPacketSingleCounter)
2811{
Matteo Martincighe8485382019-10-10 14:08:21 +01002812 // Reset the profiling service to the uninitialized state
Kevin Mayd92a6e42021-02-04 10:27:41 +00002813 armnn::IRuntime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002814 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +00002815 armnn::profiling::ProfilingService profilingService;
Matteo Martincighe8485382019-10-10 14:08:21 +01002816 profilingService.ResetExternalProfilingOptions(options, true);
2817
Sadik Armagan3184c902020-03-18 10:57:30 +00002818 // Swap the profiling connection factory in the profiling service instance with our mock one
2819 SwapProfilingConnectionFactoryHelper helper(profilingService);
2820
Matteo Martincighe8485382019-10-10 14:08:21 +01002821 // Bring the profiling service to the "Active" state
2822 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002823 profilingService.Update(); // Initialize the counter directory
Matteo Martincighe8485382019-10-10 14:08:21 +01002824 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002825 profilingService.Update(); // Create the profiling connection
Matteo Martincighe8485382019-10-10 14:08:21 +01002826 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002827 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002828
Colm Donelan2ba48d22019-11-29 09:10:59 +00002829 // Get the mock profiling connection
2830 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2831 BOOST_CHECK(mockProfilingConnection);
2832
Finn Williams09ad6f92019-12-19 17:05:18 +00002833 // Wait for the Stream Metadata packet to be sent
Matteo Martincighe8485382019-10-10 14:08:21 +01002834 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Finn Williams09ad6f92019-12-19 17:05:18 +00002835 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData);
Matteo Martincighe8485382019-10-10 14:08:21 +01002836
2837 // Force the profiling service to the "Active" state
2838 helper.ForceTransitionToState(ProfilingState::Active);
2839 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2840
Matteo Martincighe8485382019-10-10 14:08:21 +01002841 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2842 // external profiling service
2843
2844 // Periodic Counter Selection packet header:
2845 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2846 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2847 // 8:15 [8] reserved: Reserved, value 0b00000000
2848 // 0:7 [8] reserved: Reserved, value 0b00000000
2849 uint32_t packetFamily = 0;
2850 uint32_t packetId = 4;
Keith Davis3201eea2019-10-24 17:30:41 +01002851 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincighe8485382019-10-10 14:08:21 +01002852
Keith Davis3201eea2019-10-24 17:30:41 +01002853 uint32_t capturePeriod = 123456; // Some capture period (microseconds)
Matteo Martincighe8485382019-10-10 14:08:21 +01002854
2855 // Get the first valid counter UID
2856 const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
Keith Davis3201eea2019-10-24 17:30:41 +01002857 const Counters& counters = counterDirectory.GetCounters();
Matteo Martincighe8485382019-10-10 14:08:21 +01002858 BOOST_CHECK(!counters.empty());
Keith Davis3201eea2019-10-24 17:30:41 +01002859 uint16_t counterUid = counters.begin()->first; // Valid counter UID
Matteo Martincighe8485382019-10-10 14:08:21 +01002860
2861 uint32_t length = 6;
2862
2863 auto data = std::make_unique<unsigned char[]>(length);
2864 WriteUint32(data.get(), 0, capturePeriod);
2865 WriteUint16(data.get(), 4, counterUid);
2866
2867 // Create the Periodic Counter Selection packet
Jim Flynnbbfe6032020-07-20 16:57:44 +01002868 // Length > 0, this will start the Period Counter Capture thread
2869 arm::pipe::Packet periodicCounterSelectionPacket(header, length, data);
Matteo Martincighe8485382019-10-10 14:08:21 +01002870
2871 // Write the packet to the mock profiling connection
2872 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2873
Finn Williams09ad6f92019-12-19 17:05:18 +00002874 // Expecting one Periodic Counter Selection packet of length 14
2875 // and at least one Periodic Counter Capture packet of length 22
2876 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterSelection, 14) == 1);
2877 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterCapture, 22) >= 1);
Matteo Martincighe8485382019-10-10 14:08:21 +01002878
2879 // The Periodic Counter Selection Handler should not have updated the profiling state
2880 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2881
2882 // Reset the profiling service to stop any running thread
2883 options.m_EnableProfiling = false;
2884 profilingService.ResetExternalProfilingOptions(options, true);
2885}
2886
2887BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodPeriodicCounterSelectionPacketMultipleCounters)
2888{
Matteo Martincighe8485382019-10-10 14:08:21 +01002889 // Reset the profiling service to the uninitialized state
Kevin Mayd92a6e42021-02-04 10:27:41 +00002890 armnn::IRuntime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002891 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +00002892 armnn::profiling::ProfilingService profilingService;
Matteo Martincighe8485382019-10-10 14:08:21 +01002893 profilingService.ResetExternalProfilingOptions(options, true);
2894
Sadik Armagan3184c902020-03-18 10:57:30 +00002895 // Swap the profiling connection factory in the profiling service instance with our mock one
2896 SwapProfilingConnectionFactoryHelper helper(profilingService);
2897
Matteo Martincighe8485382019-10-10 14:08:21 +01002898 // Bring the profiling service to the "Active" state
2899 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002900 profilingService.Update(); // Initialize the counter directory
Matteo Martincighe8485382019-10-10 14:08:21 +01002901 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002902 profilingService.Update(); // Create the profiling connection
Matteo Martincighe8485382019-10-10 14:08:21 +01002903 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002904 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002905
Colm Donelan2ba48d22019-11-29 09:10:59 +00002906 // Get the mock profiling connection
2907 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2908 BOOST_CHECK(mockProfilingConnection);
2909
Matteo Martincighe8485382019-10-10 14:08:21 +01002910 // Wait for the Stream Metadata packet the be sent
2911 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Finn Williams09ad6f92019-12-19 17:05:18 +00002912 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData);
Matteo Martincighe8485382019-10-10 14:08:21 +01002913
2914 // Force the profiling service to the "Active" state
2915 helper.ForceTransitionToState(ProfilingState::Active);
2916 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2917
Matteo Martincighe8485382019-10-10 14:08:21 +01002918 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2919 // external profiling service
2920
2921 // Periodic Counter Selection packet header:
2922 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2923 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2924 // 8:15 [8] reserved: Reserved, value 0b00000000
2925 // 0:7 [8] reserved: Reserved, value 0b00000000
2926 uint32_t packetFamily = 0;
2927 uint32_t packetId = 4;
Keith Davis3201eea2019-10-24 17:30:41 +01002928 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincighe8485382019-10-10 14:08:21 +01002929
Keith Davis3201eea2019-10-24 17:30:41 +01002930 uint32_t capturePeriod = 123456; // Some capture period (microseconds)
Matteo Martincighe8485382019-10-10 14:08:21 +01002931
2932 // Get the first valid counter UID
2933 const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
Keith Davis3201eea2019-10-24 17:30:41 +01002934 const Counters& counters = counterDirectory.GetCounters();
Matteo Martincighe8485382019-10-10 14:08:21 +01002935 BOOST_CHECK(counters.size() > 1);
Keith Davis3201eea2019-10-24 17:30:41 +01002936 uint16_t counterUidA = counters.begin()->first; // First valid counter UID
2937 uint16_t counterUidB = (counters.begin()++)->first; // Second valid counter UID
Matteo Martincighe8485382019-10-10 14:08:21 +01002938
2939 uint32_t length = 8;
2940
2941 auto data = std::make_unique<unsigned char[]>(length);
2942 WriteUint32(data.get(), 0, capturePeriod);
2943 WriteUint16(data.get(), 4, counterUidA);
2944 WriteUint16(data.get(), 6, counterUidB);
2945
2946 // Create the Periodic Counter Selection packet
Jim Flynnbbfe6032020-07-20 16:57:44 +01002947 // Length > 0, this will start the Period Counter Capture thread
2948 arm::pipe::Packet periodicCounterSelectionPacket(header, length, data);
Matteo Martincighe8485382019-10-10 14:08:21 +01002949
2950 // Write the packet to the mock profiling connection
2951 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2952
Finn Williams09ad6f92019-12-19 17:05:18 +00002953 // Expecting one PeriodicCounterSelection Packet with a length of 16
2954 // And at least one PeriodicCounterCapture Packet with a length of 28
2955 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterSelection, 16) == 1);
2956 BOOST_CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterCapture, 28) >= 1);
Matteo Martincighe8485382019-10-10 14:08:21 +01002957
2958 // The Periodic Counter Selection Handler should not have updated the profiling state
2959 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002960
2961 // Reset the profiling service to stop any running thread
2962 options.m_EnableProfiling = false;
2963 profilingService.ResetExternalProfilingOptions(options, true);
2964}
2965
Jim Flynn53e46992019-10-14 12:31:10 +01002966BOOST_AUTO_TEST_CASE(CheckProfilingServiceDisconnect)
2967{
Jim Flynn53e46992019-10-14 12:31:10 +01002968 // Reset the profiling service to the uninitialized state
Kevin Mayd92a6e42021-02-04 10:27:41 +00002969 armnn::IRuntime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002970 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +00002971 armnn::profiling::ProfilingService profilingService;
Jim Flynn53e46992019-10-14 12:31:10 +01002972 profilingService.ResetExternalProfilingOptions(options, true);
2973
Sadik Armagan3184c902020-03-18 10:57:30 +00002974 // Swap the profiling connection factory in the profiling service instance with our mock one
2975 SwapProfilingConnectionFactoryHelper helper(profilingService);
2976
Jim Flynn53e46992019-10-14 12:31:10 +01002977 // Try to disconnect the profiling service while in the "Uninitialised" state
2978 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
2979 profilingService.Disconnect();
Keith Davis3201eea2019-10-24 17:30:41 +01002980 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised); // The state should not change
Jim Flynn53e46992019-10-14 12:31:10 +01002981
2982 // Try to disconnect the profiling service while in the "NotConnected" state
Keith Davis3201eea2019-10-24 17:30:41 +01002983 profilingService.Update(); // Initialize the counter directory
Jim Flynn53e46992019-10-14 12:31:10 +01002984 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
2985 profilingService.Disconnect();
Keith Davis3201eea2019-10-24 17:30:41 +01002986 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected); // The state should not change
Jim Flynn53e46992019-10-14 12:31:10 +01002987
2988 // Try to disconnect the profiling service while in the "WaitingForAck" state
Keith Davis3201eea2019-10-24 17:30:41 +01002989 profilingService.Update(); // Create the profiling connection
Jim Flynn53e46992019-10-14 12:31:10 +01002990 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
2991 profilingService.Disconnect();
Keith Davis3201eea2019-10-24 17:30:41 +01002992 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck); // The state should not change
Jim Flynn53e46992019-10-14 12:31:10 +01002993
2994 // Try to disconnect the profiling service while in the "Active" state
Keith Davis3201eea2019-10-24 17:30:41 +01002995 profilingService.Update(); // Start the command handler and the send thread
Jim Flynn53e46992019-10-14 12:31:10 +01002996
Colm Donelan2ba48d22019-11-29 09:10:59 +00002997 // Get the mock profiling connection
2998 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2999 BOOST_CHECK(mockProfilingConnection);
3000
Jim Flynn53e46992019-10-14 12:31:10 +01003001 // Wait for the Stream Metadata packet the be sent
3002 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Finn Williams09ad6f92019-12-19 17:05:18 +00003003 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData);
Jim Flynn53e46992019-10-14 12:31:10 +01003004
3005 // Force the profiling service to the "Active" state
3006 helper.ForceTransitionToState(ProfilingState::Active);
3007 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
3008
Jim Flynn53e46992019-10-14 12:31:10 +01003009 // Check that the profiling connection is open
3010 BOOST_CHECK(mockProfilingConnection->IsOpen());
3011
3012 profilingService.Disconnect();
Colm Donelan2ba48d22019-11-29 09:10:59 +00003013 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected); // The state should have changed
Jim Flynn53e46992019-10-14 12:31:10 +01003014
3015 // Check that the profiling connection has been reset
3016 mockProfilingConnection = helper.GetMockProfilingConnection();
3017 BOOST_CHECK(mockProfilingConnection == nullptr);
3018
3019 // Reset the profiling service to stop any running thread
3020 options.m_EnableProfiling = false;
3021 profilingService.ResetExternalProfilingOptions(options, true);
3022}
3023
Matteo Martincigh994b5342019-10-11 17:19:56 +01003024BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodPerJobCounterSelectionPacket)
3025{
Matteo Martincigh994b5342019-10-11 17:19:56 +01003026 // Reset the profiling service to the uninitialized state
Kevin Mayd92a6e42021-02-04 10:27:41 +00003027 armnn::IRuntime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01003028 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +00003029 armnn::profiling::ProfilingService profilingService;
Matteo Martincigh994b5342019-10-11 17:19:56 +01003030 profilingService.ResetExternalProfilingOptions(options, true);
3031
Sadik Armagan3184c902020-03-18 10:57:30 +00003032 // Swap the profiling connection factory in the profiling service instance with our mock one
3033 SwapProfilingConnectionFactoryHelper helper(profilingService);
3034
Matteo Martincigh994b5342019-10-11 17:19:56 +01003035 // Bring the profiling service to the "Active" state
3036 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01003037 profilingService.Update(); // Initialize the counter directory
Matteo Martincigh994b5342019-10-11 17:19:56 +01003038 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01003039 profilingService.Update(); // Create the profiling connection
Matteo Martincigh994b5342019-10-11 17:19:56 +01003040 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01003041 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincigh994b5342019-10-11 17:19:56 +01003042
Colm Donelan2ba48d22019-11-29 09:10:59 +00003043 // Get the mock profiling connection
3044 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
3045 BOOST_CHECK(mockProfilingConnection);
3046
Matteo Martincigh994b5342019-10-11 17:19:56 +01003047 // Wait for the Stream Metadata packet the be sent
3048 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Finn Williams09ad6f92019-12-19 17:05:18 +00003049 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData);
Matteo Martincigh994b5342019-10-11 17:19:56 +01003050
3051 // Force the profiling service to the "Active" state
3052 helper.ForceTransitionToState(ProfilingState::Active);
3053 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
3054
Matteo Martincigh994b5342019-10-11 17:19:56 +01003055 // Write a "Per-Job Counter Selection" packet into the mock profiling connection, to simulate an input from an
3056 // external profiling service
3057
3058 // Per-Job Counter Selection packet header:
3059 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
3060 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
3061 // 8:15 [8] reserved: Reserved, value 0b00000000
3062 // 0:7 [8] reserved: Reserved, value 0b00000000
3063 uint32_t packetFamily = 0;
3064 uint32_t packetId = 5;
Keith Davis3201eea2019-10-24 17:30:41 +01003065 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincigh994b5342019-10-11 17:19:56 +01003066
3067 // Create the Per-Job Counter Selection packet
Jim Flynnbbfe6032020-07-20 16:57:44 +01003068 // Length == 0, this will disable the collection of counters
3069 arm::pipe::Packet periodicCounterSelectionPacket(header);
Matteo Martincigh994b5342019-10-11 17:19:56 +01003070
3071 // Write the packet to the mock profiling connection
3072 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
3073
3074 // Wait for a bit (must at least be the delay value of the mock profiling connection) to make sure that
3075 // the Per-Job Counter Selection packet gets processed by the profiling service
Colm Donelan2ba48d22019-11-29 09:10:59 +00003076 std::this_thread::sleep_for(std::chrono::milliseconds(5));
Matteo Martincigh994b5342019-10-11 17:19:56 +01003077
Matteo Martincigh994b5342019-10-11 17:19:56 +01003078 // The Per-Job Counter Selection Command Handler should not have updated the profiling state
3079 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
3080
Finn Williams09ad6f92019-12-19 17:05:18 +00003081 // The Per-Job Counter Selection packets are dropped silently, so there should be no reply coming
3082 // from the profiling service
3083 const auto StreamMetaDataSize = static_cast<unsigned long>(
3084 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData, 0, 0));
3085 BOOST_CHECK(StreamMetaDataSize == mockProfilingConnection->GetWrittenDataSize());
3086
Matteo Martincigh994b5342019-10-11 17:19:56 +01003087 // Reset the profiling service to stop any running thread
3088 options.m_EnableProfiling = false;
3089 profilingService.ResetExternalProfilingOptions(options, true);
3090}
3091
Jim Flynn672d06e2019-10-15 10:18:11 +01003092BOOST_AUTO_TEST_CASE(CheckConfigureProfilingServiceOn)
3093{
Kevin Mayd92a6e42021-02-04 10:27:41 +00003094 armnn::IRuntime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01003095 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +00003096 armnn::profiling::ProfilingService profilingService;
Jim Flynn672d06e2019-10-15 10:18:11 +01003097 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3098 profilingService.ConfigureProfilingService(options);
3099 // should get as far as NOT_CONNECTED
3100 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
3101 // Reset the profiling service to stop any running thread
3102 options.m_EnableProfiling = false;
3103 profilingService.ResetExternalProfilingOptions(options, true);
3104}
3105
3106BOOST_AUTO_TEST_CASE(CheckConfigureProfilingServiceOff)
3107{
Kevin Mayd92a6e42021-02-04 10:27:41 +00003108 armnn::IRuntime::CreationOptions::ExternalProfilingOptions options;
Sadik Armagan3184c902020-03-18 10:57:30 +00003109 armnn::profiling::ProfilingService profilingService;
Jim Flynn672d06e2019-10-15 10:18:11 +01003110 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3111 profilingService.ConfigureProfilingService(options);
3112 // should not move from Uninitialised
3113 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3114 // Reset the profiling service to stop any running thread
3115 options.m_EnableProfiling = false;
3116 profilingService.ResetExternalProfilingOptions(options, true);
3117}
3118
Colm Donelan2ba48d22019-11-29 09:10:59 +00003119BOOST_AUTO_TEST_CASE(CheckProfilingServiceEnabled)
3120{
3121 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
3122 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
Kevin Mayd92a6e42021-02-04 10:27:41 +00003123 armnn::IRuntime::CreationOptions::ExternalProfilingOptions options;
Colm Donelan2ba48d22019-11-29 09:10:59 +00003124 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +00003125 armnn::profiling::ProfilingService profilingService;
Colm Donelan2ba48d22019-11-29 09:10:59 +00003126 profilingService.ResetExternalProfilingOptions(options, true);
3127 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3128 profilingService.Update();
3129 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
3130
3131 // Redirect the output to a local stream so that we can parse the warning message
3132 std::stringstream ss;
3133 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3134 profilingService.Update();
Finn Williams09ad6f92019-12-19 17:05:18 +00003135
3136 // Reset the profiling service to stop any running thread
3137 options.m_EnableProfiling = false;
3138 profilingService.ResetExternalProfilingOptions(options, true);
3139
Colm Donelan2ba48d22019-11-29 09:10:59 +00003140 streamRedirector.CancelRedirect();
3141
3142 // Check that the expected error has occurred and logged to the standard output
David Monahana8837bf2020-04-16 10:01:56 +01003143 if (ss.str().find("Cannot connect to stream socket: Connection refused") == std::string::npos)
Colm Donelan2ba48d22019-11-29 09:10:59 +00003144 {
3145 std::cout << ss.str();
3146 BOOST_FAIL("Expected string not found.");
3147 }
Colm Donelan2ba48d22019-11-29 09:10:59 +00003148}
3149
3150BOOST_AUTO_TEST_CASE(CheckProfilingServiceEnabledRuntime)
3151{
3152 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
3153 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
Kevin Mayd92a6e42021-02-04 10:27:41 +00003154 armnn::IRuntime::CreationOptions::ExternalProfilingOptions options;
Sadik Armagan3184c902020-03-18 10:57:30 +00003155 armnn::profiling::ProfilingService profilingService;
Colm Donelan2ba48d22019-11-29 09:10:59 +00003156 profilingService.ResetExternalProfilingOptions(options, true);
3157 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3158 profilingService.Update();
3159 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3160 options.m_EnableProfiling = true;
3161 profilingService.ResetExternalProfilingOptions(options);
3162 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3163 profilingService.Update();
3164 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
3165
3166 // Redirect the output to a local stream so that we can parse the warning message
3167 std::stringstream ss;
3168 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3169 profilingService.Update();
3170
Finn Williams09ad6f92019-12-19 17:05:18 +00003171 // Reset the profiling service to stop any running thread
3172 options.m_EnableProfiling = false;
3173 profilingService.ResetExternalProfilingOptions(options, true);
3174
Colm Donelan2ba48d22019-11-29 09:10:59 +00003175 streamRedirector.CancelRedirect();
3176
3177 // Check that the expected error has occurred and logged to the standard output
David Monahana8837bf2020-04-16 10:01:56 +01003178 if (ss.str().find("Cannot connect to stream socket: Connection refused") == std::string::npos)
Colm Donelan2ba48d22019-11-29 09:10:59 +00003179 {
3180 std::cout << ss.str();
3181 BOOST_FAIL("Expected string not found.");
3182 }
Colm Donelan2ba48d22019-11-29 09:10:59 +00003183}
3184
3185BOOST_AUTO_TEST_CASE(CheckProfilingServiceBadConnectionAcknowledgedPacket)
3186{
3187 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
3188 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
Sadik Armagan3184c902020-03-18 10:57:30 +00003189
Colm Donelan2ba48d22019-11-29 09:10:59 +00003190
3191 // Redirect the standard output to a local stream so that we can parse the warning message
3192 std::stringstream ss;
3193 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3194
Colm Donelan2ba48d22019-11-29 09:10:59 +00003195 // Reset the profiling service to the uninitialized state
Kevin Mayd92a6e42021-02-04 10:27:41 +00003196 armnn::IRuntime::CreationOptions::ExternalProfilingOptions options;
Colm Donelan2ba48d22019-11-29 09:10:59 +00003197 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +00003198 armnn::profiling::ProfilingService profilingService;
Colm Donelan2ba48d22019-11-29 09:10:59 +00003199 profilingService.ResetExternalProfilingOptions(options, true);
3200
Sadik Armagan3184c902020-03-18 10:57:30 +00003201 // Swap the profiling connection factory in the profiling service instance with our mock one
3202 SwapProfilingConnectionFactoryHelper helper(profilingService);
3203
Colm Donelan2ba48d22019-11-29 09:10:59 +00003204 // Bring the profiling service to the "WaitingForAck" state
3205 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3206 profilingService.Update(); // Initialize the counter directory
3207 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
3208 profilingService.Update(); // Create the profiling connection
3209
3210 // Get the mock profiling connection
3211 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
3212 BOOST_CHECK(mockProfilingConnection);
3213
Colm Donelan2ba48d22019-11-29 09:10:59 +00003214 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003215
3216 // Connection Acknowledged Packet header (word 0, word 1 is always zero):
3217 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
3218 // 16:25 [10] packet_id: Packet identifier, value 0b0000000001
3219 // 8:15 [8] reserved: Reserved, value 0b00000000
3220 // 0:7 [8] reserved: Reserved, value 0b00000000
3221 uint32_t packetFamily = 0;
3222 uint32_t packetId = 37; // Wrong packet id!!!
3223 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
3224
3225 // Create the Connection Acknowledged Packet
Jim Flynnbbfe6032020-07-20 16:57:44 +01003226 arm::pipe::Packet connectionAcknowledgedPacket(header);
Finn Williams09ad6f92019-12-19 17:05:18 +00003227 // Write an invalid "Connection Acknowledged" packet into the mock profiling connection, to simulate an invalid
3228 // reply from an external profiling service
Colm Donelan2ba48d22019-11-29 09:10:59 +00003229 mockProfilingConnection->WritePacket(std::move(connectionAcknowledgedPacket));
3230
Finn Williams09ad6f92019-12-19 17:05:18 +00003231 // Start the command thread
3232 profilingService.Update();
3233
3234 // Wait for the command thread to join
3235 options.m_EnableProfiling = false;
3236 profilingService.ResetExternalProfilingOptions(options, true);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003237
3238 streamRedirector.CancelRedirect();
3239
3240 // Check that the expected error has occurred and logged to the standard output
David Monahana8837bf2020-04-16 10:01:56 +01003241 if (ss.str().find("Functor with requested PacketId=37 and Version=4194304 does not exist") == std::string::npos)
Colm Donelan2ba48d22019-11-29 09:10:59 +00003242 {
3243 std::cout << ss.str();
3244 BOOST_FAIL("Expected string not found.");
3245 }
Colm Donelan2ba48d22019-11-29 09:10:59 +00003246}
3247
3248BOOST_AUTO_TEST_CASE(CheckProfilingServiceBadRequestCounterDirectoryPacket)
3249{
3250 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
3251 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003252
3253 // Redirect the standard output to a local stream so that we can parse the warning message
3254 std::stringstream ss;
3255 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3256
3257 // Reset the profiling service to the uninitialized state
Kevin Mayd92a6e42021-02-04 10:27:41 +00003258 armnn::IRuntime::CreationOptions::ExternalProfilingOptions options;
Colm Donelan2ba48d22019-11-29 09:10:59 +00003259 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +00003260 armnn::profiling::ProfilingService profilingService;
Colm Donelan2ba48d22019-11-29 09:10:59 +00003261 profilingService.ResetExternalProfilingOptions(options, true);
3262
Sadik Armagan3184c902020-03-18 10:57:30 +00003263 // Swap the profiling connection factory in the profiling service instance with our mock one
3264 SwapProfilingConnectionFactoryHelper helper(profilingService);
3265
Colm Donelan2ba48d22019-11-29 09:10:59 +00003266 // Bring the profiling service to the "Active" state
3267 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3268 helper.ForceTransitionToState(ProfilingState::NotConnected);
3269 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
3270 profilingService.Update(); // Create the profiling connection
3271 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003272
3273 // Get the mock profiling connection
3274 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
3275 BOOST_CHECK(mockProfilingConnection);
3276
Colm Donelan2ba48d22019-11-29 09:10:59 +00003277 // Write a valid "Request Counter Directory" packet into the mock profiling connection, to simulate a valid
3278 // reply from an external profiling service
3279
3280 // Request Counter Directory packet header (word 0, word 1 is always zero):
3281 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
3282 // 16:25 [10] packet_id: Packet identifier, value 0b0000000011
3283 // 8:15 [8] reserved: Reserved, value 0b00000000
3284 // 0:7 [8] reserved: Reserved, value 0b00000000
3285 uint32_t packetFamily = 0;
3286 uint32_t packetId = 123; // Wrong packet id!!!
3287 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
3288
3289 // Create the Request Counter Directory packet
Jim Flynnbbfe6032020-07-20 16:57:44 +01003290 arm::pipe::Packet requestCounterDirectoryPacket(header);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003291
3292 // Write the packet to the mock profiling connection
3293 mockProfilingConnection->WritePacket(std::move(requestCounterDirectoryPacket));
3294
Finn Williams09ad6f92019-12-19 17:05:18 +00003295 // Start the command handler and the send thread
3296 profilingService.Update();
3297
3298 // Reset the profiling service to stop and join any running thread
3299 options.m_EnableProfiling = false;
3300 profilingService.ResetExternalProfilingOptions(options, true);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003301
3302 streamRedirector.CancelRedirect();
3303
3304 // Check that the expected error has occurred and logged to the standard output
David Monahana8837bf2020-04-16 10:01:56 +01003305 if (ss.str().find("Functor with requested PacketId=123 and Version=4194304 does not exist") == std::string::npos)
Colm Donelan2ba48d22019-11-29 09:10:59 +00003306 {
3307 std::cout << ss.str();
3308 BOOST_FAIL("Expected string not found.");
3309 }
Colm Donelan2ba48d22019-11-29 09:10:59 +00003310}
3311
3312BOOST_AUTO_TEST_CASE(CheckProfilingServiceBadPeriodicCounterSelectionPacket)
3313{
3314 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
3315 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003316
3317 // Redirect the standard output to a local stream so that we can parse the warning message
3318 std::stringstream ss;
3319 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3320
3321 // Reset the profiling service to the uninitialized state
Kevin Mayd92a6e42021-02-04 10:27:41 +00003322 armnn::IRuntime::CreationOptions::ExternalProfilingOptions options;
Colm Donelan2ba48d22019-11-29 09:10:59 +00003323 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +00003324 armnn::profiling::ProfilingService profilingService;
Colm Donelan2ba48d22019-11-29 09:10:59 +00003325 profilingService.ResetExternalProfilingOptions(options, true);
3326
Sadik Armagan3184c902020-03-18 10:57:30 +00003327 // Swap the profiling connection factory in the profiling service instance with our mock one
3328 SwapProfilingConnectionFactoryHelper helper(profilingService);
3329
Colm Donelan2ba48d22019-11-29 09:10:59 +00003330 // Bring the profiling service to the "Active" state
3331 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3332 profilingService.Update(); // Initialize the counter directory
3333 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
3334 profilingService.Update(); // Create the profiling connection
3335 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
3336 profilingService.Update(); // Start the command handler and the send thread
3337
3338 // Get the mock profiling connection
3339 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
3340 BOOST_CHECK(mockProfilingConnection);
3341
Colm Donelan2ba48d22019-11-29 09:10:59 +00003342 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
3343 // external profiling service
3344
3345 // Periodic Counter Selection packet header:
3346 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
3347 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
3348 // 8:15 [8] reserved: Reserved, value 0b00000000
3349 // 0:7 [8] reserved: Reserved, value 0b00000000
3350 uint32_t packetFamily = 0;
3351 uint32_t packetId = 999; // Wrong packet id!!!
3352 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
3353
3354 // Create the Periodic Counter Selection packet
Jim Flynnbbfe6032020-07-20 16:57:44 +01003355 // Length == 0, this will disable the collection of counters
3356 arm::pipe::Packet periodicCounterSelectionPacket(header);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003357
3358 // Write the packet to the mock profiling connection
3359 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
Finn Williams09ad6f92019-12-19 17:05:18 +00003360 profilingService.Update();
Colm Donelan2ba48d22019-11-29 09:10:59 +00003361
Finn Williams09ad6f92019-12-19 17:05:18 +00003362 // Reset the profiling service to stop any running thread
3363 options.m_EnableProfiling = false;
3364 profilingService.ResetExternalProfilingOptions(options, true);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003365
3366 // Check that the expected error has occurred and logged to the standard output
3367 streamRedirector.CancelRedirect();
3368
3369 // Check that the expected error has occurred and logged to the standard output
David Monahana8837bf2020-04-16 10:01:56 +01003370 if (ss.str().find("Functor with requested PacketId=999 and Version=4194304 does not exist") == std::string::npos)
Colm Donelan2ba48d22019-11-29 09:10:59 +00003371 {
3372 std::cout << ss.str();
3373 BOOST_FAIL("Expected string not found.");
3374 }
Colm Donelan2ba48d22019-11-29 09:10:59 +00003375}
Jim Flynn97897022020-02-02 12:52:59 +00003376
David Monahande803072020-01-30 12:44:23 +00003377BOOST_AUTO_TEST_CASE(CheckCounterIdMap)
3378{
3379 CounterIdMap counterIdMap;
3380 BOOST_CHECK_THROW(counterIdMap.GetBackendId(0), armnn::Exception);
3381 BOOST_CHECK_THROW(counterIdMap.GetGlobalId(0, armnn::profiling::BACKEND_ID), armnn::Exception);
3382
3383 uint16_t globalCounterIds = 0;
3384
3385 armnn::BackendId cpuRefId(armnn::Compute::CpuRef);
3386 armnn::BackendId cpuAccId(armnn::Compute::CpuAcc);
3387
3388 std::vector<uint16_t> cpuRefCounters = {0, 1, 2, 3};
3389 std::vector<uint16_t> cpuAccCounters = {0, 1};
3390
3391 for (uint16_t backendCounterId : cpuRefCounters)
3392 {
3393 counterIdMap.RegisterMapping(globalCounterIds, backendCounterId, cpuRefId);
3394 ++globalCounterIds;
3395 }
3396 for (uint16_t backendCounterId : cpuAccCounters)
3397 {
3398 counterIdMap.RegisterMapping(globalCounterIds, backendCounterId, cpuAccId);
3399 ++globalCounterIds;
3400 }
3401
3402 BOOST_CHECK(counterIdMap.GetBackendId(0) == (std::pair<uint16_t, armnn::BackendId>(0, cpuRefId)));
3403 BOOST_CHECK(counterIdMap.GetBackendId(1) == (std::pair<uint16_t, armnn::BackendId>(1, cpuRefId)));
3404 BOOST_CHECK(counterIdMap.GetBackendId(2) == (std::pair<uint16_t, armnn::BackendId>(2, cpuRefId)));
3405 BOOST_CHECK(counterIdMap.GetBackendId(3) == (std::pair<uint16_t, armnn::BackendId>(3, cpuRefId)));
3406 BOOST_CHECK(counterIdMap.GetBackendId(4) == (std::pair<uint16_t, armnn::BackendId>(0, cpuAccId)));
3407 BOOST_CHECK(counterIdMap.GetBackendId(5) == (std::pair<uint16_t, armnn::BackendId>(1, cpuAccId)));
3408
3409 BOOST_CHECK(counterIdMap.GetGlobalId(0, cpuRefId) == 0);
3410 BOOST_CHECK(counterIdMap.GetGlobalId(1, cpuRefId) == 1);
3411 BOOST_CHECK(counterIdMap.GetGlobalId(2, cpuRefId) == 2);
3412 BOOST_CHECK(counterIdMap.GetGlobalId(3, cpuRefId) == 3);
3413 BOOST_CHECK(counterIdMap.GetGlobalId(0, cpuAccId) == 4);
3414 BOOST_CHECK(counterIdMap.GetGlobalId(1, cpuAccId) == 5);
3415}
Colm Donelan2ba48d22019-11-29 09:10:59 +00003416
Jim Flynn97897022020-02-02 12:52:59 +00003417BOOST_AUTO_TEST_CASE(CheckRegisterBackendCounters)
3418{
3419 uint16_t globalCounterIds = armnn::profiling::INFERENCES_RUN;
3420 armnn::BackendId cpuRefId(armnn::Compute::CpuRef);
3421
Jim Flynn97897022020-02-02 12:52:59 +00003422 // Reset the profiling service to the uninitialized state
Kevin Mayd92a6e42021-02-04 10:27:41 +00003423 armnn::IRuntime::CreationOptions::ExternalProfilingOptions options;
Jim Flynn97897022020-02-02 12:52:59 +00003424 options.m_EnableProfiling = true;
Sadik Armagan3184c902020-03-18 10:57:30 +00003425 ProfilingService profilingService;
Jim Flynn97897022020-02-02 12:52:59 +00003426 profilingService.ResetExternalProfilingOptions(options, true);
3427
Sadik Armagan3184c902020-03-18 10:57:30 +00003428 RegisterBackendCounters registerBackendCounters(globalCounterIds, cpuRefId, profilingService);
3429
3430
3431
Jim Flynn97897022020-02-02 12:52:59 +00003432 BOOST_CHECK(profilingService.GetCounterDirectory().GetCategories().empty());
3433 registerBackendCounters.RegisterCategory("categoryOne");
3434 auto categoryOnePtr = profilingService.GetCounterDirectory().GetCategory("categoryOne");
3435 BOOST_CHECK(categoryOnePtr);
3436
3437 BOOST_CHECK(profilingService.GetCounterDirectory().GetDevices().empty());
3438 globalCounterIds = registerBackendCounters.RegisterDevice("deviceOne");
3439 auto deviceOnePtr = profilingService.GetCounterDirectory().GetDevice(globalCounterIds);
3440 BOOST_CHECK(deviceOnePtr);
3441 BOOST_CHECK(deviceOnePtr->m_Name == "deviceOne");
3442
3443 BOOST_CHECK(profilingService.GetCounterDirectory().GetCounterSets().empty());
3444 globalCounterIds = registerBackendCounters.RegisterCounterSet("counterSetOne");
3445 auto counterSetOnePtr = profilingService.GetCounterDirectory().GetCounterSet(globalCounterIds);
3446 BOOST_CHECK(counterSetOnePtr);
3447 BOOST_CHECK(counterSetOnePtr->m_Name == "counterSetOne");
3448
3449 uint16_t newGlobalCounterId = registerBackendCounters.RegisterCounter(0,
3450 "categoryOne",
3451 0,
3452 0,
3453 1.f,
3454 "CounterOne",
3455 "first test counter");
3456 BOOST_CHECK(newGlobalCounterId = armnn::profiling::INFERENCES_RUN + 1);
3457 uint16_t mappedGlobalId = profilingService.GetCounterMappings().GetGlobalId(0, cpuRefId);
3458 BOOST_CHECK(mappedGlobalId == newGlobalCounterId);
3459 auto backendMapping = profilingService.GetCounterMappings().GetBackendId(newGlobalCounterId);
3460 BOOST_CHECK(backendMapping.first == 0);
3461 BOOST_CHECK(backendMapping.second == cpuRefId);
3462
3463 // Reset the profiling service to stop any running thread
3464 options.m_EnableProfiling = false;
3465 profilingService.ResetExternalProfilingOptions(options, true);
3466}
3467
James Conroy2dcd3fe2020-02-06 18:34:52 +00003468BOOST_AUTO_TEST_CASE(CheckCounterStatusQuery)
3469{
3470 armnn::IRuntime::CreationOptions options;
3471 options.m_ProfilingOptions.m_EnableProfiling = true;
3472
3473 // Reset the profiling service to the uninitialized state
Sadik Armagan3184c902020-03-18 10:57:30 +00003474 ProfilingService profilingService;
James Conroy2dcd3fe2020-02-06 18:34:52 +00003475 profilingService.ResetExternalProfilingOptions(options.m_ProfilingOptions, true);
3476
3477 const armnn::BackendId cpuRefId(armnn::Compute::CpuRef);
3478 const armnn::BackendId cpuAccId(armnn::Compute::CpuAcc);
3479
3480 // Create BackendProfiling for each backend
3481 BackendProfiling backendProfilingCpuRef(options, profilingService, cpuRefId);
3482 BackendProfiling backendProfilingCpuAcc(options, profilingService, cpuAccId);
3483
3484 uint16_t initialNumGlobalCounterIds = armnn::profiling::INFERENCES_RUN;
3485
3486 // Create RegisterBackendCounters for CpuRef
Sadik Armagan3184c902020-03-18 10:57:30 +00003487 RegisterBackendCounters registerBackendCountersCpuRef(initialNumGlobalCounterIds, cpuRefId, profilingService);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003488
3489 // Create 'testCategory' in CounterDirectory (backend agnostic)
3490 BOOST_CHECK(profilingService.GetCounterDirectory().GetCategories().empty());
3491 registerBackendCountersCpuRef.RegisterCategory("testCategory");
3492 auto categoryOnePtr = profilingService.GetCounterDirectory().GetCategory("testCategory");
3493 BOOST_CHECK(categoryOnePtr);
3494
3495 // Counters:
3496 // Global | Local | Backend
3497 // 5 | 0 | CpuRef
3498 // 6 | 1 | CpuRef
3499 // 7 | 1 | CpuAcc
3500
3501 std::vector<uint16_t> cpuRefCounters = {0, 1};
3502 std::vector<uint16_t> cpuAccCounters = {0};
3503
3504 // Register the backend counters for CpuRef and validate GetGlobalId and GetBackendId
3505 uint16_t currentNumGlobalCounterIds = registerBackendCountersCpuRef.RegisterCounter(
3506 0, "testCategory", 0, 0, 1.f, "CpuRefCounter0", "Zeroth CpuRef Counter");
3507 BOOST_CHECK(currentNumGlobalCounterIds == initialNumGlobalCounterIds + 1);
3508 uint16_t mappedGlobalId = profilingService.GetCounterMappings().GetGlobalId(0, cpuRefId);
3509 BOOST_CHECK(mappedGlobalId == currentNumGlobalCounterIds);
3510 auto backendMapping = profilingService.GetCounterMappings().GetBackendId(currentNumGlobalCounterIds);
3511 BOOST_CHECK(backendMapping.first == 0);
3512 BOOST_CHECK(backendMapping.second == cpuRefId);
3513
3514 currentNumGlobalCounterIds = registerBackendCountersCpuRef.RegisterCounter(
3515 1, "testCategory", 0, 0, 1.f, "CpuRefCounter1", "First CpuRef Counter");
3516 BOOST_CHECK(currentNumGlobalCounterIds == initialNumGlobalCounterIds + 2);
3517 mappedGlobalId = profilingService.GetCounterMappings().GetGlobalId(1, cpuRefId);
3518 BOOST_CHECK(mappedGlobalId == currentNumGlobalCounterIds);
3519 backendMapping = profilingService.GetCounterMappings().GetBackendId(currentNumGlobalCounterIds);
3520 BOOST_CHECK(backendMapping.first == 1);
3521 BOOST_CHECK(backendMapping.second == cpuRefId);
3522
3523 // Create RegisterBackendCounters for CpuAcc
Sadik Armagan3184c902020-03-18 10:57:30 +00003524 RegisterBackendCounters registerBackendCountersCpuAcc(currentNumGlobalCounterIds, cpuAccId, profilingService);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003525
3526 // Register the backend counter for CpuAcc and validate GetGlobalId and GetBackendId
3527 currentNumGlobalCounterIds = registerBackendCountersCpuAcc.RegisterCounter(
3528 0, "testCategory", 0, 0, 1.f, "CpuAccCounter0", "Zeroth CpuAcc Counter");
3529 BOOST_CHECK(currentNumGlobalCounterIds == initialNumGlobalCounterIds + 3);
3530 mappedGlobalId = profilingService.GetCounterMappings().GetGlobalId(0, cpuAccId);
3531 BOOST_CHECK(mappedGlobalId == currentNumGlobalCounterIds);
3532 backendMapping = profilingService.GetCounterMappings().GetBackendId(currentNumGlobalCounterIds);
3533 BOOST_CHECK(backendMapping.first == 0);
3534 BOOST_CHECK(backendMapping.second == cpuAccId);
3535
3536 // Create vectors for active counters
3537 const std::vector<uint16_t> activeGlobalCounterIds = {5}; // CpuRef(0) activated
3538 const std::vector<uint16_t> newActiveGlobalCounterIds = {6, 7}; // CpuRef(0) and CpuAcc(1) activated
3539
3540 const uint32_t capturePeriod = 200;
3541 const uint32_t newCapturePeriod = 100;
3542
3543 // Set capture period and active counters in CaptureData
Finn Williams032bc742020-02-12 11:02:34 +00003544 profilingService.SetCaptureData(capturePeriod, activeGlobalCounterIds, {});
James Conroy2dcd3fe2020-02-06 18:34:52 +00003545
3546 // Get vector of active counters for CpuRef and CpuAcc backends
3547 std::vector<CounterStatus> cpuRefCounterStatus = backendProfilingCpuRef.GetActiveCounters();
3548 std::vector<CounterStatus> cpuAccCounterStatus = backendProfilingCpuAcc.GetActiveCounters();
3549 BOOST_CHECK_EQUAL(cpuRefCounterStatus.size(), 1);
3550 BOOST_CHECK_EQUAL(cpuAccCounterStatus.size(), 0);
3551
3552 // Check active CpuRef counter
3553 BOOST_CHECK_EQUAL(cpuRefCounterStatus[0].m_GlobalCounterId, activeGlobalCounterIds[0]);
3554 BOOST_CHECK_EQUAL(cpuRefCounterStatus[0].m_BackendCounterId, cpuRefCounters[0]);
3555 BOOST_CHECK_EQUAL(cpuRefCounterStatus[0].m_SamplingRateInMicroseconds, capturePeriod);
3556 BOOST_CHECK_EQUAL(cpuRefCounterStatus[0].m_Enabled, true);
3557
3558 // Check inactive CpuRef counter
3559 CounterStatus inactiveCpuRefCounter = backendProfilingCpuRef.GetCounterStatus(cpuRefCounters[1]);
3560 BOOST_CHECK_EQUAL(inactiveCpuRefCounter.m_GlobalCounterId, 6);
3561 BOOST_CHECK_EQUAL(inactiveCpuRefCounter.m_BackendCounterId, cpuRefCounters[1]);
3562 BOOST_CHECK_EQUAL(inactiveCpuRefCounter.m_SamplingRateInMicroseconds, 0);
3563 BOOST_CHECK_EQUAL(inactiveCpuRefCounter.m_Enabled, false);
3564
3565 // Check inactive CpuAcc counter
3566 CounterStatus inactiveCpuAccCounter = backendProfilingCpuAcc.GetCounterStatus(cpuAccCounters[0]);
3567 BOOST_CHECK_EQUAL(inactiveCpuAccCounter.m_GlobalCounterId, 7);
3568 BOOST_CHECK_EQUAL(inactiveCpuAccCounter.m_BackendCounterId, cpuAccCounters[0]);
3569 BOOST_CHECK_EQUAL(inactiveCpuAccCounter.m_SamplingRateInMicroseconds, 0);
3570 BOOST_CHECK_EQUAL(inactiveCpuAccCounter.m_Enabled, false);
3571
3572 // Set new capture period and new active counters in CaptureData
Finn Williams032bc742020-02-12 11:02:34 +00003573 profilingService.SetCaptureData(newCapturePeriod, newActiveGlobalCounterIds, {});
James Conroy2dcd3fe2020-02-06 18:34:52 +00003574
3575 // Get vector of active counters for CpuRef and CpuAcc backends
3576 cpuRefCounterStatus = backendProfilingCpuRef.GetActiveCounters();
3577 cpuAccCounterStatus = backendProfilingCpuAcc.GetActiveCounters();
3578 BOOST_CHECK_EQUAL(cpuRefCounterStatus.size(), 1);
3579 BOOST_CHECK_EQUAL(cpuAccCounterStatus.size(), 1);
3580
3581 // Check active CpuRef counter
3582 BOOST_CHECK_EQUAL(cpuRefCounterStatus[0].m_GlobalCounterId, newActiveGlobalCounterIds[0]);
3583 BOOST_CHECK_EQUAL(cpuRefCounterStatus[0].m_BackendCounterId, cpuRefCounters[1]);
3584 BOOST_CHECK_EQUAL(cpuRefCounterStatus[0].m_SamplingRateInMicroseconds, newCapturePeriod);
3585 BOOST_CHECK_EQUAL(cpuRefCounterStatus[0].m_Enabled, true);
3586
3587 // Check active CpuAcc counter
3588 BOOST_CHECK_EQUAL(cpuAccCounterStatus[0].m_GlobalCounterId, newActiveGlobalCounterIds[1]);
3589 BOOST_CHECK_EQUAL(cpuAccCounterStatus[0].m_BackendCounterId, cpuAccCounters[0]);
3590 BOOST_CHECK_EQUAL(cpuAccCounterStatus[0].m_SamplingRateInMicroseconds, newCapturePeriod);
3591 BOOST_CHECK_EQUAL(cpuAccCounterStatus[0].m_Enabled, true);
3592
3593 // Check inactive CpuRef counter
3594 inactiveCpuRefCounter = backendProfilingCpuRef.GetCounterStatus(cpuRefCounters[0]);
3595 BOOST_CHECK_EQUAL(inactiveCpuRefCounter.m_GlobalCounterId, 5);
3596 BOOST_CHECK_EQUAL(inactiveCpuRefCounter.m_BackendCounterId, cpuRefCounters[0]);
3597 BOOST_CHECK_EQUAL(inactiveCpuRefCounter.m_SamplingRateInMicroseconds, 0);
3598 BOOST_CHECK_EQUAL(inactiveCpuRefCounter.m_Enabled, false);
3599
3600 // Reset the profiling service to stop any running thread
3601 options.m_ProfilingOptions.m_EnableProfiling = false;
3602 profilingService.ResetExternalProfilingOptions(options.m_ProfilingOptions, true);
3603}
3604
Sadik Armagancab588a2020-02-17 11:33:31 +00003605BOOST_AUTO_TEST_CASE(CheckRegisterCounters)
3606{
Kevin Mayd92a6e42021-02-04 10:27:41 +00003607 armnn::IRuntime::CreationOptions options;
Sadik Armagancab588a2020-02-17 11:33:31 +00003608 options.m_ProfilingOptions.m_EnableProfiling = true;
3609 MockBufferManager mockBuffer(1024);
Sadik Armagan3184c902020-03-18 10:57:30 +00003610
Sadik Armagancab588a2020-02-17 11:33:31 +00003611 CaptureData captureData;
Keith Davis33ed2212020-03-30 10:43:41 +01003612 MockProfilingService mockProfilingService(mockBuffer, options.m_ProfilingOptions.m_EnableProfiling, captureData);
Sadik Armagancab588a2020-02-17 11:33:31 +00003613 armnn::BackendId cpuRefId(armnn::Compute::CpuRef);
3614
3615 mockProfilingService.RegisterMapping(6, 0, cpuRefId);
3616 mockProfilingService.RegisterMapping(7, 1, cpuRefId);
3617 mockProfilingService.RegisterMapping(8, 2, cpuRefId);
3618
3619 armnn::profiling::BackendProfiling backendProfiling(options,
3620 mockProfilingService,
3621 cpuRefId);
3622
3623 armnn::profiling::Timestamp timestamp;
3624 timestamp.timestamp = 1000998;
3625 timestamp.counterValues.emplace_back(0, 700);
3626 timestamp.counterValues.emplace_back(2, 93);
3627 std::vector<armnn::profiling::Timestamp> timestamps;
3628 timestamps.push_back(timestamp);
3629 backendProfiling.ReportCounters(timestamps);
3630
3631 auto readBuffer = mockBuffer.GetReadableBuffer();
3632
3633 uint32_t headerWord0 = ReadUint32(readBuffer, 0);
3634 uint32_t headerWord1 = ReadUint32(readBuffer, 4);
3635 uint64_t readTimestamp = ReadUint64(readBuffer, 8);
3636
3637 BOOST_TEST(((headerWord0 >> 26) & 0x0000003F) == 3); // packet family
3638 BOOST_TEST(((headerWord0 >> 19) & 0x0000007F) == 0); // packet class
3639 BOOST_TEST(((headerWord0 >> 16) & 0x00000007) == 0); // packet type
3640 BOOST_TEST(headerWord1 == 20); // data length
3641 BOOST_TEST(1000998 == readTimestamp); // capture period
3642
3643 uint32_t offset = 16;
3644 // Check Counter Index
3645 uint16_t readIndex = ReadUint16(readBuffer, offset);
3646 BOOST_TEST(6 == readIndex);
3647
3648 // Check Counter Value
3649 offset += 2;
3650 uint32_t readValue = ReadUint32(readBuffer, offset);
3651 BOOST_TEST(700 == readValue);
3652
3653 // Check Counter Index
3654 offset += 4;
3655 readIndex = ReadUint16(readBuffer, offset);
3656 BOOST_TEST(8 == readIndex);
3657
3658 // Check Counter Value
3659 offset += 2;
3660 readValue = ReadUint32(readBuffer, offset);
3661 BOOST_TEST(93 == readValue);
3662}
3663
Isabella Gottardia0687ee2020-03-11 18:04:20 +00003664BOOST_AUTO_TEST_CASE(CheckFileFormat) {
3665 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
3666 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
3667
3668 // Create profiling options.
Kevin Mayd92a6e42021-02-04 10:27:41 +00003669 armnn::IRuntime::CreationOptions::ExternalProfilingOptions options;
Isabella Gottardia0687ee2020-03-11 18:04:20 +00003670 options.m_EnableProfiling = true;
3671 // Check the default value set to binary
3672 BOOST_CHECK(options.m_FileFormat == "binary");
3673
3674 // Change file format to an unsupported value
3675 options.m_FileFormat = "json";
3676 // Enable the profiling service
3677 armnn::profiling::ProfilingService profilingService;
3678 profilingService.ResetExternalProfilingOptions(options, true);
3679 // Start the command handler and the send thread
3680 profilingService.Update();
3681 BOOST_CHECK(profilingService.GetCurrentState()==ProfilingState::NotConnected);
3682
3683 // Redirect the output to a local stream so that we can parse the warning message
3684 std::stringstream ss;
3685 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3686
3687 // When Update is called and the current state is ProfilingState::NotConnected
3688 // an exception will be raised from GetProfilingConnection and displayed as warning in the output local stream
3689 profilingService.Update();
3690
3691 streamRedirector.CancelRedirect();
3692
3693 // Check that the expected error has occurred and logged to the standard output
David Monahana8837bf2020-04-16 10:01:56 +01003694 if (ss.str().find("Unsupported profiling file format, only binary is supported") == std::string::npos)
Isabella Gottardia0687ee2020-03-11 18:04:20 +00003695 {
3696 std::cout << ss.str();
3697 BOOST_FAIL("Expected string not found.");
3698 }
3699}
3700
Francis Murtagh1f7db452019-08-14 09:49:34 +01003701BOOST_AUTO_TEST_SUITE_END()