blob: 9c3007b0175f188980f935fdda0ff5ecf4a7d1dc [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
Jim Flynn75c14f42022-03-10 22:05:42 +00009#include <ArmNNProfilingServiceInitialiser.hpp>
Matteo Martincigh8a837172019-10-04 17:01:07 +010010#include <CommandHandler.hpp>
Sadik Armaganb5f01b22019-09-18 17:29:00 +010011#include <ConnectionAcknowledgedCommandHandler.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010012#include <CounterDirectory.hpp>
David Monahande803072020-01-30 12:44:23 +000013#include <CounterIdMap.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010014#include <Holder.hpp>
Matteo Martincighe0e6efc2019-10-04 17:17:42 +010015#include <ICounterValues.hpp>
Francis Murtaghfcb8ef62019-09-20 15:40:09 +010016#include <PeriodicCounterCapture.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010017#include <PeriodicCounterSelectionCommandHandler.hpp>
18#include <ProfilingStateMachine.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010019#include <ProfilingUtils.hpp>
James Conroy2dcd3fe2020-02-06 18:34:52 +000020#include <RegisterBackendCounters.hpp>
Narumol Prangnawarat48033692019-09-20 12:04:55 +010021#include <RequestCounterDirectoryCommandHandler.hpp>
Teresa Charlin9bab4962019-09-06 12:28:35 +010022#include <Runtime.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010023#include <SocketProfilingConnection.hpp>
Matteo Martincighcdfb9412019-11-08 11:23:06 +000024#include <SendCounterPacket.hpp>
Sadik Armagan3896b472020-02-10 12:24:15 +000025#include <SendThread.hpp>
Matteo Martincighcdfb9412019-11-08 11:23:06 +000026#include <SendTimelinePacket.hpp>
Keith Davis02356de2019-08-26 18:28:17 +010027
Matteo Martincigh6db5f202019-09-05 12:02:04 +010028#include <armnn/Conversion.hpp>
Matteo Martincigh54fb9572019-10-02 12:50:57 +010029#include <armnn/Utils.hpp>
Jim Flynn75c14f42022-03-10 22:05:42 +000030
31#include <armnn/profiling/ArmNNProfiling.hpp>
32#include <armnn/profiling/ProfilingOptions.hpp>
33
Jim Flynn75c14f42022-03-10 22:05:42 +000034#include <backends/BackendProfiling.hpp>
Matteo Martincigh54fb9572019-10-02 12:50:57 +010035
Jim Flynnbbfe6032020-07-20 16:57:44 +010036#include <common/include/CommandHandlerKey.hpp>
37#include <common/include/CommandHandlerRegistry.hpp>
Jim Flynn75c14f42022-03-10 22:05:42 +000038#include <common/include/EncodeVersion.hpp>
Jim Flynn9265a882022-03-10 23:35:26 +000039#include <common/include/IgnoreUnused.hpp>
Jim Flynn75c14f42022-03-10 22:05:42 +000040#include <common/include/NumericCast.hpp>
Jim Flynnbbfe6032020-07-20 16:57:44 +010041#include <common/include/Packet.hpp>
Jim Flynn75c14f42022-03-10 22:05:42 +000042#include <common/include/PacketVersionResolver.hpp>
43#include <common/include/SocketConnectionException.hpp>
44#include <common/include/SwTrace.hpp>
Jim Flynnbbfe6032020-07-20 16:57:44 +010045
Sadik Armagan1625efc2021-06-10 18:24:34 +010046#include <doctest/doctest.h>
47
Sadik Armagan95e9efc2021-08-05 15:01:07 +010048#include <algorithm>
Nikhil Rajbc626052019-08-15 15:49:45 +010049#include <cstdint>
50#include <cstring>
Keith Davis3201eea2019-10-24 17:30:41 +010051#include <iostream>
Aron Virginas-Tare898db92019-08-22 12:56:34 +010052#include <limits>
Francis Murtagh11f99b42019-08-16 11:28:52 +010053#include <map>
Aron Virginas-Tare898db92019-08-22 12:56:34 +010054#include <random>
James Conroy2dcd3fe2020-02-06 18:34:52 +000055
Francis Murtagh1f7db452019-08-14 09:49:34 +010056
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000057using namespace arm::pipe;
Finn Williams09ad6f92019-12-19 17:05:18 +000058using PacketType = MockProfilingConnection::PacketType;
Aron Virginas-Tare898db92019-08-22 12:56:34 +010059
Sadik Armagan1625efc2021-06-10 18:24:34 +010060TEST_SUITE("ExternalProfiling")
61{
62TEST_CASE("CheckCommandHandlerKeyComparisons")
Francis Murtagh1f7db452019-08-14 09:49:34 +010063{
Jim Flynnbbfe6032020-07-20 16:57:44 +010064 arm::pipe::CommandHandlerKey testKey1_0(1, 1, 1);
65 arm::pipe::CommandHandlerKey testKey1_1(1, 1, 1);
66 arm::pipe::CommandHandlerKey testKey1_2(1, 2, 1);
Jim Flynn397043f2019-10-17 17:37:10 +010067
Jim Flynnbbfe6032020-07-20 16:57:44 +010068 arm::pipe::CommandHandlerKey testKey0(0, 1, 1);
69 arm::pipe::CommandHandlerKey testKey1(0, 1, 1);
70 arm::pipe::CommandHandlerKey testKey2(0, 1, 1);
71 arm::pipe::CommandHandlerKey testKey3(0, 0, 0);
72 arm::pipe::CommandHandlerKey testKey4(0, 2, 2);
73 arm::pipe::CommandHandlerKey testKey5(0, 0, 2);
Jim Flynn397043f2019-10-17 17:37:10 +010074
Sadik Armagan1625efc2021-06-10 18:24:34 +010075 CHECK(testKey1_0 > testKey0);
76 CHECK(testKey1_0 == testKey1_1);
77 CHECK(testKey1_0 < testKey1_2);
Francis Murtagh1f7db452019-08-14 09:49:34 +010078
Sadik Armagan1625efc2021-06-10 18:24:34 +010079 CHECK(testKey1 < testKey4);
80 CHECK(testKey1 > testKey3);
81 CHECK(testKey1 <= testKey4);
82 CHECK(testKey1 >= testKey3);
83 CHECK(testKey1 <= testKey2);
84 CHECK(testKey1 >= testKey2);
85 CHECK(testKey1 == testKey2);
86 CHECK(testKey1 == testKey1);
Francis Murtagh1f7db452019-08-14 09:49:34 +010087
Sadik Armagan1625efc2021-06-10 18:24:34 +010088 CHECK(!(testKey1 == testKey5));
89 CHECK(!(testKey1 != testKey1));
90 CHECK(testKey1 != testKey5);
Francis Murtagh1f7db452019-08-14 09:49:34 +010091
Sadik Armagan1625efc2021-06-10 18:24:34 +010092 CHECK((testKey1 == testKey2 && testKey2 == testKey1));
93 CHECK((testKey0 == testKey1 && testKey1 == testKey2 && testKey0 == testKey2));
Francis Murtagh1f7db452019-08-14 09:49:34 +010094
Sadik Armagan1625efc2021-06-10 18:24:34 +010095 CHECK(testKey1.GetPacketId() == 1);
96 CHECK(testKey1.GetVersion() == 1);
Francis Murtagh1f7db452019-08-14 09:49:34 +010097
Jim Flynnbbfe6032020-07-20 16:57:44 +010098 std::vector<arm::pipe::CommandHandlerKey> vect = {
99 arm::pipe::CommandHandlerKey(0, 0, 1), arm::pipe::CommandHandlerKey(0, 2, 0),
100 arm::pipe::CommandHandlerKey(0, 1, 0), arm::pipe::CommandHandlerKey(0, 2, 1),
101 arm::pipe::CommandHandlerKey(0, 1, 1), arm::pipe::CommandHandlerKey(0, 0, 1),
102 arm::pipe::CommandHandlerKey(0, 2, 0), arm::pipe::CommandHandlerKey(0, 0, 0) };
Francis Murtagh1f7db452019-08-14 09:49:34 +0100103
104 std::sort(vect.begin(), vect.end());
105
Jim Flynnbbfe6032020-07-20 16:57:44 +0100106 std::vector<arm::pipe::CommandHandlerKey> expectedVect = {
107 arm::pipe::CommandHandlerKey(0, 0, 0), arm::pipe::CommandHandlerKey(0, 0, 1),
108 arm::pipe::CommandHandlerKey(0, 0, 1), arm::pipe::CommandHandlerKey(0, 1, 0),
109 arm::pipe::CommandHandlerKey(0, 1, 1), arm::pipe::CommandHandlerKey(0, 2, 0),
110 arm::pipe::CommandHandlerKey(0, 2, 0), arm::pipe::CommandHandlerKey(0, 2, 1) };
Francis Murtagh1f7db452019-08-14 09:49:34 +0100111
Sadik Armagan1625efc2021-06-10 18:24:34 +0100112 CHECK(vect == expectedVect);
Francis Murtagh1f7db452019-08-14 09:49:34 +0100113}
114
Sadik Armagan1625efc2021-06-10 18:24:34 +0100115TEST_CASE("CheckPacketKeyComparisons")
Jim Flynned25e0e2019-10-18 13:21:43 +0100116{
Jim Flynnbbfe6032020-07-20 16:57:44 +0100117 arm::pipe::PacketKey key0(0, 0);
118 arm::pipe::PacketKey key1(0, 0);
119 arm::pipe::PacketKey key2(0, 1);
120 arm::pipe::PacketKey key3(0, 2);
121 arm::pipe::PacketKey key4(1, 0);
122 arm::pipe::PacketKey key5(1, 0);
123 arm::pipe::PacketKey key6(1, 1);
Jim Flynned25e0e2019-10-18 13:21:43 +0100124
Sadik Armagan1625efc2021-06-10 18:24:34 +0100125 CHECK(!(key0 < key1));
126 CHECK(!(key0 > key1));
127 CHECK(key0 <= key1);
128 CHECK(key0 >= key1);
129 CHECK(key0 == key1);
130 CHECK(key0 < key2);
131 CHECK(key2 < key3);
132 CHECK(key3 > key0);
133 CHECK(key4 == key5);
134 CHECK(key4 > key0);
135 CHECK(key5 < key6);
136 CHECK(key5 <= key6);
137 CHECK(key5 != key6);
Jim Flynned25e0e2019-10-18 13:21:43 +0100138}
139
Sadik Armagan1625efc2021-06-10 18:24:34 +0100140TEST_CASE("CheckCommandHandler")
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100141{
Jim Flynn6c9f17d2022-03-10 23:13:01 +0000142 LogLevelSwapper logLevelSwapper(arm::pipe::LogSeverity::Fatal);
143
Jim Flynnbbfe6032020-07-20 16:57:44 +0100144 arm::pipe::PacketVersionResolver packetVersionResolver;
Matteo Martincigh8a837172019-10-04 17:01:07 +0100145 ProfilingStateMachine profilingStateMachine;
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100146
Matteo Martincigh8a837172019-10-04 17:01:07 +0100147 TestProfilingConnectionBase testProfilingConnectionBase;
148 TestProfilingConnectionTimeoutError testProfilingConnectionTimeOutError;
149 TestProfilingConnectionArmnnError testProfilingConnectionArmnnError;
Keith Davis3201eea2019-10-24 17:30:41 +0100150 CounterDirectory counterDirectory;
151 MockBufferManager mockBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +0000152 SendCounterPacket sendCounterPacket(mockBuffer);
153 SendThread sendThread(profilingStateMachine, mockBuffer, sendCounterPacket);
Matteo Martincighcdfb9412019-11-08 11:23:06 +0000154 SendTimelinePacket sendTimelinePacket(mockBuffer);
Jim Flynn6398a982020-05-27 17:05:21 +0100155 MockProfilingServiceStatus mockProfilingServiceStatus;
Matteo Martincighcdfb9412019-11-08 11:23:06 +0000156
Keith Davis3201eea2019-10-24 17:30:41 +0100157 ConnectionAcknowledgedCommandHandler connectionAcknowledgedCommandHandler(0, 1, 4194304, counterDirectory,
Matteo Martincighcdfb9412019-11-08 11:23:06 +0000158 sendCounterPacket, sendTimelinePacket,
Jim Flynn6398a982020-05-27 17:05:21 +0100159 profilingStateMachine,
160 mockProfilingServiceStatus);
Jim Flynnbbfe6032020-07-20 16:57:44 +0100161 arm::pipe::CommandHandlerRegistry commandHandlerRegistry;
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100162
Matteo Martincighc2728f92019-10-07 12:35:21 +0100163 commandHandlerRegistry.RegisterFunctor(&connectionAcknowledgedCommandHandler);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100164
Matteo Martincigh8a837172019-10-04 17:01:07 +0100165 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
166 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100167
Keith Davis3201eea2019-10-24 17:30:41 +0100168 CommandHandler commandHandler0(1, true, commandHandlerRegistry, packetVersionResolver);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100169
Colm Donelan2ba48d22019-11-29 09:10:59 +0000170 // This should start the command handler thread return the connection ack and put the profiling
171 // service into active state.
Matteo Martincigh8a837172019-10-04 17:01:07 +0100172 commandHandler0.Start(testProfilingConnectionBase);
Colm Donelan2ba48d22019-11-29 09:10:59 +0000173 // Try to start the send thread many times, it must only start once
Matteo Martincigh8a837172019-10-04 17:01:07 +0100174 commandHandler0.Start(testProfilingConnectionBase);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100175
Colm Donelan2ba48d22019-11-29 09:10:59 +0000176 // This could take up to 20mSec but we'll check often.
177 for (int i = 0; i < 10; i++)
Matteo Martincigh8a837172019-10-04 17:01:07 +0100178 {
179 if (profilingStateMachine.GetCurrentState() == ProfilingState::Active)
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100180 {
Matteo Martincigh8a837172019-10-04 17:01:07 +0100181 break;
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100182 }
Colm Donelan2ba48d22019-11-29 09:10:59 +0000183 std::this_thread::sleep_for(std::chrono::milliseconds(2));
Matteo Martincigh8a837172019-10-04 17:01:07 +0100184 }
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100185
Sadik Armagan1625efc2021-06-10 18:24:34 +0100186 CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::Active);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100187
Colm Donelan2ba48d22019-11-29 09:10:59 +0000188 // Close the thread again.
189 commandHandler0.Stop();
190
191 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
192 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
193
194 // In this test we'll simulate a timeout without a connection ack packet being received.
195 // Stop after timeout is set so we expect the command handler to stop almost immediately.
196 CommandHandler commandHandler1(1, true, commandHandlerRegistry, packetVersionResolver);
197
198 commandHandler1.Start(testProfilingConnectionTimeOutError);
199 // Wait until we know a timeout exception has been sent at least once.
200 for (int i = 0; i < 10; i++)
201 {
202 if (testProfilingConnectionTimeOutError.ReadCalledCount())
203 {
204 break;
205 }
206 std::this_thread::sleep_for(std::chrono::milliseconds(2));
207 }
Colm Donelan2ba48d22019-11-29 09:10:59 +0000208
209 // The command handler loop should have stopped after the timeout.
Finn Williams09ad6f92019-12-19 17:05:18 +0000210 // wait for the timeout exception to be processed and the loop to break.
211 uint32_t timeout = 50;
212 uint32_t timeSlept = 0;
213 while (commandHandler1.IsRunning())
214 {
215 if (timeSlept >= timeout)
216 {
Sadik Armagan1625efc2021-06-10 18:24:34 +0100217 FAIL("Timeout: The command handler loop did not stop after the timeout");
Finn Williams09ad6f92019-12-19 17:05:18 +0000218 }
219 std::this_thread::sleep_for(std::chrono::milliseconds(1));
220 timeSlept ++;
221 }
Colm Donelan2ba48d22019-11-29 09:10:59 +0000222
223 commandHandler1.Stop();
224 // The state machine should never have received the ack so will still be in WaitingForAck.
Sadik Armagan1625efc2021-06-10 18:24:34 +0100225 CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::WaitingForAck);
Colm Donelan2ba48d22019-11-29 09:10:59 +0000226
Finn Williams09ad6f92019-12-19 17:05:18 +0000227 // Now try sending a bad connection acknowledged packet
228 TestProfilingConnectionBadAckPacket testProfilingConnectionBadAckPacket;
229 commandHandler1.Start(testProfilingConnectionBadAckPacket);
230 commandHandler1.Stop();
231 // This should also not change the state machine
Sadik Armagan1625efc2021-06-10 18:24:34 +0100232 CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::WaitingForAck);
Finn Williams09ad6f92019-12-19 17:05:18 +0000233
Colm Donelan2ba48d22019-11-29 09:10:59 +0000234 // Disable stop after timeout and now commandHandler1 should persist after a timeout
235 commandHandler1.SetStopAfterTimeout(false);
236 // Restart the thread.
237 commandHandler1.Start(testProfilingConnectionTimeOutError);
238
239 // Wait for at the three timeouts and the ack to be sent.
240 for (int i = 0; i < 10; i++)
241 {
242 if (testProfilingConnectionTimeOutError.ReadCalledCount() > 3)
243 {
244 break;
245 }
246 std::this_thread::sleep_for(std::chrono::milliseconds(2));
247 }
248 commandHandler1.Stop();
249
250 // Even after the 3 exceptions the ack packet should have transitioned the command handler to active.
Sadik Armagan1625efc2021-06-10 18:24:34 +0100251 CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::Active);
Colm Donelan2ba48d22019-11-29 09:10:59 +0000252
253 // A command handler that gets exceptions other than timeouts should keep going.
254 CommandHandler commandHandler2(1, false, commandHandlerRegistry, packetVersionResolver);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100255
Matteo Martincigh8a837172019-10-04 17:01:07 +0100256 commandHandler2.Start(testProfilingConnectionArmnnError);
257
Colm Donelan2ba48d22019-11-29 09:10:59 +0000258 // Wait for two exceptions to be thrown.
259 for (int i = 0; i < 10; i++)
260 {
261 if (testProfilingConnectionTimeOutError.ReadCalledCount() >= 2)
262 {
263 break;
264 }
265 std::this_thread::sleep_for(std::chrono::milliseconds(2));
266 }
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100267
Sadik Armagan1625efc2021-06-10 18:24:34 +0100268 CHECK(commandHandler2.IsRunning());
Matteo Martincigh8a837172019-10-04 17:01:07 +0100269 commandHandler2.Stop();
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100270}
271
Sadik Armagan1625efc2021-06-10 18:24:34 +0100272TEST_CASE("CheckEncodeVersion")
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100273{
Jim Flynnbbfe6032020-07-20 16:57:44 +0100274 arm::pipe::Version version1(12);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100275
Sadik Armagan1625efc2021-06-10 18:24:34 +0100276 CHECK(version1.GetMajor() == 0);
277 CHECK(version1.GetMinor() == 0);
278 CHECK(version1.GetPatch() == 12);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100279
Jim Flynnbbfe6032020-07-20 16:57:44 +0100280 arm::pipe::Version version2(4108);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100281
Sadik Armagan1625efc2021-06-10 18:24:34 +0100282 CHECK(version2.GetMajor() == 0);
283 CHECK(version2.GetMinor() == 1);
284 CHECK(version2.GetPatch() == 12);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100285
Jim Flynnbbfe6032020-07-20 16:57:44 +0100286 arm::pipe::Version version3(4198412);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100287
Sadik Armagan1625efc2021-06-10 18:24:34 +0100288 CHECK(version3.GetMajor() == 1);
289 CHECK(version3.GetMinor() == 1);
290 CHECK(version3.GetPatch() == 12);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100291
Jim Flynnbbfe6032020-07-20 16:57:44 +0100292 arm::pipe::Version version4(0);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100293
Sadik Armagan1625efc2021-06-10 18:24:34 +0100294 CHECK(version4.GetMajor() == 0);
295 CHECK(version4.GetMinor() == 0);
296 CHECK(version4.GetPatch() == 0);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100297
Jim Flynnbbfe6032020-07-20 16:57:44 +0100298 arm::pipe::Version version5(1, 0, 0);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100299 CHECK(version5.GetEncodedValue() == 4194304);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100300}
301
Sadik Armagan1625efc2021-06-10 18:24:34 +0100302TEST_CASE("CheckPacketClass")
Nikhil Rajbc626052019-08-15 15:49:45 +0100303{
Keith Davis3201eea2019-10-24 17:30:41 +0100304 uint32_t length = 4;
Matteo Martincigh67ef2a52019-10-10 13:29:02 +0100305 std::unique_ptr<unsigned char[]> packetData0 = std::make_unique<unsigned char[]>(length);
306 std::unique_ptr<unsigned char[]> packetData1 = std::make_unique<unsigned char[]>(0);
307 std::unique_ptr<unsigned char[]> nullPacketData;
Nikhil Rajbc626052019-08-15 15:49:45 +0100308
Jim Flynnbbfe6032020-07-20 16:57:44 +0100309 arm::pipe::Packet packetTest0(472580096, length, packetData0);
Nikhil Rajbc626052019-08-15 15:49:45 +0100310
Sadik Armagan1625efc2021-06-10 18:24:34 +0100311 CHECK(packetTest0.GetHeader() == 472580096);
312 CHECK(packetTest0.GetPacketFamily() == 7);
313 CHECK(packetTest0.GetPacketId() == 43);
314 CHECK(packetTest0.GetLength() == length);
315 CHECK(packetTest0.GetPacketType() == 3);
316 CHECK(packetTest0.GetPacketClass() == 5);
Nikhil Rajbc626052019-08-15 15:49:45 +0100317
Sadik Armagan1625efc2021-06-10 18:24:34 +0100318 CHECK_THROWS_AS(arm::pipe::Packet packetTest1(472580096, 0, packetData1), arm::pipe::InvalidArgumentException);
319 CHECK_NOTHROW(arm::pipe::Packet packetTest2(472580096, 0, nullPacketData));
Nikhil Rajbc626052019-08-15 15:49:45 +0100320
Jim Flynnbbfe6032020-07-20 16:57:44 +0100321 arm::pipe::Packet packetTest3(472580096, 0, nullPacketData);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100322 CHECK(packetTest3.GetLength() == 0);
323 CHECK(packetTest3.GetData() == nullptr);
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100324
Matteo Martincigh67ef2a52019-10-10 13:29:02 +0100325 const unsigned char* packetTest0Data = packetTest0.GetData();
Jim Flynnbbfe6032020-07-20 16:57:44 +0100326 arm::pipe::Packet packetTest4(std::move(packetTest0));
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100327
Sadik Armagan1625efc2021-06-10 18:24:34 +0100328 CHECK(packetTest0.GetData() == nullptr);
329 CHECK(packetTest4.GetData() == packetTest0Data);
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100330
Sadik Armagan1625efc2021-06-10 18:24:34 +0100331 CHECK(packetTest4.GetHeader() == 472580096);
332 CHECK(packetTest4.GetPacketFamily() == 7);
333 CHECK(packetTest4.GetPacketId() == 43);
334 CHECK(packetTest4.GetLength() == length);
335 CHECK(packetTest4.GetPacketType() == 3);
336 CHECK(packetTest4.GetPacketClass() == 5);
Nikhil Rajbc626052019-08-15 15:49:45 +0100337}
338
Sadik Armagan1625efc2021-06-10 18:24:34 +0100339TEST_CASE("CheckCommandHandlerFunctor")
Francis Murtagh11f99b42019-08-16 11:28:52 +0100340{
Francis Murtagh11f99b42019-08-16 11:28:52 +0100341 // Hard code the version as it will be the same during a single profiling session
342 uint32_t version = 1;
343
Jim Flynn397043f2019-10-17 17:37:10 +0100344 TestFunctorA testFunctorA(7, 461, version);
345 TestFunctorB testFunctorB(8, 963, version);
346 TestFunctorC testFunctorC(5, 983, version);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100347
Jim Flynnbbfe6032020-07-20 16:57:44 +0100348 arm::pipe::CommandHandlerKey keyA(
349 testFunctorA.GetFamilyId(), testFunctorA.GetPacketId(), testFunctorA.GetVersion());
350 arm::pipe::CommandHandlerKey keyB(
351 testFunctorB.GetFamilyId(), testFunctorB.GetPacketId(), testFunctorB.GetVersion());
352 arm::pipe::CommandHandlerKey keyC(
353 testFunctorC.GetFamilyId(), testFunctorC.GetPacketId(), testFunctorC.GetVersion());
Francis Murtagh11f99b42019-08-16 11:28:52 +0100354
355 // Create the unwrapped map to simulate the Command Handler Registry
Jim Flynnbbfe6032020-07-20 16:57:44 +0100356 std::map<arm::pipe::CommandHandlerKey, arm::pipe::CommandHandlerFunctor*> registry;
Francis Murtagh11f99b42019-08-16 11:28:52 +0100357
358 registry.insert(std::make_pair(keyB, &testFunctorB));
359 registry.insert(std::make_pair(keyA, &testFunctorA));
360 registry.insert(std::make_pair(keyC, &testFunctorC));
361
362 // Check the order of the map is correct
363 auto it = registry.begin();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100364 CHECK(it->first == keyC); // familyId == 5
Francis Murtagh11f99b42019-08-16 11:28:52 +0100365 it++;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100366 CHECK(it->first == keyA); // familyId == 7
Francis Murtagh11f99b42019-08-16 11:28:52 +0100367 it++;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100368 CHECK(it->first == keyB); // familyId == 8
Francis Murtagh11f99b42019-08-16 11:28:52 +0100369
Matteo Martincigh67ef2a52019-10-10 13:29:02 +0100370 std::unique_ptr<unsigned char[]> packetDataA;
371 std::unique_ptr<unsigned char[]> packetDataB;
372 std::unique_ptr<unsigned char[]> packetDataC;
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100373
Jim Flynnbbfe6032020-07-20 16:57:44 +0100374 arm::pipe::Packet packetA(500000000, 0, packetDataA);
375 arm::pipe::Packet packetB(600000000, 0, packetDataB);
376 arm::pipe::Packet packetC(400000000, 0, packetDataC);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100377
378 // Check the correct operator of derived class is called
Jim Flynnbbfe6032020-07-20 16:57:44 +0100379 registry.at(arm::pipe::CommandHandlerKey(
380 packetA.GetPacketFamily(), packetA.GetPacketId(), version))->operator()(packetA);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100381 CHECK(testFunctorA.GetCount() == 1);
382 CHECK(testFunctorB.GetCount() == 0);
383 CHECK(testFunctorC.GetCount() == 0);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100384
Jim Flynnbbfe6032020-07-20 16:57:44 +0100385 registry.at(arm::pipe::CommandHandlerKey(
386 packetB.GetPacketFamily(), packetB.GetPacketId(), version))->operator()(packetB);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100387 CHECK(testFunctorA.GetCount() == 1);
388 CHECK(testFunctorB.GetCount() == 1);
389 CHECK(testFunctorC.GetCount() == 0);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100390
Jim Flynnbbfe6032020-07-20 16:57:44 +0100391 registry.at(arm::pipe::CommandHandlerKey(
392 packetC.GetPacketFamily(), packetC.GetPacketId(), version))->operator()(packetC);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100393 CHECK(testFunctorA.GetCount() == 1);
394 CHECK(testFunctorB.GetCount() == 1);
395 CHECK(testFunctorC.GetCount() == 1);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100396}
397
Sadik Armagan1625efc2021-06-10 18:24:34 +0100398TEST_CASE("CheckCommandHandlerRegistry")
Francis Murtagh94d79152019-08-16 17:45:07 +0100399{
400 // Hard code the version as it will be the same during a single profiling session
401 uint32_t version = 1;
402
Jim Flynn397043f2019-10-17 17:37:10 +0100403 TestFunctorA testFunctorA(7, 461, version);
404 TestFunctorB testFunctorB(8, 963, version);
405 TestFunctorC testFunctorC(5, 983, version);
Francis Murtagh94d79152019-08-16 17:45:07 +0100406
407 // Create the Command Handler Registry
Jim Flynnbbfe6032020-07-20 16:57:44 +0100408 arm::pipe::CommandHandlerRegistry registry;
Francis Murtagh94d79152019-08-16 17:45:07 +0100409
410 // Register multiple different derived classes
Matteo Martincighc2728f92019-10-07 12:35:21 +0100411 registry.RegisterFunctor(&testFunctorA);
412 registry.RegisterFunctor(&testFunctorB);
413 registry.RegisterFunctor(&testFunctorC);
Francis Murtagh94d79152019-08-16 17:45:07 +0100414
Matteo Martincigh67ef2a52019-10-10 13:29:02 +0100415 std::unique_ptr<unsigned char[]> packetDataA;
416 std::unique_ptr<unsigned char[]> packetDataB;
417 std::unique_ptr<unsigned char[]> packetDataC;
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100418
Jim Flynnbbfe6032020-07-20 16:57:44 +0100419 arm::pipe::Packet packetA(500000000, 0, packetDataA);
420 arm::pipe::Packet packetB(600000000, 0, packetDataB);
421 arm::pipe::Packet packetC(400000000, 0, packetDataC);
Francis Murtagh94d79152019-08-16 17:45:07 +0100422
423 // Check the correct operator of derived class is called
Jim Flynn397043f2019-10-17 17:37:10 +0100424 registry.GetFunctor(packetA.GetPacketFamily(), packetA.GetPacketId(), version)->operator()(packetA);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100425 CHECK(testFunctorA.GetCount() == 1);
426 CHECK(testFunctorB.GetCount() == 0);
427 CHECK(testFunctorC.GetCount() == 0);
Francis Murtagh94d79152019-08-16 17:45:07 +0100428
Jim Flynn397043f2019-10-17 17:37:10 +0100429 registry.GetFunctor(packetB.GetPacketFamily(), packetB.GetPacketId(), version)->operator()(packetB);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100430 CHECK(testFunctorA.GetCount() == 1);
431 CHECK(testFunctorB.GetCount() == 1);
432 CHECK(testFunctorC.GetCount() == 0);
Francis Murtagh94d79152019-08-16 17:45:07 +0100433
Jim Flynn397043f2019-10-17 17:37:10 +0100434 registry.GetFunctor(packetC.GetPacketFamily(), packetC.GetPacketId(), version)->operator()(packetC);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100435 CHECK(testFunctorA.GetCount() == 1);
436 CHECK(testFunctorB.GetCount() == 1);
437 CHECK(testFunctorC.GetCount() == 1);
Francis Murtagh94d79152019-08-16 17:45:07 +0100438
439 // Re-register an existing key with a new function
Jim Flynn397043f2019-10-17 17:37:10 +0100440 registry.RegisterFunctor(&testFunctorC, testFunctorA.GetFamilyId(), testFunctorA.GetPacketId(), version);
441 registry.GetFunctor(packetA.GetPacketFamily(), packetA.GetPacketId(), version)->operator()(packetC);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100442 CHECK(testFunctorA.GetCount() == 1);
443 CHECK(testFunctorB.GetCount() == 1);
444 CHECK(testFunctorC.GetCount() == 2);
Francis Murtagh94d79152019-08-16 17:45:07 +0100445
446 // Check that non-existent key returns nullptr for its functor
Sadik Armagan1625efc2021-06-10 18:24:34 +0100447 CHECK_THROWS_AS(registry.GetFunctor(0, 0, 0), arm::pipe::ProfilingException);
Francis Murtagh94d79152019-08-16 17:45:07 +0100448}
449
Sadik Armagan1625efc2021-06-10 18:24:34 +0100450TEST_CASE("CheckPacketVersionResolver")
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100451{
452 // Set up random number generator for generating packetId values
453 std::random_device device;
454 std::mt19937 generator(device());
455 std::uniform_int_distribution<uint32_t> distribution(std::numeric_limits<uint32_t>::min(),
456 std::numeric_limits<uint32_t>::max());
457
458 // NOTE: Expected version is always 1.0.0, regardless of packetId
Jim Flynnbbfe6032020-07-20 16:57:44 +0100459 const arm::pipe::Version expectedVersion(1, 0, 0);
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100460
Jim Flynnbbfe6032020-07-20 16:57:44 +0100461 arm::pipe::PacketVersionResolver packetVersionResolver;
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100462
463 constexpr unsigned int numTests = 10u;
464
465 for (unsigned int i = 0u; i < numTests; ++i)
466 {
Jim Flynned25e0e2019-10-18 13:21:43 +0100467 const uint32_t familyId = distribution(generator);
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100468 const uint32_t packetId = distribution(generator);
Jim Flynnbbfe6032020-07-20 16:57:44 +0100469 arm::pipe::Version resolvedVersion = packetVersionResolver.ResolvePacketVersion(familyId, packetId);
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100470
Sadik Armagan1625efc2021-06-10 18:24:34 +0100471 CHECK(resolvedVersion == expectedVersion);
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100472 }
473}
Matteo Martincighd0613b52019-10-09 16:47:04 +0100474
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100475void ProfilingCurrentStateThreadImpl(ProfilingStateMachine& states)
476{
477 ProfilingState newState = ProfilingState::NotConnected;
478 states.GetCurrentState();
479 states.TransitionToState(newState);
480}
481
Sadik Armagan1625efc2021-06-10 18:24:34 +0100482TEST_CASE("CheckProfilingStateMachine")
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100483{
484 ProfilingStateMachine profilingState1(ProfilingState::Uninitialised);
485 profilingState1.TransitionToState(ProfilingState::Uninitialised);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100486 CHECK(profilingState1.GetCurrentState() == ProfilingState::Uninitialised);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100487
488 ProfilingStateMachine profilingState2(ProfilingState::Uninitialised);
489 profilingState2.TransitionToState(ProfilingState::NotConnected);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100490 CHECK(profilingState2.GetCurrentState() == ProfilingState::NotConnected);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100491
492 ProfilingStateMachine profilingState3(ProfilingState::NotConnected);
493 profilingState3.TransitionToState(ProfilingState::NotConnected);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100494 CHECK(profilingState3.GetCurrentState() == ProfilingState::NotConnected);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100495
496 ProfilingStateMachine profilingState4(ProfilingState::NotConnected);
497 profilingState4.TransitionToState(ProfilingState::WaitingForAck);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100498 CHECK(profilingState4.GetCurrentState() == ProfilingState::WaitingForAck);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100499
500 ProfilingStateMachine profilingState5(ProfilingState::WaitingForAck);
501 profilingState5.TransitionToState(ProfilingState::WaitingForAck);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100502 CHECK(profilingState5.GetCurrentState() == ProfilingState::WaitingForAck);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100503
504 ProfilingStateMachine profilingState6(ProfilingState::WaitingForAck);
505 profilingState6.TransitionToState(ProfilingState::Active);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100506 CHECK(profilingState6.GetCurrentState() == ProfilingState::Active);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100507
508 ProfilingStateMachine profilingState7(ProfilingState::Active);
509 profilingState7.TransitionToState(ProfilingState::NotConnected);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100510 CHECK(profilingState7.GetCurrentState() == ProfilingState::NotConnected);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100511
512 ProfilingStateMachine profilingState8(ProfilingState::Active);
513 profilingState8.TransitionToState(ProfilingState::Active);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100514 CHECK(profilingState8.GetCurrentState() == ProfilingState::Active);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100515
516 ProfilingStateMachine profilingState9(ProfilingState::Uninitialised);
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000517 CHECK_THROWS_AS(profilingState9.TransitionToState(ProfilingState::WaitingForAck), arm::pipe::ProfilingException);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100518
519 ProfilingStateMachine profilingState10(ProfilingState::Uninitialised);
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000520 CHECK_THROWS_AS(profilingState10.TransitionToState(ProfilingState::Active), arm::pipe::ProfilingException);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100521
522 ProfilingStateMachine profilingState11(ProfilingState::NotConnected);
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000523 CHECK_THROWS_AS(profilingState11.TransitionToState(ProfilingState::Uninitialised), arm::pipe::ProfilingException);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100524
525 ProfilingStateMachine profilingState12(ProfilingState::NotConnected);
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000526 CHECK_THROWS_AS(profilingState12.TransitionToState(ProfilingState::Active), arm::pipe::ProfilingException);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100527
528 ProfilingStateMachine profilingState13(ProfilingState::WaitingForAck);
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000529 CHECK_THROWS_AS(profilingState13.TransitionToState(ProfilingState::Uninitialised), arm::pipe::ProfilingException);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100530
531 ProfilingStateMachine profilingState14(ProfilingState::WaitingForAck);
Jim Flynn53e46992019-10-14 12:31:10 +0100532 profilingState14.TransitionToState(ProfilingState::NotConnected);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100533 CHECK(profilingState14.GetCurrentState() == ProfilingState::NotConnected);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100534
535 ProfilingStateMachine profilingState15(ProfilingState::Active);
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000536 CHECK_THROWS_AS(profilingState15.TransitionToState(ProfilingState::Uninitialised), arm::pipe::ProfilingException);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100537
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000538 ProfilingStateMachine profilingState16(ProfilingState::Active);
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000539 CHECK_THROWS_AS(profilingState16.TransitionToState(ProfilingState::WaitingForAck), arm::pipe::ProfilingException);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100540
541 ProfilingStateMachine profilingState17(ProfilingState::Uninitialised);
542
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100543 std::vector<std::thread> threads;
544 for (unsigned int i = 0; i < 5; ++i)
545 {
546 threads.push_back(std::thread(ProfilingCurrentStateThreadImpl, std::ref(profilingState17)));
547 }
548 std::for_each(threads.begin(), threads.end(), [](std::thread& theThread)
549 {
550 theThread.join();
551 });
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100552
Sadik Armagan1625efc2021-06-10 18:24:34 +0100553 CHECK((profilingState17.GetCurrentState() == ProfilingState::NotConnected));
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100554}
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100555
Jim Flynn8355ec92019-09-17 12:29:50 +0100556void CaptureDataWriteThreadImpl(Holder& holder, uint32_t capturePeriod, const std::vector<uint16_t>& counterIds)
Francis Murtagh68f78d82019-09-04 16:42:29 +0100557{
Finn Williams032bc742020-02-12 11:02:34 +0000558 holder.SetCaptureData(capturePeriod, counterIds, {});
Francis Murtagh68f78d82019-09-04 16:42:29 +0100559}
560
Francis Murtaghbd707162019-09-09 11:26:44 +0100561void CaptureDataReadThreadImpl(const Holder& holder, CaptureData& captureData)
Francis Murtagh68f78d82019-09-04 16:42:29 +0100562{
563 captureData = holder.GetCaptureData();
564}
565
Sadik Armagan1625efc2021-06-10 18:24:34 +0100566TEST_CASE("CheckCaptureDataHolder")
Francis Murtagh68f78d82019-09-04 16:42:29 +0100567{
Francis Murtaghbd707162019-09-09 11:26:44 +0100568 std::map<uint32_t, std::vector<uint16_t>> periodIdMap;
569 std::vector<uint16_t> counterIds;
Jim Flynn8355ec92019-09-17 12:29:50 +0100570 uint32_t numThreads = 10;
571 for (uint32_t i = 0; i < numThreads; ++i)
Francis Murtaghbd707162019-09-09 11:26:44 +0100572 {
573 counterIds.emplace_back(i);
574 periodIdMap.insert(std::make_pair(i, counterIds));
575 }
Francis Murtagh68f78d82019-09-04 16:42:29 +0100576
Jim Flynn8355ec92019-09-17 12:29:50 +0100577 // Verify the read and write threads set the holder correctly
578 // and retrieve the expected values
Francis Murtagh68f78d82019-09-04 16:42:29 +0100579 Holder holder;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100580 CHECK((holder.GetCaptureData()).GetCapturePeriod() == 0);
581 CHECK(((holder.GetCaptureData()).GetCounterIds()).empty());
Francis Murtagh68f78d82019-09-04 16:42:29 +0100582
583 // Check Holder functions
Francis Murtaghbd707162019-09-09 11:26:44 +0100584 std::thread thread1(CaptureDataWriteThreadImpl, std::ref(holder), 2, std::ref(periodIdMap[2]));
Francis Murtagh68f78d82019-09-04 16:42:29 +0100585 thread1.join();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100586 CHECK((holder.GetCaptureData()).GetCapturePeriod() == 2);
587 CHECK((holder.GetCaptureData()).GetCounterIds() == periodIdMap[2]);
Jim Flynn8355ec92019-09-17 12:29:50 +0100588 // NOTE: now that we have some initial values in the holder we don't have to worry
589 // in the multi-threaded section below about a read thread accessing the holder
590 // before any write thread has gotten to it so we read period = 0, counterIds empty
591 // instead of period = 0, counterIds = {0} as will the case when write thread 0
592 // has executed.
Francis Murtagh68f78d82019-09-04 16:42:29 +0100593
594 CaptureData captureData;
595 std::thread thread2(CaptureDataReadThreadImpl, std::ref(holder), std::ref(captureData));
596 thread2.join();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100597 CHECK(captureData.GetCapturePeriod() == 2);
598 CHECK(captureData.GetCounterIds() == periodIdMap[2]);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100599
Jim Flynn8355ec92019-09-17 12:29:50 +0100600 std::map<uint32_t, CaptureData> captureDataIdMap;
601 for (uint32_t i = 0; i < numThreads; ++i)
602 {
603 CaptureData perThreadCaptureData;
604 captureDataIdMap.insert(std::make_pair(i, perThreadCaptureData));
605 }
606
Francis Murtaghbd707162019-09-09 11:26:44 +0100607 std::vector<std::thread> threadsVect;
Jim Flynn8355ec92019-09-17 12:29:50 +0100608 std::vector<std::thread> readThreadsVect;
609 for (uint32_t i = 0; i < numThreads; ++i)
Francis Murtaghbd707162019-09-09 11:26:44 +0100610 {
Keith Davis3201eea2019-10-24 17:30:41 +0100611 threadsVect.emplace_back(
612 std::thread(CaptureDataWriteThreadImpl, std::ref(holder), i, std::ref(periodIdMap[i])));
Francis Murtagh06965692019-09-05 16:29:01 +0100613
Jim Flynn8355ec92019-09-17 12:29:50 +0100614 // Verify that the CaptureData goes into the thread in a virgin state
Sadik Armagan1625efc2021-06-10 18:24:34 +0100615 CHECK(captureDataIdMap.at(i).GetCapturePeriod() == 0);
616 CHECK(captureDataIdMap.at(i).GetCounterIds().empty());
Keith Davis3201eea2019-10-24 17:30:41 +0100617 readThreadsVect.emplace_back(
618 std::thread(CaptureDataReadThreadImpl, std::ref(holder), std::ref(captureDataIdMap.at(i))));
Francis Murtaghbd707162019-09-09 11:26:44 +0100619 }
620
Jim Flynn8355ec92019-09-17 12:29:50 +0100621 for (uint32_t i = 0; i < numThreads; ++i)
Francis Murtaghbd707162019-09-09 11:26:44 +0100622 {
623 threadsVect[i].join();
Francis Murtaghbd707162019-09-09 11:26:44 +0100624 readThreadsVect[i].join();
625 }
Francis Murtagh68f78d82019-09-04 16:42:29 +0100626
Jim Flynn8355ec92019-09-17 12:29:50 +0100627 // Look at the CaptureData that each read thread has filled
628 // the capture period it read should match the counter ids entry
629 for (uint32_t i = 0; i < numThreads; ++i)
630 {
631 CaptureData perThreadCaptureData = captureDataIdMap.at(i);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100632 CHECK(perThreadCaptureData.GetCounterIds() == periodIdMap.at(perThreadCaptureData.GetCapturePeriod()));
Jim Flynn8355ec92019-09-17 12:29:50 +0100633 }
Matthew Bentham46d1c622019-09-13 12:45:04 +0100634}
Francis Murtagh68f78d82019-09-04 16:42:29 +0100635
Sadik Armagan1625efc2021-06-10 18:24:34 +0100636TEST_CASE("CaptureDataMethods")
Matthew Bentham46d1c622019-09-13 12:45:04 +0100637{
Jim Flynn8355ec92019-09-17 12:29:50 +0100638 // Check CaptureData setter and getter functions
Keith Davis3201eea2019-10-24 17:30:41 +0100639 std::vector<uint16_t> counterIds = { 42, 29, 13 };
Jim Flynn8355ec92019-09-17 12:29:50 +0100640 CaptureData captureData;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100641 CHECK(captureData.GetCapturePeriod() == 0);
642 CHECK((captureData.GetCounterIds()).empty());
Jim Flynn8355ec92019-09-17 12:29:50 +0100643 captureData.SetCapturePeriod(150);
644 captureData.SetCounterIds(counterIds);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100645 CHECK(captureData.GetCapturePeriod() == 150);
646 CHECK(captureData.GetCounterIds() == counterIds);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100647
Jim Flynn8355ec92019-09-17 12:29:50 +0100648 // Check assignment operator
Francis Murtagh68f78d82019-09-04 16:42:29 +0100649 CaptureData secondCaptureData;
Francis Murtagh68f78d82019-09-04 16:42:29 +0100650
Jim Flynn8355ec92019-09-17 12:29:50 +0100651 secondCaptureData = captureData;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100652 CHECK(secondCaptureData.GetCapturePeriod() == 150);
653 CHECK(secondCaptureData.GetCounterIds() == counterIds);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100654
655 // Check copy constructor
Jim Flynn8355ec92019-09-17 12:29:50 +0100656 CaptureData copyConstructedCaptureData(captureData);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100657
Sadik Armagan1625efc2021-06-10 18:24:34 +0100658 CHECK(copyConstructedCaptureData.GetCapturePeriod() == 150);
659 CHECK(copyConstructedCaptureData.GetCounterIds() == counterIds);
Keith Davis02356de2019-08-26 18:28:17 +0100660}
Francis Murtagh68f78d82019-09-04 16:42:29 +0100661
Sadik Armagan1625efc2021-06-10 18:24:34 +0100662TEST_CASE("CheckProfilingServiceDisabled")
Keith Davis02356de2019-08-26 18:28:17 +0100663{
Jim Flynn4c9ed1d2022-01-23 23:57:20 +0000664 ProfilingOptions options;
Jim Flynn34430252022-03-04 15:03:58 +0000665 armnn::ArmNNProfilingServiceInitialiser initialiser;
666 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100667 profilingService.ResetExternalProfilingOptions(options, true);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100668 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100669 profilingService.Update();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100670 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis02356de2019-08-26 18:28:17 +0100671}
672
Sadik Armagan1625efc2021-06-10 18:24:34 +0100673TEST_CASE("CheckProfilingServiceCounterDirectory")
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100674{
Jim Flynn4c9ed1d2022-01-23 23:57:20 +0000675 ProfilingOptions options;
Jim Flynn34430252022-03-04 15:03:58 +0000676 armnn::ArmNNProfilingServiceInitialiser initialiser;
677 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100678 profilingService.ResetExternalProfilingOptions(options, true);
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100679
Matteo Martincigha84edee2019-10-02 12:50:57 +0100680 const ICounterDirectory& counterDirectory0 = profilingService.GetCounterDirectory();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100681 CHECK(counterDirectory0.GetCounterCount() == 0);
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100682 profilingService.Update();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100683 CHECK(counterDirectory0.GetCounterCount() == 0);
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100684
685 options.m_EnableProfiling = true;
Matteo Martincigha84edee2019-10-02 12:50:57 +0100686 profilingService.ResetExternalProfilingOptions(options);
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100687
Matteo Martincigha84edee2019-10-02 12:50:57 +0100688 const ICounterDirectory& counterDirectory1 = profilingService.GetCounterDirectory();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100689 CHECK(counterDirectory1.GetCounterCount() == 0);
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100690 profilingService.Update();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100691 CHECK(counterDirectory1.GetCounterCount() != 0);
Colm Donelan2ba48d22019-11-29 09:10:59 +0000692 // Reset the profiling service to stop any running thread
693 options.m_EnableProfiling = false;
694 profilingService.ResetExternalProfilingOptions(options, true);
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100695}
696
Sadik Armagan1625efc2021-06-10 18:24:34 +0100697TEST_CASE("CheckProfilingServiceCounterValues")
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100698{
Jim Flynn4c9ed1d2022-01-23 23:57:20 +0000699 ProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +0100700 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +0000701 armnn::ArmNNProfilingServiceInitialiser initialiser;
702 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100703 profilingService.ResetExternalProfilingOptions(options, true);
704
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100705 profilingService.Update();
Matteo Martincigha84edee2019-10-02 12:50:57 +0100706 const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
Keith Davis3201eea2019-10-24 17:30:41 +0100707 const Counters& counters = counterDirectory.GetCounters();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100708 CHECK(!counters.empty());
Matteo Martincigha84edee2019-10-02 12:50:57 +0100709
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100710 std::vector<std::thread> writers;
711
Sadik Armagan1625efc2021-06-10 18:24:34 +0100712 CHECK(!counters.empty());
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000713 uint16_t inferencesRun = INFERENCES_RUN;
Finn Williamsf3fcf322020-05-11 14:38:02 +0100714
715 // Test GetAbsoluteCounterValue
716 for (int i = 0; i < 4; ++i)
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100717 {
Finn Williamsf3fcf322020-05-11 14:38:02 +0100718 // Increment and decrement the INFERENCES_RUN counter 250 times
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100719 writers.push_back(std::thread([&profilingService, inferencesRun]()
Finn Williamsf3fcf322020-05-11 14:38:02 +0100720 {
721 for (int i = 0; i < 250; ++i)
722 {
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100723 profilingService.IncrementCounterValue(inferencesRun);
Finn Williamsf3fcf322020-05-11 14:38:02 +0100724 }
725 }));
726 // Add 10 to the INFERENCES_RUN counter 200 times
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100727 writers.push_back(std::thread([&profilingService, inferencesRun]()
Finn Williamsf3fcf322020-05-11 14:38:02 +0100728 {
729 for (int i = 0; i < 200; ++i)
730 {
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100731 profilingService.AddCounterValue(inferencesRun, 10);
Finn Williamsf3fcf322020-05-11 14:38:02 +0100732 }
733 }));
734 // Subtract 5 from the INFERENCES_RUN counter 200 times
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100735 writers.push_back(std::thread([&profilingService, inferencesRun]()
Finn Williamsf3fcf322020-05-11 14:38:02 +0100736 {
737 for (int i = 0; i < 200; ++i)
738 {
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100739 profilingService.SubtractCounterValue(inferencesRun, 5);
Finn Williamsf3fcf322020-05-11 14:38:02 +0100740 }
741 }));
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100742 }
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100743 std::for_each(writers.begin(), writers.end(), mem_fn(&std::thread::join));
744
Finn Williamsf3fcf322020-05-11 14:38:02 +0100745 uint32_t absoluteCounterValue = 0;
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100746
Sadik Armagan1625efc2021-06-10 18:24:34 +0100747 CHECK_NOTHROW(absoluteCounterValue = profilingService.GetAbsoluteCounterValue(INFERENCES_RUN));
748 CHECK(absoluteCounterValue == 5000);
Finn Williamsf3fcf322020-05-11 14:38:02 +0100749
750 // Test SetCounterValue
Sadik Armagan1625efc2021-06-10 18:24:34 +0100751 CHECK_NOTHROW(profilingService.SetCounterValue(INFERENCES_RUN, 0));
752 CHECK_NOTHROW(absoluteCounterValue = profilingService.GetAbsoluteCounterValue(INFERENCES_RUN));
753 CHECK(absoluteCounterValue == 0);
Finn Williamsf3fcf322020-05-11 14:38:02 +0100754
755 // Test GetDeltaCounterValue
756 writers.clear();
757 uint32_t deltaCounterValue = 0;
758 //Start a reading thread to randomly read the INFERENCES_RUN counter value
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100759 std::thread reader([&profilingService, inferencesRun](uint32_t& deltaCounterValue)
Finn Williamsf3fcf322020-05-11 14:38:02 +0100760 {
761 for (int i = 0; i < 300; ++i)
762 {
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100763 deltaCounterValue += profilingService.GetDeltaCounterValue(inferencesRun);
Finn Williamsf3fcf322020-05-11 14:38:02 +0100764 }
765 }, std::ref(deltaCounterValue));
766
767 for (int i = 0; i < 4; ++i)
768 {
769 // Increment and decrement the INFERENCES_RUN counter 250 times
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100770 writers.push_back(std::thread([&profilingService, inferencesRun]()
Finn Williamsf3fcf322020-05-11 14:38:02 +0100771 {
772 for (int i = 0; i < 250; ++i)
773 {
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100774 profilingService.IncrementCounterValue(inferencesRun);
Finn Williamsf3fcf322020-05-11 14:38:02 +0100775 }
776 }));
777 // Add 10 to the INFERENCES_RUN counter 200 times
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100778 writers.push_back(std::thread([&profilingService, inferencesRun]()
Finn Williamsf3fcf322020-05-11 14:38:02 +0100779 {
780 for (int i = 0; i < 200; ++i)
781 {
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100782 profilingService.AddCounterValue(inferencesRun, 10);
Finn Williamsf3fcf322020-05-11 14:38:02 +0100783 }
784 }));
785 // Subtract 5 from the INFERENCES_RUN counter 200 times
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100786 writers.push_back(std::thread([&profilingService, inferencesRun]()
Finn Williamsf3fcf322020-05-11 14:38:02 +0100787 {
788 for (int i = 0; i < 200; ++i)
789 {
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100790 profilingService.SubtractCounterValue(inferencesRun, 5);
Finn Williamsf3fcf322020-05-11 14:38:02 +0100791 }
792 }));
793 }
794
795 std::for_each(writers.begin(), writers.end(), mem_fn(&std::thread::join));
796 reader.join();
797
798 // Do one last read in case the reader stopped early
799 deltaCounterValue += profilingService.GetDeltaCounterValue(INFERENCES_RUN);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100800 CHECK(deltaCounterValue == 5000);
Finn Williamsf3fcf322020-05-11 14:38:02 +0100801
Colm Donelan2ba48d22019-11-29 09:10:59 +0000802 // Reset the profiling service to stop any running thread
803 options.m_EnableProfiling = false;
804 profilingService.ResetExternalProfilingOptions(options, true);
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100805}
806
Sadik Armagan1625efc2021-06-10 18:24:34 +0100807TEST_CASE("CheckProfilingObjectUids")
Matteo Martincighab173e92019-09-05 12:02:04 +0100808{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100809 uint16_t uid = 0;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100810 CHECK_NOTHROW(uid = GetNextUid());
811 CHECK(uid >= 1);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100812
813 uint16_t nextUid = 0;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100814 CHECK_NOTHROW(nextUid = GetNextUid());
815 CHECK(nextUid > uid);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100816
817 std::vector<uint16_t> counterUids;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100818 CHECK_NOTHROW(counterUids = GetNextCounterUids(uid,0));
819 CHECK(counterUids.size() == 1);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100820
821 std::vector<uint16_t> nextCounterUids;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100822 CHECK_NOTHROW(nextCounterUids = GetNextCounterUids(nextUid, 2));
823 CHECK(nextCounterUids.size() == 2);
824 CHECK(nextCounterUids[0] > counterUids[0]);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100825
826 std::vector<uint16_t> counterUidsMultiCore;
Francis Murtagh1e5afee2021-05-11 09:37:47 +0100827 uint16_t thirdUid = nextCounterUids[0];
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100828 uint16_t numberOfCores = 13;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100829 CHECK_NOTHROW(counterUidsMultiCore = GetNextCounterUids(thirdUid, numberOfCores));
830 CHECK(counterUidsMultiCore.size() == numberOfCores);
831 CHECK(counterUidsMultiCore.front() >= nextCounterUids[0]);
Keith Davis3201eea2019-10-24 17:30:41 +0100832 for (size_t i = 1; i < numberOfCores; i++)
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100833 {
Sadik Armagan1625efc2021-06-10 18:24:34 +0100834 CHECK(counterUidsMultiCore[i] == counterUidsMultiCore[i - 1] + 1);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100835 }
Sadik Armagan1625efc2021-06-10 18:24:34 +0100836 CHECK(counterUidsMultiCore.back() == counterUidsMultiCore.front() + numberOfCores - 1);
Matteo Martincighab173e92019-09-05 12:02:04 +0100837}
838
Sadik Armagan1625efc2021-06-10 18:24:34 +0100839TEST_CASE("CheckCounterDirectoryRegisterCategory")
Matteo Martincighab173e92019-09-05 12:02:04 +0100840{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100841 CounterDirectory counterDirectory;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100842 CHECK(counterDirectory.GetCategoryCount() == 0);
843 CHECK(counterDirectory.GetDeviceCount() == 0);
844 CHECK(counterDirectory.GetCounterSetCount() == 0);
845 CHECK(counterDirectory.GetCounterCount() == 0);
Matteo Martincighab173e92019-09-05 12:02:04 +0100846
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100847 // Register a category with an invalid name
848 const Category* noCategory = nullptr;
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000849 CHECK_THROWS_AS(noCategory = counterDirectory.RegisterCategory(""), arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100850 CHECK(counterDirectory.GetCategoryCount() == 0);
851 CHECK(!noCategory);
Matteo Martincighab173e92019-09-05 12:02:04 +0100852
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100853 // Register a category with an invalid name
Sadik Armagan1625efc2021-06-10 18:24:34 +0100854 CHECK_THROWS_AS(noCategory = counterDirectory.RegisterCategory("invalid category"),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000855 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100856 CHECK(counterDirectory.GetCategoryCount() == 0);
857 CHECK(!noCategory);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100858
859 // Register a new category
860 const std::string categoryName = "some_category";
Keith Davis3201eea2019-10-24 17:30:41 +0100861 const Category* category = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100862 CHECK_NOTHROW(category = counterDirectory.RegisterCategory(categoryName));
863 CHECK(counterDirectory.GetCategoryCount() == 1);
864 CHECK(category);
865 CHECK(category->m_Name == categoryName);
866 CHECK(category->m_Counters.empty());
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100867
868 // Get the registered category
869 const Category* registeredCategory = counterDirectory.GetCategory(categoryName);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100870 CHECK(counterDirectory.GetCategoryCount() == 1);
871 CHECK(registeredCategory);
872 CHECK(registeredCategory == category);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100873
874 // Try to get a category not registered
875 const Category* notRegisteredCategory = counterDirectory.GetCategory("not_registered_category");
Sadik Armagan1625efc2021-06-10 18:24:34 +0100876 CHECK(counterDirectory.GetCategoryCount() == 1);
877 CHECK(!notRegisteredCategory);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100878
879 // Register a category already registered
880 const Category* anotherCategory = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100881 CHECK_THROWS_AS(anotherCategory = counterDirectory.RegisterCategory(categoryName),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000882 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100883 CHECK(counterDirectory.GetCategoryCount() == 1);
884 CHECK(!anotherCategory);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100885
886 // Register a device for testing
887 const std::string deviceName = "some_device";
Keith Davis3201eea2019-10-24 17:30:41 +0100888 const Device* device = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100889 CHECK_NOTHROW(device = counterDirectory.RegisterDevice(deviceName));
890 CHECK(counterDirectory.GetDeviceCount() == 1);
891 CHECK(device);
892 CHECK(device->m_Uid >= 1);
893 CHECK(device->m_Name == deviceName);
894 CHECK(device->m_Cores == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100895
896 // Register a new category not associated to any device
897 const std::string categoryWoDeviceName = "some_category_without_device";
Keith Davis3201eea2019-10-24 17:30:41 +0100898 const Category* categoryWoDevice = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100899 CHECK_NOTHROW(categoryWoDevice = counterDirectory.RegisterCategory(categoryWoDeviceName));
900 CHECK(counterDirectory.GetCategoryCount() == 2);
901 CHECK(categoryWoDevice);
902 CHECK(categoryWoDevice->m_Name == categoryWoDeviceName);
903 CHECK(categoryWoDevice->m_Counters.empty());
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100904
Sadik Armagan4c998992020-02-25 12:44:44 +0000905 // Register a new category associated to an invalid device name (already exist)
906 const Category* categoryInvalidDeviceName = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100907 CHECK_THROWS_AS(categoryInvalidDeviceName =
Sadik Armagan4c998992020-02-25 12:44:44 +0000908 counterDirectory.RegisterCategory(categoryWoDeviceName),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000909 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100910 CHECK(counterDirectory.GetCategoryCount() == 2);
911 CHECK(!categoryInvalidDeviceName);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100912
913 // Register a new category associated to a valid device
914 const std::string categoryWValidDeviceName = "some_category_with_valid_device";
Keith Davis3201eea2019-10-24 17:30:41 +0100915 const Category* categoryWValidDevice = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100916 CHECK_NOTHROW(categoryWValidDevice =
Sadik Armagan4c998992020-02-25 12:44:44 +0000917 counterDirectory.RegisterCategory(categoryWValidDeviceName));
Sadik Armagan1625efc2021-06-10 18:24:34 +0100918 CHECK(counterDirectory.GetCategoryCount() == 3);
919 CHECK(categoryWValidDevice);
920 CHECK(categoryWValidDevice != category);
921 CHECK(categoryWValidDevice->m_Name == categoryWValidDeviceName);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100922
923 // Register a counter set for testing
924 const std::string counterSetName = "some_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +0100925 const CounterSet* counterSet = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100926 CHECK_NOTHROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
927 CHECK(counterDirectory.GetCounterSetCount() == 1);
928 CHECK(counterSet);
929 CHECK(counterSet->m_Uid >= 1);
930 CHECK(counterSet->m_Name == counterSetName);
931 CHECK(counterSet->m_Count == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100932
933 // Register a new category not associated to any counter set
934 const std::string categoryWoCounterSetName = "some_category_without_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +0100935 const Category* categoryWoCounterSet = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100936 CHECK_NOTHROW(categoryWoCounterSet =
Sadik Armagan4c998992020-02-25 12:44:44 +0000937 counterDirectory.RegisterCategory(categoryWoCounterSetName));
Sadik Armagan1625efc2021-06-10 18:24:34 +0100938 CHECK(counterDirectory.GetCategoryCount() == 4);
939 CHECK(categoryWoCounterSet);
940 CHECK(categoryWoCounterSet->m_Name == categoryWoCounterSetName);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100941
942 // Register a new category associated to a valid counter set
943 const std::string categoryWValidCounterSetName = "some_category_with_valid_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +0100944 const Category* categoryWValidCounterSet = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100945 CHECK_NOTHROW(categoryWValidCounterSet = counterDirectory.RegisterCategory(categoryWValidCounterSetName));
946 CHECK(counterDirectory.GetCategoryCount() == 5);
947 CHECK(categoryWValidCounterSet);
948 CHECK(categoryWValidCounterSet != category);
949 CHECK(categoryWValidCounterSet->m_Name == categoryWValidCounterSetName);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100950
951 // Register a new category associated to a valid device and counter set
952 const std::string categoryWValidDeviceAndValidCounterSetName = "some_category_with_valid_device_and_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +0100953 const Category* categoryWValidDeviceAndValidCounterSet = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100954 CHECK_NOTHROW(categoryWValidDeviceAndValidCounterSet = counterDirectory.RegisterCategory(
Sadik Armagan4c998992020-02-25 12:44:44 +0000955 categoryWValidDeviceAndValidCounterSetName));
Sadik Armagan1625efc2021-06-10 18:24:34 +0100956 CHECK(counterDirectory.GetCategoryCount() == 6);
957 CHECK(categoryWValidDeviceAndValidCounterSet);
958 CHECK(categoryWValidDeviceAndValidCounterSet != category);
959 CHECK(categoryWValidDeviceAndValidCounterSet->m_Name == categoryWValidDeviceAndValidCounterSetName);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100960}
961
Sadik Armagan1625efc2021-06-10 18:24:34 +0100962TEST_CASE("CheckCounterDirectoryRegisterDevice")
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100963{
964 CounterDirectory counterDirectory;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100965 CHECK(counterDirectory.GetCategoryCount() == 0);
966 CHECK(counterDirectory.GetDeviceCount() == 0);
967 CHECK(counterDirectory.GetCounterSetCount() == 0);
968 CHECK(counterDirectory.GetCounterCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100969
970 // Register a device with an invalid name
971 const Device* noDevice = nullptr;
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000972 CHECK_THROWS_AS(noDevice = counterDirectory.RegisterDevice(""), arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100973 CHECK(counterDirectory.GetDeviceCount() == 0);
974 CHECK(!noDevice);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100975
976 // Register a device with an invalid name
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000977 CHECK_THROWS_AS(noDevice = counterDirectory.RegisterDevice("inv@lid nam€"), arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100978 CHECK(counterDirectory.GetDeviceCount() == 0);
979 CHECK(!noDevice);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100980
981 // Register a new device with no cores or parent category
982 const std::string deviceName = "some_device";
Keith Davis3201eea2019-10-24 17:30:41 +0100983 const Device* device = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100984 CHECK_NOTHROW(device = counterDirectory.RegisterDevice(deviceName));
985 CHECK(counterDirectory.GetDeviceCount() == 1);
986 CHECK(device);
987 CHECK(device->m_Name == deviceName);
988 CHECK(device->m_Uid >= 1);
989 CHECK(device->m_Cores == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100990
Matteo Martincigh657ab2d2019-09-18 10:53:24 +0100991 // Try getting an unregistered device
992 const Device* unregisteredDevice = counterDirectory.GetDevice(9999);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100993 CHECK(!unregisteredDevice);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +0100994
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100995 // Get the registered device
996 const Device* registeredDevice = counterDirectory.GetDevice(device->m_Uid);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100997 CHECK(counterDirectory.GetDeviceCount() == 1);
998 CHECK(registeredDevice);
999 CHECK(registeredDevice == device);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001000
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001001 // Register a device with the name of a device already registered
1002 const Device* deviceSameName = nullptr;
Jim Flynnf9db3ef2022-03-08 21:23:44 +00001003 CHECK_THROWS_AS(deviceSameName = counterDirectory.RegisterDevice(deviceName),
1004 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001005 CHECK(counterDirectory.GetDeviceCount() == 1);
1006 CHECK(!deviceSameName);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001007
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001008 // Register a new device with cores and no parent category
1009 const std::string deviceWCoresName = "some_device_with_cores";
Keith Davis3201eea2019-10-24 17:30:41 +01001010 const Device* deviceWCores = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001011 CHECK_NOTHROW(deviceWCores = counterDirectory.RegisterDevice(deviceWCoresName, 2));
1012 CHECK(counterDirectory.GetDeviceCount() == 2);
1013 CHECK(deviceWCores);
1014 CHECK(deviceWCores->m_Name == deviceWCoresName);
1015 CHECK(deviceWCores->m_Uid >= 1);
1016 CHECK(deviceWCores->m_Uid > device->m_Uid);
1017 CHECK(deviceWCores->m_Cores == 2);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001018
1019 // Get the registered device
1020 const Device* registeredDeviceWCores = counterDirectory.GetDevice(deviceWCores->m_Uid);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001021 CHECK(counterDirectory.GetDeviceCount() == 2);
1022 CHECK(registeredDeviceWCores);
1023 CHECK(registeredDeviceWCores == deviceWCores);
1024 CHECK(registeredDeviceWCores != device);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001025
1026 // Register a new device with cores and invalid parent category
1027 const std::string deviceWCoresWInvalidParentCategoryName = "some_device_with_cores_with_invalid_parent_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001028 const Device* deviceWCoresWInvalidParentCategory = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001029 CHECK_THROWS_AS(deviceWCoresWInvalidParentCategory =
Keith Davis3201eea2019-10-24 17:30:41 +01001030 counterDirectory.RegisterDevice(deviceWCoresWInvalidParentCategoryName, 3, std::string("")),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001031 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001032 CHECK(counterDirectory.GetDeviceCount() == 2);
1033 CHECK(!deviceWCoresWInvalidParentCategory);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001034
1035 // Register a new device with cores and invalid parent category
1036 const std::string deviceWCoresWInvalidParentCategoryName2 = "some_device_with_cores_with_invalid_parent_category2";
Keith Davis3201eea2019-10-24 17:30:41 +01001037 const Device* deviceWCoresWInvalidParentCategory2 = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001038 CHECK_THROWS_AS(deviceWCoresWInvalidParentCategory2 = counterDirectory.RegisterDevice(
Keith Davis3201eea2019-10-24 17:30:41 +01001039 deviceWCoresWInvalidParentCategoryName2, 3, std::string("invalid_parent_category")),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001040 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001041 CHECK(counterDirectory.GetDeviceCount() == 2);
1042 CHECK(!deviceWCoresWInvalidParentCategory2);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001043
1044 // Register a category for testing
1045 const std::string categoryName = "some_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001046 const Category* category = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001047 CHECK_NOTHROW(category = counterDirectory.RegisterCategory(categoryName));
1048 CHECK(counterDirectory.GetCategoryCount() == 1);
1049 CHECK(category);
1050 CHECK(category->m_Name == categoryName);
1051 CHECK(category->m_Counters.empty());
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001052
1053 // Register a new device with cores and valid parent category
1054 const std::string deviceWCoresWValidParentCategoryName = "some_device_with_cores_with_valid_parent_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001055 const Device* deviceWCoresWValidParentCategory = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001056 CHECK_NOTHROW(deviceWCoresWValidParentCategory =
Keith Davis3201eea2019-10-24 17:30:41 +01001057 counterDirectory.RegisterDevice(deviceWCoresWValidParentCategoryName, 4, categoryName));
Sadik Armagan1625efc2021-06-10 18:24:34 +01001058 CHECK(counterDirectory.GetDeviceCount() == 3);
1059 CHECK(deviceWCoresWValidParentCategory);
1060 CHECK(deviceWCoresWValidParentCategory->m_Name == deviceWCoresWValidParentCategoryName);
1061 CHECK(deviceWCoresWValidParentCategory->m_Uid >= 1);
1062 CHECK(deviceWCoresWValidParentCategory->m_Uid > device->m_Uid);
1063 CHECK(deviceWCoresWValidParentCategory->m_Uid > deviceWCores->m_Uid);
1064 CHECK(deviceWCoresWValidParentCategory->m_Cores == 4);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001065}
1066
Sadik Armagan1625efc2021-06-10 18:24:34 +01001067TEST_CASE("CheckCounterDirectoryRegisterCounterSet")
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001068{
1069 CounterDirectory counterDirectory;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001070 CHECK(counterDirectory.GetCategoryCount() == 0);
1071 CHECK(counterDirectory.GetDeviceCount() == 0);
1072 CHECK(counterDirectory.GetCounterSetCount() == 0);
1073 CHECK(counterDirectory.GetCounterCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001074
1075 // Register a counter set with an invalid name
1076 const CounterSet* noCounterSet = nullptr;
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001077 CHECK_THROWS_AS(noCounterSet = counterDirectory.RegisterCounterSet(""), arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001078 CHECK(counterDirectory.GetCounterSetCount() == 0);
1079 CHECK(!noCounterSet);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001080
1081 // Register a counter set with an invalid name
Sadik Armagan1625efc2021-06-10 18:24:34 +01001082 CHECK_THROWS_AS(noCounterSet = counterDirectory.RegisterCounterSet("invalid name"),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001083 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001084 CHECK(counterDirectory.GetCounterSetCount() == 0);
1085 CHECK(!noCounterSet);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001086
1087 // Register a new counter set with no count or parent category
1088 const std::string counterSetName = "some_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +01001089 const CounterSet* counterSet = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001090 CHECK_NOTHROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
1091 CHECK(counterDirectory.GetCounterSetCount() == 1);
1092 CHECK(counterSet);
1093 CHECK(counterSet->m_Name == counterSetName);
1094 CHECK(counterSet->m_Uid >= 1);
1095 CHECK(counterSet->m_Count == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001096
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001097 // Try getting an unregistered counter set
1098 const CounterSet* unregisteredCounterSet = counterDirectory.GetCounterSet(9999);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001099 CHECK(!unregisteredCounterSet);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001100
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001101 // Get the registered counter set
1102 const CounterSet* registeredCounterSet = counterDirectory.GetCounterSet(counterSet->m_Uid);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001103 CHECK(counterDirectory.GetCounterSetCount() == 1);
1104 CHECK(registeredCounterSet);
1105 CHECK(registeredCounterSet == counterSet);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001106
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001107 // Register a counter set with the name of a counter set already registered
1108 const CounterSet* counterSetSameName = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001109 CHECK_THROWS_AS(counterSetSameName = counterDirectory.RegisterCounterSet(counterSetName),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001110 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001111 CHECK(counterDirectory.GetCounterSetCount() == 1);
1112 CHECK(!counterSetSameName);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001113
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001114 // Register a new counter set with count and no parent category
1115 const std::string counterSetWCountName = "some_counter_set_with_count";
Keith Davis3201eea2019-10-24 17:30:41 +01001116 const CounterSet* counterSetWCount = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001117 CHECK_NOTHROW(counterSetWCount = counterDirectory.RegisterCounterSet(counterSetWCountName, 37));
1118 CHECK(counterDirectory.GetCounterSetCount() == 2);
1119 CHECK(counterSetWCount);
1120 CHECK(counterSetWCount->m_Name == counterSetWCountName);
1121 CHECK(counterSetWCount->m_Uid >= 1);
1122 CHECK(counterSetWCount->m_Uid > counterSet->m_Uid);
1123 CHECK(counterSetWCount->m_Count == 37);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001124
1125 // Get the registered counter set
1126 const CounterSet* registeredCounterSetWCount = counterDirectory.GetCounterSet(counterSetWCount->m_Uid);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001127 CHECK(counterDirectory.GetCounterSetCount() == 2);
1128 CHECK(registeredCounterSetWCount);
1129 CHECK(registeredCounterSetWCount == counterSetWCount);
1130 CHECK(registeredCounterSetWCount != counterSet);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001131
1132 // Register a new counter set with count and invalid parent category
1133 const std::string counterSetWCountWInvalidParentCategoryName = "some_counter_set_with_count_"
1134 "with_invalid_parent_category";
1135 const CounterSet* counterSetWCountWInvalidParentCategory = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001136 CHECK_THROWS_AS(counterSetWCountWInvalidParentCategory = counterDirectory.RegisterCounterSet(
Keith Davis3201eea2019-10-24 17:30:41 +01001137 counterSetWCountWInvalidParentCategoryName, 42, std::string("")),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001138 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001139 CHECK(counterDirectory.GetCounterSetCount() == 2);
1140 CHECK(!counterSetWCountWInvalidParentCategory);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001141
1142 // Register a new counter set with count and invalid parent category
1143 const std::string counterSetWCountWInvalidParentCategoryName2 = "some_counter_set_with_count_"
1144 "with_invalid_parent_category2";
1145 const CounterSet* counterSetWCountWInvalidParentCategory2 = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001146 CHECK_THROWS_AS(counterSetWCountWInvalidParentCategory2 = counterDirectory.RegisterCounterSet(
Keith Davis3201eea2019-10-24 17:30:41 +01001147 counterSetWCountWInvalidParentCategoryName2, 42, std::string("invalid_parent_category")),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001148 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001149 CHECK(counterDirectory.GetCounterSetCount() == 2);
1150 CHECK(!counterSetWCountWInvalidParentCategory2);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001151
1152 // Register a category for testing
1153 const std::string categoryName = "some_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001154 const Category* category = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001155 CHECK_NOTHROW(category = counterDirectory.RegisterCategory(categoryName));
1156 CHECK(counterDirectory.GetCategoryCount() == 1);
1157 CHECK(category);
1158 CHECK(category->m_Name == categoryName);
1159 CHECK(category->m_Counters.empty());
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001160
1161 // Register a new counter set with count and valid parent category
1162 const std::string counterSetWCountWValidParentCategoryName = "some_counter_set_with_count_"
1163 "with_valid_parent_category";
1164 const CounterSet* counterSetWCountWValidParentCategory = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001165 CHECK_NOTHROW(counterSetWCountWValidParentCategory = counterDirectory.RegisterCounterSet(
Keith Davis3201eea2019-10-24 17:30:41 +01001166 counterSetWCountWValidParentCategoryName, 42, categoryName));
Sadik Armagan1625efc2021-06-10 18:24:34 +01001167 CHECK(counterDirectory.GetCounterSetCount() == 3);
1168 CHECK(counterSetWCountWValidParentCategory);
1169 CHECK(counterSetWCountWValidParentCategory->m_Name == counterSetWCountWValidParentCategoryName);
1170 CHECK(counterSetWCountWValidParentCategory->m_Uid >= 1);
1171 CHECK(counterSetWCountWValidParentCategory->m_Uid > counterSet->m_Uid);
1172 CHECK(counterSetWCountWValidParentCategory->m_Uid > counterSetWCount->m_Uid);
1173 CHECK(counterSetWCountWValidParentCategory->m_Count == 42);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001174
Sadik Armagan4c998992020-02-25 12:44:44 +00001175 // Register a counter set associated to a category with invalid name
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001176 const std::string counterSetSameCategoryName = "some_counter_set_with_invalid_parent_category";
Sadik Armagan4c998992020-02-25 12:44:44 +00001177 const std::string invalidCategoryName = "";
Keith Davis3201eea2019-10-24 17:30:41 +01001178 const CounterSet* counterSetSameCategory = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001179 CHECK_THROWS_AS(counterSetSameCategory =
Sadik Armagan4c998992020-02-25 12:44:44 +00001180 counterDirectory.RegisterCounterSet(counterSetSameCategoryName, 0, invalidCategoryName),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001181 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001182 CHECK(counterDirectory.GetCounterSetCount() == 3);
1183 CHECK(!counterSetSameCategory);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001184}
1185
Sadik Armagan1625efc2021-06-10 18:24:34 +01001186TEST_CASE("CheckCounterDirectoryRegisterCounter")
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001187{
1188 CounterDirectory counterDirectory;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001189 CHECK(counterDirectory.GetCategoryCount() == 0);
1190 CHECK(counterDirectory.GetDeviceCount() == 0);
1191 CHECK(counterDirectory.GetCounterSetCount() == 0);
1192 CHECK(counterDirectory.GetCounterCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001193
1194 // Register a counter with an invalid parent category name
1195 const Counter* noCounter = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001196 CHECK_THROWS_AS(noCounter =
Keith Davise394bd92019-12-02 15:12:19 +00001197 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1198 0,
1199 "",
1200 0,
1201 1,
1202 123.45f,
1203 "valid ",
1204 "name"),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001205 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001206 CHECK(counterDirectory.GetCounterCount() == 0);
1207 CHECK(!noCounter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001208
1209 // Register a counter with an invalid parent category name
Sadik Armagan1625efc2021-06-10 18:24:34 +01001210 CHECK_THROWS_AS(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001211 1,
1212 "invalid parent category",
1213 0,
1214 1,
1215 123.45f,
1216 "valid name",
1217 "valid description"),
1218 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001219 CHECK(counterDirectory.GetCounterCount() == 0);
1220 CHECK(!noCounter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001221
1222 // Register a counter with an invalid class
Sadik Armagan1625efc2021-06-10 18:24:34 +01001223 CHECK_THROWS_AS(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001224 2,
1225 "valid_parent_category",
1226 2,
1227 1,
1228 123.45f,
1229 "valid "
1230 "name",
1231 "valid description"),
1232 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001233 CHECK(counterDirectory.GetCounterCount() == 0);
1234 CHECK(!noCounter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001235
1236 // Register a counter with an invalid interpolation
Sadik Armagan1625efc2021-06-10 18:24:34 +01001237 CHECK_THROWS_AS(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001238 4,
1239 "valid_parent_category",
1240 0,
1241 3,
1242 123.45f,
1243 "valid "
1244 "name",
1245 "valid description"),
1246 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001247 CHECK(counterDirectory.GetCounterCount() == 0);
1248 CHECK(!noCounter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001249
1250 // Register a counter with an invalid multiplier
Sadik Armagan1625efc2021-06-10 18:24:34 +01001251 CHECK_THROWS_AS(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001252 5,
1253 "valid_parent_category",
1254 0,
1255 1,
1256 .0f,
1257 "valid "
1258 "name",
1259 "valid description"),
1260 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001261 CHECK(counterDirectory.GetCounterCount() == 0);
1262 CHECK(!noCounter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001263
1264 // Register a counter with an invalid name
Sadik Armagan1625efc2021-06-10 18:24:34 +01001265 CHECK_THROWS_AS(
Keith Davise394bd92019-12-02 15:12:19 +00001266 noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1267 6,
1268 "valid_parent_category",
1269 0,
1270 1,
1271 123.45f,
1272 "",
1273 "valid description"),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001274 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001275 CHECK(counterDirectory.GetCounterCount() == 0);
1276 CHECK(!noCounter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001277
1278 // Register a counter with an invalid name
Sadik Armagan1625efc2021-06-10 18:24:34 +01001279 CHECK_THROWS_AS(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001280 7,
1281 "valid_parent_category",
1282 0,
1283 1,
1284 123.45f,
1285 "invalid nam€",
1286 "valid description"),
1287 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001288 CHECK(counterDirectory.GetCounterCount() == 0);
1289 CHECK(!noCounter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001290
1291 // Register a counter with an invalid description
Sadik Armagan1625efc2021-06-10 18:24:34 +01001292 CHECK_THROWS_AS(noCounter =
Keith Davise394bd92019-12-02 15:12:19 +00001293 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1294 8,
1295 "valid_parent_category",
1296 0,
1297 1,
1298 123.45f,
1299 "valid name",
1300 ""),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001301 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001302 CHECK(counterDirectory.GetCounterCount() == 0);
1303 CHECK(!noCounter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001304
1305 // Register a counter with an invalid description
Sadik Armagan1625efc2021-06-10 18:24:34 +01001306 CHECK_THROWS_AS(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001307 9,
1308 "valid_parent_category",
1309 0,
1310 1,
1311 123.45f,
1312 "valid "
1313 "name",
1314 "inv@lid description"),
1315 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001316 CHECK(counterDirectory.GetCounterCount() == 0);
1317 CHECK(!noCounter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001318
1319 // Register a counter with an invalid unit2
Sadik Armagan1625efc2021-06-10 18:24:34 +01001320 CHECK_THROWS_AS(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001321 10,
1322 "valid_parent_category",
1323 0,
1324 1,
1325 123.45f,
1326 "valid name",
1327 "valid description",
1328 std::string("Mb/s2")),
1329 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001330 CHECK(counterDirectory.GetCounterCount() == 0);
1331 CHECK(!noCounter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001332
1333 // Register a counter with a non-existing parent category name
Sadik Armagan1625efc2021-06-10 18:24:34 +01001334 CHECK_THROWS_AS(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001335 11,
1336 "invalid_parent_category",
1337 0,
1338 1,
1339 123.45f,
1340 "valid name",
1341 "valid description"),
1342 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001343 CHECK(counterDirectory.GetCounterCount() == 0);
1344 CHECK(!noCounter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001345
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001346 // Try getting an unregistered counter
1347 const Counter* unregisteredCounter = counterDirectory.GetCounter(9999);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001348 CHECK(!unregisteredCounter);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001349
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001350 // Register a category for testing
1351 const std::string categoryName = "some_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001352 const Category* category = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001353 CHECK_NOTHROW(category = counterDirectory.RegisterCategory(categoryName));
1354 CHECK(counterDirectory.GetCategoryCount() == 1);
1355 CHECK(category);
1356 CHECK(category->m_Name == categoryName);
1357 CHECK(category->m_Counters.empty());
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001358
1359 // Register a counter with a valid parent category name
1360 const Counter* counter = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001361 CHECK_NOTHROW(
Keith Davise394bd92019-12-02 15:12:19 +00001362 counter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1363 12,
1364 categoryName,
1365 0,
1366 1,
1367 123.45f,
1368 "valid name",
1369 "valid description"));
Sadik Armagan1625efc2021-06-10 18:24:34 +01001370 CHECK(counterDirectory.GetCounterCount() == 1);
1371 CHECK(counter);
1372 CHECK(counter->m_MaxCounterUid == counter->m_Uid);
1373 CHECK(counter->m_Class == 0);
1374 CHECK(counter->m_Interpolation == 1);
1375 CHECK(counter->m_Multiplier == 123.45f);
1376 CHECK(counter->m_Name == "valid name");
1377 CHECK(counter->m_Description == "valid description");
1378 CHECK(counter->m_Units == "");
1379 CHECK(counter->m_DeviceUid == 0);
1380 CHECK(counter->m_CounterSetUid == 0);
1381 CHECK(category->m_Counters.size() == 1);
1382 CHECK(category->m_Counters.back() == counter->m_Uid);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001383
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001384 // Register a counter with a name of a counter already registered for the given parent category name
1385 const Counter* counterSameName = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001386 CHECK_THROWS_AS(counterSameName =
Keith Davise394bd92019-12-02 15:12:19 +00001387 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1388 13,
1389 categoryName,
1390 0,
1391 0,
1392 1.0f,
1393 "valid name",
1394 "valid description",
1395 std::string("description")),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001396 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001397 CHECK(counterDirectory.GetCounterCount() == 1);
1398 CHECK(!counterSameName);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001399
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001400 // Register a counter with a valid parent category name and units
1401 const Counter* counterWUnits = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001402 CHECK_NOTHROW(counterWUnits = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001403 14,
1404 categoryName,
1405 0,
1406 1,
1407 123.45f,
1408 "valid name 2",
1409 "valid description",
1410 std::string("Mnnsq2"))); // Units
Sadik Armagan1625efc2021-06-10 18:24:34 +01001411 CHECK(counterDirectory.GetCounterCount() == 2);
1412 CHECK(counterWUnits);
1413 CHECK(counterWUnits->m_Uid > counter->m_Uid);
1414 CHECK(counterWUnits->m_MaxCounterUid == counterWUnits->m_Uid);
1415 CHECK(counterWUnits->m_Class == 0);
1416 CHECK(counterWUnits->m_Interpolation == 1);
1417 CHECK(counterWUnits->m_Multiplier == 123.45f);
1418 CHECK(counterWUnits->m_Name == "valid name 2");
1419 CHECK(counterWUnits->m_Description == "valid description");
1420 CHECK(counterWUnits->m_Units == "Mnnsq2");
1421 CHECK(counterWUnits->m_DeviceUid == 0);
1422 CHECK(counterWUnits->m_CounterSetUid == 0);
1423 CHECK(category->m_Counters.size() == 2);
1424 CHECK(category->m_Counters.back() == counterWUnits->m_Uid);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001425
1426 // Register a counter with a valid parent category name and not associated with a device
1427 const Counter* counterWoDevice = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001428 CHECK_NOTHROW(counterWoDevice = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001429 26,
1430 categoryName,
1431 0,
1432 1,
1433 123.45f,
1434 "valid name 3",
1435 "valid description",
1436 armnn::EmptyOptional(),// Units
1437 armnn::EmptyOptional(),// Number of cores
1438 0)); // Device UID
Sadik Armagan1625efc2021-06-10 18:24:34 +01001439 CHECK(counterDirectory.GetCounterCount() == 3);
1440 CHECK(counterWoDevice);
1441 CHECK(counterWoDevice->m_Uid > counter->m_Uid);
1442 CHECK(counterWoDevice->m_MaxCounterUid == counterWoDevice->m_Uid);
1443 CHECK(counterWoDevice->m_Class == 0);
1444 CHECK(counterWoDevice->m_Interpolation == 1);
1445 CHECK(counterWoDevice->m_Multiplier == 123.45f);
1446 CHECK(counterWoDevice->m_Name == "valid name 3");
1447 CHECK(counterWoDevice->m_Description == "valid description");
1448 CHECK(counterWoDevice->m_Units == "");
1449 CHECK(counterWoDevice->m_DeviceUid == 0);
1450 CHECK(counterWoDevice->m_CounterSetUid == 0);
1451 CHECK(category->m_Counters.size() == 3);
1452 CHECK(category->m_Counters.back() == counterWoDevice->m_Uid);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001453
1454 // Register a counter with a valid parent category name and associated to an invalid device
Sadik Armagan1625efc2021-06-10 18:24:34 +01001455 CHECK_THROWS_AS(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001456 15,
1457 categoryName,
1458 0,
1459 1,
1460 123.45f,
1461 "valid name 4",
1462 "valid description",
1463 armnn::EmptyOptional(), // Units
1464 armnn::EmptyOptional(), // Number of cores
1465 100), // Device UID
1466 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001467 CHECK(counterDirectory.GetCounterCount() == 3);
1468 CHECK(!noCounter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001469
1470 // Register a device for testing
1471 const std::string deviceName = "some_device";
Keith Davis3201eea2019-10-24 17:30:41 +01001472 const Device* device = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001473 CHECK_NOTHROW(device = counterDirectory.RegisterDevice(deviceName));
1474 CHECK(counterDirectory.GetDeviceCount() == 1);
1475 CHECK(device);
1476 CHECK(device->m_Name == deviceName);
1477 CHECK(device->m_Uid >= 1);
1478 CHECK(device->m_Cores == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001479
1480 // Register a counter with a valid parent category name and associated to a device
1481 const Counter* counterWDevice = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001482 CHECK_NOTHROW(counterWDevice = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001483 16,
1484 categoryName,
1485 0,
1486 1,
1487 123.45f,
1488 "valid name 5",
1489 std::string("valid description"),
1490 armnn::EmptyOptional(), // Units
1491 armnn::EmptyOptional(), // Number of cores
1492 device->m_Uid)); // Device UID
Sadik Armagan1625efc2021-06-10 18:24:34 +01001493 CHECK(counterDirectory.GetCounterCount() == 4);
1494 CHECK(counterWDevice);
1495 CHECK(counterWDevice->m_Uid > counter->m_Uid);
1496 CHECK(counterWDevice->m_MaxCounterUid == counterWDevice->m_Uid);
1497 CHECK(counterWDevice->m_Class == 0);
1498 CHECK(counterWDevice->m_Interpolation == 1);
1499 CHECK(counterWDevice->m_Multiplier == 123.45f);
1500 CHECK(counterWDevice->m_Name == "valid name 5");
1501 CHECK(counterWDevice->m_Description == "valid description");
1502 CHECK(counterWDevice->m_Units == "");
1503 CHECK(counterWDevice->m_DeviceUid == device->m_Uid);
1504 CHECK(counterWDevice->m_CounterSetUid == 0);
1505 CHECK(category->m_Counters.size() == 4);
1506 CHECK(category->m_Counters.back() == counterWDevice->m_Uid);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001507
1508 // Register a counter with a valid parent category name and not associated with a counter set
1509 const Counter* counterWoCounterSet = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001510 CHECK_NOTHROW(counterWoCounterSet = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001511 17,
1512 categoryName,
1513 0,
1514 1,
1515 123.45f,
1516 "valid name 6",
1517 "valid description",
1518 armnn::EmptyOptional(),// Units
1519 armnn::EmptyOptional(),// No of cores
1520 armnn::EmptyOptional(),// Device UID
1521 0)); // CounterSet UID
Sadik Armagan1625efc2021-06-10 18:24:34 +01001522 CHECK(counterDirectory.GetCounterCount() == 5);
1523 CHECK(counterWoCounterSet);
1524 CHECK(counterWoCounterSet->m_Uid > counter->m_Uid);
1525 CHECK(counterWoCounterSet->m_MaxCounterUid == counterWoCounterSet->m_Uid);
1526 CHECK(counterWoCounterSet->m_Class == 0);
1527 CHECK(counterWoCounterSet->m_Interpolation == 1);
1528 CHECK(counterWoCounterSet->m_Multiplier == 123.45f);
1529 CHECK(counterWoCounterSet->m_Name == "valid name 6");
1530 CHECK(counterWoCounterSet->m_Description == "valid description");
1531 CHECK(counterWoCounterSet->m_Units == "");
1532 CHECK(counterWoCounterSet->m_DeviceUid == 0);
1533 CHECK(counterWoCounterSet->m_CounterSetUid == 0);
1534 CHECK(category->m_Counters.size() == 5);
1535 CHECK(category->m_Counters.back() == counterWoCounterSet->m_Uid);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001536
1537 // Register a counter with a valid parent category name and associated to an invalid counter set
Sadik Armagan1625efc2021-06-10 18:24:34 +01001538 CHECK_THROWS_AS(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001539 18,
1540 categoryName,
1541 0,
1542 1,
1543 123.45f,
1544 "valid ",
1545 "name 7",
1546 std::string("valid description"),
1547 armnn::EmptyOptional(), // Units
1548 armnn::EmptyOptional(), // Number of cores
1549 100), // Counter set UID
1550 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001551 CHECK(counterDirectory.GetCounterCount() == 5);
1552 CHECK(!noCounter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001553
1554 // Register a counter with a valid parent category name and with a given number of cores
1555 const Counter* counterWNumberOfCores = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001556 uint16_t numberOfCores = 15;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001557 CHECK_NOTHROW(counterWNumberOfCores = counterDirectory.RegisterCounter(
Keith Davise394bd92019-12-02 15:12:19 +00001558 armnn::profiling::BACKEND_ID, 50,
Keith Davis3201eea2019-10-24 17:30:41 +01001559 categoryName, 0, 1, 123.45f, "valid name 8", "valid description",
1560 armnn::EmptyOptional(), // Units
1561 numberOfCores, // Number of cores
1562 armnn::EmptyOptional(), // Device UID
1563 armnn::EmptyOptional())); // Counter set UID
Sadik Armagan1625efc2021-06-10 18:24:34 +01001564 CHECK(counterDirectory.GetCounterCount() == 20);
1565 CHECK(counterWNumberOfCores);
1566 CHECK(counterWNumberOfCores->m_Uid > counter->m_Uid);
1567 CHECK(counterWNumberOfCores->m_MaxCounterUid == counterWNumberOfCores->m_Uid + numberOfCores - 1);
1568 CHECK(counterWNumberOfCores->m_Class == 0);
1569 CHECK(counterWNumberOfCores->m_Interpolation == 1);
1570 CHECK(counterWNumberOfCores->m_Multiplier == 123.45f);
1571 CHECK(counterWNumberOfCores->m_Name == "valid name 8");
1572 CHECK(counterWNumberOfCores->m_Description == "valid description");
1573 CHECK(counterWNumberOfCores->m_Units == "");
1574 CHECK(counterWNumberOfCores->m_DeviceUid == 0);
1575 CHECK(counterWNumberOfCores->m_CounterSetUid == 0);
1576 CHECK(category->m_Counters.size() == 20);
Keith Davis3201eea2019-10-24 17:30:41 +01001577 for (size_t i = 0; i < numberOfCores; i++)
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001578 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01001579 CHECK(category->m_Counters[category->m_Counters.size() - numberOfCores + i] ==
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001580 counterWNumberOfCores->m_Uid + i);
1581 }
1582
1583 // Register a multi-core device for testing
1584 const std::string multiCoreDeviceName = "some_multi_core_device";
Keith Davis3201eea2019-10-24 17:30:41 +01001585 const Device* multiCoreDevice = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001586 CHECK_NOTHROW(multiCoreDevice = counterDirectory.RegisterDevice(multiCoreDeviceName, 4));
1587 CHECK(counterDirectory.GetDeviceCount() == 2);
1588 CHECK(multiCoreDevice);
1589 CHECK(multiCoreDevice->m_Name == multiCoreDeviceName);
1590 CHECK(multiCoreDevice->m_Uid >= 1);
1591 CHECK(multiCoreDevice->m_Cores == 4);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001592
1593 // Register a counter with a valid parent category name and associated to the multi-core device
1594 const Counter* counterWMultiCoreDevice = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001595 CHECK_NOTHROW(counterWMultiCoreDevice = counterDirectory.RegisterCounter(
Keith Davise394bd92019-12-02 15:12:19 +00001596 armnn::profiling::BACKEND_ID, 19, categoryName, 0, 1,
1597 123.45f, "valid name 9", "valid description",
Keith Davis3201eea2019-10-24 17:30:41 +01001598 armnn::EmptyOptional(), // Units
1599 armnn::EmptyOptional(), // Number of cores
1600 multiCoreDevice->m_Uid, // Device UID
1601 armnn::EmptyOptional())); // Counter set UID
Sadik Armagan1625efc2021-06-10 18:24:34 +01001602 CHECK(counterDirectory.GetCounterCount() == 24);
1603 CHECK(counterWMultiCoreDevice);
1604 CHECK(counterWMultiCoreDevice->m_Uid > counter->m_Uid);
1605 CHECK(counterWMultiCoreDevice->m_MaxCounterUid ==
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001606 counterWMultiCoreDevice->m_Uid + multiCoreDevice->m_Cores - 1);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001607 CHECK(counterWMultiCoreDevice->m_Class == 0);
1608 CHECK(counterWMultiCoreDevice->m_Interpolation == 1);
1609 CHECK(counterWMultiCoreDevice->m_Multiplier == 123.45f);
1610 CHECK(counterWMultiCoreDevice->m_Name == "valid name 9");
1611 CHECK(counterWMultiCoreDevice->m_Description == "valid description");
1612 CHECK(counterWMultiCoreDevice->m_Units == "");
1613 CHECK(counterWMultiCoreDevice->m_DeviceUid == multiCoreDevice->m_Uid);
1614 CHECK(counterWMultiCoreDevice->m_CounterSetUid == 0);
1615 CHECK(category->m_Counters.size() == 24);
Keith Davis3201eea2019-10-24 17:30:41 +01001616 for (size_t i = 0; i < 4; i++)
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001617 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01001618 CHECK(category->m_Counters[category->m_Counters.size() - 4 + i] == counterWMultiCoreDevice->m_Uid + i);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001619 }
1620
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001621 // Register a multi-core device associate to a parent category for testing
1622 const std::string multiCoreDeviceNameWParentCategory = "some_multi_core_device_with_parent_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001623 const Device* multiCoreDeviceWParentCategory = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001624 CHECK_NOTHROW(multiCoreDeviceWParentCategory =
Keith Davis3201eea2019-10-24 17:30:41 +01001625 counterDirectory.RegisterDevice(multiCoreDeviceNameWParentCategory, 2, categoryName));
Sadik Armagan1625efc2021-06-10 18:24:34 +01001626 CHECK(counterDirectory.GetDeviceCount() == 3);
1627 CHECK(multiCoreDeviceWParentCategory);
1628 CHECK(multiCoreDeviceWParentCategory->m_Name == multiCoreDeviceNameWParentCategory);
1629 CHECK(multiCoreDeviceWParentCategory->m_Uid >= 1);
1630 CHECK(multiCoreDeviceWParentCategory->m_Cores == 2);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001631
1632 // Register a counter with a valid parent category name and getting the number of cores of the multi-core device
1633 // associated to that category
1634 const Counter* counterWMultiCoreDeviceWParentCategory = nullptr;
Sadik Armagan4c998992020-02-25 12:44:44 +00001635 uint16_t numberOfCourse = multiCoreDeviceWParentCategory->m_Cores;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001636 CHECK_NOTHROW(counterWMultiCoreDeviceWParentCategory =
Jim Flynnbbfe6032020-07-20 16:57:44 +01001637 counterDirectory.RegisterCounter(
1638 armnn::profiling::BACKEND_ID,
1639 100,
1640 categoryName,
1641 0,
1642 1,
1643 123.45f,
1644 "valid name 10",
1645 "valid description",
1646 armnn::EmptyOptional(), // Units
1647 numberOfCourse, // Number of cores
1648 armnn::EmptyOptional(), // Device UID
1649 armnn::EmptyOptional()));// Counter set UID
Sadik Armagan1625efc2021-06-10 18:24:34 +01001650 CHECK(counterDirectory.GetCounterCount() == 26);
1651 CHECK(counterWMultiCoreDeviceWParentCategory);
1652 CHECK(counterWMultiCoreDeviceWParentCategory->m_Uid > counter->m_Uid);
1653 CHECK(counterWMultiCoreDeviceWParentCategory->m_MaxCounterUid ==
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001654 counterWMultiCoreDeviceWParentCategory->m_Uid + multiCoreDeviceWParentCategory->m_Cores - 1);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001655 CHECK(counterWMultiCoreDeviceWParentCategory->m_Class == 0);
1656 CHECK(counterWMultiCoreDeviceWParentCategory->m_Interpolation == 1);
1657 CHECK(counterWMultiCoreDeviceWParentCategory->m_Multiplier == 123.45f);
1658 CHECK(counterWMultiCoreDeviceWParentCategory->m_Name == "valid name 10");
1659 CHECK(counterWMultiCoreDeviceWParentCategory->m_Description == "valid description");
1660 CHECK(counterWMultiCoreDeviceWParentCategory->m_Units == "");
1661 CHECK(category->m_Counters.size() == 26);
Keith Davis3201eea2019-10-24 17:30:41 +01001662 for (size_t i = 0; i < 2; i++)
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001663 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01001664 CHECK(category->m_Counters[category->m_Counters.size() - 2 + i] ==
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001665 counterWMultiCoreDeviceWParentCategory->m_Uid + i);
1666 }
1667
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001668 // Register a counter set for testing
1669 const std::string counterSetName = "some_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +01001670 const CounterSet* counterSet = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001671 CHECK_NOTHROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
1672 CHECK(counterDirectory.GetCounterSetCount() == 1);
1673 CHECK(counterSet);
1674 CHECK(counterSet->m_Name == counterSetName);
1675 CHECK(counterSet->m_Uid >= 1);
1676 CHECK(counterSet->m_Count == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001677
1678 // Register a counter with a valid parent category name and associated to a counter set
1679 const Counter* counterWCounterSet = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001680 CHECK_NOTHROW(counterWCounterSet = counterDirectory.RegisterCounter(
Keith Davise394bd92019-12-02 15:12:19 +00001681 armnn::profiling::BACKEND_ID, 300,
Keith Davis3201eea2019-10-24 17:30:41 +01001682 categoryName, 0, 1, 123.45f, "valid name 11", "valid description",
1683 armnn::EmptyOptional(), // Units
1684 0, // Number of cores
1685 armnn::EmptyOptional(), // Device UID
1686 counterSet->m_Uid)); // Counter set UID
Sadik Armagan1625efc2021-06-10 18:24:34 +01001687 CHECK(counterDirectory.GetCounterCount() == 27);
1688 CHECK(counterWCounterSet);
1689 CHECK(counterWCounterSet->m_Uid > counter->m_Uid);
1690 CHECK(counterWCounterSet->m_MaxCounterUid == counterWCounterSet->m_Uid);
1691 CHECK(counterWCounterSet->m_Class == 0);
1692 CHECK(counterWCounterSet->m_Interpolation == 1);
1693 CHECK(counterWCounterSet->m_Multiplier == 123.45f);
1694 CHECK(counterWCounterSet->m_Name == "valid name 11");
1695 CHECK(counterWCounterSet->m_Description == "valid description");
1696 CHECK(counterWCounterSet->m_Units == "");
1697 CHECK(counterWCounterSet->m_DeviceUid == 0);
1698 CHECK(counterWCounterSet->m_CounterSetUid == counterSet->m_Uid);
1699 CHECK(category->m_Counters.size() == 27);
1700 CHECK(category->m_Counters.back() == counterWCounterSet->m_Uid);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001701
1702 // Register a counter with a valid parent category name and associated to a device and a counter set
1703 const Counter* counterWDeviceWCounterSet = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001704 CHECK_NOTHROW(counterWDeviceWCounterSet = counterDirectory.RegisterCounter(
Keith Davise394bd92019-12-02 15:12:19 +00001705 armnn::profiling::BACKEND_ID, 23,
Keith Davis3201eea2019-10-24 17:30:41 +01001706 categoryName, 0, 1, 123.45f, "valid name 12", "valid description",
1707 armnn::EmptyOptional(), // Units
1708 1, // Number of cores
1709 device->m_Uid, // Device UID
1710 counterSet->m_Uid)); // Counter set UID
Sadik Armagan1625efc2021-06-10 18:24:34 +01001711 CHECK(counterDirectory.GetCounterCount() == 28);
1712 CHECK(counterWDeviceWCounterSet);
1713 CHECK(counterWDeviceWCounterSet->m_Uid > counter->m_Uid);
1714 CHECK(counterWDeviceWCounterSet->m_MaxCounterUid == counterWDeviceWCounterSet->m_Uid);
1715 CHECK(counterWDeviceWCounterSet->m_Class == 0);
1716 CHECK(counterWDeviceWCounterSet->m_Interpolation == 1);
1717 CHECK(counterWDeviceWCounterSet->m_Multiplier == 123.45f);
1718 CHECK(counterWDeviceWCounterSet->m_Name == "valid name 12");
1719 CHECK(counterWDeviceWCounterSet->m_Description == "valid description");
1720 CHECK(counterWDeviceWCounterSet->m_Units == "");
1721 CHECK(counterWDeviceWCounterSet->m_DeviceUid == device->m_Uid);
1722 CHECK(counterWDeviceWCounterSet->m_CounterSetUid == counterSet->m_Uid);
1723 CHECK(category->m_Counters.size() == 28);
1724 CHECK(category->m_Counters.back() == counterWDeviceWCounterSet->m_Uid);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001725
1726 // Register another category for testing
1727 const std::string anotherCategoryName = "some_other_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001728 const Category* anotherCategory = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001729 CHECK_NOTHROW(anotherCategory = counterDirectory.RegisterCategory(anotherCategoryName));
1730 CHECK(counterDirectory.GetCategoryCount() == 2);
1731 CHECK(anotherCategory);
1732 CHECK(anotherCategory != category);
1733 CHECK(anotherCategory->m_Name == anotherCategoryName);
1734 CHECK(anotherCategory->m_Counters.empty());
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001735
1736 // Register a counter to the other category
1737 const Counter* anotherCounter = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001738 CHECK_NOTHROW(anotherCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID, 24,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001739 anotherCategoryName, 1, 0, .00043f,
1740 "valid name", "valid description",
1741 armnn::EmptyOptional(), // Units
1742 armnn::EmptyOptional(), // Number of cores
1743 device->m_Uid, // Device UID
1744 counterSet->m_Uid)); // Counter set UID
Sadik Armagan1625efc2021-06-10 18:24:34 +01001745 CHECK(counterDirectory.GetCounterCount() == 29);
1746 CHECK(anotherCounter);
1747 CHECK(anotherCounter->m_MaxCounterUid == anotherCounter->m_Uid);
1748 CHECK(anotherCounter->m_Class == 1);
1749 CHECK(anotherCounter->m_Interpolation == 0);
1750 CHECK(anotherCounter->m_Multiplier == .00043f);
1751 CHECK(anotherCounter->m_Name == "valid name");
1752 CHECK(anotherCounter->m_Description == "valid description");
1753 CHECK(anotherCounter->m_Units == "");
1754 CHECK(anotherCounter->m_DeviceUid == device->m_Uid);
1755 CHECK(anotherCounter->m_CounterSetUid == counterSet->m_Uid);
1756 CHECK(anotherCategory->m_Counters.size() == 1);
1757 CHECK(anotherCategory->m_Counters.back() == anotherCounter->m_Uid);
Matteo Martincighab173e92019-09-05 12:02:04 +01001758}
1759
Sadik Armagan1625efc2021-06-10 18:24:34 +01001760TEST_CASE("CounterSelectionCommandHandlerParseData")
Ferran Balaguer1b941722019-08-28 16:57:18 +01001761{
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01001762 ProfilingStateMachine profilingStateMachine;
1763
Ferran Balaguer1b941722019-08-28 16:57:18 +01001764 class TestCaptureThread : public IPeriodicCounterCapture
1765 {
Keith Davis3201eea2019-10-24 17:30:41 +01001766 void Start() override
1767 {}
1768 void Stop() override
1769 {}
Ferran Balaguer1b941722019-08-28 16:57:18 +01001770 };
1771
Matteo Martincighe8485382019-10-10 14:08:21 +01001772 class TestReadCounterValues : public IReadCounterValues
1773 {
Keith Davis3201eea2019-10-24 17:30:41 +01001774 bool IsCounterRegistered(uint16_t counterUid) const override
1775 {
Jim Flynn9265a882022-03-10 23:35:26 +00001776 arm::pipe::IgnoreUnused(counterUid);
Keith Davis3201eea2019-10-24 17:30:41 +01001777 return true;
1778 }
Jim Flynn34430252022-03-04 15:03:58 +00001779 bool IsCounterRegistered(const std::string& counterName) const override
1780 {
Jim Flynn9265a882022-03-10 23:35:26 +00001781 arm::pipe::IgnoreUnused(counterName);
Jim Flynn34430252022-03-04 15:03:58 +00001782 return true;
1783 }
Keith Davis3201eea2019-10-24 17:30:41 +01001784 uint16_t GetCounterCount() const override
1785 {
1786 return 0;
1787 }
Finn Williamsf3fcf322020-05-11 14:38:02 +01001788 uint32_t GetAbsoluteCounterValue(uint16_t counterUid) const override
1789 {
Jim Flynn9265a882022-03-10 23:35:26 +00001790 arm::pipe::IgnoreUnused(counterUid);
Finn Williamsf3fcf322020-05-11 14:38:02 +01001791 return 0;
1792 }
1793 uint32_t GetDeltaCounterValue(uint16_t counterUid) override
Keith Davis3201eea2019-10-24 17:30:41 +01001794 {
Jim Flynn9265a882022-03-10 23:35:26 +00001795 arm::pipe::IgnoreUnused(counterUid);
Keith Davis3201eea2019-10-24 17:30:41 +01001796 return 0;
1797 }
Matteo Martincighe8485382019-10-10 14:08:21 +01001798 };
Jim Flynn397043f2019-10-17 17:37:10 +01001799 const uint32_t familyId = 0;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001800 const uint32_t packetId = 0x40000;
1801
1802 uint32_t version = 1;
Cathal Corbett6f073722022-03-04 12:11:09 +00001803 const std::unordered_map<std::string,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001804 std::shared_ptr<IBackendProfilingContext>> backendProfilingContext;
Finn Williams032bc742020-02-12 11:02:34 +00001805 CounterIdMap counterIdMap;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001806 Holder holder;
1807 TestCaptureThread captureThread;
Matteo Martincighe8485382019-10-10 14:08:21 +01001808 TestReadCounterValues readCounterValues;
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001809 MockBufferManager mockBuffer(512);
Sadik Armagan3896b472020-02-10 12:24:15 +00001810 SendCounterPacket sendCounterPacket(mockBuffer);
1811 SendThread sendThread(profilingStateMachine, mockBuffer, sendCounterPacket);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001812
Jim Flynn75c14f42022-03-10 22:05:42 +00001813 uint32_t sizeOfUint32 = arm::pipe::numeric_cast<uint32_t>(sizeof(uint32_t));
1814 uint32_t sizeOfUint16 = arm::pipe::numeric_cast<uint32_t>(sizeof(uint16_t));
Ferran Balaguer1b941722019-08-28 16:57:18 +01001815
1816 // Data with period and counters
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00001817 uint32_t period1 = arm::pipe::LOWEST_CAPTURE_PERIOD;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001818 uint32_t dataLength1 = 8;
Keith Davis3201eea2019-10-24 17:30:41 +01001819 uint32_t offset = 0;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001820
Matteo Martincigh67ef2a52019-10-10 13:29:02 +01001821 std::unique_ptr<unsigned char[]> uniqueData1 = std::make_unique<unsigned char[]>(dataLength1);
Keith Davis3201eea2019-10-24 17:30:41 +01001822 unsigned char* data1 = reinterpret_cast<unsigned char*>(uniqueData1.get());
FinnWilliamsArma0c78712019-09-16 12:06:47 +01001823
Ferran Balaguer1b941722019-08-28 16:57:18 +01001824 WriteUint32(data1, offset, period1);
1825 offset += sizeOfUint32;
1826 WriteUint16(data1, offset, 4000);
1827 offset += sizeOfUint16;
1828 WriteUint16(data1, offset, 5000);
1829
Jim Flynnbbfe6032020-07-20 16:57:44 +01001830 arm::pipe::Packet packetA(packetId, dataLength1, uniqueData1);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001831
Finn Williams032bc742020-02-12 11:02:34 +00001832 PeriodicCounterSelectionCommandHandler commandHandler(familyId, packetId, version, backendProfilingContext,
1833 counterIdMap, holder, 10000u, captureThread,
Keith Davis3201eea2019-10-24 17:30:41 +01001834 readCounterValues, sendCounterPacket, profilingStateMachine);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001835
Matteo Martincighe8485382019-10-10 14:08:21 +01001836 profilingStateMachine.TransitionToState(ProfilingState::Uninitialised);
Jim Flynnf9db3ef2022-03-08 21:23:44 +00001837 CHECK_THROWS_AS(commandHandler(packetA), arm::pipe::ProfilingException);
Matteo Martincighe8485382019-10-10 14:08:21 +01001838 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
Jim Flynnf9db3ef2022-03-08 21:23:44 +00001839 CHECK_THROWS_AS(commandHandler(packetA), arm::pipe::ProfilingException);
Matteo Martincighe8485382019-10-10 14:08:21 +01001840 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
Jim Flynnf9db3ef2022-03-08 21:23:44 +00001841 CHECK_THROWS_AS(commandHandler(packetA), arm::pipe::ProfilingException);
Matteo Martincighe8485382019-10-10 14:08:21 +01001842 profilingStateMachine.TransitionToState(ProfilingState::Active);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001843 CHECK_NOTHROW(commandHandler(packetA));
Matteo Martincighe8485382019-10-10 14:08:21 +01001844
1845 const std::vector<uint16_t> counterIdsA = holder.GetCaptureData().GetCounterIds();
Ferran Balaguer1b941722019-08-28 16:57:18 +01001846
Sadik Armagan1625efc2021-06-10 18:24:34 +01001847 CHECK(holder.GetCaptureData().GetCapturePeriod() == period1);
1848 CHECK(counterIdsA.size() == 2);
1849 CHECK(counterIdsA[0] == 4000);
1850 CHECK(counterIdsA[1] == 5000);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001851
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001852 auto readBuffer = mockBuffer.GetReadableBuffer();
Ferran Balaguer1b941722019-08-28 16:57:18 +01001853
1854 offset = 0;
1855
1856 uint32_t headerWord0 = ReadUint32(readBuffer, offset);
1857 offset += sizeOfUint32;
1858 uint32_t headerWord1 = ReadUint32(readBuffer, offset);
1859 offset += sizeOfUint32;
1860 uint32_t period = ReadUint32(readBuffer, offset);
1861
Sadik Armagan1625efc2021-06-10 18:24:34 +01001862 CHECK(((headerWord0 >> 26) & 0x3F) == 0); // packet family
1863 CHECK(((headerWord0 >> 16) & 0x3FF) == 4); // packet id
1864 CHECK(headerWord1 == 8); // data length
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00001865 CHECK(period == arm::pipe::LOWEST_CAPTURE_PERIOD); // capture period
Ferran Balaguer1b941722019-08-28 16:57:18 +01001866
1867 uint16_t counterId = 0;
1868 offset += sizeOfUint32;
1869 counterId = ReadUint16(readBuffer, offset);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001870 CHECK(counterId == 4000);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001871 offset += sizeOfUint16;
1872 counterId = ReadUint16(readBuffer, offset);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001873 CHECK(counterId == 5000);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001874
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001875 mockBuffer.MarkRead(readBuffer);
1876
Ferran Balaguer1b941722019-08-28 16:57:18 +01001877 // Data with period only
Colm Donelan02705242019-11-14 14:19:07 +00001878 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 +01001879 uint32_t dataLength2 = 4;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001880
Matteo Martincigh67ef2a52019-10-10 13:29:02 +01001881 std::unique_ptr<unsigned char[]> uniqueData2 = std::make_unique<unsigned char[]>(dataLength2);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001882
FinnWilliamsArma0c78712019-09-16 12:06:47 +01001883 WriteUint32(reinterpret_cast<unsigned char*>(uniqueData2.get()), 0, period2);
1884
Jim Flynnbbfe6032020-07-20 16:57:44 +01001885 arm::pipe::Packet packetB(packetId, dataLength2, uniqueData2);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001886
1887 commandHandler(packetB);
1888
Matteo Martincighe8485382019-10-10 14:08:21 +01001889 const std::vector<uint16_t> counterIdsB = holder.GetCaptureData().GetCounterIds();
Ferran Balaguer1b941722019-08-28 16:57:18 +01001890
Colm Donelan02705242019-11-14 14:19:07 +00001891 // Value should have been pulled up from 9000 to LOWEST_CAPTURE_PERIOD.
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00001892 CHECK(holder.GetCaptureData().GetCapturePeriod() == arm::pipe::LOWEST_CAPTURE_PERIOD);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001893 CHECK(counterIdsB.size() == 0);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001894
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001895 readBuffer = mockBuffer.GetReadableBuffer();
Ferran Balaguer1b941722019-08-28 16:57:18 +01001896
1897 offset = 0;
1898
1899 headerWord0 = ReadUint32(readBuffer, offset);
1900 offset += sizeOfUint32;
1901 headerWord1 = ReadUint32(readBuffer, offset);
1902 offset += sizeOfUint32;
1903 period = ReadUint32(readBuffer, offset);
1904
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00001905 CHECK(((headerWord0 >> 26) & 0x3F) == 0); // packet family
1906 CHECK(((headerWord0 >> 16) & 0x3FF) == 4); // packet id
1907 CHECK(headerWord1 == 4); // data length
1908 CHECK(period == arm::pipe::LOWEST_CAPTURE_PERIOD); // capture period
Ferran Balaguer1b941722019-08-28 16:57:18 +01001909}
1910
Sadik Armagan1625efc2021-06-10 18:24:34 +01001911TEST_CASE("CheckTimelineActivationAndDeactivation")
Keith Davis33ed2212020-03-30 10:43:41 +01001912{
1913 class TestReportStructure : public IReportStructure
1914 {
1915 public:
Cathal Corbett91343f32022-03-11 13:05:44 +00001916 virtual void ReportStructure(arm::pipe::IProfilingService& /*profilingService*/) override
Keith Davis33ed2212020-03-30 10:43:41 +01001917 {
1918 m_ReportStructureCalled = true;
1919 }
1920
1921 bool m_ReportStructureCalled = false;
1922 };
1923
1924 class TestNotifyBackends : public INotifyBackends
1925 {
1926 public:
1927 TestNotifyBackends() : m_timelineReporting(false) {}
1928 virtual void NotifyBackendsForTimelineReporting() override
1929 {
1930 m_TestNotifyBackendsCalled = m_timelineReporting.load();
1931 }
1932
1933 bool m_TestNotifyBackendsCalled = false;
1934 std::atomic<bool> m_timelineReporting;
1935 };
1936
Jim Flynnbbfe6032020-07-20 16:57:44 +01001937 arm::pipe::PacketVersionResolver packetVersionResolver;
Keith Davis33ed2212020-03-30 10:43:41 +01001938
1939 BufferManager bufferManager(512);
1940 SendTimelinePacket sendTimelinePacket(bufferManager);
1941 ProfilingStateMachine stateMachine;
1942 TestReportStructure testReportStructure;
1943 TestNotifyBackends testNotifyBackends;
1944
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001945 ActivateTimelineReportingCommandHandler activateTimelineReportingCommandHandler(0,
Keith Davis33ed2212020-03-30 10:43:41 +01001946 6,
1947 packetVersionResolver.ResolvePacketVersion(0, 6)
1948 .GetEncodedValue(),
1949 sendTimelinePacket,
1950 stateMachine,
1951 testReportStructure,
1952 testNotifyBackends.m_timelineReporting,
1953 testNotifyBackends);
1954
1955 // Write an "ActivateTimelineReporting" packet into the mock profiling connection, to simulate an input from an
1956 // external profiling service
1957 const uint32_t packetFamily1 = 0;
1958 const uint32_t packetId1 = 6;
1959 uint32_t packetHeader1 = ConstructHeader(packetFamily1, packetId1);
1960
1961 // Create the ActivateTimelineReportingPacket
Jim Flynnbbfe6032020-07-20 16:57:44 +01001962 arm::pipe::Packet ActivateTimelineReportingPacket(packetHeader1); // Length == 0
Keith Davis33ed2212020-03-30 10:43:41 +01001963
Sadik Armagan1625efc2021-06-10 18:24:34 +01001964 CHECK_THROWS_AS(
Jim Flynnf9db3ef2022-03-08 21:23:44 +00001965 activateTimelineReportingCommandHandler.operator()(ActivateTimelineReportingPacket),
1966 arm::pipe::ProfilingException);
Keith Davis33ed2212020-03-30 10:43:41 +01001967
1968 stateMachine.TransitionToState(ProfilingState::NotConnected);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001969 CHECK_THROWS_AS(
Jim Flynnf9db3ef2022-03-08 21:23:44 +00001970 activateTimelineReportingCommandHandler.operator()(ActivateTimelineReportingPacket),
1971 arm::pipe::ProfilingException);
Keith Davis33ed2212020-03-30 10:43:41 +01001972
1973 stateMachine.TransitionToState(ProfilingState::WaitingForAck);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001974 CHECK_THROWS_AS(
Jim Flynnf9db3ef2022-03-08 21:23:44 +00001975 activateTimelineReportingCommandHandler.operator()(ActivateTimelineReportingPacket),
1976 arm::pipe::ProfilingException);
Keith Davis33ed2212020-03-30 10:43:41 +01001977
1978 stateMachine.TransitionToState(ProfilingState::Active);
1979 activateTimelineReportingCommandHandler.operator()(ActivateTimelineReportingPacket);
1980
Sadik Armagan1625efc2021-06-10 18:24:34 +01001981 CHECK(testReportStructure.m_ReportStructureCalled);
1982 CHECK(testNotifyBackends.m_TestNotifyBackendsCalled);
1983 CHECK(testNotifyBackends.m_timelineReporting.load());
Keith Davis33ed2212020-03-30 10:43:41 +01001984
1985 DeactivateTimelineReportingCommandHandler deactivateTimelineReportingCommandHandler(0,
1986 7,
1987 packetVersionResolver.ResolvePacketVersion(0, 7).GetEncodedValue(),
1988 testNotifyBackends.m_timelineReporting,
1989 stateMachine,
1990 testNotifyBackends);
1991
1992 const uint32_t packetFamily2 = 0;
1993 const uint32_t packetId2 = 7;
1994 uint32_t packetHeader2 = ConstructHeader(packetFamily2, packetId2);
1995
1996 // Create the DeactivateTimelineReportingPacket
Jim Flynnbbfe6032020-07-20 16:57:44 +01001997 arm::pipe::Packet deactivateTimelineReportingPacket(packetHeader2); // Length == 0
Keith Davis33ed2212020-03-30 10:43:41 +01001998
1999 stateMachine.Reset();
Sadik Armagan1625efc2021-06-10 18:24:34 +01002000 CHECK_THROWS_AS(
Jim Flynnf9db3ef2022-03-08 21:23:44 +00002001 deactivateTimelineReportingCommandHandler.operator()(deactivateTimelineReportingPacket),
2002 arm::pipe::ProfilingException);
Keith Davis33ed2212020-03-30 10:43:41 +01002003
2004 stateMachine.TransitionToState(ProfilingState::NotConnected);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002005 CHECK_THROWS_AS(
Jim Flynnf9db3ef2022-03-08 21:23:44 +00002006 deactivateTimelineReportingCommandHandler.operator()(deactivateTimelineReportingPacket),
2007 arm::pipe::ProfilingException);
Keith Davis33ed2212020-03-30 10:43:41 +01002008
2009 stateMachine.TransitionToState(ProfilingState::WaitingForAck);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002010 CHECK_THROWS_AS(
Jim Flynnf9db3ef2022-03-08 21:23:44 +00002011 deactivateTimelineReportingCommandHandler.operator()(deactivateTimelineReportingPacket),
2012 arm::pipe::ProfilingException);
Keith Davis33ed2212020-03-30 10:43:41 +01002013
2014 stateMachine.TransitionToState(ProfilingState::Active);
2015 deactivateTimelineReportingCommandHandler.operator()(deactivateTimelineReportingPacket);
2016
Sadik Armagan1625efc2021-06-10 18:24:34 +01002017 CHECK(!testNotifyBackends.m_TestNotifyBackendsCalled);
2018 CHECK(!testNotifyBackends.m_timelineReporting.load());
Keith Davis33ed2212020-03-30 10:43:41 +01002019}
2020
Sadik Armagan1625efc2021-06-10 18:24:34 +01002021TEST_CASE("CheckProfilingServiceNotActive")
Keith Davis33ed2212020-03-30 10:43:41 +01002022{
2023 using namespace armnn;
Keith Davis33ed2212020-03-30 10:43:41 +01002024
2025 // Create runtime in which the test will run
2026 armnn::IRuntime::CreationOptions options;
2027 options.m_ProfilingOptions.m_EnableProfiling = true;
2028
Kevin Mayd92a6e42021-02-04 10:27:41 +00002029 armnn::RuntimeImpl runtime(options);
Jim Flynn34430252022-03-04 15:03:58 +00002030 armnn::ArmNNProfilingServiceInitialiser initialiser;
2031 ProfilingServiceRuntimeHelper profilingServiceHelper(
2032 arm::pipe::MAX_ARMNN_COUNTER, initialiser, GetProfilingService(&runtime));
Keith Davis33ed2212020-03-30 10:43:41 +01002033 profilingServiceHelper.ForceTransitionToState(ProfilingState::NotConnected);
2034 profilingServiceHelper.ForceTransitionToState(ProfilingState::WaitingForAck);
2035 profilingServiceHelper.ForceTransitionToState(ProfilingState::Active);
2036
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00002037 BufferManager& bufferManager = profilingServiceHelper.GetProfilingBufferManager();
Keith Davis33ed2212020-03-30 10:43:41 +01002038 auto readableBuffer = bufferManager.GetReadableBuffer();
2039
2040 // Profiling is enabled, the post-optimisation structure should be created
Sadik Armagan1625efc2021-06-10 18:24:34 +01002041 CHECK(readableBuffer == nullptr);
Keith Davis33ed2212020-03-30 10:43:41 +01002042}
2043
Sadik Armagan1625efc2021-06-10 18:24:34 +01002044TEST_CASE("CheckConnectionAcknowledged")
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002045{
Keith Davis3201eea2019-10-24 17:30:41 +01002046 const uint32_t packetFamilyId = 0;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002047 const uint32_t connectionPacketId = 0x10000;
Keith Davis3201eea2019-10-24 17:30:41 +01002048 const uint32_t version = 1;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002049
Jim Flynn75c14f42022-03-10 22:05:42 +00002050 uint32_t sizeOfUint32 = arm::pipe::numeric_cast<uint32_t>(sizeof(uint32_t));
2051 uint32_t sizeOfUint16 = arm::pipe::numeric_cast<uint32_t>(sizeof(uint16_t));
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002052
2053 // Data with period and counters
Keith Davis3201eea2019-10-24 17:30:41 +01002054 uint32_t period1 = 10;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002055 uint32_t dataLength1 = 8;
Keith Davis3201eea2019-10-24 17:30:41 +01002056 uint32_t offset = 0;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002057
Matteo Martincigh67ef2a52019-10-10 13:29:02 +01002058 std::unique_ptr<unsigned char[]> uniqueData1 = std::make_unique<unsigned char[]>(dataLength1);
Keith Davis3201eea2019-10-24 17:30:41 +01002059 unsigned char* data1 = reinterpret_cast<unsigned char*>(uniqueData1.get());
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002060
2061 WriteUint32(data1, offset, period1);
2062 offset += sizeOfUint32;
2063 WriteUint16(data1, offset, 4000);
2064 offset += sizeOfUint16;
2065 WriteUint16(data1, offset, 5000);
2066
Jim Flynnbbfe6032020-07-20 16:57:44 +01002067 arm::pipe::Packet packetA(connectionPacketId, dataLength1, uniqueData1);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002068
2069 ProfilingStateMachine profilingState(ProfilingState::Uninitialised);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002070 CHECK(profilingState.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002071 CounterDirectory counterDirectory;
2072 MockBufferManager mockBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002073 SendCounterPacket sendCounterPacket(mockBuffer);
2074 SendThread sendThread(profilingState, mockBuffer, sendCounterPacket);
Matteo Martincighcdfb9412019-11-08 11:23:06 +00002075 SendTimelinePacket sendTimelinePacket(mockBuffer);
Jim Flynn6398a982020-05-27 17:05:21 +01002076 MockProfilingServiceStatus mockProfilingServiceStatus;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002077
Jim Flynn6398a982020-05-27 17:05:21 +01002078 ConnectionAcknowledgedCommandHandler commandHandler(packetFamilyId,
2079 connectionPacketId,
2080 version,
2081 counterDirectory,
2082 sendCounterPacket,
2083 sendTimelinePacket,
2084 profilingState,
2085 mockProfilingServiceStatus);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002086
2087 // command handler received packet on ProfilingState::Uninitialised
Jim Flynnf9db3ef2022-03-08 21:23:44 +00002088 CHECK_THROWS_AS(commandHandler(packetA), arm::pipe::ProfilingException);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002089
2090 profilingState.TransitionToState(ProfilingState::NotConnected);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002091 CHECK(profilingState.GetCurrentState() == ProfilingState::NotConnected);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002092 // command handler received packet on ProfilingState::NotConnected
Jim Flynnf9db3ef2022-03-08 21:23:44 +00002093 CHECK_THROWS_AS(commandHandler(packetA), arm::pipe::ProfilingException);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002094
2095 profilingState.TransitionToState(ProfilingState::WaitingForAck);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002096 CHECK(profilingState.GetCurrentState() == ProfilingState::WaitingForAck);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002097 // command handler received packet on ProfilingState::WaitingForAck
Sadik Armagan1625efc2021-06-10 18:24:34 +01002098 CHECK_NOTHROW(commandHandler(packetA));
2099 CHECK(profilingState.GetCurrentState() == ProfilingState::Active);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002100
2101 // command handler received packet on ProfilingState::Active
Sadik Armagan1625efc2021-06-10 18:24:34 +01002102 CHECK_NOTHROW(commandHandler(packetA));
2103 CHECK(profilingState.GetCurrentState() == ProfilingState::Active);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002104
2105 // command handler received different packet
2106 const uint32_t differentPacketId = 0x40000;
Jim Flynnbbfe6032020-07-20 16:57:44 +01002107 arm::pipe::Packet packetB(differentPacketId, dataLength1, uniqueData1);
Matteo Martincighd0613b52019-10-09 16:47:04 +01002108 profilingState.TransitionToState(ProfilingState::NotConnected);
2109 profilingState.TransitionToState(ProfilingState::WaitingForAck);
Jim Flynn6398a982020-05-27 17:05:21 +01002110 ConnectionAcknowledgedCommandHandler differentCommandHandler(packetFamilyId,
2111 differentPacketId,
2112 version,
2113 counterDirectory,
2114 sendCounterPacket,
2115 sendTimelinePacket,
2116 profilingState,
2117 mockProfilingServiceStatus);
Jim Flynnf9db3ef2022-03-08 21:23:44 +00002118 CHECK_THROWS_AS(differentCommandHandler(packetB), arm::pipe::ProfilingException);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002119}
2120
Sadik Armagan1625efc2021-06-10 18:24:34 +01002121TEST_CASE("CheckSocketConnectionException")
Teresa Charlin9bab4962019-09-06 12:28:35 +01002122{
Sadik Armagana97a0be2020-03-03 10:44:56 +00002123 // Check that creating a SocketProfilingConnection armnnProfiling in an exception as the Gator UDS doesn't exist.
Sadik Armagan1625efc2021-06-10 18:24:34 +01002124 CHECK_THROWS_AS(new SocketProfilingConnection(), arm::pipe::SocketConnectionException);
Sadik Armagana97a0be2020-03-03 10:44:56 +00002125}
2126
Sadik Armagan1625efc2021-06-10 18:24:34 +01002127TEST_CASE("CheckSocketConnectionException2")
Sadik Armagana97a0be2020-03-03 10:44:56 +00002128{
2129 try
2130 {
2131 new SocketProfilingConnection();
2132 }
Jim Flynnbbfe6032020-07-20 16:57:44 +01002133 catch (const arm::pipe::SocketConnectionException& ex)
Sadik Armagana97a0be2020-03-03 10:44:56 +00002134 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01002135 CHECK(ex.GetSocketFd() == 0);
2136 CHECK(ex.GetErrorNo() == ECONNREFUSED);
2137 CHECK(ex.what()
Sadik Armagana97a0be2020-03-03 10:44:56 +00002138 == std::string("SocketProfilingConnection: Cannot connect to stream socket: Connection refused"));
2139 }
Teresa Charlin9bab4962019-09-06 12:28:35 +01002140}
2141
Sadik Armagan1625efc2021-06-10 18:24:34 +01002142TEST_CASE("SwTraceIsValidCharTest")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002143{
2144 // Only ASCII 7-bit encoding supported
2145 for (unsigned char c = 0; c < 128; c++)
2146 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01002147 CHECK(arm::pipe::SwTraceCharPolicy::IsValidChar(c));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002148 }
2149
2150 // Not ASCII
2151 for (unsigned char c = 255; c >= 128; c++)
2152 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01002153 CHECK(!arm::pipe::SwTraceCharPolicy::IsValidChar(c));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002154 }
2155}
2156
Sadik Armagan1625efc2021-06-10 18:24:34 +01002157TEST_CASE("SwTraceIsValidNameCharTest")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002158{
2159 // Only alpha-numeric and underscore ASCII 7-bit encoding supported
2160 const unsigned char validChars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
2161 for (unsigned char i = 0; i < sizeof(validChars) / sizeof(validChars[0]) - 1; i++)
2162 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01002163 CHECK(arm::pipe::SwTraceNameCharPolicy::IsValidChar(validChars[i]));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002164 }
2165
2166 // Non alpha-numeric chars
2167 for (unsigned char c = 0; c < 48; c++)
2168 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01002169 CHECK(!arm::pipe::SwTraceNameCharPolicy::IsValidChar(c));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002170 }
2171 for (unsigned char c = 58; c < 65; c++)
2172 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01002173 CHECK(!arm::pipe::SwTraceNameCharPolicy::IsValidChar(c));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002174 }
2175 for (unsigned char c = 91; c < 95; c++)
2176 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01002177 CHECK(!arm::pipe::SwTraceNameCharPolicy::IsValidChar(c));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002178 }
2179 for (unsigned char c = 96; c < 97; c++)
2180 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01002181 CHECK(!arm::pipe::SwTraceNameCharPolicy::IsValidChar(c));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002182 }
2183 for (unsigned char c = 123; c < 128; c++)
2184 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01002185 CHECK(!arm::pipe::SwTraceNameCharPolicy::IsValidChar(c));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002186 }
2187
2188 // Not ASCII
2189 for (unsigned char c = 255; c >= 128; c++)
2190 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01002191 CHECK(!arm::pipe::SwTraceNameCharPolicy::IsValidChar(c));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002192 }
2193}
2194
Sadik Armagan1625efc2021-06-10 18:24:34 +01002195TEST_CASE("IsValidSwTraceStringTest")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002196{
2197 // Valid SWTrace strings
Sadik Armagan1625efc2021-06-10 18:24:34 +01002198 CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>(""));
2199 CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>("_"));
2200 CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>("0123"));
2201 CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>("valid_string"));
2202 CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>("VALID_string_456"));
2203 CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>(" "));
2204 CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>("valid string"));
2205 CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>("!$%"));
2206 CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>("valid|\\~string#123"));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002207
2208 // Invalid SWTrace strings
Sadik Armagan1625efc2021-06-10 18:24:34 +01002209 CHECK(!arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>("€£"));
2210 CHECK(!arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>("invalid‡string"));
2211 CHECK(!arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>("12Ž34"));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002212}
2213
Sadik Armagan1625efc2021-06-10 18:24:34 +01002214TEST_CASE("IsValidSwTraceNameStringTest")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002215{
2216 // Valid SWTrace name strings
Sadik Armagan1625efc2021-06-10 18:24:34 +01002217 CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>(""));
2218 CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>("_"));
2219 CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>("0123"));
2220 CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>("valid_string"));
2221 CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>("VALID_string_456"));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002222
2223 // Invalid SWTrace name strings
Sadik Armagan1625efc2021-06-10 18:24:34 +01002224 CHECK(!arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>(" "));
2225 CHECK(!arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>("invalid string"));
2226 CHECK(!arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>("!$%"));
2227 CHECK(!arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>("invalid|\\~string#123"));
2228 CHECK(!arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>("€£"));
2229 CHECK(!arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>("invalid‡string"));
2230 CHECK(!arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>("12Ž34"));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002231}
2232
2233template <typename SwTracePolicy>
2234void StringToSwTraceStringTestHelper(const std::string& testString, std::vector<uint32_t> buffer, size_t expectedSize)
2235{
2236 // Convert the test string to a SWTrace string
Sadik Armagan1625efc2021-06-10 18:24:34 +01002237 CHECK(arm::pipe::StringToSwTraceString<SwTracePolicy>(testString, buffer));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002238
2239 // The buffer must contain at least the length of the string
Sadik Armagan1625efc2021-06-10 18:24:34 +01002240 CHECK(!buffer.empty());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002241
2242 // The buffer must be of the expected size (in words)
Sadik Armagan1625efc2021-06-10 18:24:34 +01002243 CHECK(buffer.size() == expectedSize);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002244
2245 // The first word of the byte must be the length of the string including the null-terminator
Sadik Armagan1625efc2021-06-10 18:24:34 +01002246 CHECK(buffer[0] == testString.size() + 1);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002247
2248 // The contents of the buffer must match the test string
Sadik Armagan1625efc2021-06-10 18:24:34 +01002249 CHECK(std::memcmp(testString.data(), buffer.data() + 1, testString.size()) == 0);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002250
2251 // The buffer must include the null-terminator at the end of the string
2252 size_t nullTerminatorIndex = sizeof(uint32_t) + testString.size();
Sadik Armagan1625efc2021-06-10 18:24:34 +01002253 CHECK(reinterpret_cast<unsigned char*>(buffer.data())[nullTerminatorIndex] == '\0');
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002254}
2255
Sadik Armagan1625efc2021-06-10 18:24:34 +01002256TEST_CASE("StringToSwTraceStringTest")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002257{
2258 std::vector<uint32_t> buffer;
2259
2260 // Valid SWTrace strings (expected size in words)
Jim Flynnbbfe6032020-07-20 16:57:44 +01002261 StringToSwTraceStringTestHelper<arm::pipe::SwTraceCharPolicy>("", buffer, 2);
2262 StringToSwTraceStringTestHelper<arm::pipe::SwTraceCharPolicy>("_", buffer, 2);
2263 StringToSwTraceStringTestHelper<arm::pipe::SwTraceCharPolicy>("0123", buffer, 3);
2264 StringToSwTraceStringTestHelper<arm::pipe::SwTraceCharPolicy>("valid_string", buffer, 5);
2265 StringToSwTraceStringTestHelper<arm::pipe::SwTraceCharPolicy>("VALID_string_456", buffer, 6);
2266 StringToSwTraceStringTestHelper<arm::pipe::SwTraceCharPolicy>(" ", buffer, 2);
2267 StringToSwTraceStringTestHelper<arm::pipe::SwTraceCharPolicy>("valid string", buffer, 5);
2268 StringToSwTraceStringTestHelper<arm::pipe::SwTraceCharPolicy>("!$%", buffer, 2);
2269 StringToSwTraceStringTestHelper<arm::pipe::SwTraceCharPolicy>("valid|\\~string#123", buffer, 6);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002270
2271 // Invalid SWTrace strings
Sadik Armagan1625efc2021-06-10 18:24:34 +01002272 CHECK(!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceCharPolicy>("€£", buffer));
2273 CHECK(buffer.empty());
2274 CHECK(!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceCharPolicy>("invalid‡string", buffer));
2275 CHECK(buffer.empty());
2276 CHECK(!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceCharPolicy>("12Ž34", buffer));
2277 CHECK(buffer.empty());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002278}
2279
Sadik Armagan1625efc2021-06-10 18:24:34 +01002280TEST_CASE("StringToSwTraceNameStringTest")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002281{
2282 std::vector<uint32_t> buffer;
2283
2284 // Valid SWTrace namestrings (expected size in words)
Jim Flynnbbfe6032020-07-20 16:57:44 +01002285 StringToSwTraceStringTestHelper<arm::pipe::SwTraceNameCharPolicy>("", buffer, 2);
2286 StringToSwTraceStringTestHelper<arm::pipe::SwTraceNameCharPolicy>("_", buffer, 2);
2287 StringToSwTraceStringTestHelper<arm::pipe::SwTraceNameCharPolicy>("0123", buffer, 3);
2288 StringToSwTraceStringTestHelper<arm::pipe::SwTraceNameCharPolicy>("valid_string", buffer, 5);
2289 StringToSwTraceStringTestHelper<arm::pipe::SwTraceNameCharPolicy>("VALID_string_456", buffer, 6);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002290
2291 // Invalid SWTrace namestrings
Sadik Armagan1625efc2021-06-10 18:24:34 +01002292 CHECK(!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceNameCharPolicy>(" ", buffer));
2293 CHECK(buffer.empty());
2294 CHECK(!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceNameCharPolicy>("invalid string", buffer));
2295 CHECK(buffer.empty());
2296 CHECK(!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceNameCharPolicy>("!$%", buffer));
2297 CHECK(buffer.empty());
2298 CHECK(!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceNameCharPolicy>("invalid|\\~string#123", buffer));
2299 CHECK(buffer.empty());
2300 CHECK(!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceNameCharPolicy>("€£", buffer));
2301 CHECK(buffer.empty());
2302 CHECK(!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceNameCharPolicy>("invalid‡string", buffer));
2303 CHECK(buffer.empty());
2304 CHECK(!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceNameCharPolicy>("12Ž34", buffer));
2305 CHECK(buffer.empty());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002306}
2307
Sadik Armagan1625efc2021-06-10 18:24:34 +01002308TEST_CASE("CheckPeriodicCounterCaptureThread")
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002309{
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002310 class CaptureReader : public IReadCounterValues
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002311 {
2312 public:
Finn Williamsf4d59a62019-10-14 15:55:18 +01002313 CaptureReader(uint16_t counterSize)
2314 {
Keith Davis3201eea2019-10-24 17:30:41 +01002315 for (uint16_t i = 0; i < counterSize; ++i)
Finn Williamsf4d59a62019-10-14 15:55:18 +01002316 {
2317 m_Data[i] = 0;
2318 }
2319 m_CounterSize = counterSize;
2320 }
2321 //not used
Matteo Martincighe8485382019-10-10 14:08:21 +01002322 bool IsCounterRegistered(uint16_t counterUid) const override
2323 {
Jim Flynn9265a882022-03-10 23:35:26 +00002324 arm::pipe::IgnoreUnused(counterUid);
Finn Williamsf4d59a62019-10-14 15:55:18 +01002325 return false;
Matteo Martincighe8485382019-10-10 14:08:21 +01002326 }
Jim Flynn34430252022-03-04 15:03:58 +00002327 bool IsCounterRegistered(const std::string& counterName) const override
2328 {
Jim Flynn9265a882022-03-10 23:35:26 +00002329 arm::pipe::IgnoreUnused(counterName);
Jim Flynn34430252022-03-04 15:03:58 +00002330 return false;
2331 }
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002332 uint16_t GetCounterCount() const override
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002333 {
Finn Williamsf4d59a62019-10-14 15:55:18 +01002334 return m_CounterSize;
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002335 }
2336
Finn Williamsf3fcf322020-05-11 14:38:02 +01002337 uint32_t GetAbsoluteCounterValue(uint16_t counterUid) const override
2338 {
2339 if (counterUid > m_CounterSize)
2340 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01002341 FAIL("Invalid counter Uid");
Finn Williamsf3fcf322020-05-11 14:38:02 +01002342 }
2343 return m_Data.at(counterUid).load();
2344 }
2345
2346 uint32_t GetDeltaCounterValue(uint16_t counterUid) override
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002347 {
Keith Davis3201eea2019-10-24 17:30:41 +01002348 if (counterUid > m_CounterSize)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002349 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01002350 FAIL("Invalid counter Uid");
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002351 }
Matteo Martincighe8485382019-10-10 14:08:21 +01002352 return m_Data.at(counterUid).load();
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002353 }
2354
Matteo Martincighe8485382019-10-10 14:08:21 +01002355 void SetCounterValue(uint16_t counterUid, uint32_t value)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002356 {
Keith Davis3201eea2019-10-24 17:30:41 +01002357 if (counterUid > m_CounterSize)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002358 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01002359 FAIL("Invalid counter Uid");
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002360 }
Finn Williamsf4d59a62019-10-14 15:55:18 +01002361 m_Data.at(counterUid).store(value);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002362 }
2363
2364 private:
Matteo Martincighe8485382019-10-10 14:08:21 +01002365 std::unordered_map<uint16_t, std::atomic<uint32_t>> m_Data;
Finn Williamsf4d59a62019-10-14 15:55:18 +01002366 uint16_t m_CounterSize;
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002367 };
2368
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002369 ProfilingStateMachine profilingStateMachine;
2370
Cathal Corbett6f073722022-03-04 12:11:09 +00002371 const std::unordered_map<std::string,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00002372 std::shared_ptr<IBackendProfilingContext>> backendProfilingContext;
Finn Williams032bc742020-02-12 11:02:34 +00002373 CounterIdMap counterIdMap;
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002374 Holder data;
2375 std::vector<uint16_t> captureIds1 = { 0, 1 };
2376 std::vector<uint16_t> captureIds2;
2377
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002378 MockBufferManager mockBuffer(512);
Sadik Armagan3896b472020-02-10 12:24:15 +00002379 SendCounterPacket sendCounterPacket(mockBuffer);
2380 SendThread sendThread(profilingStateMachine, mockBuffer, sendCounterPacket);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002381
2382 std::vector<uint16_t> counterIds;
Finn Williamsf4d59a62019-10-14 15:55:18 +01002383 CaptureReader captureReader(2);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002384
Keith Davis3201eea2019-10-24 17:30:41 +01002385 unsigned int valueA = 10;
2386 unsigned int valueB = 15;
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002387 unsigned int numSteps = 5;
2388
Finn Williams032bc742020-02-12 11:02:34 +00002389 PeriodicCounterCapture periodicCounterCapture(std::ref(data), std::ref(sendCounterPacket), captureReader,
2390 counterIdMap, backendProfilingContext);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002391
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002392 for (unsigned int i = 0; i < numSteps; ++i)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002393 {
Finn Williams032bc742020-02-12 11:02:34 +00002394 data.SetCaptureData(1, captureIds1, {});
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002395 captureReader.SetCounterValue(0, valueA * (i + 1));
2396 captureReader.SetCounterValue(1, valueB * (i + 1));
2397
2398 periodicCounterCapture.Start();
Finn Williamsf4d59a62019-10-14 15:55:18 +01002399 periodicCounterCapture.Stop();
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002400 }
2401
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002402 auto buffer = mockBuffer.GetReadableBuffer();
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002403
2404 uint32_t headerWord0 = ReadUint32(buffer, 0);
2405 uint32_t headerWord1 = ReadUint32(buffer, 4);
2406
Sadik Armagan1625efc2021-06-10 18:24:34 +01002407 CHECK(((headerWord0 >> 26) & 0x0000003F) == 3); // packet family
2408 CHECK(((headerWord0 >> 19) & 0x0000007F) == 0); // packet class
2409 CHECK(((headerWord0 >> 16) & 0x00000007) == 0); // packet type
2410 CHECK(headerWord1 == 20);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002411
Keith Davis3201eea2019-10-24 17:30:41 +01002412 uint32_t offset = 16;
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002413 uint16_t readIndex = ReadUint16(buffer, offset);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002414 CHECK(0 == readIndex);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002415
2416 offset += 2;
2417 uint32_t readValue = ReadUint32(buffer, offset);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002418 CHECK((valueA * numSteps) == readValue);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002419
2420 offset += 4;
2421 readIndex = ReadUint16(buffer, offset);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002422 CHECK(1 == readIndex);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002423
2424 offset += 2;
2425 readValue = ReadUint32(buffer, offset);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002426 CHECK((valueB * numSteps) == readValue);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002427}
2428
Sadik Armagan1625efc2021-06-10 18:24:34 +01002429TEST_CASE("RequestCounterDirectoryCommandHandlerTest1")
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002430{
Jim Flynn397043f2019-10-17 17:37:10 +01002431 const uint32_t familyId = 0;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002432 const uint32_t packetId = 3;
Keith Davis3201eea2019-10-24 17:30:41 +01002433 const uint32_t version = 1;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002434 ProfilingStateMachine profilingStateMachine;
2435 CounterDirectory counterDirectory;
Matteo Martincigh9723d022019-11-13 10:56:41 +00002436 MockBufferManager mockBuffer1(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002437 SendCounterPacket sendCounterPacket(mockBuffer1);
2438 SendThread sendThread(profilingStateMachine, mockBuffer1, sendCounterPacket);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002439 MockBufferManager mockBuffer2(1024);
2440 SendTimelinePacket sendTimelinePacket(mockBuffer2);
Keith Davis3201eea2019-10-24 17:30:41 +01002441 RequestCounterDirectoryCommandHandler commandHandler(familyId, packetId, version, counterDirectory,
Matteo Martincigh9723d022019-11-13 10:56:41 +00002442 sendCounterPacket, sendTimelinePacket, profilingStateMachine);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002443
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002444 const uint32_t wrongPacketId = 47;
Keith Davis3201eea2019-10-24 17:30:41 +01002445 const uint32_t wrongHeader = (wrongPacketId & 0x000003FF) << 16;
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002446
Jim Flynnbbfe6032020-07-20 16:57:44 +01002447 arm::pipe::Packet wrongPacket(wrongHeader);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002448
2449 profilingStateMachine.TransitionToState(ProfilingState::Uninitialised);
Jim Flynnf9db3ef2022-03-08 21:23:44 +00002450 CHECK_THROWS_AS(commandHandler(wrongPacket), arm::pipe::ProfilingException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002451 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
Jim Flynnf9db3ef2022-03-08 21:23:44 +00002452 CHECK_THROWS_AS(commandHandler(wrongPacket), arm::pipe::ProfilingException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002453 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
Jim Flynnf9db3ef2022-03-08 21:23:44 +00002454 CHECK_THROWS_AS(commandHandler(wrongPacket), arm::pipe::ProfilingException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002455 profilingStateMachine.TransitionToState(ProfilingState::Active);
Jim Flynnf9db3ef2022-03-08 21:23:44 +00002456 CHECK_THROWS_AS(commandHandler(wrongPacket), arm::pipe::InvalidArgumentException); // Wrong packet
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002457
2458 const uint32_t rightHeader = (packetId & 0x000003FF) << 16;
2459
Jim Flynnbbfe6032020-07-20 16:57:44 +01002460 arm::pipe::Packet rightPacket(rightHeader);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002461
Sadik Armagan1625efc2021-06-10 18:24:34 +01002462 CHECK_NOTHROW(commandHandler(rightPacket)); // Right packet
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002463
Matteo Martincigh9723d022019-11-13 10:56:41 +00002464 auto readBuffer1 = mockBuffer1.GetReadableBuffer();
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002465
Matteo Martincigh9723d022019-11-13 10:56:41 +00002466 uint32_t header1Word0 = ReadUint32(readBuffer1, 0);
2467 uint32_t header1Word1 = ReadUint32(readBuffer1, 4);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002468
Matteo Martincigh9723d022019-11-13 10:56:41 +00002469 // Counter directory packet
Sadik Armagan1625efc2021-06-10 18:24:34 +01002470 CHECK(((header1Word0 >> 26) & 0x0000003F) == 0); // packet family
2471 CHECK(((header1Word0 >> 16) & 0x000003FF) == 2); // packet id
2472 CHECK(header1Word1 == 24); // data length
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002473
Matteo Martincigh9723d022019-11-13 10:56:41 +00002474 uint32_t bodyHeader1Word0 = ReadUint32(readBuffer1, 8);
Jim Flynn75c14f42022-03-10 22:05:42 +00002475 uint16_t deviceRecordCount = arm::pipe::numeric_cast<uint16_t>(bodyHeader1Word0 >> 16);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002476 CHECK(deviceRecordCount == 0); // device_records_count
Matteo Martincigh9723d022019-11-13 10:56:41 +00002477
2478 auto readBuffer2 = mockBuffer2.GetReadableBuffer();
2479
2480 uint32_t header2Word0 = ReadUint32(readBuffer2, 0);
2481 uint32_t header2Word1 = ReadUint32(readBuffer2, 4);
2482
2483 // Timeline message directory packet
Sadik Armagan1625efc2021-06-10 18:24:34 +01002484 CHECK(((header2Word0 >> 26) & 0x0000003F) == 1); // packet family
2485 CHECK(((header2Word0 >> 16) & 0x000003FF) == 0); // packet id
2486 CHECK(header2Word1 == 443); // data length
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002487}
2488
Sadik Armagan1625efc2021-06-10 18:24:34 +01002489TEST_CASE("RequestCounterDirectoryCommandHandlerTest2")
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002490{
Jim Flynn397043f2019-10-17 17:37:10 +01002491 const uint32_t familyId = 0;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002492 const uint32_t packetId = 3;
Keith Davis3201eea2019-10-24 17:30:41 +01002493 const uint32_t version = 1;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002494 ProfilingStateMachine profilingStateMachine;
2495 CounterDirectory counterDirectory;
Matteo Martincigh9723d022019-11-13 10:56:41 +00002496 MockBufferManager mockBuffer1(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002497 SendCounterPacket sendCounterPacket(mockBuffer1);
2498 SendThread sendThread(profilingStateMachine, mockBuffer1, sendCounterPacket);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002499 MockBufferManager mockBuffer2(1024);
2500 SendTimelinePacket sendTimelinePacket(mockBuffer2);
Keith Davis3201eea2019-10-24 17:30:41 +01002501 RequestCounterDirectoryCommandHandler commandHandler(familyId, packetId, version, counterDirectory,
Matteo Martincigh9723d022019-11-13 10:56:41 +00002502 sendCounterPacket, sendTimelinePacket, profilingStateMachine);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002503 const uint32_t header = (packetId & 0x000003FF) << 16;
Jim Flynnbbfe6032020-07-20 16:57:44 +01002504 const arm::pipe::Packet packet(header);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002505
Matteo Martincigh9723d022019-11-13 10:56:41 +00002506 const Device* device = counterDirectory.RegisterDevice("deviceA", 1);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002507 CHECK(device != nullptr);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002508 const CounterSet* counterSet = counterDirectory.RegisterCounterSet("countersetA");
Sadik Armagan1625efc2021-06-10 18:24:34 +01002509 CHECK(counterSet != nullptr);
Sadik Armagan4c998992020-02-25 12:44:44 +00002510 counterDirectory.RegisterCategory("categoryA");
Keith Davise394bd92019-12-02 15:12:19 +00002511 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID, 24,
2512 "categoryA", 0, 1, 2.0f, "counterA", "descA");
2513 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID, 25,
2514 "categoryA", 1, 1, 3.0f, "counterB", "descB");
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002515
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002516 profilingStateMachine.TransitionToState(ProfilingState::Uninitialised);
Jim Flynnf9db3ef2022-03-08 21:23:44 +00002517 CHECK_THROWS_AS(commandHandler(packet), arm::pipe::ProfilingException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002518 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
Jim Flynnf9db3ef2022-03-08 21:23:44 +00002519 CHECK_THROWS_AS(commandHandler(packet), arm::pipe::ProfilingException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002520 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
Jim Flynnf9db3ef2022-03-08 21:23:44 +00002521 CHECK_THROWS_AS(commandHandler(packet), arm::pipe::ProfilingException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002522 profilingStateMachine.TransitionToState(ProfilingState::Active);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002523 CHECK_NOTHROW(commandHandler(packet));
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002524
Matteo Martincigh9723d022019-11-13 10:56:41 +00002525 auto readBuffer1 = mockBuffer1.GetReadableBuffer();
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002526
Finn Williams985fecf2020-04-30 11:06:43 +01002527 const uint32_t header1Word0 = ReadUint32(readBuffer1, 0);
2528 const uint32_t header1Word1 = ReadUint32(readBuffer1, 4);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002529
Sadik Armagan1625efc2021-06-10 18:24:34 +01002530 CHECK(((header1Word0 >> 26) & 0x0000003F) == 0); // packet family
2531 CHECK(((header1Word0 >> 16) & 0x000003FF) == 2); // packet id
2532 CHECK(header1Word1 == 236); // data length
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002533
Finn Williams985fecf2020-04-30 11:06:43 +01002534 const uint32_t bodyHeaderSizeBytes = bodyHeaderSize * sizeof(uint32_t);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002535
Finn Williams985fecf2020-04-30 11:06:43 +01002536 const uint32_t bodyHeader1Word0 = ReadUint32(readBuffer1, 8);
2537 const uint32_t bodyHeader1Word1 = ReadUint32(readBuffer1, 12);
2538 const uint32_t bodyHeader1Word2 = ReadUint32(readBuffer1, 16);
2539 const uint32_t bodyHeader1Word3 = ReadUint32(readBuffer1, 20);
2540 const uint32_t bodyHeader1Word4 = ReadUint32(readBuffer1, 24);
2541 const uint32_t bodyHeader1Word5 = ReadUint32(readBuffer1, 28);
Jim Flynn75c14f42022-03-10 22:05:42 +00002542 const uint16_t deviceRecordCount = arm::pipe::numeric_cast<uint16_t>(bodyHeader1Word0 >> 16);
2543 const uint16_t counterSetRecordCount = arm::pipe::numeric_cast<uint16_t>(bodyHeader1Word2 >> 16);
2544 const uint16_t categoryRecordCount = arm::pipe::numeric_cast<uint16_t>(bodyHeader1Word4 >> 16);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002545 CHECK(deviceRecordCount == 1); // device_records_count
2546 CHECK(bodyHeader1Word1 == 0 + bodyHeaderSizeBytes); // device_records_pointer_table_offset
2547 CHECK(counterSetRecordCount == 1); // counter_set_count
2548 CHECK(bodyHeader1Word3 == 4 + bodyHeaderSizeBytes); // counter_set_pointer_table_offset
2549 CHECK(categoryRecordCount == 1); // categories_count
2550 CHECK(bodyHeader1Word5 == 8 + bodyHeaderSizeBytes); // categories_pointer_table_offset
Finn Williams985fecf2020-04-30 11:06:43 +01002551
2552 const uint32_t deviceRecordOffset = ReadUint32(readBuffer1, 32);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002553 CHECK(deviceRecordOffset == 12);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002554
Finn Williams985fecf2020-04-30 11:06:43 +01002555 const uint32_t counterSetRecordOffset = ReadUint32(readBuffer1, 36);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002556 CHECK(counterSetRecordOffset == 28);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002557
Finn Williams985fecf2020-04-30 11:06:43 +01002558 const uint32_t categoryRecordOffset = ReadUint32(readBuffer1, 40);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002559 CHECK(categoryRecordOffset == 48);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002560
2561 auto readBuffer2 = mockBuffer2.GetReadableBuffer();
2562
Finn Williams985fecf2020-04-30 11:06:43 +01002563 const uint32_t header2Word0 = ReadUint32(readBuffer2, 0);
2564 const uint32_t header2Word1 = ReadUint32(readBuffer2, 4);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002565
2566 // Timeline message directory packet
Sadik Armagan1625efc2021-06-10 18:24:34 +01002567 CHECK(((header2Word0 >> 26) & 0x0000003F) == 1); // packet family
2568 CHECK(((header2Word0 >> 16) & 0x000003FF) == 0); // packet id
2569 CHECK(header2Word1 == 443); // data length
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002570}
2571
Sadik Armagan1625efc2021-06-10 18:24:34 +01002572TEST_CASE("CheckProfilingServiceGoodConnectionAcknowledgedPacket")
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002573{
Finn Williamsa0de0562020-04-22 12:27:37 +01002574 unsigned int streamMetadataPacketsize = GetStreamMetaDataPacketSize();
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002575
Jim Flynn53e46992019-10-14 12:31:10 +01002576 // Reset the profiling service to the uninitialized state
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00002577 ProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002578 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +00002579 armnn::ArmNNProfilingServiceInitialiser initialiser;
2580 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002581 profilingService.ResetExternalProfilingOptions(options, true);
2582
Sadik Armagan3184c902020-03-18 10:57:30 +00002583 // Swap the profiling connection factory in the profiling service instance with our mock one
Jim Flynn34430252022-03-04 15:03:58 +00002584 SwapProfilingConnectionFactoryHelper helper(arm::pipe::MAX_ARMNN_COUNTER, initialiser, profilingService);
Sadik Armagan3184c902020-03-18 10:57:30 +00002585
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002586 // Bring the profiling service to the "WaitingForAck" state
Sadik Armagan1625efc2021-06-10 18:24:34 +01002587 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002588 profilingService.Update(); // Initialize the counter directory
Sadik Armagan1625efc2021-06-10 18:24:34 +01002589 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002590 profilingService.Update(); // Create the profiling connection
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002591
Matteo Martincighd0613b52019-10-09 16:47:04 +01002592 // Get the mock profiling connection
2593 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
Sadik Armagan1625efc2021-06-10 18:24:34 +01002594 CHECK(mockProfilingConnection);
Matteo Martincighd0613b52019-10-09 16:47:04 +01002595
Matteo Martincighe8485382019-10-10 14:08:21 +01002596 // Remove the packets received so far
2597 mockProfilingConnection->Clear();
2598
Sadik Armagan1625efc2021-06-10 18:24:34 +01002599 CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002600 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002601
2602 // Wait for the Stream Metadata packet to be sent
Sadik Armagan1625efc2021-06-10 18:24:34 +01002603 CHECK(helper.WaitForPacketsSent(
Finn Williams09ad6f92019-12-19 17:05:18 +00002604 mockProfilingConnection, PacketType::StreamMetaData, streamMetadataPacketsize) >= 1);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002605
2606 // Write a valid "Connection Acknowledged" packet into the mock profiling connection, to simulate a valid
2607 // reply from an external profiling service
2608
2609 // Connection Acknowledged Packet header (word 0, word 1 is always zero):
2610 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2611 // 16:25 [10] packet_id: Packet identifier, value 0b0000000001
2612 // 8:15 [8] reserved: Reserved, value 0b00000000
2613 // 0:7 [8] reserved: Reserved, value 0b00000000
2614 uint32_t packetFamily = 0;
2615 uint32_t packetId = 1;
Keith Davis3201eea2019-10-24 17:30:41 +01002616 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002617
Matteo Martincighd0613b52019-10-09 16:47:04 +01002618 // Create the Connection Acknowledged Packet
Jim Flynnbbfe6032020-07-20 16:57:44 +01002619 arm::pipe::Packet connectionAcknowledgedPacket(header);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002620
2621 // Write the packet to the mock profiling connection
2622 mockProfilingConnection->WritePacket(std::move(connectionAcknowledgedPacket));
2623
Colm Donelan2ba48d22019-11-29 09:10:59 +00002624 // Wait for the counter directory packet to ensure the ConnectionAcknowledgedCommandHandler has run.
Sadik Armagan1625efc2021-06-10 18:24:34 +01002625 CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::CounterDirectory) == 1);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002626
2627 // The Connection Acknowledged Command Handler should have updated the profiling state accordingly
Sadik Armagan1625efc2021-06-10 18:24:34 +01002628 CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincighd0613b52019-10-09 16:47:04 +01002629
2630 // Reset the profiling service to stop any running thread
2631 options.m_EnableProfiling = false;
2632 profilingService.ResetExternalProfilingOptions(options, true);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002633}
2634
Sadik Armagan1625efc2021-06-10 18:24:34 +01002635TEST_CASE("CheckProfilingServiceGoodRequestCounterDirectoryPacket")
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002636{
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002637 // Reset the profiling service to the uninitialized state
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00002638 ProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002639 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +00002640 armnn::ArmNNProfilingServiceInitialiser initialiser;
2641 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002642 profilingService.ResetExternalProfilingOptions(options, true);
2643
Sadik Armagan3184c902020-03-18 10:57:30 +00002644 // Swap the profiling connection factory in the profiling service instance with our mock one
Jim Flynn34430252022-03-04 15:03:58 +00002645 SwapProfilingConnectionFactoryHelper helper(arm::pipe::MAX_ARMNN_COUNTER, initialiser, profilingService);
Sadik Armagan3184c902020-03-18 10:57:30 +00002646
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002647 // Bring the profiling service to the "Active" state
Sadik Armagan1625efc2021-06-10 18:24:34 +01002648 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002649 profilingService.Update(); // Initialize the counter directory
Sadik Armagan1625efc2021-06-10 18:24:34 +01002650 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002651 profilingService.Update(); // Create the profiling connection
Sadik Armagan1625efc2021-06-10 18:24:34 +01002652 CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002653 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002654
Colm Donelan2ba48d22019-11-29 09:10:59 +00002655 // Get the mock profiling connection
2656 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
Sadik Armagan1625efc2021-06-10 18:24:34 +01002657 CHECK(mockProfilingConnection);
Colm Donelan2ba48d22019-11-29 09:10:59 +00002658
Matteo Martincighe8485382019-10-10 14:08:21 +01002659 // Force the profiling service to the "Active" state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002660 helper.ForceTransitionToState(ProfilingState::Active);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002661 CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002662
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002663 // Write a valid "Request Counter Directory" packet into the mock profiling connection, to simulate a valid
2664 // reply from an external profiling service
2665
2666 // Request Counter Directory packet header (word 0, word 1 is always zero):
2667 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2668 // 16:25 [10] packet_id: Packet identifier, value 0b0000000011
2669 // 8:15 [8] reserved: Reserved, value 0b00000000
2670 // 0:7 [8] reserved: Reserved, value 0b00000000
2671 uint32_t packetFamily = 0;
2672 uint32_t packetId = 3;
Keith Davis3201eea2019-10-24 17:30:41 +01002673 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002674
2675 // Create the Request Counter Directory packet
Jim Flynnbbfe6032020-07-20 16:57:44 +01002676 arm::pipe::Packet requestCounterDirectoryPacket(header);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002677
2678 // Write the packet to the mock profiling connection
2679 mockProfilingConnection->WritePacket(std::move(requestCounterDirectoryPacket));
2680
Sadik Armagan4c998992020-02-25 12:44:44 +00002681 // Expecting one CounterDirectory Packet of length 652
Jim Flynn6398a982020-05-27 17:05:21 +01002682 // and one TimelineMessageDirectory packet of length 451
Sadik Armagan1625efc2021-06-10 18:24:34 +01002683 CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::CounterDirectory, 652) == 1);
2684 CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::TimelineMessageDirectory, 451) == 1);
Matteo Martincighe8485382019-10-10 14:08:21 +01002685
2686 // The Request Counter Directory Command Handler should not have updated the profiling state
Sadik Armagan1625efc2021-06-10 18:24:34 +01002687 CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincighe8485382019-10-10 14:08:21 +01002688
2689 // Reset the profiling service to stop any running thread
2690 options.m_EnableProfiling = false;
2691 profilingService.ResetExternalProfilingOptions(options, true);
2692}
2693
Sadik Armagan1625efc2021-06-10 18:24:34 +01002694TEST_CASE("CheckProfilingServiceBadPeriodicCounterSelectionPacketInvalidCounterUid")
Matteo Martincighe8485382019-10-10 14:08:21 +01002695{
Matteo Martincighe8485382019-10-10 14:08:21 +01002696 // Reset the profiling service to the uninitialized state
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00002697 ProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002698 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +00002699 armnn::ArmNNProfilingServiceInitialiser initialiser;
2700 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Matteo Martincighe8485382019-10-10 14:08:21 +01002701 profilingService.ResetExternalProfilingOptions(options, true);
2702
Sadik Armagan3184c902020-03-18 10:57:30 +00002703 // Swap the profiling connection factory in the profiling service instance with our mock one
Jim Flynn34430252022-03-04 15:03:58 +00002704 SwapProfilingConnectionFactoryHelper helper(arm::pipe::MAX_ARMNN_COUNTER, initialiser, profilingService);
Sadik Armagan3184c902020-03-18 10:57:30 +00002705
Matteo Martincighe8485382019-10-10 14:08:21 +01002706 // Bring the profiling service to the "Active" state
Sadik Armagan1625efc2021-06-10 18:24:34 +01002707 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002708 profilingService.Update(); // Initialize the counter directory
Sadik Armagan1625efc2021-06-10 18:24:34 +01002709 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002710 profilingService.Update(); // Create the profiling connection
Sadik Armagan1625efc2021-06-10 18:24:34 +01002711 CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002712 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002713
Colm Donelan2ba48d22019-11-29 09:10:59 +00002714 // Get the mock profiling connection
2715 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
Sadik Armagan1625efc2021-06-10 18:24:34 +01002716 CHECK(mockProfilingConnection);
Colm Donelan2ba48d22019-11-29 09:10:59 +00002717
Matteo Martincighe8485382019-10-10 14:08:21 +01002718 // Force the profiling service to the "Active" state
2719 helper.ForceTransitionToState(ProfilingState::Active);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002720 CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincighe8485382019-10-10 14:08:21 +01002721
Matteo Martincighe8485382019-10-10 14:08:21 +01002722 // Remove the packets received so far
2723 mockProfilingConnection->Clear();
2724
2725 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2726 // external profiling service
2727
2728 // Periodic Counter Selection packet header:
2729 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2730 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2731 // 8:15 [8] reserved: Reserved, value 0b00000000
2732 // 0:7 [8] reserved: Reserved, value 0b00000000
2733 uint32_t packetFamily = 0;
2734 uint32_t packetId = 4;
Keith Davis3201eea2019-10-24 17:30:41 +01002735 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincighe8485382019-10-10 14:08:21 +01002736
Keith Davis3201eea2019-10-24 17:30:41 +01002737 uint32_t capturePeriod = 123456; // Some capture period (microseconds)
Matteo Martincighe8485382019-10-10 14:08:21 +01002738
2739 // Get the first valid counter UID
2740 const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
Keith Davis3201eea2019-10-24 17:30:41 +01002741 const Counters& counters = counterDirectory.GetCounters();
Sadik Armagan1625efc2021-06-10 18:24:34 +01002742 CHECK(counters.size() > 1);
Keith Davis3201eea2019-10-24 17:30:41 +01002743 uint16_t counterUidA = counters.begin()->first; // First valid counter UID
2744 uint16_t counterUidB = 9999; // Second invalid counter UID
Matteo Martincighe8485382019-10-10 14:08:21 +01002745
2746 uint32_t length = 8;
2747
2748 auto data = std::make_unique<unsigned char[]>(length);
2749 WriteUint32(data.get(), 0, capturePeriod);
2750 WriteUint16(data.get(), 4, counterUidA);
2751 WriteUint16(data.get(), 6, counterUidB);
2752
2753 // Create the Periodic Counter Selection packet
Jim Flynnbbfe6032020-07-20 16:57:44 +01002754 // Length > 0, this will start the Period Counter Capture thread
2755 arm::pipe::Packet periodicCounterSelectionPacket(header, length, data);
2756
Matteo Martincighe8485382019-10-10 14:08:21 +01002757
2758 // Write the packet to the mock profiling connection
2759 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2760
Finn Williams09ad6f92019-12-19 17:05:18 +00002761 // Expecting one Periodic Counter Selection packet of length 14
2762 // and at least one Periodic Counter Capture packet of length 22
Sadik Armagan1625efc2021-06-10 18:24:34 +01002763 CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterSelection, 14) == 1);
2764 CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterCapture, 22) >= 1);
Matteo Martincighe8485382019-10-10 14:08:21 +01002765
2766 // The Periodic Counter Selection Handler should not have updated the profiling state
Sadik Armagan1625efc2021-06-10 18:24:34 +01002767 CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincighe8485382019-10-10 14:08:21 +01002768
2769 // Reset the profiling service to stop any running thread
2770 options.m_EnableProfiling = false;
2771 profilingService.ResetExternalProfilingOptions(options, true);
2772}
2773
Sadik Armagan1625efc2021-06-10 18:24:34 +01002774TEST_CASE("CheckProfilingServiceGoodPeriodicCounterSelectionPacketNoCounters")
Matteo Martincighe8485382019-10-10 14:08:21 +01002775{
Matteo Martincighe8485382019-10-10 14:08:21 +01002776 // Reset the profiling service to the uninitialized state
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00002777 ProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002778 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +00002779 armnn::ArmNNProfilingServiceInitialiser initialiser;
2780 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Matteo Martincighe8485382019-10-10 14:08:21 +01002781 profilingService.ResetExternalProfilingOptions(options, true);
2782
Sadik Armagan3184c902020-03-18 10:57:30 +00002783 // Swap the profiling connection factory in the profiling service instance with our mock one
Jim Flynn34430252022-03-04 15:03:58 +00002784 SwapProfilingConnectionFactoryHelper helper(arm::pipe::MAX_ARMNN_COUNTER, initialiser, profilingService);
Sadik Armagan3184c902020-03-18 10:57:30 +00002785
Matteo Martincighe8485382019-10-10 14:08:21 +01002786 // Bring the profiling service to the "Active" state
Sadik Armagan1625efc2021-06-10 18:24:34 +01002787 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002788 profilingService.Update(); // Initialize the counter directory
Sadik Armagan1625efc2021-06-10 18:24:34 +01002789 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002790 profilingService.Update(); // Create the profiling connection
Sadik Armagan1625efc2021-06-10 18:24:34 +01002791 CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002792 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002793
Colm Donelan2ba48d22019-11-29 09:10:59 +00002794 // Get the mock profiling connection
2795 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
Sadik Armagan1625efc2021-06-10 18:24:34 +01002796 CHECK(mockProfilingConnection);
Colm Donelan2ba48d22019-11-29 09:10:59 +00002797
Matteo Martincighe8485382019-10-10 14:08:21 +01002798 // Wait for the Stream Metadata packet the be sent
2799 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Finn Williams09ad6f92019-12-19 17:05:18 +00002800 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData);
Matteo Martincighe8485382019-10-10 14:08:21 +01002801
2802 // Force the profiling service to the "Active" state
2803 helper.ForceTransitionToState(ProfilingState::Active);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002804 CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincighe8485382019-10-10 14:08:21 +01002805
Matteo Martincighe8485382019-10-10 14:08:21 +01002806 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2807 // external profiling service
2808
2809 // Periodic Counter Selection packet header:
2810 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2811 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2812 // 8:15 [8] reserved: Reserved, value 0b00000000
2813 // 0:7 [8] reserved: Reserved, value 0b00000000
2814 uint32_t packetFamily = 0;
2815 uint32_t packetId = 4;
Keith Davis3201eea2019-10-24 17:30:41 +01002816 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincighe8485382019-10-10 14:08:21 +01002817
2818 // Create the Periodic Counter Selection packet
Jim Flynnbbfe6032020-07-20 16:57:44 +01002819 // Length == 0, this will disable the collection of counters
2820 arm::pipe::Packet periodicCounterSelectionPacket(header);
Matteo Martincighe8485382019-10-10 14:08:21 +01002821
2822 // Write the packet to the mock profiling connection
2823 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2824
Finn Williams09ad6f92019-12-19 17:05:18 +00002825 // Wait for the Periodic Counter Selection packet of length 12 to be sent
2826 // The size of the expected Periodic Counter Selection (echos the sent one)
Sadik Armagan1625efc2021-06-10 18:24:34 +01002827 CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterSelection, 12) == 1);
Matteo Martincighe8485382019-10-10 14:08:21 +01002828
2829 // The Periodic Counter Selection Handler should not have updated the profiling state
Sadik Armagan1625efc2021-06-10 18:24:34 +01002830 CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincighe8485382019-10-10 14:08:21 +01002831
Finn Williams09ad6f92019-12-19 17:05:18 +00002832 // No Periodic Counter packets are expected
Sadik Armagan1625efc2021-06-10 18:24:34 +01002833 CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterCapture, 0, 0) == 0);
Matteo Martincighe8485382019-10-10 14:08:21 +01002834
2835 // Reset the profiling service to stop any running thread
2836 options.m_EnableProfiling = false;
2837 profilingService.ResetExternalProfilingOptions(options, true);
2838}
2839
Sadik Armagan1625efc2021-06-10 18:24:34 +01002840TEST_CASE("CheckProfilingServiceGoodPeriodicCounterSelectionPacketSingleCounter")
Matteo Martincighe8485382019-10-10 14:08:21 +01002841{
Matteo Martincighe8485382019-10-10 14:08:21 +01002842 // Reset the profiling service to the uninitialized state
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00002843 ProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002844 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +00002845 armnn::ArmNNProfilingServiceInitialiser initialiser;
2846 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Matteo Martincighe8485382019-10-10 14:08:21 +01002847 profilingService.ResetExternalProfilingOptions(options, true);
2848
Sadik Armagan3184c902020-03-18 10:57:30 +00002849 // Swap the profiling connection factory in the profiling service instance with our mock one
Jim Flynn34430252022-03-04 15:03:58 +00002850 SwapProfilingConnectionFactoryHelper helper(arm::pipe::MAX_ARMNN_COUNTER, initialiser, profilingService);
Sadik Armagan3184c902020-03-18 10:57:30 +00002851
Matteo Martincighe8485382019-10-10 14:08:21 +01002852 // Bring the profiling service to the "Active" state
Sadik Armagan1625efc2021-06-10 18:24:34 +01002853 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002854 profilingService.Update(); // Initialize the counter directory
Sadik Armagan1625efc2021-06-10 18:24:34 +01002855 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002856 profilingService.Update(); // Create the profiling connection
Sadik Armagan1625efc2021-06-10 18:24:34 +01002857 CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002858 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002859
Colm Donelan2ba48d22019-11-29 09:10:59 +00002860 // Get the mock profiling connection
2861 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
Sadik Armagan1625efc2021-06-10 18:24:34 +01002862 CHECK(mockProfilingConnection);
Colm Donelan2ba48d22019-11-29 09:10:59 +00002863
Finn Williams09ad6f92019-12-19 17:05:18 +00002864 // Wait for the Stream Metadata packet to be sent
Matteo Martincighe8485382019-10-10 14:08:21 +01002865 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Finn Williams09ad6f92019-12-19 17:05:18 +00002866 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData);
Matteo Martincighe8485382019-10-10 14:08:21 +01002867
2868 // Force the profiling service to the "Active" state
2869 helper.ForceTransitionToState(ProfilingState::Active);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002870 CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincighe8485382019-10-10 14:08:21 +01002871
Matteo Martincighe8485382019-10-10 14:08:21 +01002872 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2873 // external profiling service
2874
2875 // Periodic Counter Selection packet header:
2876 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2877 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2878 // 8:15 [8] reserved: Reserved, value 0b00000000
2879 // 0:7 [8] reserved: Reserved, value 0b00000000
2880 uint32_t packetFamily = 0;
2881 uint32_t packetId = 4;
Keith Davis3201eea2019-10-24 17:30:41 +01002882 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincighe8485382019-10-10 14:08:21 +01002883
Keith Davis3201eea2019-10-24 17:30:41 +01002884 uint32_t capturePeriod = 123456; // Some capture period (microseconds)
Matteo Martincighe8485382019-10-10 14:08:21 +01002885
2886 // Get the first valid counter UID
2887 const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
Keith Davis3201eea2019-10-24 17:30:41 +01002888 const Counters& counters = counterDirectory.GetCounters();
Sadik Armagan1625efc2021-06-10 18:24:34 +01002889 CHECK(!counters.empty());
Keith Davis3201eea2019-10-24 17:30:41 +01002890 uint16_t counterUid = counters.begin()->first; // Valid counter UID
Matteo Martincighe8485382019-10-10 14:08:21 +01002891
2892 uint32_t length = 6;
2893
2894 auto data = std::make_unique<unsigned char[]>(length);
2895 WriteUint32(data.get(), 0, capturePeriod);
2896 WriteUint16(data.get(), 4, counterUid);
2897
2898 // Create the Periodic Counter Selection packet
Jim Flynnbbfe6032020-07-20 16:57:44 +01002899 // Length > 0, this will start the Period Counter Capture thread
2900 arm::pipe::Packet periodicCounterSelectionPacket(header, length, data);
Matteo Martincighe8485382019-10-10 14:08:21 +01002901
2902 // Write the packet to the mock profiling connection
2903 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2904
Finn Williams09ad6f92019-12-19 17:05:18 +00002905 // Expecting one Periodic Counter Selection packet of length 14
2906 // and at least one Periodic Counter Capture packet of length 22
Sadik Armagan1625efc2021-06-10 18:24:34 +01002907 CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterSelection, 14) == 1);
2908 CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterCapture, 22) >= 1);
Matteo Martincighe8485382019-10-10 14:08:21 +01002909
2910 // The Periodic Counter Selection Handler should not have updated the profiling state
Sadik Armagan1625efc2021-06-10 18:24:34 +01002911 CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincighe8485382019-10-10 14:08:21 +01002912
2913 // Reset the profiling service to stop any running thread
2914 options.m_EnableProfiling = false;
2915 profilingService.ResetExternalProfilingOptions(options, true);
2916}
2917
Sadik Armagan1625efc2021-06-10 18:24:34 +01002918TEST_CASE("CheckProfilingServiceGoodPeriodicCounterSelectionPacketMultipleCounters")
Matteo Martincighe8485382019-10-10 14:08:21 +01002919{
Matteo Martincighe8485382019-10-10 14:08:21 +01002920 // Reset the profiling service to the uninitialized state
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00002921 ProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002922 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +00002923 armnn::ArmNNProfilingServiceInitialiser initialiser;
2924 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Matteo Martincighe8485382019-10-10 14:08:21 +01002925 profilingService.ResetExternalProfilingOptions(options, true);
2926
Sadik Armagan3184c902020-03-18 10:57:30 +00002927 // Swap the profiling connection factory in the profiling service instance with our mock one
Jim Flynn34430252022-03-04 15:03:58 +00002928 SwapProfilingConnectionFactoryHelper helper(arm::pipe::MAX_ARMNN_COUNTER, initialiser, profilingService);
Sadik Armagan3184c902020-03-18 10:57:30 +00002929
Matteo Martincighe8485382019-10-10 14:08:21 +01002930 // Bring the profiling service to the "Active" state
Sadik Armagan1625efc2021-06-10 18:24:34 +01002931 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002932 profilingService.Update(); // Initialize the counter directory
Sadik Armagan1625efc2021-06-10 18:24:34 +01002933 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002934 profilingService.Update(); // Create the profiling connection
Sadik Armagan1625efc2021-06-10 18:24:34 +01002935 CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002936 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002937
Colm Donelan2ba48d22019-11-29 09:10:59 +00002938 // Get the mock profiling connection
2939 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
Sadik Armagan1625efc2021-06-10 18:24:34 +01002940 CHECK(mockProfilingConnection);
Colm Donelan2ba48d22019-11-29 09:10:59 +00002941
Matteo Martincighe8485382019-10-10 14:08:21 +01002942 // Wait for the Stream Metadata packet the be sent
2943 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Finn Williams09ad6f92019-12-19 17:05:18 +00002944 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData);
Matteo Martincighe8485382019-10-10 14:08:21 +01002945
2946 // Force the profiling service to the "Active" state
2947 helper.ForceTransitionToState(ProfilingState::Active);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002948 CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincighe8485382019-10-10 14:08:21 +01002949
Matteo Martincighe8485382019-10-10 14:08:21 +01002950 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2951 // external profiling service
2952
2953 // Periodic Counter Selection packet header:
2954 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2955 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2956 // 8:15 [8] reserved: Reserved, value 0b00000000
2957 // 0:7 [8] reserved: Reserved, value 0b00000000
2958 uint32_t packetFamily = 0;
2959 uint32_t packetId = 4;
Keith Davis3201eea2019-10-24 17:30:41 +01002960 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincighe8485382019-10-10 14:08:21 +01002961
Keith Davis3201eea2019-10-24 17:30:41 +01002962 uint32_t capturePeriod = 123456; // Some capture period (microseconds)
Matteo Martincighe8485382019-10-10 14:08:21 +01002963
2964 // Get the first valid counter UID
2965 const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
Keith Davis3201eea2019-10-24 17:30:41 +01002966 const Counters& counters = counterDirectory.GetCounters();
Sadik Armagan1625efc2021-06-10 18:24:34 +01002967 CHECK(counters.size() > 1);
Keith Davis3201eea2019-10-24 17:30:41 +01002968 uint16_t counterUidA = counters.begin()->first; // First valid counter UID
2969 uint16_t counterUidB = (counters.begin()++)->first; // Second valid counter UID
Matteo Martincighe8485382019-10-10 14:08:21 +01002970
2971 uint32_t length = 8;
2972
2973 auto data = std::make_unique<unsigned char[]>(length);
2974 WriteUint32(data.get(), 0, capturePeriod);
2975 WriteUint16(data.get(), 4, counterUidA);
2976 WriteUint16(data.get(), 6, counterUidB);
2977
2978 // Create the Periodic Counter Selection packet
Jim Flynnbbfe6032020-07-20 16:57:44 +01002979 // Length > 0, this will start the Period Counter Capture thread
2980 arm::pipe::Packet periodicCounterSelectionPacket(header, length, data);
Matteo Martincighe8485382019-10-10 14:08:21 +01002981
2982 // Write the packet to the mock profiling connection
2983 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2984
Finn Williams09ad6f92019-12-19 17:05:18 +00002985 // Expecting one PeriodicCounterSelection Packet with a length of 16
2986 // And at least one PeriodicCounterCapture Packet with a length of 28
Sadik Armagan1625efc2021-06-10 18:24:34 +01002987 CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterSelection, 16) == 1);
2988 CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterCapture, 28) >= 1);
Matteo Martincighe8485382019-10-10 14:08:21 +01002989
2990 // The Periodic Counter Selection Handler should not have updated the profiling state
Sadik Armagan1625efc2021-06-10 18:24:34 +01002991 CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002992
2993 // Reset the profiling service to stop any running thread
2994 options.m_EnableProfiling = false;
2995 profilingService.ResetExternalProfilingOptions(options, true);
2996}
2997
Sadik Armagan1625efc2021-06-10 18:24:34 +01002998TEST_CASE("CheckProfilingServiceDisconnect")
Jim Flynn53e46992019-10-14 12:31:10 +01002999{
Jim Flynn53e46992019-10-14 12:31:10 +01003000 // Reset the profiling service to the uninitialized state
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003001 ProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01003002 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +00003003 armnn::ArmNNProfilingServiceInitialiser initialiser;
3004 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Jim Flynn53e46992019-10-14 12:31:10 +01003005 profilingService.ResetExternalProfilingOptions(options, true);
3006
Sadik Armagan3184c902020-03-18 10:57:30 +00003007 // Swap the profiling connection factory in the profiling service instance with our mock one
Jim Flynn34430252022-03-04 15:03:58 +00003008 SwapProfilingConnectionFactoryHelper helper(arm::pipe::MAX_ARMNN_COUNTER, initialiser, profilingService);
Sadik Armagan3184c902020-03-18 10:57:30 +00003009
Jim Flynn53e46992019-10-14 12:31:10 +01003010 // Try to disconnect the profiling service while in the "Uninitialised" state
Sadik Armagan1625efc2021-06-10 18:24:34 +01003011 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Jim Flynn53e46992019-10-14 12:31:10 +01003012 profilingService.Disconnect();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003013 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised); // The state should not change
Jim Flynn53e46992019-10-14 12:31:10 +01003014
3015 // Try to disconnect the profiling service while in the "NotConnected" state
Keith Davis3201eea2019-10-24 17:30:41 +01003016 profilingService.Update(); // Initialize the counter directory
Sadik Armagan1625efc2021-06-10 18:24:34 +01003017 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Jim Flynn53e46992019-10-14 12:31:10 +01003018 profilingService.Disconnect();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003019 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected); // The state should not change
Jim Flynn53e46992019-10-14 12:31:10 +01003020
3021 // Try to disconnect the profiling service while in the "WaitingForAck" state
Keith Davis3201eea2019-10-24 17:30:41 +01003022 profilingService.Update(); // Create the profiling connection
Sadik Armagan1625efc2021-06-10 18:24:34 +01003023 CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Jim Flynn53e46992019-10-14 12:31:10 +01003024 profilingService.Disconnect();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003025 CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck); // The state should not change
Jim Flynn53e46992019-10-14 12:31:10 +01003026
3027 // Try to disconnect the profiling service while in the "Active" state
Keith Davis3201eea2019-10-24 17:30:41 +01003028 profilingService.Update(); // Start the command handler and the send thread
Jim Flynn53e46992019-10-14 12:31:10 +01003029
Colm Donelan2ba48d22019-11-29 09:10:59 +00003030 // Get the mock profiling connection
3031 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003032 CHECK(mockProfilingConnection);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003033
Jim Flynn53e46992019-10-14 12:31:10 +01003034 // Wait for the Stream Metadata packet the be sent
3035 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Finn Williams09ad6f92019-12-19 17:05:18 +00003036 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData);
Jim Flynn53e46992019-10-14 12:31:10 +01003037
3038 // Force the profiling service to the "Active" state
3039 helper.ForceTransitionToState(ProfilingState::Active);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003040 CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Jim Flynn53e46992019-10-14 12:31:10 +01003041
Jim Flynn53e46992019-10-14 12:31:10 +01003042 // Check that the profiling connection is open
Sadik Armagan1625efc2021-06-10 18:24:34 +01003043 CHECK(mockProfilingConnection->IsOpen());
Jim Flynn53e46992019-10-14 12:31:10 +01003044
3045 profilingService.Disconnect();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003046 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected); // The state should have changed
Jim Flynn53e46992019-10-14 12:31:10 +01003047
3048 // Check that the profiling connection has been reset
3049 mockProfilingConnection = helper.GetMockProfilingConnection();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003050 CHECK(mockProfilingConnection == nullptr);
Jim Flynn53e46992019-10-14 12:31:10 +01003051
3052 // Reset the profiling service to stop any running thread
3053 options.m_EnableProfiling = false;
3054 profilingService.ResetExternalProfilingOptions(options, true);
3055}
3056
Sadik Armagan1625efc2021-06-10 18:24:34 +01003057TEST_CASE("CheckProfilingServiceGoodPerJobCounterSelectionPacket")
Matteo Martincigh994b5342019-10-11 17:19:56 +01003058{
Matteo Martincigh994b5342019-10-11 17:19:56 +01003059 // Reset the profiling service to the uninitialized state
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003060 ProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01003061 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +00003062 armnn::ArmNNProfilingServiceInitialiser initialiser;
3063 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Matteo Martincigh994b5342019-10-11 17:19:56 +01003064 profilingService.ResetExternalProfilingOptions(options, true);
3065
Sadik Armagan3184c902020-03-18 10:57:30 +00003066 // Swap the profiling connection factory in the profiling service instance with our mock one
Jim Flynn34430252022-03-04 15:03:58 +00003067 SwapProfilingConnectionFactoryHelper helper(arm::pipe::MAX_ARMNN_COUNTER, initialiser, profilingService);
Sadik Armagan3184c902020-03-18 10:57:30 +00003068
Matteo Martincigh994b5342019-10-11 17:19:56 +01003069 // Bring the profiling service to the "Active" state
Sadik Armagan1625efc2021-06-10 18:24:34 +01003070 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01003071 profilingService.Update(); // Initialize the counter directory
Sadik Armagan1625efc2021-06-10 18:24:34 +01003072 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01003073 profilingService.Update(); // Create the profiling connection
Sadik Armagan1625efc2021-06-10 18:24:34 +01003074 CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01003075 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincigh994b5342019-10-11 17:19:56 +01003076
Colm Donelan2ba48d22019-11-29 09:10:59 +00003077 // Get the mock profiling connection
3078 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003079 CHECK(mockProfilingConnection);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003080
Matteo Martincigh994b5342019-10-11 17:19:56 +01003081 // Wait for the Stream Metadata packet the be sent
3082 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Finn Williams09ad6f92019-12-19 17:05:18 +00003083 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData);
Matteo Martincigh994b5342019-10-11 17:19:56 +01003084
3085 // Force the profiling service to the "Active" state
3086 helper.ForceTransitionToState(ProfilingState::Active);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003087 CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincigh994b5342019-10-11 17:19:56 +01003088
Matteo Martincigh994b5342019-10-11 17:19:56 +01003089 // Write a "Per-Job Counter Selection" packet into the mock profiling connection, to simulate an input from an
3090 // external profiling service
3091
3092 // Per-Job Counter Selection packet header:
3093 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
3094 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
3095 // 8:15 [8] reserved: Reserved, value 0b00000000
3096 // 0:7 [8] reserved: Reserved, value 0b00000000
3097 uint32_t packetFamily = 0;
3098 uint32_t packetId = 5;
Keith Davis3201eea2019-10-24 17:30:41 +01003099 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincigh994b5342019-10-11 17:19:56 +01003100
3101 // Create the Per-Job Counter Selection packet
Jim Flynnbbfe6032020-07-20 16:57:44 +01003102 // Length == 0, this will disable the collection of counters
3103 arm::pipe::Packet periodicCounterSelectionPacket(header);
Matteo Martincigh994b5342019-10-11 17:19:56 +01003104
3105 // Write the packet to the mock profiling connection
3106 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
3107
3108 // Wait for a bit (must at least be the delay value of the mock profiling connection) to make sure that
3109 // the Per-Job Counter Selection packet gets processed by the profiling service
Colm Donelan2ba48d22019-11-29 09:10:59 +00003110 std::this_thread::sleep_for(std::chrono::milliseconds(5));
Matteo Martincigh994b5342019-10-11 17:19:56 +01003111
Matteo Martincigh994b5342019-10-11 17:19:56 +01003112 // The Per-Job Counter Selection Command Handler should not have updated the profiling state
Sadik Armagan1625efc2021-06-10 18:24:34 +01003113 CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincigh994b5342019-10-11 17:19:56 +01003114
Finn Williams09ad6f92019-12-19 17:05:18 +00003115 // The Per-Job Counter Selection packets are dropped silently, so there should be no reply coming
3116 // from the profiling service
3117 const auto StreamMetaDataSize = static_cast<unsigned long>(
3118 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData, 0, 0));
Sadik Armagan1625efc2021-06-10 18:24:34 +01003119 CHECK(StreamMetaDataSize == mockProfilingConnection->GetWrittenDataSize());
Finn Williams09ad6f92019-12-19 17:05:18 +00003120
Matteo Martincigh994b5342019-10-11 17:19:56 +01003121 // Reset the profiling service to stop any running thread
3122 options.m_EnableProfiling = false;
3123 profilingService.ResetExternalProfilingOptions(options, true);
3124}
3125
Sadik Armagan1625efc2021-06-10 18:24:34 +01003126TEST_CASE("CheckConfigureProfilingServiceOn")
Jim Flynn672d06e2019-10-15 10:18:11 +01003127{
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003128 ProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01003129 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +00003130 armnn::ArmNNProfilingServiceInitialiser initialiser;
3131 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003132 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Jim Flynn672d06e2019-10-15 10:18:11 +01003133 profilingService.ConfigureProfilingService(options);
3134 // should get as far as NOT_CONNECTED
Sadik Armagan1625efc2021-06-10 18:24:34 +01003135 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Jim Flynn672d06e2019-10-15 10:18:11 +01003136 // Reset the profiling service to stop any running thread
3137 options.m_EnableProfiling = false;
3138 profilingService.ResetExternalProfilingOptions(options, true);
3139}
3140
Sadik Armagan1625efc2021-06-10 18:24:34 +01003141TEST_CASE("CheckConfigureProfilingServiceOff")
Jim Flynn672d06e2019-10-15 10:18:11 +01003142{
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003143 ProfilingOptions options;
Jim Flynn34430252022-03-04 15:03:58 +00003144 armnn::ArmNNProfilingServiceInitialiser initialiser;
3145 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003146 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Jim Flynn672d06e2019-10-15 10:18:11 +01003147 profilingService.ConfigureProfilingService(options);
3148 // should not move from Uninitialised
Sadik Armagan1625efc2021-06-10 18:24:34 +01003149 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Jim Flynn672d06e2019-10-15 10:18:11 +01003150 // Reset the profiling service to stop any running thread
3151 options.m_EnableProfiling = false;
3152 profilingService.ResetExternalProfilingOptions(options, true);
3153}
3154
Sadik Armagan1625efc2021-06-10 18:24:34 +01003155TEST_CASE("CheckProfilingServiceEnabled")
Colm Donelan2ba48d22019-11-29 09:10:59 +00003156{
3157 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
Jim Flynn6c9f17d2022-03-10 23:13:01 +00003158 LogLevelSwapper logLevelSwapper(arm::pipe::LogSeverity::Warning);
3159
3160 // Redirect the output to a local stream so that we can parse the warning message
3161 std::stringstream ss;
3162 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3163
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003164 ProfilingOptions options;
Colm Donelan2ba48d22019-11-29 09:10:59 +00003165 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +00003166 armnn::ArmNNProfilingServiceInitialiser initialiser;
3167 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003168 profilingService.ResetExternalProfilingOptions(options, true);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003169 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003170 profilingService.Update();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003171 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003172
Colm Donelan2ba48d22019-11-29 09:10:59 +00003173 profilingService.Update();
Finn Williams09ad6f92019-12-19 17:05:18 +00003174
3175 // Reset the profiling service to stop any running thread
3176 options.m_EnableProfiling = false;
3177 profilingService.ResetExternalProfilingOptions(options, true);
3178
Colm Donelan2ba48d22019-11-29 09:10:59 +00003179 streamRedirector.CancelRedirect();
3180
3181 // Check that the expected error has occurred and logged to the standard output
David Monahana8837bf2020-04-16 10:01:56 +01003182 if (ss.str().find("Cannot connect to stream socket: Connection refused") == std::string::npos)
Colm Donelan2ba48d22019-11-29 09:10:59 +00003183 {
3184 std::cout << ss.str();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003185 FAIL("Expected string not found.");
Colm Donelan2ba48d22019-11-29 09:10:59 +00003186 }
Colm Donelan2ba48d22019-11-29 09:10:59 +00003187}
3188
Sadik Armagan1625efc2021-06-10 18:24:34 +01003189TEST_CASE("CheckProfilingServiceEnabledRuntime")
Colm Donelan2ba48d22019-11-29 09:10:59 +00003190{
3191 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
Jim Flynn6c9f17d2022-03-10 23:13:01 +00003192 LogLevelSwapper logLevelSwapper(arm::pipe::LogSeverity::Warning);
3193
3194 // Redirect the output to a local stream so that we can parse the warning message
3195 std::stringstream ss;
3196 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3197
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003198 ProfilingOptions options;
Jim Flynn34430252022-03-04 15:03:58 +00003199 armnn::ArmNNProfilingServiceInitialiser initialiser;
3200 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003201 profilingService.ResetExternalProfilingOptions(options, true);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003202 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003203 profilingService.Update();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003204 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003205 options.m_EnableProfiling = true;
3206 profilingService.ResetExternalProfilingOptions(options);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003207 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003208 profilingService.Update();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003209 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003210
Colm Donelan2ba48d22019-11-29 09:10:59 +00003211 profilingService.Update();
3212
Finn Williams09ad6f92019-12-19 17:05:18 +00003213 // Reset the profiling service to stop any running thread
3214 options.m_EnableProfiling = false;
3215 profilingService.ResetExternalProfilingOptions(options, true);
3216
Colm Donelan2ba48d22019-11-29 09:10:59 +00003217 streamRedirector.CancelRedirect();
3218
3219 // Check that the expected error has occurred and logged to the standard output
David Monahana8837bf2020-04-16 10:01:56 +01003220 if (ss.str().find("Cannot connect to stream socket: Connection refused") == std::string::npos)
Colm Donelan2ba48d22019-11-29 09:10:59 +00003221 {
3222 std::cout << ss.str();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003223 FAIL("Expected string not found.");
Colm Donelan2ba48d22019-11-29 09:10:59 +00003224 }
Colm Donelan2ba48d22019-11-29 09:10:59 +00003225}
3226
Sadik Armagan1625efc2021-06-10 18:24:34 +01003227TEST_CASE("CheckProfilingServiceBadConnectionAcknowledgedPacket")
Colm Donelan2ba48d22019-11-29 09:10:59 +00003228{
3229 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
Jim Flynn6c9f17d2022-03-10 23:13:01 +00003230 LogLevelSwapper logLevelSwapper(arm::pipe::LogSeverity::Warning);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003231
3232 // Redirect the standard output to a local stream so that we can parse the warning message
3233 std::stringstream ss;
3234 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3235
Colm Donelan2ba48d22019-11-29 09:10:59 +00003236 // Reset the profiling service to the uninitialized state
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003237 ProfilingOptions options;
Colm Donelan2ba48d22019-11-29 09:10:59 +00003238 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +00003239 armnn::ArmNNProfilingServiceInitialiser initialiser;
3240 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003241 profilingService.ResetExternalProfilingOptions(options, true);
3242
Sadik Armagan3184c902020-03-18 10:57:30 +00003243 // Swap the profiling connection factory in the profiling service instance with our mock one
Jim Flynn34430252022-03-04 15:03:58 +00003244 SwapProfilingConnectionFactoryHelper helper(arm::pipe::MAX_ARMNN_COUNTER, initialiser, profilingService);
Sadik Armagan3184c902020-03-18 10:57:30 +00003245
Colm Donelan2ba48d22019-11-29 09:10:59 +00003246 // Bring the profiling service to the "WaitingForAck" state
Sadik Armagan1625efc2021-06-10 18:24:34 +01003247 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003248 profilingService.Update(); // Initialize the counter directory
Sadik Armagan1625efc2021-06-10 18:24:34 +01003249 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003250 profilingService.Update(); // Create the profiling connection
3251
3252 // Get the mock profiling connection
3253 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003254 CHECK(mockProfilingConnection);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003255
Sadik Armagan1625efc2021-06-10 18:24:34 +01003256 CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003257
3258 // Connection Acknowledged Packet header (word 0, word 1 is always zero):
3259 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
3260 // 16:25 [10] packet_id: Packet identifier, value 0b0000000001
3261 // 8:15 [8] reserved: Reserved, value 0b00000000
3262 // 0:7 [8] reserved: Reserved, value 0b00000000
3263 uint32_t packetFamily = 0;
3264 uint32_t packetId = 37; // Wrong packet id!!!
3265 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
3266
3267 // Create the Connection Acknowledged Packet
Jim Flynnbbfe6032020-07-20 16:57:44 +01003268 arm::pipe::Packet connectionAcknowledgedPacket(header);
Finn Williams09ad6f92019-12-19 17:05:18 +00003269 // Write an invalid "Connection Acknowledged" packet into the mock profiling connection, to simulate an invalid
3270 // reply from an external profiling service
Colm Donelan2ba48d22019-11-29 09:10:59 +00003271 mockProfilingConnection->WritePacket(std::move(connectionAcknowledgedPacket));
3272
Finn Williams09ad6f92019-12-19 17:05:18 +00003273 // Start the command thread
3274 profilingService.Update();
3275
3276 // Wait for the command thread to join
3277 options.m_EnableProfiling = false;
3278 profilingService.ResetExternalProfilingOptions(options, true);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003279
3280 streamRedirector.CancelRedirect();
3281
3282 // Check that the expected error has occurred and logged to the standard output
David Monahana8837bf2020-04-16 10:01:56 +01003283 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 +00003284 {
3285 std::cout << ss.str();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003286 FAIL("Expected string not found.");
Colm Donelan2ba48d22019-11-29 09:10:59 +00003287 }
Colm Donelan2ba48d22019-11-29 09:10:59 +00003288}
3289
Sadik Armagan1625efc2021-06-10 18:24:34 +01003290TEST_CASE("CheckProfilingServiceBadRequestCounterDirectoryPacket")
Colm Donelan2ba48d22019-11-29 09:10:59 +00003291{
3292 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
Jim Flynn6c9f17d2022-03-10 23:13:01 +00003293 LogLevelSwapper logLevelSwapper(arm::pipe::LogSeverity::Warning);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003294
3295 // Redirect the standard output to a local stream so that we can parse the warning message
3296 std::stringstream ss;
3297 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3298
3299 // Reset the profiling service to the uninitialized state
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003300 ProfilingOptions options;
Colm Donelan2ba48d22019-11-29 09:10:59 +00003301 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +00003302 armnn::ArmNNProfilingServiceInitialiser initialiser;
3303 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003304 profilingService.ResetExternalProfilingOptions(options, true);
3305
Sadik Armagan3184c902020-03-18 10:57:30 +00003306 // Swap the profiling connection factory in the profiling service instance with our mock one
Jim Flynn34430252022-03-04 15:03:58 +00003307 SwapProfilingConnectionFactoryHelper helper(arm::pipe::MAX_ARMNN_COUNTER, initialiser, profilingService);
Sadik Armagan3184c902020-03-18 10:57:30 +00003308
Colm Donelan2ba48d22019-11-29 09:10:59 +00003309 // Bring the profiling service to the "Active" state
Sadik Armagan1625efc2021-06-10 18:24:34 +01003310 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003311 helper.ForceTransitionToState(ProfilingState::NotConnected);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003312 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003313 profilingService.Update(); // Create the profiling connection
Sadik Armagan1625efc2021-06-10 18:24:34 +01003314 CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003315
3316 // Get the mock profiling connection
3317 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003318 CHECK(mockProfilingConnection);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003319
Colm Donelan2ba48d22019-11-29 09:10:59 +00003320 // Write a valid "Request Counter Directory" packet into the mock profiling connection, to simulate a valid
3321 // reply from an external profiling service
3322
3323 // Request Counter Directory packet header (word 0, word 1 is always zero):
3324 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
3325 // 16:25 [10] packet_id: Packet identifier, value 0b0000000011
3326 // 8:15 [8] reserved: Reserved, value 0b00000000
3327 // 0:7 [8] reserved: Reserved, value 0b00000000
3328 uint32_t packetFamily = 0;
3329 uint32_t packetId = 123; // Wrong packet id!!!
3330 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
3331
3332 // Create the Request Counter Directory packet
Jim Flynnbbfe6032020-07-20 16:57:44 +01003333 arm::pipe::Packet requestCounterDirectoryPacket(header);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003334
3335 // Write the packet to the mock profiling connection
3336 mockProfilingConnection->WritePacket(std::move(requestCounterDirectoryPacket));
3337
Finn Williams09ad6f92019-12-19 17:05:18 +00003338 // Start the command handler and the send thread
3339 profilingService.Update();
3340
3341 // Reset the profiling service to stop and join any running thread
3342 options.m_EnableProfiling = false;
3343 profilingService.ResetExternalProfilingOptions(options, true);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003344
3345 streamRedirector.CancelRedirect();
3346
3347 // Check that the expected error has occurred and logged to the standard output
David Monahana8837bf2020-04-16 10:01:56 +01003348 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 +00003349 {
3350 std::cout << ss.str();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003351 FAIL("Expected string not found.");
Colm Donelan2ba48d22019-11-29 09:10:59 +00003352 }
Colm Donelan2ba48d22019-11-29 09:10:59 +00003353}
3354
Sadik Armagan1625efc2021-06-10 18:24:34 +01003355TEST_CASE("CheckProfilingServiceBadPeriodicCounterSelectionPacket")
Colm Donelan2ba48d22019-11-29 09:10:59 +00003356{
3357 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
Jim Flynn6c9f17d2022-03-10 23:13:01 +00003358 LogLevelSwapper logLevelSwapper(arm::pipe::LogSeverity::Warning);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003359
3360 // Redirect the standard output to a local stream so that we can parse the warning message
3361 std::stringstream ss;
3362 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3363
3364 // Reset the profiling service to the uninitialized state
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003365 ProfilingOptions options;
Colm Donelan2ba48d22019-11-29 09:10:59 +00003366 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +00003367 armnn::ArmNNProfilingServiceInitialiser initialiser;
3368 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003369 profilingService.ResetExternalProfilingOptions(options, true);
3370
Sadik Armagan3184c902020-03-18 10:57:30 +00003371 // Swap the profiling connection factory in the profiling service instance with our mock one
Jim Flynn34430252022-03-04 15:03:58 +00003372 SwapProfilingConnectionFactoryHelper helper(arm::pipe::MAX_ARMNN_COUNTER, initialiser, profilingService);
Sadik Armagan3184c902020-03-18 10:57:30 +00003373
Colm Donelan2ba48d22019-11-29 09:10:59 +00003374 // Bring the profiling service to the "Active" state
Sadik Armagan1625efc2021-06-10 18:24:34 +01003375 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003376 profilingService.Update(); // Initialize the counter directory
Sadik Armagan1625efc2021-06-10 18:24:34 +01003377 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003378 profilingService.Update(); // Create the profiling connection
Sadik Armagan1625efc2021-06-10 18:24:34 +01003379 CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003380 profilingService.Update(); // Start the command handler and the send thread
3381
3382 // Get the mock profiling connection
3383 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003384 CHECK(mockProfilingConnection);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003385
Colm Donelan2ba48d22019-11-29 09:10:59 +00003386 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
3387 // external profiling service
3388
3389 // Periodic Counter Selection packet header:
3390 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
3391 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
3392 // 8:15 [8] reserved: Reserved, value 0b00000000
3393 // 0:7 [8] reserved: Reserved, value 0b00000000
3394 uint32_t packetFamily = 0;
3395 uint32_t packetId = 999; // Wrong packet id!!!
3396 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
3397
3398 // Create the Periodic Counter Selection packet
Jim Flynnbbfe6032020-07-20 16:57:44 +01003399 // Length == 0, this will disable the collection of counters
3400 arm::pipe::Packet periodicCounterSelectionPacket(header);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003401
3402 // Write the packet to the mock profiling connection
3403 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
Finn Williams09ad6f92019-12-19 17:05:18 +00003404 profilingService.Update();
Colm Donelan2ba48d22019-11-29 09:10:59 +00003405
Finn Williams09ad6f92019-12-19 17:05:18 +00003406 // Reset the profiling service to stop any running thread
3407 options.m_EnableProfiling = false;
3408 profilingService.ResetExternalProfilingOptions(options, true);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003409
3410 // Check that the expected error has occurred and logged to the standard output
3411 streamRedirector.CancelRedirect();
3412
3413 // Check that the expected error has occurred and logged to the standard output
David Monahana8837bf2020-04-16 10:01:56 +01003414 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 +00003415 {
3416 std::cout << ss.str();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003417 FAIL("Expected string not found.");
Colm Donelan2ba48d22019-11-29 09:10:59 +00003418 }
Colm Donelan2ba48d22019-11-29 09:10:59 +00003419}
Jim Flynn97897022020-02-02 12:52:59 +00003420
Sadik Armagan1625efc2021-06-10 18:24:34 +01003421TEST_CASE("CheckCounterIdMap")
David Monahande803072020-01-30 12:44:23 +00003422{
3423 CounterIdMap counterIdMap;
Jim Flynnf9db3ef2022-03-08 21:23:44 +00003424 CHECK_THROWS_AS(counterIdMap.GetBackendId(0), arm::pipe::ProfilingException);
3425 CHECK_THROWS_AS(counterIdMap.GetGlobalId(0, armnn::profiling::BACKEND_ID), arm::pipe::ProfilingException);
David Monahande803072020-01-30 12:44:23 +00003426
3427 uint16_t globalCounterIds = 0;
3428
Cathal Corbett6f073722022-03-04 12:11:09 +00003429 std::string cpuRefId(GetComputeDeviceAsCString(armnn::Compute::CpuRef));
3430 std::string cpuAccId(GetComputeDeviceAsCString(armnn::Compute::CpuAcc));
David Monahande803072020-01-30 12:44:23 +00003431
3432 std::vector<uint16_t> cpuRefCounters = {0, 1, 2, 3};
3433 std::vector<uint16_t> cpuAccCounters = {0, 1};
3434
3435 for (uint16_t backendCounterId : cpuRefCounters)
3436 {
3437 counterIdMap.RegisterMapping(globalCounterIds, backendCounterId, cpuRefId);
3438 ++globalCounterIds;
3439 }
3440 for (uint16_t backendCounterId : cpuAccCounters)
3441 {
3442 counterIdMap.RegisterMapping(globalCounterIds, backendCounterId, cpuAccId);
3443 ++globalCounterIds;
3444 }
3445
Cathal Corbett6f073722022-03-04 12:11:09 +00003446 CHECK(counterIdMap.GetBackendId(0) == (std::pair<uint16_t, std::string>(0, cpuRefId)));
3447 CHECK(counterIdMap.GetBackendId(1) == (std::pair<uint16_t, std::string>(1, cpuRefId)));
3448 CHECK(counterIdMap.GetBackendId(2) == (std::pair<uint16_t, std::string>(2, cpuRefId)));
3449 CHECK(counterIdMap.GetBackendId(3) == (std::pair<uint16_t, std::string>(3, cpuRefId)));
3450 CHECK(counterIdMap.GetBackendId(4) == (std::pair<uint16_t, std::string>(0, cpuAccId)));
3451 CHECK(counterIdMap.GetBackendId(5) == (std::pair<uint16_t, std::string>(1, cpuAccId)));
David Monahande803072020-01-30 12:44:23 +00003452
Sadik Armagan1625efc2021-06-10 18:24:34 +01003453 CHECK(counterIdMap.GetGlobalId(0, cpuRefId) == 0);
3454 CHECK(counterIdMap.GetGlobalId(1, cpuRefId) == 1);
3455 CHECK(counterIdMap.GetGlobalId(2, cpuRefId) == 2);
3456 CHECK(counterIdMap.GetGlobalId(3, cpuRefId) == 3);
3457 CHECK(counterIdMap.GetGlobalId(0, cpuAccId) == 4);
3458 CHECK(counterIdMap.GetGlobalId(1, cpuAccId) == 5);
David Monahande803072020-01-30 12:44:23 +00003459}
Colm Donelan2ba48d22019-11-29 09:10:59 +00003460
Sadik Armagan1625efc2021-06-10 18:24:34 +01003461TEST_CASE("CheckRegisterBackendCounters")
Jim Flynn97897022020-02-02 12:52:59 +00003462{
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00003463 uint16_t globalCounterIds = INFERENCES_RUN;
Cathal Corbett6f073722022-03-04 12:11:09 +00003464 std::string cpuRefId(GetComputeDeviceAsCString(armnn::Compute::CpuRef));
Jim Flynn97897022020-02-02 12:52:59 +00003465
Jim Flynn97897022020-02-02 12:52:59 +00003466 // Reset the profiling service to the uninitialized state
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003467 ProfilingOptions options;
Jim Flynn97897022020-02-02 12:52:59 +00003468 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +00003469 armnn::ArmNNProfilingServiceInitialiser initialiser;
3470 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Jim Flynn97897022020-02-02 12:52:59 +00003471 profilingService.ResetExternalProfilingOptions(options, true);
3472
Sadik Armagan3184c902020-03-18 10:57:30 +00003473 RegisterBackendCounters registerBackendCounters(globalCounterIds, cpuRefId, profilingService);
3474
3475
3476
Sadik Armagan1625efc2021-06-10 18:24:34 +01003477 CHECK(profilingService.GetCounterDirectory().GetCategories().empty());
Jim Flynn97897022020-02-02 12:52:59 +00003478 registerBackendCounters.RegisterCategory("categoryOne");
3479 auto categoryOnePtr = profilingService.GetCounterDirectory().GetCategory("categoryOne");
Sadik Armagan1625efc2021-06-10 18:24:34 +01003480 CHECK(categoryOnePtr);
Jim Flynn97897022020-02-02 12:52:59 +00003481
Sadik Armagan1625efc2021-06-10 18:24:34 +01003482 CHECK(profilingService.GetCounterDirectory().GetDevices().empty());
Jim Flynn97897022020-02-02 12:52:59 +00003483 globalCounterIds = registerBackendCounters.RegisterDevice("deviceOne");
3484 auto deviceOnePtr = profilingService.GetCounterDirectory().GetDevice(globalCounterIds);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003485 CHECK(deviceOnePtr);
3486 CHECK(deviceOnePtr->m_Name == "deviceOne");
Jim Flynn97897022020-02-02 12:52:59 +00003487
Sadik Armagan1625efc2021-06-10 18:24:34 +01003488 CHECK(profilingService.GetCounterDirectory().GetCounterSets().empty());
Jim Flynn97897022020-02-02 12:52:59 +00003489 globalCounterIds = registerBackendCounters.RegisterCounterSet("counterSetOne");
3490 auto counterSetOnePtr = profilingService.GetCounterDirectory().GetCounterSet(globalCounterIds);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003491 CHECK(counterSetOnePtr);
3492 CHECK(counterSetOnePtr->m_Name == "counterSetOne");
Jim Flynn97897022020-02-02 12:52:59 +00003493
3494 uint16_t newGlobalCounterId = registerBackendCounters.RegisterCounter(0,
3495 "categoryOne",
3496 0,
3497 0,
3498 1.f,
3499 "CounterOne",
3500 "first test counter");
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00003501 CHECK((newGlobalCounterId = INFERENCES_RUN + 1));
Jim Flynn97897022020-02-02 12:52:59 +00003502 uint16_t mappedGlobalId = profilingService.GetCounterMappings().GetGlobalId(0, cpuRefId);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003503 CHECK(mappedGlobalId == newGlobalCounterId);
Jim Flynn97897022020-02-02 12:52:59 +00003504 auto backendMapping = profilingService.GetCounterMappings().GetBackendId(newGlobalCounterId);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003505 CHECK(backendMapping.first == 0);
3506 CHECK(backendMapping.second == cpuRefId);
Jim Flynn97897022020-02-02 12:52:59 +00003507
3508 // Reset the profiling service to stop any running thread
3509 options.m_EnableProfiling = false;
3510 profilingService.ResetExternalProfilingOptions(options, true);
3511}
3512
Sadik Armagan1625efc2021-06-10 18:24:34 +01003513TEST_CASE("CheckCounterStatusQuery")
James Conroy2dcd3fe2020-02-06 18:34:52 +00003514{
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003515 ProfilingOptions options;
3516 options.m_EnableProfiling = true;
James Conroy2dcd3fe2020-02-06 18:34:52 +00003517
3518 // Reset the profiling service to the uninitialized state
Jim Flynn34430252022-03-04 15:03:58 +00003519 armnn::ArmNNProfilingServiceInitialiser initialiser;
3520 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003521 profilingService.ResetExternalProfilingOptions(options, true);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003522
Cathal Corbett6f073722022-03-04 12:11:09 +00003523 const std::string cpuRefId(GetComputeDeviceAsCString(armnn::Compute::CpuRef));
3524 const std::string cpuAccId(GetComputeDeviceAsCString(armnn::Compute::CpuAcc));
James Conroy2dcd3fe2020-02-06 18:34:52 +00003525
3526 // Create BackendProfiling for each backend
3527 BackendProfiling backendProfilingCpuRef(options, profilingService, cpuRefId);
3528 BackendProfiling backendProfilingCpuAcc(options, profilingService, cpuAccId);
3529
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00003530 uint16_t initialNumGlobalCounterIds = INFERENCES_RUN;
James Conroy2dcd3fe2020-02-06 18:34:52 +00003531
3532 // Create RegisterBackendCounters for CpuRef
Sadik Armagan3184c902020-03-18 10:57:30 +00003533 RegisterBackendCounters registerBackendCountersCpuRef(initialNumGlobalCounterIds, cpuRefId, profilingService);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003534
3535 // Create 'testCategory' in CounterDirectory (backend agnostic)
Sadik Armagan1625efc2021-06-10 18:24:34 +01003536 CHECK(profilingService.GetCounterDirectory().GetCategories().empty());
James Conroy2dcd3fe2020-02-06 18:34:52 +00003537 registerBackendCountersCpuRef.RegisterCategory("testCategory");
3538 auto categoryOnePtr = profilingService.GetCounterDirectory().GetCategory("testCategory");
Sadik Armagan1625efc2021-06-10 18:24:34 +01003539 CHECK(categoryOnePtr);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003540
3541 // Counters:
3542 // Global | Local | Backend
3543 // 5 | 0 | CpuRef
3544 // 6 | 1 | CpuRef
3545 // 7 | 1 | CpuAcc
3546
3547 std::vector<uint16_t> cpuRefCounters = {0, 1};
3548 std::vector<uint16_t> cpuAccCounters = {0};
3549
3550 // Register the backend counters for CpuRef and validate GetGlobalId and GetBackendId
3551 uint16_t currentNumGlobalCounterIds = registerBackendCountersCpuRef.RegisterCounter(
3552 0, "testCategory", 0, 0, 1.f, "CpuRefCounter0", "Zeroth CpuRef Counter");
Sadik Armagan1625efc2021-06-10 18:24:34 +01003553 CHECK(currentNumGlobalCounterIds == initialNumGlobalCounterIds + 1);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003554 uint16_t mappedGlobalId = profilingService.GetCounterMappings().GetGlobalId(0, cpuRefId);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003555 CHECK(mappedGlobalId == currentNumGlobalCounterIds);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003556 auto backendMapping = profilingService.GetCounterMappings().GetBackendId(currentNumGlobalCounterIds);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003557 CHECK(backendMapping.first == 0);
3558 CHECK(backendMapping.second == cpuRefId);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003559
3560 currentNumGlobalCounterIds = registerBackendCountersCpuRef.RegisterCounter(
3561 1, "testCategory", 0, 0, 1.f, "CpuRefCounter1", "First CpuRef Counter");
Sadik Armagan1625efc2021-06-10 18:24:34 +01003562 CHECK(currentNumGlobalCounterIds == initialNumGlobalCounterIds + 2);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003563 mappedGlobalId = profilingService.GetCounterMappings().GetGlobalId(1, cpuRefId);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003564 CHECK(mappedGlobalId == currentNumGlobalCounterIds);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003565 backendMapping = profilingService.GetCounterMappings().GetBackendId(currentNumGlobalCounterIds);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003566 CHECK(backendMapping.first == 1);
3567 CHECK(backendMapping.second == cpuRefId);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003568
3569 // Create RegisterBackendCounters for CpuAcc
Sadik Armagan3184c902020-03-18 10:57:30 +00003570 RegisterBackendCounters registerBackendCountersCpuAcc(currentNumGlobalCounterIds, cpuAccId, profilingService);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003571
3572 // Register the backend counter for CpuAcc and validate GetGlobalId and GetBackendId
3573 currentNumGlobalCounterIds = registerBackendCountersCpuAcc.RegisterCounter(
3574 0, "testCategory", 0, 0, 1.f, "CpuAccCounter0", "Zeroth CpuAcc Counter");
Sadik Armagan1625efc2021-06-10 18:24:34 +01003575 CHECK(currentNumGlobalCounterIds == initialNumGlobalCounterIds + 3);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003576 mappedGlobalId = profilingService.GetCounterMappings().GetGlobalId(0, cpuAccId);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003577 CHECK(mappedGlobalId == currentNumGlobalCounterIds);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003578 backendMapping = profilingService.GetCounterMappings().GetBackendId(currentNumGlobalCounterIds);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003579 CHECK(backendMapping.first == 0);
3580 CHECK(backendMapping.second == cpuAccId);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003581
3582 // Create vectors for active counters
3583 const std::vector<uint16_t> activeGlobalCounterIds = {5}; // CpuRef(0) activated
3584 const std::vector<uint16_t> newActiveGlobalCounterIds = {6, 7}; // CpuRef(0) and CpuAcc(1) activated
3585
3586 const uint32_t capturePeriod = 200;
3587 const uint32_t newCapturePeriod = 100;
3588
3589 // Set capture period and active counters in CaptureData
Finn Williams032bc742020-02-12 11:02:34 +00003590 profilingService.SetCaptureData(capturePeriod, activeGlobalCounterIds, {});
James Conroy2dcd3fe2020-02-06 18:34:52 +00003591
3592 // Get vector of active counters for CpuRef and CpuAcc backends
3593 std::vector<CounterStatus> cpuRefCounterStatus = backendProfilingCpuRef.GetActiveCounters();
3594 std::vector<CounterStatus> cpuAccCounterStatus = backendProfilingCpuAcc.GetActiveCounters();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003595 CHECK_EQ(cpuRefCounterStatus.size(), 1);
3596 CHECK_EQ(cpuAccCounterStatus.size(), 0);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003597
3598 // Check active CpuRef counter
Sadik Armagan1625efc2021-06-10 18:24:34 +01003599 CHECK_EQ(cpuRefCounterStatus[0].m_GlobalCounterId, activeGlobalCounterIds[0]);
3600 CHECK_EQ(cpuRefCounterStatus[0].m_BackendCounterId, cpuRefCounters[0]);
3601 CHECK_EQ(cpuRefCounterStatus[0].m_SamplingRateInMicroseconds, capturePeriod);
3602 CHECK_EQ(cpuRefCounterStatus[0].m_Enabled, true);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003603
3604 // Check inactive CpuRef counter
3605 CounterStatus inactiveCpuRefCounter = backendProfilingCpuRef.GetCounterStatus(cpuRefCounters[1]);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003606 CHECK_EQ(inactiveCpuRefCounter.m_GlobalCounterId, 6);
3607 CHECK_EQ(inactiveCpuRefCounter.m_BackendCounterId, cpuRefCounters[1]);
3608 CHECK_EQ(inactiveCpuRefCounter.m_SamplingRateInMicroseconds, 0);
3609 CHECK_EQ(inactiveCpuRefCounter.m_Enabled, false);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003610
3611 // Check inactive CpuAcc counter
3612 CounterStatus inactiveCpuAccCounter = backendProfilingCpuAcc.GetCounterStatus(cpuAccCounters[0]);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003613 CHECK_EQ(inactiveCpuAccCounter.m_GlobalCounterId, 7);
3614 CHECK_EQ(inactiveCpuAccCounter.m_BackendCounterId, cpuAccCounters[0]);
3615 CHECK_EQ(inactiveCpuAccCounter.m_SamplingRateInMicroseconds, 0);
3616 CHECK_EQ(inactiveCpuAccCounter.m_Enabled, false);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003617
3618 // Set new capture period and new active counters in CaptureData
Finn Williams032bc742020-02-12 11:02:34 +00003619 profilingService.SetCaptureData(newCapturePeriod, newActiveGlobalCounterIds, {});
James Conroy2dcd3fe2020-02-06 18:34:52 +00003620
3621 // Get vector of active counters for CpuRef and CpuAcc backends
3622 cpuRefCounterStatus = backendProfilingCpuRef.GetActiveCounters();
3623 cpuAccCounterStatus = backendProfilingCpuAcc.GetActiveCounters();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003624 CHECK_EQ(cpuRefCounterStatus.size(), 1);
3625 CHECK_EQ(cpuAccCounterStatus.size(), 1);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003626
3627 // Check active CpuRef counter
Sadik Armagan1625efc2021-06-10 18:24:34 +01003628 CHECK_EQ(cpuRefCounterStatus[0].m_GlobalCounterId, newActiveGlobalCounterIds[0]);
3629 CHECK_EQ(cpuRefCounterStatus[0].m_BackendCounterId, cpuRefCounters[1]);
3630 CHECK_EQ(cpuRefCounterStatus[0].m_SamplingRateInMicroseconds, newCapturePeriod);
3631 CHECK_EQ(cpuRefCounterStatus[0].m_Enabled, true);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003632
3633 // Check active CpuAcc counter
Sadik Armagan1625efc2021-06-10 18:24:34 +01003634 CHECK_EQ(cpuAccCounterStatus[0].m_GlobalCounterId, newActiveGlobalCounterIds[1]);
3635 CHECK_EQ(cpuAccCounterStatus[0].m_BackendCounterId, cpuAccCounters[0]);
3636 CHECK_EQ(cpuAccCounterStatus[0].m_SamplingRateInMicroseconds, newCapturePeriod);
3637 CHECK_EQ(cpuAccCounterStatus[0].m_Enabled, true);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003638
3639 // Check inactive CpuRef counter
3640 inactiveCpuRefCounter = backendProfilingCpuRef.GetCounterStatus(cpuRefCounters[0]);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003641 CHECK_EQ(inactiveCpuRefCounter.m_GlobalCounterId, 5);
3642 CHECK_EQ(inactiveCpuRefCounter.m_BackendCounterId, cpuRefCounters[0]);
3643 CHECK_EQ(inactiveCpuRefCounter.m_SamplingRateInMicroseconds, 0);
3644 CHECK_EQ(inactiveCpuRefCounter.m_Enabled, false);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003645
3646 // Reset the profiling service to stop any running thread
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003647 options.m_EnableProfiling = false;
3648 profilingService.ResetExternalProfilingOptions(options, true);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003649}
3650
Sadik Armagan1625efc2021-06-10 18:24:34 +01003651TEST_CASE("CheckRegisterCounters")
Sadik Armagancab588a2020-02-17 11:33:31 +00003652{
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003653 ProfilingOptions options;
3654 options.m_EnableProfiling = true;
Sadik Armagancab588a2020-02-17 11:33:31 +00003655 MockBufferManager mockBuffer(1024);
Sadik Armagan3184c902020-03-18 10:57:30 +00003656
Sadik Armagancab588a2020-02-17 11:33:31 +00003657 CaptureData captureData;
Jim Flynn34430252022-03-04 15:03:58 +00003658
3659 armnn::ArmNNProfilingServiceInitialiser initialiser;
3660 MockProfilingService mockProfilingService(
3661 arm::pipe::MAX_ARMNN_COUNTER, initialiser, mockBuffer, options.m_EnableProfiling, captureData);
Cathal Corbett6f073722022-03-04 12:11:09 +00003662 std::string cpuRefId(GetComputeDeviceAsCString(armnn::Compute::CpuRef));
Sadik Armagancab588a2020-02-17 11:33:31 +00003663
3664 mockProfilingService.RegisterMapping(6, 0, cpuRefId);
3665 mockProfilingService.RegisterMapping(7, 1, cpuRefId);
3666 mockProfilingService.RegisterMapping(8, 2, cpuRefId);
3667
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00003668 BackendProfiling backendProfiling(options,
Sadik Armagancab588a2020-02-17 11:33:31 +00003669 mockProfilingService,
3670 cpuRefId);
3671
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00003672 Timestamp timestamp;
Sadik Armagancab588a2020-02-17 11:33:31 +00003673 timestamp.timestamp = 1000998;
3674 timestamp.counterValues.emplace_back(0, 700);
3675 timestamp.counterValues.emplace_back(2, 93);
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00003676 std::vector<Timestamp> timestamps;
Sadik Armagancab588a2020-02-17 11:33:31 +00003677 timestamps.push_back(timestamp);
3678 backendProfiling.ReportCounters(timestamps);
3679
3680 auto readBuffer = mockBuffer.GetReadableBuffer();
3681
3682 uint32_t headerWord0 = ReadUint32(readBuffer, 0);
3683 uint32_t headerWord1 = ReadUint32(readBuffer, 4);
3684 uint64_t readTimestamp = ReadUint64(readBuffer, 8);
3685
Sadik Armagan1625efc2021-06-10 18:24:34 +01003686 CHECK(((headerWord0 >> 26) & 0x0000003F) == 3); // packet family
3687 CHECK(((headerWord0 >> 19) & 0x0000007F) == 0); // packet class
3688 CHECK(((headerWord0 >> 16) & 0x00000007) == 0); // packet type
3689 CHECK(headerWord1 == 20); // data length
3690 CHECK(1000998 == readTimestamp); // capture period
Sadik Armagancab588a2020-02-17 11:33:31 +00003691
3692 uint32_t offset = 16;
3693 // Check Counter Index
3694 uint16_t readIndex = ReadUint16(readBuffer, offset);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003695 CHECK(6 == readIndex);
Sadik Armagancab588a2020-02-17 11:33:31 +00003696
3697 // Check Counter Value
3698 offset += 2;
3699 uint32_t readValue = ReadUint32(readBuffer, offset);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003700 CHECK(700 == readValue);
Sadik Armagancab588a2020-02-17 11:33:31 +00003701
3702 // Check Counter Index
3703 offset += 4;
3704 readIndex = ReadUint16(readBuffer, offset);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003705 CHECK(8 == readIndex);
Sadik Armagancab588a2020-02-17 11:33:31 +00003706
3707 // Check Counter Value
3708 offset += 2;
3709 readValue = ReadUint32(readBuffer, offset);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003710 CHECK(93 == readValue);
Sadik Armagancab588a2020-02-17 11:33:31 +00003711}
3712
Sadik Armagan1625efc2021-06-10 18:24:34 +01003713TEST_CASE("CheckFileFormat") {
Isabella Gottardia0687ee2020-03-11 18:04:20 +00003714 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
Jim Flynn6c9f17d2022-03-10 23:13:01 +00003715 LogLevelSwapper logLevelSwapper(arm::pipe::LogSeverity::Warning);
3716
3717 // Redirect the output to a local stream so that we can parse the warning message
3718 std::stringstream ss;
3719 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
Isabella Gottardia0687ee2020-03-11 18:04:20 +00003720
3721 // Create profiling options.
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003722 ProfilingOptions options;
Isabella Gottardia0687ee2020-03-11 18:04:20 +00003723 options.m_EnableProfiling = true;
3724 // Check the default value set to binary
Sadik Armagan1625efc2021-06-10 18:24:34 +01003725 CHECK(options.m_FileFormat == "binary");
Isabella Gottardia0687ee2020-03-11 18:04:20 +00003726
3727 // Change file format to an unsupported value
3728 options.m_FileFormat = "json";
3729 // Enable the profiling service
Jim Flynn34430252022-03-04 15:03:58 +00003730 armnn::ArmNNProfilingServiceInitialiser initialiser;
3731 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Isabella Gottardia0687ee2020-03-11 18:04:20 +00003732 profilingService.ResetExternalProfilingOptions(options, true);
3733 // Start the command handler and the send thread
3734 profilingService.Update();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003735 CHECK(profilingService.GetCurrentState()==ProfilingState::NotConnected);
Isabella Gottardia0687ee2020-03-11 18:04:20 +00003736
Isabella Gottardia0687ee2020-03-11 18:04:20 +00003737 // When Update is called and the current state is ProfilingState::NotConnected
3738 // an exception will be raised from GetProfilingConnection and displayed as warning in the output local stream
3739 profilingService.Update();
3740
3741 streamRedirector.CancelRedirect();
3742
3743 // Check that the expected error has occurred and logged to the standard output
David Monahana8837bf2020-04-16 10:01:56 +01003744 if (ss.str().find("Unsupported profiling file format, only binary is supported") == std::string::npos)
Isabella Gottardia0687ee2020-03-11 18:04:20 +00003745 {
3746 std::cout << ss.str();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003747 FAIL("Expected string not found.");
Isabella Gottardia0687ee2020-03-11 18:04:20 +00003748 }
3749}
3750
Sadik Armagan1625efc2021-06-10 18:24:34 +01003751}