blob: 22b2ae233a958b96398754704701a2dd488e9846 [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>
David Monahande803072020-01-30 12:44:23 +000012#include <CounterIdMap.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010013#include <Holder.hpp>
Matteo Martincighe0e6efc2019-10-04 17:17:42 +010014#include <ICounterValues.hpp>
Francis Murtaghfcb8ef62019-09-20 15:40:09 +010015#include <PeriodicCounterCapture.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010016#include <PeriodicCounterSelectionCommandHandler.hpp>
17#include <ProfilingStateMachine.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010018#include <ProfilingUtils.hpp>
James Conroy2dcd3fe2020-02-06 18:34:52 +000019#include <RegisterBackendCounters.hpp>
Narumol Prangnawarat48033692019-09-20 12:04:55 +010020#include <RequestCounterDirectoryCommandHandler.hpp>
Teresa Charlin9bab4962019-09-06 12:28:35 +010021#include <Runtime.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010022#include <SocketProfilingConnection.hpp>
Matteo Martincighcdfb9412019-11-08 11:23:06 +000023#include <SendCounterPacket.hpp>
Sadik Armagan3896b472020-02-10 12:24:15 +000024#include <SendThread.hpp>
Matteo Martincighcdfb9412019-11-08 11:23:06 +000025#include <SendTimelinePacket.hpp>
Keith Davis02356de2019-08-26 18:28:17 +010026
Matteo Martincigh54fb9572019-10-02 12:50:57 +010027#include <armnn/Utils.hpp>
Jim Flynn75c14f42022-03-10 22:05:42 +000028
29#include <armnn/profiling/ArmNNProfiling.hpp>
30#include <armnn/profiling/ProfilingOptions.hpp>
31
Jim Flynn75c14f42022-03-10 22:05:42 +000032#include <backends/BackendProfiling.hpp>
Matteo Martincigh54fb9572019-10-02 12:50:57 +010033
Jim Flynnbbfe6032020-07-20 16:57:44 +010034#include <common/include/CommandHandlerKey.hpp>
35#include <common/include/CommandHandlerRegistry.hpp>
Jim Flynnc454ac92022-03-16 18:43:18 +000036#include <common/include/CounterDirectory.hpp>
Jim Flynn75c14f42022-03-10 22:05:42 +000037#include <common/include/EncodeVersion.hpp>
Jim Flynn9265a882022-03-10 23:35:26 +000038#include <common/include/IgnoreUnused.hpp>
Jim Flynn75c14f42022-03-10 22:05:42 +000039#include <common/include/NumericCast.hpp>
Jim Flynnbbfe6032020-07-20 16:57:44 +010040#include <common/include/Packet.hpp>
Jim Flynn75c14f42022-03-10 22:05:42 +000041#include <common/include/PacketVersionResolver.hpp>
42#include <common/include/SocketConnectionException.hpp>
43#include <common/include/SwTrace.hpp>
Jim Flynnbbfe6032020-07-20 16:57:44 +010044
Sadik Armagan1625efc2021-06-10 18:24:34 +010045#include <doctest/doctest.h>
46
Sadik Armagan95e9efc2021-08-05 15:01:07 +010047#include <algorithm>
Nikhil Rajbc626052019-08-15 15:49:45 +010048#include <cstdint>
49#include <cstring>
Keith Davis3201eea2019-10-24 17:30:41 +010050#include <iostream>
Aron Virginas-Tare898db92019-08-22 12:56:34 +010051#include <limits>
Francis Murtagh11f99b42019-08-16 11:28:52 +010052#include <map>
Aron Virginas-Tare898db92019-08-22 12:56:34 +010053#include <random>
James Conroy2dcd3fe2020-02-06 18:34:52 +000054
Francis Murtagh1f7db452019-08-14 09:49:34 +010055
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000056using namespace arm::pipe;
Finn Williams09ad6f92019-12-19 17:05:18 +000057using PacketType = MockProfilingConnection::PacketType;
Aron Virginas-Tare898db92019-08-22 12:56:34 +010058
Sadik Armagan1625efc2021-06-10 18:24:34 +010059TEST_SUITE("ExternalProfiling")
60{
61TEST_CASE("CheckCommandHandlerKeyComparisons")
Francis Murtagh1f7db452019-08-14 09:49:34 +010062{
Jim Flynnbbfe6032020-07-20 16:57:44 +010063 arm::pipe::CommandHandlerKey testKey1_0(1, 1, 1);
64 arm::pipe::CommandHandlerKey testKey1_1(1, 1, 1);
65 arm::pipe::CommandHandlerKey testKey1_2(1, 2, 1);
Jim Flynn397043f2019-10-17 17:37:10 +010066
Jim Flynnbbfe6032020-07-20 16:57:44 +010067 arm::pipe::CommandHandlerKey testKey0(0, 1, 1);
68 arm::pipe::CommandHandlerKey testKey1(0, 1, 1);
69 arm::pipe::CommandHandlerKey testKey2(0, 1, 1);
70 arm::pipe::CommandHandlerKey testKey3(0, 0, 0);
71 arm::pipe::CommandHandlerKey testKey4(0, 2, 2);
72 arm::pipe::CommandHandlerKey testKey5(0, 0, 2);
Jim Flynn397043f2019-10-17 17:37:10 +010073
Sadik Armagan1625efc2021-06-10 18:24:34 +010074 CHECK(testKey1_0 > testKey0);
75 CHECK(testKey1_0 == testKey1_1);
76 CHECK(testKey1_0 < testKey1_2);
Francis Murtagh1f7db452019-08-14 09:49:34 +010077
Sadik Armagan1625efc2021-06-10 18:24:34 +010078 CHECK(testKey1 < testKey4);
79 CHECK(testKey1 > testKey3);
80 CHECK(testKey1 <= testKey4);
81 CHECK(testKey1 >= testKey3);
82 CHECK(testKey1 <= testKey2);
83 CHECK(testKey1 >= testKey2);
84 CHECK(testKey1 == testKey2);
85 CHECK(testKey1 == testKey1);
Francis Murtagh1f7db452019-08-14 09:49:34 +010086
Sadik Armagan1625efc2021-06-10 18:24:34 +010087 CHECK(!(testKey1 == testKey5));
88 CHECK(!(testKey1 != testKey1));
89 CHECK(testKey1 != testKey5);
Francis Murtagh1f7db452019-08-14 09:49:34 +010090
Sadik Armagan1625efc2021-06-10 18:24:34 +010091 CHECK((testKey1 == testKey2 && testKey2 == testKey1));
92 CHECK((testKey0 == testKey1 && testKey1 == testKey2 && testKey0 == testKey2));
Francis Murtagh1f7db452019-08-14 09:49:34 +010093
Sadik Armagan1625efc2021-06-10 18:24:34 +010094 CHECK(testKey1.GetPacketId() == 1);
95 CHECK(testKey1.GetVersion() == 1);
Francis Murtagh1f7db452019-08-14 09:49:34 +010096
Jim Flynnbbfe6032020-07-20 16:57:44 +010097 std::vector<arm::pipe::CommandHandlerKey> vect = {
98 arm::pipe::CommandHandlerKey(0, 0, 1), arm::pipe::CommandHandlerKey(0, 2, 0),
99 arm::pipe::CommandHandlerKey(0, 1, 0), arm::pipe::CommandHandlerKey(0, 2, 1),
100 arm::pipe::CommandHandlerKey(0, 1, 1), arm::pipe::CommandHandlerKey(0, 0, 1),
101 arm::pipe::CommandHandlerKey(0, 2, 0), arm::pipe::CommandHandlerKey(0, 0, 0) };
Francis Murtagh1f7db452019-08-14 09:49:34 +0100102
103 std::sort(vect.begin(), vect.end());
104
Jim Flynnbbfe6032020-07-20 16:57:44 +0100105 std::vector<arm::pipe::CommandHandlerKey> expectedVect = {
106 arm::pipe::CommandHandlerKey(0, 0, 0), arm::pipe::CommandHandlerKey(0, 0, 1),
107 arm::pipe::CommandHandlerKey(0, 0, 1), arm::pipe::CommandHandlerKey(0, 1, 0),
108 arm::pipe::CommandHandlerKey(0, 1, 1), arm::pipe::CommandHandlerKey(0, 2, 0),
109 arm::pipe::CommandHandlerKey(0, 2, 0), arm::pipe::CommandHandlerKey(0, 2, 1) };
Francis Murtagh1f7db452019-08-14 09:49:34 +0100110
Sadik Armagan1625efc2021-06-10 18:24:34 +0100111 CHECK(vect == expectedVect);
Francis Murtagh1f7db452019-08-14 09:49:34 +0100112}
113
Sadik Armagan1625efc2021-06-10 18:24:34 +0100114TEST_CASE("CheckPacketKeyComparisons")
Jim Flynned25e0e2019-10-18 13:21:43 +0100115{
Jim Flynnbbfe6032020-07-20 16:57:44 +0100116 arm::pipe::PacketKey key0(0, 0);
117 arm::pipe::PacketKey key1(0, 0);
118 arm::pipe::PacketKey key2(0, 1);
119 arm::pipe::PacketKey key3(0, 2);
120 arm::pipe::PacketKey key4(1, 0);
121 arm::pipe::PacketKey key5(1, 0);
122 arm::pipe::PacketKey key6(1, 1);
Jim Flynned25e0e2019-10-18 13:21:43 +0100123
Sadik Armagan1625efc2021-06-10 18:24:34 +0100124 CHECK(!(key0 < key1));
125 CHECK(!(key0 > key1));
126 CHECK(key0 <= key1);
127 CHECK(key0 >= key1);
128 CHECK(key0 == key1);
129 CHECK(key0 < key2);
130 CHECK(key2 < key3);
131 CHECK(key3 > key0);
132 CHECK(key4 == key5);
133 CHECK(key4 > key0);
134 CHECK(key5 < key6);
135 CHECK(key5 <= key6);
136 CHECK(key5 != key6);
Jim Flynned25e0e2019-10-18 13:21:43 +0100137}
138
Sadik Armagan1625efc2021-06-10 18:24:34 +0100139TEST_CASE("CheckCommandHandler")
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100140{
Jim Flynn6c9f17d2022-03-10 23:13:01 +0000141 LogLevelSwapper logLevelSwapper(arm::pipe::LogSeverity::Fatal);
142
Jim Flynnbbfe6032020-07-20 16:57:44 +0100143 arm::pipe::PacketVersionResolver packetVersionResolver;
Matteo Martincigh8a837172019-10-04 17:01:07 +0100144 ProfilingStateMachine profilingStateMachine;
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100145
Matteo Martincigh8a837172019-10-04 17:01:07 +0100146 TestProfilingConnectionBase testProfilingConnectionBase;
147 TestProfilingConnectionTimeoutError testProfilingConnectionTimeOutError;
148 TestProfilingConnectionArmnnError testProfilingConnectionArmnnError;
Keith Davis3201eea2019-10-24 17:30:41 +0100149 CounterDirectory counterDirectory;
150 MockBufferManager mockBuffer(1024);
Jim Flynn9c85b412022-03-16 00:27:43 +0000151 SendCounterPacket sendCounterPacket(mockBuffer,
152 arm::pipe::ARMNN_SOFTWARE_INFO,
153 arm::pipe::ARMNN_SOFTWARE_VERSION,
154 arm::pipe::ARMNN_HARDWARE_VERSION);
Sadik Armagan3896b472020-02-10 12:24:15 +0000155 SendThread sendThread(profilingStateMachine, mockBuffer, sendCounterPacket);
Matteo Martincighcdfb9412019-11-08 11:23:06 +0000156 SendTimelinePacket sendTimelinePacket(mockBuffer);
Jim Flynn6398a982020-05-27 17:05:21 +0100157 MockProfilingServiceStatus mockProfilingServiceStatus;
Matteo Martincighcdfb9412019-11-08 11:23:06 +0000158
Keith Davis3201eea2019-10-24 17:30:41 +0100159 ConnectionAcknowledgedCommandHandler connectionAcknowledgedCommandHandler(0, 1, 4194304, counterDirectory,
Matteo Martincighcdfb9412019-11-08 11:23:06 +0000160 sendCounterPacket, sendTimelinePacket,
Jim Flynn6398a982020-05-27 17:05:21 +0100161 profilingStateMachine,
162 mockProfilingServiceStatus);
Jim Flynnbbfe6032020-07-20 16:57:44 +0100163 arm::pipe::CommandHandlerRegistry commandHandlerRegistry;
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100164
Matteo Martincighc2728f92019-10-07 12:35:21 +0100165 commandHandlerRegistry.RegisterFunctor(&connectionAcknowledgedCommandHandler);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100166
Matteo Martincigh8a837172019-10-04 17:01:07 +0100167 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
168 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100169
Keith Davis3201eea2019-10-24 17:30:41 +0100170 CommandHandler commandHandler0(1, true, commandHandlerRegistry, packetVersionResolver);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100171
Colm Donelan2ba48d22019-11-29 09:10:59 +0000172 // This should start the command handler thread return the connection ack and put the profiling
173 // service into active state.
Matteo Martincigh8a837172019-10-04 17:01:07 +0100174 commandHandler0.Start(testProfilingConnectionBase);
Colm Donelan2ba48d22019-11-29 09:10:59 +0000175 // Try to start the send thread many times, it must only start once
Matteo Martincigh8a837172019-10-04 17:01:07 +0100176 commandHandler0.Start(testProfilingConnectionBase);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100177
Colm Donelan2ba48d22019-11-29 09:10:59 +0000178 // This could take up to 20mSec but we'll check often.
179 for (int i = 0; i < 10; i++)
Matteo Martincigh8a837172019-10-04 17:01:07 +0100180 {
181 if (profilingStateMachine.GetCurrentState() == ProfilingState::Active)
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100182 {
Matteo Martincigh8a837172019-10-04 17:01:07 +0100183 break;
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100184 }
Colm Donelan2ba48d22019-11-29 09:10:59 +0000185 std::this_thread::sleep_for(std::chrono::milliseconds(2));
Matteo Martincigh8a837172019-10-04 17:01:07 +0100186 }
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100187
Sadik Armagan1625efc2021-06-10 18:24:34 +0100188 CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::Active);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100189
Colm Donelan2ba48d22019-11-29 09:10:59 +0000190 // Close the thread again.
191 commandHandler0.Stop();
192
193 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
194 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
195
196 // In this test we'll simulate a timeout without a connection ack packet being received.
197 // Stop after timeout is set so we expect the command handler to stop almost immediately.
198 CommandHandler commandHandler1(1, true, commandHandlerRegistry, packetVersionResolver);
199
200 commandHandler1.Start(testProfilingConnectionTimeOutError);
201 // Wait until we know a timeout exception has been sent at least once.
202 for (int i = 0; i < 10; i++)
203 {
204 if (testProfilingConnectionTimeOutError.ReadCalledCount())
205 {
206 break;
207 }
208 std::this_thread::sleep_for(std::chrono::milliseconds(2));
209 }
Colm Donelan2ba48d22019-11-29 09:10:59 +0000210
211 // The command handler loop should have stopped after the timeout.
Finn Williams09ad6f92019-12-19 17:05:18 +0000212 // wait for the timeout exception to be processed and the loop to break.
213 uint32_t timeout = 50;
214 uint32_t timeSlept = 0;
215 while (commandHandler1.IsRunning())
216 {
217 if (timeSlept >= timeout)
218 {
Sadik Armagan1625efc2021-06-10 18:24:34 +0100219 FAIL("Timeout: The command handler loop did not stop after the timeout");
Finn Williams09ad6f92019-12-19 17:05:18 +0000220 }
221 std::this_thread::sleep_for(std::chrono::milliseconds(1));
222 timeSlept ++;
223 }
Colm Donelan2ba48d22019-11-29 09:10:59 +0000224
225 commandHandler1.Stop();
226 // The state machine should never have received the ack so will still be in WaitingForAck.
Sadik Armagan1625efc2021-06-10 18:24:34 +0100227 CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::WaitingForAck);
Colm Donelan2ba48d22019-11-29 09:10:59 +0000228
Finn Williams09ad6f92019-12-19 17:05:18 +0000229 // Now try sending a bad connection acknowledged packet
230 TestProfilingConnectionBadAckPacket testProfilingConnectionBadAckPacket;
231 commandHandler1.Start(testProfilingConnectionBadAckPacket);
232 commandHandler1.Stop();
233 // This should also not change the state machine
Sadik Armagan1625efc2021-06-10 18:24:34 +0100234 CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::WaitingForAck);
Finn Williams09ad6f92019-12-19 17:05:18 +0000235
Colm Donelan2ba48d22019-11-29 09:10:59 +0000236 // Disable stop after timeout and now commandHandler1 should persist after a timeout
237 commandHandler1.SetStopAfterTimeout(false);
238 // Restart the thread.
239 commandHandler1.Start(testProfilingConnectionTimeOutError);
240
241 // Wait for at the three timeouts and the ack to be sent.
242 for (int i = 0; i < 10; i++)
243 {
244 if (testProfilingConnectionTimeOutError.ReadCalledCount() > 3)
245 {
246 break;
247 }
248 std::this_thread::sleep_for(std::chrono::milliseconds(2));
249 }
250 commandHandler1.Stop();
251
252 // Even after the 3 exceptions the ack packet should have transitioned the command handler to active.
Sadik Armagan1625efc2021-06-10 18:24:34 +0100253 CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::Active);
Colm Donelan2ba48d22019-11-29 09:10:59 +0000254
255 // A command handler that gets exceptions other than timeouts should keep going.
256 CommandHandler commandHandler2(1, false, commandHandlerRegistry, packetVersionResolver);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100257
Matteo Martincigh8a837172019-10-04 17:01:07 +0100258 commandHandler2.Start(testProfilingConnectionArmnnError);
259
Colm Donelan2ba48d22019-11-29 09:10:59 +0000260 // Wait for two exceptions to be thrown.
261 for (int i = 0; i < 10; i++)
262 {
263 if (testProfilingConnectionTimeOutError.ReadCalledCount() >= 2)
264 {
265 break;
266 }
267 std::this_thread::sleep_for(std::chrono::milliseconds(2));
268 }
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100269
Sadik Armagan1625efc2021-06-10 18:24:34 +0100270 CHECK(commandHandler2.IsRunning());
Matteo Martincigh8a837172019-10-04 17:01:07 +0100271 commandHandler2.Stop();
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100272}
273
Sadik Armagan1625efc2021-06-10 18:24:34 +0100274TEST_CASE("CheckEncodeVersion")
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100275{
Jim Flynnbbfe6032020-07-20 16:57:44 +0100276 arm::pipe::Version version1(12);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100277
Sadik Armagan1625efc2021-06-10 18:24:34 +0100278 CHECK(version1.GetMajor() == 0);
279 CHECK(version1.GetMinor() == 0);
280 CHECK(version1.GetPatch() == 12);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100281
Jim Flynnbbfe6032020-07-20 16:57:44 +0100282 arm::pipe::Version version2(4108);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100283
Sadik Armagan1625efc2021-06-10 18:24:34 +0100284 CHECK(version2.GetMajor() == 0);
285 CHECK(version2.GetMinor() == 1);
286 CHECK(version2.GetPatch() == 12);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100287
Jim Flynnbbfe6032020-07-20 16:57:44 +0100288 arm::pipe::Version version3(4198412);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100289
Sadik Armagan1625efc2021-06-10 18:24:34 +0100290 CHECK(version3.GetMajor() == 1);
291 CHECK(version3.GetMinor() == 1);
292 CHECK(version3.GetPatch() == 12);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100293
Jim Flynnbbfe6032020-07-20 16:57:44 +0100294 arm::pipe::Version version4(0);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100295
Sadik Armagan1625efc2021-06-10 18:24:34 +0100296 CHECK(version4.GetMajor() == 0);
297 CHECK(version4.GetMinor() == 0);
298 CHECK(version4.GetPatch() == 0);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100299
Jim Flynnbbfe6032020-07-20 16:57:44 +0100300 arm::pipe::Version version5(1, 0, 0);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100301 CHECK(version5.GetEncodedValue() == 4194304);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100302}
303
Sadik Armagan1625efc2021-06-10 18:24:34 +0100304TEST_CASE("CheckPacketClass")
Nikhil Rajbc626052019-08-15 15:49:45 +0100305{
Keith Davis3201eea2019-10-24 17:30:41 +0100306 uint32_t length = 4;
Matteo Martincigh67ef2a52019-10-10 13:29:02 +0100307 std::unique_ptr<unsigned char[]> packetData0 = std::make_unique<unsigned char[]>(length);
308 std::unique_ptr<unsigned char[]> packetData1 = std::make_unique<unsigned char[]>(0);
309 std::unique_ptr<unsigned char[]> nullPacketData;
Nikhil Rajbc626052019-08-15 15:49:45 +0100310
Jim Flynnbbfe6032020-07-20 16:57:44 +0100311 arm::pipe::Packet packetTest0(472580096, length, packetData0);
Nikhil Rajbc626052019-08-15 15:49:45 +0100312
Sadik Armagan1625efc2021-06-10 18:24:34 +0100313 CHECK(packetTest0.GetHeader() == 472580096);
314 CHECK(packetTest0.GetPacketFamily() == 7);
315 CHECK(packetTest0.GetPacketId() == 43);
316 CHECK(packetTest0.GetLength() == length);
317 CHECK(packetTest0.GetPacketType() == 3);
318 CHECK(packetTest0.GetPacketClass() == 5);
Nikhil Rajbc626052019-08-15 15:49:45 +0100319
Sadik Armagan1625efc2021-06-10 18:24:34 +0100320 CHECK_THROWS_AS(arm::pipe::Packet packetTest1(472580096, 0, packetData1), arm::pipe::InvalidArgumentException);
321 CHECK_NOTHROW(arm::pipe::Packet packetTest2(472580096, 0, nullPacketData));
Nikhil Rajbc626052019-08-15 15:49:45 +0100322
Jim Flynnbbfe6032020-07-20 16:57:44 +0100323 arm::pipe::Packet packetTest3(472580096, 0, nullPacketData);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100324 CHECK(packetTest3.GetLength() == 0);
325 CHECK(packetTest3.GetData() == nullptr);
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100326
Matteo Martincigh67ef2a52019-10-10 13:29:02 +0100327 const unsigned char* packetTest0Data = packetTest0.GetData();
Jim Flynnbbfe6032020-07-20 16:57:44 +0100328 arm::pipe::Packet packetTest4(std::move(packetTest0));
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100329
Sadik Armagan1625efc2021-06-10 18:24:34 +0100330 CHECK(packetTest0.GetData() == nullptr);
331 CHECK(packetTest4.GetData() == packetTest0Data);
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100332
Sadik Armagan1625efc2021-06-10 18:24:34 +0100333 CHECK(packetTest4.GetHeader() == 472580096);
334 CHECK(packetTest4.GetPacketFamily() == 7);
335 CHECK(packetTest4.GetPacketId() == 43);
336 CHECK(packetTest4.GetLength() == length);
337 CHECK(packetTest4.GetPacketType() == 3);
338 CHECK(packetTest4.GetPacketClass() == 5);
Nikhil Rajbc626052019-08-15 15:49:45 +0100339}
340
Sadik Armagan1625efc2021-06-10 18:24:34 +0100341TEST_CASE("CheckCommandHandlerFunctor")
Francis Murtagh11f99b42019-08-16 11:28:52 +0100342{
Francis Murtagh11f99b42019-08-16 11:28:52 +0100343 // Hard code the version as it will be the same during a single profiling session
344 uint32_t version = 1;
345
Jim Flynn397043f2019-10-17 17:37:10 +0100346 TestFunctorA testFunctorA(7, 461, version);
347 TestFunctorB testFunctorB(8, 963, version);
348 TestFunctorC testFunctorC(5, 983, version);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100349
Jim Flynnbbfe6032020-07-20 16:57:44 +0100350 arm::pipe::CommandHandlerKey keyA(
351 testFunctorA.GetFamilyId(), testFunctorA.GetPacketId(), testFunctorA.GetVersion());
352 arm::pipe::CommandHandlerKey keyB(
353 testFunctorB.GetFamilyId(), testFunctorB.GetPacketId(), testFunctorB.GetVersion());
354 arm::pipe::CommandHandlerKey keyC(
355 testFunctorC.GetFamilyId(), testFunctorC.GetPacketId(), testFunctorC.GetVersion());
Francis Murtagh11f99b42019-08-16 11:28:52 +0100356
357 // Create the unwrapped map to simulate the Command Handler Registry
Jim Flynnbbfe6032020-07-20 16:57:44 +0100358 std::map<arm::pipe::CommandHandlerKey, arm::pipe::CommandHandlerFunctor*> registry;
Francis Murtagh11f99b42019-08-16 11:28:52 +0100359
360 registry.insert(std::make_pair(keyB, &testFunctorB));
361 registry.insert(std::make_pair(keyA, &testFunctorA));
362 registry.insert(std::make_pair(keyC, &testFunctorC));
363
364 // Check the order of the map is correct
365 auto it = registry.begin();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100366 CHECK(it->first == keyC); // familyId == 5
Francis Murtagh11f99b42019-08-16 11:28:52 +0100367 it++;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100368 CHECK(it->first == keyA); // familyId == 7
Francis Murtagh11f99b42019-08-16 11:28:52 +0100369 it++;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100370 CHECK(it->first == keyB); // familyId == 8
Francis Murtagh11f99b42019-08-16 11:28:52 +0100371
Matteo Martincigh67ef2a52019-10-10 13:29:02 +0100372 std::unique_ptr<unsigned char[]> packetDataA;
373 std::unique_ptr<unsigned char[]> packetDataB;
374 std::unique_ptr<unsigned char[]> packetDataC;
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100375
Jim Flynnbbfe6032020-07-20 16:57:44 +0100376 arm::pipe::Packet packetA(500000000, 0, packetDataA);
377 arm::pipe::Packet packetB(600000000, 0, packetDataB);
378 arm::pipe::Packet packetC(400000000, 0, packetDataC);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100379
380 // Check the correct operator of derived class is called
Jim Flynnbbfe6032020-07-20 16:57:44 +0100381 registry.at(arm::pipe::CommandHandlerKey(
382 packetA.GetPacketFamily(), packetA.GetPacketId(), version))->operator()(packetA);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100383 CHECK(testFunctorA.GetCount() == 1);
384 CHECK(testFunctorB.GetCount() == 0);
385 CHECK(testFunctorC.GetCount() == 0);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100386
Jim Flynnbbfe6032020-07-20 16:57:44 +0100387 registry.at(arm::pipe::CommandHandlerKey(
388 packetB.GetPacketFamily(), packetB.GetPacketId(), version))->operator()(packetB);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100389 CHECK(testFunctorA.GetCount() == 1);
390 CHECK(testFunctorB.GetCount() == 1);
391 CHECK(testFunctorC.GetCount() == 0);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100392
Jim Flynnbbfe6032020-07-20 16:57:44 +0100393 registry.at(arm::pipe::CommandHandlerKey(
394 packetC.GetPacketFamily(), packetC.GetPacketId(), version))->operator()(packetC);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100395 CHECK(testFunctorA.GetCount() == 1);
396 CHECK(testFunctorB.GetCount() == 1);
397 CHECK(testFunctorC.GetCount() == 1);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100398}
399
Sadik Armagan1625efc2021-06-10 18:24:34 +0100400TEST_CASE("CheckCommandHandlerRegistry")
Francis Murtagh94d79152019-08-16 17:45:07 +0100401{
402 // Hard code the version as it will be the same during a single profiling session
403 uint32_t version = 1;
404
Jim Flynn397043f2019-10-17 17:37:10 +0100405 TestFunctorA testFunctorA(7, 461, version);
406 TestFunctorB testFunctorB(8, 963, version);
407 TestFunctorC testFunctorC(5, 983, version);
Francis Murtagh94d79152019-08-16 17:45:07 +0100408
409 // Create the Command Handler Registry
Jim Flynnbbfe6032020-07-20 16:57:44 +0100410 arm::pipe::CommandHandlerRegistry registry;
Francis Murtagh94d79152019-08-16 17:45:07 +0100411
412 // Register multiple different derived classes
Matteo Martincighc2728f92019-10-07 12:35:21 +0100413 registry.RegisterFunctor(&testFunctorA);
414 registry.RegisterFunctor(&testFunctorB);
415 registry.RegisterFunctor(&testFunctorC);
Francis Murtagh94d79152019-08-16 17:45:07 +0100416
Matteo Martincigh67ef2a52019-10-10 13:29:02 +0100417 std::unique_ptr<unsigned char[]> packetDataA;
418 std::unique_ptr<unsigned char[]> packetDataB;
419 std::unique_ptr<unsigned char[]> packetDataC;
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100420
Jim Flynnbbfe6032020-07-20 16:57:44 +0100421 arm::pipe::Packet packetA(500000000, 0, packetDataA);
422 arm::pipe::Packet packetB(600000000, 0, packetDataB);
423 arm::pipe::Packet packetC(400000000, 0, packetDataC);
Francis Murtagh94d79152019-08-16 17:45:07 +0100424
425 // Check the correct operator of derived class is called
Jim Flynn397043f2019-10-17 17:37:10 +0100426 registry.GetFunctor(packetA.GetPacketFamily(), packetA.GetPacketId(), version)->operator()(packetA);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100427 CHECK(testFunctorA.GetCount() == 1);
428 CHECK(testFunctorB.GetCount() == 0);
429 CHECK(testFunctorC.GetCount() == 0);
Francis Murtagh94d79152019-08-16 17:45:07 +0100430
Jim Flynn397043f2019-10-17 17:37:10 +0100431 registry.GetFunctor(packetB.GetPacketFamily(), packetB.GetPacketId(), version)->operator()(packetB);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100432 CHECK(testFunctorA.GetCount() == 1);
433 CHECK(testFunctorB.GetCount() == 1);
434 CHECK(testFunctorC.GetCount() == 0);
Francis Murtagh94d79152019-08-16 17:45:07 +0100435
Jim Flynn397043f2019-10-17 17:37:10 +0100436 registry.GetFunctor(packetC.GetPacketFamily(), packetC.GetPacketId(), version)->operator()(packetC);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100437 CHECK(testFunctorA.GetCount() == 1);
438 CHECK(testFunctorB.GetCount() == 1);
439 CHECK(testFunctorC.GetCount() == 1);
Francis Murtagh94d79152019-08-16 17:45:07 +0100440
441 // Re-register an existing key with a new function
Jim Flynn397043f2019-10-17 17:37:10 +0100442 registry.RegisterFunctor(&testFunctorC, testFunctorA.GetFamilyId(), testFunctorA.GetPacketId(), version);
443 registry.GetFunctor(packetA.GetPacketFamily(), packetA.GetPacketId(), version)->operator()(packetC);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100444 CHECK(testFunctorA.GetCount() == 1);
445 CHECK(testFunctorB.GetCount() == 1);
446 CHECK(testFunctorC.GetCount() == 2);
Francis Murtagh94d79152019-08-16 17:45:07 +0100447
448 // Check that non-existent key returns nullptr for its functor
Sadik Armagan1625efc2021-06-10 18:24:34 +0100449 CHECK_THROWS_AS(registry.GetFunctor(0, 0, 0), arm::pipe::ProfilingException);
Francis Murtagh94d79152019-08-16 17:45:07 +0100450}
451
Sadik Armagan1625efc2021-06-10 18:24:34 +0100452TEST_CASE("CheckPacketVersionResolver")
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100453{
454 // Set up random number generator for generating packetId values
455 std::random_device device;
456 std::mt19937 generator(device());
457 std::uniform_int_distribution<uint32_t> distribution(std::numeric_limits<uint32_t>::min(),
458 std::numeric_limits<uint32_t>::max());
459
460 // NOTE: Expected version is always 1.0.0, regardless of packetId
Jim Flynnbbfe6032020-07-20 16:57:44 +0100461 const arm::pipe::Version expectedVersion(1, 0, 0);
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100462
Jim Flynnbbfe6032020-07-20 16:57:44 +0100463 arm::pipe::PacketVersionResolver packetVersionResolver;
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100464
465 constexpr unsigned int numTests = 10u;
466
467 for (unsigned int i = 0u; i < numTests; ++i)
468 {
Jim Flynned25e0e2019-10-18 13:21:43 +0100469 const uint32_t familyId = distribution(generator);
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100470 const uint32_t packetId = distribution(generator);
Jim Flynnbbfe6032020-07-20 16:57:44 +0100471 arm::pipe::Version resolvedVersion = packetVersionResolver.ResolvePacketVersion(familyId, packetId);
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100472
Sadik Armagan1625efc2021-06-10 18:24:34 +0100473 CHECK(resolvedVersion == expectedVersion);
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100474 }
475}
Matteo Martincighd0613b52019-10-09 16:47:04 +0100476
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100477void ProfilingCurrentStateThreadImpl(ProfilingStateMachine& states)
478{
479 ProfilingState newState = ProfilingState::NotConnected;
480 states.GetCurrentState();
481 states.TransitionToState(newState);
482}
483
Sadik Armagan1625efc2021-06-10 18:24:34 +0100484TEST_CASE("CheckProfilingStateMachine")
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100485{
486 ProfilingStateMachine profilingState1(ProfilingState::Uninitialised);
487 profilingState1.TransitionToState(ProfilingState::Uninitialised);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100488 CHECK(profilingState1.GetCurrentState() == ProfilingState::Uninitialised);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100489
490 ProfilingStateMachine profilingState2(ProfilingState::Uninitialised);
491 profilingState2.TransitionToState(ProfilingState::NotConnected);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100492 CHECK(profilingState2.GetCurrentState() == ProfilingState::NotConnected);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100493
494 ProfilingStateMachine profilingState3(ProfilingState::NotConnected);
495 profilingState3.TransitionToState(ProfilingState::NotConnected);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100496 CHECK(profilingState3.GetCurrentState() == ProfilingState::NotConnected);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100497
498 ProfilingStateMachine profilingState4(ProfilingState::NotConnected);
499 profilingState4.TransitionToState(ProfilingState::WaitingForAck);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100500 CHECK(profilingState4.GetCurrentState() == ProfilingState::WaitingForAck);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100501
502 ProfilingStateMachine profilingState5(ProfilingState::WaitingForAck);
503 profilingState5.TransitionToState(ProfilingState::WaitingForAck);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100504 CHECK(profilingState5.GetCurrentState() == ProfilingState::WaitingForAck);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100505
506 ProfilingStateMachine profilingState6(ProfilingState::WaitingForAck);
507 profilingState6.TransitionToState(ProfilingState::Active);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100508 CHECK(profilingState6.GetCurrentState() == ProfilingState::Active);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100509
510 ProfilingStateMachine profilingState7(ProfilingState::Active);
511 profilingState7.TransitionToState(ProfilingState::NotConnected);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100512 CHECK(profilingState7.GetCurrentState() == ProfilingState::NotConnected);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100513
514 ProfilingStateMachine profilingState8(ProfilingState::Active);
515 profilingState8.TransitionToState(ProfilingState::Active);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100516 CHECK(profilingState8.GetCurrentState() == ProfilingState::Active);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100517
518 ProfilingStateMachine profilingState9(ProfilingState::Uninitialised);
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000519 CHECK_THROWS_AS(profilingState9.TransitionToState(ProfilingState::WaitingForAck), arm::pipe::ProfilingException);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100520
521 ProfilingStateMachine profilingState10(ProfilingState::Uninitialised);
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000522 CHECK_THROWS_AS(profilingState10.TransitionToState(ProfilingState::Active), arm::pipe::ProfilingException);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100523
524 ProfilingStateMachine profilingState11(ProfilingState::NotConnected);
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000525 CHECK_THROWS_AS(profilingState11.TransitionToState(ProfilingState::Uninitialised), arm::pipe::ProfilingException);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100526
527 ProfilingStateMachine profilingState12(ProfilingState::NotConnected);
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000528 CHECK_THROWS_AS(profilingState12.TransitionToState(ProfilingState::Active), arm::pipe::ProfilingException);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100529
530 ProfilingStateMachine profilingState13(ProfilingState::WaitingForAck);
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000531 CHECK_THROWS_AS(profilingState13.TransitionToState(ProfilingState::Uninitialised), arm::pipe::ProfilingException);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100532
533 ProfilingStateMachine profilingState14(ProfilingState::WaitingForAck);
Jim Flynn53e46992019-10-14 12:31:10 +0100534 profilingState14.TransitionToState(ProfilingState::NotConnected);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100535 CHECK(profilingState14.GetCurrentState() == ProfilingState::NotConnected);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100536
537 ProfilingStateMachine profilingState15(ProfilingState::Active);
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000538 CHECK_THROWS_AS(profilingState15.TransitionToState(ProfilingState::Uninitialised), arm::pipe::ProfilingException);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100539
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000540 ProfilingStateMachine profilingState16(ProfilingState::Active);
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000541 CHECK_THROWS_AS(profilingState16.TransitionToState(ProfilingState::WaitingForAck), arm::pipe::ProfilingException);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100542
543 ProfilingStateMachine profilingState17(ProfilingState::Uninitialised);
544
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100545 std::vector<std::thread> threads;
546 for (unsigned int i = 0; i < 5; ++i)
547 {
548 threads.push_back(std::thread(ProfilingCurrentStateThreadImpl, std::ref(profilingState17)));
549 }
550 std::for_each(threads.begin(), threads.end(), [](std::thread& theThread)
551 {
552 theThread.join();
553 });
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100554
Sadik Armagan1625efc2021-06-10 18:24:34 +0100555 CHECK((profilingState17.GetCurrentState() == ProfilingState::NotConnected));
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100556}
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100557
Jim Flynn8355ec92019-09-17 12:29:50 +0100558void CaptureDataWriteThreadImpl(Holder& holder, uint32_t capturePeriod, const std::vector<uint16_t>& counterIds)
Francis Murtagh68f78d82019-09-04 16:42:29 +0100559{
Finn Williams032bc742020-02-12 11:02:34 +0000560 holder.SetCaptureData(capturePeriod, counterIds, {});
Francis Murtagh68f78d82019-09-04 16:42:29 +0100561}
562
Francis Murtaghbd707162019-09-09 11:26:44 +0100563void CaptureDataReadThreadImpl(const Holder& holder, CaptureData& captureData)
Francis Murtagh68f78d82019-09-04 16:42:29 +0100564{
565 captureData = holder.GetCaptureData();
566}
567
Sadik Armagan1625efc2021-06-10 18:24:34 +0100568TEST_CASE("CheckCaptureDataHolder")
Francis Murtagh68f78d82019-09-04 16:42:29 +0100569{
Francis Murtaghbd707162019-09-09 11:26:44 +0100570 std::map<uint32_t, std::vector<uint16_t>> periodIdMap;
571 std::vector<uint16_t> counterIds;
Jim Flynn8355ec92019-09-17 12:29:50 +0100572 uint32_t numThreads = 10;
573 for (uint32_t i = 0; i < numThreads; ++i)
Francis Murtaghbd707162019-09-09 11:26:44 +0100574 {
575 counterIds.emplace_back(i);
576 periodIdMap.insert(std::make_pair(i, counterIds));
577 }
Francis Murtagh68f78d82019-09-04 16:42:29 +0100578
Jim Flynn8355ec92019-09-17 12:29:50 +0100579 // Verify the read and write threads set the holder correctly
580 // and retrieve the expected values
Francis Murtagh68f78d82019-09-04 16:42:29 +0100581 Holder holder;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100582 CHECK((holder.GetCaptureData()).GetCapturePeriod() == 0);
583 CHECK(((holder.GetCaptureData()).GetCounterIds()).empty());
Francis Murtagh68f78d82019-09-04 16:42:29 +0100584
585 // Check Holder functions
Francis Murtaghbd707162019-09-09 11:26:44 +0100586 std::thread thread1(CaptureDataWriteThreadImpl, std::ref(holder), 2, std::ref(periodIdMap[2]));
Francis Murtagh68f78d82019-09-04 16:42:29 +0100587 thread1.join();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100588 CHECK((holder.GetCaptureData()).GetCapturePeriod() == 2);
589 CHECK((holder.GetCaptureData()).GetCounterIds() == periodIdMap[2]);
Jim Flynn8355ec92019-09-17 12:29:50 +0100590 // NOTE: now that we have some initial values in the holder we don't have to worry
591 // in the multi-threaded section below about a read thread accessing the holder
592 // before any write thread has gotten to it so we read period = 0, counterIds empty
593 // instead of period = 0, counterIds = {0} as will the case when write thread 0
594 // has executed.
Francis Murtagh68f78d82019-09-04 16:42:29 +0100595
596 CaptureData captureData;
597 std::thread thread2(CaptureDataReadThreadImpl, std::ref(holder), std::ref(captureData));
598 thread2.join();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100599 CHECK(captureData.GetCapturePeriod() == 2);
600 CHECK(captureData.GetCounterIds() == periodIdMap[2]);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100601
Jim Flynn8355ec92019-09-17 12:29:50 +0100602 std::map<uint32_t, CaptureData> captureDataIdMap;
603 for (uint32_t i = 0; i < numThreads; ++i)
604 {
605 CaptureData perThreadCaptureData;
606 captureDataIdMap.insert(std::make_pair(i, perThreadCaptureData));
607 }
608
Francis Murtaghbd707162019-09-09 11:26:44 +0100609 std::vector<std::thread> threadsVect;
Jim Flynn8355ec92019-09-17 12:29:50 +0100610 std::vector<std::thread> readThreadsVect;
611 for (uint32_t i = 0; i < numThreads; ++i)
Francis Murtaghbd707162019-09-09 11:26:44 +0100612 {
Keith Davis3201eea2019-10-24 17:30:41 +0100613 threadsVect.emplace_back(
614 std::thread(CaptureDataWriteThreadImpl, std::ref(holder), i, std::ref(periodIdMap[i])));
Francis Murtagh06965692019-09-05 16:29:01 +0100615
Jim Flynn8355ec92019-09-17 12:29:50 +0100616 // Verify that the CaptureData goes into the thread in a virgin state
Sadik Armagan1625efc2021-06-10 18:24:34 +0100617 CHECK(captureDataIdMap.at(i).GetCapturePeriod() == 0);
618 CHECK(captureDataIdMap.at(i).GetCounterIds().empty());
Keith Davis3201eea2019-10-24 17:30:41 +0100619 readThreadsVect.emplace_back(
620 std::thread(CaptureDataReadThreadImpl, std::ref(holder), std::ref(captureDataIdMap.at(i))));
Francis Murtaghbd707162019-09-09 11:26:44 +0100621 }
622
Jim Flynn8355ec92019-09-17 12:29:50 +0100623 for (uint32_t i = 0; i < numThreads; ++i)
Francis Murtaghbd707162019-09-09 11:26:44 +0100624 {
625 threadsVect[i].join();
Francis Murtaghbd707162019-09-09 11:26:44 +0100626 readThreadsVect[i].join();
627 }
Francis Murtagh68f78d82019-09-04 16:42:29 +0100628
Jim Flynn8355ec92019-09-17 12:29:50 +0100629 // Look at the CaptureData that each read thread has filled
630 // the capture period it read should match the counter ids entry
631 for (uint32_t i = 0; i < numThreads; ++i)
632 {
633 CaptureData perThreadCaptureData = captureDataIdMap.at(i);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100634 CHECK(perThreadCaptureData.GetCounterIds() == periodIdMap.at(perThreadCaptureData.GetCapturePeriod()));
Jim Flynn8355ec92019-09-17 12:29:50 +0100635 }
Matthew Bentham46d1c622019-09-13 12:45:04 +0100636}
Francis Murtagh68f78d82019-09-04 16:42:29 +0100637
Sadik Armagan1625efc2021-06-10 18:24:34 +0100638TEST_CASE("CaptureDataMethods")
Matthew Bentham46d1c622019-09-13 12:45:04 +0100639{
Jim Flynn8355ec92019-09-17 12:29:50 +0100640 // Check CaptureData setter and getter functions
Keith Davis3201eea2019-10-24 17:30:41 +0100641 std::vector<uint16_t> counterIds = { 42, 29, 13 };
Jim Flynn8355ec92019-09-17 12:29:50 +0100642 CaptureData captureData;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100643 CHECK(captureData.GetCapturePeriod() == 0);
644 CHECK((captureData.GetCounterIds()).empty());
Jim Flynn8355ec92019-09-17 12:29:50 +0100645 captureData.SetCapturePeriod(150);
646 captureData.SetCounterIds(counterIds);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100647 CHECK(captureData.GetCapturePeriod() == 150);
648 CHECK(captureData.GetCounterIds() == counterIds);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100649
Jim Flynn8355ec92019-09-17 12:29:50 +0100650 // Check assignment operator
Francis Murtagh68f78d82019-09-04 16:42:29 +0100651 CaptureData secondCaptureData;
Francis Murtagh68f78d82019-09-04 16:42:29 +0100652
Jim Flynn8355ec92019-09-17 12:29:50 +0100653 secondCaptureData = captureData;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100654 CHECK(secondCaptureData.GetCapturePeriod() == 150);
655 CHECK(secondCaptureData.GetCounterIds() == counterIds);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100656
657 // Check copy constructor
Jim Flynn8355ec92019-09-17 12:29:50 +0100658 CaptureData copyConstructedCaptureData(captureData);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100659
Sadik Armagan1625efc2021-06-10 18:24:34 +0100660 CHECK(copyConstructedCaptureData.GetCapturePeriod() == 150);
661 CHECK(copyConstructedCaptureData.GetCounterIds() == counterIds);
Keith Davis02356de2019-08-26 18:28:17 +0100662}
Francis Murtagh68f78d82019-09-04 16:42:29 +0100663
Sadik Armagan1625efc2021-06-10 18:24:34 +0100664TEST_CASE("CheckProfilingServiceDisabled")
Keith Davis02356de2019-08-26 18:28:17 +0100665{
Jim Flynn4c9ed1d2022-01-23 23:57:20 +0000666 ProfilingOptions options;
Jim Flynn34430252022-03-04 15:03:58 +0000667 armnn::ArmNNProfilingServiceInitialiser initialiser;
Jim Flynn9c85b412022-03-16 00:27:43 +0000668 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER,
669 initialiser,
670 arm::pipe::ARMNN_SOFTWARE_INFO,
671 arm::pipe::ARMNN_SOFTWARE_VERSION,
672 arm::pipe::ARMNN_HARDWARE_VERSION);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100673 profilingService.ResetExternalProfilingOptions(options, true);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100674 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100675 profilingService.Update();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100676 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis02356de2019-08-26 18:28:17 +0100677}
678
Sadik Armagan1625efc2021-06-10 18:24:34 +0100679TEST_CASE("CheckProfilingServiceCounterDirectory")
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100680{
Jim Flynn4c9ed1d2022-01-23 23:57:20 +0000681 ProfilingOptions options;
Jim Flynn34430252022-03-04 15:03:58 +0000682 armnn::ArmNNProfilingServiceInitialiser initialiser;
Jim Flynn9c85b412022-03-16 00:27:43 +0000683 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER,
684 initialiser,
685 arm::pipe::ARMNN_SOFTWARE_INFO,
686 arm::pipe::ARMNN_SOFTWARE_VERSION,
687 arm::pipe::ARMNN_HARDWARE_VERSION);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100688 profilingService.ResetExternalProfilingOptions(options, true);
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100689
Matteo Martincigha84edee2019-10-02 12:50:57 +0100690 const ICounterDirectory& counterDirectory0 = profilingService.GetCounterDirectory();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100691 CHECK(counterDirectory0.GetCounterCount() == 0);
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100692 profilingService.Update();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100693 CHECK(counterDirectory0.GetCounterCount() == 0);
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100694
695 options.m_EnableProfiling = true;
Matteo Martincigha84edee2019-10-02 12:50:57 +0100696 profilingService.ResetExternalProfilingOptions(options);
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100697
Matteo Martincigha84edee2019-10-02 12:50:57 +0100698 const ICounterDirectory& counterDirectory1 = profilingService.GetCounterDirectory();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100699 CHECK(counterDirectory1.GetCounterCount() == 0);
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100700 profilingService.Update();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100701 CHECK(counterDirectory1.GetCounterCount() != 0);
Colm Donelan2ba48d22019-11-29 09:10:59 +0000702 // Reset the profiling service to stop any running thread
703 options.m_EnableProfiling = false;
704 profilingService.ResetExternalProfilingOptions(options, true);
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100705}
706
Sadik Armagan1625efc2021-06-10 18:24:34 +0100707TEST_CASE("CheckProfilingServiceCounterValues")
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100708{
Jim Flynn4c9ed1d2022-01-23 23:57:20 +0000709 ProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +0100710 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +0000711 armnn::ArmNNProfilingServiceInitialiser initialiser;
Jim Flynn9c85b412022-03-16 00:27:43 +0000712 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER,
713 initialiser,
714 arm::pipe::ARMNN_SOFTWARE_INFO,
715 arm::pipe::ARMNN_SOFTWARE_VERSION,
716 arm::pipe::ARMNN_HARDWARE_VERSION);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100717 profilingService.ResetExternalProfilingOptions(options, true);
718
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100719 profilingService.Update();
Matteo Martincigha84edee2019-10-02 12:50:57 +0100720 const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
Keith Davis3201eea2019-10-24 17:30:41 +0100721 const Counters& counters = counterDirectory.GetCounters();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100722 CHECK(!counters.empty());
Matteo Martincigha84edee2019-10-02 12:50:57 +0100723
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100724 std::vector<std::thread> writers;
725
Sadik Armagan1625efc2021-06-10 18:24:34 +0100726 CHECK(!counters.empty());
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000727 uint16_t inferencesRun = INFERENCES_RUN;
Finn Williamsf3fcf322020-05-11 14:38:02 +0100728
729 // Test GetAbsoluteCounterValue
730 for (int i = 0; i < 4; ++i)
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100731 {
Finn Williamsf3fcf322020-05-11 14:38:02 +0100732 // Increment and decrement the INFERENCES_RUN counter 250 times
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100733 writers.push_back(std::thread([&profilingService, inferencesRun]()
Finn Williamsf3fcf322020-05-11 14:38:02 +0100734 {
735 for (int i = 0; i < 250; ++i)
736 {
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100737 profilingService.IncrementCounterValue(inferencesRun);
Finn Williamsf3fcf322020-05-11 14:38:02 +0100738 }
739 }));
740 // Add 10 to the INFERENCES_RUN counter 200 times
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100741 writers.push_back(std::thread([&profilingService, inferencesRun]()
Finn Williamsf3fcf322020-05-11 14:38:02 +0100742 {
743 for (int i = 0; i < 200; ++i)
744 {
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100745 profilingService.AddCounterValue(inferencesRun, 10);
Finn Williamsf3fcf322020-05-11 14:38:02 +0100746 }
747 }));
748 // Subtract 5 from the INFERENCES_RUN counter 200 times
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100749 writers.push_back(std::thread([&profilingService, inferencesRun]()
Finn Williamsf3fcf322020-05-11 14:38:02 +0100750 {
751 for (int i = 0; i < 200; ++i)
752 {
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100753 profilingService.SubtractCounterValue(inferencesRun, 5);
Finn Williamsf3fcf322020-05-11 14:38:02 +0100754 }
755 }));
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100756 }
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100757 std::for_each(writers.begin(), writers.end(), mem_fn(&std::thread::join));
758
Finn Williamsf3fcf322020-05-11 14:38:02 +0100759 uint32_t absoluteCounterValue = 0;
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100760
Sadik Armagan1625efc2021-06-10 18:24:34 +0100761 CHECK_NOTHROW(absoluteCounterValue = profilingService.GetAbsoluteCounterValue(INFERENCES_RUN));
762 CHECK(absoluteCounterValue == 5000);
Finn Williamsf3fcf322020-05-11 14:38:02 +0100763
764 // Test SetCounterValue
Sadik Armagan1625efc2021-06-10 18:24:34 +0100765 CHECK_NOTHROW(profilingService.SetCounterValue(INFERENCES_RUN, 0));
766 CHECK_NOTHROW(absoluteCounterValue = profilingService.GetAbsoluteCounterValue(INFERENCES_RUN));
767 CHECK(absoluteCounterValue == 0);
Finn Williamsf3fcf322020-05-11 14:38:02 +0100768
769 // Test GetDeltaCounterValue
770 writers.clear();
771 uint32_t deltaCounterValue = 0;
772 //Start a reading thread to randomly read the INFERENCES_RUN counter value
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100773 std::thread reader([&profilingService, inferencesRun](uint32_t& deltaCounterValue)
Finn Williamsf3fcf322020-05-11 14:38:02 +0100774 {
775 for (int i = 0; i < 300; ++i)
776 {
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100777 deltaCounterValue += profilingService.GetDeltaCounterValue(inferencesRun);
Finn Williamsf3fcf322020-05-11 14:38:02 +0100778 }
779 }, std::ref(deltaCounterValue));
780
781 for (int i = 0; i < 4; ++i)
782 {
783 // Increment and decrement the INFERENCES_RUN counter 250 times
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100784 writers.push_back(std::thread([&profilingService, inferencesRun]()
Finn Williamsf3fcf322020-05-11 14:38:02 +0100785 {
786 for (int i = 0; i < 250; ++i)
787 {
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100788 profilingService.IncrementCounterValue(inferencesRun);
Finn Williamsf3fcf322020-05-11 14:38:02 +0100789 }
790 }));
791 // Add 10 to the INFERENCES_RUN counter 200 times
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100792 writers.push_back(std::thread([&profilingService, inferencesRun]()
Finn Williamsf3fcf322020-05-11 14:38:02 +0100793 {
794 for (int i = 0; i < 200; ++i)
795 {
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100796 profilingService.AddCounterValue(inferencesRun, 10);
Finn Williamsf3fcf322020-05-11 14:38:02 +0100797 }
798 }));
799 // Subtract 5 from the INFERENCES_RUN counter 200 times
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100800 writers.push_back(std::thread([&profilingService, inferencesRun]()
Finn Williamsf3fcf322020-05-11 14:38:02 +0100801 {
802 for (int i = 0; i < 200; ++i)
803 {
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100804 profilingService.SubtractCounterValue(inferencesRun, 5);
Finn Williamsf3fcf322020-05-11 14:38:02 +0100805 }
806 }));
807 }
808
809 std::for_each(writers.begin(), writers.end(), mem_fn(&std::thread::join));
810 reader.join();
811
812 // Do one last read in case the reader stopped early
813 deltaCounterValue += profilingService.GetDeltaCounterValue(INFERENCES_RUN);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100814 CHECK(deltaCounterValue == 5000);
Finn Williamsf3fcf322020-05-11 14:38:02 +0100815
Colm Donelan2ba48d22019-11-29 09:10:59 +0000816 // Reset the profiling service to stop any running thread
817 options.m_EnableProfiling = false;
818 profilingService.ResetExternalProfilingOptions(options, true);
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100819}
820
Sadik Armagan1625efc2021-06-10 18:24:34 +0100821TEST_CASE("CheckProfilingObjectUids")
Matteo Martincighab173e92019-09-05 12:02:04 +0100822{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100823 uint16_t uid = 0;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100824 CHECK_NOTHROW(uid = GetNextUid());
825 CHECK(uid >= 1);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100826
827 uint16_t nextUid = 0;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100828 CHECK_NOTHROW(nextUid = GetNextUid());
829 CHECK(nextUid > uid);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100830
831 std::vector<uint16_t> counterUids;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100832 CHECK_NOTHROW(counterUids = GetNextCounterUids(uid,0));
833 CHECK(counterUids.size() == 1);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100834
835 std::vector<uint16_t> nextCounterUids;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100836 CHECK_NOTHROW(nextCounterUids = GetNextCounterUids(nextUid, 2));
837 CHECK(nextCounterUids.size() == 2);
838 CHECK(nextCounterUids[0] > counterUids[0]);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100839
840 std::vector<uint16_t> counterUidsMultiCore;
Francis Murtagh1e5afee2021-05-11 09:37:47 +0100841 uint16_t thirdUid = nextCounterUids[0];
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100842 uint16_t numberOfCores = 13;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100843 CHECK_NOTHROW(counterUidsMultiCore = GetNextCounterUids(thirdUid, numberOfCores));
844 CHECK(counterUidsMultiCore.size() == numberOfCores);
845 CHECK(counterUidsMultiCore.front() >= nextCounterUids[0]);
Keith Davis3201eea2019-10-24 17:30:41 +0100846 for (size_t i = 1; i < numberOfCores; i++)
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100847 {
Sadik Armagan1625efc2021-06-10 18:24:34 +0100848 CHECK(counterUidsMultiCore[i] == counterUidsMultiCore[i - 1] + 1);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100849 }
Sadik Armagan1625efc2021-06-10 18:24:34 +0100850 CHECK(counterUidsMultiCore.back() == counterUidsMultiCore.front() + numberOfCores - 1);
Matteo Martincighab173e92019-09-05 12:02:04 +0100851}
852
Sadik Armagan1625efc2021-06-10 18:24:34 +0100853TEST_CASE("CheckCounterDirectoryRegisterCategory")
Matteo Martincighab173e92019-09-05 12:02:04 +0100854{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100855 CounterDirectory counterDirectory;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100856 CHECK(counterDirectory.GetCategoryCount() == 0);
857 CHECK(counterDirectory.GetDeviceCount() == 0);
858 CHECK(counterDirectory.GetCounterSetCount() == 0);
859 CHECK(counterDirectory.GetCounterCount() == 0);
Matteo Martincighab173e92019-09-05 12:02:04 +0100860
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100861 // Register a category with an invalid name
862 const Category* noCategory = nullptr;
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000863 CHECK_THROWS_AS(noCategory = counterDirectory.RegisterCategory(""), arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100864 CHECK(counterDirectory.GetCategoryCount() == 0);
865 CHECK(!noCategory);
Matteo Martincighab173e92019-09-05 12:02:04 +0100866
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100867 // Register a category with an invalid name
Sadik Armagan1625efc2021-06-10 18:24:34 +0100868 CHECK_THROWS_AS(noCategory = counterDirectory.RegisterCategory("invalid category"),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000869 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100870 CHECK(counterDirectory.GetCategoryCount() == 0);
871 CHECK(!noCategory);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100872
873 // Register a new category
874 const std::string categoryName = "some_category";
Keith Davis3201eea2019-10-24 17:30:41 +0100875 const Category* category = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100876 CHECK_NOTHROW(category = counterDirectory.RegisterCategory(categoryName));
877 CHECK(counterDirectory.GetCategoryCount() == 1);
878 CHECK(category);
879 CHECK(category->m_Name == categoryName);
880 CHECK(category->m_Counters.empty());
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100881
882 // Get the registered category
883 const Category* registeredCategory = counterDirectory.GetCategory(categoryName);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100884 CHECK(counterDirectory.GetCategoryCount() == 1);
885 CHECK(registeredCategory);
886 CHECK(registeredCategory == category);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100887
888 // Try to get a category not registered
889 const Category* notRegisteredCategory = counterDirectory.GetCategory("not_registered_category");
Sadik Armagan1625efc2021-06-10 18:24:34 +0100890 CHECK(counterDirectory.GetCategoryCount() == 1);
891 CHECK(!notRegisteredCategory);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100892
893 // Register a category already registered
894 const Category* anotherCategory = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100895 CHECK_THROWS_AS(anotherCategory = counterDirectory.RegisterCategory(categoryName),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000896 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100897 CHECK(counterDirectory.GetCategoryCount() == 1);
898 CHECK(!anotherCategory);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100899
900 // Register a device for testing
901 const std::string deviceName = "some_device";
Keith Davis3201eea2019-10-24 17:30:41 +0100902 const Device* device = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100903 CHECK_NOTHROW(device = counterDirectory.RegisterDevice(deviceName));
904 CHECK(counterDirectory.GetDeviceCount() == 1);
905 CHECK(device);
906 CHECK(device->m_Uid >= 1);
907 CHECK(device->m_Name == deviceName);
908 CHECK(device->m_Cores == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100909
910 // Register a new category not associated to any device
911 const std::string categoryWoDeviceName = "some_category_without_device";
Keith Davis3201eea2019-10-24 17:30:41 +0100912 const Category* categoryWoDevice = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100913 CHECK_NOTHROW(categoryWoDevice = counterDirectory.RegisterCategory(categoryWoDeviceName));
914 CHECK(counterDirectory.GetCategoryCount() == 2);
915 CHECK(categoryWoDevice);
916 CHECK(categoryWoDevice->m_Name == categoryWoDeviceName);
917 CHECK(categoryWoDevice->m_Counters.empty());
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100918
Sadik Armagan4c998992020-02-25 12:44:44 +0000919 // Register a new category associated to an invalid device name (already exist)
920 const Category* categoryInvalidDeviceName = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100921 CHECK_THROWS_AS(categoryInvalidDeviceName =
Sadik Armagan4c998992020-02-25 12:44:44 +0000922 counterDirectory.RegisterCategory(categoryWoDeviceName),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000923 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100924 CHECK(counterDirectory.GetCategoryCount() == 2);
925 CHECK(!categoryInvalidDeviceName);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100926
927 // Register a new category associated to a valid device
928 const std::string categoryWValidDeviceName = "some_category_with_valid_device";
Keith Davis3201eea2019-10-24 17:30:41 +0100929 const Category* categoryWValidDevice = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100930 CHECK_NOTHROW(categoryWValidDevice =
Sadik Armagan4c998992020-02-25 12:44:44 +0000931 counterDirectory.RegisterCategory(categoryWValidDeviceName));
Sadik Armagan1625efc2021-06-10 18:24:34 +0100932 CHECK(counterDirectory.GetCategoryCount() == 3);
933 CHECK(categoryWValidDevice);
934 CHECK(categoryWValidDevice != category);
935 CHECK(categoryWValidDevice->m_Name == categoryWValidDeviceName);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100936
937 // Register a counter set for testing
938 const std::string counterSetName = "some_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +0100939 const CounterSet* counterSet = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100940 CHECK_NOTHROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
941 CHECK(counterDirectory.GetCounterSetCount() == 1);
942 CHECK(counterSet);
943 CHECK(counterSet->m_Uid >= 1);
944 CHECK(counterSet->m_Name == counterSetName);
945 CHECK(counterSet->m_Count == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100946
947 // Register a new category not associated to any counter set
948 const std::string categoryWoCounterSetName = "some_category_without_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +0100949 const Category* categoryWoCounterSet = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100950 CHECK_NOTHROW(categoryWoCounterSet =
Sadik Armagan4c998992020-02-25 12:44:44 +0000951 counterDirectory.RegisterCategory(categoryWoCounterSetName));
Sadik Armagan1625efc2021-06-10 18:24:34 +0100952 CHECK(counterDirectory.GetCategoryCount() == 4);
953 CHECK(categoryWoCounterSet);
954 CHECK(categoryWoCounterSet->m_Name == categoryWoCounterSetName);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100955
956 // Register a new category associated to a valid counter set
957 const std::string categoryWValidCounterSetName = "some_category_with_valid_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +0100958 const Category* categoryWValidCounterSet = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100959 CHECK_NOTHROW(categoryWValidCounterSet = counterDirectory.RegisterCategory(categoryWValidCounterSetName));
960 CHECK(counterDirectory.GetCategoryCount() == 5);
961 CHECK(categoryWValidCounterSet);
962 CHECK(categoryWValidCounterSet != category);
963 CHECK(categoryWValidCounterSet->m_Name == categoryWValidCounterSetName);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100964
965 // Register a new category associated to a valid device and counter set
966 const std::string categoryWValidDeviceAndValidCounterSetName = "some_category_with_valid_device_and_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +0100967 const Category* categoryWValidDeviceAndValidCounterSet = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100968 CHECK_NOTHROW(categoryWValidDeviceAndValidCounterSet = counterDirectory.RegisterCategory(
Sadik Armagan4c998992020-02-25 12:44:44 +0000969 categoryWValidDeviceAndValidCounterSetName));
Sadik Armagan1625efc2021-06-10 18:24:34 +0100970 CHECK(counterDirectory.GetCategoryCount() == 6);
971 CHECK(categoryWValidDeviceAndValidCounterSet);
972 CHECK(categoryWValidDeviceAndValidCounterSet != category);
973 CHECK(categoryWValidDeviceAndValidCounterSet->m_Name == categoryWValidDeviceAndValidCounterSetName);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100974}
975
Sadik Armagan1625efc2021-06-10 18:24:34 +0100976TEST_CASE("CheckCounterDirectoryRegisterDevice")
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100977{
978 CounterDirectory counterDirectory;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100979 CHECK(counterDirectory.GetCategoryCount() == 0);
980 CHECK(counterDirectory.GetDeviceCount() == 0);
981 CHECK(counterDirectory.GetCounterSetCount() == 0);
982 CHECK(counterDirectory.GetCounterCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100983
984 // Register a device with an invalid name
985 const Device* noDevice = nullptr;
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000986 CHECK_THROWS_AS(noDevice = counterDirectory.RegisterDevice(""), arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100987 CHECK(counterDirectory.GetDeviceCount() == 0);
988 CHECK(!noDevice);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100989
990 // Register a device with an invalid name
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000991 CHECK_THROWS_AS(noDevice = counterDirectory.RegisterDevice("inv@lid nam€"), arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100992 CHECK(counterDirectory.GetDeviceCount() == 0);
993 CHECK(!noDevice);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100994
995 // Register a new device with no cores or parent category
996 const std::string deviceName = "some_device";
Keith Davis3201eea2019-10-24 17:30:41 +0100997 const Device* device = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100998 CHECK_NOTHROW(device = counterDirectory.RegisterDevice(deviceName));
999 CHECK(counterDirectory.GetDeviceCount() == 1);
1000 CHECK(device);
1001 CHECK(device->m_Name == deviceName);
1002 CHECK(device->m_Uid >= 1);
1003 CHECK(device->m_Cores == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001004
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001005 // Try getting an unregistered device
1006 const Device* unregisteredDevice = counterDirectory.GetDevice(9999);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001007 CHECK(!unregisteredDevice);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001008
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001009 // Get the registered device
1010 const Device* registeredDevice = counterDirectory.GetDevice(device->m_Uid);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001011 CHECK(counterDirectory.GetDeviceCount() == 1);
1012 CHECK(registeredDevice);
1013 CHECK(registeredDevice == device);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001014
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001015 // Register a device with the name of a device already registered
1016 const Device* deviceSameName = nullptr;
Jim Flynnf9db3ef2022-03-08 21:23:44 +00001017 CHECK_THROWS_AS(deviceSameName = counterDirectory.RegisterDevice(deviceName),
1018 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001019 CHECK(counterDirectory.GetDeviceCount() == 1);
1020 CHECK(!deviceSameName);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001021
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001022 // Register a new device with cores and no parent category
1023 const std::string deviceWCoresName = "some_device_with_cores";
Keith Davis3201eea2019-10-24 17:30:41 +01001024 const Device* deviceWCores = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001025 CHECK_NOTHROW(deviceWCores = counterDirectory.RegisterDevice(deviceWCoresName, 2));
1026 CHECK(counterDirectory.GetDeviceCount() == 2);
1027 CHECK(deviceWCores);
1028 CHECK(deviceWCores->m_Name == deviceWCoresName);
1029 CHECK(deviceWCores->m_Uid >= 1);
1030 CHECK(deviceWCores->m_Uid > device->m_Uid);
1031 CHECK(deviceWCores->m_Cores == 2);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001032
1033 // Get the registered device
1034 const Device* registeredDeviceWCores = counterDirectory.GetDevice(deviceWCores->m_Uid);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001035 CHECK(counterDirectory.GetDeviceCount() == 2);
1036 CHECK(registeredDeviceWCores);
1037 CHECK(registeredDeviceWCores == deviceWCores);
1038 CHECK(registeredDeviceWCores != device);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001039
1040 // Register a new device with cores and invalid parent category
1041 const std::string deviceWCoresWInvalidParentCategoryName = "some_device_with_cores_with_invalid_parent_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001042 const Device* deviceWCoresWInvalidParentCategory = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001043 CHECK_THROWS_AS(deviceWCoresWInvalidParentCategory =
Keith Davis3201eea2019-10-24 17:30:41 +01001044 counterDirectory.RegisterDevice(deviceWCoresWInvalidParentCategoryName, 3, std::string("")),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001045 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001046 CHECK(counterDirectory.GetDeviceCount() == 2);
1047 CHECK(!deviceWCoresWInvalidParentCategory);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001048
1049 // Register a new device with cores and invalid parent category
1050 const std::string deviceWCoresWInvalidParentCategoryName2 = "some_device_with_cores_with_invalid_parent_category2";
Keith Davis3201eea2019-10-24 17:30:41 +01001051 const Device* deviceWCoresWInvalidParentCategory2 = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001052 CHECK_THROWS_AS(deviceWCoresWInvalidParentCategory2 = counterDirectory.RegisterDevice(
Keith Davis3201eea2019-10-24 17:30:41 +01001053 deviceWCoresWInvalidParentCategoryName2, 3, std::string("invalid_parent_category")),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001054 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001055 CHECK(counterDirectory.GetDeviceCount() == 2);
1056 CHECK(!deviceWCoresWInvalidParentCategory2);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001057
1058 // Register a category for testing
1059 const std::string categoryName = "some_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001060 const Category* category = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001061 CHECK_NOTHROW(category = counterDirectory.RegisterCategory(categoryName));
1062 CHECK(counterDirectory.GetCategoryCount() == 1);
1063 CHECK(category);
1064 CHECK(category->m_Name == categoryName);
1065 CHECK(category->m_Counters.empty());
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001066
1067 // Register a new device with cores and valid parent category
1068 const std::string deviceWCoresWValidParentCategoryName = "some_device_with_cores_with_valid_parent_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001069 const Device* deviceWCoresWValidParentCategory = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001070 CHECK_NOTHROW(deviceWCoresWValidParentCategory =
Keith Davis3201eea2019-10-24 17:30:41 +01001071 counterDirectory.RegisterDevice(deviceWCoresWValidParentCategoryName, 4, categoryName));
Sadik Armagan1625efc2021-06-10 18:24:34 +01001072 CHECK(counterDirectory.GetDeviceCount() == 3);
1073 CHECK(deviceWCoresWValidParentCategory);
1074 CHECK(deviceWCoresWValidParentCategory->m_Name == deviceWCoresWValidParentCategoryName);
1075 CHECK(deviceWCoresWValidParentCategory->m_Uid >= 1);
1076 CHECK(deviceWCoresWValidParentCategory->m_Uid > device->m_Uid);
1077 CHECK(deviceWCoresWValidParentCategory->m_Uid > deviceWCores->m_Uid);
1078 CHECK(deviceWCoresWValidParentCategory->m_Cores == 4);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001079}
1080
Sadik Armagan1625efc2021-06-10 18:24:34 +01001081TEST_CASE("CheckCounterDirectoryRegisterCounterSet")
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001082{
1083 CounterDirectory counterDirectory;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001084 CHECK(counterDirectory.GetCategoryCount() == 0);
1085 CHECK(counterDirectory.GetDeviceCount() == 0);
1086 CHECK(counterDirectory.GetCounterSetCount() == 0);
1087 CHECK(counterDirectory.GetCounterCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001088
1089 // Register a counter set with an invalid name
1090 const CounterSet* noCounterSet = nullptr;
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001091 CHECK_THROWS_AS(noCounterSet = counterDirectory.RegisterCounterSet(""), arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001092 CHECK(counterDirectory.GetCounterSetCount() == 0);
1093 CHECK(!noCounterSet);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001094
1095 // Register a counter set with an invalid name
Sadik Armagan1625efc2021-06-10 18:24:34 +01001096 CHECK_THROWS_AS(noCounterSet = counterDirectory.RegisterCounterSet("invalid name"),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001097 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001098 CHECK(counterDirectory.GetCounterSetCount() == 0);
1099 CHECK(!noCounterSet);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001100
1101 // Register a new counter set with no count or parent category
1102 const std::string counterSetName = "some_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +01001103 const CounterSet* counterSet = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001104 CHECK_NOTHROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
1105 CHECK(counterDirectory.GetCounterSetCount() == 1);
1106 CHECK(counterSet);
1107 CHECK(counterSet->m_Name == counterSetName);
1108 CHECK(counterSet->m_Uid >= 1);
1109 CHECK(counterSet->m_Count == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001110
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001111 // Try getting an unregistered counter set
1112 const CounterSet* unregisteredCounterSet = counterDirectory.GetCounterSet(9999);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001113 CHECK(!unregisteredCounterSet);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001114
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001115 // Get the registered counter set
1116 const CounterSet* registeredCounterSet = counterDirectory.GetCounterSet(counterSet->m_Uid);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001117 CHECK(counterDirectory.GetCounterSetCount() == 1);
1118 CHECK(registeredCounterSet);
1119 CHECK(registeredCounterSet == counterSet);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001120
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001121 // Register a counter set with the name of a counter set already registered
1122 const CounterSet* counterSetSameName = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001123 CHECK_THROWS_AS(counterSetSameName = counterDirectory.RegisterCounterSet(counterSetName),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001124 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001125 CHECK(counterDirectory.GetCounterSetCount() == 1);
1126 CHECK(!counterSetSameName);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001127
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001128 // Register a new counter set with count and no parent category
1129 const std::string counterSetWCountName = "some_counter_set_with_count";
Keith Davis3201eea2019-10-24 17:30:41 +01001130 const CounterSet* counterSetWCount = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001131 CHECK_NOTHROW(counterSetWCount = counterDirectory.RegisterCounterSet(counterSetWCountName, 37));
1132 CHECK(counterDirectory.GetCounterSetCount() == 2);
1133 CHECK(counterSetWCount);
1134 CHECK(counterSetWCount->m_Name == counterSetWCountName);
1135 CHECK(counterSetWCount->m_Uid >= 1);
1136 CHECK(counterSetWCount->m_Uid > counterSet->m_Uid);
1137 CHECK(counterSetWCount->m_Count == 37);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001138
1139 // Get the registered counter set
1140 const CounterSet* registeredCounterSetWCount = counterDirectory.GetCounterSet(counterSetWCount->m_Uid);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001141 CHECK(counterDirectory.GetCounterSetCount() == 2);
1142 CHECK(registeredCounterSetWCount);
1143 CHECK(registeredCounterSetWCount == counterSetWCount);
1144 CHECK(registeredCounterSetWCount != counterSet);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001145
1146 // Register a new counter set with count and invalid parent category
1147 const std::string counterSetWCountWInvalidParentCategoryName = "some_counter_set_with_count_"
1148 "with_invalid_parent_category";
1149 const CounterSet* counterSetWCountWInvalidParentCategory = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001150 CHECK_THROWS_AS(counterSetWCountWInvalidParentCategory = counterDirectory.RegisterCounterSet(
Keith Davis3201eea2019-10-24 17:30:41 +01001151 counterSetWCountWInvalidParentCategoryName, 42, std::string("")),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001152 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001153 CHECK(counterDirectory.GetCounterSetCount() == 2);
1154 CHECK(!counterSetWCountWInvalidParentCategory);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001155
1156 // Register a new counter set with count and invalid parent category
1157 const std::string counterSetWCountWInvalidParentCategoryName2 = "some_counter_set_with_count_"
1158 "with_invalid_parent_category2";
1159 const CounterSet* counterSetWCountWInvalidParentCategory2 = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001160 CHECK_THROWS_AS(counterSetWCountWInvalidParentCategory2 = counterDirectory.RegisterCounterSet(
Keith Davis3201eea2019-10-24 17:30:41 +01001161 counterSetWCountWInvalidParentCategoryName2, 42, std::string("invalid_parent_category")),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001162 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001163 CHECK(counterDirectory.GetCounterSetCount() == 2);
1164 CHECK(!counterSetWCountWInvalidParentCategory2);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001165
1166 // Register a category for testing
1167 const std::string categoryName = "some_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001168 const Category* category = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001169 CHECK_NOTHROW(category = counterDirectory.RegisterCategory(categoryName));
1170 CHECK(counterDirectory.GetCategoryCount() == 1);
1171 CHECK(category);
1172 CHECK(category->m_Name == categoryName);
1173 CHECK(category->m_Counters.empty());
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001174
1175 // Register a new counter set with count and valid parent category
1176 const std::string counterSetWCountWValidParentCategoryName = "some_counter_set_with_count_"
1177 "with_valid_parent_category";
1178 const CounterSet* counterSetWCountWValidParentCategory = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001179 CHECK_NOTHROW(counterSetWCountWValidParentCategory = counterDirectory.RegisterCounterSet(
Keith Davis3201eea2019-10-24 17:30:41 +01001180 counterSetWCountWValidParentCategoryName, 42, categoryName));
Sadik Armagan1625efc2021-06-10 18:24:34 +01001181 CHECK(counterDirectory.GetCounterSetCount() == 3);
1182 CHECK(counterSetWCountWValidParentCategory);
1183 CHECK(counterSetWCountWValidParentCategory->m_Name == counterSetWCountWValidParentCategoryName);
1184 CHECK(counterSetWCountWValidParentCategory->m_Uid >= 1);
1185 CHECK(counterSetWCountWValidParentCategory->m_Uid > counterSet->m_Uid);
1186 CHECK(counterSetWCountWValidParentCategory->m_Uid > counterSetWCount->m_Uid);
1187 CHECK(counterSetWCountWValidParentCategory->m_Count == 42);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001188
Sadik Armagan4c998992020-02-25 12:44:44 +00001189 // Register a counter set associated to a category with invalid name
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001190 const std::string counterSetSameCategoryName = "some_counter_set_with_invalid_parent_category";
Sadik Armagan4c998992020-02-25 12:44:44 +00001191 const std::string invalidCategoryName = "";
Keith Davis3201eea2019-10-24 17:30:41 +01001192 const CounterSet* counterSetSameCategory = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001193 CHECK_THROWS_AS(counterSetSameCategory =
Sadik Armagan4c998992020-02-25 12:44:44 +00001194 counterDirectory.RegisterCounterSet(counterSetSameCategoryName, 0, invalidCategoryName),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001195 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001196 CHECK(counterDirectory.GetCounterSetCount() == 3);
1197 CHECK(!counterSetSameCategory);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001198}
1199
Sadik Armagan1625efc2021-06-10 18:24:34 +01001200TEST_CASE("CheckCounterDirectoryRegisterCounter")
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001201{
1202 CounterDirectory counterDirectory;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001203 CHECK(counterDirectory.GetCategoryCount() == 0);
1204 CHECK(counterDirectory.GetDeviceCount() == 0);
1205 CHECK(counterDirectory.GetCounterSetCount() == 0);
1206 CHECK(counterDirectory.GetCounterCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001207
1208 // Register a counter with an invalid parent category name
1209 const Counter* noCounter = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001210 CHECK_THROWS_AS(noCounter =
Keith Davise394bd92019-12-02 15:12:19 +00001211 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1212 0,
1213 "",
1214 0,
1215 1,
1216 123.45f,
1217 "valid ",
1218 "name"),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001219 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001220 CHECK(counterDirectory.GetCounterCount() == 0);
1221 CHECK(!noCounter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001222
1223 // Register a counter with an invalid parent category name
Sadik Armagan1625efc2021-06-10 18:24:34 +01001224 CHECK_THROWS_AS(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001225 1,
1226 "invalid parent category",
1227 0,
1228 1,
1229 123.45f,
1230 "valid 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 class
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 2,
1239 "valid_parent_category",
1240 2,
1241 1,
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 interpolation
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 4,
1253 "valid_parent_category",
1254 0,
1255 3,
1256 123.45f,
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 multiplier
Sadik Armagan1625efc2021-06-10 18:24:34 +01001265 CHECK_THROWS_AS(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001266 5,
1267 "valid_parent_category",
1268 0,
1269 1,
1270 .0f,
1271 "valid "
1272 "name",
1273 "valid description"),
1274 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(
Keith Davise394bd92019-12-02 15:12:19 +00001280 noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1281 6,
1282 "valid_parent_category",
1283 0,
1284 1,
1285 123.45f,
1286 "",
1287 "valid description"),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001288 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001289 CHECK(counterDirectory.GetCounterCount() == 0);
1290 CHECK(!noCounter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001291
1292 // Register a counter with an invalid name
Sadik Armagan1625efc2021-06-10 18:24:34 +01001293 CHECK_THROWS_AS(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001294 7,
1295 "valid_parent_category",
1296 0,
1297 1,
1298 123.45f,
1299 "invalid nam€",
1300 "valid description"),
1301 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 =
Keith Davise394bd92019-12-02 15:12:19 +00001307 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1308 8,
1309 "valid_parent_category",
1310 0,
1311 1,
1312 123.45f,
1313 "valid name",
1314 ""),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001315 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 description
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 9,
1322 "valid_parent_category",
1323 0,
1324 1,
1325 123.45f,
1326 "valid "
1327 "name",
1328 "inv@lid description"),
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 an invalid unit2
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 10,
1336 "valid_parent_category",
1337 0,
1338 1,
1339 123.45f,
1340 "valid name",
1341 "valid description",
1342 std::string("Mb/s2")),
1343 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001344 CHECK(counterDirectory.GetCounterCount() == 0);
1345 CHECK(!noCounter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001346
1347 // Register a counter with a non-existing parent category name
Sadik Armagan1625efc2021-06-10 18:24:34 +01001348 CHECK_THROWS_AS(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001349 11,
1350 "invalid_parent_category",
1351 0,
1352 1,
1353 123.45f,
1354 "valid name",
1355 "valid description"),
1356 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001357 CHECK(counterDirectory.GetCounterCount() == 0);
1358 CHECK(!noCounter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001359
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001360 // Try getting an unregistered counter
1361 const Counter* unregisteredCounter = counterDirectory.GetCounter(9999);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001362 CHECK(!unregisteredCounter);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001363
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001364 // Register a category for testing
1365 const std::string categoryName = "some_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001366 const Category* category = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001367 CHECK_NOTHROW(category = counterDirectory.RegisterCategory(categoryName));
1368 CHECK(counterDirectory.GetCategoryCount() == 1);
1369 CHECK(category);
1370 CHECK(category->m_Name == categoryName);
1371 CHECK(category->m_Counters.empty());
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001372
1373 // Register a counter with a valid parent category name
1374 const Counter* counter = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001375 CHECK_NOTHROW(
Keith Davise394bd92019-12-02 15:12:19 +00001376 counter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1377 12,
1378 categoryName,
1379 0,
1380 1,
1381 123.45f,
1382 "valid name",
1383 "valid description"));
Sadik Armagan1625efc2021-06-10 18:24:34 +01001384 CHECK(counterDirectory.GetCounterCount() == 1);
1385 CHECK(counter);
1386 CHECK(counter->m_MaxCounterUid == counter->m_Uid);
1387 CHECK(counter->m_Class == 0);
1388 CHECK(counter->m_Interpolation == 1);
1389 CHECK(counter->m_Multiplier == 123.45f);
1390 CHECK(counter->m_Name == "valid name");
1391 CHECK(counter->m_Description == "valid description");
1392 CHECK(counter->m_Units == "");
1393 CHECK(counter->m_DeviceUid == 0);
1394 CHECK(counter->m_CounterSetUid == 0);
1395 CHECK(category->m_Counters.size() == 1);
1396 CHECK(category->m_Counters.back() == counter->m_Uid);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001397
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001398 // Register a counter with a name of a counter already registered for the given parent category name
1399 const Counter* counterSameName = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001400 CHECK_THROWS_AS(counterSameName =
Keith Davise394bd92019-12-02 15:12:19 +00001401 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1402 13,
1403 categoryName,
1404 0,
1405 0,
1406 1.0f,
1407 "valid name",
1408 "valid description",
1409 std::string("description")),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001410 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001411 CHECK(counterDirectory.GetCounterCount() == 1);
1412 CHECK(!counterSameName);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001413
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001414 // Register a counter with a valid parent category name and units
1415 const Counter* counterWUnits = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001416 CHECK_NOTHROW(counterWUnits = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001417 14,
1418 categoryName,
1419 0,
1420 1,
1421 123.45f,
1422 "valid name 2",
1423 "valid description",
1424 std::string("Mnnsq2"))); // Units
Sadik Armagan1625efc2021-06-10 18:24:34 +01001425 CHECK(counterDirectory.GetCounterCount() == 2);
1426 CHECK(counterWUnits);
1427 CHECK(counterWUnits->m_Uid > counter->m_Uid);
1428 CHECK(counterWUnits->m_MaxCounterUid == counterWUnits->m_Uid);
1429 CHECK(counterWUnits->m_Class == 0);
1430 CHECK(counterWUnits->m_Interpolation == 1);
1431 CHECK(counterWUnits->m_Multiplier == 123.45f);
1432 CHECK(counterWUnits->m_Name == "valid name 2");
1433 CHECK(counterWUnits->m_Description == "valid description");
1434 CHECK(counterWUnits->m_Units == "Mnnsq2");
1435 CHECK(counterWUnits->m_DeviceUid == 0);
1436 CHECK(counterWUnits->m_CounterSetUid == 0);
1437 CHECK(category->m_Counters.size() == 2);
1438 CHECK(category->m_Counters.back() == counterWUnits->m_Uid);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001439
1440 // Register a counter with a valid parent category name and not associated with a device
1441 const Counter* counterWoDevice = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001442 CHECK_NOTHROW(counterWoDevice = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001443 26,
1444 categoryName,
1445 0,
1446 1,
1447 123.45f,
1448 "valid name 3",
1449 "valid description",
Jim Flynndecd08b2022-03-13 22:35:46 +00001450 arm::pipe::EmptyOptional(),// Units
1451 arm::pipe::EmptyOptional(),// Number of cores
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001452 0)); // Device UID
Sadik Armagan1625efc2021-06-10 18:24:34 +01001453 CHECK(counterDirectory.GetCounterCount() == 3);
1454 CHECK(counterWoDevice);
1455 CHECK(counterWoDevice->m_Uid > counter->m_Uid);
1456 CHECK(counterWoDevice->m_MaxCounterUid == counterWoDevice->m_Uid);
1457 CHECK(counterWoDevice->m_Class == 0);
1458 CHECK(counterWoDevice->m_Interpolation == 1);
1459 CHECK(counterWoDevice->m_Multiplier == 123.45f);
1460 CHECK(counterWoDevice->m_Name == "valid name 3");
1461 CHECK(counterWoDevice->m_Description == "valid description");
1462 CHECK(counterWoDevice->m_Units == "");
1463 CHECK(counterWoDevice->m_DeviceUid == 0);
1464 CHECK(counterWoDevice->m_CounterSetUid == 0);
1465 CHECK(category->m_Counters.size() == 3);
1466 CHECK(category->m_Counters.back() == counterWoDevice->m_Uid);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001467
1468 // Register a counter with a valid parent category name and associated to an invalid device
Sadik Armagan1625efc2021-06-10 18:24:34 +01001469 CHECK_THROWS_AS(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001470 15,
1471 categoryName,
1472 0,
1473 1,
1474 123.45f,
1475 "valid name 4",
1476 "valid description",
Jim Flynndecd08b2022-03-13 22:35:46 +00001477 arm::pipe::EmptyOptional(), // Units
1478 arm::pipe::EmptyOptional(), // Number of cores
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001479 100), // Device UID
1480 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001481 CHECK(counterDirectory.GetCounterCount() == 3);
1482 CHECK(!noCounter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001483
1484 // Register a device for testing
1485 const std::string deviceName = "some_device";
Keith Davis3201eea2019-10-24 17:30:41 +01001486 const Device* device = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001487 CHECK_NOTHROW(device = counterDirectory.RegisterDevice(deviceName));
1488 CHECK(counterDirectory.GetDeviceCount() == 1);
1489 CHECK(device);
1490 CHECK(device->m_Name == deviceName);
1491 CHECK(device->m_Uid >= 1);
1492 CHECK(device->m_Cores == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001493
1494 // Register a counter with a valid parent category name and associated to a device
1495 const Counter* counterWDevice = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001496 CHECK_NOTHROW(counterWDevice = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001497 16,
1498 categoryName,
1499 0,
1500 1,
1501 123.45f,
1502 "valid name 5",
1503 std::string("valid description"),
Jim Flynndecd08b2022-03-13 22:35:46 +00001504 arm::pipe::EmptyOptional(), // Units
1505 arm::pipe::EmptyOptional(), // Number of cores
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001506 device->m_Uid)); // Device UID
Sadik Armagan1625efc2021-06-10 18:24:34 +01001507 CHECK(counterDirectory.GetCounterCount() == 4);
1508 CHECK(counterWDevice);
1509 CHECK(counterWDevice->m_Uid > counter->m_Uid);
1510 CHECK(counterWDevice->m_MaxCounterUid == counterWDevice->m_Uid);
1511 CHECK(counterWDevice->m_Class == 0);
1512 CHECK(counterWDevice->m_Interpolation == 1);
1513 CHECK(counterWDevice->m_Multiplier == 123.45f);
1514 CHECK(counterWDevice->m_Name == "valid name 5");
1515 CHECK(counterWDevice->m_Description == "valid description");
1516 CHECK(counterWDevice->m_Units == "");
1517 CHECK(counterWDevice->m_DeviceUid == device->m_Uid);
1518 CHECK(counterWDevice->m_CounterSetUid == 0);
1519 CHECK(category->m_Counters.size() == 4);
1520 CHECK(category->m_Counters.back() == counterWDevice->m_Uid);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001521
1522 // Register a counter with a valid parent category name and not associated with a counter set
1523 const Counter* counterWoCounterSet = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001524 CHECK_NOTHROW(counterWoCounterSet = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001525 17,
1526 categoryName,
1527 0,
1528 1,
1529 123.45f,
1530 "valid name 6",
1531 "valid description",
Jim Flynndecd08b2022-03-13 22:35:46 +00001532 arm::pipe::EmptyOptional(),// Units
1533 arm::pipe::EmptyOptional(),// No of cores
1534 arm::pipe::EmptyOptional(),// Device UID
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001535 0)); // CounterSet UID
Sadik Armagan1625efc2021-06-10 18:24:34 +01001536 CHECK(counterDirectory.GetCounterCount() == 5);
1537 CHECK(counterWoCounterSet);
1538 CHECK(counterWoCounterSet->m_Uid > counter->m_Uid);
1539 CHECK(counterWoCounterSet->m_MaxCounterUid == counterWoCounterSet->m_Uid);
1540 CHECK(counterWoCounterSet->m_Class == 0);
1541 CHECK(counterWoCounterSet->m_Interpolation == 1);
1542 CHECK(counterWoCounterSet->m_Multiplier == 123.45f);
1543 CHECK(counterWoCounterSet->m_Name == "valid name 6");
1544 CHECK(counterWoCounterSet->m_Description == "valid description");
1545 CHECK(counterWoCounterSet->m_Units == "");
1546 CHECK(counterWoCounterSet->m_DeviceUid == 0);
1547 CHECK(counterWoCounterSet->m_CounterSetUid == 0);
1548 CHECK(category->m_Counters.size() == 5);
1549 CHECK(category->m_Counters.back() == counterWoCounterSet->m_Uid);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001550
1551 // Register a counter with a valid parent category name and associated to an invalid counter set
Sadik Armagan1625efc2021-06-10 18:24:34 +01001552 CHECK_THROWS_AS(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001553 18,
1554 categoryName,
1555 0,
1556 1,
1557 123.45f,
1558 "valid ",
1559 "name 7",
1560 std::string("valid description"),
Jim Flynndecd08b2022-03-13 22:35:46 +00001561 arm::pipe::EmptyOptional(), // Units
1562 arm::pipe::EmptyOptional(), // Number of cores
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001563 100), // Counter set UID
1564 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001565 CHECK(counterDirectory.GetCounterCount() == 5);
1566 CHECK(!noCounter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001567
1568 // Register a counter with a valid parent category name and with a given number of cores
1569 const Counter* counterWNumberOfCores = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001570 uint16_t numberOfCores = 15;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001571 CHECK_NOTHROW(counterWNumberOfCores = counterDirectory.RegisterCounter(
Keith Davise394bd92019-12-02 15:12:19 +00001572 armnn::profiling::BACKEND_ID, 50,
Keith Davis3201eea2019-10-24 17:30:41 +01001573 categoryName, 0, 1, 123.45f, "valid name 8", "valid description",
Jim Flynndecd08b2022-03-13 22:35:46 +00001574 arm::pipe::EmptyOptional(), // Units
Keith Davis3201eea2019-10-24 17:30:41 +01001575 numberOfCores, // Number of cores
Jim Flynndecd08b2022-03-13 22:35:46 +00001576 arm::pipe::EmptyOptional(), // Device UID
1577 arm::pipe::EmptyOptional())); // Counter set UID
Sadik Armagan1625efc2021-06-10 18:24:34 +01001578 CHECK(counterDirectory.GetCounterCount() == 20);
1579 CHECK(counterWNumberOfCores);
1580 CHECK(counterWNumberOfCores->m_Uid > counter->m_Uid);
1581 CHECK(counterWNumberOfCores->m_MaxCounterUid == counterWNumberOfCores->m_Uid + numberOfCores - 1);
1582 CHECK(counterWNumberOfCores->m_Class == 0);
1583 CHECK(counterWNumberOfCores->m_Interpolation == 1);
1584 CHECK(counterWNumberOfCores->m_Multiplier == 123.45f);
1585 CHECK(counterWNumberOfCores->m_Name == "valid name 8");
1586 CHECK(counterWNumberOfCores->m_Description == "valid description");
1587 CHECK(counterWNumberOfCores->m_Units == "");
1588 CHECK(counterWNumberOfCores->m_DeviceUid == 0);
1589 CHECK(counterWNumberOfCores->m_CounterSetUid == 0);
1590 CHECK(category->m_Counters.size() == 20);
Keith Davis3201eea2019-10-24 17:30:41 +01001591 for (size_t i = 0; i < numberOfCores; i++)
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001592 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01001593 CHECK(category->m_Counters[category->m_Counters.size() - numberOfCores + i] ==
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001594 counterWNumberOfCores->m_Uid + i);
1595 }
1596
1597 // Register a multi-core device for testing
1598 const std::string multiCoreDeviceName = "some_multi_core_device";
Keith Davis3201eea2019-10-24 17:30:41 +01001599 const Device* multiCoreDevice = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001600 CHECK_NOTHROW(multiCoreDevice = counterDirectory.RegisterDevice(multiCoreDeviceName, 4));
1601 CHECK(counterDirectory.GetDeviceCount() == 2);
1602 CHECK(multiCoreDevice);
1603 CHECK(multiCoreDevice->m_Name == multiCoreDeviceName);
1604 CHECK(multiCoreDevice->m_Uid >= 1);
1605 CHECK(multiCoreDevice->m_Cores == 4);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001606
1607 // Register a counter with a valid parent category name and associated to the multi-core device
1608 const Counter* counterWMultiCoreDevice = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001609 CHECK_NOTHROW(counterWMultiCoreDevice = counterDirectory.RegisterCounter(
Keith Davise394bd92019-12-02 15:12:19 +00001610 armnn::profiling::BACKEND_ID, 19, categoryName, 0, 1,
1611 123.45f, "valid name 9", "valid description",
Jim Flynndecd08b2022-03-13 22:35:46 +00001612 arm::pipe::EmptyOptional(), // Units
1613 arm::pipe::EmptyOptional(), // Number of cores
Keith Davis3201eea2019-10-24 17:30:41 +01001614 multiCoreDevice->m_Uid, // Device UID
Jim Flynndecd08b2022-03-13 22:35:46 +00001615 arm::pipe::EmptyOptional())); // Counter set UID
Sadik Armagan1625efc2021-06-10 18:24:34 +01001616 CHECK(counterDirectory.GetCounterCount() == 24);
1617 CHECK(counterWMultiCoreDevice);
1618 CHECK(counterWMultiCoreDevice->m_Uid > counter->m_Uid);
1619 CHECK(counterWMultiCoreDevice->m_MaxCounterUid ==
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001620 counterWMultiCoreDevice->m_Uid + multiCoreDevice->m_Cores - 1);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001621 CHECK(counterWMultiCoreDevice->m_Class == 0);
1622 CHECK(counterWMultiCoreDevice->m_Interpolation == 1);
1623 CHECK(counterWMultiCoreDevice->m_Multiplier == 123.45f);
1624 CHECK(counterWMultiCoreDevice->m_Name == "valid name 9");
1625 CHECK(counterWMultiCoreDevice->m_Description == "valid description");
1626 CHECK(counterWMultiCoreDevice->m_Units == "");
1627 CHECK(counterWMultiCoreDevice->m_DeviceUid == multiCoreDevice->m_Uid);
1628 CHECK(counterWMultiCoreDevice->m_CounterSetUid == 0);
1629 CHECK(category->m_Counters.size() == 24);
Keith Davis3201eea2019-10-24 17:30:41 +01001630 for (size_t i = 0; i < 4; i++)
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001631 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01001632 CHECK(category->m_Counters[category->m_Counters.size() - 4 + i] == counterWMultiCoreDevice->m_Uid + i);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001633 }
1634
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001635 // Register a multi-core device associate to a parent category for testing
1636 const std::string multiCoreDeviceNameWParentCategory = "some_multi_core_device_with_parent_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001637 const Device* multiCoreDeviceWParentCategory = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001638 CHECK_NOTHROW(multiCoreDeviceWParentCategory =
Keith Davis3201eea2019-10-24 17:30:41 +01001639 counterDirectory.RegisterDevice(multiCoreDeviceNameWParentCategory, 2, categoryName));
Sadik Armagan1625efc2021-06-10 18:24:34 +01001640 CHECK(counterDirectory.GetDeviceCount() == 3);
1641 CHECK(multiCoreDeviceWParentCategory);
1642 CHECK(multiCoreDeviceWParentCategory->m_Name == multiCoreDeviceNameWParentCategory);
1643 CHECK(multiCoreDeviceWParentCategory->m_Uid >= 1);
1644 CHECK(multiCoreDeviceWParentCategory->m_Cores == 2);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001645
1646 // Register a counter with a valid parent category name and getting the number of cores of the multi-core device
1647 // associated to that category
1648 const Counter* counterWMultiCoreDeviceWParentCategory = nullptr;
Sadik Armagan4c998992020-02-25 12:44:44 +00001649 uint16_t numberOfCourse = multiCoreDeviceWParentCategory->m_Cores;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001650 CHECK_NOTHROW(counterWMultiCoreDeviceWParentCategory =
Jim Flynnbbfe6032020-07-20 16:57:44 +01001651 counterDirectory.RegisterCounter(
1652 armnn::profiling::BACKEND_ID,
1653 100,
1654 categoryName,
1655 0,
1656 1,
1657 123.45f,
1658 "valid name 10",
1659 "valid description",
Jim Flynndecd08b2022-03-13 22:35:46 +00001660 arm::pipe::EmptyOptional(), // Units
Jim Flynnbbfe6032020-07-20 16:57:44 +01001661 numberOfCourse, // Number of cores
Jim Flynndecd08b2022-03-13 22:35:46 +00001662 arm::pipe::EmptyOptional(), // Device UID
1663 arm::pipe::EmptyOptional()));// Counter set UID
Sadik Armagan1625efc2021-06-10 18:24:34 +01001664 CHECK(counterDirectory.GetCounterCount() == 26);
1665 CHECK(counterWMultiCoreDeviceWParentCategory);
1666 CHECK(counterWMultiCoreDeviceWParentCategory->m_Uid > counter->m_Uid);
1667 CHECK(counterWMultiCoreDeviceWParentCategory->m_MaxCounterUid ==
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001668 counterWMultiCoreDeviceWParentCategory->m_Uid + multiCoreDeviceWParentCategory->m_Cores - 1);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001669 CHECK(counterWMultiCoreDeviceWParentCategory->m_Class == 0);
1670 CHECK(counterWMultiCoreDeviceWParentCategory->m_Interpolation == 1);
1671 CHECK(counterWMultiCoreDeviceWParentCategory->m_Multiplier == 123.45f);
1672 CHECK(counterWMultiCoreDeviceWParentCategory->m_Name == "valid name 10");
1673 CHECK(counterWMultiCoreDeviceWParentCategory->m_Description == "valid description");
1674 CHECK(counterWMultiCoreDeviceWParentCategory->m_Units == "");
1675 CHECK(category->m_Counters.size() == 26);
Keith Davis3201eea2019-10-24 17:30:41 +01001676 for (size_t i = 0; i < 2; i++)
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001677 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01001678 CHECK(category->m_Counters[category->m_Counters.size() - 2 + i] ==
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001679 counterWMultiCoreDeviceWParentCategory->m_Uid + i);
1680 }
1681
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001682 // Register a counter set for testing
1683 const std::string counterSetName = "some_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +01001684 const CounterSet* counterSet = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001685 CHECK_NOTHROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
1686 CHECK(counterDirectory.GetCounterSetCount() == 1);
1687 CHECK(counterSet);
1688 CHECK(counterSet->m_Name == counterSetName);
1689 CHECK(counterSet->m_Uid >= 1);
1690 CHECK(counterSet->m_Count == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001691
1692 // Register a counter with a valid parent category name and associated to a counter set
1693 const Counter* counterWCounterSet = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001694 CHECK_NOTHROW(counterWCounterSet = counterDirectory.RegisterCounter(
Keith Davise394bd92019-12-02 15:12:19 +00001695 armnn::profiling::BACKEND_ID, 300,
Keith Davis3201eea2019-10-24 17:30:41 +01001696 categoryName, 0, 1, 123.45f, "valid name 11", "valid description",
Jim Flynndecd08b2022-03-13 22:35:46 +00001697 arm::pipe::EmptyOptional(), // Units
Keith Davis3201eea2019-10-24 17:30:41 +01001698 0, // Number of cores
Jim Flynndecd08b2022-03-13 22:35:46 +00001699 arm::pipe::EmptyOptional(), // Device UID
Keith Davis3201eea2019-10-24 17:30:41 +01001700 counterSet->m_Uid)); // Counter set UID
Sadik Armagan1625efc2021-06-10 18:24:34 +01001701 CHECK(counterDirectory.GetCounterCount() == 27);
1702 CHECK(counterWCounterSet);
1703 CHECK(counterWCounterSet->m_Uid > counter->m_Uid);
1704 CHECK(counterWCounterSet->m_MaxCounterUid == counterWCounterSet->m_Uid);
1705 CHECK(counterWCounterSet->m_Class == 0);
1706 CHECK(counterWCounterSet->m_Interpolation == 1);
1707 CHECK(counterWCounterSet->m_Multiplier == 123.45f);
1708 CHECK(counterWCounterSet->m_Name == "valid name 11");
1709 CHECK(counterWCounterSet->m_Description == "valid description");
1710 CHECK(counterWCounterSet->m_Units == "");
1711 CHECK(counterWCounterSet->m_DeviceUid == 0);
1712 CHECK(counterWCounterSet->m_CounterSetUid == counterSet->m_Uid);
1713 CHECK(category->m_Counters.size() == 27);
1714 CHECK(category->m_Counters.back() == counterWCounterSet->m_Uid);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001715
1716 // Register a counter with a valid parent category name and associated to a device and a counter set
1717 const Counter* counterWDeviceWCounterSet = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001718 CHECK_NOTHROW(counterWDeviceWCounterSet = counterDirectory.RegisterCounter(
Keith Davise394bd92019-12-02 15:12:19 +00001719 armnn::profiling::BACKEND_ID, 23,
Keith Davis3201eea2019-10-24 17:30:41 +01001720 categoryName, 0, 1, 123.45f, "valid name 12", "valid description",
Jim Flynndecd08b2022-03-13 22:35:46 +00001721 arm::pipe::EmptyOptional(), // Units
Keith Davis3201eea2019-10-24 17:30:41 +01001722 1, // Number of cores
1723 device->m_Uid, // Device UID
1724 counterSet->m_Uid)); // Counter set UID
Sadik Armagan1625efc2021-06-10 18:24:34 +01001725 CHECK(counterDirectory.GetCounterCount() == 28);
1726 CHECK(counterWDeviceWCounterSet);
1727 CHECK(counterWDeviceWCounterSet->m_Uid > counter->m_Uid);
1728 CHECK(counterWDeviceWCounterSet->m_MaxCounterUid == counterWDeviceWCounterSet->m_Uid);
1729 CHECK(counterWDeviceWCounterSet->m_Class == 0);
1730 CHECK(counterWDeviceWCounterSet->m_Interpolation == 1);
1731 CHECK(counterWDeviceWCounterSet->m_Multiplier == 123.45f);
1732 CHECK(counterWDeviceWCounterSet->m_Name == "valid name 12");
1733 CHECK(counterWDeviceWCounterSet->m_Description == "valid description");
1734 CHECK(counterWDeviceWCounterSet->m_Units == "");
1735 CHECK(counterWDeviceWCounterSet->m_DeviceUid == device->m_Uid);
1736 CHECK(counterWDeviceWCounterSet->m_CounterSetUid == counterSet->m_Uid);
1737 CHECK(category->m_Counters.size() == 28);
1738 CHECK(category->m_Counters.back() == counterWDeviceWCounterSet->m_Uid);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001739
1740 // Register another category for testing
1741 const std::string anotherCategoryName = "some_other_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001742 const Category* anotherCategory = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001743 CHECK_NOTHROW(anotherCategory = counterDirectory.RegisterCategory(anotherCategoryName));
1744 CHECK(counterDirectory.GetCategoryCount() == 2);
1745 CHECK(anotherCategory);
1746 CHECK(anotherCategory != category);
1747 CHECK(anotherCategory->m_Name == anotherCategoryName);
1748 CHECK(anotherCategory->m_Counters.empty());
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001749
1750 // Register a counter to the other category
1751 const Counter* anotherCounter = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001752 CHECK_NOTHROW(anotherCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID, 24,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001753 anotherCategoryName, 1, 0, .00043f,
1754 "valid name", "valid description",
Jim Flynndecd08b2022-03-13 22:35:46 +00001755 arm::pipe::EmptyOptional(), // Units
1756 arm::pipe::EmptyOptional(), // Number of cores
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001757 device->m_Uid, // Device UID
1758 counterSet->m_Uid)); // Counter set UID
Sadik Armagan1625efc2021-06-10 18:24:34 +01001759 CHECK(counterDirectory.GetCounterCount() == 29);
1760 CHECK(anotherCounter);
1761 CHECK(anotherCounter->m_MaxCounterUid == anotherCounter->m_Uid);
1762 CHECK(anotherCounter->m_Class == 1);
1763 CHECK(anotherCounter->m_Interpolation == 0);
1764 CHECK(anotherCounter->m_Multiplier == .00043f);
1765 CHECK(anotherCounter->m_Name == "valid name");
1766 CHECK(anotherCounter->m_Description == "valid description");
1767 CHECK(anotherCounter->m_Units == "");
1768 CHECK(anotherCounter->m_DeviceUid == device->m_Uid);
1769 CHECK(anotherCounter->m_CounterSetUid == counterSet->m_Uid);
1770 CHECK(anotherCategory->m_Counters.size() == 1);
1771 CHECK(anotherCategory->m_Counters.back() == anotherCounter->m_Uid);
Matteo Martincighab173e92019-09-05 12:02:04 +01001772}
1773
Sadik Armagan1625efc2021-06-10 18:24:34 +01001774TEST_CASE("CounterSelectionCommandHandlerParseData")
Ferran Balaguer1b941722019-08-28 16:57:18 +01001775{
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01001776 ProfilingStateMachine profilingStateMachine;
1777
Ferran Balaguer1b941722019-08-28 16:57:18 +01001778 class TestCaptureThread : public IPeriodicCounterCapture
1779 {
Keith Davis3201eea2019-10-24 17:30:41 +01001780 void Start() override
1781 {}
1782 void Stop() override
1783 {}
Ferran Balaguer1b941722019-08-28 16:57:18 +01001784 };
1785
Matteo Martincighe8485382019-10-10 14:08:21 +01001786 class TestReadCounterValues : public IReadCounterValues
1787 {
Keith Davis3201eea2019-10-24 17:30:41 +01001788 bool IsCounterRegistered(uint16_t counterUid) const override
1789 {
Jim Flynn9265a882022-03-10 23:35:26 +00001790 arm::pipe::IgnoreUnused(counterUid);
Keith Davis3201eea2019-10-24 17:30:41 +01001791 return true;
1792 }
Jim Flynn34430252022-03-04 15:03:58 +00001793 bool IsCounterRegistered(const std::string& counterName) const override
1794 {
Jim Flynn9265a882022-03-10 23:35:26 +00001795 arm::pipe::IgnoreUnused(counterName);
Jim Flynn34430252022-03-04 15:03:58 +00001796 return true;
1797 }
Keith Davis3201eea2019-10-24 17:30:41 +01001798 uint16_t GetCounterCount() const override
1799 {
1800 return 0;
1801 }
Finn Williamsf3fcf322020-05-11 14:38:02 +01001802 uint32_t GetAbsoluteCounterValue(uint16_t counterUid) const override
1803 {
Jim Flynn9265a882022-03-10 23:35:26 +00001804 arm::pipe::IgnoreUnused(counterUid);
Finn Williamsf3fcf322020-05-11 14:38:02 +01001805 return 0;
1806 }
1807 uint32_t GetDeltaCounterValue(uint16_t counterUid) override
Keith Davis3201eea2019-10-24 17:30:41 +01001808 {
Jim Flynn9265a882022-03-10 23:35:26 +00001809 arm::pipe::IgnoreUnused(counterUid);
Keith Davis3201eea2019-10-24 17:30:41 +01001810 return 0;
1811 }
Matteo Martincighe8485382019-10-10 14:08:21 +01001812 };
Jim Flynn397043f2019-10-17 17:37:10 +01001813 const uint32_t familyId = 0;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001814 const uint32_t packetId = 0x40000;
1815
1816 uint32_t version = 1;
Cathal Corbett6f073722022-03-04 12:11:09 +00001817 const std::unordered_map<std::string,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001818 std::shared_ptr<IBackendProfilingContext>> backendProfilingContext;
Finn Williams032bc742020-02-12 11:02:34 +00001819 CounterIdMap counterIdMap;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001820 Holder holder;
1821 TestCaptureThread captureThread;
Matteo Martincighe8485382019-10-10 14:08:21 +01001822 TestReadCounterValues readCounterValues;
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001823 MockBufferManager mockBuffer(512);
Jim Flynn9c85b412022-03-16 00:27:43 +00001824 SendCounterPacket sendCounterPacket(mockBuffer,
1825 arm::pipe::ARMNN_SOFTWARE_INFO,
1826 arm::pipe::ARMNN_SOFTWARE_VERSION,
1827 arm::pipe::ARMNN_HARDWARE_VERSION);
Sadik Armagan3896b472020-02-10 12:24:15 +00001828 SendThread sendThread(profilingStateMachine, mockBuffer, sendCounterPacket);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001829
Jim Flynn75c14f42022-03-10 22:05:42 +00001830 uint32_t sizeOfUint32 = arm::pipe::numeric_cast<uint32_t>(sizeof(uint32_t));
1831 uint32_t sizeOfUint16 = arm::pipe::numeric_cast<uint32_t>(sizeof(uint16_t));
Ferran Balaguer1b941722019-08-28 16:57:18 +01001832
1833 // Data with period and counters
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00001834 uint32_t period1 = arm::pipe::LOWEST_CAPTURE_PERIOD;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001835 uint32_t dataLength1 = 8;
Keith Davis3201eea2019-10-24 17:30:41 +01001836 uint32_t offset = 0;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001837
Matteo Martincigh67ef2a52019-10-10 13:29:02 +01001838 std::unique_ptr<unsigned char[]> uniqueData1 = std::make_unique<unsigned char[]>(dataLength1);
Keith Davis3201eea2019-10-24 17:30:41 +01001839 unsigned char* data1 = reinterpret_cast<unsigned char*>(uniqueData1.get());
FinnWilliamsArma0c78712019-09-16 12:06:47 +01001840
Ferran Balaguer1b941722019-08-28 16:57:18 +01001841 WriteUint32(data1, offset, period1);
1842 offset += sizeOfUint32;
1843 WriteUint16(data1, offset, 4000);
1844 offset += sizeOfUint16;
1845 WriteUint16(data1, offset, 5000);
1846
Jim Flynnbbfe6032020-07-20 16:57:44 +01001847 arm::pipe::Packet packetA(packetId, dataLength1, uniqueData1);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001848
Finn Williams032bc742020-02-12 11:02:34 +00001849 PeriodicCounterSelectionCommandHandler commandHandler(familyId, packetId, version, backendProfilingContext,
1850 counterIdMap, holder, 10000u, captureThread,
Keith Davis3201eea2019-10-24 17:30:41 +01001851 readCounterValues, sendCounterPacket, profilingStateMachine);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001852
Matteo Martincighe8485382019-10-10 14:08:21 +01001853 profilingStateMachine.TransitionToState(ProfilingState::Uninitialised);
Jim Flynnf9db3ef2022-03-08 21:23:44 +00001854 CHECK_THROWS_AS(commandHandler(packetA), arm::pipe::ProfilingException);
Matteo Martincighe8485382019-10-10 14:08:21 +01001855 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
Jim Flynnf9db3ef2022-03-08 21:23:44 +00001856 CHECK_THROWS_AS(commandHandler(packetA), arm::pipe::ProfilingException);
Matteo Martincighe8485382019-10-10 14:08:21 +01001857 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
Jim Flynnf9db3ef2022-03-08 21:23:44 +00001858 CHECK_THROWS_AS(commandHandler(packetA), arm::pipe::ProfilingException);
Matteo Martincighe8485382019-10-10 14:08:21 +01001859 profilingStateMachine.TransitionToState(ProfilingState::Active);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001860 CHECK_NOTHROW(commandHandler(packetA));
Matteo Martincighe8485382019-10-10 14:08:21 +01001861
1862 const std::vector<uint16_t> counterIdsA = holder.GetCaptureData().GetCounterIds();
Ferran Balaguer1b941722019-08-28 16:57:18 +01001863
Sadik Armagan1625efc2021-06-10 18:24:34 +01001864 CHECK(holder.GetCaptureData().GetCapturePeriod() == period1);
1865 CHECK(counterIdsA.size() == 2);
1866 CHECK(counterIdsA[0] == 4000);
1867 CHECK(counterIdsA[1] == 5000);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001868
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001869 auto readBuffer = mockBuffer.GetReadableBuffer();
Ferran Balaguer1b941722019-08-28 16:57:18 +01001870
1871 offset = 0;
1872
1873 uint32_t headerWord0 = ReadUint32(readBuffer, offset);
1874 offset += sizeOfUint32;
1875 uint32_t headerWord1 = ReadUint32(readBuffer, offset);
1876 offset += sizeOfUint32;
1877 uint32_t period = ReadUint32(readBuffer, offset);
1878
Sadik Armagan1625efc2021-06-10 18:24:34 +01001879 CHECK(((headerWord0 >> 26) & 0x3F) == 0); // packet family
1880 CHECK(((headerWord0 >> 16) & 0x3FF) == 4); // packet id
1881 CHECK(headerWord1 == 8); // data length
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00001882 CHECK(period == arm::pipe::LOWEST_CAPTURE_PERIOD); // capture period
Ferran Balaguer1b941722019-08-28 16:57:18 +01001883
1884 uint16_t counterId = 0;
1885 offset += sizeOfUint32;
1886 counterId = ReadUint16(readBuffer, offset);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001887 CHECK(counterId == 4000);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001888 offset += sizeOfUint16;
1889 counterId = ReadUint16(readBuffer, offset);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001890 CHECK(counterId == 5000);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001891
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001892 mockBuffer.MarkRead(readBuffer);
1893
Ferran Balaguer1b941722019-08-28 16:57:18 +01001894 // Data with period only
Colm Donelan02705242019-11-14 14:19:07 +00001895 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 +01001896 uint32_t dataLength2 = 4;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001897
Matteo Martincigh67ef2a52019-10-10 13:29:02 +01001898 std::unique_ptr<unsigned char[]> uniqueData2 = std::make_unique<unsigned char[]>(dataLength2);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001899
FinnWilliamsArma0c78712019-09-16 12:06:47 +01001900 WriteUint32(reinterpret_cast<unsigned char*>(uniqueData2.get()), 0, period2);
1901
Jim Flynnbbfe6032020-07-20 16:57:44 +01001902 arm::pipe::Packet packetB(packetId, dataLength2, uniqueData2);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001903
1904 commandHandler(packetB);
1905
Matteo Martincighe8485382019-10-10 14:08:21 +01001906 const std::vector<uint16_t> counterIdsB = holder.GetCaptureData().GetCounterIds();
Ferran Balaguer1b941722019-08-28 16:57:18 +01001907
Colm Donelan02705242019-11-14 14:19:07 +00001908 // Value should have been pulled up from 9000 to LOWEST_CAPTURE_PERIOD.
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00001909 CHECK(holder.GetCaptureData().GetCapturePeriod() == arm::pipe::LOWEST_CAPTURE_PERIOD);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001910 CHECK(counterIdsB.size() == 0);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001911
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001912 readBuffer = mockBuffer.GetReadableBuffer();
Ferran Balaguer1b941722019-08-28 16:57:18 +01001913
1914 offset = 0;
1915
1916 headerWord0 = ReadUint32(readBuffer, offset);
1917 offset += sizeOfUint32;
1918 headerWord1 = ReadUint32(readBuffer, offset);
1919 offset += sizeOfUint32;
1920 period = ReadUint32(readBuffer, offset);
1921
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00001922 CHECK(((headerWord0 >> 26) & 0x3F) == 0); // packet family
1923 CHECK(((headerWord0 >> 16) & 0x3FF) == 4); // packet id
1924 CHECK(headerWord1 == 4); // data length
1925 CHECK(period == arm::pipe::LOWEST_CAPTURE_PERIOD); // capture period
Ferran Balaguer1b941722019-08-28 16:57:18 +01001926}
1927
Sadik Armagan1625efc2021-06-10 18:24:34 +01001928TEST_CASE("CheckTimelineActivationAndDeactivation")
Keith Davis33ed2212020-03-30 10:43:41 +01001929{
1930 class TestReportStructure : public IReportStructure
1931 {
1932 public:
Jim Flynn9c85b412022-03-16 00:27:43 +00001933 virtual void ReportStructure(arm::pipe::IProfilingService& profilingService) override
Keith Davis33ed2212020-03-30 10:43:41 +01001934 {
Jim Flynn9c85b412022-03-16 00:27:43 +00001935 arm::pipe::IgnoreUnused(profilingService);
Keith Davis33ed2212020-03-30 10:43:41 +01001936 m_ReportStructureCalled = true;
1937 }
1938
1939 bool m_ReportStructureCalled = false;
1940 };
1941
1942 class TestNotifyBackends : public INotifyBackends
1943 {
1944 public:
1945 TestNotifyBackends() : m_timelineReporting(false) {}
1946 virtual void NotifyBackendsForTimelineReporting() override
1947 {
1948 m_TestNotifyBackendsCalled = m_timelineReporting.load();
1949 }
1950
1951 bool m_TestNotifyBackendsCalled = false;
1952 std::atomic<bool> m_timelineReporting;
1953 };
1954
Jim Flynnbbfe6032020-07-20 16:57:44 +01001955 arm::pipe::PacketVersionResolver packetVersionResolver;
Keith Davis33ed2212020-03-30 10:43:41 +01001956
1957 BufferManager bufferManager(512);
1958 SendTimelinePacket sendTimelinePacket(bufferManager);
1959 ProfilingStateMachine stateMachine;
1960 TestReportStructure testReportStructure;
1961 TestNotifyBackends testNotifyBackends;
Jim Flynn9c85b412022-03-16 00:27:43 +00001962 armnn::ArmNNProfilingServiceInitialiser initialiser;
1963 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER,
1964 initialiser,
1965 arm::pipe::ARMNN_SOFTWARE_INFO,
1966 arm::pipe::ARMNN_SOFTWARE_VERSION,
1967 arm::pipe::ARMNN_HARDWARE_VERSION);
1968
Keith Davis33ed2212020-03-30 10:43:41 +01001969
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001970 ActivateTimelineReportingCommandHandler activateTimelineReportingCommandHandler(0,
Keith Davis33ed2212020-03-30 10:43:41 +01001971 6,
1972 packetVersionResolver.ResolvePacketVersion(0, 6)
1973 .GetEncodedValue(),
1974 sendTimelinePacket,
1975 stateMachine,
1976 testReportStructure,
1977 testNotifyBackends.m_timelineReporting,
Jim Flynn9c85b412022-03-16 00:27:43 +00001978 testNotifyBackends,
1979 profilingService);
Keith Davis33ed2212020-03-30 10:43:41 +01001980
1981 // Write an "ActivateTimelineReporting" packet into the mock profiling connection, to simulate an input from an
1982 // external profiling service
1983 const uint32_t packetFamily1 = 0;
1984 const uint32_t packetId1 = 6;
1985 uint32_t packetHeader1 = ConstructHeader(packetFamily1, packetId1);
1986
1987 // Create the ActivateTimelineReportingPacket
Jim Flynnbbfe6032020-07-20 16:57:44 +01001988 arm::pipe::Packet ActivateTimelineReportingPacket(packetHeader1); // Length == 0
Keith Davis33ed2212020-03-30 10:43:41 +01001989
Sadik Armagan1625efc2021-06-10 18:24:34 +01001990 CHECK_THROWS_AS(
Jim Flynnf9db3ef2022-03-08 21:23:44 +00001991 activateTimelineReportingCommandHandler.operator()(ActivateTimelineReportingPacket),
1992 arm::pipe::ProfilingException);
Keith Davis33ed2212020-03-30 10:43:41 +01001993
1994 stateMachine.TransitionToState(ProfilingState::NotConnected);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001995 CHECK_THROWS_AS(
Jim Flynnf9db3ef2022-03-08 21:23:44 +00001996 activateTimelineReportingCommandHandler.operator()(ActivateTimelineReportingPacket),
1997 arm::pipe::ProfilingException);
Keith Davis33ed2212020-03-30 10:43:41 +01001998
1999 stateMachine.TransitionToState(ProfilingState::WaitingForAck);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002000 CHECK_THROWS_AS(
Jim Flynnf9db3ef2022-03-08 21:23:44 +00002001 activateTimelineReportingCommandHandler.operator()(ActivateTimelineReportingPacket),
2002 arm::pipe::ProfilingException);
Keith Davis33ed2212020-03-30 10:43:41 +01002003
2004 stateMachine.TransitionToState(ProfilingState::Active);
2005 activateTimelineReportingCommandHandler.operator()(ActivateTimelineReportingPacket);
2006
Sadik Armagan1625efc2021-06-10 18:24:34 +01002007 CHECK(testReportStructure.m_ReportStructureCalled);
2008 CHECK(testNotifyBackends.m_TestNotifyBackendsCalled);
2009 CHECK(testNotifyBackends.m_timelineReporting.load());
Keith Davis33ed2212020-03-30 10:43:41 +01002010
2011 DeactivateTimelineReportingCommandHandler deactivateTimelineReportingCommandHandler(0,
2012 7,
2013 packetVersionResolver.ResolvePacketVersion(0, 7).GetEncodedValue(),
2014 testNotifyBackends.m_timelineReporting,
2015 stateMachine,
2016 testNotifyBackends);
2017
2018 const uint32_t packetFamily2 = 0;
2019 const uint32_t packetId2 = 7;
2020 uint32_t packetHeader2 = ConstructHeader(packetFamily2, packetId2);
2021
2022 // Create the DeactivateTimelineReportingPacket
Jim Flynnbbfe6032020-07-20 16:57:44 +01002023 arm::pipe::Packet deactivateTimelineReportingPacket(packetHeader2); // Length == 0
Keith Davis33ed2212020-03-30 10:43:41 +01002024
2025 stateMachine.Reset();
Sadik Armagan1625efc2021-06-10 18:24:34 +01002026 CHECK_THROWS_AS(
Jim Flynnf9db3ef2022-03-08 21:23:44 +00002027 deactivateTimelineReportingCommandHandler.operator()(deactivateTimelineReportingPacket),
2028 arm::pipe::ProfilingException);
Keith Davis33ed2212020-03-30 10:43:41 +01002029
2030 stateMachine.TransitionToState(ProfilingState::NotConnected);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002031 CHECK_THROWS_AS(
Jim Flynnf9db3ef2022-03-08 21:23:44 +00002032 deactivateTimelineReportingCommandHandler.operator()(deactivateTimelineReportingPacket),
2033 arm::pipe::ProfilingException);
Keith Davis33ed2212020-03-30 10:43:41 +01002034
2035 stateMachine.TransitionToState(ProfilingState::WaitingForAck);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002036 CHECK_THROWS_AS(
Jim Flynnf9db3ef2022-03-08 21:23:44 +00002037 deactivateTimelineReportingCommandHandler.operator()(deactivateTimelineReportingPacket),
2038 arm::pipe::ProfilingException);
Keith Davis33ed2212020-03-30 10:43:41 +01002039
2040 stateMachine.TransitionToState(ProfilingState::Active);
2041 deactivateTimelineReportingCommandHandler.operator()(deactivateTimelineReportingPacket);
2042
Sadik Armagan1625efc2021-06-10 18:24:34 +01002043 CHECK(!testNotifyBackends.m_TestNotifyBackendsCalled);
2044 CHECK(!testNotifyBackends.m_timelineReporting.load());
Keith Davis33ed2212020-03-30 10:43:41 +01002045}
2046
Sadik Armagan1625efc2021-06-10 18:24:34 +01002047TEST_CASE("CheckProfilingServiceNotActive")
Keith Davis33ed2212020-03-30 10:43:41 +01002048{
2049 using namespace armnn;
Keith Davis33ed2212020-03-30 10:43:41 +01002050
2051 // Create runtime in which the test will run
2052 armnn::IRuntime::CreationOptions options;
2053 options.m_ProfilingOptions.m_EnableProfiling = true;
2054
Kevin Mayd92a6e42021-02-04 10:27:41 +00002055 armnn::RuntimeImpl runtime(options);
Jim Flynn34430252022-03-04 15:03:58 +00002056 armnn::ArmNNProfilingServiceInitialiser initialiser;
2057 ProfilingServiceRuntimeHelper profilingServiceHelper(
2058 arm::pipe::MAX_ARMNN_COUNTER, initialiser, GetProfilingService(&runtime));
Keith Davis33ed2212020-03-30 10:43:41 +01002059 profilingServiceHelper.ForceTransitionToState(ProfilingState::NotConnected);
2060 profilingServiceHelper.ForceTransitionToState(ProfilingState::WaitingForAck);
2061 profilingServiceHelper.ForceTransitionToState(ProfilingState::Active);
2062
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00002063 BufferManager& bufferManager = profilingServiceHelper.GetProfilingBufferManager();
Keith Davis33ed2212020-03-30 10:43:41 +01002064 auto readableBuffer = bufferManager.GetReadableBuffer();
2065
2066 // Profiling is enabled, the post-optimisation structure should be created
Sadik Armagan1625efc2021-06-10 18:24:34 +01002067 CHECK(readableBuffer == nullptr);
Keith Davis33ed2212020-03-30 10:43:41 +01002068}
2069
Sadik Armagan1625efc2021-06-10 18:24:34 +01002070TEST_CASE("CheckConnectionAcknowledged")
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002071{
Keith Davis3201eea2019-10-24 17:30:41 +01002072 const uint32_t packetFamilyId = 0;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002073 const uint32_t connectionPacketId = 0x10000;
Keith Davis3201eea2019-10-24 17:30:41 +01002074 const uint32_t version = 1;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002075
Jim Flynn75c14f42022-03-10 22:05:42 +00002076 uint32_t sizeOfUint32 = arm::pipe::numeric_cast<uint32_t>(sizeof(uint32_t));
2077 uint32_t sizeOfUint16 = arm::pipe::numeric_cast<uint32_t>(sizeof(uint16_t));
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002078
2079 // Data with period and counters
Keith Davis3201eea2019-10-24 17:30:41 +01002080 uint32_t period1 = 10;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002081 uint32_t dataLength1 = 8;
Keith Davis3201eea2019-10-24 17:30:41 +01002082 uint32_t offset = 0;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002083
Matteo Martincigh67ef2a52019-10-10 13:29:02 +01002084 std::unique_ptr<unsigned char[]> uniqueData1 = std::make_unique<unsigned char[]>(dataLength1);
Keith Davis3201eea2019-10-24 17:30:41 +01002085 unsigned char* data1 = reinterpret_cast<unsigned char*>(uniqueData1.get());
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002086
2087 WriteUint32(data1, offset, period1);
2088 offset += sizeOfUint32;
2089 WriteUint16(data1, offset, 4000);
2090 offset += sizeOfUint16;
2091 WriteUint16(data1, offset, 5000);
2092
Jim Flynnbbfe6032020-07-20 16:57:44 +01002093 arm::pipe::Packet packetA(connectionPacketId, dataLength1, uniqueData1);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002094
2095 ProfilingStateMachine profilingState(ProfilingState::Uninitialised);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002096 CHECK(profilingState.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002097 CounterDirectory counterDirectory;
2098 MockBufferManager mockBuffer(1024);
Jim Flynn9c85b412022-03-16 00:27:43 +00002099 SendCounterPacket sendCounterPacket(mockBuffer,
2100 arm::pipe::ARMNN_SOFTWARE_INFO,
2101 arm::pipe::ARMNN_SOFTWARE_VERSION,
2102 arm::pipe::ARMNN_HARDWARE_VERSION);
Sadik Armagan3896b472020-02-10 12:24:15 +00002103 SendThread sendThread(profilingState, mockBuffer, sendCounterPacket);
Matteo Martincighcdfb9412019-11-08 11:23:06 +00002104 SendTimelinePacket sendTimelinePacket(mockBuffer);
Jim Flynn6398a982020-05-27 17:05:21 +01002105 MockProfilingServiceStatus mockProfilingServiceStatus;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002106
Jim Flynn6398a982020-05-27 17:05:21 +01002107 ConnectionAcknowledgedCommandHandler commandHandler(packetFamilyId,
2108 connectionPacketId,
2109 version,
2110 counterDirectory,
2111 sendCounterPacket,
2112 sendTimelinePacket,
2113 profilingState,
2114 mockProfilingServiceStatus);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002115
2116 // command handler received packet on ProfilingState::Uninitialised
Jim Flynnf9db3ef2022-03-08 21:23:44 +00002117 CHECK_THROWS_AS(commandHandler(packetA), arm::pipe::ProfilingException);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002118
2119 profilingState.TransitionToState(ProfilingState::NotConnected);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002120 CHECK(profilingState.GetCurrentState() == ProfilingState::NotConnected);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002121 // command handler received packet on ProfilingState::NotConnected
Jim Flynnf9db3ef2022-03-08 21:23:44 +00002122 CHECK_THROWS_AS(commandHandler(packetA), arm::pipe::ProfilingException);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002123
2124 profilingState.TransitionToState(ProfilingState::WaitingForAck);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002125 CHECK(profilingState.GetCurrentState() == ProfilingState::WaitingForAck);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002126 // command handler received packet on ProfilingState::WaitingForAck
Sadik Armagan1625efc2021-06-10 18:24:34 +01002127 CHECK_NOTHROW(commandHandler(packetA));
2128 CHECK(profilingState.GetCurrentState() == ProfilingState::Active);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002129
2130 // command handler received packet on ProfilingState::Active
Sadik Armagan1625efc2021-06-10 18:24:34 +01002131 CHECK_NOTHROW(commandHandler(packetA));
2132 CHECK(profilingState.GetCurrentState() == ProfilingState::Active);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002133
2134 // command handler received different packet
2135 const uint32_t differentPacketId = 0x40000;
Jim Flynnbbfe6032020-07-20 16:57:44 +01002136 arm::pipe::Packet packetB(differentPacketId, dataLength1, uniqueData1);
Matteo Martincighd0613b52019-10-09 16:47:04 +01002137 profilingState.TransitionToState(ProfilingState::NotConnected);
2138 profilingState.TransitionToState(ProfilingState::WaitingForAck);
Jim Flynn6398a982020-05-27 17:05:21 +01002139 ConnectionAcknowledgedCommandHandler differentCommandHandler(packetFamilyId,
2140 differentPacketId,
2141 version,
2142 counterDirectory,
2143 sendCounterPacket,
2144 sendTimelinePacket,
2145 profilingState,
2146 mockProfilingServiceStatus);
Jim Flynnf9db3ef2022-03-08 21:23:44 +00002147 CHECK_THROWS_AS(differentCommandHandler(packetB), arm::pipe::ProfilingException);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002148}
2149
Sadik Armagan1625efc2021-06-10 18:24:34 +01002150TEST_CASE("CheckSocketConnectionException")
Teresa Charlin9bab4962019-09-06 12:28:35 +01002151{
Sadik Armagana97a0be2020-03-03 10:44:56 +00002152 // Check that creating a SocketProfilingConnection armnnProfiling in an exception as the Gator UDS doesn't exist.
Sadik Armagan1625efc2021-06-10 18:24:34 +01002153 CHECK_THROWS_AS(new SocketProfilingConnection(), arm::pipe::SocketConnectionException);
Sadik Armagana97a0be2020-03-03 10:44:56 +00002154}
2155
Sadik Armagan1625efc2021-06-10 18:24:34 +01002156TEST_CASE("CheckSocketConnectionException2")
Sadik Armagana97a0be2020-03-03 10:44:56 +00002157{
2158 try
2159 {
2160 new SocketProfilingConnection();
2161 }
Jim Flynnbbfe6032020-07-20 16:57:44 +01002162 catch (const arm::pipe::SocketConnectionException& ex)
Sadik Armagana97a0be2020-03-03 10:44:56 +00002163 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01002164 CHECK(ex.GetSocketFd() == 0);
2165 CHECK(ex.GetErrorNo() == ECONNREFUSED);
2166 CHECK(ex.what()
Sadik Armagana97a0be2020-03-03 10:44:56 +00002167 == std::string("SocketProfilingConnection: Cannot connect to stream socket: Connection refused"));
2168 }
Teresa Charlin9bab4962019-09-06 12:28:35 +01002169}
2170
Sadik Armagan1625efc2021-06-10 18:24:34 +01002171TEST_CASE("SwTraceIsValidCharTest")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002172{
2173 // Only ASCII 7-bit encoding supported
2174 for (unsigned char c = 0; c < 128; c++)
2175 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01002176 CHECK(arm::pipe::SwTraceCharPolicy::IsValidChar(c));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002177 }
2178
2179 // Not ASCII
2180 for (unsigned char c = 255; c >= 128; c++)
2181 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01002182 CHECK(!arm::pipe::SwTraceCharPolicy::IsValidChar(c));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002183 }
2184}
2185
Sadik Armagan1625efc2021-06-10 18:24:34 +01002186TEST_CASE("SwTraceIsValidNameCharTest")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002187{
2188 // Only alpha-numeric and underscore ASCII 7-bit encoding supported
2189 const unsigned char validChars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
2190 for (unsigned char i = 0; i < sizeof(validChars) / sizeof(validChars[0]) - 1; i++)
2191 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01002192 CHECK(arm::pipe::SwTraceNameCharPolicy::IsValidChar(validChars[i]));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002193 }
2194
2195 // Non alpha-numeric chars
2196 for (unsigned char c = 0; c < 48; c++)
2197 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01002198 CHECK(!arm::pipe::SwTraceNameCharPolicy::IsValidChar(c));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002199 }
2200 for (unsigned char c = 58; c < 65; c++)
2201 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01002202 CHECK(!arm::pipe::SwTraceNameCharPolicy::IsValidChar(c));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002203 }
2204 for (unsigned char c = 91; c < 95; c++)
2205 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01002206 CHECK(!arm::pipe::SwTraceNameCharPolicy::IsValidChar(c));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002207 }
2208 for (unsigned char c = 96; c < 97; c++)
2209 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01002210 CHECK(!arm::pipe::SwTraceNameCharPolicy::IsValidChar(c));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002211 }
2212 for (unsigned char c = 123; c < 128; c++)
2213 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01002214 CHECK(!arm::pipe::SwTraceNameCharPolicy::IsValidChar(c));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002215 }
2216
2217 // Not ASCII
2218 for (unsigned char c = 255; c >= 128; c++)
2219 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01002220 CHECK(!arm::pipe::SwTraceNameCharPolicy::IsValidChar(c));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002221 }
2222}
2223
Sadik Armagan1625efc2021-06-10 18:24:34 +01002224TEST_CASE("IsValidSwTraceStringTest")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002225{
2226 // Valid SWTrace strings
Sadik Armagan1625efc2021-06-10 18:24:34 +01002227 CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>(""));
2228 CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>("_"));
2229 CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>("0123"));
2230 CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>("valid_string"));
2231 CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>("VALID_string_456"));
2232 CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>(" "));
2233 CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>("valid string"));
2234 CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>("!$%"));
2235 CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>("valid|\\~string#123"));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002236
2237 // Invalid SWTrace strings
Sadik Armagan1625efc2021-06-10 18:24:34 +01002238 CHECK(!arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>("€£"));
2239 CHECK(!arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>("invalid‡string"));
2240 CHECK(!arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>("12Ž34"));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002241}
2242
Sadik Armagan1625efc2021-06-10 18:24:34 +01002243TEST_CASE("IsValidSwTraceNameStringTest")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002244{
2245 // Valid SWTrace name strings
Sadik Armagan1625efc2021-06-10 18:24:34 +01002246 CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>(""));
2247 CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>("_"));
2248 CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>("0123"));
2249 CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>("valid_string"));
2250 CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>("VALID_string_456"));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002251
2252 // Invalid SWTrace name strings
Sadik Armagan1625efc2021-06-10 18:24:34 +01002253 CHECK(!arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>(" "));
2254 CHECK(!arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>("invalid string"));
2255 CHECK(!arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>("!$%"));
2256 CHECK(!arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>("invalid|\\~string#123"));
2257 CHECK(!arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>("€£"));
2258 CHECK(!arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>("invalid‡string"));
2259 CHECK(!arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>("12Ž34"));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002260}
2261
2262template <typename SwTracePolicy>
2263void StringToSwTraceStringTestHelper(const std::string& testString, std::vector<uint32_t> buffer, size_t expectedSize)
2264{
2265 // Convert the test string to a SWTrace string
Sadik Armagan1625efc2021-06-10 18:24:34 +01002266 CHECK(arm::pipe::StringToSwTraceString<SwTracePolicy>(testString, buffer));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002267
2268 // The buffer must contain at least the length of the string
Sadik Armagan1625efc2021-06-10 18:24:34 +01002269 CHECK(!buffer.empty());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002270
2271 // The buffer must be of the expected size (in words)
Sadik Armagan1625efc2021-06-10 18:24:34 +01002272 CHECK(buffer.size() == expectedSize);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002273
2274 // The first word of the byte must be the length of the string including the null-terminator
Sadik Armagan1625efc2021-06-10 18:24:34 +01002275 CHECK(buffer[0] == testString.size() + 1);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002276
2277 // The contents of the buffer must match the test string
Sadik Armagan1625efc2021-06-10 18:24:34 +01002278 CHECK(std::memcmp(testString.data(), buffer.data() + 1, testString.size()) == 0);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002279
2280 // The buffer must include the null-terminator at the end of the string
2281 size_t nullTerminatorIndex = sizeof(uint32_t) + testString.size();
Sadik Armagan1625efc2021-06-10 18:24:34 +01002282 CHECK(reinterpret_cast<unsigned char*>(buffer.data())[nullTerminatorIndex] == '\0');
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002283}
2284
Sadik Armagan1625efc2021-06-10 18:24:34 +01002285TEST_CASE("StringToSwTraceStringTest")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002286{
2287 std::vector<uint32_t> buffer;
2288
2289 // Valid SWTrace strings (expected size in words)
Jim Flynnbbfe6032020-07-20 16:57:44 +01002290 StringToSwTraceStringTestHelper<arm::pipe::SwTraceCharPolicy>("", buffer, 2);
2291 StringToSwTraceStringTestHelper<arm::pipe::SwTraceCharPolicy>("_", buffer, 2);
2292 StringToSwTraceStringTestHelper<arm::pipe::SwTraceCharPolicy>("0123", buffer, 3);
2293 StringToSwTraceStringTestHelper<arm::pipe::SwTraceCharPolicy>("valid_string", buffer, 5);
2294 StringToSwTraceStringTestHelper<arm::pipe::SwTraceCharPolicy>("VALID_string_456", buffer, 6);
2295 StringToSwTraceStringTestHelper<arm::pipe::SwTraceCharPolicy>(" ", buffer, 2);
2296 StringToSwTraceStringTestHelper<arm::pipe::SwTraceCharPolicy>("valid string", buffer, 5);
2297 StringToSwTraceStringTestHelper<arm::pipe::SwTraceCharPolicy>("!$%", buffer, 2);
2298 StringToSwTraceStringTestHelper<arm::pipe::SwTraceCharPolicy>("valid|\\~string#123", buffer, 6);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002299
2300 // Invalid SWTrace strings
Sadik Armagan1625efc2021-06-10 18:24:34 +01002301 CHECK(!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceCharPolicy>("€£", buffer));
2302 CHECK(buffer.empty());
2303 CHECK(!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceCharPolicy>("invalid‡string", buffer));
2304 CHECK(buffer.empty());
2305 CHECK(!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceCharPolicy>("12Ž34", buffer));
2306 CHECK(buffer.empty());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002307}
2308
Sadik Armagan1625efc2021-06-10 18:24:34 +01002309TEST_CASE("StringToSwTraceNameStringTest")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002310{
2311 std::vector<uint32_t> buffer;
2312
2313 // Valid SWTrace namestrings (expected size in words)
Jim Flynnbbfe6032020-07-20 16:57:44 +01002314 StringToSwTraceStringTestHelper<arm::pipe::SwTraceNameCharPolicy>("", buffer, 2);
2315 StringToSwTraceStringTestHelper<arm::pipe::SwTraceNameCharPolicy>("_", buffer, 2);
2316 StringToSwTraceStringTestHelper<arm::pipe::SwTraceNameCharPolicy>("0123", buffer, 3);
2317 StringToSwTraceStringTestHelper<arm::pipe::SwTraceNameCharPolicy>("valid_string", buffer, 5);
2318 StringToSwTraceStringTestHelper<arm::pipe::SwTraceNameCharPolicy>("VALID_string_456", buffer, 6);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002319
2320 // Invalid SWTrace namestrings
Sadik Armagan1625efc2021-06-10 18:24:34 +01002321 CHECK(!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceNameCharPolicy>(" ", buffer));
2322 CHECK(buffer.empty());
2323 CHECK(!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceNameCharPolicy>("invalid string", buffer));
2324 CHECK(buffer.empty());
2325 CHECK(!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceNameCharPolicy>("!$%", buffer));
2326 CHECK(buffer.empty());
2327 CHECK(!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceNameCharPolicy>("invalid|\\~string#123", buffer));
2328 CHECK(buffer.empty());
2329 CHECK(!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceNameCharPolicy>("€£", buffer));
2330 CHECK(buffer.empty());
2331 CHECK(!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceNameCharPolicy>("invalid‡string", buffer));
2332 CHECK(buffer.empty());
2333 CHECK(!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceNameCharPolicy>("12Ž34", buffer));
2334 CHECK(buffer.empty());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002335}
2336
Sadik Armagan1625efc2021-06-10 18:24:34 +01002337TEST_CASE("CheckPeriodicCounterCaptureThread")
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002338{
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002339 class CaptureReader : public IReadCounterValues
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002340 {
2341 public:
Finn Williamsf4d59a62019-10-14 15:55:18 +01002342 CaptureReader(uint16_t counterSize)
2343 {
Keith Davis3201eea2019-10-24 17:30:41 +01002344 for (uint16_t i = 0; i < counterSize; ++i)
Finn Williamsf4d59a62019-10-14 15:55:18 +01002345 {
2346 m_Data[i] = 0;
2347 }
2348 m_CounterSize = counterSize;
2349 }
2350 //not used
Matteo Martincighe8485382019-10-10 14:08:21 +01002351 bool IsCounterRegistered(uint16_t counterUid) const override
2352 {
Jim Flynn9265a882022-03-10 23:35:26 +00002353 arm::pipe::IgnoreUnused(counterUid);
Finn Williamsf4d59a62019-10-14 15:55:18 +01002354 return false;
Matteo Martincighe8485382019-10-10 14:08:21 +01002355 }
Jim Flynn34430252022-03-04 15:03:58 +00002356 bool IsCounterRegistered(const std::string& counterName) const override
2357 {
Jim Flynn9265a882022-03-10 23:35:26 +00002358 arm::pipe::IgnoreUnused(counterName);
Jim Flynn34430252022-03-04 15:03:58 +00002359 return false;
2360 }
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002361 uint16_t GetCounterCount() const override
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002362 {
Finn Williamsf4d59a62019-10-14 15:55:18 +01002363 return m_CounterSize;
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002364 }
2365
Finn Williamsf3fcf322020-05-11 14:38:02 +01002366 uint32_t GetAbsoluteCounterValue(uint16_t counterUid) const override
2367 {
2368 if (counterUid > m_CounterSize)
2369 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01002370 FAIL("Invalid counter Uid");
Finn Williamsf3fcf322020-05-11 14:38:02 +01002371 }
2372 return m_Data.at(counterUid).load();
2373 }
2374
2375 uint32_t GetDeltaCounterValue(uint16_t counterUid) override
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002376 {
Keith Davis3201eea2019-10-24 17:30:41 +01002377 if (counterUid > m_CounterSize)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002378 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01002379 FAIL("Invalid counter Uid");
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002380 }
Matteo Martincighe8485382019-10-10 14:08:21 +01002381 return m_Data.at(counterUid).load();
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002382 }
2383
Matteo Martincighe8485382019-10-10 14:08:21 +01002384 void SetCounterValue(uint16_t counterUid, uint32_t value)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002385 {
Keith Davis3201eea2019-10-24 17:30:41 +01002386 if (counterUid > m_CounterSize)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002387 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01002388 FAIL("Invalid counter Uid");
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002389 }
Finn Williamsf4d59a62019-10-14 15:55:18 +01002390 m_Data.at(counterUid).store(value);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002391 }
2392
2393 private:
Matteo Martincighe8485382019-10-10 14:08:21 +01002394 std::unordered_map<uint16_t, std::atomic<uint32_t>> m_Data;
Finn Williamsf4d59a62019-10-14 15:55:18 +01002395 uint16_t m_CounterSize;
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002396 };
2397
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002398 ProfilingStateMachine profilingStateMachine;
2399
Cathal Corbett6f073722022-03-04 12:11:09 +00002400 const std::unordered_map<std::string,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00002401 std::shared_ptr<IBackendProfilingContext>> backendProfilingContext;
Finn Williams032bc742020-02-12 11:02:34 +00002402 CounterIdMap counterIdMap;
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002403 Holder data;
2404 std::vector<uint16_t> captureIds1 = { 0, 1 };
2405 std::vector<uint16_t> captureIds2;
2406
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002407 MockBufferManager mockBuffer(512);
Jim Flynn9c85b412022-03-16 00:27:43 +00002408 SendCounterPacket sendCounterPacket(mockBuffer,
2409 arm::pipe::ARMNN_SOFTWARE_INFO,
2410 arm::pipe::ARMNN_SOFTWARE_VERSION,
2411 arm::pipe::ARMNN_HARDWARE_VERSION);
Sadik Armagan3896b472020-02-10 12:24:15 +00002412 SendThread sendThread(profilingStateMachine, mockBuffer, sendCounterPacket);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002413
2414 std::vector<uint16_t> counterIds;
Finn Williamsf4d59a62019-10-14 15:55:18 +01002415 CaptureReader captureReader(2);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002416
Keith Davis3201eea2019-10-24 17:30:41 +01002417 unsigned int valueA = 10;
2418 unsigned int valueB = 15;
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002419 unsigned int numSteps = 5;
2420
Finn Williams032bc742020-02-12 11:02:34 +00002421 PeriodicCounterCapture periodicCounterCapture(std::ref(data), std::ref(sendCounterPacket), captureReader,
2422 counterIdMap, backendProfilingContext);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002423
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002424 for (unsigned int i = 0; i < numSteps; ++i)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002425 {
Finn Williams032bc742020-02-12 11:02:34 +00002426 data.SetCaptureData(1, captureIds1, {});
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002427 captureReader.SetCounterValue(0, valueA * (i + 1));
2428 captureReader.SetCounterValue(1, valueB * (i + 1));
2429
2430 periodicCounterCapture.Start();
Finn Williamsf4d59a62019-10-14 15:55:18 +01002431 periodicCounterCapture.Stop();
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002432 }
2433
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002434 auto buffer = mockBuffer.GetReadableBuffer();
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002435
2436 uint32_t headerWord0 = ReadUint32(buffer, 0);
2437 uint32_t headerWord1 = ReadUint32(buffer, 4);
2438
Sadik Armagan1625efc2021-06-10 18:24:34 +01002439 CHECK(((headerWord0 >> 26) & 0x0000003F) == 3); // packet family
2440 CHECK(((headerWord0 >> 19) & 0x0000007F) == 0); // packet class
2441 CHECK(((headerWord0 >> 16) & 0x00000007) == 0); // packet type
2442 CHECK(headerWord1 == 20);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002443
Keith Davis3201eea2019-10-24 17:30:41 +01002444 uint32_t offset = 16;
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002445 uint16_t readIndex = ReadUint16(buffer, offset);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002446 CHECK(0 == readIndex);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002447
2448 offset += 2;
2449 uint32_t readValue = ReadUint32(buffer, offset);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002450 CHECK((valueA * numSteps) == readValue);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002451
2452 offset += 4;
2453 readIndex = ReadUint16(buffer, offset);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002454 CHECK(1 == readIndex);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002455
2456 offset += 2;
2457 readValue = ReadUint32(buffer, offset);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002458 CHECK((valueB * numSteps) == readValue);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002459}
2460
Sadik Armagan1625efc2021-06-10 18:24:34 +01002461TEST_CASE("RequestCounterDirectoryCommandHandlerTest1")
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002462{
Jim Flynn397043f2019-10-17 17:37:10 +01002463 const uint32_t familyId = 0;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002464 const uint32_t packetId = 3;
Keith Davis3201eea2019-10-24 17:30:41 +01002465 const uint32_t version = 1;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002466 ProfilingStateMachine profilingStateMachine;
2467 CounterDirectory counterDirectory;
Matteo Martincigh9723d022019-11-13 10:56:41 +00002468 MockBufferManager mockBuffer1(1024);
Jim Flynn9c85b412022-03-16 00:27:43 +00002469 SendCounterPacket sendCounterPacket(mockBuffer1,
2470 arm::pipe::ARMNN_SOFTWARE_INFO,
2471 arm::pipe::ARMNN_SOFTWARE_VERSION,
2472 arm::pipe::ARMNN_HARDWARE_VERSION);
Sadik Armagan3896b472020-02-10 12:24:15 +00002473 SendThread sendThread(profilingStateMachine, mockBuffer1, sendCounterPacket);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002474 MockBufferManager mockBuffer2(1024);
2475 SendTimelinePacket sendTimelinePacket(mockBuffer2);
Keith Davis3201eea2019-10-24 17:30:41 +01002476 RequestCounterDirectoryCommandHandler commandHandler(familyId, packetId, version, counterDirectory,
Matteo Martincigh9723d022019-11-13 10:56:41 +00002477 sendCounterPacket, sendTimelinePacket, profilingStateMachine);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002478
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002479 const uint32_t wrongPacketId = 47;
Keith Davis3201eea2019-10-24 17:30:41 +01002480 const uint32_t wrongHeader = (wrongPacketId & 0x000003FF) << 16;
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002481
Jim Flynnbbfe6032020-07-20 16:57:44 +01002482 arm::pipe::Packet wrongPacket(wrongHeader);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002483
2484 profilingStateMachine.TransitionToState(ProfilingState::Uninitialised);
Jim Flynnf9db3ef2022-03-08 21:23:44 +00002485 CHECK_THROWS_AS(commandHandler(wrongPacket), arm::pipe::ProfilingException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002486 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
Jim Flynnf9db3ef2022-03-08 21:23:44 +00002487 CHECK_THROWS_AS(commandHandler(wrongPacket), arm::pipe::ProfilingException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002488 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
Jim Flynnf9db3ef2022-03-08 21:23:44 +00002489 CHECK_THROWS_AS(commandHandler(wrongPacket), arm::pipe::ProfilingException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002490 profilingStateMachine.TransitionToState(ProfilingState::Active);
Jim Flynnf9db3ef2022-03-08 21:23:44 +00002491 CHECK_THROWS_AS(commandHandler(wrongPacket), arm::pipe::InvalidArgumentException); // Wrong packet
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002492
2493 const uint32_t rightHeader = (packetId & 0x000003FF) << 16;
2494
Jim Flynnbbfe6032020-07-20 16:57:44 +01002495 arm::pipe::Packet rightPacket(rightHeader);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002496
Sadik Armagan1625efc2021-06-10 18:24:34 +01002497 CHECK_NOTHROW(commandHandler(rightPacket)); // Right packet
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002498
Matteo Martincigh9723d022019-11-13 10:56:41 +00002499 auto readBuffer1 = mockBuffer1.GetReadableBuffer();
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002500
Matteo Martincigh9723d022019-11-13 10:56:41 +00002501 uint32_t header1Word0 = ReadUint32(readBuffer1, 0);
2502 uint32_t header1Word1 = ReadUint32(readBuffer1, 4);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002503
Matteo Martincigh9723d022019-11-13 10:56:41 +00002504 // Counter directory packet
Sadik Armagan1625efc2021-06-10 18:24:34 +01002505 CHECK(((header1Word0 >> 26) & 0x0000003F) == 0); // packet family
2506 CHECK(((header1Word0 >> 16) & 0x000003FF) == 2); // packet id
2507 CHECK(header1Word1 == 24); // data length
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002508
Matteo Martincigh9723d022019-11-13 10:56:41 +00002509 uint32_t bodyHeader1Word0 = ReadUint32(readBuffer1, 8);
Jim Flynn75c14f42022-03-10 22:05:42 +00002510 uint16_t deviceRecordCount = arm::pipe::numeric_cast<uint16_t>(bodyHeader1Word0 >> 16);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002511 CHECK(deviceRecordCount == 0); // device_records_count
Matteo Martincigh9723d022019-11-13 10:56:41 +00002512
2513 auto readBuffer2 = mockBuffer2.GetReadableBuffer();
2514
2515 uint32_t header2Word0 = ReadUint32(readBuffer2, 0);
2516 uint32_t header2Word1 = ReadUint32(readBuffer2, 4);
2517
2518 // Timeline message directory packet
Sadik Armagan1625efc2021-06-10 18:24:34 +01002519 CHECK(((header2Word0 >> 26) & 0x0000003F) == 1); // packet family
2520 CHECK(((header2Word0 >> 16) & 0x000003FF) == 0); // packet id
2521 CHECK(header2Word1 == 443); // data length
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002522}
2523
Sadik Armagan1625efc2021-06-10 18:24:34 +01002524TEST_CASE("RequestCounterDirectoryCommandHandlerTest2")
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002525{
Jim Flynn397043f2019-10-17 17:37:10 +01002526 const uint32_t familyId = 0;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002527 const uint32_t packetId = 3;
Keith Davis3201eea2019-10-24 17:30:41 +01002528 const uint32_t version = 1;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002529 ProfilingStateMachine profilingStateMachine;
2530 CounterDirectory counterDirectory;
Matteo Martincigh9723d022019-11-13 10:56:41 +00002531 MockBufferManager mockBuffer1(1024);
Jim Flynn9c85b412022-03-16 00:27:43 +00002532 SendCounterPacket sendCounterPacket(mockBuffer1,
2533 arm::pipe::ARMNN_SOFTWARE_INFO,
2534 arm::pipe::ARMNN_SOFTWARE_VERSION,
2535 arm::pipe::ARMNN_HARDWARE_VERSION);
Sadik Armagan3896b472020-02-10 12:24:15 +00002536 SendThread sendThread(profilingStateMachine, mockBuffer1, sendCounterPacket);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002537 MockBufferManager mockBuffer2(1024);
2538 SendTimelinePacket sendTimelinePacket(mockBuffer2);
Keith Davis3201eea2019-10-24 17:30:41 +01002539 RequestCounterDirectoryCommandHandler commandHandler(familyId, packetId, version, counterDirectory,
Matteo Martincigh9723d022019-11-13 10:56:41 +00002540 sendCounterPacket, sendTimelinePacket, profilingStateMachine);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002541 const uint32_t header = (packetId & 0x000003FF) << 16;
Jim Flynnbbfe6032020-07-20 16:57:44 +01002542 const arm::pipe::Packet packet(header);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002543
Matteo Martincigh9723d022019-11-13 10:56:41 +00002544 const Device* device = counterDirectory.RegisterDevice("deviceA", 1);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002545 CHECK(device != nullptr);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002546 const CounterSet* counterSet = counterDirectory.RegisterCounterSet("countersetA");
Sadik Armagan1625efc2021-06-10 18:24:34 +01002547 CHECK(counterSet != nullptr);
Sadik Armagan4c998992020-02-25 12:44:44 +00002548 counterDirectory.RegisterCategory("categoryA");
Keith Davise394bd92019-12-02 15:12:19 +00002549 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID, 24,
2550 "categoryA", 0, 1, 2.0f, "counterA", "descA");
2551 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID, 25,
2552 "categoryA", 1, 1, 3.0f, "counterB", "descB");
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002553
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002554 profilingStateMachine.TransitionToState(ProfilingState::Uninitialised);
Jim Flynnf9db3ef2022-03-08 21:23:44 +00002555 CHECK_THROWS_AS(commandHandler(packet), arm::pipe::ProfilingException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002556 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
Jim Flynnf9db3ef2022-03-08 21:23:44 +00002557 CHECK_THROWS_AS(commandHandler(packet), arm::pipe::ProfilingException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002558 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
Jim Flynnf9db3ef2022-03-08 21:23:44 +00002559 CHECK_THROWS_AS(commandHandler(packet), arm::pipe::ProfilingException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002560 profilingStateMachine.TransitionToState(ProfilingState::Active);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002561 CHECK_NOTHROW(commandHandler(packet));
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002562
Matteo Martincigh9723d022019-11-13 10:56:41 +00002563 auto readBuffer1 = mockBuffer1.GetReadableBuffer();
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002564
Finn Williams985fecf2020-04-30 11:06:43 +01002565 const uint32_t header1Word0 = ReadUint32(readBuffer1, 0);
2566 const uint32_t header1Word1 = ReadUint32(readBuffer1, 4);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002567
Sadik Armagan1625efc2021-06-10 18:24:34 +01002568 CHECK(((header1Word0 >> 26) & 0x0000003F) == 0); // packet family
2569 CHECK(((header1Word0 >> 16) & 0x000003FF) == 2); // packet id
2570 CHECK(header1Word1 == 236); // data length
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002571
Finn Williams985fecf2020-04-30 11:06:43 +01002572 const uint32_t bodyHeaderSizeBytes = bodyHeaderSize * sizeof(uint32_t);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002573
Finn Williams985fecf2020-04-30 11:06:43 +01002574 const uint32_t bodyHeader1Word0 = ReadUint32(readBuffer1, 8);
2575 const uint32_t bodyHeader1Word1 = ReadUint32(readBuffer1, 12);
2576 const uint32_t bodyHeader1Word2 = ReadUint32(readBuffer1, 16);
2577 const uint32_t bodyHeader1Word3 = ReadUint32(readBuffer1, 20);
2578 const uint32_t bodyHeader1Word4 = ReadUint32(readBuffer1, 24);
2579 const uint32_t bodyHeader1Word5 = ReadUint32(readBuffer1, 28);
Jim Flynn75c14f42022-03-10 22:05:42 +00002580 const uint16_t deviceRecordCount = arm::pipe::numeric_cast<uint16_t>(bodyHeader1Word0 >> 16);
2581 const uint16_t counterSetRecordCount = arm::pipe::numeric_cast<uint16_t>(bodyHeader1Word2 >> 16);
2582 const uint16_t categoryRecordCount = arm::pipe::numeric_cast<uint16_t>(bodyHeader1Word4 >> 16);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002583 CHECK(deviceRecordCount == 1); // device_records_count
2584 CHECK(bodyHeader1Word1 == 0 + bodyHeaderSizeBytes); // device_records_pointer_table_offset
2585 CHECK(counterSetRecordCount == 1); // counter_set_count
2586 CHECK(bodyHeader1Word3 == 4 + bodyHeaderSizeBytes); // counter_set_pointer_table_offset
2587 CHECK(categoryRecordCount == 1); // categories_count
2588 CHECK(bodyHeader1Word5 == 8 + bodyHeaderSizeBytes); // categories_pointer_table_offset
Finn Williams985fecf2020-04-30 11:06:43 +01002589
2590 const uint32_t deviceRecordOffset = ReadUint32(readBuffer1, 32);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002591 CHECK(deviceRecordOffset == 12);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002592
Finn Williams985fecf2020-04-30 11:06:43 +01002593 const uint32_t counterSetRecordOffset = ReadUint32(readBuffer1, 36);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002594 CHECK(counterSetRecordOffset == 28);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002595
Finn Williams985fecf2020-04-30 11:06:43 +01002596 const uint32_t categoryRecordOffset = ReadUint32(readBuffer1, 40);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002597 CHECK(categoryRecordOffset == 48);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002598
2599 auto readBuffer2 = mockBuffer2.GetReadableBuffer();
2600
Finn Williams985fecf2020-04-30 11:06:43 +01002601 const uint32_t header2Word0 = ReadUint32(readBuffer2, 0);
2602 const uint32_t header2Word1 = ReadUint32(readBuffer2, 4);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002603
2604 // Timeline message directory packet
Sadik Armagan1625efc2021-06-10 18:24:34 +01002605 CHECK(((header2Word0 >> 26) & 0x0000003F) == 1); // packet family
2606 CHECK(((header2Word0 >> 16) & 0x000003FF) == 0); // packet id
2607 CHECK(header2Word1 == 443); // data length
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002608}
2609
Sadik Armagan1625efc2021-06-10 18:24:34 +01002610TEST_CASE("CheckProfilingServiceGoodConnectionAcknowledgedPacket")
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002611{
Finn Williamsa0de0562020-04-22 12:27:37 +01002612 unsigned int streamMetadataPacketsize = GetStreamMetaDataPacketSize();
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002613
Jim Flynn53e46992019-10-14 12:31:10 +01002614 // Reset the profiling service to the uninitialized state
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00002615 ProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002616 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +00002617 armnn::ArmNNProfilingServiceInitialiser initialiser;
Jim Flynn9c85b412022-03-16 00:27:43 +00002618 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER,
2619 initialiser,
2620 arm::pipe::ARMNN_SOFTWARE_INFO,
2621 arm::pipe::ARMNN_SOFTWARE_VERSION,
2622 arm::pipe::ARMNN_HARDWARE_VERSION);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002623 profilingService.ResetExternalProfilingOptions(options, true);
2624
Sadik Armagan3184c902020-03-18 10:57:30 +00002625 // Swap the profiling connection factory in the profiling service instance with our mock one
Jim Flynn34430252022-03-04 15:03:58 +00002626 SwapProfilingConnectionFactoryHelper helper(arm::pipe::MAX_ARMNN_COUNTER, initialiser, profilingService);
Sadik Armagan3184c902020-03-18 10:57:30 +00002627
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002628 // Bring the profiling service to the "WaitingForAck" state
Sadik Armagan1625efc2021-06-10 18:24:34 +01002629 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002630 profilingService.Update(); // Initialize the counter directory
Sadik Armagan1625efc2021-06-10 18:24:34 +01002631 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002632 profilingService.Update(); // Create the profiling connection
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002633
Matteo Martincighd0613b52019-10-09 16:47:04 +01002634 // Get the mock profiling connection
2635 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
Sadik Armagan1625efc2021-06-10 18:24:34 +01002636 CHECK(mockProfilingConnection);
Matteo Martincighd0613b52019-10-09 16:47:04 +01002637
Matteo Martincighe8485382019-10-10 14:08:21 +01002638 // Remove the packets received so far
2639 mockProfilingConnection->Clear();
2640
Sadik Armagan1625efc2021-06-10 18:24:34 +01002641 CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002642 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002643
2644 // Wait for the Stream Metadata packet to be sent
Sadik Armagan1625efc2021-06-10 18:24:34 +01002645 CHECK(helper.WaitForPacketsSent(
Finn Williams09ad6f92019-12-19 17:05:18 +00002646 mockProfilingConnection, PacketType::StreamMetaData, streamMetadataPacketsize) >= 1);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002647
2648 // Write a valid "Connection Acknowledged" packet into the mock profiling connection, to simulate a valid
2649 // reply from an external profiling service
2650
2651 // Connection Acknowledged Packet header (word 0, word 1 is always zero):
2652 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2653 // 16:25 [10] packet_id: Packet identifier, value 0b0000000001
2654 // 8:15 [8] reserved: Reserved, value 0b00000000
2655 // 0:7 [8] reserved: Reserved, value 0b00000000
2656 uint32_t packetFamily = 0;
2657 uint32_t packetId = 1;
Keith Davis3201eea2019-10-24 17:30:41 +01002658 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002659
Matteo Martincighd0613b52019-10-09 16:47:04 +01002660 // Create the Connection Acknowledged Packet
Jim Flynnbbfe6032020-07-20 16:57:44 +01002661 arm::pipe::Packet connectionAcknowledgedPacket(header);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002662
2663 // Write the packet to the mock profiling connection
2664 mockProfilingConnection->WritePacket(std::move(connectionAcknowledgedPacket));
2665
Colm Donelan2ba48d22019-11-29 09:10:59 +00002666 // Wait for the counter directory packet to ensure the ConnectionAcknowledgedCommandHandler has run.
Sadik Armagan1625efc2021-06-10 18:24:34 +01002667 CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::CounterDirectory) == 1);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002668
2669 // The Connection Acknowledged Command Handler should have updated the profiling state accordingly
Sadik Armagan1625efc2021-06-10 18:24:34 +01002670 CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincighd0613b52019-10-09 16:47:04 +01002671
2672 // Reset the profiling service to stop any running thread
2673 options.m_EnableProfiling = false;
2674 profilingService.ResetExternalProfilingOptions(options, true);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002675}
2676
Sadik Armagan1625efc2021-06-10 18:24:34 +01002677TEST_CASE("CheckProfilingServiceGoodRequestCounterDirectoryPacket")
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002678{
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002679 // Reset the profiling service to the uninitialized state
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00002680 ProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002681 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +00002682 armnn::ArmNNProfilingServiceInitialiser initialiser;
Jim Flynn9c85b412022-03-16 00:27:43 +00002683 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER,
2684 initialiser,
2685 arm::pipe::ARMNN_SOFTWARE_INFO,
2686 arm::pipe::ARMNN_SOFTWARE_VERSION,
2687 arm::pipe::ARMNN_HARDWARE_VERSION);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002688 profilingService.ResetExternalProfilingOptions(options, true);
2689
Sadik Armagan3184c902020-03-18 10:57:30 +00002690 // Swap the profiling connection factory in the profiling service instance with our mock one
Jim Flynn34430252022-03-04 15:03:58 +00002691 SwapProfilingConnectionFactoryHelper helper(arm::pipe::MAX_ARMNN_COUNTER, initialiser, profilingService);
Sadik Armagan3184c902020-03-18 10:57:30 +00002692
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002693 // Bring the profiling service to the "Active" state
Sadik Armagan1625efc2021-06-10 18:24:34 +01002694 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002695 profilingService.Update(); // Initialize the counter directory
Sadik Armagan1625efc2021-06-10 18:24:34 +01002696 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002697 profilingService.Update(); // Create the profiling connection
Sadik Armagan1625efc2021-06-10 18:24:34 +01002698 CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002699 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002700
Colm Donelan2ba48d22019-11-29 09:10:59 +00002701 // Get the mock profiling connection
2702 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
Sadik Armagan1625efc2021-06-10 18:24:34 +01002703 CHECK(mockProfilingConnection);
Colm Donelan2ba48d22019-11-29 09:10:59 +00002704
Matteo Martincighe8485382019-10-10 14:08:21 +01002705 // Force the profiling service to the "Active" state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002706 helper.ForceTransitionToState(ProfilingState::Active);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002707 CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002708
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002709 // Write a valid "Request Counter Directory" packet into the mock profiling connection, to simulate a valid
2710 // reply from an external profiling service
2711
2712 // Request Counter Directory packet header (word 0, word 1 is always zero):
2713 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2714 // 16:25 [10] packet_id: Packet identifier, value 0b0000000011
2715 // 8:15 [8] reserved: Reserved, value 0b00000000
2716 // 0:7 [8] reserved: Reserved, value 0b00000000
2717 uint32_t packetFamily = 0;
2718 uint32_t packetId = 3;
Keith Davis3201eea2019-10-24 17:30:41 +01002719 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002720
2721 // Create the Request Counter Directory packet
Jim Flynnbbfe6032020-07-20 16:57:44 +01002722 arm::pipe::Packet requestCounterDirectoryPacket(header);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002723
2724 // Write the packet to the mock profiling connection
2725 mockProfilingConnection->WritePacket(std::move(requestCounterDirectoryPacket));
2726
Sadik Armagan4c998992020-02-25 12:44:44 +00002727 // Expecting one CounterDirectory Packet of length 652
Jim Flynn6398a982020-05-27 17:05:21 +01002728 // and one TimelineMessageDirectory packet of length 451
Sadik Armagan1625efc2021-06-10 18:24:34 +01002729 CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::CounterDirectory, 652) == 1);
2730 CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::TimelineMessageDirectory, 451) == 1);
Matteo Martincighe8485382019-10-10 14:08:21 +01002731
2732 // The Request Counter Directory Command Handler should not have updated the profiling state
Sadik Armagan1625efc2021-06-10 18:24:34 +01002733 CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincighe8485382019-10-10 14:08:21 +01002734
2735 // Reset the profiling service to stop any running thread
2736 options.m_EnableProfiling = false;
2737 profilingService.ResetExternalProfilingOptions(options, true);
2738}
2739
Sadik Armagan1625efc2021-06-10 18:24:34 +01002740TEST_CASE("CheckProfilingServiceBadPeriodicCounterSelectionPacketInvalidCounterUid")
Matteo Martincighe8485382019-10-10 14:08:21 +01002741{
Matteo Martincighe8485382019-10-10 14:08:21 +01002742 // Reset the profiling service to the uninitialized state
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00002743 ProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002744 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +00002745 armnn::ArmNNProfilingServiceInitialiser initialiser;
Jim Flynn9c85b412022-03-16 00:27:43 +00002746 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER,
2747 initialiser,
2748 arm::pipe::ARMNN_SOFTWARE_INFO,
2749 arm::pipe::ARMNN_SOFTWARE_VERSION,
2750 arm::pipe::ARMNN_HARDWARE_VERSION);
Matteo Martincighe8485382019-10-10 14:08:21 +01002751 profilingService.ResetExternalProfilingOptions(options, true);
2752
Sadik Armagan3184c902020-03-18 10:57:30 +00002753 // Swap the profiling connection factory in the profiling service instance with our mock one
Jim Flynn34430252022-03-04 15:03:58 +00002754 SwapProfilingConnectionFactoryHelper helper(arm::pipe::MAX_ARMNN_COUNTER, initialiser, profilingService);
Sadik Armagan3184c902020-03-18 10:57:30 +00002755
Matteo Martincighe8485382019-10-10 14:08:21 +01002756 // Bring the profiling service to the "Active" state
Sadik Armagan1625efc2021-06-10 18:24:34 +01002757 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002758 profilingService.Update(); // Initialize the counter directory
Sadik Armagan1625efc2021-06-10 18:24:34 +01002759 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002760 profilingService.Update(); // Create the profiling connection
Sadik Armagan1625efc2021-06-10 18:24:34 +01002761 CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002762 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002763
Colm Donelan2ba48d22019-11-29 09:10:59 +00002764 // Get the mock profiling connection
2765 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
Sadik Armagan1625efc2021-06-10 18:24:34 +01002766 CHECK(mockProfilingConnection);
Colm Donelan2ba48d22019-11-29 09:10:59 +00002767
Matteo Martincighe8485382019-10-10 14:08:21 +01002768 // Force the profiling service to the "Active" state
2769 helper.ForceTransitionToState(ProfilingState::Active);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002770 CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincighe8485382019-10-10 14:08:21 +01002771
Matteo Martincighe8485382019-10-10 14:08:21 +01002772 // Remove the packets received so far
2773 mockProfilingConnection->Clear();
2774
2775 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2776 // external profiling service
2777
2778 // Periodic Counter Selection packet header:
2779 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2780 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2781 // 8:15 [8] reserved: Reserved, value 0b00000000
2782 // 0:7 [8] reserved: Reserved, value 0b00000000
2783 uint32_t packetFamily = 0;
2784 uint32_t packetId = 4;
Keith Davis3201eea2019-10-24 17:30:41 +01002785 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincighe8485382019-10-10 14:08:21 +01002786
Keith Davis3201eea2019-10-24 17:30:41 +01002787 uint32_t capturePeriod = 123456; // Some capture period (microseconds)
Matteo Martincighe8485382019-10-10 14:08:21 +01002788
2789 // Get the first valid counter UID
2790 const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
Keith Davis3201eea2019-10-24 17:30:41 +01002791 const Counters& counters = counterDirectory.GetCounters();
Sadik Armagan1625efc2021-06-10 18:24:34 +01002792 CHECK(counters.size() > 1);
Keith Davis3201eea2019-10-24 17:30:41 +01002793 uint16_t counterUidA = counters.begin()->first; // First valid counter UID
2794 uint16_t counterUidB = 9999; // Second invalid counter UID
Matteo Martincighe8485382019-10-10 14:08:21 +01002795
2796 uint32_t length = 8;
2797
2798 auto data = std::make_unique<unsigned char[]>(length);
2799 WriteUint32(data.get(), 0, capturePeriod);
2800 WriteUint16(data.get(), 4, counterUidA);
2801 WriteUint16(data.get(), 6, counterUidB);
2802
2803 // Create the Periodic Counter Selection packet
Jim Flynnbbfe6032020-07-20 16:57:44 +01002804 // Length > 0, this will start the Period Counter Capture thread
2805 arm::pipe::Packet periodicCounterSelectionPacket(header, length, data);
2806
Matteo Martincighe8485382019-10-10 14:08:21 +01002807
2808 // Write the packet to the mock profiling connection
2809 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2810
Finn Williams09ad6f92019-12-19 17:05:18 +00002811 // Expecting one Periodic Counter Selection packet of length 14
2812 // and at least one Periodic Counter Capture packet of length 22
Sadik Armagan1625efc2021-06-10 18:24:34 +01002813 CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterSelection, 14) == 1);
2814 CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterCapture, 22) >= 1);
Matteo Martincighe8485382019-10-10 14:08:21 +01002815
2816 // The Periodic Counter Selection Handler should not have updated the profiling state
Sadik Armagan1625efc2021-06-10 18:24:34 +01002817 CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincighe8485382019-10-10 14:08:21 +01002818
2819 // Reset the profiling service to stop any running thread
2820 options.m_EnableProfiling = false;
2821 profilingService.ResetExternalProfilingOptions(options, true);
2822}
2823
Sadik Armagan1625efc2021-06-10 18:24:34 +01002824TEST_CASE("CheckProfilingServiceGoodPeriodicCounterSelectionPacketNoCounters")
Matteo Martincighe8485382019-10-10 14:08:21 +01002825{
Matteo Martincighe8485382019-10-10 14:08:21 +01002826 // Reset the profiling service to the uninitialized state
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00002827 ProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002828 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +00002829 armnn::ArmNNProfilingServiceInitialiser initialiser;
Jim Flynn9c85b412022-03-16 00:27:43 +00002830 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER,
2831 initialiser,
2832 arm::pipe::ARMNN_SOFTWARE_INFO,
2833 arm::pipe::ARMNN_SOFTWARE_VERSION,
2834 arm::pipe::ARMNN_HARDWARE_VERSION);
Matteo Martincighe8485382019-10-10 14:08:21 +01002835 profilingService.ResetExternalProfilingOptions(options, true);
2836
Sadik Armagan3184c902020-03-18 10:57:30 +00002837 // Swap the profiling connection factory in the profiling service instance with our mock one
Jim Flynn34430252022-03-04 15:03:58 +00002838 SwapProfilingConnectionFactoryHelper helper(arm::pipe::MAX_ARMNN_COUNTER, initialiser, profilingService);
Sadik Armagan3184c902020-03-18 10:57:30 +00002839
Matteo Martincighe8485382019-10-10 14:08:21 +01002840 // Bring the profiling service to the "Active" state
Sadik Armagan1625efc2021-06-10 18:24:34 +01002841 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002842 profilingService.Update(); // Initialize the counter directory
Sadik Armagan1625efc2021-06-10 18:24:34 +01002843 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002844 profilingService.Update(); // Create the profiling connection
Sadik Armagan1625efc2021-06-10 18:24:34 +01002845 CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002846 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002847
Colm Donelan2ba48d22019-11-29 09:10:59 +00002848 // Get the mock profiling connection
2849 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
Sadik Armagan1625efc2021-06-10 18:24:34 +01002850 CHECK(mockProfilingConnection);
Colm Donelan2ba48d22019-11-29 09:10:59 +00002851
Matteo Martincighe8485382019-10-10 14:08:21 +01002852 // Wait for the Stream Metadata packet the be sent
2853 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Finn Williams09ad6f92019-12-19 17:05:18 +00002854 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData);
Matteo Martincighe8485382019-10-10 14:08:21 +01002855
2856 // Force the profiling service to the "Active" state
2857 helper.ForceTransitionToState(ProfilingState::Active);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002858 CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincighe8485382019-10-10 14:08:21 +01002859
Matteo Martincighe8485382019-10-10 14:08:21 +01002860 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2861 // external profiling service
2862
2863 // Periodic Counter Selection packet header:
2864 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2865 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2866 // 8:15 [8] reserved: Reserved, value 0b00000000
2867 // 0:7 [8] reserved: Reserved, value 0b00000000
2868 uint32_t packetFamily = 0;
2869 uint32_t packetId = 4;
Keith Davis3201eea2019-10-24 17:30:41 +01002870 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincighe8485382019-10-10 14:08:21 +01002871
2872 // Create the Periodic Counter Selection packet
Jim Flynnbbfe6032020-07-20 16:57:44 +01002873 // Length == 0, this will disable the collection of counters
2874 arm::pipe::Packet periodicCounterSelectionPacket(header);
Matteo Martincighe8485382019-10-10 14:08:21 +01002875
2876 // Write the packet to the mock profiling connection
2877 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2878
Finn Williams09ad6f92019-12-19 17:05:18 +00002879 // Wait for the Periodic Counter Selection packet of length 12 to be sent
2880 // The size of the expected Periodic Counter Selection (echos the sent one)
Sadik Armagan1625efc2021-06-10 18:24:34 +01002881 CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterSelection, 12) == 1);
Matteo Martincighe8485382019-10-10 14:08:21 +01002882
2883 // The Periodic Counter Selection Handler should not have updated the profiling state
Sadik Armagan1625efc2021-06-10 18:24:34 +01002884 CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincighe8485382019-10-10 14:08:21 +01002885
Finn Williams09ad6f92019-12-19 17:05:18 +00002886 // No Periodic Counter packets are expected
Sadik Armagan1625efc2021-06-10 18:24:34 +01002887 CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterCapture, 0, 0) == 0);
Matteo Martincighe8485382019-10-10 14:08:21 +01002888
2889 // Reset the profiling service to stop any running thread
2890 options.m_EnableProfiling = false;
2891 profilingService.ResetExternalProfilingOptions(options, true);
2892}
2893
Sadik Armagan1625efc2021-06-10 18:24:34 +01002894TEST_CASE("CheckProfilingServiceGoodPeriodicCounterSelectionPacketSingleCounter")
Matteo Martincighe8485382019-10-10 14:08:21 +01002895{
Matteo Martincighe8485382019-10-10 14:08:21 +01002896 // Reset the profiling service to the uninitialized state
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00002897 ProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002898 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +00002899 armnn::ArmNNProfilingServiceInitialiser initialiser;
Jim Flynn9c85b412022-03-16 00:27:43 +00002900 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER,
2901 initialiser,
2902 arm::pipe::ARMNN_SOFTWARE_INFO,
2903 arm::pipe::ARMNN_SOFTWARE_VERSION,
2904 arm::pipe::ARMNN_HARDWARE_VERSION);
Matteo Martincighe8485382019-10-10 14:08:21 +01002905 profilingService.ResetExternalProfilingOptions(options, true);
2906
Sadik Armagan3184c902020-03-18 10:57:30 +00002907 // Swap the profiling connection factory in the profiling service instance with our mock one
Jim Flynn34430252022-03-04 15:03:58 +00002908 SwapProfilingConnectionFactoryHelper helper(arm::pipe::MAX_ARMNN_COUNTER, initialiser, profilingService);
Sadik Armagan3184c902020-03-18 10:57:30 +00002909
Matteo Martincighe8485382019-10-10 14:08:21 +01002910 // Bring the profiling service to the "Active" state
Sadik Armagan1625efc2021-06-10 18:24:34 +01002911 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002912 profilingService.Update(); // Initialize the counter directory
Sadik Armagan1625efc2021-06-10 18:24:34 +01002913 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002914 profilingService.Update(); // Create the profiling connection
Sadik Armagan1625efc2021-06-10 18:24:34 +01002915 CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002916 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002917
Colm Donelan2ba48d22019-11-29 09:10:59 +00002918 // Get the mock profiling connection
2919 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
Sadik Armagan1625efc2021-06-10 18:24:34 +01002920 CHECK(mockProfilingConnection);
Colm Donelan2ba48d22019-11-29 09:10:59 +00002921
Finn Williams09ad6f92019-12-19 17:05:18 +00002922 // Wait for the Stream Metadata packet to be sent
Matteo Martincighe8485382019-10-10 14:08:21 +01002923 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Finn Williams09ad6f92019-12-19 17:05:18 +00002924 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData);
Matteo Martincighe8485382019-10-10 14:08:21 +01002925
2926 // Force the profiling service to the "Active" state
2927 helper.ForceTransitionToState(ProfilingState::Active);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002928 CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincighe8485382019-10-10 14:08:21 +01002929
Matteo Martincighe8485382019-10-10 14:08:21 +01002930 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2931 // external profiling service
2932
2933 // Periodic Counter Selection packet header:
2934 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2935 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2936 // 8:15 [8] reserved: Reserved, value 0b00000000
2937 // 0:7 [8] reserved: Reserved, value 0b00000000
2938 uint32_t packetFamily = 0;
2939 uint32_t packetId = 4;
Keith Davis3201eea2019-10-24 17:30:41 +01002940 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincighe8485382019-10-10 14:08:21 +01002941
Keith Davis3201eea2019-10-24 17:30:41 +01002942 uint32_t capturePeriod = 123456; // Some capture period (microseconds)
Matteo Martincighe8485382019-10-10 14:08:21 +01002943
2944 // Get the first valid counter UID
2945 const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
Keith Davis3201eea2019-10-24 17:30:41 +01002946 const Counters& counters = counterDirectory.GetCounters();
Sadik Armagan1625efc2021-06-10 18:24:34 +01002947 CHECK(!counters.empty());
Keith Davis3201eea2019-10-24 17:30:41 +01002948 uint16_t counterUid = counters.begin()->first; // Valid counter UID
Matteo Martincighe8485382019-10-10 14:08:21 +01002949
2950 uint32_t length = 6;
2951
2952 auto data = std::make_unique<unsigned char[]>(length);
2953 WriteUint32(data.get(), 0, capturePeriod);
2954 WriteUint16(data.get(), 4, counterUid);
2955
2956 // Create the Periodic Counter Selection packet
Jim Flynnbbfe6032020-07-20 16:57:44 +01002957 // Length > 0, this will start the Period Counter Capture thread
2958 arm::pipe::Packet periodicCounterSelectionPacket(header, length, data);
Matteo Martincighe8485382019-10-10 14:08:21 +01002959
2960 // Write the packet to the mock profiling connection
2961 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2962
Finn Williams09ad6f92019-12-19 17:05:18 +00002963 // Expecting one Periodic Counter Selection packet of length 14
2964 // and at least one Periodic Counter Capture packet of length 22
Sadik Armagan1625efc2021-06-10 18:24:34 +01002965 CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterSelection, 14) == 1);
2966 CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterCapture, 22) >= 1);
Matteo Martincighe8485382019-10-10 14:08:21 +01002967
2968 // The Periodic Counter Selection Handler should not have updated the profiling state
Sadik Armagan1625efc2021-06-10 18:24:34 +01002969 CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincighe8485382019-10-10 14:08:21 +01002970
2971 // Reset the profiling service to stop any running thread
2972 options.m_EnableProfiling = false;
2973 profilingService.ResetExternalProfilingOptions(options, true);
2974}
2975
Sadik Armagan1625efc2021-06-10 18:24:34 +01002976TEST_CASE("CheckProfilingServiceGoodPeriodicCounterSelectionPacketMultipleCounters")
Matteo Martincighe8485382019-10-10 14:08:21 +01002977{
Matteo Martincighe8485382019-10-10 14:08:21 +01002978 // Reset the profiling service to the uninitialized state
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00002979 ProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002980 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +00002981 armnn::ArmNNProfilingServiceInitialiser initialiser;
Jim Flynn9c85b412022-03-16 00:27:43 +00002982 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER,
2983 initialiser,
2984 arm::pipe::ARMNN_SOFTWARE_INFO,
2985 arm::pipe::ARMNN_SOFTWARE_VERSION,
2986 arm::pipe::ARMNN_HARDWARE_VERSION);
Matteo Martincighe8485382019-10-10 14:08:21 +01002987 profilingService.ResetExternalProfilingOptions(options, true);
2988
Sadik Armagan3184c902020-03-18 10:57:30 +00002989 // Swap the profiling connection factory in the profiling service instance with our mock one
Jim Flynn34430252022-03-04 15:03:58 +00002990 SwapProfilingConnectionFactoryHelper helper(arm::pipe::MAX_ARMNN_COUNTER, initialiser, profilingService);
Sadik Armagan3184c902020-03-18 10:57:30 +00002991
Matteo Martincighe8485382019-10-10 14:08:21 +01002992 // Bring the profiling service to the "Active" state
Sadik Armagan1625efc2021-06-10 18:24:34 +01002993 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002994 profilingService.Update(); // Initialize the counter directory
Sadik Armagan1625efc2021-06-10 18:24:34 +01002995 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002996 profilingService.Update(); // Create the profiling connection
Sadik Armagan1625efc2021-06-10 18:24:34 +01002997 CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002998 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002999
Colm Donelan2ba48d22019-11-29 09:10:59 +00003000 // Get the mock profiling connection
3001 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003002 CHECK(mockProfilingConnection);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003003
Matteo Martincighe8485382019-10-10 14:08:21 +01003004 // Wait for the Stream Metadata packet the be sent
3005 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Finn Williams09ad6f92019-12-19 17:05:18 +00003006 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData);
Matteo Martincighe8485382019-10-10 14:08:21 +01003007
3008 // Force the profiling service to the "Active" state
3009 helper.ForceTransitionToState(ProfilingState::Active);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003010 CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincighe8485382019-10-10 14:08:21 +01003011
Matteo Martincighe8485382019-10-10 14:08:21 +01003012 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
3013 // external profiling service
3014
3015 // Periodic Counter Selection packet header:
3016 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
3017 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
3018 // 8:15 [8] reserved: Reserved, value 0b00000000
3019 // 0:7 [8] reserved: Reserved, value 0b00000000
3020 uint32_t packetFamily = 0;
3021 uint32_t packetId = 4;
Keith Davis3201eea2019-10-24 17:30:41 +01003022 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincighe8485382019-10-10 14:08:21 +01003023
Keith Davis3201eea2019-10-24 17:30:41 +01003024 uint32_t capturePeriod = 123456; // Some capture period (microseconds)
Matteo Martincighe8485382019-10-10 14:08:21 +01003025
3026 // Get the first valid counter UID
3027 const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
Keith Davis3201eea2019-10-24 17:30:41 +01003028 const Counters& counters = counterDirectory.GetCounters();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003029 CHECK(counters.size() > 1);
Keith Davis3201eea2019-10-24 17:30:41 +01003030 uint16_t counterUidA = counters.begin()->first; // First valid counter UID
3031 uint16_t counterUidB = (counters.begin()++)->first; // Second valid counter UID
Matteo Martincighe8485382019-10-10 14:08:21 +01003032
3033 uint32_t length = 8;
3034
3035 auto data = std::make_unique<unsigned char[]>(length);
3036 WriteUint32(data.get(), 0, capturePeriod);
3037 WriteUint16(data.get(), 4, counterUidA);
3038 WriteUint16(data.get(), 6, counterUidB);
3039
3040 // Create the Periodic Counter Selection packet
Jim Flynnbbfe6032020-07-20 16:57:44 +01003041 // Length > 0, this will start the Period Counter Capture thread
3042 arm::pipe::Packet periodicCounterSelectionPacket(header, length, data);
Matteo Martincighe8485382019-10-10 14:08:21 +01003043
3044 // Write the packet to the mock profiling connection
3045 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
3046
Finn Williams09ad6f92019-12-19 17:05:18 +00003047 // Expecting one PeriodicCounterSelection Packet with a length of 16
3048 // And at least one PeriodicCounterCapture Packet with a length of 28
Sadik Armagan1625efc2021-06-10 18:24:34 +01003049 CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterSelection, 16) == 1);
3050 CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterCapture, 28) >= 1);
Matteo Martincighe8485382019-10-10 14:08:21 +01003051
3052 // The Periodic Counter Selection Handler should not have updated the profiling state
Sadik Armagan1625efc2021-06-10 18:24:34 +01003053 CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01003054
3055 // Reset the profiling service to stop any running thread
3056 options.m_EnableProfiling = false;
3057 profilingService.ResetExternalProfilingOptions(options, true);
3058}
3059
Sadik Armagan1625efc2021-06-10 18:24:34 +01003060TEST_CASE("CheckProfilingServiceDisconnect")
Jim Flynn53e46992019-10-14 12:31:10 +01003061{
Jim Flynn53e46992019-10-14 12:31:10 +01003062 // Reset the profiling service to the uninitialized state
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003063 ProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01003064 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +00003065 armnn::ArmNNProfilingServiceInitialiser initialiser;
Jim Flynn9c85b412022-03-16 00:27:43 +00003066 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER,
3067 initialiser,
3068 arm::pipe::ARMNN_SOFTWARE_INFO,
3069 arm::pipe::ARMNN_SOFTWARE_VERSION,
3070 arm::pipe::ARMNN_HARDWARE_VERSION);
Jim Flynn53e46992019-10-14 12:31:10 +01003071 profilingService.ResetExternalProfilingOptions(options, true);
3072
Sadik Armagan3184c902020-03-18 10:57:30 +00003073 // Swap the profiling connection factory in the profiling service instance with our mock one
Jim Flynn34430252022-03-04 15:03:58 +00003074 SwapProfilingConnectionFactoryHelper helper(arm::pipe::MAX_ARMNN_COUNTER, initialiser, profilingService);
Sadik Armagan3184c902020-03-18 10:57:30 +00003075
Jim Flynn53e46992019-10-14 12:31:10 +01003076 // Try to disconnect the profiling service while in the "Uninitialised" state
Sadik Armagan1625efc2021-06-10 18:24:34 +01003077 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Jim Flynn53e46992019-10-14 12:31:10 +01003078 profilingService.Disconnect();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003079 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised); // The state should not change
Jim Flynn53e46992019-10-14 12:31:10 +01003080
3081 // Try to disconnect the profiling service while in the "NotConnected" state
Keith Davis3201eea2019-10-24 17:30:41 +01003082 profilingService.Update(); // Initialize the counter directory
Sadik Armagan1625efc2021-06-10 18:24:34 +01003083 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Jim Flynn53e46992019-10-14 12:31:10 +01003084 profilingService.Disconnect();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003085 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected); // The state should not change
Jim Flynn53e46992019-10-14 12:31:10 +01003086
3087 // Try to disconnect the profiling service while in the "WaitingForAck" state
Keith Davis3201eea2019-10-24 17:30:41 +01003088 profilingService.Update(); // Create the profiling connection
Sadik Armagan1625efc2021-06-10 18:24:34 +01003089 CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Jim Flynn53e46992019-10-14 12:31:10 +01003090 profilingService.Disconnect();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003091 CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck); // The state should not change
Jim Flynn53e46992019-10-14 12:31:10 +01003092
3093 // Try to disconnect the profiling service while in the "Active" state
Keith Davis3201eea2019-10-24 17:30:41 +01003094 profilingService.Update(); // Start the command handler and the send thread
Jim Flynn53e46992019-10-14 12:31:10 +01003095
Colm Donelan2ba48d22019-11-29 09:10:59 +00003096 // Get the mock profiling connection
3097 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003098 CHECK(mockProfilingConnection);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003099
Jim Flynn53e46992019-10-14 12:31:10 +01003100 // Wait for the Stream Metadata packet the be sent
3101 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Finn Williams09ad6f92019-12-19 17:05:18 +00003102 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData);
Jim Flynn53e46992019-10-14 12:31:10 +01003103
3104 // Force the profiling service to the "Active" state
3105 helper.ForceTransitionToState(ProfilingState::Active);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003106 CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Jim Flynn53e46992019-10-14 12:31:10 +01003107
Jim Flynn53e46992019-10-14 12:31:10 +01003108 // Check that the profiling connection is open
Sadik Armagan1625efc2021-06-10 18:24:34 +01003109 CHECK(mockProfilingConnection->IsOpen());
Jim Flynn53e46992019-10-14 12:31:10 +01003110
3111 profilingService.Disconnect();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003112 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected); // The state should have changed
Jim Flynn53e46992019-10-14 12:31:10 +01003113
3114 // Check that the profiling connection has been reset
3115 mockProfilingConnection = helper.GetMockProfilingConnection();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003116 CHECK(mockProfilingConnection == nullptr);
Jim Flynn53e46992019-10-14 12:31:10 +01003117
3118 // Reset the profiling service to stop any running thread
3119 options.m_EnableProfiling = false;
3120 profilingService.ResetExternalProfilingOptions(options, true);
3121}
3122
Sadik Armagan1625efc2021-06-10 18:24:34 +01003123TEST_CASE("CheckProfilingServiceGoodPerJobCounterSelectionPacket")
Matteo Martincigh994b5342019-10-11 17:19:56 +01003124{
Matteo Martincigh994b5342019-10-11 17:19:56 +01003125 // Reset the profiling service to the uninitialized state
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003126 ProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01003127 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +00003128 armnn::ArmNNProfilingServiceInitialiser initialiser;
Jim Flynn9c85b412022-03-16 00:27:43 +00003129 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER,
3130 initialiser,
3131 arm::pipe::ARMNN_SOFTWARE_INFO,
3132 arm::pipe::ARMNN_SOFTWARE_VERSION,
3133 arm::pipe::ARMNN_HARDWARE_VERSION);
Matteo Martincigh994b5342019-10-11 17:19:56 +01003134 profilingService.ResetExternalProfilingOptions(options, true);
3135
Sadik Armagan3184c902020-03-18 10:57:30 +00003136 // Swap the profiling connection factory in the profiling service instance with our mock one
Jim Flynn34430252022-03-04 15:03:58 +00003137 SwapProfilingConnectionFactoryHelper helper(arm::pipe::MAX_ARMNN_COUNTER, initialiser, profilingService);
Sadik Armagan3184c902020-03-18 10:57:30 +00003138
Matteo Martincigh994b5342019-10-11 17:19:56 +01003139 // Bring the profiling service to the "Active" state
Sadik Armagan1625efc2021-06-10 18:24:34 +01003140 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01003141 profilingService.Update(); // Initialize the counter directory
Sadik Armagan1625efc2021-06-10 18:24:34 +01003142 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01003143 profilingService.Update(); // Create the profiling connection
Sadik Armagan1625efc2021-06-10 18:24:34 +01003144 CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01003145 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincigh994b5342019-10-11 17:19:56 +01003146
Colm Donelan2ba48d22019-11-29 09:10:59 +00003147 // Get the mock profiling connection
3148 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003149 CHECK(mockProfilingConnection);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003150
Matteo Martincigh994b5342019-10-11 17:19:56 +01003151 // Wait for the Stream Metadata packet the be sent
3152 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Finn Williams09ad6f92019-12-19 17:05:18 +00003153 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData);
Matteo Martincigh994b5342019-10-11 17:19:56 +01003154
3155 // Force the profiling service to the "Active" state
3156 helper.ForceTransitionToState(ProfilingState::Active);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003157 CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincigh994b5342019-10-11 17:19:56 +01003158
Matteo Martincigh994b5342019-10-11 17:19:56 +01003159 // Write a "Per-Job Counter Selection" packet into the mock profiling connection, to simulate an input from an
3160 // external profiling service
3161
3162 // Per-Job Counter Selection packet header:
3163 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
3164 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
3165 // 8:15 [8] reserved: Reserved, value 0b00000000
3166 // 0:7 [8] reserved: Reserved, value 0b00000000
3167 uint32_t packetFamily = 0;
3168 uint32_t packetId = 5;
Keith Davis3201eea2019-10-24 17:30:41 +01003169 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincigh994b5342019-10-11 17:19:56 +01003170
3171 // Create the Per-Job Counter Selection packet
Jim Flynnbbfe6032020-07-20 16:57:44 +01003172 // Length == 0, this will disable the collection of counters
3173 arm::pipe::Packet periodicCounterSelectionPacket(header);
Matteo Martincigh994b5342019-10-11 17:19:56 +01003174
3175 // Write the packet to the mock profiling connection
3176 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
3177
3178 // Wait for a bit (must at least be the delay value of the mock profiling connection) to make sure that
3179 // the Per-Job Counter Selection packet gets processed by the profiling service
Colm Donelan2ba48d22019-11-29 09:10:59 +00003180 std::this_thread::sleep_for(std::chrono::milliseconds(5));
Matteo Martincigh994b5342019-10-11 17:19:56 +01003181
Matteo Martincigh994b5342019-10-11 17:19:56 +01003182 // The Per-Job Counter Selection Command Handler should not have updated the profiling state
Sadik Armagan1625efc2021-06-10 18:24:34 +01003183 CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincigh994b5342019-10-11 17:19:56 +01003184
Finn Williams09ad6f92019-12-19 17:05:18 +00003185 // The Per-Job Counter Selection packets are dropped silently, so there should be no reply coming
3186 // from the profiling service
3187 const auto StreamMetaDataSize = static_cast<unsigned long>(
3188 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData, 0, 0));
Sadik Armagan1625efc2021-06-10 18:24:34 +01003189 CHECK(StreamMetaDataSize == mockProfilingConnection->GetWrittenDataSize());
Finn Williams09ad6f92019-12-19 17:05:18 +00003190
Matteo Martincigh994b5342019-10-11 17:19:56 +01003191 // Reset the profiling service to stop any running thread
3192 options.m_EnableProfiling = false;
3193 profilingService.ResetExternalProfilingOptions(options, true);
3194}
3195
Sadik Armagan1625efc2021-06-10 18:24:34 +01003196TEST_CASE("CheckConfigureProfilingServiceOn")
Jim Flynn672d06e2019-10-15 10:18:11 +01003197{
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003198 ProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01003199 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +00003200 armnn::ArmNNProfilingServiceInitialiser initialiser;
Jim Flynn9c85b412022-03-16 00:27:43 +00003201 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER,
3202 initialiser,
3203 arm::pipe::ARMNN_SOFTWARE_INFO,
3204 arm::pipe::ARMNN_SOFTWARE_VERSION,
3205 arm::pipe::ARMNN_HARDWARE_VERSION);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003206 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Jim Flynn672d06e2019-10-15 10:18:11 +01003207 profilingService.ConfigureProfilingService(options);
3208 // should get as far as NOT_CONNECTED
Sadik Armagan1625efc2021-06-10 18:24:34 +01003209 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Jim Flynn672d06e2019-10-15 10:18:11 +01003210 // Reset the profiling service to stop any running thread
3211 options.m_EnableProfiling = false;
3212 profilingService.ResetExternalProfilingOptions(options, true);
3213}
3214
Sadik Armagan1625efc2021-06-10 18:24:34 +01003215TEST_CASE("CheckConfigureProfilingServiceOff")
Jim Flynn672d06e2019-10-15 10:18:11 +01003216{
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003217 ProfilingOptions options;
Jim Flynn34430252022-03-04 15:03:58 +00003218 armnn::ArmNNProfilingServiceInitialiser initialiser;
Jim Flynn9c85b412022-03-16 00:27:43 +00003219 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER,
3220 initialiser,
3221 arm::pipe::ARMNN_SOFTWARE_INFO,
3222 arm::pipe::ARMNN_SOFTWARE_VERSION,
3223 arm::pipe::ARMNN_HARDWARE_VERSION);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003224 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Jim Flynn672d06e2019-10-15 10:18:11 +01003225 profilingService.ConfigureProfilingService(options);
3226 // should not move from Uninitialised
Sadik Armagan1625efc2021-06-10 18:24:34 +01003227 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Jim Flynn672d06e2019-10-15 10:18:11 +01003228 // Reset the profiling service to stop any running thread
3229 options.m_EnableProfiling = false;
3230 profilingService.ResetExternalProfilingOptions(options, true);
3231}
3232
Sadik Armagan1625efc2021-06-10 18:24:34 +01003233TEST_CASE("CheckProfilingServiceEnabled")
Colm Donelan2ba48d22019-11-29 09:10:59 +00003234{
3235 // 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 +00003236 LogLevelSwapper logLevelSwapper(arm::pipe::LogSeverity::Warning);
3237
3238 // Redirect the output to a local stream so that we can parse the warning message
3239 std::stringstream ss;
3240 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3241
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003242 ProfilingOptions options;
Colm Donelan2ba48d22019-11-29 09:10:59 +00003243 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +00003244 armnn::ArmNNProfilingServiceInitialiser initialiser;
Jim Flynn9c85b412022-03-16 00:27:43 +00003245 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER,
3246 initialiser,
3247 arm::pipe::ARMNN_SOFTWARE_INFO,
3248 arm::pipe::ARMNN_SOFTWARE_VERSION,
3249 arm::pipe::ARMNN_HARDWARE_VERSION);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003250 profilingService.ResetExternalProfilingOptions(options, true);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003251 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003252 profilingService.Update();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003253 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003254
Colm Donelan2ba48d22019-11-29 09:10:59 +00003255 profilingService.Update();
Finn Williams09ad6f92019-12-19 17:05:18 +00003256
3257 // Reset the profiling service to stop any running thread
3258 options.m_EnableProfiling = false;
3259 profilingService.ResetExternalProfilingOptions(options, true);
3260
Colm Donelan2ba48d22019-11-29 09:10:59 +00003261 streamRedirector.CancelRedirect();
3262
3263 // Check that the expected error has occurred and logged to the standard output
David Monahana8837bf2020-04-16 10:01:56 +01003264 if (ss.str().find("Cannot connect to stream socket: Connection refused") == std::string::npos)
Colm Donelan2ba48d22019-11-29 09:10:59 +00003265 {
3266 std::cout << ss.str();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003267 FAIL("Expected string not found.");
Colm Donelan2ba48d22019-11-29 09:10:59 +00003268 }
Colm Donelan2ba48d22019-11-29 09:10:59 +00003269}
3270
Sadik Armagan1625efc2021-06-10 18:24:34 +01003271TEST_CASE("CheckProfilingServiceEnabledRuntime")
Colm Donelan2ba48d22019-11-29 09:10:59 +00003272{
3273 // 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 +00003274 LogLevelSwapper logLevelSwapper(arm::pipe::LogSeverity::Warning);
3275
3276 // Redirect the output to a local stream so that we can parse the warning message
3277 std::stringstream ss;
3278 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3279
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003280 ProfilingOptions options;
Jim Flynn34430252022-03-04 15:03:58 +00003281 armnn::ArmNNProfilingServiceInitialiser initialiser;
Jim Flynn9c85b412022-03-16 00:27:43 +00003282 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER,
3283 initialiser,
3284 arm::pipe::ARMNN_SOFTWARE_INFO,
3285 arm::pipe::ARMNN_SOFTWARE_VERSION,
3286 arm::pipe::ARMNN_HARDWARE_VERSION);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003287 profilingService.ResetExternalProfilingOptions(options, true);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003288 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003289 profilingService.Update();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003290 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003291 options.m_EnableProfiling = true;
3292 profilingService.ResetExternalProfilingOptions(options);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003293 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003294 profilingService.Update();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003295 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003296
Colm Donelan2ba48d22019-11-29 09:10:59 +00003297 profilingService.Update();
3298
Finn Williams09ad6f92019-12-19 17:05:18 +00003299 // Reset the profiling service to stop any running thread
3300 options.m_EnableProfiling = false;
3301 profilingService.ResetExternalProfilingOptions(options, true);
3302
Colm Donelan2ba48d22019-11-29 09:10:59 +00003303 streamRedirector.CancelRedirect();
3304
3305 // Check that the expected error has occurred and logged to the standard output
David Monahana8837bf2020-04-16 10:01:56 +01003306 if (ss.str().find("Cannot connect to stream socket: Connection refused") == std::string::npos)
Colm Donelan2ba48d22019-11-29 09:10:59 +00003307 {
3308 std::cout << ss.str();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003309 FAIL("Expected string not found.");
Colm Donelan2ba48d22019-11-29 09:10:59 +00003310 }
Colm Donelan2ba48d22019-11-29 09:10:59 +00003311}
3312
Sadik Armagan1625efc2021-06-10 18:24:34 +01003313TEST_CASE("CheckProfilingServiceBadConnectionAcknowledgedPacket")
Colm Donelan2ba48d22019-11-29 09:10:59 +00003314{
3315 // 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 +00003316 LogLevelSwapper logLevelSwapper(arm::pipe::LogSeverity::Warning);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003317
3318 // Redirect the standard output to a local stream so that we can parse the warning message
3319 std::stringstream ss;
3320 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3321
Colm Donelan2ba48d22019-11-29 09:10:59 +00003322 // Reset the profiling service to the uninitialized state
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003323 ProfilingOptions options;
Colm Donelan2ba48d22019-11-29 09:10:59 +00003324 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +00003325 armnn::ArmNNProfilingServiceInitialiser initialiser;
Jim Flynn9c85b412022-03-16 00:27:43 +00003326 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER,
3327 initialiser,
3328 arm::pipe::ARMNN_SOFTWARE_INFO,
3329 arm::pipe::ARMNN_SOFTWARE_VERSION,
3330 arm::pipe::ARMNN_HARDWARE_VERSION);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003331 profilingService.ResetExternalProfilingOptions(options, true);
3332
Sadik Armagan3184c902020-03-18 10:57:30 +00003333 // Swap the profiling connection factory in the profiling service instance with our mock one
Jim Flynn34430252022-03-04 15:03:58 +00003334 SwapProfilingConnectionFactoryHelper helper(arm::pipe::MAX_ARMNN_COUNTER, initialiser, profilingService);
Sadik Armagan3184c902020-03-18 10:57:30 +00003335
Colm Donelan2ba48d22019-11-29 09:10:59 +00003336 // Bring the profiling service to the "WaitingForAck" state
Sadik Armagan1625efc2021-06-10 18:24:34 +01003337 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003338 profilingService.Update(); // Initialize the counter directory
Sadik Armagan1625efc2021-06-10 18:24:34 +01003339 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003340 profilingService.Update(); // Create the profiling connection
3341
3342 // Get the mock profiling connection
3343 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003344 CHECK(mockProfilingConnection);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003345
Sadik Armagan1625efc2021-06-10 18:24:34 +01003346 CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003347
3348 // Connection Acknowledged Packet header (word 0, word 1 is always zero):
3349 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
3350 // 16:25 [10] packet_id: Packet identifier, value 0b0000000001
3351 // 8:15 [8] reserved: Reserved, value 0b00000000
3352 // 0:7 [8] reserved: Reserved, value 0b00000000
3353 uint32_t packetFamily = 0;
3354 uint32_t packetId = 37; // Wrong packet id!!!
3355 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
3356
3357 // Create the Connection Acknowledged Packet
Jim Flynnbbfe6032020-07-20 16:57:44 +01003358 arm::pipe::Packet connectionAcknowledgedPacket(header);
Finn Williams09ad6f92019-12-19 17:05:18 +00003359 // Write an invalid "Connection Acknowledged" packet into the mock profiling connection, to simulate an invalid
3360 // reply from an external profiling service
Colm Donelan2ba48d22019-11-29 09:10:59 +00003361 mockProfilingConnection->WritePacket(std::move(connectionAcknowledgedPacket));
3362
Finn Williams09ad6f92019-12-19 17:05:18 +00003363 // Start the command thread
3364 profilingService.Update();
3365
3366 // Wait for the command thread to join
3367 options.m_EnableProfiling = false;
3368 profilingService.ResetExternalProfilingOptions(options, true);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003369
3370 streamRedirector.CancelRedirect();
3371
3372 // Check that the expected error has occurred and logged to the standard output
David Monahana8837bf2020-04-16 10:01:56 +01003373 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 +00003374 {
3375 std::cout << ss.str();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003376 FAIL("Expected string not found.");
Colm Donelan2ba48d22019-11-29 09:10:59 +00003377 }
Colm Donelan2ba48d22019-11-29 09:10:59 +00003378}
3379
Sadik Armagan1625efc2021-06-10 18:24:34 +01003380TEST_CASE("CheckProfilingServiceBadRequestCounterDirectoryPacket")
Colm Donelan2ba48d22019-11-29 09:10:59 +00003381{
3382 // 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 +00003383 LogLevelSwapper logLevelSwapper(arm::pipe::LogSeverity::Warning);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003384
3385 // Redirect the standard output to a local stream so that we can parse the warning message
3386 std::stringstream ss;
3387 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3388
3389 // Reset the profiling service to the uninitialized state
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003390 ProfilingOptions options;
Colm Donelan2ba48d22019-11-29 09:10:59 +00003391 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +00003392 armnn::ArmNNProfilingServiceInitialiser initialiser;
Jim Flynn9c85b412022-03-16 00:27:43 +00003393 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER,
3394 initialiser,
3395 arm::pipe::ARMNN_SOFTWARE_INFO,
3396 arm::pipe::ARMNN_SOFTWARE_VERSION,
3397 arm::pipe::ARMNN_HARDWARE_VERSION);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003398 profilingService.ResetExternalProfilingOptions(options, true);
3399
Sadik Armagan3184c902020-03-18 10:57:30 +00003400 // Swap the profiling connection factory in the profiling service instance with our mock one
Jim Flynn34430252022-03-04 15:03:58 +00003401 SwapProfilingConnectionFactoryHelper helper(arm::pipe::MAX_ARMNN_COUNTER, initialiser, profilingService);
Sadik Armagan3184c902020-03-18 10:57:30 +00003402
Colm Donelan2ba48d22019-11-29 09:10:59 +00003403 // Bring the profiling service to the "Active" state
Sadik Armagan1625efc2021-06-10 18:24:34 +01003404 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003405 helper.ForceTransitionToState(ProfilingState::NotConnected);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003406 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003407 profilingService.Update(); // Create the profiling connection
Sadik Armagan1625efc2021-06-10 18:24:34 +01003408 CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003409
3410 // Get the mock profiling connection
3411 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003412 CHECK(mockProfilingConnection);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003413
Colm Donelan2ba48d22019-11-29 09:10:59 +00003414 // Write a valid "Request Counter Directory" packet into the mock profiling connection, to simulate a valid
3415 // reply from an external profiling service
3416
3417 // Request Counter Directory packet header (word 0, word 1 is always zero):
3418 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
3419 // 16:25 [10] packet_id: Packet identifier, value 0b0000000011
3420 // 8:15 [8] reserved: Reserved, value 0b00000000
3421 // 0:7 [8] reserved: Reserved, value 0b00000000
3422 uint32_t packetFamily = 0;
3423 uint32_t packetId = 123; // Wrong packet id!!!
3424 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
3425
3426 // Create the Request Counter Directory packet
Jim Flynnbbfe6032020-07-20 16:57:44 +01003427 arm::pipe::Packet requestCounterDirectoryPacket(header);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003428
3429 // Write the packet to the mock profiling connection
3430 mockProfilingConnection->WritePacket(std::move(requestCounterDirectoryPacket));
3431
Finn Williams09ad6f92019-12-19 17:05:18 +00003432 // Start the command handler and the send thread
3433 profilingService.Update();
3434
3435 // Reset the profiling service to stop and join any running thread
3436 options.m_EnableProfiling = false;
3437 profilingService.ResetExternalProfilingOptions(options, true);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003438
3439 streamRedirector.CancelRedirect();
3440
3441 // Check that the expected error has occurred and logged to the standard output
David Monahana8837bf2020-04-16 10:01:56 +01003442 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 +00003443 {
3444 std::cout << ss.str();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003445 FAIL("Expected string not found.");
Colm Donelan2ba48d22019-11-29 09:10:59 +00003446 }
Colm Donelan2ba48d22019-11-29 09:10:59 +00003447}
3448
Sadik Armagan1625efc2021-06-10 18:24:34 +01003449TEST_CASE("CheckProfilingServiceBadPeriodicCounterSelectionPacket")
Colm Donelan2ba48d22019-11-29 09:10:59 +00003450{
3451 // 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 +00003452 LogLevelSwapper logLevelSwapper(arm::pipe::LogSeverity::Warning);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003453
3454 // Redirect the standard output to a local stream so that we can parse the warning message
3455 std::stringstream ss;
3456 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3457
3458 // Reset the profiling service to the uninitialized state
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003459 ProfilingOptions options;
Colm Donelan2ba48d22019-11-29 09:10:59 +00003460 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +00003461 armnn::ArmNNProfilingServiceInitialiser initialiser;
Jim Flynn9c85b412022-03-16 00:27:43 +00003462 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER,
3463 initialiser,
3464 arm::pipe::ARMNN_SOFTWARE_INFO,
3465 arm::pipe::ARMNN_SOFTWARE_VERSION,
3466 arm::pipe::ARMNN_HARDWARE_VERSION);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003467 profilingService.ResetExternalProfilingOptions(options, true);
3468
Sadik Armagan3184c902020-03-18 10:57:30 +00003469 // Swap the profiling connection factory in the profiling service instance with our mock one
Jim Flynn34430252022-03-04 15:03:58 +00003470 SwapProfilingConnectionFactoryHelper helper(arm::pipe::MAX_ARMNN_COUNTER, initialiser, profilingService);
Sadik Armagan3184c902020-03-18 10:57:30 +00003471
Colm Donelan2ba48d22019-11-29 09:10:59 +00003472 // Bring the profiling service to the "Active" state
Sadik Armagan1625efc2021-06-10 18:24:34 +01003473 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003474 profilingService.Update(); // Initialize the counter directory
Sadik Armagan1625efc2021-06-10 18:24:34 +01003475 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003476 profilingService.Update(); // Create the profiling connection
Sadik Armagan1625efc2021-06-10 18:24:34 +01003477 CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003478 profilingService.Update(); // Start the command handler and the send thread
3479
3480 // Get the mock profiling connection
3481 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003482 CHECK(mockProfilingConnection);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003483
Colm Donelan2ba48d22019-11-29 09:10:59 +00003484 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
3485 // external profiling service
3486
3487 // Periodic Counter Selection packet header:
3488 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
3489 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
3490 // 8:15 [8] reserved: Reserved, value 0b00000000
3491 // 0:7 [8] reserved: Reserved, value 0b00000000
3492 uint32_t packetFamily = 0;
3493 uint32_t packetId = 999; // Wrong packet id!!!
3494 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
3495
3496 // Create the Periodic Counter Selection packet
Jim Flynnbbfe6032020-07-20 16:57:44 +01003497 // Length == 0, this will disable the collection of counters
3498 arm::pipe::Packet periodicCounterSelectionPacket(header);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003499
3500 // Write the packet to the mock profiling connection
3501 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
Finn Williams09ad6f92019-12-19 17:05:18 +00003502 profilingService.Update();
Colm Donelan2ba48d22019-11-29 09:10:59 +00003503
Finn Williams09ad6f92019-12-19 17:05:18 +00003504 // Reset the profiling service to stop any running thread
3505 options.m_EnableProfiling = false;
3506 profilingService.ResetExternalProfilingOptions(options, true);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003507
3508 // Check that the expected error has occurred and logged to the standard output
3509 streamRedirector.CancelRedirect();
3510
3511 // Check that the expected error has occurred and logged to the standard output
David Monahana8837bf2020-04-16 10:01:56 +01003512 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 +00003513 {
3514 std::cout << ss.str();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003515 FAIL("Expected string not found.");
Colm Donelan2ba48d22019-11-29 09:10:59 +00003516 }
Colm Donelan2ba48d22019-11-29 09:10:59 +00003517}
Jim Flynn97897022020-02-02 12:52:59 +00003518
Sadik Armagan1625efc2021-06-10 18:24:34 +01003519TEST_CASE("CheckCounterIdMap")
David Monahande803072020-01-30 12:44:23 +00003520{
3521 CounterIdMap counterIdMap;
Jim Flynnf9db3ef2022-03-08 21:23:44 +00003522 CHECK_THROWS_AS(counterIdMap.GetBackendId(0), arm::pipe::ProfilingException);
3523 CHECK_THROWS_AS(counterIdMap.GetGlobalId(0, armnn::profiling::BACKEND_ID), arm::pipe::ProfilingException);
David Monahande803072020-01-30 12:44:23 +00003524
3525 uint16_t globalCounterIds = 0;
3526
Cathal Corbett6f073722022-03-04 12:11:09 +00003527 std::string cpuRefId(GetComputeDeviceAsCString(armnn::Compute::CpuRef));
3528 std::string cpuAccId(GetComputeDeviceAsCString(armnn::Compute::CpuAcc));
David Monahande803072020-01-30 12:44:23 +00003529
3530 std::vector<uint16_t> cpuRefCounters = {0, 1, 2, 3};
3531 std::vector<uint16_t> cpuAccCounters = {0, 1};
3532
3533 for (uint16_t backendCounterId : cpuRefCounters)
3534 {
3535 counterIdMap.RegisterMapping(globalCounterIds, backendCounterId, cpuRefId);
3536 ++globalCounterIds;
3537 }
3538 for (uint16_t backendCounterId : cpuAccCounters)
3539 {
3540 counterIdMap.RegisterMapping(globalCounterIds, backendCounterId, cpuAccId);
3541 ++globalCounterIds;
3542 }
3543
Cathal Corbett6f073722022-03-04 12:11:09 +00003544 CHECK(counterIdMap.GetBackendId(0) == (std::pair<uint16_t, std::string>(0, cpuRefId)));
3545 CHECK(counterIdMap.GetBackendId(1) == (std::pair<uint16_t, std::string>(1, cpuRefId)));
3546 CHECK(counterIdMap.GetBackendId(2) == (std::pair<uint16_t, std::string>(2, cpuRefId)));
3547 CHECK(counterIdMap.GetBackendId(3) == (std::pair<uint16_t, std::string>(3, cpuRefId)));
3548 CHECK(counterIdMap.GetBackendId(4) == (std::pair<uint16_t, std::string>(0, cpuAccId)));
3549 CHECK(counterIdMap.GetBackendId(5) == (std::pair<uint16_t, std::string>(1, cpuAccId)));
David Monahande803072020-01-30 12:44:23 +00003550
Sadik Armagan1625efc2021-06-10 18:24:34 +01003551 CHECK(counterIdMap.GetGlobalId(0, cpuRefId) == 0);
3552 CHECK(counterIdMap.GetGlobalId(1, cpuRefId) == 1);
3553 CHECK(counterIdMap.GetGlobalId(2, cpuRefId) == 2);
3554 CHECK(counterIdMap.GetGlobalId(3, cpuRefId) == 3);
3555 CHECK(counterIdMap.GetGlobalId(0, cpuAccId) == 4);
3556 CHECK(counterIdMap.GetGlobalId(1, cpuAccId) == 5);
David Monahande803072020-01-30 12:44:23 +00003557}
Colm Donelan2ba48d22019-11-29 09:10:59 +00003558
Sadik Armagan1625efc2021-06-10 18:24:34 +01003559TEST_CASE("CheckRegisterBackendCounters")
Jim Flynn97897022020-02-02 12:52:59 +00003560{
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00003561 uint16_t globalCounterIds = INFERENCES_RUN;
Cathal Corbett6f073722022-03-04 12:11:09 +00003562 std::string cpuRefId(GetComputeDeviceAsCString(armnn::Compute::CpuRef));
Jim Flynn97897022020-02-02 12:52:59 +00003563
Jim Flynn97897022020-02-02 12:52:59 +00003564 // Reset the profiling service to the uninitialized state
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003565 ProfilingOptions options;
Jim Flynn97897022020-02-02 12:52:59 +00003566 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +00003567 armnn::ArmNNProfilingServiceInitialiser initialiser;
Jim Flynn9c85b412022-03-16 00:27:43 +00003568 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER,
3569 initialiser,
3570 arm::pipe::ARMNN_SOFTWARE_INFO,
3571 arm::pipe::ARMNN_SOFTWARE_VERSION,
3572 arm::pipe::ARMNN_HARDWARE_VERSION);
Jim Flynn97897022020-02-02 12:52:59 +00003573 profilingService.ResetExternalProfilingOptions(options, true);
3574
Sadik Armagan3184c902020-03-18 10:57:30 +00003575 RegisterBackendCounters registerBackendCounters(globalCounterIds, cpuRefId, profilingService);
3576
3577
3578
Sadik Armagan1625efc2021-06-10 18:24:34 +01003579 CHECK(profilingService.GetCounterDirectory().GetCategories().empty());
Jim Flynn97897022020-02-02 12:52:59 +00003580 registerBackendCounters.RegisterCategory("categoryOne");
3581 auto categoryOnePtr = profilingService.GetCounterDirectory().GetCategory("categoryOne");
Sadik Armagan1625efc2021-06-10 18:24:34 +01003582 CHECK(categoryOnePtr);
Jim Flynn97897022020-02-02 12:52:59 +00003583
Sadik Armagan1625efc2021-06-10 18:24:34 +01003584 CHECK(profilingService.GetCounterDirectory().GetDevices().empty());
Jim Flynn97897022020-02-02 12:52:59 +00003585 globalCounterIds = registerBackendCounters.RegisterDevice("deviceOne");
3586 auto deviceOnePtr = profilingService.GetCounterDirectory().GetDevice(globalCounterIds);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003587 CHECK(deviceOnePtr);
3588 CHECK(deviceOnePtr->m_Name == "deviceOne");
Jim Flynn97897022020-02-02 12:52:59 +00003589
Sadik Armagan1625efc2021-06-10 18:24:34 +01003590 CHECK(profilingService.GetCounterDirectory().GetCounterSets().empty());
Jim Flynn97897022020-02-02 12:52:59 +00003591 globalCounterIds = registerBackendCounters.RegisterCounterSet("counterSetOne");
3592 auto counterSetOnePtr = profilingService.GetCounterDirectory().GetCounterSet(globalCounterIds);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003593 CHECK(counterSetOnePtr);
3594 CHECK(counterSetOnePtr->m_Name == "counterSetOne");
Jim Flynn97897022020-02-02 12:52:59 +00003595
3596 uint16_t newGlobalCounterId = registerBackendCounters.RegisterCounter(0,
3597 "categoryOne",
3598 0,
3599 0,
3600 1.f,
3601 "CounterOne",
3602 "first test counter");
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00003603 CHECK((newGlobalCounterId = INFERENCES_RUN + 1));
Jim Flynn97897022020-02-02 12:52:59 +00003604 uint16_t mappedGlobalId = profilingService.GetCounterMappings().GetGlobalId(0, cpuRefId);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003605 CHECK(mappedGlobalId == newGlobalCounterId);
Jim Flynn97897022020-02-02 12:52:59 +00003606 auto backendMapping = profilingService.GetCounterMappings().GetBackendId(newGlobalCounterId);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003607 CHECK(backendMapping.first == 0);
3608 CHECK(backendMapping.second == cpuRefId);
Jim Flynn97897022020-02-02 12:52:59 +00003609
3610 // Reset the profiling service to stop any running thread
3611 options.m_EnableProfiling = false;
3612 profilingService.ResetExternalProfilingOptions(options, true);
3613}
3614
Sadik Armagan1625efc2021-06-10 18:24:34 +01003615TEST_CASE("CheckCounterStatusQuery")
James Conroy2dcd3fe2020-02-06 18:34:52 +00003616{
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003617 ProfilingOptions options;
3618 options.m_EnableProfiling = true;
James Conroy2dcd3fe2020-02-06 18:34:52 +00003619
3620 // Reset the profiling service to the uninitialized state
Jim Flynn34430252022-03-04 15:03:58 +00003621 armnn::ArmNNProfilingServiceInitialiser initialiser;
Jim Flynn9c85b412022-03-16 00:27:43 +00003622 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER,
3623 initialiser,
3624 arm::pipe::ARMNN_SOFTWARE_INFO,
3625 arm::pipe::ARMNN_SOFTWARE_VERSION,
3626 arm::pipe::ARMNN_HARDWARE_VERSION);
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003627 profilingService.ResetExternalProfilingOptions(options, true);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003628
Cathal Corbett6f073722022-03-04 12:11:09 +00003629 const std::string cpuRefId(GetComputeDeviceAsCString(armnn::Compute::CpuRef));
3630 const std::string cpuAccId(GetComputeDeviceAsCString(armnn::Compute::CpuAcc));
James Conroy2dcd3fe2020-02-06 18:34:52 +00003631
3632 // Create BackendProfiling for each backend
3633 BackendProfiling backendProfilingCpuRef(options, profilingService, cpuRefId);
3634 BackendProfiling backendProfilingCpuAcc(options, profilingService, cpuAccId);
3635
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00003636 uint16_t initialNumGlobalCounterIds = INFERENCES_RUN;
James Conroy2dcd3fe2020-02-06 18:34:52 +00003637
3638 // Create RegisterBackendCounters for CpuRef
Sadik Armagan3184c902020-03-18 10:57:30 +00003639 RegisterBackendCounters registerBackendCountersCpuRef(initialNumGlobalCounterIds, cpuRefId, profilingService);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003640
3641 // Create 'testCategory' in CounterDirectory (backend agnostic)
Sadik Armagan1625efc2021-06-10 18:24:34 +01003642 CHECK(profilingService.GetCounterDirectory().GetCategories().empty());
James Conroy2dcd3fe2020-02-06 18:34:52 +00003643 registerBackendCountersCpuRef.RegisterCategory("testCategory");
3644 auto categoryOnePtr = profilingService.GetCounterDirectory().GetCategory("testCategory");
Sadik Armagan1625efc2021-06-10 18:24:34 +01003645 CHECK(categoryOnePtr);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003646
3647 // Counters:
3648 // Global | Local | Backend
3649 // 5 | 0 | CpuRef
3650 // 6 | 1 | CpuRef
3651 // 7 | 1 | CpuAcc
3652
3653 std::vector<uint16_t> cpuRefCounters = {0, 1};
3654 std::vector<uint16_t> cpuAccCounters = {0};
3655
3656 // Register the backend counters for CpuRef and validate GetGlobalId and GetBackendId
3657 uint16_t currentNumGlobalCounterIds = registerBackendCountersCpuRef.RegisterCounter(
3658 0, "testCategory", 0, 0, 1.f, "CpuRefCounter0", "Zeroth CpuRef Counter");
Sadik Armagan1625efc2021-06-10 18:24:34 +01003659 CHECK(currentNumGlobalCounterIds == initialNumGlobalCounterIds + 1);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003660 uint16_t mappedGlobalId = profilingService.GetCounterMappings().GetGlobalId(0, cpuRefId);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003661 CHECK(mappedGlobalId == currentNumGlobalCounterIds);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003662 auto backendMapping = profilingService.GetCounterMappings().GetBackendId(currentNumGlobalCounterIds);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003663 CHECK(backendMapping.first == 0);
3664 CHECK(backendMapping.second == cpuRefId);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003665
3666 currentNumGlobalCounterIds = registerBackendCountersCpuRef.RegisterCounter(
3667 1, "testCategory", 0, 0, 1.f, "CpuRefCounter1", "First CpuRef Counter");
Sadik Armagan1625efc2021-06-10 18:24:34 +01003668 CHECK(currentNumGlobalCounterIds == initialNumGlobalCounterIds + 2);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003669 mappedGlobalId = profilingService.GetCounterMappings().GetGlobalId(1, cpuRefId);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003670 CHECK(mappedGlobalId == currentNumGlobalCounterIds);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003671 backendMapping = profilingService.GetCounterMappings().GetBackendId(currentNumGlobalCounterIds);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003672 CHECK(backendMapping.first == 1);
3673 CHECK(backendMapping.second == cpuRefId);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003674
3675 // Create RegisterBackendCounters for CpuAcc
Sadik Armagan3184c902020-03-18 10:57:30 +00003676 RegisterBackendCounters registerBackendCountersCpuAcc(currentNumGlobalCounterIds, cpuAccId, profilingService);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003677
3678 // Register the backend counter for CpuAcc and validate GetGlobalId and GetBackendId
3679 currentNumGlobalCounterIds = registerBackendCountersCpuAcc.RegisterCounter(
3680 0, "testCategory", 0, 0, 1.f, "CpuAccCounter0", "Zeroth CpuAcc Counter");
Sadik Armagan1625efc2021-06-10 18:24:34 +01003681 CHECK(currentNumGlobalCounterIds == initialNumGlobalCounterIds + 3);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003682 mappedGlobalId = profilingService.GetCounterMappings().GetGlobalId(0, cpuAccId);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003683 CHECK(mappedGlobalId == currentNumGlobalCounterIds);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003684 backendMapping = profilingService.GetCounterMappings().GetBackendId(currentNumGlobalCounterIds);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003685 CHECK(backendMapping.first == 0);
3686 CHECK(backendMapping.second == cpuAccId);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003687
3688 // Create vectors for active counters
3689 const std::vector<uint16_t> activeGlobalCounterIds = {5}; // CpuRef(0) activated
3690 const std::vector<uint16_t> newActiveGlobalCounterIds = {6, 7}; // CpuRef(0) and CpuAcc(1) activated
3691
3692 const uint32_t capturePeriod = 200;
3693 const uint32_t newCapturePeriod = 100;
3694
3695 // Set capture period and active counters in CaptureData
Finn Williams032bc742020-02-12 11:02:34 +00003696 profilingService.SetCaptureData(capturePeriod, activeGlobalCounterIds, {});
James Conroy2dcd3fe2020-02-06 18:34:52 +00003697
3698 // Get vector of active counters for CpuRef and CpuAcc backends
3699 std::vector<CounterStatus> cpuRefCounterStatus = backendProfilingCpuRef.GetActiveCounters();
3700 std::vector<CounterStatus> cpuAccCounterStatus = backendProfilingCpuAcc.GetActiveCounters();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003701 CHECK_EQ(cpuRefCounterStatus.size(), 1);
3702 CHECK_EQ(cpuAccCounterStatus.size(), 0);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003703
3704 // Check active CpuRef counter
Sadik Armagan1625efc2021-06-10 18:24:34 +01003705 CHECK_EQ(cpuRefCounterStatus[0].m_GlobalCounterId, activeGlobalCounterIds[0]);
3706 CHECK_EQ(cpuRefCounterStatus[0].m_BackendCounterId, cpuRefCounters[0]);
3707 CHECK_EQ(cpuRefCounterStatus[0].m_SamplingRateInMicroseconds, capturePeriod);
3708 CHECK_EQ(cpuRefCounterStatus[0].m_Enabled, true);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003709
3710 // Check inactive CpuRef counter
3711 CounterStatus inactiveCpuRefCounter = backendProfilingCpuRef.GetCounterStatus(cpuRefCounters[1]);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003712 CHECK_EQ(inactiveCpuRefCounter.m_GlobalCounterId, 6);
3713 CHECK_EQ(inactiveCpuRefCounter.m_BackendCounterId, cpuRefCounters[1]);
3714 CHECK_EQ(inactiveCpuRefCounter.m_SamplingRateInMicroseconds, 0);
3715 CHECK_EQ(inactiveCpuRefCounter.m_Enabled, false);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003716
3717 // Check inactive CpuAcc counter
3718 CounterStatus inactiveCpuAccCounter = backendProfilingCpuAcc.GetCounterStatus(cpuAccCounters[0]);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003719 CHECK_EQ(inactiveCpuAccCounter.m_GlobalCounterId, 7);
3720 CHECK_EQ(inactiveCpuAccCounter.m_BackendCounterId, cpuAccCounters[0]);
3721 CHECK_EQ(inactiveCpuAccCounter.m_SamplingRateInMicroseconds, 0);
3722 CHECK_EQ(inactiveCpuAccCounter.m_Enabled, false);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003723
3724 // Set new capture period and new active counters in CaptureData
Finn Williams032bc742020-02-12 11:02:34 +00003725 profilingService.SetCaptureData(newCapturePeriod, newActiveGlobalCounterIds, {});
James Conroy2dcd3fe2020-02-06 18:34:52 +00003726
3727 // Get vector of active counters for CpuRef and CpuAcc backends
3728 cpuRefCounterStatus = backendProfilingCpuRef.GetActiveCounters();
3729 cpuAccCounterStatus = backendProfilingCpuAcc.GetActiveCounters();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003730 CHECK_EQ(cpuRefCounterStatus.size(), 1);
3731 CHECK_EQ(cpuAccCounterStatus.size(), 1);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003732
3733 // Check active CpuRef counter
Sadik Armagan1625efc2021-06-10 18:24:34 +01003734 CHECK_EQ(cpuRefCounterStatus[0].m_GlobalCounterId, newActiveGlobalCounterIds[0]);
3735 CHECK_EQ(cpuRefCounterStatus[0].m_BackendCounterId, cpuRefCounters[1]);
3736 CHECK_EQ(cpuRefCounterStatus[0].m_SamplingRateInMicroseconds, newCapturePeriod);
3737 CHECK_EQ(cpuRefCounterStatus[0].m_Enabled, true);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003738
3739 // Check active CpuAcc counter
Sadik Armagan1625efc2021-06-10 18:24:34 +01003740 CHECK_EQ(cpuAccCounterStatus[0].m_GlobalCounterId, newActiveGlobalCounterIds[1]);
3741 CHECK_EQ(cpuAccCounterStatus[0].m_BackendCounterId, cpuAccCounters[0]);
3742 CHECK_EQ(cpuAccCounterStatus[0].m_SamplingRateInMicroseconds, newCapturePeriod);
3743 CHECK_EQ(cpuAccCounterStatus[0].m_Enabled, true);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003744
3745 // Check inactive CpuRef counter
3746 inactiveCpuRefCounter = backendProfilingCpuRef.GetCounterStatus(cpuRefCounters[0]);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003747 CHECK_EQ(inactiveCpuRefCounter.m_GlobalCounterId, 5);
3748 CHECK_EQ(inactiveCpuRefCounter.m_BackendCounterId, cpuRefCounters[0]);
3749 CHECK_EQ(inactiveCpuRefCounter.m_SamplingRateInMicroseconds, 0);
3750 CHECK_EQ(inactiveCpuRefCounter.m_Enabled, false);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003751
3752 // Reset the profiling service to stop any running thread
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003753 options.m_EnableProfiling = false;
3754 profilingService.ResetExternalProfilingOptions(options, true);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003755}
3756
Sadik Armagan1625efc2021-06-10 18:24:34 +01003757TEST_CASE("CheckRegisterCounters")
Sadik Armagancab588a2020-02-17 11:33:31 +00003758{
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003759 ProfilingOptions options;
3760 options.m_EnableProfiling = true;
Sadik Armagancab588a2020-02-17 11:33:31 +00003761 MockBufferManager mockBuffer(1024);
Sadik Armagan3184c902020-03-18 10:57:30 +00003762
Sadik Armagancab588a2020-02-17 11:33:31 +00003763 CaptureData captureData;
Jim Flynn34430252022-03-04 15:03:58 +00003764
3765 armnn::ArmNNProfilingServiceInitialiser initialiser;
3766 MockProfilingService mockProfilingService(
3767 arm::pipe::MAX_ARMNN_COUNTER, initialiser, mockBuffer, options.m_EnableProfiling, captureData);
Cathal Corbett6f073722022-03-04 12:11:09 +00003768 std::string cpuRefId(GetComputeDeviceAsCString(armnn::Compute::CpuRef));
Sadik Armagancab588a2020-02-17 11:33:31 +00003769
3770 mockProfilingService.RegisterMapping(6, 0, cpuRefId);
3771 mockProfilingService.RegisterMapping(7, 1, cpuRefId);
3772 mockProfilingService.RegisterMapping(8, 2, cpuRefId);
3773
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00003774 BackendProfiling backendProfiling(options,
Sadik Armagancab588a2020-02-17 11:33:31 +00003775 mockProfilingService,
3776 cpuRefId);
3777
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00003778 Timestamp timestamp;
Sadik Armagancab588a2020-02-17 11:33:31 +00003779 timestamp.timestamp = 1000998;
3780 timestamp.counterValues.emplace_back(0, 700);
3781 timestamp.counterValues.emplace_back(2, 93);
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00003782 std::vector<Timestamp> timestamps;
Sadik Armagancab588a2020-02-17 11:33:31 +00003783 timestamps.push_back(timestamp);
3784 backendProfiling.ReportCounters(timestamps);
3785
3786 auto readBuffer = mockBuffer.GetReadableBuffer();
3787
3788 uint32_t headerWord0 = ReadUint32(readBuffer, 0);
3789 uint32_t headerWord1 = ReadUint32(readBuffer, 4);
3790 uint64_t readTimestamp = ReadUint64(readBuffer, 8);
3791
Sadik Armagan1625efc2021-06-10 18:24:34 +01003792 CHECK(((headerWord0 >> 26) & 0x0000003F) == 3); // packet family
3793 CHECK(((headerWord0 >> 19) & 0x0000007F) == 0); // packet class
3794 CHECK(((headerWord0 >> 16) & 0x00000007) == 0); // packet type
3795 CHECK(headerWord1 == 20); // data length
3796 CHECK(1000998 == readTimestamp); // capture period
Sadik Armagancab588a2020-02-17 11:33:31 +00003797
3798 uint32_t offset = 16;
3799 // Check Counter Index
3800 uint16_t readIndex = ReadUint16(readBuffer, offset);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003801 CHECK(6 == readIndex);
Sadik Armagancab588a2020-02-17 11:33:31 +00003802
3803 // Check Counter Value
3804 offset += 2;
3805 uint32_t readValue = ReadUint32(readBuffer, offset);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003806 CHECK(700 == readValue);
Sadik Armagancab588a2020-02-17 11:33:31 +00003807
3808 // Check Counter Index
3809 offset += 4;
3810 readIndex = ReadUint16(readBuffer, offset);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003811 CHECK(8 == readIndex);
Sadik Armagancab588a2020-02-17 11:33:31 +00003812
3813 // Check Counter Value
3814 offset += 2;
3815 readValue = ReadUint32(readBuffer, offset);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003816 CHECK(93 == readValue);
Sadik Armagancab588a2020-02-17 11:33:31 +00003817}
3818
Sadik Armagan1625efc2021-06-10 18:24:34 +01003819TEST_CASE("CheckFileFormat") {
Isabella Gottardia0687ee2020-03-11 18:04:20 +00003820 // 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 +00003821 LogLevelSwapper logLevelSwapper(arm::pipe::LogSeverity::Warning);
3822
3823 // Redirect the output to a local stream so that we can parse the warning message
3824 std::stringstream ss;
3825 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
Isabella Gottardia0687ee2020-03-11 18:04:20 +00003826
3827 // Create profiling options.
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003828 ProfilingOptions options;
Isabella Gottardia0687ee2020-03-11 18:04:20 +00003829 options.m_EnableProfiling = true;
3830 // Check the default value set to binary
Sadik Armagan1625efc2021-06-10 18:24:34 +01003831 CHECK(options.m_FileFormat == "binary");
Isabella Gottardia0687ee2020-03-11 18:04:20 +00003832
3833 // Change file format to an unsupported value
3834 options.m_FileFormat = "json";
3835 // Enable the profiling service
Jim Flynn34430252022-03-04 15:03:58 +00003836 armnn::ArmNNProfilingServiceInitialiser initialiser;
Jim Flynn9c85b412022-03-16 00:27:43 +00003837 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER,
3838 initialiser,
3839 arm::pipe::ARMNN_SOFTWARE_INFO,
3840 arm::pipe::ARMNN_SOFTWARE_VERSION,
3841 arm::pipe::ARMNN_HARDWARE_VERSION);
Isabella Gottardia0687ee2020-03-11 18:04:20 +00003842 profilingService.ResetExternalProfilingOptions(options, true);
3843 // Start the command handler and the send thread
3844 profilingService.Update();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003845 CHECK(profilingService.GetCurrentState()==ProfilingState::NotConnected);
Isabella Gottardia0687ee2020-03-11 18:04:20 +00003846
Isabella Gottardia0687ee2020-03-11 18:04:20 +00003847 // When Update is called and the current state is ProfilingState::NotConnected
3848 // an exception will be raised from GetProfilingConnection and displayed as warning in the output local stream
3849 profilingService.Update();
3850
3851 streamRedirector.CancelRedirect();
3852
3853 // Check that the expected error has occurred and logged to the standard output
David Monahana8837bf2020-04-16 10:01:56 +01003854 if (ss.str().find("Unsupported profiling file format, only binary is supported") == std::string::npos)
Isabella Gottardia0687ee2020-03-11 18:04:20 +00003855 {
3856 std::cout << ss.str();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003857 FAIL("Expected string not found.");
Isabella Gottardia0687ee2020-03-11 18:04:20 +00003858 }
3859}
3860
Sadik Armagan1625efc2021-06-10 18:24:34 +01003861}