blob: 9873527aa99a2d3b99d787a3f6f8ead7f0a2a303 [file] [log] [blame]
Francis Murtagh1f7db452019-08-14 09:49:34 +01001//
Jim Flynn6398a982020-05-27 17:05:21 +01002// Copyright © 2019 Arm Ltd and Contributors. All rights reserved.
Francis Murtagh1f7db452019-08-14 09:49:34 +01003// SPDX-License-Identifier: MIT
4//
5
Matteo Martincighd0613b52019-10-09 16:47:04 +01006#include "ProfilingTests.hpp"
Keith Davis33ed2212020-03-30 10:43:41 +01007#include "ProfilingTestUtils.hpp"
Teresa Charlin9bab4962019-09-06 12:28:35 +01008
Jim Flynn75c14f42022-03-10 22:05:42 +00009#include <ArmNNProfilingServiceInitialiser.hpp>
Matteo Martincigh8a837172019-10-04 17:01:07 +010010#include <CommandHandler.hpp>
Sadik Armaganb5f01b22019-09-18 17:29:00 +010011#include <ConnectionAcknowledgedCommandHandler.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010012#include <CounterDirectory.hpp>
David Monahande803072020-01-30 12:44:23 +000013#include <CounterIdMap.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010014#include <Holder.hpp>
Matteo Martincighe0e6efc2019-10-04 17:17:42 +010015#include <ICounterValues.hpp>
Francis Murtaghfcb8ef62019-09-20 15:40:09 +010016#include <PeriodicCounterCapture.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010017#include <PeriodicCounterSelectionCommandHandler.hpp>
18#include <ProfilingStateMachine.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010019#include <ProfilingUtils.hpp>
James Conroy2dcd3fe2020-02-06 18:34:52 +000020#include <RegisterBackendCounters.hpp>
Narumol Prangnawarat48033692019-09-20 12:04:55 +010021#include <RequestCounterDirectoryCommandHandler.hpp>
Teresa Charlin9bab4962019-09-06 12:28:35 +010022#include <Runtime.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010023#include <SocketProfilingConnection.hpp>
Matteo Martincighcdfb9412019-11-08 11:23:06 +000024#include <SendCounterPacket.hpp>
Sadik Armagan3896b472020-02-10 12:24:15 +000025#include <SendThread.hpp>
Matteo Martincighcdfb9412019-11-08 11:23:06 +000026#include <SendTimelinePacket.hpp>
Keith Davis02356de2019-08-26 18:28:17 +010027
Matteo Martincigh6db5f202019-09-05 12:02:04 +010028#include <armnn/Conversion.hpp>
Matteo Martincigh54fb9572019-10-02 12:50:57 +010029#include <armnn/Utils.hpp>
Jim Flynn75c14f42022-03-10 22:05:42 +000030
31#include <armnn/profiling/ArmNNProfiling.hpp>
32#include <armnn/profiling/ProfilingOptions.hpp>
33
Jan Eilers8eb25602020-03-09 12:13:48 +000034#include <armnn/utility/IgnoreUnused.hpp>
Jim Flynn75c14f42022-03-10 22:05:42 +000035
36#include <backends/BackendProfiling.hpp>
Matteo Martincigh54fb9572019-10-02 12:50:57 +010037
Jim Flynnbbfe6032020-07-20 16:57:44 +010038#include <common/include/CommandHandlerKey.hpp>
39#include <common/include/CommandHandlerRegistry.hpp>
Jim Flynn75c14f42022-03-10 22:05:42 +000040#include <common/include/EncodeVersion.hpp>
41#include <common/include/NumericCast.hpp>
Jim Flynnbbfe6032020-07-20 16:57:44 +010042#include <common/include/Packet.hpp>
Jim Flynn75c14f42022-03-10 22:05:42 +000043#include <common/include/PacketVersionResolver.hpp>
44#include <common/include/SocketConnectionException.hpp>
45#include <common/include/SwTrace.hpp>
Jim Flynnbbfe6032020-07-20 16:57:44 +010046
Sadik Armagan1625efc2021-06-10 18:24:34 +010047#include <doctest/doctest.h>
48
Sadik Armagan95e9efc2021-08-05 15:01:07 +010049#include <algorithm>
Nikhil Rajbc626052019-08-15 15:49:45 +010050#include <cstdint>
51#include <cstring>
Keith Davis3201eea2019-10-24 17:30:41 +010052#include <iostream>
Aron Virginas-Tare898db92019-08-22 12:56:34 +010053#include <limits>
Francis Murtagh11f99b42019-08-16 11:28:52 +010054#include <map>
Aron Virginas-Tare898db92019-08-22 12:56:34 +010055#include <random>
James Conroy2dcd3fe2020-02-06 18:34:52 +000056
Francis Murtagh1f7db452019-08-14 09:49:34 +010057
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000058using namespace arm::pipe;
Finn Williams09ad6f92019-12-19 17:05:18 +000059using PacketType = MockProfilingConnection::PacketType;
Aron Virginas-Tare898db92019-08-22 12:56:34 +010060
Sadik Armagan1625efc2021-06-10 18:24:34 +010061TEST_SUITE("ExternalProfiling")
62{
63TEST_CASE("CheckCommandHandlerKeyComparisons")
Francis Murtagh1f7db452019-08-14 09:49:34 +010064{
Jim Flynnbbfe6032020-07-20 16:57:44 +010065 arm::pipe::CommandHandlerKey testKey1_0(1, 1, 1);
66 arm::pipe::CommandHandlerKey testKey1_1(1, 1, 1);
67 arm::pipe::CommandHandlerKey testKey1_2(1, 2, 1);
Jim Flynn397043f2019-10-17 17:37:10 +010068
Jim Flynnbbfe6032020-07-20 16:57:44 +010069 arm::pipe::CommandHandlerKey testKey0(0, 1, 1);
70 arm::pipe::CommandHandlerKey testKey1(0, 1, 1);
71 arm::pipe::CommandHandlerKey testKey2(0, 1, 1);
72 arm::pipe::CommandHandlerKey testKey3(0, 0, 0);
73 arm::pipe::CommandHandlerKey testKey4(0, 2, 2);
74 arm::pipe::CommandHandlerKey testKey5(0, 0, 2);
Jim Flynn397043f2019-10-17 17:37:10 +010075
Sadik Armagan1625efc2021-06-10 18:24:34 +010076 CHECK(testKey1_0 > testKey0);
77 CHECK(testKey1_0 == testKey1_1);
78 CHECK(testKey1_0 < testKey1_2);
Francis Murtagh1f7db452019-08-14 09:49:34 +010079
Sadik Armagan1625efc2021-06-10 18:24:34 +010080 CHECK(testKey1 < testKey4);
81 CHECK(testKey1 > testKey3);
82 CHECK(testKey1 <= testKey4);
83 CHECK(testKey1 >= testKey3);
84 CHECK(testKey1 <= testKey2);
85 CHECK(testKey1 >= testKey2);
86 CHECK(testKey1 == testKey2);
87 CHECK(testKey1 == testKey1);
Francis Murtagh1f7db452019-08-14 09:49:34 +010088
Sadik Armagan1625efc2021-06-10 18:24:34 +010089 CHECK(!(testKey1 == testKey5));
90 CHECK(!(testKey1 != testKey1));
91 CHECK(testKey1 != testKey5);
Francis Murtagh1f7db452019-08-14 09:49:34 +010092
Sadik Armagan1625efc2021-06-10 18:24:34 +010093 CHECK((testKey1 == testKey2 && testKey2 == testKey1));
94 CHECK((testKey0 == testKey1 && testKey1 == testKey2 && testKey0 == testKey2));
Francis Murtagh1f7db452019-08-14 09:49:34 +010095
Sadik Armagan1625efc2021-06-10 18:24:34 +010096 CHECK(testKey1.GetPacketId() == 1);
97 CHECK(testKey1.GetVersion() == 1);
Francis Murtagh1f7db452019-08-14 09:49:34 +010098
Jim Flynnbbfe6032020-07-20 16:57:44 +010099 std::vector<arm::pipe::CommandHandlerKey> vect = {
100 arm::pipe::CommandHandlerKey(0, 0, 1), arm::pipe::CommandHandlerKey(0, 2, 0),
101 arm::pipe::CommandHandlerKey(0, 1, 0), arm::pipe::CommandHandlerKey(0, 2, 1),
102 arm::pipe::CommandHandlerKey(0, 1, 1), arm::pipe::CommandHandlerKey(0, 0, 1),
103 arm::pipe::CommandHandlerKey(0, 2, 0), arm::pipe::CommandHandlerKey(0, 0, 0) };
Francis Murtagh1f7db452019-08-14 09:49:34 +0100104
105 std::sort(vect.begin(), vect.end());
106
Jim Flynnbbfe6032020-07-20 16:57:44 +0100107 std::vector<arm::pipe::CommandHandlerKey> expectedVect = {
108 arm::pipe::CommandHandlerKey(0, 0, 0), arm::pipe::CommandHandlerKey(0, 0, 1),
109 arm::pipe::CommandHandlerKey(0, 0, 1), arm::pipe::CommandHandlerKey(0, 1, 0),
110 arm::pipe::CommandHandlerKey(0, 1, 1), arm::pipe::CommandHandlerKey(0, 2, 0),
111 arm::pipe::CommandHandlerKey(0, 2, 0), arm::pipe::CommandHandlerKey(0, 2, 1) };
Francis Murtagh1f7db452019-08-14 09:49:34 +0100112
Sadik Armagan1625efc2021-06-10 18:24:34 +0100113 CHECK(vect == expectedVect);
Francis Murtagh1f7db452019-08-14 09:49:34 +0100114}
115
Sadik Armagan1625efc2021-06-10 18:24:34 +0100116TEST_CASE("CheckPacketKeyComparisons")
Jim Flynned25e0e2019-10-18 13:21:43 +0100117{
Jim Flynnbbfe6032020-07-20 16:57:44 +0100118 arm::pipe::PacketKey key0(0, 0);
119 arm::pipe::PacketKey key1(0, 0);
120 arm::pipe::PacketKey key2(0, 1);
121 arm::pipe::PacketKey key3(0, 2);
122 arm::pipe::PacketKey key4(1, 0);
123 arm::pipe::PacketKey key5(1, 0);
124 arm::pipe::PacketKey key6(1, 1);
Jim Flynned25e0e2019-10-18 13:21:43 +0100125
Sadik Armagan1625efc2021-06-10 18:24:34 +0100126 CHECK(!(key0 < key1));
127 CHECK(!(key0 > key1));
128 CHECK(key0 <= key1);
129 CHECK(key0 >= key1);
130 CHECK(key0 == key1);
131 CHECK(key0 < key2);
132 CHECK(key2 < key3);
133 CHECK(key3 > key0);
134 CHECK(key4 == key5);
135 CHECK(key4 > key0);
136 CHECK(key5 < key6);
137 CHECK(key5 <= key6);
138 CHECK(key5 != key6);
Jim Flynned25e0e2019-10-18 13:21:43 +0100139}
140
Sadik Armagan1625efc2021-06-10 18:24:34 +0100141TEST_CASE("CheckCommandHandler")
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100142{
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);
Sadik Armagan3896b472020-02-10 12:24:15 +0000151 SendCounterPacket sendCounterPacket(mockBuffer);
152 SendThread sendThread(profilingStateMachine, mockBuffer, sendCounterPacket);
Matteo Martincighcdfb9412019-11-08 11:23:06 +0000153 SendTimelinePacket sendTimelinePacket(mockBuffer);
Jim Flynn6398a982020-05-27 17:05:21 +0100154 MockProfilingServiceStatus mockProfilingServiceStatus;
Matteo Martincighcdfb9412019-11-08 11:23:06 +0000155
Keith Davis3201eea2019-10-24 17:30:41 +0100156 ConnectionAcknowledgedCommandHandler connectionAcknowledgedCommandHandler(0, 1, 4194304, counterDirectory,
Matteo Martincighcdfb9412019-11-08 11:23:06 +0000157 sendCounterPacket, sendTimelinePacket,
Jim Flynn6398a982020-05-27 17:05:21 +0100158 profilingStateMachine,
159 mockProfilingServiceStatus);
Jim Flynnbbfe6032020-07-20 16:57:44 +0100160 arm::pipe::CommandHandlerRegistry commandHandlerRegistry;
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100161
Matteo Martincighc2728f92019-10-07 12:35:21 +0100162 commandHandlerRegistry.RegisterFunctor(&connectionAcknowledgedCommandHandler);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100163
Matteo Martincigh8a837172019-10-04 17:01:07 +0100164 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
165 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100166
Keith Davis3201eea2019-10-24 17:30:41 +0100167 CommandHandler commandHandler0(1, true, commandHandlerRegistry, packetVersionResolver);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100168
Colm Donelan2ba48d22019-11-29 09:10:59 +0000169 // This should start the command handler thread return the connection ack and put the profiling
170 // service into active state.
Matteo Martincigh8a837172019-10-04 17:01:07 +0100171 commandHandler0.Start(testProfilingConnectionBase);
Colm Donelan2ba48d22019-11-29 09:10:59 +0000172 // Try to start the send thread many times, it must only start once
Matteo Martincigh8a837172019-10-04 17:01:07 +0100173 commandHandler0.Start(testProfilingConnectionBase);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100174
Colm Donelan2ba48d22019-11-29 09:10:59 +0000175 // This could take up to 20mSec but we'll check often.
176 for (int i = 0; i < 10; i++)
Matteo Martincigh8a837172019-10-04 17:01:07 +0100177 {
178 if (profilingStateMachine.GetCurrentState() == ProfilingState::Active)
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100179 {
Matteo Martincigh8a837172019-10-04 17:01:07 +0100180 break;
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100181 }
Colm Donelan2ba48d22019-11-29 09:10:59 +0000182 std::this_thread::sleep_for(std::chrono::milliseconds(2));
Matteo Martincigh8a837172019-10-04 17:01:07 +0100183 }
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100184
Sadik Armagan1625efc2021-06-10 18:24:34 +0100185 CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::Active);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100186
Colm Donelan2ba48d22019-11-29 09:10:59 +0000187 // Close the thread again.
188 commandHandler0.Stop();
189
190 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
191 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
192
193 // In this test we'll simulate a timeout without a connection ack packet being received.
194 // Stop after timeout is set so we expect the command handler to stop almost immediately.
195 CommandHandler commandHandler1(1, true, commandHandlerRegistry, packetVersionResolver);
196
197 commandHandler1.Start(testProfilingConnectionTimeOutError);
198 // Wait until we know a timeout exception has been sent at least once.
199 for (int i = 0; i < 10; i++)
200 {
201 if (testProfilingConnectionTimeOutError.ReadCalledCount())
202 {
203 break;
204 }
205 std::this_thread::sleep_for(std::chrono::milliseconds(2));
206 }
Colm Donelan2ba48d22019-11-29 09:10:59 +0000207
208 // The command handler loop should have stopped after the timeout.
Finn Williams09ad6f92019-12-19 17:05:18 +0000209 // wait for the timeout exception to be processed and the loop to break.
210 uint32_t timeout = 50;
211 uint32_t timeSlept = 0;
212 while (commandHandler1.IsRunning())
213 {
214 if (timeSlept >= timeout)
215 {
Sadik Armagan1625efc2021-06-10 18:24:34 +0100216 FAIL("Timeout: The command handler loop did not stop after the timeout");
Finn Williams09ad6f92019-12-19 17:05:18 +0000217 }
218 std::this_thread::sleep_for(std::chrono::milliseconds(1));
219 timeSlept ++;
220 }
Colm Donelan2ba48d22019-11-29 09:10:59 +0000221
222 commandHandler1.Stop();
223 // The state machine should never have received the ack so will still be in WaitingForAck.
Sadik Armagan1625efc2021-06-10 18:24:34 +0100224 CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::WaitingForAck);
Colm Donelan2ba48d22019-11-29 09:10:59 +0000225
Finn Williams09ad6f92019-12-19 17:05:18 +0000226 // Now try sending a bad connection acknowledged packet
227 TestProfilingConnectionBadAckPacket testProfilingConnectionBadAckPacket;
228 commandHandler1.Start(testProfilingConnectionBadAckPacket);
229 commandHandler1.Stop();
230 // This should also not change the state machine
Sadik Armagan1625efc2021-06-10 18:24:34 +0100231 CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::WaitingForAck);
Finn Williams09ad6f92019-12-19 17:05:18 +0000232
Colm Donelan2ba48d22019-11-29 09:10:59 +0000233 // Disable stop after timeout and now commandHandler1 should persist after a timeout
234 commandHandler1.SetStopAfterTimeout(false);
235 // Restart the thread.
236 commandHandler1.Start(testProfilingConnectionTimeOutError);
237
238 // Wait for at the three timeouts and the ack to be sent.
239 for (int i = 0; i < 10; i++)
240 {
241 if (testProfilingConnectionTimeOutError.ReadCalledCount() > 3)
242 {
243 break;
244 }
245 std::this_thread::sleep_for(std::chrono::milliseconds(2));
246 }
247 commandHandler1.Stop();
248
249 // Even after the 3 exceptions the ack packet should have transitioned the command handler to active.
Sadik Armagan1625efc2021-06-10 18:24:34 +0100250 CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::Active);
Colm Donelan2ba48d22019-11-29 09:10:59 +0000251
252 // A command handler that gets exceptions other than timeouts should keep going.
253 CommandHandler commandHandler2(1, false, commandHandlerRegistry, packetVersionResolver);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100254
Matteo Martincigh8a837172019-10-04 17:01:07 +0100255 commandHandler2.Start(testProfilingConnectionArmnnError);
256
Colm Donelan2ba48d22019-11-29 09:10:59 +0000257 // Wait for two exceptions to be thrown.
258 for (int i = 0; i < 10; i++)
259 {
260 if (testProfilingConnectionTimeOutError.ReadCalledCount() >= 2)
261 {
262 break;
263 }
264 std::this_thread::sleep_for(std::chrono::milliseconds(2));
265 }
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100266
Sadik Armagan1625efc2021-06-10 18:24:34 +0100267 CHECK(commandHandler2.IsRunning());
Matteo Martincigh8a837172019-10-04 17:01:07 +0100268 commandHandler2.Stop();
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100269}
270
Sadik Armagan1625efc2021-06-10 18:24:34 +0100271TEST_CASE("CheckEncodeVersion")
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100272{
Jim Flynnbbfe6032020-07-20 16:57:44 +0100273 arm::pipe::Version version1(12);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100274
Sadik Armagan1625efc2021-06-10 18:24:34 +0100275 CHECK(version1.GetMajor() == 0);
276 CHECK(version1.GetMinor() == 0);
277 CHECK(version1.GetPatch() == 12);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100278
Jim Flynnbbfe6032020-07-20 16:57:44 +0100279 arm::pipe::Version version2(4108);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100280
Sadik Armagan1625efc2021-06-10 18:24:34 +0100281 CHECK(version2.GetMajor() == 0);
282 CHECK(version2.GetMinor() == 1);
283 CHECK(version2.GetPatch() == 12);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100284
Jim Flynnbbfe6032020-07-20 16:57:44 +0100285 arm::pipe::Version version3(4198412);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100286
Sadik Armagan1625efc2021-06-10 18:24:34 +0100287 CHECK(version3.GetMajor() == 1);
288 CHECK(version3.GetMinor() == 1);
289 CHECK(version3.GetPatch() == 12);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100290
Jim Flynnbbfe6032020-07-20 16:57:44 +0100291 arm::pipe::Version version4(0);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100292
Sadik Armagan1625efc2021-06-10 18:24:34 +0100293 CHECK(version4.GetMajor() == 0);
294 CHECK(version4.GetMinor() == 0);
295 CHECK(version4.GetPatch() == 0);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100296
Jim Flynnbbfe6032020-07-20 16:57:44 +0100297 arm::pipe::Version version5(1, 0, 0);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100298 CHECK(version5.GetEncodedValue() == 4194304);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100299}
300
Sadik Armagan1625efc2021-06-10 18:24:34 +0100301TEST_CASE("CheckPacketClass")
Nikhil Rajbc626052019-08-15 15:49:45 +0100302{
Keith Davis3201eea2019-10-24 17:30:41 +0100303 uint32_t length = 4;
Matteo Martincigh67ef2a52019-10-10 13:29:02 +0100304 std::unique_ptr<unsigned char[]> packetData0 = std::make_unique<unsigned char[]>(length);
305 std::unique_ptr<unsigned char[]> packetData1 = std::make_unique<unsigned char[]>(0);
306 std::unique_ptr<unsigned char[]> nullPacketData;
Nikhil Rajbc626052019-08-15 15:49:45 +0100307
Jim Flynnbbfe6032020-07-20 16:57:44 +0100308 arm::pipe::Packet packetTest0(472580096, length, packetData0);
Nikhil Rajbc626052019-08-15 15:49:45 +0100309
Sadik Armagan1625efc2021-06-10 18:24:34 +0100310 CHECK(packetTest0.GetHeader() == 472580096);
311 CHECK(packetTest0.GetPacketFamily() == 7);
312 CHECK(packetTest0.GetPacketId() == 43);
313 CHECK(packetTest0.GetLength() == length);
314 CHECK(packetTest0.GetPacketType() == 3);
315 CHECK(packetTest0.GetPacketClass() == 5);
Nikhil Rajbc626052019-08-15 15:49:45 +0100316
Sadik Armagan1625efc2021-06-10 18:24:34 +0100317 CHECK_THROWS_AS(arm::pipe::Packet packetTest1(472580096, 0, packetData1), arm::pipe::InvalidArgumentException);
318 CHECK_NOTHROW(arm::pipe::Packet packetTest2(472580096, 0, nullPacketData));
Nikhil Rajbc626052019-08-15 15:49:45 +0100319
Jim Flynnbbfe6032020-07-20 16:57:44 +0100320 arm::pipe::Packet packetTest3(472580096, 0, nullPacketData);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100321 CHECK(packetTest3.GetLength() == 0);
322 CHECK(packetTest3.GetData() == nullptr);
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100323
Matteo Martincigh67ef2a52019-10-10 13:29:02 +0100324 const unsigned char* packetTest0Data = packetTest0.GetData();
Jim Flynnbbfe6032020-07-20 16:57:44 +0100325 arm::pipe::Packet packetTest4(std::move(packetTest0));
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100326
Sadik Armagan1625efc2021-06-10 18:24:34 +0100327 CHECK(packetTest0.GetData() == nullptr);
328 CHECK(packetTest4.GetData() == packetTest0Data);
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100329
Sadik Armagan1625efc2021-06-10 18:24:34 +0100330 CHECK(packetTest4.GetHeader() == 472580096);
331 CHECK(packetTest4.GetPacketFamily() == 7);
332 CHECK(packetTest4.GetPacketId() == 43);
333 CHECK(packetTest4.GetLength() == length);
334 CHECK(packetTest4.GetPacketType() == 3);
335 CHECK(packetTest4.GetPacketClass() == 5);
Nikhil Rajbc626052019-08-15 15:49:45 +0100336}
337
Sadik Armagan1625efc2021-06-10 18:24:34 +0100338TEST_CASE("CheckCommandHandlerFunctor")
Francis Murtagh11f99b42019-08-16 11:28:52 +0100339{
Francis Murtagh11f99b42019-08-16 11:28:52 +0100340 // Hard code the version as it will be the same during a single profiling session
341 uint32_t version = 1;
342
Jim Flynn397043f2019-10-17 17:37:10 +0100343 TestFunctorA testFunctorA(7, 461, version);
344 TestFunctorB testFunctorB(8, 963, version);
345 TestFunctorC testFunctorC(5, 983, version);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100346
Jim Flynnbbfe6032020-07-20 16:57:44 +0100347 arm::pipe::CommandHandlerKey keyA(
348 testFunctorA.GetFamilyId(), testFunctorA.GetPacketId(), testFunctorA.GetVersion());
349 arm::pipe::CommandHandlerKey keyB(
350 testFunctorB.GetFamilyId(), testFunctorB.GetPacketId(), testFunctorB.GetVersion());
351 arm::pipe::CommandHandlerKey keyC(
352 testFunctorC.GetFamilyId(), testFunctorC.GetPacketId(), testFunctorC.GetVersion());
Francis Murtagh11f99b42019-08-16 11:28:52 +0100353
354 // Create the unwrapped map to simulate the Command Handler Registry
Jim Flynnbbfe6032020-07-20 16:57:44 +0100355 std::map<arm::pipe::CommandHandlerKey, arm::pipe::CommandHandlerFunctor*> registry;
Francis Murtagh11f99b42019-08-16 11:28:52 +0100356
357 registry.insert(std::make_pair(keyB, &testFunctorB));
358 registry.insert(std::make_pair(keyA, &testFunctorA));
359 registry.insert(std::make_pair(keyC, &testFunctorC));
360
361 // Check the order of the map is correct
362 auto it = registry.begin();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100363 CHECK(it->first == keyC); // familyId == 5
Francis Murtagh11f99b42019-08-16 11:28:52 +0100364 it++;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100365 CHECK(it->first == keyA); // familyId == 7
Francis Murtagh11f99b42019-08-16 11:28:52 +0100366 it++;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100367 CHECK(it->first == keyB); // familyId == 8
Francis Murtagh11f99b42019-08-16 11:28:52 +0100368
Matteo Martincigh67ef2a52019-10-10 13:29:02 +0100369 std::unique_ptr<unsigned char[]> packetDataA;
370 std::unique_ptr<unsigned char[]> packetDataB;
371 std::unique_ptr<unsigned char[]> packetDataC;
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100372
Jim Flynnbbfe6032020-07-20 16:57:44 +0100373 arm::pipe::Packet packetA(500000000, 0, packetDataA);
374 arm::pipe::Packet packetB(600000000, 0, packetDataB);
375 arm::pipe::Packet packetC(400000000, 0, packetDataC);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100376
377 // Check the correct operator of derived class is called
Jim Flynnbbfe6032020-07-20 16:57:44 +0100378 registry.at(arm::pipe::CommandHandlerKey(
379 packetA.GetPacketFamily(), packetA.GetPacketId(), version))->operator()(packetA);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100380 CHECK(testFunctorA.GetCount() == 1);
381 CHECK(testFunctorB.GetCount() == 0);
382 CHECK(testFunctorC.GetCount() == 0);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100383
Jim Flynnbbfe6032020-07-20 16:57:44 +0100384 registry.at(arm::pipe::CommandHandlerKey(
385 packetB.GetPacketFamily(), packetB.GetPacketId(), version))->operator()(packetB);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100386 CHECK(testFunctorA.GetCount() == 1);
387 CHECK(testFunctorB.GetCount() == 1);
388 CHECK(testFunctorC.GetCount() == 0);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100389
Jim Flynnbbfe6032020-07-20 16:57:44 +0100390 registry.at(arm::pipe::CommandHandlerKey(
391 packetC.GetPacketFamily(), packetC.GetPacketId(), version))->operator()(packetC);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100392 CHECK(testFunctorA.GetCount() == 1);
393 CHECK(testFunctorB.GetCount() == 1);
394 CHECK(testFunctorC.GetCount() == 1);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100395}
396
Sadik Armagan1625efc2021-06-10 18:24:34 +0100397TEST_CASE("CheckCommandHandlerRegistry")
Francis Murtagh94d79152019-08-16 17:45:07 +0100398{
399 // Hard code the version as it will be the same during a single profiling session
400 uint32_t version = 1;
401
Jim Flynn397043f2019-10-17 17:37:10 +0100402 TestFunctorA testFunctorA(7, 461, version);
403 TestFunctorB testFunctorB(8, 963, version);
404 TestFunctorC testFunctorC(5, 983, version);
Francis Murtagh94d79152019-08-16 17:45:07 +0100405
406 // Create the Command Handler Registry
Jim Flynnbbfe6032020-07-20 16:57:44 +0100407 arm::pipe::CommandHandlerRegistry registry;
Francis Murtagh94d79152019-08-16 17:45:07 +0100408
409 // Register multiple different derived classes
Matteo Martincighc2728f92019-10-07 12:35:21 +0100410 registry.RegisterFunctor(&testFunctorA);
411 registry.RegisterFunctor(&testFunctorB);
412 registry.RegisterFunctor(&testFunctorC);
Francis Murtagh94d79152019-08-16 17:45:07 +0100413
Matteo Martincigh67ef2a52019-10-10 13:29:02 +0100414 std::unique_ptr<unsigned char[]> packetDataA;
415 std::unique_ptr<unsigned char[]> packetDataB;
416 std::unique_ptr<unsigned char[]> packetDataC;
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100417
Jim Flynnbbfe6032020-07-20 16:57:44 +0100418 arm::pipe::Packet packetA(500000000, 0, packetDataA);
419 arm::pipe::Packet packetB(600000000, 0, packetDataB);
420 arm::pipe::Packet packetC(400000000, 0, packetDataC);
Francis Murtagh94d79152019-08-16 17:45:07 +0100421
422 // Check the correct operator of derived class is called
Jim Flynn397043f2019-10-17 17:37:10 +0100423 registry.GetFunctor(packetA.GetPacketFamily(), packetA.GetPacketId(), version)->operator()(packetA);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100424 CHECK(testFunctorA.GetCount() == 1);
425 CHECK(testFunctorB.GetCount() == 0);
426 CHECK(testFunctorC.GetCount() == 0);
Francis Murtagh94d79152019-08-16 17:45:07 +0100427
Jim Flynn397043f2019-10-17 17:37:10 +0100428 registry.GetFunctor(packetB.GetPacketFamily(), packetB.GetPacketId(), version)->operator()(packetB);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100429 CHECK(testFunctorA.GetCount() == 1);
430 CHECK(testFunctorB.GetCount() == 1);
431 CHECK(testFunctorC.GetCount() == 0);
Francis Murtagh94d79152019-08-16 17:45:07 +0100432
Jim Flynn397043f2019-10-17 17:37:10 +0100433 registry.GetFunctor(packetC.GetPacketFamily(), packetC.GetPacketId(), version)->operator()(packetC);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100434 CHECK(testFunctorA.GetCount() == 1);
435 CHECK(testFunctorB.GetCount() == 1);
436 CHECK(testFunctorC.GetCount() == 1);
Francis Murtagh94d79152019-08-16 17:45:07 +0100437
438 // Re-register an existing key with a new function
Jim Flynn397043f2019-10-17 17:37:10 +0100439 registry.RegisterFunctor(&testFunctorC, testFunctorA.GetFamilyId(), testFunctorA.GetPacketId(), version);
440 registry.GetFunctor(packetA.GetPacketFamily(), packetA.GetPacketId(), version)->operator()(packetC);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100441 CHECK(testFunctorA.GetCount() == 1);
442 CHECK(testFunctorB.GetCount() == 1);
443 CHECK(testFunctorC.GetCount() == 2);
Francis Murtagh94d79152019-08-16 17:45:07 +0100444
445 // Check that non-existent key returns nullptr for its functor
Sadik Armagan1625efc2021-06-10 18:24:34 +0100446 CHECK_THROWS_AS(registry.GetFunctor(0, 0, 0), arm::pipe::ProfilingException);
Francis Murtagh94d79152019-08-16 17:45:07 +0100447}
448
Sadik Armagan1625efc2021-06-10 18:24:34 +0100449TEST_CASE("CheckPacketVersionResolver")
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100450{
451 // Set up random number generator for generating packetId values
452 std::random_device device;
453 std::mt19937 generator(device());
454 std::uniform_int_distribution<uint32_t> distribution(std::numeric_limits<uint32_t>::min(),
455 std::numeric_limits<uint32_t>::max());
456
457 // NOTE: Expected version is always 1.0.0, regardless of packetId
Jim Flynnbbfe6032020-07-20 16:57:44 +0100458 const arm::pipe::Version expectedVersion(1, 0, 0);
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100459
Jim Flynnbbfe6032020-07-20 16:57:44 +0100460 arm::pipe::PacketVersionResolver packetVersionResolver;
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100461
462 constexpr unsigned int numTests = 10u;
463
464 for (unsigned int i = 0u; i < numTests; ++i)
465 {
Jim Flynned25e0e2019-10-18 13:21:43 +0100466 const uint32_t familyId = distribution(generator);
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100467 const uint32_t packetId = distribution(generator);
Jim Flynnbbfe6032020-07-20 16:57:44 +0100468 arm::pipe::Version resolvedVersion = packetVersionResolver.ResolvePacketVersion(familyId, packetId);
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100469
Sadik Armagan1625efc2021-06-10 18:24:34 +0100470 CHECK(resolvedVersion == expectedVersion);
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100471 }
472}
Matteo Martincighd0613b52019-10-09 16:47:04 +0100473
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100474void ProfilingCurrentStateThreadImpl(ProfilingStateMachine& states)
475{
476 ProfilingState newState = ProfilingState::NotConnected;
477 states.GetCurrentState();
478 states.TransitionToState(newState);
479}
480
Sadik Armagan1625efc2021-06-10 18:24:34 +0100481TEST_CASE("CheckProfilingStateMachine")
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100482{
483 ProfilingStateMachine profilingState1(ProfilingState::Uninitialised);
484 profilingState1.TransitionToState(ProfilingState::Uninitialised);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100485 CHECK(profilingState1.GetCurrentState() == ProfilingState::Uninitialised);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100486
487 ProfilingStateMachine profilingState2(ProfilingState::Uninitialised);
488 profilingState2.TransitionToState(ProfilingState::NotConnected);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100489 CHECK(profilingState2.GetCurrentState() == ProfilingState::NotConnected);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100490
491 ProfilingStateMachine profilingState3(ProfilingState::NotConnected);
492 profilingState3.TransitionToState(ProfilingState::NotConnected);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100493 CHECK(profilingState3.GetCurrentState() == ProfilingState::NotConnected);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100494
495 ProfilingStateMachine profilingState4(ProfilingState::NotConnected);
496 profilingState4.TransitionToState(ProfilingState::WaitingForAck);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100497 CHECK(profilingState4.GetCurrentState() == ProfilingState::WaitingForAck);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100498
499 ProfilingStateMachine profilingState5(ProfilingState::WaitingForAck);
500 profilingState5.TransitionToState(ProfilingState::WaitingForAck);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100501 CHECK(profilingState5.GetCurrentState() == ProfilingState::WaitingForAck);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100502
503 ProfilingStateMachine profilingState6(ProfilingState::WaitingForAck);
504 profilingState6.TransitionToState(ProfilingState::Active);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100505 CHECK(profilingState6.GetCurrentState() == ProfilingState::Active);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100506
507 ProfilingStateMachine profilingState7(ProfilingState::Active);
508 profilingState7.TransitionToState(ProfilingState::NotConnected);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100509 CHECK(profilingState7.GetCurrentState() == ProfilingState::NotConnected);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100510
511 ProfilingStateMachine profilingState8(ProfilingState::Active);
512 profilingState8.TransitionToState(ProfilingState::Active);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100513 CHECK(profilingState8.GetCurrentState() == ProfilingState::Active);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100514
515 ProfilingStateMachine profilingState9(ProfilingState::Uninitialised);
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000516 CHECK_THROWS_AS(profilingState9.TransitionToState(ProfilingState::WaitingForAck), arm::pipe::ProfilingException);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100517
518 ProfilingStateMachine profilingState10(ProfilingState::Uninitialised);
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000519 CHECK_THROWS_AS(profilingState10.TransitionToState(ProfilingState::Active), arm::pipe::ProfilingException);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100520
521 ProfilingStateMachine profilingState11(ProfilingState::NotConnected);
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000522 CHECK_THROWS_AS(profilingState11.TransitionToState(ProfilingState::Uninitialised), arm::pipe::ProfilingException);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100523
524 ProfilingStateMachine profilingState12(ProfilingState::NotConnected);
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000525 CHECK_THROWS_AS(profilingState12.TransitionToState(ProfilingState::Active), arm::pipe::ProfilingException);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100526
527 ProfilingStateMachine profilingState13(ProfilingState::WaitingForAck);
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000528 CHECK_THROWS_AS(profilingState13.TransitionToState(ProfilingState::Uninitialised), arm::pipe::ProfilingException);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100529
530 ProfilingStateMachine profilingState14(ProfilingState::WaitingForAck);
Jim Flynn53e46992019-10-14 12:31:10 +0100531 profilingState14.TransitionToState(ProfilingState::NotConnected);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100532 CHECK(profilingState14.GetCurrentState() == ProfilingState::NotConnected);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100533
534 ProfilingStateMachine profilingState15(ProfilingState::Active);
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000535 CHECK_THROWS_AS(profilingState15.TransitionToState(ProfilingState::Uninitialised), arm::pipe::ProfilingException);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100536
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000537 ProfilingStateMachine profilingState16(ProfilingState::Active);
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000538 CHECK_THROWS_AS(profilingState16.TransitionToState(ProfilingState::WaitingForAck), arm::pipe::ProfilingException);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100539
540 ProfilingStateMachine profilingState17(ProfilingState::Uninitialised);
541
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100542 std::vector<std::thread> threads;
543 for (unsigned int i = 0; i < 5; ++i)
544 {
545 threads.push_back(std::thread(ProfilingCurrentStateThreadImpl, std::ref(profilingState17)));
546 }
547 std::for_each(threads.begin(), threads.end(), [](std::thread& theThread)
548 {
549 theThread.join();
550 });
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100551
Sadik Armagan1625efc2021-06-10 18:24:34 +0100552 CHECK((profilingState17.GetCurrentState() == ProfilingState::NotConnected));
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100553}
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100554
Jim Flynn8355ec92019-09-17 12:29:50 +0100555void CaptureDataWriteThreadImpl(Holder& holder, uint32_t capturePeriod, const std::vector<uint16_t>& counterIds)
Francis Murtagh68f78d82019-09-04 16:42:29 +0100556{
Finn Williams032bc742020-02-12 11:02:34 +0000557 holder.SetCaptureData(capturePeriod, counterIds, {});
Francis Murtagh68f78d82019-09-04 16:42:29 +0100558}
559
Francis Murtaghbd707162019-09-09 11:26:44 +0100560void CaptureDataReadThreadImpl(const Holder& holder, CaptureData& captureData)
Francis Murtagh68f78d82019-09-04 16:42:29 +0100561{
562 captureData = holder.GetCaptureData();
563}
564
Sadik Armagan1625efc2021-06-10 18:24:34 +0100565TEST_CASE("CheckCaptureDataHolder")
Francis Murtagh68f78d82019-09-04 16:42:29 +0100566{
Francis Murtaghbd707162019-09-09 11:26:44 +0100567 std::map<uint32_t, std::vector<uint16_t>> periodIdMap;
568 std::vector<uint16_t> counterIds;
Jim Flynn8355ec92019-09-17 12:29:50 +0100569 uint32_t numThreads = 10;
570 for (uint32_t i = 0; i < numThreads; ++i)
Francis Murtaghbd707162019-09-09 11:26:44 +0100571 {
572 counterIds.emplace_back(i);
573 periodIdMap.insert(std::make_pair(i, counterIds));
574 }
Francis Murtagh68f78d82019-09-04 16:42:29 +0100575
Jim Flynn8355ec92019-09-17 12:29:50 +0100576 // Verify the read and write threads set the holder correctly
577 // and retrieve the expected values
Francis Murtagh68f78d82019-09-04 16:42:29 +0100578 Holder holder;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100579 CHECK((holder.GetCaptureData()).GetCapturePeriod() == 0);
580 CHECK(((holder.GetCaptureData()).GetCounterIds()).empty());
Francis Murtagh68f78d82019-09-04 16:42:29 +0100581
582 // Check Holder functions
Francis Murtaghbd707162019-09-09 11:26:44 +0100583 std::thread thread1(CaptureDataWriteThreadImpl, std::ref(holder), 2, std::ref(periodIdMap[2]));
Francis Murtagh68f78d82019-09-04 16:42:29 +0100584 thread1.join();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100585 CHECK((holder.GetCaptureData()).GetCapturePeriod() == 2);
586 CHECK((holder.GetCaptureData()).GetCounterIds() == periodIdMap[2]);
Jim Flynn8355ec92019-09-17 12:29:50 +0100587 // NOTE: now that we have some initial values in the holder we don't have to worry
588 // in the multi-threaded section below about a read thread accessing the holder
589 // before any write thread has gotten to it so we read period = 0, counterIds empty
590 // instead of period = 0, counterIds = {0} as will the case when write thread 0
591 // has executed.
Francis Murtagh68f78d82019-09-04 16:42:29 +0100592
593 CaptureData captureData;
594 std::thread thread2(CaptureDataReadThreadImpl, std::ref(holder), std::ref(captureData));
595 thread2.join();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100596 CHECK(captureData.GetCapturePeriod() == 2);
597 CHECK(captureData.GetCounterIds() == periodIdMap[2]);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100598
Jim Flynn8355ec92019-09-17 12:29:50 +0100599 std::map<uint32_t, CaptureData> captureDataIdMap;
600 for (uint32_t i = 0; i < numThreads; ++i)
601 {
602 CaptureData perThreadCaptureData;
603 captureDataIdMap.insert(std::make_pair(i, perThreadCaptureData));
604 }
605
Francis Murtaghbd707162019-09-09 11:26:44 +0100606 std::vector<std::thread> threadsVect;
Jim Flynn8355ec92019-09-17 12:29:50 +0100607 std::vector<std::thread> readThreadsVect;
608 for (uint32_t i = 0; i < numThreads; ++i)
Francis Murtaghbd707162019-09-09 11:26:44 +0100609 {
Keith Davis3201eea2019-10-24 17:30:41 +0100610 threadsVect.emplace_back(
611 std::thread(CaptureDataWriteThreadImpl, std::ref(holder), i, std::ref(periodIdMap[i])));
Francis Murtagh06965692019-09-05 16:29:01 +0100612
Jim Flynn8355ec92019-09-17 12:29:50 +0100613 // Verify that the CaptureData goes into the thread in a virgin state
Sadik Armagan1625efc2021-06-10 18:24:34 +0100614 CHECK(captureDataIdMap.at(i).GetCapturePeriod() == 0);
615 CHECK(captureDataIdMap.at(i).GetCounterIds().empty());
Keith Davis3201eea2019-10-24 17:30:41 +0100616 readThreadsVect.emplace_back(
617 std::thread(CaptureDataReadThreadImpl, std::ref(holder), std::ref(captureDataIdMap.at(i))));
Francis Murtaghbd707162019-09-09 11:26:44 +0100618 }
619
Jim Flynn8355ec92019-09-17 12:29:50 +0100620 for (uint32_t i = 0; i < numThreads; ++i)
Francis Murtaghbd707162019-09-09 11:26:44 +0100621 {
622 threadsVect[i].join();
Francis Murtaghbd707162019-09-09 11:26:44 +0100623 readThreadsVect[i].join();
624 }
Francis Murtagh68f78d82019-09-04 16:42:29 +0100625
Jim Flynn8355ec92019-09-17 12:29:50 +0100626 // Look at the CaptureData that each read thread has filled
627 // the capture period it read should match the counter ids entry
628 for (uint32_t i = 0; i < numThreads; ++i)
629 {
630 CaptureData perThreadCaptureData = captureDataIdMap.at(i);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100631 CHECK(perThreadCaptureData.GetCounterIds() == periodIdMap.at(perThreadCaptureData.GetCapturePeriod()));
Jim Flynn8355ec92019-09-17 12:29:50 +0100632 }
Matthew Bentham46d1c622019-09-13 12:45:04 +0100633}
Francis Murtagh68f78d82019-09-04 16:42:29 +0100634
Sadik Armagan1625efc2021-06-10 18:24:34 +0100635TEST_CASE("CaptureDataMethods")
Matthew Bentham46d1c622019-09-13 12:45:04 +0100636{
Jim Flynn8355ec92019-09-17 12:29:50 +0100637 // Check CaptureData setter and getter functions
Keith Davis3201eea2019-10-24 17:30:41 +0100638 std::vector<uint16_t> counterIds = { 42, 29, 13 };
Jim Flynn8355ec92019-09-17 12:29:50 +0100639 CaptureData captureData;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100640 CHECK(captureData.GetCapturePeriod() == 0);
641 CHECK((captureData.GetCounterIds()).empty());
Jim Flynn8355ec92019-09-17 12:29:50 +0100642 captureData.SetCapturePeriod(150);
643 captureData.SetCounterIds(counterIds);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100644 CHECK(captureData.GetCapturePeriod() == 150);
645 CHECK(captureData.GetCounterIds() == counterIds);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100646
Jim Flynn8355ec92019-09-17 12:29:50 +0100647 // Check assignment operator
Francis Murtagh68f78d82019-09-04 16:42:29 +0100648 CaptureData secondCaptureData;
Francis Murtagh68f78d82019-09-04 16:42:29 +0100649
Jim Flynn8355ec92019-09-17 12:29:50 +0100650 secondCaptureData = captureData;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100651 CHECK(secondCaptureData.GetCapturePeriod() == 150);
652 CHECK(secondCaptureData.GetCounterIds() == counterIds);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100653
654 // Check copy constructor
Jim Flynn8355ec92019-09-17 12:29:50 +0100655 CaptureData copyConstructedCaptureData(captureData);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100656
Sadik Armagan1625efc2021-06-10 18:24:34 +0100657 CHECK(copyConstructedCaptureData.GetCapturePeriod() == 150);
658 CHECK(copyConstructedCaptureData.GetCounterIds() == counterIds);
Keith Davis02356de2019-08-26 18:28:17 +0100659}
Francis Murtagh68f78d82019-09-04 16:42:29 +0100660
Sadik Armagan1625efc2021-06-10 18:24:34 +0100661TEST_CASE("CheckProfilingServiceDisabled")
Keith Davis02356de2019-08-26 18:28:17 +0100662{
Jim Flynn4c9ed1d2022-01-23 23:57:20 +0000663 ProfilingOptions options;
Jim Flynn34430252022-03-04 15:03:58 +0000664 armnn::ArmNNProfilingServiceInitialiser initialiser;
665 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100666 profilingService.ResetExternalProfilingOptions(options, true);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100667 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100668 profilingService.Update();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100669 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis02356de2019-08-26 18:28:17 +0100670}
671
Sadik Armagan1625efc2021-06-10 18:24:34 +0100672TEST_CASE("CheckProfilingServiceCounterDirectory")
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100673{
Jim Flynn4c9ed1d2022-01-23 23:57:20 +0000674 ProfilingOptions options;
Jim Flynn34430252022-03-04 15:03:58 +0000675 armnn::ArmNNProfilingServiceInitialiser initialiser;
676 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100677 profilingService.ResetExternalProfilingOptions(options, true);
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100678
Matteo Martincigha84edee2019-10-02 12:50:57 +0100679 const ICounterDirectory& counterDirectory0 = profilingService.GetCounterDirectory();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100680 CHECK(counterDirectory0.GetCounterCount() == 0);
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100681 profilingService.Update();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100682 CHECK(counterDirectory0.GetCounterCount() == 0);
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100683
684 options.m_EnableProfiling = true;
Matteo Martincigha84edee2019-10-02 12:50:57 +0100685 profilingService.ResetExternalProfilingOptions(options);
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100686
Matteo Martincigha84edee2019-10-02 12:50:57 +0100687 const ICounterDirectory& counterDirectory1 = profilingService.GetCounterDirectory();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100688 CHECK(counterDirectory1.GetCounterCount() == 0);
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100689 profilingService.Update();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100690 CHECK(counterDirectory1.GetCounterCount() != 0);
Colm Donelan2ba48d22019-11-29 09:10:59 +0000691 // Reset the profiling service to stop any running thread
692 options.m_EnableProfiling = false;
693 profilingService.ResetExternalProfilingOptions(options, true);
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100694}
695
Sadik Armagan1625efc2021-06-10 18:24:34 +0100696TEST_CASE("CheckProfilingServiceCounterValues")
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100697{
Jim Flynn4c9ed1d2022-01-23 23:57:20 +0000698 ProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +0100699 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +0000700 armnn::ArmNNProfilingServiceInitialiser initialiser;
701 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Matteo Martincigha84edee2019-10-02 12:50:57 +0100702 profilingService.ResetExternalProfilingOptions(options, true);
703
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100704 profilingService.Update();
Matteo Martincigha84edee2019-10-02 12:50:57 +0100705 const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
Keith Davis3201eea2019-10-24 17:30:41 +0100706 const Counters& counters = counterDirectory.GetCounters();
Sadik Armagan1625efc2021-06-10 18:24:34 +0100707 CHECK(!counters.empty());
Matteo Martincigha84edee2019-10-02 12:50:57 +0100708
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100709 std::vector<std::thread> writers;
710
Sadik Armagan1625efc2021-06-10 18:24:34 +0100711 CHECK(!counters.empty());
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000712 uint16_t inferencesRun = INFERENCES_RUN;
Finn Williamsf3fcf322020-05-11 14:38:02 +0100713
714 // Test GetAbsoluteCounterValue
715 for (int i = 0; i < 4; ++i)
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100716 {
Finn Williamsf3fcf322020-05-11 14:38:02 +0100717 // Increment and decrement the INFERENCES_RUN counter 250 times
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100718 writers.push_back(std::thread([&profilingService, inferencesRun]()
Finn Williamsf3fcf322020-05-11 14:38:02 +0100719 {
720 for (int i = 0; i < 250; ++i)
721 {
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100722 profilingService.IncrementCounterValue(inferencesRun);
Finn Williamsf3fcf322020-05-11 14:38:02 +0100723 }
724 }));
725 // Add 10 to the INFERENCES_RUN counter 200 times
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100726 writers.push_back(std::thread([&profilingService, inferencesRun]()
Finn Williamsf3fcf322020-05-11 14:38:02 +0100727 {
728 for (int i = 0; i < 200; ++i)
729 {
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100730 profilingService.AddCounterValue(inferencesRun, 10);
Finn Williamsf3fcf322020-05-11 14:38:02 +0100731 }
732 }));
733 // Subtract 5 from the INFERENCES_RUN counter 200 times
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100734 writers.push_back(std::thread([&profilingService, inferencesRun]()
Finn Williamsf3fcf322020-05-11 14:38:02 +0100735 {
736 for (int i = 0; i < 200; ++i)
737 {
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100738 profilingService.SubtractCounterValue(inferencesRun, 5);
Finn Williamsf3fcf322020-05-11 14:38:02 +0100739 }
740 }));
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100741 }
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100742 std::for_each(writers.begin(), writers.end(), mem_fn(&std::thread::join));
743
Finn Williamsf3fcf322020-05-11 14:38:02 +0100744 uint32_t absoluteCounterValue = 0;
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100745
Sadik Armagan1625efc2021-06-10 18:24:34 +0100746 CHECK_NOTHROW(absoluteCounterValue = profilingService.GetAbsoluteCounterValue(INFERENCES_RUN));
747 CHECK(absoluteCounterValue == 5000);
Finn Williamsf3fcf322020-05-11 14:38:02 +0100748
749 // Test SetCounterValue
Sadik Armagan1625efc2021-06-10 18:24:34 +0100750 CHECK_NOTHROW(profilingService.SetCounterValue(INFERENCES_RUN, 0));
751 CHECK_NOTHROW(absoluteCounterValue = profilingService.GetAbsoluteCounterValue(INFERENCES_RUN));
752 CHECK(absoluteCounterValue == 0);
Finn Williamsf3fcf322020-05-11 14:38:02 +0100753
754 // Test GetDeltaCounterValue
755 writers.clear();
756 uint32_t deltaCounterValue = 0;
757 //Start a reading thread to randomly read the INFERENCES_RUN counter value
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100758 std::thread reader([&profilingService, inferencesRun](uint32_t& deltaCounterValue)
Finn Williamsf3fcf322020-05-11 14:38:02 +0100759 {
760 for (int i = 0; i < 300; ++i)
761 {
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100762 deltaCounterValue += profilingService.GetDeltaCounterValue(inferencesRun);
Finn Williamsf3fcf322020-05-11 14:38:02 +0100763 }
764 }, std::ref(deltaCounterValue));
765
766 for (int i = 0; i < 4; ++i)
767 {
768 // Increment and decrement the INFERENCES_RUN counter 250 times
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100769 writers.push_back(std::thread([&profilingService, inferencesRun]()
Finn Williamsf3fcf322020-05-11 14:38:02 +0100770 {
771 for (int i = 0; i < 250; ++i)
772 {
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100773 profilingService.IncrementCounterValue(inferencesRun);
Finn Williamsf3fcf322020-05-11 14:38:02 +0100774 }
775 }));
776 // Add 10 to the INFERENCES_RUN counter 200 times
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100777 writers.push_back(std::thread([&profilingService, inferencesRun]()
Finn Williamsf3fcf322020-05-11 14:38:02 +0100778 {
779 for (int i = 0; i < 200; ++i)
780 {
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100781 profilingService.AddCounterValue(inferencesRun, 10);
Finn Williamsf3fcf322020-05-11 14:38:02 +0100782 }
783 }));
784 // Subtract 5 from the INFERENCES_RUN counter 200 times
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100785 writers.push_back(std::thread([&profilingService, inferencesRun]()
Finn Williamsf3fcf322020-05-11 14:38:02 +0100786 {
787 for (int i = 0; i < 200; ++i)
788 {
Sadik Armagan95e9efc2021-08-05 15:01:07 +0100789 profilingService.SubtractCounterValue(inferencesRun, 5);
Finn Williamsf3fcf322020-05-11 14:38:02 +0100790 }
791 }));
792 }
793
794 std::for_each(writers.begin(), writers.end(), mem_fn(&std::thread::join));
795 reader.join();
796
797 // Do one last read in case the reader stopped early
798 deltaCounterValue += profilingService.GetDeltaCounterValue(INFERENCES_RUN);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100799 CHECK(deltaCounterValue == 5000);
Finn Williamsf3fcf322020-05-11 14:38:02 +0100800
Colm Donelan2ba48d22019-11-29 09:10:59 +0000801 // Reset the profiling service to stop any running thread
802 options.m_EnableProfiling = false;
803 profilingService.ResetExternalProfilingOptions(options, true);
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100804}
805
Sadik Armagan1625efc2021-06-10 18:24:34 +0100806TEST_CASE("CheckProfilingObjectUids")
Matteo Martincighab173e92019-09-05 12:02:04 +0100807{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100808 uint16_t uid = 0;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100809 CHECK_NOTHROW(uid = GetNextUid());
810 CHECK(uid >= 1);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100811
812 uint16_t nextUid = 0;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100813 CHECK_NOTHROW(nextUid = GetNextUid());
814 CHECK(nextUid > uid);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100815
816 std::vector<uint16_t> counterUids;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100817 CHECK_NOTHROW(counterUids = GetNextCounterUids(uid,0));
818 CHECK(counterUids.size() == 1);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100819
820 std::vector<uint16_t> nextCounterUids;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100821 CHECK_NOTHROW(nextCounterUids = GetNextCounterUids(nextUid, 2));
822 CHECK(nextCounterUids.size() == 2);
823 CHECK(nextCounterUids[0] > counterUids[0]);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100824
825 std::vector<uint16_t> counterUidsMultiCore;
Francis Murtagh1e5afee2021-05-11 09:37:47 +0100826 uint16_t thirdUid = nextCounterUids[0];
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100827 uint16_t numberOfCores = 13;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100828 CHECK_NOTHROW(counterUidsMultiCore = GetNextCounterUids(thirdUid, numberOfCores));
829 CHECK(counterUidsMultiCore.size() == numberOfCores);
830 CHECK(counterUidsMultiCore.front() >= nextCounterUids[0]);
Keith Davis3201eea2019-10-24 17:30:41 +0100831 for (size_t i = 1; i < numberOfCores; i++)
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100832 {
Sadik Armagan1625efc2021-06-10 18:24:34 +0100833 CHECK(counterUidsMultiCore[i] == counterUidsMultiCore[i - 1] + 1);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100834 }
Sadik Armagan1625efc2021-06-10 18:24:34 +0100835 CHECK(counterUidsMultiCore.back() == counterUidsMultiCore.front() + numberOfCores - 1);
Matteo Martincighab173e92019-09-05 12:02:04 +0100836}
837
Sadik Armagan1625efc2021-06-10 18:24:34 +0100838TEST_CASE("CheckCounterDirectoryRegisterCategory")
Matteo Martincighab173e92019-09-05 12:02:04 +0100839{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100840 CounterDirectory counterDirectory;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100841 CHECK(counterDirectory.GetCategoryCount() == 0);
842 CHECK(counterDirectory.GetDeviceCount() == 0);
843 CHECK(counterDirectory.GetCounterSetCount() == 0);
844 CHECK(counterDirectory.GetCounterCount() == 0);
Matteo Martincighab173e92019-09-05 12:02:04 +0100845
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100846 // Register a category with an invalid name
847 const Category* noCategory = nullptr;
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000848 CHECK_THROWS_AS(noCategory = counterDirectory.RegisterCategory(""), arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100849 CHECK(counterDirectory.GetCategoryCount() == 0);
850 CHECK(!noCategory);
Matteo Martincighab173e92019-09-05 12:02:04 +0100851
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100852 // Register a category with an invalid name
Sadik Armagan1625efc2021-06-10 18:24:34 +0100853 CHECK_THROWS_AS(noCategory = counterDirectory.RegisterCategory("invalid category"),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000854 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100855 CHECK(counterDirectory.GetCategoryCount() == 0);
856 CHECK(!noCategory);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100857
858 // Register a new category
859 const std::string categoryName = "some_category";
Keith Davis3201eea2019-10-24 17:30:41 +0100860 const Category* category = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100861 CHECK_NOTHROW(category = counterDirectory.RegisterCategory(categoryName));
862 CHECK(counterDirectory.GetCategoryCount() == 1);
863 CHECK(category);
864 CHECK(category->m_Name == categoryName);
865 CHECK(category->m_Counters.empty());
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100866
867 // Get the registered category
868 const Category* registeredCategory = counterDirectory.GetCategory(categoryName);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100869 CHECK(counterDirectory.GetCategoryCount() == 1);
870 CHECK(registeredCategory);
871 CHECK(registeredCategory == category);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100872
873 // Try to get a category not registered
874 const Category* notRegisteredCategory = counterDirectory.GetCategory("not_registered_category");
Sadik Armagan1625efc2021-06-10 18:24:34 +0100875 CHECK(counterDirectory.GetCategoryCount() == 1);
876 CHECK(!notRegisteredCategory);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100877
878 // Register a category already registered
879 const Category* anotherCategory = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100880 CHECK_THROWS_AS(anotherCategory = counterDirectory.RegisterCategory(categoryName),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000881 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100882 CHECK(counterDirectory.GetCategoryCount() == 1);
883 CHECK(!anotherCategory);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100884
885 // Register a device for testing
886 const std::string deviceName = "some_device";
Keith Davis3201eea2019-10-24 17:30:41 +0100887 const Device* device = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100888 CHECK_NOTHROW(device = counterDirectory.RegisterDevice(deviceName));
889 CHECK(counterDirectory.GetDeviceCount() == 1);
890 CHECK(device);
891 CHECK(device->m_Uid >= 1);
892 CHECK(device->m_Name == deviceName);
893 CHECK(device->m_Cores == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100894
895 // Register a new category not associated to any device
896 const std::string categoryWoDeviceName = "some_category_without_device";
Keith Davis3201eea2019-10-24 17:30:41 +0100897 const Category* categoryWoDevice = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100898 CHECK_NOTHROW(categoryWoDevice = counterDirectory.RegisterCategory(categoryWoDeviceName));
899 CHECK(counterDirectory.GetCategoryCount() == 2);
900 CHECK(categoryWoDevice);
901 CHECK(categoryWoDevice->m_Name == categoryWoDeviceName);
902 CHECK(categoryWoDevice->m_Counters.empty());
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100903
Sadik Armagan4c998992020-02-25 12:44:44 +0000904 // Register a new category associated to an invalid device name (already exist)
905 const Category* categoryInvalidDeviceName = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100906 CHECK_THROWS_AS(categoryInvalidDeviceName =
Sadik Armagan4c998992020-02-25 12:44:44 +0000907 counterDirectory.RegisterCategory(categoryWoDeviceName),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000908 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100909 CHECK(counterDirectory.GetCategoryCount() == 2);
910 CHECK(!categoryInvalidDeviceName);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100911
912 // Register a new category associated to a valid device
913 const std::string categoryWValidDeviceName = "some_category_with_valid_device";
Keith Davis3201eea2019-10-24 17:30:41 +0100914 const Category* categoryWValidDevice = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100915 CHECK_NOTHROW(categoryWValidDevice =
Sadik Armagan4c998992020-02-25 12:44:44 +0000916 counterDirectory.RegisterCategory(categoryWValidDeviceName));
Sadik Armagan1625efc2021-06-10 18:24:34 +0100917 CHECK(counterDirectory.GetCategoryCount() == 3);
918 CHECK(categoryWValidDevice);
919 CHECK(categoryWValidDevice != category);
920 CHECK(categoryWValidDevice->m_Name == categoryWValidDeviceName);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100921
922 // Register a counter set for testing
923 const std::string counterSetName = "some_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +0100924 const CounterSet* counterSet = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100925 CHECK_NOTHROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
926 CHECK(counterDirectory.GetCounterSetCount() == 1);
927 CHECK(counterSet);
928 CHECK(counterSet->m_Uid >= 1);
929 CHECK(counterSet->m_Name == counterSetName);
930 CHECK(counterSet->m_Count == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100931
932 // Register a new category not associated to any counter set
933 const std::string categoryWoCounterSetName = "some_category_without_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +0100934 const Category* categoryWoCounterSet = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100935 CHECK_NOTHROW(categoryWoCounterSet =
Sadik Armagan4c998992020-02-25 12:44:44 +0000936 counterDirectory.RegisterCategory(categoryWoCounterSetName));
Sadik Armagan1625efc2021-06-10 18:24:34 +0100937 CHECK(counterDirectory.GetCategoryCount() == 4);
938 CHECK(categoryWoCounterSet);
939 CHECK(categoryWoCounterSet->m_Name == categoryWoCounterSetName);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100940
941 // Register a new category associated to a valid counter set
942 const std::string categoryWValidCounterSetName = "some_category_with_valid_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +0100943 const Category* categoryWValidCounterSet = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100944 CHECK_NOTHROW(categoryWValidCounterSet = counterDirectory.RegisterCategory(categoryWValidCounterSetName));
945 CHECK(counterDirectory.GetCategoryCount() == 5);
946 CHECK(categoryWValidCounterSet);
947 CHECK(categoryWValidCounterSet != category);
948 CHECK(categoryWValidCounterSet->m_Name == categoryWValidCounterSetName);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100949
950 // Register a new category associated to a valid device and counter set
951 const std::string categoryWValidDeviceAndValidCounterSetName = "some_category_with_valid_device_and_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +0100952 const Category* categoryWValidDeviceAndValidCounterSet = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100953 CHECK_NOTHROW(categoryWValidDeviceAndValidCounterSet = counterDirectory.RegisterCategory(
Sadik Armagan4c998992020-02-25 12:44:44 +0000954 categoryWValidDeviceAndValidCounterSetName));
Sadik Armagan1625efc2021-06-10 18:24:34 +0100955 CHECK(counterDirectory.GetCategoryCount() == 6);
956 CHECK(categoryWValidDeviceAndValidCounterSet);
957 CHECK(categoryWValidDeviceAndValidCounterSet != category);
958 CHECK(categoryWValidDeviceAndValidCounterSet->m_Name == categoryWValidDeviceAndValidCounterSetName);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100959}
960
Sadik Armagan1625efc2021-06-10 18:24:34 +0100961TEST_CASE("CheckCounterDirectoryRegisterDevice")
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100962{
963 CounterDirectory counterDirectory;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100964 CHECK(counterDirectory.GetCategoryCount() == 0);
965 CHECK(counterDirectory.GetDeviceCount() == 0);
966 CHECK(counterDirectory.GetCounterSetCount() == 0);
967 CHECK(counterDirectory.GetCounterCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100968
969 // Register a device with an invalid name
970 const Device* noDevice = nullptr;
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000971 CHECK_THROWS_AS(noDevice = counterDirectory.RegisterDevice(""), arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100972 CHECK(counterDirectory.GetDeviceCount() == 0);
973 CHECK(!noDevice);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100974
975 // Register a device with an invalid name
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000976 CHECK_THROWS_AS(noDevice = counterDirectory.RegisterDevice("inv@lid nam€"), arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100977 CHECK(counterDirectory.GetDeviceCount() == 0);
978 CHECK(!noDevice);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100979
980 // Register a new device with no cores or parent category
981 const std::string deviceName = "some_device";
Keith Davis3201eea2019-10-24 17:30:41 +0100982 const Device* device = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100983 CHECK_NOTHROW(device = counterDirectory.RegisterDevice(deviceName));
984 CHECK(counterDirectory.GetDeviceCount() == 1);
985 CHECK(device);
986 CHECK(device->m_Name == deviceName);
987 CHECK(device->m_Uid >= 1);
988 CHECK(device->m_Cores == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100989
Matteo Martincigh657ab2d2019-09-18 10:53:24 +0100990 // Try getting an unregistered device
991 const Device* unregisteredDevice = counterDirectory.GetDevice(9999);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100992 CHECK(!unregisteredDevice);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +0100993
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100994 // Get the registered device
995 const Device* registeredDevice = counterDirectory.GetDevice(device->m_Uid);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100996 CHECK(counterDirectory.GetDeviceCount() == 1);
997 CHECK(registeredDevice);
998 CHECK(registeredDevice == device);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100999
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001000 // Register a device with the name of a device already registered
1001 const Device* deviceSameName = nullptr;
Jim Flynnf9db3ef2022-03-08 21:23:44 +00001002 CHECK_THROWS_AS(deviceSameName = counterDirectory.RegisterDevice(deviceName),
1003 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001004 CHECK(counterDirectory.GetDeviceCount() == 1);
1005 CHECK(!deviceSameName);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001006
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001007 // Register a new device with cores and no parent category
1008 const std::string deviceWCoresName = "some_device_with_cores";
Keith Davis3201eea2019-10-24 17:30:41 +01001009 const Device* deviceWCores = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001010 CHECK_NOTHROW(deviceWCores = counterDirectory.RegisterDevice(deviceWCoresName, 2));
1011 CHECK(counterDirectory.GetDeviceCount() == 2);
1012 CHECK(deviceWCores);
1013 CHECK(deviceWCores->m_Name == deviceWCoresName);
1014 CHECK(deviceWCores->m_Uid >= 1);
1015 CHECK(deviceWCores->m_Uid > device->m_Uid);
1016 CHECK(deviceWCores->m_Cores == 2);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001017
1018 // Get the registered device
1019 const Device* registeredDeviceWCores = counterDirectory.GetDevice(deviceWCores->m_Uid);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001020 CHECK(counterDirectory.GetDeviceCount() == 2);
1021 CHECK(registeredDeviceWCores);
1022 CHECK(registeredDeviceWCores == deviceWCores);
1023 CHECK(registeredDeviceWCores != device);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001024
1025 // Register a new device with cores and invalid parent category
1026 const std::string deviceWCoresWInvalidParentCategoryName = "some_device_with_cores_with_invalid_parent_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001027 const Device* deviceWCoresWInvalidParentCategory = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001028 CHECK_THROWS_AS(deviceWCoresWInvalidParentCategory =
Keith Davis3201eea2019-10-24 17:30:41 +01001029 counterDirectory.RegisterDevice(deviceWCoresWInvalidParentCategoryName, 3, std::string("")),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001030 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001031 CHECK(counterDirectory.GetDeviceCount() == 2);
1032 CHECK(!deviceWCoresWInvalidParentCategory);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001033
1034 // Register a new device with cores and invalid parent category
1035 const std::string deviceWCoresWInvalidParentCategoryName2 = "some_device_with_cores_with_invalid_parent_category2";
Keith Davis3201eea2019-10-24 17:30:41 +01001036 const Device* deviceWCoresWInvalidParentCategory2 = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001037 CHECK_THROWS_AS(deviceWCoresWInvalidParentCategory2 = counterDirectory.RegisterDevice(
Keith Davis3201eea2019-10-24 17:30:41 +01001038 deviceWCoresWInvalidParentCategoryName2, 3, std::string("invalid_parent_category")),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001039 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001040 CHECK(counterDirectory.GetDeviceCount() == 2);
1041 CHECK(!deviceWCoresWInvalidParentCategory2);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001042
1043 // Register a category for testing
1044 const std::string categoryName = "some_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001045 const Category* category = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001046 CHECK_NOTHROW(category = counterDirectory.RegisterCategory(categoryName));
1047 CHECK(counterDirectory.GetCategoryCount() == 1);
1048 CHECK(category);
1049 CHECK(category->m_Name == categoryName);
1050 CHECK(category->m_Counters.empty());
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001051
1052 // Register a new device with cores and valid parent category
1053 const std::string deviceWCoresWValidParentCategoryName = "some_device_with_cores_with_valid_parent_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001054 const Device* deviceWCoresWValidParentCategory = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001055 CHECK_NOTHROW(deviceWCoresWValidParentCategory =
Keith Davis3201eea2019-10-24 17:30:41 +01001056 counterDirectory.RegisterDevice(deviceWCoresWValidParentCategoryName, 4, categoryName));
Sadik Armagan1625efc2021-06-10 18:24:34 +01001057 CHECK(counterDirectory.GetDeviceCount() == 3);
1058 CHECK(deviceWCoresWValidParentCategory);
1059 CHECK(deviceWCoresWValidParentCategory->m_Name == deviceWCoresWValidParentCategoryName);
1060 CHECK(deviceWCoresWValidParentCategory->m_Uid >= 1);
1061 CHECK(deviceWCoresWValidParentCategory->m_Uid > device->m_Uid);
1062 CHECK(deviceWCoresWValidParentCategory->m_Uid > deviceWCores->m_Uid);
1063 CHECK(deviceWCoresWValidParentCategory->m_Cores == 4);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001064}
1065
Sadik Armagan1625efc2021-06-10 18:24:34 +01001066TEST_CASE("CheckCounterDirectoryRegisterCounterSet")
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001067{
1068 CounterDirectory counterDirectory;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001069 CHECK(counterDirectory.GetCategoryCount() == 0);
1070 CHECK(counterDirectory.GetDeviceCount() == 0);
1071 CHECK(counterDirectory.GetCounterSetCount() == 0);
1072 CHECK(counterDirectory.GetCounterCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001073
1074 // Register a counter set with an invalid name
1075 const CounterSet* noCounterSet = nullptr;
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001076 CHECK_THROWS_AS(noCounterSet = counterDirectory.RegisterCounterSet(""), arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001077 CHECK(counterDirectory.GetCounterSetCount() == 0);
1078 CHECK(!noCounterSet);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001079
1080 // Register a counter set with an invalid name
Sadik Armagan1625efc2021-06-10 18:24:34 +01001081 CHECK_THROWS_AS(noCounterSet = counterDirectory.RegisterCounterSet("invalid name"),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001082 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001083 CHECK(counterDirectory.GetCounterSetCount() == 0);
1084 CHECK(!noCounterSet);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001085
1086 // Register a new counter set with no count or parent category
1087 const std::string counterSetName = "some_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +01001088 const CounterSet* counterSet = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001089 CHECK_NOTHROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
1090 CHECK(counterDirectory.GetCounterSetCount() == 1);
1091 CHECK(counterSet);
1092 CHECK(counterSet->m_Name == counterSetName);
1093 CHECK(counterSet->m_Uid >= 1);
1094 CHECK(counterSet->m_Count == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001095
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001096 // Try getting an unregistered counter set
1097 const CounterSet* unregisteredCounterSet = counterDirectory.GetCounterSet(9999);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001098 CHECK(!unregisteredCounterSet);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001099
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001100 // Get the registered counter set
1101 const CounterSet* registeredCounterSet = counterDirectory.GetCounterSet(counterSet->m_Uid);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001102 CHECK(counterDirectory.GetCounterSetCount() == 1);
1103 CHECK(registeredCounterSet);
1104 CHECK(registeredCounterSet == counterSet);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001105
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001106 // Register a counter set with the name of a counter set already registered
1107 const CounterSet* counterSetSameName = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001108 CHECK_THROWS_AS(counterSetSameName = counterDirectory.RegisterCounterSet(counterSetName),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001109 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001110 CHECK(counterDirectory.GetCounterSetCount() == 1);
1111 CHECK(!counterSetSameName);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001112
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001113 // Register a new counter set with count and no parent category
1114 const std::string counterSetWCountName = "some_counter_set_with_count";
Keith Davis3201eea2019-10-24 17:30:41 +01001115 const CounterSet* counterSetWCount = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001116 CHECK_NOTHROW(counterSetWCount = counterDirectory.RegisterCounterSet(counterSetWCountName, 37));
1117 CHECK(counterDirectory.GetCounterSetCount() == 2);
1118 CHECK(counterSetWCount);
1119 CHECK(counterSetWCount->m_Name == counterSetWCountName);
1120 CHECK(counterSetWCount->m_Uid >= 1);
1121 CHECK(counterSetWCount->m_Uid > counterSet->m_Uid);
1122 CHECK(counterSetWCount->m_Count == 37);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001123
1124 // Get the registered counter set
1125 const CounterSet* registeredCounterSetWCount = counterDirectory.GetCounterSet(counterSetWCount->m_Uid);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001126 CHECK(counterDirectory.GetCounterSetCount() == 2);
1127 CHECK(registeredCounterSetWCount);
1128 CHECK(registeredCounterSetWCount == counterSetWCount);
1129 CHECK(registeredCounterSetWCount != counterSet);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001130
1131 // Register a new counter set with count and invalid parent category
1132 const std::string counterSetWCountWInvalidParentCategoryName = "some_counter_set_with_count_"
1133 "with_invalid_parent_category";
1134 const CounterSet* counterSetWCountWInvalidParentCategory = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001135 CHECK_THROWS_AS(counterSetWCountWInvalidParentCategory = counterDirectory.RegisterCounterSet(
Keith Davis3201eea2019-10-24 17:30:41 +01001136 counterSetWCountWInvalidParentCategoryName, 42, std::string("")),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001137 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001138 CHECK(counterDirectory.GetCounterSetCount() == 2);
1139 CHECK(!counterSetWCountWInvalidParentCategory);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001140
1141 // Register a new counter set with count and invalid parent category
1142 const std::string counterSetWCountWInvalidParentCategoryName2 = "some_counter_set_with_count_"
1143 "with_invalid_parent_category2";
1144 const CounterSet* counterSetWCountWInvalidParentCategory2 = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001145 CHECK_THROWS_AS(counterSetWCountWInvalidParentCategory2 = counterDirectory.RegisterCounterSet(
Keith Davis3201eea2019-10-24 17:30:41 +01001146 counterSetWCountWInvalidParentCategoryName2, 42, std::string("invalid_parent_category")),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001147 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001148 CHECK(counterDirectory.GetCounterSetCount() == 2);
1149 CHECK(!counterSetWCountWInvalidParentCategory2);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001150
1151 // Register a category for testing
1152 const std::string categoryName = "some_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001153 const Category* category = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001154 CHECK_NOTHROW(category = counterDirectory.RegisterCategory(categoryName));
1155 CHECK(counterDirectory.GetCategoryCount() == 1);
1156 CHECK(category);
1157 CHECK(category->m_Name == categoryName);
1158 CHECK(category->m_Counters.empty());
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001159
1160 // Register a new counter set with count and valid parent category
1161 const std::string counterSetWCountWValidParentCategoryName = "some_counter_set_with_count_"
1162 "with_valid_parent_category";
1163 const CounterSet* counterSetWCountWValidParentCategory = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001164 CHECK_NOTHROW(counterSetWCountWValidParentCategory = counterDirectory.RegisterCounterSet(
Keith Davis3201eea2019-10-24 17:30:41 +01001165 counterSetWCountWValidParentCategoryName, 42, categoryName));
Sadik Armagan1625efc2021-06-10 18:24:34 +01001166 CHECK(counterDirectory.GetCounterSetCount() == 3);
1167 CHECK(counterSetWCountWValidParentCategory);
1168 CHECK(counterSetWCountWValidParentCategory->m_Name == counterSetWCountWValidParentCategoryName);
1169 CHECK(counterSetWCountWValidParentCategory->m_Uid >= 1);
1170 CHECK(counterSetWCountWValidParentCategory->m_Uid > counterSet->m_Uid);
1171 CHECK(counterSetWCountWValidParentCategory->m_Uid > counterSetWCount->m_Uid);
1172 CHECK(counterSetWCountWValidParentCategory->m_Count == 42);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001173
Sadik Armagan4c998992020-02-25 12:44:44 +00001174 // Register a counter set associated to a category with invalid name
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001175 const std::string counterSetSameCategoryName = "some_counter_set_with_invalid_parent_category";
Sadik Armagan4c998992020-02-25 12:44:44 +00001176 const std::string invalidCategoryName = "";
Keith Davis3201eea2019-10-24 17:30:41 +01001177 const CounterSet* counterSetSameCategory = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001178 CHECK_THROWS_AS(counterSetSameCategory =
Sadik Armagan4c998992020-02-25 12:44:44 +00001179 counterDirectory.RegisterCounterSet(counterSetSameCategoryName, 0, invalidCategoryName),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001180 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001181 CHECK(counterDirectory.GetCounterSetCount() == 3);
1182 CHECK(!counterSetSameCategory);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001183}
1184
Sadik Armagan1625efc2021-06-10 18:24:34 +01001185TEST_CASE("CheckCounterDirectoryRegisterCounter")
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001186{
1187 CounterDirectory counterDirectory;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001188 CHECK(counterDirectory.GetCategoryCount() == 0);
1189 CHECK(counterDirectory.GetDeviceCount() == 0);
1190 CHECK(counterDirectory.GetCounterSetCount() == 0);
1191 CHECK(counterDirectory.GetCounterCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001192
1193 // Register a counter with an invalid parent category name
1194 const Counter* noCounter = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001195 CHECK_THROWS_AS(noCounter =
Keith Davise394bd92019-12-02 15:12:19 +00001196 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1197 0,
1198 "",
1199 0,
1200 1,
1201 123.45f,
1202 "valid ",
1203 "name"),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001204 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001205 CHECK(counterDirectory.GetCounterCount() == 0);
1206 CHECK(!noCounter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001207
1208 // Register a counter with an invalid parent category name
Sadik Armagan1625efc2021-06-10 18:24:34 +01001209 CHECK_THROWS_AS(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001210 1,
1211 "invalid parent category",
1212 0,
1213 1,
1214 123.45f,
1215 "valid name",
1216 "valid description"),
1217 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001218 CHECK(counterDirectory.GetCounterCount() == 0);
1219 CHECK(!noCounter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001220
1221 // Register a counter with an invalid class
Sadik Armagan1625efc2021-06-10 18:24:34 +01001222 CHECK_THROWS_AS(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001223 2,
1224 "valid_parent_category",
1225 2,
1226 1,
1227 123.45f,
1228 "valid "
1229 "name",
1230 "valid description"),
1231 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001232 CHECK(counterDirectory.GetCounterCount() == 0);
1233 CHECK(!noCounter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001234
1235 // Register a counter with an invalid interpolation
Sadik Armagan1625efc2021-06-10 18:24:34 +01001236 CHECK_THROWS_AS(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001237 4,
1238 "valid_parent_category",
1239 0,
1240 3,
1241 123.45f,
1242 "valid "
1243 "name",
1244 "valid description"),
1245 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001246 CHECK(counterDirectory.GetCounterCount() == 0);
1247 CHECK(!noCounter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001248
1249 // Register a counter with an invalid multiplier
Sadik Armagan1625efc2021-06-10 18:24:34 +01001250 CHECK_THROWS_AS(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001251 5,
1252 "valid_parent_category",
1253 0,
1254 1,
1255 .0f,
1256 "valid "
1257 "name",
1258 "valid description"),
1259 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001260 CHECK(counterDirectory.GetCounterCount() == 0);
1261 CHECK(!noCounter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001262
1263 // Register a counter with an invalid name
Sadik Armagan1625efc2021-06-10 18:24:34 +01001264 CHECK_THROWS_AS(
Keith Davise394bd92019-12-02 15:12:19 +00001265 noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1266 6,
1267 "valid_parent_category",
1268 0,
1269 1,
1270 123.45f,
1271 "",
1272 "valid description"),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001273 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001274 CHECK(counterDirectory.GetCounterCount() == 0);
1275 CHECK(!noCounter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001276
1277 // Register a counter with an invalid name
Sadik Armagan1625efc2021-06-10 18:24:34 +01001278 CHECK_THROWS_AS(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001279 7,
1280 "valid_parent_category",
1281 0,
1282 1,
1283 123.45f,
1284 "invalid nam€",
1285 "valid description"),
1286 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001287 CHECK(counterDirectory.GetCounterCount() == 0);
1288 CHECK(!noCounter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001289
1290 // Register a counter with an invalid description
Sadik Armagan1625efc2021-06-10 18:24:34 +01001291 CHECK_THROWS_AS(noCounter =
Keith Davise394bd92019-12-02 15:12:19 +00001292 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1293 8,
1294 "valid_parent_category",
1295 0,
1296 1,
1297 123.45f,
1298 "valid name",
1299 ""),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001300 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001301 CHECK(counterDirectory.GetCounterCount() == 0);
1302 CHECK(!noCounter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001303
1304 // Register a counter with an invalid description
Sadik Armagan1625efc2021-06-10 18:24:34 +01001305 CHECK_THROWS_AS(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001306 9,
1307 "valid_parent_category",
1308 0,
1309 1,
1310 123.45f,
1311 "valid "
1312 "name",
1313 "inv@lid description"),
1314 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001315 CHECK(counterDirectory.GetCounterCount() == 0);
1316 CHECK(!noCounter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001317
1318 // Register a counter with an invalid unit2
Sadik Armagan1625efc2021-06-10 18:24:34 +01001319 CHECK_THROWS_AS(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001320 10,
1321 "valid_parent_category",
1322 0,
1323 1,
1324 123.45f,
1325 "valid name",
1326 "valid description",
1327 std::string("Mb/s2")),
1328 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001329 CHECK(counterDirectory.GetCounterCount() == 0);
1330 CHECK(!noCounter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001331
1332 // Register a counter with a non-existing parent category name
Sadik Armagan1625efc2021-06-10 18:24:34 +01001333 CHECK_THROWS_AS(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001334 11,
1335 "invalid_parent_category",
1336 0,
1337 1,
1338 123.45f,
1339 "valid name",
1340 "valid description"),
1341 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001342 CHECK(counterDirectory.GetCounterCount() == 0);
1343 CHECK(!noCounter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001344
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001345 // Try getting an unregistered counter
1346 const Counter* unregisteredCounter = counterDirectory.GetCounter(9999);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001347 CHECK(!unregisteredCounter);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001348
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001349 // Register a category for testing
1350 const std::string categoryName = "some_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001351 const Category* category = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001352 CHECK_NOTHROW(category = counterDirectory.RegisterCategory(categoryName));
1353 CHECK(counterDirectory.GetCategoryCount() == 1);
1354 CHECK(category);
1355 CHECK(category->m_Name == categoryName);
1356 CHECK(category->m_Counters.empty());
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001357
1358 // Register a counter with a valid parent category name
1359 const Counter* counter = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001360 CHECK_NOTHROW(
Keith Davise394bd92019-12-02 15:12:19 +00001361 counter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1362 12,
1363 categoryName,
1364 0,
1365 1,
1366 123.45f,
1367 "valid name",
1368 "valid description"));
Sadik Armagan1625efc2021-06-10 18:24:34 +01001369 CHECK(counterDirectory.GetCounterCount() == 1);
1370 CHECK(counter);
1371 CHECK(counter->m_MaxCounterUid == counter->m_Uid);
1372 CHECK(counter->m_Class == 0);
1373 CHECK(counter->m_Interpolation == 1);
1374 CHECK(counter->m_Multiplier == 123.45f);
1375 CHECK(counter->m_Name == "valid name");
1376 CHECK(counter->m_Description == "valid description");
1377 CHECK(counter->m_Units == "");
1378 CHECK(counter->m_DeviceUid == 0);
1379 CHECK(counter->m_CounterSetUid == 0);
1380 CHECK(category->m_Counters.size() == 1);
1381 CHECK(category->m_Counters.back() == counter->m_Uid);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001382
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001383 // Register a counter with a name of a counter already registered for the given parent category name
1384 const Counter* counterSameName = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001385 CHECK_THROWS_AS(counterSameName =
Keith Davise394bd92019-12-02 15:12:19 +00001386 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1387 13,
1388 categoryName,
1389 0,
1390 0,
1391 1.0f,
1392 "valid name",
1393 "valid description",
1394 std::string("description")),
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001395 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001396 CHECK(counterDirectory.GetCounterCount() == 1);
1397 CHECK(!counterSameName);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001398
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001399 // Register a counter with a valid parent category name and units
1400 const Counter* counterWUnits = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001401 CHECK_NOTHROW(counterWUnits = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001402 14,
1403 categoryName,
1404 0,
1405 1,
1406 123.45f,
1407 "valid name 2",
1408 "valid description",
1409 std::string("Mnnsq2"))); // Units
Sadik Armagan1625efc2021-06-10 18:24:34 +01001410 CHECK(counterDirectory.GetCounterCount() == 2);
1411 CHECK(counterWUnits);
1412 CHECK(counterWUnits->m_Uid > counter->m_Uid);
1413 CHECK(counterWUnits->m_MaxCounterUid == counterWUnits->m_Uid);
1414 CHECK(counterWUnits->m_Class == 0);
1415 CHECK(counterWUnits->m_Interpolation == 1);
1416 CHECK(counterWUnits->m_Multiplier == 123.45f);
1417 CHECK(counterWUnits->m_Name == "valid name 2");
1418 CHECK(counterWUnits->m_Description == "valid description");
1419 CHECK(counterWUnits->m_Units == "Mnnsq2");
1420 CHECK(counterWUnits->m_DeviceUid == 0);
1421 CHECK(counterWUnits->m_CounterSetUid == 0);
1422 CHECK(category->m_Counters.size() == 2);
1423 CHECK(category->m_Counters.back() == counterWUnits->m_Uid);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001424
1425 // Register a counter with a valid parent category name and not associated with a device
1426 const Counter* counterWoDevice = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001427 CHECK_NOTHROW(counterWoDevice = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001428 26,
1429 categoryName,
1430 0,
1431 1,
1432 123.45f,
1433 "valid name 3",
1434 "valid description",
1435 armnn::EmptyOptional(),// Units
1436 armnn::EmptyOptional(),// Number of cores
1437 0)); // Device UID
Sadik Armagan1625efc2021-06-10 18:24:34 +01001438 CHECK(counterDirectory.GetCounterCount() == 3);
1439 CHECK(counterWoDevice);
1440 CHECK(counterWoDevice->m_Uid > counter->m_Uid);
1441 CHECK(counterWoDevice->m_MaxCounterUid == counterWoDevice->m_Uid);
1442 CHECK(counterWoDevice->m_Class == 0);
1443 CHECK(counterWoDevice->m_Interpolation == 1);
1444 CHECK(counterWoDevice->m_Multiplier == 123.45f);
1445 CHECK(counterWoDevice->m_Name == "valid name 3");
1446 CHECK(counterWoDevice->m_Description == "valid description");
1447 CHECK(counterWoDevice->m_Units == "");
1448 CHECK(counterWoDevice->m_DeviceUid == 0);
1449 CHECK(counterWoDevice->m_CounterSetUid == 0);
1450 CHECK(category->m_Counters.size() == 3);
1451 CHECK(category->m_Counters.back() == counterWoDevice->m_Uid);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001452
1453 // Register a counter with a valid parent category name and associated to an invalid device
Sadik Armagan1625efc2021-06-10 18:24:34 +01001454 CHECK_THROWS_AS(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001455 15,
1456 categoryName,
1457 0,
1458 1,
1459 123.45f,
1460 "valid name 4",
1461 "valid description",
1462 armnn::EmptyOptional(), // Units
1463 armnn::EmptyOptional(), // Number of cores
1464 100), // Device UID
1465 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001466 CHECK(counterDirectory.GetCounterCount() == 3);
1467 CHECK(!noCounter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001468
1469 // Register a device for testing
1470 const std::string deviceName = "some_device";
Keith Davis3201eea2019-10-24 17:30:41 +01001471 const Device* device = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001472 CHECK_NOTHROW(device = counterDirectory.RegisterDevice(deviceName));
1473 CHECK(counterDirectory.GetDeviceCount() == 1);
1474 CHECK(device);
1475 CHECK(device->m_Name == deviceName);
1476 CHECK(device->m_Uid >= 1);
1477 CHECK(device->m_Cores == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001478
1479 // Register a counter with a valid parent category name and associated to a device
1480 const Counter* counterWDevice = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001481 CHECK_NOTHROW(counterWDevice = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001482 16,
1483 categoryName,
1484 0,
1485 1,
1486 123.45f,
1487 "valid name 5",
1488 std::string("valid description"),
1489 armnn::EmptyOptional(), // Units
1490 armnn::EmptyOptional(), // Number of cores
1491 device->m_Uid)); // Device UID
Sadik Armagan1625efc2021-06-10 18:24:34 +01001492 CHECK(counterDirectory.GetCounterCount() == 4);
1493 CHECK(counterWDevice);
1494 CHECK(counterWDevice->m_Uid > counter->m_Uid);
1495 CHECK(counterWDevice->m_MaxCounterUid == counterWDevice->m_Uid);
1496 CHECK(counterWDevice->m_Class == 0);
1497 CHECK(counterWDevice->m_Interpolation == 1);
1498 CHECK(counterWDevice->m_Multiplier == 123.45f);
1499 CHECK(counterWDevice->m_Name == "valid name 5");
1500 CHECK(counterWDevice->m_Description == "valid description");
1501 CHECK(counterWDevice->m_Units == "");
1502 CHECK(counterWDevice->m_DeviceUid == device->m_Uid);
1503 CHECK(counterWDevice->m_CounterSetUid == 0);
1504 CHECK(category->m_Counters.size() == 4);
1505 CHECK(category->m_Counters.back() == counterWDevice->m_Uid);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001506
1507 // Register a counter with a valid parent category name and not associated with a counter set
1508 const Counter* counterWoCounterSet = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001509 CHECK_NOTHROW(counterWoCounterSet = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001510 17,
1511 categoryName,
1512 0,
1513 1,
1514 123.45f,
1515 "valid name 6",
1516 "valid description",
1517 armnn::EmptyOptional(),// Units
1518 armnn::EmptyOptional(),// No of cores
1519 armnn::EmptyOptional(),// Device UID
1520 0)); // CounterSet UID
Sadik Armagan1625efc2021-06-10 18:24:34 +01001521 CHECK(counterDirectory.GetCounterCount() == 5);
1522 CHECK(counterWoCounterSet);
1523 CHECK(counterWoCounterSet->m_Uid > counter->m_Uid);
1524 CHECK(counterWoCounterSet->m_MaxCounterUid == counterWoCounterSet->m_Uid);
1525 CHECK(counterWoCounterSet->m_Class == 0);
1526 CHECK(counterWoCounterSet->m_Interpolation == 1);
1527 CHECK(counterWoCounterSet->m_Multiplier == 123.45f);
1528 CHECK(counterWoCounterSet->m_Name == "valid name 6");
1529 CHECK(counterWoCounterSet->m_Description == "valid description");
1530 CHECK(counterWoCounterSet->m_Units == "");
1531 CHECK(counterWoCounterSet->m_DeviceUid == 0);
1532 CHECK(counterWoCounterSet->m_CounterSetUid == 0);
1533 CHECK(category->m_Counters.size() == 5);
1534 CHECK(category->m_Counters.back() == counterWoCounterSet->m_Uid);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001535
1536 // Register a counter with a valid parent category name and associated to an invalid counter set
Sadik Armagan1625efc2021-06-10 18:24:34 +01001537 CHECK_THROWS_AS(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001538 18,
1539 categoryName,
1540 0,
1541 1,
1542 123.45f,
1543 "valid ",
1544 "name 7",
1545 std::string("valid description"),
1546 armnn::EmptyOptional(), // Units
1547 armnn::EmptyOptional(), // Number of cores
1548 100), // Counter set UID
1549 arm::pipe::InvalidArgumentException);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001550 CHECK(counterDirectory.GetCounterCount() == 5);
1551 CHECK(!noCounter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001552
1553 // Register a counter with a valid parent category name and with a given number of cores
1554 const Counter* counterWNumberOfCores = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001555 uint16_t numberOfCores = 15;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001556 CHECK_NOTHROW(counterWNumberOfCores = counterDirectory.RegisterCounter(
Keith Davise394bd92019-12-02 15:12:19 +00001557 armnn::profiling::BACKEND_ID, 50,
Keith Davis3201eea2019-10-24 17:30:41 +01001558 categoryName, 0, 1, 123.45f, "valid name 8", "valid description",
1559 armnn::EmptyOptional(), // Units
1560 numberOfCores, // Number of cores
1561 armnn::EmptyOptional(), // Device UID
1562 armnn::EmptyOptional())); // Counter set UID
Sadik Armagan1625efc2021-06-10 18:24:34 +01001563 CHECK(counterDirectory.GetCounterCount() == 20);
1564 CHECK(counterWNumberOfCores);
1565 CHECK(counterWNumberOfCores->m_Uid > counter->m_Uid);
1566 CHECK(counterWNumberOfCores->m_MaxCounterUid == counterWNumberOfCores->m_Uid + numberOfCores - 1);
1567 CHECK(counterWNumberOfCores->m_Class == 0);
1568 CHECK(counterWNumberOfCores->m_Interpolation == 1);
1569 CHECK(counterWNumberOfCores->m_Multiplier == 123.45f);
1570 CHECK(counterWNumberOfCores->m_Name == "valid name 8");
1571 CHECK(counterWNumberOfCores->m_Description == "valid description");
1572 CHECK(counterWNumberOfCores->m_Units == "");
1573 CHECK(counterWNumberOfCores->m_DeviceUid == 0);
1574 CHECK(counterWNumberOfCores->m_CounterSetUid == 0);
1575 CHECK(category->m_Counters.size() == 20);
Keith Davis3201eea2019-10-24 17:30:41 +01001576 for (size_t i = 0; i < numberOfCores; i++)
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001577 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01001578 CHECK(category->m_Counters[category->m_Counters.size() - numberOfCores + i] ==
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001579 counterWNumberOfCores->m_Uid + i);
1580 }
1581
1582 // Register a multi-core device for testing
1583 const std::string multiCoreDeviceName = "some_multi_core_device";
Keith Davis3201eea2019-10-24 17:30:41 +01001584 const Device* multiCoreDevice = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001585 CHECK_NOTHROW(multiCoreDevice = counterDirectory.RegisterDevice(multiCoreDeviceName, 4));
1586 CHECK(counterDirectory.GetDeviceCount() == 2);
1587 CHECK(multiCoreDevice);
1588 CHECK(multiCoreDevice->m_Name == multiCoreDeviceName);
1589 CHECK(multiCoreDevice->m_Uid >= 1);
1590 CHECK(multiCoreDevice->m_Cores == 4);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001591
1592 // Register a counter with a valid parent category name and associated to the multi-core device
1593 const Counter* counterWMultiCoreDevice = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001594 CHECK_NOTHROW(counterWMultiCoreDevice = counterDirectory.RegisterCounter(
Keith Davise394bd92019-12-02 15:12:19 +00001595 armnn::profiling::BACKEND_ID, 19, categoryName, 0, 1,
1596 123.45f, "valid name 9", "valid description",
Keith Davis3201eea2019-10-24 17:30:41 +01001597 armnn::EmptyOptional(), // Units
1598 armnn::EmptyOptional(), // Number of cores
1599 multiCoreDevice->m_Uid, // Device UID
1600 armnn::EmptyOptional())); // Counter set UID
Sadik Armagan1625efc2021-06-10 18:24:34 +01001601 CHECK(counterDirectory.GetCounterCount() == 24);
1602 CHECK(counterWMultiCoreDevice);
1603 CHECK(counterWMultiCoreDevice->m_Uid > counter->m_Uid);
1604 CHECK(counterWMultiCoreDevice->m_MaxCounterUid ==
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001605 counterWMultiCoreDevice->m_Uid + multiCoreDevice->m_Cores - 1);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001606 CHECK(counterWMultiCoreDevice->m_Class == 0);
1607 CHECK(counterWMultiCoreDevice->m_Interpolation == 1);
1608 CHECK(counterWMultiCoreDevice->m_Multiplier == 123.45f);
1609 CHECK(counterWMultiCoreDevice->m_Name == "valid name 9");
1610 CHECK(counterWMultiCoreDevice->m_Description == "valid description");
1611 CHECK(counterWMultiCoreDevice->m_Units == "");
1612 CHECK(counterWMultiCoreDevice->m_DeviceUid == multiCoreDevice->m_Uid);
1613 CHECK(counterWMultiCoreDevice->m_CounterSetUid == 0);
1614 CHECK(category->m_Counters.size() == 24);
Keith Davis3201eea2019-10-24 17:30:41 +01001615 for (size_t i = 0; i < 4; i++)
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001616 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01001617 CHECK(category->m_Counters[category->m_Counters.size() - 4 + i] == counterWMultiCoreDevice->m_Uid + i);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001618 }
1619
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001620 // Register a multi-core device associate to a parent category for testing
1621 const std::string multiCoreDeviceNameWParentCategory = "some_multi_core_device_with_parent_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001622 const Device* multiCoreDeviceWParentCategory = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001623 CHECK_NOTHROW(multiCoreDeviceWParentCategory =
Keith Davis3201eea2019-10-24 17:30:41 +01001624 counterDirectory.RegisterDevice(multiCoreDeviceNameWParentCategory, 2, categoryName));
Sadik Armagan1625efc2021-06-10 18:24:34 +01001625 CHECK(counterDirectory.GetDeviceCount() == 3);
1626 CHECK(multiCoreDeviceWParentCategory);
1627 CHECK(multiCoreDeviceWParentCategory->m_Name == multiCoreDeviceNameWParentCategory);
1628 CHECK(multiCoreDeviceWParentCategory->m_Uid >= 1);
1629 CHECK(multiCoreDeviceWParentCategory->m_Cores == 2);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001630
1631 // Register a counter with a valid parent category name and getting the number of cores of the multi-core device
1632 // associated to that category
1633 const Counter* counterWMultiCoreDeviceWParentCategory = nullptr;
Sadik Armagan4c998992020-02-25 12:44:44 +00001634 uint16_t numberOfCourse = multiCoreDeviceWParentCategory->m_Cores;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001635 CHECK_NOTHROW(counterWMultiCoreDeviceWParentCategory =
Jim Flynnbbfe6032020-07-20 16:57:44 +01001636 counterDirectory.RegisterCounter(
1637 armnn::profiling::BACKEND_ID,
1638 100,
1639 categoryName,
1640 0,
1641 1,
1642 123.45f,
1643 "valid name 10",
1644 "valid description",
1645 armnn::EmptyOptional(), // Units
1646 numberOfCourse, // Number of cores
1647 armnn::EmptyOptional(), // Device UID
1648 armnn::EmptyOptional()));// Counter set UID
Sadik Armagan1625efc2021-06-10 18:24:34 +01001649 CHECK(counterDirectory.GetCounterCount() == 26);
1650 CHECK(counterWMultiCoreDeviceWParentCategory);
1651 CHECK(counterWMultiCoreDeviceWParentCategory->m_Uid > counter->m_Uid);
1652 CHECK(counterWMultiCoreDeviceWParentCategory->m_MaxCounterUid ==
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001653 counterWMultiCoreDeviceWParentCategory->m_Uid + multiCoreDeviceWParentCategory->m_Cores - 1);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001654 CHECK(counterWMultiCoreDeviceWParentCategory->m_Class == 0);
1655 CHECK(counterWMultiCoreDeviceWParentCategory->m_Interpolation == 1);
1656 CHECK(counterWMultiCoreDeviceWParentCategory->m_Multiplier == 123.45f);
1657 CHECK(counterWMultiCoreDeviceWParentCategory->m_Name == "valid name 10");
1658 CHECK(counterWMultiCoreDeviceWParentCategory->m_Description == "valid description");
1659 CHECK(counterWMultiCoreDeviceWParentCategory->m_Units == "");
1660 CHECK(category->m_Counters.size() == 26);
Keith Davis3201eea2019-10-24 17:30:41 +01001661 for (size_t i = 0; i < 2; i++)
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001662 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01001663 CHECK(category->m_Counters[category->m_Counters.size() - 2 + i] ==
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001664 counterWMultiCoreDeviceWParentCategory->m_Uid + i);
1665 }
1666
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001667 // Register a counter set for testing
1668 const std::string counterSetName = "some_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +01001669 const CounterSet* counterSet = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001670 CHECK_NOTHROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
1671 CHECK(counterDirectory.GetCounterSetCount() == 1);
1672 CHECK(counterSet);
1673 CHECK(counterSet->m_Name == counterSetName);
1674 CHECK(counterSet->m_Uid >= 1);
1675 CHECK(counterSet->m_Count == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001676
1677 // Register a counter with a valid parent category name and associated to a counter set
1678 const Counter* counterWCounterSet = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001679 CHECK_NOTHROW(counterWCounterSet = counterDirectory.RegisterCounter(
Keith Davise394bd92019-12-02 15:12:19 +00001680 armnn::profiling::BACKEND_ID, 300,
Keith Davis3201eea2019-10-24 17:30:41 +01001681 categoryName, 0, 1, 123.45f, "valid name 11", "valid description",
1682 armnn::EmptyOptional(), // Units
1683 0, // Number of cores
1684 armnn::EmptyOptional(), // Device UID
1685 counterSet->m_Uid)); // Counter set UID
Sadik Armagan1625efc2021-06-10 18:24:34 +01001686 CHECK(counterDirectory.GetCounterCount() == 27);
1687 CHECK(counterWCounterSet);
1688 CHECK(counterWCounterSet->m_Uid > counter->m_Uid);
1689 CHECK(counterWCounterSet->m_MaxCounterUid == counterWCounterSet->m_Uid);
1690 CHECK(counterWCounterSet->m_Class == 0);
1691 CHECK(counterWCounterSet->m_Interpolation == 1);
1692 CHECK(counterWCounterSet->m_Multiplier == 123.45f);
1693 CHECK(counterWCounterSet->m_Name == "valid name 11");
1694 CHECK(counterWCounterSet->m_Description == "valid description");
1695 CHECK(counterWCounterSet->m_Units == "");
1696 CHECK(counterWCounterSet->m_DeviceUid == 0);
1697 CHECK(counterWCounterSet->m_CounterSetUid == counterSet->m_Uid);
1698 CHECK(category->m_Counters.size() == 27);
1699 CHECK(category->m_Counters.back() == counterWCounterSet->m_Uid);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001700
1701 // Register a counter with a valid parent category name and associated to a device and a counter set
1702 const Counter* counterWDeviceWCounterSet = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001703 CHECK_NOTHROW(counterWDeviceWCounterSet = counterDirectory.RegisterCounter(
Keith Davise394bd92019-12-02 15:12:19 +00001704 armnn::profiling::BACKEND_ID, 23,
Keith Davis3201eea2019-10-24 17:30:41 +01001705 categoryName, 0, 1, 123.45f, "valid name 12", "valid description",
1706 armnn::EmptyOptional(), // Units
1707 1, // Number of cores
1708 device->m_Uid, // Device UID
1709 counterSet->m_Uid)); // Counter set UID
Sadik Armagan1625efc2021-06-10 18:24:34 +01001710 CHECK(counterDirectory.GetCounterCount() == 28);
1711 CHECK(counterWDeviceWCounterSet);
1712 CHECK(counterWDeviceWCounterSet->m_Uid > counter->m_Uid);
1713 CHECK(counterWDeviceWCounterSet->m_MaxCounterUid == counterWDeviceWCounterSet->m_Uid);
1714 CHECK(counterWDeviceWCounterSet->m_Class == 0);
1715 CHECK(counterWDeviceWCounterSet->m_Interpolation == 1);
1716 CHECK(counterWDeviceWCounterSet->m_Multiplier == 123.45f);
1717 CHECK(counterWDeviceWCounterSet->m_Name == "valid name 12");
1718 CHECK(counterWDeviceWCounterSet->m_Description == "valid description");
1719 CHECK(counterWDeviceWCounterSet->m_Units == "");
1720 CHECK(counterWDeviceWCounterSet->m_DeviceUid == device->m_Uid);
1721 CHECK(counterWDeviceWCounterSet->m_CounterSetUid == counterSet->m_Uid);
1722 CHECK(category->m_Counters.size() == 28);
1723 CHECK(category->m_Counters.back() == counterWDeviceWCounterSet->m_Uid);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001724
1725 // Register another category for testing
1726 const std::string anotherCategoryName = "some_other_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001727 const Category* anotherCategory = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001728 CHECK_NOTHROW(anotherCategory = counterDirectory.RegisterCategory(anotherCategoryName));
1729 CHECK(counterDirectory.GetCategoryCount() == 2);
1730 CHECK(anotherCategory);
1731 CHECK(anotherCategory != category);
1732 CHECK(anotherCategory->m_Name == anotherCategoryName);
1733 CHECK(anotherCategory->m_Counters.empty());
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001734
1735 // Register a counter to the other category
1736 const Counter* anotherCounter = nullptr;
Sadik Armagan1625efc2021-06-10 18:24:34 +01001737 CHECK_NOTHROW(anotherCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID, 24,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001738 anotherCategoryName, 1, 0, .00043f,
1739 "valid name", "valid description",
1740 armnn::EmptyOptional(), // Units
1741 armnn::EmptyOptional(), // Number of cores
1742 device->m_Uid, // Device UID
1743 counterSet->m_Uid)); // Counter set UID
Sadik Armagan1625efc2021-06-10 18:24:34 +01001744 CHECK(counterDirectory.GetCounterCount() == 29);
1745 CHECK(anotherCounter);
1746 CHECK(anotherCounter->m_MaxCounterUid == anotherCounter->m_Uid);
1747 CHECK(anotherCounter->m_Class == 1);
1748 CHECK(anotherCounter->m_Interpolation == 0);
1749 CHECK(anotherCounter->m_Multiplier == .00043f);
1750 CHECK(anotherCounter->m_Name == "valid name");
1751 CHECK(anotherCounter->m_Description == "valid description");
1752 CHECK(anotherCounter->m_Units == "");
1753 CHECK(anotherCounter->m_DeviceUid == device->m_Uid);
1754 CHECK(anotherCounter->m_CounterSetUid == counterSet->m_Uid);
1755 CHECK(anotherCategory->m_Counters.size() == 1);
1756 CHECK(anotherCategory->m_Counters.back() == anotherCounter->m_Uid);
Matteo Martincighab173e92019-09-05 12:02:04 +01001757}
1758
Sadik Armagan1625efc2021-06-10 18:24:34 +01001759TEST_CASE("CounterSelectionCommandHandlerParseData")
Ferran Balaguer1b941722019-08-28 16:57:18 +01001760{
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01001761 ProfilingStateMachine profilingStateMachine;
1762
Ferran Balaguer1b941722019-08-28 16:57:18 +01001763 class TestCaptureThread : public IPeriodicCounterCapture
1764 {
Keith Davis3201eea2019-10-24 17:30:41 +01001765 void Start() override
1766 {}
1767 void Stop() override
1768 {}
Ferran Balaguer1b941722019-08-28 16:57:18 +01001769 };
1770
Matteo Martincighe8485382019-10-10 14:08:21 +01001771 class TestReadCounterValues : public IReadCounterValues
1772 {
Keith Davis3201eea2019-10-24 17:30:41 +01001773 bool IsCounterRegistered(uint16_t counterUid) const override
1774 {
Jan Eilers8eb25602020-03-09 12:13:48 +00001775 armnn::IgnoreUnused(counterUid);
Keith Davis3201eea2019-10-24 17:30:41 +01001776 return true;
1777 }
Jim Flynn34430252022-03-04 15:03:58 +00001778 bool IsCounterRegistered(const std::string& counterName) const override
1779 {
1780 armnn::IgnoreUnused(counterName);
1781 return true;
1782 }
Keith Davis3201eea2019-10-24 17:30:41 +01001783 uint16_t GetCounterCount() const override
1784 {
1785 return 0;
1786 }
Finn Williamsf3fcf322020-05-11 14:38:02 +01001787 uint32_t GetAbsoluteCounterValue(uint16_t counterUid) const override
1788 {
1789 armnn::IgnoreUnused(counterUid);
1790 return 0;
1791 }
1792 uint32_t GetDeltaCounterValue(uint16_t counterUid) override
Keith Davis3201eea2019-10-24 17:30:41 +01001793 {
Jan Eilers8eb25602020-03-09 12:13:48 +00001794 armnn::IgnoreUnused(counterUid);
Keith Davis3201eea2019-10-24 17:30:41 +01001795 return 0;
1796 }
Matteo Martincighe8485382019-10-10 14:08:21 +01001797 };
Jim Flynn397043f2019-10-17 17:37:10 +01001798 const uint32_t familyId = 0;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001799 const uint32_t packetId = 0x40000;
1800
1801 uint32_t version = 1;
Cathal Corbett6f073722022-03-04 12:11:09 +00001802 const std::unordered_map<std::string,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001803 std::shared_ptr<IBackendProfilingContext>> backendProfilingContext;
Finn Williams032bc742020-02-12 11:02:34 +00001804 CounterIdMap counterIdMap;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001805 Holder holder;
1806 TestCaptureThread captureThread;
Matteo Martincighe8485382019-10-10 14:08:21 +01001807 TestReadCounterValues readCounterValues;
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001808 MockBufferManager mockBuffer(512);
Sadik Armagan3896b472020-02-10 12:24:15 +00001809 SendCounterPacket sendCounterPacket(mockBuffer);
1810 SendThread sendThread(profilingStateMachine, mockBuffer, sendCounterPacket);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001811
Jim Flynn75c14f42022-03-10 22:05:42 +00001812 uint32_t sizeOfUint32 = arm::pipe::numeric_cast<uint32_t>(sizeof(uint32_t));
1813 uint32_t sizeOfUint16 = arm::pipe::numeric_cast<uint32_t>(sizeof(uint16_t));
Ferran Balaguer1b941722019-08-28 16:57:18 +01001814
1815 // Data with period and counters
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00001816 uint32_t period1 = arm::pipe::LOWEST_CAPTURE_PERIOD;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001817 uint32_t dataLength1 = 8;
Keith Davis3201eea2019-10-24 17:30:41 +01001818 uint32_t offset = 0;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001819
Matteo Martincigh67ef2a52019-10-10 13:29:02 +01001820 std::unique_ptr<unsigned char[]> uniqueData1 = std::make_unique<unsigned char[]>(dataLength1);
Keith Davis3201eea2019-10-24 17:30:41 +01001821 unsigned char* data1 = reinterpret_cast<unsigned char*>(uniqueData1.get());
FinnWilliamsArma0c78712019-09-16 12:06:47 +01001822
Ferran Balaguer1b941722019-08-28 16:57:18 +01001823 WriteUint32(data1, offset, period1);
1824 offset += sizeOfUint32;
1825 WriteUint16(data1, offset, 4000);
1826 offset += sizeOfUint16;
1827 WriteUint16(data1, offset, 5000);
1828
Jim Flynnbbfe6032020-07-20 16:57:44 +01001829 arm::pipe::Packet packetA(packetId, dataLength1, uniqueData1);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001830
Finn Williams032bc742020-02-12 11:02:34 +00001831 PeriodicCounterSelectionCommandHandler commandHandler(familyId, packetId, version, backendProfilingContext,
1832 counterIdMap, holder, 10000u, captureThread,
Keith Davis3201eea2019-10-24 17:30:41 +01001833 readCounterValues, sendCounterPacket, profilingStateMachine);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001834
Matteo Martincighe8485382019-10-10 14:08:21 +01001835 profilingStateMachine.TransitionToState(ProfilingState::Uninitialised);
Jim Flynnf9db3ef2022-03-08 21:23:44 +00001836 CHECK_THROWS_AS(commandHandler(packetA), arm::pipe::ProfilingException);
Matteo Martincighe8485382019-10-10 14:08:21 +01001837 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
Jim Flynnf9db3ef2022-03-08 21:23:44 +00001838 CHECK_THROWS_AS(commandHandler(packetA), arm::pipe::ProfilingException);
Matteo Martincighe8485382019-10-10 14:08:21 +01001839 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
Jim Flynnf9db3ef2022-03-08 21:23:44 +00001840 CHECK_THROWS_AS(commandHandler(packetA), arm::pipe::ProfilingException);
Matteo Martincighe8485382019-10-10 14:08:21 +01001841 profilingStateMachine.TransitionToState(ProfilingState::Active);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001842 CHECK_NOTHROW(commandHandler(packetA));
Matteo Martincighe8485382019-10-10 14:08:21 +01001843
1844 const std::vector<uint16_t> counterIdsA = holder.GetCaptureData().GetCounterIds();
Ferran Balaguer1b941722019-08-28 16:57:18 +01001845
Sadik Armagan1625efc2021-06-10 18:24:34 +01001846 CHECK(holder.GetCaptureData().GetCapturePeriod() == period1);
1847 CHECK(counterIdsA.size() == 2);
1848 CHECK(counterIdsA[0] == 4000);
1849 CHECK(counterIdsA[1] == 5000);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001850
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001851 auto readBuffer = mockBuffer.GetReadableBuffer();
Ferran Balaguer1b941722019-08-28 16:57:18 +01001852
1853 offset = 0;
1854
1855 uint32_t headerWord0 = ReadUint32(readBuffer, offset);
1856 offset += sizeOfUint32;
1857 uint32_t headerWord1 = ReadUint32(readBuffer, offset);
1858 offset += sizeOfUint32;
1859 uint32_t period = ReadUint32(readBuffer, offset);
1860
Sadik Armagan1625efc2021-06-10 18:24:34 +01001861 CHECK(((headerWord0 >> 26) & 0x3F) == 0); // packet family
1862 CHECK(((headerWord0 >> 16) & 0x3FF) == 4); // packet id
1863 CHECK(headerWord1 == 8); // data length
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00001864 CHECK(period == arm::pipe::LOWEST_CAPTURE_PERIOD); // capture period
Ferran Balaguer1b941722019-08-28 16:57:18 +01001865
1866 uint16_t counterId = 0;
1867 offset += sizeOfUint32;
1868 counterId = ReadUint16(readBuffer, offset);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001869 CHECK(counterId == 4000);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001870 offset += sizeOfUint16;
1871 counterId = ReadUint16(readBuffer, offset);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001872 CHECK(counterId == 5000);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001873
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001874 mockBuffer.MarkRead(readBuffer);
1875
Ferran Balaguer1b941722019-08-28 16:57:18 +01001876 // Data with period only
Colm Donelan02705242019-11-14 14:19:07 +00001877 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 +01001878 uint32_t dataLength2 = 4;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001879
Matteo Martincigh67ef2a52019-10-10 13:29:02 +01001880 std::unique_ptr<unsigned char[]> uniqueData2 = std::make_unique<unsigned char[]>(dataLength2);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001881
FinnWilliamsArma0c78712019-09-16 12:06:47 +01001882 WriteUint32(reinterpret_cast<unsigned char*>(uniqueData2.get()), 0, period2);
1883
Jim Flynnbbfe6032020-07-20 16:57:44 +01001884 arm::pipe::Packet packetB(packetId, dataLength2, uniqueData2);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001885
1886 commandHandler(packetB);
1887
Matteo Martincighe8485382019-10-10 14:08:21 +01001888 const std::vector<uint16_t> counterIdsB = holder.GetCaptureData().GetCounterIds();
Ferran Balaguer1b941722019-08-28 16:57:18 +01001889
Colm Donelan02705242019-11-14 14:19:07 +00001890 // Value should have been pulled up from 9000 to LOWEST_CAPTURE_PERIOD.
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00001891 CHECK(holder.GetCaptureData().GetCapturePeriod() == arm::pipe::LOWEST_CAPTURE_PERIOD);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001892 CHECK(counterIdsB.size() == 0);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001893
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001894 readBuffer = mockBuffer.GetReadableBuffer();
Ferran Balaguer1b941722019-08-28 16:57:18 +01001895
1896 offset = 0;
1897
1898 headerWord0 = ReadUint32(readBuffer, offset);
1899 offset += sizeOfUint32;
1900 headerWord1 = ReadUint32(readBuffer, offset);
1901 offset += sizeOfUint32;
1902 period = ReadUint32(readBuffer, offset);
1903
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00001904 CHECK(((headerWord0 >> 26) & 0x3F) == 0); // packet family
1905 CHECK(((headerWord0 >> 16) & 0x3FF) == 4); // packet id
1906 CHECK(headerWord1 == 4); // data length
1907 CHECK(period == arm::pipe::LOWEST_CAPTURE_PERIOD); // capture period
Ferran Balaguer1b941722019-08-28 16:57:18 +01001908}
1909
Sadik Armagan1625efc2021-06-10 18:24:34 +01001910TEST_CASE("CheckTimelineActivationAndDeactivation")
Keith Davis33ed2212020-03-30 10:43:41 +01001911{
1912 class TestReportStructure : public IReportStructure
1913 {
1914 public:
Cathal Corbett91343f32022-03-11 13:05:44 +00001915 virtual void ReportStructure(arm::pipe::IProfilingService& /*profilingService*/) override
Keith Davis33ed2212020-03-30 10:43:41 +01001916 {
1917 m_ReportStructureCalled = true;
1918 }
1919
1920 bool m_ReportStructureCalled = false;
1921 };
1922
1923 class TestNotifyBackends : public INotifyBackends
1924 {
1925 public:
1926 TestNotifyBackends() : m_timelineReporting(false) {}
1927 virtual void NotifyBackendsForTimelineReporting() override
1928 {
1929 m_TestNotifyBackendsCalled = m_timelineReporting.load();
1930 }
1931
1932 bool m_TestNotifyBackendsCalled = false;
1933 std::atomic<bool> m_timelineReporting;
1934 };
1935
Jim Flynnbbfe6032020-07-20 16:57:44 +01001936 arm::pipe::PacketVersionResolver packetVersionResolver;
Keith Davis33ed2212020-03-30 10:43:41 +01001937
1938 BufferManager bufferManager(512);
1939 SendTimelinePacket sendTimelinePacket(bufferManager);
1940 ProfilingStateMachine stateMachine;
1941 TestReportStructure testReportStructure;
1942 TestNotifyBackends testNotifyBackends;
1943
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00001944 ActivateTimelineReportingCommandHandler activateTimelineReportingCommandHandler(0,
Keith Davis33ed2212020-03-30 10:43:41 +01001945 6,
1946 packetVersionResolver.ResolvePacketVersion(0, 6)
1947 .GetEncodedValue(),
1948 sendTimelinePacket,
1949 stateMachine,
1950 testReportStructure,
1951 testNotifyBackends.m_timelineReporting,
1952 testNotifyBackends);
1953
1954 // Write an "ActivateTimelineReporting" packet into the mock profiling connection, to simulate an input from an
1955 // external profiling service
1956 const uint32_t packetFamily1 = 0;
1957 const uint32_t packetId1 = 6;
1958 uint32_t packetHeader1 = ConstructHeader(packetFamily1, packetId1);
1959
1960 // Create the ActivateTimelineReportingPacket
Jim Flynnbbfe6032020-07-20 16:57:44 +01001961 arm::pipe::Packet ActivateTimelineReportingPacket(packetHeader1); // Length == 0
Keith Davis33ed2212020-03-30 10:43:41 +01001962
Sadik Armagan1625efc2021-06-10 18:24:34 +01001963 CHECK_THROWS_AS(
Jim Flynnf9db3ef2022-03-08 21:23:44 +00001964 activateTimelineReportingCommandHandler.operator()(ActivateTimelineReportingPacket),
1965 arm::pipe::ProfilingException);
Keith Davis33ed2212020-03-30 10:43:41 +01001966
1967 stateMachine.TransitionToState(ProfilingState::NotConnected);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001968 CHECK_THROWS_AS(
Jim Flynnf9db3ef2022-03-08 21:23:44 +00001969 activateTimelineReportingCommandHandler.operator()(ActivateTimelineReportingPacket),
1970 arm::pipe::ProfilingException);
Keith Davis33ed2212020-03-30 10:43:41 +01001971
1972 stateMachine.TransitionToState(ProfilingState::WaitingForAck);
Sadik Armagan1625efc2021-06-10 18:24:34 +01001973 CHECK_THROWS_AS(
Jim Flynnf9db3ef2022-03-08 21:23:44 +00001974 activateTimelineReportingCommandHandler.operator()(ActivateTimelineReportingPacket),
1975 arm::pipe::ProfilingException);
Keith Davis33ed2212020-03-30 10:43:41 +01001976
1977 stateMachine.TransitionToState(ProfilingState::Active);
1978 activateTimelineReportingCommandHandler.operator()(ActivateTimelineReportingPacket);
1979
Sadik Armagan1625efc2021-06-10 18:24:34 +01001980 CHECK(testReportStructure.m_ReportStructureCalled);
1981 CHECK(testNotifyBackends.m_TestNotifyBackendsCalled);
1982 CHECK(testNotifyBackends.m_timelineReporting.load());
Keith Davis33ed2212020-03-30 10:43:41 +01001983
1984 DeactivateTimelineReportingCommandHandler deactivateTimelineReportingCommandHandler(0,
1985 7,
1986 packetVersionResolver.ResolvePacketVersion(0, 7).GetEncodedValue(),
1987 testNotifyBackends.m_timelineReporting,
1988 stateMachine,
1989 testNotifyBackends);
1990
1991 const uint32_t packetFamily2 = 0;
1992 const uint32_t packetId2 = 7;
1993 uint32_t packetHeader2 = ConstructHeader(packetFamily2, packetId2);
1994
1995 // Create the DeactivateTimelineReportingPacket
Jim Flynnbbfe6032020-07-20 16:57:44 +01001996 arm::pipe::Packet deactivateTimelineReportingPacket(packetHeader2); // Length == 0
Keith Davis33ed2212020-03-30 10:43:41 +01001997
1998 stateMachine.Reset();
Sadik Armagan1625efc2021-06-10 18:24:34 +01001999 CHECK_THROWS_AS(
Jim Flynnf9db3ef2022-03-08 21:23:44 +00002000 deactivateTimelineReportingCommandHandler.operator()(deactivateTimelineReportingPacket),
2001 arm::pipe::ProfilingException);
Keith Davis33ed2212020-03-30 10:43:41 +01002002
2003 stateMachine.TransitionToState(ProfilingState::NotConnected);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002004 CHECK_THROWS_AS(
Jim Flynnf9db3ef2022-03-08 21:23:44 +00002005 deactivateTimelineReportingCommandHandler.operator()(deactivateTimelineReportingPacket),
2006 arm::pipe::ProfilingException);
Keith Davis33ed2212020-03-30 10:43:41 +01002007
2008 stateMachine.TransitionToState(ProfilingState::WaitingForAck);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002009 CHECK_THROWS_AS(
Jim Flynnf9db3ef2022-03-08 21:23:44 +00002010 deactivateTimelineReportingCommandHandler.operator()(deactivateTimelineReportingPacket),
2011 arm::pipe::ProfilingException);
Keith Davis33ed2212020-03-30 10:43:41 +01002012
2013 stateMachine.TransitionToState(ProfilingState::Active);
2014 deactivateTimelineReportingCommandHandler.operator()(deactivateTimelineReportingPacket);
2015
Sadik Armagan1625efc2021-06-10 18:24:34 +01002016 CHECK(!testNotifyBackends.m_TestNotifyBackendsCalled);
2017 CHECK(!testNotifyBackends.m_timelineReporting.load());
Keith Davis33ed2212020-03-30 10:43:41 +01002018}
2019
Sadik Armagan1625efc2021-06-10 18:24:34 +01002020TEST_CASE("CheckProfilingServiceNotActive")
Keith Davis33ed2212020-03-30 10:43:41 +01002021{
2022 using namespace armnn;
Keith Davis33ed2212020-03-30 10:43:41 +01002023
2024 // Create runtime in which the test will run
2025 armnn::IRuntime::CreationOptions options;
2026 options.m_ProfilingOptions.m_EnableProfiling = true;
2027
Kevin Mayd92a6e42021-02-04 10:27:41 +00002028 armnn::RuntimeImpl runtime(options);
Jim Flynn34430252022-03-04 15:03:58 +00002029 armnn::ArmNNProfilingServiceInitialiser initialiser;
2030 ProfilingServiceRuntimeHelper profilingServiceHelper(
2031 arm::pipe::MAX_ARMNN_COUNTER, initialiser, GetProfilingService(&runtime));
Keith Davis33ed2212020-03-30 10:43:41 +01002032 profilingServiceHelper.ForceTransitionToState(ProfilingState::NotConnected);
2033 profilingServiceHelper.ForceTransitionToState(ProfilingState::WaitingForAck);
2034 profilingServiceHelper.ForceTransitionToState(ProfilingState::Active);
2035
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00002036 BufferManager& bufferManager = profilingServiceHelper.GetProfilingBufferManager();
Keith Davis33ed2212020-03-30 10:43:41 +01002037 auto readableBuffer = bufferManager.GetReadableBuffer();
2038
2039 // Profiling is enabled, the post-optimisation structure should be created
Sadik Armagan1625efc2021-06-10 18:24:34 +01002040 CHECK(readableBuffer == nullptr);
Keith Davis33ed2212020-03-30 10:43:41 +01002041}
2042
Sadik Armagan1625efc2021-06-10 18:24:34 +01002043TEST_CASE("CheckConnectionAcknowledged")
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002044{
Keith Davis3201eea2019-10-24 17:30:41 +01002045 const uint32_t packetFamilyId = 0;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002046 const uint32_t connectionPacketId = 0x10000;
Keith Davis3201eea2019-10-24 17:30:41 +01002047 const uint32_t version = 1;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002048
Jim Flynn75c14f42022-03-10 22:05:42 +00002049 uint32_t sizeOfUint32 = arm::pipe::numeric_cast<uint32_t>(sizeof(uint32_t));
2050 uint32_t sizeOfUint16 = arm::pipe::numeric_cast<uint32_t>(sizeof(uint16_t));
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002051
2052 // Data with period and counters
Keith Davis3201eea2019-10-24 17:30:41 +01002053 uint32_t period1 = 10;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002054 uint32_t dataLength1 = 8;
Keith Davis3201eea2019-10-24 17:30:41 +01002055 uint32_t offset = 0;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002056
Matteo Martincigh67ef2a52019-10-10 13:29:02 +01002057 std::unique_ptr<unsigned char[]> uniqueData1 = std::make_unique<unsigned char[]>(dataLength1);
Keith Davis3201eea2019-10-24 17:30:41 +01002058 unsigned char* data1 = reinterpret_cast<unsigned char*>(uniqueData1.get());
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002059
2060 WriteUint32(data1, offset, period1);
2061 offset += sizeOfUint32;
2062 WriteUint16(data1, offset, 4000);
2063 offset += sizeOfUint16;
2064 WriteUint16(data1, offset, 5000);
2065
Jim Flynnbbfe6032020-07-20 16:57:44 +01002066 arm::pipe::Packet packetA(connectionPacketId, dataLength1, uniqueData1);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002067
2068 ProfilingStateMachine profilingState(ProfilingState::Uninitialised);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002069 CHECK(profilingState.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002070 CounterDirectory counterDirectory;
2071 MockBufferManager mockBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002072 SendCounterPacket sendCounterPacket(mockBuffer);
2073 SendThread sendThread(profilingState, mockBuffer, sendCounterPacket);
Matteo Martincighcdfb9412019-11-08 11:23:06 +00002074 SendTimelinePacket sendTimelinePacket(mockBuffer);
Jim Flynn6398a982020-05-27 17:05:21 +01002075 MockProfilingServiceStatus mockProfilingServiceStatus;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002076
Jim Flynn6398a982020-05-27 17:05:21 +01002077 ConnectionAcknowledgedCommandHandler commandHandler(packetFamilyId,
2078 connectionPacketId,
2079 version,
2080 counterDirectory,
2081 sendCounterPacket,
2082 sendTimelinePacket,
2083 profilingState,
2084 mockProfilingServiceStatus);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002085
2086 // command handler received packet on ProfilingState::Uninitialised
Jim Flynnf9db3ef2022-03-08 21:23:44 +00002087 CHECK_THROWS_AS(commandHandler(packetA), arm::pipe::ProfilingException);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002088
2089 profilingState.TransitionToState(ProfilingState::NotConnected);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002090 CHECK(profilingState.GetCurrentState() == ProfilingState::NotConnected);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002091 // command handler received packet on ProfilingState::NotConnected
Jim Flynnf9db3ef2022-03-08 21:23:44 +00002092 CHECK_THROWS_AS(commandHandler(packetA), arm::pipe::ProfilingException);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002093
2094 profilingState.TransitionToState(ProfilingState::WaitingForAck);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002095 CHECK(profilingState.GetCurrentState() == ProfilingState::WaitingForAck);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002096 // command handler received packet on ProfilingState::WaitingForAck
Sadik Armagan1625efc2021-06-10 18:24:34 +01002097 CHECK_NOTHROW(commandHandler(packetA));
2098 CHECK(profilingState.GetCurrentState() == ProfilingState::Active);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002099
2100 // command handler received packet on ProfilingState::Active
Sadik Armagan1625efc2021-06-10 18:24:34 +01002101 CHECK_NOTHROW(commandHandler(packetA));
2102 CHECK(profilingState.GetCurrentState() == ProfilingState::Active);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002103
2104 // command handler received different packet
2105 const uint32_t differentPacketId = 0x40000;
Jim Flynnbbfe6032020-07-20 16:57:44 +01002106 arm::pipe::Packet packetB(differentPacketId, dataLength1, uniqueData1);
Matteo Martincighd0613b52019-10-09 16:47:04 +01002107 profilingState.TransitionToState(ProfilingState::NotConnected);
2108 profilingState.TransitionToState(ProfilingState::WaitingForAck);
Jim Flynn6398a982020-05-27 17:05:21 +01002109 ConnectionAcknowledgedCommandHandler differentCommandHandler(packetFamilyId,
2110 differentPacketId,
2111 version,
2112 counterDirectory,
2113 sendCounterPacket,
2114 sendTimelinePacket,
2115 profilingState,
2116 mockProfilingServiceStatus);
Jim Flynnf9db3ef2022-03-08 21:23:44 +00002117 CHECK_THROWS_AS(differentCommandHandler(packetB), arm::pipe::ProfilingException);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01002118}
2119
Sadik Armagan1625efc2021-06-10 18:24:34 +01002120TEST_CASE("CheckSocketConnectionException")
Teresa Charlin9bab4962019-09-06 12:28:35 +01002121{
Sadik Armagana97a0be2020-03-03 10:44:56 +00002122 // Check that creating a SocketProfilingConnection armnnProfiling in an exception as the Gator UDS doesn't exist.
Sadik Armagan1625efc2021-06-10 18:24:34 +01002123 CHECK_THROWS_AS(new SocketProfilingConnection(), arm::pipe::SocketConnectionException);
Sadik Armagana97a0be2020-03-03 10:44:56 +00002124}
2125
Sadik Armagan1625efc2021-06-10 18:24:34 +01002126TEST_CASE("CheckSocketConnectionException2")
Sadik Armagana97a0be2020-03-03 10:44:56 +00002127{
2128 try
2129 {
2130 new SocketProfilingConnection();
2131 }
Jim Flynnbbfe6032020-07-20 16:57:44 +01002132 catch (const arm::pipe::SocketConnectionException& ex)
Sadik Armagana97a0be2020-03-03 10:44:56 +00002133 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01002134 CHECK(ex.GetSocketFd() == 0);
2135 CHECK(ex.GetErrorNo() == ECONNREFUSED);
2136 CHECK(ex.what()
Sadik Armagana97a0be2020-03-03 10:44:56 +00002137 == std::string("SocketProfilingConnection: Cannot connect to stream socket: Connection refused"));
2138 }
Teresa Charlin9bab4962019-09-06 12:28:35 +01002139}
2140
Sadik Armagan1625efc2021-06-10 18:24:34 +01002141TEST_CASE("SwTraceIsValidCharTest")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002142{
2143 // Only ASCII 7-bit encoding supported
2144 for (unsigned char c = 0; c < 128; c++)
2145 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01002146 CHECK(arm::pipe::SwTraceCharPolicy::IsValidChar(c));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002147 }
2148
2149 // Not ASCII
2150 for (unsigned char c = 255; c >= 128; c++)
2151 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01002152 CHECK(!arm::pipe::SwTraceCharPolicy::IsValidChar(c));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002153 }
2154}
2155
Sadik Armagan1625efc2021-06-10 18:24:34 +01002156TEST_CASE("SwTraceIsValidNameCharTest")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002157{
2158 // Only alpha-numeric and underscore ASCII 7-bit encoding supported
2159 const unsigned char validChars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
2160 for (unsigned char i = 0; i < sizeof(validChars) / sizeof(validChars[0]) - 1; i++)
2161 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01002162 CHECK(arm::pipe::SwTraceNameCharPolicy::IsValidChar(validChars[i]));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002163 }
2164
2165 // Non alpha-numeric chars
2166 for (unsigned char c = 0; c < 48; c++)
2167 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01002168 CHECK(!arm::pipe::SwTraceNameCharPolicy::IsValidChar(c));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002169 }
2170 for (unsigned char c = 58; c < 65; c++)
2171 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01002172 CHECK(!arm::pipe::SwTraceNameCharPolicy::IsValidChar(c));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002173 }
2174 for (unsigned char c = 91; c < 95; c++)
2175 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01002176 CHECK(!arm::pipe::SwTraceNameCharPolicy::IsValidChar(c));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002177 }
2178 for (unsigned char c = 96; c < 97; c++)
2179 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01002180 CHECK(!arm::pipe::SwTraceNameCharPolicy::IsValidChar(c));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002181 }
2182 for (unsigned char c = 123; c < 128; c++)
2183 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01002184 CHECK(!arm::pipe::SwTraceNameCharPolicy::IsValidChar(c));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002185 }
2186
2187 // Not ASCII
2188 for (unsigned char c = 255; c >= 128; c++)
2189 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01002190 CHECK(!arm::pipe::SwTraceNameCharPolicy::IsValidChar(c));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002191 }
2192}
2193
Sadik Armagan1625efc2021-06-10 18:24:34 +01002194TEST_CASE("IsValidSwTraceStringTest")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002195{
2196 // Valid SWTrace strings
Sadik Armagan1625efc2021-06-10 18:24:34 +01002197 CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>(""));
2198 CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>("_"));
2199 CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>("0123"));
2200 CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>("valid_string"));
2201 CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>("VALID_string_456"));
2202 CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>(" "));
2203 CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>("valid string"));
2204 CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>("!$%"));
2205 CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>("valid|\\~string#123"));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002206
2207 // Invalid SWTrace strings
Sadik Armagan1625efc2021-06-10 18:24:34 +01002208 CHECK(!arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>("€£"));
2209 CHECK(!arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>("invalid‡string"));
2210 CHECK(!arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceCharPolicy>("12Ž34"));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002211}
2212
Sadik Armagan1625efc2021-06-10 18:24:34 +01002213TEST_CASE("IsValidSwTraceNameStringTest")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002214{
2215 // Valid SWTrace name strings
Sadik Armagan1625efc2021-06-10 18:24:34 +01002216 CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>(""));
2217 CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>("_"));
2218 CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>("0123"));
2219 CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>("valid_string"));
2220 CHECK(arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>("VALID_string_456"));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002221
2222 // Invalid SWTrace name strings
Sadik Armagan1625efc2021-06-10 18:24:34 +01002223 CHECK(!arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>(" "));
2224 CHECK(!arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>("invalid string"));
2225 CHECK(!arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>("!$%"));
2226 CHECK(!arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>("invalid|\\~string#123"));
2227 CHECK(!arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>("€£"));
2228 CHECK(!arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>("invalid‡string"));
2229 CHECK(!arm::pipe::IsValidSwTraceString<arm::pipe::SwTraceNameCharPolicy>("12Ž34"));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002230}
2231
2232template <typename SwTracePolicy>
2233void StringToSwTraceStringTestHelper(const std::string& testString, std::vector<uint32_t> buffer, size_t expectedSize)
2234{
2235 // Convert the test string to a SWTrace string
Sadik Armagan1625efc2021-06-10 18:24:34 +01002236 CHECK(arm::pipe::StringToSwTraceString<SwTracePolicy>(testString, buffer));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002237
2238 // The buffer must contain at least the length of the string
Sadik Armagan1625efc2021-06-10 18:24:34 +01002239 CHECK(!buffer.empty());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002240
2241 // The buffer must be of the expected size (in words)
Sadik Armagan1625efc2021-06-10 18:24:34 +01002242 CHECK(buffer.size() == expectedSize);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002243
2244 // The first word of the byte must be the length of the string including the null-terminator
Sadik Armagan1625efc2021-06-10 18:24:34 +01002245 CHECK(buffer[0] == testString.size() + 1);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002246
2247 // The contents of the buffer must match the test string
Sadik Armagan1625efc2021-06-10 18:24:34 +01002248 CHECK(std::memcmp(testString.data(), buffer.data() + 1, testString.size()) == 0);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002249
2250 // The buffer must include the null-terminator at the end of the string
2251 size_t nullTerminatorIndex = sizeof(uint32_t) + testString.size();
Sadik Armagan1625efc2021-06-10 18:24:34 +01002252 CHECK(reinterpret_cast<unsigned char*>(buffer.data())[nullTerminatorIndex] == '\0');
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002253}
2254
Sadik Armagan1625efc2021-06-10 18:24:34 +01002255TEST_CASE("StringToSwTraceStringTest")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002256{
2257 std::vector<uint32_t> buffer;
2258
2259 // Valid SWTrace strings (expected size in words)
Jim Flynnbbfe6032020-07-20 16:57:44 +01002260 StringToSwTraceStringTestHelper<arm::pipe::SwTraceCharPolicy>("", buffer, 2);
2261 StringToSwTraceStringTestHelper<arm::pipe::SwTraceCharPolicy>("_", buffer, 2);
2262 StringToSwTraceStringTestHelper<arm::pipe::SwTraceCharPolicy>("0123", buffer, 3);
2263 StringToSwTraceStringTestHelper<arm::pipe::SwTraceCharPolicy>("valid_string", buffer, 5);
2264 StringToSwTraceStringTestHelper<arm::pipe::SwTraceCharPolicy>("VALID_string_456", buffer, 6);
2265 StringToSwTraceStringTestHelper<arm::pipe::SwTraceCharPolicy>(" ", buffer, 2);
2266 StringToSwTraceStringTestHelper<arm::pipe::SwTraceCharPolicy>("valid string", buffer, 5);
2267 StringToSwTraceStringTestHelper<arm::pipe::SwTraceCharPolicy>("!$%", buffer, 2);
2268 StringToSwTraceStringTestHelper<arm::pipe::SwTraceCharPolicy>("valid|\\~string#123", buffer, 6);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002269
2270 // Invalid SWTrace strings
Sadik Armagan1625efc2021-06-10 18:24:34 +01002271 CHECK(!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceCharPolicy>("€£", buffer));
2272 CHECK(buffer.empty());
2273 CHECK(!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceCharPolicy>("invalid‡string", buffer));
2274 CHECK(buffer.empty());
2275 CHECK(!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceCharPolicy>("12Ž34", buffer));
2276 CHECK(buffer.empty());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002277}
2278
Sadik Armagan1625efc2021-06-10 18:24:34 +01002279TEST_CASE("StringToSwTraceNameStringTest")
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002280{
2281 std::vector<uint32_t> buffer;
2282
2283 // Valid SWTrace namestrings (expected size in words)
Jim Flynnbbfe6032020-07-20 16:57:44 +01002284 StringToSwTraceStringTestHelper<arm::pipe::SwTraceNameCharPolicy>("", buffer, 2);
2285 StringToSwTraceStringTestHelper<arm::pipe::SwTraceNameCharPolicy>("_", buffer, 2);
2286 StringToSwTraceStringTestHelper<arm::pipe::SwTraceNameCharPolicy>("0123", buffer, 3);
2287 StringToSwTraceStringTestHelper<arm::pipe::SwTraceNameCharPolicy>("valid_string", buffer, 5);
2288 StringToSwTraceStringTestHelper<arm::pipe::SwTraceNameCharPolicy>("VALID_string_456", buffer, 6);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002289
2290 // Invalid SWTrace namestrings
Sadik Armagan1625efc2021-06-10 18:24:34 +01002291 CHECK(!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceNameCharPolicy>(" ", buffer));
2292 CHECK(buffer.empty());
2293 CHECK(!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceNameCharPolicy>("invalid string", buffer));
2294 CHECK(buffer.empty());
2295 CHECK(!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceNameCharPolicy>("!$%", buffer));
2296 CHECK(buffer.empty());
2297 CHECK(!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceNameCharPolicy>("invalid|\\~string#123", buffer));
2298 CHECK(buffer.empty());
2299 CHECK(!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceNameCharPolicy>("€£", buffer));
2300 CHECK(buffer.empty());
2301 CHECK(!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceNameCharPolicy>("invalid‡string", buffer));
2302 CHECK(buffer.empty());
2303 CHECK(!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceNameCharPolicy>("12Ž34", buffer));
2304 CHECK(buffer.empty());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002305}
2306
Sadik Armagan1625efc2021-06-10 18:24:34 +01002307TEST_CASE("CheckPeriodicCounterCaptureThread")
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002308{
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002309 class CaptureReader : public IReadCounterValues
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002310 {
2311 public:
Finn Williamsf4d59a62019-10-14 15:55:18 +01002312 CaptureReader(uint16_t counterSize)
2313 {
Keith Davis3201eea2019-10-24 17:30:41 +01002314 for (uint16_t i = 0; i < counterSize; ++i)
Finn Williamsf4d59a62019-10-14 15:55:18 +01002315 {
2316 m_Data[i] = 0;
2317 }
2318 m_CounterSize = counterSize;
2319 }
2320 //not used
Matteo Martincighe8485382019-10-10 14:08:21 +01002321 bool IsCounterRegistered(uint16_t counterUid) const override
2322 {
Jan Eilers8eb25602020-03-09 12:13:48 +00002323 armnn::IgnoreUnused(counterUid);
Finn Williamsf4d59a62019-10-14 15:55:18 +01002324 return false;
Matteo Martincighe8485382019-10-10 14:08:21 +01002325 }
Jim Flynn34430252022-03-04 15:03:58 +00002326 bool IsCounterRegistered(const std::string& counterName) const override
2327 {
2328 armnn::IgnoreUnused(counterName);
2329 return false;
2330 }
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002331 uint16_t GetCounterCount() const override
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002332 {
Finn Williamsf4d59a62019-10-14 15:55:18 +01002333 return m_CounterSize;
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002334 }
2335
Finn Williamsf3fcf322020-05-11 14:38:02 +01002336 uint32_t GetAbsoluteCounterValue(uint16_t counterUid) const override
2337 {
2338 if (counterUid > m_CounterSize)
2339 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01002340 FAIL("Invalid counter Uid");
Finn Williamsf3fcf322020-05-11 14:38:02 +01002341 }
2342 return m_Data.at(counterUid).load();
2343 }
2344
2345 uint32_t GetDeltaCounterValue(uint16_t counterUid) override
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002346 {
Keith Davis3201eea2019-10-24 17:30:41 +01002347 if (counterUid > m_CounterSize)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002348 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01002349 FAIL("Invalid counter Uid");
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002350 }
Matteo Martincighe8485382019-10-10 14:08:21 +01002351 return m_Data.at(counterUid).load();
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002352 }
2353
Matteo Martincighe8485382019-10-10 14:08:21 +01002354 void SetCounterValue(uint16_t counterUid, uint32_t value)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002355 {
Keith Davis3201eea2019-10-24 17:30:41 +01002356 if (counterUid > m_CounterSize)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002357 {
Sadik Armagan1625efc2021-06-10 18:24:34 +01002358 FAIL("Invalid counter Uid");
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002359 }
Finn Williamsf4d59a62019-10-14 15:55:18 +01002360 m_Data.at(counterUid).store(value);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002361 }
2362
2363 private:
Matteo Martincighe8485382019-10-10 14:08:21 +01002364 std::unordered_map<uint16_t, std::atomic<uint32_t>> m_Data;
Finn Williamsf4d59a62019-10-14 15:55:18 +01002365 uint16_t m_CounterSize;
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002366 };
2367
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002368 ProfilingStateMachine profilingStateMachine;
2369
Cathal Corbett6f073722022-03-04 12:11:09 +00002370 const std::unordered_map<std::string,
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00002371 std::shared_ptr<IBackendProfilingContext>> backendProfilingContext;
Finn Williams032bc742020-02-12 11:02:34 +00002372 CounterIdMap counterIdMap;
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002373 Holder data;
2374 std::vector<uint16_t> captureIds1 = { 0, 1 };
2375 std::vector<uint16_t> captureIds2;
2376
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002377 MockBufferManager mockBuffer(512);
Sadik Armagan3896b472020-02-10 12:24:15 +00002378 SendCounterPacket sendCounterPacket(mockBuffer);
2379 SendThread sendThread(profilingStateMachine, mockBuffer, sendCounterPacket);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002380
2381 std::vector<uint16_t> counterIds;
Finn Williamsf4d59a62019-10-14 15:55:18 +01002382 CaptureReader captureReader(2);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002383
Keith Davis3201eea2019-10-24 17:30:41 +01002384 unsigned int valueA = 10;
2385 unsigned int valueB = 15;
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002386 unsigned int numSteps = 5;
2387
Finn Williams032bc742020-02-12 11:02:34 +00002388 PeriodicCounterCapture periodicCounterCapture(std::ref(data), std::ref(sendCounterPacket), captureReader,
2389 counterIdMap, backendProfilingContext);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002390
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002391 for (unsigned int i = 0; i < numSteps; ++i)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002392 {
Finn Williams032bc742020-02-12 11:02:34 +00002393 data.SetCaptureData(1, captureIds1, {});
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002394 captureReader.SetCounterValue(0, valueA * (i + 1));
2395 captureReader.SetCounterValue(1, valueB * (i + 1));
2396
2397 periodicCounterCapture.Start();
Finn Williamsf4d59a62019-10-14 15:55:18 +01002398 periodicCounterCapture.Stop();
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002399 }
2400
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002401 auto buffer = mockBuffer.GetReadableBuffer();
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002402
2403 uint32_t headerWord0 = ReadUint32(buffer, 0);
2404 uint32_t headerWord1 = ReadUint32(buffer, 4);
2405
Sadik Armagan1625efc2021-06-10 18:24:34 +01002406 CHECK(((headerWord0 >> 26) & 0x0000003F) == 3); // packet family
2407 CHECK(((headerWord0 >> 19) & 0x0000007F) == 0); // packet class
2408 CHECK(((headerWord0 >> 16) & 0x00000007) == 0); // packet type
2409 CHECK(headerWord1 == 20);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002410
Keith Davis3201eea2019-10-24 17:30:41 +01002411 uint32_t offset = 16;
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002412 uint16_t readIndex = ReadUint16(buffer, offset);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002413 CHECK(0 == readIndex);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002414
2415 offset += 2;
2416 uint32_t readValue = ReadUint32(buffer, offset);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002417 CHECK((valueA * numSteps) == readValue);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002418
2419 offset += 4;
2420 readIndex = ReadUint16(buffer, offset);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002421 CHECK(1 == readIndex);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002422
2423 offset += 2;
2424 readValue = ReadUint32(buffer, offset);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002425 CHECK((valueB * numSteps) == readValue);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002426}
2427
Sadik Armagan1625efc2021-06-10 18:24:34 +01002428TEST_CASE("RequestCounterDirectoryCommandHandlerTest1")
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002429{
Jim Flynn397043f2019-10-17 17:37:10 +01002430 const uint32_t familyId = 0;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002431 const uint32_t packetId = 3;
Keith Davis3201eea2019-10-24 17:30:41 +01002432 const uint32_t version = 1;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002433 ProfilingStateMachine profilingStateMachine;
2434 CounterDirectory counterDirectory;
Matteo Martincigh9723d022019-11-13 10:56:41 +00002435 MockBufferManager mockBuffer1(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002436 SendCounterPacket sendCounterPacket(mockBuffer1);
2437 SendThread sendThread(profilingStateMachine, mockBuffer1, sendCounterPacket);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002438 MockBufferManager mockBuffer2(1024);
2439 SendTimelinePacket sendTimelinePacket(mockBuffer2);
Keith Davis3201eea2019-10-24 17:30:41 +01002440 RequestCounterDirectoryCommandHandler commandHandler(familyId, packetId, version, counterDirectory,
Matteo Martincigh9723d022019-11-13 10:56:41 +00002441 sendCounterPacket, sendTimelinePacket, profilingStateMachine);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002442
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002443 const uint32_t wrongPacketId = 47;
Keith Davis3201eea2019-10-24 17:30:41 +01002444 const uint32_t wrongHeader = (wrongPacketId & 0x000003FF) << 16;
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002445
Jim Flynnbbfe6032020-07-20 16:57:44 +01002446 arm::pipe::Packet wrongPacket(wrongHeader);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002447
2448 profilingStateMachine.TransitionToState(ProfilingState::Uninitialised);
Jim Flynnf9db3ef2022-03-08 21:23:44 +00002449 CHECK_THROWS_AS(commandHandler(wrongPacket), arm::pipe::ProfilingException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002450 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
Jim Flynnf9db3ef2022-03-08 21:23:44 +00002451 CHECK_THROWS_AS(commandHandler(wrongPacket), arm::pipe::ProfilingException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002452 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
Jim Flynnf9db3ef2022-03-08 21:23:44 +00002453 CHECK_THROWS_AS(commandHandler(wrongPacket), arm::pipe::ProfilingException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002454 profilingStateMachine.TransitionToState(ProfilingState::Active);
Jim Flynnf9db3ef2022-03-08 21:23:44 +00002455 CHECK_THROWS_AS(commandHandler(wrongPacket), arm::pipe::InvalidArgumentException); // Wrong packet
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002456
2457 const uint32_t rightHeader = (packetId & 0x000003FF) << 16;
2458
Jim Flynnbbfe6032020-07-20 16:57:44 +01002459 arm::pipe::Packet rightPacket(rightHeader);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002460
Sadik Armagan1625efc2021-06-10 18:24:34 +01002461 CHECK_NOTHROW(commandHandler(rightPacket)); // Right packet
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002462
Matteo Martincigh9723d022019-11-13 10:56:41 +00002463 auto readBuffer1 = mockBuffer1.GetReadableBuffer();
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002464
Matteo Martincigh9723d022019-11-13 10:56:41 +00002465 uint32_t header1Word0 = ReadUint32(readBuffer1, 0);
2466 uint32_t header1Word1 = ReadUint32(readBuffer1, 4);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002467
Matteo Martincigh9723d022019-11-13 10:56:41 +00002468 // Counter directory packet
Sadik Armagan1625efc2021-06-10 18:24:34 +01002469 CHECK(((header1Word0 >> 26) & 0x0000003F) == 0); // packet family
2470 CHECK(((header1Word0 >> 16) & 0x000003FF) == 2); // packet id
2471 CHECK(header1Word1 == 24); // data length
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002472
Matteo Martincigh9723d022019-11-13 10:56:41 +00002473 uint32_t bodyHeader1Word0 = ReadUint32(readBuffer1, 8);
Jim Flynn75c14f42022-03-10 22:05:42 +00002474 uint16_t deviceRecordCount = arm::pipe::numeric_cast<uint16_t>(bodyHeader1Word0 >> 16);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002475 CHECK(deviceRecordCount == 0); // device_records_count
Matteo Martincigh9723d022019-11-13 10:56:41 +00002476
2477 auto readBuffer2 = mockBuffer2.GetReadableBuffer();
2478
2479 uint32_t header2Word0 = ReadUint32(readBuffer2, 0);
2480 uint32_t header2Word1 = ReadUint32(readBuffer2, 4);
2481
2482 // Timeline message directory packet
Sadik Armagan1625efc2021-06-10 18:24:34 +01002483 CHECK(((header2Word0 >> 26) & 0x0000003F) == 1); // packet family
2484 CHECK(((header2Word0 >> 16) & 0x000003FF) == 0); // packet id
2485 CHECK(header2Word1 == 443); // data length
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002486}
2487
Sadik Armagan1625efc2021-06-10 18:24:34 +01002488TEST_CASE("RequestCounterDirectoryCommandHandlerTest2")
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002489{
Jim Flynn397043f2019-10-17 17:37:10 +01002490 const uint32_t familyId = 0;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002491 const uint32_t packetId = 3;
Keith Davis3201eea2019-10-24 17:30:41 +01002492 const uint32_t version = 1;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002493 ProfilingStateMachine profilingStateMachine;
2494 CounterDirectory counterDirectory;
Matteo Martincigh9723d022019-11-13 10:56:41 +00002495 MockBufferManager mockBuffer1(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002496 SendCounterPacket sendCounterPacket(mockBuffer1);
2497 SendThread sendThread(profilingStateMachine, mockBuffer1, sendCounterPacket);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002498 MockBufferManager mockBuffer2(1024);
2499 SendTimelinePacket sendTimelinePacket(mockBuffer2);
Keith Davis3201eea2019-10-24 17:30:41 +01002500 RequestCounterDirectoryCommandHandler commandHandler(familyId, packetId, version, counterDirectory,
Matteo Martincigh9723d022019-11-13 10:56:41 +00002501 sendCounterPacket, sendTimelinePacket, profilingStateMachine);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002502 const uint32_t header = (packetId & 0x000003FF) << 16;
Jim Flynnbbfe6032020-07-20 16:57:44 +01002503 const arm::pipe::Packet packet(header);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002504
Matteo Martincigh9723d022019-11-13 10:56:41 +00002505 const Device* device = counterDirectory.RegisterDevice("deviceA", 1);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002506 CHECK(device != nullptr);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002507 const CounterSet* counterSet = counterDirectory.RegisterCounterSet("countersetA");
Sadik Armagan1625efc2021-06-10 18:24:34 +01002508 CHECK(counterSet != nullptr);
Sadik Armagan4c998992020-02-25 12:44:44 +00002509 counterDirectory.RegisterCategory("categoryA");
Keith Davise394bd92019-12-02 15:12:19 +00002510 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID, 24,
2511 "categoryA", 0, 1, 2.0f, "counterA", "descA");
2512 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID, 25,
2513 "categoryA", 1, 1, 3.0f, "counterB", "descB");
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002514
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002515 profilingStateMachine.TransitionToState(ProfilingState::Uninitialised);
Jim Flynnf9db3ef2022-03-08 21:23:44 +00002516 CHECK_THROWS_AS(commandHandler(packet), arm::pipe::ProfilingException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002517 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
Jim Flynnf9db3ef2022-03-08 21:23:44 +00002518 CHECK_THROWS_AS(commandHandler(packet), arm::pipe::ProfilingException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002519 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
Jim Flynnf9db3ef2022-03-08 21:23:44 +00002520 CHECK_THROWS_AS(commandHandler(packet), arm::pipe::ProfilingException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002521 profilingStateMachine.TransitionToState(ProfilingState::Active);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002522 CHECK_NOTHROW(commandHandler(packet));
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002523
Matteo Martincigh9723d022019-11-13 10:56:41 +00002524 auto readBuffer1 = mockBuffer1.GetReadableBuffer();
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002525
Finn Williams985fecf2020-04-30 11:06:43 +01002526 const uint32_t header1Word0 = ReadUint32(readBuffer1, 0);
2527 const uint32_t header1Word1 = ReadUint32(readBuffer1, 4);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002528
Sadik Armagan1625efc2021-06-10 18:24:34 +01002529 CHECK(((header1Word0 >> 26) & 0x0000003F) == 0); // packet family
2530 CHECK(((header1Word0 >> 16) & 0x000003FF) == 2); // packet id
2531 CHECK(header1Word1 == 236); // data length
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002532
Finn Williams985fecf2020-04-30 11:06:43 +01002533 const uint32_t bodyHeaderSizeBytes = bodyHeaderSize * sizeof(uint32_t);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002534
Finn Williams985fecf2020-04-30 11:06:43 +01002535 const uint32_t bodyHeader1Word0 = ReadUint32(readBuffer1, 8);
2536 const uint32_t bodyHeader1Word1 = ReadUint32(readBuffer1, 12);
2537 const uint32_t bodyHeader1Word2 = ReadUint32(readBuffer1, 16);
2538 const uint32_t bodyHeader1Word3 = ReadUint32(readBuffer1, 20);
2539 const uint32_t bodyHeader1Word4 = ReadUint32(readBuffer1, 24);
2540 const uint32_t bodyHeader1Word5 = ReadUint32(readBuffer1, 28);
Jim Flynn75c14f42022-03-10 22:05:42 +00002541 const uint16_t deviceRecordCount = arm::pipe::numeric_cast<uint16_t>(bodyHeader1Word0 >> 16);
2542 const uint16_t counterSetRecordCount = arm::pipe::numeric_cast<uint16_t>(bodyHeader1Word2 >> 16);
2543 const uint16_t categoryRecordCount = arm::pipe::numeric_cast<uint16_t>(bodyHeader1Word4 >> 16);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002544 CHECK(deviceRecordCount == 1); // device_records_count
2545 CHECK(bodyHeader1Word1 == 0 + bodyHeaderSizeBytes); // device_records_pointer_table_offset
2546 CHECK(counterSetRecordCount == 1); // counter_set_count
2547 CHECK(bodyHeader1Word3 == 4 + bodyHeaderSizeBytes); // counter_set_pointer_table_offset
2548 CHECK(categoryRecordCount == 1); // categories_count
2549 CHECK(bodyHeader1Word5 == 8 + bodyHeaderSizeBytes); // categories_pointer_table_offset
Finn Williams985fecf2020-04-30 11:06:43 +01002550
2551 const uint32_t deviceRecordOffset = ReadUint32(readBuffer1, 32);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002552 CHECK(deviceRecordOffset == 12);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002553
Finn Williams985fecf2020-04-30 11:06:43 +01002554 const uint32_t counterSetRecordOffset = ReadUint32(readBuffer1, 36);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002555 CHECK(counterSetRecordOffset == 28);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002556
Finn Williams985fecf2020-04-30 11:06:43 +01002557 const uint32_t categoryRecordOffset = ReadUint32(readBuffer1, 40);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002558 CHECK(categoryRecordOffset == 48);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002559
2560 auto readBuffer2 = mockBuffer2.GetReadableBuffer();
2561
Finn Williams985fecf2020-04-30 11:06:43 +01002562 const uint32_t header2Word0 = ReadUint32(readBuffer2, 0);
2563 const uint32_t header2Word1 = ReadUint32(readBuffer2, 4);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002564
2565 // Timeline message directory packet
Sadik Armagan1625efc2021-06-10 18:24:34 +01002566 CHECK(((header2Word0 >> 26) & 0x0000003F) == 1); // packet family
2567 CHECK(((header2Word0 >> 16) & 0x000003FF) == 0); // packet id
2568 CHECK(header2Word1 == 443); // data length
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002569}
2570
Sadik Armagan1625efc2021-06-10 18:24:34 +01002571TEST_CASE("CheckProfilingServiceGoodConnectionAcknowledgedPacket")
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002572{
Finn Williamsa0de0562020-04-22 12:27:37 +01002573 unsigned int streamMetadataPacketsize = GetStreamMetaDataPacketSize();
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002574
Jim Flynn53e46992019-10-14 12:31:10 +01002575 // Reset the profiling service to the uninitialized state
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00002576 ProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002577 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +00002578 armnn::ArmNNProfilingServiceInitialiser initialiser;
2579 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002580 profilingService.ResetExternalProfilingOptions(options, true);
2581
Sadik Armagan3184c902020-03-18 10:57:30 +00002582 // Swap the profiling connection factory in the profiling service instance with our mock one
Jim Flynn34430252022-03-04 15:03:58 +00002583 SwapProfilingConnectionFactoryHelper helper(arm::pipe::MAX_ARMNN_COUNTER, initialiser, profilingService);
Sadik Armagan3184c902020-03-18 10:57:30 +00002584
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002585 // Bring the profiling service to the "WaitingForAck" state
Sadik Armagan1625efc2021-06-10 18:24:34 +01002586 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002587 profilingService.Update(); // Initialize the counter directory
Sadik Armagan1625efc2021-06-10 18:24:34 +01002588 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002589 profilingService.Update(); // Create the profiling connection
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002590
Matteo Martincighd0613b52019-10-09 16:47:04 +01002591 // Get the mock profiling connection
2592 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
Sadik Armagan1625efc2021-06-10 18:24:34 +01002593 CHECK(mockProfilingConnection);
Matteo Martincighd0613b52019-10-09 16:47:04 +01002594
Matteo Martincighe8485382019-10-10 14:08:21 +01002595 // Remove the packets received so far
2596 mockProfilingConnection->Clear();
2597
Sadik Armagan1625efc2021-06-10 18:24:34 +01002598 CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002599 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002600
2601 // Wait for the Stream Metadata packet to be sent
Sadik Armagan1625efc2021-06-10 18:24:34 +01002602 CHECK(helper.WaitForPacketsSent(
Finn Williams09ad6f92019-12-19 17:05:18 +00002603 mockProfilingConnection, PacketType::StreamMetaData, streamMetadataPacketsize) >= 1);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002604
2605 // Write a valid "Connection Acknowledged" packet into the mock profiling connection, to simulate a valid
2606 // reply from an external profiling service
2607
2608 // Connection Acknowledged Packet header (word 0, word 1 is always zero):
2609 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2610 // 16:25 [10] packet_id: Packet identifier, value 0b0000000001
2611 // 8:15 [8] reserved: Reserved, value 0b00000000
2612 // 0:7 [8] reserved: Reserved, value 0b00000000
2613 uint32_t packetFamily = 0;
2614 uint32_t packetId = 1;
Keith Davis3201eea2019-10-24 17:30:41 +01002615 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002616
Matteo Martincighd0613b52019-10-09 16:47:04 +01002617 // Create the Connection Acknowledged Packet
Jim Flynnbbfe6032020-07-20 16:57:44 +01002618 arm::pipe::Packet connectionAcknowledgedPacket(header);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002619
2620 // Write the packet to the mock profiling connection
2621 mockProfilingConnection->WritePacket(std::move(connectionAcknowledgedPacket));
2622
Colm Donelan2ba48d22019-11-29 09:10:59 +00002623 // Wait for the counter directory packet to ensure the ConnectionAcknowledgedCommandHandler has run.
Sadik Armagan1625efc2021-06-10 18:24:34 +01002624 CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::CounterDirectory) == 1);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002625
2626 // The Connection Acknowledged Command Handler should have updated the profiling state accordingly
Sadik Armagan1625efc2021-06-10 18:24:34 +01002627 CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincighd0613b52019-10-09 16:47:04 +01002628
2629 // Reset the profiling service to stop any running thread
2630 options.m_EnableProfiling = false;
2631 profilingService.ResetExternalProfilingOptions(options, true);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002632}
2633
Sadik Armagan1625efc2021-06-10 18:24:34 +01002634TEST_CASE("CheckProfilingServiceGoodRequestCounterDirectoryPacket")
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002635{
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002636 // Reset the profiling service to the uninitialized state
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00002637 ProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002638 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +00002639 armnn::ArmNNProfilingServiceInitialiser initialiser;
2640 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002641 profilingService.ResetExternalProfilingOptions(options, true);
2642
Sadik Armagan3184c902020-03-18 10:57:30 +00002643 // Swap the profiling connection factory in the profiling service instance with our mock one
Jim Flynn34430252022-03-04 15:03:58 +00002644 SwapProfilingConnectionFactoryHelper helper(arm::pipe::MAX_ARMNN_COUNTER, initialiser, profilingService);
Sadik Armagan3184c902020-03-18 10:57:30 +00002645
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002646 // Bring the profiling service to the "Active" state
Sadik Armagan1625efc2021-06-10 18:24:34 +01002647 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002648 profilingService.Update(); // Initialize the counter directory
Sadik Armagan1625efc2021-06-10 18:24:34 +01002649 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002650 profilingService.Update(); // Create the profiling connection
Sadik Armagan1625efc2021-06-10 18:24:34 +01002651 CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002652 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002653
Colm Donelan2ba48d22019-11-29 09:10:59 +00002654 // Get the mock profiling connection
2655 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
Sadik Armagan1625efc2021-06-10 18:24:34 +01002656 CHECK(mockProfilingConnection);
Colm Donelan2ba48d22019-11-29 09:10:59 +00002657
Matteo Martincighe8485382019-10-10 14:08:21 +01002658 // Force the profiling service to the "Active" state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002659 helper.ForceTransitionToState(ProfilingState::Active);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002660 CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002661
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002662 // Write a valid "Request Counter Directory" packet into the mock profiling connection, to simulate a valid
2663 // reply from an external profiling service
2664
2665 // Request Counter Directory packet header (word 0, word 1 is always zero):
2666 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2667 // 16:25 [10] packet_id: Packet identifier, value 0b0000000011
2668 // 8:15 [8] reserved: Reserved, value 0b00000000
2669 // 0:7 [8] reserved: Reserved, value 0b00000000
2670 uint32_t packetFamily = 0;
2671 uint32_t packetId = 3;
Keith Davis3201eea2019-10-24 17:30:41 +01002672 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002673
2674 // Create the Request Counter Directory packet
Jim Flynnbbfe6032020-07-20 16:57:44 +01002675 arm::pipe::Packet requestCounterDirectoryPacket(header);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002676
2677 // Write the packet to the mock profiling connection
2678 mockProfilingConnection->WritePacket(std::move(requestCounterDirectoryPacket));
2679
Sadik Armagan4c998992020-02-25 12:44:44 +00002680 // Expecting one CounterDirectory Packet of length 652
Jim Flynn6398a982020-05-27 17:05:21 +01002681 // and one TimelineMessageDirectory packet of length 451
Sadik Armagan1625efc2021-06-10 18:24:34 +01002682 CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::CounterDirectory, 652) == 1);
2683 CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::TimelineMessageDirectory, 451) == 1);
Matteo Martincighe8485382019-10-10 14:08:21 +01002684
2685 // The Request Counter Directory Command Handler should not have updated the profiling state
Sadik Armagan1625efc2021-06-10 18:24:34 +01002686 CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincighe8485382019-10-10 14:08:21 +01002687
2688 // Reset the profiling service to stop any running thread
2689 options.m_EnableProfiling = false;
2690 profilingService.ResetExternalProfilingOptions(options, true);
2691}
2692
Sadik Armagan1625efc2021-06-10 18:24:34 +01002693TEST_CASE("CheckProfilingServiceBadPeriodicCounterSelectionPacketInvalidCounterUid")
Matteo Martincighe8485382019-10-10 14:08:21 +01002694{
Matteo Martincighe8485382019-10-10 14:08:21 +01002695 // Reset the profiling service to the uninitialized state
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00002696 ProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002697 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +00002698 armnn::ArmNNProfilingServiceInitialiser initialiser;
2699 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Matteo Martincighe8485382019-10-10 14:08:21 +01002700 profilingService.ResetExternalProfilingOptions(options, true);
2701
Sadik Armagan3184c902020-03-18 10:57:30 +00002702 // Swap the profiling connection factory in the profiling service instance with our mock one
Jim Flynn34430252022-03-04 15:03:58 +00002703 SwapProfilingConnectionFactoryHelper helper(arm::pipe::MAX_ARMNN_COUNTER, initialiser, profilingService);
Sadik Armagan3184c902020-03-18 10:57:30 +00002704
Matteo Martincighe8485382019-10-10 14:08:21 +01002705 // Bring the profiling service to the "Active" state
Sadik Armagan1625efc2021-06-10 18:24:34 +01002706 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002707 profilingService.Update(); // Initialize the counter directory
Sadik Armagan1625efc2021-06-10 18:24:34 +01002708 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002709 profilingService.Update(); // Create the profiling connection
Sadik Armagan1625efc2021-06-10 18:24:34 +01002710 CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002711 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002712
Colm Donelan2ba48d22019-11-29 09:10:59 +00002713 // Get the mock profiling connection
2714 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
Sadik Armagan1625efc2021-06-10 18:24:34 +01002715 CHECK(mockProfilingConnection);
Colm Donelan2ba48d22019-11-29 09:10:59 +00002716
Matteo Martincighe8485382019-10-10 14:08:21 +01002717 // Force the profiling service to the "Active" state
2718 helper.ForceTransitionToState(ProfilingState::Active);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002719 CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincighe8485382019-10-10 14:08:21 +01002720
Matteo Martincighe8485382019-10-10 14:08:21 +01002721 // Remove the packets received so far
2722 mockProfilingConnection->Clear();
2723
2724 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2725 // external profiling service
2726
2727 // Periodic Counter Selection packet header:
2728 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2729 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2730 // 8:15 [8] reserved: Reserved, value 0b00000000
2731 // 0:7 [8] reserved: Reserved, value 0b00000000
2732 uint32_t packetFamily = 0;
2733 uint32_t packetId = 4;
Keith Davis3201eea2019-10-24 17:30:41 +01002734 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincighe8485382019-10-10 14:08:21 +01002735
Keith Davis3201eea2019-10-24 17:30:41 +01002736 uint32_t capturePeriod = 123456; // Some capture period (microseconds)
Matteo Martincighe8485382019-10-10 14:08:21 +01002737
2738 // Get the first valid counter UID
2739 const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
Keith Davis3201eea2019-10-24 17:30:41 +01002740 const Counters& counters = counterDirectory.GetCounters();
Sadik Armagan1625efc2021-06-10 18:24:34 +01002741 CHECK(counters.size() > 1);
Keith Davis3201eea2019-10-24 17:30:41 +01002742 uint16_t counterUidA = counters.begin()->first; // First valid counter UID
2743 uint16_t counterUidB = 9999; // Second invalid counter UID
Matteo Martincighe8485382019-10-10 14:08:21 +01002744
2745 uint32_t length = 8;
2746
2747 auto data = std::make_unique<unsigned char[]>(length);
2748 WriteUint32(data.get(), 0, capturePeriod);
2749 WriteUint16(data.get(), 4, counterUidA);
2750 WriteUint16(data.get(), 6, counterUidB);
2751
2752 // Create the Periodic Counter Selection packet
Jim Flynnbbfe6032020-07-20 16:57:44 +01002753 // Length > 0, this will start the Period Counter Capture thread
2754 arm::pipe::Packet periodicCounterSelectionPacket(header, length, data);
2755
Matteo Martincighe8485382019-10-10 14:08:21 +01002756
2757 // Write the packet to the mock profiling connection
2758 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2759
Finn Williams09ad6f92019-12-19 17:05:18 +00002760 // Expecting one Periodic Counter Selection packet of length 14
2761 // and at least one Periodic Counter Capture packet of length 22
Sadik Armagan1625efc2021-06-10 18:24:34 +01002762 CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterSelection, 14) == 1);
2763 CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterCapture, 22) >= 1);
Matteo Martincighe8485382019-10-10 14:08:21 +01002764
2765 // The Periodic Counter Selection Handler should not have updated the profiling state
Sadik Armagan1625efc2021-06-10 18:24:34 +01002766 CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincighe8485382019-10-10 14:08:21 +01002767
2768 // Reset the profiling service to stop any running thread
2769 options.m_EnableProfiling = false;
2770 profilingService.ResetExternalProfilingOptions(options, true);
2771}
2772
Sadik Armagan1625efc2021-06-10 18:24:34 +01002773TEST_CASE("CheckProfilingServiceGoodPeriodicCounterSelectionPacketNoCounters")
Matteo Martincighe8485382019-10-10 14:08:21 +01002774{
Matteo Martincighe8485382019-10-10 14:08:21 +01002775 // Reset the profiling service to the uninitialized state
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00002776 ProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002777 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +00002778 armnn::ArmNNProfilingServiceInitialiser initialiser;
2779 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Matteo Martincighe8485382019-10-10 14:08:21 +01002780 profilingService.ResetExternalProfilingOptions(options, true);
2781
Sadik Armagan3184c902020-03-18 10:57:30 +00002782 // Swap the profiling connection factory in the profiling service instance with our mock one
Jim Flynn34430252022-03-04 15:03:58 +00002783 SwapProfilingConnectionFactoryHelper helper(arm::pipe::MAX_ARMNN_COUNTER, initialiser, profilingService);
Sadik Armagan3184c902020-03-18 10:57:30 +00002784
Matteo Martincighe8485382019-10-10 14:08:21 +01002785 // Bring the profiling service to the "Active" state
Sadik Armagan1625efc2021-06-10 18:24:34 +01002786 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002787 profilingService.Update(); // Initialize the counter directory
Sadik Armagan1625efc2021-06-10 18:24:34 +01002788 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002789 profilingService.Update(); // Create the profiling connection
Sadik Armagan1625efc2021-06-10 18:24:34 +01002790 CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002791 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002792
Colm Donelan2ba48d22019-11-29 09:10:59 +00002793 // Get the mock profiling connection
2794 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
Sadik Armagan1625efc2021-06-10 18:24:34 +01002795 CHECK(mockProfilingConnection);
Colm Donelan2ba48d22019-11-29 09:10:59 +00002796
Matteo Martincighe8485382019-10-10 14:08:21 +01002797 // Wait for the Stream Metadata packet the be sent
2798 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Finn Williams09ad6f92019-12-19 17:05:18 +00002799 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData);
Matteo Martincighe8485382019-10-10 14:08:21 +01002800
2801 // Force the profiling service to the "Active" state
2802 helper.ForceTransitionToState(ProfilingState::Active);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002803 CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincighe8485382019-10-10 14:08:21 +01002804
Matteo Martincighe8485382019-10-10 14:08:21 +01002805 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2806 // external profiling service
2807
2808 // Periodic Counter Selection packet header:
2809 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2810 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2811 // 8:15 [8] reserved: Reserved, value 0b00000000
2812 // 0:7 [8] reserved: Reserved, value 0b00000000
2813 uint32_t packetFamily = 0;
2814 uint32_t packetId = 4;
Keith Davis3201eea2019-10-24 17:30:41 +01002815 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincighe8485382019-10-10 14:08:21 +01002816
2817 // Create the Periodic Counter Selection packet
Jim Flynnbbfe6032020-07-20 16:57:44 +01002818 // Length == 0, this will disable the collection of counters
2819 arm::pipe::Packet periodicCounterSelectionPacket(header);
Matteo Martincighe8485382019-10-10 14:08:21 +01002820
2821 // Write the packet to the mock profiling connection
2822 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2823
Finn Williams09ad6f92019-12-19 17:05:18 +00002824 // Wait for the Periodic Counter Selection packet of length 12 to be sent
2825 // The size of the expected Periodic Counter Selection (echos the sent one)
Sadik Armagan1625efc2021-06-10 18:24:34 +01002826 CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterSelection, 12) == 1);
Matteo Martincighe8485382019-10-10 14:08:21 +01002827
2828 // The Periodic Counter Selection Handler should not have updated the profiling state
Sadik Armagan1625efc2021-06-10 18:24:34 +01002829 CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincighe8485382019-10-10 14:08:21 +01002830
Finn Williams09ad6f92019-12-19 17:05:18 +00002831 // No Periodic Counter packets are expected
Sadik Armagan1625efc2021-06-10 18:24:34 +01002832 CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterCapture, 0, 0) == 0);
Matteo Martincighe8485382019-10-10 14:08:21 +01002833
2834 // Reset the profiling service to stop any running thread
2835 options.m_EnableProfiling = false;
2836 profilingService.ResetExternalProfilingOptions(options, true);
2837}
2838
Sadik Armagan1625efc2021-06-10 18:24:34 +01002839TEST_CASE("CheckProfilingServiceGoodPeriodicCounterSelectionPacketSingleCounter")
Matteo Martincighe8485382019-10-10 14:08:21 +01002840{
Matteo Martincighe8485382019-10-10 14:08:21 +01002841 // Reset the profiling service to the uninitialized state
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00002842 ProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002843 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +00002844 armnn::ArmNNProfilingServiceInitialiser initialiser;
2845 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Matteo Martincighe8485382019-10-10 14:08:21 +01002846 profilingService.ResetExternalProfilingOptions(options, true);
2847
Sadik Armagan3184c902020-03-18 10:57:30 +00002848 // Swap the profiling connection factory in the profiling service instance with our mock one
Jim Flynn34430252022-03-04 15:03:58 +00002849 SwapProfilingConnectionFactoryHelper helper(arm::pipe::MAX_ARMNN_COUNTER, initialiser, profilingService);
Sadik Armagan3184c902020-03-18 10:57:30 +00002850
Matteo Martincighe8485382019-10-10 14:08:21 +01002851 // Bring the profiling service to the "Active" state
Sadik Armagan1625efc2021-06-10 18:24:34 +01002852 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002853 profilingService.Update(); // Initialize the counter directory
Sadik Armagan1625efc2021-06-10 18:24:34 +01002854 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002855 profilingService.Update(); // Create the profiling connection
Sadik Armagan1625efc2021-06-10 18:24:34 +01002856 CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002857 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002858
Colm Donelan2ba48d22019-11-29 09:10:59 +00002859 // Get the mock profiling connection
2860 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
Sadik Armagan1625efc2021-06-10 18:24:34 +01002861 CHECK(mockProfilingConnection);
Colm Donelan2ba48d22019-11-29 09:10:59 +00002862
Finn Williams09ad6f92019-12-19 17:05:18 +00002863 // Wait for the Stream Metadata packet to be sent
Matteo Martincighe8485382019-10-10 14:08:21 +01002864 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Finn Williams09ad6f92019-12-19 17:05:18 +00002865 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData);
Matteo Martincighe8485382019-10-10 14:08:21 +01002866
2867 // Force the profiling service to the "Active" state
2868 helper.ForceTransitionToState(ProfilingState::Active);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002869 CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincighe8485382019-10-10 14:08:21 +01002870
Matteo Martincighe8485382019-10-10 14:08:21 +01002871 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2872 // external profiling service
2873
2874 // Periodic Counter Selection packet header:
2875 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2876 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2877 // 8:15 [8] reserved: Reserved, value 0b00000000
2878 // 0:7 [8] reserved: Reserved, value 0b00000000
2879 uint32_t packetFamily = 0;
2880 uint32_t packetId = 4;
Keith Davis3201eea2019-10-24 17:30:41 +01002881 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincighe8485382019-10-10 14:08:21 +01002882
Keith Davis3201eea2019-10-24 17:30:41 +01002883 uint32_t capturePeriod = 123456; // Some capture period (microseconds)
Matteo Martincighe8485382019-10-10 14:08:21 +01002884
2885 // Get the first valid counter UID
2886 const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
Keith Davis3201eea2019-10-24 17:30:41 +01002887 const Counters& counters = counterDirectory.GetCounters();
Sadik Armagan1625efc2021-06-10 18:24:34 +01002888 CHECK(!counters.empty());
Keith Davis3201eea2019-10-24 17:30:41 +01002889 uint16_t counterUid = counters.begin()->first; // Valid counter UID
Matteo Martincighe8485382019-10-10 14:08:21 +01002890
2891 uint32_t length = 6;
2892
2893 auto data = std::make_unique<unsigned char[]>(length);
2894 WriteUint32(data.get(), 0, capturePeriod);
2895 WriteUint16(data.get(), 4, counterUid);
2896
2897 // Create the Periodic Counter Selection packet
Jim Flynnbbfe6032020-07-20 16:57:44 +01002898 // Length > 0, this will start the Period Counter Capture thread
2899 arm::pipe::Packet periodicCounterSelectionPacket(header, length, data);
Matteo Martincighe8485382019-10-10 14:08:21 +01002900
2901 // Write the packet to the mock profiling connection
2902 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2903
Finn Williams09ad6f92019-12-19 17:05:18 +00002904 // Expecting one Periodic Counter Selection packet of length 14
2905 // and at least one Periodic Counter Capture packet of length 22
Sadik Armagan1625efc2021-06-10 18:24:34 +01002906 CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterSelection, 14) == 1);
2907 CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterCapture, 22) >= 1);
Matteo Martincighe8485382019-10-10 14:08:21 +01002908
2909 // The Periodic Counter Selection Handler should not have updated the profiling state
Sadik Armagan1625efc2021-06-10 18:24:34 +01002910 CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincighe8485382019-10-10 14:08:21 +01002911
2912 // Reset the profiling service to stop any running thread
2913 options.m_EnableProfiling = false;
2914 profilingService.ResetExternalProfilingOptions(options, true);
2915}
2916
Sadik Armagan1625efc2021-06-10 18:24:34 +01002917TEST_CASE("CheckProfilingServiceGoodPeriodicCounterSelectionPacketMultipleCounters")
Matteo Martincighe8485382019-10-10 14:08:21 +01002918{
Matteo Martincighe8485382019-10-10 14:08:21 +01002919 // Reset the profiling service to the uninitialized state
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00002920 ProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002921 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +00002922 armnn::ArmNNProfilingServiceInitialiser initialiser;
2923 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Matteo Martincighe8485382019-10-10 14:08:21 +01002924 profilingService.ResetExternalProfilingOptions(options, true);
2925
Sadik Armagan3184c902020-03-18 10:57:30 +00002926 // Swap the profiling connection factory in the profiling service instance with our mock one
Jim Flynn34430252022-03-04 15:03:58 +00002927 SwapProfilingConnectionFactoryHelper helper(arm::pipe::MAX_ARMNN_COUNTER, initialiser, profilingService);
Sadik Armagan3184c902020-03-18 10:57:30 +00002928
Matteo Martincighe8485382019-10-10 14:08:21 +01002929 // Bring the profiling service to the "Active" state
Sadik Armagan1625efc2021-06-10 18:24:34 +01002930 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002931 profilingService.Update(); // Initialize the counter directory
Sadik Armagan1625efc2021-06-10 18:24:34 +01002932 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002933 profilingService.Update(); // Create the profiling connection
Sadik Armagan1625efc2021-06-10 18:24:34 +01002934 CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002935 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002936
Colm Donelan2ba48d22019-11-29 09:10:59 +00002937 // Get the mock profiling connection
2938 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
Sadik Armagan1625efc2021-06-10 18:24:34 +01002939 CHECK(mockProfilingConnection);
Colm Donelan2ba48d22019-11-29 09:10:59 +00002940
Matteo Martincighe8485382019-10-10 14:08:21 +01002941 // Wait for the Stream Metadata packet the be sent
2942 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Finn Williams09ad6f92019-12-19 17:05:18 +00002943 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData);
Matteo Martincighe8485382019-10-10 14:08:21 +01002944
2945 // Force the profiling service to the "Active" state
2946 helper.ForceTransitionToState(ProfilingState::Active);
Sadik Armagan1625efc2021-06-10 18:24:34 +01002947 CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincighe8485382019-10-10 14:08:21 +01002948
Matteo Martincighe8485382019-10-10 14:08:21 +01002949 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2950 // external profiling service
2951
2952 // Periodic Counter Selection packet header:
2953 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2954 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2955 // 8:15 [8] reserved: Reserved, value 0b00000000
2956 // 0:7 [8] reserved: Reserved, value 0b00000000
2957 uint32_t packetFamily = 0;
2958 uint32_t packetId = 4;
Keith Davis3201eea2019-10-24 17:30:41 +01002959 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincighe8485382019-10-10 14:08:21 +01002960
Keith Davis3201eea2019-10-24 17:30:41 +01002961 uint32_t capturePeriod = 123456; // Some capture period (microseconds)
Matteo Martincighe8485382019-10-10 14:08:21 +01002962
2963 // Get the first valid counter UID
2964 const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
Keith Davis3201eea2019-10-24 17:30:41 +01002965 const Counters& counters = counterDirectory.GetCounters();
Sadik Armagan1625efc2021-06-10 18:24:34 +01002966 CHECK(counters.size() > 1);
Keith Davis3201eea2019-10-24 17:30:41 +01002967 uint16_t counterUidA = counters.begin()->first; // First valid counter UID
2968 uint16_t counterUidB = (counters.begin()++)->first; // Second valid counter UID
Matteo Martincighe8485382019-10-10 14:08:21 +01002969
2970 uint32_t length = 8;
2971
2972 auto data = std::make_unique<unsigned char[]>(length);
2973 WriteUint32(data.get(), 0, capturePeriod);
2974 WriteUint16(data.get(), 4, counterUidA);
2975 WriteUint16(data.get(), 6, counterUidB);
2976
2977 // Create the Periodic Counter Selection packet
Jim Flynnbbfe6032020-07-20 16:57:44 +01002978 // Length > 0, this will start the Period Counter Capture thread
2979 arm::pipe::Packet periodicCounterSelectionPacket(header, length, data);
Matteo Martincighe8485382019-10-10 14:08:21 +01002980
2981 // Write the packet to the mock profiling connection
2982 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2983
Finn Williams09ad6f92019-12-19 17:05:18 +00002984 // Expecting one PeriodicCounterSelection Packet with a length of 16
2985 // And at least one PeriodicCounterCapture Packet with a length of 28
Sadik Armagan1625efc2021-06-10 18:24:34 +01002986 CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterSelection, 16) == 1);
2987 CHECK(helper.WaitForPacketsSent(mockProfilingConnection, PacketType::PeriodicCounterCapture, 28) >= 1);
Matteo Martincighe8485382019-10-10 14:08:21 +01002988
2989 // The Periodic Counter Selection Handler should not have updated the profiling state
Sadik Armagan1625efc2021-06-10 18:24:34 +01002990 CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002991
2992 // Reset the profiling service to stop any running thread
2993 options.m_EnableProfiling = false;
2994 profilingService.ResetExternalProfilingOptions(options, true);
2995}
2996
Sadik Armagan1625efc2021-06-10 18:24:34 +01002997TEST_CASE("CheckProfilingServiceDisconnect")
Jim Flynn53e46992019-10-14 12:31:10 +01002998{
Jim Flynn53e46992019-10-14 12:31:10 +01002999 // Reset the profiling service to the uninitialized state
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003000 ProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01003001 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +00003002 armnn::ArmNNProfilingServiceInitialiser initialiser;
3003 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Jim Flynn53e46992019-10-14 12:31:10 +01003004 profilingService.ResetExternalProfilingOptions(options, true);
3005
Sadik Armagan3184c902020-03-18 10:57:30 +00003006 // Swap the profiling connection factory in the profiling service instance with our mock one
Jim Flynn34430252022-03-04 15:03:58 +00003007 SwapProfilingConnectionFactoryHelper helper(arm::pipe::MAX_ARMNN_COUNTER, initialiser, profilingService);
Sadik Armagan3184c902020-03-18 10:57:30 +00003008
Jim Flynn53e46992019-10-14 12:31:10 +01003009 // Try to disconnect the profiling service while in the "Uninitialised" state
Sadik Armagan1625efc2021-06-10 18:24:34 +01003010 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Jim Flynn53e46992019-10-14 12:31:10 +01003011 profilingService.Disconnect();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003012 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised); // The state should not change
Jim Flynn53e46992019-10-14 12:31:10 +01003013
3014 // Try to disconnect the profiling service while in the "NotConnected" state
Keith Davis3201eea2019-10-24 17:30:41 +01003015 profilingService.Update(); // Initialize the counter directory
Sadik Armagan1625efc2021-06-10 18:24:34 +01003016 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Jim Flynn53e46992019-10-14 12:31:10 +01003017 profilingService.Disconnect();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003018 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected); // The state should not change
Jim Flynn53e46992019-10-14 12:31:10 +01003019
3020 // Try to disconnect the profiling service while in the "WaitingForAck" state
Keith Davis3201eea2019-10-24 17:30:41 +01003021 profilingService.Update(); // Create the profiling connection
Sadik Armagan1625efc2021-06-10 18:24:34 +01003022 CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Jim Flynn53e46992019-10-14 12:31:10 +01003023 profilingService.Disconnect();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003024 CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck); // The state should not change
Jim Flynn53e46992019-10-14 12:31:10 +01003025
3026 // Try to disconnect the profiling service while in the "Active" state
Keith Davis3201eea2019-10-24 17:30:41 +01003027 profilingService.Update(); // Start the command handler and the send thread
Jim Flynn53e46992019-10-14 12:31:10 +01003028
Colm Donelan2ba48d22019-11-29 09:10:59 +00003029 // Get the mock profiling connection
3030 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003031 CHECK(mockProfilingConnection);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003032
Jim Flynn53e46992019-10-14 12:31:10 +01003033 // Wait for the Stream Metadata packet the be sent
3034 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Finn Williams09ad6f92019-12-19 17:05:18 +00003035 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData);
Jim Flynn53e46992019-10-14 12:31:10 +01003036
3037 // Force the profiling service to the "Active" state
3038 helper.ForceTransitionToState(ProfilingState::Active);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003039 CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Jim Flynn53e46992019-10-14 12:31:10 +01003040
Jim Flynn53e46992019-10-14 12:31:10 +01003041 // Check that the profiling connection is open
Sadik Armagan1625efc2021-06-10 18:24:34 +01003042 CHECK(mockProfilingConnection->IsOpen());
Jim Flynn53e46992019-10-14 12:31:10 +01003043
3044 profilingService.Disconnect();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003045 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected); // The state should have changed
Jim Flynn53e46992019-10-14 12:31:10 +01003046
3047 // Check that the profiling connection has been reset
3048 mockProfilingConnection = helper.GetMockProfilingConnection();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003049 CHECK(mockProfilingConnection == nullptr);
Jim Flynn53e46992019-10-14 12:31:10 +01003050
3051 // Reset the profiling service to stop any running thread
3052 options.m_EnableProfiling = false;
3053 profilingService.ResetExternalProfilingOptions(options, true);
3054}
3055
Sadik Armagan1625efc2021-06-10 18:24:34 +01003056TEST_CASE("CheckProfilingServiceGoodPerJobCounterSelectionPacket")
Matteo Martincigh994b5342019-10-11 17:19:56 +01003057{
Matteo Martincigh994b5342019-10-11 17:19:56 +01003058 // Reset the profiling service to the uninitialized state
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003059 ProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01003060 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +00003061 armnn::ArmNNProfilingServiceInitialiser initialiser;
3062 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Matteo Martincigh994b5342019-10-11 17:19:56 +01003063 profilingService.ResetExternalProfilingOptions(options, true);
3064
Sadik Armagan3184c902020-03-18 10:57:30 +00003065 // Swap the profiling connection factory in the profiling service instance with our mock one
Jim Flynn34430252022-03-04 15:03:58 +00003066 SwapProfilingConnectionFactoryHelper helper(arm::pipe::MAX_ARMNN_COUNTER, initialiser, profilingService);
Sadik Armagan3184c902020-03-18 10:57:30 +00003067
Matteo Martincigh994b5342019-10-11 17:19:56 +01003068 // Bring the profiling service to the "Active" state
Sadik Armagan1625efc2021-06-10 18:24:34 +01003069 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01003070 profilingService.Update(); // Initialize the counter directory
Sadik Armagan1625efc2021-06-10 18:24:34 +01003071 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01003072 profilingService.Update(); // Create the profiling connection
Sadik Armagan1625efc2021-06-10 18:24:34 +01003073 CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01003074 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincigh994b5342019-10-11 17:19:56 +01003075
Colm Donelan2ba48d22019-11-29 09:10:59 +00003076 // Get the mock profiling connection
3077 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003078 CHECK(mockProfilingConnection);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003079
Matteo Martincigh994b5342019-10-11 17:19:56 +01003080 // Wait for the Stream Metadata packet the be sent
3081 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Finn Williams09ad6f92019-12-19 17:05:18 +00003082 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData);
Matteo Martincigh994b5342019-10-11 17:19:56 +01003083
3084 // Force the profiling service to the "Active" state
3085 helper.ForceTransitionToState(ProfilingState::Active);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003086 CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincigh994b5342019-10-11 17:19:56 +01003087
Matteo Martincigh994b5342019-10-11 17:19:56 +01003088 // Write a "Per-Job Counter Selection" packet into the mock profiling connection, to simulate an input from an
3089 // external profiling service
3090
3091 // Per-Job Counter Selection packet header:
3092 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
3093 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
3094 // 8:15 [8] reserved: Reserved, value 0b00000000
3095 // 0:7 [8] reserved: Reserved, value 0b00000000
3096 uint32_t packetFamily = 0;
3097 uint32_t packetId = 5;
Keith Davis3201eea2019-10-24 17:30:41 +01003098 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincigh994b5342019-10-11 17:19:56 +01003099
3100 // Create the Per-Job Counter Selection packet
Jim Flynnbbfe6032020-07-20 16:57:44 +01003101 // Length == 0, this will disable the collection of counters
3102 arm::pipe::Packet periodicCounterSelectionPacket(header);
Matteo Martincigh994b5342019-10-11 17:19:56 +01003103
3104 // Write the packet to the mock profiling connection
3105 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
3106
3107 // Wait for a bit (must at least be the delay value of the mock profiling connection) to make sure that
3108 // the Per-Job Counter Selection packet gets processed by the profiling service
Colm Donelan2ba48d22019-11-29 09:10:59 +00003109 std::this_thread::sleep_for(std::chrono::milliseconds(5));
Matteo Martincigh994b5342019-10-11 17:19:56 +01003110
Matteo Martincigh994b5342019-10-11 17:19:56 +01003111 // The Per-Job Counter Selection Command Handler should not have updated the profiling state
Sadik Armagan1625efc2021-06-10 18:24:34 +01003112 CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincigh994b5342019-10-11 17:19:56 +01003113
Finn Williams09ad6f92019-12-19 17:05:18 +00003114 // The Per-Job Counter Selection packets are dropped silently, so there should be no reply coming
3115 // from the profiling service
3116 const auto StreamMetaDataSize = static_cast<unsigned long>(
3117 helper.WaitForPacketsSent(mockProfilingConnection, PacketType::StreamMetaData, 0, 0));
Sadik Armagan1625efc2021-06-10 18:24:34 +01003118 CHECK(StreamMetaDataSize == mockProfilingConnection->GetWrittenDataSize());
Finn Williams09ad6f92019-12-19 17:05:18 +00003119
Matteo Martincigh994b5342019-10-11 17:19:56 +01003120 // Reset the profiling service to stop any running thread
3121 options.m_EnableProfiling = false;
3122 profilingService.ResetExternalProfilingOptions(options, true);
3123}
3124
Sadik Armagan1625efc2021-06-10 18:24:34 +01003125TEST_CASE("CheckConfigureProfilingServiceOn")
Jim Flynn672d06e2019-10-15 10:18:11 +01003126{
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003127 ProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01003128 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +00003129 armnn::ArmNNProfilingServiceInitialiser initialiser;
3130 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003131 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Jim Flynn672d06e2019-10-15 10:18:11 +01003132 profilingService.ConfigureProfilingService(options);
3133 // should get as far as NOT_CONNECTED
Sadik Armagan1625efc2021-06-10 18:24:34 +01003134 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Jim Flynn672d06e2019-10-15 10:18:11 +01003135 // Reset the profiling service to stop any running thread
3136 options.m_EnableProfiling = false;
3137 profilingService.ResetExternalProfilingOptions(options, true);
3138}
3139
Sadik Armagan1625efc2021-06-10 18:24:34 +01003140TEST_CASE("CheckConfigureProfilingServiceOff")
Jim Flynn672d06e2019-10-15 10:18:11 +01003141{
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003142 ProfilingOptions options;
Jim Flynn34430252022-03-04 15:03:58 +00003143 armnn::ArmNNProfilingServiceInitialiser initialiser;
3144 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003145 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Jim Flynn672d06e2019-10-15 10:18:11 +01003146 profilingService.ConfigureProfilingService(options);
3147 // should not move from Uninitialised
Sadik Armagan1625efc2021-06-10 18:24:34 +01003148 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Jim Flynn672d06e2019-10-15 10:18:11 +01003149 // Reset the profiling service to stop any running thread
3150 options.m_EnableProfiling = false;
3151 profilingService.ResetExternalProfilingOptions(options, true);
3152}
3153
Sadik Armagan1625efc2021-06-10 18:24:34 +01003154TEST_CASE("CheckProfilingServiceEnabled")
Colm Donelan2ba48d22019-11-29 09:10:59 +00003155{
3156 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
3157 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003158 ProfilingOptions options;
Colm Donelan2ba48d22019-11-29 09:10:59 +00003159 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +00003160 armnn::ArmNNProfilingServiceInitialiser initialiser;
3161 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003162 profilingService.ResetExternalProfilingOptions(options, true);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003163 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003164 profilingService.Update();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003165 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003166
3167 // Redirect the output to a local stream so that we can parse the warning message
3168 std::stringstream ss;
3169 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3170 profilingService.Update();
Finn Williams09ad6f92019-12-19 17:05:18 +00003171
3172 // Reset the profiling service to stop any running thread
3173 options.m_EnableProfiling = false;
3174 profilingService.ResetExternalProfilingOptions(options, true);
3175
Colm Donelan2ba48d22019-11-29 09:10:59 +00003176 streamRedirector.CancelRedirect();
3177
3178 // Check that the expected error has occurred and logged to the standard output
David Monahana8837bf2020-04-16 10:01:56 +01003179 if (ss.str().find("Cannot connect to stream socket: Connection refused") == std::string::npos)
Colm Donelan2ba48d22019-11-29 09:10:59 +00003180 {
3181 std::cout << ss.str();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003182 FAIL("Expected string not found.");
Colm Donelan2ba48d22019-11-29 09:10:59 +00003183 }
Colm Donelan2ba48d22019-11-29 09:10:59 +00003184}
3185
Sadik Armagan1625efc2021-06-10 18:24:34 +01003186TEST_CASE("CheckProfilingServiceEnabledRuntime")
Colm Donelan2ba48d22019-11-29 09:10:59 +00003187{
3188 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
3189 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003190 ProfilingOptions options;
Jim Flynn34430252022-03-04 15:03:58 +00003191 armnn::ArmNNProfilingServiceInitialiser initialiser;
3192 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003193 profilingService.ResetExternalProfilingOptions(options, true);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003194 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003195 profilingService.Update();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003196 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003197 options.m_EnableProfiling = true;
3198 profilingService.ResetExternalProfilingOptions(options);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003199 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003200 profilingService.Update();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003201 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003202
3203 // Redirect the output to a local stream so that we can parse the warning message
3204 std::stringstream ss;
3205 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3206 profilingService.Update();
3207
Finn Williams09ad6f92019-12-19 17:05:18 +00003208 // Reset the profiling service to stop any running thread
3209 options.m_EnableProfiling = false;
3210 profilingService.ResetExternalProfilingOptions(options, true);
3211
Colm Donelan2ba48d22019-11-29 09:10:59 +00003212 streamRedirector.CancelRedirect();
3213
3214 // Check that the expected error has occurred and logged to the standard output
David Monahana8837bf2020-04-16 10:01:56 +01003215 if (ss.str().find("Cannot connect to stream socket: Connection refused") == std::string::npos)
Colm Donelan2ba48d22019-11-29 09:10:59 +00003216 {
3217 std::cout << ss.str();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003218 FAIL("Expected string not found.");
Colm Donelan2ba48d22019-11-29 09:10:59 +00003219 }
Colm Donelan2ba48d22019-11-29 09:10:59 +00003220}
3221
Sadik Armagan1625efc2021-06-10 18:24:34 +01003222TEST_CASE("CheckProfilingServiceBadConnectionAcknowledgedPacket")
Colm Donelan2ba48d22019-11-29 09:10:59 +00003223{
3224 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
3225 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
Sadik Armagan3184c902020-03-18 10:57:30 +00003226
Colm Donelan2ba48d22019-11-29 09:10:59 +00003227
3228 // Redirect the standard output to a local stream so that we can parse the warning message
3229 std::stringstream ss;
3230 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3231
Colm Donelan2ba48d22019-11-29 09:10:59 +00003232 // Reset the profiling service to the uninitialized state
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003233 ProfilingOptions options;
Colm Donelan2ba48d22019-11-29 09:10:59 +00003234 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +00003235 armnn::ArmNNProfilingServiceInitialiser initialiser;
3236 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003237 profilingService.ResetExternalProfilingOptions(options, true);
3238
Sadik Armagan3184c902020-03-18 10:57:30 +00003239 // Swap the profiling connection factory in the profiling service instance with our mock one
Jim Flynn34430252022-03-04 15:03:58 +00003240 SwapProfilingConnectionFactoryHelper helper(arm::pipe::MAX_ARMNN_COUNTER, initialiser, profilingService);
Sadik Armagan3184c902020-03-18 10:57:30 +00003241
Colm Donelan2ba48d22019-11-29 09:10:59 +00003242 // Bring the profiling service to the "WaitingForAck" state
Sadik Armagan1625efc2021-06-10 18:24:34 +01003243 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003244 profilingService.Update(); // Initialize the counter directory
Sadik Armagan1625efc2021-06-10 18:24:34 +01003245 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003246 profilingService.Update(); // Create the profiling connection
3247
3248 // Get the mock profiling connection
3249 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003250 CHECK(mockProfilingConnection);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003251
Sadik Armagan1625efc2021-06-10 18:24:34 +01003252 CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003253
3254 // Connection Acknowledged Packet header (word 0, word 1 is always zero):
3255 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
3256 // 16:25 [10] packet_id: Packet identifier, value 0b0000000001
3257 // 8:15 [8] reserved: Reserved, value 0b00000000
3258 // 0:7 [8] reserved: Reserved, value 0b00000000
3259 uint32_t packetFamily = 0;
3260 uint32_t packetId = 37; // Wrong packet id!!!
3261 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
3262
3263 // Create the Connection Acknowledged Packet
Jim Flynnbbfe6032020-07-20 16:57:44 +01003264 arm::pipe::Packet connectionAcknowledgedPacket(header);
Finn Williams09ad6f92019-12-19 17:05:18 +00003265 // Write an invalid "Connection Acknowledged" packet into the mock profiling connection, to simulate an invalid
3266 // reply from an external profiling service
Colm Donelan2ba48d22019-11-29 09:10:59 +00003267 mockProfilingConnection->WritePacket(std::move(connectionAcknowledgedPacket));
3268
Finn Williams09ad6f92019-12-19 17:05:18 +00003269 // Start the command thread
3270 profilingService.Update();
3271
3272 // Wait for the command thread to join
3273 options.m_EnableProfiling = false;
3274 profilingService.ResetExternalProfilingOptions(options, true);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003275
3276 streamRedirector.CancelRedirect();
3277
3278 // Check that the expected error has occurred and logged to the standard output
David Monahana8837bf2020-04-16 10:01:56 +01003279 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 +00003280 {
3281 std::cout << ss.str();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003282 FAIL("Expected string not found.");
Colm Donelan2ba48d22019-11-29 09:10:59 +00003283 }
Colm Donelan2ba48d22019-11-29 09:10:59 +00003284}
3285
Sadik Armagan1625efc2021-06-10 18:24:34 +01003286TEST_CASE("CheckProfilingServiceBadRequestCounterDirectoryPacket")
Colm Donelan2ba48d22019-11-29 09:10:59 +00003287{
3288 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
3289 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003290
3291 // Redirect the standard output to a local stream so that we can parse the warning message
3292 std::stringstream ss;
3293 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3294
3295 // Reset the profiling service to the uninitialized state
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003296 ProfilingOptions options;
Colm Donelan2ba48d22019-11-29 09:10:59 +00003297 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +00003298 armnn::ArmNNProfilingServiceInitialiser initialiser;
3299 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003300 profilingService.ResetExternalProfilingOptions(options, true);
3301
Sadik Armagan3184c902020-03-18 10:57:30 +00003302 // Swap the profiling connection factory in the profiling service instance with our mock one
Jim Flynn34430252022-03-04 15:03:58 +00003303 SwapProfilingConnectionFactoryHelper helper(arm::pipe::MAX_ARMNN_COUNTER, initialiser, profilingService);
Sadik Armagan3184c902020-03-18 10:57:30 +00003304
Colm Donelan2ba48d22019-11-29 09:10:59 +00003305 // Bring the profiling service to the "Active" state
Sadik Armagan1625efc2021-06-10 18:24:34 +01003306 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003307 helper.ForceTransitionToState(ProfilingState::NotConnected);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003308 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003309 profilingService.Update(); // Create the profiling connection
Sadik Armagan1625efc2021-06-10 18:24:34 +01003310 CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003311
3312 // Get the mock profiling connection
3313 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003314 CHECK(mockProfilingConnection);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003315
Colm Donelan2ba48d22019-11-29 09:10:59 +00003316 // Write a valid "Request Counter Directory" packet into the mock profiling connection, to simulate a valid
3317 // reply from an external profiling service
3318
3319 // Request Counter Directory packet header (word 0, word 1 is always zero):
3320 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
3321 // 16:25 [10] packet_id: Packet identifier, value 0b0000000011
3322 // 8:15 [8] reserved: Reserved, value 0b00000000
3323 // 0:7 [8] reserved: Reserved, value 0b00000000
3324 uint32_t packetFamily = 0;
3325 uint32_t packetId = 123; // Wrong packet id!!!
3326 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
3327
3328 // Create the Request Counter Directory packet
Jim Flynnbbfe6032020-07-20 16:57:44 +01003329 arm::pipe::Packet requestCounterDirectoryPacket(header);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003330
3331 // Write the packet to the mock profiling connection
3332 mockProfilingConnection->WritePacket(std::move(requestCounterDirectoryPacket));
3333
Finn Williams09ad6f92019-12-19 17:05:18 +00003334 // Start the command handler and the send thread
3335 profilingService.Update();
3336
3337 // Reset the profiling service to stop and join any running thread
3338 options.m_EnableProfiling = false;
3339 profilingService.ResetExternalProfilingOptions(options, true);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003340
3341 streamRedirector.CancelRedirect();
3342
3343 // Check that the expected error has occurred and logged to the standard output
David Monahana8837bf2020-04-16 10:01:56 +01003344 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 +00003345 {
3346 std::cout << ss.str();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003347 FAIL("Expected string not found.");
Colm Donelan2ba48d22019-11-29 09:10:59 +00003348 }
Colm Donelan2ba48d22019-11-29 09:10:59 +00003349}
3350
Sadik Armagan1625efc2021-06-10 18:24:34 +01003351TEST_CASE("CheckProfilingServiceBadPeriodicCounterSelectionPacket")
Colm Donelan2ba48d22019-11-29 09:10:59 +00003352{
3353 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
3354 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003355
3356 // Redirect the standard output to a local stream so that we can parse the warning message
3357 std::stringstream ss;
3358 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3359
3360 // Reset the profiling service to the uninitialized state
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003361 ProfilingOptions options;
Colm Donelan2ba48d22019-11-29 09:10:59 +00003362 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +00003363 armnn::ArmNNProfilingServiceInitialiser initialiser;
3364 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003365 profilingService.ResetExternalProfilingOptions(options, true);
3366
Sadik Armagan3184c902020-03-18 10:57:30 +00003367 // Swap the profiling connection factory in the profiling service instance with our mock one
Jim Flynn34430252022-03-04 15:03:58 +00003368 SwapProfilingConnectionFactoryHelper helper(arm::pipe::MAX_ARMNN_COUNTER, initialiser, profilingService);
Sadik Armagan3184c902020-03-18 10:57:30 +00003369
Colm Donelan2ba48d22019-11-29 09:10:59 +00003370 // Bring the profiling service to the "Active" state
Sadik Armagan1625efc2021-06-10 18:24:34 +01003371 CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003372 profilingService.Update(); // Initialize the counter directory
Sadik Armagan1625efc2021-06-10 18:24:34 +01003373 CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003374 profilingService.Update(); // Create the profiling connection
Sadik Armagan1625efc2021-06-10 18:24:34 +01003375 CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003376 profilingService.Update(); // Start the command handler and the send thread
3377
3378 // Get the mock profiling connection
3379 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003380 CHECK(mockProfilingConnection);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003381
Colm Donelan2ba48d22019-11-29 09:10:59 +00003382 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
3383 // external profiling service
3384
3385 // Periodic Counter Selection packet header:
3386 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
3387 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
3388 // 8:15 [8] reserved: Reserved, value 0b00000000
3389 // 0:7 [8] reserved: Reserved, value 0b00000000
3390 uint32_t packetFamily = 0;
3391 uint32_t packetId = 999; // Wrong packet id!!!
3392 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
3393
3394 // Create the Periodic Counter Selection packet
Jim Flynnbbfe6032020-07-20 16:57:44 +01003395 // Length == 0, this will disable the collection of counters
3396 arm::pipe::Packet periodicCounterSelectionPacket(header);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003397
3398 // Write the packet to the mock profiling connection
3399 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
Finn Williams09ad6f92019-12-19 17:05:18 +00003400 profilingService.Update();
Colm Donelan2ba48d22019-11-29 09:10:59 +00003401
Finn Williams09ad6f92019-12-19 17:05:18 +00003402 // Reset the profiling service to stop any running thread
3403 options.m_EnableProfiling = false;
3404 profilingService.ResetExternalProfilingOptions(options, true);
Colm Donelan2ba48d22019-11-29 09:10:59 +00003405
3406 // Check that the expected error has occurred and logged to the standard output
3407 streamRedirector.CancelRedirect();
3408
3409 // Check that the expected error has occurred and logged to the standard output
David Monahana8837bf2020-04-16 10:01:56 +01003410 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 +00003411 {
3412 std::cout << ss.str();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003413 FAIL("Expected string not found.");
Colm Donelan2ba48d22019-11-29 09:10:59 +00003414 }
Colm Donelan2ba48d22019-11-29 09:10:59 +00003415}
Jim Flynn97897022020-02-02 12:52:59 +00003416
Sadik Armagan1625efc2021-06-10 18:24:34 +01003417TEST_CASE("CheckCounterIdMap")
David Monahande803072020-01-30 12:44:23 +00003418{
3419 CounterIdMap counterIdMap;
Jim Flynnf9db3ef2022-03-08 21:23:44 +00003420 CHECK_THROWS_AS(counterIdMap.GetBackendId(0), arm::pipe::ProfilingException);
3421 CHECK_THROWS_AS(counterIdMap.GetGlobalId(0, armnn::profiling::BACKEND_ID), arm::pipe::ProfilingException);
David Monahande803072020-01-30 12:44:23 +00003422
3423 uint16_t globalCounterIds = 0;
3424
Cathal Corbett6f073722022-03-04 12:11:09 +00003425 std::string cpuRefId(GetComputeDeviceAsCString(armnn::Compute::CpuRef));
3426 std::string cpuAccId(GetComputeDeviceAsCString(armnn::Compute::CpuAcc));
David Monahande803072020-01-30 12:44:23 +00003427
3428 std::vector<uint16_t> cpuRefCounters = {0, 1, 2, 3};
3429 std::vector<uint16_t> cpuAccCounters = {0, 1};
3430
3431 for (uint16_t backendCounterId : cpuRefCounters)
3432 {
3433 counterIdMap.RegisterMapping(globalCounterIds, backendCounterId, cpuRefId);
3434 ++globalCounterIds;
3435 }
3436 for (uint16_t backendCounterId : cpuAccCounters)
3437 {
3438 counterIdMap.RegisterMapping(globalCounterIds, backendCounterId, cpuAccId);
3439 ++globalCounterIds;
3440 }
3441
Cathal Corbett6f073722022-03-04 12:11:09 +00003442 CHECK(counterIdMap.GetBackendId(0) == (std::pair<uint16_t, std::string>(0, cpuRefId)));
3443 CHECK(counterIdMap.GetBackendId(1) == (std::pair<uint16_t, std::string>(1, cpuRefId)));
3444 CHECK(counterIdMap.GetBackendId(2) == (std::pair<uint16_t, std::string>(2, cpuRefId)));
3445 CHECK(counterIdMap.GetBackendId(3) == (std::pair<uint16_t, std::string>(3, cpuRefId)));
3446 CHECK(counterIdMap.GetBackendId(4) == (std::pair<uint16_t, std::string>(0, cpuAccId)));
3447 CHECK(counterIdMap.GetBackendId(5) == (std::pair<uint16_t, std::string>(1, cpuAccId)));
David Monahande803072020-01-30 12:44:23 +00003448
Sadik Armagan1625efc2021-06-10 18:24:34 +01003449 CHECK(counterIdMap.GetGlobalId(0, cpuRefId) == 0);
3450 CHECK(counterIdMap.GetGlobalId(1, cpuRefId) == 1);
3451 CHECK(counterIdMap.GetGlobalId(2, cpuRefId) == 2);
3452 CHECK(counterIdMap.GetGlobalId(3, cpuRefId) == 3);
3453 CHECK(counterIdMap.GetGlobalId(0, cpuAccId) == 4);
3454 CHECK(counterIdMap.GetGlobalId(1, cpuAccId) == 5);
David Monahande803072020-01-30 12:44:23 +00003455}
Colm Donelan2ba48d22019-11-29 09:10:59 +00003456
Sadik Armagan1625efc2021-06-10 18:24:34 +01003457TEST_CASE("CheckRegisterBackendCounters")
Jim Flynn97897022020-02-02 12:52:59 +00003458{
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00003459 uint16_t globalCounterIds = INFERENCES_RUN;
Cathal Corbett6f073722022-03-04 12:11:09 +00003460 std::string cpuRefId(GetComputeDeviceAsCString(armnn::Compute::CpuRef));
Jim Flynn97897022020-02-02 12:52:59 +00003461
Jim Flynn97897022020-02-02 12:52:59 +00003462 // Reset the profiling service to the uninitialized state
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003463 ProfilingOptions options;
Jim Flynn97897022020-02-02 12:52:59 +00003464 options.m_EnableProfiling = true;
Jim Flynn34430252022-03-04 15:03:58 +00003465 armnn::ArmNNProfilingServiceInitialiser initialiser;
3466 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Jim Flynn97897022020-02-02 12:52:59 +00003467 profilingService.ResetExternalProfilingOptions(options, true);
3468
Sadik Armagan3184c902020-03-18 10:57:30 +00003469 RegisterBackendCounters registerBackendCounters(globalCounterIds, cpuRefId, profilingService);
3470
3471
3472
Sadik Armagan1625efc2021-06-10 18:24:34 +01003473 CHECK(profilingService.GetCounterDirectory().GetCategories().empty());
Jim Flynn97897022020-02-02 12:52:59 +00003474 registerBackendCounters.RegisterCategory("categoryOne");
3475 auto categoryOnePtr = profilingService.GetCounterDirectory().GetCategory("categoryOne");
Sadik Armagan1625efc2021-06-10 18:24:34 +01003476 CHECK(categoryOnePtr);
Jim Flynn97897022020-02-02 12:52:59 +00003477
Sadik Armagan1625efc2021-06-10 18:24:34 +01003478 CHECK(profilingService.GetCounterDirectory().GetDevices().empty());
Jim Flynn97897022020-02-02 12:52:59 +00003479 globalCounterIds = registerBackendCounters.RegisterDevice("deviceOne");
3480 auto deviceOnePtr = profilingService.GetCounterDirectory().GetDevice(globalCounterIds);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003481 CHECK(deviceOnePtr);
3482 CHECK(deviceOnePtr->m_Name == "deviceOne");
Jim Flynn97897022020-02-02 12:52:59 +00003483
Sadik Armagan1625efc2021-06-10 18:24:34 +01003484 CHECK(profilingService.GetCounterDirectory().GetCounterSets().empty());
Jim Flynn97897022020-02-02 12:52:59 +00003485 globalCounterIds = registerBackendCounters.RegisterCounterSet("counterSetOne");
3486 auto counterSetOnePtr = profilingService.GetCounterDirectory().GetCounterSet(globalCounterIds);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003487 CHECK(counterSetOnePtr);
3488 CHECK(counterSetOnePtr->m_Name == "counterSetOne");
Jim Flynn97897022020-02-02 12:52:59 +00003489
3490 uint16_t newGlobalCounterId = registerBackendCounters.RegisterCounter(0,
3491 "categoryOne",
3492 0,
3493 0,
3494 1.f,
3495 "CounterOne",
3496 "first test counter");
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00003497 CHECK((newGlobalCounterId = INFERENCES_RUN + 1));
Jim Flynn97897022020-02-02 12:52:59 +00003498 uint16_t mappedGlobalId = profilingService.GetCounterMappings().GetGlobalId(0, cpuRefId);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003499 CHECK(mappedGlobalId == newGlobalCounterId);
Jim Flynn97897022020-02-02 12:52:59 +00003500 auto backendMapping = profilingService.GetCounterMappings().GetBackendId(newGlobalCounterId);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003501 CHECK(backendMapping.first == 0);
3502 CHECK(backendMapping.second == cpuRefId);
Jim Flynn97897022020-02-02 12:52:59 +00003503
3504 // Reset the profiling service to stop any running thread
3505 options.m_EnableProfiling = false;
3506 profilingService.ResetExternalProfilingOptions(options, true);
3507}
3508
Sadik Armagan1625efc2021-06-10 18:24:34 +01003509TEST_CASE("CheckCounterStatusQuery")
James Conroy2dcd3fe2020-02-06 18:34:52 +00003510{
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003511 ProfilingOptions options;
3512 options.m_EnableProfiling = true;
James Conroy2dcd3fe2020-02-06 18:34:52 +00003513
3514 // Reset the profiling service to the uninitialized state
Jim Flynn34430252022-03-04 15:03:58 +00003515 armnn::ArmNNProfilingServiceInitialiser initialiser;
3516 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003517 profilingService.ResetExternalProfilingOptions(options, true);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003518
Cathal Corbett6f073722022-03-04 12:11:09 +00003519 const std::string cpuRefId(GetComputeDeviceAsCString(armnn::Compute::CpuRef));
3520 const std::string cpuAccId(GetComputeDeviceAsCString(armnn::Compute::CpuAcc));
James Conroy2dcd3fe2020-02-06 18:34:52 +00003521
3522 // Create BackendProfiling for each backend
3523 BackendProfiling backendProfilingCpuRef(options, profilingService, cpuRefId);
3524 BackendProfiling backendProfilingCpuAcc(options, profilingService, cpuAccId);
3525
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00003526 uint16_t initialNumGlobalCounterIds = INFERENCES_RUN;
James Conroy2dcd3fe2020-02-06 18:34:52 +00003527
3528 // Create RegisterBackendCounters for CpuRef
Sadik Armagan3184c902020-03-18 10:57:30 +00003529 RegisterBackendCounters registerBackendCountersCpuRef(initialNumGlobalCounterIds, cpuRefId, profilingService);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003530
3531 // Create 'testCategory' in CounterDirectory (backend agnostic)
Sadik Armagan1625efc2021-06-10 18:24:34 +01003532 CHECK(profilingService.GetCounterDirectory().GetCategories().empty());
James Conroy2dcd3fe2020-02-06 18:34:52 +00003533 registerBackendCountersCpuRef.RegisterCategory("testCategory");
3534 auto categoryOnePtr = profilingService.GetCounterDirectory().GetCategory("testCategory");
Sadik Armagan1625efc2021-06-10 18:24:34 +01003535 CHECK(categoryOnePtr);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003536
3537 // Counters:
3538 // Global | Local | Backend
3539 // 5 | 0 | CpuRef
3540 // 6 | 1 | CpuRef
3541 // 7 | 1 | CpuAcc
3542
3543 std::vector<uint16_t> cpuRefCounters = {0, 1};
3544 std::vector<uint16_t> cpuAccCounters = {0};
3545
3546 // Register the backend counters for CpuRef and validate GetGlobalId and GetBackendId
3547 uint16_t currentNumGlobalCounterIds = registerBackendCountersCpuRef.RegisterCounter(
3548 0, "testCategory", 0, 0, 1.f, "CpuRefCounter0", "Zeroth CpuRef Counter");
Sadik Armagan1625efc2021-06-10 18:24:34 +01003549 CHECK(currentNumGlobalCounterIds == initialNumGlobalCounterIds + 1);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003550 uint16_t mappedGlobalId = profilingService.GetCounterMappings().GetGlobalId(0, cpuRefId);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003551 CHECK(mappedGlobalId == currentNumGlobalCounterIds);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003552 auto backendMapping = profilingService.GetCounterMappings().GetBackendId(currentNumGlobalCounterIds);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003553 CHECK(backendMapping.first == 0);
3554 CHECK(backendMapping.second == cpuRefId);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003555
3556 currentNumGlobalCounterIds = registerBackendCountersCpuRef.RegisterCounter(
3557 1, "testCategory", 0, 0, 1.f, "CpuRefCounter1", "First CpuRef Counter");
Sadik Armagan1625efc2021-06-10 18:24:34 +01003558 CHECK(currentNumGlobalCounterIds == initialNumGlobalCounterIds + 2);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003559 mappedGlobalId = profilingService.GetCounterMappings().GetGlobalId(1, cpuRefId);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003560 CHECK(mappedGlobalId == currentNumGlobalCounterIds);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003561 backendMapping = profilingService.GetCounterMappings().GetBackendId(currentNumGlobalCounterIds);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003562 CHECK(backendMapping.first == 1);
3563 CHECK(backendMapping.second == cpuRefId);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003564
3565 // Create RegisterBackendCounters for CpuAcc
Sadik Armagan3184c902020-03-18 10:57:30 +00003566 RegisterBackendCounters registerBackendCountersCpuAcc(currentNumGlobalCounterIds, cpuAccId, profilingService);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003567
3568 // Register the backend counter for CpuAcc and validate GetGlobalId and GetBackendId
3569 currentNumGlobalCounterIds = registerBackendCountersCpuAcc.RegisterCounter(
3570 0, "testCategory", 0, 0, 1.f, "CpuAccCounter0", "Zeroth CpuAcc Counter");
Sadik Armagan1625efc2021-06-10 18:24:34 +01003571 CHECK(currentNumGlobalCounterIds == initialNumGlobalCounterIds + 3);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003572 mappedGlobalId = profilingService.GetCounterMappings().GetGlobalId(0, cpuAccId);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003573 CHECK(mappedGlobalId == currentNumGlobalCounterIds);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003574 backendMapping = profilingService.GetCounterMappings().GetBackendId(currentNumGlobalCounterIds);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003575 CHECK(backendMapping.first == 0);
3576 CHECK(backendMapping.second == cpuAccId);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003577
3578 // Create vectors for active counters
3579 const std::vector<uint16_t> activeGlobalCounterIds = {5}; // CpuRef(0) activated
3580 const std::vector<uint16_t> newActiveGlobalCounterIds = {6, 7}; // CpuRef(0) and CpuAcc(1) activated
3581
3582 const uint32_t capturePeriod = 200;
3583 const uint32_t newCapturePeriod = 100;
3584
3585 // Set capture period and active counters in CaptureData
Finn Williams032bc742020-02-12 11:02:34 +00003586 profilingService.SetCaptureData(capturePeriod, activeGlobalCounterIds, {});
James Conroy2dcd3fe2020-02-06 18:34:52 +00003587
3588 // Get vector of active counters for CpuRef and CpuAcc backends
3589 std::vector<CounterStatus> cpuRefCounterStatus = backendProfilingCpuRef.GetActiveCounters();
3590 std::vector<CounterStatus> cpuAccCounterStatus = backendProfilingCpuAcc.GetActiveCounters();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003591 CHECK_EQ(cpuRefCounterStatus.size(), 1);
3592 CHECK_EQ(cpuAccCounterStatus.size(), 0);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003593
3594 // Check active CpuRef counter
Sadik Armagan1625efc2021-06-10 18:24:34 +01003595 CHECK_EQ(cpuRefCounterStatus[0].m_GlobalCounterId, activeGlobalCounterIds[0]);
3596 CHECK_EQ(cpuRefCounterStatus[0].m_BackendCounterId, cpuRefCounters[0]);
3597 CHECK_EQ(cpuRefCounterStatus[0].m_SamplingRateInMicroseconds, capturePeriod);
3598 CHECK_EQ(cpuRefCounterStatus[0].m_Enabled, true);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003599
3600 // Check inactive CpuRef counter
3601 CounterStatus inactiveCpuRefCounter = backendProfilingCpuRef.GetCounterStatus(cpuRefCounters[1]);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003602 CHECK_EQ(inactiveCpuRefCounter.m_GlobalCounterId, 6);
3603 CHECK_EQ(inactiveCpuRefCounter.m_BackendCounterId, cpuRefCounters[1]);
3604 CHECK_EQ(inactiveCpuRefCounter.m_SamplingRateInMicroseconds, 0);
3605 CHECK_EQ(inactiveCpuRefCounter.m_Enabled, false);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003606
3607 // Check inactive CpuAcc counter
3608 CounterStatus inactiveCpuAccCounter = backendProfilingCpuAcc.GetCounterStatus(cpuAccCounters[0]);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003609 CHECK_EQ(inactiveCpuAccCounter.m_GlobalCounterId, 7);
3610 CHECK_EQ(inactiveCpuAccCounter.m_BackendCounterId, cpuAccCounters[0]);
3611 CHECK_EQ(inactiveCpuAccCounter.m_SamplingRateInMicroseconds, 0);
3612 CHECK_EQ(inactiveCpuAccCounter.m_Enabled, false);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003613
3614 // Set new capture period and new active counters in CaptureData
Finn Williams032bc742020-02-12 11:02:34 +00003615 profilingService.SetCaptureData(newCapturePeriod, newActiveGlobalCounterIds, {});
James Conroy2dcd3fe2020-02-06 18:34:52 +00003616
3617 // Get vector of active counters for CpuRef and CpuAcc backends
3618 cpuRefCounterStatus = backendProfilingCpuRef.GetActiveCounters();
3619 cpuAccCounterStatus = backendProfilingCpuAcc.GetActiveCounters();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003620 CHECK_EQ(cpuRefCounterStatus.size(), 1);
3621 CHECK_EQ(cpuAccCounterStatus.size(), 1);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003622
3623 // Check active CpuRef counter
Sadik Armagan1625efc2021-06-10 18:24:34 +01003624 CHECK_EQ(cpuRefCounterStatus[0].m_GlobalCounterId, newActiveGlobalCounterIds[0]);
3625 CHECK_EQ(cpuRefCounterStatus[0].m_BackendCounterId, cpuRefCounters[1]);
3626 CHECK_EQ(cpuRefCounterStatus[0].m_SamplingRateInMicroseconds, newCapturePeriod);
3627 CHECK_EQ(cpuRefCounterStatus[0].m_Enabled, true);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003628
3629 // Check active CpuAcc counter
Sadik Armagan1625efc2021-06-10 18:24:34 +01003630 CHECK_EQ(cpuAccCounterStatus[0].m_GlobalCounterId, newActiveGlobalCounterIds[1]);
3631 CHECK_EQ(cpuAccCounterStatus[0].m_BackendCounterId, cpuAccCounters[0]);
3632 CHECK_EQ(cpuAccCounterStatus[0].m_SamplingRateInMicroseconds, newCapturePeriod);
3633 CHECK_EQ(cpuAccCounterStatus[0].m_Enabled, true);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003634
3635 // Check inactive CpuRef counter
3636 inactiveCpuRefCounter = backendProfilingCpuRef.GetCounterStatus(cpuRefCounters[0]);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003637 CHECK_EQ(inactiveCpuRefCounter.m_GlobalCounterId, 5);
3638 CHECK_EQ(inactiveCpuRefCounter.m_BackendCounterId, cpuRefCounters[0]);
3639 CHECK_EQ(inactiveCpuRefCounter.m_SamplingRateInMicroseconds, 0);
3640 CHECK_EQ(inactiveCpuRefCounter.m_Enabled, false);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003641
3642 // Reset the profiling service to stop any running thread
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003643 options.m_EnableProfiling = false;
3644 profilingService.ResetExternalProfilingOptions(options, true);
James Conroy2dcd3fe2020-02-06 18:34:52 +00003645}
3646
Sadik Armagan1625efc2021-06-10 18:24:34 +01003647TEST_CASE("CheckRegisterCounters")
Sadik Armagancab588a2020-02-17 11:33:31 +00003648{
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003649 ProfilingOptions options;
3650 options.m_EnableProfiling = true;
Sadik Armagancab588a2020-02-17 11:33:31 +00003651 MockBufferManager mockBuffer(1024);
Sadik Armagan3184c902020-03-18 10:57:30 +00003652
Sadik Armagancab588a2020-02-17 11:33:31 +00003653 CaptureData captureData;
Jim Flynn34430252022-03-04 15:03:58 +00003654
3655 armnn::ArmNNProfilingServiceInitialiser initialiser;
3656 MockProfilingService mockProfilingService(
3657 arm::pipe::MAX_ARMNN_COUNTER, initialiser, mockBuffer, options.m_EnableProfiling, captureData);
Cathal Corbett6f073722022-03-04 12:11:09 +00003658 std::string cpuRefId(GetComputeDeviceAsCString(armnn::Compute::CpuRef));
Sadik Armagancab588a2020-02-17 11:33:31 +00003659
3660 mockProfilingService.RegisterMapping(6, 0, cpuRefId);
3661 mockProfilingService.RegisterMapping(7, 1, cpuRefId);
3662 mockProfilingService.RegisterMapping(8, 2, cpuRefId);
3663
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00003664 BackendProfiling backendProfiling(options,
Sadik Armagancab588a2020-02-17 11:33:31 +00003665 mockProfilingService,
3666 cpuRefId);
3667
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00003668 Timestamp timestamp;
Sadik Armagancab588a2020-02-17 11:33:31 +00003669 timestamp.timestamp = 1000998;
3670 timestamp.counterValues.emplace_back(0, 700);
3671 timestamp.counterValues.emplace_back(2, 93);
Cathal Corbett5aa9fd72022-02-25 15:33:28 +00003672 std::vector<Timestamp> timestamps;
Sadik Armagancab588a2020-02-17 11:33:31 +00003673 timestamps.push_back(timestamp);
3674 backendProfiling.ReportCounters(timestamps);
3675
3676 auto readBuffer = mockBuffer.GetReadableBuffer();
3677
3678 uint32_t headerWord0 = ReadUint32(readBuffer, 0);
3679 uint32_t headerWord1 = ReadUint32(readBuffer, 4);
3680 uint64_t readTimestamp = ReadUint64(readBuffer, 8);
3681
Sadik Armagan1625efc2021-06-10 18:24:34 +01003682 CHECK(((headerWord0 >> 26) & 0x0000003F) == 3); // packet family
3683 CHECK(((headerWord0 >> 19) & 0x0000007F) == 0); // packet class
3684 CHECK(((headerWord0 >> 16) & 0x00000007) == 0); // packet type
3685 CHECK(headerWord1 == 20); // data length
3686 CHECK(1000998 == readTimestamp); // capture period
Sadik Armagancab588a2020-02-17 11:33:31 +00003687
3688 uint32_t offset = 16;
3689 // Check Counter Index
3690 uint16_t readIndex = ReadUint16(readBuffer, offset);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003691 CHECK(6 == readIndex);
Sadik Armagancab588a2020-02-17 11:33:31 +00003692
3693 // Check Counter Value
3694 offset += 2;
3695 uint32_t readValue = ReadUint32(readBuffer, offset);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003696 CHECK(700 == readValue);
Sadik Armagancab588a2020-02-17 11:33:31 +00003697
3698 // Check Counter Index
3699 offset += 4;
3700 readIndex = ReadUint16(readBuffer, offset);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003701 CHECK(8 == readIndex);
Sadik Armagancab588a2020-02-17 11:33:31 +00003702
3703 // Check Counter Value
3704 offset += 2;
3705 readValue = ReadUint32(readBuffer, offset);
Sadik Armagan1625efc2021-06-10 18:24:34 +01003706 CHECK(93 == readValue);
Sadik Armagancab588a2020-02-17 11:33:31 +00003707}
3708
Sadik Armagan1625efc2021-06-10 18:24:34 +01003709TEST_CASE("CheckFileFormat") {
Isabella Gottardia0687ee2020-03-11 18:04:20 +00003710 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
3711 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
3712
3713 // Create profiling options.
Jim Flynn4c9ed1d2022-01-23 23:57:20 +00003714 ProfilingOptions options;
Isabella Gottardia0687ee2020-03-11 18:04:20 +00003715 options.m_EnableProfiling = true;
3716 // Check the default value set to binary
Sadik Armagan1625efc2021-06-10 18:24:34 +01003717 CHECK(options.m_FileFormat == "binary");
Isabella Gottardia0687ee2020-03-11 18:04:20 +00003718
3719 // Change file format to an unsupported value
3720 options.m_FileFormat = "json";
3721 // Enable the profiling service
Jim Flynn34430252022-03-04 15:03:58 +00003722 armnn::ArmNNProfilingServiceInitialiser initialiser;
3723 ProfilingService profilingService(arm::pipe::MAX_ARMNN_COUNTER, initialiser);
Isabella Gottardia0687ee2020-03-11 18:04:20 +00003724 profilingService.ResetExternalProfilingOptions(options, true);
3725 // Start the command handler and the send thread
3726 profilingService.Update();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003727 CHECK(profilingService.GetCurrentState()==ProfilingState::NotConnected);
Isabella Gottardia0687ee2020-03-11 18:04:20 +00003728
3729 // Redirect the output to a local stream so that we can parse the warning message
3730 std::stringstream ss;
3731 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3732
3733 // When Update is called and the current state is ProfilingState::NotConnected
3734 // an exception will be raised from GetProfilingConnection and displayed as warning in the output local stream
3735 profilingService.Update();
3736
3737 streamRedirector.CancelRedirect();
3738
3739 // Check that the expected error has occurred and logged to the standard output
David Monahana8837bf2020-04-16 10:01:56 +01003740 if (ss.str().find("Unsupported profiling file format, only binary is supported") == std::string::npos)
Isabella Gottardia0687ee2020-03-11 18:04:20 +00003741 {
3742 std::cout << ss.str();
Sadik Armagan1625efc2021-06-10 18:24:34 +01003743 FAIL("Expected string not found.");
Isabella Gottardia0687ee2020-03-11 18:04:20 +00003744 }
3745}
3746
Sadik Armagan1625efc2021-06-10 18:24:34 +01003747}