blob: dd271c95941d508fcb22be4f68d27fee341064b7 [file] [log] [blame]
Ferran Balagueraf5c46b2019-08-30 15:49:15 +01001//
Ferran Balaguer1b941722019-08-28 16:57:18 +01002// Copyright © 2019 Arm Ltd. All rights reserved.
Ferran Balagueraf5c46b2019-08-30 15:49:15 +01003// SPDX-License-Identifier: MIT
4//
5
Jim Flynn64063552020-02-14 10:18:08 +00006#include "ProfilingMocks.hpp"
Finn Williamsa0de0562020-04-22 12:27:37 +01007#include "ProfilingTestUtils.hpp"
Ferran Balaguer1b941722019-08-28 16:57:18 +01008#include "SendCounterPacketTests.hpp"
Ferran Balagueraf5c46b2019-08-30 15:49:15 +01009
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +010010#include <BufferManager.hpp>
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +010011#include <CounterDirectory.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010012#include <EncodeVersion.hpp>
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +010013#include <ProfilingUtils.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010014#include <SendCounterPacket.hpp>
Rob Hughesbdee4262020-01-07 17:05:24 +000015#include <Processes.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010016
Ferran Balaguer73882172019-09-02 16:39:42 +010017#include <armnn/Exceptions.hpp>
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +010018#include <armnn/Conversion.hpp>
Rob Hughes122f3252020-01-09 12:46:21 +000019#include <armnn/Utils.hpp>
Ferran Balaguer73882172019-09-02 16:39:42 +010020
Ferran Balagueraf5c46b2019-08-30 15:49:15 +010021#include <boost/test/unit_test.hpp>
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010022#include <boost/numeric/conversion/cast.hpp>
Ferran Balagueraf5c46b2019-08-30 15:49:15 +010023
Francis Murtagh3a161982019-09-04 15:25:02 +010024#include <chrono>
Ferran Balagueraf5c46b2019-08-30 15:49:15 +010025
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +010026using namespace armnn::profiling;
27
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010028namespace
29{
30
Colm Donelan2ba48d22019-11-29 09:10:59 +000031// A short delay to wait for the thread to process a packet.
Finn Williams09ad6f92019-12-19 17:05:18 +000032uint16_t constexpr WAIT_UNTIL_READABLE_MS = 20;
Colm Donelan2ba48d22019-11-29 09:10:59 +000033
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010034void SetNotConnectedProfilingState(ProfilingStateMachine& profilingStateMachine)
35{
36 ProfilingState currentState = profilingStateMachine.GetCurrentState();
37 switch (currentState)
38 {
39 case ProfilingState::WaitingForAck:
40 profilingStateMachine.TransitionToState(ProfilingState::Active);
Rob Hughes122f3252020-01-09 12:46:21 +000041 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010042 case ProfilingState::Uninitialised:
Rob Hughes122f3252020-01-09 12:46:21 +000043 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010044 case ProfilingState::Active:
45 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
Rob Hughes122f3252020-01-09 12:46:21 +000046 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010047 case ProfilingState::NotConnected:
48 return;
49 default:
50 BOOST_CHECK_MESSAGE(false, "Invalid profiling state");
51 }
52}
53
54void SetWaitingForAckProfilingState(ProfilingStateMachine& profilingStateMachine)
55{
56 ProfilingState currentState = profilingStateMachine.GetCurrentState();
57 switch (currentState)
58 {
59 case ProfilingState::Uninitialised:
Rob Hughes122f3252020-01-09 12:46:21 +000060 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010061 case ProfilingState::Active:
62 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
Rob Hughes122f3252020-01-09 12:46:21 +000063 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010064 case ProfilingState::NotConnected:
65 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
Rob Hughes122f3252020-01-09 12:46:21 +000066 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010067 case ProfilingState::WaitingForAck:
68 return;
69 default:
70 BOOST_CHECK_MESSAGE(false, "Invalid profiling state");
71 }
72}
73
74void SetActiveProfilingState(ProfilingStateMachine& profilingStateMachine)
75{
76 ProfilingState currentState = profilingStateMachine.GetCurrentState();
77 switch (currentState)
78 {
79 case ProfilingState::Uninitialised:
80 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
Rob Hughes122f3252020-01-09 12:46:21 +000081 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010082 case ProfilingState::NotConnected:
83 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
Rob Hughes122f3252020-01-09 12:46:21 +000084 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010085 case ProfilingState::WaitingForAck:
86 profilingStateMachine.TransitionToState(ProfilingState::Active);
Rob Hughes122f3252020-01-09 12:46:21 +000087 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010088 case ProfilingState::Active:
89 return;
90 default:
91 BOOST_CHECK_MESSAGE(false, "Invalid profiling state");
92 }
93}
94
95} // Anonymous namespace
96
Ferran Balagueraf5c46b2019-08-30 15:49:15 +010097BOOST_AUTO_TEST_SUITE(SendCounterPacketTests)
98
Finn Williams09ad6f92019-12-19 17:05:18 +000099using PacketType = MockProfilingConnection::PacketType;
100
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100101BOOST_AUTO_TEST_CASE(MockSendCounterPacketTest)
102{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100103 MockBufferManager mockBuffer(512);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +0100104 MockSendCounterPacket mockSendCounterPacket(mockBuffer);
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100105
Matteo Martincigh5d737fb2019-10-07 13:05:13 +0100106 mockSendCounterPacket.SendStreamMetaDataPacket();
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100107
108 auto packetBuffer = mockBuffer.GetReadableBuffer();
109 const char* buffer = reinterpret_cast<const char*>(packetBuffer->GetReadableData());
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100110
111 BOOST_TEST(strcmp(buffer, "SendStreamMetaDataPacket") == 0);
112
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100113 mockBuffer.MarkRead(packetBuffer);
114
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100115 CounterDirectory counterDirectory;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +0100116 mockSendCounterPacket.SendCounterDirectoryPacket(counterDirectory);
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100117
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100118 packetBuffer = mockBuffer.GetReadableBuffer();
119 buffer = reinterpret_cast<const char*>(packetBuffer->GetReadableData());
120
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100121 BOOST_TEST(strcmp(buffer, "SendCounterDirectoryPacket") == 0);
122
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100123 mockBuffer.MarkRead(packetBuffer);
124
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100125 uint64_t timestamp = 0;
Finn Williams032bc742020-02-12 11:02:34 +0000126 std::vector<CounterValue> indexValuePairs;
Francis Murtagh3a161982019-09-04 15:25:02 +0100127
Matteo Martincigh5d737fb2019-10-07 13:05:13 +0100128 mockSendCounterPacket.SendPeriodicCounterCapturePacket(timestamp, indexValuePairs);
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100129
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100130 packetBuffer = mockBuffer.GetReadableBuffer();
131 buffer = reinterpret_cast<const char*>(packetBuffer->GetReadableData());
132
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100133 BOOST_TEST(strcmp(buffer, "SendPeriodicCounterCapturePacket") == 0);
134
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100135 mockBuffer.MarkRead(packetBuffer);
136
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100137 uint32_t capturePeriod = 0;
138 std::vector<uint16_t> selectedCounterIds;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +0100139 mockSendCounterPacket.SendPeriodicCounterSelectionPacket(capturePeriod, selectedCounterIds);
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100140
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100141 packetBuffer = mockBuffer.GetReadableBuffer();
142 buffer = reinterpret_cast<const char*>(packetBuffer->GetReadableData());
143
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100144 BOOST_TEST(strcmp(buffer, "SendPeriodicCounterSelectionPacket") == 0);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100145
146 mockBuffer.MarkRead(packetBuffer);
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100147}
148
Ferran Balaguer73882172019-09-02 16:39:42 +0100149BOOST_AUTO_TEST_CASE(SendPeriodicCounterSelectionPacketTest)
150{
151 // Error no space left in buffer
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100152 MockBufferManager mockBuffer1(10);
Sadik Armagan3896b472020-02-10 12:24:15 +0000153 SendCounterPacket sendPacket1(mockBuffer1);
Ferran Balaguer73882172019-09-02 16:39:42 +0100154
155 uint32_t capturePeriod = 1000;
156 std::vector<uint16_t> selectedCounterIds;
157 BOOST_CHECK_THROW(sendPacket1.SendPeriodicCounterSelectionPacket(capturePeriod, selectedCounterIds),
Matteo Martincigh24e8f922019-09-19 11:57:46 +0100158 BufferExhaustion);
Ferran Balaguer73882172019-09-02 16:39:42 +0100159
160 // Packet without any counters
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100161 MockBufferManager mockBuffer2(512);
Sadik Armagan3896b472020-02-10 12:24:15 +0000162 SendCounterPacket sendPacket2(mockBuffer2);
Ferran Balaguer73882172019-09-02 16:39:42 +0100163
164 sendPacket2.SendPeriodicCounterSelectionPacket(capturePeriod, selectedCounterIds);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100165 auto readBuffer2 = mockBuffer2.GetReadableBuffer();
Ferran Balaguer73882172019-09-02 16:39:42 +0100166
167 uint32_t headerWord0 = ReadUint32(readBuffer2, 0);
168 uint32_t headerWord1 = ReadUint32(readBuffer2, 4);
169 uint32_t period = ReadUint32(readBuffer2, 8);
170
171 BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family
172 BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 4); // packet id
173 BOOST_TEST(headerWord1 == 4); // data lenght
174 BOOST_TEST(period == 1000); // capture period
175
176 // Full packet message
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100177 MockBufferManager mockBuffer3(512);
Sadik Armagan3896b472020-02-10 12:24:15 +0000178 SendCounterPacket sendPacket3(mockBuffer3);
Ferran Balaguer73882172019-09-02 16:39:42 +0100179
180 selectedCounterIds.reserve(5);
181 selectedCounterIds.emplace_back(100);
182 selectedCounterIds.emplace_back(200);
183 selectedCounterIds.emplace_back(300);
184 selectedCounterIds.emplace_back(400);
185 selectedCounterIds.emplace_back(500);
186 sendPacket3.SendPeriodicCounterSelectionPacket(capturePeriod, selectedCounterIds);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100187 auto readBuffer3 = mockBuffer3.GetReadableBuffer();
Ferran Balaguer73882172019-09-02 16:39:42 +0100188
189 headerWord0 = ReadUint32(readBuffer3, 0);
190 headerWord1 = ReadUint32(readBuffer3, 4);
191 period = ReadUint32(readBuffer3, 8);
192
193 BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family
194 BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 4); // packet id
195 BOOST_TEST(headerWord1 == 14); // data lenght
196 BOOST_TEST(period == 1000); // capture period
197
198 uint16_t counterId = 0;
199 uint32_t offset = 12;
200
201 // Counter Ids
202 for(const uint16_t& id : selectedCounterIds)
203 {
204 counterId = ReadUint16(readBuffer3, offset);
205 BOOST_TEST(counterId == id);
206 offset += 2;
207 }
208}
209
Francis Murtagh3a161982019-09-04 15:25:02 +0100210BOOST_AUTO_TEST_CASE(SendPeriodicCounterCapturePacketTest)
211{
Matteo Martincigh5d737fb2019-10-07 13:05:13 +0100212 ProfilingStateMachine profilingStateMachine;
213
Francis Murtagh3a161982019-09-04 15:25:02 +0100214 // Error no space left in buffer
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100215 MockBufferManager mockBuffer1(10);
Sadik Armagan3896b472020-02-10 12:24:15 +0000216 SendCounterPacket sendPacket1(mockBuffer1);
Francis Murtagh3a161982019-09-04 15:25:02 +0100217
218 auto captureTimestamp = std::chrono::steady_clock::now();
219 uint64_t time = static_cast<uint64_t >(captureTimestamp.time_since_epoch().count());
Finn Williams032bc742020-02-12 11:02:34 +0000220 std::vector<CounterValue> indexValuePairs;
Francis Murtagh3a161982019-09-04 15:25:02 +0100221
222 BOOST_CHECK_THROW(sendPacket1.SendPeriodicCounterCapturePacket(time, indexValuePairs),
223 BufferExhaustion);
224
225 // Packet without any counters
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100226 MockBufferManager mockBuffer2(512);
Sadik Armagan3896b472020-02-10 12:24:15 +0000227 SendCounterPacket sendPacket2(mockBuffer2);
Francis Murtagh3a161982019-09-04 15:25:02 +0100228
229 sendPacket2.SendPeriodicCounterCapturePacket(time, indexValuePairs);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100230 auto readBuffer2 = mockBuffer2.GetReadableBuffer();
Francis Murtagh3a161982019-09-04 15:25:02 +0100231
232 uint32_t headerWord0 = ReadUint32(readBuffer2, 0);
233 uint32_t headerWord1 = ReadUint32(readBuffer2, 4);
234 uint64_t readTimestamp = ReadUint64(readBuffer2, 8);
235
Jim Flynnfc365622019-12-04 10:07:20 +0000236 BOOST_TEST(((headerWord0 >> 26) & 0x0000003F) == 3); // packet family
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100237 BOOST_TEST(((headerWord0 >> 19) & 0x0000007F) == 0); // packet class
238 BOOST_TEST(((headerWord0 >> 16) & 0x00000007) == 0); // packet type
239 BOOST_TEST(headerWord1 == 8); // data length
240 BOOST_TEST(time == readTimestamp); // capture period
Francis Murtagh3a161982019-09-04 15:25:02 +0100241
242 // Full packet message
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100243 MockBufferManager mockBuffer3(512);
Sadik Armagan3896b472020-02-10 12:24:15 +0000244 SendCounterPacket sendPacket3(mockBuffer3);
Francis Murtagh3a161982019-09-04 15:25:02 +0100245
246 indexValuePairs.reserve(5);
Finn Williams032bc742020-02-12 11:02:34 +0000247 indexValuePairs.emplace_back(CounterValue{0, 100});
248 indexValuePairs.emplace_back(CounterValue{1, 200});
249 indexValuePairs.emplace_back(CounterValue{2, 300});
250 indexValuePairs.emplace_back(CounterValue{3, 400});
251 indexValuePairs.emplace_back(CounterValue{4, 500});
Francis Murtagh3a161982019-09-04 15:25:02 +0100252 sendPacket3.SendPeriodicCounterCapturePacket(time, indexValuePairs);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100253 auto readBuffer3 = mockBuffer3.GetReadableBuffer();
Francis Murtagh3a161982019-09-04 15:25:02 +0100254
255 headerWord0 = ReadUint32(readBuffer3, 0);
256 headerWord1 = ReadUint32(readBuffer3, 4);
257 uint64_t readTimestamp2 = ReadUint64(readBuffer3, 8);
258
Jim Flynnfc365622019-12-04 10:07:20 +0000259 BOOST_TEST(((headerWord0 >> 26) & 0x0000003F) == 3); // packet family
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100260 BOOST_TEST(((headerWord0 >> 19) & 0x0000007F) == 0); // packet class
261 BOOST_TEST(((headerWord0 >> 16) & 0x00000007) == 0); // packet type
262 BOOST_TEST(headerWord1 == 38); // data length
263 BOOST_TEST(time == readTimestamp2); // capture period
Francis Murtagh3a161982019-09-04 15:25:02 +0100264
265 uint16_t counterIndex = 0;
266 uint32_t counterValue = 100;
267 uint32_t offset = 16;
268
269 // Counter Ids
270 for (auto it = indexValuePairs.begin(), end = indexValuePairs.end(); it != end; ++it)
271 {
272 // Check Counter Index
273 uint16_t readIndex = ReadUint16(readBuffer3, offset);
274 BOOST_TEST(counterIndex == readIndex);
275 counterIndex++;
276 offset += 2;
277
278 // Check Counter Value
279 uint32_t readValue = ReadUint32(readBuffer3, offset);
280 BOOST_TEST(counterValue == readValue);
281 counterValue += 100;
282 offset += 4;
283 }
284
285}
286
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100287BOOST_AUTO_TEST_CASE(SendStreamMetaDataPacketTest)
288{
289 using boost::numeric_cast;
290
291 uint32_t sizeUint32 = numeric_cast<uint32_t>(sizeof(uint32_t));
292
293 // Error no space left in buffer
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100294 MockBufferManager mockBuffer1(10);
Sadik Armagan3896b472020-02-10 12:24:15 +0000295 SendCounterPacket sendPacket1(mockBuffer1);
Matteo Martincigh149528e2019-09-05 12:02:04 +0100296 BOOST_CHECK_THROW(sendPacket1.SendStreamMetaDataPacket(), armnn::profiling::BufferExhaustion);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100297
298 // Full metadata packet
299
300 std::string processName = GetProcessName().substr(0, 60);
301
Finn Williamsa0de0562020-04-22 12:27:37 +0100302 uint32_t infoSize = numeric_cast<uint32_t>(GetSoftwareInfo().size()) + 1;
303 uint32_t hardwareVersionSize = numeric_cast<uint32_t>(GetHardwareVersion().size()) + 1;
304 uint32_t softwareVersionSize = numeric_cast<uint32_t>(GetSoftwareVersion().size()) + 1;
305 uint32_t processNameSize = numeric_cast<uint32_t>(processName.size()) + 1;
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100306
Ferran Balaguer5bf1d322019-09-13 13:31:40 +0100307 uint32_t packetEntries = 6;
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100308
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100309 MockBufferManager mockBuffer2(512);
Sadik Armagan3896b472020-02-10 12:24:15 +0000310 SendCounterPacket sendPacket2(mockBuffer2);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100311 sendPacket2.SendStreamMetaDataPacket();
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100312 auto readBuffer2 = mockBuffer2.GetReadableBuffer();
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100313
314 uint32_t headerWord0 = ReadUint32(readBuffer2, 0);
315 uint32_t headerWord1 = ReadUint32(readBuffer2, sizeUint32);
316
317 BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family
318 BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 0); // packet id
319
320 uint32_t totalLength = numeric_cast<uint32_t>(2 * sizeUint32 + 10 * sizeUint32 + infoSize + hardwareVersionSize +
321 softwareVersionSize + processNameSize + sizeUint32 +
322 2 * packetEntries * sizeUint32);
323
324 BOOST_TEST(headerWord1 == totalLength - (2 * sizeUint32)); // data length
325
326 uint32_t offset = sizeUint32 * 2;
327 BOOST_TEST(ReadUint32(readBuffer2, offset) == SendCounterPacket::PIPE_MAGIC); // pipe_magic
328 offset += sizeUint32;
329 BOOST_TEST(ReadUint32(readBuffer2, offset) == EncodeVersion(1, 0, 0)); // stream_metadata_version
330 offset += sizeUint32;
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +0100331 BOOST_TEST(ReadUint32(readBuffer2, offset) == MAX_METADATA_PACKET_LENGTH); // max_data_len
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100332 offset += sizeUint32;
Rob Hughesbdee4262020-01-07 17:05:24 +0000333 int pid = armnnUtils::Processes::GetCurrentId();
334 BOOST_TEST(ReadUint32(readBuffer2, offset) == numeric_cast<uint32_t>(pid));
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100335 offset += sizeUint32;
336 uint32_t poolOffset = 10 * sizeUint32;
Finn Williamsa0de0562020-04-22 12:27:37 +0100337 BOOST_TEST(ReadUint32(readBuffer2, offset) == poolOffset); // offset_info
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100338 offset += sizeUint32;
339 poolOffset += infoSize;
Finn Williamsa0de0562020-04-22 12:27:37 +0100340 BOOST_TEST(ReadUint32(readBuffer2, offset) == poolOffset); // offset_hw_version
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100341 offset += sizeUint32;
342 poolOffset += hardwareVersionSize;
Finn Williamsa0de0562020-04-22 12:27:37 +0100343 BOOST_TEST(ReadUint32(readBuffer2, offset) == poolOffset); // offset_sw_version
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100344 offset += sizeUint32;
345 poolOffset += softwareVersionSize;
Finn Williamsa0de0562020-04-22 12:27:37 +0100346 BOOST_TEST(ReadUint32(readBuffer2, offset) == poolOffset); // offset_process_name
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100347 offset += sizeUint32;
348 poolOffset += processNameSize;
Finn Williamsa0de0562020-04-22 12:27:37 +0100349 BOOST_TEST(ReadUint32(readBuffer2, offset) == poolOffset); // offset_packet_version_table
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100350 offset += sizeUint32;
351 BOOST_TEST(ReadUint32(readBuffer2, offset) == 0); // reserved
352
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100353 const unsigned char* readData2 = readBuffer2->GetReadableData();
354
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100355 offset += sizeUint32;
356 if (infoSize)
357 {
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100358 BOOST_TEST(strcmp(reinterpret_cast<const char *>(&readData2[offset]), GetSoftwareInfo().c_str()) == 0);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100359 offset += infoSize;
360 }
361
362 if (hardwareVersionSize)
363 {
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100364 BOOST_TEST(strcmp(reinterpret_cast<const char *>(&readData2[offset]), GetHardwareVersion().c_str()) == 0);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100365 offset += hardwareVersionSize;
366 }
367
368 if (softwareVersionSize)
369 {
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100370 BOOST_TEST(strcmp(reinterpret_cast<const char *>(&readData2[offset]), GetSoftwareVersion().c_str()) == 0);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100371 offset += softwareVersionSize;
372 }
373
374 if (processNameSize)
375 {
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100376 BOOST_TEST(strcmp(reinterpret_cast<const char *>(&readData2[offset]), GetProcessName().c_str()) == 0);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100377 offset += processNameSize;
378 }
379
380 if (packetEntries)
381 {
382 BOOST_TEST((ReadUint32(readBuffer2, offset) >> 16) == packetEntries);
383 offset += sizeUint32;
Ferran Balaguer5bf1d322019-09-13 13:31:40 +0100384 for (uint32_t i = 0; i < packetEntries - 1; ++i)
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100385 {
386 BOOST_TEST(((ReadUint32(readBuffer2, offset) >> 26) & 0x3F) == 0);
387 BOOST_TEST(((ReadUint32(readBuffer2, offset) >> 16) & 0x3FF) == i);
388 offset += sizeUint32;
389 BOOST_TEST(ReadUint32(readBuffer2, offset) == EncodeVersion(1, 0, 0));
390 offset += sizeUint32;
391 }
Ferran Balaguer5bf1d322019-09-13 13:31:40 +0100392
393 BOOST_TEST(((ReadUint32(readBuffer2, offset) >> 26) & 0x3F) == 1);
394 BOOST_TEST(((ReadUint32(readBuffer2, offset) >> 16) & 0x3FF) == 0);
395 offset += sizeUint32;
396 BOOST_TEST(ReadUint32(readBuffer2, offset) == EncodeVersion(1, 0, 0));
397 offset += sizeUint32;
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100398 }
399
400 BOOST_TEST(offset == totalLength);
401}
402
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100403BOOST_AUTO_TEST_CASE(CreateDeviceRecordTest)
404{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100405 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000406 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100407
408 // Create a device for testing
409 uint16_t deviceUid = 27;
410 const std::string deviceName = "some_device";
411 uint16_t deviceCores = 3;
412 const DevicePtr device = std::make_unique<Device>(deviceUid, deviceName, deviceCores);
413
414 // Create a device record
415 SendCounterPacket::DeviceRecord deviceRecord;
416 std::string errorMessage;
417 bool result = sendCounterPacketTest.CreateDeviceRecordTest(device, deviceRecord, errorMessage);
418
419 BOOST_CHECK(result);
420 BOOST_CHECK(errorMessage.empty());
421 BOOST_CHECK(deviceRecord.size() == 6); // Size in words: header [2] + device name [4]
422
423 uint16_t deviceRecordWord0[]
424 {
425 static_cast<uint16_t>(deviceRecord[0] >> 16),
426 static_cast<uint16_t>(deviceRecord[0])
427 };
428 BOOST_CHECK(deviceRecordWord0[0] == deviceUid); // uid
429 BOOST_CHECK(deviceRecordWord0[1] == deviceCores); // cores
Finn Williamsd44815f2020-05-01 13:25:55 +0100430 BOOST_CHECK(deviceRecord[1] == 2); // name_offset
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100431 BOOST_CHECK(deviceRecord[2] == deviceName.size() + 1); // The length of the SWTrace string (name)
432 BOOST_CHECK(std::memcmp(deviceRecord.data() + 3, deviceName.data(), deviceName.size()) == 0); // name
433}
434
435BOOST_AUTO_TEST_CASE(CreateInvalidDeviceRecordTest)
436{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100437 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000438 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100439
440 // Create a device for testing
441 uint16_t deviceUid = 27;
442 const std::string deviceName = "some€£invalid‡device";
443 uint16_t deviceCores = 3;
444 const DevicePtr device = std::make_unique<Device>(deviceUid, deviceName, deviceCores);
445
446 // Create a device record
447 SendCounterPacket::DeviceRecord deviceRecord;
448 std::string errorMessage;
449 bool result = sendCounterPacketTest.CreateDeviceRecordTest(device, deviceRecord, errorMessage);
450
451 BOOST_CHECK(!result);
452 BOOST_CHECK(!errorMessage.empty());
453 BOOST_CHECK(deviceRecord.empty());
454}
455
456BOOST_AUTO_TEST_CASE(CreateCounterSetRecordTest)
457{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100458 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000459 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100460
461 // Create a counter set for testing
462 uint16_t counterSetUid = 27;
463 const std::string counterSetName = "some_counter_set";
464 uint16_t counterSetCount = 3421;
465 const CounterSetPtr counterSet = std::make_unique<CounterSet>(counterSetUid, counterSetName, counterSetCount);
466
467 // Create a counter set record
468 SendCounterPacket::CounterSetRecord counterSetRecord;
469 std::string errorMessage;
470 bool result = sendCounterPacketTest.CreateCounterSetRecordTest(counterSet, counterSetRecord, errorMessage);
471
472 BOOST_CHECK(result);
473 BOOST_CHECK(errorMessage.empty());
474 BOOST_CHECK(counterSetRecord.size() == 8); // Size in words: header [2] + counter set name [6]
475
476 uint16_t counterSetRecordWord0[]
477 {
478 static_cast<uint16_t>(counterSetRecord[0] >> 16),
479 static_cast<uint16_t>(counterSetRecord[0])
480 };
481 BOOST_CHECK(counterSetRecordWord0[0] == counterSetUid); // uid
482 BOOST_CHECK(counterSetRecordWord0[1] == counterSetCount); // cores
Finn Williamsd44815f2020-05-01 13:25:55 +0100483 BOOST_CHECK(counterSetRecord[1] == 2); // name_offset
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100484 BOOST_CHECK(counterSetRecord[2] == counterSetName.size() + 1); // The length of the SWTrace string (name)
485 BOOST_CHECK(std::memcmp(counterSetRecord.data() + 3, counterSetName.data(), counterSetName.size()) == 0); // name
486}
487
488BOOST_AUTO_TEST_CASE(CreateInvalidCounterSetRecordTest)
489{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100490 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000491 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100492
493 // Create a counter set for testing
494 uint16_t counterSetUid = 27;
495 const std::string counterSetName = "some invalid_counter€£set";
496 uint16_t counterSetCount = 3421;
497 const CounterSetPtr counterSet = std::make_unique<CounterSet>(counterSetUid, counterSetName, counterSetCount);
498
499 // Create a counter set record
500 SendCounterPacket::CounterSetRecord counterSetRecord;
501 std::string errorMessage;
502 bool result = sendCounterPacketTest.CreateCounterSetRecordTest(counterSet, counterSetRecord, errorMessage);
503
504 BOOST_CHECK(!result);
505 BOOST_CHECK(!errorMessage.empty());
506 BOOST_CHECK(counterSetRecord.empty());
507}
508
509BOOST_AUTO_TEST_CASE(CreateEventRecordTest)
510{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100511 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000512 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100513
514 // Create a counter for testing
515 uint16_t counterUid = 7256;
516 uint16_t maxCounterUid = 132;
517 uint16_t deviceUid = 132;
518 uint16_t counterSetUid = 4497;
519 uint16_t counterClass = 1;
520 uint16_t counterInterpolation = 1;
521 double counterMultiplier = 1234.567f;
522 const std::string counterName = "some_valid_counter";
523 const std::string counterDescription = "a_counter_for_testing";
524 const std::string counterUnits = "Mrads2";
Keith Davise394bd92019-12-02 15:12:19 +0000525 const CounterPtr counter = std::make_unique<Counter>(armnn::profiling::BACKEND_ID,
526 counterUid,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100527 maxCounterUid,
528 counterClass,
529 counterInterpolation,
530 counterMultiplier,
531 counterName,
532 counterDescription,
533 counterUnits,
534 deviceUid,
535 counterSetUid);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100536 ARMNN_ASSERT(counter);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100537
538 // Create an event record
539 SendCounterPacket::EventRecord eventRecord;
540 std::string errorMessage;
541 bool result = sendCounterPacketTest.CreateEventRecordTest(counter, eventRecord, errorMessage);
542
543 BOOST_CHECK(result);
544 BOOST_CHECK(errorMessage.empty());
545 BOOST_CHECK(eventRecord.size() == 24); // Size in words: header [8] + counter name [6] + description [7] + units [3]
546
547 uint16_t eventRecordWord0[]
548 {
549 static_cast<uint16_t>(eventRecord[0] >> 16),
550 static_cast<uint16_t>(eventRecord[0])
551 };
552 uint16_t eventRecordWord1[]
553 {
554 static_cast<uint16_t>(eventRecord[1] >> 16),
555 static_cast<uint16_t>(eventRecord[1])
556 };
557 uint16_t eventRecordWord2[]
558 {
559 static_cast<uint16_t>(eventRecord[2] >> 16),
560 static_cast<uint16_t>(eventRecord[2])
561 };
562 uint32_t eventRecordWord34[]
563 {
564 eventRecord[3],
565 eventRecord[4]
566 };
Finn Williamsd44815f2020-05-01 13:25:55 +0100567
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100568 BOOST_CHECK(eventRecordWord0[0] == maxCounterUid); // max_counter_uid
569 BOOST_CHECK(eventRecordWord0[1] == counterUid); // counter_uid
570 BOOST_CHECK(eventRecordWord1[0] == deviceUid); // device
Finn Williamsd44815f2020-05-01 13:25:55 +0100571
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100572 BOOST_CHECK(eventRecordWord1[1] == counterSetUid); // counter_set
573 BOOST_CHECK(eventRecordWord2[0] == counterClass); // class
574 BOOST_CHECK(eventRecordWord2[1] == counterInterpolation); // interpolation
575 BOOST_CHECK(std::memcmp(eventRecordWord34, &counterMultiplier, sizeof(counterMultiplier)) == 0); // multiplier
576
577 ARMNN_NO_CONVERSION_WARN_BEGIN
Finn Williamsd44815f2020-05-01 13:25:55 +0100578 uint32_t eventRecordBlockSize = 8u * sizeof(uint32_t);
579 uint32_t counterNameOffset = eventRecordBlockSize; // The name is the first item in pool
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100580 uint32_t counterDescriptionOffset = counterNameOffset + // Counter name offset
581 4u + // Counter name length (uint32_t)
582 counterName.size() + // 18u
583 1u + // Null-terminator
584 1u; // Rounding to the next word
Finn Williamsd44815f2020-05-01 13:25:55 +0100585
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100586 size_t counterUnitsOffset = counterDescriptionOffset + // Counter description offset
587 4u + // Counter description length (uint32_t)
588 counterDescription.size() + // 21u
589 1u + // Null-terminator
Finn Williamsd44815f2020-05-01 13:25:55 +0100590 2u; // Rounding to the next word
591
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100592 ARMNN_NO_CONVERSION_WARN_END
593
594 BOOST_CHECK(eventRecord[5] == counterNameOffset); // name_offset
595 BOOST_CHECK(eventRecord[6] == counterDescriptionOffset); // description_offset
596 BOOST_CHECK(eventRecord[7] == counterUnitsOffset); // units_offset
597
Finn Williamsd44815f2020-05-01 13:25:55 +0100598 // Offsets are relative to the start of the eventRecord
599 auto eventRecordPool = reinterpret_cast<unsigned char*>(eventRecord.data());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100600 size_t uint32_t_size = sizeof(uint32_t);
601
602 // The length of the SWTrace string (name)
603 BOOST_CHECK(eventRecordPool[counterNameOffset] == counterName.size() + 1);
604 // The counter name
605 BOOST_CHECK(std::memcmp(eventRecordPool +
606 counterNameOffset + // Offset
607 uint32_t_size /* The length of the name */,
608 counterName.data(),
609 counterName.size()) == 0); // name
610 // The null-terminator at the end of the name
611 BOOST_CHECK(eventRecordPool[counterNameOffset + uint32_t_size + counterName.size()] == '\0');
612
613 // The length of the SWTrace string (description)
614 BOOST_CHECK(eventRecordPool[counterDescriptionOffset] == counterDescription.size() + 1);
615 // The counter description
616 BOOST_CHECK(std::memcmp(eventRecordPool +
617 counterDescriptionOffset + // Offset
618 uint32_t_size /* The length of the description */,
619 counterDescription.data(),
620 counterDescription.size()) == 0); // description
621 // The null-terminator at the end of the description
622 BOOST_CHECK(eventRecordPool[counterDescriptionOffset + uint32_t_size + counterDescription.size()] == '\0');
623
624 // The length of the SWTrace namestring (units)
625 BOOST_CHECK(eventRecordPool[counterUnitsOffset] == counterUnits.size() + 1);
626 // The counter units
627 BOOST_CHECK(std::memcmp(eventRecordPool +
628 counterUnitsOffset + // Offset
629 uint32_t_size /* The length of the units */,
630 counterUnits.data(),
631 counterUnits.size()) == 0); // units
632 // The null-terminator at the end of the units
633 BOOST_CHECK(eventRecordPool[counterUnitsOffset + uint32_t_size + counterUnits.size()] == '\0');
634}
635
636BOOST_AUTO_TEST_CASE(CreateEventRecordNoUnitsTest)
637{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100638 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000639 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100640
641 // Create a counter for testing
642 uint16_t counterUid = 44312;
643 uint16_t maxCounterUid = 345;
644 uint16_t deviceUid = 101;
645 uint16_t counterSetUid = 34035;
646 uint16_t counterClass = 0;
647 uint16_t counterInterpolation = 1;
648 double counterMultiplier = 4435.0023f;
649 const std::string counterName = "some_valid_counter";
650 const std::string counterDescription = "a_counter_for_testing";
Keith Davise394bd92019-12-02 15:12:19 +0000651 const CounterPtr counter = std::make_unique<Counter>(armnn::profiling::BACKEND_ID,
652 counterUid,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100653 maxCounterUid,
654 counterClass,
655 counterInterpolation,
656 counterMultiplier,
657 counterName,
658 counterDescription,
659 "",
660 deviceUid,
661 counterSetUid);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100662 ARMNN_ASSERT(counter);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100663
664 // Create an event record
665 SendCounterPacket::EventRecord eventRecord;
666 std::string errorMessage;
667 bool result = sendCounterPacketTest.CreateEventRecordTest(counter, eventRecord, errorMessage);
668
669 BOOST_CHECK(result);
670 BOOST_CHECK(errorMessage.empty());
671 BOOST_CHECK(eventRecord.size() == 21); // Size in words: header [8] + counter name [6] + description [7]
672
673 uint16_t eventRecordWord0[]
674 {
675 static_cast<uint16_t>(eventRecord[0] >> 16),
676 static_cast<uint16_t>(eventRecord[0])
677 };
678 uint16_t eventRecordWord1[]
679 {
680 static_cast<uint16_t>(eventRecord[1] >> 16),
681 static_cast<uint16_t>(eventRecord[1])
682 };
683 uint16_t eventRecordWord2[]
684 {
685 static_cast<uint16_t>(eventRecord[2] >> 16),
686 static_cast<uint16_t>(eventRecord[2])
687 };
688 uint32_t eventRecordWord34[]
689 {
690 eventRecord[3],
691 eventRecord[4]
692 };
693 BOOST_CHECK(eventRecordWord0[0] == maxCounterUid); // max_counter_uid
694 BOOST_CHECK(eventRecordWord0[1] == counterUid); // counter_uid
695 BOOST_CHECK(eventRecordWord1[0] == deviceUid); // device
696 BOOST_CHECK(eventRecordWord1[1] == counterSetUid); // counter_set
697 BOOST_CHECK(eventRecordWord2[0] == counterClass); // class
698 BOOST_CHECK(eventRecordWord2[1] == counterInterpolation); // interpolation
699 BOOST_CHECK(std::memcmp(eventRecordWord34, &counterMultiplier, sizeof(counterMultiplier)) == 0); // multiplier
700
701 ARMNN_NO_CONVERSION_WARN_BEGIN
Finn Williamsd44815f2020-05-01 13:25:55 +0100702 uint32_t eventRecordBlockSize = 8u * sizeof(uint32_t);
703 uint32_t counterNameOffset = eventRecordBlockSize; // The name is the first item in pool
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100704 uint32_t counterDescriptionOffset = counterNameOffset + // Counter name offset
705 4u + // Counter name length (uint32_t)
706 counterName.size() + // 18u
707 1u + // Null-terminator
708 1u; // Rounding to the next word
709 ARMNN_NO_CONVERSION_WARN_END
710
711 BOOST_CHECK(eventRecord[5] == counterNameOffset); // name_offset
712 BOOST_CHECK(eventRecord[6] == counterDescriptionOffset); // description_offset
713 BOOST_CHECK(eventRecord[7] == 0); // units_offset
714
Finn Williamsd44815f2020-05-01 13:25:55 +0100715 // Offsets are relative to the start of the eventRecord
716 auto eventRecordPool = reinterpret_cast<unsigned char*>(eventRecord.data());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100717 size_t uint32_t_size = sizeof(uint32_t);
718
719 // The length of the SWTrace string (name)
720 BOOST_CHECK(eventRecordPool[counterNameOffset] == counterName.size() + 1);
721 // The counter name
722 BOOST_CHECK(std::memcmp(eventRecordPool +
723 counterNameOffset + // Offset
724 uint32_t_size, // The length of the name
725 counterName.data(),
726 counterName.size()) == 0); // name
727 // The null-terminator at the end of the name
728 BOOST_CHECK(eventRecordPool[counterNameOffset + uint32_t_size + counterName.size()] == '\0');
729
730 // The length of the SWTrace string (description)
731 BOOST_CHECK(eventRecordPool[counterDescriptionOffset] == counterDescription.size() + 1);
732 // The counter description
733 BOOST_CHECK(std::memcmp(eventRecordPool +
734 counterDescriptionOffset + // Offset
735 uint32_t_size, // The length of the description
736 counterDescription.data(),
737 counterDescription.size()) == 0); // description
738 // The null-terminator at the end of the description
739 BOOST_CHECK(eventRecordPool[counterDescriptionOffset + uint32_t_size + counterDescription.size()] == '\0');
740}
741
742BOOST_AUTO_TEST_CASE(CreateInvalidEventRecordTest1)
743{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100744 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000745 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100746
747 // Create a counter for testing
748 uint16_t counterUid = 7256;
749 uint16_t maxCounterUid = 132;
750 uint16_t deviceUid = 132;
751 uint16_t counterSetUid = 4497;
752 uint16_t counterClass = 1;
753 uint16_t counterInterpolation = 1;
754 double counterMultiplier = 1234.567f;
755 const std::string counterName = "some_invalid_counter £££"; // Invalid name
756 const std::string counterDescription = "a_counter_for_testing";
757 const std::string counterUnits = "Mrads2";
Keith Davise394bd92019-12-02 15:12:19 +0000758 const CounterPtr counter = std::make_unique<Counter>(armnn::profiling::BACKEND_ID,
759 counterUid,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100760 maxCounterUid,
761 counterClass,
762 counterInterpolation,
763 counterMultiplier,
764 counterName,
765 counterDescription,
766 counterUnits,
767 deviceUid,
768 counterSetUid);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100769 ARMNN_ASSERT(counter);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100770
771 // Create an event record
772 SendCounterPacket::EventRecord eventRecord;
773 std::string errorMessage;
774 bool result = sendCounterPacketTest.CreateEventRecordTest(counter, eventRecord, errorMessage);
775
776 BOOST_CHECK(!result);
777 BOOST_CHECK(!errorMessage.empty());
778 BOOST_CHECK(eventRecord.empty());
779}
780
781BOOST_AUTO_TEST_CASE(CreateInvalidEventRecordTest2)
782{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100783 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000784 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100785
786 // Create a counter for testing
787 uint16_t counterUid = 7256;
788 uint16_t maxCounterUid = 132;
789 uint16_t deviceUid = 132;
790 uint16_t counterSetUid = 4497;
791 uint16_t counterClass = 1;
792 uint16_t counterInterpolation = 1;
793 double counterMultiplier = 1234.567f;
794 const std::string counterName = "some_invalid_counter";
795 const std::string counterDescription = "an invalid d€scription"; // Invalid description
796 const std::string counterUnits = "Mrads2";
Keith Davise394bd92019-12-02 15:12:19 +0000797 const CounterPtr counter = std::make_unique<Counter>(armnn::profiling::BACKEND_ID,
798 counterUid,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100799 maxCounterUid,
800 counterClass,
801 counterInterpolation,
802 counterMultiplier,
803 counterName,
804 counterDescription,
805 counterUnits,
806 deviceUid,
807 counterSetUid);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100808 ARMNN_ASSERT(counter);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100809
810 // Create an event record
811 SendCounterPacket::EventRecord eventRecord;
812 std::string errorMessage;
813 bool result = sendCounterPacketTest.CreateEventRecordTest(counter, eventRecord, errorMessage);
814
815 BOOST_CHECK(!result);
816 BOOST_CHECK(!errorMessage.empty());
817 BOOST_CHECK(eventRecord.empty());
818}
819
820BOOST_AUTO_TEST_CASE(CreateInvalidEventRecordTest3)
821{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100822 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000823 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100824
825 // Create a counter for testing
826 uint16_t counterUid = 7256;
827 uint16_t maxCounterUid = 132;
828 uint16_t deviceUid = 132;
829 uint16_t counterSetUid = 4497;
830 uint16_t counterClass = 1;
831 uint16_t counterInterpolation = 1;
832 double counterMultiplier = 1234.567f;
833 const std::string counterName = "some_invalid_counter";
834 const std::string counterDescription = "a valid description";
835 const std::string counterUnits = "Mrad s2"; // Invalid units
Keith Davise394bd92019-12-02 15:12:19 +0000836 const CounterPtr counter = std::make_unique<Counter>(armnn::profiling::BACKEND_ID,
837 counterUid,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100838 maxCounterUid,
839 counterClass,
840 counterInterpolation,
841 counterMultiplier,
842 counterName,
843 counterDescription,
844 counterUnits,
845 deviceUid,
846 counterSetUid);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100847 ARMNN_ASSERT(counter);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100848
849 // Create an event record
850 SendCounterPacket::EventRecord eventRecord;
851 std::string errorMessage;
852 bool result = sendCounterPacketTest.CreateEventRecordTest(counter, eventRecord, errorMessage);
853
854 BOOST_CHECK(!result);
855 BOOST_CHECK(!errorMessage.empty());
856 BOOST_CHECK(eventRecord.empty());
857}
858
859BOOST_AUTO_TEST_CASE(CreateCategoryRecordTest)
860{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100861 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000862 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100863
864 // Create a category for testing
865 const std::string categoryName = "some_category";
Sadik Armagan4c998992020-02-25 12:44:44 +0000866 const CategoryPtr category = std::make_unique<Category>(categoryName);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100867 ARMNN_ASSERT(category);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100868 category->m_Counters = { 11u, 23u, 5670u };
869
870 // Create a collection of counters
871 Counters counters;
872 counters.insert(std::make_pair<uint16_t, CounterPtr>(11,
Keith Davise394bd92019-12-02 15:12:19 +0000873 CounterPtr(new Counter(armnn::profiling::BACKEND_ID,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100874 0,
Keith Davise394bd92019-12-02 15:12:19 +0000875 11,
876 0,
877 0,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100878 534.0003f,
879 "counter1",
880 "the first counter",
881 "millipi2",
882 0,
883 0))));
884 counters.insert(std::make_pair<uint16_t, CounterPtr>(23,
Keith Davise394bd92019-12-02 15:12:19 +0000885 CounterPtr(new Counter(armnn::profiling::BACKEND_ID,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100886 1,
Keith Davise394bd92019-12-02 15:12:19 +0000887 23,
888 0,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100889 1,
890 534.0003f,
891 "this is counter 2",
892 "the second counter",
893 "",
894 0,
895 0))));
896 counters.insert(std::make_pair<uint16_t, CounterPtr>(5670,
Keith Davise394bd92019-12-02 15:12:19 +0000897 CounterPtr(new Counter(armnn::profiling::BACKEND_ID,
898 2,
899 5670,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100900 0,
901 0,
902 534.0003f,
903 "and this is number 3",
904 "the third counter",
905 "blah_per_second",
906 0,
907 0))));
908 Counter* counter1 = counters.find(11)->second.get();
909 Counter* counter2 = counters.find(23)->second.get();
910 Counter* counter3 = counters.find(5670)->second.get();
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100911 ARMNN_ASSERT(counter1);
912 ARMNN_ASSERT(counter2);
913 ARMNN_ASSERT(counter3);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100914 uint16_t categoryEventCount = boost::numeric_cast<uint16_t>(counters.size());
915
916 // Create a category record
917 SendCounterPacket::CategoryRecord categoryRecord;
918 std::string errorMessage;
919 bool result = sendCounterPacketTest.CreateCategoryRecordTest(category, counters, categoryRecord, errorMessage);
920
921 BOOST_CHECK(result);
922 BOOST_CHECK(errorMessage.empty());
Sadik Armagan4c998992020-02-25 12:44:44 +0000923 BOOST_CHECK(categoryRecord.size() == 79); // Size in words: header [3] + event pointer table [3] +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100924 // category name [5] + event records [68 = 22 + 20 + 26]
925
Sadik Armagan4c998992020-02-25 12:44:44 +0000926 uint16_t categoryRecordWord1[]
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100927 {
928 static_cast<uint16_t>(categoryRecord[0] >> 16),
929 static_cast<uint16_t>(categoryRecord[0])
930 };
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100931 BOOST_CHECK(categoryRecordWord1[0] == categoryEventCount); // event_count
932 BOOST_CHECK(categoryRecordWord1[1] == 0); // reserved
933
934 size_t uint32_t_size = sizeof(uint32_t);
935
936 ARMNN_NO_CONVERSION_WARN_BEGIN
Finn Williamsd44815f2020-05-01 13:25:55 +0100937 uint32_t categoryRecordBlockSize = 3u * uint32_t_size;
938 uint32_t eventPointerTableOffset = categoryRecordBlockSize; // The event pointer table is the first item in pool
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100939 uint32_t categoryNameOffset = eventPointerTableOffset + // Event pointer table offset
940 categoryEventCount * uint32_t_size; // The size of the event pointer table
941 ARMNN_NO_CONVERSION_WARN_END
942
Sadik Armagan4c998992020-02-25 12:44:44 +0000943 BOOST_CHECK(categoryRecord[1] == eventPointerTableOffset); // event_pointer_table_offset
944 BOOST_CHECK(categoryRecord[2] == categoryNameOffset); // name_offset
Finn Williamsd44815f2020-05-01 13:25:55 +0100945 // Offsets are relative to the start of the category record
946 auto categoryRecordPool = reinterpret_cast<unsigned char*>(categoryRecord.data());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100947
948 // The event pointer table
949 uint32_t eventRecord0Offset = categoryRecordPool[eventPointerTableOffset + 0 * uint32_t_size];
950 uint32_t eventRecord1Offset = categoryRecordPool[eventPointerTableOffset + 1 * uint32_t_size];
951 uint32_t eventRecord2Offset = categoryRecordPool[eventPointerTableOffset + 2 * uint32_t_size];
952 BOOST_CHECK(eventRecord0Offset == 32);
953 BOOST_CHECK(eventRecord1Offset == 120);
954 BOOST_CHECK(eventRecord2Offset == 200);
955
956 // The length of the SWTrace namestring (name)
957 BOOST_CHECK(categoryRecordPool[categoryNameOffset] == categoryName.size() + 1);
958 // The category name
959 BOOST_CHECK(std::memcmp(categoryRecordPool +
960 categoryNameOffset + // Offset
961 uint32_t_size, // The length of the name
962 categoryName.data(),
963 categoryName.size()) == 0); // name
964 // The null-terminator at the end of the name
965 BOOST_CHECK(categoryRecordPool[categoryNameOffset + uint32_t_size + categoryName.size()] == '\0');
966
967 // For brevity, checking only the UIDs, max counter UIDs and names of the counters in the event records,
968 // as the event records already have a number of unit tests dedicated to them
969
970 // Counter1 UID and max counter UID
971 uint16_t eventRecord0Word0[2] = { 0u, 0u };
Finn Williamsd44815f2020-05-01 13:25:55 +0100972 std::memcpy(eventRecord0Word0, categoryRecordPool + categoryRecordBlockSize + eventRecord0Offset,
973 sizeof(eventRecord0Word0));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100974 BOOST_CHECK(eventRecord0Word0[0] == counter1->m_Uid);
975 BOOST_CHECK(eventRecord0Word0[1] == counter1->m_MaxCounterUid);
976
977 // Counter1 name
978 uint32_t counter1NameOffset = 0;
Finn Williamsd44815f2020-05-01 13:25:55 +0100979 std::memcpy(&counter1NameOffset, categoryRecordPool + eventRecord0Offset + 5u * uint32_t_size, uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100980 BOOST_CHECK(counter1NameOffset == 0);
981 // The length of the SWTrace string (name)
Finn Williamsd44815f2020-05-01 13:25:55 +0100982 BOOST_CHECK(categoryRecordPool[eventRecord0Offset + // Offset to the event record
983 categoryRecordBlockSize + // Offset to the end of the category record block
984 8u * uint32_t_size + // Offset to the event record pool
985 counter1NameOffset // Offset to the name of the counter
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100986 ] == counter1->m_Name.size() + 1); // The length of the name including the
987 // null-terminator
988 // The counter1 name
Finn Williamsd44815f2020-05-01 13:25:55 +0100989 BOOST_CHECK(std::memcmp(categoryRecordPool + // The beginning of the category pool
990 categoryRecordBlockSize + // Offset to the end of the category record block
991 eventRecord0Offset + // Offset to the event record
992 8u * uint32_t_size + // Offset to the event record pool
993 counter1NameOffset + // Offset to the name of the counter
994 uint32_t_size, // The length of the name
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100995 counter1->m_Name.data(),
996 counter1->m_Name.size()) == 0); // name
997 // The null-terminator at the end of the counter1 name
Finn Williamsd44815f2020-05-01 13:25:55 +0100998 BOOST_CHECK(categoryRecordPool[eventRecord0Offset + // Offset to the event record
999 categoryRecordBlockSize + // Offset to the end of the category record block
1000 8u * uint32_t_size + // Offset to the event record pool
1001 counter1NameOffset + // Offset to the name of the counter
1002 uint32_t_size + // The length of the name
1003 counter1->m_Name.size() // The name of the counter
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001004 ] == '\0');
1005
1006 // Counter2 name
1007 uint32_t counter2NameOffset = 0;
Finn Williamsd44815f2020-05-01 13:25:55 +01001008 std::memcpy(&counter2NameOffset, categoryRecordPool +
1009 categoryRecordBlockSize +
1010 eventRecord1Offset +
1011 5u * uint32_t_size,
1012 uint32_t_size);
1013 BOOST_CHECK(counter2NameOffset == 8u * uint32_t_size );
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001014 // The length of the SWTrace string (name)
Finn Williamsd44815f2020-05-01 13:25:55 +01001015
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001016 BOOST_CHECK(categoryRecordPool[eventRecord1Offset + // Offset to the event record
Finn Williamsd44815f2020-05-01 13:25:55 +01001017 categoryRecordBlockSize +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001018 counter2NameOffset // Offset to the name of the counter
1019 ] == counter2->m_Name.size() + 1); // The length of the name including the
1020 // null-terminator
1021 // The counter2 name
Finn Williamsd44815f2020-05-01 13:25:55 +01001022 BOOST_CHECK(std::memcmp(categoryRecordPool + // The beginning of the category pool
1023 categoryRecordBlockSize + // Offset to the end of the category record block
1024 eventRecord1Offset + // Offset to the event record
1025 counter2NameOffset + // Offset to the name of the counter
1026 uint32_t_size, // The length of the name
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001027 counter2->m_Name.data(),
1028 counter2->m_Name.size()) == 0); // name
Finn Williamsd44815f2020-05-01 13:25:55 +01001029
1030
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001031 // The null-terminator at the end of the counter2 name
Finn Williamsd44815f2020-05-01 13:25:55 +01001032 BOOST_CHECK(categoryRecordPool[eventRecord1Offset + // Offset to the event record
1033 categoryRecordBlockSize + // Offset to the end of the category record block
1034 counter2NameOffset + // Offset to the name of the counter
1035 uint32_t_size + // The length of the name
1036 counter2->m_Name.size() // The name of the counter
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001037 ] == '\0');
1038
1039 // Counter3 name
1040 uint32_t counter3NameOffset = 0;
1041 std::memcpy(&counter3NameOffset, categoryRecordPool + eventRecord2Offset + 5u * uint32_t_size, uint32_t_size);
1042 BOOST_CHECK(counter3NameOffset == 0);
1043 // The length of the SWTrace string (name)
1044 BOOST_CHECK(categoryRecordPool[eventRecord2Offset + // Offset to the event record
Finn Williamsd44815f2020-05-01 13:25:55 +01001045 categoryRecordBlockSize +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001046 8u * uint32_t_size + // Offset to the event record pool
1047 counter3NameOffset // Offset to the name of the counter
1048 ] == counter3->m_Name.size() + 1); // The length of the name including the
1049 // null-terminator
1050 // The counter3 name
1051 BOOST_CHECK(std::memcmp(categoryRecordPool + // The beginning of the category pool
Finn Williamsd44815f2020-05-01 13:25:55 +01001052 categoryRecordBlockSize +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001053 eventRecord2Offset + // Offset to the event record
1054 8u * uint32_t_size + // Offset to the event record pool
1055 counter3NameOffset + // Offset to the name of the counter
1056 uint32_t_size, // The length of the name
1057 counter3->m_Name.data(),
1058 counter3->m_Name.size()) == 0); // name
1059 // The null-terminator at the end of the counter3 name
1060 BOOST_CHECK(categoryRecordPool[eventRecord2Offset + // Offset to the event record
Finn Williamsd44815f2020-05-01 13:25:55 +01001061 categoryRecordBlockSize +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001062 8u * uint32_t_size + // Offset to the event record pool
1063 counter3NameOffset + // Offset to the name of the counter
1064 uint32_t_size + // The length of the name
1065 counter3->m_Name.size() // The name of the counter
1066 ] == '\0');
1067}
1068
1069BOOST_AUTO_TEST_CASE(CreateInvalidCategoryRecordTest1)
1070{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001071 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +00001072 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001073
1074 // Create a category for testing
1075 const std::string categoryName = "some invalid category";
Sadik Armagan4c998992020-02-25 12:44:44 +00001076 const CategoryPtr category = std::make_unique<Category>(categoryName);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001077 BOOST_CHECK(category);
1078
1079 // Create a category record
1080 Counters counters;
1081 SendCounterPacket::CategoryRecord categoryRecord;
1082 std::string errorMessage;
1083 bool result = sendCounterPacketTest.CreateCategoryRecordTest(category, counters, categoryRecord, errorMessage);
1084
1085 BOOST_CHECK(!result);
1086 BOOST_CHECK(!errorMessage.empty());
1087 BOOST_CHECK(categoryRecord.empty());
1088}
1089
1090BOOST_AUTO_TEST_CASE(CreateInvalidCategoryRecordTest2)
1091{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001092 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +00001093 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001094
1095 // Create a category for testing
1096 const std::string categoryName = "some_category";
Sadik Armagan4c998992020-02-25 12:44:44 +00001097 const CategoryPtr category = std::make_unique<Category>(categoryName);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001098 BOOST_CHECK(category);
1099 category->m_Counters = { 11u, 23u, 5670u };
1100
1101 // Create a collection of counters
1102 Counters counters;
1103 counters.insert(std::make_pair<uint16_t, CounterPtr>(11,
Keith Davise394bd92019-12-02 15:12:19 +00001104 CounterPtr(new Counter(armnn::profiling::BACKEND_ID,
1105 11,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001106 1234,
1107 0,
1108 1,
1109 534.0003f,
1110 "count€r1", // Invalid name
1111 "the first counter",
1112 "millipi2",
1113 0,
1114 0))));
1115
1116 Counter* counter1 = counters.find(11)->second.get();
1117 BOOST_CHECK(counter1);
1118
1119 // Create a category record
1120 SendCounterPacket::CategoryRecord categoryRecord;
1121 std::string errorMessage;
1122 bool result = sendCounterPacketTest.CreateCategoryRecordTest(category, counters, categoryRecord, errorMessage);
1123
1124 BOOST_CHECK(!result);
1125 BOOST_CHECK(!errorMessage.empty());
1126 BOOST_CHECK(categoryRecord.empty());
1127}
1128
1129BOOST_AUTO_TEST_CASE(SendCounterDirectoryPacketTest1)
1130{
1131 // The counter directory used for testing
1132 CounterDirectory counterDirectory;
1133
1134 // Register a device
1135 const std::string device1Name = "device1";
1136 const Device* device1 = nullptr;
1137 BOOST_CHECK_NO_THROW(device1 = counterDirectory.RegisterDevice(device1Name, 3));
1138 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
1139 BOOST_CHECK(device1);
1140
1141 // Register a device
1142 const std::string device2Name = "device2";
1143 const Device* device2 = nullptr;
1144 BOOST_CHECK_NO_THROW(device2 = counterDirectory.RegisterDevice(device2Name));
1145 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
1146 BOOST_CHECK(device2);
1147
1148 // Buffer with not enough space
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001149 MockBufferManager mockBuffer(10);
Sadik Armagan3896b472020-02-10 12:24:15 +00001150 SendCounterPacket sendCounterPacket(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001151 BOOST_CHECK_THROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory),
1152 armnn::profiling::BufferExhaustion);
1153}
1154
1155BOOST_AUTO_TEST_CASE(SendCounterDirectoryPacketTest2)
1156{
1157 // The counter directory used for testing
1158 CounterDirectory counterDirectory;
1159
1160 // Register a device
1161 const std::string device1Name = "device1";
1162 const Device* device1 = nullptr;
1163 BOOST_CHECK_NO_THROW(device1 = counterDirectory.RegisterDevice(device1Name, 3));
1164 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
1165 BOOST_CHECK(device1);
1166
1167 // Register a device
1168 const std::string device2Name = "device2";
1169 const Device* device2 = nullptr;
1170 BOOST_CHECK_NO_THROW(device2 = counterDirectory.RegisterDevice(device2Name));
1171 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
1172 BOOST_CHECK(device2);
1173
1174 // Register a counter set
1175 const std::string counterSet1Name = "counterset1";
1176 const CounterSet* counterSet1 = nullptr;
1177 BOOST_CHECK_NO_THROW(counterSet1 = counterDirectory.RegisterCounterSet(counterSet1Name));
1178 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1179 BOOST_CHECK(counterSet1);
1180
1181 // Register a category associated to "device1" and "counterset1"
1182 const std::string category1Name = "category1";
1183 const Category* category1 = nullptr;
Sadik Armagan4c998992020-02-25 12:44:44 +00001184 BOOST_CHECK_NO_THROW(category1 = counterDirectory.RegisterCategory(category1Name));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001185 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
1186 BOOST_CHECK(category1);
1187
1188 // Register a category not associated to "device2" but no counter set
1189 const std::string category2Name = "category2";
1190 const Category* category2 = nullptr;
Sadik Armagan4c998992020-02-25 12:44:44 +00001191 BOOST_CHECK_NO_THROW(category2 = counterDirectory.RegisterCategory(category2Name));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001192 BOOST_CHECK(counterDirectory.GetCategoryCount() == 2);
1193 BOOST_CHECK(category2);
1194
Keith Davis33ed2212020-03-30 10:43:41 +01001195 uint16_t numberOfCores = 4;
Sadik Armagan4c998992020-02-25 12:44:44 +00001196
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001197 // Register a counter associated to "category1"
1198 const Counter* counter1 = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001199 BOOST_CHECK_NO_THROW(counter1 = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1200 0,
1201 category1Name,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001202 0,
1203 1,
1204 123.45f,
1205 "counter1",
1206 "counter1description",
Sadik Armagan4c998992020-02-25 12:44:44 +00001207 std::string("counter1units"),
1208 numberOfCores));
Keith Davis33ed2212020-03-30 10:43:41 +01001209 BOOST_CHECK(counterDirectory.GetCounterCount() == 4);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001210 BOOST_CHECK(counter1);
1211
1212 // Register a counter associated to "category1"
1213 const Counter* counter2 = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001214 BOOST_CHECK_NO_THROW(counter2 = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1215 4,
1216 category1Name,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001217 1,
1218 0,
1219 330.1245656765f,
1220 "counter2",
1221 "counter2description",
1222 std::string("counter2units"),
1223 armnn::EmptyOptional(),
1224 device2->m_Uid,
1225 0));
Keith Davis33ed2212020-03-30 10:43:41 +01001226 BOOST_CHECK(counterDirectory.GetCounterCount() == 5);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001227 BOOST_CHECK(counter2);
1228
1229 // Register a counter associated to "category2"
1230 const Counter* counter3 = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001231 BOOST_CHECK_NO_THROW(counter3 = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1232 5,
1233 category2Name,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001234 1,
1235 1,
1236 0.0000045399f,
1237 "counter3",
1238 "counter3description",
1239 armnn::EmptyOptional(),
Keith Davis33ed2212020-03-30 10:43:41 +01001240 numberOfCores,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001241 device2->m_Uid,
1242 counterSet1->m_Uid));
1243 BOOST_CHECK(counterDirectory.GetCounterCount() == 9);
1244 BOOST_CHECK(counter3);
1245
1246 // Buffer with enough space
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001247 MockBufferManager mockBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00001248 SendCounterPacket sendCounterPacket(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001249 BOOST_CHECK_NO_THROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory));
1250
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001251 // Get the readable buffer
1252 auto readBuffer = mockBuffer.GetReadableBuffer();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001253
1254 // Check the packet header
Finn Williams985fecf2020-04-30 11:06:43 +01001255 const uint32_t packetHeaderWord0 = ReadUint32(readBuffer, 0);
1256 const uint32_t packetHeaderWord1 = ReadUint32(readBuffer, 4);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001257 BOOST_TEST(((packetHeaderWord0 >> 26) & 0x3F) == 0); // packet_family
1258 BOOST_TEST(((packetHeaderWord0 >> 16) & 0x3FF) == 2); // packet_id
Keith Davis33ed2212020-03-30 10:43:41 +01001259 BOOST_TEST(packetHeaderWord1 == 432); // data_length
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001260
1261 // Check the body header
Finn Williams985fecf2020-04-30 11:06:43 +01001262 const uint32_t bodyHeaderWord0 = ReadUint32(readBuffer, 8);
1263 const uint32_t bodyHeaderWord1 = ReadUint32(readBuffer, 12);
1264 const uint32_t bodyHeaderWord2 = ReadUint32(readBuffer, 16);
1265 const uint32_t bodyHeaderWord3 = ReadUint32(readBuffer, 20);
1266 const uint32_t bodyHeaderWord4 = ReadUint32(readBuffer, 24);
1267 const uint32_t bodyHeaderWord5 = ReadUint32(readBuffer, 28);
1268 const uint16_t deviceRecordCount = static_cast<uint16_t>(bodyHeaderWord0 >> 16);
1269 const uint16_t counterSetRecordCount = static_cast<uint16_t>(bodyHeaderWord2 >> 16);
1270 const uint16_t categoryRecordCount = static_cast<uint16_t>(bodyHeaderWord4 >> 16);
1271 BOOST_TEST(deviceRecordCount == 2); // device_records_count
1272 BOOST_TEST(bodyHeaderWord1 == bodyHeaderSize * 4); // device_records_pointer_table_offset
1273 BOOST_TEST(counterSetRecordCount == 1); // counter_set_count
1274 BOOST_TEST(bodyHeaderWord3 == 8 + bodyHeaderSize * 4); // counter_set_pointer_table_offset
1275 BOOST_TEST(categoryRecordCount == 2); // categories_count
1276 BOOST_TEST(bodyHeaderWord5 == 12 + bodyHeaderSize * 4); // categories_pointer_table_offset
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001277
1278 // Check the device records pointer table
Finn Williams985fecf2020-04-30 11:06:43 +01001279 const uint32_t deviceRecordOffset0 = ReadUint32(readBuffer, 32);
1280 const uint32_t deviceRecordOffset1 = ReadUint32(readBuffer, 36);
Finn Williamsd44815f2020-05-01 13:25:55 +01001281 BOOST_TEST(deviceRecordOffset0 == 20); // Device record offset for "device1"
1282 BOOST_TEST(deviceRecordOffset1 == 40); // Device record offset for "device2"
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001283
1284 // Check the counter set pointer table
Finn Williams985fecf2020-04-30 11:06:43 +01001285 const uint32_t counterSetRecordOffset0 = ReadUint32(readBuffer, 40);
Finn Williamsd44815f2020-05-01 13:25:55 +01001286 BOOST_TEST(counterSetRecordOffset0 == 52); // Counter set record offset for "counterset1"
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001287
1288 // Check the category pointer table
Finn Williams985fecf2020-04-30 11:06:43 +01001289 const uint32_t categoryRecordOffset0 = ReadUint32(readBuffer, 44);
1290 const uint32_t categoryRecordOffset1 = ReadUint32(readBuffer, 48);
Finn Williamsd44815f2020-05-01 13:25:55 +01001291 BOOST_TEST(categoryRecordOffset0 == 72); // Category record offset for "category1"
1292 BOOST_TEST(categoryRecordOffset1 == 176); // Category record offset for "category2"
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001293
1294 // Get the device record pool offset
Finn Williams985fecf2020-04-30 11:06:43 +01001295 const uint32_t uint32_t_size = sizeof(uint32_t);
Finn Williamsd44815f2020-05-01 13:25:55 +01001296 const uint32_t packetHeaderSize = 2u * uint32_t_size;
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001297
1298 // Device record structure/collection used for testing
1299 struct DeviceRecord
1300 {
1301 uint16_t uid;
1302 uint16_t cores;
1303 uint32_t name_offset;
1304 uint32_t name_length;
1305 std::string name;
1306 };
1307 std::vector<DeviceRecord> deviceRecords;
Finn Williamsd44815f2020-05-01 13:25:55 +01001308 const uint32_t deviceRecordsPointerTableOffset = packetHeaderSize +
Finn Williams985fecf2020-04-30 11:06:43 +01001309 bodyHeaderWord1; // device_records_pointer_table_offset
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001310
1311 const unsigned char* readData = readBuffer->GetReadableData();
1312
Finn Williamsd44815f2020-05-01 13:25:55 +01001313 uint32_t offset = 0;
1314 std::vector<uint32_t> data(800);
1315
1316 for (uint32_t i = 0; i < 800; i+=uint32_t_size)
1317 {
1318 data[i] = ReadUint32(readBuffer, offset);
1319 offset += uint32_t_size;
1320 }
1321
1322 std::vector<uint32_t> deviceRecordOffsets(deviceRecordCount);
1323 offset = deviceRecordsPointerTableOffset;
1324 for (uint32_t i = 0; i < deviceRecordCount; ++i)
1325 {
1326 // deviceRecordOffset is relative to the start of the deviceRecordsPointerTable
1327 deviceRecordOffsets[i] = ReadUint32(readBuffer, offset) + deviceRecordsPointerTableOffset;
1328 offset += uint32_t_size;
1329 }
1330
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001331 for (uint32_t i = 0; i < deviceRecordCount; i++)
1332 {
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001333 // Collect the data for the device record
Finn Williamsd44815f2020-05-01 13:25:55 +01001334 const uint32_t deviceRecordWord0 = ReadUint32(readBuffer, deviceRecordOffsets[i] + 0 * uint32_t_size);
1335 const uint32_t deviceRecordWord1 = ReadUint32(readBuffer, deviceRecordOffsets[i] + 1 * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001336 DeviceRecord deviceRecord;
1337 deviceRecord.uid = static_cast<uint16_t>(deviceRecordWord0 >> 16); // uid
1338 deviceRecord.cores = static_cast<uint16_t>(deviceRecordWord0); // cores
1339 deviceRecord.name_offset = deviceRecordWord1; // name_offset
1340
Finn Williamsd44815f2020-05-01 13:25:55 +01001341 uint32_t deviceRecordPoolOffset = deviceRecordOffsets[i] + // Packet body offset
1342 deviceRecord.name_offset * uint32_t_size; // Device name offset
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001343 uint32_t deviceRecordNameLength = ReadUint32(readBuffer, deviceRecordPoolOffset);
1344 deviceRecord.name_length = deviceRecordNameLength; // name_length
1345 unsigned char deviceRecordNameNullTerminator = // name null-terminator
1346 ReadUint8(readBuffer, deviceRecordPoolOffset + uint32_t_size + deviceRecordNameLength - 1);
1347 BOOST_CHECK(deviceRecordNameNullTerminator == '\0');
1348 std::vector<unsigned char> deviceRecordNameBuffer(deviceRecord.name_length - 1);
1349 std::memcpy(deviceRecordNameBuffer.data(),
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001350 readData + deviceRecordPoolOffset + uint32_t_size, deviceRecordNameBuffer.size());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001351 deviceRecord.name.assign(deviceRecordNameBuffer.begin(), deviceRecordNameBuffer.end()); // name
1352
1353 deviceRecords.push_back(deviceRecord);
1354 }
1355
1356 // Check that the device records are correct
1357 BOOST_CHECK(deviceRecords.size() == 2);
1358 for (const DeviceRecord& deviceRecord : deviceRecords)
1359 {
1360 const Device* device = counterDirectory.GetDevice(deviceRecord.uid);
1361 BOOST_CHECK(device);
1362 BOOST_CHECK(device->m_Uid == deviceRecord.uid);
1363 BOOST_CHECK(device->m_Cores == deviceRecord.cores);
1364 BOOST_CHECK(device->m_Name == deviceRecord.name);
1365 }
1366
Finn Williamsd44815f2020-05-01 13:25:55 +01001367
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001368 // Counter set record structure/collection used for testing
1369 struct CounterSetRecord
1370 {
1371 uint16_t uid;
1372 uint16_t count;
1373 uint32_t name_offset;
1374 uint32_t name_length;
1375 std::string name;
1376 };
1377 std::vector<CounterSetRecord> counterSetRecords;
Finn Williams985fecf2020-04-30 11:06:43 +01001378 const uint32_t counterSetRecordsPointerTableOffset = 2u * uint32_t_size + // packet_header
1379 bodyHeaderWord3; // counter_set_pointer_table_offset
Finn Williamsd44815f2020-05-01 13:25:55 +01001380
1381 offset = counterSetRecordsPointerTableOffset;
1382 std::vector<uint32_t> counterSetRecordOffsets(counterSetRecordCount);
1383
1384 for (uint32_t i = 0; i < counterSetRecordCount; ++i)
1385 {
1386 // counterSetRecordOffset is relative to the start of the dcounterSetRecordsPointerTable
1387 counterSetRecordOffsets[i] = ReadUint32(readBuffer, offset) + counterSetRecordsPointerTableOffset;
1388 offset += uint32_t_size;
1389 }
1390
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001391 for (uint32_t i = 0; i < counterSetRecordCount; i++)
1392 {
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001393 // Collect the data for the counter set record
Finn Williamsd44815f2020-05-01 13:25:55 +01001394 const uint32_t counterSetRecordWord0 = ReadUint32(readBuffer, counterSetRecordOffsets[i] + 0 * uint32_t_size);
1395 const uint32_t counterSetRecordWord1 = ReadUint32(readBuffer, counterSetRecordOffsets[i] + 1 * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001396 CounterSetRecord counterSetRecord;
1397 counterSetRecord.uid = static_cast<uint16_t>(counterSetRecordWord0 >> 16); // uid
1398 counterSetRecord.count = static_cast<uint16_t>(counterSetRecordWord0); // count
1399 counterSetRecord.name_offset = counterSetRecordWord1; // name_offset
1400
Finn Williamsd44815f2020-05-01 13:25:55 +01001401 uint32_t counterSetRecordPoolOffset = counterSetRecordOffsets[i] + // Packet body offset
1402 counterSetRecord.name_offset * uint32_t_size; // Counter set name offset
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001403 uint32_t counterSetRecordNameLength = ReadUint32(readBuffer, counterSetRecordPoolOffset);
1404 counterSetRecord.name_length = counterSetRecordNameLength; // name_length
1405 unsigned char counterSetRecordNameNullTerminator = // name null-terminator
1406 ReadUint8(readBuffer, counterSetRecordPoolOffset + uint32_t_size + counterSetRecordNameLength - 1);
1407 BOOST_CHECK(counterSetRecordNameNullTerminator == '\0');
1408 std::vector<unsigned char> counterSetRecordNameBuffer(counterSetRecord.name_length - 1);
1409 std::memcpy(counterSetRecordNameBuffer.data(),
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001410 readData + counterSetRecordPoolOffset + uint32_t_size, counterSetRecordNameBuffer.size());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001411 counterSetRecord.name.assign(counterSetRecordNameBuffer.begin(), counterSetRecordNameBuffer.end()); // name
1412
1413 counterSetRecords.push_back(counterSetRecord);
1414 }
1415
1416 // Check that the counter set records are correct
1417 BOOST_CHECK(counterSetRecords.size() == 1);
1418 for (const CounterSetRecord& counterSetRecord : counterSetRecords)
1419 {
1420 const CounterSet* counterSet = counterDirectory.GetCounterSet(counterSetRecord.uid);
1421 BOOST_CHECK(counterSet);
1422 BOOST_CHECK(counterSet->m_Uid == counterSetRecord.uid);
1423 BOOST_CHECK(counterSet->m_Count == counterSetRecord.count);
1424 BOOST_CHECK(counterSet->m_Name == counterSetRecord.name);
1425 }
1426
1427 // Event record structure/collection used for testing
1428 struct EventRecord
1429 {
1430 uint16_t counter_uid;
1431 uint16_t max_counter_uid;
1432 uint16_t device;
1433 uint16_t counter_set;
1434 uint16_t counter_class;
1435 uint16_t interpolation;
1436 double multiplier;
1437 uint32_t name_offset;
1438 uint32_t name_length;
1439 std::string name;
1440 uint32_t description_offset;
1441 uint32_t description_length;
1442 std::string description;
1443 uint32_t units_offset;
1444 uint32_t units_length;
1445 std::string units;
1446 };
1447 // Category record structure/collection used for testing
1448 struct CategoryRecord
1449 {
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001450 uint16_t event_count;
1451 uint32_t event_pointer_table_offset;
1452 uint32_t name_offset;
1453 uint32_t name_length;
1454 std::string name;
1455 std::vector<uint32_t> event_pointer_table;
1456 std::vector<EventRecord> event_records;
1457 };
1458 std::vector<CategoryRecord> categoryRecords;
Finn Williams985fecf2020-04-30 11:06:43 +01001459 const uint32_t categoryRecordsPointerTableOffset = 2u * uint32_t_size + // packet_header
1460 bodyHeaderWord5; // categories_pointer_table_offset
Finn Williamsd44815f2020-05-01 13:25:55 +01001461
1462 offset = categoryRecordsPointerTableOffset;
1463 std::vector<uint32_t> categoryRecordOffsets(categoryRecordCount);
1464 for (uint32_t i = 0; i < categoryRecordCount; ++i)
1465 {
1466 // categoryRecordOffset is relative to the start of the categoryRecordsPointerTable
1467 categoryRecordOffsets[i] = ReadUint32(readBuffer, offset) + categoryRecordsPointerTableOffset;
1468 offset += uint32_t_size;
1469 }
1470
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001471 for (uint32_t i = 0; i < categoryRecordCount; i++)
1472 {
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001473 // Collect the data for the category record
Finn Williamsd44815f2020-05-01 13:25:55 +01001474 const uint32_t categoryRecordWord1 = ReadUint32(readBuffer, categoryRecordOffsets[i] + 0 * uint32_t_size);
1475 const uint32_t categoryRecordWord2 = ReadUint32(readBuffer, categoryRecordOffsets[i] + 1 * uint32_t_size);
1476 const uint32_t categoryRecordWord3 = ReadUint32(readBuffer, categoryRecordOffsets[i] + 2 * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001477 CategoryRecord categoryRecord;
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001478 categoryRecord.event_count = static_cast<uint16_t>(categoryRecordWord1 >> 16); // event_count
1479 categoryRecord.event_pointer_table_offset = categoryRecordWord2; // event_pointer_table_offset
1480 categoryRecord.name_offset = categoryRecordWord3; // name_offset
1481
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001482 uint32_t categoryRecordNameLength = ReadUint32(readBuffer,
Finn Williamsd44815f2020-05-01 13:25:55 +01001483 categoryRecordOffsets[i] + categoryRecord.name_offset);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001484 categoryRecord.name_length = categoryRecordNameLength; // name_length
1485 unsigned char categoryRecordNameNullTerminator =
1486 ReadUint8(readBuffer,
Finn Williamsd44815f2020-05-01 13:25:55 +01001487 categoryRecordOffsets[i] +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001488 categoryRecord.name_offset +
1489 uint32_t_size +
1490 categoryRecordNameLength - 1); // name null-terminator
1491 BOOST_CHECK(categoryRecordNameNullTerminator == '\0');
1492 std::vector<unsigned char> categoryRecordNameBuffer(categoryRecord.name_length - 1);
1493 std::memcpy(categoryRecordNameBuffer.data(),
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001494 readData +
Finn Williamsd44815f2020-05-01 13:25:55 +01001495 categoryRecordOffsets[i] +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001496 categoryRecord.name_offset +
1497 uint32_t_size,
1498 categoryRecordNameBuffer.size());
1499 categoryRecord.name.assign(categoryRecordNameBuffer.begin(), categoryRecordNameBuffer.end()); // name
1500
1501 categoryRecord.event_pointer_table.resize(categoryRecord.event_count);
Finn Williamsd44815f2020-05-01 13:25:55 +01001502 offset = categoryRecordOffsets[i] + categoryRecord.event_pointer_table_offset;
1503 for (uint32_t eventOffsetIndex = 0; eventOffsetIndex < categoryRecord.event_count; ++eventOffsetIndex)
1504 {
1505 // eventRecordOffset is relative to the start of the event pointer table
1506 categoryRecord.event_pointer_table[eventOffsetIndex] = ReadUint32(readBuffer, offset) +
1507 categoryRecordOffsets[i] +
1508 categoryRecord.event_pointer_table_offset;
1509 offset += uint32_t_size;
1510 }
1511
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001512 for (uint32_t eventIndex = 0; eventIndex < categoryRecord.event_count; eventIndex++)
1513 {
Finn Williamsd44815f2020-05-01 13:25:55 +01001514 const uint32_t eventOffset = categoryRecord.event_pointer_table[eventIndex];
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001515 // Collect the data for the event record
Finn Williamsd44815f2020-05-01 13:25:55 +01001516 const uint32_t eventRecordWord0 = ReadUint32(readBuffer, eventOffset + 0 * uint32_t_size);
1517 const uint32_t eventRecordWord1 = ReadUint32(readBuffer, eventOffset + 1 * uint32_t_size);
1518 const uint32_t eventRecordWord2 = ReadUint32(readBuffer, eventOffset + 2 * uint32_t_size);
1519 const uint64_t eventRecordWord34 = ReadUint64(readBuffer, eventOffset + 3 * uint32_t_size);
1520 const uint32_t eventRecordWord5 = ReadUint32(readBuffer, eventOffset + 5 * uint32_t_size);
1521 const uint32_t eventRecordWord6 = ReadUint32(readBuffer, eventOffset + 6 * uint32_t_size);
1522 const uint32_t eventRecordWord7 = ReadUint32(readBuffer, eventOffset + 7 * uint32_t_size);
1523
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001524 EventRecord eventRecord;
1525 eventRecord.counter_uid = static_cast<uint16_t>(eventRecordWord0); // counter_uid
1526 eventRecord.max_counter_uid = static_cast<uint16_t>(eventRecordWord0 >> 16); // max_counter_uid
1527 eventRecord.device = static_cast<uint16_t>(eventRecordWord1 >> 16); // device
1528 eventRecord.counter_set = static_cast<uint16_t>(eventRecordWord1); // counter_set
1529 eventRecord.counter_class = static_cast<uint16_t>(eventRecordWord2 >> 16); // class
1530 eventRecord.interpolation = static_cast<uint16_t>(eventRecordWord2); // interpolation
1531 std::memcpy(&eventRecord.multiplier, &eventRecordWord34, sizeof(eventRecord.multiplier)); // multiplier
1532 eventRecord.name_offset = static_cast<uint32_t>(eventRecordWord5); // name_offset
1533 eventRecord.description_offset = static_cast<uint32_t>(eventRecordWord6); // description_offset
1534 eventRecord.units_offset = static_cast<uint32_t>(eventRecordWord7); // units_offset
1535
Finn Williamsd44815f2020-05-01 13:25:55 +01001536 uint32_t eventRecordNameLength = ReadUint32(readBuffer, eventOffset + eventRecord.name_offset);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001537 eventRecord.name_length = eventRecordNameLength; // name_length
1538 unsigned char eventRecordNameNullTerminator =
1539 ReadUint8(readBuffer,
Finn Williamsd44815f2020-05-01 13:25:55 +01001540 eventOffset +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001541 eventRecord.name_offset +
1542 uint32_t_size +
1543 eventRecordNameLength - 1); // name null-terminator
1544 BOOST_CHECK(eventRecordNameNullTerminator == '\0');
1545 std::vector<unsigned char> eventRecordNameBuffer(eventRecord.name_length - 1);
1546 std::memcpy(eventRecordNameBuffer.data(),
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001547 readData +
Finn Williamsd44815f2020-05-01 13:25:55 +01001548 eventOffset +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001549 eventRecord.name_offset +
1550 uint32_t_size,
1551 eventRecordNameBuffer.size());
1552 eventRecord.name.assign(eventRecordNameBuffer.begin(), eventRecordNameBuffer.end()); // name
1553
1554 uint32_t eventRecordDescriptionLength = ReadUint32(readBuffer,
Finn Williamsd44815f2020-05-01 13:25:55 +01001555 eventOffset + eventRecord.description_offset);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001556 eventRecord.description_length = eventRecordDescriptionLength; // description_length
1557 unsigned char eventRecordDescriptionNullTerminator =
1558 ReadUint8(readBuffer,
Finn Williamsd44815f2020-05-01 13:25:55 +01001559 eventOffset +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001560 eventRecord.description_offset +
1561 uint32_t_size +
1562 eventRecordDescriptionLength - 1); // description null-terminator
1563 BOOST_CHECK(eventRecordDescriptionNullTerminator == '\0');
1564 std::vector<unsigned char> eventRecordDescriptionBuffer(eventRecord.description_length - 1);
1565 std::memcpy(eventRecordDescriptionBuffer.data(),
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001566 readData +
Finn Williamsd44815f2020-05-01 13:25:55 +01001567 eventOffset +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001568 eventRecord.description_offset +
1569 uint32_t_size,
1570 eventRecordDescriptionBuffer.size());
1571 eventRecord.description.assign(eventRecordDescriptionBuffer.begin(),
1572 eventRecordDescriptionBuffer.end()); // description
1573
1574 if (eventRecord.units_offset > 0)
1575 {
1576 uint32_t eventRecordUnitsLength = ReadUint32(readBuffer,
Finn Williamsd44815f2020-05-01 13:25:55 +01001577 eventOffset + eventRecord.units_offset);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001578 eventRecord.units_length = eventRecordUnitsLength; // units_length
1579 unsigned char eventRecordUnitsNullTerminator =
1580 ReadUint8(readBuffer,
Finn Williamsd44815f2020-05-01 13:25:55 +01001581 eventOffset +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001582 eventRecord.units_offset +
1583 uint32_t_size +
1584 eventRecordUnitsLength - 1); // units null-terminator
1585 BOOST_CHECK(eventRecordUnitsNullTerminator == '\0');
1586 std::vector<unsigned char> eventRecordUnitsBuffer(eventRecord.units_length - 1);
1587 std::memcpy(eventRecordUnitsBuffer.data(),
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001588 readData +
Finn Williamsd44815f2020-05-01 13:25:55 +01001589 eventOffset +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001590 eventRecord.units_offset +
1591 uint32_t_size,
1592 eventRecordUnitsBuffer.size());
1593 eventRecord.units.assign(eventRecordUnitsBuffer.begin(), eventRecordUnitsBuffer.end()); // units
1594 }
1595
1596 categoryRecord.event_records.push_back(eventRecord);
1597 }
1598
1599 categoryRecords.push_back(categoryRecord);
1600 }
1601
1602 // Check that the category records are correct
1603 BOOST_CHECK(categoryRecords.size() == 2);
1604 for (const CategoryRecord& categoryRecord : categoryRecords)
1605 {
1606 const Category* category = counterDirectory.GetCategory(categoryRecord.name);
1607 BOOST_CHECK(category);
1608 BOOST_CHECK(category->m_Name == categoryRecord.name);
Keith Davis33ed2212020-03-30 10:43:41 +01001609 BOOST_CHECK(category->m_Counters.size() == categoryRecord.event_count + static_cast<size_t>(numberOfCores) -1);
1610 BOOST_CHECK(category->m_Counters.size() == categoryRecord.event_count + static_cast<size_t>(numberOfCores) -1);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001611
1612 // Check that the event records are correct
1613 for (const EventRecord& eventRecord : categoryRecord.event_records)
1614 {
1615 const Counter* counter = counterDirectory.GetCounter(eventRecord.counter_uid);
1616 BOOST_CHECK(counter);
1617 BOOST_CHECK(counter->m_MaxCounterUid == eventRecord.max_counter_uid);
1618 BOOST_CHECK(counter->m_DeviceUid == eventRecord.device);
1619 BOOST_CHECK(counter->m_CounterSetUid == eventRecord.counter_set);
1620 BOOST_CHECK(counter->m_Class == eventRecord.counter_class);
1621 BOOST_CHECK(counter->m_Interpolation == eventRecord.interpolation);
1622 BOOST_CHECK(counter->m_Multiplier == eventRecord.multiplier);
1623 BOOST_CHECK(counter->m_Name == eventRecord.name);
1624 BOOST_CHECK(counter->m_Description == eventRecord.description);
1625 BOOST_CHECK(counter->m_Units == eventRecord.units);
1626 }
1627 }
1628}
1629
1630BOOST_AUTO_TEST_CASE(SendCounterDirectoryPacketTest3)
1631{
1632 // Using a mock counter directory that allows to register invalid objects
1633 MockCounterDirectory counterDirectory;
1634
1635 // Register an invalid device
1636 const std::string deviceName = "inv@lid dev!c€";
1637 const Device* device = nullptr;
1638 BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName, 3));
1639 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
1640 BOOST_CHECK(device);
1641
1642 // Buffer with enough space
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001643 MockBufferManager mockBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00001644 SendCounterPacket sendCounterPacket(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001645 BOOST_CHECK_THROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory), armnn::RuntimeException);
1646}
1647
1648BOOST_AUTO_TEST_CASE(SendCounterDirectoryPacketTest4)
1649{
1650 // Using a mock counter directory that allows to register invalid objects
1651 MockCounterDirectory counterDirectory;
1652
1653 // Register an invalid counter set
1654 const std::string counterSetName = "inv@lid count€rs€t";
1655 const CounterSet* counterSet = nullptr;
1656 BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
1657 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1658 BOOST_CHECK(counterSet);
1659
1660 // Buffer with enough space
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001661 MockBufferManager mockBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00001662 SendCounterPacket sendCounterPacket(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001663 BOOST_CHECK_THROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory), armnn::RuntimeException);
1664}
1665
1666BOOST_AUTO_TEST_CASE(SendCounterDirectoryPacketTest5)
1667{
1668 // Using a mock counter directory that allows to register invalid objects
1669 MockCounterDirectory counterDirectory;
1670
1671 // Register an invalid category
1672 const std::string categoryName = "c@t€gory";
1673 const Category* category = nullptr;
1674 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
1675 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
1676 BOOST_CHECK(category);
1677
1678 // Buffer with enough space
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001679 MockBufferManager mockBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00001680 SendCounterPacket sendCounterPacket(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001681 BOOST_CHECK_THROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory), armnn::RuntimeException);
1682}
1683
1684BOOST_AUTO_TEST_CASE(SendCounterDirectoryPacketTest6)
1685{
1686 // Using a mock counter directory that allows to register invalid objects
1687 MockCounterDirectory counterDirectory;
1688
1689 // Register an invalid device
1690 const std::string deviceName = "inv@lid dev!c€";
1691 const Device* device = nullptr;
1692 BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName, 3));
1693 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
1694 BOOST_CHECK(device);
1695
1696 // Register an invalid counter set
1697 const std::string counterSetName = "inv@lid count€rs€t";
1698 const CounterSet* counterSet = nullptr;
1699 BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
1700 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1701 BOOST_CHECK(counterSet);
1702
1703 // Register an invalid category associated to an invalid device and an invalid counter set
1704 const std::string categoryName = "c@t€gory";
1705 const Category* category = nullptr;
Sadik Armagan4c998992020-02-25 12:44:44 +00001706 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001707 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
1708 BOOST_CHECK(category);
1709
1710 // Buffer with enough space
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001711 MockBufferManager mockBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00001712 SendCounterPacket sendCounterPacket(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001713 BOOST_CHECK_THROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory), armnn::RuntimeException);
1714}
1715
1716BOOST_AUTO_TEST_CASE(SendCounterDirectoryPacketTest7)
1717{
1718 // Using a mock counter directory that allows to register invalid objects
1719 MockCounterDirectory counterDirectory;
1720
1721 // Register an valid device
1722 const std::string deviceName = "valid device";
1723 const Device* device = nullptr;
1724 BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName, 3));
1725 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
1726 BOOST_CHECK(device);
1727
1728 // Register an valid counter set
1729 const std::string counterSetName = "valid counterset";
1730 const CounterSet* counterSet = nullptr;
1731 BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
1732 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1733 BOOST_CHECK(counterSet);
1734
1735 // Register an valid category associated to a valid device and a valid counter set
1736 const std::string categoryName = "category";
1737 const Category* category = nullptr;
Sadik Armagan4c998992020-02-25 12:44:44 +00001738 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001739 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
1740 BOOST_CHECK(category);
1741
1742 // Register an invalid counter associated to a valid category
1743 const Counter* counter = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001744 BOOST_CHECK_NO_THROW(counter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1745 0,
1746 categoryName,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001747 0,
1748 1,
1749 123.45f,
1750 "counter",
1751 "counter description",
1752 std::string("invalid counter units"),
1753 5,
1754 device->m_Uid,
1755 counterSet->m_Uid));
1756 BOOST_CHECK(counterDirectory.GetCounterCount() == 5);
1757 BOOST_CHECK(counter);
1758
1759 // Buffer with enough space
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001760 MockBufferManager mockBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00001761 SendCounterPacket sendCounterPacket(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001762 BOOST_CHECK_THROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory), armnn::RuntimeException);
1763}
Ferran Balaguer47d0fe92019-09-04 16:47:34 +01001764
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001765BOOST_AUTO_TEST_CASE(SendThreadTest0)
1766{
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01001767 ProfilingStateMachine profilingStateMachine;
1768 SetActiveProfilingState(profilingStateMachine);
1769
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001770 MockProfilingConnection mockProfilingConnection;
Matteo Martincigh61d6f732019-10-03 11:21:18 +01001771 MockStreamCounterBuffer mockStreamCounterBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +00001772 SendCounterPacket sendCounterPacket(mockStreamCounterBuffer);
1773 SendThread sendThread(profilingStateMachine, mockStreamCounterBuffer, sendCounterPacket);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001774
1775 // Try to start the send thread many times, it must only start once
1776
Sadik Armagan3896b472020-02-10 12:24:15 +00001777 sendThread.Start(mockProfilingConnection);
1778 BOOST_CHECK(sendThread.IsRunning());
1779 sendThread.Start(mockProfilingConnection);
1780 sendThread.Start(mockProfilingConnection);
1781 sendThread.Start(mockProfilingConnection);
1782 sendThread.Start(mockProfilingConnection);
1783 BOOST_CHECK(sendThread.IsRunning());
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001784
Sadik Armagan3896b472020-02-10 12:24:15 +00001785 sendThread.Stop();
1786 BOOST_CHECK(!sendThread.IsRunning());
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001787}
1788
1789BOOST_AUTO_TEST_CASE(SendThreadTest1)
1790{
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01001791 ProfilingStateMachine profilingStateMachine;
1792 SetActiveProfilingState(profilingStateMachine);
1793
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001794 unsigned int totalWrittenSize = 0;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001795
1796 MockProfilingConnection mockProfilingConnection;
Matteo Martincigh61d6f732019-10-03 11:21:18 +01001797 MockStreamCounterBuffer mockStreamCounterBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00001798 SendCounterPacket sendCounterPacket(mockStreamCounterBuffer);
1799 SendThread sendThread(profilingStateMachine, mockStreamCounterBuffer, sendCounterPacket);
1800 sendThread.Start(mockProfilingConnection);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001801
1802 // Interleaving writes and reads to/from the buffer with pauses to test that the send thread actually waits for
1803 // something to become available for reading
1804
Colm Donelan2ba48d22019-11-29 09:10:59 +00001805 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001806
1807 CounterDirectory counterDirectory;
1808 sendCounterPacket.SendStreamMetaDataPacket();
1809
Finn Williamsa0de0562020-04-22 12:27:37 +01001810 totalWrittenSize += GetStreamMetaDataPacketSize();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001811
Sadik Armagan3896b472020-02-10 12:24:15 +00001812 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001813
Colm Donelan2ba48d22019-11-29 09:10:59 +00001814 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001815
1816 sendCounterPacket.SendCounterDirectoryPacket(counterDirectory);
1817
1818 // Get the size of the Counter Directory Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001819 unsigned int counterDirectoryPacketSize = 32;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001820 totalWrittenSize += counterDirectoryPacketSize;
1821
Sadik Armagan3896b472020-02-10 12:24:15 +00001822 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001823
Colm Donelan2ba48d22019-11-29 09:10:59 +00001824 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001825
1826 sendCounterPacket.SendPeriodicCounterCapturePacket(123u,
1827 {
1828 { 1u, 23u },
1829 { 33u, 1207623u }
1830 });
1831
1832 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001833 unsigned int periodicCounterCapturePacketSize = 28;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001834 totalWrittenSize += periodicCounterCapturePacketSize;
1835
Sadik Armagan3896b472020-02-10 12:24:15 +00001836 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001837
Colm Donelan2ba48d22019-11-29 09:10:59 +00001838 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001839
1840 sendCounterPacket.SendPeriodicCounterCapturePacket(44u,
1841 {
1842 { 211u, 923u }
1843 });
1844
1845 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001846 periodicCounterCapturePacketSize = 22;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001847 totalWrittenSize += periodicCounterCapturePacketSize;
1848
1849 sendCounterPacket.SendPeriodicCounterCapturePacket(1234u,
1850 {
1851 { 555u, 23u },
1852 { 556u, 6u },
1853 { 557u, 893454u },
1854 { 558u, 1456623u },
1855 { 559u, 571090u }
1856 });
1857
1858 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001859 periodicCounterCapturePacketSize = 46;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001860 totalWrittenSize += periodicCounterCapturePacketSize;
1861
1862 sendCounterPacket.SendPeriodicCounterCapturePacket(997u,
1863 {
1864 { 88u, 11u },
1865 { 96u, 22u },
1866 { 97u, 33u },
1867 { 999u, 444u }
1868 });
1869
1870 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001871 periodicCounterCapturePacketSize = 40;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001872 totalWrittenSize += periodicCounterCapturePacketSize;
1873
Sadik Armagan3896b472020-02-10 12:24:15 +00001874 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001875
Colm Donelan2ba48d22019-11-29 09:10:59 +00001876 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001877
1878 sendCounterPacket.SendPeriodicCounterSelectionPacket(1000u, { 1345u, 254u, 4536u, 408u, 54u, 6323u, 428u, 1u, 6u });
1879
1880 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001881 periodicCounterCapturePacketSize = 30;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001882 totalWrittenSize += periodicCounterCapturePacketSize;
1883
Sadik Armagan3896b472020-02-10 12:24:15 +00001884 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001885
Finn Williams109c05b2019-11-29 13:56:33 +00001886 // To test an exact value of the "read size" in the mock buffer, wait to allow the send thread to
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001887 // read all what's remaining in the buffer
Colm Donelan2ba48d22019-11-29 09:10:59 +00001888 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001889
Sadik Armagan3896b472020-02-10 12:24:15 +00001890 sendThread.Stop();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001891
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001892 BOOST_CHECK(mockStreamCounterBuffer.GetCommittedSize() == totalWrittenSize);
Matteo Martincigh61d6f732019-10-03 11:21:18 +01001893 BOOST_CHECK(mockStreamCounterBuffer.GetReadableSize() == totalWrittenSize);
1894 BOOST_CHECK(mockStreamCounterBuffer.GetReadSize() == totalWrittenSize);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001895}
1896
1897BOOST_AUTO_TEST_CASE(SendThreadTest2)
1898{
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01001899 ProfilingStateMachine profilingStateMachine;
1900 SetActiveProfilingState(profilingStateMachine);
1901
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001902 unsigned int totalWrittenSize = 0;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001903
1904 MockProfilingConnection mockProfilingConnection;
Matteo Martincigh61d6f732019-10-03 11:21:18 +01001905 MockStreamCounterBuffer mockStreamCounterBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00001906 SendCounterPacket sendCounterPacket(mockStreamCounterBuffer);
1907 SendThread sendThread(profilingStateMachine, mockStreamCounterBuffer, sendCounterPacket);
1908 sendThread.Start(mockProfilingConnection);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001909
1910 // Adding many spurious "ready to read" signals throughout the test to check that the send thread is
1911 // capable of handling unnecessary read requests
1912
Colm Donelan2ba48d22019-11-29 09:10:59 +00001913 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001914
Sadik Armagan3896b472020-02-10 12:24:15 +00001915 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001916
1917 CounterDirectory counterDirectory;
1918 sendCounterPacket.SendStreamMetaDataPacket();
1919
Finn Williamsa0de0562020-04-22 12:27:37 +01001920 totalWrittenSize += GetStreamMetaDataPacketSize();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001921
Sadik Armagan3896b472020-02-10 12:24:15 +00001922 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001923
Colm Donelan2ba48d22019-11-29 09:10:59 +00001924 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001925
1926 sendCounterPacket.SendCounterDirectoryPacket(counterDirectory);
1927
1928 // Get the size of the Counter Directory Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001929 unsigned int counterDirectoryPacketSize = 32;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001930 totalWrittenSize += counterDirectoryPacketSize;
1931
Sadik Armagan3896b472020-02-10 12:24:15 +00001932 sendThread.SetReadyToRead();
1933 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001934
Colm Donelan2ba48d22019-11-29 09:10:59 +00001935 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001936
1937 sendCounterPacket.SendPeriodicCounterCapturePacket(123u,
1938 {
1939 { 1u, 23u },
1940 { 33u, 1207623u }
1941 });
1942
1943 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001944 unsigned int periodicCounterCapturePacketSize = 28;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001945 totalWrittenSize += periodicCounterCapturePacketSize;
1946
Sadik Armagan3896b472020-02-10 12:24:15 +00001947 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001948
Colm Donelan2ba48d22019-11-29 09:10:59 +00001949 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001950
Sadik Armagan3896b472020-02-10 12:24:15 +00001951 sendThread.SetReadyToRead();
1952 sendThread.SetReadyToRead();
1953 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001954
Colm Donelan2ba48d22019-11-29 09:10:59 +00001955 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001956
Sadik Armagan3896b472020-02-10 12:24:15 +00001957 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001958 sendCounterPacket.SendPeriodicCounterCapturePacket(44u,
1959 {
1960 { 211u, 923u }
1961 });
1962
1963 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001964 periodicCounterCapturePacketSize = 22;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001965 totalWrittenSize += periodicCounterCapturePacketSize;
1966
1967 sendCounterPacket.SendPeriodicCounterCapturePacket(1234u,
1968 {
1969 { 555u, 23u },
1970 { 556u, 6u },
1971 { 557u, 893454u },
1972 { 558u, 1456623u },
1973 { 559u, 571090u }
1974 });
1975
1976 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001977 periodicCounterCapturePacketSize = 46;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001978 totalWrittenSize += periodicCounterCapturePacketSize;
1979
Sadik Armagan3896b472020-02-10 12:24:15 +00001980 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001981 sendCounterPacket.SendPeriodicCounterCapturePacket(997u,
1982 {
1983 { 88u, 11u },
1984 { 96u, 22u },
1985 { 97u, 33u },
1986 { 999u, 444u }
1987 });
1988
1989 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001990 periodicCounterCapturePacketSize = 40;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001991 totalWrittenSize += periodicCounterCapturePacketSize;
1992
Sadik Armagan3896b472020-02-10 12:24:15 +00001993 sendThread.SetReadyToRead();
1994 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001995
Colm Donelan2ba48d22019-11-29 09:10:59 +00001996 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001997
1998 sendCounterPacket.SendPeriodicCounterSelectionPacket(1000u, { 1345u, 254u, 4536u, 408u, 54u, 6323u, 428u, 1u, 6u });
1999
2000 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002001 periodicCounterCapturePacketSize = 30;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002002 totalWrittenSize += periodicCounterCapturePacketSize;
2003
Sadik Armagan3896b472020-02-10 12:24:15 +00002004 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002005
Finn Williams109c05b2019-11-29 13:56:33 +00002006 // To test an exact value of the "read size" in the mock buffer, wait to allow the send thread to
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002007 // read all what's remaining in the buffer
Sadik Armagan3896b472020-02-10 12:24:15 +00002008 sendThread.Stop();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002009
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002010 BOOST_CHECK(mockStreamCounterBuffer.GetCommittedSize() == totalWrittenSize);
Matteo Martincigh61d6f732019-10-03 11:21:18 +01002011 BOOST_CHECK(mockStreamCounterBuffer.GetReadableSize() == totalWrittenSize);
2012 BOOST_CHECK(mockStreamCounterBuffer.GetReadSize() == totalWrittenSize);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002013}
2014
2015BOOST_AUTO_TEST_CASE(SendThreadTest3)
2016{
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002017 ProfilingStateMachine profilingStateMachine;
2018 SetActiveProfilingState(profilingStateMachine);
2019
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002020 unsigned int totalWrittenSize = 0;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002021
2022 MockProfilingConnection mockProfilingConnection;
Matteo Martincigh61d6f732019-10-03 11:21:18 +01002023 MockStreamCounterBuffer mockStreamCounterBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002024 SendCounterPacket sendCounterPacket(mockStreamCounterBuffer);
2025 SendThread sendThread(profilingStateMachine, mockStreamCounterBuffer, sendCounterPacket);
2026 sendThread.Start(mockProfilingConnection);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002027
2028 // Not using pauses or "grace periods" to stress test the send thread
2029
Sadik Armagan3896b472020-02-10 12:24:15 +00002030 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002031
2032 CounterDirectory counterDirectory;
2033 sendCounterPacket.SendStreamMetaDataPacket();
2034
Finn Williamsa0de0562020-04-22 12:27:37 +01002035 totalWrittenSize += GetStreamMetaDataPacketSize();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002036
Sadik Armagan3896b472020-02-10 12:24:15 +00002037 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002038 sendCounterPacket.SendCounterDirectoryPacket(counterDirectory);
2039
2040 // Get the size of the Counter Directory Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002041 unsigned int counterDirectoryPacketSize =32;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002042 totalWrittenSize += counterDirectoryPacketSize;
2043
Sadik Armagan3896b472020-02-10 12:24:15 +00002044 sendThread.SetReadyToRead();
2045 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002046 sendCounterPacket.SendPeriodicCounterCapturePacket(123u,
2047 {
2048 { 1u, 23u },
2049 { 33u, 1207623u }
2050 });
2051
2052 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002053 unsigned int periodicCounterCapturePacketSize = 28;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002054 totalWrittenSize += periodicCounterCapturePacketSize;
2055
Sadik Armagan3896b472020-02-10 12:24:15 +00002056 sendThread.SetReadyToRead();
2057 sendThread.SetReadyToRead();
2058 sendThread.SetReadyToRead();
2059 sendThread.SetReadyToRead();
2060 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002061 sendCounterPacket.SendPeriodicCounterCapturePacket(44u,
2062 {
2063 { 211u, 923u }
2064 });
2065
2066 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002067 periodicCounterCapturePacketSize = 22;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002068 totalWrittenSize += periodicCounterCapturePacketSize;
2069
2070 sendCounterPacket.SendPeriodicCounterCapturePacket(1234u,
2071 {
2072 { 555u, 23u },
2073 { 556u, 6u },
2074 { 557u, 893454u },
2075 { 558u, 1456623u },
2076 { 559u, 571090u }
2077 });
2078
2079 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002080 periodicCounterCapturePacketSize = 46;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002081 totalWrittenSize += periodicCounterCapturePacketSize;
2082
Sadik Armagan3896b472020-02-10 12:24:15 +00002083 sendThread.SetReadyToRead();
2084 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002085 sendCounterPacket.SendPeriodicCounterCapturePacket(997u,
2086 {
2087 { 88u, 11u },
2088 { 96u, 22u },
2089 { 97u, 33u },
2090 { 999u, 444u }
2091 });
2092
2093 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002094 periodicCounterCapturePacketSize = 40;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002095 totalWrittenSize += periodicCounterCapturePacketSize;
2096
Sadik Armagan3896b472020-02-10 12:24:15 +00002097 sendThread.SetReadyToRead();
2098 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002099 sendCounterPacket.SendPeriodicCounterSelectionPacket(1000u, { 1345u, 254u, 4536u, 408u, 54u, 6323u, 428u, 1u, 6u });
2100
2101 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002102 periodicCounterCapturePacketSize = 30;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002103 totalWrittenSize += periodicCounterCapturePacketSize;
2104
Sadik Armagan3896b472020-02-10 12:24:15 +00002105 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002106
2107 // Abruptly terminating the send thread, the amount of data sent may be less that the amount written (the send
2108 // thread is not guaranteed to flush the buffer)
Sadik Armagan3896b472020-02-10 12:24:15 +00002109 sendThread.Stop();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002110
Matteo Martincigh61d6f732019-10-03 11:21:18 +01002111 BOOST_CHECK(mockStreamCounterBuffer.GetCommittedSize() == totalWrittenSize);
2112 BOOST_CHECK(mockStreamCounterBuffer.GetReadableSize() <= totalWrittenSize);
2113 BOOST_CHECK(mockStreamCounterBuffer.GetReadSize() <= totalWrittenSize);
2114 BOOST_CHECK(mockStreamCounterBuffer.GetReadSize() <= mockStreamCounterBuffer.GetReadableSize());
2115 BOOST_CHECK(mockStreamCounterBuffer.GetReadSize() <= mockStreamCounterBuffer.GetCommittedSize());
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002116}
2117
Sadik Armagan3896b472020-02-10 12:24:15 +00002118BOOST_AUTO_TEST_CASE(SendCounterPacketTestWithSendThread)
2119{
2120 ProfilingStateMachine profilingStateMachine;
2121 SetWaitingForAckProfilingState(profilingStateMachine);
2122
2123 MockProfilingConnection mockProfilingConnection;
2124 BufferManager bufferManager(1, 1024);
2125 SendCounterPacket sendCounterPacket(bufferManager);
2126 SendThread sendThread(profilingStateMachine, bufferManager, sendCounterPacket, -1);
2127 sendThread.Start(mockProfilingConnection);
2128
Finn Williamsa0de0562020-04-22 12:27:37 +01002129 unsigned int streamMetadataPacketsize = GetStreamMetaDataPacketSize();
Sadik Armagan3896b472020-02-10 12:24:15 +00002130
2131 sendThread.Stop();
2132
2133 // check for packet in ProfilingConnection
2134 BOOST_CHECK(mockProfilingConnection.CheckForPacket({PacketType::StreamMetaData, streamMetadataPacketsize}) == 1);
2135
2136 SetActiveProfilingState(profilingStateMachine);
2137 sendThread.Start(mockProfilingConnection);
2138
2139 // SendCounterDirectoryPacket
2140 CounterDirectory counterDirectory;
2141 sendCounterPacket.SendCounterDirectoryPacket(counterDirectory);
2142
2143 sendThread.Stop();
2144 unsigned int counterDirectoryPacketSize = 32;
2145 // check for packet in ProfilingConnection
2146 BOOST_CHECK(mockProfilingConnection.CheckForPacket(
2147 {PacketType::CounterDirectory, counterDirectoryPacketSize}) == 1);
2148
2149 sendThread.Start(mockProfilingConnection);
2150
2151 // SendPeriodicCounterCapturePacket
2152 sendCounterPacket.SendPeriodicCounterCapturePacket(123u,
2153 {
2154 { 1u, 23u },
2155 { 33u, 1207623u }
2156 });
2157
2158 sendThread.Stop();
2159
2160 unsigned int periodicCounterCapturePacketSize = 28;
2161 BOOST_CHECK(mockProfilingConnection.CheckForPacket(
2162 {PacketType::PeriodicCounterCapture, periodicCounterCapturePacketSize}) == 1);
2163}
2164
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002165BOOST_AUTO_TEST_CASE(SendThreadBufferTest)
2166{
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002167 ProfilingStateMachine profilingStateMachine;
2168 SetActiveProfilingState(profilingStateMachine);
2169
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002170 MockProfilingConnection mockProfilingConnection;
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002171 BufferManager bufferManager(3, 1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002172 SendCounterPacket sendCounterPacket(bufferManager);
2173 SendThread sendThread(profilingStateMachine, bufferManager, sendCounterPacket, -1);
2174 sendThread.Start(mockProfilingConnection);
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002175
2176 // SendStreamMetaDataPacket
2177 sendCounterPacket.SendStreamMetaDataPacket();
2178
2179 // Read data from the buffer
2180 // Buffer should become readable after commit by SendStreamMetaDataPacket
2181 auto packetBuffer = bufferManager.GetReadableBuffer();
2182 BOOST_TEST(packetBuffer.get());
2183
Finn Williamsa0de0562020-04-22 12:27:37 +01002184 unsigned int streamMetadataPacketsize = GetStreamMetaDataPacketSize();
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002185 BOOST_TEST(packetBuffer->GetSize() == streamMetadataPacketsize);
2186
2187 // Recommit to be read by sendCounterPacket
2188 bufferManager.Commit(packetBuffer, streamMetadataPacketsize);
2189
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002190 // SendCounterDirectoryPacket
2191 CounterDirectory counterDirectory;
2192 sendCounterPacket.SendCounterDirectoryPacket(counterDirectory);
2193
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002194 // SendPeriodicCounterCapturePacket
2195 sendCounterPacket.SendPeriodicCounterCapturePacket(123u,
2196 {
2197 { 1u, 23u },
2198 { 33u, 1207623u }
2199 });
2200
Sadik Armagan3896b472020-02-10 12:24:15 +00002201 sendThread.Stop();
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002202
2203 // The buffer is read by the send thread so it should not be in the readable buffer.
2204 auto readBuffer = bufferManager.GetReadableBuffer();
2205 BOOST_TEST(!readBuffer);
2206
2207 // Successfully reserved the buffer with requested size
2208 unsigned int reservedSize = 0;
2209 auto reservedBuffer = bufferManager.Reserve(512, reservedSize);
2210 BOOST_TEST(reservedSize == 512);
2211 BOOST_TEST(reservedBuffer.get());
2212
Finn Williams09ad6f92019-12-19 17:05:18 +00002213 const auto writtenDataSize = mockProfilingConnection.GetWrittenDataSize();
2214 const auto metaDataPacketCount =
2215 mockProfilingConnection.CheckForPacket({PacketType::StreamMetaData, streamMetadataPacketsize});
2216
2217 BOOST_TEST(metaDataPacketCount >= 1);
2218 BOOST_TEST(mockProfilingConnection.CheckForPacket({PacketType::CounterDirectory, 32}) == 1);
2219 BOOST_TEST(mockProfilingConnection.CheckForPacket({PacketType::PeriodicCounterCapture, 28}) == 1);
2220 // Check that we only received the packets we expected
2221 BOOST_TEST(metaDataPacketCount + 2 == writtenDataSize);
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002222}
2223
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002224BOOST_AUTO_TEST_CASE(SendThreadSendStreamMetadataPacket1)
2225{
2226 ProfilingStateMachine profilingStateMachine;
2227
2228 MockProfilingConnection mockProfilingConnection;
2229 BufferManager bufferManager(3, 1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002230 SendCounterPacket sendCounterPacket(bufferManager);
2231 SendThread sendThread(profilingStateMachine, bufferManager, sendCounterPacket);
2232 sendThread.Start(mockProfilingConnection);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002233
2234 // The profiling state is set to "Uninitialized", so the send thread should throw an exception
Sadik Armagan3896b472020-02-10 12:24:15 +00002235 BOOST_CHECK_THROW(sendThread.Stop(), armnn::RuntimeException);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002236}
2237
2238BOOST_AUTO_TEST_CASE(SendThreadSendStreamMetadataPacket2)
2239{
2240 ProfilingStateMachine profilingStateMachine;
2241 SetNotConnectedProfilingState(profilingStateMachine);
2242
2243 MockProfilingConnection mockProfilingConnection;
2244 BufferManager bufferManager(3, 1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002245 SendCounterPacket sendCounterPacket(bufferManager);
2246 SendThread sendThread(profilingStateMachine, bufferManager, sendCounterPacket);
2247 sendThread.Start(mockProfilingConnection);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002248
2249 // The profiling state is set to "NotConnected", so the send thread should throw an exception
Sadik Armagan3896b472020-02-10 12:24:15 +00002250 BOOST_CHECK_THROW(sendThread.Stop(), armnn::RuntimeException);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002251}
2252
2253BOOST_AUTO_TEST_CASE(SendThreadSendStreamMetadataPacket3)
2254{
2255 ProfilingStateMachine profilingStateMachine;
2256 SetWaitingForAckProfilingState(profilingStateMachine);
2257
Finn Williamsa0de0562020-04-22 12:27:37 +01002258 unsigned int streamMetadataPacketsize = GetStreamMetaDataPacketSize();
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002259
2260 MockProfilingConnection mockProfilingConnection;
2261 BufferManager bufferManager(3, 1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002262 SendCounterPacket sendCounterPacket(bufferManager);
2263 SendThread sendThread(profilingStateMachine, bufferManager, sendCounterPacket);
2264 sendThread.Start(mockProfilingConnection);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002265
2266 // The profiling state is set to "WaitingForAck", so the send thread should send a Stream Metadata packet
Sadik Armagan3896b472020-02-10 12:24:15 +00002267 // Wait for sendThread to join
2268 BOOST_CHECK_NO_THROW(sendThread.Stop());
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002269
Finn Williams09ad6f92019-12-19 17:05:18 +00002270 // Check that the buffer contains at least one Stream Metadata packet and no other packets
2271 const auto writtenDataSize = mockProfilingConnection.GetWrittenDataSize();
2272
2273 BOOST_TEST(writtenDataSize >= 1);
2274 BOOST_TEST(mockProfilingConnection.CheckForPacket(
2275 {PacketType::StreamMetaData, streamMetadataPacketsize}) == writtenDataSize);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002276}
2277
2278BOOST_AUTO_TEST_CASE(SendThreadSendStreamMetadataPacket4)
2279{
2280 ProfilingStateMachine profilingStateMachine;
2281 SetWaitingForAckProfilingState(profilingStateMachine);
2282
Finn Williamsa0de0562020-04-22 12:27:37 +01002283 unsigned int streamMetadataPacketsize = GetStreamMetaDataPacketSize();
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002284
2285 MockProfilingConnection mockProfilingConnection;
2286 BufferManager bufferManager(3, 1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002287 SendCounterPacket sendCounterPacket(bufferManager);
2288 SendThread sendThread(profilingStateMachine, bufferManager, sendCounterPacket);
2289 sendThread.Start(mockProfilingConnection);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002290
2291 // The profiling state is set to "WaitingForAck", so the send thread should send a Stream Metadata packet
Sadik Armagan3896b472020-02-10 12:24:15 +00002292 // Wait for sendThread to join
2293 sendThread.Stop();
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002294
Sadik Armagan3896b472020-02-10 12:24:15 +00002295 sendThread.Start(mockProfilingConnection);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002296 // Check that the profiling state is still "WaitingForAck"
2297 BOOST_TEST((profilingStateMachine.GetCurrentState() == ProfilingState::WaitingForAck));
2298
Finn Williams3e2969d2019-12-06 17:47:36 +00002299 // Check that the buffer contains at least one Stream Metadata packet
Finn Williams09ad6f92019-12-19 17:05:18 +00002300 BOOST_TEST(mockProfilingConnection.CheckForPacket({PacketType::StreamMetaData, streamMetadataPacketsize}) >= 1);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002301
2302 mockProfilingConnection.Clear();
2303
Sadik Armagan3896b472020-02-10 12:24:15 +00002304 sendThread.Stop();
2305 sendThread.Start(mockProfilingConnection);
Finn Williams09ad6f92019-12-19 17:05:18 +00002306
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002307 // Try triggering a new buffer read
Sadik Armagan3896b472020-02-10 12:24:15 +00002308 sendThread.SetReadyToRead();
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002309
Sadik Armagan3896b472020-02-10 12:24:15 +00002310 // Wait for sendThread to join
2311 BOOST_CHECK_NO_THROW(sendThread.Stop());
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002312
2313 // Check that the profiling state is still "WaitingForAck"
2314 BOOST_TEST((profilingStateMachine.GetCurrentState() == ProfilingState::WaitingForAck));
2315
Finn Williams09ad6f92019-12-19 17:05:18 +00002316 // Check that the buffer contains at least one Stream Metadata packet and no other packets
2317 const auto writtenDataSize = mockProfilingConnection.GetWrittenDataSize();
2318
2319 BOOST_TEST(writtenDataSize >= 1);
2320 BOOST_TEST(mockProfilingConnection.CheckForPacket(
2321 {PacketType::StreamMetaData, streamMetadataPacketsize}) == writtenDataSize);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002322}
2323
Ferran Balagueraf5c46b2019-08-30 15:49:15 +01002324BOOST_AUTO_TEST_SUITE_END()