blob: 1519cd42fd2f4d406833dbc50292d3e9291a2794 [file] [log] [blame]
Francis Murtagh1f7db452019-08-14 09:49:34 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
Matteo Martincighd0613b52019-10-09 16:47:04 +01006#include "ProfilingTests.hpp"
Teresa Charlin9bab4962019-09-06 12:28:35 +01007
Matteo Martincigh8a837172019-10-04 17:01:07 +01008#include <CommandHandler.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +01009#include <CommandHandlerKey.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010010#include <CommandHandlerRegistry.hpp>
Sadik Armaganb5f01b22019-09-18 17:29:00 +010011#include <ConnectionAcknowledgedCommandHandler.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010012#include <CounterDirectory.hpp>
13#include <EncodeVersion.hpp>
14#include <Holder.hpp>
Matteo Martincighe0e6efc2019-10-04 17:17:42 +010015#include <ICounterValues.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010016#include <Packet.hpp>
17#include <PacketVersionResolver.hpp>
Francis Murtaghfcb8ef62019-09-20 15:40:09 +010018#include <PeriodicCounterCapture.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010019#include <PeriodicCounterSelectionCommandHandler.hpp>
20#include <ProfilingStateMachine.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010021#include <ProfilingUtils.hpp>
Narumol Prangnawarat48033692019-09-20 12:04:55 +010022#include <RequestCounterDirectoryCommandHandler.hpp>
Teresa Charlin9bab4962019-09-06 12:28:35 +010023#include <Runtime.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010024#include <SocketProfilingConnection.hpp>
Matteo Martincighcdfb9412019-11-08 11:23:06 +000025#include <SendCounterPacket.hpp>
26#include <SendTimelinePacket.hpp>
Keith Davis02356de2019-08-26 18:28:17 +010027
Matteo Martincigh6db5f202019-09-05 12:02:04 +010028#include <armnn/Conversion.hpp>
Colm Donelan02705242019-11-14 14:19:07 +000029#include <armnn/Types.hpp>
Ferran Balaguer1b941722019-08-28 16:57:18 +010030
Matteo Martincigh54fb9572019-10-02 12:50:57 +010031#include <armnn/Utils.hpp>
32
Sadik Armaganbd9e2c52019-09-26 23:13:31 +010033#include <boost/algorithm/string.hpp>
Ferran Balaguer1b941722019-08-28 16:57:18 +010034#include <boost/numeric/conversion/cast.hpp>
Francis Murtagh1f7db452019-08-14 09:49:34 +010035
Nikhil Rajbc626052019-08-15 15:49:45 +010036#include <cstdint>
37#include <cstring>
Keith Davis3201eea2019-10-24 17:30:41 +010038#include <iostream>
Aron Virginas-Tare898db92019-08-22 12:56:34 +010039#include <limits>
Francis Murtagh11f99b42019-08-16 11:28:52 +010040#include <map>
Aron Virginas-Tare898db92019-08-22 12:56:34 +010041#include <random>
Francis Murtagh1f7db452019-08-14 09:49:34 +010042
Aron Virginas-Tare898db92019-08-22 12:56:34 +010043using namespace armnn::profiling;
44
Matteo Martincigh8a837172019-10-04 17:01:07 +010045BOOST_AUTO_TEST_SUITE(ExternalProfiling)
46
Francis Murtagh1f7db452019-08-14 09:49:34 +010047BOOST_AUTO_TEST_CASE(CheckCommandHandlerKeyComparisons)
48{
Jim Flynn397043f2019-10-17 17:37:10 +010049 CommandHandlerKey testKey1_0(1, 1, 1);
50 CommandHandlerKey testKey1_1(1, 1, 1);
51 CommandHandlerKey testKey1_2(1, 2, 1);
52
53 CommandHandlerKey testKey0(0, 1, 1);
54 CommandHandlerKey testKey1(0, 1, 1);
55 CommandHandlerKey testKey2(0, 1, 1);
56 CommandHandlerKey testKey3(0, 0, 0);
57 CommandHandlerKey testKey4(0, 2, 2);
58 CommandHandlerKey testKey5(0, 0, 2);
59
60 BOOST_CHECK(testKey1_0 > testKey0);
61 BOOST_CHECK(testKey1_0 == testKey1_1);
62 BOOST_CHECK(testKey1_0 < testKey1_2);
Francis Murtagh1f7db452019-08-14 09:49:34 +010063
Keith Davis3201eea2019-10-24 17:30:41 +010064 BOOST_CHECK(testKey1 < testKey4);
65 BOOST_CHECK(testKey1 > testKey3);
66 BOOST_CHECK(testKey1 <= testKey4);
67 BOOST_CHECK(testKey1 >= testKey3);
68 BOOST_CHECK(testKey1 <= testKey2);
69 BOOST_CHECK(testKey1 >= testKey2);
70 BOOST_CHECK(testKey1 == testKey2);
71 BOOST_CHECK(testKey1 == testKey1);
Francis Murtagh1f7db452019-08-14 09:49:34 +010072
Keith Davis3201eea2019-10-24 17:30:41 +010073 BOOST_CHECK(!(testKey1 == testKey5));
74 BOOST_CHECK(!(testKey1 != testKey1));
75 BOOST_CHECK(testKey1 != testKey5);
Francis Murtagh1f7db452019-08-14 09:49:34 +010076
Keith Davis3201eea2019-10-24 17:30:41 +010077 BOOST_CHECK(testKey1 == testKey2 && testKey2 == testKey1);
78 BOOST_CHECK(testKey0 == testKey1 && testKey1 == testKey2 && testKey0 == testKey2);
Francis Murtagh1f7db452019-08-14 09:49:34 +010079
Keith Davis3201eea2019-10-24 17:30:41 +010080 BOOST_CHECK(testKey1.GetPacketId() == 1);
81 BOOST_CHECK(testKey1.GetVersion() == 1);
Francis Murtagh1f7db452019-08-14 09:49:34 +010082
Keith Davis3201eea2019-10-24 17:30:41 +010083 std::vector<CommandHandlerKey> vect = { CommandHandlerKey(0, 0, 1), CommandHandlerKey(0, 2, 0),
84 CommandHandlerKey(0, 1, 0), CommandHandlerKey(0, 2, 1),
85 CommandHandlerKey(0, 1, 1), CommandHandlerKey(0, 0, 1),
86 CommandHandlerKey(0, 2, 0), CommandHandlerKey(0, 0, 0) };
Francis Murtagh1f7db452019-08-14 09:49:34 +010087
88 std::sort(vect.begin(), vect.end());
89
Keith Davis3201eea2019-10-24 17:30:41 +010090 std::vector<CommandHandlerKey> expectedVect = { CommandHandlerKey(0, 0, 0), CommandHandlerKey(0, 0, 1),
91 CommandHandlerKey(0, 0, 1), CommandHandlerKey(0, 1, 0),
92 CommandHandlerKey(0, 1, 1), CommandHandlerKey(0, 2, 0),
93 CommandHandlerKey(0, 2, 0), CommandHandlerKey(0, 2, 1) };
Francis Murtagh1f7db452019-08-14 09:49:34 +010094
95 BOOST_CHECK(vect == expectedVect);
96}
97
Jim Flynned25e0e2019-10-18 13:21:43 +010098BOOST_AUTO_TEST_CASE(CheckPacketKeyComparisons)
99{
Keith Davis3201eea2019-10-24 17:30:41 +0100100 PacketKey key0(0, 0);
101 PacketKey key1(0, 0);
102 PacketKey key2(0, 1);
103 PacketKey key3(0, 2);
104 PacketKey key4(1, 0);
105 PacketKey key5(1, 0);
106 PacketKey key6(1, 1);
Jim Flynned25e0e2019-10-18 13:21:43 +0100107
108 BOOST_CHECK(!(key0 < key1));
109 BOOST_CHECK(!(key0 > key1));
110 BOOST_CHECK(key0 <= key1);
111 BOOST_CHECK(key0 >= key1);
112 BOOST_CHECK(key0 == key1);
113 BOOST_CHECK(key0 < key2);
114 BOOST_CHECK(key2 < key3);
115 BOOST_CHECK(key3 > key0);
116 BOOST_CHECK(key4 == key5);
117 BOOST_CHECK(key4 > key0);
118 BOOST_CHECK(key5 < key6);
119 BOOST_CHECK(key5 <= key6);
120 BOOST_CHECK(key5 != key6);
121}
122
Matteo Martincigh8a837172019-10-04 17:01:07 +0100123BOOST_AUTO_TEST_CASE(CheckCommandHandler)
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100124{
Matteo Martincigh8a837172019-10-04 17:01:07 +0100125 PacketVersionResolver packetVersionResolver;
126 ProfilingStateMachine profilingStateMachine;
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100127
Matteo Martincigh8a837172019-10-04 17:01:07 +0100128 TestProfilingConnectionBase testProfilingConnectionBase;
129 TestProfilingConnectionTimeoutError testProfilingConnectionTimeOutError;
130 TestProfilingConnectionArmnnError testProfilingConnectionArmnnError;
Keith Davis3201eea2019-10-24 17:30:41 +0100131 CounterDirectory counterDirectory;
132 MockBufferManager mockBuffer(1024);
133 SendCounterPacket sendCounterPacket(profilingStateMachine, mockBuffer);
Matteo Martincighcdfb9412019-11-08 11:23:06 +0000134 SendTimelinePacket sendTimelinePacket(mockBuffer);
135
Keith Davis3201eea2019-10-24 17:30:41 +0100136 ConnectionAcknowledgedCommandHandler connectionAcknowledgedCommandHandler(0, 1, 4194304, counterDirectory,
Matteo Martincighcdfb9412019-11-08 11:23:06 +0000137 sendCounterPacket, sendTimelinePacket,
138 profilingStateMachine);
Matteo Martincigh8a837172019-10-04 17:01:07 +0100139 CommandHandlerRegistry commandHandlerRegistry;
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100140
Matteo Martincighc2728f92019-10-07 12:35:21 +0100141 commandHandlerRegistry.RegisterFunctor(&connectionAcknowledgedCommandHandler);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100142
Matteo Martincigh8a837172019-10-04 17:01:07 +0100143 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
144 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100145
Colm Donelan2ba48d22019-11-29 09:10:59 +0000146 // A 1mSec timeout should be enough to slow the command handler thread a little.
Keith Davis3201eea2019-10-24 17:30:41 +0100147 CommandHandler commandHandler0(1, true, commandHandlerRegistry, packetVersionResolver);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100148
Colm Donelan2ba48d22019-11-29 09:10:59 +0000149 // This should start the command handler thread return the connection ack and put the profiling
150 // service into active state.
Matteo Martincigh8a837172019-10-04 17:01:07 +0100151 commandHandler0.Start(testProfilingConnectionBase);
Colm Donelan2ba48d22019-11-29 09:10:59 +0000152 // Try to start the send thread many times, it must only start once
Matteo Martincigh8a837172019-10-04 17:01:07 +0100153 commandHandler0.Start(testProfilingConnectionBase);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100154
Colm Donelan2ba48d22019-11-29 09:10:59 +0000155 // This could take up to 20mSec but we'll check often.
156 for (int i = 0; i < 10; i++)
Matteo Martincigh8a837172019-10-04 17:01:07 +0100157 {
158 if (profilingStateMachine.GetCurrentState() == ProfilingState::Active)
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100159 {
Matteo Martincigh8a837172019-10-04 17:01:07 +0100160 break;
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100161 }
Colm Donelan2ba48d22019-11-29 09:10:59 +0000162 std::this_thread::sleep_for(std::chrono::milliseconds(2));
Matteo Martincigh8a837172019-10-04 17:01:07 +0100163 }
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100164
Matteo Martincigh8a837172019-10-04 17:01:07 +0100165 BOOST_CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::Active);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100166
Colm Donelan2ba48d22019-11-29 09:10:59 +0000167 // Close the thread again.
168 commandHandler0.Stop();
169
170 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
171 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
172
173 // In this test we'll simulate a timeout without a connection ack packet being received.
174 // Stop after timeout is set so we expect the command handler to stop almost immediately.
175 CommandHandler commandHandler1(1, true, commandHandlerRegistry, packetVersionResolver);
176
177 commandHandler1.Start(testProfilingConnectionTimeOutError);
178 // Wait until we know a timeout exception has been sent at least once.
179 for (int i = 0; i < 10; i++)
180 {
181 if (testProfilingConnectionTimeOutError.ReadCalledCount())
182 {
183 break;
184 }
185 std::this_thread::sleep_for(std::chrono::milliseconds(2));
186 }
187 // and leave another short period for the timeout exception to be processed and the loop to break.
188 std::this_thread::sleep_for(std::chrono::milliseconds(3));
189
190 // The command handler loop should have stopped after the timeout.
191 BOOST_CHECK(!commandHandler1.IsRunning());
192
193 commandHandler1.Stop();
194 // The state machine should never have received the ack so will still be in WaitingForAck.
195 BOOST_CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::WaitingForAck);
196
197 // Disable stop after timeout and now commandHandler1 should persist after a timeout
198 commandHandler1.SetStopAfterTimeout(false);
199 // Restart the thread.
200 commandHandler1.Start(testProfilingConnectionTimeOutError);
201
202 // Wait for at the three timeouts and the ack to be sent.
203 for (int i = 0; i < 10; i++)
204 {
205 if (testProfilingConnectionTimeOutError.ReadCalledCount() > 3)
206 {
207 break;
208 }
209 std::this_thread::sleep_for(std::chrono::milliseconds(2));
210 }
211 commandHandler1.Stop();
212
213 // Even after the 3 exceptions the ack packet should have transitioned the command handler to active.
214 BOOST_CHECK(profilingStateMachine.GetCurrentState() == ProfilingState::Active);
215
216 // A command handler that gets exceptions other than timeouts should keep going.
217 CommandHandler commandHandler2(1, false, commandHandlerRegistry, packetVersionResolver);
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100218
Matteo Martincigh8a837172019-10-04 17:01:07 +0100219 commandHandler2.Start(testProfilingConnectionArmnnError);
220
Colm Donelan2ba48d22019-11-29 09:10:59 +0000221 // Wait for two exceptions to be thrown.
222 for (int i = 0; i < 10; i++)
223 {
224 if (testProfilingConnectionTimeOutError.ReadCalledCount() >= 2)
225 {
226 break;
227 }
228 std::this_thread::sleep_for(std::chrono::milliseconds(2));
229 }
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100230
Matteo Martincighd0613b52019-10-09 16:47:04 +0100231 BOOST_CHECK(commandHandler2.IsRunning());
Matteo Martincigh8a837172019-10-04 17:01:07 +0100232 commandHandler2.Stop();
FinnWilliamsArm4833cea2019-09-17 16:53:53 +0100233}
234
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100235BOOST_AUTO_TEST_CASE(CheckEncodeVersion)
236{
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100237 Version version1(12);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100238
239 BOOST_CHECK(version1.GetMajor() == 0);
240 BOOST_CHECK(version1.GetMinor() == 0);
241 BOOST_CHECK(version1.GetPatch() == 12);
242
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100243 Version version2(4108);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100244
245 BOOST_CHECK(version2.GetMajor() == 0);
246 BOOST_CHECK(version2.GetMinor() == 1);
247 BOOST_CHECK(version2.GetPatch() == 12);
248
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100249 Version version3(4198412);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100250
251 BOOST_CHECK(version3.GetMajor() == 1);
252 BOOST_CHECK(version3.GetMinor() == 1);
253 BOOST_CHECK(version3.GetPatch() == 12);
254
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100255 Version version4(0);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100256
257 BOOST_CHECK(version4.GetMajor() == 0);
258 BOOST_CHECK(version4.GetMinor() == 0);
259 BOOST_CHECK(version4.GetPatch() == 0);
260
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100261 Version version5(1, 0, 0);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100262 BOOST_CHECK(version5.GetEncodedValue() == 4194304);
263}
264
Nikhil Rajbc626052019-08-15 15:49:45 +0100265BOOST_AUTO_TEST_CASE(CheckPacketClass)
266{
Keith Davis3201eea2019-10-24 17:30:41 +0100267 uint32_t length = 4;
Matteo Martincigh67ef2a52019-10-10 13:29:02 +0100268 std::unique_ptr<unsigned char[]> packetData0 = std::make_unique<unsigned char[]>(length);
269 std::unique_ptr<unsigned char[]> packetData1 = std::make_unique<unsigned char[]>(0);
270 std::unique_ptr<unsigned char[]> nullPacketData;
Nikhil Rajbc626052019-08-15 15:49:45 +0100271
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100272 Packet packetTest0(472580096, length, packetData0);
Nikhil Rajbc626052019-08-15 15:49:45 +0100273
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100274 BOOST_CHECK(packetTest0.GetHeader() == 472580096);
275 BOOST_CHECK(packetTest0.GetPacketFamily() == 7);
276 BOOST_CHECK(packetTest0.GetPacketId() == 43);
277 BOOST_CHECK(packetTest0.GetLength() == length);
278 BOOST_CHECK(packetTest0.GetPacketType() == 3);
279 BOOST_CHECK(packetTest0.GetPacketClass() == 5);
Nikhil Rajbc626052019-08-15 15:49:45 +0100280
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100281 BOOST_CHECK_THROW(Packet packetTest1(472580096, 0, packetData1), armnn::Exception);
282 BOOST_CHECK_NO_THROW(Packet packetTest2(472580096, 0, nullPacketData));
Nikhil Rajbc626052019-08-15 15:49:45 +0100283
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100284 Packet packetTest3(472580096, 0, nullPacketData);
285 BOOST_CHECK(packetTest3.GetLength() == 0);
286 BOOST_CHECK(packetTest3.GetData() == nullptr);
287
Matteo Martincigh67ef2a52019-10-10 13:29:02 +0100288 const unsigned char* packetTest0Data = packetTest0.GetData();
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100289 Packet packetTest4(std::move(packetTest0));
290
291 BOOST_CHECK(packetTest0.GetData() == nullptr);
292 BOOST_CHECK(packetTest4.GetData() == packetTest0Data);
293
294 BOOST_CHECK(packetTest4.GetHeader() == 472580096);
295 BOOST_CHECK(packetTest4.GetPacketFamily() == 7);
296 BOOST_CHECK(packetTest4.GetPacketId() == 43);
297 BOOST_CHECK(packetTest4.GetLength() == length);
298 BOOST_CHECK(packetTest4.GetPacketType() == 3);
299 BOOST_CHECK(packetTest4.GetPacketClass() == 5);
Nikhil Rajbc626052019-08-15 15:49:45 +0100300}
301
Francis Murtagh11f99b42019-08-16 11:28:52 +0100302BOOST_AUTO_TEST_CASE(CheckCommandHandlerFunctor)
303{
Francis Murtagh11f99b42019-08-16 11:28:52 +0100304 // Hard code the version as it will be the same during a single profiling session
305 uint32_t version = 1;
306
Jim Flynn397043f2019-10-17 17:37:10 +0100307 TestFunctorA testFunctorA(7, 461, version);
308 TestFunctorB testFunctorB(8, 963, version);
309 TestFunctorC testFunctorC(5, 983, version);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100310
Jim Flynn397043f2019-10-17 17:37:10 +0100311 CommandHandlerKey keyA(testFunctorA.GetFamilyId(), testFunctorA.GetPacketId(), testFunctorA.GetVersion());
312 CommandHandlerKey keyB(testFunctorB.GetFamilyId(), testFunctorB.GetPacketId(), testFunctorB.GetVersion());
313 CommandHandlerKey keyC(testFunctorC.GetFamilyId(), testFunctorC.GetPacketId(), testFunctorC.GetVersion());
Francis Murtagh11f99b42019-08-16 11:28:52 +0100314
315 // Create the unwrapped map to simulate the Command Handler Registry
316 std::map<CommandHandlerKey, CommandHandlerFunctor*> registry;
317
318 registry.insert(std::make_pair(keyB, &testFunctorB));
319 registry.insert(std::make_pair(keyA, &testFunctorA));
320 registry.insert(std::make_pair(keyC, &testFunctorC));
321
322 // Check the order of the map is correct
323 auto it = registry.begin();
Keith Davis3201eea2019-10-24 17:30:41 +0100324 BOOST_CHECK(it->first == keyC); // familyId == 5
Francis Murtagh11f99b42019-08-16 11:28:52 +0100325 it++;
Keith Davis3201eea2019-10-24 17:30:41 +0100326 BOOST_CHECK(it->first == keyA); // familyId == 7
Francis Murtagh11f99b42019-08-16 11:28:52 +0100327 it++;
Keith Davis3201eea2019-10-24 17:30:41 +0100328 BOOST_CHECK(it->first == keyB); // familyId == 8
Francis Murtagh11f99b42019-08-16 11:28:52 +0100329
Matteo Martincigh67ef2a52019-10-10 13:29:02 +0100330 std::unique_ptr<unsigned char[]> packetDataA;
331 std::unique_ptr<unsigned char[]> packetDataB;
332 std::unique_ptr<unsigned char[]> packetDataC;
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100333
334 Packet packetA(500000000, 0, packetDataA);
335 Packet packetB(600000000, 0, packetDataB);
336 Packet packetC(400000000, 0, packetDataC);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100337
338 // Check the correct operator of derived class is called
Jim Flynn397043f2019-10-17 17:37:10 +0100339 registry.at(CommandHandlerKey(packetA.GetPacketFamily(), packetA.GetPacketId(), version))->operator()(packetA);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100340 BOOST_CHECK(testFunctorA.GetCount() == 1);
341 BOOST_CHECK(testFunctorB.GetCount() == 0);
342 BOOST_CHECK(testFunctorC.GetCount() == 0);
343
Jim Flynn397043f2019-10-17 17:37:10 +0100344 registry.at(CommandHandlerKey(packetB.GetPacketFamily(), packetB.GetPacketId(), version))->operator()(packetB);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100345 BOOST_CHECK(testFunctorA.GetCount() == 1);
346 BOOST_CHECK(testFunctorB.GetCount() == 1);
347 BOOST_CHECK(testFunctorC.GetCount() == 0);
348
Jim Flynn397043f2019-10-17 17:37:10 +0100349 registry.at(CommandHandlerKey(packetC.GetPacketFamily(), packetC.GetPacketId(), version))->operator()(packetC);
Francis Murtagh11f99b42019-08-16 11:28:52 +0100350 BOOST_CHECK(testFunctorA.GetCount() == 1);
351 BOOST_CHECK(testFunctorB.GetCount() == 1);
352 BOOST_CHECK(testFunctorC.GetCount() == 1);
353}
354
Francis Murtagh94d79152019-08-16 17:45:07 +0100355BOOST_AUTO_TEST_CASE(CheckCommandHandlerRegistry)
356{
357 // Hard code the version as it will be the same during a single profiling session
358 uint32_t version = 1;
359
Jim Flynn397043f2019-10-17 17:37:10 +0100360 TestFunctorA testFunctorA(7, 461, version);
361 TestFunctorB testFunctorB(8, 963, version);
362 TestFunctorC testFunctorC(5, 983, version);
Francis Murtagh94d79152019-08-16 17:45:07 +0100363
364 // Create the Command Handler Registry
365 CommandHandlerRegistry registry;
366
367 // Register multiple different derived classes
Matteo Martincighc2728f92019-10-07 12:35:21 +0100368 registry.RegisterFunctor(&testFunctorA);
369 registry.RegisterFunctor(&testFunctorB);
370 registry.RegisterFunctor(&testFunctorC);
Francis Murtagh94d79152019-08-16 17:45:07 +0100371
Matteo Martincigh67ef2a52019-10-10 13:29:02 +0100372 std::unique_ptr<unsigned char[]> packetDataA;
373 std::unique_ptr<unsigned char[]> packetDataB;
374 std::unique_ptr<unsigned char[]> packetDataC;
FinnWilliamsArma0c78712019-09-16 12:06:47 +0100375
376 Packet packetA(500000000, 0, packetDataA);
377 Packet packetB(600000000, 0, packetDataB);
378 Packet packetC(400000000, 0, packetDataC);
Francis Murtagh94d79152019-08-16 17:45:07 +0100379
380 // Check the correct operator of derived class is called
Jim Flynn397043f2019-10-17 17:37:10 +0100381 registry.GetFunctor(packetA.GetPacketFamily(), packetA.GetPacketId(), version)->operator()(packetA);
Francis Murtagh94d79152019-08-16 17:45:07 +0100382 BOOST_CHECK(testFunctorA.GetCount() == 1);
383 BOOST_CHECK(testFunctorB.GetCount() == 0);
384 BOOST_CHECK(testFunctorC.GetCount() == 0);
385
Jim Flynn397043f2019-10-17 17:37:10 +0100386 registry.GetFunctor(packetB.GetPacketFamily(), packetB.GetPacketId(), version)->operator()(packetB);
Francis Murtagh94d79152019-08-16 17:45:07 +0100387 BOOST_CHECK(testFunctorA.GetCount() == 1);
388 BOOST_CHECK(testFunctorB.GetCount() == 1);
389 BOOST_CHECK(testFunctorC.GetCount() == 0);
390
Jim Flynn397043f2019-10-17 17:37:10 +0100391 registry.GetFunctor(packetC.GetPacketFamily(), packetC.GetPacketId(), version)->operator()(packetC);
Francis Murtagh94d79152019-08-16 17:45:07 +0100392 BOOST_CHECK(testFunctorA.GetCount() == 1);
393 BOOST_CHECK(testFunctorB.GetCount() == 1);
394 BOOST_CHECK(testFunctorC.GetCount() == 1);
395
396 // Re-register an existing key with a new function
Jim Flynn397043f2019-10-17 17:37:10 +0100397 registry.RegisterFunctor(&testFunctorC, testFunctorA.GetFamilyId(), testFunctorA.GetPacketId(), version);
398 registry.GetFunctor(packetA.GetPacketFamily(), packetA.GetPacketId(), version)->operator()(packetC);
Francis Murtagh94d79152019-08-16 17:45:07 +0100399 BOOST_CHECK(testFunctorA.GetCount() == 1);
400 BOOST_CHECK(testFunctorB.GetCount() == 1);
401 BOOST_CHECK(testFunctorC.GetCount() == 2);
402
403 // Check that non-existent key returns nullptr for its functor
Jim Flynn397043f2019-10-17 17:37:10 +0100404 BOOST_CHECK_THROW(registry.GetFunctor(0, 0, 0), armnn::Exception);
Francis Murtagh94d79152019-08-16 17:45:07 +0100405}
406
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100407BOOST_AUTO_TEST_CASE(CheckPacketVersionResolver)
408{
409 // Set up random number generator for generating packetId values
410 std::random_device device;
411 std::mt19937 generator(device());
412 std::uniform_int_distribution<uint32_t> distribution(std::numeric_limits<uint32_t>::min(),
413 std::numeric_limits<uint32_t>::max());
414
415 // NOTE: Expected version is always 1.0.0, regardless of packetId
416 const Version expectedVersion(1, 0, 0);
417
418 PacketVersionResolver packetVersionResolver;
419
420 constexpr unsigned int numTests = 10u;
421
422 for (unsigned int i = 0u; i < numTests; ++i)
423 {
Jim Flynned25e0e2019-10-18 13:21:43 +0100424 const uint32_t familyId = distribution(generator);
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100425 const uint32_t packetId = distribution(generator);
Jim Flynned25e0e2019-10-18 13:21:43 +0100426 Version resolvedVersion = packetVersionResolver.ResolvePacketVersion(familyId, packetId);
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100427
428 BOOST_TEST(resolvedVersion == expectedVersion);
429 }
430}
Matteo Martincighd0613b52019-10-09 16:47:04 +0100431
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100432void ProfilingCurrentStateThreadImpl(ProfilingStateMachine& states)
433{
434 ProfilingState newState = ProfilingState::NotConnected;
435 states.GetCurrentState();
436 states.TransitionToState(newState);
437}
438
439BOOST_AUTO_TEST_CASE(CheckProfilingStateMachine)
440{
441 ProfilingStateMachine profilingState1(ProfilingState::Uninitialised);
442 profilingState1.TransitionToState(ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +0100443 BOOST_CHECK(profilingState1.GetCurrentState() == ProfilingState::Uninitialised);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100444
445 ProfilingStateMachine profilingState2(ProfilingState::Uninitialised);
446 profilingState2.TransitionToState(ProfilingState::NotConnected);
447 BOOST_CHECK(profilingState2.GetCurrentState() == ProfilingState::NotConnected);
448
449 ProfilingStateMachine profilingState3(ProfilingState::NotConnected);
450 profilingState3.TransitionToState(ProfilingState::NotConnected);
451 BOOST_CHECK(profilingState3.GetCurrentState() == ProfilingState::NotConnected);
452
453 ProfilingStateMachine profilingState4(ProfilingState::NotConnected);
454 profilingState4.TransitionToState(ProfilingState::WaitingForAck);
455 BOOST_CHECK(profilingState4.GetCurrentState() == ProfilingState::WaitingForAck);
456
457 ProfilingStateMachine profilingState5(ProfilingState::WaitingForAck);
458 profilingState5.TransitionToState(ProfilingState::WaitingForAck);
459 BOOST_CHECK(profilingState5.GetCurrentState() == ProfilingState::WaitingForAck);
460
461 ProfilingStateMachine profilingState6(ProfilingState::WaitingForAck);
462 profilingState6.TransitionToState(ProfilingState::Active);
463 BOOST_CHECK(profilingState6.GetCurrentState() == ProfilingState::Active);
464
465 ProfilingStateMachine profilingState7(ProfilingState::Active);
466 profilingState7.TransitionToState(ProfilingState::NotConnected);
467 BOOST_CHECK(profilingState7.GetCurrentState() == ProfilingState::NotConnected);
468
469 ProfilingStateMachine profilingState8(ProfilingState::Active);
470 profilingState8.TransitionToState(ProfilingState::Active);
471 BOOST_CHECK(profilingState8.GetCurrentState() == ProfilingState::Active);
472
473 ProfilingStateMachine profilingState9(ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +0100474 BOOST_CHECK_THROW(profilingState9.TransitionToState(ProfilingState::WaitingForAck), armnn::Exception);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100475
476 ProfilingStateMachine profilingState10(ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +0100477 BOOST_CHECK_THROW(profilingState10.TransitionToState(ProfilingState::Active), armnn::Exception);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100478
479 ProfilingStateMachine profilingState11(ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +0100480 BOOST_CHECK_THROW(profilingState11.TransitionToState(ProfilingState::Uninitialised), armnn::Exception);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100481
482 ProfilingStateMachine profilingState12(ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +0100483 BOOST_CHECK_THROW(profilingState12.TransitionToState(ProfilingState::Active), armnn::Exception);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100484
485 ProfilingStateMachine profilingState13(ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +0100486 BOOST_CHECK_THROW(profilingState13.TransitionToState(ProfilingState::Uninitialised), armnn::Exception);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100487
488 ProfilingStateMachine profilingState14(ProfilingState::WaitingForAck);
Jim Flynn53e46992019-10-14 12:31:10 +0100489 profilingState14.TransitionToState(ProfilingState::NotConnected);
490 BOOST_CHECK(profilingState14.GetCurrentState() == ProfilingState::NotConnected);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100491
492 ProfilingStateMachine profilingState15(ProfilingState::Active);
Keith Davis3201eea2019-10-24 17:30:41 +0100493 BOOST_CHECK_THROW(profilingState15.TransitionToState(ProfilingState::Uninitialised), armnn::Exception);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100494
495 ProfilingStateMachine profilingState16(armnn::profiling::ProfilingState::Active);
Keith Davis3201eea2019-10-24 17:30:41 +0100496 BOOST_CHECK_THROW(profilingState16.TransitionToState(ProfilingState::WaitingForAck), armnn::Exception);
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100497
498 ProfilingStateMachine profilingState17(ProfilingState::Uninitialised);
499
Keith Davis3201eea2019-10-24 17:30:41 +0100500 std::thread thread1(ProfilingCurrentStateThreadImpl, std::ref(profilingState17));
501 std::thread thread2(ProfilingCurrentStateThreadImpl, std::ref(profilingState17));
502 std::thread thread3(ProfilingCurrentStateThreadImpl, std::ref(profilingState17));
503 std::thread thread4(ProfilingCurrentStateThreadImpl, std::ref(profilingState17));
504 std::thread thread5(ProfilingCurrentStateThreadImpl, std::ref(profilingState17));
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100505
506 thread1.join();
507 thread2.join();
508 thread3.join();
509 thread4.join();
510 thread5.join();
511
512 BOOST_TEST((profilingState17.GetCurrentState() == ProfilingState::NotConnected));
513}
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100514
Jim Flynn8355ec92019-09-17 12:29:50 +0100515void CaptureDataWriteThreadImpl(Holder& holder, uint32_t capturePeriod, const std::vector<uint16_t>& counterIds)
Francis Murtagh68f78d82019-09-04 16:42:29 +0100516{
517 holder.SetCaptureData(capturePeriod, counterIds);
518}
519
Francis Murtaghbd707162019-09-09 11:26:44 +0100520void CaptureDataReadThreadImpl(const Holder& holder, CaptureData& captureData)
Francis Murtagh68f78d82019-09-04 16:42:29 +0100521{
522 captureData = holder.GetCaptureData();
523}
524
525BOOST_AUTO_TEST_CASE(CheckCaptureDataHolder)
526{
Francis Murtaghbd707162019-09-09 11:26:44 +0100527 std::map<uint32_t, std::vector<uint16_t>> periodIdMap;
528 std::vector<uint16_t> counterIds;
Jim Flynn8355ec92019-09-17 12:29:50 +0100529 uint32_t numThreads = 10;
530 for (uint32_t i = 0; i < numThreads; ++i)
Francis Murtaghbd707162019-09-09 11:26:44 +0100531 {
532 counterIds.emplace_back(i);
533 periodIdMap.insert(std::make_pair(i, counterIds));
534 }
Francis Murtagh68f78d82019-09-04 16:42:29 +0100535
Jim Flynn8355ec92019-09-17 12:29:50 +0100536 // Verify the read and write threads set the holder correctly
537 // and retrieve the expected values
Francis Murtagh68f78d82019-09-04 16:42:29 +0100538 Holder holder;
539 BOOST_CHECK((holder.GetCaptureData()).GetCapturePeriod() == 0);
540 BOOST_CHECK(((holder.GetCaptureData()).GetCounterIds()).empty());
541
542 // Check Holder functions
Francis Murtaghbd707162019-09-09 11:26:44 +0100543 std::thread thread1(CaptureDataWriteThreadImpl, std::ref(holder), 2, std::ref(periodIdMap[2]));
Francis Murtagh68f78d82019-09-04 16:42:29 +0100544 thread1.join();
Francis Murtaghbd707162019-09-09 11:26:44 +0100545 BOOST_CHECK((holder.GetCaptureData()).GetCapturePeriod() == 2);
546 BOOST_CHECK((holder.GetCaptureData()).GetCounterIds() == periodIdMap[2]);
Jim Flynn8355ec92019-09-17 12:29:50 +0100547 // NOTE: now that we have some initial values in the holder we don't have to worry
548 // in the multi-threaded section below about a read thread accessing the holder
549 // before any write thread has gotten to it so we read period = 0, counterIds empty
550 // instead of period = 0, counterIds = {0} as will the case when write thread 0
551 // has executed.
Francis Murtagh68f78d82019-09-04 16:42:29 +0100552
553 CaptureData captureData;
554 std::thread thread2(CaptureDataReadThreadImpl, std::ref(holder), std::ref(captureData));
555 thread2.join();
Jim Flynn8355ec92019-09-17 12:29:50 +0100556 BOOST_CHECK(captureData.GetCapturePeriod() == 2);
Francis Murtaghbd707162019-09-09 11:26:44 +0100557 BOOST_CHECK(captureData.GetCounterIds() == periodIdMap[2]);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100558
Jim Flynn8355ec92019-09-17 12:29:50 +0100559 std::map<uint32_t, CaptureData> captureDataIdMap;
560 for (uint32_t i = 0; i < numThreads; ++i)
561 {
562 CaptureData perThreadCaptureData;
563 captureDataIdMap.insert(std::make_pair(i, perThreadCaptureData));
564 }
565
Francis Murtaghbd707162019-09-09 11:26:44 +0100566 std::vector<std::thread> threadsVect;
Jim Flynn8355ec92019-09-17 12:29:50 +0100567 std::vector<std::thread> readThreadsVect;
568 for (uint32_t i = 0; i < numThreads; ++i)
Francis Murtaghbd707162019-09-09 11:26:44 +0100569 {
Keith Davis3201eea2019-10-24 17:30:41 +0100570 threadsVect.emplace_back(
571 std::thread(CaptureDataWriteThreadImpl, std::ref(holder), i, std::ref(periodIdMap[i])));
Francis Murtagh06965692019-09-05 16:29:01 +0100572
Jim Flynn8355ec92019-09-17 12:29:50 +0100573 // Verify that the CaptureData goes into the thread in a virgin state
574 BOOST_CHECK(captureDataIdMap.at(i).GetCapturePeriod() == 0);
575 BOOST_CHECK(captureDataIdMap.at(i).GetCounterIds().empty());
Keith Davis3201eea2019-10-24 17:30:41 +0100576 readThreadsVect.emplace_back(
577 std::thread(CaptureDataReadThreadImpl, std::ref(holder), std::ref(captureDataIdMap.at(i))));
Francis Murtaghbd707162019-09-09 11:26:44 +0100578 }
579
Jim Flynn8355ec92019-09-17 12:29:50 +0100580 for (uint32_t i = 0; i < numThreads; ++i)
Francis Murtaghbd707162019-09-09 11:26:44 +0100581 {
582 threadsVect[i].join();
Francis Murtaghbd707162019-09-09 11:26:44 +0100583 readThreadsVect[i].join();
584 }
Francis Murtagh68f78d82019-09-04 16:42:29 +0100585
Jim Flynn8355ec92019-09-17 12:29:50 +0100586 // Look at the CaptureData that each read thread has filled
587 // the capture period it read should match the counter ids entry
588 for (uint32_t i = 0; i < numThreads; ++i)
589 {
590 CaptureData perThreadCaptureData = captureDataIdMap.at(i);
591 BOOST_CHECK(perThreadCaptureData.GetCounterIds() == periodIdMap.at(perThreadCaptureData.GetCapturePeriod()));
592 }
Matthew Bentham46d1c622019-09-13 12:45:04 +0100593}
Francis Murtagh68f78d82019-09-04 16:42:29 +0100594
Matthew Bentham46d1c622019-09-13 12:45:04 +0100595BOOST_AUTO_TEST_CASE(CaptureDataMethods)
596{
Jim Flynn8355ec92019-09-17 12:29:50 +0100597 // Check CaptureData setter and getter functions
Keith Davis3201eea2019-10-24 17:30:41 +0100598 std::vector<uint16_t> counterIds = { 42, 29, 13 };
Jim Flynn8355ec92019-09-17 12:29:50 +0100599 CaptureData captureData;
600 BOOST_CHECK(captureData.GetCapturePeriod() == 0);
601 BOOST_CHECK((captureData.GetCounterIds()).empty());
602 captureData.SetCapturePeriod(150);
603 captureData.SetCounterIds(counterIds);
604 BOOST_CHECK(captureData.GetCapturePeriod() == 150);
605 BOOST_CHECK(captureData.GetCounterIds() == counterIds);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100606
Jim Flynn8355ec92019-09-17 12:29:50 +0100607 // Check assignment operator
Francis Murtagh68f78d82019-09-04 16:42:29 +0100608 CaptureData secondCaptureData;
Francis Murtagh68f78d82019-09-04 16:42:29 +0100609
Jim Flynn8355ec92019-09-17 12:29:50 +0100610 secondCaptureData = captureData;
611 BOOST_CHECK(secondCaptureData.GetCapturePeriod() == 150);
Matthew Bentham46d1c622019-09-13 12:45:04 +0100612 BOOST_CHECK(secondCaptureData.GetCounterIds() == counterIds);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100613
614 // Check copy constructor
Jim Flynn8355ec92019-09-17 12:29:50 +0100615 CaptureData copyConstructedCaptureData(captureData);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100616
Jim Flynn8355ec92019-09-17 12:29:50 +0100617 BOOST_CHECK(copyConstructedCaptureData.GetCapturePeriod() == 150);
Matthew Bentham46d1c622019-09-13 12:45:04 +0100618 BOOST_CHECK(copyConstructedCaptureData.GetCounterIds() == counterIds);
Keith Davis02356de2019-08-26 18:28:17 +0100619}
Francis Murtagh68f78d82019-09-04 16:42:29 +0100620
Keith Davis02356de2019-08-26 18:28:17 +0100621BOOST_AUTO_TEST_CASE(CheckProfilingServiceDisabled)
622{
623 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Matteo Martincigha84edee2019-10-02 12:50:57 +0100624 ProfilingService& profilingService = ProfilingService::Instance();
625 profilingService.ResetExternalProfilingOptions(options, true);
626 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100627 profilingService.Update();
Matteo Martincigha84edee2019-10-02 12:50:57 +0100628 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis02356de2019-08-26 18:28:17 +0100629}
630
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100631BOOST_AUTO_TEST_CASE(CheckProfilingServiceCounterDirectory)
632{
633 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Matteo Martincigha84edee2019-10-02 12:50:57 +0100634 ProfilingService& profilingService = ProfilingService::Instance();
635 profilingService.ResetExternalProfilingOptions(options, true);
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100636
Matteo Martincigha84edee2019-10-02 12:50:57 +0100637 const ICounterDirectory& counterDirectory0 = profilingService.GetCounterDirectory();
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100638 BOOST_CHECK(counterDirectory0.GetCounterCount() == 0);
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100639 profilingService.Update();
640 BOOST_CHECK(counterDirectory0.GetCounterCount() == 0);
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100641
642 options.m_EnableProfiling = true;
Matteo Martincigha84edee2019-10-02 12:50:57 +0100643 profilingService.ResetExternalProfilingOptions(options);
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100644
Matteo Martincigha84edee2019-10-02 12:50:57 +0100645 const ICounterDirectory& counterDirectory1 = profilingService.GetCounterDirectory();
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100646 BOOST_CHECK(counterDirectory1.GetCounterCount() == 0);
647 profilingService.Update();
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100648 BOOST_CHECK(counterDirectory1.GetCounterCount() != 0);
Colm Donelan2ba48d22019-11-29 09:10:59 +0000649 // Reset the profiling service to stop any running thread
650 options.m_EnableProfiling = false;
651 profilingService.ResetExternalProfilingOptions(options, true);
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100652}
653
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100654BOOST_AUTO_TEST_CASE(CheckProfilingServiceCounterValues)
655{
656 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +0100657 options.m_EnableProfiling = true;
Matteo Martincigha84edee2019-10-02 12:50:57 +0100658 ProfilingService& profilingService = ProfilingService::Instance();
659 profilingService.ResetExternalProfilingOptions(options, true);
660
Matteo Martincigh54fb9572019-10-02 12:50:57 +0100661 profilingService.Update();
Matteo Martincigha84edee2019-10-02 12:50:57 +0100662 const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
Keith Davis3201eea2019-10-24 17:30:41 +0100663 const Counters& counters = counterDirectory.GetCounters();
Matteo Martincigha84edee2019-10-02 12:50:57 +0100664 BOOST_CHECK(!counters.empty());
665
Keith Davise394bd92019-12-02 15:12:19 +0000666 // Get the UID of the first counter for testing;
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100667
668 ProfilingService* profilingServicePtr = &profilingService;
669 std::vector<std::thread> writers;
670
Keith Davis3201eea2019-10-24 17:30:41 +0100671 for (int i = 0; i < 100; ++i)
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100672 {
Matteo Martincigha84edee2019-10-02 12:50:57 +0100673 // Increment and decrement the first counter
Keith Davise394bd92019-12-02 15:12:19 +0000674 writers.push_back(std::thread(&ProfilingService::IncrementCounterValue,
675 profilingServicePtr,
676 armnn::profiling::REGISTERED_BACKENDS));
677
678 writers.push_back(std::thread(&ProfilingService::IncrementCounterValue,
679 profilingServicePtr,
680 armnn::profiling::UNREGISTERED_BACKENDS));
681
Matteo Martincigha84edee2019-10-02 12:50:57 +0100682 // Add 10 and subtract 5 from the first counter
Keith Davise394bd92019-12-02 15:12:19 +0000683 writers.push_back(std::thread(&ProfilingService::AddCounterValue,
684 profilingServicePtr,
685 armnn::profiling::INFERENCES_RUN,
686 10));
687 writers.push_back(std::thread(&ProfilingService::SubtractCounterValue,
688 profilingServicePtr,
689 armnn::profiling::INFERENCES_RUN,
690 5));
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100691 }
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100692 std::for_each(writers.begin(), writers.end(), mem_fn(&std::thread::join));
693
Matteo Martincigha84edee2019-10-02 12:50:57 +0100694 uint32_t counterValue = 0;
Keith Davise394bd92019-12-02 15:12:19 +0000695 BOOST_CHECK(counterValue ==
696 (profilingService.GetCounterValue(armnn::profiling::UNREGISTERED_BACKENDS)
697 - profilingService.GetCounterValue(armnn::profiling::REGISTERED_BACKENDS)));
698 BOOST_CHECK(profilingService.GetCounterValue(armnn::profiling::INFERENCES_RUN) == 500);
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100699
Keith Davise394bd92019-12-02 15:12:19 +0000700 BOOST_CHECK_NO_THROW(profilingService.SetCounterValue(armnn::profiling::UNREGISTERED_BACKENDS, 4));
701 BOOST_CHECK_NO_THROW(counterValue = profilingService.GetCounterValue(armnn::profiling::UNREGISTERED_BACKENDS));
702 BOOST_CHECK(counterValue == 4);
Colm Donelan2ba48d22019-11-29 09:10:59 +0000703 // Reset the profiling service to stop any running thread
704 options.m_EnableProfiling = false;
705 profilingService.ResetExternalProfilingOptions(options, true);
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100706}
707
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100708BOOST_AUTO_TEST_CASE(CheckProfilingObjectUids)
Matteo Martincighab173e92019-09-05 12:02:04 +0100709{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100710 uint16_t uid = 0;
711 BOOST_CHECK_NO_THROW(uid = GetNextUid());
712 BOOST_CHECK(uid >= 1);
713
714 uint16_t nextUid = 0;
715 BOOST_CHECK_NO_THROW(nextUid = GetNextUid());
716 BOOST_CHECK(nextUid > uid);
717
718 std::vector<uint16_t> counterUids;
Keith Davise394bd92019-12-02 15:12:19 +0000719 BOOST_CHECK_NO_THROW(counterUids = GetNextCounterUids(uid,0));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100720 BOOST_CHECK(counterUids.size() == 1);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100721
722 std::vector<uint16_t> nextCounterUids;
Keith Davise394bd92019-12-02 15:12:19 +0000723 BOOST_CHECK_NO_THROW(nextCounterUids = GetNextCounterUids(nextUid, 2));
724 BOOST_CHECK(nextCounterUids.size() == 2);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100725 BOOST_CHECK(nextCounterUids[0] > counterUids[0]);
726
727 std::vector<uint16_t> counterUidsMultiCore;
Keith Davise394bd92019-12-02 15:12:19 +0000728 uint16_t thirdUid = 4;
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100729 uint16_t numberOfCores = 13;
Keith Davise394bd92019-12-02 15:12:19 +0000730 BOOST_CHECK_NO_THROW(counterUidsMultiCore = GetNextCounterUids(thirdUid, numberOfCores));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100731 BOOST_CHECK(counterUidsMultiCore.size() == numberOfCores);
732 BOOST_CHECK(counterUidsMultiCore.front() >= nextCounterUids[0]);
Keith Davis3201eea2019-10-24 17:30:41 +0100733 for (size_t i = 1; i < numberOfCores; i++)
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100734 {
735 BOOST_CHECK(counterUidsMultiCore[i] == counterUidsMultiCore[i - 1] + 1);
736 }
737 BOOST_CHECK(counterUidsMultiCore.back() == counterUidsMultiCore.front() + numberOfCores - 1);
Matteo Martincighab173e92019-09-05 12:02:04 +0100738}
739
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100740BOOST_AUTO_TEST_CASE(CheckCounterDirectoryRegisterCategory)
Matteo Martincighab173e92019-09-05 12:02:04 +0100741{
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100742 CounterDirectory counterDirectory;
Keith Davis3201eea2019-10-24 17:30:41 +0100743 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
744 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100745 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
Keith Davis3201eea2019-10-24 17:30:41 +0100746 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
Matteo Martincighab173e92019-09-05 12:02:04 +0100747
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100748 // Register a category with an invalid name
749 const Category* noCategory = nullptr;
750 BOOST_CHECK_THROW(noCategory = counterDirectory.RegisterCategory(""), armnn::InvalidArgumentException);
751 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
752 BOOST_CHECK(!noCategory);
Matteo Martincighab173e92019-09-05 12:02:04 +0100753
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100754 // Register a category with an invalid name
755 BOOST_CHECK_THROW(noCategory = counterDirectory.RegisterCategory("invalid category"),
756 armnn::InvalidArgumentException);
757 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
758 BOOST_CHECK(!noCategory);
759
760 // Register a new category
761 const std::string categoryName = "some_category";
Keith Davis3201eea2019-10-24 17:30:41 +0100762 const Category* category = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100763 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
764 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
765 BOOST_CHECK(category);
766 BOOST_CHECK(category->m_Name == categoryName);
767 BOOST_CHECK(category->m_Counters.empty());
768 BOOST_CHECK(category->m_DeviceUid == 0);
769 BOOST_CHECK(category->m_CounterSetUid == 0);
770
771 // Get the registered category
772 const Category* registeredCategory = counterDirectory.GetCategory(categoryName);
773 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
774 BOOST_CHECK(registeredCategory);
775 BOOST_CHECK(registeredCategory == category);
776
777 // Try to get a category not registered
778 const Category* notRegisteredCategory = counterDirectory.GetCategory("not_registered_category");
779 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
780 BOOST_CHECK(!notRegisteredCategory);
781
782 // Register a category already registered
783 const Category* anotherCategory = nullptr;
784 BOOST_CHECK_THROW(anotherCategory = counterDirectory.RegisterCategory(categoryName),
785 armnn::InvalidArgumentException);
786 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
787 BOOST_CHECK(!anotherCategory);
788
789 // Register a device for testing
790 const std::string deviceName = "some_device";
Keith Davis3201eea2019-10-24 17:30:41 +0100791 const Device* device = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100792 BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName));
793 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
794 BOOST_CHECK(device);
795 BOOST_CHECK(device->m_Uid >= 1);
796 BOOST_CHECK(device->m_Name == deviceName);
797 BOOST_CHECK(device->m_Cores == 0);
798
799 // Register a new category not associated to any device
800 const std::string categoryWoDeviceName = "some_category_without_device";
Keith Davis3201eea2019-10-24 17:30:41 +0100801 const Category* categoryWoDevice = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100802 BOOST_CHECK_NO_THROW(categoryWoDevice = counterDirectory.RegisterCategory(categoryWoDeviceName, 0));
803 BOOST_CHECK(counterDirectory.GetCategoryCount() == 2);
804 BOOST_CHECK(categoryWoDevice);
805 BOOST_CHECK(categoryWoDevice->m_Name == categoryWoDeviceName);
806 BOOST_CHECK(categoryWoDevice->m_Counters.empty());
807 BOOST_CHECK(categoryWoDevice->m_DeviceUid == 0);
808 BOOST_CHECK(categoryWoDevice->m_CounterSetUid == 0);
809
810 // Register a new category associated to an invalid device
811 const std::string categoryWInvalidDeviceName = "some_category_with_invalid_device";
812
813 ARMNN_NO_CONVERSION_WARN_BEGIN
814 uint16_t invalidDeviceUid = device->m_Uid + 10;
815 ARMNN_NO_CONVERSION_WARN_END
816
817 const Category* categoryWInvalidDevice = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +0100818 BOOST_CHECK_THROW(categoryWInvalidDevice =
819 counterDirectory.RegisterCategory(categoryWInvalidDeviceName, invalidDeviceUid),
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100820 armnn::InvalidArgumentException);
821 BOOST_CHECK(counterDirectory.GetCategoryCount() == 2);
822 BOOST_CHECK(!categoryWInvalidDevice);
823
824 // Register a new category associated to a valid device
825 const std::string categoryWValidDeviceName = "some_category_with_valid_device";
Keith Davis3201eea2019-10-24 17:30:41 +0100826 const Category* categoryWValidDevice = nullptr;
827 BOOST_CHECK_NO_THROW(categoryWValidDevice =
828 counterDirectory.RegisterCategory(categoryWValidDeviceName, device->m_Uid));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100829 BOOST_CHECK(counterDirectory.GetCategoryCount() == 3);
830 BOOST_CHECK(categoryWValidDevice);
831 BOOST_CHECK(categoryWValidDevice != category);
832 BOOST_CHECK(categoryWValidDevice->m_Name == categoryWValidDeviceName);
833 BOOST_CHECK(categoryWValidDevice->m_DeviceUid == device->m_Uid);
834 BOOST_CHECK(categoryWValidDevice->m_CounterSetUid == 0);
835
836 // Register a counter set for testing
837 const std::string counterSetName = "some_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +0100838 const CounterSet* counterSet = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100839 BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
840 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
841 BOOST_CHECK(counterSet);
842 BOOST_CHECK(counterSet->m_Uid >= 1);
843 BOOST_CHECK(counterSet->m_Name == counterSetName);
844 BOOST_CHECK(counterSet->m_Count == 0);
845
846 // Register a new category not associated to any counter set
847 const std::string categoryWoCounterSetName = "some_category_without_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +0100848 const Category* categoryWoCounterSet = nullptr;
849 BOOST_CHECK_NO_THROW(categoryWoCounterSet =
850 counterDirectory.RegisterCategory(categoryWoCounterSetName, armnn::EmptyOptional(), 0));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100851 BOOST_CHECK(counterDirectory.GetCategoryCount() == 4);
852 BOOST_CHECK(categoryWoCounterSet);
853 BOOST_CHECK(categoryWoCounterSet->m_Name == categoryWoCounterSetName);
854 BOOST_CHECK(categoryWoCounterSet->m_DeviceUid == 0);
855 BOOST_CHECK(categoryWoCounterSet->m_CounterSetUid == 0);
856
857 // Register a new category associated to an invalid counter set
858 const std::string categoryWInvalidCounterSetName = "some_category_with_invalid_counter_set";
859
860 ARMNN_NO_CONVERSION_WARN_BEGIN
861 uint16_t invalidCunterSetUid = counterSet->m_Uid + 10;
862 ARMNN_NO_CONVERSION_WARN_END
863
864 const Category* categoryWInvalidCounterSet = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +0100865 BOOST_CHECK_THROW(categoryWInvalidCounterSet = counterDirectory.RegisterCategory(
866 categoryWInvalidCounterSetName, armnn::EmptyOptional(), invalidCunterSetUid),
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100867 armnn::InvalidArgumentException);
868 BOOST_CHECK(counterDirectory.GetCategoryCount() == 4);
869 BOOST_CHECK(!categoryWInvalidCounterSet);
870
871 // Register a new category associated to a valid counter set
872 const std::string categoryWValidCounterSetName = "some_category_with_valid_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +0100873 const Category* categoryWValidCounterSet = nullptr;
874 BOOST_CHECK_NO_THROW(categoryWValidCounterSet = counterDirectory.RegisterCategory(
875 categoryWValidCounterSetName, armnn::EmptyOptional(), counterSet->m_Uid));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100876 BOOST_CHECK(counterDirectory.GetCategoryCount() == 5);
877 BOOST_CHECK(categoryWValidCounterSet);
878 BOOST_CHECK(categoryWValidCounterSet != category);
879 BOOST_CHECK(categoryWValidCounterSet->m_Name == categoryWValidCounterSetName);
880 BOOST_CHECK(categoryWValidCounterSet->m_DeviceUid == 0);
881 BOOST_CHECK(categoryWValidCounterSet->m_CounterSetUid == counterSet->m_Uid);
882
883 // Register a new category associated to a valid device and counter set
884 const std::string categoryWValidDeviceAndValidCounterSetName = "some_category_with_valid_device_and_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +0100885 const Category* categoryWValidDeviceAndValidCounterSet = nullptr;
886 BOOST_CHECK_NO_THROW(categoryWValidDeviceAndValidCounterSet = counterDirectory.RegisterCategory(
887 categoryWValidDeviceAndValidCounterSetName, device->m_Uid, counterSet->m_Uid));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100888 BOOST_CHECK(counterDirectory.GetCategoryCount() == 6);
889 BOOST_CHECK(categoryWValidDeviceAndValidCounterSet);
890 BOOST_CHECK(categoryWValidDeviceAndValidCounterSet != category);
891 BOOST_CHECK(categoryWValidDeviceAndValidCounterSet->m_Name == categoryWValidDeviceAndValidCounterSetName);
892 BOOST_CHECK(categoryWValidDeviceAndValidCounterSet->m_DeviceUid == device->m_Uid);
893 BOOST_CHECK(categoryWValidDeviceAndValidCounterSet->m_CounterSetUid == counterSet->m_Uid);
894}
895
896BOOST_AUTO_TEST_CASE(CheckCounterDirectoryRegisterDevice)
897{
898 CounterDirectory counterDirectory;
Keith Davis3201eea2019-10-24 17:30:41 +0100899 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
900 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100901 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
Keith Davis3201eea2019-10-24 17:30:41 +0100902 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100903
904 // Register a device with an invalid name
905 const Device* noDevice = nullptr;
906 BOOST_CHECK_THROW(noDevice = counterDirectory.RegisterDevice(""), armnn::InvalidArgumentException);
907 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
908 BOOST_CHECK(!noDevice);
909
910 // Register a device with an invalid name
911 BOOST_CHECK_THROW(noDevice = counterDirectory.RegisterDevice("inv@lid nam€"), armnn::InvalidArgumentException);
912 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
913 BOOST_CHECK(!noDevice);
914
915 // Register a new device with no cores or parent category
916 const std::string deviceName = "some_device";
Keith Davis3201eea2019-10-24 17:30:41 +0100917 const Device* device = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100918 BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName));
919 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
920 BOOST_CHECK(device);
921 BOOST_CHECK(device->m_Name == deviceName);
922 BOOST_CHECK(device->m_Uid >= 1);
923 BOOST_CHECK(device->m_Cores == 0);
924
Matteo Martincigh657ab2d2019-09-18 10:53:24 +0100925 // Try getting an unregistered device
926 const Device* unregisteredDevice = counterDirectory.GetDevice(9999);
927 BOOST_CHECK(!unregisteredDevice);
928
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100929 // Get the registered device
930 const Device* registeredDevice = counterDirectory.GetDevice(device->m_Uid);
931 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
932 BOOST_CHECK(registeredDevice);
933 BOOST_CHECK(registeredDevice == device);
934
Matteo Martincigh657ab2d2019-09-18 10:53:24 +0100935 // Register a device with the name of a device already registered
936 const Device* deviceSameName = nullptr;
937 BOOST_CHECK_THROW(deviceSameName = counterDirectory.RegisterDevice(deviceName), armnn::InvalidArgumentException);
938 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
939 BOOST_CHECK(!deviceSameName);
940
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100941 // Register a new device with cores and no parent category
942 const std::string deviceWCoresName = "some_device_with_cores";
Keith Davis3201eea2019-10-24 17:30:41 +0100943 const Device* deviceWCores = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100944 BOOST_CHECK_NO_THROW(deviceWCores = counterDirectory.RegisterDevice(deviceWCoresName, 2));
945 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
946 BOOST_CHECK(deviceWCores);
947 BOOST_CHECK(deviceWCores->m_Name == deviceWCoresName);
948 BOOST_CHECK(deviceWCores->m_Uid >= 1);
949 BOOST_CHECK(deviceWCores->m_Uid > device->m_Uid);
950 BOOST_CHECK(deviceWCores->m_Cores == 2);
951
952 // Get the registered device
953 const Device* registeredDeviceWCores = counterDirectory.GetDevice(deviceWCores->m_Uid);
954 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
955 BOOST_CHECK(registeredDeviceWCores);
956 BOOST_CHECK(registeredDeviceWCores == deviceWCores);
957 BOOST_CHECK(registeredDeviceWCores != device);
958
959 // Register a new device with cores and invalid parent category
960 const std::string deviceWCoresWInvalidParentCategoryName = "some_device_with_cores_with_invalid_parent_category";
Keith Davis3201eea2019-10-24 17:30:41 +0100961 const Device* deviceWCoresWInvalidParentCategory = nullptr;
962 BOOST_CHECK_THROW(deviceWCoresWInvalidParentCategory =
963 counterDirectory.RegisterDevice(deviceWCoresWInvalidParentCategoryName, 3, std::string("")),
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100964 armnn::InvalidArgumentException);
965 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
966 BOOST_CHECK(!deviceWCoresWInvalidParentCategory);
967
968 // Register a new device with cores and invalid parent category
969 const std::string deviceWCoresWInvalidParentCategoryName2 = "some_device_with_cores_with_invalid_parent_category2";
Keith Davis3201eea2019-10-24 17:30:41 +0100970 const Device* deviceWCoresWInvalidParentCategory2 = nullptr;
971 BOOST_CHECK_THROW(deviceWCoresWInvalidParentCategory2 = counterDirectory.RegisterDevice(
972 deviceWCoresWInvalidParentCategoryName2, 3, std::string("invalid_parent_category")),
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100973 armnn::InvalidArgumentException);
974 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
975 BOOST_CHECK(!deviceWCoresWInvalidParentCategory2);
976
977 // Register a category for testing
978 const std::string categoryName = "some_category";
Keith Davis3201eea2019-10-24 17:30:41 +0100979 const Category* category = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100980 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
981 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
982 BOOST_CHECK(category);
983 BOOST_CHECK(category->m_Name == categoryName);
984 BOOST_CHECK(category->m_Counters.empty());
985 BOOST_CHECK(category->m_DeviceUid == 0);
986 BOOST_CHECK(category->m_CounterSetUid == 0);
987
988 // Register a new device with cores and valid parent category
989 const std::string deviceWCoresWValidParentCategoryName = "some_device_with_cores_with_valid_parent_category";
Keith Davis3201eea2019-10-24 17:30:41 +0100990 const Device* deviceWCoresWValidParentCategory = nullptr;
991 BOOST_CHECK_NO_THROW(deviceWCoresWValidParentCategory =
992 counterDirectory.RegisterDevice(deviceWCoresWValidParentCategoryName, 4, categoryName));
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100993 BOOST_CHECK(counterDirectory.GetDeviceCount() == 3);
994 BOOST_CHECK(deviceWCoresWValidParentCategory);
995 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Name == deviceWCoresWValidParentCategoryName);
996 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Uid >= 1);
997 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Uid > device->m_Uid);
998 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Uid > deviceWCores->m_Uid);
999 BOOST_CHECK(deviceWCoresWValidParentCategory->m_Cores == 4);
1000 BOOST_CHECK(category->m_DeviceUid == deviceWCoresWValidParentCategory->m_Uid);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001001
1002 // Register a device associated to a category already associated to a different device
1003 const std::string deviceSameCategoryName = "some_device_with_invalid_parent_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001004 const Device* deviceSameCategory = nullptr;
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001005 BOOST_CHECK_THROW(deviceSameCategory = counterDirectory.RegisterDevice(deviceSameCategoryName, 0, categoryName),
1006 armnn::InvalidArgumentException);
1007 BOOST_CHECK(counterDirectory.GetDeviceCount() == 3);
1008 BOOST_CHECK(!deviceSameCategory);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001009}
1010
1011BOOST_AUTO_TEST_CASE(CheckCounterDirectoryRegisterCounterSet)
1012{
1013 CounterDirectory counterDirectory;
Keith Davis3201eea2019-10-24 17:30:41 +01001014 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
1015 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001016 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
Keith Davis3201eea2019-10-24 17:30:41 +01001017 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001018
1019 // Register a counter set with an invalid name
1020 const CounterSet* noCounterSet = nullptr;
1021 BOOST_CHECK_THROW(noCounterSet = counterDirectory.RegisterCounterSet(""), armnn::InvalidArgumentException);
1022 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
1023 BOOST_CHECK(!noCounterSet);
1024
1025 // Register a counter set with an invalid name
1026 BOOST_CHECK_THROW(noCounterSet = counterDirectory.RegisterCounterSet("invalid name"),
1027 armnn::InvalidArgumentException);
1028 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
1029 BOOST_CHECK(!noCounterSet);
1030
1031 // Register a new counter set with no count or parent category
1032 const std::string counterSetName = "some_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +01001033 const CounterSet* counterSet = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001034 BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
1035 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1036 BOOST_CHECK(counterSet);
1037 BOOST_CHECK(counterSet->m_Name == counterSetName);
1038 BOOST_CHECK(counterSet->m_Uid >= 1);
1039 BOOST_CHECK(counterSet->m_Count == 0);
1040
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001041 // Try getting an unregistered counter set
1042 const CounterSet* unregisteredCounterSet = counterDirectory.GetCounterSet(9999);
1043 BOOST_CHECK(!unregisteredCounterSet);
1044
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001045 // Get the registered counter set
1046 const CounterSet* registeredCounterSet = counterDirectory.GetCounterSet(counterSet->m_Uid);
1047 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1048 BOOST_CHECK(registeredCounterSet);
1049 BOOST_CHECK(registeredCounterSet == counterSet);
1050
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001051 // Register a counter set with the name of a counter set already registered
1052 const CounterSet* counterSetSameName = nullptr;
1053 BOOST_CHECK_THROW(counterSetSameName = counterDirectory.RegisterCounterSet(counterSetName),
1054 armnn::InvalidArgumentException);
1055 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1056 BOOST_CHECK(!counterSetSameName);
1057
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001058 // Register a new counter set with count and no parent category
1059 const std::string counterSetWCountName = "some_counter_set_with_count";
Keith Davis3201eea2019-10-24 17:30:41 +01001060 const CounterSet* counterSetWCount = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001061 BOOST_CHECK_NO_THROW(counterSetWCount = counterDirectory.RegisterCounterSet(counterSetWCountName, 37));
1062 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 2);
1063 BOOST_CHECK(counterSetWCount);
1064 BOOST_CHECK(counterSetWCount->m_Name == counterSetWCountName);
1065 BOOST_CHECK(counterSetWCount->m_Uid >= 1);
1066 BOOST_CHECK(counterSetWCount->m_Uid > counterSet->m_Uid);
1067 BOOST_CHECK(counterSetWCount->m_Count == 37);
1068
1069 // Get the registered counter set
1070 const CounterSet* registeredCounterSetWCount = counterDirectory.GetCounterSet(counterSetWCount->m_Uid);
1071 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 2);
1072 BOOST_CHECK(registeredCounterSetWCount);
1073 BOOST_CHECK(registeredCounterSetWCount == counterSetWCount);
1074 BOOST_CHECK(registeredCounterSetWCount != counterSet);
1075
1076 // Register a new counter set with count and invalid parent category
1077 const std::string counterSetWCountWInvalidParentCategoryName = "some_counter_set_with_count_"
1078 "with_invalid_parent_category";
1079 const CounterSet* counterSetWCountWInvalidParentCategory = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001080 BOOST_CHECK_THROW(counterSetWCountWInvalidParentCategory = counterDirectory.RegisterCounterSet(
1081 counterSetWCountWInvalidParentCategoryName, 42, std::string("")),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001082 armnn::InvalidArgumentException);
1083 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 2);
1084 BOOST_CHECK(!counterSetWCountWInvalidParentCategory);
1085
1086 // Register a new counter set with count and invalid parent category
1087 const std::string counterSetWCountWInvalidParentCategoryName2 = "some_counter_set_with_count_"
1088 "with_invalid_parent_category2";
1089 const CounterSet* counterSetWCountWInvalidParentCategory2 = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001090 BOOST_CHECK_THROW(counterSetWCountWInvalidParentCategory2 = counterDirectory.RegisterCounterSet(
1091 counterSetWCountWInvalidParentCategoryName2, 42, std::string("invalid_parent_category")),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001092 armnn::InvalidArgumentException);
1093 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 2);
1094 BOOST_CHECK(!counterSetWCountWInvalidParentCategory2);
1095
1096 // Register a category for testing
1097 const std::string categoryName = "some_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001098 const Category* category = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001099 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
1100 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
1101 BOOST_CHECK(category);
1102 BOOST_CHECK(category->m_Name == categoryName);
1103 BOOST_CHECK(category->m_Counters.empty());
1104 BOOST_CHECK(category->m_DeviceUid == 0);
1105 BOOST_CHECK(category->m_CounterSetUid == 0);
1106
1107 // Register a new counter set with count and valid parent category
1108 const std::string counterSetWCountWValidParentCategoryName = "some_counter_set_with_count_"
1109 "with_valid_parent_category";
1110 const CounterSet* counterSetWCountWValidParentCategory = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001111 BOOST_CHECK_NO_THROW(counterSetWCountWValidParentCategory = counterDirectory.RegisterCounterSet(
1112 counterSetWCountWValidParentCategoryName, 42, categoryName));
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001113 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 3);
1114 BOOST_CHECK(counterSetWCountWValidParentCategory);
1115 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Name == counterSetWCountWValidParentCategoryName);
1116 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Uid >= 1);
1117 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Uid > counterSet->m_Uid);
1118 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Uid > counterSetWCount->m_Uid);
1119 BOOST_CHECK(counterSetWCountWValidParentCategory->m_Count == 42);
1120 BOOST_CHECK(category->m_CounterSetUid == counterSetWCountWValidParentCategory->m_Uid);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001121
1122 // Register a counter set associated to a category already associated to a different counter set
1123 const std::string counterSetSameCategoryName = "some_counter_set_with_invalid_parent_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001124 const CounterSet* counterSetSameCategory = nullptr;
1125 BOOST_CHECK_THROW(counterSetSameCategory =
1126 counterDirectory.RegisterCounterSet(counterSetSameCategoryName, 0, categoryName),
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001127 armnn::InvalidArgumentException);
1128 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 3);
1129 BOOST_CHECK(!counterSetSameCategory);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001130}
1131
1132BOOST_AUTO_TEST_CASE(CheckCounterDirectoryRegisterCounter)
1133{
1134 CounterDirectory counterDirectory;
Keith Davis3201eea2019-10-24 17:30:41 +01001135 BOOST_CHECK(counterDirectory.GetCategoryCount() == 0);
1136 BOOST_CHECK(counterDirectory.GetDeviceCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001137 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 0);
Keith Davis3201eea2019-10-24 17:30:41 +01001138 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001139
1140 // Register a counter with an invalid parent category name
1141 const Counter* noCounter = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001142 BOOST_CHECK_THROW(noCounter =
Keith Davise394bd92019-12-02 15:12:19 +00001143 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1144 0,
1145 "",
1146 0,
1147 1,
1148 123.45f,
1149 "valid ",
1150 "name"),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001151 armnn::InvalidArgumentException);
1152 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1153 BOOST_CHECK(!noCounter);
1154
1155 // Register a counter with an invalid parent category name
Keith Davise394bd92019-12-02 15:12:19 +00001156 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1157 1,
1158 "invalid parent category",
1159 0,
1160 1,
1161 123.45f,
1162 "valid name",
1163 "valid description"),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001164 armnn::InvalidArgumentException);
1165 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1166 BOOST_CHECK(!noCounter);
1167
1168 // Register a counter with an invalid class
Keith Davise394bd92019-12-02 15:12:19 +00001169 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1170 2,
1171 "valid_parent_category",
1172 2,
1173 1,
1174 123.45f,
1175 "valid "
1176 "name",
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001177 "valid description"),
1178 armnn::InvalidArgumentException);
1179 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1180 BOOST_CHECK(!noCounter);
1181
1182 // Register a counter with an invalid interpolation
Keith Davise394bd92019-12-02 15:12:19 +00001183 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1184 4,
1185 "valid_parent_category",
1186 0,
1187 3,
1188 123.45f,
1189 "valid "
1190 "name",
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001191 "valid description"),
1192 armnn::InvalidArgumentException);
1193 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1194 BOOST_CHECK(!noCounter);
1195
1196 // Register a counter with an invalid multiplier
Keith Davise394bd92019-12-02 15:12:19 +00001197 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1198 5,
1199 "valid_parent_category",
1200 0,
1201 1,
1202 .0f,
1203 "valid "
1204 "name",
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001205 "valid description"),
1206 armnn::InvalidArgumentException);
1207 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1208 BOOST_CHECK(!noCounter);
1209
1210 // Register a counter with an invalid name
Keith Davis3201eea2019-10-24 17:30:41 +01001211 BOOST_CHECK_THROW(
Keith Davise394bd92019-12-02 15:12:19 +00001212 noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1213 6,
1214 "valid_parent_category",
1215 0,
1216 1,
1217 123.45f,
1218 "",
1219 "valid description"),
Keith Davis3201eea2019-10-24 17:30:41 +01001220 armnn::InvalidArgumentException);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001221 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1222 BOOST_CHECK(!noCounter);
1223
1224 // Register a counter with an invalid name
Keith Davise394bd92019-12-02 15:12:19 +00001225 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1226 7,
1227 "valid_parent_category",
1228 0,
1229 1,
1230 123.45f,
1231 "invalid nam€",
1232 "valid description"),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001233 armnn::InvalidArgumentException);
1234 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1235 BOOST_CHECK(!noCounter);
1236
1237 // Register a counter with an invalid description
Keith Davis3201eea2019-10-24 17:30:41 +01001238 BOOST_CHECK_THROW(noCounter =
Keith Davise394bd92019-12-02 15:12:19 +00001239 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1240 8,
1241 "valid_parent_category",
1242 0,
1243 1,
1244 123.45f,
1245 "valid name",
1246 ""),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001247 armnn::InvalidArgumentException);
1248 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1249 BOOST_CHECK(!noCounter);
1250
1251 // Register a counter with an invalid description
Keith Davise394bd92019-12-02 15:12:19 +00001252 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1253 9,
1254 "valid_parent_category",
1255 0,
1256 1,
1257 123.45f,
1258 "valid "
1259 "name",
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001260 "inv@lid description"),
1261 armnn::InvalidArgumentException);
1262 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1263 BOOST_CHECK(!noCounter);
1264
1265 // Register a counter with an invalid unit2
Keith Davise394bd92019-12-02 15:12:19 +00001266 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1267 10,
1268 "valid_parent_category",
1269 0,
1270 1,
1271 123.45f,
1272 "valid name",
1273 "valid description",
1274 std::string("Mb/s2")),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001275 armnn::InvalidArgumentException);
1276 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1277 BOOST_CHECK(!noCounter);
1278
1279 // Register a counter with a non-existing parent category name
Keith Davise394bd92019-12-02 15:12:19 +00001280 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1281 11,
1282 "invalid_parent_category",
1283 0,
1284 1,
1285 123.45f,
1286 "valid name",
1287 "valid description"),
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001288 armnn::InvalidArgumentException);
1289 BOOST_CHECK(counterDirectory.GetCounterCount() == 0);
1290 BOOST_CHECK(!noCounter);
1291
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001292 // Try getting an unregistered counter
1293 const Counter* unregisteredCounter = counterDirectory.GetCounter(9999);
1294 BOOST_CHECK(!unregisteredCounter);
1295
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001296 // Register a category for testing
1297 const std::string categoryName = "some_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001298 const Category* category = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001299 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
1300 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
1301 BOOST_CHECK(category);
1302 BOOST_CHECK(category->m_Name == categoryName);
1303 BOOST_CHECK(category->m_Counters.empty());
1304 BOOST_CHECK(category->m_DeviceUid == 0);
1305 BOOST_CHECK(category->m_CounterSetUid == 0);
1306
1307 // Register a counter with a valid parent category name
1308 const Counter* counter = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001309 BOOST_CHECK_NO_THROW(
Keith Davise394bd92019-12-02 15:12:19 +00001310 counter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1311 12,
1312 categoryName,
1313 0,
1314 1,
1315 123.45f,
1316 "valid name",
1317 "valid description"));
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001318 BOOST_CHECK(counterDirectory.GetCounterCount() == 1);
1319 BOOST_CHECK(counter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001320 BOOST_CHECK(counter->m_MaxCounterUid == counter->m_Uid);
1321 BOOST_CHECK(counter->m_Class == 0);
1322 BOOST_CHECK(counter->m_Interpolation == 1);
1323 BOOST_CHECK(counter->m_Multiplier == 123.45f);
1324 BOOST_CHECK(counter->m_Name == "valid name");
1325 BOOST_CHECK(counter->m_Description == "valid description");
1326 BOOST_CHECK(counter->m_Units == "");
1327 BOOST_CHECK(counter->m_DeviceUid == 0);
1328 BOOST_CHECK(counter->m_CounterSetUid == 0);
1329 BOOST_CHECK(category->m_Counters.size() == 1);
1330 BOOST_CHECK(category->m_Counters.back() == counter->m_Uid);
1331
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001332 // Register a counter with a name of a counter already registered for the given parent category name
1333 const Counter* counterSameName = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001334 BOOST_CHECK_THROW(counterSameName =
Keith Davise394bd92019-12-02 15:12:19 +00001335 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1336 13,
1337 categoryName,
1338 0,
1339 0,
1340 1.0f,
1341 "valid name",
1342 "valid description",
1343 std::string("description")),
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001344 armnn::InvalidArgumentException);
1345 BOOST_CHECK(counterDirectory.GetCounterCount() == 1);
1346 BOOST_CHECK(!counterSameName);
1347
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001348 // Register a counter with a valid parent category name and units
1349 const Counter* counterWUnits = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001350 BOOST_CHECK_NO_THROW(counterWUnits = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1351 14,
1352 categoryName,
1353 0,
1354 1,
1355 123.45f,
1356 "valid name 2",
1357 "valid description",
1358 std::string("Mnnsq2"))); // Units
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001359 BOOST_CHECK(counterDirectory.GetCounterCount() == 2);
1360 BOOST_CHECK(counterWUnits);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001361 BOOST_CHECK(counterWUnits->m_Uid > counter->m_Uid);
1362 BOOST_CHECK(counterWUnits->m_MaxCounterUid == counterWUnits->m_Uid);
1363 BOOST_CHECK(counterWUnits->m_Class == 0);
1364 BOOST_CHECK(counterWUnits->m_Interpolation == 1);
1365 BOOST_CHECK(counterWUnits->m_Multiplier == 123.45f);
1366 BOOST_CHECK(counterWUnits->m_Name == "valid name 2");
1367 BOOST_CHECK(counterWUnits->m_Description == "valid description");
1368 BOOST_CHECK(counterWUnits->m_Units == "Mnnsq2");
1369 BOOST_CHECK(counterWUnits->m_DeviceUid == 0);
1370 BOOST_CHECK(counterWUnits->m_CounterSetUid == 0);
1371 BOOST_CHECK(category->m_Counters.size() == 2);
1372 BOOST_CHECK(category->m_Counters.back() == counterWUnits->m_Uid);
1373
1374 // Register a counter with a valid parent category name and not associated with a device
1375 const Counter* counterWoDevice = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001376 BOOST_CHECK_NO_THROW(counterWoDevice = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1377 26,
1378 categoryName,
1379 0,
1380 1,
1381 123.45f,
1382 "valid name 3",
1383 "valid description",
1384 armnn::EmptyOptional(),// Units
1385 armnn::EmptyOptional(),// Number of cores
1386 0)); // Device UID
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001387 BOOST_CHECK(counterDirectory.GetCounterCount() == 3);
1388 BOOST_CHECK(counterWoDevice);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001389 BOOST_CHECK(counterWoDevice->m_Uid > counter->m_Uid);
1390 BOOST_CHECK(counterWoDevice->m_MaxCounterUid == counterWoDevice->m_Uid);
1391 BOOST_CHECK(counterWoDevice->m_Class == 0);
1392 BOOST_CHECK(counterWoDevice->m_Interpolation == 1);
1393 BOOST_CHECK(counterWoDevice->m_Multiplier == 123.45f);
1394 BOOST_CHECK(counterWoDevice->m_Name == "valid name 3");
1395 BOOST_CHECK(counterWoDevice->m_Description == "valid description");
1396 BOOST_CHECK(counterWoDevice->m_Units == "");
1397 BOOST_CHECK(counterWoDevice->m_DeviceUid == 0);
1398 BOOST_CHECK(counterWoDevice->m_CounterSetUid == 0);
1399 BOOST_CHECK(category->m_Counters.size() == 3);
1400 BOOST_CHECK(category->m_Counters.back() == counterWoDevice->m_Uid);
1401
1402 // Register a counter with a valid parent category name and associated to an invalid device
Keith Davise394bd92019-12-02 15:12:19 +00001403 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1404 15,
1405 categoryName,
1406 0,
1407 1,
1408 123.45f,
1409 "valid name 4",
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001410 "valid description",
Keith Davis3201eea2019-10-24 17:30:41 +01001411 armnn::EmptyOptional(), // Units
1412 armnn::EmptyOptional(), // Number of cores
1413 100), // Device UID
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001414 armnn::InvalidArgumentException);
1415 BOOST_CHECK(counterDirectory.GetCounterCount() == 3);
1416 BOOST_CHECK(!noCounter);
1417
1418 // Register a device for testing
1419 const std::string deviceName = "some_device";
Keith Davis3201eea2019-10-24 17:30:41 +01001420 const Device* device = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001421 BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName));
1422 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
1423 BOOST_CHECK(device);
1424 BOOST_CHECK(device->m_Name == deviceName);
1425 BOOST_CHECK(device->m_Uid >= 1);
1426 BOOST_CHECK(device->m_Cores == 0);
1427
1428 // Register a counter with a valid parent category name and associated to a device
1429 const Counter* counterWDevice = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001430 BOOST_CHECK_NO_THROW(counterWDevice = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1431 16,
1432 categoryName,
1433 0,
1434 1,
1435 123.45f,
1436 "valid name 5",
1437 std::string("valid description"),
Keith Davis3201eea2019-10-24 17:30:41 +01001438 armnn::EmptyOptional(), // Units
1439 armnn::EmptyOptional(), // Number of cores
1440 device->m_Uid)); // Device UID
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001441 BOOST_CHECK(counterDirectory.GetCounterCount() == 4);
1442 BOOST_CHECK(counterWDevice);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001443 BOOST_CHECK(counterWDevice->m_Uid > counter->m_Uid);
1444 BOOST_CHECK(counterWDevice->m_MaxCounterUid == counterWDevice->m_Uid);
1445 BOOST_CHECK(counterWDevice->m_Class == 0);
1446 BOOST_CHECK(counterWDevice->m_Interpolation == 1);
1447 BOOST_CHECK(counterWDevice->m_Multiplier == 123.45f);
1448 BOOST_CHECK(counterWDevice->m_Name == "valid name 5");
1449 BOOST_CHECK(counterWDevice->m_Description == "valid description");
1450 BOOST_CHECK(counterWDevice->m_Units == "");
1451 BOOST_CHECK(counterWDevice->m_DeviceUid == device->m_Uid);
1452 BOOST_CHECK(counterWDevice->m_CounterSetUid == 0);
1453 BOOST_CHECK(category->m_Counters.size() == 4);
1454 BOOST_CHECK(category->m_Counters.back() == counterWDevice->m_Uid);
1455
1456 // Register a counter with a valid parent category name and not associated with a counter set
1457 const Counter* counterWoCounterSet = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001458 BOOST_CHECK_NO_THROW(counterWoCounterSet = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1459 17,
1460 categoryName,
1461 0,
1462 1,
1463 123.45f,
1464 "valid name 6",
1465 "valid description",
1466 armnn::EmptyOptional(),// Units
1467 armnn::EmptyOptional(),// No of cores
1468 armnn::EmptyOptional(),// Device UID
1469 0)); // CounterSet UID
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001470 BOOST_CHECK(counterDirectory.GetCounterCount() == 5);
1471 BOOST_CHECK(counterWoCounterSet);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001472 BOOST_CHECK(counterWoCounterSet->m_Uid > counter->m_Uid);
1473 BOOST_CHECK(counterWoCounterSet->m_MaxCounterUid == counterWoCounterSet->m_Uid);
1474 BOOST_CHECK(counterWoCounterSet->m_Class == 0);
1475 BOOST_CHECK(counterWoCounterSet->m_Interpolation == 1);
1476 BOOST_CHECK(counterWoCounterSet->m_Multiplier == 123.45f);
1477 BOOST_CHECK(counterWoCounterSet->m_Name == "valid name 6");
1478 BOOST_CHECK(counterWoCounterSet->m_Description == "valid description");
1479 BOOST_CHECK(counterWoCounterSet->m_Units == "");
1480 BOOST_CHECK(counterWoCounterSet->m_DeviceUid == 0);
1481 BOOST_CHECK(counterWoCounterSet->m_CounterSetUid == 0);
1482 BOOST_CHECK(category->m_Counters.size() == 5);
1483 BOOST_CHECK(category->m_Counters.back() == counterWoCounterSet->m_Uid);
1484
1485 // Register a counter with a valid parent category name and associated to an invalid counter set
Keith Davise394bd92019-12-02 15:12:19 +00001486 BOOST_CHECK_THROW(noCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1487 18,
1488 categoryName,
1489 0,
1490 1,
1491 123.45f,
1492 "valid ",
1493 "name 7",
1494 std::string("valid description"),
Keith Davis3201eea2019-10-24 17:30:41 +01001495 armnn::EmptyOptional(), // Units
1496 armnn::EmptyOptional(), // Number of cores
Keith Davise394bd92019-12-02 15:12:19 +00001497 100), // Counter set UID
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001498 armnn::InvalidArgumentException);
1499 BOOST_CHECK(counterDirectory.GetCounterCount() == 5);
1500 BOOST_CHECK(!noCounter);
1501
1502 // Register a counter with a valid parent category name and with a given number of cores
1503 const Counter* counterWNumberOfCores = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001504 uint16_t numberOfCores = 15;
1505 BOOST_CHECK_NO_THROW(counterWNumberOfCores = counterDirectory.RegisterCounter(
Keith Davise394bd92019-12-02 15:12:19 +00001506 armnn::profiling::BACKEND_ID, 50,
Keith Davis3201eea2019-10-24 17:30:41 +01001507 categoryName, 0, 1, 123.45f, "valid name 8", "valid description",
1508 armnn::EmptyOptional(), // Units
1509 numberOfCores, // Number of cores
1510 armnn::EmptyOptional(), // Device UID
1511 armnn::EmptyOptional())); // Counter set UID
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001512 BOOST_CHECK(counterDirectory.GetCounterCount() == 20);
1513 BOOST_CHECK(counterWNumberOfCores);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001514 BOOST_CHECK(counterWNumberOfCores->m_Uid > counter->m_Uid);
1515 BOOST_CHECK(counterWNumberOfCores->m_MaxCounterUid == counterWNumberOfCores->m_Uid + numberOfCores - 1);
1516 BOOST_CHECK(counterWNumberOfCores->m_Class == 0);
1517 BOOST_CHECK(counterWNumberOfCores->m_Interpolation == 1);
1518 BOOST_CHECK(counterWNumberOfCores->m_Multiplier == 123.45f);
1519 BOOST_CHECK(counterWNumberOfCores->m_Name == "valid name 8");
1520 BOOST_CHECK(counterWNumberOfCores->m_Description == "valid description");
1521 BOOST_CHECK(counterWNumberOfCores->m_Units == "");
1522 BOOST_CHECK(counterWNumberOfCores->m_DeviceUid == 0);
1523 BOOST_CHECK(counterWNumberOfCores->m_CounterSetUid == 0);
1524 BOOST_CHECK(category->m_Counters.size() == 20);
Keith Davis3201eea2019-10-24 17:30:41 +01001525 for (size_t i = 0; i < numberOfCores; i++)
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001526 {
1527 BOOST_CHECK(category->m_Counters[category->m_Counters.size() - numberOfCores + i] ==
1528 counterWNumberOfCores->m_Uid + i);
1529 }
1530
1531 // Register a multi-core device for testing
1532 const std::string multiCoreDeviceName = "some_multi_core_device";
Keith Davis3201eea2019-10-24 17:30:41 +01001533 const Device* multiCoreDevice = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001534 BOOST_CHECK_NO_THROW(multiCoreDevice = counterDirectory.RegisterDevice(multiCoreDeviceName, 4));
1535 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
1536 BOOST_CHECK(multiCoreDevice);
1537 BOOST_CHECK(multiCoreDevice->m_Name == multiCoreDeviceName);
1538 BOOST_CHECK(multiCoreDevice->m_Uid >= 1);
1539 BOOST_CHECK(multiCoreDevice->m_Cores == 4);
1540
1541 // Register a counter with a valid parent category name and associated to the multi-core device
1542 const Counter* counterWMultiCoreDevice = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001543 BOOST_CHECK_NO_THROW(counterWMultiCoreDevice = counterDirectory.RegisterCounter(
Keith Davise394bd92019-12-02 15:12:19 +00001544 armnn::profiling::BACKEND_ID, 19, categoryName, 0, 1,
1545 123.45f, "valid name 9", "valid description",
Keith Davis3201eea2019-10-24 17:30:41 +01001546 armnn::EmptyOptional(), // Units
1547 armnn::EmptyOptional(), // Number of cores
1548 multiCoreDevice->m_Uid, // Device UID
1549 armnn::EmptyOptional())); // Counter set UID
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001550 BOOST_CHECK(counterDirectory.GetCounterCount() == 24);
1551 BOOST_CHECK(counterWMultiCoreDevice);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001552 BOOST_CHECK(counterWMultiCoreDevice->m_Uid > counter->m_Uid);
1553 BOOST_CHECK(counterWMultiCoreDevice->m_MaxCounterUid ==
1554 counterWMultiCoreDevice->m_Uid + multiCoreDevice->m_Cores - 1);
1555 BOOST_CHECK(counterWMultiCoreDevice->m_Class == 0);
1556 BOOST_CHECK(counterWMultiCoreDevice->m_Interpolation == 1);
1557 BOOST_CHECK(counterWMultiCoreDevice->m_Multiplier == 123.45f);
1558 BOOST_CHECK(counterWMultiCoreDevice->m_Name == "valid name 9");
1559 BOOST_CHECK(counterWMultiCoreDevice->m_Description == "valid description");
1560 BOOST_CHECK(counterWMultiCoreDevice->m_Units == "");
1561 BOOST_CHECK(counterWMultiCoreDevice->m_DeviceUid == multiCoreDevice->m_Uid);
1562 BOOST_CHECK(counterWMultiCoreDevice->m_CounterSetUid == 0);
1563 BOOST_CHECK(category->m_Counters.size() == 24);
Keith Davis3201eea2019-10-24 17:30:41 +01001564 for (size_t i = 0; i < 4; i++)
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001565 {
1566 BOOST_CHECK(category->m_Counters[category->m_Counters.size() - 4 + i] == counterWMultiCoreDevice->m_Uid + i);
1567 }
1568
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001569 // Register a multi-core device associate to a parent category for testing
1570 const std::string multiCoreDeviceNameWParentCategory = "some_multi_core_device_with_parent_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001571 const Device* multiCoreDeviceWParentCategory = nullptr;
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001572 BOOST_CHECK_NO_THROW(multiCoreDeviceWParentCategory =
Keith Davis3201eea2019-10-24 17:30:41 +01001573 counterDirectory.RegisterDevice(multiCoreDeviceNameWParentCategory, 2, categoryName));
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001574 BOOST_CHECK(counterDirectory.GetDeviceCount() == 3);
1575 BOOST_CHECK(multiCoreDeviceWParentCategory);
1576 BOOST_CHECK(multiCoreDeviceWParentCategory->m_Name == multiCoreDeviceNameWParentCategory);
1577 BOOST_CHECK(multiCoreDeviceWParentCategory->m_Uid >= 1);
1578 BOOST_CHECK(multiCoreDeviceWParentCategory->m_Cores == 2);
1579
1580 // Register a counter with a valid parent category name and getting the number of cores of the multi-core device
1581 // associated to that category
1582 const Counter* counterWMultiCoreDeviceWParentCategory = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001583 BOOST_CHECK_NO_THROW(counterWMultiCoreDeviceWParentCategory =
1584 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1585 100,
1586 categoryName,
1587 0,
1588 1,
1589 123.45f,
1590 "valid name 10",
1591 "valid description",
1592 armnn::EmptyOptional(),// Units
1593 armnn::EmptyOptional(),// Number of cores
1594 armnn::EmptyOptional(),// Device UID
1595 armnn::EmptyOptional()));// Counter set UID
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001596 BOOST_CHECK(counterDirectory.GetCounterCount() == 26);
1597 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001598 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Uid > counter->m_Uid);
1599 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_MaxCounterUid ==
1600 counterWMultiCoreDeviceWParentCategory->m_Uid + multiCoreDeviceWParentCategory->m_Cores - 1);
1601 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Class == 0);
1602 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Interpolation == 1);
1603 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Multiplier == 123.45f);
1604 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Name == "valid name 10");
1605 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Description == "valid description");
1606 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_Units == "");
1607 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_DeviceUid == 0);
1608 BOOST_CHECK(counterWMultiCoreDeviceWParentCategory->m_CounterSetUid == 0);
1609 BOOST_CHECK(category->m_Counters.size() == 26);
Keith Davis3201eea2019-10-24 17:30:41 +01001610 for (size_t i = 0; i < 2; i++)
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001611 {
1612 BOOST_CHECK(category->m_Counters[category->m_Counters.size() - 2 + i] ==
1613 counterWMultiCoreDeviceWParentCategory->m_Uid + i);
1614 }
1615
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001616 // Register a counter set for testing
1617 const std::string counterSetName = "some_counter_set";
Keith Davis3201eea2019-10-24 17:30:41 +01001618 const CounterSet* counterSet = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001619 BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
1620 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1621 BOOST_CHECK(counterSet);
1622 BOOST_CHECK(counterSet->m_Name == counterSetName);
1623 BOOST_CHECK(counterSet->m_Uid >= 1);
1624 BOOST_CHECK(counterSet->m_Count == 0);
1625
1626 // Register a counter with a valid parent category name and associated to a counter set
1627 const Counter* counterWCounterSet = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001628 BOOST_CHECK_NO_THROW(counterWCounterSet = counterDirectory.RegisterCounter(
Keith Davise394bd92019-12-02 15:12:19 +00001629 armnn::profiling::BACKEND_ID, 300,
Keith Davis3201eea2019-10-24 17:30:41 +01001630 categoryName, 0, 1, 123.45f, "valid name 11", "valid description",
1631 armnn::EmptyOptional(), // Units
1632 0, // Number of cores
1633 armnn::EmptyOptional(), // Device UID
1634 counterSet->m_Uid)); // Counter set UID
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001635 BOOST_CHECK(counterDirectory.GetCounterCount() == 27);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001636 BOOST_CHECK(counterWCounterSet);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001637 BOOST_CHECK(counterWCounterSet->m_Uid > counter->m_Uid);
1638 BOOST_CHECK(counterWCounterSet->m_MaxCounterUid == counterWCounterSet->m_Uid);
1639 BOOST_CHECK(counterWCounterSet->m_Class == 0);
1640 BOOST_CHECK(counterWCounterSet->m_Interpolation == 1);
1641 BOOST_CHECK(counterWCounterSet->m_Multiplier == 123.45f);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001642 BOOST_CHECK(counterWCounterSet->m_Name == "valid name 11");
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001643 BOOST_CHECK(counterWCounterSet->m_Description == "valid description");
1644 BOOST_CHECK(counterWCounterSet->m_Units == "");
1645 BOOST_CHECK(counterWCounterSet->m_DeviceUid == 0);
1646 BOOST_CHECK(counterWCounterSet->m_CounterSetUid == counterSet->m_Uid);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001647 BOOST_CHECK(category->m_Counters.size() == 27);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001648 BOOST_CHECK(category->m_Counters.back() == counterWCounterSet->m_Uid);
1649
1650 // Register a counter with a valid parent category name and associated to a device and a counter set
1651 const Counter* counterWDeviceWCounterSet = nullptr;
Keith Davis3201eea2019-10-24 17:30:41 +01001652 BOOST_CHECK_NO_THROW(counterWDeviceWCounterSet = counterDirectory.RegisterCounter(
Keith Davise394bd92019-12-02 15:12:19 +00001653 armnn::profiling::BACKEND_ID, 23,
Keith Davis3201eea2019-10-24 17:30:41 +01001654 categoryName, 0, 1, 123.45f, "valid name 12", "valid description",
1655 armnn::EmptyOptional(), // Units
1656 1, // Number of cores
1657 device->m_Uid, // Device UID
1658 counterSet->m_Uid)); // Counter set UID
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001659 BOOST_CHECK(counterDirectory.GetCounterCount() == 28);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001660 BOOST_CHECK(counterWDeviceWCounterSet);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001661 BOOST_CHECK(counterWDeviceWCounterSet->m_Uid > counter->m_Uid);
1662 BOOST_CHECK(counterWDeviceWCounterSet->m_MaxCounterUid == counterWDeviceWCounterSet->m_Uid);
1663 BOOST_CHECK(counterWDeviceWCounterSet->m_Class == 0);
1664 BOOST_CHECK(counterWDeviceWCounterSet->m_Interpolation == 1);
1665 BOOST_CHECK(counterWDeviceWCounterSet->m_Multiplier == 123.45f);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001666 BOOST_CHECK(counterWDeviceWCounterSet->m_Name == "valid name 12");
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001667 BOOST_CHECK(counterWDeviceWCounterSet->m_Description == "valid description");
1668 BOOST_CHECK(counterWDeviceWCounterSet->m_Units == "");
1669 BOOST_CHECK(counterWDeviceWCounterSet->m_DeviceUid == device->m_Uid);
1670 BOOST_CHECK(counterWDeviceWCounterSet->m_CounterSetUid == counterSet->m_Uid);
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001671 BOOST_CHECK(category->m_Counters.size() == 28);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001672 BOOST_CHECK(category->m_Counters.back() == counterWDeviceWCounterSet->m_Uid);
1673
1674 // Register another category for testing
1675 const std::string anotherCategoryName = "some_other_category";
Keith Davis3201eea2019-10-24 17:30:41 +01001676 const Category* anotherCategory = nullptr;
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001677 BOOST_CHECK_NO_THROW(anotherCategory = counterDirectory.RegisterCategory(anotherCategoryName));
1678 BOOST_CHECK(counterDirectory.GetCategoryCount() == 2);
1679 BOOST_CHECK(anotherCategory);
1680 BOOST_CHECK(anotherCategory != category);
1681 BOOST_CHECK(anotherCategory->m_Name == anotherCategoryName);
1682 BOOST_CHECK(anotherCategory->m_Counters.empty());
1683 BOOST_CHECK(anotherCategory->m_DeviceUid == 0);
1684 BOOST_CHECK(anotherCategory->m_CounterSetUid == 0);
1685
1686 // Register a counter to the other category
1687 const Counter* anotherCounter = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001688 BOOST_CHECK_NO_THROW(anotherCounter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID, 24,
1689 anotherCategoryName, 1, 0, .00043f,
Keith Davis3201eea2019-10-24 17:30:41 +01001690 "valid name", "valid description",
1691 armnn::EmptyOptional(), // Units
1692 armnn::EmptyOptional(), // Number of cores
1693 device->m_Uid, // Device UID
1694 counterSet->m_Uid)); // Counter set UID
Matteo Martincigh657ab2d2019-09-18 10:53:24 +01001695 BOOST_CHECK(counterDirectory.GetCounterCount() == 29);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001696 BOOST_CHECK(anotherCounter);
Matteo Martincigh6db5f202019-09-05 12:02:04 +01001697 BOOST_CHECK(anotherCounter->m_MaxCounterUid == anotherCounter->m_Uid);
1698 BOOST_CHECK(anotherCounter->m_Class == 1);
1699 BOOST_CHECK(anotherCounter->m_Interpolation == 0);
1700 BOOST_CHECK(anotherCounter->m_Multiplier == .00043f);
1701 BOOST_CHECK(anotherCounter->m_Name == "valid name");
1702 BOOST_CHECK(anotherCounter->m_Description == "valid description");
1703 BOOST_CHECK(anotherCounter->m_Units == "");
1704 BOOST_CHECK(anotherCounter->m_DeviceUid == device->m_Uid);
1705 BOOST_CHECK(anotherCounter->m_CounterSetUid == counterSet->m_Uid);
1706 BOOST_CHECK(anotherCategory->m_Counters.size() == 1);
1707 BOOST_CHECK(anotherCategory->m_Counters.back() == anotherCounter->m_Uid);
Matteo Martincighab173e92019-09-05 12:02:04 +01001708}
1709
Ferran Balaguer1b941722019-08-28 16:57:18 +01001710BOOST_AUTO_TEST_CASE(CounterSelectionCommandHandlerParseData)
1711{
1712 using boost::numeric_cast;
1713
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01001714 ProfilingStateMachine profilingStateMachine;
1715
Ferran Balaguer1b941722019-08-28 16:57:18 +01001716 class TestCaptureThread : public IPeriodicCounterCapture
1717 {
Keith Davis3201eea2019-10-24 17:30:41 +01001718 void Start() override
1719 {}
1720 void Stop() override
1721 {}
Ferran Balaguer1b941722019-08-28 16:57:18 +01001722 };
1723
Matteo Martincighe8485382019-10-10 14:08:21 +01001724 class TestReadCounterValues : public IReadCounterValues
1725 {
Keith Davis3201eea2019-10-24 17:30:41 +01001726 bool IsCounterRegistered(uint16_t counterUid) const override
1727 {
Derek Lamberti1dd75b32019-12-10 21:23:23 +00001728 boost::ignore_unused(counterUid);
Keith Davis3201eea2019-10-24 17:30:41 +01001729 return true;
1730 }
1731 uint16_t GetCounterCount() const override
1732 {
1733 return 0;
1734 }
1735 uint32_t GetCounterValue(uint16_t counterUid) const override
1736 {
Derek Lamberti1dd75b32019-12-10 21:23:23 +00001737 boost::ignore_unused(counterUid);
Keith Davis3201eea2019-10-24 17:30:41 +01001738 return 0;
1739 }
Matteo Martincighe8485382019-10-10 14:08:21 +01001740 };
Jim Flynn397043f2019-10-17 17:37:10 +01001741 const uint32_t familyId = 0;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001742 const uint32_t packetId = 0x40000;
1743
1744 uint32_t version = 1;
1745 Holder holder;
1746 TestCaptureThread captureThread;
Matteo Martincighe8485382019-10-10 14:08:21 +01001747 TestReadCounterValues readCounterValues;
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001748 MockBufferManager mockBuffer(512);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01001749 SendCounterPacket sendCounterPacket(profilingStateMachine, mockBuffer);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001750
1751 uint32_t sizeOfUint32 = numeric_cast<uint32_t>(sizeof(uint32_t));
1752 uint32_t sizeOfUint16 = numeric_cast<uint32_t>(sizeof(uint16_t));
1753
1754 // Data with period and counters
Colm Donelan02705242019-11-14 14:19:07 +00001755 uint32_t period1 = armnn::LOWEST_CAPTURE_PERIOD;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001756 uint32_t dataLength1 = 8;
Keith Davis3201eea2019-10-24 17:30:41 +01001757 uint32_t offset = 0;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001758
Matteo Martincigh67ef2a52019-10-10 13:29:02 +01001759 std::unique_ptr<unsigned char[]> uniqueData1 = std::make_unique<unsigned char[]>(dataLength1);
Keith Davis3201eea2019-10-24 17:30:41 +01001760 unsigned char* data1 = reinterpret_cast<unsigned char*>(uniqueData1.get());
FinnWilliamsArma0c78712019-09-16 12:06:47 +01001761
Ferran Balaguer1b941722019-08-28 16:57:18 +01001762 WriteUint32(data1, offset, period1);
1763 offset += sizeOfUint32;
1764 WriteUint16(data1, offset, 4000);
1765 offset += sizeOfUint16;
1766 WriteUint16(data1, offset, 5000);
1767
FinnWilliamsArma0c78712019-09-16 12:06:47 +01001768 Packet packetA(packetId, dataLength1, uniqueData1);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001769
Keith Davis3201eea2019-10-24 17:30:41 +01001770 PeriodicCounterSelectionCommandHandler commandHandler(familyId, packetId, version, holder, captureThread,
1771 readCounterValues, sendCounterPacket, profilingStateMachine);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001772
Matteo Martincighe8485382019-10-10 14:08:21 +01001773 profilingStateMachine.TransitionToState(ProfilingState::Uninitialised);
1774 BOOST_CHECK_THROW(commandHandler(packetA), armnn::RuntimeException);
1775 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
1776 BOOST_CHECK_THROW(commandHandler(packetA), armnn::RuntimeException);
1777 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
1778 BOOST_CHECK_THROW(commandHandler(packetA), armnn::RuntimeException);
1779 profilingStateMachine.TransitionToState(ProfilingState::Active);
1780 BOOST_CHECK_NO_THROW(commandHandler(packetA));
1781
1782 const std::vector<uint16_t> counterIdsA = holder.GetCaptureData().GetCounterIds();
Ferran Balaguer1b941722019-08-28 16:57:18 +01001783
1784 BOOST_TEST(holder.GetCaptureData().GetCapturePeriod() == period1);
Matteo Martincighe8485382019-10-10 14:08:21 +01001785 BOOST_TEST(counterIdsA.size() == 2);
1786 BOOST_TEST(counterIdsA[0] == 4000);
1787 BOOST_TEST(counterIdsA[1] == 5000);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001788
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001789 auto readBuffer = mockBuffer.GetReadableBuffer();
Ferran Balaguer1b941722019-08-28 16:57:18 +01001790
1791 offset = 0;
1792
1793 uint32_t headerWord0 = ReadUint32(readBuffer, offset);
1794 offset += sizeOfUint32;
1795 uint32_t headerWord1 = ReadUint32(readBuffer, offset);
1796 offset += sizeOfUint32;
1797 uint32_t period = ReadUint32(readBuffer, offset);
1798
Colm Donelan02705242019-11-14 14:19:07 +00001799 BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family
1800 BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 4); // packet id
1801 BOOST_TEST(headerWord1 == 8); // data length
1802 BOOST_TEST(period == armnn::LOWEST_CAPTURE_PERIOD); // capture period
Ferran Balaguer1b941722019-08-28 16:57:18 +01001803
1804 uint16_t counterId = 0;
1805 offset += sizeOfUint32;
1806 counterId = ReadUint16(readBuffer, offset);
1807 BOOST_TEST(counterId == 4000);
1808 offset += sizeOfUint16;
1809 counterId = ReadUint16(readBuffer, offset);
1810 BOOST_TEST(counterId == 5000);
1811
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001812 mockBuffer.MarkRead(readBuffer);
1813
Ferran Balaguer1b941722019-08-28 16:57:18 +01001814 // Data with period only
Colm Donelan02705242019-11-14 14:19:07 +00001815 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 +01001816 uint32_t dataLength2 = 4;
Ferran Balaguer1b941722019-08-28 16:57:18 +01001817
Matteo Martincigh67ef2a52019-10-10 13:29:02 +01001818 std::unique_ptr<unsigned char[]> uniqueData2 = std::make_unique<unsigned char[]>(dataLength2);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001819
FinnWilliamsArma0c78712019-09-16 12:06:47 +01001820 WriteUint32(reinterpret_cast<unsigned char*>(uniqueData2.get()), 0, period2);
1821
1822 Packet packetB(packetId, dataLength2, uniqueData2);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001823
1824 commandHandler(packetB);
1825
Matteo Martincighe8485382019-10-10 14:08:21 +01001826 const std::vector<uint16_t> counterIdsB = holder.GetCaptureData().GetCounterIds();
Ferran Balaguer1b941722019-08-28 16:57:18 +01001827
Colm Donelan02705242019-11-14 14:19:07 +00001828 // Value should have been pulled up from 9000 to LOWEST_CAPTURE_PERIOD.
1829 BOOST_TEST(holder.GetCaptureData().GetCapturePeriod() == armnn::LOWEST_CAPTURE_PERIOD);
Matteo Martincighe8485382019-10-10 14:08:21 +01001830 BOOST_TEST(counterIdsB.size() == 0);
Ferran Balaguer1b941722019-08-28 16:57:18 +01001831
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001832 readBuffer = mockBuffer.GetReadableBuffer();
Ferran Balaguer1b941722019-08-28 16:57:18 +01001833
1834 offset = 0;
1835
1836 headerWord0 = ReadUint32(readBuffer, offset);
1837 offset += sizeOfUint32;
1838 headerWord1 = ReadUint32(readBuffer, offset);
1839 offset += sizeOfUint32;
1840 period = ReadUint32(readBuffer, offset);
1841
Colm Donelan02705242019-11-14 14:19:07 +00001842 BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family
1843 BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 4); // packet id
1844 BOOST_TEST(headerWord1 == 4); // data length
1845 BOOST_TEST(period == armnn::LOWEST_CAPTURE_PERIOD); // capture period
Ferran Balaguer1b941722019-08-28 16:57:18 +01001846}
1847
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001848BOOST_AUTO_TEST_CASE(CheckConnectionAcknowledged)
1849{
1850 using boost::numeric_cast;
1851
Keith Davis3201eea2019-10-24 17:30:41 +01001852 const uint32_t packetFamilyId = 0;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001853 const uint32_t connectionPacketId = 0x10000;
Keith Davis3201eea2019-10-24 17:30:41 +01001854 const uint32_t version = 1;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001855
1856 uint32_t sizeOfUint32 = numeric_cast<uint32_t>(sizeof(uint32_t));
1857 uint32_t sizeOfUint16 = numeric_cast<uint32_t>(sizeof(uint16_t));
1858
1859 // Data with period and counters
Keith Davis3201eea2019-10-24 17:30:41 +01001860 uint32_t period1 = 10;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001861 uint32_t dataLength1 = 8;
Keith Davis3201eea2019-10-24 17:30:41 +01001862 uint32_t offset = 0;
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001863
Matteo Martincigh67ef2a52019-10-10 13:29:02 +01001864 std::unique_ptr<unsigned char[]> uniqueData1 = std::make_unique<unsigned char[]>(dataLength1);
Keith Davis3201eea2019-10-24 17:30:41 +01001865 unsigned char* data1 = reinterpret_cast<unsigned char*>(uniqueData1.get());
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001866
1867 WriteUint32(data1, offset, period1);
1868 offset += sizeOfUint32;
1869 WriteUint16(data1, offset, 4000);
1870 offset += sizeOfUint16;
1871 WriteUint16(data1, offset, 5000);
1872
1873 Packet packetA(connectionPacketId, dataLength1, uniqueData1);
1874
1875 ProfilingStateMachine profilingState(ProfilingState::Uninitialised);
1876 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01001877 CounterDirectory counterDirectory;
1878 MockBufferManager mockBuffer(1024);
1879 SendCounterPacket sendCounterPacket(profilingState, mockBuffer);
Matteo Martincighcdfb9412019-11-08 11:23:06 +00001880 SendTimelinePacket sendTimelinePacket(mockBuffer);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001881
Keith Davis3201eea2019-10-24 17:30:41 +01001882 ConnectionAcknowledgedCommandHandler commandHandler(packetFamilyId, connectionPacketId, version, counterDirectory,
Matteo Martincighcdfb9412019-11-08 11:23:06 +00001883 sendCounterPacket, sendTimelinePacket, profilingState);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001884
1885 // command handler received packet on ProfilingState::Uninitialised
1886 BOOST_CHECK_THROW(commandHandler(packetA), armnn::Exception);
1887
1888 profilingState.TransitionToState(ProfilingState::NotConnected);
1889 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::NotConnected);
1890 // command handler received packet on ProfilingState::NotConnected
1891 BOOST_CHECK_THROW(commandHandler(packetA), armnn::Exception);
1892
1893 profilingState.TransitionToState(ProfilingState::WaitingForAck);
1894 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::WaitingForAck);
1895 // command handler received packet on ProfilingState::WaitingForAck
Matteo Martincighd0613b52019-10-09 16:47:04 +01001896 BOOST_CHECK_NO_THROW(commandHandler(packetA));
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001897 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::Active);
1898
1899 // command handler received packet on ProfilingState::Active
Matteo Martincighd0613b52019-10-09 16:47:04 +01001900 BOOST_CHECK_NO_THROW(commandHandler(packetA));
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001901 BOOST_CHECK(profilingState.GetCurrentState() == ProfilingState::Active);
1902
1903 // command handler received different packet
1904 const uint32_t differentPacketId = 0x40000;
1905 Packet packetB(differentPacketId, dataLength1, uniqueData1);
Matteo Martincighd0613b52019-10-09 16:47:04 +01001906 profilingState.TransitionToState(ProfilingState::NotConnected);
1907 profilingState.TransitionToState(ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01001908 ConnectionAcknowledgedCommandHandler differentCommandHandler(packetFamilyId, differentPacketId, version,
Matteo Martincighcdfb9412019-11-08 11:23:06 +00001909 counterDirectory, sendCounterPacket,
1910 sendTimelinePacket, profilingState);
Sadik Armaganb5f01b22019-09-18 17:29:00 +01001911 BOOST_CHECK_THROW(differentCommandHandler(packetB), armnn::Exception);
1912}
1913
Teresa Charlin9bab4962019-09-06 12:28:35 +01001914BOOST_AUTO_TEST_CASE(CheckSocketProfilingConnection)
1915{
1916 // Check that creating a SocketProfilingConnection results in an exception as the Gator UDS doesn't exist.
1917 BOOST_CHECK_THROW(new SocketProfilingConnection(), armnn::Exception);
1918}
1919
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001920BOOST_AUTO_TEST_CASE(SwTraceIsValidCharTest)
1921{
1922 // Only ASCII 7-bit encoding supported
1923 for (unsigned char c = 0; c < 128; c++)
1924 {
1925 BOOST_CHECK(SwTraceCharPolicy::IsValidChar(c));
1926 }
1927
1928 // Not ASCII
1929 for (unsigned char c = 255; c >= 128; c++)
1930 {
1931 BOOST_CHECK(!SwTraceCharPolicy::IsValidChar(c));
1932 }
1933}
1934
1935BOOST_AUTO_TEST_CASE(SwTraceIsValidNameCharTest)
1936{
1937 // Only alpha-numeric and underscore ASCII 7-bit encoding supported
1938 const unsigned char validChars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
1939 for (unsigned char i = 0; i < sizeof(validChars) / sizeof(validChars[0]) - 1; i++)
1940 {
1941 BOOST_CHECK(SwTraceNameCharPolicy::IsValidChar(validChars[i]));
1942 }
1943
1944 // Non alpha-numeric chars
1945 for (unsigned char c = 0; c < 48; c++)
1946 {
1947 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1948 }
1949 for (unsigned char c = 58; c < 65; c++)
1950 {
1951 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1952 }
1953 for (unsigned char c = 91; c < 95; c++)
1954 {
1955 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1956 }
1957 for (unsigned char c = 96; c < 97; c++)
1958 {
1959 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1960 }
1961 for (unsigned char c = 123; c < 128; c++)
1962 {
1963 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1964 }
1965
1966 // Not ASCII
1967 for (unsigned char c = 255; c >= 128; c++)
1968 {
1969 BOOST_CHECK(!SwTraceNameCharPolicy::IsValidChar(c));
1970 }
1971}
1972
1973BOOST_AUTO_TEST_CASE(IsValidSwTraceStringTest)
1974{
1975 // Valid SWTrace strings
1976 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>(""));
1977 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("_"));
1978 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("0123"));
1979 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("valid_string"));
1980 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("VALID_string_456"));
1981 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>(" "));
1982 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("valid string"));
1983 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("!$%"));
1984 BOOST_CHECK(IsValidSwTraceString<SwTraceCharPolicy>("valid|\\~string#123"));
1985
1986 // Invalid SWTrace strings
1987 BOOST_CHECK(!IsValidSwTraceString<SwTraceCharPolicy>("€£"));
1988 BOOST_CHECK(!IsValidSwTraceString<SwTraceCharPolicy>("invalid‡string"));
1989 BOOST_CHECK(!IsValidSwTraceString<SwTraceCharPolicy>("12Ž34"));
1990}
1991
1992BOOST_AUTO_TEST_CASE(IsValidSwTraceNameStringTest)
1993{
1994 // Valid SWTrace name strings
1995 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>(""));
1996 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>("_"));
1997 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>("0123"));
1998 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>("valid_string"));
1999 BOOST_CHECK(IsValidSwTraceString<SwTraceNameCharPolicy>("VALID_string_456"));
2000
2001 // Invalid SWTrace name strings
2002 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>(" "));
2003 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("invalid string"));
2004 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("!$%"));
2005 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("invalid|\\~string#123"));
2006 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("€£"));
2007 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("invalid‡string"));
2008 BOOST_CHECK(!IsValidSwTraceString<SwTraceNameCharPolicy>("12Ž34"));
2009}
2010
2011template <typename SwTracePolicy>
2012void StringToSwTraceStringTestHelper(const std::string& testString, std::vector<uint32_t> buffer, size_t expectedSize)
2013{
2014 // Convert the test string to a SWTrace string
2015 BOOST_CHECK(StringToSwTraceString<SwTracePolicy>(testString, buffer));
2016
2017 // The buffer must contain at least the length of the string
2018 BOOST_CHECK(!buffer.empty());
2019
2020 // The buffer must be of the expected size (in words)
2021 BOOST_CHECK(buffer.size() == expectedSize);
2022
2023 // The first word of the byte must be the length of the string including the null-terminator
2024 BOOST_CHECK(buffer[0] == testString.size() + 1);
2025
2026 // The contents of the buffer must match the test string
2027 BOOST_CHECK(std::memcmp(testString.data(), buffer.data() + 1, testString.size()) == 0);
2028
2029 // The buffer must include the null-terminator at the end of the string
2030 size_t nullTerminatorIndex = sizeof(uint32_t) + testString.size();
Keith Davis3201eea2019-10-24 17:30:41 +01002031 BOOST_CHECK(reinterpret_cast<unsigned char*>(buffer.data())[nullTerminatorIndex] == '\0');
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01002032}
2033
2034BOOST_AUTO_TEST_CASE(StringToSwTraceStringTest)
2035{
2036 std::vector<uint32_t> buffer;
2037
2038 // Valid SWTrace strings (expected size in words)
2039 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("", buffer, 2);
2040 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("_", buffer, 2);
2041 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("0123", buffer, 3);
2042 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("valid_string", buffer, 5);
2043 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("VALID_string_456", buffer, 6);
2044 StringToSwTraceStringTestHelper<SwTraceCharPolicy>(" ", buffer, 2);
2045 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("valid string", buffer, 5);
2046 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("!$%", buffer, 2);
2047 StringToSwTraceStringTestHelper<SwTraceCharPolicy>("valid|\\~string#123", buffer, 6);
2048
2049 // Invalid SWTrace strings
2050 BOOST_CHECK(!StringToSwTraceString<SwTraceCharPolicy>("€£", buffer));
2051 BOOST_CHECK(buffer.empty());
2052 BOOST_CHECK(!StringToSwTraceString<SwTraceCharPolicy>("invalid‡string", buffer));
2053 BOOST_CHECK(buffer.empty());
2054 BOOST_CHECK(!StringToSwTraceString<SwTraceCharPolicy>("12Ž34", buffer));
2055 BOOST_CHECK(buffer.empty());
2056}
2057
2058BOOST_AUTO_TEST_CASE(StringToSwTraceNameStringTest)
2059{
2060 std::vector<uint32_t> buffer;
2061
2062 // Valid SWTrace namestrings (expected size in words)
2063 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("", buffer, 2);
2064 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("_", buffer, 2);
2065 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("0123", buffer, 3);
2066 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("valid_string", buffer, 5);
2067 StringToSwTraceStringTestHelper<SwTraceNameCharPolicy>("VALID_string_456", buffer, 6);
2068
2069 // Invalid SWTrace namestrings
2070 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>(" ", buffer));
2071 BOOST_CHECK(buffer.empty());
2072 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("invalid string", buffer));
2073 BOOST_CHECK(buffer.empty());
2074 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("!$%", buffer));
2075 BOOST_CHECK(buffer.empty());
2076 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("invalid|\\~string#123", buffer));
2077 BOOST_CHECK(buffer.empty());
2078 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("€£", buffer));
2079 BOOST_CHECK(buffer.empty());
2080 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("invalid‡string", buffer));
2081 BOOST_CHECK(buffer.empty());
2082 BOOST_CHECK(!StringToSwTraceString<SwTraceNameCharPolicy>("12Ž34", buffer));
2083 BOOST_CHECK(buffer.empty());
2084}
2085
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002086BOOST_AUTO_TEST_CASE(CheckPeriodicCounterCaptureThread)
2087{
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002088 class CaptureReader : public IReadCounterValues
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002089 {
2090 public:
Finn Williamsf4d59a62019-10-14 15:55:18 +01002091 CaptureReader(uint16_t counterSize)
2092 {
Keith Davis3201eea2019-10-24 17:30:41 +01002093 for (uint16_t i = 0; i < counterSize; ++i)
Finn Williamsf4d59a62019-10-14 15:55:18 +01002094 {
2095 m_Data[i] = 0;
2096 }
2097 m_CounterSize = counterSize;
2098 }
2099 //not used
Matteo Martincighe8485382019-10-10 14:08:21 +01002100 bool IsCounterRegistered(uint16_t counterUid) const override
2101 {
Derek Lamberti1dd75b32019-12-10 21:23:23 +00002102 boost::ignore_unused(counterUid);
Finn Williamsf4d59a62019-10-14 15:55:18 +01002103 return false;
Matteo Martincighe8485382019-10-10 14:08:21 +01002104 }
2105
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002106 uint16_t GetCounterCount() const override
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002107 {
Finn Williamsf4d59a62019-10-14 15:55:18 +01002108 return m_CounterSize;
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002109 }
2110
Matteo Martincighe8485382019-10-10 14:08:21 +01002111 uint32_t GetCounterValue(uint16_t counterUid) const override
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002112 {
Keith Davis3201eea2019-10-24 17:30:41 +01002113 if (counterUid > m_CounterSize)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002114 {
Finn Williamsf4d59a62019-10-14 15:55:18 +01002115 BOOST_FAIL("Invalid counter Uid");
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002116 }
Matteo Martincighe8485382019-10-10 14:08:21 +01002117 return m_Data.at(counterUid).load();
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002118 }
2119
Matteo Martincighe8485382019-10-10 14:08:21 +01002120 void SetCounterValue(uint16_t counterUid, uint32_t value)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002121 {
Keith Davis3201eea2019-10-24 17:30:41 +01002122 if (counterUid > m_CounterSize)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002123 {
Finn Williamsf4d59a62019-10-14 15:55:18 +01002124 BOOST_FAIL("Invalid counter Uid");
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002125 }
Finn Williamsf4d59a62019-10-14 15:55:18 +01002126 m_Data.at(counterUid).store(value);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002127 }
2128
2129 private:
Matteo Martincighe8485382019-10-10 14:08:21 +01002130 std::unordered_map<uint16_t, std::atomic<uint32_t>> m_Data;
Finn Williamsf4d59a62019-10-14 15:55:18 +01002131 uint16_t m_CounterSize;
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002132 };
2133
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002134 ProfilingStateMachine profilingStateMachine;
2135
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002136 Holder data;
2137 std::vector<uint16_t> captureIds1 = { 0, 1 };
2138 std::vector<uint16_t> captureIds2;
2139
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002140 MockBufferManager mockBuffer(512);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002141 SendCounterPacket sendCounterPacket(profilingStateMachine, mockBuffer);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002142
2143 std::vector<uint16_t> counterIds;
Finn Williamsf4d59a62019-10-14 15:55:18 +01002144 CaptureReader captureReader(2);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002145
Keith Davis3201eea2019-10-24 17:30:41 +01002146 unsigned int valueA = 10;
2147 unsigned int valueB = 15;
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002148 unsigned int numSteps = 5;
2149
2150 PeriodicCounterCapture periodicCounterCapture(std::ref(data), std::ref(sendCounterPacket), captureReader);
2151
Matteo Martincighe0e6efc2019-10-04 17:17:42 +01002152 for (unsigned int i = 0; i < numSteps; ++i)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002153 {
2154 data.SetCaptureData(1, captureIds1);
2155 captureReader.SetCounterValue(0, valueA * (i + 1));
2156 captureReader.SetCounterValue(1, valueB * (i + 1));
2157
2158 periodicCounterCapture.Start();
Finn Williamsf4d59a62019-10-14 15:55:18 +01002159 periodicCounterCapture.Stop();
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002160 }
2161
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002162 auto buffer = mockBuffer.GetReadableBuffer();
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002163
2164 uint32_t headerWord0 = ReadUint32(buffer, 0);
2165 uint32_t headerWord1 = ReadUint32(buffer, 4);
2166
Jim Flynnfc365622019-12-04 10:07:20 +00002167 BOOST_TEST(((headerWord0 >> 26) & 0x0000003F) == 3); // packet family
Keith Davis3201eea2019-10-24 17:30:41 +01002168 BOOST_TEST(((headerWord0 >> 19) & 0x0000007F) == 0); // packet class
2169 BOOST_TEST(((headerWord0 >> 16) & 0x00000007) == 0); // packet type
Matteo Martincigh8d9590e2019-10-15 09:35:29 +01002170 BOOST_TEST(headerWord1 == 20);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002171
Keith Davis3201eea2019-10-24 17:30:41 +01002172 uint32_t offset = 16;
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01002173 uint16_t readIndex = ReadUint16(buffer, offset);
2174 BOOST_TEST(0 == readIndex);
2175
2176 offset += 2;
2177 uint32_t readValue = ReadUint32(buffer, offset);
2178 BOOST_TEST((valueA * numSteps) == readValue);
2179
2180 offset += 4;
2181 readIndex = ReadUint16(buffer, offset);
2182 BOOST_TEST(1 == readIndex);
2183
2184 offset += 2;
2185 readValue = ReadUint32(buffer, offset);
2186 BOOST_TEST((valueB * numSteps) == readValue);
2187}
2188
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002189BOOST_AUTO_TEST_CASE(RequestCounterDirectoryCommandHandlerTest1)
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002190{
2191 using boost::numeric_cast;
2192
Jim Flynn397043f2019-10-17 17:37:10 +01002193 const uint32_t familyId = 0;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002194 const uint32_t packetId = 3;
Keith Davis3201eea2019-10-24 17:30:41 +01002195 const uint32_t version = 1;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002196 ProfilingStateMachine profilingStateMachine;
2197 CounterDirectory counterDirectory;
Matteo Martincigh9723d022019-11-13 10:56:41 +00002198 MockBufferManager mockBuffer1(1024);
2199 SendCounterPacket sendCounterPacket(profilingStateMachine, mockBuffer1);
2200 MockBufferManager mockBuffer2(1024);
2201 SendTimelinePacket sendTimelinePacket(mockBuffer2);
Keith Davis3201eea2019-10-24 17:30:41 +01002202 RequestCounterDirectoryCommandHandler commandHandler(familyId, packetId, version, counterDirectory,
Matteo Martincigh9723d022019-11-13 10:56:41 +00002203 sendCounterPacket, sendTimelinePacket, profilingStateMachine);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002204
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002205 const uint32_t wrongPacketId = 47;
Keith Davis3201eea2019-10-24 17:30:41 +01002206 const uint32_t wrongHeader = (wrongPacketId & 0x000003FF) << 16;
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002207
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002208 Packet wrongPacket(wrongHeader);
2209
2210 profilingStateMachine.TransitionToState(ProfilingState::Uninitialised);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002211 BOOST_CHECK_THROW(commandHandler(wrongPacket), armnn::RuntimeException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002212 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002213 BOOST_CHECK_THROW(commandHandler(wrongPacket), armnn::RuntimeException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002214 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002215 BOOST_CHECK_THROW(commandHandler(wrongPacket), armnn::RuntimeException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002216 profilingStateMachine.TransitionToState(ProfilingState::Active);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002217 BOOST_CHECK_THROW(commandHandler(wrongPacket), armnn::InvalidArgumentException); // Wrong packet
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002218
2219 const uint32_t rightHeader = (packetId & 0x000003FF) << 16;
2220
2221 Packet rightPacket(rightHeader);
2222
Matteo Martincigh9723d022019-11-13 10:56:41 +00002223 BOOST_CHECK_NO_THROW(commandHandler(rightPacket)); // Right packet
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002224
Matteo Martincigh9723d022019-11-13 10:56:41 +00002225 auto readBuffer1 = mockBuffer1.GetReadableBuffer();
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002226
Matteo Martincigh9723d022019-11-13 10:56:41 +00002227 uint32_t header1Word0 = ReadUint32(readBuffer1, 0);
2228 uint32_t header1Word1 = ReadUint32(readBuffer1, 4);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002229
Matteo Martincigh9723d022019-11-13 10:56:41 +00002230 // Counter directory packet
2231 BOOST_TEST(((header1Word0 >> 26) & 0x0000003F) == 0); // packet family
2232 BOOST_TEST(((header1Word0 >> 16) & 0x000003FF) == 2); // packet id
2233 BOOST_TEST(header1Word1 == 24); // data length
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002234
Matteo Martincigh9723d022019-11-13 10:56:41 +00002235 uint32_t bodyHeader1Word0 = ReadUint32(readBuffer1, 8);
2236 uint16_t deviceRecordCount = numeric_cast<uint16_t>(bodyHeader1Word0 >> 16);
2237 BOOST_TEST(deviceRecordCount == 0); // device_records_count
2238
2239 auto readBuffer2 = mockBuffer2.GetReadableBuffer();
2240
2241 uint32_t header2Word0 = ReadUint32(readBuffer2, 0);
2242 uint32_t header2Word1 = ReadUint32(readBuffer2, 4);
2243
2244 // Timeline message directory packet
2245 BOOST_TEST(((header2Word0 >> 26) & 0x0000003F) == 1); // packet family
2246 BOOST_TEST(((header2Word0 >> 16) & 0x000003FF) == 0); // packet id
2247 BOOST_TEST(header2Word1 == 419); // data length
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002248}
2249
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002250BOOST_AUTO_TEST_CASE(RequestCounterDirectoryCommandHandlerTest2)
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002251{
2252 using boost::numeric_cast;
2253
Jim Flynn397043f2019-10-17 17:37:10 +01002254 const uint32_t familyId = 0;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002255 const uint32_t packetId = 3;
Keith Davis3201eea2019-10-24 17:30:41 +01002256 const uint32_t version = 1;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002257 ProfilingStateMachine profilingStateMachine;
2258 CounterDirectory counterDirectory;
Matteo Martincigh9723d022019-11-13 10:56:41 +00002259 MockBufferManager mockBuffer1(1024);
2260 SendCounterPacket sendCounterPacket(profilingStateMachine, mockBuffer1);
2261 MockBufferManager mockBuffer2(1024);
2262 SendTimelinePacket sendTimelinePacket(mockBuffer2);
Keith Davis3201eea2019-10-24 17:30:41 +01002263 RequestCounterDirectoryCommandHandler commandHandler(familyId, packetId, version, counterDirectory,
Matteo Martincigh9723d022019-11-13 10:56:41 +00002264 sendCounterPacket, sendTimelinePacket, profilingStateMachine);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002265 const uint32_t header = (packetId & 0x000003FF) << 16;
2266 Packet packet(header);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002267
Matteo Martincigh9723d022019-11-13 10:56:41 +00002268 const Device* device = counterDirectory.RegisterDevice("deviceA", 1);
2269 BOOST_CHECK(device != nullptr);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002270 const CounterSet* counterSet = counterDirectory.RegisterCounterSet("countersetA");
Matteo Martincigh9723d022019-11-13 10:56:41 +00002271 BOOST_CHECK(counterSet != nullptr);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002272 counterDirectory.RegisterCategory("categoryA", device->m_Uid, counterSet->m_Uid);
Keith Davise394bd92019-12-02 15:12:19 +00002273 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID, 24,
2274 "categoryA", 0, 1, 2.0f, "counterA", "descA");
2275 counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID, 25,
2276 "categoryA", 1, 1, 3.0f, "counterB", "descB");
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002277
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002278 profilingStateMachine.TransitionToState(ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002279 BOOST_CHECK_THROW(commandHandler(packet), armnn::RuntimeException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002280 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002281 BOOST_CHECK_THROW(commandHandler(packet), armnn::RuntimeException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002282 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002283 BOOST_CHECK_THROW(commandHandler(packet), armnn::RuntimeException); // Wrong profiling state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002284 profilingStateMachine.TransitionToState(ProfilingState::Active);
2285 BOOST_CHECK_NO_THROW(commandHandler(packet));
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002286
Matteo Martincigh9723d022019-11-13 10:56:41 +00002287 auto readBuffer1 = mockBuffer1.GetReadableBuffer();
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002288
Matteo Martincigh9723d022019-11-13 10:56:41 +00002289 uint32_t header1Word0 = ReadUint32(readBuffer1, 0);
2290 uint32_t header1Word1 = ReadUint32(readBuffer1, 4);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002291
Matteo Martincigh9723d022019-11-13 10:56:41 +00002292 BOOST_TEST(((header1Word0 >> 26) & 0x0000003F) == 0); // packet family
2293 BOOST_TEST(((header1Word0 >> 16) & 0x000003FF) == 2); // packet id
2294 BOOST_TEST(header1Word1 == 240); // data length
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002295
Matteo Martincigh9723d022019-11-13 10:56:41 +00002296 uint32_t bodyHeader1Word0 = ReadUint32(readBuffer1, 8);
2297 uint32_t bodyHeader1Word1 = ReadUint32(readBuffer1, 12);
2298 uint32_t bodyHeader1Word2 = ReadUint32(readBuffer1, 16);
2299 uint32_t bodyHeader1Word3 = ReadUint32(readBuffer1, 20);
2300 uint32_t bodyHeader1Word4 = ReadUint32(readBuffer1, 24);
2301 uint32_t bodyHeader1Word5 = ReadUint32(readBuffer1, 28);
2302 uint16_t deviceRecordCount = numeric_cast<uint16_t>(bodyHeader1Word0 >> 16);
2303 uint16_t counterSetRecordCount = numeric_cast<uint16_t>(bodyHeader1Word2 >> 16);
2304 uint16_t categoryRecordCount = numeric_cast<uint16_t>(bodyHeader1Word4 >> 16);
2305 BOOST_TEST(deviceRecordCount == 1); // device_records_count
2306 BOOST_TEST(bodyHeader1Word1 == 0); // device_records_pointer_table_offset
2307 BOOST_TEST(counterSetRecordCount == 1); // counter_set_count
2308 BOOST_TEST(bodyHeader1Word3 == 4); // counter_set_pointer_table_offset
2309 BOOST_TEST(categoryRecordCount == 1); // categories_count
2310 BOOST_TEST(bodyHeader1Word5 == 8); // categories_pointer_table_offset
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002311
Matteo Martincigh9723d022019-11-13 10:56:41 +00002312 uint32_t deviceRecordOffset = ReadUint32(readBuffer1, 32);
Keith Davis3201eea2019-10-24 17:30:41 +01002313 BOOST_TEST(deviceRecordOffset == 0);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002314
Matteo Martincigh9723d022019-11-13 10:56:41 +00002315 uint32_t counterSetRecordOffset = ReadUint32(readBuffer1, 36);
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002316 BOOST_TEST(counterSetRecordOffset == 20);
2317
Matteo Martincigh9723d022019-11-13 10:56:41 +00002318 uint32_t categoryRecordOffset = ReadUint32(readBuffer1, 40);
Keith Davis3201eea2019-10-24 17:30:41 +01002319 BOOST_TEST(categoryRecordOffset == 44);
Matteo Martincigh9723d022019-11-13 10:56:41 +00002320
2321 auto readBuffer2 = mockBuffer2.GetReadableBuffer();
2322
2323 uint32_t header2Word0 = ReadUint32(readBuffer2, 0);
2324 uint32_t header2Word1 = ReadUint32(readBuffer2, 4);
2325
2326 // Timeline message directory packet
2327 BOOST_TEST(((header2Word0 >> 26) & 0x0000003F) == 1); // packet family
2328 BOOST_TEST(((header2Word0 >> 16) & 0x000003FF) == 0); // packet id
2329 BOOST_TEST(header2Word1 == 419); // data length
Narumol Prangnawarat48033692019-09-20 12:04:55 +01002330}
2331
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002332BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodConnectionAcknowledgedPacket)
2333{
Matteo Martincighd0613b52019-10-09 16:47:04 +01002334 // Swap the profiling connection factory in the profiling service instance with our mock one
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002335 SwapProfilingConnectionFactoryHelper helper;
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002336
2337 // Calculate the size of a Stream Metadata packet
Keith Davis3201eea2019-10-24 17:30:41 +01002338 std::string processName = GetProcessName().substr(0, 60);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002339 unsigned int processNameSize = processName.empty() ? 0 : boost::numeric_cast<unsigned int>(processName.size()) + 1;
2340 unsigned int streamMetadataPacketsize = 118 + processNameSize;
2341
Jim Flynn53e46992019-10-14 12:31:10 +01002342 // Reset the profiling service to the uninitialized state
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002343 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002344 options.m_EnableProfiling = true;
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002345 ProfilingService& profilingService = ProfilingService::Instance();
2346 profilingService.ResetExternalProfilingOptions(options, true);
2347
2348 // Bring the profiling service to the "WaitingForAck" state
2349 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002350 profilingService.Update(); // Initialize the counter directory
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002351 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002352 profilingService.Update(); // Create the profiling connection
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002353
Matteo Martincighd0613b52019-10-09 16:47:04 +01002354 // Get the mock profiling connection
2355 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2356 BOOST_CHECK(mockProfilingConnection);
2357
Matteo Martincighe8485382019-10-10 14:08:21 +01002358 // Remove the packets received so far
2359 mockProfilingConnection->Clear();
2360
2361 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002362 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002363
2364 // Wait for the Stream Metadata packet to be sent
Colm Donelan2ba48d22019-11-29 09:10:59 +00002365 helper.WaitForProfilingPacketsSent(mockProfilingConnection);
Matteo Martincighe8485382019-10-10 14:08:21 +01002366
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002367 // Check that the mock profiling connection contains one Stream Metadata packet
Matteo Martincighd0613b52019-10-09 16:47:04 +01002368 const std::vector<uint32_t> writtenData = mockProfilingConnection->GetWrittenData();
Colm Donelan2ba48d22019-11-29 09:10:59 +00002369 if (writtenData.size() > 1)
2370 {
2371 // If this thread has been blocked for some time a second or more Stream Metadata packet could have been sent.
2372 // In these cases make sure all packet are of length streamMetadataPacketsize
2373 for(uint32_t packetLength : writtenData)
2374 {
2375 BOOST_TEST(packetLength == streamMetadataPacketsize);
2376 }
2377 }
2378 else
2379 {
2380 BOOST_TEST(writtenData.size() == 1);
2381 BOOST_TEST(writtenData[0] == streamMetadataPacketsize);
2382 }
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002383
2384 // Write a valid "Connection Acknowledged" packet into the mock profiling connection, to simulate a valid
2385 // reply from an external profiling service
2386
2387 // Connection Acknowledged Packet header (word 0, word 1 is always zero):
2388 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2389 // 16:25 [10] packet_id: Packet identifier, value 0b0000000001
2390 // 8:15 [8] reserved: Reserved, value 0b00000000
2391 // 0:7 [8] reserved: Reserved, value 0b00000000
2392 uint32_t packetFamily = 0;
2393 uint32_t packetId = 1;
Keith Davis3201eea2019-10-24 17:30:41 +01002394 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002395
Matteo Martincighd0613b52019-10-09 16:47:04 +01002396 // Create the Connection Acknowledged Packet
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002397 Packet connectionAcknowledgedPacket(header);
2398
2399 // Write the packet to the mock profiling connection
2400 mockProfilingConnection->WritePacket(std::move(connectionAcknowledgedPacket));
2401
Colm Donelan2ba48d22019-11-29 09:10:59 +00002402 // Wait for the counter directory packet to ensure the ConnectionAcknowledgedCommandHandler has run.
2403 helper.WaitForProfilingPacketsSent(mockProfilingConnection, 5000);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002404
2405 // The Connection Acknowledged Command Handler should have updated the profiling state accordingly
2406 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincighd0613b52019-10-09 16:47:04 +01002407
2408 // Reset the profiling service to stop any running thread
2409 options.m_EnableProfiling = false;
2410 profilingService.ResetExternalProfilingOptions(options, true);
Matteo Martincigh54fb9572019-10-02 12:50:57 +01002411}
2412
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002413BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodRequestCounterDirectoryPacket)
2414{
2415 // Swap the profiling connection factory in the profiling service instance with our mock one
2416 SwapProfilingConnectionFactoryHelper helper;
2417
2418 // Reset the profiling service to the uninitialized state
2419 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002420 options.m_EnableProfiling = true;
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002421 ProfilingService& profilingService = ProfilingService::Instance();
2422 profilingService.ResetExternalProfilingOptions(options, true);
2423
2424 // Bring the profiling service to the "Active" state
2425 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002426 profilingService.Update(); // Initialize the counter directory
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002427 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002428 profilingService.Update(); // Create the profiling connection
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002429 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002430 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002431
Colm Donelan2ba48d22019-11-29 09:10:59 +00002432 // Get the mock profiling connection
2433 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2434 BOOST_CHECK(mockProfilingConnection);
2435
Matteo Martincighe8485382019-10-10 14:08:21 +01002436 // Wait for the Stream Metadata packet the be sent
2437 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Colm Donelan2ba48d22019-11-29 09:10:59 +00002438 helper.WaitForProfilingPacketsSent(mockProfilingConnection);
Matteo Martincighe8485382019-10-10 14:08:21 +01002439
2440 // Force the profiling service to the "Active" state
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002441 helper.ForceTransitionToState(ProfilingState::Active);
2442 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2443
Matteo Martincighe8485382019-10-10 14:08:21 +01002444 // Remove the packets received so far
2445 mockProfilingConnection->Clear();
2446
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002447 // Write a valid "Request Counter Directory" packet into the mock profiling connection, to simulate a valid
2448 // reply from an external profiling service
2449
2450 // Request Counter Directory packet header (word 0, word 1 is always zero):
2451 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2452 // 16:25 [10] packet_id: Packet identifier, value 0b0000000011
2453 // 8:15 [8] reserved: Reserved, value 0b00000000
2454 // 0:7 [8] reserved: Reserved, value 0b00000000
2455 uint32_t packetFamily = 0;
2456 uint32_t packetId = 3;
Keith Davis3201eea2019-10-24 17:30:41 +01002457 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002458
2459 // Create the Request Counter Directory packet
2460 Packet requestCounterDirectoryPacket(header);
2461
2462 // Write the packet to the mock profiling connection
2463 mockProfilingConnection->WritePacket(std::move(requestCounterDirectoryPacket));
2464
Matteo Martincighe8485382019-10-10 14:08:21 +01002465 // Wait for the Counter Directory packet to be sent
Colm Donelan2ba48d22019-11-29 09:10:59 +00002466 helper.WaitForProfilingPacketsSent(mockProfilingConnection, 5000);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002467
2468 // Check that the mock profiling connection contains one Counter Directory packet
2469 const std::vector<uint32_t> writtenData = mockProfilingConnection->GetWrittenData();
Matteo Martincigh9723d022019-11-13 10:56:41 +00002470 BOOST_TEST(writtenData.size() == 2);
2471 BOOST_TEST(writtenData[0] == 427); // The size of the expected Timeline Directory packet
Keith Davise394bd92019-12-02 15:12:19 +00002472 BOOST_TEST(writtenData[1] ==656); // The size of the expected Counter Directory packet
Matteo Martincighe8485382019-10-10 14:08:21 +01002473
2474 // The Request Counter Directory Command Handler should not have updated the profiling state
2475 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2476
2477 // Reset the profiling service to stop any running thread
2478 options.m_EnableProfiling = false;
2479 profilingService.ResetExternalProfilingOptions(options, true);
2480}
2481
Matteo Martincighe8485382019-10-10 14:08:21 +01002482BOOST_AUTO_TEST_CASE(CheckProfilingServiceBadPeriodicCounterSelectionPacketInvalidCounterUid)
2483{
Matteo Martincighe8485382019-10-10 14:08:21 +01002484 // Swap the profiling connection factory in the profiling service instance with our mock one
2485 SwapProfilingConnectionFactoryHelper helper;
2486
2487 // Reset the profiling service to the uninitialized state
2488 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002489 options.m_EnableProfiling = true;
Matteo Martincighe8485382019-10-10 14:08:21 +01002490 ProfilingService& profilingService = ProfilingService::Instance();
2491 profilingService.ResetExternalProfilingOptions(options, true);
2492
2493 // Bring the profiling service to the "Active" state
2494 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002495 profilingService.Update(); // Initialize the counter directory
Matteo Martincighe8485382019-10-10 14:08:21 +01002496 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002497 profilingService.Update(); // Create the profiling connection
Matteo Martincighe8485382019-10-10 14:08:21 +01002498 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002499 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002500
Colm Donelan2ba48d22019-11-29 09:10:59 +00002501 // Get the mock profiling connection
2502 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2503 BOOST_CHECK(mockProfilingConnection);
2504
Matteo Martincighe8485382019-10-10 14:08:21 +01002505 // Wait for the Stream Metadata packet the be sent
2506 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Colm Donelan2ba48d22019-11-29 09:10:59 +00002507 helper.WaitForProfilingPacketsSent(mockProfilingConnection);
Matteo Martincighe8485382019-10-10 14:08:21 +01002508
2509 // Force the profiling service to the "Active" state
2510 helper.ForceTransitionToState(ProfilingState::Active);
2511 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2512
Matteo Martincighe8485382019-10-10 14:08:21 +01002513 // Remove the packets received so far
2514 mockProfilingConnection->Clear();
2515
2516 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2517 // external profiling service
2518
2519 // Periodic Counter Selection packet header:
2520 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2521 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2522 // 8:15 [8] reserved: Reserved, value 0b00000000
2523 // 0:7 [8] reserved: Reserved, value 0b00000000
2524 uint32_t packetFamily = 0;
2525 uint32_t packetId = 4;
Keith Davis3201eea2019-10-24 17:30:41 +01002526 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincighe8485382019-10-10 14:08:21 +01002527
Keith Davis3201eea2019-10-24 17:30:41 +01002528 uint32_t capturePeriod = 123456; // Some capture period (microseconds)
Matteo Martincighe8485382019-10-10 14:08:21 +01002529
2530 // Get the first valid counter UID
2531 const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
Keith Davis3201eea2019-10-24 17:30:41 +01002532 const Counters& counters = counterDirectory.GetCounters();
Matteo Martincighe8485382019-10-10 14:08:21 +01002533 BOOST_CHECK(counters.size() > 1);
Keith Davis3201eea2019-10-24 17:30:41 +01002534 uint16_t counterUidA = counters.begin()->first; // First valid counter UID
2535 uint16_t counterUidB = 9999; // Second invalid counter UID
Matteo Martincighe8485382019-10-10 14:08:21 +01002536
2537 uint32_t length = 8;
2538
2539 auto data = std::make_unique<unsigned char[]>(length);
2540 WriteUint32(data.get(), 0, capturePeriod);
2541 WriteUint16(data.get(), 4, counterUidA);
2542 WriteUint16(data.get(), 6, counterUidB);
2543
2544 // Create the Periodic Counter Selection packet
Keith Davis3201eea2019-10-24 17:30:41 +01002545 Packet periodicCounterSelectionPacket(header, length, data); // Length > 0, this will start the Period Counter
2546 // Capture thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002547
2548 // Write the packet to the mock profiling connection
2549 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2550
2551 // Expecting one Periodic Counter Selection packet and at least one Periodic Counter Capture packet
2552 int expectedPackets = 2;
2553 std::vector<uint32_t> receivedPackets;
2554
2555 // Keep waiting until all the expected packets have been received
2556 do
2557 {
Colm Donelan2ba48d22019-11-29 09:10:59 +00002558 helper.WaitForProfilingPacketsSent(mockProfilingConnection);
Matteo Martincighe8485382019-10-10 14:08:21 +01002559 const std::vector<uint32_t> writtenData = mockProfilingConnection->GetWrittenData();
2560 if (writtenData.empty())
2561 {
2562 BOOST_ERROR("Packets should be available for reading at this point");
2563 return;
2564 }
2565 receivedPackets.insert(receivedPackets.end(), writtenData.begin(), writtenData.end());
2566 expectedPackets -= boost::numeric_cast<int>(writtenData.size());
Keith Davis3201eea2019-10-24 17:30:41 +01002567 } while (expectedPackets > 0);
Matteo Martincighe8485382019-10-10 14:08:21 +01002568 BOOST_TEST(!receivedPackets.empty());
2569
2570 // The size of the expected Periodic Counter Selection packet
2571 BOOST_TEST((std::find(receivedPackets.begin(), receivedPackets.end(), 14) != receivedPackets.end()));
2572 // The size of the expected Periodic Counter Capture packet
2573 BOOST_TEST((std::find(receivedPackets.begin(), receivedPackets.end(), 22) != receivedPackets.end()));
2574
2575 // The Periodic Counter Selection Handler should not have updated the profiling state
2576 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2577
2578 // Reset the profiling service to stop any running thread
2579 options.m_EnableProfiling = false;
2580 profilingService.ResetExternalProfilingOptions(options, true);
2581}
2582
2583BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodPeriodicCounterSelectionPacketNoCounters)
2584{
2585 // Swap the profiling connection factory in the profiling service instance with our mock one
2586 SwapProfilingConnectionFactoryHelper helper;
2587
2588 // Reset the profiling service to the uninitialized state
2589 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002590 options.m_EnableProfiling = true;
Matteo Martincighe8485382019-10-10 14:08:21 +01002591 ProfilingService& profilingService = ProfilingService::Instance();
2592 profilingService.ResetExternalProfilingOptions(options, true);
2593
2594 // Bring the profiling service to the "Active" state
2595 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002596 profilingService.Update(); // Initialize the counter directory
Matteo Martincighe8485382019-10-10 14:08:21 +01002597 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002598 profilingService.Update(); // Create the profiling connection
Matteo Martincighe8485382019-10-10 14:08:21 +01002599 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002600 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002601
Colm Donelan2ba48d22019-11-29 09:10:59 +00002602 // Get the mock profiling connection
2603 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2604 BOOST_CHECK(mockProfilingConnection);
2605
Matteo Martincighe8485382019-10-10 14:08:21 +01002606 // Wait for the Stream Metadata packet the be sent
2607 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Colm Donelan2ba48d22019-11-29 09:10:59 +00002608 helper.WaitForProfilingPacketsSent(mockProfilingConnection);
Matteo Martincighe8485382019-10-10 14:08:21 +01002609
2610 // Force the profiling service to the "Active" state
2611 helper.ForceTransitionToState(ProfilingState::Active);
2612 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2613
Matteo Martincighe8485382019-10-10 14:08:21 +01002614 // Remove the packets received so far
2615 mockProfilingConnection->Clear();
2616
2617 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2618 // external profiling service
2619
2620 // Periodic Counter Selection packet header:
2621 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2622 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2623 // 8:15 [8] reserved: Reserved, value 0b00000000
2624 // 0:7 [8] reserved: Reserved, value 0b00000000
2625 uint32_t packetFamily = 0;
2626 uint32_t packetId = 4;
Keith Davis3201eea2019-10-24 17:30:41 +01002627 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincighe8485382019-10-10 14:08:21 +01002628
2629 // Create the Periodic Counter Selection packet
Keith Davis3201eea2019-10-24 17:30:41 +01002630 Packet periodicCounterSelectionPacket(header); // Length == 0, this will disable the collection of counters
Matteo Martincighe8485382019-10-10 14:08:21 +01002631
2632 // Write the packet to the mock profiling connection
2633 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2634
2635 // Wait for the Periodic Counter Selection packet to be sent
Colm Donelan2ba48d22019-11-29 09:10:59 +00002636 helper.WaitForProfilingPacketsSent(mockProfilingConnection, 5000);
Matteo Martincighe8485382019-10-10 14:08:21 +01002637
2638 // The Periodic Counter Selection Handler should not have updated the profiling state
2639 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2640
2641 // Check that the mock profiling connection contains one Periodic Counter Selection
2642 const std::vector<uint32_t> writtenData = mockProfilingConnection->GetWrittenData();
Keith Davis3201eea2019-10-24 17:30:41 +01002643 BOOST_TEST(writtenData.size() == 1); // Only one packet is expected (no Periodic Counter packets)
2644 BOOST_TEST(writtenData[0] == 12); // The size of the expected Periodic Counter Selection (echos the sent one)
Matteo Martincighe8485382019-10-10 14:08:21 +01002645
2646 // Reset the profiling service to stop any running thread
2647 options.m_EnableProfiling = false;
2648 profilingService.ResetExternalProfilingOptions(options, true);
2649}
2650
2651BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodPeriodicCounterSelectionPacketSingleCounter)
2652{
2653 // Swap the profiling connection factory in the profiling service instance with our mock one
2654 SwapProfilingConnectionFactoryHelper helper;
2655
2656 // Reset the profiling service to the uninitialized state
2657 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002658 options.m_EnableProfiling = true;
Matteo Martincighe8485382019-10-10 14:08:21 +01002659 ProfilingService& profilingService = ProfilingService::Instance();
2660 profilingService.ResetExternalProfilingOptions(options, true);
2661
2662 // Bring the profiling service to the "Active" state
2663 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002664 profilingService.Update(); // Initialize the counter directory
Matteo Martincighe8485382019-10-10 14:08:21 +01002665 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002666 profilingService.Update(); // Create the profiling connection
Matteo Martincighe8485382019-10-10 14:08:21 +01002667 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002668 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002669
Colm Donelan2ba48d22019-11-29 09:10:59 +00002670 // Get the mock profiling connection
2671 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2672 BOOST_CHECK(mockProfilingConnection);
2673
Matteo Martincighe8485382019-10-10 14:08:21 +01002674 // Wait for the Stream Metadata packet the be sent
2675 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Colm Donelan2ba48d22019-11-29 09:10:59 +00002676 helper.WaitForProfilingPacketsSent(mockProfilingConnection);
Matteo Martincighe8485382019-10-10 14:08:21 +01002677
2678 // Force the profiling service to the "Active" state
2679 helper.ForceTransitionToState(ProfilingState::Active);
2680 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2681
Matteo Martincighe8485382019-10-10 14:08:21 +01002682 // Remove the packets received so far
2683 mockProfilingConnection->Clear();
2684
2685 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2686 // external profiling service
2687
2688 // Periodic Counter Selection packet header:
2689 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2690 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2691 // 8:15 [8] reserved: Reserved, value 0b00000000
2692 // 0:7 [8] reserved: Reserved, value 0b00000000
2693 uint32_t packetFamily = 0;
2694 uint32_t packetId = 4;
Keith Davis3201eea2019-10-24 17:30:41 +01002695 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincighe8485382019-10-10 14:08:21 +01002696
Keith Davis3201eea2019-10-24 17:30:41 +01002697 uint32_t capturePeriod = 123456; // Some capture period (microseconds)
Matteo Martincighe8485382019-10-10 14:08:21 +01002698
2699 // Get the first valid counter UID
2700 const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
Keith Davis3201eea2019-10-24 17:30:41 +01002701 const Counters& counters = counterDirectory.GetCounters();
Matteo Martincighe8485382019-10-10 14:08:21 +01002702 BOOST_CHECK(!counters.empty());
Keith Davis3201eea2019-10-24 17:30:41 +01002703 uint16_t counterUid = counters.begin()->first; // Valid counter UID
Matteo Martincighe8485382019-10-10 14:08:21 +01002704
2705 uint32_t length = 6;
2706
2707 auto data = std::make_unique<unsigned char[]>(length);
2708 WriteUint32(data.get(), 0, capturePeriod);
2709 WriteUint16(data.get(), 4, counterUid);
2710
2711 // Create the Periodic Counter Selection packet
Keith Davis3201eea2019-10-24 17:30:41 +01002712 Packet periodicCounterSelectionPacket(header, length, data); // Length > 0, this will start the Period Counter
2713 // Capture thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002714
2715 // Write the packet to the mock profiling connection
2716 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2717
2718 // Expecting one Periodic Counter Selection packet and at least one Periodic Counter Capture packet
2719 int expectedPackets = 2;
2720 std::vector<uint32_t> receivedPackets;
2721
2722 // Keep waiting until all the expected packets have been received
2723 do
2724 {
Colm Donelan2ba48d22019-11-29 09:10:59 +00002725 helper.WaitForProfilingPacketsSent(mockProfilingConnection);
Matteo Martincighe8485382019-10-10 14:08:21 +01002726 const std::vector<uint32_t> writtenData = mockProfilingConnection->GetWrittenData();
2727 if (writtenData.empty())
2728 {
2729 BOOST_ERROR("Packets should be available for reading at this point");
2730 return;
2731 }
2732 receivedPackets.insert(receivedPackets.end(), writtenData.begin(), writtenData.end());
2733 expectedPackets -= boost::numeric_cast<int>(writtenData.size());
Keith Davis3201eea2019-10-24 17:30:41 +01002734 } while (expectedPackets > 0);
Matteo Martincighe8485382019-10-10 14:08:21 +01002735 BOOST_TEST(!receivedPackets.empty());
2736
2737 // The size of the expected Periodic Counter Selection packet (echos the sent one)
2738 BOOST_TEST((std::find(receivedPackets.begin(), receivedPackets.end(), 14) != receivedPackets.end()));
2739 // The size of the expected Periodic Counter Capture packet
2740 BOOST_TEST((std::find(receivedPackets.begin(), receivedPackets.end(), 22) != receivedPackets.end()));
2741
2742 // The Periodic Counter Selection Handler should not have updated the profiling state
2743 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2744
2745 // Reset the profiling service to stop any running thread
2746 options.m_EnableProfiling = false;
2747 profilingService.ResetExternalProfilingOptions(options, true);
2748}
2749
2750BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodPeriodicCounterSelectionPacketMultipleCounters)
2751{
2752 // Swap the profiling connection factory in the profiling service instance with our mock one
2753 SwapProfilingConnectionFactoryHelper helper;
Matteo Martincighe8485382019-10-10 14:08:21 +01002754 // Reset the profiling service to the uninitialized state
2755 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002756 options.m_EnableProfiling = true;
Matteo Martincighe8485382019-10-10 14:08:21 +01002757 ProfilingService& profilingService = ProfilingService::Instance();
2758 profilingService.ResetExternalProfilingOptions(options, true);
2759
2760 // Bring the profiling service to the "Active" state
2761 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002762 profilingService.Update(); // Initialize the counter directory
Matteo Martincighe8485382019-10-10 14:08:21 +01002763 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002764 profilingService.Update(); // Create the profiling connection
Matteo Martincighe8485382019-10-10 14:08:21 +01002765 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002766 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002767
Colm Donelan2ba48d22019-11-29 09:10:59 +00002768 // Get the mock profiling connection
2769 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2770 BOOST_CHECK(mockProfilingConnection);
2771
Matteo Martincighe8485382019-10-10 14:08:21 +01002772 // Wait for the Stream Metadata packet the be sent
2773 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Colm Donelan2ba48d22019-11-29 09:10:59 +00002774 helper.WaitForProfilingPacketsSent(mockProfilingConnection);
Matteo Martincighe8485382019-10-10 14:08:21 +01002775
2776 // Force the profiling service to the "Active" state
2777 helper.ForceTransitionToState(ProfilingState::Active);
2778 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2779
Matteo Martincighe8485382019-10-10 14:08:21 +01002780 // Remove the packets received so far
2781 mockProfilingConnection->Clear();
2782
2783 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
2784 // external profiling service
2785
2786 // Periodic Counter Selection packet header:
2787 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2788 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2789 // 8:15 [8] reserved: Reserved, value 0b00000000
2790 // 0:7 [8] reserved: Reserved, value 0b00000000
2791 uint32_t packetFamily = 0;
2792 uint32_t packetId = 4;
Keith Davis3201eea2019-10-24 17:30:41 +01002793 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincighe8485382019-10-10 14:08:21 +01002794
Keith Davis3201eea2019-10-24 17:30:41 +01002795 uint32_t capturePeriod = 123456; // Some capture period (microseconds)
Matteo Martincighe8485382019-10-10 14:08:21 +01002796
2797 // Get the first valid counter UID
2798 const ICounterDirectory& counterDirectory = profilingService.GetCounterDirectory();
Keith Davis3201eea2019-10-24 17:30:41 +01002799 const Counters& counters = counterDirectory.GetCounters();
Matteo Martincighe8485382019-10-10 14:08:21 +01002800 BOOST_CHECK(counters.size() > 1);
Keith Davis3201eea2019-10-24 17:30:41 +01002801 uint16_t counterUidA = counters.begin()->first; // First valid counter UID
2802 uint16_t counterUidB = (counters.begin()++)->first; // Second valid counter UID
Matteo Martincighe8485382019-10-10 14:08:21 +01002803
2804 uint32_t length = 8;
2805
2806 auto data = std::make_unique<unsigned char[]>(length);
2807 WriteUint32(data.get(), 0, capturePeriod);
2808 WriteUint16(data.get(), 4, counterUidA);
2809 WriteUint16(data.get(), 6, counterUidB);
2810
2811 // Create the Periodic Counter Selection packet
Keith Davis3201eea2019-10-24 17:30:41 +01002812 Packet periodicCounterSelectionPacket(header, length, data); // Length > 0, this will start the Period Counter
2813 // Capture thread
Matteo Martincighe8485382019-10-10 14:08:21 +01002814
2815 // Write the packet to the mock profiling connection
2816 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2817
2818 // Expecting one Periodic Counter Selection packet and at least one Periodic Counter Capture packet
2819 int expectedPackets = 2;
2820 std::vector<uint32_t> receivedPackets;
2821
2822 // Keep waiting until all the expected packets have been received
2823 do
2824 {
Colm Donelan2ba48d22019-11-29 09:10:59 +00002825 helper.WaitForProfilingPacketsSent(mockProfilingConnection);
Matteo Martincighe8485382019-10-10 14:08:21 +01002826 const std::vector<uint32_t> writtenData = mockProfilingConnection->GetWrittenData();
2827 if (writtenData.empty())
2828 {
2829 BOOST_ERROR("Packets should be available for reading at this point");
2830 return;
2831 }
2832 receivedPackets.insert(receivedPackets.end(), writtenData.begin(), writtenData.end());
2833 expectedPackets -= boost::numeric_cast<int>(writtenData.size());
Keith Davis3201eea2019-10-24 17:30:41 +01002834 } while (expectedPackets > 0);
Matteo Martincighe8485382019-10-10 14:08:21 +01002835 BOOST_TEST(!receivedPackets.empty());
2836
2837 // The size of the expected Periodic Counter Selection packet (echos the sent one)
2838 BOOST_TEST((std::find(receivedPackets.begin(), receivedPackets.end(), 16) != receivedPackets.end()));
2839 // The size of the expected Periodic Counter Capture packet
2840 BOOST_TEST((std::find(receivedPackets.begin(), receivedPackets.end(), 28) != receivedPackets.end()));
2841
2842 // The Periodic Counter Selection Handler should not have updated the profiling state
2843 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
Matteo Martincigh8efc5002019-10-10 14:30:29 +01002844
2845 // Reset the profiling service to stop any running thread
2846 options.m_EnableProfiling = false;
2847 profilingService.ResetExternalProfilingOptions(options, true);
2848}
2849
Jim Flynn53e46992019-10-14 12:31:10 +01002850BOOST_AUTO_TEST_CASE(CheckProfilingServiceDisconnect)
2851{
2852 // Swap the profiling connection factory in the profiling service instance with our mock one
2853 SwapProfilingConnectionFactoryHelper helper;
Jim Flynn53e46992019-10-14 12:31:10 +01002854 // Reset the profiling service to the uninitialized state
2855 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002856 options.m_EnableProfiling = true;
Jim Flynn53e46992019-10-14 12:31:10 +01002857 ProfilingService& profilingService = ProfilingService::Instance();
2858 profilingService.ResetExternalProfilingOptions(options, true);
2859
2860 // Try to disconnect the profiling service while in the "Uninitialised" state
2861 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
2862 profilingService.Disconnect();
Keith Davis3201eea2019-10-24 17:30:41 +01002863 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised); // The state should not change
Jim Flynn53e46992019-10-14 12:31:10 +01002864
2865 // Try to disconnect the profiling service while in the "NotConnected" state
Keith Davis3201eea2019-10-24 17:30:41 +01002866 profilingService.Update(); // Initialize the counter directory
Jim Flynn53e46992019-10-14 12:31:10 +01002867 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
2868 profilingService.Disconnect();
Keith Davis3201eea2019-10-24 17:30:41 +01002869 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected); // The state should not change
Jim Flynn53e46992019-10-14 12:31:10 +01002870
2871 // Try to disconnect the profiling service while in the "WaitingForAck" state
Keith Davis3201eea2019-10-24 17:30:41 +01002872 profilingService.Update(); // Create the profiling connection
Jim Flynn53e46992019-10-14 12:31:10 +01002873 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
2874 profilingService.Disconnect();
Keith Davis3201eea2019-10-24 17:30:41 +01002875 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck); // The state should not change
Jim Flynn53e46992019-10-14 12:31:10 +01002876
2877 // Try to disconnect the profiling service while in the "Active" state
Keith Davis3201eea2019-10-24 17:30:41 +01002878 profilingService.Update(); // Start the command handler and the send thread
Jim Flynn53e46992019-10-14 12:31:10 +01002879
Colm Donelan2ba48d22019-11-29 09:10:59 +00002880 // Get the mock profiling connection
2881 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2882 BOOST_CHECK(mockProfilingConnection);
2883
Jim Flynn53e46992019-10-14 12:31:10 +01002884 // Wait for the Stream Metadata packet the be sent
2885 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Colm Donelan2ba48d22019-11-29 09:10:59 +00002886 helper.WaitForProfilingPacketsSent(mockProfilingConnection);
Jim Flynn53e46992019-10-14 12:31:10 +01002887
2888 // Force the profiling service to the "Active" state
2889 helper.ForceTransitionToState(ProfilingState::Active);
2890 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2891
Jim Flynn53e46992019-10-14 12:31:10 +01002892 // Check that the profiling connection is open
2893 BOOST_CHECK(mockProfilingConnection->IsOpen());
2894
2895 profilingService.Disconnect();
Colm Donelan2ba48d22019-11-29 09:10:59 +00002896 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected); // The state should have changed
Jim Flynn53e46992019-10-14 12:31:10 +01002897
2898 // Check that the profiling connection has been reset
2899 mockProfilingConnection = helper.GetMockProfilingConnection();
2900 BOOST_CHECK(mockProfilingConnection == nullptr);
2901
2902 // Reset the profiling service to stop any running thread
2903 options.m_EnableProfiling = false;
2904 profilingService.ResetExternalProfilingOptions(options, true);
2905}
2906
Matteo Martincigh994b5342019-10-11 17:19:56 +01002907BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodPerJobCounterSelectionPacket)
2908{
2909 // Swap the profiling connection factory in the profiling service instance with our mock one
2910 SwapProfilingConnectionFactoryHelper helper;
Matteo Martincigh994b5342019-10-11 17:19:56 +01002911 // Reset the profiling service to the uninitialized state
2912 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002913 options.m_EnableProfiling = true;
Matteo Martincigh994b5342019-10-11 17:19:56 +01002914 ProfilingService& profilingService = ProfilingService::Instance();
2915 profilingService.ResetExternalProfilingOptions(options, true);
2916
2917 // Bring the profiling service to the "Active" state
2918 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
Keith Davis3201eea2019-10-24 17:30:41 +01002919 profilingService.Update(); // Initialize the counter directory
Matteo Martincigh994b5342019-10-11 17:19:56 +01002920 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
Keith Davis3201eea2019-10-24 17:30:41 +01002921 profilingService.Update(); // Create the profiling connection
Matteo Martincigh994b5342019-10-11 17:19:56 +01002922 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
Keith Davis3201eea2019-10-24 17:30:41 +01002923 profilingService.Update(); // Start the command handler and the send thread
Matteo Martincigh994b5342019-10-11 17:19:56 +01002924
Colm Donelan2ba48d22019-11-29 09:10:59 +00002925 // Get the mock profiling connection
2926 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
2927 BOOST_CHECK(mockProfilingConnection);
2928
Matteo Martincigh994b5342019-10-11 17:19:56 +01002929 // Wait for the Stream Metadata packet the be sent
2930 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
Colm Donelan2ba48d22019-11-29 09:10:59 +00002931 helper.WaitForProfilingPacketsSent(mockProfilingConnection);
Matteo Martincigh994b5342019-10-11 17:19:56 +01002932
2933 // Force the profiling service to the "Active" state
2934 helper.ForceTransitionToState(ProfilingState::Active);
2935 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2936
Matteo Martincigh994b5342019-10-11 17:19:56 +01002937 // Remove the packets received so far
2938 mockProfilingConnection->Clear();
2939
2940 // Write a "Per-Job Counter Selection" packet into the mock profiling connection, to simulate an input from an
2941 // external profiling service
2942
2943 // Per-Job Counter Selection packet header:
2944 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
2945 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
2946 // 8:15 [8] reserved: Reserved, value 0b00000000
2947 // 0:7 [8] reserved: Reserved, value 0b00000000
2948 uint32_t packetFamily = 0;
2949 uint32_t packetId = 5;
Keith Davis3201eea2019-10-24 17:30:41 +01002950 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
Matteo Martincigh994b5342019-10-11 17:19:56 +01002951
2952 // Create the Per-Job Counter Selection packet
Keith Davis3201eea2019-10-24 17:30:41 +01002953 Packet periodicCounterSelectionPacket(header); // Length == 0, this will disable the collection of counters
Matteo Martincigh994b5342019-10-11 17:19:56 +01002954
2955 // Write the packet to the mock profiling connection
2956 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
2957
2958 // Wait for a bit (must at least be the delay value of the mock profiling connection) to make sure that
2959 // the Per-Job Counter Selection packet gets processed by the profiling service
Colm Donelan2ba48d22019-11-29 09:10:59 +00002960 std::this_thread::sleep_for(std::chrono::milliseconds(5));
Matteo Martincigh994b5342019-10-11 17:19:56 +01002961
2962 // The Per-Job Counter Selection packets are dropped silently, so there should be no reply coming
2963 // from the profiling service
2964 const std::vector<uint32_t> writtenData = mockProfilingConnection->GetWrittenData();
2965 BOOST_TEST(writtenData.empty());
2966
2967 // The Per-Job Counter Selection Command Handler should not have updated the profiling state
2968 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
2969
2970 // Reset the profiling service to stop any running thread
2971 options.m_EnableProfiling = false;
2972 profilingService.ResetExternalProfilingOptions(options, true);
2973}
2974
Jim Flynn672d06e2019-10-15 10:18:11 +01002975BOOST_AUTO_TEST_CASE(CheckConfigureProfilingServiceOn)
2976{
2977 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
Keith Davis3201eea2019-10-24 17:30:41 +01002978 options.m_EnableProfiling = true;
Jim Flynn672d06e2019-10-15 10:18:11 +01002979 ProfilingService& profilingService = ProfilingService::Instance();
2980 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
2981 profilingService.ConfigureProfilingService(options);
2982 // should get as far as NOT_CONNECTED
2983 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
2984 // Reset the profiling service to stop any running thread
2985 options.m_EnableProfiling = false;
2986 profilingService.ResetExternalProfilingOptions(options, true);
2987}
2988
2989BOOST_AUTO_TEST_CASE(CheckConfigureProfilingServiceOff)
2990{
2991 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
2992 ProfilingService& profilingService = ProfilingService::Instance();
2993 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
2994 profilingService.ConfigureProfilingService(options);
2995 // should not move from Uninitialised
2996 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
2997 // Reset the profiling service to stop any running thread
2998 options.m_EnableProfiling = false;
2999 profilingService.ResetExternalProfilingOptions(options, true);
3000}
3001
Colm Donelan2ba48d22019-11-29 09:10:59 +00003002BOOST_AUTO_TEST_CASE(CheckProfilingServiceEnabled)
3003{
3004 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
3005 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
3006 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
3007 options.m_EnableProfiling = true;
3008 ProfilingService& profilingService = ProfilingService::Instance();
3009 profilingService.ResetExternalProfilingOptions(options, true);
3010 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3011 profilingService.Update();
3012 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
3013
3014 // Redirect the output to a local stream so that we can parse the warning message
3015 std::stringstream ss;
3016 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3017 profilingService.Update();
3018 streamRedirector.CancelRedirect();
3019
3020 // Check that the expected error has occurred and logged to the standard output
3021 if (!boost::contains(ss.str(), "Cannot connect to stream socket: Connection refused"))
3022 {
3023 std::cout << ss.str();
3024 BOOST_FAIL("Expected string not found.");
3025 }
3026 // Reset the profiling service to stop any running thread
3027 options.m_EnableProfiling = false;
3028 profilingService.ResetExternalProfilingOptions(options, true);
3029}
3030
3031BOOST_AUTO_TEST_CASE(CheckProfilingServiceEnabledRuntime)
3032{
3033 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
3034 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
3035 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
3036 ProfilingService& profilingService = ProfilingService::Instance();
3037 profilingService.ResetExternalProfilingOptions(options, true);
3038 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3039 profilingService.Update();
3040 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3041 options.m_EnableProfiling = true;
3042 profilingService.ResetExternalProfilingOptions(options);
3043 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3044 profilingService.Update();
3045 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
3046
3047 // Redirect the output to a local stream so that we can parse the warning message
3048 std::stringstream ss;
3049 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3050 profilingService.Update();
3051
3052 streamRedirector.CancelRedirect();
3053
3054 // Check that the expected error has occurred and logged to the standard output
3055 if (!boost::contains(ss.str(), "Cannot connect to stream socket: Connection refused"))
3056 {
3057 std::cout << ss.str();
3058 BOOST_FAIL("Expected string not found.");
3059 }
3060 // Reset the profiling service to stop any running thread
3061 options.m_EnableProfiling = false;
3062 profilingService.ResetExternalProfilingOptions(options, true);
3063}
3064
3065BOOST_AUTO_TEST_CASE(CheckProfilingServiceBadConnectionAcknowledgedPacket)
3066{
3067 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
3068 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
3069 // Swap the profiling connection factory in the profiling service instance with our mock one
3070 SwapProfilingConnectionFactoryHelper helper;
3071
3072 // Redirect the standard output to a local stream so that we can parse the warning message
3073 std::stringstream ss;
3074 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3075
3076 // Calculate the size of a Stream Metadata packet
3077 std::string processName = GetProcessName().substr(0, 60);
3078 unsigned int processNameSize = processName.empty() ? 0 : boost::numeric_cast<unsigned int>(processName.size()) + 1;
3079 unsigned int streamMetadataPacketsize = 118 + processNameSize;
3080
3081 // Reset the profiling service to the uninitialized state
3082 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
3083 options.m_EnableProfiling = true;
3084 ProfilingService& profilingService = ProfilingService::Instance();
3085 profilingService.ResetExternalProfilingOptions(options, true);
3086
3087 // Bring the profiling service to the "WaitingForAck" state
3088 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3089 profilingService.Update(); // Initialize the counter directory
3090 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
3091 profilingService.Update(); // Create the profiling connection
3092
3093 // Get the mock profiling connection
3094 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
3095 BOOST_CHECK(mockProfilingConnection);
3096
3097 // Remove the packets received so far
3098 mockProfilingConnection->Clear();
3099
3100 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
3101 profilingService.Update();
3102
3103 // Wait for the Stream Metadata packet to be sent
3104 helper.WaitForProfilingPacketsSent(mockProfilingConnection);
3105
3106 // Check that the mock profiling connection contains one Stream Metadata packet
3107 const std::vector<uint32_t> writtenData = mockProfilingConnection->GetWrittenData();
3108 if (writtenData.size() > 1)
3109 {
3110 // If this thread has been blocked for some time a second or more Stream Metadata packet could have been sent.
3111 // In these cases make sure all packet are of length streamMetadataPacketsize
3112 for(uint32_t packetLength : writtenData)
3113 {
3114 BOOST_TEST(packetLength == streamMetadataPacketsize);
3115 }
3116 }
3117 else
3118 {
3119 BOOST_TEST(writtenData.size() == 1);
3120 BOOST_TEST(writtenData[0] == streamMetadataPacketsize);
3121 }
3122
3123 // Write a valid "Connection Acknowledged" packet into the mock profiling connection, to simulate a valid
3124 // reply from an external profiling service
3125
3126 // Connection Acknowledged Packet header (word 0, word 1 is always zero):
3127 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
3128 // 16:25 [10] packet_id: Packet identifier, value 0b0000000001
3129 // 8:15 [8] reserved: Reserved, value 0b00000000
3130 // 0:7 [8] reserved: Reserved, value 0b00000000
3131 uint32_t packetFamily = 0;
3132 uint32_t packetId = 37; // Wrong packet id!!!
3133 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
3134
3135 // Create the Connection Acknowledged Packet
3136 Packet connectionAcknowledgedPacket(header);
3137
3138 // Write the packet to the mock profiling connection
3139 mockProfilingConnection->WritePacket(std::move(connectionAcknowledgedPacket));
3140
3141 // Wait for a bit (must at least be the delay value of the mock profiling connection) to make sure that
3142 // the Connection Acknowledged packet gets processed by the profiling service
Keith Davise394bd92019-12-02 15:12:19 +00003143 std::this_thread::sleep_for(std::chrono::milliseconds(15));
Colm Donelan2ba48d22019-11-29 09:10:59 +00003144
3145 streamRedirector.CancelRedirect();
3146
3147 // Check that the expected error has occurred and logged to the standard output
3148 if (!boost::contains(ss.str(), "Functor with requested PacketId=37 and Version=4194304 does not exist"))
3149 {
3150 std::cout << ss.str();
3151 BOOST_FAIL("Expected string not found.");
3152 }
3153
3154 // The Connection Acknowledged Command Handler should not have updated the profiling state
3155 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
3156
3157 // Reset the profiling service to stop any running thread
3158 options.m_EnableProfiling = false;
3159 profilingService.ResetExternalProfilingOptions(options, true);
3160}
3161
3162BOOST_AUTO_TEST_CASE(CheckProfilingServiceBadRequestCounterDirectoryPacket)
3163{
3164 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
3165 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
3166 // Swap the profiling connection factory in the profiling service instance with our mock one
3167 SwapProfilingConnectionFactoryHelper helper;
3168
3169 // Redirect the standard output to a local stream so that we can parse the warning message
3170 std::stringstream ss;
3171 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3172
3173 // Reset the profiling service to the uninitialized state
3174 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
3175 options.m_EnableProfiling = true;
3176 ProfilingService& profilingService = ProfilingService::Instance();
3177 profilingService.ResetExternalProfilingOptions(options, true);
3178
3179 // Bring the profiling service to the "Active" state
3180 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3181 helper.ForceTransitionToState(ProfilingState::NotConnected);
3182 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
3183 profilingService.Update(); // Create the profiling connection
3184 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
3185 profilingService.Update(); // Start the command handler and the send thread
3186
3187 // Get the mock profiling connection
3188 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
3189 BOOST_CHECK(mockProfilingConnection);
3190
3191 // Wait for the Stream Metadata packet the be sent
3192 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
3193 helper.WaitForProfilingPacketsSent(mockProfilingConnection);
3194
3195 // Force the profiling service to the "Active" state
3196 helper.ForceTransitionToState(ProfilingState::Active);
3197 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
3198
3199 // Remove the packets received so far
3200 mockProfilingConnection->Clear();
3201
3202 // Write a valid "Request Counter Directory" packet into the mock profiling connection, to simulate a valid
3203 // reply from an external profiling service
3204
3205 // Request Counter Directory packet header (word 0, word 1 is always zero):
3206 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
3207 // 16:25 [10] packet_id: Packet identifier, value 0b0000000011
3208 // 8:15 [8] reserved: Reserved, value 0b00000000
3209 // 0:7 [8] reserved: Reserved, value 0b00000000
3210 uint32_t packetFamily = 0;
3211 uint32_t packetId = 123; // Wrong packet id!!!
3212 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
3213
3214 // Create the Request Counter Directory packet
3215 Packet requestCounterDirectoryPacket(header);
3216
3217 // Write the packet to the mock profiling connection
3218 mockProfilingConnection->WritePacket(std::move(requestCounterDirectoryPacket));
3219
3220 // Wait for a bit (must at least be the delay value of the mock profiling connection) to make sure that
3221 // the Create the Request Counter packet gets processed by the profiling service
Keith Davise394bd92019-12-02 15:12:19 +00003222 std::this_thread::sleep_for(std::chrono::milliseconds(15));
Colm Donelan2ba48d22019-11-29 09:10:59 +00003223
3224 streamRedirector.CancelRedirect();
3225
3226 // Check that the expected error has occurred and logged to the standard output
3227 if (!boost::contains(ss.str(), "Functor with requested PacketId=123 and Version=4194304 does not exist"))
3228 {
3229 std::cout << ss.str();
3230 BOOST_FAIL("Expected string not found.");
3231 }
3232
3233 // The Request Counter Directory Command Handler should not have updated the profiling state
3234 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
3235
3236 // Reset the profiling service to stop any running thread
3237 options.m_EnableProfiling = false;
3238 profilingService.ResetExternalProfilingOptions(options, true);
3239}
3240
3241BOOST_AUTO_TEST_CASE(CheckProfilingServiceBadPeriodicCounterSelectionPacket)
3242{
3243 // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output
3244 LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning);
3245 // Swap the profiling connection factory in the profiling service instance with our mock one
3246 SwapProfilingConnectionFactoryHelper helper;
3247
3248 // Redirect the standard output to a local stream so that we can parse the warning message
3249 std::stringstream ss;
3250 StreamRedirector streamRedirector(std::cout, ss.rdbuf());
3251
3252 // Reset the profiling service to the uninitialized state
3253 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
3254 options.m_EnableProfiling = true;
3255 ProfilingService& profilingService = ProfilingService::Instance();
3256 profilingService.ResetExternalProfilingOptions(options, true);
3257
3258 // Bring the profiling service to the "Active" state
3259 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised);
3260 profilingService.Update(); // Initialize the counter directory
3261 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::NotConnected);
3262 profilingService.Update(); // Create the profiling connection
3263 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::WaitingForAck);
3264 profilingService.Update(); // Start the command handler and the send thread
3265
3266 // Get the mock profiling connection
3267 MockProfilingConnection* mockProfilingConnection = helper.GetMockProfilingConnection();
3268 BOOST_CHECK(mockProfilingConnection);
3269
3270 // Wait for the Stream Metadata packet the be sent
3271 // (we are not testing the connection acknowledgement here so it will be ignored by this test)
3272 helper.WaitForProfilingPacketsSent(mockProfilingConnection);
3273
3274 // Force the profiling service to the "Active" state
3275 helper.ForceTransitionToState(ProfilingState::Active);
3276 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
3277
3278 // Remove the packets received so far
3279 mockProfilingConnection->Clear();
3280
3281 // Write a "Periodic Counter Selection" packet into the mock profiling connection, to simulate an input from an
3282 // external profiling service
3283
3284 // Periodic Counter Selection packet header:
3285 // 26:31 [6] packet_family: Control Packet Family, value 0b000000
3286 // 16:25 [10] packet_id: Packet identifier, value 0b0000000100
3287 // 8:15 [8] reserved: Reserved, value 0b00000000
3288 // 0:7 [8] reserved: Reserved, value 0b00000000
3289 uint32_t packetFamily = 0;
3290 uint32_t packetId = 999; // Wrong packet id!!!
3291 uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16);
3292
3293 // Create the Periodic Counter Selection packet
3294 Packet periodicCounterSelectionPacket(header); // Length == 0, this will disable the collection of counters
3295
3296 // Write the packet to the mock profiling connection
3297 mockProfilingConnection->WritePacket(std::move(periodicCounterSelectionPacket));
3298
3299 // Wait for a bit (must at least be the delay value of the mock profiling connection) to make sure that
3300 // the Periodic Counter Selection packet gets processed by the profiling service
3301 std::this_thread::sleep_for(std::chrono::milliseconds(7));
3302
3303 // Check that the expected error has occurred and logged to the standard output
3304 streamRedirector.CancelRedirect();
3305
3306 // Check that the expected error has occurred and logged to the standard output
3307 if (!boost::contains(ss.str(), "Functor with requested PacketId=999 and Version=4194304 does not exist"))
3308 {
3309 std::cout << ss.str();
3310 BOOST_FAIL("Expected string not found.");
3311 }
3312
3313 // The Periodic Counter Selection Handler should not have updated the profiling state
3314 BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Active);
3315
3316 // Reset the profiling service to stop any running thread
3317 options.m_EnableProfiling = false;
3318 profilingService.ResetExternalProfilingOptions(options, true);
3319}
3320
Francis Murtagh1f7db452019-08-14 09:49:34 +01003321BOOST_AUTO_TEST_SUITE_END()