blob: 4913dde6ee8fab89f7920f8e09a230b99497dbba [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
6#include "../CommandHandlerKey.hpp"
Francis Murtagh11f99b42019-08-16 11:28:52 +01007#include "../CommandHandlerFunctor.hpp"
Francis Murtagh94d79152019-08-16 17:45:07 +01008#include "../CommandHandlerRegistry.hpp"
Nikhil Rajd88e47c2019-08-19 10:04:23 +01009#include "../EncodeVersion.hpp"
Francis Murtagh68f78d82019-09-04 16:42:29 +010010#include "../Holder.hpp"
Nikhil Rajbc626052019-08-15 15:49:45 +010011#include "../Packet.hpp"
Aron Virginas-Tare898db92019-08-22 12:56:34 +010012#include "../PacketVersionResolver.hpp"
Teresa Charlin9bab4962019-09-06 12:28:35 +010013#include "../ProfilingService.hpp"
Nikhil Raj3ecc5102019-09-03 15:55:33 +010014#include "../ProfilingStateMachine.hpp"
Ferran Balaguer1b941722019-08-28 16:57:18 +010015#include "../PeriodicCounterSelectionCommandHandler.hpp"
Matteo Martincighab173e92019-09-05 12:02:04 +010016#include "../ProfilingUtils.hpp"
Teresa Charlin9bab4962019-09-06 12:28:35 +010017#include "../SocketProfilingConnection.hpp"
Ferran Balaguer1b941722019-08-28 16:57:18 +010018#include "../IPeriodicCounterCapture.hpp"
19#include "SendCounterPacketTests.hpp"
Teresa Charlin9bab4962019-09-06 12:28:35 +010020
21#include <Runtime.hpp>
Keith Davis02356de2019-08-26 18:28:17 +010022
Ferran Balaguer1b941722019-08-28 16:57:18 +010023
Aron Virginas-Tare898db92019-08-22 12:56:34 +010024#include <boost/test/unit_test.hpp>
Ferran Balaguer1b941722019-08-28 16:57:18 +010025#include <boost/numeric/conversion/cast.hpp>
Francis Murtagh1f7db452019-08-14 09:49:34 +010026
Nikhil Rajbc626052019-08-15 15:49:45 +010027#include <cstdint>
28#include <cstring>
Aron Virginas-Tare898db92019-08-22 12:56:34 +010029#include <limits>
Francis Murtagh11f99b42019-08-16 11:28:52 +010030#include <map>
Aron Virginas-Tare898db92019-08-22 12:56:34 +010031#include <random>
Nikhil Raj3ecc5102019-09-03 15:55:33 +010032#include <thread>
Francis Murtagh1f7db452019-08-14 09:49:34 +010033
34BOOST_AUTO_TEST_SUITE(ExternalProfiling)
35
Aron Virginas-Tare898db92019-08-22 12:56:34 +010036using namespace armnn::profiling;
37
Francis Murtagh1f7db452019-08-14 09:49:34 +010038BOOST_AUTO_TEST_CASE(CheckCommandHandlerKeyComparisons)
39{
40 CommandHandlerKey testKey0(1, 1);
41 CommandHandlerKey testKey1(1, 1);
42 CommandHandlerKey testKey2(1, 1);
43 CommandHandlerKey testKey3(0, 0);
44 CommandHandlerKey testKey4(2, 2);
45 CommandHandlerKey testKey5(0, 2);
46
47 BOOST_CHECK(testKey1<testKey4);
48 BOOST_CHECK(testKey1>testKey3);
49 BOOST_CHECK(testKey1<=testKey4);
50 BOOST_CHECK(testKey1>=testKey3);
51 BOOST_CHECK(testKey1<=testKey2);
52 BOOST_CHECK(testKey1>=testKey2);
53 BOOST_CHECK(testKey1==testKey2);
54 BOOST_CHECK(testKey1==testKey1);
55
56 BOOST_CHECK(!(testKey1==testKey5));
57 BOOST_CHECK(!(testKey1!=testKey1));
58 BOOST_CHECK(testKey1!=testKey5);
59
60 BOOST_CHECK(testKey1==testKey2 && testKey2==testKey1);
61 BOOST_CHECK(testKey0==testKey1 && testKey1==testKey2 && testKey0==testKey2);
62
63 BOOST_CHECK(testKey1.GetPacketId()==1);
64 BOOST_CHECK(testKey1.GetVersion()==1);
65
66 std::vector<CommandHandlerKey> vect =
Aron Virginas-Tare898db92019-08-22 12:56:34 +010067 {
68 CommandHandlerKey(0,1), CommandHandlerKey(2,0), CommandHandlerKey(1,0),
69 CommandHandlerKey(2,1), CommandHandlerKey(1,1), CommandHandlerKey(0,1),
70 CommandHandlerKey(2,0), CommandHandlerKey(0,0)
71 };
Francis Murtagh1f7db452019-08-14 09:49:34 +010072
73 std::sort(vect.begin(), vect.end());
74
75 std::vector<CommandHandlerKey> expectedVect =
Aron Virginas-Tare898db92019-08-22 12:56:34 +010076 {
77 CommandHandlerKey(0,0), CommandHandlerKey(0,1), CommandHandlerKey(0,1),
78 CommandHandlerKey(1,0), CommandHandlerKey(1,1), CommandHandlerKey(2,0),
79 CommandHandlerKey(2,0), CommandHandlerKey(2,1)
80 };
Francis Murtagh1f7db452019-08-14 09:49:34 +010081
82 BOOST_CHECK(vect == expectedVect);
83}
84
Nikhil Rajd88e47c2019-08-19 10:04:23 +010085BOOST_AUTO_TEST_CASE(CheckEncodeVersion)
86{
Aron Virginas-Tare898db92019-08-22 12:56:34 +010087 Version version1(12);
Nikhil Rajd88e47c2019-08-19 10:04:23 +010088
89 BOOST_CHECK(version1.GetMajor() == 0);
90 BOOST_CHECK(version1.GetMinor() == 0);
91 BOOST_CHECK(version1.GetPatch() == 12);
92
Aron Virginas-Tare898db92019-08-22 12:56:34 +010093 Version version2(4108);
Nikhil Rajd88e47c2019-08-19 10:04:23 +010094
95 BOOST_CHECK(version2.GetMajor() == 0);
96 BOOST_CHECK(version2.GetMinor() == 1);
97 BOOST_CHECK(version2.GetPatch() == 12);
98
Aron Virginas-Tare898db92019-08-22 12:56:34 +010099 Version version3(4198412);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100100
101 BOOST_CHECK(version3.GetMajor() == 1);
102 BOOST_CHECK(version3.GetMinor() == 1);
103 BOOST_CHECK(version3.GetPatch() == 12);
104
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100105 Version version4(0);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100106
107 BOOST_CHECK(version4.GetMajor() == 0);
108 BOOST_CHECK(version4.GetMinor() == 0);
109 BOOST_CHECK(version4.GetPatch() == 0);
110
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100111 Version version5(1, 0, 0);
Nikhil Rajd88e47c2019-08-19 10:04:23 +0100112 BOOST_CHECK(version5.GetEncodedValue() == 4194304);
113}
114
Nikhil Rajbc626052019-08-15 15:49:45 +0100115BOOST_AUTO_TEST_CASE(CheckPacketClass)
116{
117 const char* data = "test";
118 unsigned int length = static_cast<unsigned int>(std::strlen(data));
119
120 Packet packetTest1(472580096,length,data);
121 BOOST_CHECK_THROW(Packet packetTest2(472580096,0,""), armnn::Exception);
122
123 Packet packetTest3(472580096,0, nullptr);
124
125 BOOST_CHECK(packetTest1.GetLength() == length);
126 BOOST_CHECK(packetTest1.GetData() == data);
127
128 BOOST_CHECK(packetTest1.GetPacketFamily() == 7);
129 BOOST_CHECK(packetTest1.GetPacketId() == 43);
130 BOOST_CHECK(packetTest1.GetPacketType() == 3);
131 BOOST_CHECK(packetTest1.GetPacketClass() == 5);
132}
133
Francis Murtagh94d79152019-08-16 17:45:07 +0100134// Create Derived Classes
135class TestFunctorA : public CommandHandlerFunctor
136{
137public:
138 using CommandHandlerFunctor::CommandHandlerFunctor;
139
140 int GetCount() { return m_Count; }
141
142 void operator()(const Packet& packet) override
143 {
144 m_Count++;
145 }
146
147private:
148 int m_Count = 0;
149};
150
151class TestFunctorB : public TestFunctorA
152{
153 using TestFunctorA::TestFunctorA;
154};
155
156class TestFunctorC : public TestFunctorA
157{
158 using TestFunctorA::TestFunctorA;
159};
160
Francis Murtagh11f99b42019-08-16 11:28:52 +0100161BOOST_AUTO_TEST_CASE(CheckCommandHandlerFunctor)
162{
Francis Murtagh11f99b42019-08-16 11:28:52 +0100163 // Hard code the version as it will be the same during a single profiling session
164 uint32_t version = 1;
165
166 TestFunctorA testFunctorA(461, version);
167 TestFunctorB testFunctorB(963, version);
168 TestFunctorC testFunctorC(983, version);
169
170 CommandHandlerKey keyA(testFunctorA.GetPacketId(), testFunctorA.GetVersion());
171 CommandHandlerKey keyB(testFunctorB.GetPacketId(), testFunctorB.GetVersion());
172 CommandHandlerKey keyC(testFunctorC.GetPacketId(), testFunctorC.GetVersion());
173
174 // Create the unwrapped map to simulate the Command Handler Registry
175 std::map<CommandHandlerKey, CommandHandlerFunctor*> registry;
176
177 registry.insert(std::make_pair(keyB, &testFunctorB));
178 registry.insert(std::make_pair(keyA, &testFunctorA));
179 registry.insert(std::make_pair(keyC, &testFunctorC));
180
181 // Check the order of the map is correct
182 auto it = registry.begin();
183 BOOST_CHECK(it->first==keyA);
184 it++;
185 BOOST_CHECK(it->first==keyB);
186 it++;
187 BOOST_CHECK(it->first==keyC);
188
189 Packet packetA(500000000, 0, nullptr);
190 Packet packetB(600000000, 0, nullptr);
191 Packet packetC(400000000, 0, nullptr);
192
193 // Check the correct operator of derived class is called
194 registry.at(CommandHandlerKey(packetA.GetPacketId(), version))->operator()(packetA);
195 BOOST_CHECK(testFunctorA.GetCount() == 1);
196 BOOST_CHECK(testFunctorB.GetCount() == 0);
197 BOOST_CHECK(testFunctorC.GetCount() == 0);
198
199 registry.at(CommandHandlerKey(packetB.GetPacketId(), version))->operator()(packetB);
200 BOOST_CHECK(testFunctorA.GetCount() == 1);
201 BOOST_CHECK(testFunctorB.GetCount() == 1);
202 BOOST_CHECK(testFunctorC.GetCount() == 0);
203
204 registry.at(CommandHandlerKey(packetC.GetPacketId(), version))->operator()(packetC);
205 BOOST_CHECK(testFunctorA.GetCount() == 1);
206 BOOST_CHECK(testFunctorB.GetCount() == 1);
207 BOOST_CHECK(testFunctorC.GetCount() == 1);
208}
209
Francis Murtagh94d79152019-08-16 17:45:07 +0100210BOOST_AUTO_TEST_CASE(CheckCommandHandlerRegistry)
211{
212 // Hard code the version as it will be the same during a single profiling session
213 uint32_t version = 1;
214
215 TestFunctorA testFunctorA(461, version);
216 TestFunctorB testFunctorB(963, version);
217 TestFunctorC testFunctorC(983, version);
218
219 // Create the Command Handler Registry
220 CommandHandlerRegistry registry;
221
222 // Register multiple different derived classes
223 registry.RegisterFunctor(&testFunctorA, testFunctorA.GetPacketId(), testFunctorA.GetVersion());
224 registry.RegisterFunctor(&testFunctorB, testFunctorB.GetPacketId(), testFunctorB.GetVersion());
225 registry.RegisterFunctor(&testFunctorC, testFunctorC.GetPacketId(), testFunctorC.GetVersion());
226
227 Packet packetA(500000000, 0, nullptr);
228 Packet packetB(600000000, 0, nullptr);
229 Packet packetC(400000000, 0, nullptr);
230
231 // Check the correct operator of derived class is called
232 registry.GetFunctor(packetA.GetPacketId(), version)->operator()(packetA);
233 BOOST_CHECK(testFunctorA.GetCount() == 1);
234 BOOST_CHECK(testFunctorB.GetCount() == 0);
235 BOOST_CHECK(testFunctorC.GetCount() == 0);
236
237 registry.GetFunctor(packetB.GetPacketId(), version)->operator()(packetB);
238 BOOST_CHECK(testFunctorA.GetCount() == 1);
239 BOOST_CHECK(testFunctorB.GetCount() == 1);
240 BOOST_CHECK(testFunctorC.GetCount() == 0);
241
242 registry.GetFunctor(packetC.GetPacketId(), version)->operator()(packetC);
243 BOOST_CHECK(testFunctorA.GetCount() == 1);
244 BOOST_CHECK(testFunctorB.GetCount() == 1);
245 BOOST_CHECK(testFunctorC.GetCount() == 1);
246
247 // Re-register an existing key with a new function
248 registry.RegisterFunctor(&testFunctorC, testFunctorA.GetPacketId(), version);
249 registry.GetFunctor(packetA.GetPacketId(), version)->operator()(packetC);
250 BOOST_CHECK(testFunctorA.GetCount() == 1);
251 BOOST_CHECK(testFunctorB.GetCount() == 1);
252 BOOST_CHECK(testFunctorC.GetCount() == 2);
253
254 // Check that non-existent key returns nullptr for its functor
255 BOOST_CHECK_THROW(registry.GetFunctor(0, 0), armnn::Exception);
256}
257
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100258BOOST_AUTO_TEST_CASE(CheckPacketVersionResolver)
259{
260 // Set up random number generator for generating packetId values
261 std::random_device device;
262 std::mt19937 generator(device());
263 std::uniform_int_distribution<uint32_t> distribution(std::numeric_limits<uint32_t>::min(),
264 std::numeric_limits<uint32_t>::max());
265
266 // NOTE: Expected version is always 1.0.0, regardless of packetId
267 const Version expectedVersion(1, 0, 0);
268
269 PacketVersionResolver packetVersionResolver;
270
271 constexpr unsigned int numTests = 10u;
272
273 for (unsigned int i = 0u; i < numTests; ++i)
274 {
275 const uint32_t packetId = distribution(generator);
276 Version resolvedVersion = packetVersionResolver.ResolvePacketVersion(packetId);
277
278 BOOST_TEST(resolvedVersion == expectedVersion);
279 }
280}
Nikhil Raj3ecc5102019-09-03 15:55:33 +0100281void ProfilingCurrentStateThreadImpl(ProfilingStateMachine& states)
282{
283 ProfilingState newState = ProfilingState::NotConnected;
284 states.GetCurrentState();
285 states.TransitionToState(newState);
286}
287
288BOOST_AUTO_TEST_CASE(CheckProfilingStateMachine)
289{
290 ProfilingStateMachine profilingState1(ProfilingState::Uninitialised);
291 profilingState1.TransitionToState(ProfilingState::Uninitialised);
292 BOOST_CHECK(profilingState1.GetCurrentState() == ProfilingState::Uninitialised);
293
294 ProfilingStateMachine profilingState2(ProfilingState::Uninitialised);
295 profilingState2.TransitionToState(ProfilingState::NotConnected);
296 BOOST_CHECK(profilingState2.GetCurrentState() == ProfilingState::NotConnected);
297
298 ProfilingStateMachine profilingState3(ProfilingState::NotConnected);
299 profilingState3.TransitionToState(ProfilingState::NotConnected);
300 BOOST_CHECK(profilingState3.GetCurrentState() == ProfilingState::NotConnected);
301
302 ProfilingStateMachine profilingState4(ProfilingState::NotConnected);
303 profilingState4.TransitionToState(ProfilingState::WaitingForAck);
304 BOOST_CHECK(profilingState4.GetCurrentState() == ProfilingState::WaitingForAck);
305
306 ProfilingStateMachine profilingState5(ProfilingState::WaitingForAck);
307 profilingState5.TransitionToState(ProfilingState::WaitingForAck);
308 BOOST_CHECK(profilingState5.GetCurrentState() == ProfilingState::WaitingForAck);
309
310 ProfilingStateMachine profilingState6(ProfilingState::WaitingForAck);
311 profilingState6.TransitionToState(ProfilingState::Active);
312 BOOST_CHECK(profilingState6.GetCurrentState() == ProfilingState::Active);
313
314 ProfilingStateMachine profilingState7(ProfilingState::Active);
315 profilingState7.TransitionToState(ProfilingState::NotConnected);
316 BOOST_CHECK(profilingState7.GetCurrentState() == ProfilingState::NotConnected);
317
318 ProfilingStateMachine profilingState8(ProfilingState::Active);
319 profilingState8.TransitionToState(ProfilingState::Active);
320 BOOST_CHECK(profilingState8.GetCurrentState() == ProfilingState::Active);
321
322 ProfilingStateMachine profilingState9(ProfilingState::Uninitialised);
323 BOOST_CHECK_THROW(profilingState9.TransitionToState(ProfilingState::WaitingForAck),
324 armnn::Exception);
325
326 ProfilingStateMachine profilingState10(ProfilingState::Uninitialised);
327 BOOST_CHECK_THROW(profilingState10.TransitionToState(ProfilingState::Active),
328 armnn::Exception);
329
330 ProfilingStateMachine profilingState11(ProfilingState::NotConnected);
331 BOOST_CHECK_THROW(profilingState11.TransitionToState(ProfilingState::Uninitialised),
332 armnn::Exception);
333
334 ProfilingStateMachine profilingState12(ProfilingState::NotConnected);
335 BOOST_CHECK_THROW(profilingState12.TransitionToState(ProfilingState::Active),
336 armnn::Exception);
337
338 ProfilingStateMachine profilingState13(ProfilingState::WaitingForAck);
339 BOOST_CHECK_THROW(profilingState13.TransitionToState(ProfilingState::Uninitialised),
340 armnn::Exception);
341
342 ProfilingStateMachine profilingState14(ProfilingState::WaitingForAck);
343 BOOST_CHECK_THROW(profilingState14.TransitionToState(ProfilingState::NotConnected),
344 armnn::Exception);
345
346 ProfilingStateMachine profilingState15(ProfilingState::Active);
347 BOOST_CHECK_THROW(profilingState15.TransitionToState(ProfilingState::Uninitialised),
348 armnn::Exception);
349
350 ProfilingStateMachine profilingState16(armnn::profiling::ProfilingState::Active);
351 BOOST_CHECK_THROW(profilingState16.TransitionToState(ProfilingState::WaitingForAck),
352 armnn::Exception);
353
354 ProfilingStateMachine profilingState17(ProfilingState::Uninitialised);
355
356 std::thread thread1 (ProfilingCurrentStateThreadImpl,std::ref(profilingState17));
357 std::thread thread2 (ProfilingCurrentStateThreadImpl,std::ref(profilingState17));
358 std::thread thread3 (ProfilingCurrentStateThreadImpl,std::ref(profilingState17));
359 std::thread thread4 (ProfilingCurrentStateThreadImpl,std::ref(profilingState17));
360 std::thread thread5 (ProfilingCurrentStateThreadImpl,std::ref(profilingState17));
361
362 thread1.join();
363 thread2.join();
364 thread3.join();
365 thread4.join();
366 thread5.join();
367
368 BOOST_TEST((profilingState17.GetCurrentState() == ProfilingState::NotConnected));
369}
Aron Virginas-Tare898db92019-08-22 12:56:34 +0100370
Francis Murtagh68f78d82019-09-04 16:42:29 +0100371void CaptureDataWriteThreadImpl(Holder &holder, uint32_t capturePeriod, std::vector<uint16_t>& counterIds)
372{
373 holder.SetCaptureData(capturePeriod, counterIds);
374}
375
Francis Murtaghbd707162019-09-09 11:26:44 +0100376void CaptureDataReadThreadImpl(const Holder& holder, CaptureData& captureData)
Francis Murtagh68f78d82019-09-04 16:42:29 +0100377{
378 captureData = holder.GetCaptureData();
379}
380
381BOOST_AUTO_TEST_CASE(CheckCaptureDataHolder)
382{
Francis Murtaghbd707162019-09-09 11:26:44 +0100383 std::map<uint32_t, std::vector<uint16_t>> periodIdMap;
384 std::vector<uint16_t> counterIds;
385 uint16_t numThreads = 50;
386 for (uint16_t i = 0; i < numThreads; ++i)
387 {
388 counterIds.emplace_back(i);
389 periodIdMap.insert(std::make_pair(i, counterIds));
390 }
Francis Murtagh68f78d82019-09-04 16:42:29 +0100391
392 // Check CaptureData functions
393 CaptureData capture;
394 BOOST_CHECK(capture.GetCapturePeriod() == 0);
395 BOOST_CHECK((capture.GetCounterIds()).empty());
Francis Murtaghbd707162019-09-09 11:26:44 +0100396 capture.SetCapturePeriod(0);
397 capture.SetCounterIds(periodIdMap[0]);
398 BOOST_CHECK(capture.GetCapturePeriod() == 0);
399 BOOST_CHECK(capture.GetCounterIds() == periodIdMap[0]);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100400
401 Holder holder;
402 BOOST_CHECK((holder.GetCaptureData()).GetCapturePeriod() == 0);
403 BOOST_CHECK(((holder.GetCaptureData()).GetCounterIds()).empty());
404
405 // Check Holder functions
Francis Murtaghbd707162019-09-09 11:26:44 +0100406 std::thread thread1(CaptureDataWriteThreadImpl, std::ref(holder), 2, std::ref(periodIdMap[2]));
Francis Murtagh68f78d82019-09-04 16:42:29 +0100407 thread1.join();
408
Francis Murtaghbd707162019-09-09 11:26:44 +0100409 BOOST_CHECK((holder.GetCaptureData()).GetCapturePeriod() == 2);
410 BOOST_CHECK((holder.GetCaptureData()).GetCounterIds() == periodIdMap[2]);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100411
412 CaptureData captureData;
413 std::thread thread2(CaptureDataReadThreadImpl, std::ref(holder), std::ref(captureData));
414 thread2.join();
Francis Murtaghbd707162019-09-09 11:26:44 +0100415 BOOST_CHECK(captureData.GetCounterIds() == periodIdMap[2]);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100416
Francis Murtaghbd707162019-09-09 11:26:44 +0100417 std::vector<std::thread> threadsVect;
418 for (int i = 0; i < numThreads; i+=2)
419 {
420 threadsVect.emplace_back(std::thread(CaptureDataWriteThreadImpl,
421 std::ref(holder),
422 i,
423 std::ref(periodIdMap[static_cast<uint16_t >(i)])));
Francis Murtagh06965692019-09-05 16:29:01 +0100424
Francis Murtaghbd707162019-09-09 11:26:44 +0100425 threadsVect.emplace_back(std::thread(CaptureDataReadThreadImpl,
426 std::ref(holder),
427 std::ref(captureData)));
428 }
429
430 for (uint16_t i = 0; i < numThreads; ++i)
431 {
432 threadsVect[i].join();
433 }
434
435 std::vector<std::thread> readThreadsVect;
436 for (uint16_t i = 0; i < numThreads; ++i)
437 {
438 readThreadsVect.emplace_back(
439 std::thread(CaptureDataReadThreadImpl, std::ref(holder), std::ref(captureData)));
440 }
441
442 for (uint16_t i = 0; i < numThreads; ++i)
443 {
444 readThreadsVect[i].join();
445 }
Francis Murtagh68f78d82019-09-04 16:42:29 +0100446
447 // Check CaptureData was written/read correctly from multiple threads
448 std::vector<uint16_t> captureIds = captureData.GetCounterIds();
449 uint32_t capturePeriod = captureData.GetCapturePeriod();
Francis Murtaghbd707162019-09-09 11:26:44 +0100450
451 BOOST_CHECK(captureIds == periodIdMap[capturePeriod]);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100452
453 std::vector<uint16_t> readIds = holder.GetCaptureData().GetCounterIds();
Francis Murtaghbd707162019-09-09 11:26:44 +0100454 BOOST_CHECK(captureIds == readIds);
Matthew Bentham46d1c622019-09-13 12:45:04 +0100455}
Francis Murtagh68f78d82019-09-04 16:42:29 +0100456
Matthew Bentham46d1c622019-09-13 12:45:04 +0100457BOOST_AUTO_TEST_CASE(CaptureDataMethods)
458{
Francis Murtagh68f78d82019-09-04 16:42:29 +0100459 // Check assignment operator
460 CaptureData assignableCaptureData;
Matthew Bentham46d1c622019-09-13 12:45:04 +0100461 std::vector<uint16_t> counterIds = {42, 29, 13};
Francis Murtaghbd707162019-09-09 11:26:44 +0100462 assignableCaptureData.SetCapturePeriod(3);
Matthew Bentham46d1c622019-09-13 12:45:04 +0100463 assignableCaptureData.SetCounterIds(counterIds);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100464
465 CaptureData secondCaptureData;
Francis Murtagh68f78d82019-09-04 16:42:29 +0100466
Matthew Bentham46d1c622019-09-13 12:45:04 +0100467 BOOST_CHECK(assignableCaptureData.GetCapturePeriod() == 3);
468 BOOST_CHECK(assignableCaptureData.GetCounterIds() == counterIds);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100469
470 secondCaptureData = assignableCaptureData;
471 BOOST_CHECK(secondCaptureData.GetCapturePeriod() == 3);
Matthew Bentham46d1c622019-09-13 12:45:04 +0100472 BOOST_CHECK(secondCaptureData.GetCounterIds() == counterIds);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100473
474 // Check copy constructor
475 CaptureData copyConstructedCaptureData(assignableCaptureData);
476
477 BOOST_CHECK(copyConstructedCaptureData.GetCapturePeriod() == 3);
Matthew Bentham46d1c622019-09-13 12:45:04 +0100478 BOOST_CHECK(copyConstructedCaptureData.GetCounterIds() == counterIds);
Keith Davis02356de2019-08-26 18:28:17 +0100479}
Francis Murtagh68f78d82019-09-04 16:42:29 +0100480
Keith Davis02356de2019-08-26 18:28:17 +0100481BOOST_AUTO_TEST_CASE(CheckProfilingServiceDisabled)
482{
483 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
484 ProfilingService service(options);
485 BOOST_CHECK(service.GetCurrentState() == ProfilingState::Uninitialised);
486 service.Run();
487 BOOST_CHECK(service.GetCurrentState() == ProfilingState::Uninitialised);
488}
489
Keith Davis02356de2019-08-26 18:28:17 +0100490BOOST_AUTO_TEST_CASE(CheckProfilingServiceEnabled)
491{
492 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
493 options.m_EnableProfiling = true;
494 ProfilingService service(options);
495 BOOST_CHECK(service.GetCurrentState() == ProfilingState::NotConnected);
496 service.Run();
497 BOOST_CHECK(service.GetCurrentState() == ProfilingState::WaitingForAck);
498}
499
500
501BOOST_AUTO_TEST_CASE(CheckProfilingServiceEnabledRuntime)
502{
503 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
504 ProfilingService service(options);
505 BOOST_CHECK(service.GetCurrentState() == ProfilingState::Uninitialised);
506 service.Run();
507 BOOST_CHECK(service.GetCurrentState() == ProfilingState::Uninitialised);
508 service.m_Options.m_EnableProfiling = true;
509 service.Run();
510 BOOST_CHECK(service.GetCurrentState() == ProfilingState::NotConnected);
511 service.Run();
512 BOOST_CHECK(service.GetCurrentState() == ProfilingState::WaitingForAck);
Francis Murtagh68f78d82019-09-04 16:42:29 +0100513}
514
Matteo Martincighab173e92019-09-05 12:02:04 +0100515void GetNextUidTestImpl(uint16_t& outUid)
516{
517 outUid = GetNextUid();
518}
519
520BOOST_AUTO_TEST_CASE(GetNextUidTest)
521{
522 uint16_t uid0 = 0;
523 uint16_t uid1 = 0;
524 uint16_t uid2 = 0;
525
526 std::thread thread1(GetNextUidTestImpl, std::ref(uid0));
527 std::thread thread2(GetNextUidTestImpl, std::ref(uid1));
528 std::thread thread3(GetNextUidTestImpl, std::ref(uid2));
529 thread1.join();
530 thread2.join();
531 thread3.join();
532
533 BOOST_TEST(uid0 > 0);
534 BOOST_TEST(uid1 > 0);
535 BOOST_TEST(uid2 > 0);
536 BOOST_TEST(uid0 != uid1);
537 BOOST_TEST(uid0 != uid2);
538 BOOST_TEST(uid1 != uid2);
539}
540
Ferran Balaguer1b941722019-08-28 16:57:18 +0100541BOOST_AUTO_TEST_CASE(CounterSelectionCommandHandlerParseData)
542{
543 using boost::numeric_cast;
544
545 class TestCaptureThread : public IPeriodicCounterCapture
546 {
547 void Start() override {};
548 };
549
550 const uint32_t packetId = 0x40000;
551
552 uint32_t version = 1;
553 Holder holder;
554 TestCaptureThread captureThread;
555 MockBuffer mockBuffer(512);
556 SendCounterPacket sendCounterPacket(mockBuffer);
557
558 uint32_t sizeOfUint32 = numeric_cast<uint32_t>(sizeof(uint32_t));
559 uint32_t sizeOfUint16 = numeric_cast<uint32_t>(sizeof(uint16_t));
560
561 // Data with period and counters
562 uint32_t period1 = 10;
563 uint32_t dataLength1 = 8;
564 unsigned char data1[dataLength1];
565 uint32_t offset = 0;
566
567 WriteUint32(data1, offset, period1);
568 offset += sizeOfUint32;
569 WriteUint16(data1, offset, 4000);
570 offset += sizeOfUint16;
571 WriteUint16(data1, offset, 5000);
572
573 Packet packetA(packetId, dataLength1, reinterpret_cast<const char*>(data1));
574
575 PeriodicCounterSelectionCommandHandler commandHandler(packetId, version, holder, captureThread,
576 sendCounterPacket);
577 commandHandler(packetA);
578
579 std::vector<uint16_t> counterIds = holder.GetCaptureData().GetCounterIds();
580
581 BOOST_TEST(holder.GetCaptureData().GetCapturePeriod() == period1);
582 BOOST_TEST(counterIds.size() == 2);
583 BOOST_TEST(counterIds[0] == 4000);
584 BOOST_TEST(counterIds[1] == 5000);
585
586 unsigned int size = 0;
587
588 const unsigned char* readBuffer = mockBuffer.GetReadBuffer(size);
589
590 offset = 0;
591
592 uint32_t headerWord0 = ReadUint32(readBuffer, offset);
593 offset += sizeOfUint32;
594 uint32_t headerWord1 = ReadUint32(readBuffer, offset);
595 offset += sizeOfUint32;
596 uint32_t period = ReadUint32(readBuffer, offset);
597
598 BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family
599 BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 4); // packet id
600 BOOST_TEST(headerWord1 == 8); // data lenght
601 BOOST_TEST(period == 10); // capture period
602
603 uint16_t counterId = 0;
604 offset += sizeOfUint32;
605 counterId = ReadUint16(readBuffer, offset);
606 BOOST_TEST(counterId == 4000);
607 offset += sizeOfUint16;
608 counterId = ReadUint16(readBuffer, offset);
609 BOOST_TEST(counterId == 5000);
610
611 // Data with period only
612 uint32_t period2 = 11;
613 uint32_t dataLength2 = 4;
614 unsigned char data2[dataLength2];
615
616 WriteUint32(data2, 0, period2);
617
618 Packet packetB(packetId, dataLength2, reinterpret_cast<const char*>(data2));
619
620 commandHandler(packetB);
621
622 counterIds = holder.GetCaptureData().GetCounterIds();
623
624 BOOST_TEST(holder.GetCaptureData().GetCapturePeriod() == period2);
625 BOOST_TEST(counterIds.size() == 0);
626
627 readBuffer = mockBuffer.GetReadBuffer(size);
628
629 offset = 0;
630
631 headerWord0 = ReadUint32(readBuffer, offset);
632 offset += sizeOfUint32;
633 headerWord1 = ReadUint32(readBuffer, offset);
634 offset += sizeOfUint32;
635 period = ReadUint32(readBuffer, offset);
636
637 BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family
638 BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 4); // packet id
639 BOOST_TEST(headerWord1 == 4); // data lenght
640 BOOST_TEST(period == 11); // capture period
641
642}
643
Teresa Charlin9bab4962019-09-06 12:28:35 +0100644BOOST_AUTO_TEST_CASE(CheckSocketProfilingConnection)
645{
646 // Check that creating a SocketProfilingConnection results in an exception as the Gator UDS doesn't exist.
647 BOOST_CHECK_THROW(new SocketProfilingConnection(), armnn::Exception);
648}
649
Francis Murtagh1f7db452019-08-14 09:49:34 +0100650BOOST_AUTO_TEST_SUITE_END()