blob: 9ec24e539fd26f8ffec010cbee0b6e5d9f4c2e1b [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
Ferran Balaguer1b941722019-08-28 16:57:18 +01006#include "SendCounterPacketTests.hpp"
Ferran Balagueraf5c46b2019-08-30 15:49:15 +01007
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01008#include <BufferManager.hpp>
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +01009#include <CounterDirectory.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010010#include <EncodeVersion.hpp>
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +010011#include <ProfilingUtils.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010012#include <SendCounterPacket.hpp>
Rob Hughesbdee4262020-01-07 17:05:24 +000013#include <Processes.hpp>
Matteo Martincigh6db5f202019-09-05 12:02:04 +010014
Ferran Balaguer73882172019-09-02 16:39:42 +010015#include <armnn/Exceptions.hpp>
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +010016#include <armnn/Conversion.hpp>
Rob Hughes122f3252020-01-09 12:46:21 +000017#include <armnn/Utils.hpp>
Ferran Balaguer73882172019-09-02 16:39:42 +010018
Ferran Balagueraf5c46b2019-08-30 15:49:15 +010019#include <boost/test/unit_test.hpp>
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010020#include <boost/numeric/conversion/cast.hpp>
Ferran Balagueraf5c46b2019-08-30 15:49:15 +010021
Francis Murtagh3a161982019-09-04 15:25:02 +010022#include <chrono>
Ferran Balagueraf5c46b2019-08-30 15:49:15 +010023
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +010024using namespace armnn::profiling;
25
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010026namespace
27{
28
Colm Donelan2ba48d22019-11-29 09:10:59 +000029// A short delay to wait for the thread to process a packet.
Finn Williams09ad6f92019-12-19 17:05:18 +000030uint16_t constexpr WAIT_UNTIL_READABLE_MS = 20;
Colm Donelan2ba48d22019-11-29 09:10:59 +000031
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010032void SetNotConnectedProfilingState(ProfilingStateMachine& profilingStateMachine)
33{
34 ProfilingState currentState = profilingStateMachine.GetCurrentState();
35 switch (currentState)
36 {
37 case ProfilingState::WaitingForAck:
38 profilingStateMachine.TransitionToState(ProfilingState::Active);
Rob Hughes122f3252020-01-09 12:46:21 +000039 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010040 case ProfilingState::Uninitialised:
Rob Hughes122f3252020-01-09 12:46:21 +000041 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010042 case ProfilingState::Active:
43 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
Rob Hughes122f3252020-01-09 12:46:21 +000044 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010045 case ProfilingState::NotConnected:
46 return;
47 default:
48 BOOST_CHECK_MESSAGE(false, "Invalid profiling state");
49 }
50}
51
52void SetWaitingForAckProfilingState(ProfilingStateMachine& profilingStateMachine)
53{
54 ProfilingState currentState = profilingStateMachine.GetCurrentState();
55 switch (currentState)
56 {
57 case ProfilingState::Uninitialised:
Rob Hughes122f3252020-01-09 12:46:21 +000058 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010059 case ProfilingState::Active:
60 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
Rob Hughes122f3252020-01-09 12:46:21 +000061 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010062 case ProfilingState::NotConnected:
63 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
Rob Hughes122f3252020-01-09 12:46:21 +000064 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010065 case ProfilingState::WaitingForAck:
66 return;
67 default:
68 BOOST_CHECK_MESSAGE(false, "Invalid profiling state");
69 }
70}
71
72void SetActiveProfilingState(ProfilingStateMachine& profilingStateMachine)
73{
74 ProfilingState currentState = profilingStateMachine.GetCurrentState();
75 switch (currentState)
76 {
77 case ProfilingState::Uninitialised:
78 profilingStateMachine.TransitionToState(ProfilingState::NotConnected);
Rob Hughes122f3252020-01-09 12:46:21 +000079 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010080 case ProfilingState::NotConnected:
81 profilingStateMachine.TransitionToState(ProfilingState::WaitingForAck);
Rob Hughes122f3252020-01-09 12:46:21 +000082 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010083 case ProfilingState::WaitingForAck:
84 profilingStateMachine.TransitionToState(ProfilingState::Active);
Rob Hughes122f3252020-01-09 12:46:21 +000085 ARMNN_FALLTHROUGH;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010086 case ProfilingState::Active:
87 return;
88 default:
89 BOOST_CHECK_MESSAGE(false, "Invalid profiling state");
90 }
91}
92
93} // Anonymous namespace
94
Ferran Balagueraf5c46b2019-08-30 15:49:15 +010095BOOST_AUTO_TEST_SUITE(SendCounterPacketTests)
96
Finn Williams09ad6f92019-12-19 17:05:18 +000097using PacketType = MockProfilingConnection::PacketType;
98
Ferran Balagueraf5c46b2019-08-30 15:49:15 +010099BOOST_AUTO_TEST_CASE(MockSendCounterPacketTest)
100{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100101 MockBufferManager mockBuffer(512);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +0100102 MockSendCounterPacket mockSendCounterPacket(mockBuffer);
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100103
Matteo Martincigh5d737fb2019-10-07 13:05:13 +0100104 mockSendCounterPacket.SendStreamMetaDataPacket();
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100105
106 auto packetBuffer = mockBuffer.GetReadableBuffer();
107 const char* buffer = reinterpret_cast<const char*>(packetBuffer->GetReadableData());
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100108
109 BOOST_TEST(strcmp(buffer, "SendStreamMetaDataPacket") == 0);
110
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100111 mockBuffer.MarkRead(packetBuffer);
112
Matteo Martincigh6db5f202019-09-05 12:02:04 +0100113 CounterDirectory counterDirectory;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +0100114 mockSendCounterPacket.SendCounterDirectoryPacket(counterDirectory);
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100115
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100116 packetBuffer = mockBuffer.GetReadableBuffer();
117 buffer = reinterpret_cast<const char*>(packetBuffer->GetReadableData());
118
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100119 BOOST_TEST(strcmp(buffer, "SendCounterDirectoryPacket") == 0);
120
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100121 mockBuffer.MarkRead(packetBuffer);
122
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100123 uint64_t timestamp = 0;
Francis Murtagh3a161982019-09-04 15:25:02 +0100124 std::vector<std::pair<uint16_t, uint32_t>> indexValuePairs;
125
Matteo Martincigh5d737fb2019-10-07 13:05:13 +0100126 mockSendCounterPacket.SendPeriodicCounterCapturePacket(timestamp, indexValuePairs);
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100127
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100128 packetBuffer = mockBuffer.GetReadableBuffer();
129 buffer = reinterpret_cast<const char*>(packetBuffer->GetReadableData());
130
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100131 BOOST_TEST(strcmp(buffer, "SendPeriodicCounterCapturePacket") == 0);
132
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100133 mockBuffer.MarkRead(packetBuffer);
134
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100135 uint32_t capturePeriod = 0;
136 std::vector<uint16_t> selectedCounterIds;
Matteo Martincigh5d737fb2019-10-07 13:05:13 +0100137 mockSendCounterPacket.SendPeriodicCounterSelectionPacket(capturePeriod, selectedCounterIds);
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100138
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100139 packetBuffer = mockBuffer.GetReadableBuffer();
140 buffer = reinterpret_cast<const char*>(packetBuffer->GetReadableData());
141
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100142 BOOST_TEST(strcmp(buffer, "SendPeriodicCounterSelectionPacket") == 0);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100143
144 mockBuffer.MarkRead(packetBuffer);
Ferran Balagueraf5c46b2019-08-30 15:49:15 +0100145}
146
Ferran Balaguer73882172019-09-02 16:39:42 +0100147BOOST_AUTO_TEST_CASE(SendPeriodicCounterSelectionPacketTest)
148{
149 // Error no space left in buffer
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100150 MockBufferManager mockBuffer1(10);
Sadik Armagan3896b472020-02-10 12:24:15 +0000151 SendCounterPacket sendPacket1(mockBuffer1);
Ferran Balaguer73882172019-09-02 16:39:42 +0100152
153 uint32_t capturePeriod = 1000;
154 std::vector<uint16_t> selectedCounterIds;
155 BOOST_CHECK_THROW(sendPacket1.SendPeriodicCounterSelectionPacket(capturePeriod, selectedCounterIds),
Matteo Martincigh24e8f922019-09-19 11:57:46 +0100156 BufferExhaustion);
Ferran Balaguer73882172019-09-02 16:39:42 +0100157
158 // Packet without any counters
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100159 MockBufferManager mockBuffer2(512);
Sadik Armagan3896b472020-02-10 12:24:15 +0000160 SendCounterPacket sendPacket2(mockBuffer2);
Ferran Balaguer73882172019-09-02 16:39:42 +0100161
162 sendPacket2.SendPeriodicCounterSelectionPacket(capturePeriod, selectedCounterIds);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100163 auto readBuffer2 = mockBuffer2.GetReadableBuffer();
Ferran Balaguer73882172019-09-02 16:39:42 +0100164
165 uint32_t headerWord0 = ReadUint32(readBuffer2, 0);
166 uint32_t headerWord1 = ReadUint32(readBuffer2, 4);
167 uint32_t period = ReadUint32(readBuffer2, 8);
168
169 BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family
170 BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 4); // packet id
171 BOOST_TEST(headerWord1 == 4); // data lenght
172 BOOST_TEST(period == 1000); // capture period
173
174 // Full packet message
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100175 MockBufferManager mockBuffer3(512);
Sadik Armagan3896b472020-02-10 12:24:15 +0000176 SendCounterPacket sendPacket3(mockBuffer3);
Ferran Balaguer73882172019-09-02 16:39:42 +0100177
178 selectedCounterIds.reserve(5);
179 selectedCounterIds.emplace_back(100);
180 selectedCounterIds.emplace_back(200);
181 selectedCounterIds.emplace_back(300);
182 selectedCounterIds.emplace_back(400);
183 selectedCounterIds.emplace_back(500);
184 sendPacket3.SendPeriodicCounterSelectionPacket(capturePeriod, selectedCounterIds);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100185 auto readBuffer3 = mockBuffer3.GetReadableBuffer();
Ferran Balaguer73882172019-09-02 16:39:42 +0100186
187 headerWord0 = ReadUint32(readBuffer3, 0);
188 headerWord1 = ReadUint32(readBuffer3, 4);
189 period = ReadUint32(readBuffer3, 8);
190
191 BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family
192 BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 4); // packet id
193 BOOST_TEST(headerWord1 == 14); // data lenght
194 BOOST_TEST(period == 1000); // capture period
195
196 uint16_t counterId = 0;
197 uint32_t offset = 12;
198
199 // Counter Ids
200 for(const uint16_t& id : selectedCounterIds)
201 {
202 counterId = ReadUint16(readBuffer3, offset);
203 BOOST_TEST(counterId == id);
204 offset += 2;
205 }
206}
207
Francis Murtagh3a161982019-09-04 15:25:02 +0100208BOOST_AUTO_TEST_CASE(SendPeriodicCounterCapturePacketTest)
209{
Matteo Martincigh5d737fb2019-10-07 13:05:13 +0100210 ProfilingStateMachine profilingStateMachine;
211
Francis Murtagh3a161982019-09-04 15:25:02 +0100212 // Error no space left in buffer
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100213 MockBufferManager mockBuffer1(10);
Sadik Armagan3896b472020-02-10 12:24:15 +0000214 SendCounterPacket sendPacket1(mockBuffer1);
Francis Murtagh3a161982019-09-04 15:25:02 +0100215
216 auto captureTimestamp = std::chrono::steady_clock::now();
217 uint64_t time = static_cast<uint64_t >(captureTimestamp.time_since_epoch().count());
218 std::vector<std::pair<uint16_t, uint32_t>> indexValuePairs;
219
220 BOOST_CHECK_THROW(sendPacket1.SendPeriodicCounterCapturePacket(time, indexValuePairs),
221 BufferExhaustion);
222
223 // Packet without any counters
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100224 MockBufferManager mockBuffer2(512);
Sadik Armagan3896b472020-02-10 12:24:15 +0000225 SendCounterPacket sendPacket2(mockBuffer2);
Francis Murtagh3a161982019-09-04 15:25:02 +0100226
227 sendPacket2.SendPeriodicCounterCapturePacket(time, indexValuePairs);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100228 auto readBuffer2 = mockBuffer2.GetReadableBuffer();
Francis Murtagh3a161982019-09-04 15:25:02 +0100229
230 uint32_t headerWord0 = ReadUint32(readBuffer2, 0);
231 uint32_t headerWord1 = ReadUint32(readBuffer2, 4);
232 uint64_t readTimestamp = ReadUint64(readBuffer2, 8);
233
Jim Flynnfc365622019-12-04 10:07:20 +0000234 BOOST_TEST(((headerWord0 >> 26) & 0x0000003F) == 3); // packet family
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100235 BOOST_TEST(((headerWord0 >> 19) & 0x0000007F) == 0); // packet class
236 BOOST_TEST(((headerWord0 >> 16) & 0x00000007) == 0); // packet type
237 BOOST_TEST(headerWord1 == 8); // data length
238 BOOST_TEST(time == readTimestamp); // capture period
Francis Murtagh3a161982019-09-04 15:25:02 +0100239
240 // Full packet message
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100241 MockBufferManager mockBuffer3(512);
Sadik Armagan3896b472020-02-10 12:24:15 +0000242 SendCounterPacket sendPacket3(mockBuffer3);
Francis Murtagh3a161982019-09-04 15:25:02 +0100243
244 indexValuePairs.reserve(5);
245 indexValuePairs.emplace_back(std::make_pair<uint16_t, uint32_t >(0, 100));
246 indexValuePairs.emplace_back(std::make_pair<uint16_t, uint32_t >(1, 200));
247 indexValuePairs.emplace_back(std::make_pair<uint16_t, uint32_t >(2, 300));
248 indexValuePairs.emplace_back(std::make_pair<uint16_t, uint32_t >(3, 400));
249 indexValuePairs.emplace_back(std::make_pair<uint16_t, uint32_t >(4, 500));
250 sendPacket3.SendPeriodicCounterCapturePacket(time, indexValuePairs);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100251 auto readBuffer3 = mockBuffer3.GetReadableBuffer();
Francis Murtagh3a161982019-09-04 15:25:02 +0100252
253 headerWord0 = ReadUint32(readBuffer3, 0);
254 headerWord1 = ReadUint32(readBuffer3, 4);
255 uint64_t readTimestamp2 = ReadUint64(readBuffer3, 8);
256
Jim Flynnfc365622019-12-04 10:07:20 +0000257 BOOST_TEST(((headerWord0 >> 26) & 0x0000003F) == 3); // packet family
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100258 BOOST_TEST(((headerWord0 >> 19) & 0x0000007F) == 0); // packet class
259 BOOST_TEST(((headerWord0 >> 16) & 0x00000007) == 0); // packet type
260 BOOST_TEST(headerWord1 == 38); // data length
261 BOOST_TEST(time == readTimestamp2); // capture period
Francis Murtagh3a161982019-09-04 15:25:02 +0100262
263 uint16_t counterIndex = 0;
264 uint32_t counterValue = 100;
265 uint32_t offset = 16;
266
267 // Counter Ids
268 for (auto it = indexValuePairs.begin(), end = indexValuePairs.end(); it != end; ++it)
269 {
270 // Check Counter Index
271 uint16_t readIndex = ReadUint16(readBuffer3, offset);
272 BOOST_TEST(counterIndex == readIndex);
273 counterIndex++;
274 offset += 2;
275
276 // Check Counter Value
277 uint32_t readValue = ReadUint32(readBuffer3, offset);
278 BOOST_TEST(counterValue == readValue);
279 counterValue += 100;
280 offset += 4;
281 }
282
283}
284
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100285BOOST_AUTO_TEST_CASE(SendStreamMetaDataPacketTest)
286{
287 using boost::numeric_cast;
288
289 uint32_t sizeUint32 = numeric_cast<uint32_t>(sizeof(uint32_t));
290
291 // Error no space left in buffer
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100292 MockBufferManager mockBuffer1(10);
Sadik Armagan3896b472020-02-10 12:24:15 +0000293 SendCounterPacket sendPacket1(mockBuffer1);
Matteo Martincigh149528e2019-09-05 12:02:04 +0100294 BOOST_CHECK_THROW(sendPacket1.SendStreamMetaDataPacket(), armnn::profiling::BufferExhaustion);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100295
296 // Full metadata packet
297
298 std::string processName = GetProcessName().substr(0, 60);
299
300 uint32_t infoSize = numeric_cast<uint32_t>(GetSoftwareInfo().size()) > 0 ?
301 numeric_cast<uint32_t>(GetSoftwareInfo().size()) + 1 : 0;
302 uint32_t hardwareVersionSize = numeric_cast<uint32_t>(GetHardwareVersion().size()) > 0 ?
303 numeric_cast<uint32_t>(GetHardwareVersion().size()) + 1 : 0;
304 uint32_t softwareVersionSize = numeric_cast<uint32_t>(GetSoftwareVersion().size()) > 0 ?
305 numeric_cast<uint32_t>(GetSoftwareVersion().size()) + 1 : 0;
306 uint32_t processNameSize = numeric_cast<uint32_t>(processName.size()) > 0 ?
307 numeric_cast<uint32_t>(processName.size()) + 1 : 0;
308
Ferran Balaguer5bf1d322019-09-13 13:31:40 +0100309 uint32_t packetEntries = 6;
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100310
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100311 MockBufferManager mockBuffer2(512);
Sadik Armagan3896b472020-02-10 12:24:15 +0000312 SendCounterPacket sendPacket2(mockBuffer2);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100313 sendPacket2.SendStreamMetaDataPacket();
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100314 auto readBuffer2 = mockBuffer2.GetReadableBuffer();
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100315
316 uint32_t headerWord0 = ReadUint32(readBuffer2, 0);
317 uint32_t headerWord1 = ReadUint32(readBuffer2, sizeUint32);
318
319 BOOST_TEST(((headerWord0 >> 26) & 0x3F) == 0); // packet family
320 BOOST_TEST(((headerWord0 >> 16) & 0x3FF) == 0); // packet id
321
322 uint32_t totalLength = numeric_cast<uint32_t>(2 * sizeUint32 + 10 * sizeUint32 + infoSize + hardwareVersionSize +
323 softwareVersionSize + processNameSize + sizeUint32 +
324 2 * packetEntries * sizeUint32);
325
326 BOOST_TEST(headerWord1 == totalLength - (2 * sizeUint32)); // data length
327
328 uint32_t offset = sizeUint32 * 2;
329 BOOST_TEST(ReadUint32(readBuffer2, offset) == SendCounterPacket::PIPE_MAGIC); // pipe_magic
330 offset += sizeUint32;
331 BOOST_TEST(ReadUint32(readBuffer2, offset) == EncodeVersion(1, 0, 0)); // stream_metadata_version
332 offset += sizeUint32;
Sadik Armagan7bbdf9d2019-10-24 10:26:05 +0100333 BOOST_TEST(ReadUint32(readBuffer2, offset) == MAX_METADATA_PACKET_LENGTH); // max_data_len
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100334 offset += sizeUint32;
Rob Hughesbdee4262020-01-07 17:05:24 +0000335 int pid = armnnUtils::Processes::GetCurrentId();
336 BOOST_TEST(ReadUint32(readBuffer2, offset) == numeric_cast<uint32_t>(pid));
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100337 offset += sizeUint32;
338 uint32_t poolOffset = 10 * sizeUint32;
339 BOOST_TEST(ReadUint32(readBuffer2, offset) == (infoSize ? poolOffset : 0)); // offset_info
340 offset += sizeUint32;
341 poolOffset += infoSize;
342 BOOST_TEST(ReadUint32(readBuffer2, offset) == (hardwareVersionSize ? poolOffset : 0)); // offset_hw_version
343 offset += sizeUint32;
344 poolOffset += hardwareVersionSize;
345 BOOST_TEST(ReadUint32(readBuffer2, offset) == (softwareVersionSize ? poolOffset : 0)); // offset_sw_version
346 offset += sizeUint32;
347 poolOffset += softwareVersionSize;
348 BOOST_TEST(ReadUint32(readBuffer2, offset) == (processNameSize ? poolOffset : 0)); // offset_process_name
349 offset += sizeUint32;
350 poolOffset += processNameSize;
351 BOOST_TEST(ReadUint32(readBuffer2, offset) == (packetEntries ? poolOffset : 0)); // offset_packet_version_table
352 offset += sizeUint32;
353 BOOST_TEST(ReadUint32(readBuffer2, offset) == 0); // reserved
354
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100355 const unsigned char* readData2 = readBuffer2->GetReadableData();
356
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100357 offset += sizeUint32;
358 if (infoSize)
359 {
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100360 BOOST_TEST(strcmp(reinterpret_cast<const char *>(&readData2[offset]), GetSoftwareInfo().c_str()) == 0);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100361 offset += infoSize;
362 }
363
364 if (hardwareVersionSize)
365 {
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100366 BOOST_TEST(strcmp(reinterpret_cast<const char *>(&readData2[offset]), GetHardwareVersion().c_str()) == 0);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100367 offset += hardwareVersionSize;
368 }
369
370 if (softwareVersionSize)
371 {
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100372 BOOST_TEST(strcmp(reinterpret_cast<const char *>(&readData2[offset]), GetSoftwareVersion().c_str()) == 0);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100373 offset += softwareVersionSize;
374 }
375
376 if (processNameSize)
377 {
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100378 BOOST_TEST(strcmp(reinterpret_cast<const char *>(&readData2[offset]), GetProcessName().c_str()) == 0);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100379 offset += processNameSize;
380 }
381
382 if (packetEntries)
383 {
384 BOOST_TEST((ReadUint32(readBuffer2, offset) >> 16) == packetEntries);
385 offset += sizeUint32;
Ferran Balaguer5bf1d322019-09-13 13:31:40 +0100386 for (uint32_t i = 0; i < packetEntries - 1; ++i)
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100387 {
388 BOOST_TEST(((ReadUint32(readBuffer2, offset) >> 26) & 0x3F) == 0);
389 BOOST_TEST(((ReadUint32(readBuffer2, offset) >> 16) & 0x3FF) == i);
390 offset += sizeUint32;
391 BOOST_TEST(ReadUint32(readBuffer2, offset) == EncodeVersion(1, 0, 0));
392 offset += sizeUint32;
393 }
Ferran Balaguer5bf1d322019-09-13 13:31:40 +0100394
395 BOOST_TEST(((ReadUint32(readBuffer2, offset) >> 26) & 0x3F) == 1);
396 BOOST_TEST(((ReadUint32(readBuffer2, offset) >> 16) & 0x3FF) == 0);
397 offset += sizeUint32;
398 BOOST_TEST(ReadUint32(readBuffer2, offset) == EncodeVersion(1, 0, 0));
399 offset += sizeUint32;
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100400 }
401
402 BOOST_TEST(offset == totalLength);
403}
404
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100405BOOST_AUTO_TEST_CASE(CreateDeviceRecordTest)
406{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100407 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000408 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100409
410 // Create a device for testing
411 uint16_t deviceUid = 27;
412 const std::string deviceName = "some_device";
413 uint16_t deviceCores = 3;
414 const DevicePtr device = std::make_unique<Device>(deviceUid, deviceName, deviceCores);
415
416 // Create a device record
417 SendCounterPacket::DeviceRecord deviceRecord;
418 std::string errorMessage;
419 bool result = sendCounterPacketTest.CreateDeviceRecordTest(device, deviceRecord, errorMessage);
420
421 BOOST_CHECK(result);
422 BOOST_CHECK(errorMessage.empty());
423 BOOST_CHECK(deviceRecord.size() == 6); // Size in words: header [2] + device name [4]
424
425 uint16_t deviceRecordWord0[]
426 {
427 static_cast<uint16_t>(deviceRecord[0] >> 16),
428 static_cast<uint16_t>(deviceRecord[0])
429 };
430 BOOST_CHECK(deviceRecordWord0[0] == deviceUid); // uid
431 BOOST_CHECK(deviceRecordWord0[1] == deviceCores); // cores
432 BOOST_CHECK(deviceRecord[1] == 0); // name_offset
433 BOOST_CHECK(deviceRecord[2] == deviceName.size() + 1); // The length of the SWTrace string (name)
434 BOOST_CHECK(std::memcmp(deviceRecord.data() + 3, deviceName.data(), deviceName.size()) == 0); // name
435}
436
437BOOST_AUTO_TEST_CASE(CreateInvalidDeviceRecordTest)
438{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100439 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000440 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100441
442 // Create a device for testing
443 uint16_t deviceUid = 27;
444 const std::string deviceName = "some€£invalid‡device";
445 uint16_t deviceCores = 3;
446 const DevicePtr device = std::make_unique<Device>(deviceUid, deviceName, deviceCores);
447
448 // Create a device record
449 SendCounterPacket::DeviceRecord deviceRecord;
450 std::string errorMessage;
451 bool result = sendCounterPacketTest.CreateDeviceRecordTest(device, deviceRecord, errorMessage);
452
453 BOOST_CHECK(!result);
454 BOOST_CHECK(!errorMessage.empty());
455 BOOST_CHECK(deviceRecord.empty());
456}
457
458BOOST_AUTO_TEST_CASE(CreateCounterSetRecordTest)
459{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100460 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000461 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100462
463 // Create a counter set for testing
464 uint16_t counterSetUid = 27;
465 const std::string counterSetName = "some_counter_set";
466 uint16_t counterSetCount = 3421;
467 const CounterSetPtr counterSet = std::make_unique<CounterSet>(counterSetUid, counterSetName, counterSetCount);
468
469 // Create a counter set record
470 SendCounterPacket::CounterSetRecord counterSetRecord;
471 std::string errorMessage;
472 bool result = sendCounterPacketTest.CreateCounterSetRecordTest(counterSet, counterSetRecord, errorMessage);
473
474 BOOST_CHECK(result);
475 BOOST_CHECK(errorMessage.empty());
476 BOOST_CHECK(counterSetRecord.size() == 8); // Size in words: header [2] + counter set name [6]
477
478 uint16_t counterSetRecordWord0[]
479 {
480 static_cast<uint16_t>(counterSetRecord[0] >> 16),
481 static_cast<uint16_t>(counterSetRecord[0])
482 };
483 BOOST_CHECK(counterSetRecordWord0[0] == counterSetUid); // uid
484 BOOST_CHECK(counterSetRecordWord0[1] == counterSetCount); // cores
485 BOOST_CHECK(counterSetRecord[1] == 0); // name_offset
486 BOOST_CHECK(counterSetRecord[2] == counterSetName.size() + 1); // The length of the SWTrace string (name)
487 BOOST_CHECK(std::memcmp(counterSetRecord.data() + 3, counterSetName.data(), counterSetName.size()) == 0); // name
488}
489
490BOOST_AUTO_TEST_CASE(CreateInvalidCounterSetRecordTest)
491{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100492 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000493 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100494
495 // Create a counter set for testing
496 uint16_t counterSetUid = 27;
497 const std::string counterSetName = "some invalid_counter€£set";
498 uint16_t counterSetCount = 3421;
499 const CounterSetPtr counterSet = std::make_unique<CounterSet>(counterSetUid, counterSetName, counterSetCount);
500
501 // Create a counter set record
502 SendCounterPacket::CounterSetRecord counterSetRecord;
503 std::string errorMessage;
504 bool result = sendCounterPacketTest.CreateCounterSetRecordTest(counterSet, counterSetRecord, errorMessage);
505
506 BOOST_CHECK(!result);
507 BOOST_CHECK(!errorMessage.empty());
508 BOOST_CHECK(counterSetRecord.empty());
509}
510
511BOOST_AUTO_TEST_CASE(CreateEventRecordTest)
512{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100513 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000514 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100515
516 // Create a counter for testing
517 uint16_t counterUid = 7256;
518 uint16_t maxCounterUid = 132;
519 uint16_t deviceUid = 132;
520 uint16_t counterSetUid = 4497;
521 uint16_t counterClass = 1;
522 uint16_t counterInterpolation = 1;
523 double counterMultiplier = 1234.567f;
524 const std::string counterName = "some_valid_counter";
525 const std::string counterDescription = "a_counter_for_testing";
526 const std::string counterUnits = "Mrads2";
Keith Davise394bd92019-12-02 15:12:19 +0000527 const CounterPtr counter = std::make_unique<Counter>(armnn::profiling::BACKEND_ID,
528 counterUid,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100529 maxCounterUid,
530 counterClass,
531 counterInterpolation,
532 counterMultiplier,
533 counterName,
534 counterDescription,
535 counterUnits,
536 deviceUid,
537 counterSetUid);
538 BOOST_ASSERT(counter);
539
540 // Create an event record
541 SendCounterPacket::EventRecord eventRecord;
542 std::string errorMessage;
543 bool result = sendCounterPacketTest.CreateEventRecordTest(counter, eventRecord, errorMessage);
544
545 BOOST_CHECK(result);
546 BOOST_CHECK(errorMessage.empty());
547 BOOST_CHECK(eventRecord.size() == 24); // Size in words: header [8] + counter name [6] + description [7] + units [3]
548
549 uint16_t eventRecordWord0[]
550 {
551 static_cast<uint16_t>(eventRecord[0] >> 16),
552 static_cast<uint16_t>(eventRecord[0])
553 };
554 uint16_t eventRecordWord1[]
555 {
556 static_cast<uint16_t>(eventRecord[1] >> 16),
557 static_cast<uint16_t>(eventRecord[1])
558 };
559 uint16_t eventRecordWord2[]
560 {
561 static_cast<uint16_t>(eventRecord[2] >> 16),
562 static_cast<uint16_t>(eventRecord[2])
563 };
564 uint32_t eventRecordWord34[]
565 {
566 eventRecord[3],
567 eventRecord[4]
568 };
569 BOOST_CHECK(eventRecordWord0[0] == maxCounterUid); // max_counter_uid
570 BOOST_CHECK(eventRecordWord0[1] == counterUid); // counter_uid
571 BOOST_CHECK(eventRecordWord1[0] == deviceUid); // device
572 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
578 uint32_t counterNameOffset = 0; // The name is the first item in pool
579 uint32_t counterDescriptionOffset = counterNameOffset + // Counter name offset
580 4u + // Counter name length (uint32_t)
581 counterName.size() + // 18u
582 1u + // Null-terminator
583 1u; // Rounding to the next word
584 size_t counterUnitsOffset = counterDescriptionOffset + // Counter description offset
585 4u + // Counter description length (uint32_t)
586 counterDescription.size() + // 21u
587 1u + // Null-terminator
588 2u; // Rounding to the next word
589 ARMNN_NO_CONVERSION_WARN_END
590
591 BOOST_CHECK(eventRecord[5] == counterNameOffset); // name_offset
592 BOOST_CHECK(eventRecord[6] == counterDescriptionOffset); // description_offset
593 BOOST_CHECK(eventRecord[7] == counterUnitsOffset); // units_offset
594
595 auto eventRecordPool = reinterpret_cast<unsigned char*>(eventRecord.data() + 8u); // The start of the pool
596 size_t uint32_t_size = sizeof(uint32_t);
597
598 // The length of the SWTrace string (name)
599 BOOST_CHECK(eventRecordPool[counterNameOffset] == counterName.size() + 1);
600 // The counter name
601 BOOST_CHECK(std::memcmp(eventRecordPool +
602 counterNameOffset + // Offset
603 uint32_t_size /* The length of the name */,
604 counterName.data(),
605 counterName.size()) == 0); // name
606 // The null-terminator at the end of the name
607 BOOST_CHECK(eventRecordPool[counterNameOffset + uint32_t_size + counterName.size()] == '\0');
608
609 // The length of the SWTrace string (description)
610 BOOST_CHECK(eventRecordPool[counterDescriptionOffset] == counterDescription.size() + 1);
611 // The counter description
612 BOOST_CHECK(std::memcmp(eventRecordPool +
613 counterDescriptionOffset + // Offset
614 uint32_t_size /* The length of the description */,
615 counterDescription.data(),
616 counterDescription.size()) == 0); // description
617 // The null-terminator at the end of the description
618 BOOST_CHECK(eventRecordPool[counterDescriptionOffset + uint32_t_size + counterDescription.size()] == '\0');
619
620 // The length of the SWTrace namestring (units)
621 BOOST_CHECK(eventRecordPool[counterUnitsOffset] == counterUnits.size() + 1);
622 // The counter units
623 BOOST_CHECK(std::memcmp(eventRecordPool +
624 counterUnitsOffset + // Offset
625 uint32_t_size /* The length of the units */,
626 counterUnits.data(),
627 counterUnits.size()) == 0); // units
628 // The null-terminator at the end of the units
629 BOOST_CHECK(eventRecordPool[counterUnitsOffset + uint32_t_size + counterUnits.size()] == '\0');
630}
631
632BOOST_AUTO_TEST_CASE(CreateEventRecordNoUnitsTest)
633{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100634 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000635 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100636
637 // Create a counter for testing
638 uint16_t counterUid = 44312;
639 uint16_t maxCounterUid = 345;
640 uint16_t deviceUid = 101;
641 uint16_t counterSetUid = 34035;
642 uint16_t counterClass = 0;
643 uint16_t counterInterpolation = 1;
644 double counterMultiplier = 4435.0023f;
645 const std::string counterName = "some_valid_counter";
646 const std::string counterDescription = "a_counter_for_testing";
Keith Davise394bd92019-12-02 15:12:19 +0000647 const CounterPtr counter = std::make_unique<Counter>(armnn::profiling::BACKEND_ID,
648 counterUid,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100649 maxCounterUid,
650 counterClass,
651 counterInterpolation,
652 counterMultiplier,
653 counterName,
654 counterDescription,
655 "",
656 deviceUid,
657 counterSetUid);
658 BOOST_ASSERT(counter);
659
660 // Create an event record
661 SendCounterPacket::EventRecord eventRecord;
662 std::string errorMessage;
663 bool result = sendCounterPacketTest.CreateEventRecordTest(counter, eventRecord, errorMessage);
664
665 BOOST_CHECK(result);
666 BOOST_CHECK(errorMessage.empty());
667 BOOST_CHECK(eventRecord.size() == 21); // Size in words: header [8] + counter name [6] + description [7]
668
669 uint16_t eventRecordWord0[]
670 {
671 static_cast<uint16_t>(eventRecord[0] >> 16),
672 static_cast<uint16_t>(eventRecord[0])
673 };
674 uint16_t eventRecordWord1[]
675 {
676 static_cast<uint16_t>(eventRecord[1] >> 16),
677 static_cast<uint16_t>(eventRecord[1])
678 };
679 uint16_t eventRecordWord2[]
680 {
681 static_cast<uint16_t>(eventRecord[2] >> 16),
682 static_cast<uint16_t>(eventRecord[2])
683 };
684 uint32_t eventRecordWord34[]
685 {
686 eventRecord[3],
687 eventRecord[4]
688 };
689 BOOST_CHECK(eventRecordWord0[0] == maxCounterUid); // max_counter_uid
690 BOOST_CHECK(eventRecordWord0[1] == counterUid); // counter_uid
691 BOOST_CHECK(eventRecordWord1[0] == deviceUid); // device
692 BOOST_CHECK(eventRecordWord1[1] == counterSetUid); // counter_set
693 BOOST_CHECK(eventRecordWord2[0] == counterClass); // class
694 BOOST_CHECK(eventRecordWord2[1] == counterInterpolation); // interpolation
695 BOOST_CHECK(std::memcmp(eventRecordWord34, &counterMultiplier, sizeof(counterMultiplier)) == 0); // multiplier
696
697 ARMNN_NO_CONVERSION_WARN_BEGIN
698 uint32_t counterNameOffset = 0; // The name is the first item in pool
699 uint32_t counterDescriptionOffset = counterNameOffset + // Counter name offset
700 4u + // Counter name length (uint32_t)
701 counterName.size() + // 18u
702 1u + // Null-terminator
703 1u; // Rounding to the next word
704 ARMNN_NO_CONVERSION_WARN_END
705
706 BOOST_CHECK(eventRecord[5] == counterNameOffset); // name_offset
707 BOOST_CHECK(eventRecord[6] == counterDescriptionOffset); // description_offset
708 BOOST_CHECK(eventRecord[7] == 0); // units_offset
709
710 auto eventRecordPool = reinterpret_cast<unsigned char*>(eventRecord.data() + 8u); // The start of the pool
711 size_t uint32_t_size = sizeof(uint32_t);
712
713 // The length of the SWTrace string (name)
714 BOOST_CHECK(eventRecordPool[counterNameOffset] == counterName.size() + 1);
715 // The counter name
716 BOOST_CHECK(std::memcmp(eventRecordPool +
717 counterNameOffset + // Offset
718 uint32_t_size, // The length of the name
719 counterName.data(),
720 counterName.size()) == 0); // name
721 // The null-terminator at the end of the name
722 BOOST_CHECK(eventRecordPool[counterNameOffset + uint32_t_size + counterName.size()] == '\0');
723
724 // The length of the SWTrace string (description)
725 BOOST_CHECK(eventRecordPool[counterDescriptionOffset] == counterDescription.size() + 1);
726 // The counter description
727 BOOST_CHECK(std::memcmp(eventRecordPool +
728 counterDescriptionOffset + // Offset
729 uint32_t_size, // The length of the description
730 counterDescription.data(),
731 counterDescription.size()) == 0); // description
732 // The null-terminator at the end of the description
733 BOOST_CHECK(eventRecordPool[counterDescriptionOffset + uint32_t_size + counterDescription.size()] == '\0');
734}
735
736BOOST_AUTO_TEST_CASE(CreateInvalidEventRecordTest1)
737{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100738 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000739 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100740
741 // Create a counter for testing
742 uint16_t counterUid = 7256;
743 uint16_t maxCounterUid = 132;
744 uint16_t deviceUid = 132;
745 uint16_t counterSetUid = 4497;
746 uint16_t counterClass = 1;
747 uint16_t counterInterpolation = 1;
748 double counterMultiplier = 1234.567f;
749 const std::string counterName = "some_invalid_counter £££"; // Invalid name
750 const std::string counterDescription = "a_counter_for_testing";
751 const std::string counterUnits = "Mrads2";
Keith Davise394bd92019-12-02 15:12:19 +0000752 const CounterPtr counter = std::make_unique<Counter>(armnn::profiling::BACKEND_ID,
753 counterUid,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100754 maxCounterUid,
755 counterClass,
756 counterInterpolation,
757 counterMultiplier,
758 counterName,
759 counterDescription,
760 counterUnits,
761 deviceUid,
762 counterSetUid);
763 BOOST_ASSERT(counter);
764
765 // Create an event record
766 SendCounterPacket::EventRecord eventRecord;
767 std::string errorMessage;
768 bool result = sendCounterPacketTest.CreateEventRecordTest(counter, eventRecord, errorMessage);
769
770 BOOST_CHECK(!result);
771 BOOST_CHECK(!errorMessage.empty());
772 BOOST_CHECK(eventRecord.empty());
773}
774
775BOOST_AUTO_TEST_CASE(CreateInvalidEventRecordTest2)
776{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100777 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000778 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100779
780 // Create a counter for testing
781 uint16_t counterUid = 7256;
782 uint16_t maxCounterUid = 132;
783 uint16_t deviceUid = 132;
784 uint16_t counterSetUid = 4497;
785 uint16_t counterClass = 1;
786 uint16_t counterInterpolation = 1;
787 double counterMultiplier = 1234.567f;
788 const std::string counterName = "some_invalid_counter";
789 const std::string counterDescription = "an invalid d€scription"; // Invalid description
790 const std::string counterUnits = "Mrads2";
Keith Davise394bd92019-12-02 15:12:19 +0000791 const CounterPtr counter = std::make_unique<Counter>(armnn::profiling::BACKEND_ID,
792 counterUid,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100793 maxCounterUid,
794 counterClass,
795 counterInterpolation,
796 counterMultiplier,
797 counterName,
798 counterDescription,
799 counterUnits,
800 deviceUid,
801 counterSetUid);
802 BOOST_ASSERT(counter);
803
804 // Create an event record
805 SendCounterPacket::EventRecord eventRecord;
806 std::string errorMessage;
807 bool result = sendCounterPacketTest.CreateEventRecordTest(counter, eventRecord, errorMessage);
808
809 BOOST_CHECK(!result);
810 BOOST_CHECK(!errorMessage.empty());
811 BOOST_CHECK(eventRecord.empty());
812}
813
814BOOST_AUTO_TEST_CASE(CreateInvalidEventRecordTest3)
815{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100816 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000817 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100818
819 // Create a counter for testing
820 uint16_t counterUid = 7256;
821 uint16_t maxCounterUid = 132;
822 uint16_t deviceUid = 132;
823 uint16_t counterSetUid = 4497;
824 uint16_t counterClass = 1;
825 uint16_t counterInterpolation = 1;
826 double counterMultiplier = 1234.567f;
827 const std::string counterName = "some_invalid_counter";
828 const std::string counterDescription = "a valid description";
829 const std::string counterUnits = "Mrad s2"; // Invalid units
Keith Davise394bd92019-12-02 15:12:19 +0000830 const CounterPtr counter = std::make_unique<Counter>(armnn::profiling::BACKEND_ID,
831 counterUid,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100832 maxCounterUid,
833 counterClass,
834 counterInterpolation,
835 counterMultiplier,
836 counterName,
837 counterDescription,
838 counterUnits,
839 deviceUid,
840 counterSetUid);
841 BOOST_ASSERT(counter);
842
843 // Create an event record
844 SendCounterPacket::EventRecord eventRecord;
845 std::string errorMessage;
846 bool result = sendCounterPacketTest.CreateEventRecordTest(counter, eventRecord, errorMessage);
847
848 BOOST_CHECK(!result);
849 BOOST_CHECK(!errorMessage.empty());
850 BOOST_CHECK(eventRecord.empty());
851}
852
853BOOST_AUTO_TEST_CASE(CreateCategoryRecordTest)
854{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100855 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +0000856 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100857
858 // Create a category for testing
859 const std::string categoryName = "some_category";
860 uint16_t deviceUid = 1302;
861 uint16_t counterSetUid = 20734;
862 const CategoryPtr category = std::make_unique<Category>(categoryName, deviceUid, counterSetUid);
863 BOOST_ASSERT(category);
864 category->m_Counters = { 11u, 23u, 5670u };
865
866 // Create a collection of counters
867 Counters counters;
868 counters.insert(std::make_pair<uint16_t, CounterPtr>(11,
Keith Davise394bd92019-12-02 15:12:19 +0000869 CounterPtr(new Counter(armnn::profiling::BACKEND_ID,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100870 0,
Keith Davise394bd92019-12-02 15:12:19 +0000871 11,
872 0,
873 0,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100874 534.0003f,
875 "counter1",
876 "the first counter",
877 "millipi2",
878 0,
879 0))));
880 counters.insert(std::make_pair<uint16_t, CounterPtr>(23,
Keith Davise394bd92019-12-02 15:12:19 +0000881 CounterPtr(new Counter(armnn::profiling::BACKEND_ID,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100882 1,
Keith Davise394bd92019-12-02 15:12:19 +0000883 23,
884 0,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100885 1,
886 534.0003f,
887 "this is counter 2",
888 "the second counter",
889 "",
890 0,
891 0))));
892 counters.insert(std::make_pair<uint16_t, CounterPtr>(5670,
Keith Davise394bd92019-12-02 15:12:19 +0000893 CounterPtr(new Counter(armnn::profiling::BACKEND_ID,
894 2,
895 5670,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100896 0,
897 0,
898 534.0003f,
899 "and this is number 3",
900 "the third counter",
901 "blah_per_second",
902 0,
903 0))));
904 Counter* counter1 = counters.find(11)->second.get();
905 Counter* counter2 = counters.find(23)->second.get();
906 Counter* counter3 = counters.find(5670)->second.get();
907 BOOST_ASSERT(counter1);
908 BOOST_ASSERT(counter2);
909 BOOST_ASSERT(counter3);
910 uint16_t categoryEventCount = boost::numeric_cast<uint16_t>(counters.size());
911
912 // Create a category record
913 SendCounterPacket::CategoryRecord categoryRecord;
914 std::string errorMessage;
915 bool result = sendCounterPacketTest.CreateCategoryRecordTest(category, counters, categoryRecord, errorMessage);
916
917 BOOST_CHECK(result);
918 BOOST_CHECK(errorMessage.empty());
919 BOOST_CHECK(categoryRecord.size() == 80); // Size in words: header [4] + event pointer table [3] +
920 // category name [5] + event records [68 = 22 + 20 + 26]
921
922 uint16_t categoryRecordWord0[]
923 {
924 static_cast<uint16_t>(categoryRecord[0] >> 16),
925 static_cast<uint16_t>(categoryRecord[0])
926 };
927 uint16_t categoryRecordWord1[]
928 {
929 static_cast<uint16_t>(categoryRecord[1] >> 16),
930 static_cast<uint16_t>(categoryRecord[1])
931 };
932 BOOST_CHECK(categoryRecordWord0[0] == deviceUid); // device
933 BOOST_CHECK(categoryRecordWord0[1] == counterSetUid); // counter_set
934 BOOST_CHECK(categoryRecordWord1[0] == categoryEventCount); // event_count
935 BOOST_CHECK(categoryRecordWord1[1] == 0); // reserved
936
937 size_t uint32_t_size = sizeof(uint32_t);
938
939 ARMNN_NO_CONVERSION_WARN_BEGIN
940 uint32_t eventPointerTableOffset = 0; // The event pointer table is the first item in pool
941 uint32_t categoryNameOffset = eventPointerTableOffset + // Event pointer table offset
942 categoryEventCount * uint32_t_size; // The size of the event pointer table
943 ARMNN_NO_CONVERSION_WARN_END
944
945 BOOST_CHECK(categoryRecord[2] == eventPointerTableOffset); // event_pointer_table_offset
946 BOOST_CHECK(categoryRecord[3] == categoryNameOffset); // name_offset
947
948 auto categoryRecordPool = reinterpret_cast<unsigned char*>(categoryRecord.data() + 4u); // The start of the pool
949
950 // The event pointer table
951 uint32_t eventRecord0Offset = categoryRecordPool[eventPointerTableOffset + 0 * uint32_t_size];
952 uint32_t eventRecord1Offset = categoryRecordPool[eventPointerTableOffset + 1 * uint32_t_size];
953 uint32_t eventRecord2Offset = categoryRecordPool[eventPointerTableOffset + 2 * uint32_t_size];
954 BOOST_CHECK(eventRecord0Offset == 32);
955 BOOST_CHECK(eventRecord1Offset == 120);
956 BOOST_CHECK(eventRecord2Offset == 200);
957
958 // The length of the SWTrace namestring (name)
959 BOOST_CHECK(categoryRecordPool[categoryNameOffset] == categoryName.size() + 1);
960 // The category name
961 BOOST_CHECK(std::memcmp(categoryRecordPool +
962 categoryNameOffset + // Offset
963 uint32_t_size, // The length of the name
964 categoryName.data(),
965 categoryName.size()) == 0); // name
966 // The null-terminator at the end of the name
967 BOOST_CHECK(categoryRecordPool[categoryNameOffset + uint32_t_size + categoryName.size()] == '\0');
968
969 // For brevity, checking only the UIDs, max counter UIDs and names of the counters in the event records,
970 // as the event records already have a number of unit tests dedicated to them
971
972 // Counter1 UID and max counter UID
973 uint16_t eventRecord0Word0[2] = { 0u, 0u };
974 std::memcpy(eventRecord0Word0, categoryRecordPool + eventRecord0Offset, sizeof(eventRecord0Word0));
975 BOOST_CHECK(eventRecord0Word0[0] == counter1->m_Uid);
976 BOOST_CHECK(eventRecord0Word0[1] == counter1->m_MaxCounterUid);
977
978 // Counter1 name
979 uint32_t counter1NameOffset = 0;
980 std::memcpy(&counter1NameOffset, categoryRecordPool + eventRecord0Offset + 5u * uint32_t_size, uint32_t_size);
981 BOOST_CHECK(counter1NameOffset == 0);
982 // The length of the SWTrace string (name)
983 BOOST_CHECK(categoryRecordPool[eventRecord0Offset + // Offset to the event record
984 8u * uint32_t_size + // Offset to the event record pool
985 counter1NameOffset // Offset to the name of the counter
986 ] == counter1->m_Name.size() + 1); // The length of the name including the
987 // null-terminator
988 // The counter1 name
989 BOOST_CHECK(std::memcmp(categoryRecordPool + // The beginning of the category pool
990 eventRecord0Offset + // Offset to the event record
991 8u * uint32_t_size + // Offset to the event record pool
992 counter1NameOffset + // Offset to the name of the counter
993 uint32_t_size, // The length of the name
994 counter1->m_Name.data(),
995 counter1->m_Name.size()) == 0); // name
996 // The null-terminator at the end of the counter1 name
997 BOOST_CHECK(categoryRecordPool[eventRecord0Offset + // Offset to the event record
998 8u * uint32_t_size + // Offset to the event record pool
999 counter1NameOffset + // Offset to the name of the counter
1000 uint32_t_size + // The length of the name
1001 counter1->m_Name.size() // The name of the counter
1002 ] == '\0');
1003
1004 // Counter2 name
1005 uint32_t counter2NameOffset = 0;
1006 std::memcpy(&counter2NameOffset, categoryRecordPool + eventRecord1Offset + 5u * uint32_t_size, uint32_t_size);
1007 BOOST_CHECK(counter2NameOffset == 0);
1008 // The length of the SWTrace string (name)
1009 BOOST_CHECK(categoryRecordPool[eventRecord1Offset + // Offset to the event record
1010 8u * uint32_t_size + // Offset to the event record pool
1011 counter2NameOffset // Offset to the name of the counter
1012 ] == counter2->m_Name.size() + 1); // The length of the name including the
1013 // null-terminator
1014 // The counter2 name
1015 BOOST_CHECK(std::memcmp(categoryRecordPool + // The beginning of the category pool
1016 eventRecord1Offset + // Offset to the event record
1017 8u * uint32_t_size + // Offset to the event record pool
1018 counter2NameOffset + // Offset to the name of the counter
1019 uint32_t_size, // The length of the name
1020 counter2->m_Name.data(),
1021 counter2->m_Name.size()) == 0); // name
1022 // The null-terminator at the end of the counter2 name
1023 BOOST_CHECK(categoryRecordPool[eventRecord1Offset + // Offset to the event record
1024 8u * uint32_t_size + // Offset to the event record pool
1025 counter2NameOffset + // Offset to the name of the counter
1026 uint32_t_size + // The length of the name
1027 counter2->m_Name.size() // The name of the counter
1028 ] == '\0');
1029
1030 // Counter3 name
1031 uint32_t counter3NameOffset = 0;
1032 std::memcpy(&counter3NameOffset, categoryRecordPool + eventRecord2Offset + 5u * uint32_t_size, uint32_t_size);
1033 BOOST_CHECK(counter3NameOffset == 0);
1034 // The length of the SWTrace string (name)
1035 BOOST_CHECK(categoryRecordPool[eventRecord2Offset + // Offset to the event record
1036 8u * uint32_t_size + // Offset to the event record pool
1037 counter3NameOffset // Offset to the name of the counter
1038 ] == counter3->m_Name.size() + 1); // The length of the name including the
1039 // null-terminator
1040 // The counter3 name
1041 BOOST_CHECK(std::memcmp(categoryRecordPool + // The beginning of the category pool
1042 eventRecord2Offset + // Offset to the event record
1043 8u * uint32_t_size + // Offset to the event record pool
1044 counter3NameOffset + // Offset to the name of the counter
1045 uint32_t_size, // The length of the name
1046 counter3->m_Name.data(),
1047 counter3->m_Name.size()) == 0); // name
1048 // The null-terminator at the end of the counter3 name
1049 BOOST_CHECK(categoryRecordPool[eventRecord2Offset + // Offset to the event record
1050 8u * uint32_t_size + // Offset to the event record pool
1051 counter3NameOffset + // Offset to the name of the counter
1052 uint32_t_size + // The length of the name
1053 counter3->m_Name.size() // The name of the counter
1054 ] == '\0');
1055}
1056
1057BOOST_AUTO_TEST_CASE(CreateInvalidCategoryRecordTest1)
1058{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001059 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +00001060 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001061
1062 // Create a category for testing
1063 const std::string categoryName = "some invalid category";
1064 uint16_t deviceUid = 1302;
1065 uint16_t counterSetUid = 20734;
1066 const CategoryPtr category = std::make_unique<Category>(categoryName, deviceUid, counterSetUid);
1067 BOOST_CHECK(category);
1068
1069 // Create a category record
1070 Counters counters;
1071 SendCounterPacket::CategoryRecord categoryRecord;
1072 std::string errorMessage;
1073 bool result = sendCounterPacketTest.CreateCategoryRecordTest(category, counters, categoryRecord, errorMessage);
1074
1075 BOOST_CHECK(!result);
1076 BOOST_CHECK(!errorMessage.empty());
1077 BOOST_CHECK(categoryRecord.empty());
1078}
1079
1080BOOST_AUTO_TEST_CASE(CreateInvalidCategoryRecordTest2)
1081{
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001082 MockBufferManager mockBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +00001083 SendCounterPacketTest sendCounterPacketTest(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001084
1085 // Create a category for testing
1086 const std::string categoryName = "some_category";
1087 uint16_t deviceUid = 1302;
1088 uint16_t counterSetUid = 20734;
1089 const CategoryPtr category = std::make_unique<Category>(categoryName, deviceUid, counterSetUid);
1090 BOOST_CHECK(category);
1091 category->m_Counters = { 11u, 23u, 5670u };
1092
1093 // Create a collection of counters
1094 Counters counters;
1095 counters.insert(std::make_pair<uint16_t, CounterPtr>(11,
Keith Davise394bd92019-12-02 15:12:19 +00001096 CounterPtr(new Counter(armnn::profiling::BACKEND_ID,
1097 11,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001098 1234,
1099 0,
1100 1,
1101 534.0003f,
1102 "count€r1", // Invalid name
1103 "the first counter",
1104 "millipi2",
1105 0,
1106 0))));
1107
1108 Counter* counter1 = counters.find(11)->second.get();
1109 BOOST_CHECK(counter1);
1110
1111 // Create a category record
1112 SendCounterPacket::CategoryRecord categoryRecord;
1113 std::string errorMessage;
1114 bool result = sendCounterPacketTest.CreateCategoryRecordTest(category, counters, categoryRecord, errorMessage);
1115
1116 BOOST_CHECK(!result);
1117 BOOST_CHECK(!errorMessage.empty());
1118 BOOST_CHECK(categoryRecord.empty());
1119}
1120
1121BOOST_AUTO_TEST_CASE(SendCounterDirectoryPacketTest1)
1122{
1123 // The counter directory used for testing
1124 CounterDirectory counterDirectory;
1125
1126 // Register a device
1127 const std::string device1Name = "device1";
1128 const Device* device1 = nullptr;
1129 BOOST_CHECK_NO_THROW(device1 = counterDirectory.RegisterDevice(device1Name, 3));
1130 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
1131 BOOST_CHECK(device1);
1132
1133 // Register a device
1134 const std::string device2Name = "device2";
1135 const Device* device2 = nullptr;
1136 BOOST_CHECK_NO_THROW(device2 = counterDirectory.RegisterDevice(device2Name));
1137 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
1138 BOOST_CHECK(device2);
1139
1140 // Buffer with not enough space
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001141 MockBufferManager mockBuffer(10);
Sadik Armagan3896b472020-02-10 12:24:15 +00001142 SendCounterPacket sendCounterPacket(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001143 BOOST_CHECK_THROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory),
1144 armnn::profiling::BufferExhaustion);
1145}
1146
1147BOOST_AUTO_TEST_CASE(SendCounterDirectoryPacketTest2)
1148{
1149 // The counter directory used for testing
1150 CounterDirectory counterDirectory;
1151
1152 // Register a device
1153 const std::string device1Name = "device1";
1154 const Device* device1 = nullptr;
1155 BOOST_CHECK_NO_THROW(device1 = counterDirectory.RegisterDevice(device1Name, 3));
1156 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
1157 BOOST_CHECK(device1);
1158
1159 // Register a device
1160 const std::string device2Name = "device2";
1161 const Device* device2 = nullptr;
1162 BOOST_CHECK_NO_THROW(device2 = counterDirectory.RegisterDevice(device2Name));
1163 BOOST_CHECK(counterDirectory.GetDeviceCount() == 2);
1164 BOOST_CHECK(device2);
1165
1166 // Register a counter set
1167 const std::string counterSet1Name = "counterset1";
1168 const CounterSet* counterSet1 = nullptr;
1169 BOOST_CHECK_NO_THROW(counterSet1 = counterDirectory.RegisterCounterSet(counterSet1Name));
1170 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1171 BOOST_CHECK(counterSet1);
1172
1173 // Register a category associated to "device1" and "counterset1"
1174 const std::string category1Name = "category1";
1175 const Category* category1 = nullptr;
1176 BOOST_CHECK_NO_THROW(category1 = counterDirectory.RegisterCategory(category1Name,
1177 device1->m_Uid,
1178 counterSet1->m_Uid));
1179 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
1180 BOOST_CHECK(category1);
1181
1182 // Register a category not associated to "device2" but no counter set
1183 const std::string category2Name = "category2";
1184 const Category* category2 = nullptr;
1185 BOOST_CHECK_NO_THROW(category2 = counterDirectory.RegisterCategory(category2Name,
1186 device2->m_Uid));
1187 BOOST_CHECK(counterDirectory.GetCategoryCount() == 2);
1188 BOOST_CHECK(category2);
1189
1190 // Register a counter associated to "category1"
1191 const Counter* counter1 = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001192 BOOST_CHECK_NO_THROW(counter1 = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1193 0,
1194 category1Name,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001195 0,
1196 1,
1197 123.45f,
1198 "counter1",
1199 "counter1description",
1200 std::string("counter1units")));
1201 BOOST_CHECK(counterDirectory.GetCounterCount() == 3);
1202 BOOST_CHECK(counter1);
1203
1204 // Register a counter associated to "category1"
1205 const Counter* counter2 = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001206 BOOST_CHECK_NO_THROW(counter2 = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1207 4,
1208 category1Name,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001209 1,
1210 0,
1211 330.1245656765f,
1212 "counter2",
1213 "counter2description",
1214 std::string("counter2units"),
1215 armnn::EmptyOptional(),
1216 device2->m_Uid,
1217 0));
1218 BOOST_CHECK(counterDirectory.GetCounterCount() == 4);
1219 BOOST_CHECK(counter2);
1220
1221 // Register a counter associated to "category2"
1222 const Counter* counter3 = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001223 BOOST_CHECK_NO_THROW(counter3 = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1224 5,
1225 category2Name,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001226 1,
1227 1,
1228 0.0000045399f,
1229 "counter3",
1230 "counter3description",
1231 armnn::EmptyOptional(),
1232 5,
1233 device2->m_Uid,
1234 counterSet1->m_Uid));
1235 BOOST_CHECK(counterDirectory.GetCounterCount() == 9);
1236 BOOST_CHECK(counter3);
1237
1238 // Buffer with enough space
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001239 MockBufferManager mockBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00001240 SendCounterPacket sendCounterPacket(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001241 BOOST_CHECK_NO_THROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory));
1242
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001243 // Get the readable buffer
1244 auto readBuffer = mockBuffer.GetReadableBuffer();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001245
1246 // Check the packet header
1247 uint32_t packetHeaderWord0 = ReadUint32(readBuffer, 0);
1248 uint32_t packetHeaderWord1 = ReadUint32(readBuffer, 4);
1249 BOOST_TEST(((packetHeaderWord0 >> 26) & 0x3F) == 0); // packet_family
1250 BOOST_TEST(((packetHeaderWord0 >> 16) & 0x3FF) == 2); // packet_id
Matteo Martincighf74ff2f2019-09-24 11:38:32 +01001251 BOOST_TEST(packetHeaderWord1 == 936); // data_length
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001252
1253 // Check the body header
1254 uint32_t bodyHeaderWord0 = ReadUint32(readBuffer, 8);
1255 uint32_t bodyHeaderWord1 = ReadUint32(readBuffer, 12);
1256 uint32_t bodyHeaderWord2 = ReadUint32(readBuffer, 16);
1257 uint32_t bodyHeaderWord3 = ReadUint32(readBuffer, 20);
1258 uint32_t bodyHeaderWord4 = ReadUint32(readBuffer, 24);
1259 uint32_t bodyHeaderWord5 = ReadUint32(readBuffer, 28);
1260 uint16_t deviceRecordCount = static_cast<uint16_t>(bodyHeaderWord0 >> 16);
1261 uint16_t counterSetRecordCount = static_cast<uint16_t>(bodyHeaderWord2 >> 16);
1262 uint16_t categoryRecordCount = static_cast<uint16_t>(bodyHeaderWord4 >> 16);
1263 BOOST_TEST(deviceRecordCount == 2); // device_records_count
1264 BOOST_TEST(bodyHeaderWord1 == 0); // device_records_pointer_table_offset
1265 BOOST_TEST(counterSetRecordCount == 1); // counter_set_count
1266 BOOST_TEST(bodyHeaderWord3 == 8); // counter_set_pointer_table_offset
1267 BOOST_TEST(categoryRecordCount == 2); // categories_count
1268 BOOST_TEST(bodyHeaderWord5 == 12); // categories_pointer_table_offset
1269
1270 // Check the device records pointer table
1271 uint32_t deviceRecordOffset0 = ReadUint32(readBuffer, 32);
1272 uint32_t deviceRecordOffset1 = ReadUint32(readBuffer, 36);
1273 BOOST_TEST(deviceRecordOffset0 == 0); // Device record offset for "device1"
1274 BOOST_TEST(deviceRecordOffset1 == 20); // Device record offset for "device2"
1275
1276 // Check the counter set pointer table
1277 uint32_t counterSetRecordOffset0 = ReadUint32(readBuffer, 40);
1278 BOOST_TEST(counterSetRecordOffset0 == 40); // Counter set record offset for "counterset1"
1279
1280 // Check the category pointer table
1281 uint32_t categoryRecordOffset0 = ReadUint32(readBuffer, 44);
1282 uint32_t categoryRecordOffset1 = ReadUint32(readBuffer, 48);
1283 BOOST_TEST(categoryRecordOffset0 == 64); // Category record offset for "category1"
1284 BOOST_TEST(categoryRecordOffset1 == 476); // Category record offset for "category2"
1285
1286 // Get the device record pool offset
1287 uint32_t uint32_t_size = sizeof(uint32_t);
1288 uint32_t packetBodyPoolOffset = 2u * uint32_t_size + // packet_header
1289 6u * uint32_t_size + // body_header
1290 deviceRecordCount * uint32_t_size + // Size of device_records_pointer_table
1291 counterSetRecordCount * uint32_t_size + // Size of counter_set_pointer_table
1292 categoryRecordCount * uint32_t_size; // Size of categories_pointer_table
1293
1294 // Device record structure/collection used for testing
1295 struct DeviceRecord
1296 {
1297 uint16_t uid;
1298 uint16_t cores;
1299 uint32_t name_offset;
1300 uint32_t name_length;
1301 std::string name;
1302 };
1303 std::vector<DeviceRecord> deviceRecords;
1304 uint32_t deviceRecordsPointerTableOffset = 2u * uint32_t_size + // packet_header
1305 6u * uint32_t_size + // body_header
1306 bodyHeaderWord1; // device_records_pointer_table_offset
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001307
1308 const unsigned char* readData = readBuffer->GetReadableData();
1309
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001310 for (uint32_t i = 0; i < deviceRecordCount; i++)
1311 {
1312 // Get the device record offset
1313 uint32_t deviceRecordOffset = ReadUint32(readBuffer, deviceRecordsPointerTableOffset + i * uint32_t_size);
1314
1315 // Collect the data for the device record
1316 uint32_t deviceRecordWord0 = ReadUint32(readBuffer,
1317 packetBodyPoolOffset + deviceRecordOffset + 0 * uint32_t_size);
1318 uint32_t deviceRecordWord1 = ReadUint32(readBuffer,
1319 packetBodyPoolOffset + deviceRecordOffset + 1 * uint32_t_size);
1320 DeviceRecord deviceRecord;
1321 deviceRecord.uid = static_cast<uint16_t>(deviceRecordWord0 >> 16); // uid
1322 deviceRecord.cores = static_cast<uint16_t>(deviceRecordWord0); // cores
1323 deviceRecord.name_offset = deviceRecordWord1; // name_offset
1324
1325 uint32_t deviceRecordPoolOffset = packetBodyPoolOffset + // Packet body offset
1326 deviceRecordOffset + // Device record offset
1327 2 * uint32_t_size + // Device record header
1328 deviceRecord.name_offset; // Device name offset
1329 uint32_t deviceRecordNameLength = ReadUint32(readBuffer, deviceRecordPoolOffset);
1330 deviceRecord.name_length = deviceRecordNameLength; // name_length
1331 unsigned char deviceRecordNameNullTerminator = // name null-terminator
1332 ReadUint8(readBuffer, deviceRecordPoolOffset + uint32_t_size + deviceRecordNameLength - 1);
1333 BOOST_CHECK(deviceRecordNameNullTerminator == '\0');
1334 std::vector<unsigned char> deviceRecordNameBuffer(deviceRecord.name_length - 1);
1335 std::memcpy(deviceRecordNameBuffer.data(),
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001336 readData + deviceRecordPoolOffset + uint32_t_size, deviceRecordNameBuffer.size());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001337 deviceRecord.name.assign(deviceRecordNameBuffer.begin(), deviceRecordNameBuffer.end()); // name
1338
1339 deviceRecords.push_back(deviceRecord);
1340 }
1341
1342 // Check that the device records are correct
1343 BOOST_CHECK(deviceRecords.size() == 2);
1344 for (const DeviceRecord& deviceRecord : deviceRecords)
1345 {
1346 const Device* device = counterDirectory.GetDevice(deviceRecord.uid);
1347 BOOST_CHECK(device);
1348 BOOST_CHECK(device->m_Uid == deviceRecord.uid);
1349 BOOST_CHECK(device->m_Cores == deviceRecord.cores);
1350 BOOST_CHECK(device->m_Name == deviceRecord.name);
1351 }
1352
1353 // Counter set record structure/collection used for testing
1354 struct CounterSetRecord
1355 {
1356 uint16_t uid;
1357 uint16_t count;
1358 uint32_t name_offset;
1359 uint32_t name_length;
1360 std::string name;
1361 };
1362 std::vector<CounterSetRecord> counterSetRecords;
1363 uint32_t counterSetRecordsPointerTableOffset = 2u * uint32_t_size + // packet_header
1364 6u * uint32_t_size + // body_header
1365 bodyHeaderWord3; // counter_set_pointer_table_offset
1366 for (uint32_t i = 0; i < counterSetRecordCount; i++)
1367 {
1368 // Get the counter set record offset
1369 uint32_t counterSetRecordOffset = ReadUint32(readBuffer,
1370 counterSetRecordsPointerTableOffset + i * uint32_t_size);
1371
1372 // Collect the data for the counter set record
1373 uint32_t counterSetRecordWord0 = ReadUint32(readBuffer,
1374 packetBodyPoolOffset + counterSetRecordOffset + 0 * uint32_t_size);
1375 uint32_t counterSetRecordWord1 = ReadUint32(readBuffer,
1376 packetBodyPoolOffset + counterSetRecordOffset + 1 * uint32_t_size);
1377 CounterSetRecord counterSetRecord;
1378 counterSetRecord.uid = static_cast<uint16_t>(counterSetRecordWord0 >> 16); // uid
1379 counterSetRecord.count = static_cast<uint16_t>(counterSetRecordWord0); // count
1380 counterSetRecord.name_offset = counterSetRecordWord1; // name_offset
1381
1382 uint32_t counterSetRecordPoolOffset = packetBodyPoolOffset + // Packet body offset
1383 counterSetRecordOffset + // Counter set record offset
1384 2 * uint32_t_size + // Counter set record header
1385 counterSetRecord.name_offset; // Counter set name offset
1386 uint32_t counterSetRecordNameLength = ReadUint32(readBuffer, counterSetRecordPoolOffset);
1387 counterSetRecord.name_length = counterSetRecordNameLength; // name_length
1388 unsigned char counterSetRecordNameNullTerminator = // name null-terminator
1389 ReadUint8(readBuffer, counterSetRecordPoolOffset + uint32_t_size + counterSetRecordNameLength - 1);
1390 BOOST_CHECK(counterSetRecordNameNullTerminator == '\0');
1391 std::vector<unsigned char> counterSetRecordNameBuffer(counterSetRecord.name_length - 1);
1392 std::memcpy(counterSetRecordNameBuffer.data(),
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001393 readData + counterSetRecordPoolOffset + uint32_t_size, counterSetRecordNameBuffer.size());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001394 counterSetRecord.name.assign(counterSetRecordNameBuffer.begin(), counterSetRecordNameBuffer.end()); // name
1395
1396 counterSetRecords.push_back(counterSetRecord);
1397 }
1398
1399 // Check that the counter set records are correct
1400 BOOST_CHECK(counterSetRecords.size() == 1);
1401 for (const CounterSetRecord& counterSetRecord : counterSetRecords)
1402 {
1403 const CounterSet* counterSet = counterDirectory.GetCounterSet(counterSetRecord.uid);
1404 BOOST_CHECK(counterSet);
1405 BOOST_CHECK(counterSet->m_Uid == counterSetRecord.uid);
1406 BOOST_CHECK(counterSet->m_Count == counterSetRecord.count);
1407 BOOST_CHECK(counterSet->m_Name == counterSetRecord.name);
1408 }
1409
1410 // Event record structure/collection used for testing
1411 struct EventRecord
1412 {
1413 uint16_t counter_uid;
1414 uint16_t max_counter_uid;
1415 uint16_t device;
1416 uint16_t counter_set;
1417 uint16_t counter_class;
1418 uint16_t interpolation;
1419 double multiplier;
1420 uint32_t name_offset;
1421 uint32_t name_length;
1422 std::string name;
1423 uint32_t description_offset;
1424 uint32_t description_length;
1425 std::string description;
1426 uint32_t units_offset;
1427 uint32_t units_length;
1428 std::string units;
1429 };
1430 // Category record structure/collection used for testing
1431 struct CategoryRecord
1432 {
1433 uint16_t device;
1434 uint16_t counter_set;
1435 uint16_t event_count;
1436 uint32_t event_pointer_table_offset;
1437 uint32_t name_offset;
1438 uint32_t name_length;
1439 std::string name;
1440 std::vector<uint32_t> event_pointer_table;
1441 std::vector<EventRecord> event_records;
1442 };
1443 std::vector<CategoryRecord> categoryRecords;
1444 uint32_t categoryRecordsPointerTableOffset = 2u * uint32_t_size + // packet_header
1445 6u * uint32_t_size + // body_header
1446 bodyHeaderWord5; // categories_pointer_table_offset
1447 for (uint32_t i = 0; i < categoryRecordCount; i++)
1448 {
1449 // Get the category record offset
1450 uint32_t categoryRecordOffset = ReadUint32(readBuffer, categoryRecordsPointerTableOffset + i * uint32_t_size);
1451
1452 // Collect the data for the category record
1453 uint32_t categoryRecordWord0 = ReadUint32(readBuffer,
1454 packetBodyPoolOffset + categoryRecordOffset + 0 * uint32_t_size);
1455 uint32_t categoryRecordWord1 = ReadUint32(readBuffer,
1456 packetBodyPoolOffset + categoryRecordOffset + 1 * uint32_t_size);
1457 uint32_t categoryRecordWord2 = ReadUint32(readBuffer,
1458 packetBodyPoolOffset + categoryRecordOffset + 2 * uint32_t_size);
1459 uint32_t categoryRecordWord3 = ReadUint32(readBuffer,
1460 packetBodyPoolOffset + categoryRecordOffset + 3 * uint32_t_size);
1461 CategoryRecord categoryRecord;
1462 categoryRecord.device = static_cast<uint16_t>(categoryRecordWord0 >> 16); // device
1463 categoryRecord.counter_set = static_cast<uint16_t>(categoryRecordWord0); // counter_set
1464 categoryRecord.event_count = static_cast<uint16_t>(categoryRecordWord1 >> 16); // event_count
1465 categoryRecord.event_pointer_table_offset = categoryRecordWord2; // event_pointer_table_offset
1466 categoryRecord.name_offset = categoryRecordWord3; // name_offset
1467
1468 uint32_t categoryRecordPoolOffset = packetBodyPoolOffset + // Packet body offset
1469 categoryRecordOffset + // Category record offset
1470 4 * uint32_t_size; // Category record header
1471
1472 uint32_t categoryRecordNameLength = ReadUint32(readBuffer,
1473 categoryRecordPoolOffset + categoryRecord.name_offset);
1474 categoryRecord.name_length = categoryRecordNameLength; // name_length
1475 unsigned char categoryRecordNameNullTerminator =
1476 ReadUint8(readBuffer,
1477 categoryRecordPoolOffset +
1478 categoryRecord.name_offset +
1479 uint32_t_size +
1480 categoryRecordNameLength - 1); // name null-terminator
1481 BOOST_CHECK(categoryRecordNameNullTerminator == '\0');
1482 std::vector<unsigned char> categoryRecordNameBuffer(categoryRecord.name_length - 1);
1483 std::memcpy(categoryRecordNameBuffer.data(),
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001484 readData +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001485 categoryRecordPoolOffset +
1486 categoryRecord.name_offset +
1487 uint32_t_size,
1488 categoryRecordNameBuffer.size());
1489 categoryRecord.name.assign(categoryRecordNameBuffer.begin(), categoryRecordNameBuffer.end()); // name
1490
1491 categoryRecord.event_pointer_table.resize(categoryRecord.event_count);
1492 for (uint32_t eventIndex = 0; eventIndex < categoryRecord.event_count; eventIndex++)
1493 {
1494 uint32_t eventRecordOffset = ReadUint32(readBuffer,
1495 categoryRecordPoolOffset +
1496 categoryRecord.event_pointer_table_offset +
1497 eventIndex * uint32_t_size);
1498 categoryRecord.event_pointer_table[eventIndex] = eventRecordOffset;
1499
1500 // Collect the data for the event record
1501 uint32_t eventRecordWord0 = ReadUint32(readBuffer,
1502 categoryRecordPoolOffset + eventRecordOffset + 0 * uint32_t_size);
1503 uint32_t eventRecordWord1 = ReadUint32(readBuffer,
1504 categoryRecordPoolOffset + eventRecordOffset + 1 * uint32_t_size);
1505 uint32_t eventRecordWord2 = ReadUint32(readBuffer,
1506 categoryRecordPoolOffset + eventRecordOffset + 2 * uint32_t_size);
1507 uint64_t eventRecordWord34 = ReadUint64(readBuffer,
1508 categoryRecordPoolOffset + eventRecordOffset + 3 * uint32_t_size);
1509 uint32_t eventRecordWord5 = ReadUint32(readBuffer,
1510 categoryRecordPoolOffset + eventRecordOffset + 5 * uint32_t_size);
1511 uint32_t eventRecordWord6 = ReadUint32(readBuffer,
1512 categoryRecordPoolOffset + eventRecordOffset + 6 * uint32_t_size);
1513 uint32_t eventRecordWord7 = ReadUint32(readBuffer,
1514 categoryRecordPoolOffset + eventRecordOffset + 7 * uint32_t_size);
1515 EventRecord eventRecord;
1516 eventRecord.counter_uid = static_cast<uint16_t>(eventRecordWord0); // counter_uid
1517 eventRecord.max_counter_uid = static_cast<uint16_t>(eventRecordWord0 >> 16); // max_counter_uid
1518 eventRecord.device = static_cast<uint16_t>(eventRecordWord1 >> 16); // device
1519 eventRecord.counter_set = static_cast<uint16_t>(eventRecordWord1); // counter_set
1520 eventRecord.counter_class = static_cast<uint16_t>(eventRecordWord2 >> 16); // class
1521 eventRecord.interpolation = static_cast<uint16_t>(eventRecordWord2); // interpolation
1522 std::memcpy(&eventRecord.multiplier, &eventRecordWord34, sizeof(eventRecord.multiplier)); // multiplier
1523 eventRecord.name_offset = static_cast<uint32_t>(eventRecordWord5); // name_offset
1524 eventRecord.description_offset = static_cast<uint32_t>(eventRecordWord6); // description_offset
1525 eventRecord.units_offset = static_cast<uint32_t>(eventRecordWord7); // units_offset
1526
1527 uint32_t eventRecordPoolOffset = categoryRecordPoolOffset + // Category record pool offset
1528 eventRecordOffset + // Event record offset
1529 8 * uint32_t_size; // Event record header
1530
1531 uint32_t eventRecordNameLength = ReadUint32(readBuffer,
1532 eventRecordPoolOffset + eventRecord.name_offset);
1533 eventRecord.name_length = eventRecordNameLength; // name_length
1534 unsigned char eventRecordNameNullTerminator =
1535 ReadUint8(readBuffer,
1536 eventRecordPoolOffset +
1537 eventRecord.name_offset +
1538 uint32_t_size +
1539 eventRecordNameLength - 1); // name null-terminator
1540 BOOST_CHECK(eventRecordNameNullTerminator == '\0');
1541 std::vector<unsigned char> eventRecordNameBuffer(eventRecord.name_length - 1);
1542 std::memcpy(eventRecordNameBuffer.data(),
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001543 readData +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001544 eventRecordPoolOffset +
1545 eventRecord.name_offset +
1546 uint32_t_size,
1547 eventRecordNameBuffer.size());
1548 eventRecord.name.assign(eventRecordNameBuffer.begin(), eventRecordNameBuffer.end()); // name
1549
1550 uint32_t eventRecordDescriptionLength = ReadUint32(readBuffer,
1551 eventRecordPoolOffset + eventRecord.description_offset);
1552 eventRecord.description_length = eventRecordDescriptionLength; // description_length
1553 unsigned char eventRecordDescriptionNullTerminator =
1554 ReadUint8(readBuffer,
1555 eventRecordPoolOffset +
1556 eventRecord.description_offset +
1557 uint32_t_size +
1558 eventRecordDescriptionLength - 1); // description null-terminator
1559 BOOST_CHECK(eventRecordDescriptionNullTerminator == '\0');
1560 std::vector<unsigned char> eventRecordDescriptionBuffer(eventRecord.description_length - 1);
1561 std::memcpy(eventRecordDescriptionBuffer.data(),
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001562 readData +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001563 eventRecordPoolOffset +
1564 eventRecord.description_offset +
1565 uint32_t_size,
1566 eventRecordDescriptionBuffer.size());
1567 eventRecord.description.assign(eventRecordDescriptionBuffer.begin(),
1568 eventRecordDescriptionBuffer.end()); // description
1569
1570 if (eventRecord.units_offset > 0)
1571 {
1572 uint32_t eventRecordUnitsLength = ReadUint32(readBuffer,
1573 eventRecordPoolOffset + eventRecord.units_offset);
1574 eventRecord.units_length = eventRecordUnitsLength; // units_length
1575 unsigned char eventRecordUnitsNullTerminator =
1576 ReadUint8(readBuffer,
1577 eventRecordPoolOffset +
1578 eventRecord.units_offset +
1579 uint32_t_size +
1580 eventRecordUnitsLength - 1); // units null-terminator
1581 BOOST_CHECK(eventRecordUnitsNullTerminator == '\0');
1582 std::vector<unsigned char> eventRecordUnitsBuffer(eventRecord.units_length - 1);
1583 std::memcpy(eventRecordUnitsBuffer.data(),
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001584 readData +
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001585 eventRecordPoolOffset +
1586 eventRecord.units_offset +
1587 uint32_t_size,
1588 eventRecordUnitsBuffer.size());
1589 eventRecord.units.assign(eventRecordUnitsBuffer.begin(), eventRecordUnitsBuffer.end()); // units
1590 }
1591
1592 categoryRecord.event_records.push_back(eventRecord);
1593 }
1594
1595 categoryRecords.push_back(categoryRecord);
1596 }
1597
1598 // Check that the category records are correct
1599 BOOST_CHECK(categoryRecords.size() == 2);
1600 for (const CategoryRecord& categoryRecord : categoryRecords)
1601 {
1602 const Category* category = counterDirectory.GetCategory(categoryRecord.name);
1603 BOOST_CHECK(category);
1604 BOOST_CHECK(category->m_Name == categoryRecord.name);
1605 BOOST_CHECK(category->m_DeviceUid == categoryRecord.device);
1606 BOOST_CHECK(category->m_CounterSetUid == categoryRecord.counter_set);
1607 BOOST_CHECK(category->m_Counters.size() == categoryRecord.event_count);
1608
1609 // Check that the event records are correct
1610 for (const EventRecord& eventRecord : categoryRecord.event_records)
1611 {
1612 const Counter* counter = counterDirectory.GetCounter(eventRecord.counter_uid);
1613 BOOST_CHECK(counter);
1614 BOOST_CHECK(counter->m_MaxCounterUid == eventRecord.max_counter_uid);
1615 BOOST_CHECK(counter->m_DeviceUid == eventRecord.device);
1616 BOOST_CHECK(counter->m_CounterSetUid == eventRecord.counter_set);
1617 BOOST_CHECK(counter->m_Class == eventRecord.counter_class);
1618 BOOST_CHECK(counter->m_Interpolation == eventRecord.interpolation);
1619 BOOST_CHECK(counter->m_Multiplier == eventRecord.multiplier);
1620 BOOST_CHECK(counter->m_Name == eventRecord.name);
1621 BOOST_CHECK(counter->m_Description == eventRecord.description);
1622 BOOST_CHECK(counter->m_Units == eventRecord.units);
1623 }
1624 }
1625}
1626
1627BOOST_AUTO_TEST_CASE(SendCounterDirectoryPacketTest3)
1628{
1629 // Using a mock counter directory that allows to register invalid objects
1630 MockCounterDirectory counterDirectory;
1631
1632 // Register an invalid device
1633 const std::string deviceName = "inv@lid dev!c€";
1634 const Device* device = nullptr;
1635 BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName, 3));
1636 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
1637 BOOST_CHECK(device);
1638
1639 // Buffer with enough space
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001640 MockBufferManager mockBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00001641 SendCounterPacket sendCounterPacket(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001642 BOOST_CHECK_THROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory), armnn::RuntimeException);
1643}
1644
1645BOOST_AUTO_TEST_CASE(SendCounterDirectoryPacketTest4)
1646{
1647 // Using a mock counter directory that allows to register invalid objects
1648 MockCounterDirectory counterDirectory;
1649
1650 // Register an invalid counter set
1651 const std::string counterSetName = "inv@lid count€rs€t";
1652 const CounterSet* counterSet = nullptr;
1653 BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
1654 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1655 BOOST_CHECK(counterSet);
1656
1657 // Buffer with enough space
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001658 MockBufferManager mockBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00001659 SendCounterPacket sendCounterPacket(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001660 BOOST_CHECK_THROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory), armnn::RuntimeException);
1661}
1662
1663BOOST_AUTO_TEST_CASE(SendCounterDirectoryPacketTest5)
1664{
1665 // Using a mock counter directory that allows to register invalid objects
1666 MockCounterDirectory counterDirectory;
1667
1668 // Register an invalid category
1669 const std::string categoryName = "c@t€gory";
1670 const Category* category = nullptr;
1671 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName));
1672 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
1673 BOOST_CHECK(category);
1674
1675 // Buffer with enough space
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001676 MockBufferManager mockBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00001677 SendCounterPacket sendCounterPacket(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001678 BOOST_CHECK_THROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory), armnn::RuntimeException);
1679}
1680
1681BOOST_AUTO_TEST_CASE(SendCounterDirectoryPacketTest6)
1682{
1683 // Using a mock counter directory that allows to register invalid objects
1684 MockCounterDirectory counterDirectory;
1685
1686 // Register an invalid device
1687 const std::string deviceName = "inv@lid dev!c€";
1688 const Device* device = nullptr;
1689 BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName, 3));
1690 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
1691 BOOST_CHECK(device);
1692
1693 // Register an invalid counter set
1694 const std::string counterSetName = "inv@lid count€rs€t";
1695 const CounterSet* counterSet = nullptr;
1696 BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
1697 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1698 BOOST_CHECK(counterSet);
1699
1700 // Register an invalid category associated to an invalid device and an invalid counter set
1701 const std::string categoryName = "c@t€gory";
1702 const Category* category = nullptr;
1703 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName,
1704 device->m_Uid,
1705 counterSet->m_Uid));
1706 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
1707 BOOST_CHECK(category);
1708
1709 // Buffer with enough space
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001710 MockBufferManager mockBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00001711 SendCounterPacket sendCounterPacket(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001712 BOOST_CHECK_THROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory), armnn::RuntimeException);
1713}
1714
1715BOOST_AUTO_TEST_CASE(SendCounterDirectoryPacketTest7)
1716{
1717 // Using a mock counter directory that allows to register invalid objects
1718 MockCounterDirectory counterDirectory;
1719
1720 // Register an valid device
1721 const std::string deviceName = "valid device";
1722 const Device* device = nullptr;
1723 BOOST_CHECK_NO_THROW(device = counterDirectory.RegisterDevice(deviceName, 3));
1724 BOOST_CHECK(counterDirectory.GetDeviceCount() == 1);
1725 BOOST_CHECK(device);
1726
1727 // Register an valid counter set
1728 const std::string counterSetName = "valid counterset";
1729 const CounterSet* counterSet = nullptr;
1730 BOOST_CHECK_NO_THROW(counterSet = counterDirectory.RegisterCounterSet(counterSetName));
1731 BOOST_CHECK(counterDirectory.GetCounterSetCount() == 1);
1732 BOOST_CHECK(counterSet);
1733
1734 // Register an valid category associated to a valid device and a valid counter set
1735 const std::string categoryName = "category";
1736 const Category* category = nullptr;
1737 BOOST_CHECK_NO_THROW(category = counterDirectory.RegisterCategory(categoryName,
1738 device->m_Uid,
1739 counterSet->m_Uid));
1740 BOOST_CHECK(counterDirectory.GetCategoryCount() == 1);
1741 BOOST_CHECK(category);
1742
1743 // Register an invalid counter associated to a valid category
1744 const Counter* counter = nullptr;
Keith Davise394bd92019-12-02 15:12:19 +00001745 BOOST_CHECK_NO_THROW(counter = counterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID,
1746 0,
1747 categoryName,
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001748 0,
1749 1,
1750 123.45f,
1751 "counter",
1752 "counter description",
1753 std::string("invalid counter units"),
1754 5,
1755 device->m_Uid,
1756 counterSet->m_Uid));
1757 BOOST_CHECK(counterDirectory.GetCounterCount() == 5);
1758 BOOST_CHECK(counter);
1759
1760 // Buffer with enough space
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001761 MockBufferManager mockBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00001762 SendCounterPacket sendCounterPacket(mockBuffer);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +01001763 BOOST_CHECK_THROW(sendCounterPacket.SendCounterDirectoryPacket(counterDirectory), armnn::RuntimeException);
1764}
Ferran Balaguer47d0fe92019-09-04 16:47:34 +01001765
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001766BOOST_AUTO_TEST_CASE(SendThreadTest0)
1767{
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01001768 ProfilingStateMachine profilingStateMachine;
1769 SetActiveProfilingState(profilingStateMachine);
1770
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001771 MockProfilingConnection mockProfilingConnection;
Matteo Martincigh61d6f732019-10-03 11:21:18 +01001772 MockStreamCounterBuffer mockStreamCounterBuffer(0);
Sadik Armagan3896b472020-02-10 12:24:15 +00001773 SendCounterPacket sendCounterPacket(mockStreamCounterBuffer);
1774 SendThread sendThread(profilingStateMachine, mockStreamCounterBuffer, sendCounterPacket);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001775
1776 // Try to start the send thread many times, it must only start once
1777
Sadik Armagan3896b472020-02-10 12:24:15 +00001778 sendThread.Start(mockProfilingConnection);
1779 BOOST_CHECK(sendThread.IsRunning());
1780 sendThread.Start(mockProfilingConnection);
1781 sendThread.Start(mockProfilingConnection);
1782 sendThread.Start(mockProfilingConnection);
1783 sendThread.Start(mockProfilingConnection);
1784 BOOST_CHECK(sendThread.IsRunning());
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001785
Sadik Armagan3896b472020-02-10 12:24:15 +00001786 sendThread.Stop();
1787 BOOST_CHECK(!sendThread.IsRunning());
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001788}
1789
1790BOOST_AUTO_TEST_CASE(SendThreadTest1)
1791{
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01001792 ProfilingStateMachine profilingStateMachine;
1793 SetActiveProfilingState(profilingStateMachine);
1794
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001795 unsigned int totalWrittenSize = 0;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001796
1797 MockProfilingConnection mockProfilingConnection;
Matteo Martincigh61d6f732019-10-03 11:21:18 +01001798 MockStreamCounterBuffer mockStreamCounterBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00001799 SendCounterPacket sendCounterPacket(mockStreamCounterBuffer);
1800 SendThread sendThread(profilingStateMachine, mockStreamCounterBuffer, sendCounterPacket);
1801 sendThread.Start(mockProfilingConnection);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001802
1803 // Interleaving writes and reads to/from the buffer with pauses to test that the send thread actually waits for
1804 // something to become available for reading
1805
Colm Donelan2ba48d22019-11-29 09:10:59 +00001806 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001807
1808 CounterDirectory counterDirectory;
1809 sendCounterPacket.SendStreamMetaDataPacket();
1810
1811 // Get the size of the Stream Metadata Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001812 std::string processName = GetProcessName().substr(0, 60);
Matteo Martincigh61d6f732019-10-03 11:21:18 +01001813 unsigned int processNameSize = processName.empty() ? 0 : boost::numeric_cast<unsigned int>(processName.size()) + 1;
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001814 unsigned int streamMetadataPacketsize = 118 + processNameSize;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001815 totalWrittenSize += streamMetadataPacketsize;
1816
Sadik Armagan3896b472020-02-10 12:24:15 +00001817 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001818
Colm Donelan2ba48d22019-11-29 09:10:59 +00001819 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001820
1821 sendCounterPacket.SendCounterDirectoryPacket(counterDirectory);
1822
1823 // Get the size of the Counter Directory Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001824 unsigned int counterDirectoryPacketSize = 32;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001825 totalWrittenSize += counterDirectoryPacketSize;
1826
Sadik Armagan3896b472020-02-10 12:24:15 +00001827 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001828
Colm Donelan2ba48d22019-11-29 09:10:59 +00001829 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001830
1831 sendCounterPacket.SendPeriodicCounterCapturePacket(123u,
1832 {
1833 { 1u, 23u },
1834 { 33u, 1207623u }
1835 });
1836
1837 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001838 unsigned int periodicCounterCapturePacketSize = 28;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001839 totalWrittenSize += periodicCounterCapturePacketSize;
1840
Sadik Armagan3896b472020-02-10 12:24:15 +00001841 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001842
Colm Donelan2ba48d22019-11-29 09:10:59 +00001843 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001844
1845 sendCounterPacket.SendPeriodicCounterCapturePacket(44u,
1846 {
1847 { 211u, 923u }
1848 });
1849
1850 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001851 periodicCounterCapturePacketSize = 22;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001852 totalWrittenSize += periodicCounterCapturePacketSize;
1853
1854 sendCounterPacket.SendPeriodicCounterCapturePacket(1234u,
1855 {
1856 { 555u, 23u },
1857 { 556u, 6u },
1858 { 557u, 893454u },
1859 { 558u, 1456623u },
1860 { 559u, 571090u }
1861 });
1862
1863 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001864 periodicCounterCapturePacketSize = 46;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001865 totalWrittenSize += periodicCounterCapturePacketSize;
1866
1867 sendCounterPacket.SendPeriodicCounterCapturePacket(997u,
1868 {
1869 { 88u, 11u },
1870 { 96u, 22u },
1871 { 97u, 33u },
1872 { 999u, 444u }
1873 });
1874
1875 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001876 periodicCounterCapturePacketSize = 40;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001877 totalWrittenSize += periodicCounterCapturePacketSize;
1878
Sadik Armagan3896b472020-02-10 12:24:15 +00001879 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001880
Colm Donelan2ba48d22019-11-29 09:10:59 +00001881 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001882
1883 sendCounterPacket.SendPeriodicCounterSelectionPacket(1000u, { 1345u, 254u, 4536u, 408u, 54u, 6323u, 428u, 1u, 6u });
1884
1885 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001886 periodicCounterCapturePacketSize = 30;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001887 totalWrittenSize += periodicCounterCapturePacketSize;
1888
Sadik Armagan3896b472020-02-10 12:24:15 +00001889 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001890
Finn Williams109c05b2019-11-29 13:56:33 +00001891 // 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 +01001892 // read all what's remaining in the buffer
Colm Donelan2ba48d22019-11-29 09:10:59 +00001893 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001894
Sadik Armagan3896b472020-02-10 12:24:15 +00001895 sendThread.Stop();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001896
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001897 BOOST_CHECK(mockStreamCounterBuffer.GetCommittedSize() == totalWrittenSize);
Matteo Martincigh61d6f732019-10-03 11:21:18 +01001898 BOOST_CHECK(mockStreamCounterBuffer.GetReadableSize() == totalWrittenSize);
1899 BOOST_CHECK(mockStreamCounterBuffer.GetReadSize() == totalWrittenSize);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001900}
1901
1902BOOST_AUTO_TEST_CASE(SendThreadTest2)
1903{
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01001904 ProfilingStateMachine profilingStateMachine;
1905 SetActiveProfilingState(profilingStateMachine);
1906
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001907 unsigned int totalWrittenSize = 0;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001908
1909 MockProfilingConnection mockProfilingConnection;
Matteo Martincigh61d6f732019-10-03 11:21:18 +01001910 MockStreamCounterBuffer mockStreamCounterBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00001911 SendCounterPacket sendCounterPacket(mockStreamCounterBuffer);
1912 SendThread sendThread(profilingStateMachine, mockStreamCounterBuffer, sendCounterPacket);
1913 sendThread.Start(mockProfilingConnection);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001914
1915 // Adding many spurious "ready to read" signals throughout the test to check that the send thread is
1916 // capable of handling unnecessary read requests
1917
Colm Donelan2ba48d22019-11-29 09:10:59 +00001918 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001919
Sadik Armagan3896b472020-02-10 12:24:15 +00001920 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001921
1922 CounterDirectory counterDirectory;
1923 sendCounterPacket.SendStreamMetaDataPacket();
1924
1925 // Get the size of the Stream Metadata Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001926 std::string processName = GetProcessName().substr(0, 60);
Matteo Martincigh61d6f732019-10-03 11:21:18 +01001927 unsigned int processNameSize = processName.empty() ? 0 : boost::numeric_cast<unsigned int>(processName.size()) + 1;
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001928 unsigned int streamMetadataPacketsize = 118 + processNameSize;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001929 totalWrittenSize += streamMetadataPacketsize;
1930
Sadik Armagan3896b472020-02-10 12:24:15 +00001931 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001932
Colm Donelan2ba48d22019-11-29 09:10:59 +00001933 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001934
1935 sendCounterPacket.SendCounterDirectoryPacket(counterDirectory);
1936
1937 // Get the size of the Counter Directory Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001938 unsigned int counterDirectoryPacketSize = 32;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001939 totalWrittenSize += counterDirectoryPacketSize;
1940
Sadik Armagan3896b472020-02-10 12:24:15 +00001941 sendThread.SetReadyToRead();
1942 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001943
Colm Donelan2ba48d22019-11-29 09:10:59 +00001944 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001945
1946 sendCounterPacket.SendPeriodicCounterCapturePacket(123u,
1947 {
1948 { 1u, 23u },
1949 { 33u, 1207623u }
1950 });
1951
1952 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001953 unsigned int periodicCounterCapturePacketSize = 28;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001954 totalWrittenSize += periodicCounterCapturePacketSize;
1955
Sadik Armagan3896b472020-02-10 12:24:15 +00001956 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001957
Colm Donelan2ba48d22019-11-29 09:10:59 +00001958 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001959
Sadik Armagan3896b472020-02-10 12:24:15 +00001960 sendThread.SetReadyToRead();
1961 sendThread.SetReadyToRead();
1962 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001963
Colm Donelan2ba48d22019-11-29 09:10:59 +00001964 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001965
Sadik Armagan3896b472020-02-10 12:24:15 +00001966 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001967 sendCounterPacket.SendPeriodicCounterCapturePacket(44u,
1968 {
1969 { 211u, 923u }
1970 });
1971
1972 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001973 periodicCounterCapturePacketSize = 22;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001974 totalWrittenSize += periodicCounterCapturePacketSize;
1975
1976 sendCounterPacket.SendPeriodicCounterCapturePacket(1234u,
1977 {
1978 { 555u, 23u },
1979 { 556u, 6u },
1980 { 557u, 893454u },
1981 { 558u, 1456623u },
1982 { 559u, 571090u }
1983 });
1984
1985 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001986 periodicCounterCapturePacketSize = 46;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001987 totalWrittenSize += periodicCounterCapturePacketSize;
1988
Sadik Armagan3896b472020-02-10 12:24:15 +00001989 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01001990 sendCounterPacket.SendPeriodicCounterCapturePacket(997u,
1991 {
1992 { 88u, 11u },
1993 { 96u, 22u },
1994 { 97u, 33u },
1995 { 999u, 444u }
1996 });
1997
1998 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01001999 periodicCounterCapturePacketSize = 40;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002000 totalWrittenSize += periodicCounterCapturePacketSize;
2001
Sadik Armagan3896b472020-02-10 12:24:15 +00002002 sendThread.SetReadyToRead();
2003 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002004
Colm Donelan2ba48d22019-11-29 09:10:59 +00002005 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_UNTIL_READABLE_MS));
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002006
2007 sendCounterPacket.SendPeriodicCounterSelectionPacket(1000u, { 1345u, 254u, 4536u, 408u, 54u, 6323u, 428u, 1u, 6u });
2008
2009 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002010 periodicCounterCapturePacketSize = 30;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002011 totalWrittenSize += periodicCounterCapturePacketSize;
2012
Sadik Armagan3896b472020-02-10 12:24:15 +00002013 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002014
Finn Williams109c05b2019-11-29 13:56:33 +00002015 // 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 +01002016 // read all what's remaining in the buffer
Sadik Armagan3896b472020-02-10 12:24:15 +00002017 sendThread.Stop();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002018
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002019 BOOST_CHECK(mockStreamCounterBuffer.GetCommittedSize() == totalWrittenSize);
Matteo Martincigh61d6f732019-10-03 11:21:18 +01002020 BOOST_CHECK(mockStreamCounterBuffer.GetReadableSize() == totalWrittenSize);
2021 BOOST_CHECK(mockStreamCounterBuffer.GetReadSize() == totalWrittenSize);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002022}
2023
2024BOOST_AUTO_TEST_CASE(SendThreadTest3)
2025{
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002026 ProfilingStateMachine profilingStateMachine;
2027 SetActiveProfilingState(profilingStateMachine);
2028
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002029 unsigned int totalWrittenSize = 0;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002030
2031 MockProfilingConnection mockProfilingConnection;
Matteo Martincigh61d6f732019-10-03 11:21:18 +01002032 MockStreamCounterBuffer mockStreamCounterBuffer(1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002033 SendCounterPacket sendCounterPacket(mockStreamCounterBuffer);
2034 SendThread sendThread(profilingStateMachine, mockStreamCounterBuffer, sendCounterPacket);
2035 sendThread.Start(mockProfilingConnection);
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002036
2037 // Not using pauses or "grace periods" to stress test the send thread
2038
Sadik Armagan3896b472020-02-10 12:24:15 +00002039 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002040
2041 CounterDirectory counterDirectory;
2042 sendCounterPacket.SendStreamMetaDataPacket();
2043
2044 // Get the size of the Stream Metadata Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002045 std::string processName = GetProcessName().substr(0, 60);
Matteo Martincigh61d6f732019-10-03 11:21:18 +01002046 unsigned int processNameSize = processName.empty() ? 0 : boost::numeric_cast<unsigned int>(processName.size()) + 1;
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002047 unsigned int streamMetadataPacketsize = 118 + processNameSize;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002048 totalWrittenSize += streamMetadataPacketsize;
2049
Sadik Armagan3896b472020-02-10 12:24:15 +00002050 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002051 sendCounterPacket.SendCounterDirectoryPacket(counterDirectory);
2052
2053 // Get the size of the Counter Directory Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002054 unsigned int counterDirectoryPacketSize =32;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002055 totalWrittenSize += counterDirectoryPacketSize;
2056
Sadik Armagan3896b472020-02-10 12:24:15 +00002057 sendThread.SetReadyToRead();
2058 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002059 sendCounterPacket.SendPeriodicCounterCapturePacket(123u,
2060 {
2061 { 1u, 23u },
2062 { 33u, 1207623u }
2063 });
2064
2065 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002066 unsigned int periodicCounterCapturePacketSize = 28;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002067 totalWrittenSize += periodicCounterCapturePacketSize;
2068
Sadik Armagan3896b472020-02-10 12:24:15 +00002069 sendThread.SetReadyToRead();
2070 sendThread.SetReadyToRead();
2071 sendThread.SetReadyToRead();
2072 sendThread.SetReadyToRead();
2073 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002074 sendCounterPacket.SendPeriodicCounterCapturePacket(44u,
2075 {
2076 { 211u, 923u }
2077 });
2078
2079 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002080 periodicCounterCapturePacketSize = 22;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002081 totalWrittenSize += periodicCounterCapturePacketSize;
2082
2083 sendCounterPacket.SendPeriodicCounterCapturePacket(1234u,
2084 {
2085 { 555u, 23u },
2086 { 556u, 6u },
2087 { 557u, 893454u },
2088 { 558u, 1456623u },
2089 { 559u, 571090u }
2090 });
2091
2092 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002093 periodicCounterCapturePacketSize = 46;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002094 totalWrittenSize += periodicCounterCapturePacketSize;
2095
Sadik Armagan3896b472020-02-10 12:24:15 +00002096 sendThread.SetReadyToRead();
2097 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002098 sendCounterPacket.SendPeriodicCounterCapturePacket(997u,
2099 {
2100 { 88u, 11u },
2101 { 96u, 22u },
2102 { 97u, 33u },
2103 { 999u, 444u }
2104 });
2105
2106 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002107 periodicCounterCapturePacketSize = 40;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002108 totalWrittenSize += periodicCounterCapturePacketSize;
2109
Sadik Armagan3896b472020-02-10 12:24:15 +00002110 sendThread.SetReadyToRead();
2111 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002112 sendCounterPacket.SendPeriodicCounterSelectionPacket(1000u, { 1345u, 254u, 4536u, 408u, 54u, 6323u, 428u, 1u, 6u });
2113
2114 // Get the size of the Periodic Counter Capture Packet
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01002115 periodicCounterCapturePacketSize = 30;
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002116 totalWrittenSize += periodicCounterCapturePacketSize;
2117
Sadik Armagan3896b472020-02-10 12:24:15 +00002118 sendThread.SetReadyToRead();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002119
2120 // Abruptly terminating the send thread, the amount of data sent may be less that the amount written (the send
2121 // thread is not guaranteed to flush the buffer)
Sadik Armagan3896b472020-02-10 12:24:15 +00002122 sendThread.Stop();
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002123
Matteo Martincigh61d6f732019-10-03 11:21:18 +01002124 BOOST_CHECK(mockStreamCounterBuffer.GetCommittedSize() == totalWrittenSize);
2125 BOOST_CHECK(mockStreamCounterBuffer.GetReadableSize() <= totalWrittenSize);
2126 BOOST_CHECK(mockStreamCounterBuffer.GetReadSize() <= totalWrittenSize);
2127 BOOST_CHECK(mockStreamCounterBuffer.GetReadSize() <= mockStreamCounterBuffer.GetReadableSize());
2128 BOOST_CHECK(mockStreamCounterBuffer.GetReadSize() <= mockStreamCounterBuffer.GetCommittedSize());
Matteo Martincigh24e8f922019-09-19 11:57:46 +01002129}
2130
Sadik Armagan3896b472020-02-10 12:24:15 +00002131BOOST_AUTO_TEST_CASE(SendCounterPacketTestWithSendThread)
2132{
2133 ProfilingStateMachine profilingStateMachine;
2134 SetWaitingForAckProfilingState(profilingStateMachine);
2135
2136 MockProfilingConnection mockProfilingConnection;
2137 BufferManager bufferManager(1, 1024);
2138 SendCounterPacket sendCounterPacket(bufferManager);
2139 SendThread sendThread(profilingStateMachine, bufferManager, sendCounterPacket, -1);
2140 sendThread.Start(mockProfilingConnection);
2141
2142 std::string processName = GetProcessName().substr(0, 60);
2143 unsigned int processNameSize = processName.empty() ? 0 : boost::numeric_cast<unsigned int>(processName.size()) + 1;
2144 unsigned int streamMetadataPacketsize = 118 + processNameSize;
2145
2146 sendThread.Stop();
2147
2148 // check for packet in ProfilingConnection
2149 BOOST_CHECK(mockProfilingConnection.CheckForPacket({PacketType::StreamMetaData, streamMetadataPacketsize}) == 1);
2150
2151 SetActiveProfilingState(profilingStateMachine);
2152 sendThread.Start(mockProfilingConnection);
2153
2154 // SendCounterDirectoryPacket
2155 CounterDirectory counterDirectory;
2156 sendCounterPacket.SendCounterDirectoryPacket(counterDirectory);
2157
2158 sendThread.Stop();
2159 unsigned int counterDirectoryPacketSize = 32;
2160 // check for packet in ProfilingConnection
2161 BOOST_CHECK(mockProfilingConnection.CheckForPacket(
2162 {PacketType::CounterDirectory, counterDirectoryPacketSize}) == 1);
2163
2164 sendThread.Start(mockProfilingConnection);
2165
2166 // SendPeriodicCounterCapturePacket
2167 sendCounterPacket.SendPeriodicCounterCapturePacket(123u,
2168 {
2169 { 1u, 23u },
2170 { 33u, 1207623u }
2171 });
2172
2173 sendThread.Stop();
2174
2175 unsigned int periodicCounterCapturePacketSize = 28;
2176 BOOST_CHECK(mockProfilingConnection.CheckForPacket(
2177 {PacketType::PeriodicCounterCapture, periodicCounterCapturePacketSize}) == 1);
2178}
2179
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002180BOOST_AUTO_TEST_CASE(SendThreadBufferTest)
2181{
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002182 ProfilingStateMachine profilingStateMachine;
2183 SetActiveProfilingState(profilingStateMachine);
2184
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002185 MockProfilingConnection mockProfilingConnection;
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002186 BufferManager bufferManager(3, 1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002187 SendCounterPacket sendCounterPacket(bufferManager);
2188 SendThread sendThread(profilingStateMachine, bufferManager, sendCounterPacket, -1);
2189 sendThread.Start(mockProfilingConnection);
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002190
2191 // SendStreamMetaDataPacket
2192 sendCounterPacket.SendStreamMetaDataPacket();
2193
2194 // Read data from the buffer
2195 // Buffer should become readable after commit by SendStreamMetaDataPacket
2196 auto packetBuffer = bufferManager.GetReadableBuffer();
2197 BOOST_TEST(packetBuffer.get());
2198
2199 std::string processName = GetProcessName().substr(0, 60);
2200 unsigned int processNameSize = processName.empty() ? 0 : boost::numeric_cast<unsigned int>(processName.size()) + 1;
2201 unsigned int streamMetadataPacketsize = 118 + processNameSize;
2202 BOOST_TEST(packetBuffer->GetSize() == streamMetadataPacketsize);
2203
2204 // Recommit to be read by sendCounterPacket
2205 bufferManager.Commit(packetBuffer, streamMetadataPacketsize);
2206
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002207 // SendCounterDirectoryPacket
2208 CounterDirectory counterDirectory;
2209 sendCounterPacket.SendCounterDirectoryPacket(counterDirectory);
2210
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002211 // SendPeriodicCounterCapturePacket
2212 sendCounterPacket.SendPeriodicCounterCapturePacket(123u,
2213 {
2214 { 1u, 23u },
2215 { 33u, 1207623u }
2216 });
2217
Sadik Armagan3896b472020-02-10 12:24:15 +00002218 sendThread.Stop();
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002219
2220 // The buffer is read by the send thread so it should not be in the readable buffer.
2221 auto readBuffer = bufferManager.GetReadableBuffer();
2222 BOOST_TEST(!readBuffer);
2223
2224 // Successfully reserved the buffer with requested size
2225 unsigned int reservedSize = 0;
2226 auto reservedBuffer = bufferManager.Reserve(512, reservedSize);
2227 BOOST_TEST(reservedSize == 512);
2228 BOOST_TEST(reservedBuffer.get());
2229
Finn Williams09ad6f92019-12-19 17:05:18 +00002230 const auto writtenDataSize = mockProfilingConnection.GetWrittenDataSize();
2231 const auto metaDataPacketCount =
2232 mockProfilingConnection.CheckForPacket({PacketType::StreamMetaData, streamMetadataPacketsize});
2233
2234 BOOST_TEST(metaDataPacketCount >= 1);
2235 BOOST_TEST(mockProfilingConnection.CheckForPacket({PacketType::CounterDirectory, 32}) == 1);
2236 BOOST_TEST(mockProfilingConnection.CheckForPacket({PacketType::PeriodicCounterCapture, 28}) == 1);
2237 // Check that we only received the packets we expected
2238 BOOST_TEST(metaDataPacketCount + 2 == writtenDataSize);
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +01002239}
2240
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002241BOOST_AUTO_TEST_CASE(SendThreadSendStreamMetadataPacket1)
2242{
2243 ProfilingStateMachine profilingStateMachine;
2244
2245 MockProfilingConnection mockProfilingConnection;
2246 BufferManager bufferManager(3, 1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002247 SendCounterPacket sendCounterPacket(bufferManager);
2248 SendThread sendThread(profilingStateMachine, bufferManager, sendCounterPacket);
2249 sendThread.Start(mockProfilingConnection);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002250
2251 // The profiling state is set to "Uninitialized", so the send thread should throw an exception
Sadik Armagan3896b472020-02-10 12:24:15 +00002252 BOOST_CHECK_THROW(sendThread.Stop(), armnn::RuntimeException);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002253}
2254
2255BOOST_AUTO_TEST_CASE(SendThreadSendStreamMetadataPacket2)
2256{
2257 ProfilingStateMachine profilingStateMachine;
2258 SetNotConnectedProfilingState(profilingStateMachine);
2259
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 "NotConnected", so the send thread should throw an exception
Sadik Armagan3896b472020-02-10 12:24:15 +00002267 BOOST_CHECK_THROW(sendThread.Stop(), armnn::RuntimeException);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002268}
2269
2270BOOST_AUTO_TEST_CASE(SendThreadSendStreamMetadataPacket3)
2271{
2272 ProfilingStateMachine profilingStateMachine;
2273 SetWaitingForAckProfilingState(profilingStateMachine);
2274
2275 // Calculate the size of a Stream Metadata packet
2276 std::string processName = GetProcessName().substr(0, 60);
2277 unsigned int processNameSize = processName.empty() ? 0 : boost::numeric_cast<unsigned int>(processName.size()) + 1;
2278 unsigned int streamMetadataPacketsize = 118 + processNameSize;
2279
2280 MockProfilingConnection mockProfilingConnection;
2281 BufferManager bufferManager(3, 1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002282 SendCounterPacket sendCounterPacket(bufferManager);
2283 SendThread sendThread(profilingStateMachine, bufferManager, sendCounterPacket);
2284 sendThread.Start(mockProfilingConnection);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002285
2286 // The profiling state is set to "WaitingForAck", so the send thread should send a Stream Metadata packet
Sadik Armagan3896b472020-02-10 12:24:15 +00002287 // Wait for sendThread to join
2288 BOOST_CHECK_NO_THROW(sendThread.Stop());
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002289
Finn Williams09ad6f92019-12-19 17:05:18 +00002290 // Check that the buffer contains at least one Stream Metadata packet and no other packets
2291 const auto writtenDataSize = mockProfilingConnection.GetWrittenDataSize();
2292
2293 BOOST_TEST(writtenDataSize >= 1);
2294 BOOST_TEST(mockProfilingConnection.CheckForPacket(
2295 {PacketType::StreamMetaData, streamMetadataPacketsize}) == writtenDataSize);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002296}
2297
2298BOOST_AUTO_TEST_CASE(SendThreadSendStreamMetadataPacket4)
2299{
2300 ProfilingStateMachine profilingStateMachine;
2301 SetWaitingForAckProfilingState(profilingStateMachine);
2302
2303 // Calculate the size of a Stream Metadata packet
2304 std::string processName = GetProcessName().substr(0, 60);
2305 unsigned int processNameSize = processName.empty() ? 0 : boost::numeric_cast<unsigned int>(processName.size()) + 1;
2306 unsigned int streamMetadataPacketsize = 118 + processNameSize;
2307
2308 MockProfilingConnection mockProfilingConnection;
2309 BufferManager bufferManager(3, 1024);
Sadik Armagan3896b472020-02-10 12:24:15 +00002310 SendCounterPacket sendCounterPacket(bufferManager);
2311 SendThread sendThread(profilingStateMachine, bufferManager, sendCounterPacket);
2312 sendThread.Start(mockProfilingConnection);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002313
2314 // The profiling state is set to "WaitingForAck", so the send thread should send a Stream Metadata packet
Sadik Armagan3896b472020-02-10 12:24:15 +00002315 // Wait for sendThread to join
2316 sendThread.Stop();
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002317
Sadik Armagan3896b472020-02-10 12:24:15 +00002318 sendThread.Start(mockProfilingConnection);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002319 // Check that the profiling state is still "WaitingForAck"
2320 BOOST_TEST((profilingStateMachine.GetCurrentState() == ProfilingState::WaitingForAck));
2321
Finn Williams3e2969d2019-12-06 17:47:36 +00002322 // Check that the buffer contains at least one Stream Metadata packet
Finn Williams09ad6f92019-12-19 17:05:18 +00002323 BOOST_TEST(mockProfilingConnection.CheckForPacket({PacketType::StreamMetaData, streamMetadataPacketsize}) >= 1);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002324
2325 mockProfilingConnection.Clear();
2326
Sadik Armagan3896b472020-02-10 12:24:15 +00002327 sendThread.Stop();
2328 sendThread.Start(mockProfilingConnection);
Finn Williams09ad6f92019-12-19 17:05:18 +00002329
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002330 // Try triggering a new buffer read
Sadik Armagan3896b472020-02-10 12:24:15 +00002331 sendThread.SetReadyToRead();
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002332
Sadik Armagan3896b472020-02-10 12:24:15 +00002333 // Wait for sendThread to join
2334 BOOST_CHECK_NO_THROW(sendThread.Stop());
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002335
2336 // Check that the profiling state is still "WaitingForAck"
2337 BOOST_TEST((profilingStateMachine.GetCurrentState() == ProfilingState::WaitingForAck));
2338
Finn Williams09ad6f92019-12-19 17:05:18 +00002339 // Check that the buffer contains at least one Stream Metadata packet and no other packets
2340 const auto writtenDataSize = mockProfilingConnection.GetWrittenDataSize();
2341
2342 BOOST_TEST(writtenDataSize >= 1);
2343 BOOST_TEST(mockProfilingConnection.CheckForPacket(
2344 {PacketType::StreamMetaData, streamMetadataPacketsize}) == writtenDataSize);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01002345}
2346
Ferran Balagueraf5c46b2019-08-30 15:49:15 +01002347BOOST_AUTO_TEST_SUITE_END()